| 1 |
(* keyc maintains the C name scope and avoids C/C++ name conflicts. |
| 2 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Modula-2. |
| 5 |
|
| 6 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. *) |
| 19 |
|
| 20 |
IMPLEMENTATION MODULE keyc ; |
| 21 |
|
| 22 |
FROM mcPretty IMPORT pretty, print, prints, setNeedSpace, noSpace ; |
| 23 |
FROM Storage IMPORT ALLOCATE ; |
| 24 |
FROM DynamicStrings IMPORT InitString, KillString, ConCat, ConCatChar, |
| 25 |
Mark, string, InitStringCharStar ; |
| 26 |
FROM symbolKey IMPORT symbolTree, getSymKey, putSymKey, initTree, killTree ; |
| 27 |
FROM nameKey IMPORT makeKey, makekey, keyToCharStar ; |
| 28 |
FROM mcOptions IMPORT getHPrefix, getGccConfigSystem, useBool ; |
| 29 |
|
| 30 |
|
| 31 |
TYPE |
| 32 |
scope = POINTER TO RECORD |
| 33 |
scoped : node ; |
| 34 |
symbols: symbolTree ; |
| 35 |
next : scope ; |
| 36 |
END ; |
| 37 |
|
| 38 |
VAR |
| 39 |
stack, |
| 40 |
freeList : scope ; |
| 41 |
keywords, |
| 42 |
macros : symbolTree ; |
| 43 |
|
| 44 |
initializedCP, |
| 45 |
initializedGCC, |
| 46 |
|
| 47 |
seenGccTree, |
| 48 |
seenGccLocation, |
| 49 |
seenIntMin, |
| 50 |
seenUIntMin, |
| 51 |
seenLongMin, |
| 52 |
seenULongMin, |
| 53 |
seenCharMin, |
| 54 |
seenUCharMin, |
| 55 |
seenIntMax, |
| 56 |
seenUIntMax, |
| 57 |
seenLongMax, |
| 58 |
seenULongMax, |
| 59 |
seenCharMax, |
| 60 |
seenUCharMax, |
| 61 |
seenLabs, |
| 62 |
seenAbs, |
| 63 |
seenFabs, |
| 64 |
seenFabsl, |
| 65 |
seenSize_t, |
| 66 |
seenSSize_t, |
| 67 |
|
| 68 |
seenUnistd, |
| 69 |
seenSysTypes, |
| 70 |
seenThrow, |
| 71 |
seenFree, |
| 72 |
seenMalloc, |
| 73 |
seenStorage, |
| 74 |
seenProc, |
| 75 |
seenTrue, |
| 76 |
seenFalse, |
| 77 |
seenNull, |
| 78 |
seenMemcpy, |
| 79 |
seenException, |
| 80 |
seenComplex, |
| 81 |
seenM2RTS, |
| 82 |
seenStrlen, |
| 83 |
seenCtype : BOOLEAN ; |
| 84 |
|
| 85 |
|
| 86 |
(* |
| 87 |
checkGccConfigSystem - issues the GCC include config.h, include system.h |
| 88 |
instead of the standard host include. |
| 89 |
*) |
| 90 |
|
| 91 |
PROCEDURE checkGccConfigSystem (p: pretty) ; |
| 92 |
BEGIN |
| 93 |
IF getGccConfigSystem () |
| 94 |
THEN |
| 95 |
IF NOT initializedGCC |
| 96 |
THEN |
| 97 |
initializedGCC := TRUE ; |
| 98 |
print (p, '#include "config.h"\n'); |
| 99 |
print (p, '#include "system.h"\n'); |
| 100 |
checkGccTypes (p) |
| 101 |
END |
| 102 |
END |
| 103 |
END checkGccConfigSystem ; |
| 104 |
|
| 105 |
|
| 106 |
(* |
| 107 |
useGccTree - indicate we have imported tree from gcctypes. |
| 108 |
*) |
| 109 |
|
| 110 |
PROCEDURE useGccTree ; |
| 111 |
BEGIN |
| 112 |
seenGccTree := TRUE |
| 113 |
END useGccTree ; |
| 114 |
|
| 115 |
|
| 116 |
(* |
| 117 |
useGccLocation - indicate we have imported tree from gcctypes. |
| 118 |
*) |
| 119 |
|
| 120 |
PROCEDURE useGccLocation ; |
| 121 |
BEGIN |
| 122 |
seenGccLocation := TRUE |
| 123 |
END useGccLocation ; |
| 124 |
|
| 125 |
|
| 126 |
(* |
| 127 |
useStorage - indicate we have used storage. |
| 128 |
*) |
| 129 |
|
| 130 |
PROCEDURE useStorage ; |
| 131 |
BEGIN |
| 132 |
seenStorage := TRUE |
| 133 |
END useStorage ; |
| 134 |
|
| 135 |
|
| 136 |
(* |
| 137 |
useFree - indicate we have used free. |
| 138 |
*) |
| 139 |
|
| 140 |
PROCEDURE useFree ; |
| 141 |
BEGIN |
| 142 |
seenFree := TRUE |
| 143 |
END useFree ; |
| 144 |
|
| 145 |
|
| 146 |
(* |
| 147 |
useMalloc - indicate we have used malloc. |
| 148 |
*) |
| 149 |
|
| 150 |
PROCEDURE useMalloc ; |
| 151 |
BEGIN |
| 152 |
seenMalloc := TRUE |
| 153 |
END useMalloc ; |
| 154 |
|
| 155 |
|
| 156 |
(* |
| 157 |
useProc - indicate we have used proc. |
| 158 |
*) |
| 159 |
|
| 160 |
PROCEDURE useProc ; |
| 161 |
BEGIN |
| 162 |
seenProc := TRUE |
| 163 |
END useProc ; |
| 164 |
|
| 165 |
|
| 166 |
(* |
| 167 |
useTrue - indicate we have used TRUE. |
| 168 |
*) |
| 169 |
|
| 170 |
PROCEDURE useTrue ; |
| 171 |
BEGIN |
| 172 |
seenTrue := TRUE |
| 173 |
END useTrue ; |
| 174 |
|
| 175 |
|
| 176 |
(* |
| 177 |
useFalse - indicate we have used FALSE. |
| 178 |
*) |
| 179 |
|
| 180 |
PROCEDURE useFalse ; |
| 181 |
BEGIN |
| 182 |
seenFalse := TRUE |
| 183 |
END useFalse ; |
| 184 |
|
| 185 |
|
| 186 |
(* |
| 187 |
useNull - indicate we have used NULL. |
| 188 |
*) |
| 189 |
|
| 190 |
PROCEDURE useNull ; |
| 191 |
BEGIN |
| 192 |
seenNull := TRUE |
| 193 |
END useNull ; |
| 194 |
|
| 195 |
|
| 196 |
(* |
| 197 |
useMemcpy - indicate we have used memcpy. |
| 198 |
*) |
| 199 |
|
| 200 |
PROCEDURE useMemcpy ; |
| 201 |
BEGIN |
| 202 |
seenMemcpy := TRUE |
| 203 |
END useMemcpy ; |
| 204 |
|
| 205 |
|
| 206 |
(* |
| 207 |
useIntMin - indicate we have used INT_MIN. |
| 208 |
*) |
| 209 |
|
| 210 |
PROCEDURE useIntMin ; |
| 211 |
BEGIN |
| 212 |
seenIntMin := TRUE |
| 213 |
END useIntMin ; |
| 214 |
|
| 215 |
|
| 216 |
(* |
| 217 |
useUIntMin - indicate we have used UINT_MIN. |
| 218 |
*) |
| 219 |
|
| 220 |
PROCEDURE useUIntMin ; |
| 221 |
BEGIN |
| 222 |
seenUIntMin := TRUE |
| 223 |
END useUIntMin ; |
| 224 |
|
| 225 |
|
| 226 |
(* |
| 227 |
useLongMin - indicate we have used LONG_MIN. |
| 228 |
*) |
| 229 |
|
| 230 |
PROCEDURE useLongMin ; |
| 231 |
BEGIN |
| 232 |
seenLongMin := TRUE |
| 233 |
END useLongMin ; |
| 234 |
|
| 235 |
|
| 236 |
(* |
| 237 |
useULongMin - indicate we have used ULONG_MIN. |
| 238 |
*) |
| 239 |
|
| 240 |
PROCEDURE useULongMin ; |
| 241 |
BEGIN |
| 242 |
seenULongMin := TRUE |
| 243 |
END useULongMin ; |
| 244 |
|
| 245 |
|
| 246 |
(* |
| 247 |
useCharMin - indicate we have used CHAR_MIN. |
| 248 |
*) |
| 249 |
|
| 250 |
PROCEDURE useCharMin ; |
| 251 |
BEGIN |
| 252 |
seenCharMin := TRUE |
| 253 |
END useCharMin ; |
| 254 |
|
| 255 |
|
| 256 |
(* |
| 257 |
useUCharMin - indicate we have used UCHAR_MIN. |
| 258 |
*) |
| 259 |
|
| 260 |
PROCEDURE useUCharMin ; |
| 261 |
BEGIN |
| 262 |
seenUCharMin := TRUE |
| 263 |
END useUCharMin ; |
| 264 |
|
| 265 |
|
| 266 |
(* |
| 267 |
useIntMax - indicate we have used INT_MAX. |
| 268 |
*) |
| 269 |
|
| 270 |
PROCEDURE useIntMax ; |
| 271 |
BEGIN |
| 272 |
seenIntMax := TRUE |
| 273 |
END useIntMax ; |
| 274 |
|
| 275 |
|
| 276 |
(* |
| 277 |
useUIntMax - indicate we have used UINT_MAX. |
| 278 |
*) |
| 279 |
|
| 280 |
PROCEDURE useUIntMax ; |
| 281 |
BEGIN |
| 282 |
seenUIntMax := TRUE |
| 283 |
END useUIntMax ; |
| 284 |
|
| 285 |
|
| 286 |
(* |
| 287 |
useLongMax - indicate we have used LONG_MAX. |
| 288 |
*) |
| 289 |
|
| 290 |
PROCEDURE useLongMax ; |
| 291 |
BEGIN |
| 292 |
seenLongMax := TRUE |
| 293 |
END useLongMax ; |
| 294 |
|
| 295 |
|
| 296 |
(* |
| 297 |
useULongMax - indicate we have used ULONG_MAX. |
| 298 |
*) |
| 299 |
|
| 300 |
PROCEDURE useULongMax ; |
| 301 |
BEGIN |
| 302 |
seenULongMax := TRUE |
| 303 |
END useULongMax ; |
| 304 |
|
| 305 |
|
| 306 |
(* |
| 307 |
useCharMax - indicate we have used CHAR_MAX. |
| 308 |
*) |
| 309 |
|
| 310 |
PROCEDURE useCharMax ; |
| 311 |
BEGIN |
| 312 |
seenCharMax := TRUE |
| 313 |
END useCharMax ; |
| 314 |
|
| 315 |
|
| 316 |
(* |
| 317 |
useUCharMax - indicate we have used UChar_MAX. |
| 318 |
*) |
| 319 |
|
| 320 |
PROCEDURE useUCharMax ; |
| 321 |
BEGIN |
| 322 |
seenUCharMax := TRUE |
| 323 |
END useUCharMax ; |
| 324 |
|
| 325 |
|
| 326 |
(* |
| 327 |
useSize_t - indicate we have used size_t. |
| 328 |
*) |
| 329 |
|
| 330 |
PROCEDURE useSize_t ; |
| 331 |
BEGIN |
| 332 |
seenSize_t := TRUE |
| 333 |
END useSize_t ; |
| 334 |
|
| 335 |
|
| 336 |
(* |
| 337 |
useSSize_t - indicate we have used ssize_t. |
| 338 |
*) |
| 339 |
|
| 340 |
PROCEDURE useSSize_t ; |
| 341 |
BEGIN |
| 342 |
seenSSize_t := TRUE ; |
| 343 |
seenSysTypes := TRUE |
| 344 |
END useSSize_t ; |
| 345 |
|
| 346 |
|
| 347 |
(* |
| 348 |
useLabs - indicate we have used labs. |
| 349 |
*) |
| 350 |
|
| 351 |
PROCEDURE useLabs ; |
| 352 |
BEGIN |
| 353 |
seenLabs := TRUE |
| 354 |
END useLabs ; |
| 355 |
|
| 356 |
|
| 357 |
(* |
| 358 |
useAbs - indicate we have used abs. |
| 359 |
*) |
| 360 |
|
| 361 |
PROCEDURE useAbs ; |
| 362 |
BEGIN |
| 363 |
seenAbs := TRUE |
| 364 |
END useAbs ; |
| 365 |
|
| 366 |
|
| 367 |
(* |
| 368 |
useFabs - indicate we have used fabs. |
| 369 |
*) |
| 370 |
|
| 371 |
PROCEDURE useFabs ; |
| 372 |
BEGIN |
| 373 |
seenFabs := TRUE |
| 374 |
END useFabs ; |
| 375 |
|
| 376 |
|
| 377 |
(* |
| 378 |
useFabsl - indicate we have used fabsl. |
| 379 |
*) |
| 380 |
|
| 381 |
PROCEDURE useFabsl ; |
| 382 |
BEGIN |
| 383 |
seenFabsl := TRUE |
| 384 |
END useFabsl ; |
| 385 |
|
| 386 |
|
| 387 |
(* |
| 388 |
useM2RTS - indicate we have used M2RTS in the converted code. |
| 389 |
*) |
| 390 |
|
| 391 |
PROCEDURE useM2RTS ; |
| 392 |
BEGIN |
| 393 |
seenM2RTS := TRUE |
| 394 |
END useM2RTS ; |
| 395 |
|
| 396 |
|
| 397 |
(* |
| 398 |
useStrlen - indicate we have used strlen in the converted code. |
| 399 |
*) |
| 400 |
|
| 401 |
PROCEDURE useStrlen ; |
| 402 |
BEGIN |
| 403 |
seenStrlen := TRUE |
| 404 |
END useStrlen ; |
| 405 |
|
| 406 |
|
| 407 |
(* |
| 408 |
useCtype - indicate we have used the toupper function. |
| 409 |
*) |
| 410 |
|
| 411 |
PROCEDURE useCtype ; |
| 412 |
BEGIN |
| 413 |
seenCtype := TRUE |
| 414 |
END useCtype ; |
| 415 |
|
| 416 |
|
| 417 |
(* |
| 418 |
checkGccTypes - if we have imported tree or location_t from gcctypes |
| 419 |
then we include the gcc headers. |
| 420 |
*) |
| 421 |
|
| 422 |
PROCEDURE checkGccTypes (p: pretty) ; |
| 423 |
BEGIN |
| 424 |
IF seenGccTree OR seenGccLocation |
| 425 |
THEN |
| 426 |
print (p, '#include "gcc-consolidation.h"\n\n') |
| 427 |
END |
| 428 |
END checkGccTypes ; |
| 429 |
|
| 430 |
|
| 431 |
(* |
| 432 |
checkCtype - |
| 433 |
*) |
| 434 |
|
| 435 |
PROCEDURE checkCtype (p: pretty) ; |
| 436 |
BEGIN |
| 437 |
IF seenCtype |
| 438 |
THEN |
| 439 |
checkGccConfigSystem (p) ; |
| 440 |
IF getGccConfigSystem () |
| 441 |
THEN |
| 442 |
(* GCC header files use a safe variant. *) |
| 443 |
print (p, "#include <safe-ctype.h>\n") |
| 444 |
ELSE |
| 445 |
print (p, "#include <ctype.h>\n") |
| 446 |
END |
| 447 |
END |
| 448 |
END checkCtype ; |
| 449 |
|
| 450 |
|
| 451 |
(* |
| 452 |
checkAbs - check to see if the abs family, size_t or ssize_t have been used. |
| 453 |
*) |
| 454 |
|
| 455 |
PROCEDURE checkAbs (p: pretty) ; |
| 456 |
BEGIN |
| 457 |
IF seenLabs OR seenAbs OR seenFabs OR seenFabsl OR seenSize_t OR seenSSize_t |
| 458 |
THEN |
| 459 |
checkGccConfigSystem (p); |
| 460 |
IF NOT getGccConfigSystem () |
| 461 |
THEN |
| 462 |
print (p, "#include <stdlib.h>\n") |
| 463 |
END |
| 464 |
END |
| 465 |
END checkAbs ; |
| 466 |
|
| 467 |
|
| 468 |
(* |
| 469 |
checkLimits - |
| 470 |
*) |
| 471 |
|
| 472 |
PROCEDURE checkLimits (p: pretty) ; |
| 473 |
BEGIN |
| 474 |
IF seenMemcpy OR seenIntMin OR seenUIntMin OR |
| 475 |
seenLongMin OR seenULongMin OR seenCharMin OR |
| 476 |
seenUCharMin OR (* seenUIntMin OR *) seenIntMax OR |
| 477 |
seenUIntMax OR seenLongMax OR seenULongMax OR |
| 478 |
seenCharMax OR seenUCharMax (* OR seenUIntMax *) |
| 479 |
THEN |
| 480 |
checkGccConfigSystem (p); |
| 481 |
IF NOT getGccConfigSystem () |
| 482 |
THEN |
| 483 |
print (p, "#include <limits.h>\n") |
| 484 |
END |
| 485 |
END |
| 486 |
END checkLimits ; |
| 487 |
|
| 488 |
|
| 489 |
(* |
| 490 |
checkFreeMalloc - |
| 491 |
*) |
| 492 |
|
| 493 |
PROCEDURE checkFreeMalloc (p: pretty) ; |
| 494 |
BEGIN |
| 495 |
IF seenFree OR seenMalloc |
| 496 |
THEN |
| 497 |
checkGccConfigSystem (p); |
| 498 |
IF NOT getGccConfigSystem () |
| 499 |
THEN |
| 500 |
print (p, "#include <stdlib.h>\n") |
| 501 |
END |
| 502 |
END |
| 503 |
END checkFreeMalloc ; |
| 504 |
|
| 505 |
|
| 506 |
(* |
| 507 |
checkStorage - |
| 508 |
*) |
| 509 |
|
| 510 |
PROCEDURE checkStorage (p: pretty) ; |
| 511 |
BEGIN |
| 512 |
IF seenStorage |
| 513 |
THEN |
| 514 |
print (p, '# include "') ; |
| 515 |
prints (p, getHPrefix ()) ; |
| 516 |
print (p, 'Storage.h"\n') |
| 517 |
END |
| 518 |
END checkStorage ; |
| 519 |
|
| 520 |
|
| 521 |
(* |
| 522 |
checkProc - |
| 523 |
*) |
| 524 |
|
| 525 |
PROCEDURE checkProc (p: pretty) ; |
| 526 |
BEGIN |
| 527 |
IF seenProc |
| 528 |
THEN |
| 529 |
print (p, "# if !defined (PROC_D)\n") ; |
| 530 |
print (p, "# define PROC_D\n") ; |
| 531 |
print (p, " typedef void (*PROC_t) (void);\n") ; |
| 532 |
print (p, " typedef struct { PROC_t proc; } PROC;\n") ; |
| 533 |
print (p, "# endif\n\n") |
| 534 |
END |
| 535 |
END checkProc ; |
| 536 |
|
| 537 |
|
| 538 |
(* |
| 539 |
checkTrue - |
| 540 |
*) |
| 541 |
|
| 542 |
PROCEDURE checkTrue (p: pretty) ; |
| 543 |
BEGIN |
| 544 |
IF seenTrue |
| 545 |
THEN |
| 546 |
print (p, "# if !defined (TRUE)\n") ; |
| 547 |
print (p, "# define TRUE (1==1)\n") ; |
| 548 |
print (p, "# endif\n\n") |
| 549 |
END |
| 550 |
END checkTrue ; |
| 551 |
|
| 552 |
|
| 553 |
(* |
| 554 |
checkFalse - |
| 555 |
*) |
| 556 |
|
| 557 |
PROCEDURE checkFalse (p: pretty) ; |
| 558 |
BEGIN |
| 559 |
IF seenFalse |
| 560 |
THEN |
| 561 |
print (p, "# if !defined (FALSE)\n") ; |
| 562 |
print (p, "# define FALSE (1==0)\n") ; |
| 563 |
print (p, "# endif\n\n") |
| 564 |
END |
| 565 |
END checkFalse ; |
| 566 |
|
| 567 |
|
| 568 |
(* |
| 569 |
checkNull - |
| 570 |
*) |
| 571 |
|
| 572 |
PROCEDURE checkNull (p: pretty) ; |
| 573 |
BEGIN |
| 574 |
IF seenNull |
| 575 |
THEN |
| 576 |
checkGccConfigSystem (p); |
| 577 |
IF NOT getGccConfigSystem () |
| 578 |
THEN |
| 579 |
print (p, "#include <stddef.h>\n") |
| 580 |
END |
| 581 |
END |
| 582 |
END checkNull ; |
| 583 |
|
| 584 |
|
| 585 |
(* |
| 586 |
checkMemcpy - |
| 587 |
*) |
| 588 |
|
| 589 |
PROCEDURE checkMemcpy (p: pretty) ; |
| 590 |
BEGIN |
| 591 |
IF seenMemcpy OR seenStrlen |
| 592 |
THEN |
| 593 |
checkGccConfigSystem (p); |
| 594 |
IF NOT getGccConfigSystem () |
| 595 |
THEN |
| 596 |
print (p, "#include <string.h>\n") |
| 597 |
END |
| 598 |
END |
| 599 |
END checkMemcpy ; |
| 600 |
|
| 601 |
|
| 602 |
(* |
| 603 |
checkM2RTS - |
| 604 |
*) |
| 605 |
|
| 606 |
PROCEDURE checkM2RTS (p: pretty) ; |
| 607 |
BEGIN |
| 608 |
IF seenM2RTS |
| 609 |
THEN |
| 610 |
print (p, '# include "') ; |
| 611 |
prints (p, getHPrefix ()) ; |
| 612 |
print (p, 'M2RTS.h"\n') |
| 613 |
END |
| 614 |
END checkM2RTS ; |
| 615 |
|
| 616 |
|
| 617 |
(* |
| 618 |
useException - use the exceptions module, mcrts. |
| 619 |
*) |
| 620 |
|
| 621 |
PROCEDURE useException ; |
| 622 |
BEGIN |
| 623 |
seenException := TRUE |
| 624 |
END useException ; |
| 625 |
|
| 626 |
|
| 627 |
(* |
| 628 |
checkException - check to see if exceptions were used. |
| 629 |
*) |
| 630 |
|
| 631 |
PROCEDURE checkException (p: pretty) ; |
| 632 |
BEGIN |
| 633 |
IF seenException |
| 634 |
THEN |
| 635 |
print (p, '# include "Gmcrts.h"\n') |
| 636 |
END |
| 637 |
END checkException ; |
| 638 |
|
| 639 |
|
| 640 |
(* |
| 641 |
useThrow - use the throw function. |
| 642 |
*) |
| 643 |
|
| 644 |
PROCEDURE useThrow ; |
| 645 |
BEGIN |
| 646 |
seenThrow := TRUE |
| 647 |
END useThrow ; |
| 648 |
|
| 649 |
|
| 650 |
(* |
| 651 |
checkThrow - check to see if the throw function is used. |
| 652 |
*) |
| 653 |
|
| 654 |
PROCEDURE checkThrow (p: pretty) ; |
| 655 |
BEGIN |
| 656 |
IF seenThrow |
| 657 |
THEN |
| 658 |
(* print (p, '# include "sys/cdefs.h"\n') ; *) |
| 659 |
print (p, '#ifndef __cplusplus\n') ; |
| 660 |
print (p, 'extern void throw (unsigned int);\n') ; |
| 661 |
print (p, '#endif\n') |
| 662 |
END |
| 663 |
END checkThrow ; |
| 664 |
|
| 665 |
|
| 666 |
(* |
| 667 |
useUnistd - need to use unistd.h call using open/close/read/write require this header. |
| 668 |
*) |
| 669 |
|
| 670 |
PROCEDURE useUnistd ; |
| 671 |
BEGIN |
| 672 |
seenUnistd := TRUE |
| 673 |
END useUnistd ; |
| 674 |
|
| 675 |
|
| 676 |
(* |
| 677 |
checkUnistd - check to see if the unistd.h header file is required. |
| 678 |
*) |
| 679 |
|
| 680 |
PROCEDURE checkUnistd (p: pretty) ; |
| 681 |
BEGIN |
| 682 |
IF seenUnistd |
| 683 |
THEN |
| 684 |
checkGccConfigSystem (p); |
| 685 |
IF NOT getGccConfigSystem () |
| 686 |
THEN |
| 687 |
print (p, '#include <unistd.h>\n') |
| 688 |
END |
| 689 |
END |
| 690 |
END checkUnistd ; |
| 691 |
|
| 692 |
|
| 693 |
(* |
| 694 |
useComplex - use the complex data type. |
| 695 |
*) |
| 696 |
|
| 697 |
PROCEDURE useComplex ; |
| 698 |
BEGIN |
| 699 |
seenComplex := TRUE |
| 700 |
END useComplex ; |
| 701 |
|
| 702 |
|
| 703 |
(* |
| 704 |
checkComplex - check to see if the type complex was used. |
| 705 |
*) |
| 706 |
|
| 707 |
PROCEDURE checkComplex (p: pretty) ; |
| 708 |
BEGIN |
| 709 |
IF seenComplex |
| 710 |
THEN |
| 711 |
checkGccConfigSystem (p); |
| 712 |
IF NOT getGccConfigSystem () |
| 713 |
THEN |
| 714 |
print (p, '# include <complex.h>\n') |
| 715 |
END |
| 716 |
END |
| 717 |
END checkComplex ; |
| 718 |
|
| 719 |
|
| 720 |
(* |
| 721 |
checkSysTypes - emit header for sys/types.h if necessary. |
| 722 |
*) |
| 723 |
|
| 724 |
PROCEDURE checkSysTypes (p: pretty) ; |
| 725 |
BEGIN |
| 726 |
IF seenSysTypes |
| 727 |
THEN |
| 728 |
checkGccConfigSystem (p); |
| 729 |
IF NOT getGccConfigSystem () |
| 730 |
THEN |
| 731 |
print (p, '# include <sys/types.h>\n') |
| 732 |
END |
| 733 |
END |
| 734 |
END checkSysTypes ; |
| 735 |
|
| 736 |
|
| 737 |
(* |
| 738 |
fixNullPointerConst - fixup for NULL on some C++11 systems. |
| 739 |
*) |
| 740 |
|
| 741 |
PROCEDURE fixNullPointerConst (p: pretty) ; |
| 742 |
BEGIN |
| 743 |
IF seenNull |
| 744 |
THEN |
| 745 |
print (p, '#if defined(__cplusplus)\n') ; |
| 746 |
print (p, '# undef NULL\n') ; |
| 747 |
print (p, '# define NULL 0\n') ; |
| 748 |
print (p, '#endif\n') |
| 749 |
END |
| 750 |
END fixNullPointerConst ; |
| 751 |
|
| 752 |
|
| 753 |
(* |
| 754 |
genBool - |
| 755 |
*) |
| 756 |
|
| 757 |
PROCEDURE genBool (p: pretty) ; |
| 758 |
BEGIN |
| 759 |
IF useBool () |
| 760 |
THEN |
| 761 |
print (p, '#include <stdbool.h>\n') ; |
| 762 |
END |
| 763 |
END genBool ; |
| 764 |
|
| 765 |
|
| 766 |
(* |
| 767 |
genDefs - generate definitions or includes for all |
| 768 |
macros and prototypes used. |
| 769 |
*) |
| 770 |
|
| 771 |
PROCEDURE genDefs (p: pretty) ; |
| 772 |
BEGIN |
| 773 |
genBool (p) ; |
| 774 |
checkFreeMalloc (p) ; |
| 775 |
checkProc (p) ; |
| 776 |
checkTrue (p) ; |
| 777 |
checkFalse (p) ; |
| 778 |
checkNull (p) ; |
| 779 |
checkMemcpy (p) ; |
| 780 |
checkLimits (p) ; |
| 781 |
checkAbs (p) ; |
| 782 |
checkStorage (p) ; |
| 783 |
checkException (p) ; |
| 784 |
checkComplex (p) ; |
| 785 |
checkCtype (p) ; |
| 786 |
checkUnistd (p) ; |
| 787 |
checkSysTypes (p) ; |
| 788 |
checkM2RTS (p) ; |
| 789 |
checkThrow (p) ; |
| 790 |
fixNullPointerConst (p) |
| 791 |
END genDefs ; |
| 792 |
|
| 793 |
|
| 794 |
(* |
| 795 |
genConfigSystem - generate include files for config.h and system.h |
| 796 |
within the GCC framework. |
| 797 |
*) |
| 798 |
|
| 799 |
PROCEDURE genConfigSystem (p: pretty) ; |
| 800 |
BEGIN |
| 801 |
checkGccConfigSystem (p) |
| 802 |
END genConfigSystem ; |
| 803 |
|
| 804 |
|
| 805 |
(* |
| 806 |
new - |
| 807 |
*) |
| 808 |
|
| 809 |
PROCEDURE new (n: node) : scope ; |
| 810 |
VAR |
| 811 |
s: scope ; |
| 812 |
BEGIN |
| 813 |
IF freeList = NIL |
| 814 |
THEN |
| 815 |
NEW (s) |
| 816 |
ELSE |
| 817 |
s := freeList ; |
| 818 |
freeList := freeList^.next |
| 819 |
END ; |
| 820 |
RETURN s |
| 821 |
END new ; |
| 822 |
|
| 823 |
|
| 824 |
(* |
| 825 |
enterScope - enter a scope defined by, n. |
| 826 |
*) |
| 827 |
|
| 828 |
PROCEDURE enterScope (n: node) ; |
| 829 |
VAR |
| 830 |
s: scope ; |
| 831 |
BEGIN |
| 832 |
s := new (n) ; |
| 833 |
WITH s^ DO |
| 834 |
scoped := n ; |
| 835 |
symbols := initTree () ; |
| 836 |
next := stack |
| 837 |
END ; |
| 838 |
stack := s |
| 839 |
END enterScope ; |
| 840 |
|
| 841 |
|
| 842 |
(* |
| 843 |
leaveScope - leave the scope defined by, n. |
| 844 |
*) |
| 845 |
|
| 846 |
PROCEDURE leaveScope (n: node) ; |
| 847 |
VAR |
| 848 |
s: scope ; |
| 849 |
BEGIN |
| 850 |
IF n = stack^.scoped |
| 851 |
THEN |
| 852 |
s := stack ; |
| 853 |
stack := stack^.next ; |
| 854 |
WITH s^ DO |
| 855 |
scoped := NIL ; |
| 856 |
killTree (symbols) ; |
| 857 |
next := NIL |
| 858 |
END |
| 859 |
ELSE |
| 860 |
HALT |
| 861 |
END |
| 862 |
END leaveScope ; |
| 863 |
|
| 864 |
|
| 865 |
(* |
| 866 |
mangle1 - returns TRUE if name is unique if we add _ |
| 867 |
to its end. |
| 868 |
*) |
| 869 |
|
| 870 |
PROCEDURE mangle1 (n: Name; VAR m: String; scopes: BOOLEAN) : BOOLEAN ; |
| 871 |
BEGIN |
| 872 |
m := KillString (m) ; |
| 873 |
m := InitStringCharStar (keyToCharStar (n)) ; |
| 874 |
m := ConCatChar (m, '_') ; |
| 875 |
RETURN NOT clash (makekey (string (m)), scopes) |
| 876 |
END mangle1 ; |
| 877 |
|
| 878 |
|
| 879 |
(* |
| 880 |
mangle2 - returns TRUE if name is unique if we prepend _ |
| 881 |
to, n. |
| 882 |
*) |
| 883 |
|
| 884 |
PROCEDURE mangle2 (n: Name; VAR m: String; scopes: BOOLEAN) : BOOLEAN ; |
| 885 |
BEGIN |
| 886 |
m := KillString (m) ; |
| 887 |
m := InitStringCharStar (keyToCharStar (n)) ; |
| 888 |
m := ConCat (InitString ('_'), Mark (m)) ; |
| 889 |
RETURN NOT clash (makekey (string (m)), scopes) |
| 890 |
END mangle2 ; |
| 891 |
|
| 892 |
|
| 893 |
(* |
| 894 |
mangleN - keep adding '_' to the end of n until it |
| 895 |
no longer clashes. |
| 896 |
*) |
| 897 |
|
| 898 |
PROCEDURE mangleN (n: Name; VAR m: String; scopes: BOOLEAN) : BOOLEAN ; |
| 899 |
BEGIN |
| 900 |
m := KillString (m) ; |
| 901 |
m := InitStringCharStar (keyToCharStar (n)) ; |
| 902 |
LOOP |
| 903 |
m := ConCatChar (m, '_') ; |
| 904 |
IF NOT clash (makekey (string (m)), scopes) |
| 905 |
THEN |
| 906 |
RETURN TRUE |
| 907 |
END |
| 908 |
END |
| 909 |
END mangleN ; |
| 910 |
|
| 911 |
|
| 912 |
(* |
| 913 |
clash - returns TRUE if there is a clash with name, n, |
| 914 |
in the current scope or C keywords or C macros. |
| 915 |
*) |
| 916 |
|
| 917 |
PROCEDURE clash (n: Name; scopes: BOOLEAN) : BOOLEAN ; |
| 918 |
BEGIN |
| 919 |
IF (getSymKey (macros, n) # NIL) OR |
| 920 |
(getSymKey (keywords, n) # NIL) |
| 921 |
THEN |
| 922 |
RETURN TRUE |
| 923 |
END ; |
| 924 |
RETURN scopes AND (getSymKey (stack^.symbols, n) # NIL) |
| 925 |
END clash ; |
| 926 |
|
| 927 |
|
| 928 |
(* |
| 929 |
cname - attempts to declare a symbol with name, n, in the |
| 930 |
current scope. If there is no conflict with the |
| 931 |
target language then NIL is returned, otherwise |
| 932 |
a mangled name is returned as a String. |
| 933 |
If scopes is FALSE then only the keywords and |
| 934 |
macros are detected for a clash (all scoping |
| 935 |
is ignored). |
| 936 |
*) |
| 937 |
|
| 938 |
PROCEDURE cname (n: Name; scopes: BOOLEAN) : String ; |
| 939 |
VAR |
| 940 |
m: String ; |
| 941 |
BEGIN |
| 942 |
m := NIL ; |
| 943 |
IF clash (n, scopes) |
| 944 |
THEN |
| 945 |
IF mangle1 (n, m, scopes) OR mangle2 (n, m, scopes) OR mangleN (n, m, scopes) |
| 946 |
THEN |
| 947 |
IF scopes |
| 948 |
THEN |
| 949 |
(* no longer a clash with, m, so add it to the current scope. *) |
| 950 |
n := makekey (string (m)) ; |
| 951 |
putSymKey (stack^.symbols, n, m) |
| 952 |
END |
| 953 |
ELSE |
| 954 |
(* mangleN must always succeed. *) |
| 955 |
HALT |
| 956 |
END |
| 957 |
ELSIF scopes |
| 958 |
THEN |
| 959 |
(* no clash, add it to the current scope. *) |
| 960 |
putSymKey (stack^.symbols, n, InitStringCharStar (keyToCharStar (n))) |
| 961 |
END ; |
| 962 |
RETURN m |
| 963 |
END cname ; |
| 964 |
|
| 965 |
|
| 966 |
(* |
| 967 |
cnamen - attempts to declare a symbol with name, n, in the |
| 968 |
current scope. If there is no conflict with the |
| 969 |
target language then NIL is returned, otherwise |
| 970 |
a mangled name is returned as a Name |
| 971 |
If scopes is FALSE then only the keywords and |
| 972 |
macros are detected for a clash (all scoping |
| 973 |
is ignored). |
| 974 |
*) |
| 975 |
|
| 976 |
PROCEDURE cnamen (n: Name; scopes: BOOLEAN) : Name ; |
| 977 |
VAR |
| 978 |
m: String ; |
| 979 |
BEGIN |
| 980 |
m := NIL ; |
| 981 |
IF clash (n, scopes) |
| 982 |
THEN |
| 983 |
IF mangle1 (n, m, scopes) OR mangle2 (n, m, scopes) OR mangleN (n, m, scopes) |
| 984 |
THEN |
| 985 |
n := makekey (string (m)) ; |
| 986 |
IF scopes |
| 987 |
THEN |
| 988 |
(* no longer a clash with, m, so add it to the current scope. *) |
| 989 |
putSymKey (stack^.symbols, n, m) |
| 990 |
END |
| 991 |
ELSE |
| 992 |
(* mangleN must always succeed. *) |
| 993 |
HALT |
| 994 |
END |
| 995 |
ELSIF scopes |
| 996 |
THEN |
| 997 |
(* no clash, add it to the current scope. *) |
| 998 |
putSymKey (stack^.symbols, n, InitStringCharStar (keyToCharStar (n))) |
| 999 |
END ; |
| 1000 |
m := KillString (m) ; |
| 1001 |
RETURN n |
| 1002 |
END cnamen ; |
| 1003 |
|
| 1004 |
|
| 1005 |
(* |
| 1006 |
cp - include C++ keywords and standard declarations to avoid. |
| 1007 |
*) |
| 1008 |
|
| 1009 |
PROCEDURE cp ; |
| 1010 |
BEGIN |
| 1011 |
IF NOT initializedCP |
| 1012 |
THEN |
| 1013 |
initializedCP := TRUE ; |
| 1014 |
initCP |
| 1015 |
END |
| 1016 |
END cp ; |
| 1017 |
|
| 1018 |
|
| 1019 |
(* |
| 1020 |
initCP - add the extra keywords and standard definitions used by C++. |
| 1021 |
*) |
| 1022 |
|
| 1023 |
PROCEDURE initCP ; |
| 1024 |
BEGIN |
| 1025 |
add (keywords, 'delete') ; |
| 1026 |
add (keywords, 'try') ; |
| 1027 |
add (keywords, 'catch') ; |
| 1028 |
add (keywords, 'operator') ; |
| 1029 |
add (keywords, 'complex') ; |
| 1030 |
add (keywords, 'export') ; |
| 1031 |
add (keywords, 'public') |
| 1032 |
END initCP ; |
| 1033 |
|
| 1034 |
|
| 1035 |
(* |
| 1036 |
add - |
| 1037 |
*) |
| 1038 |
|
| 1039 |
PROCEDURE add (s: symbolTree; a: ARRAY OF CHAR) ; |
| 1040 |
BEGIN |
| 1041 |
putSymKey (s, makeKey (a), InitString (a)) |
| 1042 |
END add ; |
| 1043 |
|
| 1044 |
|
| 1045 |
(* |
| 1046 |
initMacros - macros and library function names to avoid. |
| 1047 |
*) |
| 1048 |
|
| 1049 |
PROCEDURE initMacros ; |
| 1050 |
BEGIN |
| 1051 |
macros := initTree () ; |
| 1052 |
add (macros, 'FILE') ; |
| 1053 |
add (macros, 'EOF') ; |
| 1054 |
add (macros, 'stdio') ; |
| 1055 |
add (macros, 'stdout') ; |
| 1056 |
add (macros, 'stderr') ; |
| 1057 |
add (macros, 'write') ; |
| 1058 |
add (macros, 'read') ; |
| 1059 |
add (macros, 'exit') ; |
| 1060 |
add (macros, 'abs') ; |
| 1061 |
add (macros, 'optarg') ; |
| 1062 |
add (macros, 'div') ; |
| 1063 |
add (macros, 'sin') ; |
| 1064 |
add (macros, 'cos') ; |
| 1065 |
add (macros, 'tan') ; |
| 1066 |
add (macros, 'log10') ; |
| 1067 |
add (macros, 'trunc') ; |
| 1068 |
add (macros, 'I') ; |
| 1069 |
add (macros, 'csqrt') ; |
| 1070 |
add (macros, 'strlen') ; |
| 1071 |
add (macros, 'strcpy') ; |
| 1072 |
add (macros, 'free') ; |
| 1073 |
add (macros, 'malloc') ; |
| 1074 |
add (macros, 'time') ; |
| 1075 |
add (macros, 'main') ; |
| 1076 |
add (macros, 'true') ; |
| 1077 |
add (macros, 'false') ; |
| 1078 |
add (macros, 'sigfpe') |
| 1079 |
END initMacros ; |
| 1080 |
|
| 1081 |
|
| 1082 |
(* |
| 1083 |
initKeywords - keywords to avoid. |
| 1084 |
*) |
| 1085 |
|
| 1086 |
PROCEDURE initKeywords ; |
| 1087 |
BEGIN |
| 1088 |
keywords := initTree () ; |
| 1089 |
add (keywords, 'auto') ; |
| 1090 |
add (keywords, 'break') ; |
| 1091 |
add (keywords, 'case') ; |
| 1092 |
add (keywords, 'char') ; |
| 1093 |
add (keywords, 'const') ; |
| 1094 |
add (keywords, 'continue') ; |
| 1095 |
add (keywords, 'default') ; |
| 1096 |
add (keywords, 'do') ; |
| 1097 |
add (keywords, 'double') ; |
| 1098 |
add (keywords, 'else') ; |
| 1099 |
add (keywords, 'enum') ; |
| 1100 |
add (keywords, 'extern') ; |
| 1101 |
add (keywords, 'float') ; |
| 1102 |
add (keywords, 'for') ; |
| 1103 |
add (keywords, 'goto') ; |
| 1104 |
add (keywords, 'if') ; |
| 1105 |
add (keywords, 'int') ; |
| 1106 |
add (keywords, 'long') ; |
| 1107 |
add (keywords, 'register') ; |
| 1108 |
add (keywords, 'return') ; |
| 1109 |
add (keywords, 'short') ; |
| 1110 |
add (keywords, 'signed') ; |
| 1111 |
add (keywords, 'sizeof') ; |
| 1112 |
add (keywords, 'static') ; |
| 1113 |
add (keywords, 'struct') ; |
| 1114 |
add (keywords, 'switch') ; |
| 1115 |
add (keywords, 'typedef') ; |
| 1116 |
add (keywords, 'union') ; |
| 1117 |
add (keywords, 'unsigned') ; |
| 1118 |
add (keywords, 'void') ; |
| 1119 |
add (keywords, 'volatile') ; |
| 1120 |
add (keywords, 'while') ; |
| 1121 |
add (keywords, 'and') ; |
| 1122 |
add (keywords, 'or') ; |
| 1123 |
add (keywords, 'not') ; |
| 1124 |
add (keywords, 'throw') ; |
| 1125 |
add (keywords, 'new') |
| 1126 |
END initKeywords ; |
| 1127 |
|
| 1128 |
|
| 1129 |
(* |
| 1130 |
init - |
| 1131 |
*) |
| 1132 |
|
| 1133 |
PROCEDURE init ; |
| 1134 |
BEGIN |
| 1135 |
seenUnistd := FALSE ; |
| 1136 |
seenThrow := FALSE ; |
| 1137 |
seenFree := FALSE ; |
| 1138 |
seenMalloc := FALSE ; |
| 1139 |
seenStorage := FALSE ; |
| 1140 |
seenProc := FALSE ; |
| 1141 |
seenTrue := FALSE ; |
| 1142 |
seenFalse := FALSE ; |
| 1143 |
seenNull := FALSE ; |
| 1144 |
seenMemcpy := FALSE ; |
| 1145 |
seenIntMin := FALSE ; |
| 1146 |
seenUIntMin := FALSE ; |
| 1147 |
seenLongMin := FALSE ; |
| 1148 |
seenULongMin := FALSE ; |
| 1149 |
seenCharMin := FALSE ; |
| 1150 |
seenUCharMin := FALSE ; |
| 1151 |
seenIntMax := FALSE ; |
| 1152 |
seenUIntMax := FALSE ; |
| 1153 |
seenLongMax := FALSE ; |
| 1154 |
seenULongMax := FALSE ; |
| 1155 |
seenCharMax := FALSE ; |
| 1156 |
seenUCharMax := FALSE ; |
| 1157 |
seenLabs := FALSE ; |
| 1158 |
seenAbs := FALSE ; |
| 1159 |
seenFabs := FALSE ; |
| 1160 |
seenFabsl := FALSE ; |
| 1161 |
seenException := FALSE ; |
| 1162 |
seenComplex := FALSE ; |
| 1163 |
seenM2RTS := FALSE ; |
| 1164 |
seenStrlen := FALSE ; |
| 1165 |
seenCtype := FALSE ; |
| 1166 |
seenSize_t := FALSE ; |
| 1167 |
seenSSize_t := FALSE ; |
| 1168 |
seenSysTypes := FALSE ; |
| 1169 |
seenGccTree := FALSE ; |
| 1170 |
seenGccLocation := FALSE ; |
| 1171 |
initializedCP := FALSE ; |
| 1172 |
initializedGCC := FALSE ; |
| 1173 |
|
| 1174 |
stack := NIL ; |
| 1175 |
freeList := NIL ; |
| 1176 |
initKeywords ; |
| 1177 |
initMacros |
| 1178 |
END init ; |
| 1179 |
|
| 1180 |
|
| 1181 |
BEGIN |
| 1182 |
init |
| 1183 |
END keyc. |