| 1 |
/* do not edit automatically generated by mc from M2RTS. */ |
| 2 |
/* M2RTS.mod Implements the run time system facilities of Modula-2. |
| 3 |
|
| 4 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
Under Section 7 of GPL version 3, you are granted additional |
| 20 |
permissions described in the GCC Runtime Library Exception, version |
| 21 |
3.1, as published by the Free Software Foundation. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License and |
| 24 |
a copy of the GCC Runtime Library Exception along with this program; |
| 25 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 26 |
<http://www.gnu.org/licenses/>. */ |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
#include "system.h" |
| 30 |
#include <stdbool.h> |
| 31 |
# if !defined (PROC_D) |
| 32 |
# define PROC_D |
| 33 |
typedef void (*PROC_t) (void); |
| 34 |
typedef struct { PROC_t proc; } PROC; |
| 35 |
# endif |
| 36 |
|
| 37 |
# if !defined (TRUE) |
| 38 |
# define TRUE (1==1) |
| 39 |
# endif |
| 40 |
|
| 41 |
# if !defined (FALSE) |
| 42 |
# define FALSE (1==0) |
| 43 |
# endif |
| 44 |
|
| 45 |
#define _M2RTS_C |
| 46 |
|
| 47 |
#include "GM2RTS.h" |
| 48 |
# include "Glibc.h" |
| 49 |
# include "GNumberIO.h" |
| 50 |
# include "GStrLib.h" |
| 51 |
# include "GSYSTEM.h" |
| 52 |
# include "GASCII.h" |
| 53 |
# include "GStorage.h" |
| 54 |
# include "GRTExceptions.h" |
| 55 |
# include "GM2EXCEPTION.h" |
| 56 |
# include "GM2Dependent.h" |
| 57 |
|
| 58 |
typedef struct M2RTS_ArgCVEnvP_p M2RTS_ArgCVEnvP; |
| 59 |
|
| 60 |
# define stderrFd 2 |
| 61 |
typedef char *M2RTS_PtrToChar; |
| 62 |
|
| 63 |
static int ExitValue; |
| 64 |
static bool isHalting; |
| 65 |
static bool CallExit; |
| 66 |
static bool Initialized; |
| 67 |
|
| 68 |
/* |
| 69 |
ConstructModules - resolve dependencies and then call each |
| 70 |
module constructor in turn. |
| 71 |
*/ |
| 72 |
|
| 73 |
extern "C" void M2RTS_ConstructModules (void * applicationmodule, void * libname, void * overrideliborder, int argc, void * argv, void * envp); |
| 74 |
|
| 75 |
/* |
| 76 |
DeconstructModules - resolve dependencies and then call each |
| 77 |
module constructor in turn. |
| 78 |
*/ |
| 79 |
|
| 80 |
extern "C" void M2RTS_DeconstructModules (void * applicationmodule, void * libname, int argc, void * argv, void * envp); |
| 81 |
|
| 82 |
/* |
| 83 |
RegisterModule - adds module name to the list of outstanding |
| 84 |
modules which need to have their dependencies |
| 85 |
explored to determine initialization order. |
| 86 |
*/ |
| 87 |
|
| 88 |
extern "C" void M2RTS_RegisterModule (void * name, void * libname, M2RTS_ArgCVEnvP init, M2RTS_ArgCVEnvP fini, PROC dependencies); |
| 89 |
|
| 90 |
/* |
| 91 |
RequestDependant - used to specify that modulename is dependant upon |
| 92 |
module dependantmodule. |
| 93 |
*/ |
| 94 |
|
| 95 |
extern "C" void M2RTS_RequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname); |
| 96 |
|
| 97 |
/* |
| 98 |
InstallTerminationProcedure - installs a procedure, p, which will |
| 99 |
be called when the procedure |
| 100 |
ExecuteTerminationProcedures |
| 101 |
is invoked. It returns TRUE if the |
| 102 |
procedure is installed. |
| 103 |
*/ |
| 104 |
|
| 105 |
extern "C" bool M2RTS_InstallTerminationProcedure (PROC p); |
| 106 |
|
| 107 |
/* |
| 108 |
ExecuteInitialProcedures - executes the initial procedures installed by |
| 109 |
InstallInitialProcedure. |
| 110 |
*/ |
| 111 |
|
| 112 |
extern "C" void M2RTS_ExecuteInitialProcedures (void); |
| 113 |
|
| 114 |
/* |
| 115 |
InstallInitialProcedure - installs a procedure to be executed just |
| 116 |
before the BEGIN code section of the |
| 117 |
main program module. |
| 118 |
*/ |
| 119 |
|
| 120 |
extern "C" bool M2RTS_InstallInitialProcedure (PROC p); |
| 121 |
|
| 122 |
/* |
| 123 |
ExecuteTerminationProcedures - calls each installed termination procedure |
| 124 |
in reverse order. |
| 125 |
*/ |
| 126 |
|
| 127 |
extern "C" void M2RTS_ExecuteTerminationProcedures (void); |
| 128 |
|
| 129 |
/* |
| 130 |
Terminate - provides compatibility for pim. It calls exit with |
| 131 |
the exitcode provided in a prior call to ExitOnHalt |
| 132 |
(or zero if ExitOnHalt was never called). It does |
| 133 |
not call ExecuteTerminationProcedures. |
| 134 |
*/ |
| 135 |
|
| 136 |
extern "C" void M2RTS_Terminate (void) __attribute__ ((noreturn)); |
| 137 |
|
| 138 |
/* |
| 139 |
HALT - terminate the current program. The procedure |
| 140 |
ExecuteTerminationProcedures |
| 141 |
is called before the program is stopped. The parameter |
| 142 |
exitcode is optional. If the parameter is not supplied |
| 143 |
HALT will call libc 'abort', otherwise it will exit with |
| 144 |
the code supplied. Supplying a parameter to HALT has the |
| 145 |
same effect as calling ExitOnHalt with the same code and |
| 146 |
then calling HALT with no parameter. |
| 147 |
*/ |
| 148 |
|
| 149 |
extern "C" void M2RTS_HALT (int exitcode) __attribute__ ((noreturn)); |
| 150 |
|
| 151 |
/* |
| 152 |
Halt - provides a more user friendly version of HALT, which takes |
| 153 |
four parameters to aid debugging. It writes an error message |
| 154 |
to stderr and calls exit (1). |
| 155 |
*/ |
| 156 |
|
| 157 |
extern "C" void M2RTS_Halt (const char *description_, unsigned int _description_high, const char *filename_, unsigned int _filename_high, const char *function_, unsigned int _function_high, unsigned int line) __attribute__ ((noreturn)); |
| 158 |
|
| 159 |
/* |
| 160 |
HaltC - provides a more user friendly version of HALT, which takes |
| 161 |
four parameters to aid debugging. It writes an error message |
| 162 |
to stderr and calls exit (1). |
| 163 |
*/ |
| 164 |
|
| 165 |
extern "C" void M2RTS_HaltC (void * description, void * filename, void * function, unsigned int line) __attribute__ ((noreturn)); |
| 166 |
|
| 167 |
/* |
| 168 |
ExitOnHalt - if HALT is executed then call exit with the exit code, e. |
| 169 |
*/ |
| 170 |
|
| 171 |
extern "C" void M2RTS_ExitOnHalt (int e); |
| 172 |
|
| 173 |
/* |
| 174 |
ErrorMessage - emits an error message to stderr and then calls exit (1). |
| 175 |
*/ |
| 176 |
|
| 177 |
extern "C" void M2RTS_ErrorMessage (const char *message_, unsigned int _message_high, const char *filename_, unsigned int _filename_high, unsigned int line, const char *function_, unsigned int _function_high) __attribute__ ((noreturn)); |
| 178 |
|
| 179 |
/* |
| 180 |
Length - returns the length of a string, a. This is called whenever |
| 181 |
the user calls LENGTH and the parameter cannot be calculated |
| 182 |
at compile time. |
| 183 |
*/ |
| 184 |
|
| 185 |
extern "C" unsigned int M2RTS_Length (const char *a_, unsigned int _a_high); |
| 186 |
extern "C" void M2RTS_AssignmentException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 187 |
extern "C" void M2RTS_ReturnException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 188 |
extern "C" void M2RTS_IncException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 189 |
extern "C" void M2RTS_DecException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 190 |
extern "C" void M2RTS_InclException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 191 |
extern "C" void M2RTS_ExclException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 192 |
extern "C" void M2RTS_ShiftException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 193 |
extern "C" void M2RTS_RotateException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 194 |
extern "C" void M2RTS_StaticArraySubscriptException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 195 |
extern "C" void M2RTS_DynamicArraySubscriptException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 196 |
extern "C" void M2RTS_ForLoopBeginException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 197 |
extern "C" void M2RTS_ForLoopToException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 198 |
extern "C" void M2RTS_ForLoopEndException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 199 |
extern "C" void M2RTS_PointerNilException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 200 |
extern "C" void M2RTS_NoReturnException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 201 |
extern "C" void M2RTS_CaseException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 202 |
extern "C" void M2RTS_WholeNonPosDivException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 203 |
extern "C" void M2RTS_WholeNonPosModException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 204 |
extern "C" void M2RTS_WholeZeroDivException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 205 |
extern "C" void M2RTS_WholeZeroRemException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 206 |
extern "C" void M2RTS_WholeValueException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 207 |
extern "C" void M2RTS_RealValueException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 208 |
extern "C" void M2RTS_ParameterException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 209 |
extern "C" void M2RTS_NoException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) __attribute__ ((noreturn)); |
| 210 |
|
| 211 |
/* |
| 212 |
ErrorString - writes a string to stderr. |
| 213 |
*/ |
| 214 |
|
| 215 |
static void ErrorString (const char *a_, unsigned int _a_high); |
| 216 |
|
| 217 |
/* |
| 218 |
ErrorStringC - writes a string to stderr. |
| 219 |
*/ |
| 220 |
|
| 221 |
static void ErrorStringC (void * str); |
| 222 |
|
| 223 |
/* |
| 224 |
ErrorMessageC - emits an error message to stderr and then calls exit (1). |
| 225 |
*/ |
| 226 |
|
| 227 |
static void ErrorMessageC (void * message, void * filename, unsigned int line, void * function) __attribute__ ((noreturn)); |
| 228 |
|
| 229 |
/* |
| 230 |
Init - initialize the initial, terminate procedure lists and booleans. |
| 231 |
*/ |
| 232 |
|
| 233 |
static void Init (void); |
| 234 |
|
| 235 |
/* |
| 236 |
CheckInitialized - checks to see if this module has been initialized |
| 237 |
and if it has not it calls Init. We need this |
| 238 |
approach as this module is called by module ctors |
| 239 |
before we reach main. |
| 240 |
*/ |
| 241 |
|
| 242 |
static void CheckInitialized (void); |
| 243 |
|
| 244 |
|
| 245 |
/* |
| 246 |
ErrorString - writes a string to stderr. |
| 247 |
*/ |
| 248 |
|
| 249 |
static void ErrorString (const char *a_, unsigned int _a_high) |
| 250 |
{ |
| 251 |
int n; |
| 252 |
char a[_a_high+1]; |
| 253 |
|
| 254 |
/* make a local copy of each unbounded array. */ |
| 255 |
memcpy (a, a_, _a_high+1); |
| 256 |
|
| 257 |
n = static_cast<int> (libc_write (stderrFd, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (StrLib_StrLen ((const char *) a, _a_high)))); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
/* |
| 262 |
ErrorStringC - writes a string to stderr. |
| 263 |
*/ |
| 264 |
|
| 265 |
static void ErrorStringC (void * str) |
| 266 |
{ |
| 267 |
int len; |
| 268 |
|
| 269 |
len = static_cast<int> (libc_write (stderrFd, str, libc_strlen (str))); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/* |
| 274 |
ErrorMessageC - emits an error message to stderr and then calls exit (1). |
| 275 |
*/ |
| 276 |
|
| 277 |
static void ErrorMessageC (void * message, void * filename, unsigned int line, void * function) |
| 278 |
{ |
| 279 |
typedef struct ErrorMessageC__T1_a ErrorMessageC__T1; |
| 280 |
|
| 281 |
struct ErrorMessageC__T1_a { char array[10+1]; }; |
| 282 |
ErrorMessageC__T1 buffer; |
| 283 |
|
| 284 |
ErrorStringC (filename); |
| 285 |
ErrorString ((const char *) ":", 1); |
| 286 |
NumberIO_CardToStr (line, 0, (char *) &buffer.array[0], 10); |
| 287 |
ErrorString ((const char *) &buffer.array[0], 10); |
| 288 |
ErrorString ((const char *) ":", 1); |
| 289 |
if ((libc_strlen (function)) > 0) |
| 290 |
{ |
| 291 |
ErrorString ((const char *) "in ", 3); |
| 292 |
ErrorStringC (function); |
| 293 |
ErrorString ((const char *) " has caused ", 12); |
| 294 |
} |
| 295 |
ErrorStringC (message); |
| 296 |
buffer.array[0] = ASCII_nl; |
| 297 |
buffer.array[1] = ASCII_nul; |
| 298 |
ErrorString ((const char *) &buffer.array[0], 10); |
| 299 |
libc_exit (1); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
/* |
| 304 |
Init - initialize the initial, terminate procedure lists and booleans. |
| 305 |
*/ |
| 306 |
|
| 307 |
static void Init (void) |
| 308 |
{ |
| 309 |
ExitValue = 0; |
| 310 |
isHalting = false; |
| 311 |
CallExit = false; /* default by calling abort */ |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
/* |
| 316 |
CheckInitialized - checks to see if this module has been initialized |
| 317 |
and if it has not it calls Init. We need this |
| 318 |
approach as this module is called by module ctors |
| 319 |
before we reach main. |
| 320 |
*/ |
| 321 |
|
| 322 |
static void CheckInitialized (void) |
| 323 |
{ |
| 324 |
if (! Initialized) |
| 325 |
{ |
| 326 |
Initialized = true; |
| 327 |
Init (); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
/* |
| 333 |
ConstructModules - resolve dependencies and then call each |
| 334 |
module constructor in turn. |
| 335 |
*/ |
| 336 |
|
| 337 |
extern "C" void M2RTS_ConstructModules (void * applicationmodule, void * libname, void * overrideliborder, int argc, void * argv, void * envp) |
| 338 |
{ |
| 339 |
M2Dependent_ConstructModules (applicationmodule, libname, overrideliborder, argc, argv, envp); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/* |
| 344 |
DeconstructModules - resolve dependencies and then call each |
| 345 |
module constructor in turn. |
| 346 |
*/ |
| 347 |
|
| 348 |
extern "C" void M2RTS_DeconstructModules (void * applicationmodule, void * libname, int argc, void * argv, void * envp) |
| 349 |
{ |
| 350 |
M2Dependent_DeconstructModules (applicationmodule, libname, argc, argv, envp); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/* |
| 355 |
RegisterModule - adds module name to the list of outstanding |
| 356 |
modules which need to have their dependencies |
| 357 |
explored to determine initialization order. |
| 358 |
*/ |
| 359 |
|
| 360 |
extern "C" void M2RTS_RegisterModule (void * name, void * libname, M2RTS_ArgCVEnvP init, M2RTS_ArgCVEnvP fini, PROC dependencies) |
| 361 |
{ |
| 362 |
M2Dependent_RegisterModule (name, libname, (M2Dependent_ArgCVEnvP) {(M2Dependent_ArgCVEnvP_t) init.proc}, (M2Dependent_ArgCVEnvP) {(M2Dependent_ArgCVEnvP_t) fini.proc}, dependencies); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
/* |
| 367 |
RequestDependant - used to specify that modulename is dependant upon |
| 368 |
module dependantmodule. |
| 369 |
*/ |
| 370 |
|
| 371 |
extern "C" void M2RTS_RequestDependant (void * modulename, void * libname, void * dependantmodule, void * dependantlibname) |
| 372 |
{ |
| 373 |
M2Dependent_RequestDependant (modulename, libname, dependantmodule, dependantlibname); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/* |
| 378 |
InstallTerminationProcedure - installs a procedure, p, which will |
| 379 |
be called when the procedure |
| 380 |
ExecuteTerminationProcedures |
| 381 |
is invoked. It returns TRUE if the |
| 382 |
procedure is installed. |
| 383 |
*/ |
| 384 |
|
| 385 |
extern "C" bool M2RTS_InstallTerminationProcedure (PROC p) |
| 386 |
{ |
| 387 |
return M2Dependent_InstallTerminationProcedure (p); |
| 388 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 389 |
__builtin_unreachable (); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
/* |
| 394 |
ExecuteInitialProcedures - executes the initial procedures installed by |
| 395 |
InstallInitialProcedure. |
| 396 |
*/ |
| 397 |
|
| 398 |
extern "C" void M2RTS_ExecuteInitialProcedures (void) |
| 399 |
{ |
| 400 |
M2Dependent_ExecuteInitialProcedures (); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/* |
| 405 |
InstallInitialProcedure - installs a procedure to be executed just |
| 406 |
before the BEGIN code section of the |
| 407 |
main program module. |
| 408 |
*/ |
| 409 |
|
| 410 |
extern "C" bool M2RTS_InstallInitialProcedure (PROC p) |
| 411 |
{ |
| 412 |
return M2Dependent_InstallInitialProcedure (p); |
| 413 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 414 |
__builtin_unreachable (); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/* |
| 419 |
ExecuteTerminationProcedures - calls each installed termination procedure |
| 420 |
in reverse order. |
| 421 |
*/ |
| 422 |
|
| 423 |
extern "C" void M2RTS_ExecuteTerminationProcedures (void) |
| 424 |
{ |
| 425 |
M2Dependent_ExecuteTerminationProcedures (); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
/* |
| 430 |
Terminate - provides compatibility for pim. It calls exit with |
| 431 |
the exitcode provided in a prior call to ExitOnHalt |
| 432 |
(or zero if ExitOnHalt was never called). It does |
| 433 |
not call ExecuteTerminationProcedures. |
| 434 |
*/ |
| 435 |
|
| 436 |
extern "C" void M2RTS_Terminate (void) |
| 437 |
{ |
| 438 |
libc_exit (ExitValue); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
/* |
| 443 |
HALT - terminate the current program. The procedure |
| 444 |
ExecuteTerminationProcedures |
| 445 |
is called before the program is stopped. The parameter |
| 446 |
exitcode is optional. If the parameter is not supplied |
| 447 |
HALT will call libc 'abort', otherwise it will exit with |
| 448 |
the code supplied. Supplying a parameter to HALT has the |
| 449 |
same effect as calling ExitOnHalt with the same code and |
| 450 |
then calling HALT with no parameter. |
| 451 |
*/ |
| 452 |
|
| 453 |
extern "C" void M2RTS_HALT (int exitcode) |
| 454 |
{ |
| 455 |
if (exitcode != -1) |
| 456 |
{ |
| 457 |
CallExit = true; |
| 458 |
ExitValue = exitcode; |
| 459 |
} |
| 460 |
if (isHalting) |
| 461 |
{ |
| 462 |
/* double HALT found */ |
| 463 |
libc_exit (-1); |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
isHalting = true; |
| 468 |
M2RTS_ExecuteTerminationProcedures (); |
| 469 |
} |
| 470 |
if (CallExit) |
| 471 |
{ |
| 472 |
libc_exit (ExitValue); |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
libc_abort (); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
/* |
| 482 |
Halt - provides a more user friendly version of HALT, which takes |
| 483 |
four parameters to aid debugging. It writes an error message |
| 484 |
to stderr and calls exit (1). |
| 485 |
*/ |
| 486 |
|
| 487 |
extern "C" void M2RTS_Halt (const char *description_, unsigned int _description_high, const char *filename_, unsigned int _filename_high, const char *function_, unsigned int _function_high, unsigned int line) |
| 488 |
{ |
| 489 |
char description[_description_high+1]; |
| 490 |
char filename[_filename_high+1]; |
| 491 |
char function[_function_high+1]; |
| 492 |
|
| 493 |
/* make a local copy of each unbounded array. */ |
| 494 |
memcpy (description, description_, _description_high+1); |
| 495 |
memcpy (filename, filename_, _filename_high+1); |
| 496 |
memcpy (function, function_, _function_high+1); |
| 497 |
|
| 498 |
M2RTS_ErrorMessage ((const char *) description, _description_high, (const char *) filename, _filename_high, line, (const char *) function, _function_high); |
| 499 |
libc_exit (1); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
/* |
| 504 |
HaltC - provides a more user friendly version of HALT, which takes |
| 505 |
four parameters to aid debugging. It writes an error message |
| 506 |
to stderr and calls exit (1). |
| 507 |
*/ |
| 508 |
|
| 509 |
extern "C" void M2RTS_HaltC (void * description, void * filename, void * function, unsigned int line) |
| 510 |
{ |
| 511 |
ErrorMessageC (description, filename, line, function); |
| 512 |
libc_exit (1); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
/* |
| 517 |
ExitOnHalt - if HALT is executed then call exit with the exit code, e. |
| 518 |
*/ |
| 519 |
|
| 520 |
extern "C" void M2RTS_ExitOnHalt (int e) |
| 521 |
{ |
| 522 |
ExitValue = e; |
| 523 |
CallExit = true; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
/* |
| 528 |
ErrorMessage - emits an error message to stderr and then calls exit (1). |
| 529 |
*/ |
| 530 |
|
| 531 |
extern "C" void M2RTS_ErrorMessage (const char *message_, unsigned int _message_high, const char *filename_, unsigned int _filename_high, unsigned int line, const char *function_, unsigned int _function_high) |
| 532 |
{ |
| 533 |
typedef struct ErrorMessage__T2_a ErrorMessage__T2; |
| 534 |
|
| 535 |
struct ErrorMessage__T2_a { char array[10+1]; }; |
| 536 |
ErrorMessage__T2 buffer; |
| 537 |
char message[_message_high+1]; |
| 538 |
char filename[_filename_high+1]; |
| 539 |
char function[_function_high+1]; |
| 540 |
|
| 541 |
/* make a local copy of each unbounded array. */ |
| 542 |
memcpy (message, message_, _message_high+1); |
| 543 |
memcpy (filename, filename_, _filename_high+1); |
| 544 |
memcpy (function, function_, _function_high+1); |
| 545 |
|
| 546 |
ErrorString ((const char *) filename, _filename_high); |
| 547 |
ErrorString ((const char *) ":", 1); |
| 548 |
NumberIO_CardToStr (line, 0, (char *) &buffer.array[0], 10); |
| 549 |
ErrorString ((const char *) &buffer.array[0], 10); |
| 550 |
ErrorString ((const char *) ":", 1); |
| 551 |
if (! (StrLib_StrEqual ((const char *) function, _function_high, (const char *) "", 0))) |
| 552 |
{ |
| 553 |
ErrorString ((const char *) "in ", 3); |
| 554 |
ErrorString ((const char *) function, _function_high); |
| 555 |
ErrorString ((const char *) " has caused ", 12); |
| 556 |
} |
| 557 |
ErrorString ((const char *) message, _message_high); |
| 558 |
buffer.array[0] = ASCII_nl; |
| 559 |
buffer.array[1] = ASCII_nul; |
| 560 |
ErrorString ((const char *) &buffer.array[0], 10); |
| 561 |
libc_exit (1); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
/* |
| 566 |
Length - returns the length of a string, a. This is called whenever |
| 567 |
the user calls LENGTH and the parameter cannot be calculated |
| 568 |
at compile time. |
| 569 |
*/ |
| 570 |
|
| 571 |
extern "C" unsigned int M2RTS_Length (const char *a_, unsigned int _a_high) |
| 572 |
{ |
| 573 |
unsigned int l; |
| 574 |
unsigned int h; |
| 575 |
char a[_a_high+1]; |
| 576 |
|
| 577 |
/* make a local copy of each unbounded array. */ |
| 578 |
memcpy (a, a_, _a_high+1); |
| 579 |
|
| 580 |
l = 0; |
| 581 |
h = _a_high; |
| 582 |
while ((l <= h) && (a[l] != ASCII_nul)) |
| 583 |
{ |
| 584 |
l += 1; |
| 585 |
} |
| 586 |
return l; |
| 587 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 588 |
__builtin_unreachable (); |
| 589 |
} |
| 590 |
|
| 591 |
extern "C" void M2RTS_AssignmentException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 592 |
{ |
| 593 |
/* |
| 594 |
The following are the runtime exception handler routines. |
| 595 |
*/ |
| 596 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 597 |
} |
| 598 |
|
| 599 |
extern "C" void M2RTS_ReturnException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 600 |
{ |
| 601 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 602 |
} |
| 603 |
|
| 604 |
extern "C" void M2RTS_IncException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 605 |
{ |
| 606 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 607 |
} |
| 608 |
|
| 609 |
extern "C" void M2RTS_DecException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 610 |
{ |
| 611 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 612 |
} |
| 613 |
|
| 614 |
extern "C" void M2RTS_InclException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 615 |
{ |
| 616 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 617 |
} |
| 618 |
|
| 619 |
extern "C" void M2RTS_ExclException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 620 |
{ |
| 621 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 622 |
} |
| 623 |
|
| 624 |
extern "C" void M2RTS_ShiftException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 625 |
{ |
| 626 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 627 |
} |
| 628 |
|
| 629 |
extern "C" void M2RTS_RotateException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 630 |
{ |
| 631 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 632 |
} |
| 633 |
|
| 634 |
extern "C" void M2RTS_StaticArraySubscriptException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 635 |
{ |
| 636 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_indexException)), filename, line, column, scope, message); |
| 637 |
} |
| 638 |
|
| 639 |
extern "C" void M2RTS_DynamicArraySubscriptException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 640 |
{ |
| 641 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_indexException)), filename, line, column, scope, message); |
| 642 |
} |
| 643 |
|
| 644 |
extern "C" void M2RTS_ForLoopBeginException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 645 |
{ |
| 646 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 647 |
} |
| 648 |
|
| 649 |
extern "C" void M2RTS_ForLoopToException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 650 |
{ |
| 651 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 652 |
} |
| 653 |
|
| 654 |
extern "C" void M2RTS_ForLoopEndException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 655 |
{ |
| 656 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 657 |
} |
| 658 |
|
| 659 |
extern "C" void M2RTS_PointerNilException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 660 |
{ |
| 661 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_invalidLocation)), filename, line, column, scope, message); |
| 662 |
} |
| 663 |
|
| 664 |
extern "C" void M2RTS_NoReturnException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 665 |
{ |
| 666 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_functionException)), filename, line, column, scope, message); |
| 667 |
} |
| 668 |
|
| 669 |
extern "C" void M2RTS_CaseException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 670 |
{ |
| 671 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_caseSelectException)), filename, line, column, scope, message); |
| 672 |
} |
| 673 |
|
| 674 |
extern "C" void M2RTS_WholeNonPosDivException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 675 |
{ |
| 676 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeDivException)), filename, line, column, scope, message); |
| 677 |
} |
| 678 |
|
| 679 |
extern "C" void M2RTS_WholeNonPosModException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 680 |
{ |
| 681 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeDivException)), filename, line, column, scope, message); |
| 682 |
} |
| 683 |
|
| 684 |
extern "C" void M2RTS_WholeZeroDivException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 685 |
{ |
| 686 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeDivException)), filename, line, column, scope, message); |
| 687 |
} |
| 688 |
|
| 689 |
extern "C" void M2RTS_WholeZeroRemException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 690 |
{ |
| 691 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeDivException)), filename, line, column, scope, message); |
| 692 |
} |
| 693 |
|
| 694 |
extern "C" void M2RTS_WholeValueException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 695 |
{ |
| 696 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_wholeValueException)), filename, line, column, scope, message); |
| 697 |
} |
| 698 |
|
| 699 |
extern "C" void M2RTS_RealValueException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 700 |
{ |
| 701 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_realValueException)), filename, line, column, scope, message); |
| 702 |
} |
| 703 |
|
| 704 |
extern "C" void M2RTS_ParameterException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 705 |
{ |
| 706 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_rangeException)), filename, line, column, scope, message); |
| 707 |
} |
| 708 |
|
| 709 |
extern "C" void M2RTS_NoException (void * filename, unsigned int line, unsigned int column, void * scope, void * message) |
| 710 |
{ |
| 711 |
RTExceptions_Raise ( ((unsigned int) (M2EXCEPTION_exException)), filename, line, column, scope, message); |
| 712 |
} |
| 713 |
|
| 714 |
extern "C" void _M2_M2RTS_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 715 |
{ |
| 716 |
CheckInitialized (); |
| 717 |
} |
| 718 |
|
| 719 |
extern "C" void _M2_M2RTS_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 720 |
{ |
| 721 |
} |