Debian Codegraph @

gcc/m2/mc/decl.mod

raw
1 (* decl.mod declaration nodes used to create the AST.
2
3 Copyright (C) 2015-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE decl ; (*!m2pim*)
23
24 FROM ASCII IMPORT lf, tab ;
25 FROM symbolKey IMPORT NulKey, symbolTree, initTree, getSymKey, putSymKey, foreachNodeDo ;
26 FROM mcDebug IMPORT assert ;
27 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
28 FROM nameKey IMPORT NulName, makeKey, lengthKey, makekey, keyToCharStar ;
29 FROM SFIO IMPORT OpenToWrite, WriteS ;
30 FROM FIO IMPORT File, Close, FlushBuffer, StdOut, WriteLine, WriteChar ;
31 FROM DynamicStrings IMPORT String, InitString, EqualArray, InitStringCharStar, KillString, ConCat, Mark, RemoveWhitePostfix, RemoveWhitePrefix ;
32 FROM StringConvert IMPORT CardinalToString, ostoc ;
33
34 FROM mcOptions IMPORT getOutputFile, getDebugTopological, getHPrefix, getIgnoreFQ,
35 getExtendedOpaque, writeGPLheader, getGccConfigSystem,
36 getScaffoldDynamic, getScaffoldMain, getSuppressNoReturn,
37 useBool, getCRealType, getCShortRealType,
38 getCLongRealType ;
39
40 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3 ;
41 FROM libc IMPORT printf, memset ;
42 FROM mcMetaError IMPORT metaError1, metaError2, metaError3, metaErrors1, metaErrors2 ;
43 FROM mcError IMPORT errorAbort0, flushErrors ;
44
45 FROM mcLexBuf IMPORT findFileNameFromToken, tokenToLineNo, tokenToColumnNo,
46 getProcedureComment, getBodyComment, getAfterComment,
47 lastcomment ;
48
49 FROM mcComment IMPORT commentDesc, isProcedureComment, isAfterComment, isBodyComment, getContent, initComment, addText ;
50
51 FROM StrLib IMPORT StrEqual, StrLen ;
52
53 FROM mcPretty IMPORT pretty, initPretty, dupPretty, killPretty, print, prints, raw,
54 setNeedSpace, noSpace, setindent, getindent, getcurpos,
55 getseekpos, getcurline,
56 pushPretty, popPretty ;
57
58 FROM Indexing IMPORT Index, InitIndex, ForeachIndiceInIndexDo,
59 IncludeIndiceIntoIndex, IsIndiceInIndex,
60 HighIndice, LowIndice, GetIndice, RemoveIndiceFromIndex,
61 PutIndice, InBounds ;
62
63 IMPORT DynamicStrings ;
64 IMPORT alists, wlists ;
65 IMPORT keyc ;
66 IMPORT mcStream ;
67
68 FROM alists IMPORT alist ;
69 FROM wlists IMPORT wlist ;
70
71
72 CONST
73 indentation = 3 ;
74 indentationC = 2 ;
75 debugScopes = FALSE ;
76 debugDecl = FALSE ;
77 caseException = TRUE ;
78 returnException = TRUE ;
79 (* this is a work around to avoid ever having to handle dangling else. *)
80 forceCompoundStatement = TRUE ; (* TRUE will avoid dangling else, by always using {}. *)
81 enableDefForCStrings = FALSE ; (* currently disabled. *)
82 enableMemsetOnAllocation = TRUE ; (* Should we memset (..., 0, ...) the allocated mem? *)
83 forceQualified = TRUE ;
84 debugOpaque = FALSE ;
85
86 TYPE
87 language = (ansiC, ansiCP, pim4) ;
88
89 nodeT = (explist, funccall,
90 exit, return, stmtseq, comment, halt,
91 new, dispose, inc, dec, incl, excl,
92 length,
93 (* base constants. *)
94 nil, true, false,
95 (* system types. *)
96 address, loc, byte, word,
97 csizet, cssizet, cofft,
98 cardinal64,
99 (* base types. *)
100 char,
101 cardinal, longcard, shortcard,
102 integer, longint, shortint,
103 real, longreal, shortreal,
104 bitset, boolean, proc,
105 ztype, rtype,
106 complex, longcomplex, shortcomplex,
107 (* language features and compound type attributes. *)
108 type, record, varient, var, enumeration,
109 subrange, array, subscript,
110 string, const, literal, varparam, param, varargs, optarg,
111 pointer, recordfield, varientfield, enumerationfield,
112 set, proctype,
113 (* blocks. *)
114 procedure, def, imp, module,
115 (* statements. *)
116 loop, while, for, repeat,
117 case, caselabellist, caselist, range,
118 assignment,
119 if, elsif,
120 (* expressions. *)
121 constexp,
122 neg,
123 cast, val,
124 plus, sub, div, mod, mult, divide, in,
125 adr, size, tsize, ord, float, trunc, chr, abs, cap,
126 high, throw, unreachable,
127 cmplx, re, im,
128 min, max,
129 componentref, pointerref, arrayref, deref,
130 equal, notequal, less, greater, greequal, lessequal,
131 lsl, lsr, lor, land, lnot, lxor,
132 and, or, not, identlist, vardecl, setvalue, opaquecast) ;
133
134 node = POINTER TO nodeRec ;
135
136 nodeRec = RECORD
137 CASE kind: nodeT OF
138
139 unreachable,
140 throw,
141 new,
142 dispose,
143 inc,
144 dec,
145 incl,
146 excl,
147 halt : intrinsicF: intrinsicT |
148 explist : explistF: explistT |
149 exit : exitF : exitT |
150 return : returnF : returnT |
151 stmtseq : stmtF : stmtT |
152 comment : commentF: commentT |
153 (* base constants. *)
154 nil,
155 true,
156 false,
157 (* system types. *)
158 address,
159 loc,
160 byte,
161 word,
162 csizet,
163 cssizet,
164 cofft,
165 cardinal64 : |
166 (* base types. *)
167 boolean,
168 proc,
169 char,
170 integer,
171 cardinal,
172 longcard,
173 shortcard,
174 longint,
175 shortint,
176 real,
177 longreal,
178 shortreal,
179 bitset,
180 ztype,
181 rtype,
182 complex,
183 longcomplex,
184 shortcomplex : |
185 (* language features and compound type attributes. *)
186 type : typeF : typeT |
187 record : recordF : recordT |
188 varient : varientF : varientT |
189 var : varF : varT |
190 enumeration : enumerationF : enumerationT |
191 subrange : subrangeF : subrangeT |
192 subscript : subscriptF : subscriptT |
193 array : arrayF : arrayT |
194 string : stringF : stringT |
195 const : constF : constT |
196 literal : literalF : literalT |
197 varparam : varparamF : varparamT |
198 param : paramF : paramT |
199 varargs : varargsF : varargsT |
200 optarg : optargF : optargT |
201 pointer : pointerF : pointerT |
202 recordfield : recordfieldF : recordfieldT |
203 varientfield : varientfieldF : varientfieldT |
204 enumerationfield: enumerationfieldF: enumerationfieldT |
205 set : setF : setT |
206 proctype : proctypeF : proctypeT |
207 (* blocks. *)
208 procedure : procedureF : procedureT |
209 def : defF : defT |
210 imp : impF : impT |
211 module : moduleF : moduleT |
212 (* statements. *)
213 loop : loopF : loopT |
214 while : whileF : whileT |
215 for : forF : forT |
216 repeat : repeatF : repeatT |
217 case : caseF : caseT |
218 caselabellist : caselabellistF : caselabellistT |
219 caselist : caselistF : caselistT |
220 range : rangeF : rangeT |
221 if : ifF : ifT |
222 elsif : elsifF : elsifT |
223 assignment : assignmentF : assignmentT |
224 (* expressions. *)
225 arrayref : arrayrefF : arrayrefT |
226 pointerref : pointerrefF : pointerrefT |
227 componentref : componentrefF : componentrefT |
228 cmplx,
229 and,
230 or,
231 equal,
232 notequal,
233 less,
234 greater,
235 greequal,
236 lessequal,
237 val,
238 cast,
239 plus,
240 sub,
241 div,
242 mod,
243 mult,
244 divide,
245 in : binaryF : binaryT |
246 constexp,
247 deref,
248 abs,
249 chr,
250 cap,
251 high,
252 ord,
253 float,
254 trunc,
255 re,
256 im,
257 not,
258 neg,
259 adr,
260 size,
261 tsize,
262 min,
263 max : unaryF : unaryT |
264 identlist : identlistF : identlistT |
265 vardecl : vardeclF : vardeclT |
266 funccall : funccallF : funccallT |
267 setvalue : setvalueF : setvalueT |
268 opaquecast : opaquecastF : opaquecastT
269
270 END ;
271 at: where ;
272 END ;
273
274 opaqueCastState = RECORD
275 opaque,
276 voidStar: BOOLEAN ;
277 END ;
278
279 opaquecastT = RECORD
280 (* Describes the cast of the opaque. *)
281 exp : node ;
282 opaqueState: opaqueCastState ;
283 END ;
284
285 intrinsicT = RECORD
286 args : node ;
287 noArgs : CARDINAL ;
288 type : node ;
289 intrinsicComment: commentPair ;
290 postUnreachable : BOOLEAN ;
291 END ;
292
293 fixupInfo = RECORD
294 count: CARDINAL ;
295 info : Index ;
296 END ;
297
298 explistT = RECORD
299 exp: Index ;
300 END ;
301
302 setvalueT = RECORD
303 type: node ;
304 values: Index ;
305 END ;
306
307 identlistT = RECORD
308 names : wlist ;
309 cnamed: BOOLEAN ;
310 END ;
311
312 funccallT = RECORD
313 function : node ;
314 args : node ;
315 type : node ;
316 funccallComment: commentPair ;
317 opaqueState : opaqueCastState ;
318 END ;
319
320 commentT = RECORD
321 content: commentDesc ;
322 END ;
323
324 stmtT = RECORD
325 statements: Index ;
326 END ;
327
328 returnT = RECORD
329 exp : node ;
330 scope : node ;
331 returnComment: commentPair ;
332 END ;
333
334 exitT = RECORD
335 loop: node ;
336 END ;
337
338 vardeclT = RECORD
339 names: wlist ;
340 type : node ;
341 scope: node ;
342 END ;
343
344 typeT = RECORD
345 name : Name ;
346 type : node ;
347 scope : node ;
348 isOpaque,
349 isHidden,
350 isInternal: BOOLEAN ;
351 END ;
352
353 recordT = RECORD
354 localSymbols: symbolTree ;
355 listOfSons : Index ;
356 scope : node ;
357 END ;
358
359 varientT = RECORD
360 listOfSons: Index ;
361 varient : node ;
362 tag : node ;
363 scope : node ;
364 END ;
365
366 varT = RECORD
367 name : Name ;
368 type : node ;
369 decl : node ;
370 scope : node ;
371 isInitialised,
372 isParameter,
373 isVarParameter,
374 isUsed : BOOLEAN ;
375 cname : cnameT ;
376 opaqueState : opaqueCastState ;
377 END ;
378
379 enumerationT = RECORD
380 noOfElements: CARDINAL ;
381 localSymbols: symbolTree ;
382 listOfSons : Index ;
383 low, high : node ;
384 scope : node ;
385 END ;
386
387 subrangeT = RECORD
388 low,
389 high : node ;
390 type : node ;
391 scope: node ;
392 END ;
393
394 subscriptT = RECORD
395 type: node ;
396 expr: node ;
397 END ;
398
399 arrayT = RECORD
400 subr : node ;
401 type,
402 scope : node ;
403 isUnbounded: BOOLEAN ;
404 opaqueState: opaqueCastState ;
405 END ;
406
407 stringT = RECORD
408 name : Name ;
409 length : CARDINAL ;
410 isCharCompatible: BOOLEAN ;
411 cstring : String ;
412 clength : CARDINAL ;
413 cchar : String ;
414 END ;
415
416 literalT = RECORD
417 name : Name ;
418 type : node ;
419 END ;
420
421 constT = RECORD
422 name : Name ;
423 type : node ;
424 value: node ;
425 scope: node ;
426 END ;
427
428 varparamT = RECORD
429 namelist : node ;
430 type : node ;
431 scope : node ;
432 isUnbounded: BOOLEAN ;
433 isForC : BOOLEAN ;
434 isUsed : BOOLEAN ;
435 opaqueState: opaqueCastState ;
436 END ;
437
438 paramT = RECORD
439 namelist : node ;
440 type : node ;
441 scope : node ;
442 isUnbounded: BOOLEAN ;
443 isForC : BOOLEAN ;
444 isUsed : BOOLEAN ;
445 opaqueState: opaqueCastState ;
446 END ;
447
448 varargsT = RECORD
449 scope : node ;
450 END ;
451
452 optargT = RECORD
453 namelist : node ;
454 type : node ;
455 scope : node ;
456 init : node ;
457 END ;
458
459 pointerT = RECORD
460 type : node ;
461 scope : node ;
462 opaqueState: opaqueCastState ;
463 END ;
464
465 recordfieldT = RECORD
466 name : Name ;
467 type : node ;
468 tag : BOOLEAN ;
469 parent : node ;
470 varient : node ;
471 scope : node ;
472 cname : cnameT ;
473 opaqueState: opaqueCastState ;
474 END ;
475
476 varientfieldT = RECORD
477 name : Name ;
478 parent : node ;
479 varient : node ;
480 simple : BOOLEAN ;
481 listOfSons: Index ;
482 scope : node ;
483 END ;
484
485 enumerationfieldT = RECORD
486 name : Name ;
487 type : node ;
488 scope: node ;
489 value: CARDINAL ;
490 cname: cnameT ;
491 END ;
492
493 setT = RECORD
494 type : node ;
495 scope: node ;
496 END ;
497
498 componentrefT = RECORD
499 rec : node ;
500 field : node ;
501 resultType : node ;
502 opaqueState: opaqueCastState ;
503 END ;
504
505 pointerrefT = RECORD
506 ptr : node ;
507 field : node ;
508 resultType : node ;
509 opaqueState: opaqueCastState ;
510 END ;
511
512 arrayrefT = RECORD
513 array : node ;
514 index : node ;
515 resultType : node ;
516 opaqueState: opaqueCastState ;
517 END ;
518
519 commentPair = RECORD
520 after,
521 body : node ;
522 END ;
523
524 assignmentT = RECORD
525 des,
526 expr : node ;
527 assignComment: commentPair ;
528 END ;
529
530 ifT = RECORD
531 expr,
532 elsif, (* either else or elsif must be NIL. *)
533 then,
534 else : node ;
535 ifComment,
536 elseComment, (* used for else or elsif *)
537 endComment : commentPair ;
538 END ;
539
540 elsifT = RECORD
541 expr,
542 elsif, (* either else or elsif must be NIL. *)
543 then,
544 else : node ;
545 elseComment: commentPair ; (* used for else or elsif *)
546 END ;
547
548 loopT = RECORD
549 statements: node ;
550 labelno : CARDINAL ; (* 0 means no label. *)
551 END ;
552
553 whileT = RECORD
554 expr,
555 statements: node ;
556 doComment,
557 endComment: commentPair ;
558 END ;
559
560 repeatT = RECORD
561 expr,
562 statements : node ;
563 repeatComment,
564 untilComment : commentPair ;
565 END ;
566
567 caseT = RECORD
568 expression : node ;
569 caseLabelList: Index ;
570 else : node ;
571 END ;
572
573 caselabellistT = RECORD
574 caseList : node ;
575 statements: node ;
576 END ;
577
578 caselistT = RECORD
579 rangePairs: Index ;
580 END ;
581
582 rangeT = RECORD
583 lo,
584 hi: node ;
585 END ;
586
587 forT = RECORD
588 des,
589 start,
590 end,
591 increment,
592 statements: node ;
593 END ;
594
595 statementT = RECORD
596 sequence: Index ;
597 END ;
598
599 scopeT = RECORD
600 symbols : symbolTree ;
601 constants,
602 types,
603 procedures,
604 variables : Index ;
605 END ;
606
607 procedureT = RECORD
608 name : Name ;
609 decls : scopeT ;
610 scope : node ;
611 parameters : Index ;
612 isForC,
613 built,
614 checking,
615 returnopt,
616 vararg,
617 noreturnused,
618 noreturn : BOOLEAN ;
619 paramcount : CARDINAL ;
620 optarg : node ;
621 returnType : node ;
622 beginStatements: node ;
623 cname : cnameT ;
624 defComment,
625 modComment : commentDesc ;
626 opaqueState : opaqueCastState ;
627 END ;
628
629 proctypeT = RECORD
630 parameters : Index ;
631 returnopt,
632 vararg : BOOLEAN ;
633 optarg : node ;
634 scope : node ;
635 returnType : node ;
636 opaqueState: opaqueCastState ;
637 END ;
638
639 binaryT = RECORD
640 left,
641 right,
642 resultType: node ;
643 END ;
644
645 unaryT = RECORD
646 arg,
647 resultType: node ;
648 END ;
649
650 moduleT = RECORD
651 name : Name ;
652 source : Name ;
653 importedModules : Index ;
654 constFixup,
655 enumFixup : fixupInfo ;
656 decls : scopeT ;
657 beginStatements,
658 finallyStatements: node ;
659 enumsComplete,
660 constsComplete,
661 visited : BOOLEAN ;
662 com : commentPair ;
663 END ;
664
665 defT = RECORD
666 name : Name ;
667 source : Name ;
668 unqualified,
669 hasHidden,
670 forC : BOOLEAN ;
671 exported,
672 importedModules : Index ;
673 constFixup,
674 enumFixup : fixupInfo ;
675 decls : scopeT ;
676 enumsComplete,
677 constsComplete,
678 visited : BOOLEAN ;
679 com : commentPair ;
680 END ;
681
682 impT = RECORD
683 name : Name ;
684 source : Name ;
685 importedModules : Index ;
686 constFixup,
687 enumFixup : fixupInfo ;
688 beginStatements,
689 finallyStatements: node ;
690 definitionModule : node ;
691 decls : scopeT ;
692 enumsComplete,
693 constsComplete,
694 visited : BOOLEAN ;
695 com : commentPair ;
696 END ;
697
698 where = RECORD
699 defDeclared,
700 modDeclared,
701 firstUsed : CARDINAL ;
702 END ;
703
704 outputStates = (text, punct, space) ;
705
706 nodeProcedure = PROCEDURE (node) ;
707
708 dependentState = (completed, blocked, partial, recursive) ;
709
710 cnameT = RECORD
711 name : Name ;
712 init : BOOLEAN ;
713 END ;
714
715 group = POINTER TO RECORD
716 todoQ,
717 partialQ,
718 doneQ : alist ;
719 next : group ;
720 END ;
721
722
723 VAR
724 freeGroup,
725 globalGroup : group ; (* The global group of all alists. *)
726 outputFile : File ;
727 lang : language ;
728 charStarN,
729 constCharStarN,
730 bitsperunitN,
731 bitsperwordN,
732 bitspercharN,
733 unitsperwordN,
734 mainModule,
735 currentModule,
736 defModule,
737 systemN,
738 addressN,
739 locN,
740 byteN,
741 wordN,
742 csizetN,
743 cssizetN,
744 cofftN,
745 cardinal64N,
746 adrN,
747 sizeN,
748 tsizeN,
749 newN,
750 disposeN,
751 lengthN,
752 incN,
753 decN,
754 inclN,
755 exclN,
756 highN,
757 m2rtsN,
758 haltN,
759 throwN,
760 chrN,
761 capN,
762 absN,
763 floatN,
764 truncN,
765 ordN,
766 valN,
767 minN,
768 maxN,
769 booleanN,
770 procN,
771 charN,
772 integerN,
773 cardinalN,
774 longcardN,
775 shortcardN,
776 longintN,
777 shortintN,
778 bitsetN,
779 bitnumN,
780 ztypeN,
781 rtypeN,
782 complexN,
783 longcomplexN,
784 shortcomplexN,
785 cmplxN,
786 reN,
787 imN,
788 realN,
789 longrealN,
790 shortrealN,
791 nilN,
792 trueN,
793 falseN : node ;
794 scopeStack,
795 defUniverseI,
796 modUniverseI : Index ;
797 modUniverse,
798 defUniverse : symbolTree ;
799 baseSymbols : symbolTree ;
800 outputState : outputStates ;
801 doP : pretty ;
802 mustVisitScope,
803 simplified : BOOLEAN ;
804 tempCount : CARDINAL ;
805
806
807 (*
808 newNode - create and return a new node of kind k.
809 *)
810
811 PROCEDURE newNode (k: nodeT) : node ;
812 VAR
813 d: node ;
814 BEGIN
815 NEW (d) ;
816 IF enableMemsetOnAllocation
817 THEN
818 d := memset (d, 0, SIZE (d^))
819 END ;
820 IF d=NIL
821 THEN
822 HALT
823 ELSE
824 d^.kind := k ;
825 d^.at.defDeclared := 0 ;
826 d^.at.modDeclared := 0 ;
827 d^.at.firstUsed := 0 ;
828 RETURN d
829 END
830 END newNode ;
831
832
833 (*
834 disposeNode - dispose node, n.
835 *)
836
837 PROCEDURE disposeNode (VAR n: node) ;
838 BEGIN
839 DISPOSE (n) ;
840 n := NIL
841 END disposeNode ;
842
843
844 (*
845 newGroup -
846 *)
847
848 PROCEDURE newGroup (VAR g: group) ;
849 BEGIN
850 IF freeGroup = NIL
851 THEN
852 NEW (g)
853 ELSE
854 g := freeGroup ;
855 freeGroup := freeGroup^.next
856 END
857 END newGroup ;
858
859
860 (*
861 initGroup - returns a group which with all lists initialized.
862 *)
863
864 PROCEDURE initGroup () : group ;
865 VAR
866 g: group ;
867 BEGIN
868 newGroup (g) ;
869 WITH g^ DO
870 todoQ := alists.initList () ;
871 partialQ := alists.initList () ;
872 doneQ := alists.initList () ;
873 next := NIL
874 END ;
875 RETURN g
876 END initGroup ;
877
878
879 (*
880 killGroup - deallocate the group and place the group record into the freeGroup list.
881 *)
882
883 PROCEDURE killGroup (VAR g: group) ;
884 BEGIN
885 alists.killList (g^.todoQ) ;
886 alists.killList (g^.partialQ) ;
887 alists.killList (g^.doneQ) ;
888 g^.next := freeGroup ;
889 freeGroup := g ;
890 END killGroup ;
891
892
893 (*
894 dupGroup - If g is not NIL then destroy g.
895 Return a duplicate of GlobalGroup (not g).
896 *)
897
898 PROCEDURE dupGroup (g: group) : group ;
899 BEGIN
900 IF g # NIL
901 THEN
902 (* Kill old group. *)
903 killGroup (g)
904 END ;
905 newGroup (g) ;
906 WITH g^ DO
907 (* Copy all lists. *)
908 todoQ := alists.duplicateList (globalGroup^.todoQ) ;
909 partialQ := alists.duplicateList (globalGroup^.partialQ) ;
910 doneQ := alists.duplicateList (globalGroup^.doneQ) ;
911 next := NIL
912 END ;
913 RETURN g
914 END dupGroup ;
915
916
917 (*
918 equalGroup - return TRUE if group left = right.
919 *)
920
921 PROCEDURE equalGroup (left, right: group) : BOOLEAN ;
922 BEGIN
923 RETURN ((left = right) OR
924 (alists.equalList (left^.todoQ, right^.todoQ) AND
925 alists.equalList (left^.partialQ, right^.partialQ) AND
926 alists.equalList (left^.doneQ, right^.doneQ)))
927 END equalGroup ;
928
929
930 (*
931 getDeclaredDef - returns the token number associated with the nodes declaration
932 in the definition module.
933 *)
934
935 PROCEDURE getDeclaredDef (n: node) : CARDINAL ;
936 BEGIN
937 RETURN n^.at.defDeclared
938 END getDeclaredDef ;
939
940
941 (*
942 getDeclaredMod - returns the token number associated with the nodes declaration
943 in the implementation or program module.
944 *)
945
946 PROCEDURE getDeclaredMod (n: node) : CARDINAL ;
947 BEGIN
948 RETURN n^.at.modDeclared
949 END getDeclaredMod ;
950
951
952 (*
953 getFirstUsed - returns the token number associated with the first use of
954 node, n.
955 *)
956
957 PROCEDURE getFirstUsed (n: node) : CARDINAL ;
958 BEGIN
959 RETURN n^.at.firstUsed
960 END getFirstUsed ;
961
962
963 (*
964 setVisited - set the visited flag on a def/imp/module node.
965 *)
966
967 PROCEDURE setVisited (n: node) ;
968 BEGIN
969 CASE n^.kind OF
970
971 def : n^.defF.visited := TRUE |
972 imp : n^.impF.visited := TRUE |
973 module: n^.moduleF.visited := TRUE
974
975 END
976 END setVisited ;
977
978
979 (*
980 unsetVisited - unset the visited flag on a def/imp/module node.
981 *)
982
983 PROCEDURE unsetVisited (n: node) ;
984 BEGIN
985 CASE n^.kind OF
986
987 def : n^.defF.visited := FALSE |
988 imp : n^.impF.visited := FALSE |
989 module: n^.moduleF.visited := FALSE
990
991 END
992 END unsetVisited ;
993
994
995 (*
996 isVisited - returns TRUE if the node was visited.
997 *)
998
999 PROCEDURE isVisited (n: node) : BOOLEAN ;
1000 BEGIN
1001 CASE n^.kind OF
1002
1003 def : RETURN n^.defF.visited |
1004 imp : RETURN n^.impF.visited |
1005 module: RETURN n^.moduleF.visited
1006
1007 END
1008 END isVisited ;
1009
1010
1011 (*
1012 isDef - return TRUE if node, n, is a definition module.
1013 *)
1014
1015 PROCEDURE isDef (n: node) : BOOLEAN ;
1016 BEGIN
1017 assert (n#NIL) ;
1018 RETURN n^.kind = def
1019 END isDef ;
1020
1021
1022 (*
1023 isImp - return TRUE if node, n, is an implementation module.
1024 *)
1025
1026 PROCEDURE isImp (n: node) : BOOLEAN ;
1027 BEGIN
1028 assert (n#NIL) ;
1029 RETURN n^.kind = imp
1030 END isImp ;
1031
1032
1033 (*
1034 isModule - return TRUE if node, n, is a program module.
1035 *)
1036
1037 PROCEDURE isModule (n: node) : BOOLEAN ;
1038 BEGIN
1039 assert (n#NIL) ;
1040 RETURN n^.kind = module
1041 END isModule ;
1042
1043
1044 (*
1045 isImpOrModule - returns TRUE if, n, is a program module or implementation module.
1046 *)
1047
1048 PROCEDURE isImpOrModule (n: node) : BOOLEAN ;
1049 BEGIN
1050 RETURN isImp (n) OR isModule (n)
1051 END isImpOrModule ;
1052
1053
1054 (*
1055 isProcedure - returns TRUE if node, n, is a procedure.
1056 *)
1057
1058 PROCEDURE isProcedure (n: node) : BOOLEAN ;
1059 BEGIN
1060 assert (n#NIL) ;
1061 RETURN n^.kind = procedure
1062 END isProcedure ;
1063
1064
1065 (*
1066 isConst - returns TRUE if node, n, is a const.
1067 *)
1068
1069 PROCEDURE isConst (n: node) : BOOLEAN ;
1070 BEGIN
1071 assert (n#NIL) ;
1072 RETURN n^.kind = const
1073 END isConst ;
1074
1075
1076 (*
1077 isType - returns TRUE if node, n, is a type.
1078 *)
1079
1080 PROCEDURE isType (n: node) : BOOLEAN ;
1081 BEGIN
1082 assert (n#NIL) ;
1083 RETURN n^.kind = type
1084 END isType ;
1085
1086
1087 (*
1088 isVar - returns TRUE if node, n, is a type.
1089 *)
1090
1091 PROCEDURE isVar (n: node) : BOOLEAN ;
1092 BEGIN
1093 assert (n#NIL) ;
1094 RETURN n^.kind = var
1095 END isVar ;
1096
1097
1098 (*
1099 isTemporary - returns TRUE if node, n, is a variable and temporary.
1100 *)
1101
1102 PROCEDURE isTemporary (n: node) : BOOLEAN ;
1103 BEGIN
1104 RETURN FALSE
1105 END isTemporary ;
1106
1107
1108 (*
1109 isExported - returns TRUE if symbol, n, is exported from
1110 the definition module.
1111 *)
1112
1113 PROCEDURE isExported (n: node) : BOOLEAN ;
1114 VAR
1115 s: node ;
1116 BEGIN
1117 s := getScope (n) ;
1118 IF s#NIL
1119 THEN
1120 CASE s^.kind OF
1121
1122 def: RETURN IsIndiceInIndex (s^.defF.exported, n)
1123
1124 ELSE
1125 RETURN FALSE
1126 END
1127 END ;
1128 RETURN FALSE
1129 END isExported ;
1130
1131
1132 (*
1133 isLocal - returns TRUE if symbol, n, is locally declared in a procedure.
1134 *)
1135
1136 PROCEDURE isLocal (n: node) : BOOLEAN ;
1137 VAR
1138 s: node ;
1139 BEGIN
1140 s := getScope (n) ;
1141 IF s#NIL
1142 THEN
1143 RETURN isProcedure (s)
1144 END ;
1145 RETURN FALSE
1146 END isLocal ;
1147
1148
1149 (*
1150 lookupExported - attempts to lookup a node named, i, from definition
1151 module, n. The node is returned if found.
1152 NIL is returned if not found.
1153 *)
1154
1155 PROCEDURE lookupExported (n: node; i: Name) : node ;
1156 VAR
1157 r: node ;
1158 BEGIN
1159 assert (isDef (n)) ;
1160 r := getSymKey (n^.defF.decls.symbols, i) ;
1161 IF (r#NIL) AND isExported (r)
1162 THEN
1163 RETURN r
1164 END ;
1165 RETURN NIL
1166 END lookupExported ;
1167
1168
1169 (*
1170 importEnumFields - if, n, is an enumeration type import the all fields into module, m.
1171 *)
1172
1173 PROCEDURE importEnumFields (m, n: node) ;
1174 VAR
1175 r, e: node ;
1176 i, h: CARDINAL ;
1177 BEGIN
1178 assert (isDef (m) OR isModule (m) OR isImp (m)) ;
1179 n := skipType (n) ;
1180 IF (n#NIL) AND isEnumeration (n)
1181 THEN
1182 i := LowIndice (n^.enumerationF.listOfSons) ;
1183 h := HighIndice (n^.enumerationF.listOfSons) ;
1184 WHILE i<=h DO
1185 e := GetIndice (n^.enumerationF.listOfSons, i) ;
1186 r := import (m, e) ;
1187 IF e#r
1188 THEN
1189 metaError2 ('enumeration field {%1ad} cannot be imported implicitly into {%2d} due to a name clash',
1190 e, m)
1191 END ;
1192 INC (i)
1193 END
1194 END
1195 END importEnumFields ;
1196
1197
1198 (*
1199 checkGccType - check to see if node n is gcc tree or location_t
1200 and record its use in keyc.
1201 *)
1202
1203 PROCEDURE checkGccType (n: node) ;
1204 BEGIN
1205 IF getGccConfigSystem () AND (getScope (n) # NIL) AND
1206 (getSymName (getScope (n)) = makeKey ('gcctypes'))
1207 THEN
1208 IF getSymName (n) = makeKey ('location_t')
1209 THEN
1210 keyc.useGccLocation
1211 ELSIF getSymName (n) = makeKey ('tree')
1212 THEN
1213 keyc.useGccTree
1214 END
1215 END
1216 END checkGccType ;
1217
1218
1219 (*
1220 checkCDataTypes - check to see if node n is CharStar or ConstCharStar
1221 and if necessary assign n to the global variable.
1222 *)
1223
1224 PROCEDURE checkCDataTypes (n: node) ;
1225 BEGIN
1226 IF (getScope (n) # NIL) AND (getSymName (getScope (n)) = makeKey ('CDataTypes'))
1227 THEN
1228 IF getSymName (n) = makeKey ('CharStar')
1229 THEN
1230 charStarN := n
1231 ELSIF getSymName (n) = makeKey ('ConstCharStar')
1232 THEN
1233 constCharStarN := n
1234 END
1235 END
1236 END checkCDataTypes ;
1237
1238
1239 (*
1240 import - attempts to add node, n, into the scope of module, m.
1241 It might fail due to a name clash in which case the
1242 previous named symbol is returned. On success, n,
1243 is returned.
1244 *)
1245
1246 PROCEDURE import (m, n: node) : node ;
1247 VAR
1248 name: Name ;
1249 r : node ;
1250 BEGIN
1251 assert (isDef (m) OR isModule (m) OR isImp (m)) ;
1252 name := getSymName (n) ;
1253 checkGccType (n) ;
1254 checkCDataTypes (n) ;
1255 r := lookupInScope (m, name) ;
1256 IF r=NIL
1257 THEN
1258 CASE m^.kind OF
1259
1260 def : putSymKey (m^.defF.decls.symbols, name, n) |
1261 imp : putSymKey (m^.impF.decls.symbols, name, n) |
1262 module: putSymKey (m^.moduleF.decls.symbols, name, n)
1263
1264 END ;
1265 importEnumFields (m, n) ;
1266 RETURN n
1267 END ;
1268 RETURN r
1269 END import ;
1270
1271
1272 (*
1273 isZtype - returns TRUE if, n, is the Z type.
1274 *)
1275
1276 PROCEDURE isZtype (n: node) : BOOLEAN ;
1277 BEGIN
1278 RETURN n = ztypeN
1279 END isZtype ;
1280
1281
1282 (*
1283 isRtype - returns TRUE if, n, is the R type.
1284 *)
1285
1286 PROCEDURE isRtype (n: node) : BOOLEAN ;
1287 BEGIN
1288 RETURN n = rtypeN
1289 END isRtype ;
1290
1291
1292 (*
1293 isComplex - returns TRUE if, n, is the complex type.
1294 *)
1295
1296 PROCEDURE isComplex (n: node) : BOOLEAN ;
1297 BEGIN
1298 RETURN n = complexN
1299 END isComplex ;
1300
1301
1302 (*
1303 isLongComplex - returns TRUE if, n, is the longcomplex type.
1304 *)
1305
1306 PROCEDURE isLongComplex (n: node) : BOOLEAN ;
1307 BEGIN
1308 RETURN n = longcomplexN
1309 END isLongComplex ;
1310
1311
1312 (*
1313 isShortComplex - returns TRUE if, n, is the shortcomplex type.
1314 *)
1315
1316 PROCEDURE isShortComplex (n: node) : BOOLEAN ;
1317 BEGIN
1318 RETURN n = shortcomplexN
1319 END isShortComplex ;
1320
1321
1322 (*
1323 isLiteral - returns TRUE if, n, is a literal.
1324 *)
1325
1326 PROCEDURE isLiteral (n: node) : BOOLEAN ;
1327 BEGIN
1328 assert (n # NIL) ;
1329 RETURN n^.kind = literal
1330 END isLiteral ;
1331
1332
1333 (*
1334 isConstSet - returns TRUE if, n, is a constant set.
1335 *)
1336
1337 PROCEDURE isConstSet (n: node) : BOOLEAN ;
1338 BEGIN
1339 assert (n # NIL) ;
1340 IF isLiteral (n) OR isConst (n)
1341 THEN
1342 RETURN isSet (skipType (getType (n)))
1343 END ;
1344 RETURN FALSE
1345 END isConstSet ;
1346
1347
1348 (*
1349 isEnumerationField - returns TRUE if, n, is an enumeration field.
1350 *)
1351
1352 PROCEDURE isEnumerationField (n: node) : BOOLEAN ;
1353 BEGIN
1354 assert (n # NIL) ;
1355 RETURN n^.kind = enumerationfield
1356 END isEnumerationField ;
1357
1358
1359 (*
1360 isUnbounded - returns TRUE if, n, is an unbounded array.
1361 *)
1362
1363 PROCEDURE isUnbounded (n: node) : BOOLEAN ;
1364 BEGIN
1365 assert (n # NIL) ;
1366 RETURN (n^.kind = array) AND (n^.arrayF.isUnbounded)
1367 END isUnbounded ;
1368
1369
1370 (*
1371 isParameter - returns TRUE if, n, is a parameter.
1372 *)
1373
1374 PROCEDURE isParameter (n: node) : BOOLEAN ;
1375 BEGIN
1376 assert (n # NIL) ;
1377 RETURN (n^.kind = param) OR (n^.kind = varparam)
1378 END isParameter ;
1379
1380
1381 (*
1382 isVarParam - returns TRUE if, n, is a var parameter.
1383 *)
1384
1385 PROCEDURE isVarParam (n: node) : BOOLEAN ;
1386 BEGIN
1387 assert (n # NIL) ;
1388 RETURN n^.kind = varparam
1389 END isVarParam ;
1390
1391
1392 (*
1393 isParam - returns TRUE if, n, is a non var parameter.
1394 *)
1395
1396 PROCEDURE isParam (n: node) : BOOLEAN ;
1397 BEGIN
1398 assert (n # NIL) ;
1399 RETURN n^.kind = param
1400 END isParam ;
1401
1402
1403 (*
1404 isNonVarParam - is an alias to isParam.
1405 *)
1406
1407 PROCEDURE isNonVarParam (n: node) : BOOLEAN ;
1408 BEGIN
1409 RETURN isParam (n)
1410 END isNonVarParam ;
1411
1412
1413 (*
1414 isRecord - returns TRUE if, n, is a record.
1415 *)
1416
1417 PROCEDURE isRecord (n: node) : BOOLEAN ;
1418 BEGIN
1419 assert (n # NIL) ;
1420 RETURN n^.kind = record
1421 END isRecord ;
1422
1423
1424 (*
1425 isRecordField - returns TRUE if, n, is a record field.
1426 *)
1427
1428 PROCEDURE isRecordField (n: node) : BOOLEAN ;
1429 BEGIN
1430 assert (n # NIL) ;
1431 RETURN n^.kind = recordfield
1432 END isRecordField ;
1433
1434
1435 (*
1436 isArray - returns TRUE if, n, is an array.
1437 *)
1438
1439 PROCEDURE isArray (n: node) : BOOLEAN ;
1440 BEGIN
1441 assert (n # NIL) ;
1442 RETURN n^.kind = array
1443 END isArray ;
1444
1445
1446 (*
1447 isProcType - returns TRUE if, n, is a procedure type.
1448 *)
1449
1450 PROCEDURE isProcType (n: node) : BOOLEAN ;
1451 BEGIN
1452 assert (n # NIL) ;
1453 RETURN n^.kind = proctype
1454 END isProcType ;
1455
1456
1457 (*
1458 isAProcType - returns TRUE if, n, is a proctype or proc node.
1459 *)
1460
1461 PROCEDURE isAProcType (n: node) : BOOLEAN ;
1462 BEGIN
1463 assert (n # NIL) ;
1464 RETURN isProcType (n) OR (n = procN)
1465 END isAProcType ;
1466
1467
1468 (*
1469 isPointer - returns TRUE if, n, is a pointer.
1470 *)
1471
1472 PROCEDURE isPointer (n: node) : BOOLEAN ;
1473 BEGIN
1474 assert (n # NIL) ;
1475 RETURN n^.kind = pointer
1476 END isPointer ;
1477
1478
1479 (*
1480 isVarient - returns TRUE if, n, is a varient record.
1481 *)
1482
1483 PROCEDURE isVarient (n: node) : BOOLEAN ;
1484 BEGIN
1485 assert (n # NIL) ;
1486 RETURN n^.kind = varient
1487 END isVarient ;
1488
1489
1490 (*
1491 isVarientField - returns TRUE if, n, is a varient field.
1492 *)
1493
1494 PROCEDURE isVarientField (n: node) : BOOLEAN ;
1495 BEGIN
1496 assert (n # NIL) ;
1497 RETURN n^.kind = varientfield
1498 END isVarientField ;
1499
1500
1501 (*
1502 isSet - returns TRUE if, n, is a set type.
1503 *)
1504
1505 PROCEDURE isSet (n: node) : BOOLEAN ;
1506 BEGIN
1507 assert (n # NIL) ;
1508 RETURN n^.kind = set
1509 END isSet ;
1510
1511
1512 (*
1513 isSubrange - returns TRUE if, n, is a subrange type.
1514 *)
1515
1516 PROCEDURE isSubrange (n: node) : BOOLEAN ;
1517 BEGIN
1518 assert (n # NIL) ;
1519 RETURN n^.kind = subrange
1520 END isSubrange ;
1521
1522
1523 (*
1524 isMainModule - return TRUE if node, n, is the main module specified
1525 by the source file. This might be a definition,
1526 implementation or program module.
1527 *)
1528
1529 PROCEDURE isMainModule (n: node) : BOOLEAN ;
1530 BEGIN
1531 assert (n#NIL) ;
1532 RETURN n = mainModule
1533 END isMainModule ;
1534
1535
1536 (*
1537 setMainModule - sets node, n, as the main module to be compiled.
1538 *)
1539
1540 PROCEDURE setMainModule (n: node) ;
1541 BEGIN
1542 assert (n#NIL) ;
1543 mainModule := n
1544 END setMainModule ;
1545
1546
1547 (*
1548 getMainModule - returns the main module node.
1549 *)
1550
1551 PROCEDURE getMainModule () : node ;
1552 BEGIN
1553 RETURN mainModule
1554 END getMainModule ;
1555
1556
1557 (*
1558 setCurrentModule - sets node, n, as the current module being compiled.
1559 *)
1560
1561 PROCEDURE setCurrentModule (n: node) ;
1562 BEGIN
1563 assert (n#NIL) ;
1564 currentModule := n
1565 END setCurrentModule ;
1566
1567
1568 (*
1569 getCurrentModule - returns the current module being compiled.
1570 *)
1571
1572 PROCEDURE getCurrentModule () : node ;
1573 BEGIN
1574 RETURN currentModule
1575 END getCurrentModule ;
1576
1577
1578 (*
1579 initFixupInfo - initialize the fixupInfo record.
1580 *)
1581
1582 PROCEDURE initFixupInfo () : fixupInfo ;
1583 VAR
1584 f: fixupInfo ;
1585 BEGIN
1586 f.count := 0 ;
1587 f.info := InitIndex (1) ;
1588 RETURN f
1589 END initFixupInfo ;
1590
1591
1592 (*
1593 makeDef - returns a definition module node named, n.
1594 *)
1595
1596 PROCEDURE makeDef (n: Name) : node ;
1597 VAR
1598 d: node ;
1599 BEGIN
1600 d := newNode (def) ;
1601 WITH d^ DO
1602 defF.name := n ;
1603 defF.source := NulName ;
1604 defF.hasHidden := FALSE ;
1605 defF.forC := FALSE ;
1606 defF.unqualified := FALSE ;
1607 defF.exported := InitIndex (1) ;
1608 defF.importedModules := InitIndex (1) ;
1609 defF.constFixup := initFixupInfo () ;
1610 defF.enumFixup := initFixupInfo () ;
1611 initDecls (defF.decls) ;
1612 defF.enumsComplete := FALSE ;
1613 defF.constsComplete := FALSE ;
1614 defF.visited := FALSE ;
1615 initPair (defF.com)
1616 END ;
1617 RETURN d
1618 END makeDef ;
1619
1620
1621 (*
1622 makeImp - returns an implementation module node named, n.
1623 *)
1624
1625 PROCEDURE makeImp (n: Name) : node ;
1626 VAR
1627 d: node ;
1628 BEGIN
1629 d := newNode (imp) ;
1630 WITH d^ DO
1631 impF.name := n ;
1632 impF.source := NulName ;
1633 impF.importedModules := InitIndex (1) ;
1634 impF.constFixup := initFixupInfo () ;
1635 impF.enumFixup := initFixupInfo () ;
1636 initDecls (impF.decls) ;
1637 impF.beginStatements := NIL ;
1638 impF.finallyStatements := NIL ;
1639 impF.definitionModule := NIL ;
1640 impF.enumsComplete := FALSE ;
1641 impF.constsComplete := FALSE ;
1642 impF.visited := FALSE ;
1643 initPair (impF.com)
1644 END ;
1645 RETURN d
1646 END makeImp ;
1647
1648
1649 (*
1650 makeModule - returns a module node named, n.
1651 *)
1652
1653 PROCEDURE makeModule (n: Name) : node ;
1654 VAR
1655 d: node ;
1656 BEGIN
1657 d := newNode (module) ;
1658 WITH d^ DO
1659 moduleF.name := n ;
1660 moduleF.source := NulName ;
1661 moduleF.importedModules := InitIndex (1) ;
1662 moduleF.constFixup := initFixupInfo () ;
1663 moduleF.enumFixup := initFixupInfo () ;
1664 initDecls (moduleF.decls) ;
1665 moduleF.beginStatements := NIL ;
1666 moduleF.finallyStatements := NIL ;
1667 moduleF.enumsComplete := FALSE ;
1668 moduleF.constsComplete := FALSE ;
1669 moduleF.visited := FALSE ;
1670 initPair (moduleF.com)
1671 END ;
1672 RETURN d
1673 END makeModule ;
1674
1675
1676 (*
1677 putDefForC - the definition module was defined FOR "C".
1678 *)
1679
1680 PROCEDURE putDefForC (n: node) ;
1681 BEGIN
1682 assert (isDef (n)) ;
1683 n^.defF.forC := TRUE
1684 END putDefForC ;
1685
1686
1687 (*
1688 isDefForC - returns TRUE if the definition module was defined FOR "C".
1689 *)
1690
1691 PROCEDURE isDefForC (n: node) : BOOLEAN ;
1692 BEGIN
1693 RETURN isDef (n) AND n^.defF.forC
1694 END isDefForC ;
1695
1696
1697 (*
1698 putDefUnqualified - the definition module uses unqualified.
1699 *)
1700
1701 PROCEDURE putDefUnqualified (n: node) ;
1702 BEGIN
1703 assert (isDef (n)) ;
1704 (* Currently (and this is a temporary development restriction to
1705 reduce any search space for bugs) the only module which can be
1706 export unqualified is gcctypes. *)
1707 IF n^.defF.name = makeKey ('gcctypes')
1708 THEN
1709 n^.defF.unqualified := TRUE
1710 END
1711 END putDefUnqualified ;
1712
1713
1714 (*
1715 isDefUnqualified - returns TRUE if the definition module uses unqualified.
1716 *)
1717
1718 PROCEDURE isDefUnqualified (n: node) : BOOLEAN ;
1719 BEGIN
1720 RETURN isDef (n) AND n^.defF.unqualified
1721 END isDefUnqualified ;
1722
1723
1724 (*
1725 lookupDef - returns a definition module node named, n.
1726 *)
1727
1728 PROCEDURE lookupDef (n: Name) : node ;
1729 VAR
1730 d: node ;
1731 BEGIN
1732 d := getSymKey (defUniverse, n) ;
1733 IF d=NIL
1734 THEN
1735 d := makeDef (n) ;
1736 putSymKey (defUniverse, n, d) ;
1737 IncludeIndiceIntoIndex (defUniverseI, d)
1738 END ;
1739 RETURN d
1740 END lookupDef ;
1741
1742
1743 (*
1744 lookupImp - returns an implementation module node named, n.
1745 *)
1746
1747 PROCEDURE lookupImp (n: Name) : node ;
1748 VAR
1749 m: node ;
1750 BEGIN
1751 m := getSymKey (modUniverse, n) ;
1752 IF m=NIL
1753 THEN
1754 m := makeImp (n) ;
1755 putSymKey (modUniverse, n, m) ;
1756 IncludeIndiceIntoIndex (modUniverseI, m)
1757 END ;
1758 assert (NOT isModule (m)) ;
1759 RETURN m
1760 END lookupImp ;
1761
1762
1763 (*
1764 lookupModule - returns a module node named, n.
1765 *)
1766
1767 PROCEDURE lookupModule (n: Name) : node ;
1768 VAR
1769 m: node ;
1770 BEGIN
1771 m := getSymKey (modUniverse, n) ;
1772 IF m=NIL
1773 THEN
1774 m := makeModule (n) ;
1775 putSymKey (modUniverse, n, m) ;
1776 IncludeIndiceIntoIndex (modUniverseI, m)
1777 END ;
1778 assert (NOT isImp (m)) ;
1779 RETURN m
1780 END lookupModule ;
1781
1782
1783 (*
1784 setSource - sets the source filename for module, n, to s.
1785 *)
1786
1787 PROCEDURE setSource (n: node; s: Name) ;
1788 BEGIN
1789 WITH n^ DO
1790 CASE kind OF
1791
1792 def : defF.source := s |
1793 module: moduleF.source := s |
1794 imp : impF.source := s
1795
1796 END
1797 END
1798 END setSource ;
1799
1800
1801 (*
1802 getSource - returns the source filename for module, n.
1803 *)
1804
1805 PROCEDURE getSource (n: node) : Name ;
1806 BEGIN
1807 WITH n^ DO
1808 CASE kind OF
1809
1810 def : RETURN defF.source |
1811 module: RETURN moduleF.source |
1812 imp : RETURN impF.source
1813
1814 END
1815 END
1816 END getSource ;
1817
1818
1819 (*
1820 initDecls - initialize the decls, scopeT.
1821 *)
1822
1823 PROCEDURE initDecls (VAR decls: scopeT) ;
1824 BEGIN
1825 decls.symbols := initTree () ;
1826 decls.constants := InitIndex (1) ;
1827 decls.types := InitIndex (1) ;
1828 decls.procedures := InitIndex (1) ;
1829 decls.variables := InitIndex (1)
1830 END initDecls ;
1831
1832
1833 (*
1834 enterScope - pushes symbol, n, to the scope stack.
1835 *)
1836
1837 PROCEDURE enterScope (n: node) ;
1838 BEGIN
1839 IF IsIndiceInIndex (scopeStack, n)
1840 THEN
1841 HALT
1842 ELSE
1843 IncludeIndiceIntoIndex (scopeStack, n)
1844 END ;
1845 IF debugScopes
1846 THEN
1847 printf ("enter scope\n") ;
1848 dumpScopes
1849 END
1850 END enterScope ;
1851
1852
1853 (*
1854 leaveScope - removes the top level scope.
1855 *)
1856
1857 PROCEDURE leaveScope ;
1858 VAR
1859 i: CARDINAL ;
1860 n: node ;
1861 BEGIN
1862 i := HighIndice (scopeStack) ;
1863 n := GetIndice (scopeStack, i) ;
1864 RemoveIndiceFromIndex (scopeStack, n) ;
1865 IF debugScopes
1866 THEN
1867 printf ("leave scope\n") ;
1868 dumpScopes
1869 END
1870 END leaveScope ;
1871
1872
1873 (*
1874 getDeclScope - returns the node representing the
1875 current declaration scope.
1876 *)
1877
1878 PROCEDURE getDeclScope () : node ;
1879 VAR
1880 i: CARDINAL ;
1881 BEGIN
1882 i := HighIndice (scopeStack) ;
1883 RETURN GetIndice (scopeStack, i)
1884 END getDeclScope ;
1885
1886
1887 (*
1888 addTo - adds node, d, to scope decls and returns, d.
1889 It stores, d, in the symbols tree associated with decls.
1890 *)
1891
1892 PROCEDURE addTo (VAR decls: scopeT; d: node) : node ;
1893 VAR
1894 n: Name ;
1895 BEGIN
1896 n := getSymName (d) ;
1897 IF n#NulName
1898 THEN
1899 IF getSymKey (decls.symbols, n)=NIL
1900 THEN
1901 putSymKey (decls.symbols, n, d)
1902 ELSE
1903 metaError1 ('{%1DMad} was declared', d) ;
1904 metaError1 ('{%1k} and is being declared again', n)
1905 END
1906 END ;
1907 IF isConst (d)
1908 THEN
1909 IncludeIndiceIntoIndex (decls.constants, d)
1910 ELSIF isVar (d)
1911 THEN
1912 IncludeIndiceIntoIndex (decls.variables, d)
1913 ELSIF isType (d)
1914 THEN
1915 IncludeIndiceIntoIndex (decls.types, d)
1916 ELSIF isProcedure (d)
1917 THEN
1918 IncludeIndiceIntoIndex (decls.procedures, d) ;
1919 IF debugDecl
1920 THEN
1921 printf ("%d procedures on the dynamic array\n",
1922 HighIndice (decls.procedures))
1923 END
1924 END ;
1925 RETURN d
1926 END addTo ;
1927
1928
1929 (*
1930 export - export node, n, from definition module, d.
1931 *)
1932
1933 PROCEDURE export (d, n: node) ;
1934 BEGIN
1935 assert (isDef (d)) ;
1936 IncludeIndiceIntoIndex (d^.defF.exported, n)
1937 END export ;
1938
1939
1940 (*
1941 addToScope - adds node, n, to the current scope and returns, n.
1942 *)
1943
1944 PROCEDURE addToScope (n: node) : node ;
1945 VAR
1946 s: node ;
1947 i: CARDINAL ;
1948 BEGIN
1949 i := HighIndice (scopeStack) ;
1950 s := GetIndice (scopeStack, i) ;
1951 IF isProcedure (s)
1952 THEN
1953 IF debugDecl
1954 THEN
1955 outText (doP, "adding ") ;
1956 doNameC (doP, n) ;
1957 outText (doP, " to procedure\n")
1958 END ;
1959 RETURN addTo (s^.procedureF.decls, n)
1960 ELSIF isModule (s)
1961 THEN
1962 IF debugDecl
1963 THEN
1964 outText (doP, "adding ") ;
1965 doNameC (doP, n) ;
1966 outText (doP, " to module\n")
1967 END ;
1968 RETURN addTo (s^.moduleF.decls, n)
1969 ELSIF isDef (s)
1970 THEN
1971 IF debugDecl
1972 THEN
1973 outText (doP, "adding ") ;
1974 doNameC (doP, n) ;
1975 outText (doP, " to definition module\n")
1976 END ;
1977 export (s, n) ;
1978 RETURN addTo (s^.defF.decls, n)
1979 ELSIF isImp (s)
1980 THEN
1981 IF debugDecl
1982 THEN
1983 outText (doP, "adding ") ;
1984 doNameC (doP, n) ;
1985 outText (doP, " to implementation module\n")
1986 END ;
1987 RETURN addTo (s^.impF.decls, n)
1988 END ;
1989 HALT
1990 END addToScope ;
1991
1992
1993 (*
1994 addModuleToScope - adds module, i, to module, m, scope.
1995 *)
1996
1997 PROCEDURE addModuleToScope (m, i: node) ;
1998 BEGIN
1999 assert (getDeclScope () = m) ;
2000 IF lookupSym (getSymName (i))=NIL
2001 THEN
2002 i := addToScope (i)
2003 END
2004 END addModuleToScope ;
2005
2006
2007 (*
2008 addImportedModule - add module, i, to be imported by, m.
2009 If scoped then module, i, is added to the
2010 module, m, scope.
2011 *)
2012
2013 PROCEDURE addImportedModule (m, i: node; scoped: BOOLEAN) ;
2014 BEGIN
2015 assert (isDef (i) OR isModule (i)) ;
2016 IF isDef (m)
2017 THEN
2018 IncludeIndiceIntoIndex (m^.defF.importedModules, i)
2019 ELSIF isImp (m)
2020 THEN
2021 IncludeIndiceIntoIndex (m^.impF.importedModules, i)
2022 ELSIF isModule (m)
2023 THEN
2024 IncludeIndiceIntoIndex (m^.moduleF.importedModules, i)
2025 ELSE
2026 HALT
2027 END ;
2028 IF scoped
2029 THEN
2030 addModuleToScope (m, i)
2031 END
2032 END addImportedModule ;
2033
2034
2035 (*
2036 completedEnum - assign boolean enumsComplete to TRUE if a definition,
2037 implementation or module symbol.
2038 *)
2039
2040 PROCEDURE completedEnum (n: node) ;
2041 BEGIN
2042 assert (isDef (n) OR isImp (n) OR isModule (n)) ;
2043 IF isDef (n)
2044 THEN
2045 n^.defF.enumsComplete := TRUE
2046 ELSIF isImp (n)
2047 THEN
2048 n^.impF.enumsComplete := TRUE
2049 ELSIF isModule (n)
2050 THEN
2051 n^.moduleF.enumsComplete := TRUE
2052 END
2053 END completedEnum ;
2054
2055
2056 (*
2057 setUnary - sets a unary node to contain, arg, a, and type, t.
2058 *)
2059
2060 PROCEDURE setUnary (u: node; k: nodeT; a, t: node) ;
2061 BEGIN
2062 CASE k OF
2063
2064 constexp,
2065 deref,
2066 chr,
2067 cap,
2068 abs,
2069 float,
2070 trunc,
2071 ord,
2072 high,
2073 throw,
2074 re,
2075 im,
2076 not,
2077 neg,
2078 adr,
2079 size,
2080 tsize,
2081 min,
2082 max : u^.kind := k ;
2083 u^.unaryF.arg := a ;
2084 u^.unaryF.resultType := t
2085
2086 END
2087 END setUnary ;
2088
2089
2090 (*
2091 makeConst - create, initialise and return a const node.
2092 *)
2093
2094 PROCEDURE makeConst (n: Name) : node ;
2095 VAR
2096 d: node ;
2097 BEGIN
2098 d := newNode (const) ;
2099 WITH d^ DO
2100 constF.name := n ;
2101 constF.type := NIL ;
2102 constF.scope := getDeclScope () ;
2103 constF.value := NIL
2104 END ;
2105 RETURN addToScope (d)
2106 END makeConst ;
2107
2108
2109 (*
2110 makeType - create, initialise and return a type node.
2111 *)
2112
2113 PROCEDURE makeType (n: Name) : node ;
2114 VAR
2115 d: node ;
2116 BEGIN
2117 d := newNode (type) ;
2118 WITH d^ DO
2119 typeF.name := n ;
2120 typeF.type := NIL ;
2121 typeF.scope := getDeclScope () ;
2122 typeF.isOpaque := FALSE ;
2123 typeF.isHidden := FALSE ;
2124 typeF.isInternal := FALSE
2125 END ;
2126 RETURN addToScope (d)
2127 END makeType ;
2128
2129
2130 (*
2131 makeTypeImp - lookup a type in the definition module
2132 and return it. Otherwise create a new type.
2133 *)
2134
2135 PROCEDURE makeTypeImp (n: Name) : node ;
2136 VAR
2137 d: node ;
2138 BEGIN
2139 d := lookupSym (n) ;
2140 IF d#NIL
2141 THEN
2142 d^.typeF.isHidden := FALSE ;
2143 RETURN addToScope (d)
2144 ELSE
2145 d := newNode (type) ;
2146 WITH d^ DO
2147 typeF.name := n ;
2148 typeF.type := NIL ;
2149 typeF.scope := getDeclScope () ;
2150 typeF.isOpaque := FALSE ;
2151 typeF.isHidden := FALSE
2152 END ;
2153 RETURN addToScope (d)
2154 END
2155 END makeTypeImp ;
2156
2157
2158 (*
2159 makeVar - create, initialise and return a var node.
2160 *)
2161
2162 PROCEDURE makeVar (n: Name) : node ;
2163 VAR
2164 d: node ;
2165 BEGIN
2166 d := newNode (var) ;
2167 WITH d^ DO
2168 varF.name := n ;
2169 varF.type := NIL ;
2170 varF.decl := NIL ;
2171 varF.scope := getDeclScope () ;
2172 varF.isInitialised := FALSE ;
2173 varF.isParameter := FALSE ;
2174 varF.isVarParameter := FALSE ;
2175 initCname (varF.cname)
2176 END ;
2177 RETURN addToScope (d)
2178 END makeVar ;
2179
2180
2181 (*
2182 putVar - places, type, as the type for var.
2183 *)
2184
2185 PROCEDURE putVar (var, type, decl: node) ;
2186 BEGIN
2187 assert (var#NIL) ;
2188 assert (isVar (var)) ;
2189 var^.varF.type := type ;
2190 var^.varF.decl := decl ;
2191 initNodeOpaqueState (var) ;
2192 END putVar ;
2193
2194
2195 (*
2196 putVarBool - assigns the four booleans associated with a variable.
2197 *)
2198
2199 PROCEDURE putVarBool (v: node; init, param, isvar, isused: BOOLEAN) ;
2200 BEGIN
2201 assert (isVar (v)) ;
2202 v^.varF.isInitialised := init ;
2203 v^.varF.isParameter := param ;
2204 v^.varF.isVarParameter := isvar ;
2205 v^.varF.isUsed := isused
2206 END putVarBool ;
2207
2208
2209 (*
2210 checkPtr - in C++ we need to create a typedef for a pointer
2211 in case we need to use reinterpret_cast.
2212 *)
2213
2214 PROCEDURE checkPtr (n: node) : node ;
2215 VAR
2216 s: String ;
2217 p: node ;
2218 BEGIN
2219 IF lang = ansiCP
2220 THEN
2221 IF isPointer (n)
2222 THEN
2223 s := tempName () ;
2224 p := makeType (makekey (DynamicStrings.string (s))) ;
2225 putType (p, n) ;
2226 s := KillString (s) ;
2227 RETURN p
2228 END
2229 END ;
2230 RETURN n
2231 END checkPtr ;
2232
2233
2234 (*
2235 makeVarDecl - create a vardecl node and create a shadow variable in the
2236 current scope.
2237 *)
2238
2239 PROCEDURE makeVarDecl (i: node; type: node) : node ;
2240 VAR
2241 d, v: node ;
2242 j, n: CARDINAL ;
2243 BEGIN
2244 type := checkPtr (type) ;
2245 d := newNode (vardecl) ;
2246 WITH d^ DO
2247 vardeclF.names := i^.identlistF.names ;
2248 vardeclF.type := type ;
2249 vardeclF.scope := getDeclScope ()
2250 END ;
2251 n := wlists.noOfItemsInList (d^.vardeclF.names) ;
2252 j := 1 ;
2253 WHILE j<=n DO
2254 v := lookupSym (wlists.getItemFromList (d^.vardeclF.names, j)) ;
2255 assert (isVar (v)) ;
2256 putVar (v, type, d) ;
2257 INC (j)
2258 END ;
2259 RETURN d
2260 END makeVarDecl ;
2261
2262
2263 (*
2264 isVarDecl - returns TRUE if, n, is a vardecl node.
2265 *)
2266
2267 PROCEDURE isVarDecl (n: node) : BOOLEAN ;
2268 BEGIN
2269 RETURN n^.kind = vardecl
2270 END isVarDecl ;
2271
2272
2273 (*
2274 makeVariablesFromParameters - creates variables which are really parameters.
2275 *)
2276
2277 PROCEDURE makeVariablesFromParameters (proc, id, type: node; isvar, isused: BOOLEAN) ;
2278 VAR
2279 v : node ;
2280 i, n: CARDINAL ;
2281 m : Name ;
2282 s : String ;
2283 BEGIN
2284 assert (isProcedure (proc)) ;
2285 assert (isIdentList (id)) ;
2286 i := 1 ;
2287 n := wlists.noOfItemsInList (id^.identlistF.names) ;
2288 WHILE i<=n DO
2289 m := wlists.getItemFromList (id^.identlistF.names, i) ;
2290 v := makeVar (m) ;
2291 putVar (v, type, NIL) ;
2292 putVarBool (v, TRUE, TRUE, isvar, isused) ;
2293 IF debugScopes
2294 THEN
2295 printf ("adding parameter variable into top scope\n") ;
2296 dumpScopes ;
2297 printf (" variable name is: ") ;
2298 s := InitStringCharStar (keyToCharStar (m)) ;
2299 IF KillString (WriteS (StdOut, s))=NIL
2300 THEN
2301 END ;
2302 printf ("\n")
2303 END ;
2304 INC (i)
2305 END
2306 END makeVariablesFromParameters ;
2307
2308
2309 (*
2310 addProcedureToScope - add a procedure name n and node d to the
2311 current scope.
2312 *)
2313
2314 PROCEDURE addProcedureToScope (d: node; n: Name) : node ;
2315 VAR
2316 m: node ;
2317 i: CARDINAL ;
2318 BEGIN
2319 i := HighIndice (scopeStack) ;
2320 m := GetIndice (scopeStack, i) ;
2321 IF isDef (m) AND
2322 (getSymName (m) = makeKey ('M2RTS')) AND
2323 (getSymName (d) = makeKey ('HALT'))
2324 THEN
2325 haltN := d ;
2326 putSymKey (baseSymbols, n, haltN)
2327 END ;
2328 RETURN addToScope (d)
2329 END addProcedureToScope ;
2330
2331
2332 (*
2333 makeProcedure - create, initialise and return a procedure node.
2334 *)
2335
2336 PROCEDURE makeProcedure (n: Name) : node ;
2337 VAR
2338 d: node ;
2339 BEGIN
2340 d := lookupSym (n) ;
2341 IF d=NIL
2342 THEN
2343 d := newNode (procedure) ;
2344 WITH d^ DO
2345 procedureF.name := n ;
2346 initDecls (procedureF.decls) ;
2347 procedureF.scope := getDeclScope () ;
2348 procedureF.parameters := InitIndex (1) ;
2349 procedureF.isForC := isDefForCNode (getDeclScope ()) ;
2350 procedureF.built := FALSE ;
2351 procedureF.returnopt := FALSE ;
2352 procedureF.optarg := NIL ;
2353 procedureF.noreturnused := FALSE ;
2354 procedureF.noreturn := FALSE ;
2355 procedureF.vararg := FALSE ;
2356 procedureF.checking := FALSE ;
2357 procedureF.paramcount := 0 ;
2358 procedureF.returnType := NIL ;
2359 procedureF.beginStatements := NIL ;
2360 initCname (procedureF.cname) ;
2361 procedureF.defComment := NIL ;
2362 procedureF.modComment := NIL ;
2363 END
2364 END ;
2365 RETURN addProcedureToScope (d, n)
2366 END makeProcedure ;
2367
2368
2369 (*
2370 putCommentDefProcedure - remembers the procedure comment (if it exists) as a
2371 definition module procedure heading. NIL is placed
2372 if there is no procedure comment available.
2373 *)
2374
2375 PROCEDURE putCommentDefProcedure (n: node) ;
2376 BEGIN
2377 assert (isProcedure (n)) ;
2378 IF isProcedureComment (lastcomment)
2379 THEN
2380 n^.procedureF.defComment := lastcomment
2381 END
2382 END putCommentDefProcedure ;
2383
2384
2385 (*
2386 putCommentModProcedure - remembers the procedure comment (if it exists) as an
2387 implementation/program module procedure heading. NIL is placed
2388 if there is no procedure comment available.
2389 *)
2390
2391 PROCEDURE putCommentModProcedure (n: node) ;
2392 BEGIN
2393 assert (isProcedure (n)) ;
2394 IF isProcedureComment (lastcomment)
2395 THEN
2396 n^.procedureF.modComment := lastcomment
2397 END
2398 END putCommentModProcedure ;
2399
2400
2401 (*
2402 paramEnter - reset the parameter count.
2403 *)
2404
2405 PROCEDURE paramEnter (n: node) ;
2406 BEGIN
2407 assert (isProcedure (n)) ;
2408 n^.procedureF.paramcount := 0
2409 END paramEnter ;
2410
2411
2412 (*
2413 paramLeave - set paramater checking to TRUE from now onwards.
2414 *)
2415
2416 PROCEDURE paramLeave (n: node) ;
2417 BEGIN
2418 assert (isProcedure (n)) ;
2419 n^.procedureF.checking := TRUE ;
2420 IF isImp (currentModule) OR isModule (currentModule)
2421 THEN
2422 n^.procedureF.built := TRUE
2423 END
2424 END paramLeave ;
2425
2426
2427 (*
2428 putReturnType - sets the return type of procedure or proctype proc to type.
2429 *)
2430
2431 PROCEDURE putReturnType (proc, type: node) ;
2432 BEGIN
2433 assert (isProcedure (proc) OR isProcType (proc)) ;
2434 IF isProcedure (proc)
2435 THEN
2436 proc^.procedureF.returnType := type
2437 ELSE
2438 proc^.proctypeF.returnType := type
2439 END ;
2440 initNodeOpaqueState (proc)
2441 END putReturnType ;
2442
2443
2444 (*
2445 putOptReturn - sets, proctype or procedure, proc, to have an optional return type.
2446 *)
2447
2448 PROCEDURE putOptReturn (proc: node) ;
2449 BEGIN
2450 assert (isProcedure (proc) OR isProcType (proc)) ;
2451 IF isProcedure (proc)
2452 THEN
2453 proc^.procedureF.returnopt := TRUE
2454 ELSE
2455 proc^.proctypeF.returnopt := TRUE
2456 END
2457 END putOptReturn ;
2458
2459
2460 (*
2461 makeProcType - returns a proctype node.
2462 *)
2463
2464 PROCEDURE makeProcType () : node ;
2465 VAR
2466 d: node ;
2467 BEGIN
2468 d := newNode (proctype) ;
2469 WITH d^ DO
2470 proctypeF.scope := getDeclScope () ;
2471 proctypeF.parameters := InitIndex (1) ;
2472 proctypeF.returnopt := FALSE ;
2473 proctypeF.optarg := NIL ;
2474 proctypeF.vararg := FALSE ;
2475 proctypeF.returnType := NIL ;
2476 END ;
2477 initNodeOpaqueState (d) ;
2478 RETURN d
2479 END makeProcType ;
2480
2481
2482 (*
2483 putProcTypeReturn - sets the return type of, proc, to, type.
2484 *)
2485
2486 PROCEDURE putProcTypeReturn (proc, type: node) ;
2487 BEGIN
2488 assert (isProcType (proc)) ;
2489 proc^.proctypeF.returnType := type ;
2490 initNodeOpaqueState (proc)
2491 END putProcTypeReturn ;
2492
2493
2494 (*
2495 putProcTypeOptReturn - sets, proc, to have an optional return type.
2496 *)
2497
2498 PROCEDURE putProcTypeOptReturn (proc: node) ;
2499 BEGIN
2500 assert (isProcType (proc)) ;
2501 proc^.proctypeF.returnopt := TRUE
2502 END putProcTypeOptReturn ;
2503
2504
2505 (*
2506 makeNonVarParameter - returns a non var parameter node with, name: type.
2507 *)
2508
2509 PROCEDURE makeNonVarParameter (l: node; type, proc: node; isused: BOOLEAN) : node ;
2510 VAR
2511 d: node ;
2512 BEGIN
2513 assert ((l=NIL) OR isIdentList (l)) ;
2514 d := newNode (param) ;
2515 d^.paramF.namelist := l ;
2516 d^.paramF.type := type ;
2517 d^.paramF.scope := proc ;
2518 d^.paramF.isUnbounded := FALSE ;
2519 d^.paramF.isForC := isDefForCNode (proc) ;
2520 d^.paramF.isUsed := isused ;
2521 initNodeOpaqueState (d) ;
2522 RETURN d
2523 END makeNonVarParameter ;
2524
2525
2526 (*
2527 makeVarParameter - returns a var parameter node with, name: type.
2528 *)
2529
2530 PROCEDURE makeVarParameter (l: node; type, proc: node; isused: BOOLEAN) : node ;
2531 VAR
2532 d: node ;
2533 BEGIN
2534 assert ((l=NIL) OR isIdentList (l)) ;
2535 d := newNode (varparam) ;
2536 d^.varparamF.namelist := l ;
2537 d^.varparamF.type := type ;
2538 d^.varparamF.scope := proc ;
2539 d^.varparamF.isUnbounded := FALSE ;
2540 d^.varparamF.isForC := isDefForCNode (proc) ;
2541 d^.varparamF.isUsed := isused ;
2542 initNodeOpaqueState (d) ;
2543 RETURN d
2544 END makeVarParameter ;
2545
2546
2547 (*
2548 makeVarargs - returns a varargs node.
2549 *)
2550
2551 PROCEDURE makeVarargs () : node ;
2552 VAR
2553 d: node ;
2554 BEGIN
2555 d := newNode (varargs) ;
2556 d^.varargsF.scope := NIL ;
2557 RETURN d
2558 END makeVarargs ;
2559
2560
2561 (*
2562 isVarargs - returns TRUE if, n, is a varargs node.
2563 *)
2564
2565 PROCEDURE isVarargs (n: node) : BOOLEAN ;
2566 BEGIN
2567 RETURN n^.kind = varargs
2568 END isVarargs ;
2569
2570
2571 (*
2572 addParameter - adds a parameter, param, to procedure or proctype, proc.
2573 *)
2574
2575 PROCEDURE addParameter (proc, param: node) ;
2576 BEGIN
2577 assert (isVarargs (param) OR isParam (param) OR isVarParam (param) OR isOptarg (param)) ;
2578 CASE proc^.kind OF
2579
2580 procedure: IncludeIndiceIntoIndex (proc^.procedureF.parameters, param) ;
2581 IF isVarargs (param)
2582 THEN
2583 proc^.procedureF.vararg := TRUE
2584 END ;
2585 IF isOptarg (param)
2586 THEN
2587 proc^.procedureF.optarg := param
2588 END |
2589 proctype : IncludeIndiceIntoIndex (proc^.proctypeF.parameters, param) ;
2590 IF isVarargs (param)
2591 THEN
2592 proc^.proctypeF.vararg := TRUE
2593 END ;
2594 IF isOptarg (param)
2595 THEN
2596 proc^.proctypeF.optarg := param
2597 END
2598
2599 END
2600 END addParameter ;
2601
2602
2603 (*
2604 isOptarg - returns TRUE if, n, is an optarg.
2605 *)
2606
2607 PROCEDURE isOptarg (n: node) : BOOLEAN ;
2608 BEGIN
2609 RETURN n^.kind = optarg
2610 END isOptarg ;
2611
2612
2613 (*
2614 makeOptParameter - creates and returns an optarg.
2615 *)
2616
2617 PROCEDURE makeOptParameter (l, type, init: node) : node ;
2618 VAR
2619 n: node ;
2620 BEGIN
2621 n := newNode (optarg) ;
2622 n^.optargF.namelist := l ;
2623 n^.optargF.type := type ;
2624 n^.optargF.init := init ;
2625 n^.optargF.scope := NIL ;
2626 RETURN n
2627 END makeOptParameter ;
2628
2629
2630 (*
2631 addOptParameter - returns an optarg which has been created and added to
2632 procedure node, proc. It has a name, id, and, type,
2633 and an initial value, init.
2634 *)
2635
2636 PROCEDURE addOptParameter (proc: node; id: Name; type, init: node) : node ;
2637 VAR
2638 p, l: node ;
2639 BEGIN
2640 assert (isProcedure (proc)) ;
2641 l := makeIdentList () ;
2642 assert (putIdent (l, id)) ;
2643 checkMakeVariables (proc, l, type, FALSE, TRUE) ;
2644 IF NOT proc^.procedureF.checking
2645 THEN
2646 p := makeOptParameter (l, type, init) ;
2647 addParameter (proc, p)
2648 END ;
2649 RETURN p
2650 END addOptParameter ;
2651
2652
2653 VAR
2654 globalNode: node ;
2655
2656
2657 (*
2658 setwatch - assign the globalNode to n.
2659 *)
2660
2661 PROCEDURE setwatch (n: node) : BOOLEAN ;
2662 BEGIN
2663 globalNode := n ;
2664 RETURN TRUE
2665 END setwatch ;
2666
2667
2668 (*
2669 runwatch - set the globalNode to an identlist.
2670 *)
2671
2672 PROCEDURE runwatch () : BOOLEAN ;
2673 BEGIN
2674 RETURN globalNode^.kind = identlist
2675 END runwatch ;
2676
2677
2678 (*
2679 makeIdentList - returns a node which will be used to maintain an ident list.
2680 *)
2681
2682 PROCEDURE makeIdentList () : node ;
2683 VAR
2684 n: node ;
2685 BEGIN
2686 n := newNode (identlist) ;
2687 n^.identlistF.names := wlists.initList () ;
2688 n^.identlistF.cnamed := FALSE ;
2689 RETURN n
2690 END makeIdentList ;
2691
2692
2693 (*
2694 isIdentList - returns TRUE if, n, is an identlist.
2695 *)
2696
2697 PROCEDURE isIdentList (n: node) : BOOLEAN ;
2698 BEGIN
2699 RETURN n^.kind = identlist
2700 END isIdentList ;
2701
2702
2703 (*
2704 putIdent - places ident, i, into identlist, n. It returns TRUE if
2705 ident, i, is unique.
2706 *)
2707
2708 PROCEDURE putIdent (n: node; i: Name) : BOOLEAN ;
2709 BEGIN
2710 assert (isIdentList (n)) ;
2711 IF wlists.isItemInList (n^.identlistF.names, i)
2712 THEN
2713 RETURN FALSE
2714 ELSE
2715 wlists.putItemIntoList (n^.identlistF.names, i) ;
2716 RETURN TRUE
2717 END
2718 END putIdent ;
2719
2720
2721 (*
2722 identListLen - returns the length of identlist.
2723 *)
2724
2725 PROCEDURE identListLen (n: node) : CARDINAL ;
2726 BEGIN
2727 IF n=NIL
2728 THEN
2729 RETURN 0
2730 ELSE
2731 assert (isIdentList (n)) ;
2732 RETURN wlists.noOfItemsInList (n^.identlistF.names)
2733 END
2734 END identListLen ;
2735
2736
2737 (*
2738 checkParameters - placeholder for future parameter checking.
2739 *)
2740
2741 PROCEDURE checkParameters (p: node; i: node; type: node; isvar, isused: BOOLEAN) ;
2742 BEGIN
2743 (* do check. *)
2744 disposeNode (i)
2745 END checkParameters ;
2746
2747 (*
2748 (*
2749 avoidCnames - checks each name in, n, against C reserved
2750 keywords and macros.
2751 *)
2752
2753 PROCEDURE avoidCnames (n: node) ;
2754 VAR
2755 i, j: CARDINAL ;
2756 BEGIN
2757 assert (isIdentList (n)) ;
2758 IF NOT n^.identlistF.cnamed
2759 THEN
2760 n^.identlistF.cnamed := TRUE ;
2761 j := wlists.noOfItemsInList (n^.identlistF.names) ;
2762 i := 1 ;
2763 WHILE i<=j DO
2764 wlists.replaceItemInList (n^.identlistF.names,
2765 i,
2766 keyc.cnamen (wlists.getItemFromList (n^.identlistF.names, i), FALSE)) ;
2767 INC (i)
2768 END
2769 END
2770 END avoidCnames ;
2771 *)
2772
2773
2774 (*
2775 checkMakeVariables - create shadow local variables for parameters providing that
2776 procedure n has not already been built and we are compiling
2777 a module or an implementation module.
2778 *)
2779
2780 PROCEDURE checkMakeVariables (n, i, type: node; isvar, isused: BOOLEAN) ;
2781 BEGIN
2782 IF (isImp (currentModule) OR isModule (currentModule)) AND
2783 (NOT n^.procedureF.built)
2784 THEN
2785 makeVariablesFromParameters (n, i, type, isvar, isused)
2786 END ;
2787 END checkMakeVariables ;
2788
2789
2790 (*
2791 addVarParameters - adds the identlist, i, of, type, to be VAR parameters
2792 in procedure, n.
2793 *)
2794
2795 PROCEDURE addVarParameters (n: node; i: node; type: node; isused: BOOLEAN) ;
2796 VAR
2797 p: node ;
2798 BEGIN
2799 assert (isIdentList (i)) ;
2800 assert (isProcedure (n)) ;
2801 checkMakeVariables (n, i, type, TRUE, isused) ;
2802 IF n^.procedureF.checking
2803 THEN
2804 checkParameters (n, i, type, TRUE, isused) (* will destroy, i. *)
2805 ELSE
2806 p := makeVarParameter (i, type, n, isused) ;
2807 IncludeIndiceIntoIndex (n^.procedureF.parameters, p) ;
2808 END ;
2809 END addVarParameters ;
2810
2811
2812 (*
2813 addNonVarParameters - adds the identlist, i, of, type, to be parameters
2814 in procedure, n.
2815 *)
2816
2817 PROCEDURE addNonVarParameters (n: node; i: node; type: node; isused: BOOLEAN) ;
2818 VAR
2819 p: node ;
2820 BEGIN
2821 assert (isIdentList (i)) ;
2822 assert (isProcedure (n)) ;
2823 checkMakeVariables (n, i, type, FALSE, isused) ;
2824 IF n^.procedureF.checking
2825 THEN
2826 checkParameters (n, i, type, FALSE, isused) (* will destroy, i. *)
2827 ELSE
2828 p := makeNonVarParameter (i, type, n, isused) ;
2829 IncludeIndiceIntoIndex (n^.procedureF.parameters, p)
2830 END ;
2831 END addNonVarParameters ;
2832
2833
2834 (*
2835 makeSubrange - returns a subrange node, built from range: low..high.
2836 *)
2837
2838 PROCEDURE makeSubrange (low, high: node) : node ;
2839 VAR
2840 n: node ;
2841 BEGIN
2842 n := newNode (subrange) ;
2843 n^.subrangeF.low := low ;
2844 n^.subrangeF.high := high ;
2845 n^.subrangeF.type := NIL ;
2846 n^.subrangeF.scope := getDeclScope () ;
2847 RETURN n
2848 END makeSubrange ;
2849
2850
2851 (*
2852 putSubrangeType - assigns, type, to the subrange type, sub.
2853 *)
2854
2855 PROCEDURE putSubrangeType (sub, type: node) ;
2856 BEGIN
2857 assert (isSubrange (sub)) ;
2858 sub^.subrangeF.type := type
2859 END putSubrangeType ;
2860
2861
2862 (*
2863 makeSet - returns a set of, type, node.
2864 *)
2865
2866 PROCEDURE makeSet (type: node) : node ;
2867 VAR
2868 n: node ;
2869 BEGIN
2870 n := newNode (set) ;
2871 n^.setF.type := type ;
2872 n^.setF.scope := getDeclScope () ;
2873 RETURN n
2874 END makeSet ;
2875
2876
2877 (*
2878 makeSetValue - creates and returns a setvalue node.
2879 *)
2880
2881 PROCEDURE makeSetValue () : node ;
2882 VAR
2883 n: node ;
2884 BEGIN
2885 n := newNode (setvalue) ;
2886 n^.setvalueF.type := bitsetN ;
2887 n^.setvalueF.values := InitIndex (1) ;
2888 RETURN n
2889 END makeSetValue ;
2890
2891
2892 (*
2893 isSetValue - returns TRUE if, n, is a setvalue node.
2894 *)
2895
2896 PROCEDURE isSetValue (n: node) : BOOLEAN ;
2897 BEGIN
2898 assert (n # NIL) ;
2899 RETURN n^.kind = setvalue
2900 END isSetValue ;
2901
2902
2903 (*
2904 putSetValue - assigns the type, t, to the set value, n. The
2905 node, n, is returned.
2906 *)
2907
2908 PROCEDURE putSetValue (n, t: node) : node ;
2909 BEGIN
2910 assert (isSetValue (n)) ;
2911 n^.setvalueF.type := t ;
2912 RETURN n
2913 END putSetValue ;
2914
2915
2916 (*
2917 includeSetValue - includes the range l..h into the setvalue.
2918 h might be NIL indicating that a single element
2919 is to be included into the set.
2920 n is returned.
2921 *)
2922
2923 PROCEDURE includeSetValue (n: node; l, h: node) : node ;
2924 BEGIN
2925 assert (isSetValue (n)) ;
2926 IncludeIndiceIntoIndex (n^.setvalueF.values, l) ;
2927 RETURN n
2928 END includeSetValue ;
2929
2930
2931 (*
2932 makePointer - returns a pointer of, type, node.
2933 *)
2934
2935 PROCEDURE makePointer (type: node) : node ;
2936 VAR
2937 n: node ;
2938 BEGIN
2939 n := newNode (pointer) ;
2940 n^.pointerF.type := type ;
2941 n^.pointerF.scope := getDeclScope () ;
2942 RETURN n
2943 END makePointer ;
2944
2945
2946 (*
2947 makeArray - returns a node representing ARRAY subr OF type.
2948 *)
2949
2950 PROCEDURE makeArray (subr, type: node) : node ;
2951 VAR
2952 n, s: node ;
2953 BEGIN
2954 s := skipType (subr) ;
2955 assert (isSubrange (s) OR isOrdinal (s) OR isEnumeration (s)) ;
2956 n := newNode (array) ;
2957 n^.arrayF.subr := subr ;
2958 n^.arrayF.type := type ;
2959 n^.arrayF.scope := getDeclScope () ;
2960 n^.arrayF.isUnbounded := FALSE ;
2961 RETURN n
2962 END makeArray ;
2963
2964
2965 (*
2966 makeRecord - creates and returns a record node.
2967 *)
2968
2969 PROCEDURE makeRecord () : node ;
2970 VAR
2971 n: node ;
2972 BEGIN
2973 n := newNode (record) ;
2974 n^.recordF.localSymbols := initTree () ;
2975 n^.recordF.listOfSons := InitIndex (1) ;
2976 n^.recordF.scope := getDeclScope () ;
2977 RETURN n
2978 END makeRecord ;
2979
2980
2981 (*
2982 addFieldsToRecord - adds fields, i, of type, t, into a record, r.
2983 It returns, r.
2984 *)
2985
2986 PROCEDURE addFieldsToRecord (r, v, i, t: node) : node ;
2987 VAR
2988 p, fj: node ;
2989 j, n : CARDINAL ;
2990 fn : Name ;
2991 BEGIN
2992 IF isRecord (r)
2993 THEN
2994 p := r ;
2995 v := NIL
2996 ELSE
2997 p := getRecord (getParent (r)) ;
2998 assert (isVarientField (r)) ;
2999 assert (isVarient (v)) ;
3000 putFieldVarient (r, v)
3001 END ;
3002 n := wlists.noOfItemsInList (i^.identlistF.names) ;
3003 j := 1 ;
3004 WHILE j<=n DO
3005 fn := wlists.getItemFromList (i^.identlistF.names, j) ;
3006 fj := getSymKey (p^.recordF.localSymbols, n) ;
3007 IF fj=NIL
3008 THEN
3009 fj := putFieldRecord (r, fn, t, v)
3010 ELSE
3011 metaErrors2 ('record field {%1ad} has already been declared inside a {%2Dd} {%2a}',
3012 'attempting to declare a duplicate record field', fj, p)
3013 END ;
3014 INC (j)
3015 END ;
3016 RETURN r;
3017 END addFieldsToRecord ;
3018
3019
3020 (*
3021 makeVarient - creates a new symbol, a varient symbol for record or varient field
3022 symbol, r.
3023 *)
3024
3025 PROCEDURE makeVarient (r: node) : node ;
3026 VAR
3027 n: node ;
3028 BEGIN
3029 n := newNode (varient) ;
3030 WITH n^ DO
3031 varientF.listOfSons := InitIndex (1) ;
3032 (* do we need to remember our parent (r) ? *)
3033 (* if so use this n^.varientF.parent := r *)
3034 IF isRecord (r)
3035 THEN
3036 varientF.varient := NIL
3037 ELSE
3038 varientF.varient := r
3039 END ;
3040 varientF.tag := NIL ;
3041 varientF.scope := getDeclScope () ;
3042 END ;
3043 (* now add, n, to the record/varient, r, field list *)
3044 WITH r^ DO
3045 CASE kind OF
3046
3047 record : IncludeIndiceIntoIndex (recordF.listOfSons, n) |
3048 varientfield: IncludeIndiceIntoIndex (varientfieldF.listOfSons, n)
3049
3050 END
3051 END ;
3052 RETURN n
3053 END makeVarient ;
3054
3055
3056 (*
3057 buildVarientFieldRecord - builds a varient field into a varient symbol, v.
3058 The varient field is returned.
3059 *)
3060
3061 PROCEDURE buildVarientFieldRecord (v: node; p: node) : node ;
3062 VAR
3063 f: node ;
3064 BEGIN
3065 assert (isVarient (v)) ;
3066 f := makeVarientField (v, p) ;
3067 assert (isVarientField (f)) ;
3068 putFieldVarient (f, v) ;
3069 RETURN f
3070 END buildVarientFieldRecord ;
3071
3072
3073 (*
3074 makeVarientField - create a varient field within varient, v,
3075 The new varient field is returned.
3076 *)
3077
3078 PROCEDURE makeVarientField (v: node; p: node) : node ;
3079 VAR
3080 n: node ;
3081 BEGIN
3082 n := newNode (varientfield) ;
3083 WITH n^.varientfieldF DO
3084 name := NulName ;
3085 parent := p ;
3086 varient := v ;
3087 simple := FALSE ;
3088 listOfSons := InitIndex (1) ;
3089 scope := getDeclScope ()
3090 END ;
3091 RETURN n
3092 END makeVarientField ;
3093
3094
3095 (*
3096 putFieldVarient - places the field varient, f, as a brother to, the
3097 varient symbol, v, and also tells, f, that its varient
3098 parent is, v.
3099 *)
3100
3101 PROCEDURE putFieldVarient (f, v: node) ;
3102 BEGIN
3103 assert (isVarient (v)) ;
3104 assert (isVarientField (f)) ;
3105 WITH v^ DO
3106 CASE kind OF
3107
3108 varient: IncludeIndiceIntoIndex (varientF.listOfSons, f)
3109
3110 END
3111 END ;
3112 WITH f^ DO
3113 CASE kind OF
3114
3115 varientfield: varientfieldF.varient := v
3116
3117 END
3118 END
3119 END putFieldVarient ;
3120
3121
3122 (*
3123 putFieldRecord - create a new recordfield and place it into record r.
3124 The new field has a tagname and type and can have a
3125 variant field v.
3126 *)
3127
3128 PROCEDURE putFieldRecord (r: node; tag: Name; type, v: node) : node ;
3129 VAR
3130 f, n, p: node ;
3131 BEGIN
3132 n := newNode (recordfield) ;
3133 WITH r^ DO
3134 CASE kind OF
3135
3136 record: IncludeIndiceIntoIndex (recordF.listOfSons, n) ;
3137 (* ensure that field, n, is in the parents Local Symbols. *)
3138 IF tag#NulName
3139 THEN
3140 IF getSymKey (recordF.localSymbols, tag) = NulKey
3141 THEN
3142 putSymKey (recordF.localSymbols, tag, n)
3143 ELSE
3144 f := getSymKey (recordF.localSymbols, tag) ;
3145 metaErrors1 ('field record {%1Dad} has already been declared',
3146 'field record duplicate', f)
3147 END
3148 END |
3149 varientfield: IncludeIndiceIntoIndex (varientfieldF.listOfSons, n) ;
3150 p := getParent (r) ;
3151 assert (p^.kind=record) ;
3152 IF tag#NulName
3153 THEN
3154 putSymKey (p^.recordF.localSymbols, tag, n)
3155 END
3156
3157 END
3158 END ;
3159 (* fill in, n. *)
3160 n^.recordfieldF.type := type ;
3161 n^.recordfieldF.name := tag ;
3162 n^.recordfieldF.parent := r ;
3163 n^.recordfieldF.varient := v ;
3164 n^.recordfieldF.tag := FALSE ;
3165 n^.recordfieldF.scope := NIL ;
3166 initCname (n^.recordfieldF.cname) ;
3167 (*
3168 IF r^.kind=record
3169 THEN
3170 doRecordM2 (doP, r)
3171 END ;
3172 *)
3173 RETURN n
3174 END putFieldRecord ;
3175
3176
3177 (*
3178 buildVarientSelector - builds a field of name, tag, of, type onto:
3179 record or varient field, r.
3180 varient, v.
3181 *)
3182
3183 PROCEDURE buildVarientSelector (r, v: node; tag: Name; type: node) ;
3184 VAR
3185 f: node ;
3186 BEGIN
3187 assert (isRecord (r) OR isVarientField (r)) ;
3188 IF isRecord (r) OR isVarientField (r)
3189 THEN
3190 IF (type=NIL) AND (tag=NulName)
3191 THEN
3192 metaError1 ('expecting a tag field in the declaration of a varient record {%1Ua}', r)
3193 ELSIF type=NIL
3194 THEN
3195 f := lookupSym (tag) ;
3196 putVarientTag (v, f)
3197 ELSE
3198 f := putFieldRecord (r, tag, type, v) ;
3199 assert (isRecordField (f)) ;
3200 f^.recordfieldF.tag := TRUE ;
3201 putVarientTag (v, f)
3202 END
3203 END
3204 END buildVarientSelector ;
3205
3206
3207 (*
3208 ensureOrder - ensures that, a, and, b, exist in, i, and also
3209 ensure that, a, is before, b.
3210 *)
3211
3212 PROCEDURE ensureOrder (i: Index; a, b: node) ;
3213 BEGIN
3214 assert (IsIndiceInIndex (i, a)) ;
3215 assert (IsIndiceInIndex (i, b)) ;
3216 RemoveIndiceFromIndex (i, a) ;
3217 RemoveIndiceFromIndex (i, b) ;
3218 IncludeIndiceIntoIndex (i, a) ;
3219 IncludeIndiceIntoIndex (i, b) ;
3220 assert (IsIndiceInIndex (i, a)) ;
3221 assert (IsIndiceInIndex (i, b))
3222 END ensureOrder ;
3223
3224
3225 (*
3226 putVarientTag - places tag into variant v.
3227 *)
3228
3229 PROCEDURE putVarientTag (v: node; tag: node) ;
3230 VAR
3231 p: node ;
3232 BEGIN
3233 assert (isVarient (v)) ;
3234 CASE v^.kind OF
3235
3236 varient: v^.varientF.tag := tag
3237
3238 END
3239 END putVarientTag ;
3240
3241
3242 (*
3243 getParent - returns the parent field of recordfield or varientfield symbol, n.
3244 *)
3245
3246 PROCEDURE getParent (n: node) : node ;
3247 BEGIN
3248 CASE n^.kind OF
3249
3250 recordfield: RETURN n^.recordfieldF.parent |
3251 varientfield: RETURN n^.varientfieldF.parent
3252
3253 END
3254 END getParent ;
3255
3256
3257 (*
3258 getRecord - returns the record associated with node, n.
3259 (Parental record).
3260 *)
3261
3262 PROCEDURE getRecord (n: node) : node ;
3263 BEGIN
3264 assert (n^.kind # varient) ; (* if this fails then we need to add parent field to varient. *)
3265 CASE n^.kind OF
3266
3267 record : RETURN n |
3268 varientfield: RETURN getRecord (getParent (n))
3269
3270 END
3271 END getRecord ;
3272
3273
3274 (*
3275 putUnbounded - sets array, n, as unbounded.
3276 *)
3277
3278 PROCEDURE putUnbounded (n: node) ;
3279 BEGIN
3280 assert (n^.kind = array) ;
3281 n^.arrayF.isUnbounded := TRUE
3282 END putUnbounded ;
3283
3284
3285 (*
3286 isConstExp - return TRUE if the node kind is a constexp.
3287 *)
3288
3289 PROCEDURE isConstExp (c: node) : BOOLEAN ;
3290 BEGIN
3291 assert (c#NIL) ;
3292 RETURN c^.kind = constexp
3293 END isConstExp ;
3294
3295
3296 (*
3297 addEnumToModule - adds enumeration type, e, into the list of enums
3298 in module, m.
3299 *)
3300
3301 PROCEDURE addEnumToModule (m, e: node) ;
3302 BEGIN
3303 assert (isEnumeration (e) OR isEnumerationField (e)) ;
3304 assert (isModule (m) OR isDef (m) OR isImp (m)) ;
3305 IF isModule (m)
3306 THEN
3307 IncludeIndiceIntoIndex (m^.moduleF.enumFixup.info, e)
3308 ELSIF isDef (m)
3309 THEN
3310 IncludeIndiceIntoIndex (m^.defF.enumFixup.info, e)
3311 ELSIF isImp (m)
3312 THEN
3313 IncludeIndiceIntoIndex (m^.impF.enumFixup.info, e)
3314 END
3315 END addEnumToModule ;
3316
3317
3318 (*
3319 getNextFixup - return the next fixup from from f.
3320 *)
3321
3322 PROCEDURE getNextFixup (VAR f: fixupInfo) : node ;
3323 BEGIN
3324 INC (f.count) ;
3325 RETURN GetIndice (f.info, f.count)
3326 END getNextFixup ;
3327
3328
3329 (*
3330 getNextEnum - returns the next enumeration node.
3331 *)
3332
3333 PROCEDURE getNextEnum () : node ;
3334 VAR
3335 n: node ;
3336 BEGIN
3337 n := NIL ;
3338 assert (isDef (currentModule) OR isImp (currentModule) OR isModule (currentModule)) ;
3339 WITH currentModule^ DO
3340 IF isDef (currentModule)
3341 THEN
3342 n := getNextFixup (defF.enumFixup)
3343 ELSIF isImp (currentModule)
3344 THEN
3345 n := getNextFixup (impF.enumFixup)
3346 ELSIF isModule (currentModule)
3347 THEN
3348 n := getNextFixup (moduleF.enumFixup)
3349 END
3350 END ;
3351 assert (n # NIL) ;
3352 assert (isEnumeration (n) OR isEnumerationField (n)) ;
3353 RETURN n
3354 END getNextEnum ;
3355
3356
3357 (*
3358 resetEnumPos - resets the index into the saved list of enums inside
3359 module, n.
3360 *)
3361
3362 PROCEDURE resetEnumPos (n: node) ;
3363 BEGIN
3364 assert (isDef (n) OR isImp (n) OR isModule (n)) ;
3365 IF isDef (n)
3366 THEN
3367 n^.defF.enumFixup.count := 0
3368 ELSIF isImp (n)
3369 THEN
3370 n^.impF.enumFixup.count := 0
3371 ELSIF isModule (n)
3372 THEN
3373 n^.moduleF.enumFixup.count := 0
3374 END
3375 END resetEnumPos ;
3376
3377
3378 (*
3379 getEnumsComplete - gets the field from the def or imp or module, n.
3380 *)
3381
3382 PROCEDURE getEnumsComplete (n: node) : BOOLEAN ;
3383 BEGIN
3384 CASE n^.kind OF
3385
3386 def : RETURN n^.defF.enumsComplete |
3387 imp : RETURN n^.impF.enumsComplete |
3388 module: RETURN n^.moduleF.enumsComplete
3389
3390 END
3391 END getEnumsComplete ;
3392
3393
3394 (*
3395 setEnumsComplete - sets the field inside the def or imp or module, n.
3396 *)
3397
3398 PROCEDURE setEnumsComplete (n: node) ;
3399 BEGIN
3400 CASE n^.kind OF
3401
3402 def : n^.defF.enumsComplete := TRUE |
3403 imp : n^.impF.enumsComplete := TRUE |
3404 module: n^.moduleF.enumsComplete := TRUE
3405
3406 END
3407 END setEnumsComplete ;
3408
3409
3410 (*
3411 doMakeEnum - create an enumeration type and add it to the current module.
3412 *)
3413
3414 PROCEDURE doMakeEnum () : node ;
3415 VAR
3416 e: node ;
3417 BEGIN
3418 e := newNode (enumeration) ;
3419 WITH e^ DO
3420 enumerationF.noOfElements := 0 ;
3421 enumerationF.localSymbols := initTree () ;
3422 enumerationF.scope := getDeclScope () ;
3423 enumerationF.listOfSons := InitIndex (1) ;
3424 enumerationF.low := NIL ;
3425 enumerationF.high := NIL ;
3426 END ;
3427 addEnumToModule (currentModule, e) ;
3428 RETURN e
3429 END doMakeEnum ;
3430
3431
3432 (*
3433 makeEnum - creates an enumerated type and returns the node.
3434 *)
3435
3436 PROCEDURE makeEnum () : node ;
3437 BEGIN
3438 IF (currentModule#NIL) AND getEnumsComplete (currentModule)
3439 THEN
3440 RETURN getNextEnum ()
3441 ELSE
3442 RETURN doMakeEnum ()
3443 END
3444 END makeEnum ;
3445
3446
3447 (*
3448 doMakeEnumField - create an enumeration field name and add it to enumeration e.
3449 Return the new field.
3450 *)
3451
3452 PROCEDURE doMakeEnumField (e: node; n: Name) : node ;
3453 VAR
3454 f: node ;
3455 BEGIN
3456 assert (isEnumeration (e)) ;
3457 f := lookupSym (n) ;
3458 IF f=NIL
3459 THEN
3460 f := newNode (enumerationfield) ;
3461 putSymKey (e^.enumerationF.localSymbols, n, f) ;
3462 IncludeIndiceIntoIndex (e^.enumerationF.listOfSons, f) ;
3463 WITH f^ DO
3464 enumerationfieldF.name := n ;
3465 enumerationfieldF.type := e ;
3466 enumerationfieldF.scope := getDeclScope () ;
3467 enumerationfieldF.value := e^.enumerationF.noOfElements ;
3468 initCname (enumerationfieldF.cname)
3469 END ;
3470 INC (e^.enumerationF.noOfElements) ;
3471 assert (GetIndice (e^.enumerationF.listOfSons, e^.enumerationF.noOfElements) = f) ;
3472 addEnumToModule (currentModule, f) ;
3473 IF e^.enumerationF.low = NIL
3474 THEN
3475 e^.enumerationF.low := f
3476 END ;
3477 e^.enumerationF.high := f ;
3478 RETURN addToScope (f)
3479 ELSE
3480 metaErrors2 ('cannot create enumeration field {%1k} as the name is already in use',
3481 '{%2DMad} was declared elsewhere', n, f)
3482 END ;
3483 RETURN f
3484 END doMakeEnumField ;
3485
3486
3487 (*
3488 makeEnumField - returns an enumeration field, named, n.
3489 *)
3490
3491 PROCEDURE makeEnumField (e: node; n: Name) : node ;
3492 BEGIN
3493 IF (currentModule#NIL) AND getEnumsComplete (currentModule)
3494 THEN
3495 RETURN getNextEnum ()
3496 ELSE
3497 RETURN doMakeEnumField (e, n)
3498 END
3499 END makeEnumField ;
3500
3501
3502 (*
3503 isEnumeration - returns TRUE if node, n, is an enumeration type.
3504 *)
3505
3506 PROCEDURE isEnumeration (n: node) : BOOLEAN ;
3507 BEGIN
3508 assert (n#NIL) ;
3509 RETURN n^.kind = enumeration
3510 END isEnumeration ;
3511
3512
3513 (*
3514 makeExpList - creates and returns an expList node.
3515 *)
3516
3517 PROCEDURE makeExpList () : node ;
3518 VAR
3519 n: node ;
3520 BEGIN
3521 n := newNode (explist) ;
3522 n^.explistF.exp := InitIndex (1) ;
3523 RETURN n
3524 END makeExpList ;
3525
3526
3527 (*
3528 isExpList - returns TRUE if, n, is an explist node.
3529 *)
3530
3531 PROCEDURE isExpList (n: node) : BOOLEAN ;
3532 BEGIN
3533 assert (n # NIL) ;
3534 RETURN n^.kind = explist
3535 END isExpList ;
3536
3537
3538 (*
3539 putExpList - places, expression, e, within the explist, n.
3540 *)
3541
3542 PROCEDURE putExpList (n: node; e: node) ;
3543 BEGIN
3544 assert (n # NIL) ;
3545 assert (isExpList (n)) ;
3546 PutIndice (n^.explistF.exp, HighIndice (n^.explistF.exp) + 1, e)
3547 END putExpList ;
3548
3549
3550 (*
3551 getExpList - returns the, n, th argument in an explist.
3552 *)
3553
3554 PROCEDURE getExpList (p: node; n: CARDINAL) : node ;
3555 BEGIN
3556 assert (p#NIL) ;
3557 assert (isExpList (p)) ;
3558 assert (n <= HighIndice (p^.explistF.exp)) ;
3559 RETURN GetIndice (p^.explistF.exp, n)
3560 END getExpList ;
3561
3562
3563 (*
3564 expListLen - returns the length of explist, p.
3565 *)
3566
3567 PROCEDURE expListLen (p: node) : CARDINAL ;
3568 BEGIN
3569 IF p = NIL
3570 THEN
3571 RETURN 0
3572 ELSE
3573 assert (isExpList (p)) ;
3574 RETURN HighIndice (p^.explistF.exp)
3575 END
3576 END expListLen ;
3577
3578
3579 (*
3580 getConstExpComplete - gets the field from the def or imp or module, n.
3581 *)
3582
3583 PROCEDURE getConstExpComplete (n: node) : BOOLEAN ;
3584 BEGIN
3585 CASE n^.kind OF
3586
3587 def : RETURN n^.defF.constsComplete |
3588 imp : RETURN n^.impF.constsComplete |
3589 module: RETURN n^.moduleF.constsComplete
3590
3591 END
3592 END getConstExpComplete ;
3593
3594
3595 (*
3596 setConstExpComplete - sets the field inside the def or imp or module, n.
3597 *)
3598
3599 PROCEDURE setConstExpComplete (n: node) ;
3600 BEGIN
3601 CASE n^.kind OF
3602
3603 def : n^.defF.constsComplete := TRUE |
3604 imp : n^.impF.constsComplete := TRUE |
3605 module: n^.moduleF.constsComplete := TRUE
3606
3607 END
3608 END setConstExpComplete ;
3609
3610
3611 (*
3612 getNextConstExp - returns the next constexp node.
3613 *)
3614
3615 PROCEDURE getNextConstExp () : node ;
3616 BEGIN
3617 assert (isDef (currentModule) OR isImp (currentModule) OR isModule (currentModule)) ;
3618 WITH currentModule^ DO
3619 IF isDef (currentModule)
3620 THEN
3621 RETURN getNextFixup (defF.constFixup)
3622 ELSIF isImp (currentModule)
3623 THEN
3624 RETURN getNextFixup (impF.constFixup)
3625 ELSE
3626 assert (isModule (currentModule)) ;
3627 RETURN getNextFixup (moduleF.constFixup)
3628 END
3629 END
3630 END getNextConstExp ;
3631
3632
3633 (*
3634 resetConstExpPos - resets the index into the saved list of constexps inside
3635 module, n.
3636 *)
3637
3638 PROCEDURE resetConstExpPos (n: node) ;
3639 BEGIN
3640 assert (isDef (n) OR isImp (n) OR isModule (n)) ;
3641 IF isDef (n)
3642 THEN
3643 n^.defF.constFixup.count := 0
3644 ELSIF isImp (n)
3645 THEN
3646 n^.impF.constFixup.count := 0
3647 ELSIF isModule (n)
3648 THEN
3649 n^.moduleF.constFixup.count := 0
3650 END
3651 END resetConstExpPos ;
3652
3653
3654 (*
3655 addConstToModule - adds const exp, e, into the list of constant
3656 expressions in module, m.
3657 *)
3658
3659 PROCEDURE addConstToModule (m, e: node) ;
3660 BEGIN
3661 assert (isModule (m) OR isDef (m) OR isImp (m)) ;
3662 IF isModule (m)
3663 THEN
3664 IncludeIndiceIntoIndex (m^.moduleF.constFixup.info, e)
3665 ELSIF isDef (m)
3666 THEN
3667 IncludeIndiceIntoIndex (m^.defF.constFixup.info, e)
3668 ELSIF isImp (m)
3669 THEN
3670 IncludeIndiceIntoIndex (m^.impF.constFixup.info, e)
3671 END
3672 END addConstToModule ;
3673
3674
3675 (*
3676 doMakeConstExp - create a constexp node and add it to the current module.
3677 *)
3678
3679 PROCEDURE doMakeConstExp () : node ;
3680 VAR
3681 c: node ;
3682 BEGIN
3683 c := makeUnary (constexp, NIL, NIL) ;
3684 addConstToModule (currentModule, c) ;
3685 RETURN c
3686 END doMakeConstExp ;
3687
3688
3689 (*
3690 makeConstExp - returns a constexp node.
3691 *)
3692
3693 PROCEDURE makeConstExp () : node ;
3694 BEGIN
3695 IF (currentModule#NIL) AND getConstExpComplete (currentModule)
3696 THEN
3697 RETURN getNextConstExp ()
3698 ELSE
3699 RETURN doMakeConstExp ()
3700 END
3701 END makeConstExp ;
3702
3703
3704 (*
3705 fixupConstExp - assign fixup expression, e, into the argument of, c.
3706 *)
3707
3708 PROCEDURE fixupConstExp (c, e: node) : node ;
3709 BEGIN
3710 assert (isConstExp (c)) ;
3711 c^.unaryF.arg := e ;
3712 RETURN c
3713 END fixupConstExp ;
3714
3715
3716 (*
3717 isAnyType - return TRUE if node n is any type kind.
3718 *)
3719
3720 PROCEDURE isAnyType (n: node) : BOOLEAN ;
3721 BEGIN
3722 assert (n # NIL) ;
3723 CASE n^.kind OF
3724
3725 address,
3726 loc,
3727 byte,
3728 word,
3729 char,
3730 cardinal,
3731 longcard,
3732 shortcard,
3733 integer,
3734 longint,
3735 shortint,
3736 complex,
3737 longcomplex,
3738 shortcomplex,
3739 bitset,
3740 boolean,
3741 proc,
3742 type : RETURN TRUE
3743
3744 ELSE
3745 RETURN FALSE
3746 END
3747 END isAnyType ;
3748
3749
3750 (*
3751 makeVal - creates a VAL (type, expression) node.
3752 *)
3753
3754 PROCEDURE makeVal (params: node) : node ;
3755 BEGIN
3756 assert (isExpList (params)) ;
3757 IF expListLen (params) = 2
3758 THEN
3759 RETURN makeBinary (val,
3760 getExpList (params, 1),
3761 getExpList (params, 2),
3762 getExpList (params, 1))
3763 ELSE
3764 HALT
3765 END
3766 END makeVal ;
3767
3768
3769 (*
3770 makeCast - creates a cast node TYPENAME (expr).
3771 *)
3772
3773 PROCEDURE makeCast (c, p: node) : node ;
3774 BEGIN
3775 assert (isExpList (p)) ;
3776 IF expListLen (p) = 1
3777 THEN
3778 RETURN makeBinary (cast, c, getExpList (p, 1), c)
3779 ELSE
3780 HALT
3781 END
3782 END makeCast ;
3783
3784
3785 (*
3786 makeIntrisicProc - create an intrinsic node.
3787 *)
3788
3789 PROCEDURE makeIntrinsicProc (k: nodeT; noArgs: CARDINAL; p: node) : node ;
3790 VAR
3791 f: node ;
3792 BEGIN
3793 f := newNode (k) ;
3794 f^.intrinsicF.args := p ;
3795 f^.intrinsicF.noArgs := noArgs ;
3796 f^.intrinsicF.type := NIL ;
3797 f^.intrinsicF.postUnreachable := (k = halt) ;
3798 initPair (f^.intrinsicF.intrinsicComment) ;
3799 RETURN f
3800 END makeIntrinsicProc ;
3801
3802
3803 (*
3804 makeIntrinsicUnaryType - create an intrisic unary type.
3805 *)
3806
3807 PROCEDURE makeIntrinsicUnaryType (k: nodeT; paramList: node; returnType: node) : node ;
3808 BEGIN
3809 RETURN makeUnary (k, getExpList (paramList, 1), returnType)
3810 END makeIntrinsicUnaryType ;
3811
3812
3813 (*
3814 makeIntrinsicBinaryType - create an intrisic binary type.
3815 *)
3816
3817 PROCEDURE makeIntrinsicBinaryType (k: nodeT; paramList: node; returnType: node) : node ;
3818 BEGIN
3819 RETURN makeBinary (k, getExpList (paramList, 1), getExpList (paramList, 2), returnType)
3820 END makeIntrinsicBinaryType ;
3821
3822
3823 (*
3824 checkIntrinsic - checks to see if the function call to, c, with
3825 parameter list, n, is really an intrinic. If it
3826 is an intrinic then an intrinic node is created
3827 and returned. Otherwise NIL is returned.
3828 *)
3829
3830 PROCEDURE checkIntrinsic (c, n: node) : node ;
3831 BEGIN
3832 IF isAnyType (c)
3833 THEN
3834 RETURN makeCast (c, n)
3835 ELSIF c = maxN
3836 THEN
3837 RETURN makeIntrinsicUnaryType (max, n, NIL)
3838 ELSIF c = minN
3839 THEN
3840 RETURN makeIntrinsicUnaryType (min, n, NIL)
3841 ELSIF c = haltN
3842 THEN
3843 RETURN makeIntrinsicProc (halt, expListLen (n), n)
3844 ELSIF c = valN
3845 THEN
3846 RETURN makeVal (n)
3847 ELSIF c = adrN
3848 THEN
3849 RETURN makeIntrinsicUnaryType (adr, n, addressN)
3850 ELSIF c = sizeN
3851 THEN
3852 RETURN makeIntrinsicUnaryType (size, n, cardinalN)
3853 ELSIF c = tsizeN
3854 THEN
3855 RETURN makeIntrinsicUnaryType (tsize, n, cardinalN)
3856 ELSIF c = floatN
3857 THEN
3858 RETURN makeIntrinsicUnaryType (float, n, realN)
3859 ELSIF c = truncN
3860 THEN
3861 RETURN makeIntrinsicUnaryType (trunc, n, integerN)
3862 ELSIF c = ordN
3863 THEN
3864 RETURN makeIntrinsicUnaryType (ord, n, cardinalN)
3865 ELSIF c = chrN
3866 THEN
3867 RETURN makeIntrinsicUnaryType (chr, n, charN)
3868 ELSIF c = capN
3869 THEN
3870 RETURN makeIntrinsicUnaryType (cap, n, charN)
3871 ELSIF c = absN
3872 THEN
3873 RETURN makeIntrinsicUnaryType (abs, n, NIL)
3874 ELSIF c = imN
3875 THEN
3876 RETURN makeIntrinsicUnaryType (im, n, NIL)
3877 ELSIF c = reN
3878 THEN
3879 RETURN makeIntrinsicUnaryType (re, n, NIL)
3880 ELSIF c = cmplxN
3881 THEN
3882 RETURN makeIntrinsicBinaryType (cmplx, n, NIL)
3883 ELSIF c = highN
3884 THEN
3885 RETURN makeIntrinsicUnaryType (high, n, cardinalN)
3886 ELSIF c = incN
3887 THEN
3888 RETURN makeIntrinsicProc (inc, expListLen (n), n)
3889 ELSIF c = decN
3890 THEN
3891 RETURN makeIntrinsicProc (dec, expListLen (n), n)
3892 ELSIF c = inclN
3893 THEN
3894 RETURN makeIntrinsicProc (incl, expListLen (n), n)
3895 ELSIF c = exclN
3896 THEN
3897 RETURN makeIntrinsicProc (excl, expListLen (n), n)
3898 ELSIF c = newN
3899 THEN
3900 RETURN makeIntrinsicProc (new, 1, n)
3901 ELSIF c = disposeN
3902 THEN
3903 RETURN makeIntrinsicProc (dispose, 1, n)
3904 ELSIF c = lengthN
3905 THEN
3906 RETURN makeIntrinsicUnaryType (length, n, cardinalN)
3907 ELSIF c = throwN
3908 THEN
3909 keyc.useThrow ;
3910 RETURN makeIntrinsicProc (throw, 1, n)
3911 END ;
3912 RETURN NIL
3913 END checkIntrinsic ;
3914
3915
3916 (*
3917 checkCHeaders - check to see if the function is a C system function and
3918 requires a header file included.
3919 *)
3920
3921 PROCEDURE checkCHeaders (c: node) ;
3922 VAR
3923 name: Name ;
3924 s : node ;
3925 BEGIN
3926 IF isProcedure (c)
3927 THEN
3928 s := getScope (c) ;
3929 IF getSymName (s) = makeKey ('libc')
3930 THEN
3931 name := getSymName (c) ;
3932 IF (name = makeKey ('read')) OR
3933 (name = makeKey ('write')) OR
3934 (name = makeKey ('open')) OR
3935 (name = makeKey ('close'))
3936 THEN
3937 keyc.useUnistd
3938 END
3939 END
3940 END
3941 END checkCHeaders ;
3942
3943
3944 (*
3945 makeFuncCall - builds a function call to c with param list, n.
3946 *)
3947
3948 PROCEDURE makeFuncCall (c, n: node) : node ;
3949 VAR
3950 f: node ;
3951 BEGIN
3952 assert ((n=NIL) OR isExpList (n)) ;
3953 IF (c = haltN) AND
3954 (getMainModule () # lookupDef (makeKey ('M2RTS'))) AND
3955 (getMainModule () # lookupImp (makeKey ('M2RTS')))
3956 THEN
3957 addImportedModule (getMainModule (), lookupDef (makeKey ('M2RTS')), FALSE)
3958 END ;
3959 f := checkIntrinsic (c, n) ;
3960 checkCHeaders (c) ;
3961 IF f = NIL
3962 THEN
3963 f := newNode (funccall) ;
3964 f^.funccallF.function := c ;
3965 f^.funccallF.args := n ;
3966 f^.funccallF.type := getType (c) ;
3967 initPair (f^.funccallF.funccallComment) ;
3968 initNodeOpaqueState (f)
3969 END ;
3970 RETURN f
3971 END makeFuncCall ;
3972
3973
3974 (*
3975 isFuncCall - returns TRUE if, n, is a function/procedure call.
3976 *)
3977
3978 PROCEDURE isFuncCall (n: node) : BOOLEAN ;
3979 BEGIN
3980 assert (n # NIL) ;
3981 RETURN n^.kind = funccall
3982 END isFuncCall ;
3983
3984
3985 (*
3986 putType - places, exp, as the type alias to des.
3987 TYPE des = exp ;
3988 *)
3989
3990 PROCEDURE putType (des, exp: node) ;
3991 BEGIN
3992 assert (des#NIL) ;
3993 assert (isType (des)) ;
3994 des^.typeF.type := exp
3995 END putType ;
3996
3997
3998 (*
3999 putTypeHidden - marks type, des, as being a hidden type.
4000 TYPE des ;
4001 *)
4002
4003 PROCEDURE putTypeHidden (des: node) ;
4004 VAR
4005 s: node ;
4006 BEGIN
4007 assert (des#NIL) ;
4008 assert (isType (des)) ;
4009 des^.typeF.isHidden := TRUE ;
4010 s := getScope (des) ;
4011 assert (isDef (s)) ;
4012 s^.defF.hasHidden := TRUE
4013 END putTypeHidden ;
4014
4015
4016 (*
4017 isTypeHidden - returns TRUE if type, n, is hidden.
4018 *)
4019
4020 PROCEDURE isTypeHidden (n: node) : BOOLEAN ;
4021 BEGIN
4022 assert (n#NIL) ;
4023 assert (isType (n)) ;
4024 RETURN n^.typeF.isHidden
4025 END isTypeHidden ;
4026
4027
4028 (*
4029 hasHidden - returns TRUE if module, n, has a hidden type.
4030 *)
4031
4032 PROCEDURE hasHidden (n: node) : BOOLEAN ;
4033 BEGIN
4034 assert (isDef (n)) ;
4035 RETURN n^.defF.hasHidden
4036 END hasHidden ;
4037
4038
4039 (*
4040 putTypeOpaque - marks type, des, as being an opaque type.
4041 TYPE des ;
4042 *)
4043
4044 PROCEDURE putTypeOpaque (des: node) ;
4045 VAR
4046 s: node ;
4047 BEGIN
4048 assert (des#NIL) ;
4049 assert (isType (des)) ;
4050 des^.typeF.isOpaque := TRUE
4051 END putTypeOpaque ;
4052
4053
4054 (*
4055 isTypeOpaque - returns TRUE if type, n, is an opaque type.
4056 *)
4057
4058 PROCEDURE isTypeOpaque (n: node) : BOOLEAN ;
4059 BEGIN
4060 assert (n#NIL) ;
4061 assert (isType (n)) ;
4062 RETURN n^.typeF.isOpaque
4063 END isTypeOpaque ;
4064
4065
4066 (*
4067 putTypeInternal - marks type, des, as being an internally generated type.
4068 *)
4069
4070 PROCEDURE putTypeInternal (des: node) ;
4071 BEGIN
4072 assert (des#NIL) ;
4073 assert (isType (des)) ;
4074 des^.typeF.isInternal := TRUE
4075 END putTypeInternal ;
4076
4077
4078 (*
4079 isTypeInternal - returns TRUE if type, n, is internal.
4080 *)
4081
4082 PROCEDURE isTypeInternal (n: node) : BOOLEAN ;
4083 BEGIN
4084 assert (n#NIL) ;
4085 assert (isType (n)) ;
4086 RETURN n^.typeF.isInternal
4087 END isTypeInternal ;
4088
4089
4090 (*
4091 putConst - places value, v, into node, n.
4092 *)
4093
4094 PROCEDURE putConst (n: node; v: node) ;
4095 BEGIN
4096 assert (isConst (n)) ;
4097 n^.constF.value := v
4098 END putConst ;
4099
4100
4101 (*
4102 makeLiteralInt - creates and returns a literal node based on an integer type.
4103 *)
4104
4105 PROCEDURE makeLiteralInt (n: Name) : node ;
4106 VAR
4107 m: node ;
4108 s: String ;
4109 BEGIN
4110 m := newNode (literal) ;
4111 s := InitStringCharStar (keyToCharStar (n)) ;
4112 WITH m^ DO
4113 literalF.name := n ;
4114 IF DynamicStrings.char (s, -1)='C'
4115 THEN
4116 literalF.type := charN
4117 ELSE
4118 literalF.type := ztypeN
4119 END
4120 END ;
4121 s := KillString (s) ;
4122 RETURN m
4123 END makeLiteralInt ;
4124
4125
4126 (*
4127 makeLiteralReal - creates and returns a literal node based on a real type.
4128 *)
4129
4130 PROCEDURE makeLiteralReal (n: Name) : node ;
4131 VAR
4132 m: node ;
4133 BEGIN
4134 m := newNode (literal) ;
4135 WITH m^ DO
4136 literalF.name := n ;
4137 literalF.type := rtypeN
4138 END ;
4139 RETURN m
4140 END makeLiteralReal ;
4141
4142
4143 (*
4144 makeString - creates and returns a node containing string, n.
4145 *)
4146
4147 PROCEDURE makeString (n: Name) : node ;
4148 VAR
4149 m: node ;
4150 BEGIN
4151 m := newNode (string) ;
4152 WITH m^ DO
4153 stringF.name := n ;
4154 stringF.length := lengthKey (n) ;
4155 stringF.isCharCompatible := (stringF.length <= 3) ;
4156 stringF.cstring := toCstring (n) ;
4157 stringF.clength := lenCstring (stringF.cstring) ;
4158 IF stringF.isCharCompatible
4159 THEN
4160 stringF.cchar := toCchar (n)
4161 ELSE
4162 stringF.cchar := NIL
4163 END
4164 END ;
4165 RETURN m
4166 END makeString ;
4167
4168
4169 (*
4170 getBuiltinConst - creates and returns a builtin const if available.
4171 *)
4172
4173 PROCEDURE getBuiltinConst (n: Name) : node ;
4174 BEGIN
4175 IF n=makeKey ('BITS_PER_UNIT')
4176 THEN
4177 RETURN bitsperunitN
4178 ELSIF n=makeKey ('BITS_PER_WORD')
4179 THEN
4180 RETURN bitsperwordN
4181 ELSIF n=makeKey ('BITS_PER_CHAR')
4182 THEN
4183 RETURN bitspercharN
4184 ELSIF n=makeKey ('UNITS_PER_WORD')
4185 THEN
4186 RETURN unitsperwordN
4187 ELSE
4188 RETURN NIL
4189 END
4190 END getBuiltinConst ;
4191
4192
4193 (*
4194 lookupInScope - looks up a symbol named, n, from, scope.
4195 *)
4196
4197 PROCEDURE lookupInScope (scope: node; n: Name) : node ;
4198 BEGIN
4199 CASE scope^.kind OF
4200
4201 def : RETURN getSymKey (scope^.defF.decls.symbols, n) |
4202 module : RETURN getSymKey (scope^.moduleF.decls.symbols, n) |
4203 imp : RETURN getSymKey (scope^.impF.decls.symbols, n) |
4204 procedure: RETURN getSymKey (scope^.procedureF.decls.symbols, n) |
4205 record : RETURN getSymKey (scope^.recordF.localSymbols, n)
4206
4207 END
4208 END lookupInScope ;
4209
4210
4211 (*
4212 lookupBase - return node named n from the base symbol scope.
4213 *)
4214
4215 PROCEDURE lookupBase (n: Name) : node ;
4216 VAR
4217 m: node ;
4218 BEGIN
4219 m := getSymKey (baseSymbols, n) ;
4220 IF m=procN
4221 THEN
4222 keyc.useProc
4223 ELSIF (m=complexN) OR (m=longcomplexN) OR (m=shortcomplexN)
4224 THEN
4225 keyc.useComplex
4226 END ;
4227 RETURN m
4228 END lookupBase ;
4229
4230
4231 (*
4232 dumpScopes - display the names of all the scopes stacked.
4233 *)
4234
4235 PROCEDURE dumpScopes ;
4236 VAR
4237 h: CARDINAL ;
4238 s: node ;
4239 BEGIN
4240 h := HighIndice (scopeStack) ;
4241 printf ("total scopes stacked %d\n", h);
4242
4243 WHILE h>=1 DO
4244 s := GetIndice (scopeStack, h) ;
4245 out2 (" scope [%d] is %s\n", h, s) ;
4246 DEC (h)
4247 END
4248 END dumpScopes ;
4249
4250
4251 (*
4252 out0 - write string a to StdOut.
4253 *)
4254
4255 PROCEDURE out0 (a: ARRAY OF CHAR) ;
4256 VAR
4257 m: String ;
4258 BEGIN
4259 m := Sprintf0 (InitString (a)) ;
4260 m := KillString (WriteS (StdOut, m))
4261 END out0 ;
4262
4263
4264 (*
4265 out1 - write string a to StdOut using format specifier a.
4266 *)
4267
4268 PROCEDURE out1 (a: ARRAY OF CHAR; s: node) ;
4269 VAR
4270 m: String ;
4271 d: CARDINAL ;
4272 BEGIN
4273 m := getFQstring (s) ;
4274 IF EqualArray (m, '')
4275 THEN
4276 d := VAL (CARDINAL, VAL (LONGCARD, s)) ;
4277 m := KillString (m) ;
4278 m := Sprintf1 (InitString ('[%d]'), d)
4279 END ;
4280 m := Sprintf1 (InitString (a), m) ;
4281 m := KillString (WriteS (StdOut, m))
4282 END out1 ;
4283
4284
4285 (*
4286 out2 - write string a to StdOut using format specifier a.
4287 *)
4288
4289 PROCEDURE out2 (a: ARRAY OF CHAR; c: CARDINAL; s: node) ;
4290 VAR
4291 m, m1: String ;
4292 BEGIN
4293 m1 := getString (s) ;
4294 m := Sprintf2 (InitString (a), c, m1) ;
4295 m := KillString (WriteS (StdOut, m)) ;
4296 m1 := KillString (m1)
4297 END out2 ;
4298
4299
4300 (*
4301 out3 - write string a to StdOut using format specifier a.
4302 *)
4303
4304 PROCEDURE out3 (a: ARRAY OF CHAR; l: CARDINAL; n: Name; s: node) ;
4305 VAR
4306 m, m1, m2: String ;
4307 BEGIN
4308 m1 := InitStringCharStar (keyToCharStar (n)) ;
4309 m2 := getString (s) ;
4310 m := Sprintf3 (InitString (a), l, m1, m2) ;
4311 m := KillString (WriteS (StdOut, m)) ;
4312 m1 := KillString (m1) ;
4313 m2 := KillString (m2)
4314 END out3 ;
4315
4316
4317 (*
4318 lookupSym - returns the symbol named, n, from the scope stack.
4319 *)
4320
4321 PROCEDURE lookupSym (n: Name) : node ;
4322 VAR
4323 s, m: node ;
4324 l, h: CARDINAL ;
4325 BEGIN
4326 l := LowIndice (scopeStack) ;
4327 h := HighIndice (scopeStack) ;
4328
4329 WHILE h>=l DO
4330 s := GetIndice (scopeStack, h) ;
4331 m := lookupInScope (s, n) ;
4332 IF debugScopes AND (m=NIL)
4333 THEN
4334 out3 (" [%d] search for symbol name %s in scope %s\n", h, n, s)
4335 END ;
4336 IF m#NIL
4337 THEN
4338 IF debugScopes
4339 THEN
4340 out3 (" [%d] search for symbol name %s in scope %s (found)\n", h, n, s)
4341 END ;
4342 RETURN m
4343 END ;
4344 DEC (h)
4345 END ;
4346 RETURN lookupBase (n)
4347 END lookupSym ;
4348
4349
4350 (*
4351 getSymName - returns the name of symbol, n.
4352 *)
4353
4354 PROCEDURE getSymName (n: node) : Name ;
4355 BEGIN
4356 WITH n^ DO
4357 CASE kind OF
4358
4359 new : RETURN makeKey ('NEW') |
4360 dispose : RETURN makeKey ('DISPOSE') |
4361 length : RETURN makeKey ('LENGTH') |
4362 inc : RETURN makeKey ('INC') |
4363 dec : RETURN makeKey ('DEC') |
4364 incl : RETURN makeKey ('INCL') |
4365 excl : RETURN makeKey ('EXCL') |
4366 nil : RETURN makeKey ('NIL') |
4367 true : RETURN makeKey ('TRUE') |
4368 false : RETURN makeKey ('FALSE') |
4369 address : RETURN makeKey ('ADDRESS') |
4370 loc : RETURN makeKey ('LOC') |
4371 byte : RETURN makeKey ('BYTE') |
4372 word : RETURN makeKey ('WORD') |
4373 csizet : RETURN makeKey ('CSIZE_T') |
4374 cssizet : RETURN makeKey ('CSSIZE_T') |
4375 cofft : RETURN makeKey ('COFF_T') |
4376 cardinal64 : RETURN makeKey ('CARDINAL64') |
4377
4378 (* base types. *)
4379 boolean : RETURN makeKey ('BOOLEAN') |
4380 proc : RETURN makeKey ('PROC') |
4381 char : RETURN makeKey ('CHAR') |
4382 cardinal : RETURN makeKey ('CARDINAL') |
4383 longcard : RETURN makeKey ('LONGCARD') |
4384 shortcard : RETURN makeKey ('SHORTCARD') |
4385 integer : RETURN makeKey ('INTEGER') |
4386 longint : RETURN makeKey ('LONGINT') |
4387 shortint : RETURN makeKey ('SHORTINT') |
4388 real : RETURN makeKey ('REAL') |
4389 longreal : RETURN makeKey ('LONGREAL') |
4390 shortreal : RETURN makeKey ('SHORTREAL') |
4391 bitset : RETURN makeKey ('BITSET') |
4392 ztype : RETURN makeKey ('_ZTYPE') |
4393 rtype : RETURN makeKey ('_RTYPE') |
4394 complex : RETURN makeKey ('COMPLEX') |
4395 longcomplex : RETURN makeKey ('LONGCOMPLEX') |
4396 shortcomplex : RETURN makeKey ('SHORTCOMPLEX') |
4397
4398 (* language features and compound type attributes. *)
4399 type : RETURN typeF.name |
4400 record : RETURN NulName |
4401 varient : RETURN NulName |
4402 var : RETURN varF.name |
4403 enumeration : RETURN NulName |
4404 subrange : RETURN NulName |
4405 pointer : RETURN NulName |
4406 array : RETURN NulName |
4407 string : RETURN stringF.name |
4408 const : RETURN constF.name |
4409 literal : RETURN literalF.name |
4410 varparam : RETURN NulName |
4411 param : RETURN NulName |
4412 optarg : RETURN NulName |
4413 recordfield : RETURN recordfieldF.name |
4414 varientfield : RETURN varientfieldF.name |
4415 enumerationfield: RETURN enumerationfieldF.name |
4416 set : RETURN NulName |
4417 proctype : RETURN NulName |
4418 subscript : RETURN NulName |
4419 (* blocks. *)
4420 procedure : RETURN procedureF.name |
4421 def : RETURN defF.name |
4422 imp : RETURN impF.name |
4423 module : RETURN moduleF.name |
4424 (* statements. *)
4425 loop,
4426 while,
4427 for,
4428 repeat,
4429 if,
4430 elsif,
4431 assignment : RETURN NulName |
4432 (* expressions. *)
4433 constexp,
4434 deref,
4435 arrayref,
4436 componentref,
4437 cast,
4438 val,
4439 plus,
4440 sub,
4441 div,
4442 mod,
4443 mult,
4444 divide,
4445 in,
4446 neg,
4447 equal,
4448 notequal,
4449 less,
4450 greater,
4451 greequal,
4452 lessequal : RETURN NulName |
4453 adr : RETURN makeKey ('ADR') |
4454 size : RETURN makeKey ('SIZE') |
4455 tsize : RETURN makeKey ('TSIZE') |
4456 chr : RETURN makeKey ('CHR') |
4457 abs : RETURN makeKey ('ABS') |
4458 ord : RETURN makeKey ('ORD') |
4459 float : RETURN makeKey ('FLOAT') |
4460 trunc : RETURN makeKey ('TRUNC') |
4461 high : RETURN makeKey ('HIGH') |
4462 throw : RETURN makeKey ('THROW') |
4463 unreachable : RETURN makeKey ('builtin_unreachable') |
4464 cmplx : RETURN makeKey ('CMPLX') |
4465 re : RETURN makeKey ('RE') |
4466 im : RETURN makeKey ('IM') |
4467 max : RETURN makeKey ('MAX') |
4468 min : RETURN makeKey ('MIN') |
4469 pointerref : RETURN NulName |
4470 funccall : RETURN NulName |
4471 identlist : RETURN NulName
4472
4473 ELSE
4474 HALT
4475 END
4476 END
4477 END getSymName ;
4478
4479
4480 (*
4481 isUnary - returns TRUE if, n, is an unary node.
4482 *)
4483
4484 PROCEDURE isUnary (n: node) : BOOLEAN ;
4485 BEGIN
4486 assert (n # NIL) ;
4487 CASE n^.kind OF
4488
4489 length,
4490 re,
4491 im,
4492 deref,
4493 high,
4494 chr,
4495 cap,
4496 abs,
4497 ord,
4498 float,
4499 trunc,
4500 constexp,
4501 not,
4502 neg,
4503 adr,
4504 size,
4505 tsize,
4506 min,
4507 max : RETURN TRUE
4508
4509 ELSE
4510 RETURN FALSE
4511 END
4512 END isUnary ;
4513
4514
4515 (*
4516 isBinary - returns TRUE if, n, is an binary node.
4517 *)
4518
4519 PROCEDURE isBinary (n: node) : BOOLEAN ;
4520 BEGIN
4521 assert (n # NIL) ;
4522 CASE n^.kind OF
4523
4524 cmplx,
4525 and,
4526 or,
4527 equal,
4528 notequal,
4529 less,
4530 greater,
4531 greequal,
4532 lessequal,
4533 val,
4534 cast,
4535 plus,
4536 sub,
4537 div,
4538 mod,
4539 mult,
4540 divide,
4541 in : RETURN TRUE
4542
4543 ELSE
4544 RETURN FALSE
4545 END
4546 END isBinary ;
4547
4548
4549 (*
4550 makeUnary - create a unary expression node with, e, as the argument
4551 and res as the return type.
4552 *)
4553
4554 PROCEDURE makeUnary (k: nodeT; e: node; res: node) : node ;
4555 VAR
4556 n: node ;
4557 BEGIN
4558 IF k=plus
4559 THEN
4560 RETURN e
4561 ELSE
4562 n := newNode (k) ;
4563 WITH n^ DO
4564 CASE kind OF
4565
4566 min,
4567 max,
4568 throw,
4569 re,
4570 im,
4571 deref,
4572 high,
4573 chr,
4574 cap,
4575 abs,
4576 ord,
4577 float,
4578 trunc,
4579 length,
4580 constexp,
4581 not,
4582 neg,
4583 adr,
4584 size,
4585 tsize: WITH unaryF DO
4586 arg := e ;
4587 resultType := res
4588 END
4589
4590 END
4591 END
4592 END ;
4593 RETURN n
4594 END makeUnary ;
4595
4596
4597 (*
4598 isLeafString - returns TRUE if n is a leaf node which is a string constant.
4599 *)
4600
4601 PROCEDURE isLeafString (n: node) : BOOLEAN ;
4602 BEGIN
4603 RETURN isString (n) OR
4604 (isLiteral (n) AND (getType (n) = charN)) OR
4605 (isConst (n) AND (getExprType (n) = charN))
4606 END isLeafString ;
4607
4608
4609 (*
4610 getLiteralStringContents - return the contents of a literal node as a string.
4611 *)
4612
4613 PROCEDURE getLiteralStringContents (n: node) : String ;
4614 VAR
4615 number,
4616 content,
4617 s : String ;
4618 BEGIN
4619 assert (n^.kind = literal) ;
4620 s := InitStringCharStar (keyToCharStar (n^.literalF.name)) ;
4621 content := NIL ;
4622 IF n^.literalF.type = charN
4623 THEN
4624 IF DynamicStrings.char (s, -1) = 'C'
4625 THEN
4626 IF DynamicStrings.Length (s) > 1
4627 THEN
4628 number := DynamicStrings.Slice (s, 0, -1) ;
4629 content := DynamicStrings.InitStringChar (VAL (CHAR, ostoc (number))) ;
4630 number := DynamicStrings.KillString (number)
4631 ELSE
4632 content := DynamicStrings.InitStringChar ('C')
4633 END
4634 ELSE
4635 content := DynamicStrings.Dup (s)
4636 END
4637 ELSE
4638 metaError1 ('cannot obtain string contents from {%1k}', n^.literalF.name)
4639 END ;
4640 s := DynamicStrings.KillString (s) ;
4641 RETURN content
4642 END getLiteralStringContents ;
4643
4644
4645 (*
4646 getStringContents - return the string contents of a constant, literal,
4647 string or a constexp node.
4648 *)
4649
4650 PROCEDURE getStringContents (n: node) : String ;
4651 BEGIN
4652 IF isConst (n)
4653 THEN
4654 RETURN getStringContents (n^.constF.value)
4655 ELSIF isLiteral (n)
4656 THEN
4657 RETURN getLiteralStringContents (n)
4658 ELSIF isString (n)
4659 THEN
4660 RETURN getString (n)
4661 ELSIF isConstExp (n)
4662 THEN
4663 RETURN getStringContents (n^.unaryF.arg)
4664 END ;
4665 HALT
4666 END getStringContents ;
4667
4668
4669 (*
4670 addNames -
4671 *)
4672
4673 PROCEDURE addNames (a, b: node) : Name ;
4674 VAR
4675 sa, sb: String ;
4676 n : Name ;
4677 BEGIN
4678 sa := DynamicStrings.InitStringCharStar (keyToCharStar (getSymName (a))) ;
4679 sb := DynamicStrings.InitStringCharStar (keyToCharStar (getSymName (b))) ;
4680 sa := ConCat (sa, sb) ;
4681 n := makekey (DynamicStrings.string (sa)) ;
4682 sa := KillString (sa) ;
4683 sb := KillString (sb) ;
4684 RETURN n
4685 END addNames ;
4686
4687
4688 (*
4689 resolveString -
4690 *)
4691
4692 PROCEDURE resolveString (n: node) : node ;
4693 BEGIN
4694 WHILE isConst (n) OR isConstExp (n) DO
4695 IF isConst (n)
4696 THEN
4697 n := n^.constF.value
4698 ELSE
4699 n := n^.unaryF.arg
4700 END
4701 END ;
4702 IF n^.kind = plus
4703 THEN
4704 n := makeString (addNames (resolveString (n^.binaryF.left),
4705 resolveString (n^.binaryF.right)))
4706 END ;
4707 RETURN n
4708 END resolveString ;
4709
4710
4711 (*
4712 foldBinary -
4713 *)
4714
4715 PROCEDURE foldBinary (k: nodeT; l, r: node; res: node) : node ;
4716 VAR
4717 n : node ;
4718 ls,
4719 rs: String ;
4720 BEGIN
4721 n := NIL ;
4722 IF (k = plus) AND isLeafString (l) AND isLeafString (r)
4723 THEN
4724 ls := getStringContents (l) ;
4725 rs := getStringContents (r) ;
4726 ls := DynamicStrings.Add (ls, rs) ;
4727 n := makeString (makekey (DynamicStrings.string (ls))) ;
4728 ls := DynamicStrings.KillString (ls) ;
4729 rs := DynamicStrings.KillString (rs)
4730 END ;
4731 RETURN n
4732 END foldBinary ;
4733
4734
4735 (*
4736 makeBinary - create a binary node with left/right/result type: l, r and resultType.
4737 *)
4738
4739 PROCEDURE makeBinary (k: nodeT; l, r: node; resultType: node) : node ;
4740 VAR
4741 n: node ;
4742 BEGIN
4743 n := foldBinary (k, l, r, resultType) ;
4744 IF n = NIL
4745 THEN
4746 n := doMakeBinary (k, l, r, resultType)
4747 END ;
4748 RETURN n
4749 END makeBinary ;
4750
4751
4752 (*
4753 doMakeBinary - returns a binary node containing left/right/result values
4754 l, r, res, with a node operator, k.
4755 *)
4756
4757 PROCEDURE doMakeBinary (k: nodeT; l, r: node; res: node) : node ;
4758 VAR
4759 n: node ;
4760 BEGIN
4761 n := newNode (k) ;
4762 WITH n^ DO
4763 CASE kind OF
4764
4765 cmplx,
4766 equal,
4767 notequal,
4768 less,
4769 greater,
4770 greequal,
4771 lessequal,
4772 and,
4773 or,
4774 cast,
4775 val,
4776 plus,
4777 sub,
4778 div,
4779 mod,
4780 mult,
4781 divide,
4782 in : WITH binaryF DO
4783 left := l ;
4784 right := r ;
4785 resultType := res
4786 END
4787
4788 END
4789 END ;
4790 RETURN n
4791 END doMakeBinary ;
4792
4793
4794 (*
4795 doMakeComponentRef -
4796 *)
4797
4798 PROCEDURE doMakeComponentRef (rec, field: node) : node ;
4799 VAR
4800 n: node ;
4801 BEGIN
4802 n := newNode (componentref) ;
4803 n^.componentrefF.rec := rec ;
4804 n^.componentrefF.field := field ;
4805 n^.componentrefF.resultType := getType (field) ;
4806 initNodeOpaqueState (n) ;
4807 RETURN n
4808 END doMakeComponentRef ;
4809
4810
4811 (*
4812 makeComponentRef - build a componentref node which accesses, field,
4813 within, record, rec.
4814 *)
4815
4816 PROCEDURE makeComponentRef (rec, field: node) : node ;
4817 VAR
4818 n, a: node ;
4819 BEGIN
4820 (*
4821 n := getLastOp (rec) ;
4822 IF (n#NIL) AND (isDeref (n) OR isPointerRef (n)) AND
4823 (skipType (getType (rec)) = skipType (getType (n)))
4824 THEN
4825 a := n^.unaryF.arg ;
4826 n^.kind := pointerref ;
4827 n^.pointerrefF.ptr := a ;
4828 n^.pointerrefF.field := field ;
4829 n^.pointerrefF.resultType := getType (field) ;
4830 RETURN n
4831 ELSE
4832 RETURN doMakeComponentRef (rec, field)
4833 END
4834 *)
4835 IF isDeref (rec)
4836 THEN
4837 a := rec^.unaryF.arg ;
4838 rec^.kind := pointerref ;
4839 rec^.pointerrefF.ptr := a ;
4840 rec^.pointerrefF.field := field ;
4841 rec^.pointerrefF.resultType := getType (field) ;
4842 initNodeOpaqueState (rec) ;
4843 RETURN rec
4844 ELSE
4845 RETURN doMakeComponentRef (rec, field)
4846 END
4847 END makeComponentRef ;
4848
4849
4850 (*
4851 isComponentRef -
4852 *)
4853
4854 PROCEDURE isComponentRef (n: node) : BOOLEAN ;
4855 BEGIN
4856 assert (n # NIL) ;
4857 RETURN n^.kind = componentref
4858 END isComponentRef ;
4859
4860
4861 (*
4862 makePointerRef - build a pointerref node which accesses, field,
4863 within, pointer to record, ptr.
4864 *)
4865
4866 PROCEDURE makePointerRef (ptr, field: node) : node ;
4867 VAR
4868 n: node ;
4869 BEGIN
4870 n := newNode (pointerref) ;
4871 n^.pointerrefF.ptr := ptr ;
4872 n^.pointerrefF.field := field ;
4873 n^.pointerrefF.resultType := getType (field) ;
4874 initNodeOpaqueState (n) ;
4875 RETURN n
4876 END makePointerRef ;
4877
4878
4879 (*
4880 isPointerRef - returns TRUE if, n, is a pointerref node.
4881 *)
4882
4883 PROCEDURE isPointerRef (n: node) : BOOLEAN ;
4884 BEGIN
4885 assert (n # NIL) ;
4886 RETURN n^.kind = pointerref
4887 END isPointerRef ;
4888
4889
4890 (*
4891 makeArrayRef - build an arrayref node which access element,
4892 index, in, array. array is a variable/expression/constant
4893 which has a type array.
4894 *)
4895
4896 PROCEDURE makeArrayRef (array, index: node) : node ;
4897 VAR
4898 n, t: node ;
4899 i, j: CARDINAL ;
4900 BEGIN
4901 n := newNode (arrayref) ;
4902 n^.arrayrefF.array := array ;
4903 n^.arrayrefF.index := index ;
4904 t := array ;
4905 j := expListLen (index) ;
4906 i := 1 ;
4907 t := skipType (getType (t)) ;
4908 REPEAT
4909 IF isArray (t)
4910 THEN
4911 t := skipType (getType (t))
4912 ELSE
4913 metaError2 ('cannot access {%1N} dimension of array {%2a}', i, t)
4914 END ;
4915 INC (i)
4916 UNTIL i > j ;
4917 n^.arrayrefF.resultType := t ;
4918 RETURN n
4919 END makeArrayRef ;
4920
4921
4922 (*
4923 isArrayRef - returns TRUE if the node was an arrayref.
4924 *)
4925
4926 PROCEDURE isArrayRef (n: node) : BOOLEAN ;
4927 BEGIN
4928 assert (n # NIL) ;
4929 RETURN n^.kind = arrayref
4930 END isArrayRef ;
4931
4932
4933 (*
4934 makeDeRef - dereferences the pointer defined by, n.
4935 *)
4936
4937 PROCEDURE makeDeRef (n: node) : node ;
4938 VAR
4939 t: node ;
4940 BEGIN
4941 t := skipType (getType (n)) ;
4942 assert (isPointer (t)) ;
4943 RETURN makeUnary (deref, n, getType (t))
4944 END makeDeRef ;
4945
4946
4947 (*
4948 isDeref - returns TRUE if, n, is a deref node.
4949 *)
4950
4951 PROCEDURE isDeref (n: node) : BOOLEAN ;
4952 BEGIN
4953 assert (n # NIL) ;
4954 RETURN n^.kind = deref
4955 END isDeref ;
4956
4957
4958 (*
4959 makeBase - create a base type or constant.
4960 It only supports the base types and constants
4961 enumerated below.
4962 *)
4963
4964 PROCEDURE makeBase (k: nodeT) : node ;
4965 VAR
4966 n: node ;
4967 BEGIN
4968 n := newNode (k) ;
4969 WITH n^ DO
4970 CASE k OF
4971
4972 new,
4973 dispose,
4974 length,
4975 inc,
4976 dec,
4977 incl,
4978 excl,
4979 nil,
4980 true,
4981 false,
4982 address,
4983 loc,
4984 byte,
4985 word,
4986 csizet,
4987 cssizet,
4988 cofft,
4989 cardinal64,
4990 char,
4991 cardinal,
4992 longcard,
4993 shortcard,
4994 integer,
4995 longint,
4996 shortint,
4997 real,
4998 longreal,
4999 shortreal,
5000 bitset,
5001 boolean,
5002 proc,
5003 ztype,
5004 rtype,
5005 complex,
5006 longcomplex,
5007 shortcomplex,
5008 adr,
5009 chr,
5010 cap,
5011 abs,
5012 float,
5013 trunc,
5014 ord,
5015 high,
5016 throw,
5017 re,
5018 im,
5019 cmplx,
5020 size,
5021 tsize,
5022 val,
5023 min,
5024 max : (* legal kind. *) |
5025
5026 ELSE
5027 HALT
5028 END
5029 END ;
5030 RETURN n
5031 END makeBase ;
5032
5033
5034 (*
5035 makeBinaryTok - creates and returns a boolean type node with,
5036 l, and, r, nodes.
5037 *)
5038
5039 PROCEDURE makeBinaryTok (op: toktype; l, r: node) : node ;
5040 BEGIN
5041 IF op=equaltok
5042 THEN
5043 RETURN makeBinary (equal, l, r, booleanN)
5044 ELSIF (op=hashtok) OR (op=lessgreatertok)
5045 THEN
5046 RETURN makeBinary (notequal, l, r, booleanN)
5047 ELSIF op=lesstok
5048 THEN
5049 RETURN makeBinary (less, l, r, booleanN)
5050 ELSIF op=greatertok
5051 THEN
5052 RETURN makeBinary (greater, l, r, booleanN)
5053 ELSIF op=greaterequaltok
5054 THEN
5055 RETURN makeBinary (greequal, l, r, booleanN)
5056 ELSIF op=lessequaltok
5057 THEN
5058 RETURN makeBinary (lessequal, l, r, booleanN)
5059 ELSIF op=andtok
5060 THEN
5061 RETURN makeBinary (and, l, r, booleanN)
5062 ELSIF op=ortok
5063 THEN
5064 RETURN makeBinary (or, l, r, booleanN)
5065 ELSIF op=plustok
5066 THEN
5067 RETURN makeBinary (plus, l, r, NIL)
5068 ELSIF op=minustok
5069 THEN
5070 RETURN makeBinary (sub, l, r, NIL)
5071 ELSIF op=divtok
5072 THEN
5073 RETURN makeBinary (div, l, r, NIL)
5074 ELSIF op=timestok
5075 THEN
5076 RETURN makeBinary (mult, l, r, NIL)
5077 ELSIF op=modtok
5078 THEN
5079 RETURN makeBinary (mod, l, r, NIL)
5080 ELSIF op=intok
5081 THEN
5082 RETURN makeBinary (in, l, r, NIL)
5083 ELSIF op=dividetok
5084 THEN
5085 RETURN makeBinary (divide, l, r, NIL)
5086 ELSE
5087 HALT (* most likely op needs a clause as above. *)
5088 END
5089 END makeBinaryTok ;
5090
5091
5092 (*
5093 makeUnaryTok - creates and returns a boolean type node with,
5094 e, node.
5095 *)
5096
5097 PROCEDURE makeUnaryTok (op: toktype; e: node) : node ;
5098 BEGIN
5099 IF op=nottok
5100 THEN
5101 RETURN makeUnary (not, e, booleanN)
5102 ELSIF op=plustok
5103 THEN
5104 RETURN makeUnary (plus, e, NIL)
5105 ELSIF op=minustok
5106 THEN
5107 RETURN makeUnary (neg, e, NIL)
5108 ELSE
5109 HALT (* most likely op needs a clause as above. *)
5110 END
5111 END makeUnaryTok ;
5112
5113
5114 (*
5115 isOrdinal - returns TRUE if, n, is an ordinal type.
5116 *)
5117
5118 PROCEDURE isOrdinal (n: node) : BOOLEAN ;
5119 BEGIN
5120 CASE n^.kind OF
5121
5122 address,
5123 loc,
5124 byte,
5125 word,
5126 csizet,
5127 cssizet,
5128 cofft,
5129 cardinal64,
5130 char,
5131 integer,
5132 longint,
5133 shortint,
5134 cardinal,
5135 longcard,
5136 shortcard,
5137 bitset : RETURN TRUE
5138
5139 ELSE
5140 RETURN FALSE
5141 END
5142 END isOrdinal ;
5143
5144
5145 (*
5146 getType - returns the type associated with node, n.
5147 *)
5148
5149 PROCEDURE getType (n: node) : node ;
5150 BEGIN
5151 WITH n^ DO
5152 CASE kind OF
5153
5154 new,
5155 dispose : RETURN NIL |
5156 length : RETURN cardinalN |
5157 inc,
5158 dec,
5159 incl,
5160 excl : RETURN NIL |
5161 nil : RETURN addressN |
5162 true,
5163 false : RETURN booleanN |
5164 address : RETURN n |
5165 loc : RETURN n |
5166 byte : RETURN n |
5167 word : RETURN n |
5168 csizet : RETURN n |
5169 cssizet : RETURN n |
5170 cofft : RETURN n |
5171 cardinal64 : RETURN n |
5172 (* base types. *)
5173 boolean : RETURN n |
5174 proc : RETURN n |
5175 char : RETURN n |
5176 cardinal : RETURN n |
5177 longcard : RETURN n |
5178 shortcard : RETURN n |
5179 integer : RETURN n |
5180 longint : RETURN n |
5181 shortint : RETURN n |
5182 real : RETURN n |
5183 longreal : RETURN n |
5184 shortreal : RETURN n |
5185 bitset : RETURN n |
5186 ztype : RETURN n |
5187 rtype : RETURN n |
5188 complex : RETURN n |
5189 longcomplex : RETURN n |
5190 shortcomplex : RETURN n |
5191
5192 (* language features and compound type attributes. *)
5193 type : RETURN typeF.type |
5194 record : RETURN n |
5195 varient : RETURN n |
5196 var : RETURN varF.type |
5197 enumeration : RETURN n |
5198 subrange : RETURN subrangeF.type |
5199 array : RETURN arrayF.type |
5200 string : RETURN charN |
5201 const : RETURN constF.type |
5202 literal : RETURN literalF.type |
5203 varparam : RETURN varparamF.type |
5204 param : RETURN paramF.type |
5205 optarg : RETURN optargF.type |
5206 pointer : RETURN pointerF.type |
5207 recordfield : RETURN recordfieldF.type |
5208 varientfield : RETURN n |
5209 enumerationfield: RETURN enumerationfieldF.type |
5210 set : RETURN setF.type |
5211 proctype : RETURN proctypeF.returnType |
5212 subscript : RETURN subscriptF.type |
5213 (* blocks. *)
5214 procedure : RETURN procedureF.returnType |
5215 throw : RETURN NIL |
5216 unreachable : RETURN NIL |
5217 def,
5218 imp,
5219 module,
5220 (* statements. *)
5221 loop,
5222 while,
5223 for,
5224 repeat,
5225 if,
5226 elsif,
5227 assignment : HALT |
5228 (* expressions. *)
5229 cmplx,
5230 cast,
5231 val,
5232 plus,
5233 sub,
5234 div,
5235 mod,
5236 mult,
5237 divide : RETURN binaryF.resultType |
5238 in : RETURN booleanN |
5239 max,
5240 min,
5241 re,
5242 im,
5243 abs,
5244 constexp,
5245 deref,
5246 neg,
5247 adr,
5248 size,
5249 tsize : RETURN unaryF.resultType |
5250 and,
5251 or,
5252 not,
5253 equal,
5254 notequal,
5255 less,
5256 greater,
5257 greequal,
5258 lessequal : RETURN booleanN |
5259 trunc : RETURN integerN |
5260 float : RETURN realN |
5261 high : RETURN cardinalN |
5262 ord : RETURN cardinalN |
5263 chr : RETURN charN |
5264 cap : RETURN charN |
5265 arrayref : RETURN arrayrefF.resultType |
5266 componentref : RETURN componentrefF.resultType |
5267 pointerref : RETURN pointerrefF.resultType |
5268 funccall : RETURN funccallF.type |
5269 setvalue : RETURN setvalueF.type
5270
5271 END
5272 END ;
5273 HALT
5274 END getType ;
5275
5276
5277 (*
5278 mixTypes -
5279 *)
5280
5281 PROCEDURE mixTypes (a, b: node) : node ;
5282 BEGIN
5283 IF (a = addressN) OR (b = addressN)
5284 THEN
5285 RETURN addressN
5286 END ;
5287 RETURN a
5288 END mixTypes ;
5289
5290
5291 (*
5292 doSetExprType -
5293 *)
5294
5295 PROCEDURE doSetExprType (VAR t: node; n: node) : node ;
5296 BEGIN
5297 IF t = NIL
5298 THEN
5299 t := n
5300 END ;
5301 RETURN t
5302 END doSetExprType ;
5303
5304
5305 (*
5306 getMaxMinType -
5307 *)
5308
5309 PROCEDURE getMaxMinType (n: node) : node ;
5310 BEGIN
5311 IF isVar (n) OR isConst (n)
5312 THEN
5313 RETURN getType (n)
5314 ELSIF isConstExp (n)
5315 THEN
5316 n := getExprType (n^.unaryF.arg) ;
5317 IF n = bitsetN
5318 THEN
5319 RETURN ztypeN
5320 ELSE
5321 RETURN n
5322 END
5323 ELSE
5324 RETURN n
5325 END
5326 END getMaxMinType ;
5327
5328
5329 (*
5330 doGetFuncType -
5331 *)
5332
5333 PROCEDURE doGetFuncType (n: node) : node ;
5334 VAR
5335 result: node ;
5336 BEGIN
5337 assert (isFuncCall (n)) ;
5338 result := doSetExprType (n^.funccallF.type, getType (n^.funccallF.function)) ;
5339 initNodeOpaqueState (n) ; (* Update now that the return type is known. *)
5340 RETURN result
5341 END doGetFuncType ;
5342
5343
5344 (*
5345 doGetExprType - works out the type which is associated with node, n.
5346 *)
5347
5348 PROCEDURE doGetExprType (n: node) : node ;
5349 BEGIN
5350 WITH n^ DO
5351 CASE kind OF
5352
5353 max,
5354 min : RETURN getMaxMinType (n^.unaryF.arg) |
5355 cast,
5356 val : RETURN doSetExprType (n^.binaryF.resultType, n^.binaryF.left) |
5357 halt,
5358 new,
5359 dispose : RETURN NIL |
5360 inc,
5361 dec,
5362 incl,
5363 excl : RETURN NIL |
5364 nil : RETURN addressN |
5365 true,
5366 false : RETURN booleanN |
5367 address : RETURN n |
5368 loc : RETURN n |
5369 byte : RETURN n |
5370 word : RETURN n |
5371 csizet : RETURN n |
5372 cssizet : RETURN n |
5373 cofft : RETURN n |
5374 cardinal64 : RETURN n |
5375 (* base types. *)
5376 boolean : RETURN n |
5377 proc : RETURN n |
5378 char : RETURN n |
5379 cardinal : RETURN n |
5380 longcard : RETURN n |
5381 shortcard : RETURN n |
5382 integer : RETURN n |
5383 longint : RETURN n |
5384 shortint : RETURN n |
5385 real : RETURN n |
5386 longreal : RETURN n |
5387 shortreal : RETURN n |
5388 bitset : RETURN n |
5389 ztype : RETURN n |
5390 rtype : RETURN n |
5391 complex : RETURN n |
5392 longcomplex : RETURN n |
5393 shortcomplex : RETURN n |
5394
5395 (* language features and compound type attributes. *)
5396 type : RETURN typeF.type |
5397 record : RETURN n |
5398 varient : RETURN n |
5399 var : RETURN varF.type |
5400 enumeration : RETURN n |
5401 subrange : RETURN subrangeF.type |
5402 array : RETURN arrayF.type |
5403 string : RETURN charN |
5404 const : RETURN doSetExprType (constF.type, getExprType (constF.value)) |
5405 literal : RETURN literalF.type |
5406 varparam : RETURN varparamF.type |
5407 param : RETURN paramF.type |
5408 optarg : RETURN optargF.type |
5409 pointer : RETURN pointerF.type |
5410 recordfield : RETURN recordfieldF.type |
5411 varientfield : RETURN n |
5412 enumerationfield: RETURN enumerationfieldF.type |
5413 set : RETURN setF.type |
5414 proctype : RETURN proctypeF.returnType |
5415 subscript : RETURN subscriptF.type |
5416 (* blocks. *)
5417 procedure : RETURN procedureF.returnType |
5418 throw : RETURN NIL |
5419 unreachable : RETURN NIL |
5420 def,
5421 imp,
5422 module,
5423 (* statements. *)
5424 loop,
5425 while,
5426 for,
5427 repeat,
5428 if,
5429 elsif,
5430 assignment : HALT |
5431 (* expressions. *)
5432 plus,
5433 sub,
5434 div,
5435 mod,
5436 mult,
5437 divide : RETURN doSetExprType (binaryF.resultType, mixTypes (getExprType (binaryF.left), getExprType (binaryF.right))) |
5438 in,
5439 and,
5440 or,
5441 equal,
5442 notequal,
5443 less,
5444 greater,
5445 greequal,
5446 lessequal : RETURN doSetExprType (binaryF.resultType, booleanN) |
5447 cmplx : RETURN doSetExprType (binaryF.resultType, complexN) |
5448 abs,
5449 constexp,
5450 deref,
5451 neg : RETURN doSetExprType (unaryF.resultType, getExprType (unaryF.arg)) |
5452 adr : RETURN doSetExprType (unaryF.resultType, addressN) |
5453 size,
5454 tsize : RETURN doSetExprType (unaryF.resultType, cardinalN) |
5455 high,
5456 ord : RETURN doSetExprType (unaryF.resultType, cardinalN) |
5457 float : RETURN doSetExprType (unaryF.resultType, realN) |
5458 trunc : RETURN doSetExprType (unaryF.resultType, integerN) |
5459 chr : RETURN doSetExprType (unaryF.resultType, charN) |
5460 cap : RETURN doSetExprType (unaryF.resultType, charN) |
5461 not : RETURN doSetExprType (unaryF.resultType, booleanN) |
5462 re : RETURN doSetExprType (unaryF.resultType, realN) |
5463 im : RETURN doSetExprType (unaryF.resultType, realN) |
5464 arrayref : RETURN arrayrefF.resultType |
5465 componentref : RETURN componentrefF.resultType |
5466 pointerref : RETURN pointerrefF.resultType |
5467 funccall : RETURN doSetExprType (funccallF.type, doGetFuncType (n)) |
5468 setvalue : RETURN setvalueF.type
5469
5470 END
5471 END ;
5472 HALT
5473 END doGetExprType ;
5474
5475
5476 (*
5477 getExprType - return the expression type.
5478 *)
5479
5480 PROCEDURE getExprType (n: node) : node ;
5481 VAR
5482 t: node ;
5483 BEGIN
5484 IF isFuncCall (n) AND (getType (n) # NIL) AND isProcType (skipType (getType (n)))
5485 THEN
5486 RETURN getType (skipType (getType (n)))
5487 END ;
5488 t := getType (n) ;
5489 IF t = NIL
5490 THEN
5491 t := doGetExprType (n)
5492 END ;
5493 RETURN t
5494 END getExprType ;
5495
5496
5497 (*
5498 skipType - skips over type aliases.
5499 *)
5500
5501 PROCEDURE skipType (n: node) : node ;
5502 BEGIN
5503 WHILE (n#NIL) AND isType (n) AND (NOT isCDataType (n)) DO
5504 IF getType (n) = NIL
5505 THEN
5506 (* this will occur if, n, is an opaque type. *)
5507 RETURN n
5508 END ;
5509 n := getType (n)
5510 END ;
5511 RETURN n
5512 END skipType ;
5513
5514
5515 (*
5516 getScope - returns the scope associated with node, n.
5517 *)
5518
5519 PROCEDURE getScope (n: node) : node ;
5520 BEGIN
5521 WITH n^ DO
5522 CASE kind OF
5523
5524 stmtseq,
5525 exit,
5526 return,
5527 comment,
5528 identlist,
5529 setvalue,
5530 halt,
5531 new,
5532 dispose,
5533 length,
5534 inc,
5535 dec,
5536 incl,
5537 excl,
5538 nil,
5539 true,
5540 false : RETURN NIL |
5541 address,
5542 loc,
5543 byte,
5544 word,
5545 csizet,
5546 cssizet,
5547 cofft,
5548 cardinal64 : RETURN systemN |
5549 (* base types. *)
5550 boolean,
5551 proc,
5552 char,
5553 cardinal,
5554 longcard,
5555 shortcard,
5556 integer,
5557 longint,
5558 shortint,
5559 real,
5560 longreal,
5561 shortreal,
5562 bitset,
5563 ztype,
5564 rtype,
5565 complex,
5566 longcomplex,
5567 shortcomplex : RETURN NIL |
5568 (* language features and compound type attributes. *)
5569 type : RETURN typeF.scope |
5570 record : RETURN recordF.scope |
5571 varient : RETURN varientF.scope |
5572 var : RETURN varF.scope |
5573 enumeration : RETURN enumerationF.scope |
5574 subrange : RETURN subrangeF.scope |
5575 array : RETURN arrayF.scope |
5576 string : RETURN NIL |
5577 const : RETURN constF.scope |
5578 literal : RETURN NIL |
5579 varparam : RETURN varparamF.scope |
5580 param : RETURN paramF.scope |
5581 optarg : RETURN optargF.scope |
5582 pointer : RETURN pointerF.scope |
5583 recordfield : RETURN recordfieldF.scope |
5584 varientfield : RETURN varientfieldF.scope |
5585 enumerationfield: RETURN enumerationfieldF.scope |
5586 set : RETURN setF.scope |
5587 proctype : RETURN proctypeF.scope |
5588 subscript : RETURN NIL |
5589 (* blocks. *)
5590 procedure : RETURN procedureF.scope |
5591 def,
5592 imp,
5593 module,
5594 (* statements. *)
5595 case,
5596 loop,
5597 while,
5598 for,
5599 repeat,
5600 if,
5601 elsif,
5602 assignment : RETURN NIL |
5603 (* expressions. *)
5604 componentref,
5605 pointerref,
5606 arrayref,
5607 chr,
5608 cap,
5609 ord,
5610 float,
5611 trunc,
5612 high,
5613 cast,
5614 val,
5615 plus,
5616 sub,
5617 div,
5618 mod,
5619 mult,
5620 divide,
5621 in : RETURN NIL |
5622 neg : RETURN NIL |
5623 lsl,
5624 lsr,
5625 lor,
5626 land,
5627 lnot,
5628 lxor,
5629 and,
5630 or,
5631 not,
5632 constexp,
5633 deref,
5634 equal,
5635 notequal,
5636 less,
5637 greater,
5638 greequal,
5639 lessequal : RETURN NIL |
5640 adr,
5641 size,
5642 tsize,
5643 throw : RETURN systemN |
5644 unreachable,
5645 cmplx, re, im,
5646 min,
5647 max : RETURN NIL |
5648 vardecl : RETURN vardeclF.scope |
5649 funccall : RETURN NIL |
5650 explist : RETURN NIL |
5651 caselabellist : RETURN NIL |
5652 caselist : RETURN NIL |
5653 range : RETURN NIL |
5654 varargs : RETURN varargsF.scope
5655
5656 END
5657 END
5658 END getScope ;
5659
5660
5661 (*
5662 foreachDefModuleDo - foreach definition node, n, in the module universe,
5663 call p (n).
5664 *)
5665
5666 PROCEDURE foreachDefModuleDo (p: performOperation) ;
5667 BEGIN
5668 ForeachIndiceInIndexDo (defUniverseI, p)
5669 END foreachDefModuleDo ;
5670
5671
5672 (*
5673 foreachModModuleDo - foreach implementation or module node, n, in the module universe,
5674 call p (n).
5675 *)
5676
5677 PROCEDURE foreachModModuleDo (p: performOperation) ;
5678 BEGIN
5679 ForeachIndiceInIndexDo (modUniverseI, p)
5680 END foreachModModuleDo ;
5681
5682
5683 (*
5684 openOutput -
5685 *)
5686
5687 PROCEDURE openOutput ;
5688 VAR
5689 s: String ;
5690 BEGIN
5691 s := getOutputFile () ;
5692 IF EqualArray (s, '-')
5693 THEN
5694 outputFile := StdOut
5695 ELSE
5696 outputFile := OpenToWrite (s)
5697 END ;
5698 mcStream.setDest (outputFile)
5699 END openOutput ;
5700
5701
5702 (*
5703 closeOutput -
5704 *)
5705
5706 PROCEDURE closeOutput ;
5707 VAR
5708 s: String ;
5709 BEGIN
5710 s := getOutputFile () ;
5711 outputFile := mcStream.combine () ;
5712 IF NOT EqualArray (s, '-')
5713 THEN
5714 Close (outputFile)
5715 END
5716 END closeOutput ;
5717
5718
5719 (*
5720 write - outputs a single char, ch.
5721 *)
5722
5723 PROCEDURE write (ch: CHAR) ;
5724 BEGIN
5725 WriteChar (outputFile, ch) ;
5726 FlushBuffer (outputFile)
5727 END write ;
5728
5729
5730 (*
5731 writeln -
5732 *)
5733
5734 PROCEDURE writeln ;
5735 BEGIN
5736 WriteLine (outputFile) ;
5737 FlushBuffer (outputFile)
5738 END writeln ;
5739
5740
5741 (*
5742 doIncludeC - include header file for definition module, n.
5743 *)
5744
5745 PROCEDURE doIncludeC (n: node) ;
5746 VAR
5747 s: String ;
5748 BEGIN
5749 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
5750 IF getExtendedOpaque ()
5751 THEN
5752 (* no include in this case. *)
5753 ELSIF isDef (n)
5754 THEN
5755 print (doP, '# include "') ;
5756 prints (doP, getHPrefix ()) ;
5757 prints (doP, s) ;
5758 print (doP, '.h"\n') ;
5759 foreachNodeDo (n^.defF.decls.symbols, addDoneDef)
5760 END ;
5761 s := KillString (s)
5762 END doIncludeC ;
5763
5764
5765 (*
5766 getSymScope - returns the scope where node, n, was declared.
5767 *)
5768
5769 PROCEDURE getSymScope (n: node) : node ;
5770 BEGIN
5771 WITH n^ DO
5772 CASE kind OF
5773
5774 const : RETURN constF.scope |
5775 type : RETURN typeF.scope |
5776 var : RETURN varF.scope |
5777 procedure: RETURN procedureF.scope
5778
5779 END
5780 END ;
5781 HALT
5782 END getSymScope ;
5783
5784
5785 (*
5786 isQualifiedForced - should the node be written with a module prefix?
5787 *)
5788
5789 PROCEDURE isQualifiedForced (n: node) : BOOLEAN ;
5790 BEGIN
5791 RETURN (forceQualified AND
5792 (isType (n) OR isRecord (n) OR isArray (n) OR isEnumeration (n) OR isEnumerationField (n)))
5793 END isQualifiedForced ;
5794
5795
5796 (*
5797 getFQstring -
5798 *)
5799
5800 PROCEDURE getFQstring (n: node) : String ;
5801 VAR
5802 i, s: String ;
5803 BEGIN
5804 IF (getScope (n) = NIL) OR (isDefUnqualified (getScope (n)))
5805 THEN
5806 RETURN InitStringCharStar (keyToCharStar (getSymName (n)))
5807 ELSIF isQualifiedForced (n)
5808 THEN
5809 i := InitStringCharStar (keyToCharStar (getSymName (n))) ;
5810 s := InitStringCharStar (keyToCharStar (getSymName (getScope (n)))) ;
5811 RETURN Sprintf2 (InitString ("%s_%s"), s, i)
5812 ELSIF (NOT isExported (n)) OR getIgnoreFQ () OR (isDefUnqualified (getScope (n)))
5813 THEN
5814 RETURN InitStringCharStar (keyToCharStar (getSymName (n)))
5815 ELSE
5816 i := InitStringCharStar (keyToCharStar (getSymName (n))) ;
5817 s := InitStringCharStar (keyToCharStar (getSymName (getScope (n)))) ;
5818 RETURN Sprintf2 (InitString ("%s_%s"), s, i)
5819 END
5820 END getFQstring ;
5821
5822
5823 (*
5824 getFQDstring -
5825 *)
5826
5827 PROCEDURE getFQDstring (n: node; scopes: BOOLEAN) : String ;
5828 VAR
5829 i, s: String ;
5830 BEGIN
5831 IF (getScope (n) = NIL) OR (isDefUnqualified (getScope (n)))
5832 THEN
5833 RETURN InitStringCharStar (keyToCharStar (getDName (n, scopes)))
5834 ELSIF isQualifiedForced (n)
5835 THEN
5836 (* we assume a qualified name will never conflict. *)
5837 i := InitStringCharStar (keyToCharStar (getSymName (n))) ;
5838 s := InitStringCharStar (keyToCharStar (getSymName (getScope (n)))) ;
5839 RETURN Sprintf2 (InitString ("%s_%s"), s, i)
5840 ELSIF (NOT isExported (n)) OR getIgnoreFQ () OR (isDefUnqualified (getScope (n)))
5841 THEN
5842 RETURN InitStringCharStar (keyToCharStar (getDName (n, scopes)))
5843 ELSE
5844 (* we assume a qualified name will never conflict. *)
5845 i := InitStringCharStar (keyToCharStar (getSymName (n))) ;
5846 s := InitStringCharStar (keyToCharStar (getSymName (getScope (n)))) ;
5847 RETURN Sprintf2 (InitString ("%s_%s"), s, i)
5848 END
5849 END getFQDstring ;
5850
5851
5852 (*
5853 getString - returns the name as a string.
5854 *)
5855
5856 PROCEDURE getString (n: node) : String ;
5857 BEGIN
5858 IF getSymName (n) = NulName
5859 THEN
5860 RETURN InitString ('')
5861 ELSE
5862 RETURN InitStringCharStar (keyToCharStar (getSymName (n)))
5863 END
5864 END getString ;
5865
5866
5867 (*
5868 getCardinal - returns the cardinal type node.
5869 *)
5870
5871 PROCEDURE getCardinal () : node ;
5872 BEGIN
5873 RETURN cardinalN
5874 END getCardinal ;
5875
5876
5877 (*
5878 doNone - call HALT.
5879 *)
5880
5881 PROCEDURE doNone (n: node) ;
5882 BEGIN
5883 HALT
5884 END doNone ;
5885
5886
5887 (*
5888 doNothing - does nothing!
5889 *)
5890
5891 PROCEDURE doNothing (n: node) ;
5892 BEGIN
5893 END doNothing ;
5894
5895
5896 (*
5897 doConstC -
5898 *)
5899
5900 PROCEDURE doConstC (n: node) ;
5901 BEGIN
5902 IF NOT alists.isItemInList (globalGroup^.doneQ, n)
5903 THEN
5904 print (doP, "# define ") ;
5905 doFQNameC (doP, n) ;
5906 setNeedSpace (doP) ;
5907 doExprC (doP, n^.constF.value) ;
5908 print (doP, '\n') ;
5909 alists.includeItemIntoList (globalGroup^.doneQ, n)
5910 END
5911 END doConstC ;
5912
5913
5914 (*
5915 needsParen - returns TRUE if expression, n, needs to be enclosed in ().
5916 *)
5917
5918 PROCEDURE needsParen (n: node) : BOOLEAN ;
5919 BEGIN
5920 assert (n#NIL) ;
5921 WITH n^ DO
5922 CASE kind OF
5923
5924 nil,
5925 true,
5926 false : RETURN FALSE |
5927 constexp : RETURN needsParen (unaryF.arg) |
5928 neg : RETURN needsParen (unaryF.arg) |
5929 not : RETURN needsParen (unaryF.arg) |
5930 adr,
5931 size,
5932 tsize,
5933 ord,
5934 float,
5935 trunc,
5936 chr,
5937 cap,
5938 high : RETURN FALSE |
5939 deref : RETURN FALSE |
5940 equal,
5941 notequal,
5942 less,
5943 greater,
5944 greequal,
5945 lessequal : RETURN TRUE |
5946 componentref : RETURN FALSE |
5947 pointerref : RETURN FALSE |
5948 cast : RETURN TRUE |
5949 val : RETURN TRUE |
5950 abs : RETURN FALSE |
5951 plus,
5952 sub,
5953 div,
5954 mod,
5955 mult,
5956 divide,
5957 in : RETURN TRUE |
5958 literal,
5959 const,
5960 enumerationfield,
5961 string : RETURN FALSE |
5962 max : RETURN TRUE |
5963 min : RETURN TRUE |
5964 var : RETURN FALSE |
5965 arrayref : RETURN FALSE |
5966 and,
5967 or : RETURN TRUE |
5968 funccall : RETURN TRUE |
5969 recordfield : RETURN FALSE |
5970 loc,
5971 byte,
5972 word,
5973 type,
5974 char,
5975 cardinal,
5976 longcard,
5977 shortcard,
5978 integer,
5979 longint,
5980 shortint,
5981 real,
5982 longreal,
5983 shortreal,
5984 complex,
5985 longcomplex,
5986 shortcomplex,
5987 bitset,
5988 boolean,
5989 proc : RETURN FALSE |
5990 setvalue : RETURN FALSE |
5991 address : RETURN TRUE |
5992 procedure : RETURN FALSE |
5993 length,
5994 cmplx, re, im : RETURN TRUE
5995
5996 END
5997 END ;
5998 RETURN TRUE
5999 END needsParen ;
6000
6001
6002 (*
6003 doUnary -
6004 *)
6005
6006 PROCEDURE doUnary (p: pretty; op: ARRAY OF CHAR; expr, type: node; l, r: BOOLEAN) ;
6007 BEGIN
6008 IF l
6009 THEN
6010 setNeedSpace (p)
6011 END ;
6012 print (p, op) ;
6013 IF r
6014 THEN
6015 setNeedSpace (p)
6016 END ;
6017 IF needsParen (expr)
6018 THEN
6019 outText (p, '(') ;
6020 doExprC (p, expr) ;
6021 outText (p, ')')
6022 ELSE
6023 doExprC (p, expr)
6024 END
6025 END doUnary ;
6026
6027
6028 (*
6029 doSetSub - perform l & (~ r)
6030 *)
6031
6032 PROCEDURE doSetSub (p: pretty; left, right: node) ;
6033 BEGIN
6034 IF needsParen (left)
6035 THEN
6036 outText (p, '(') ;
6037 doExprC (p, left) ;
6038 outText (p, ')')
6039 ELSE
6040 doExprC (p, left)
6041 END ;
6042 setNeedSpace (p) ;
6043 outText (p, '&') ;
6044 setNeedSpace (p) ;
6045 IF needsParen (right)
6046 THEN
6047 outText (p, '(~(') ;
6048 doExprC (p, right) ;
6049 outText (p, '))')
6050 ELSE
6051 outText (p, '(~') ;
6052 doExprC (p, right) ;
6053 outText (p, ')')
6054 END
6055 END doSetSub ;
6056
6057
6058 (*
6059 doPolyBinary -
6060 *)
6061
6062 PROCEDURE doPolyBinary (p: pretty; op: nodeT; left, right: node; l, r: BOOLEAN) ;
6063 VAR
6064 lt, rt: node ;
6065 BEGIN
6066 lt := skipType (getExprType (left)) ;
6067 rt := skipType (getExprType (right)) ;
6068 IF ((lt # NIL) AND (isSet (lt) OR isBitset (lt))) OR
6069 ((rt # NIL) AND (isSet (rt) OR isBitset (rt)))
6070 THEN
6071 CASE op OF
6072
6073 plus : doBinary (p, '|', left, right, l, r, FALSE) |
6074 sub : doSetSub (p, left, right) |
6075 mult : doBinary (p, '&', left, right, l, r, FALSE) |
6076 divide : doBinary (p, '^', left, right, l, r, FALSE)
6077
6078 END
6079 ELSE
6080 CASE op OF
6081
6082 plus : doBinary (p, '+', left, right, l, r, FALSE) |
6083 sub : doBinary (p, '-', left, right, l, r, FALSE) |
6084 mult : doBinary (p, '*', left, right, l, r, FALSE) |
6085 divide : doBinary (p, '/', left, right, l, r, FALSE)
6086
6087 END
6088 END
6089 END doPolyBinary ;
6090
6091
6092 (*
6093 doBinary -
6094 *)
6095
6096 PROCEDURE doBinary (p: pretty; op: ARRAY OF CHAR; left, right: node; l, r, unpackProc: BOOLEAN) ;
6097 BEGIN
6098 IF needsParen (left)
6099 THEN
6100 outText (p, '(') ;
6101 left := doExprCup (p, left, unpackProc, FALSE) ;
6102 outText (p, ')')
6103 ELSE
6104 left := doExprCup (p, left, unpackProc, FALSE)
6105 END ;
6106 IF l
6107 THEN
6108 setNeedSpace (p)
6109 END ;
6110 outText (p, op) ;
6111 IF r
6112 THEN
6113 setNeedSpace (p)
6114 END ;
6115 IF needsParen (right)
6116 THEN
6117 outText (p, '(') ;
6118 right := doExprCup (p, right, unpackProc, FALSE) ;
6119 outText (p, ')')
6120 ELSE
6121 right := doExprCup (p, right, unpackProc, FALSE)
6122 END
6123 END doBinary ;
6124
6125
6126 (*
6127 doPostUnary -
6128 *)
6129
6130 PROCEDURE doPostUnary (p: pretty; op: ARRAY OF CHAR; expr: node) ;
6131 BEGIN
6132 doExprC (p, expr) ;
6133 outText (p, op)
6134 END doPostUnary ;
6135
6136
6137 (*
6138 doDeRefC -
6139 *)
6140
6141 PROCEDURE doDeRefC (p: pretty; expr: node) : node ;
6142 BEGIN
6143 outText (p, '(*') ;
6144 expr := castOpaque (p, expr, FALSE) ;
6145 outText (p, ')') ;
6146 RETURN expr
6147 END doDeRefC ;
6148
6149
6150 (*
6151 doGetLastOp - returns, a, if b is a terminal otherwise walk right.
6152 *)
6153
6154 PROCEDURE doGetLastOp (a, b: node) : node ;
6155 BEGIN
6156 WITH b^ DO
6157 CASE kind OF
6158
6159 nil : RETURN a |
6160 true : RETURN a |
6161 false : RETURN a |
6162 constexp : RETURN doGetLastOp (b, unaryF.arg) |
6163 neg : RETURN doGetLastOp (b, unaryF.arg) |
6164 not : RETURN doGetLastOp (b, unaryF.arg) |
6165 adr : RETURN doGetLastOp (b, unaryF.arg) |
6166 size : RETURN doGetLastOp (b, unaryF.arg) |
6167 tsize : RETURN doGetLastOp (b, unaryF.arg) |
6168 ord : RETURN doGetLastOp (b, unaryF.arg) |
6169 float,
6170 trunc : RETURN doGetLastOp (b, unaryF.arg) |
6171 chr : RETURN doGetLastOp (b, unaryF.arg) |
6172 cap : RETURN doGetLastOp (b, unaryF.arg) |
6173 high : RETURN doGetLastOp (b, unaryF.arg) |
6174 deref : RETURN doGetLastOp (b, unaryF.arg) |
6175 re,
6176 im : RETURN doGetLastOp (b, unaryF.arg) |
6177 equal : RETURN doGetLastOp (b, binaryF.right) |
6178 notequal : RETURN doGetLastOp (b, binaryF.right) |
6179 less : RETURN doGetLastOp (b, binaryF.right) |
6180 greater : RETURN doGetLastOp (b, binaryF.right) |
6181 greequal : RETURN doGetLastOp (b, binaryF.right) |
6182 lessequal : RETURN doGetLastOp (b, binaryF.right) |
6183 componentref : RETURN doGetLastOp (b, componentrefF.field) |
6184 pointerref : RETURN doGetLastOp (b, pointerrefF.field) |
6185 cast : RETURN doGetLastOp (b, binaryF.right) |
6186 val : RETURN doGetLastOp (b, binaryF.right) |
6187 plus : RETURN doGetLastOp (b, binaryF.right) |
6188 sub : RETURN doGetLastOp (b, binaryF.right) |
6189 div : RETURN doGetLastOp (b, binaryF.right) |
6190 mod : RETURN doGetLastOp (b, binaryF.right) |
6191 mult : RETURN doGetLastOp (b, binaryF.right) |
6192 divide : RETURN doGetLastOp (b, binaryF.right) |
6193 in : RETURN doGetLastOp (b, binaryF.right) |
6194 and : RETURN doGetLastOp (b, binaryF.right) |
6195 or : RETURN doGetLastOp (b, binaryF.right) |
6196 cmplx : RETURN doGetLastOp (b, binaryF.right) |
6197 literal : RETURN a |
6198 const : RETURN a |
6199 enumerationfield: RETURN a |
6200 string : RETURN a |
6201 max : RETURN doGetLastOp (b, unaryF.arg) |
6202 min : RETURN doGetLastOp (b, unaryF.arg) |
6203 var : RETURN a |
6204 arrayref : RETURN a |
6205 funccall : RETURN a |
6206 procedure : RETURN a |
6207 recordfield : RETURN a
6208
6209 END
6210 END
6211 END doGetLastOp ;
6212
6213
6214 (*
6215 getLastOp - return the right most non leaf node.
6216 *)
6217
6218 PROCEDURE getLastOp (n: node) : node ;
6219 BEGIN
6220 RETURN doGetLastOp (n, n)
6221 END getLastOp ;
6222
6223
6224 (*
6225 doComponentRefC -
6226 *)
6227
6228 PROCEDURE doComponentRefC (p: pretty; l, r: node) ;
6229 BEGIN
6230 flushOpaque (p, l, FALSE) ;
6231 outText (p, '.') ;
6232 doExprC (p, r)
6233 END doComponentRefC ;
6234
6235
6236 (*
6237 doPointerRefC -
6238 *)
6239
6240 PROCEDURE doPointerRefC (p: pretty; l, r: node) ;
6241 BEGIN
6242 flushOpaque (p, l, FALSE) ;
6243 outText (p, '->') ;
6244 doExprC (p, r)
6245 END doPointerRefC ;
6246
6247
6248 (*
6249 doPreBinary -
6250 *)
6251
6252 PROCEDURE doPreBinary (p: pretty; op: ARRAY OF CHAR; left, right: node; l, r: BOOLEAN) ;
6253 BEGIN
6254 IF l
6255 THEN
6256 setNeedSpace (p)
6257 END ;
6258 outText (p, op) ;
6259 IF r
6260 THEN
6261 setNeedSpace (p)
6262 END ;
6263 outText (p, '(') ;
6264 doExprC (p, left) ;
6265 outText (p, ',') ;
6266 setNeedSpace (p) ;
6267 doExprC (p, right) ;
6268 outText (p, ')')
6269 END doPreBinary ;
6270
6271
6272 (*
6273 doConstExpr -
6274 *)
6275
6276 PROCEDURE doConstExpr (p: pretty; n: node) ;
6277 BEGIN
6278 doFQNameC (p, n)
6279 END doConstExpr ;
6280
6281
6282 (*
6283 doEnumerationField -
6284 *)
6285
6286 PROCEDURE doEnumerationField (p: pretty; n: node) ;
6287 BEGIN
6288 doFQDNameC (p, n, FALSE)
6289 END doEnumerationField ;
6290
6291
6292 (*
6293 isZero - returns TRUE if node, n, is zero.
6294 *)
6295
6296 PROCEDURE isZero (n: node) : BOOLEAN ;
6297 BEGIN
6298 IF isConstExp (n)
6299 THEN
6300 RETURN isZero (n^.unaryF.arg)
6301 END ;
6302 RETURN getSymName (n)=makeKey ('0')
6303 END isZero ;
6304
6305
6306 (*
6307 doArrayRef - perform an array reference. If constCast
6308 then an unbounded array access will be const_cast
6309 (the constCast should be TRUE if an assignment to
6310 the array is required).
6311 *)
6312
6313 PROCEDURE doArrayRef (p: pretty; n: node; constCast: BOOLEAN) ;
6314 VAR
6315 type,
6316 v : node ;
6317 i, c: CARDINAL ;
6318 BEGIN
6319 assert (n # NIL) ;
6320 assert (isArrayRef (n)) ;
6321 type := skipType (getType (n^.arrayrefF.array)) ;
6322 IF isUnbounded (type)
6323 THEN
6324 v := n^.arrayrefF.array ;
6325 IF constCast AND isVar (n^.arrayrefF.array) AND
6326 (v^.varF.isParameter OR v^.varF.isVarParameter)
6327 THEN
6328 outText (p, "const_cast<") ;
6329 doTypeNameC (p, getType (v)) ;
6330 outText (p, ">(") ;
6331 outTextN (p, getSymName (n^.arrayrefF.array)) ;
6332 outText (p, ")")
6333 ELSE
6334 outTextN (p, getSymName (n^.arrayrefF.array))
6335 END
6336 ELSE
6337 doExprC (p, n^.arrayrefF.array) ;
6338 assert (isArray (type)) ;
6339 outText (p, '.array')
6340 END ;
6341 outText (p, '[') ;
6342 i := 1 ;
6343 c := expListLen (n^.arrayrefF.index) ;
6344 WHILE i<=c DO
6345 doExprC (p, getExpList (n^.arrayrefF.index, i)) ;
6346 IF isUnbounded (type)
6347 THEN
6348 assert (c = 1)
6349 ELSE
6350 doSubtractC (p, getMin (type^.arrayF.subr)) ;
6351 IF i<c
6352 THEN
6353 assert (isArray (type)) ;
6354 outText (p, '].array[') ;
6355 type := skipType (getType (type))
6356 END
6357 END ;
6358 INC (i)
6359 END ;
6360 outText (p, ']')
6361 END doArrayRef ;
6362
6363
6364 (*
6365 doProcedure -
6366 *)
6367
6368 PROCEDURE doProcedure (p: pretty; n: node) ;
6369 BEGIN
6370 assert (isProcedure (n)) ;
6371 doFQDNameC (p, n, TRUE)
6372 END doProcedure ;
6373
6374
6375 (*
6376 doRecordfield -
6377 *)
6378
6379 PROCEDURE doRecordfield (p: pretty; n: node) ;
6380 BEGIN
6381 doDNameC (p, n, FALSE)
6382 END doRecordfield ;
6383
6384
6385 (*
6386 doCastC -
6387 *)
6388
6389 PROCEDURE doCastC (p: pretty; t, e: node) ;
6390 VAR
6391 et: node ;
6392 BEGIN
6393 outText (p, '(') ;
6394 doTypeNameC (p, t) ;
6395 outText (p, ')') ;
6396 setNeedSpace (p) ;
6397 et := skipType (getType (e)) ;
6398 IF (et # NIL) AND isAProcType (et) AND isAProcType (skipType (t))
6399 THEN
6400 outText (p, '{(') ;
6401 doFQNameC (p, t) ;
6402 outText (p, '_t)') ;
6403 setNeedSpace (p) ;
6404 doExprC (p, e) ;
6405 outText (p, '.proc}')
6406 ELSE
6407 outText (p, '(') ;
6408 doExprC (p, e) ;
6409 outText (p, ')')
6410 END
6411 END doCastC ;
6412
6413
6414 (*
6415 doSetValueC -
6416 *)
6417
6418 PROCEDURE doSetValueC (p: pretty; n: node) ;
6419 VAR
6420 lo : node ;
6421 i, h: CARDINAL ;
6422 BEGIN
6423 assert (isSetValue (n)) ;
6424 lo := getSetLow (n) ;
6425 IF n^.setvalueF.type # NIL
6426 THEN
6427 outText (p, '(') ;
6428 doTypeNameC (p, n^.setvalueF.type) ;
6429 noSpace (p) ;
6430 outText (p, ')') ;
6431 setNeedSpace (p)
6432 END ;
6433 IF HighIndice (n^.setvalueF.values) = 0
6434 THEN
6435 outText (p, '0')
6436 ELSE
6437 i := LowIndice (n^.setvalueF.values) ;
6438 h := HighIndice (n^.setvalueF.values) ;
6439 outText (p, '(') ;
6440 WHILE i<=h DO
6441 outText (p, '(1') ;
6442 setNeedSpace (p) ;
6443 outText (p, '<<') ;
6444 setNeedSpace (p) ;
6445 outText (p, '(') ;
6446 doExprC (p, GetIndice (n^.setvalueF.values, i)) ;
6447 doSubtractC (p, lo) ;
6448 outText (p, ')') ;
6449 outText (p, ')') ;
6450 IF i<h
6451 THEN
6452 setNeedSpace (p) ;
6453 outText (p, '|') ;
6454 setNeedSpace (p)
6455 END ;
6456 INC (i)
6457 END ;
6458 outText (p, ')')
6459 END
6460 END doSetValueC ;
6461
6462
6463 (*
6464 getSetLow - returns the low value of the set type from
6465 expression, n.
6466 *)
6467
6468 PROCEDURE getSetLow (n: node) : node ;
6469 VAR
6470 type: node ;
6471 BEGIN
6472 IF getType (n) = NIL
6473 THEN
6474 RETURN makeLiteralInt (makeKey ('0'))
6475 ELSE
6476 type := skipType (getType (n)) ;
6477 IF isSet (type)
6478 THEN
6479 RETURN getMin (skipType (getType (type)))
6480 ELSE
6481 RETURN makeLiteralInt (makeKey ('0'))
6482 END
6483 END
6484 END getSetLow ;
6485
6486
6487 (*
6488 doInC - performs (((1 << (l)) & (r)) != 0)
6489 *)
6490
6491 PROCEDURE doInC (p: pretty; l, r: node) ;
6492 VAR
6493 lo: node ;
6494 BEGIN
6495 lo := getSetLow (r) ;
6496 outText (p, '(((1') ;
6497 setNeedSpace (p) ;
6498 outText (p, '<<') ;
6499 setNeedSpace (p) ;
6500 outText (p, '(') ;
6501 doExprC (p, l) ;
6502 doSubtractC (p, lo) ;
6503 outText (p, '))') ;
6504 setNeedSpace (p) ;
6505 outText (p, '&') ;
6506 setNeedSpace (p) ;
6507 outText (p, '(') ;
6508 doExprC (p, r) ;
6509 outText (p, '))') ;
6510 setNeedSpace (p) ;
6511 outText (p, '!=') ;
6512 setNeedSpace (p) ;
6513 outText (p, '0)')
6514 END doInC ;
6515
6516
6517 (*
6518 doThrowC -
6519 *)
6520
6521 PROCEDURE doThrowC (p: pretty; n: node) ;
6522 BEGIN
6523 assert (isIntrinsic (n)) ;
6524 outText (p, "throw") ;
6525 setNeedSpace (p) ;
6526 outText (p, '(') ;
6527 IF expListLen (n^.intrinsicF.args) = 1
6528 THEN
6529 doExprC (p, getExpList (n^.intrinsicF.args, 1))
6530 END ;
6531 outText (p, ')')
6532 END doThrowC ;
6533
6534
6535 (*
6536 doUnreachableC -
6537 *)
6538
6539 PROCEDURE doUnreachableC (p: pretty; n: node) ;
6540 BEGIN
6541 assert (isIntrinsic (n)) ;
6542 outText (p, "__builtin_unreachable") ;
6543 setNeedSpace (p) ;
6544 outText (p, '(') ;
6545 assert (expListLen (n^.intrinsicF.args) = 0) ;
6546 outText (p, ')')
6547 END doUnreachableC ;
6548
6549
6550 (*
6551 outNull -
6552 *)
6553
6554 PROCEDURE outNull (p: pretty) ;
6555 BEGIN
6556 keyc.useNull ;
6557 outText (p, 'NULL')
6558 END outNull ;
6559
6560
6561 (*
6562 outTrue -
6563 *)
6564
6565 PROCEDURE outTrue (p: pretty) ;
6566 BEGIN
6567 keyc.useTrue ;
6568 IF useBool () AND (lang = ansiCP)
6569 THEN
6570 outText (p, 'true')
6571 ELSE
6572 outText (p, 'TRUE')
6573 END
6574 END outTrue ;
6575
6576
6577 (*
6578 outFalse -
6579 *)
6580
6581 PROCEDURE outFalse (p: pretty) ;
6582 BEGIN
6583 keyc.useFalse ;
6584 IF useBool () AND (lang = ansiCP)
6585 THEN
6586 outText (p, 'false')
6587 ELSE
6588 outText (p, 'FALSE')
6589 END
6590 END outFalse ;
6591
6592
6593 (*
6594 doExprC -
6595 *)
6596
6597 PROCEDURE doExprC (p: pretty; n: node) ;
6598 VAR
6599 t: node ;
6600 BEGIN
6601 assert (n#NIL) ;
6602 t := getExprType (n) ;
6603 WITH n^ DO
6604 CASE kind OF
6605
6606 nil : outNull (p) |
6607 true : outTrue (p) |
6608 false : outFalse (p) |
6609 constexp : doUnary (p, '', unaryF.arg, unaryF.resultType, FALSE, FALSE) |
6610 neg : doUnary (p, '-', unaryF.arg, unaryF.resultType, FALSE, FALSE) |
6611 not : doUnary (p, '!', unaryF.arg, unaryF.resultType, FALSE, TRUE) |
6612 val : doValC (p, n) |
6613 adr : doAdrC (p, n) |
6614 size,
6615 tsize : doSizeC (p, n) |
6616 float : doConvertSC (p, n, getCRealType ()) |
6617 trunc : doConvertC (p, n, "int") |
6618 ord : doConvertC (p, n, "unsigned int") |
6619 chr : doConvertC (p, n, "char") |
6620 cap : doCapC (p, n) |
6621 abs : doAbsC (p, n) |
6622 high : doFuncHighC (p, n^.unaryF.arg) |
6623 length : doLengthC (p, n) |
6624 min : doMinC (p, n) |
6625 max : doMaxC (p, n) |
6626 throw : doThrowC (p, n) |
6627 unreachable : doUnreachableC (p, n) |
6628 re : doReC (p, n) |
6629 im : doImC (p, n) |
6630 cmplx : doCmplx (p, n) |
6631
6632 deref : unaryF.arg := doDeRefC (p, unaryF.arg) |
6633 equal : doBinary (p, '==', binaryF.left, binaryF.right, TRUE, TRUE, TRUE) |
6634 notequal : doBinary (p, '!=', binaryF.left, binaryF.right, TRUE, TRUE, TRUE) |
6635 less : doBinary (p, '<', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6636 greater : doBinary (p, '>', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6637 greequal : doBinary (p, '>=', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6638 lessequal : doBinary (p, '<=', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6639 componentref : doComponentRefC (p, componentrefF.rec, componentrefF.field) |
6640 pointerref : doPointerRefC (p, pointerrefF.ptr, pointerrefF.field) |
6641 cast : doCastC (p, binaryF.left, binaryF.right) |
6642 plus : doPolyBinary (p, plus, binaryF.left, binaryF.right, FALSE, FALSE) |
6643 sub : doPolyBinary (p, sub, binaryF.left, binaryF.right, FALSE, FALSE) |
6644 div : doBinary (p, '/', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6645 mod : doBinary (p, '%', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6646 mult : doPolyBinary (p, mult, binaryF.left, binaryF.right, FALSE, FALSE) |
6647 divide : doPolyBinary (p, divide, binaryF.left, binaryF.right, FALSE, FALSE) |
6648 in : doInC (p, binaryF.left, binaryF.right) |
6649 and : doBinary (p, '&&', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6650 or : doBinary (p, '||', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6651 literal : doLiteralC (p, n) |
6652 const : doConstExpr (p, n) |
6653 enumerationfield: doEnumerationField (p, n) |
6654 string : doStringC (p, n) |
6655 var : doVar (p, n) |
6656 arrayref : doArrayRef (p, n, FALSE) |
6657 funccall : doFuncExprC (p, n) |
6658 procedure : doProcedure (p, n) |
6659 recordfield : doRecordfield (p, n) |
6660 setvalue : doSetValueC (p, n) |
6661 char,
6662 cardinal,
6663 longcard,
6664 shortcard,
6665 integer,
6666 longint,
6667 shortint,
6668 complex,
6669 longcomplex,
6670 shortcomplex,
6671 real,
6672 longreal,
6673 shortreal,
6674 bitset,
6675 boolean,
6676 proc : doBaseC (p, n) |
6677 address,
6678 loc,
6679 byte,
6680 word,
6681 csizet,
6682 cssizet,
6683 cofft,
6684 cardinal64 : doSystemC (p, n) |
6685 type : doTypeNameC (p, n) |
6686 pointer : doTypeNameC (p, n)
6687
6688 END
6689 END
6690 END doExprC ;
6691
6692
6693 (*
6694 doExprCup -
6695 *)
6696
6697 PROCEDURE doExprCup (p: pretty; n: node;
6698 unpackProc, uncastConst: BOOLEAN) : node ;
6699 VAR
6700 type: node ;
6701 BEGIN
6702 IF uncastConst AND isArrayRef (n)
6703 THEN
6704 doArrayRef (p, n, TRUE)
6705 ELSE
6706 doExprC (p, n) ;
6707 IF unpackProc
6708 THEN
6709 type := skipType (getExprType (n)) ;
6710 IF (type # NIL) AND isAProcType (type)
6711 THEN
6712 outText (p, '.proc')
6713 END
6714 END
6715 END ;
6716 RETURN n
6717 END doExprCup ;
6718
6719
6720 (*
6721 doExprM2 -
6722 *)
6723
6724 PROCEDURE doExprM2 (p: pretty; n: node) ;
6725 BEGIN
6726 assert (n#NIL) ;
6727 WITH n^ DO
6728 CASE kind OF
6729
6730 nil : outText (p, 'NIL') |
6731 true : outText (p, 'TRUE') |
6732 false : outText (p, 'FALSE') |
6733 constexp : doUnary (p, '', unaryF.arg, unaryF.resultType, FALSE, FALSE) |
6734 neg : doUnary (p, '-', unaryF.arg, unaryF.resultType, FALSE, FALSE) |
6735 not : doUnary (p, 'NOT', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6736 adr : doUnary (p, 'ADR', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6737 size : doUnary (p, 'SIZE', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6738 tsize : doUnary (p, 'TSIZE', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6739 float : doUnary (p, 'FLOAT', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6740 trunc : doUnary (p, 'TRUNC', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6741 ord : doUnary (p, 'ORD', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6742 chr : doUnary (p, 'CHR', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6743 cap : doUnary (p, 'CAP', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6744 high : doUnary (p, 'HIGH', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6745 re : doUnary (p, 'RE', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6746 im : doUnary (p, 'IM', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6747 deref : doPostUnary (p, '^', unaryF.arg) |
6748 equal : doBinary (p, '=', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6749 notequal : doBinary (p, '#', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6750 less : doBinary (p, '<', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6751 greater : doBinary (p, '>', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6752 greequal : doBinary (p, '>=', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6753 lessequal : doBinary (p, '<=', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6754 componentref : doBinary (p, '.', componentrefF.rec, componentrefF.field, FALSE, FALSE, FALSE) |
6755 pointerref : doBinary (p, '^.', pointerrefF.ptr, pointerrefF.field, FALSE, FALSE, FALSE) |
6756 cast : doPreBinary (p, 'CAST', binaryF.left, binaryF.right, TRUE, TRUE) |
6757 val : doPreBinary (p, 'VAL', binaryF.left, binaryF.right, TRUE, TRUE) |
6758 cmplx : doPreBinary (p, 'CMPLX', binaryF.left, binaryF.right, TRUE, TRUE) |
6759 plus : doBinary (p, '+', binaryF.left, binaryF.right, FALSE, FALSE, FALSE) |
6760 sub : doBinary (p, '-', binaryF.left, binaryF.right, FALSE, FALSE, FALSE) |
6761 div : doBinary (p, 'DIV', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6762 mod : doBinary (p, 'MOD', binaryF.left, binaryF.right, TRUE, TRUE, FALSE) |
6763 mult : doBinary (p, '*', binaryF.left, binaryF.right, FALSE, FALSE, FALSE) |
6764 divide : doBinary (p, '/', binaryF.left, binaryF.right, FALSE, FALSE, FALSE) |
6765 literal : doLiteral (p, n) |
6766 const : doConstExpr (p, n) |
6767 enumerationfield: doEnumerationField (p, n) |
6768 string : doString (p, n) |
6769 max : doUnary (p, 'MAX', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6770 min : doUnary (p, 'MIN', unaryF.arg, unaryF.resultType, TRUE, TRUE) |
6771 var : doVar (p, n)
6772
6773 END
6774 END
6775 END doExprM2 ;
6776
6777
6778 (*
6779 doVar -
6780 *)
6781
6782 PROCEDURE doVar (p: pretty; n: node) ;
6783 BEGIN
6784 assert (isVar (n)) ;
6785 IF n^.varF.isVarParameter
6786 THEN
6787 outText (p, '(*') ;
6788 doFQDNameC (p, n, TRUE) ;
6789 outText (p, ')')
6790 ELSE
6791 doFQDNameC (p, n, TRUE)
6792 END
6793 END doVar ;
6794
6795
6796 (*
6797 doLiteralC -
6798 *)
6799
6800 PROCEDURE doLiteralC (p: pretty; n: node) ;
6801 VAR
6802 s: String ;
6803 BEGIN
6804 assert (isLiteral (n)) ;
6805 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
6806 IF n^.literalF.type=charN
6807 THEN
6808 IF DynamicStrings.char (s, -1)='C'
6809 THEN
6810 s := DynamicStrings.Slice (DynamicStrings.Mark (s), 0, -1) ;
6811 IF DynamicStrings.char (s, 0)#'0'
6812 THEN
6813 s := DynamicStrings.ConCat (InitString('0'), DynamicStrings.Mark (s))
6814 END
6815 END ;
6816 outText (p, "(char)") ;
6817 setNeedSpace (p)
6818 ELSIF DynamicStrings.char (s, -1) = 'H'
6819 THEN
6820 outText (p, "0x") ;
6821 s := DynamicStrings.Slice (DynamicStrings.Mark (s), 0, -1)
6822 ELSIF DynamicStrings.char (s, -1) = 'B'
6823 THEN
6824 outText (p, "0") ;
6825 s := DynamicStrings.Slice (DynamicStrings.Mark (s), 0, -1)
6826 END ;
6827 outTextS (p, s) ;
6828 s := KillString (s)
6829 END doLiteralC ;
6830
6831
6832 (*
6833 doLiteral -
6834 *)
6835
6836 PROCEDURE doLiteral (p: pretty; n: node) ;
6837 VAR
6838 s: String ;
6839 BEGIN
6840 assert (isLiteral (n)) ;
6841 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
6842 IF n^.literalF.type=charN
6843 THEN
6844 IF DynamicStrings.char (s, -1)='C'
6845 THEN
6846 s := DynamicStrings.Slice (DynamicStrings.Mark (s), 0, -1) ;
6847 IF DynamicStrings.char (s, 0)#'0'
6848 THEN
6849 s := DynamicStrings.ConCat (InitString('0'), DynamicStrings.Mark (s))
6850 END
6851 END ;
6852 outText (p, "(char)") ;
6853 setNeedSpace (p)
6854 END ;
6855 outTextS (p, s) ;
6856 s := KillString (s)
6857 END doLiteral ;
6858
6859
6860 (*
6861 isString - returns TRUE if node, n, is a string.
6862 *)
6863
6864 PROCEDURE isString (n: node) : BOOLEAN ;
6865 BEGIN
6866 assert (n#NIL) ;
6867 RETURN n^.kind=string
6868 END isString ;
6869
6870
6871 (*
6872 doString -
6873 *)
6874
6875 PROCEDURE doString (p: pretty; n: node) ;
6876 VAR
6877 s: String ;
6878 BEGIN
6879 assert (isString (n)) ;
6880 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
6881 outTextS (p, s) ;
6882 s := KillString (s)
6883 ; HALT
6884 (*
6885 IF DynamicStrings.Index (s, '"', 0)=-1
6886 THEN
6887 outText (p, '"') ;
6888 outTextS (p, s) ;
6889 outText (p, '"')
6890 ELSIF DynamicStrings.Index (s, "'", 0)=-1
6891 THEN
6892 outText (p, '"') ;
6893 outTextS (p, s) ;
6894 outText (p, '"')
6895 ELSE
6896 metaError1 ('illegal string {%1k}', n)
6897 END
6898 *)
6899 END doString ;
6900
6901
6902 (*
6903 replaceChar - replace every occurance of, ch, by, a and return modified string, s.
6904 *)
6905
6906 PROCEDURE replaceChar (s: String; ch: CHAR; a: ARRAY OF CHAR) : String ;
6907 VAR
6908 i: INTEGER ;
6909 BEGIN
6910 i := 0 ;
6911 LOOP
6912 i := DynamicStrings.Index (s, ch, i) ;
6913 IF i = 0
6914 THEN
6915 s := ConCat (InitString (a), DynamicStrings.Slice (s, 1, 0)) ;
6916 i := StrLen (a)
6917 ELSIF i > 0
6918 THEN
6919 s := ConCat (ConCat (DynamicStrings.Slice (s, 0, i), Mark (InitString (a))), DynamicStrings.Slice (s, i+1, 0)) ;
6920 INC (i, StrLen (a))
6921 ELSE
6922 RETURN s
6923 END
6924 END
6925 END replaceChar ;
6926
6927
6928 (*
6929 toCstring - translates string, n, into a C string
6930 and returns the new String.
6931 *)
6932
6933 PROCEDURE toCstring (n: Name) : String ;
6934 VAR
6935 s: String ;
6936 BEGIN
6937 s := DynamicStrings.Slice (InitStringCharStar (keyToCharStar (n)), 1, -1) ;
6938 RETURN replaceChar (replaceChar (s, '\', '\\'), '"', '\"')
6939 END toCstring ;
6940
6941
6942 (*
6943 toCchar -
6944 *)
6945
6946 PROCEDURE toCchar (n: Name) : String ;
6947 VAR
6948 s: String ;
6949 BEGIN
6950 s := DynamicStrings.Slice (InitStringCharStar (keyToCharStar (n)), 1, -1) ;
6951 RETURN replaceChar (replaceChar (s, '\', '\\'), "'", "\'")
6952 END toCchar ;
6953
6954
6955 (*
6956 countChar -
6957 *)
6958
6959 PROCEDURE countChar (s: String; ch: CHAR) : CARDINAL ;
6960 VAR
6961 i: INTEGER ;
6962 c: CARDINAL ;
6963 BEGIN
6964 c := 0 ;
6965 i := 0 ;
6966 LOOP
6967 i := DynamicStrings.Index (s, ch, i) ;
6968 IF i >= 0
6969 THEN
6970 INC (i) ;
6971 INC (c)
6972 ELSE
6973 RETURN c
6974 END
6975 END
6976 END countChar ;
6977
6978
6979 (*
6980 lenCstring -
6981 *)
6982
6983 PROCEDURE lenCstring (s: String) : CARDINAL ;
6984 BEGIN
6985 RETURN DynamicStrings.Length (s) - countChar (s, '\')
6986 END lenCstring ;
6987
6988
6989 (*
6990 outCstring -
6991 *)
6992
6993 PROCEDURE outCstring (p: pretty; s: node; aString: BOOLEAN) ;
6994 BEGIN
6995 IF aString
6996 THEN
6997 outText (p, '"') ;
6998 outRawS (p, s^.stringF.cstring) ;
6999 outText (p, '"')
7000 ELSE
7001 outText (p, "'") ;
7002 outRawS (p, s^.stringF.cchar) ;
7003 outText (p, "'")
7004 END
7005 END outCstring ;
7006
7007
7008 (*
7009 doStringC -
7010 *)
7011
7012 PROCEDURE doStringC (p: pretty; n: node) ;
7013 VAR
7014 s: String ;
7015 BEGIN
7016 assert (isString (n)) ;
7017 outCstring (p, n, NOT n^.stringF.isCharCompatible)
7018 (*
7019 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
7020 IF DynamicStrings.Length (s)>3
7021 THEN
7022 IF DynamicStrings.Index (s, '"', 0)=-1
7023 THEN
7024 s := DynamicStrings.Slice (s, 1, -1) ;
7025 outText (p, '"') ;
7026 outCstring (p, s) ;
7027 outText (p, '"')
7028 ELSIF DynamicStrings.Index (s, "'", 0)=-1
7029 THEN
7030 s := DynamicStrings.Slice (s, 1, -1) ;
7031 outText (p, '"') ;
7032 outCstring (p, s) ;
7033 outText (p, '"')
7034 ELSE
7035 metaError1 ('illegal string {%1k}', n)
7036 END
7037 ELSIF DynamicStrings.Length (s) = 3
7038 THEN
7039 s := DynamicStrings.Slice (s, 1, -1) ;
7040 outText (p, "'") ;
7041 IF DynamicStrings.char (s, 0) = "'"
7042 THEN
7043 outText (p, "\'")
7044 ELSIF DynamicStrings.char (s, 0) = "\"
7045 THEN
7046 outText (p, "\\")
7047 ELSE
7048 outTextS (p, s)
7049 END ;
7050 outText (p, "'")
7051 ELSE
7052 outText (p, "'\0'")
7053 END ;
7054 s := KillString (s)
7055 *)
7056 END doStringC ;
7057
7058
7059 (*
7060 isPunct -
7061 *)
7062
7063 PROCEDURE isPunct (ch: CHAR) : BOOLEAN ;
7064 BEGIN
7065 RETURN (ch = '.') OR (ch = '(') OR (ch = ')') OR
7066 (ch = '^') OR (ch = ':') OR (ch = ';') OR
7067 (ch = '{') OR (ch = '}') OR (ch = ',') OR
7068 (ch = '*')
7069 END isPunct ;
7070
7071
7072 (*
7073 isWhite -
7074 *)
7075
7076 PROCEDURE isWhite (ch: CHAR) : BOOLEAN ;
7077 BEGIN
7078 RETURN (ch = ' ') OR (ch = tab) OR (ch = lf)
7079 END isWhite ;
7080
7081
7082 (*
7083 outText -
7084 *)
7085
7086 PROCEDURE outText (p: pretty; a: ARRAY OF CHAR) ;
7087 VAR
7088 s: String ;
7089 BEGIN
7090 s := InitString (a) ;
7091 outTextS (p, s) ;
7092 s := KillString (s)
7093 END outText ;
7094
7095
7096 (*
7097 outRawS -
7098 *)
7099
7100 PROCEDURE outRawS (p: pretty; s: String) ;
7101 BEGIN
7102 raw (p, s)
7103 END outRawS ;
7104
7105
7106 (*
7107 outKm2 -
7108 *)
7109
7110 PROCEDURE outKm2 (p: pretty; a: ARRAY OF CHAR) : pretty ;
7111 VAR
7112 i: CARDINAL ;
7113 s: String ;
7114 BEGIN
7115 IF StrEqual (a, 'RECORD')
7116 THEN
7117 p := pushPretty (p) ;
7118 i := getcurpos (p) ;
7119 setindent (p, i) ;
7120 outText (p, a) ;
7121 p := pushPretty (p) ;
7122 setindent (p, i + indentation)
7123 ELSIF StrEqual (a, 'END')
7124 THEN
7125 p := popPretty (p) ;
7126 outText (p, a) ;
7127 p := popPretty (p)
7128 END ;
7129 RETURN p
7130 END outKm2 ;
7131
7132
7133 (*
7134 outKc -
7135 *)
7136
7137 PROCEDURE outKc (p: pretty; a: ARRAY OF CHAR) : pretty ;
7138 VAR
7139 i : INTEGER ;
7140 c : CARDINAL ;
7141 s, t: String ;
7142 BEGIN
7143 s := InitString (a) ;
7144 i := DynamicStrings.Index (s, '\', 0) ;
7145 IF i=-1
7146 THEN
7147 t := NIL
7148 ELSE
7149 t := DynamicStrings.Slice (s, i, 0) ;
7150 s := DynamicStrings.Slice (Mark (s), 0, i)
7151 END ;
7152 IF DynamicStrings.char (s, 0)='{'
7153 THEN
7154 p := pushPretty (p) ;
7155 c := getcurpos (p) ;
7156 setindent (p, c) ;
7157 outTextS (p, s) ;
7158 p := pushPretty (p) ;
7159 setindent (p, c + indentationC)
7160 ELSIF DynamicStrings.char (s, 0)='}'
7161 THEN
7162 p := popPretty (p) ;
7163 outTextS (p, s) ;
7164 p := popPretty (p)
7165 END ;
7166 outTextS (p, t) ;
7167 t := KillString (t) ;
7168 s := KillString (s) ;
7169 RETURN p
7170 END outKc ;
7171
7172
7173 (*
7174 outTextS -
7175 *)
7176
7177 PROCEDURE outTextS (p: pretty; s: String) ;
7178 BEGIN
7179 IF s # NIL
7180 THEN
7181 prints (p, s)
7182 END
7183 END outTextS ;
7184
7185
7186 (*
7187 outCard -
7188 *)
7189
7190 PROCEDURE outCard (p: pretty; c: CARDINAL) ;
7191 VAR
7192 s: String ;
7193 BEGIN
7194 s := CardinalToString (c, 0, ' ', 10, FALSE) ;
7195 outTextS (p, s) ;
7196 s := KillString (s)
7197 END outCard ;
7198
7199
7200 (*
7201 outTextN -
7202 *)
7203
7204 PROCEDURE outTextN (p: pretty; n: Name) ;
7205 VAR
7206 s: String ;
7207 BEGIN
7208 s := InitStringCharStar (keyToCharStar (n)) ;
7209 prints (p, s) ;
7210 s := KillString (s)
7211 END outTextN ;
7212
7213
7214 (*
7215 outputEnumerationC -
7216 *)
7217
7218 PROCEDURE outputEnumerationC (p: pretty; n: node) ;
7219 VAR
7220 i, h: CARDINAL ;
7221 s : node ;
7222 t : String ;
7223 BEGIN
7224 outText (p, "enum {") ;
7225 i := LowIndice (n^.enumerationF.listOfSons) ;
7226 h := HighIndice (n^.enumerationF.listOfSons) ;
7227 WHILE i <= h DO
7228 s := GetIndice (n^.enumerationF.listOfSons, i) ;
7229 doFQDNameC (p, s, FALSE) ;
7230 IF i < h
7231 THEN
7232 outText (p, ",") ; setNeedSpace (p)
7233 END ;
7234 INC (i)
7235 END ;
7236 outText (p, "}")
7237 END outputEnumerationC ;
7238
7239
7240 (*
7241 isDeclType - return TRUE if the current module should declare type.
7242 *)
7243
7244 PROCEDURE isDeclType (type: node) : BOOLEAN ;
7245 VAR
7246 n,
7247 def : node ;
7248 name: Name ;
7249 BEGIN
7250 IF isImp (currentModule)
7251 THEN
7252 name := getSymName (type) ;
7253 IF name # NulName
7254 THEN
7255 (* Lookup the matching .def module. *)
7256 def := lookupDef (getSymName (currentModule)) ;
7257 IF def # NIL
7258 THEN
7259 (* Return TRUE if the symbol has not already been declared in the .def. *)
7260 RETURN lookupExported (def, name) = NIL
7261 END
7262 END
7263 END ;
7264 RETURN TRUE
7265 END isDeclType ;
7266
7267
7268 (*
7269 doEnumerationC -
7270 *)
7271
7272 PROCEDURE doEnumerationC (p: pretty; n: node) ;
7273 BEGIN
7274 IF isDeclType (n)
7275 THEN
7276 outputEnumerationC (p, n)
7277 END
7278 END doEnumerationC ;
7279
7280
7281 (*
7282 doNamesC -
7283 *)
7284
7285 PROCEDURE doNamesC (p: pretty; n: Name) ;
7286 VAR
7287 s: String ;
7288 BEGIN
7289 s := InitStringCharStar (keyToCharStar (n)) ;
7290 outTextS (p, s) ;
7291 s := KillString (s)
7292 END doNamesC ;
7293
7294
7295 (*
7296 doNameC -
7297 *)
7298
7299 PROCEDURE doNameC (p: pretty; n: node) ;
7300 BEGIN
7301 IF (n#NIL) AND (getSymName (n)#NulName)
7302 THEN
7303 doNamesC (p, getSymName (n))
7304 END
7305 END doNameC ;
7306
7307
7308 (*
7309 initCname -
7310 *)
7311
7312 PROCEDURE initCname (VAR c: cnameT) ;
7313 BEGIN
7314 c.init := FALSE
7315 END initCname ;
7316
7317
7318 (*
7319 doCname -
7320 *)
7321
7322 PROCEDURE doCname (n: Name; VAR c: cnameT; scopes: BOOLEAN) : Name ;
7323 VAR
7324 s: String ;
7325 BEGIN
7326 IF c.init
7327 THEN
7328 RETURN c.name
7329 ELSE
7330 c.init := TRUE ;
7331 s := keyc.cname (n, scopes) ;
7332 IF s=NIL
7333 THEN
7334 c.name := n
7335 ELSE
7336 c.name := makekey (DynamicStrings.string (s)) ;
7337 s := KillString (s)
7338 END ;
7339 RETURN c.name
7340 END
7341 END doCname ;
7342
7343
7344 (*
7345 getDName -
7346 *)
7347
7348 PROCEDURE getDName (n: node; scopes: BOOLEAN) : Name ;
7349 VAR
7350 m: Name ;
7351 BEGIN
7352 m := getSymName (n) ;
7353 CASE n^.kind OF
7354
7355 procedure : RETURN doCname (m, n^.procedureF.cname, scopes) |
7356 var : RETURN doCname (m, n^.varF.cname, scopes) |
7357 recordfield : RETURN doCname (m, n^.recordfieldF.cname, scopes) |
7358 enumerationfield: RETURN doCname (m, n^.enumerationfieldF.cname, scopes)
7359
7360 ELSE
7361 END ;
7362 RETURN m
7363 END getDName ;
7364
7365
7366 (*
7367 doDNameC -
7368 *)
7369
7370 PROCEDURE doDNameC (p: pretty; n: node; scopes: BOOLEAN) ;
7371 BEGIN
7372 IF (n#NIL) AND (getSymName (n)#NulName)
7373 THEN
7374 doNamesC (p, getDName (n, scopes))
7375 END
7376 END doDNameC ;
7377
7378
7379 (*
7380 doFQDNameC -
7381 *)
7382
7383 PROCEDURE doFQDNameC (p: pretty; n: node; scopes: BOOLEAN) ;
7384 VAR
7385 s: String ;
7386 BEGIN
7387 s := getFQDstring (n, scopes) ;
7388 outTextS (p, s) ;
7389 s := KillString (s)
7390 END doFQDNameC ;
7391
7392
7393 (*
7394 doFQNameC -
7395 *)
7396
7397 PROCEDURE doFQNameC (p: pretty; n: node) ;
7398 VAR
7399 s: String ;
7400 BEGIN
7401 s := getFQstring (n) ;
7402 outTextS (p, s) ;
7403 s := KillString (s)
7404 END doFQNameC ;
7405
7406
7407 (*
7408 doNameM2 -
7409 *)
7410
7411 PROCEDURE doNameM2 (p: pretty; n: node) ;
7412 BEGIN
7413 doNameC (p, n)
7414 END doNameM2 ;
7415
7416
7417 (*
7418 doUsed -
7419 *)
7420
7421 PROCEDURE doUsed (p: pretty; used: BOOLEAN) ;
7422 BEGIN
7423 IF NOT used
7424 THEN
7425 setNeedSpace (p) ;
7426 outText (p, "__attribute__((unused))")
7427 END
7428 END doUsed ;
7429
7430
7431 (*
7432 doHighC -
7433 *)
7434
7435 PROCEDURE doHighC (p: pretty; a: node; n: Name; isused: BOOLEAN) ;
7436 BEGIN
7437 IF isArray (a) AND isUnbounded (a)
7438 THEN
7439 (* need to display high. *)
7440 print (p, ",") ; setNeedSpace (p) ;
7441 doTypeNameC (p, cardinalN) ; setNeedSpace (p) ;
7442 print (p, "_") ; outTextN (p, n) ; print (p, "_high") ;
7443 doUsed (p, isused)
7444 END
7445 END doHighC ;
7446
7447
7448 (*
7449 doParamConstCast -
7450 *)
7451
7452 PROCEDURE doParamConstCast (p: pretty; n: node) ;
7453 VAR
7454 ptype: node ;
7455 BEGIN
7456 ptype := getType (n) ;
7457 IF isArray (ptype) AND isUnbounded (ptype) AND (lang = ansiCP)
7458 THEN
7459 outText (p, "const") ;
7460 setNeedSpace (p)
7461 END
7462 END doParamConstCast ;
7463
7464
7465 (*
7466 getParameterVariable - returns the variable which shadows the parameter
7467 named, m, in parameter block, n.
7468 *)
7469
7470 PROCEDURE getParameterVariable (n: node; m: Name) : node ;
7471 VAR
7472 p: node ;
7473 BEGIN
7474 assert (isParam (n) OR isVarParam (n)) ;
7475 IF isParam (n)
7476 THEN
7477 p := n^.paramF.scope
7478 ELSE
7479 p := n^.varparamF.scope
7480 END ;
7481 assert (isProcedure (p)) ;
7482 RETURN lookupInScope (p, m)
7483 END getParameterVariable ;
7484
7485
7486 (*
7487 doParamTypeEmit - emit parameter type for C/C++. It checks to see if the
7488 parameter type is a procedure type and if it were declared
7489 in a definition module for "C" and if so it uses the "C"
7490 definition for a procedure type, rather than the mc
7491 C++ version.
7492 *)
7493
7494 PROCEDURE doParamTypeEmit (p: pretty; paramnode, paramtype: node) ;
7495 BEGIN
7496 assert (isParam (paramnode) OR isVarParam (paramnode)) ;
7497 IF isForC (paramnode) AND isProcType (skipType (paramtype))
7498 THEN
7499 doFQNameC (p, paramtype) ;
7500 outText (p, "_C")
7501 ELSE
7502 doTypeNameC (p, paramtype) ;
7503 doOpaqueModifier (p, paramnode) ;
7504 (*
7505 IF nodeUsesOpaque (paramnode) AND (NOT getNodeOpaqueVoidStar (paramnode))
7506 THEN
7507 outText (p, '__opaque')
7508 END
7509 *)
7510 END
7511 END doParamTypeEmit ;
7512
7513
7514 (*
7515 doParamTypeNameModifier - Add an _ to an unbounded parameter which is non var.
7516 *)
7517
7518 PROCEDURE doParamTypeNameModifier (p: pretty; ptype: node; varparam: BOOLEAN) ;
7519 BEGIN
7520 IF (NOT varparam) AND isArray (ptype) AND isUnbounded (ptype)
7521 THEN
7522 outText (p, '_')
7523 END
7524 END doParamTypeNameModifier ;
7525
7526
7527 (*
7528 initOpaqueCastState - assign fields opaque and voidstar in opaquestate.
7529 *)
7530
7531 PROCEDURE initOpaqueCastState (VAR opaquestate: opaqueCastState; opaque, voidstar: BOOLEAN) ;
7532 BEGIN
7533 opaquestate.opaque := opaque ;
7534 opaquestate.voidStar := voidstar
7535 END initOpaqueCastState ;
7536
7537
7538 (*
7539 initNodeOpaqueCastState - assign opaque and currentvoidstar
7540 *)
7541
7542 PROCEDURE initNodeOpaqueCastState (n: node; opaque, voidstar: BOOLEAN) ;
7543 BEGIN
7544 CASE n^.kind OF
7545
7546 opaquecast : initOpaqueCastState (n^.opaquecastF.opaqueState, opaque, voidstar) |
7547 funccall : initOpaqueCastState (n^.funccallF.opaqueState, opaque, voidstar) |
7548 var : initOpaqueCastState (n^.varF.opaqueState, opaque, voidstar) |
7549 array : initOpaqueCastState (n^.arrayF.opaqueState, opaque, voidstar) |
7550 varparam : initOpaqueCastState (n^.varparamF.opaqueState, opaque, voidstar) |
7551 param : initOpaqueCastState (n^.paramF.opaqueState, opaque, voidstar) |
7552 pointer : initOpaqueCastState (n^.pointerF.opaqueState, opaque, voidstar) |
7553 recordfield : initOpaqueCastState (n^.recordfieldF.opaqueState, opaque, voidstar) |
7554 componentref: initOpaqueCastState (n^.componentrefF.opaqueState, opaque, voidstar) |
7555 pointerref : initOpaqueCastState (n^.pointerrefF.opaqueState, opaque, voidstar) |
7556 arrayref : initOpaqueCastState (n^.arrayrefF.opaqueState, opaque, voidstar) |
7557 procedure : initOpaqueCastState (n^.procedureF.opaqueState, opaque, voidstar) |
7558 proctype : initOpaqueCastState (n^.proctypeF.opaqueState, opaque, voidstar)
7559
7560 ELSE
7561 HALT
7562 END
7563 END initNodeOpaqueCastState ;
7564
7565
7566 (*
7567 setOpaqueCastState - set the voidStar field in opaquestate.
7568 *)
7569
7570 PROCEDURE setOpaqueCastState (VAR opaquestate: opaqueCastState; voidstar: BOOLEAN) ;
7571 BEGIN
7572 opaquestate.voidStar := voidstar
7573 END setOpaqueCastState ;
7574
7575
7576 (*
7577 setNodeOpaqueVoidStar - sets the voidStar field in node to voidstar.
7578 *)
7579
7580 PROCEDURE setNodeOpaqueVoidStar (n: node; voidstar: BOOLEAN) ;
7581 BEGIN
7582 assert (nodeUsesOpaque (n)) ;
7583 CASE n^.kind OF
7584
7585 opaquecast : setOpaqueCastState (n^.opaquecastF.opaqueState, voidstar) |
7586 funccall : setOpaqueCastState (n^.funccallF.opaqueState, voidstar) |
7587 var : setOpaqueCastState (n^.varF.opaqueState, voidstar) |
7588 array : setOpaqueCastState (n^.arrayF.opaqueState, voidstar) |
7589 varparam : setOpaqueCastState (n^.varparamF.opaqueState, voidstar) |
7590 param : setOpaqueCastState (n^.paramF.opaqueState, voidstar) |
7591 pointer : setOpaqueCastState (n^.pointerF.opaqueState, voidstar) |
7592 recordfield : setOpaqueCastState (n^.recordfieldF.opaqueState, voidstar) |
7593 componentref: assert (NOT voidstar) ;
7594 setOpaqueCastState (n^.componentrefF.opaqueState, voidstar) |
7595 pointerref : assert (NOT voidstar) ;
7596 setOpaqueCastState (n^.pointerrefF.opaqueState, voidstar) |
7597 arrayref : setOpaqueCastState (n^.arrayrefF.opaqueState, voidstar) |
7598 procedure : setOpaqueCastState (n^.procedureF.opaqueState, voidstar) |
7599 proctype : setOpaqueCastState (n^.proctypeF.opaqueState, voidstar)
7600
7601 ELSE
7602 HALT
7603 END
7604 END setNodeOpaqueVoidStar ;
7605
7606
7607 (*
7608 nodeUsesOpaque - return TRUE if node n uses an opaque type.
7609 *)
7610
7611 PROCEDURE nodeUsesOpaque (n: node) : BOOLEAN ;
7612 BEGIN
7613 CASE n^.kind OF
7614
7615 opaquecast : RETURN n^.opaquecastF.opaqueState.opaque |
7616 funccall : RETURN n^.funccallF.opaqueState.opaque |
7617 var : RETURN n^.varF.opaqueState.opaque |
7618 array : RETURN n^.arrayF.opaqueState.opaque |
7619 varparam : RETURN n^.varparamF.opaqueState.opaque |
7620 param : RETURN n^.paramF.opaqueState.opaque |
7621 pointer : RETURN n^.pointerF.opaqueState.opaque |
7622 recordfield : RETURN n^.recordfieldF.opaqueState.opaque |
7623 componentref: RETURN n^.componentrefF.opaqueState.opaque |
7624 pointerref : RETURN n^.pointerrefF.opaqueState.opaque |
7625 arrayref : RETURN n^.arrayrefF.opaqueState.opaque |
7626 procedure : RETURN n^.procedureF.opaqueState.opaque |
7627 proctype : RETURN n^.proctypeF.opaqueState.opaque |
7628 deref : RETURN nodeUsesOpaque (n^.unaryF.arg)
7629
7630 ELSE
7631 RETURN FALSE
7632 END
7633 END nodeUsesOpaque ;
7634
7635
7636 (*
7637 getNodeOpaqueVoidStar - return TRUE if the opaque type used by node n is a void *.
7638 *)
7639
7640 PROCEDURE getNodeOpaqueVoidStar (n: node) : BOOLEAN ;
7641 BEGIN
7642 assert (nodeUsesOpaque (n)) ;
7643 CASE n^.kind OF
7644
7645 opaquecast : RETURN n^.opaquecastF.opaqueState.voidStar |
7646 funccall : RETURN n^.funccallF.opaqueState.voidStar |
7647 var : RETURN n^.varF.opaqueState.voidStar |
7648 array : RETURN n^.arrayF.opaqueState.voidStar |
7649 varparam : RETURN n^.varparamF.opaqueState.voidStar |
7650 param : RETURN n^.paramF.opaqueState.voidStar |
7651 pointer : RETURN n^.pointerF.opaqueState.voidStar |
7652 recordfield : RETURN n^.recordfieldF.opaqueState.voidStar |
7653 componentref: RETURN n^.componentrefF.opaqueState.voidStar |
7654 pointerref : RETURN n^.pointerrefF.opaqueState.voidStar |
7655 arrayref : RETURN n^.arrayrefF.opaqueState.voidStar |
7656 procedure : RETURN n^.procedureF.opaqueState.voidStar |
7657 proctype : RETURN n^.proctypeF.opaqueState.voidStar |
7658 deref : RETURN FALSE
7659
7660 ELSE
7661 HALT
7662 END
7663 END getNodeOpaqueVoidStar ;
7664
7665
7666 (*
7667 getOpaqueFlushNecessary - return TRUE if the value next differs from the opaque state.
7668 *)
7669
7670 PROCEDURE getOpaqueFlushNecessary (state: opaqueCastState; next: BOOLEAN) : BOOLEAN ;
7671 BEGIN
7672 RETURN state.opaque AND (state.voidStar # next)
7673 END getOpaqueFlushNecessary ;
7674
7675
7676 (*
7677 getNodeOpaqueFlushNecessary - return TRUE if the value of next requires a cast.
7678 *)
7679
7680 PROCEDURE getNodeOpaqueFlushNecessary (n: node; next: BOOLEAN) : BOOLEAN ;
7681 BEGIN
7682 CASE n^.kind OF
7683
7684 opaquecast : RETURN getOpaqueFlushNecessary (n^.opaquecastF.opaqueState, next) |
7685 funccall : RETURN getOpaqueFlushNecessary (n^.funccallF.opaqueState, next) |
7686 var : RETURN getOpaqueFlushNecessary (n^.varF.opaqueState, next) |
7687 array : RETURN getOpaqueFlushNecessary (n^.arrayF.opaqueState, next) |
7688 varparam : RETURN getOpaqueFlushNecessary (n^.varparamF.opaqueState, next) |
7689 param : RETURN getOpaqueFlushNecessary (n^.paramF.opaqueState, next) |
7690 pointer : RETURN getOpaqueFlushNecessary (n^.pointerF.opaqueState, next) |
7691 recordfield : RETURN getOpaqueFlushNecessary (n^.recordfieldF.opaqueState, next) |
7692 componentref: RETURN getOpaqueFlushNecessary (n^.componentrefF.opaqueState, next) |
7693 pointerref : RETURN getOpaqueFlushNecessary (n^.pointerrefF.opaqueState, next) |
7694 arrayref : RETURN getOpaqueFlushNecessary (n^.arrayrefF.opaqueState, next) |
7695 procedure : RETURN getOpaqueFlushNecessary (n^.procedureF.opaqueState, next) |
7696 proctype : RETURN getOpaqueFlushNecessary (n^.proctypeF.opaqueState, next)
7697
7698 ELSE
7699 RETURN FALSE
7700 END
7701 END getNodeOpaqueFlushNecessary ;
7702
7703
7704 (*
7705 makeOpaqueCast - wrap node n with an opaquecast node and assign
7706 voidstar into the new opaque state.
7707 *)
7708
7709 PROCEDURE makeOpaqueCast (n: node; voidstar: BOOLEAN) : node ;
7710 VAR
7711 o: node ;
7712 BEGIN
7713 o := newNode (opaquecast) ;
7714 WITH o^.opaquecastF DO
7715 exp := n ;
7716 initOpaqueCastState (opaqueState, TRUE, voidstar)
7717 END ;
7718 RETURN o
7719 END makeOpaqueCast ;
7720
7721
7722 (*
7723 flushOpaque - perform a cast to voidstar (if necessary) and ignore the new
7724 node which could be created.
7725 *)
7726
7727 PROCEDURE flushOpaque (p: pretty; n: node; toVoidStar: BOOLEAN) ;
7728 VAR
7729 o: node ;
7730 BEGIN
7731 o := castOpaque (p, n, toVoidStar)
7732 END flushOpaque ;
7733
7734
7735 (*
7736 castOpaque - flushes the opaque type casts if necessary and changes the
7737 voidstar boolean value. If necessary it creates a opaquecast
7738 and returns the new node otherwise return n.
7739 *)
7740
7741 PROCEDURE castOpaque (p: pretty; n: node; toVoidStar: BOOLEAN) : node ;
7742 VAR
7743 type: node ;
7744 BEGIN
7745 IF getNodeOpaqueFlushNecessary (n, toVoidStar)
7746 THEN
7747 type := getType (n) ;
7748 IF toVoidStar
7749 THEN
7750 (* next is true cast to void * opaque type. *)
7751 outText (p, 'static_cast<') ;
7752 doTypeNameC (p, type) ;
7753 noSpace (p) ;
7754 outText (p, '> (') ;
7755 doExprC (p, n) ;
7756 outText (p, ')') ;
7757 RETURN makeOpaqueCast (n, TRUE)
7758 ELSE
7759 (* next is false cast to __opaque opaque type. *)
7760 outText (p, 'static_cast<') ;
7761 doTypeNameC (p, type) ;
7762 outText (p, '__opaque') ;
7763 noSpace (p) ;
7764 outText (p, '> (') ;
7765 doExprC (p, n) ;
7766 outText (p, ')') ;
7767 RETURN makeOpaqueCast (n, FALSE)
7768 END
7769 ELSE
7770 IF debugOpaque
7771 THEN
7772 doP := p ;
7773 dumpOpaqueState (n) ;
7774 IF nodeUsesOpaque (n)
7775 THEN
7776 outText (p, ' /* no difference seen */ ')
7777 ELSE
7778 outText (p, ' /* no opaque used */ ')
7779 END
7780 END ;
7781 doExprC (p, n)
7782 END ;
7783 RETURN n
7784 END castOpaque ;
7785
7786
7787 (*
7788 isTypeOpaqueDefImp - returns TRUE if type is an opaque type by checking
7789 the def/imp pair of modules or fall back to the
7790 definition module.
7791 *)
7792
7793 PROCEDURE isTypeOpaqueDefImp (type: node) : BOOLEAN ;
7794 VAR
7795 scope,
7796 def,
7797 opaque: node ;
7798 BEGIN
7799 IF type = NIL
7800 THEN
7801 RETURN FALSE
7802 ELSIF isType (type)
7803 THEN
7804 scope := getScope (type) ;
7805 IF isImp (scope)
7806 THEN
7807 def := lookupDef (getSymName (scope)) ;
7808 IF def # NIL
7809 THEN
7810 (* Lookup the type name in the matching definition module. *)
7811 opaque := lookupExported (def, getSymName (type)) ;
7812 RETURN (opaque # NIL) AND isType (opaque) AND isTypeOpaque (opaque)
7813 END
7814 ELSE
7815 (* Otherwise just check the definition module. *)
7816 RETURN isTypeOpaque (type)
7817 END
7818 END ;
7819 RETURN FALSE
7820 END isTypeOpaqueDefImp ;
7821
7822
7823 (*
7824 isParamVoidStar - return TRUE if the procedure or proctype opaque type
7825 parameter should be implemented as a (void * ).
7826 *)
7827
7828 PROCEDURE isParamVoidStar (n: node) : BOOLEAN ;
7829 VAR
7830 proc,
7831 type: node ;
7832 BEGIN
7833 proc := getScope (n) ;
7834 assert (isProcedure (proc) OR isProcType (proc)) ;
7835 type := getType (n) ;
7836 RETURN isReturnVoidStar (proc, type)
7837 END isParamVoidStar ;
7838
7839
7840 (*
7841 isRefVoidStar - returns TRUE if the ref node uses an opaque type which
7842 is represented as a (void * ).
7843 *)
7844
7845 PROCEDURE isRefVoidStar (n: node) : BOOLEAN ;
7846 VAR
7847 type: node ;
7848 BEGIN
7849 type := getType (n) ;
7850 IF (NOT isType (type)) OR (NOT isTypeOpaque (type))
7851 THEN
7852 (* We should finish the procedure as the ref does not use an opaque. *)
7853 RETURN TRUE
7854 ELSE
7855 (* We check whether the opaque type was declared in the implementation
7856 module. If it is declared in the implementation module then we
7857 return FALSE. *)
7858 RETURN NOT isDeclInImp (type)
7859 END
7860 END isRefVoidStar ;
7861
7862
7863 (*
7864 isReturnVoidStar - return TRUE if the procedure or proctype opaque type
7865 return type should be implemented as a (void * ).
7866 *)
7867
7868 PROCEDURE isReturnVoidStar (proc, type: node) : BOOLEAN ;
7869 VAR
7870 def : node ;
7871 BEGIN
7872 assert (isProcedure (proc) OR isProcType (proc)) ;
7873 IF isExported (proc)
7874 THEN
7875 RETURN TRUE
7876 ELSE
7877 (* Not exported therefore local, we check whether the opaque type
7878 was declared in the implementation module. *)
7879 IF isImp (currentModule)
7880 THEN
7881 IF isType (type)
7882 THEN
7883 RETURN NOT isDeclInImp (type)
7884 ELSE
7885 RETURN FALSE
7886 END
7887 ELSE
7888 (* Always use void * in .def modules. *)
7889 RETURN TRUE
7890 END
7891 END
7892 END isReturnVoidStar ;
7893
7894
7895 (*
7896 isVarVoidStar - return TRUE if the variable using an opaque type should
7897 be implemented as a (void * ).
7898 *)
7899
7900 PROCEDURE isVarVoidStar (n: node) : BOOLEAN ;
7901 VAR
7902 type: node ;
7903 BEGIN
7904 assert (isVar (n)) ;
7905 type := getType (n) ;
7906 IF (NOT isType (type)) OR (NOT isTypeOpaque (type))
7907 THEN
7908 (* We should finish the procedure as the variable does not use an opaque. *)
7909 RETURN TRUE
7910 ELSIF isExported (n)
7911 THEN
7912 (* Exported variables using an opaque type will always be implemented
7913 with a (void * ). *)
7914 RETURN TRUE
7915 ELSE
7916 (* Not exported therefore static to the module (local or global non exported
7917 variable), we check whether the opaque type was declared in the
7918 implementation module. If it is declared in the implementation module
7919 then we return FALSE. *)
7920 RETURN NOT isDeclInImp (type)
7921 END
7922 END isVarVoidStar ;
7923
7924
7925 (*
7926 initNodeOpaqueState - initialize the node opaque state.
7927 *)
7928
7929 PROCEDURE initNodeOpaqueState (n: node) ;
7930 VAR
7931 type: node ;
7932 BEGIN
7933 CASE n^.kind OF
7934
7935 opaquecast : | (* This must be done when the cast direction is known. *)
7936 funccall : assignNodeOpaqueCastState (n, getFunction (n)) |
7937 var : type := getType (n) ;
7938 IF n^.varF.isParameter OR n^.varF.isVarParameter
7939 THEN
7940 (* If the variable is really a parameter then it uses
7941 the state of the parameter. *)
7942 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7943 isParamVoidStar (n))
7944 ELSE
7945 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7946 isVarVoidStar (n))
7947 END |
7948 array : type := getType (n) ;
7949 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7950 isExported (n)) |
7951 varparam,
7952 param : assert (isProcedure (getScope (n)) OR isProcType (getScope (n))) ;
7953 type := getType (n) ;
7954 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7955 isParamVoidStar (n)) |
7956 componentref,
7957 pointerref,
7958 pointer,
7959 recordfield,
7960 arrayref : type := getType (n) ;
7961 (* In the future this should be revisited. *)
7962 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7963 isRefVoidStar (n)) |
7964 (* For the moment treat as never exported. *)
7965 proctype,
7966 procedure : (* We only consider the return type for a procedure or proctype.
7967 The parameters and local vars are handled separately (see
7968 above). *)
7969 type := getType (n) ;
7970 IF type = NIL
7971 THEN
7972 (* No return type, therefore no opaque type used. *)
7973 initNodeOpaqueCastState (n, FALSE, FALSE)
7974 ELSE
7975 (* Init state from the return type. Is type an opaque type?
7976 Is the opaque type declared in this module? *)
7977 initNodeOpaqueCastState (n, isTypeOpaqueDefImp (type),
7978 isReturnVoidStar (n, type))
7979 END |
7980
7981 ELSE
7982 END ;
7983 IF debugOpaque
7984 THEN
7985 dumpOpaqueState (n)
7986 END
7987 END initNodeOpaqueState ;
7988
7989
7990 (*
7991 assignNodeOpaqueCastState - copy the opaqueCastState from src into dest.
7992 *)
7993
7994 PROCEDURE assignNodeOpaqueCastState (dest, src: node) ;
7995 BEGIN
7996 IF nodeUsesOpaque (src)
7997 THEN
7998 initNodeOpaqueCastState (dest, TRUE, getNodeOpaqueVoidStar (src))
7999 ELSE
8000 initNodeOpaqueCastState (dest, FALSE, FALSE)
8001 END
8002 END assignNodeOpaqueCastState ;
8003
8004
8005 (*
8006 assignNodeOpaqueCastFalse - assign the voidstar field of dest to false.
8007 It assigns the opaque field of dest to the value
8008 of the src opaque field.
8009 *)
8010
8011 PROCEDURE assignNodeOpaqueCastFalse (dest, src: node) ;
8012 BEGIN
8013 IF nodeUsesOpaque (src)
8014 THEN
8015 initNodeOpaqueCastState (dest, TRUE, FALSE)
8016 ELSE
8017 initNodeOpaqueCastState (dest, FALSE, FALSE)
8018 END
8019 END assignNodeOpaqueCastFalse ;
8020
8021
8022 (*
8023 dumpOpaqueState -
8024 *)
8025
8026 PROCEDURE dumpOpaqueState (n: node) ;
8027 VAR
8028 o: node ;
8029 BEGIN
8030 CASE n^.kind OF
8031
8032 opaquecast,
8033 funccall,
8034 var,
8035 array,
8036 varparam,
8037 param,
8038 pointer,
8039 recordfield,
8040 componentref,
8041 arrayref,
8042 procedure,
8043 proctype : o := n
8044
8045 ELSE
8046 o := NIL
8047 END ;
8048 IF o # NIL
8049 THEN
8050 outText (doP, "/* ") ;
8051 doNameC (doP, o) ;
8052 outText (doP, " ") ;
8053 CASE o^.kind OF
8054
8055 opaquecast : outText (doP, "opaquecast") |
8056 funccall : outText (doP, "funccall") |
8057 var : outText (doP, "var") |
8058 array : outText (doP, "array") |
8059 varparam : outText (doP, "varparam") |
8060 param : outText (doP, "param") |
8061 pointer : outText (doP, "pointer") |
8062 recordfield : outText (doP, "recordfield") |
8063 componentref: outText (doP, "componentref") |
8064 pointerref : outText (doP, "pointerref") |
8065 arrayref : outText (doP, "arrayref") |
8066 procedure : outText (doP, "procedure") |
8067 proctype : outText (doP, "proctype")
8068
8069 ELSE
8070 END ;
8071 IF nodeUsesOpaque (o)
8072 THEN
8073 IF getNodeOpaqueVoidStar (o)
8074 THEN
8075 outText (doP, " uses (void *) opaque")
8076 ELSE
8077 outText (doP, " uses opaque__full")
8078 END ;
8079 END ;
8080 outText (doP, " */ \n")
8081 END
8082 END dumpOpaqueState ;
8083
8084
8085 (*
8086 doParamC - emit parameter for C/C++.
8087 *)
8088
8089 PROCEDURE doParamC (p: pretty; n: node) ;
8090 VAR
8091 v,
8092 ptype: node ;
8093 i : Name ;
8094 c, t : CARDINAL ;
8095 l : wlist ;
8096 BEGIN
8097 assert (isParam (n)) ;
8098 ptype := getType (n) ;
8099 IF n^.paramF.namelist = NIL
8100 THEN
8101 doParamConstCast (p, n) ;
8102 doTypeNameC (p, ptype) ;
8103 doUsed (p, n^.paramF.isUsed) ;
8104 IF isArray (ptype) AND isUnbounded (ptype)
8105 THEN
8106 outText (p, ',') ; setNeedSpace (p) ;
8107 outText (p, 'unsigned int')
8108 END
8109 ELSE
8110 assert (isIdentList (n^.paramF.namelist)) ;
8111 l := n^.paramF.namelist^.identlistF.names ;
8112 IF l=NIL
8113 THEN
8114 doParamConstCast (p, n) ;
8115 doParamTypeEmit (p, n, ptype) ;
8116 IF isArray (ptype) AND isUnbounded (ptype)
8117 THEN
8118 doUsed (p, n^.paramF.isUsed) ;
8119 outText (p, ',') ; setNeedSpace (p) ;
8120 outText (p, 'unsigned int')
8121 END
8122 ELSE
8123 t := wlists.noOfItemsInList (l) ;
8124 c := 1 ;
8125 WHILE c <= t DO
8126 doParamConstCast (p, n) ;
8127 doParamTypeEmit (p, n, ptype) ;
8128 i := wlists.getItemFromList (l, c) ;
8129 IF isArray (ptype) AND isUnbounded (ptype)
8130 THEN
8131 noSpace (p)
8132 ELSE
8133 setNeedSpace (p)
8134 END ;
8135 v := getParameterVariable (n, i) ;
8136 IF v=NIL
8137 THEN
8138 doNamesC (p, keyc.cnamen (i, TRUE))
8139 ELSE
8140 doFQDNameC (p, v, TRUE)
8141 END ;
8142 doParamTypeNameModifier (p, ptype, FALSE) ;
8143 doUsed (p, n^.paramF.isUsed) ;
8144 doHighC (p, ptype, i, n^.paramF.isUsed) ;
8145 IF c<t
8146 THEN
8147 outText (p, ',') ; setNeedSpace (p)
8148 END ;
8149 INC (c)
8150 END
8151 END
8152 END
8153 END doParamC ;
8154
8155
8156 (*
8157 doVarParamC - emit a VAR parameter for C/C++.
8158 *)
8159
8160 PROCEDURE doVarParamC (p: pretty; n: node) ;
8161 VAR
8162 v,
8163 ptype: node ;
8164 i : Name ;
8165 c, t : CARDINAL ;
8166 l : wlist ;
8167 BEGIN
8168 assert (isVarParam (n)) ;
8169 ptype := getType (n) ;
8170 IF n^.varparamF.namelist = NIL
8171 THEN
8172 doTypeNameC (p, ptype) ;
8173 (* doTypeC (p, ptype, n) ; *)
8174 IF NOT isArray (ptype)
8175 THEN
8176 setNeedSpace (p) ;
8177 outText (p, "*")
8178 END ;
8179 doUsed (p, n^.varparamF.isUsed) ;
8180 IF isArray (ptype) AND isUnbounded (ptype)
8181 THEN
8182 outText (p, ',') ; setNeedSpace (p) ;
8183 outText (p, 'unsigned int')
8184 END
8185 ELSE
8186 assert (isIdentList (n^.varparamF.namelist)) ;
8187 l := n^.varparamF.namelist^.identlistF.names ;
8188 IF l=NIL
8189 THEN
8190 doParamTypeEmit (p, n, ptype) ;
8191 doUsed (p, n^.varparamF.isUsed)
8192 ELSE
8193 t := wlists.noOfItemsInList (l) ;
8194 c := 1 ;
8195 WHILE c <= t DO
8196 doParamTypeEmit (p, n, ptype) ;
8197 IF NOT isArray (ptype)
8198 THEN
8199 setNeedSpace (p) ;
8200 outText (p, "*")
8201 END ;
8202 i := wlists.getItemFromList (l, c) ;
8203 v := getParameterVariable (n, i) ;
8204 IF v=NIL
8205 THEN
8206 doNamesC (p, keyc.cnamen (i, TRUE))
8207 ELSE
8208 doFQDNameC (p, v, TRUE)
8209 END ;
8210 doParamTypeNameModifier (p, ptype, TRUE) ;
8211 doUsed (p, n^.varparamF.isUsed) ;
8212 doHighC (p, ptype, i, n^.varparamF.isUsed) ;
8213 IF c<t
8214 THEN
8215 outText (p, ',') ; setNeedSpace (p)
8216 END ;
8217 INC (c)
8218 END
8219 END
8220 END
8221 END doVarParamC ;
8222
8223
8224 (*
8225 doOptargC -
8226 *)
8227
8228 PROCEDURE doOptargC (p: pretty; n: node) ;
8229 VAR
8230 ptype: node ;
8231 i : Name ;
8232 t : CARDINAL ;
8233 l : wlist ;
8234 BEGIN
8235 assert (isOptarg (n)) ;
8236 ptype := getType (n) ;
8237 assert (n^.optargF.namelist # NIL) ;
8238 assert (isIdentList (n^.paramF.namelist)) ;
8239 l := n^.paramF.namelist^.identlistF.names ;
8240 assert (l # NIL) ;
8241 t := wlists.noOfItemsInList (l) ;
8242 assert (t = 1) ;
8243 doTypeNameC (p, ptype) ;
8244 i := wlists.getItemFromList (l, 1) ;
8245 setNeedSpace (p) ;
8246 doNamesC (p, i)
8247 END doOptargC ;
8248
8249
8250 (*
8251 doParameterC -
8252 *)
8253
8254 PROCEDURE doParameterC (p: pretty; n: node) ;
8255 BEGIN
8256 IF isParam (n)
8257 THEN
8258 doParamC (p, n)
8259 ELSIF isVarParam (n)
8260 THEN
8261 doVarParamC (p, n)
8262 ELSIF isVarargs (n)
8263 THEN
8264 print (p, "...")
8265 ELSIF isOptarg (n)
8266 THEN
8267 doOptargC (p, n)
8268 END
8269 END doParameterC ;
8270
8271
8272 (*
8273 doProcTypeC -
8274 *)
8275
8276 PROCEDURE doProcTypeC (p: pretty; t, n: node) ;
8277 BEGIN
8278 assert (isType (t)) ;
8279 IF isDeclType (t) AND isDeclType (n)
8280 THEN
8281 outputPartial (t) ;
8282 doCompletePartialProcType (p, t, n)
8283 END
8284 END doProcTypeC ;
8285
8286
8287 (*
8288 isDeclInImp - returns TRUE if node type is declared as an opaque and
8289 is declared fully in the current implementation module.
8290 This should only be called if isType (type). Its purpose
8291 is specific to a type checking whether it is an opaque type
8292 declared in the .def/.mod pair of the current imp module.
8293 *)
8294
8295 PROCEDURE isDeclInImp (type: node) : BOOLEAN ;
8296 VAR
8297 scope,
8298 def : node ;
8299 name : Name ;
8300 BEGIN
8301 assert (isType (type)) ;
8302 scope := getScope (type) ;
8303 IF isTypeOpaqueDefImp (type) AND isImp (currentModule)
8304 THEN
8305 name := getSymName (type) ;
8306 IF name # NulName
8307 THEN
8308 (* Lookup the matching .def module. *)
8309 def := lookupDef (getSymName (currentModule)) ;
8310 IF (def # NIL) AND ((def = scope) OR (currentModule = scope))
8311 THEN
8312 (* Return TRUE if the symbol has already been declared in the .def. *)
8313 RETURN lookupExported (def, name) # NIL
8314 END
8315 END
8316 END ;
8317 RETURN FALSE
8318 END isDeclInImp ;
8319
8320
8321 (*
8322 doTypeNameModifier - adds the __opaque modifier to the type n provided
8323 it is an opaque type which is being declared in the
8324 implementation module.
8325 *)
8326
8327 PROCEDURE doTypeNameModifier (p: pretty; n: node) ;
8328 BEGIN
8329 IF isTypeOpaqueDefImp (n) AND isImp (currentModule)
8330 THEN
8331 outText (p, '__opaque')
8332 END
8333 END doTypeNameModifier ;
8334
8335
8336 (*
8337 isGccType - return TRUE if n is tree or location_t.
8338 *)
8339
8340 PROCEDURE isGccType (n: node) : BOOLEAN ;
8341 BEGIN
8342 RETURN (getGccConfigSystem () AND
8343 ((getSymName (n) = makeKey ('location_t')) OR
8344 (getSymName (n) = makeKey ('tree'))))
8345 END isGccType ;
8346
8347
8348 (*
8349 doGccType - record whether we are going to declare tree or location_t
8350 so that the appropriate gcc header can be included instead.
8351 *)
8352
8353 PROCEDURE doGccType (p: pretty; n: node) ;
8354 BEGIN
8355 IF getGccConfigSystem ()
8356 THEN
8357 IF getSymName (n) = makeKey ('location_t')
8358 THEN
8359 outText (p, "/* Not going to declare ") ;
8360 doTypeNameC (p, n) ;
8361 outText (p, " as it is declared in the gcc header input.h. */\n\n") ;
8362 keyc.useGccLocation
8363 ELSIF getSymName (n) = makeKey ('tree')
8364 THEN
8365 outText (p, "/* Not going to declare ") ;
8366 doTypeNameC (p, n) ;
8367 outText (p, " as it is declared in the gcc header tree.h. */\n\n") ;
8368 keyc.useGccTree
8369 END
8370 END
8371 END doGccType ;
8372
8373
8374 (*
8375 isCDataType - return true if n is charStar or constCharStar.
8376 *)
8377
8378 PROCEDURE isCDataType (n: node) : BOOLEAN ;
8379 BEGIN
8380 RETURN (n # NIL) AND ((n = charStarN) OR (n = constCharStarN))
8381 END isCDataType ;
8382
8383
8384 (*
8385 isCDataTypes - return TRUE if n is CharStar or ConstCharStar.
8386 *)
8387
8388 PROCEDURE isCDataTypes (n: node) : BOOLEAN ;
8389 VAR
8390 scope: node ;
8391 BEGIN
8392 scope := getScope (n) ;
8393 RETURN (scope # NIL) AND (getSymName (scope) = makeKey ('CDataTypes')) AND
8394 ((getSymName (n) = makeKey ('CharStar')) OR
8395 (getSymName (n) = makeKey ('ConstCharStar')))
8396 END isCDataTypes ;
8397
8398
8399 (*
8400 doCDataTypes - if we are going to declare CharStar or ConstCharStar
8401 then generate a comment instead.
8402 *)
8403
8404 PROCEDURE doCDataTypes (p: pretty; n: node) ;
8405 BEGIN
8406 IF isCDataTypes (n)
8407 THEN
8408 IF getSymName (n) = makeKey ('CharStar')
8409 THEN
8410 outText (p, "/* Not going to declare ") ;
8411 doTypeNameC (p, n) ;
8412 outText (p, " as it is a C type. */\n\n") ;
8413 charStarN := n
8414 ELSIF getSymName (n) = makeKey ('ConstCharStar')
8415 THEN
8416 outText (p, "/* Not going to declare ") ;
8417 doTypeNameC (p, n) ;
8418 outText (p, " as it is a C type. */\n\n") ;
8419 constCharStarN := n
8420 END
8421 END
8422 END doCDataTypes ;
8423
8424
8425 (*
8426 doCDataTypesC - generate the C representation of the CDataTypes data types.
8427 *)
8428
8429 PROCEDURE doCDataTypesC (p: pretty; n: node) ;
8430 BEGIN
8431 IF n = charStarN
8432 THEN
8433 outText (p, "char *") ;
8434 setNeedSpace (p)
8435 ELSIF n = constCharStarN
8436 THEN
8437 outText (p, "const char *") ;
8438 setNeedSpace (p)
8439 END
8440 END doCDataTypesC ;
8441
8442
8443 (*
8444 doTypeOrPointer - only declare type or pointer n providing that
8445 the name is not location_t or tree and
8446 the --gccConfigSystem option is enabled.
8447 *)
8448
8449 PROCEDURE doTypeOrPointer (p: pretty; n: node) ;
8450 VAR
8451 m: node ;
8452 BEGIN
8453 IF isGccType (n)
8454 THEN
8455 doGccType (p, n)
8456 ELSIF isCDataTypes (n)
8457 THEN
8458 doCDataTypes (p, n)
8459 ELSE
8460 m := getType (n) ;
8461 outText (p, "typedef") ; setNeedSpace (p) ;
8462 doTypeC (p, m, m) ;
8463 IF isType (m)
8464 THEN
8465 setNeedSpace (p)
8466 END ;
8467 doTypeNameC (p, n) ;
8468 doTypeNameModifier (p, n) ;
8469 outText (p, ";\n\n")
8470 END
8471 END doTypeOrPointer ;
8472
8473
8474 (*
8475 doTypedef - generate a typedef for n provuiding it is not
8476 *)
8477
8478 PROCEDURE doTypedef (p: pretty; n: node) ;
8479 VAR
8480 m: node ;
8481 BEGIN
8482 IF isGccType (n)
8483 THEN
8484 doGccType (p, n)
8485 ELSIF isCDataTypes (n)
8486 THEN
8487 doCDataTypes (p, n)
8488 ELSE
8489 m := getType (n) ;
8490 outText (p, "typedef") ; setNeedSpace (p) ;
8491 doTypeC (p, m, m) ;
8492 IF isType (m)
8493 THEN
8494 setNeedSpace (p)
8495 END ;
8496 doTypeNameC (p, n) ;
8497 doTypeNameModifier (p, n) ;
8498 outText (p, ";\n\n")
8499 END
8500 END doTypedef ;
8501
8502
8503 (*
8504 doTypesC -
8505 *)
8506
8507 PROCEDURE doTypesC (n: node) ;
8508 VAR
8509 m: node ;
8510 BEGIN
8511 IF isType (n)
8512 THEN
8513 m := getType (n) ;
8514 IF isProcType (m)
8515 THEN
8516 doProcTypeC (doP, n, m)
8517 ELSIF isType (m) OR isPointer (m)
8518 THEN
8519 doTypeOrPointer (doP, n)
8520 ELSIF isEnumeration (m)
8521 THEN
8522 IF isDeclType (n)
8523 THEN
8524 outText (doP, "typedef") ; setNeedSpace (doP) ;
8525 doTypeC (doP, m, m) ;
8526 setNeedSpace (doP) ;
8527 doTypeNameC (doP, n) ;
8528 outText (doP, ";\n\n")
8529 END
8530 ELSE
8531 doTypedef (doP, n)
8532 END
8533 END
8534 END doTypesC ;
8535
8536
8537 (*
8538 doCompletePartialC -
8539 *)
8540
8541 PROCEDURE doCompletePartialC (n: node) ;
8542 VAR
8543 m: node ;
8544 BEGIN
8545 IF isType (n)
8546 THEN
8547 m := getType (n) ;
8548 IF isRecord (m)
8549 THEN
8550 doCompletePartialRecord (doP, n, m)
8551 ELSIF isArray (m)
8552 THEN
8553 doCompletePartialArray (doP, n, m)
8554 ELSIF isProcType (m)
8555 THEN
8556 doCompletePartialProcType (doP, n, m)
8557 END
8558 END
8559 END doCompletePartialC ;
8560
8561
8562 (*
8563 doCompletePartialRecord -
8564 *)
8565
8566 PROCEDURE doCompletePartialRecord (p: pretty; t, r: node) ;
8567 VAR
8568 i, h: CARDINAL ;
8569 f : node ;
8570 BEGIN
8571 assert (isRecord (r)) ;
8572 assert (isType (t)) ;
8573 outText (p, "struct") ; setNeedSpace (p) ;
8574 doFQNameC (p, t) ;
8575 outText (p, "_r") ; setNeedSpace (p) ;
8576 p := outKc (p, "{\n") ;
8577 i := LowIndice (r^.recordF.listOfSons) ;
8578 h := HighIndice (r^.recordF.listOfSons) ;
8579 WHILE i<=h DO
8580 f := GetIndice (r^.recordF.listOfSons, i) ;
8581 IF isRecordField (f)
8582 THEN
8583 IF NOT f^.recordfieldF.tag
8584 THEN
8585 doRecordFieldC (p, f) ;
8586 outText (p, ";\n")
8587 END
8588 ELSIF isVarient (f)
8589 THEN
8590 doVarientC (p, f) ;
8591 outText (p, ";\n")
8592 ELSIF isVarientField (f)
8593 THEN
8594 doVarientFieldC (p, f)
8595 END ;
8596 INC (i)
8597 END ;
8598 p := outKc (p, "};\n\n")
8599 END doCompletePartialRecord ;
8600
8601
8602 (*
8603 doCompletePartialArray -
8604 *)
8605
8606 PROCEDURE doCompletePartialArray (p: pretty; t, r: node) ;
8607 VAR
8608 type, s: node ;
8609 BEGIN
8610 assert (isArray (r)) ;
8611 type := r^.arrayF.type ;
8612 s := NIL ;
8613 outText (p, "struct") ; setNeedSpace (p) ;
8614 doFQNameC (p, t) ;
8615 outText (p, "_a {") ;
8616 setNeedSpace (p) ;
8617 doTypeC (p, type, s) ;
8618 setNeedSpace (p) ;
8619 outText (p, "array[") ;
8620 doSubrC (p, r^.arrayF.subr) ;
8621 outText (p, "];") ;
8622 setNeedSpace (p) ;
8623 outText (p, "};\n")
8624 END doCompletePartialArray ;
8625
8626
8627 (*
8628 lookupConst -
8629 *)
8630
8631 PROCEDURE lookupConst (type: node; n: Name) : node ;
8632 BEGIN
8633 RETURN makeLiteralInt (n)
8634 END lookupConst ;
8635
8636
8637 (*
8638 doMin -
8639 *)
8640
8641 PROCEDURE doMin (n: node) : node ;
8642 BEGIN
8643 IF n=booleanN
8644 THEN
8645 RETURN falseN
8646 ELSIF n=integerN
8647 THEN
8648 keyc.useIntMin ;
8649 RETURN lookupConst (integerN, makeKey ('INT_MIN'))
8650 ELSIF n=cardinalN
8651 THEN
8652 keyc.useUIntMin ;
8653 RETURN lookupConst (cardinalN, makeKey ('UINT_MIN'))
8654 ELSIF n=longintN
8655 THEN
8656 keyc.useLongMin ;
8657 RETURN lookupConst (longintN, makeKey ('LONG_MIN'))
8658 ELSIF n=longcardN
8659 THEN
8660 keyc.useULongMin ;
8661 RETURN lookupConst (longcardN, makeKey ('LONG_MIN'))
8662 ELSIF n=charN
8663 THEN
8664 keyc.useCharMin ;
8665 RETURN lookupConst (charN, makeKey ('CHAR_MIN'))
8666 ELSIF n=bitsetN
8667 THEN
8668 assert (isSubrange (bitnumN)) ;
8669 RETURN bitnumN^.subrangeF.low
8670 ELSIF n=locN
8671 THEN
8672 keyc.useUCharMin ;
8673 RETURN lookupConst (locN, makeKey ('UCHAR_MIN'))
8674 ELSIF n=byteN
8675 THEN
8676 keyc.useUCharMin ;
8677 RETURN lookupConst (byteN, makeKey ('UCHAR_MIN'))
8678 ELSIF n=wordN
8679 THEN
8680 keyc.useUIntMin ;
8681 RETURN lookupConst (wordN, makeKey ('UCHAR_MIN'))
8682 ELSIF n=addressN
8683 THEN
8684 RETURN lookupConst (addressN, makeKey ('((void *) 0)'))
8685 ELSE
8686 HALT (* finish the cacading elsif statement. *)
8687 END
8688 END doMin ;
8689
8690
8691 (*
8692 doMax -
8693 *)
8694
8695 PROCEDURE doMax (n: node) : node ;
8696 BEGIN
8697 IF n=booleanN
8698 THEN
8699 RETURN trueN
8700 ELSIF n=integerN
8701 THEN
8702 keyc.useIntMax ;
8703 RETURN lookupConst (integerN, makeKey ('INT_MAX'))
8704 ELSIF n=cardinalN
8705 THEN
8706 keyc.useUIntMax ;
8707 RETURN lookupConst (cardinalN, makeKey ('UINT_MAX'))
8708 ELSIF n=longintN
8709 THEN
8710 keyc.useLongMax ;
8711 RETURN lookupConst (longintN, makeKey ('LONG_MAX'))
8712 ELSIF n=longcardN
8713 THEN
8714 keyc.useULongMax ;
8715 RETURN lookupConst (longcardN, makeKey ('ULONG_MAX'))
8716 ELSIF n=charN
8717 THEN
8718 keyc.useCharMax ;
8719 RETURN lookupConst (charN, makeKey ('CHAR_MAX'))
8720 ELSIF n=bitsetN
8721 THEN
8722 assert (isSubrange (bitnumN)) ;
8723 RETURN bitnumN^.subrangeF.high
8724 ELSIF n=locN
8725 THEN
8726 keyc.useUCharMax ;
8727 RETURN lookupConst (locN, makeKey ('UCHAR_MAX'))
8728 ELSIF n=byteN
8729 THEN
8730 keyc.useUCharMax ;
8731 RETURN lookupConst (byteN, makeKey ('UCHAR_MAX'))
8732 ELSIF n=wordN
8733 THEN
8734 keyc.useUIntMax ;
8735 RETURN lookupConst (wordN, makeKey ('UINT_MAX'))
8736 ELSIF n=addressN
8737 THEN
8738 metaError1 ('trying to obtain MAX ({%1ad}) is illegal', n) ;
8739 RETURN NIL
8740 ELSE
8741 HALT (* finish the cacading elsif statement. *)
8742 END
8743 END doMax ;
8744
8745
8746 (*
8747 getMax -
8748 *)
8749
8750 PROCEDURE getMax (n: node) : node ;
8751 BEGIN
8752 n := skipType (n) ;
8753 IF isSubrange (n)
8754 THEN
8755 RETURN n^.subrangeF.high
8756 ELSIF isEnumeration (n)
8757 THEN
8758 RETURN n^.enumerationF.high
8759 ELSE
8760 assert (isOrdinal (n)) ;
8761 RETURN doMax (n)
8762 END
8763 END getMax ;
8764
8765
8766 (*
8767 getMin -
8768 *)
8769
8770 PROCEDURE getMin (n: node) : node ;
8771 BEGIN
8772 n := skipType (n) ;
8773 IF isSubrange (n)
8774 THEN
8775 RETURN n^.subrangeF.low
8776 ELSIF isEnumeration (n)
8777 THEN
8778 RETURN n^.enumerationF.low
8779 ELSE
8780 assert (isOrdinal (n)) ;
8781 RETURN doMin (n)
8782 END
8783 END getMin ;
8784
8785
8786 (*
8787 doSubtractC -
8788 *)
8789
8790 PROCEDURE doSubtractC (p: pretty; s: node) ;
8791 BEGIN
8792 IF NOT isZero (s)
8793 THEN
8794 outText (p, "-") ;
8795 doExprC (p, s)
8796 END
8797 END doSubtractC ;
8798
8799
8800 (*
8801 doSubrC -
8802 *)
8803
8804 PROCEDURE doSubrC (p: pretty; s: node) ;
8805 VAR
8806 low, high: node ;
8807 BEGIN
8808 s := skipType (s) ;
8809 IF isOrdinal (s)
8810 THEN
8811 low := getMin (s) ;
8812 high := getMax (s) ;
8813 doExprC (p, high) ;
8814 doSubtractC (p, low) ;
8815 outText (p, "+1")
8816 ELSIF isEnumeration (s)
8817 THEN
8818 low := getMin (s) ;
8819 high := getMax (s) ;
8820 doExprC (p, high) ;
8821 doSubtractC (p, low) ;
8822 outText (p, "+1")
8823 ELSE
8824 assert (isSubrange (s)) ;
8825 IF (s^.subrangeF.high = NIL) OR (s^.subrangeF.low = NIL)
8826 THEN
8827 doSubrC (p, getType (s))
8828 ELSE
8829 doExprC (p, s^.subrangeF.high) ;
8830 doSubtractC (p, s^.subrangeF.low) ;
8831 outText (p, "+1")
8832 END
8833 END
8834 END doSubrC ;
8835
8836
8837 (*
8838 doCompletePartialProcType -
8839 *)
8840
8841 PROCEDURE doCompletePartialProcType (p: pretty; t, n: node) ;
8842 BEGIN
8843 IF isDeclType (t) AND isDeclType (n)
8844 THEN
8845 outputCompletePartialProcType (p, t, n)
8846 END
8847 END doCompletePartialProcType ;
8848
8849
8850 (*
8851 outputCompletePartialProcType -
8852 *)
8853
8854 PROCEDURE outputCompletePartialProcType (p: pretty; t, n: node) ;
8855 VAR
8856 i, h: CARDINAL ;
8857 v, u: node ;
8858 BEGIN
8859 assert (isProcType (n)) ;
8860 u := NIL ;
8861 outText (p, "typedef") ; setNeedSpace (p) ;
8862 doTypeC (p, n^.proctypeF.returnType, u) ;
8863 doOpaqueModifier (p, n) ;
8864 setNeedSpace (p) ;
8865 outText (p, "(*") ;
8866 doFQNameC (p, t) ;
8867 outText (p, "_t) (") ;
8868 i := LowIndice (n^.proctypeF.parameters) ;
8869 h := HighIndice (n^.proctypeF.parameters) ;
8870 WHILE i <= h DO
8871 v := GetIndice (n^.proctypeF.parameters, i) ;
8872 doParameterC (p, v) ;
8873 noSpace (p) ;
8874 IF i < h
8875 THEN
8876 outText (p, ",") ; setNeedSpace (p)
8877 END ;
8878 INC (i)
8879 END ;
8880 IF h=0
8881 THEN
8882 outText (p, "void")
8883 END ;
8884 outText (p, ");\n") ;
8885 IF isDefForCNode (n)
8886 THEN
8887 (* emit a C named type which differs from the m2 proctype. *)
8888 outText (p, "typedef") ; setNeedSpace (p) ;
8889 doFQNameC (p, t) ;
8890 outText (p, "_t") ; setNeedSpace (p) ;
8891 doFQNameC (p, t) ;
8892 outText (p, "_C;\n\n")
8893 END ;
8894 outText (p, "struct") ; setNeedSpace (p) ;
8895 doFQNameC (p, t) ;
8896 outText (p, "_p {") ; setNeedSpace (p) ;
8897 doFQNameC (p, t) ;
8898 outText (p, "_t proc; };\n\n")
8899 END outputCompletePartialProcType ;
8900
8901
8902 (*
8903 isBase -
8904 *)
8905
8906 PROCEDURE isBase (n: node) : BOOLEAN ;
8907 BEGIN
8908 CASE n^.kind OF
8909
8910 char,
8911 cardinal,
8912 longcard,
8913 shortcard,
8914 integer,
8915 longint,
8916 shortint,
8917 complex,
8918 longcomplex,
8919 shortcomplex,
8920 real,
8921 longreal,
8922 shortreal,
8923 bitset,
8924 boolean,
8925 proc : RETURN TRUE
8926
8927 ELSE
8928 RETURN FALSE
8929 END
8930 END isBase ;
8931
8932
8933 (*
8934 doBoolC -
8935 *)
8936
8937 PROCEDURE doBoolC (p: pretty) ;
8938 BEGIN
8939 IF useBool ()
8940 THEN
8941 outText (p, 'bool')
8942 ELSE
8943 outText (p, 'unsigned int')
8944 END
8945 END doBoolC ;
8946
8947
8948 (*
8949 doBaseC -
8950 *)
8951
8952 PROCEDURE doBaseC (p: pretty; n: node) ;
8953 BEGIN
8954 CASE n^.kind OF
8955
8956 char : outText (p, 'char') |
8957 cardinal : outText (p, 'unsigned int') |
8958 longcard : outText (p, 'long unsigned int') |
8959 shortcard : outText (p, 'short unsigned int') |
8960 integer : outText (p, 'int') |
8961 longint : outText (p, 'long int') |
8962 shortint : outText (p, 'short int') |
8963 complex : outText (p, 'double complex') |
8964 longcomplex : outText (p, 'long double complex') |
8965 shortcomplex: outText (p, 'float complex') |
8966 real : outTextS (p, getCRealType ()) |
8967 longreal : outTextS (p, getCLongRealType ()) |
8968 shortreal : outTextS (p, getCShortRealType ()) |
8969 bitset : outText (p, 'unsigned int') |
8970 boolean : doBoolC (p) |
8971 proc : outText (p, 'PROC')
8972
8973 END ;
8974 setNeedSpace (p)
8975 END doBaseC ;
8976
8977
8978 (*
8979 isSystem -
8980 *)
8981
8982 PROCEDURE isSystem (n: node) : BOOLEAN ;
8983 BEGIN
8984 CASE n^.kind OF
8985
8986 address,
8987 loc,
8988 byte,
8989 word,
8990 csizet,
8991 cssizet,
8992 cofft,
8993 cardinal64: RETURN TRUE
8994
8995 ELSE
8996 RETURN FALSE
8997 END
8998 END isSystem ;
8999
9000
9001 (*
9002 doSystemC -
9003 *)
9004
9005 PROCEDURE doSystemC (p: pretty; n: node) ;
9006 BEGIN
9007 CASE n^.kind OF
9008
9009 address: outText (p, 'void *') |
9010 loc : outText (p, 'unsigned char') ; setNeedSpace (p) |
9011 byte : outText (p, 'unsigned char') ; setNeedSpace (p) |
9012 word : outText (p, 'unsigned int') ; setNeedSpace (p) |
9013 csizet : outText (p, 'size_t') ; setNeedSpace (p) ; keyc.useSize_t |
9014 cssizet : outText (p, 'ssize_t') ; setNeedSpace (p) ; keyc.useSSize_t |
9015 cofft : outText (p, 'off_t') ; setNeedSpace (p) |
9016 cardinal64: outText (p, 'uint64_t') ; setNeedSpace (p)
9017
9018 END
9019 END doSystemC ;
9020
9021
9022 (*
9023 doArrayC -
9024 *)
9025
9026 PROCEDURE doArrayC (p: pretty; n: node) ;
9027 VAR
9028 t, s, u: node ;
9029 BEGIN
9030 assert (isArray (n)) ;
9031 t := n^.arrayF.type ;
9032 s := n^.arrayF.subr ;
9033 u := NIL ;
9034 IF s=NIL
9035 THEN
9036 doTypeC (p, t, u) ;
9037 setNeedSpace (p) ;
9038 outText (p, "*")
9039 ELSE
9040 outText (p, "struct") ;
9041 setNeedSpace (p) ;
9042 outText (p, "{") ;
9043 setNeedSpace (p) ;
9044 doTypeC (p, t, u) ;
9045 setNeedSpace (p) ;
9046 outText (p, "array[") ;
9047 IF isZero (getMin (s))
9048 THEN
9049 doExprC (p, getMax (s))
9050 ELSE
9051 doExprC (p, getMax (s)) ;
9052 doSubtractC (p, getMin (s))
9053 END ;
9054 outText (p, "];") ;
9055 setNeedSpace (p) ;
9056 outText (p, "}") ;
9057 setNeedSpace (p)
9058 END
9059 END doArrayC ;
9060
9061
9062 (*
9063 doPointerC -
9064 *)
9065
9066 PROCEDURE doPointerC (p: pretty; n: node; VAR m: node) ;
9067 VAR
9068 t, s: node ;
9069 BEGIN
9070 t := n^.pointerF.type ;
9071 s := NIL ;
9072 doTypeC (p, t, s) ;
9073 setNeedSpace (p) ;
9074 outText (p, "*")
9075 END doPointerC ;
9076
9077
9078 (*
9079 doRecordFieldC -
9080 *)
9081
9082 PROCEDURE doRecordFieldC (p: pretty; f: node) ;
9083 VAR
9084 m: node ;
9085 BEGIN
9086 m := NIL ;
9087 setNeedSpace (p) ;
9088 doTypeC (p, f^.recordfieldF.type, m) ;
9089 IF isType (f^.recordfieldF.type) AND isDeclInImp (f^.recordfieldF.type)
9090 THEN
9091 outText (p, '__opaque')
9092 END ;
9093 setNeedSpace (p) ;
9094 doDNameC (p, f, FALSE)
9095 END doRecordFieldC ;
9096
9097
9098 (*
9099 doVarientFieldC -
9100 *)
9101
9102 PROCEDURE doVarientFieldC (p: pretty; n: node) ;
9103 VAR
9104 i, t: CARDINAL ;
9105 q : node ;
9106 BEGIN
9107 assert (isVarientField (n)) ;
9108 IF NOT n^.varientfieldF.simple
9109 THEN
9110 outText (p, "struct") ; setNeedSpace (p) ;
9111 p := outKc (p, "{\n")
9112 END ;
9113 i := LowIndice (n^.varientfieldF.listOfSons) ;
9114 t := HighIndice (n^.varientfieldF.listOfSons) ;
9115 WHILE i<=t DO
9116 q := GetIndice (n^.varientfieldF.listOfSons, i) ;
9117 IF isRecordField (q)
9118 THEN
9119 IF NOT q^.recordfieldF.tag
9120 THEN
9121 doRecordFieldC (p, q) ;
9122 outText (p, ";\n")
9123 END
9124 ELSIF isVarient (q)
9125 THEN
9126 doVarientC (p, q) ;
9127 outText (p, ";\n")
9128 ELSE
9129 HALT
9130 END ;
9131 INC (i)
9132 END ;
9133 IF NOT n^.varientfieldF.simple
9134 THEN
9135 p := outKc (p, "};\n")
9136 END
9137 END doVarientFieldC ;
9138
9139
9140 (*
9141 doVarientC -
9142 *)
9143
9144 PROCEDURE doVarientC (p: pretty; n: node) ;
9145 VAR
9146 i, t: CARDINAL ;
9147 q : node ;
9148 BEGIN
9149 assert (isVarient (n)) ;
9150 IF n^.varientF.tag # NIL
9151 THEN
9152 IF isRecordField (n^.varientF.tag)
9153 THEN
9154 doRecordFieldC (p, n^.varientF.tag) ;
9155 outText (p, "; /* case tag */\n")
9156 ELSIF isVarientField (n^.varientF.tag)
9157 THEN
9158 HALT
9159 (* doVarientFieldC (p, n^.varientF.tag) *)
9160 ELSE
9161 HALT
9162 END
9163 END ;
9164 outText (p, "union") ;
9165 setNeedSpace (p) ;
9166 p := outKc (p, "{\n") ;
9167 i := LowIndice (n^.varientF.listOfSons) ;
9168 t := HighIndice (n^.varientF.listOfSons) ;
9169 WHILE i<=t DO
9170 q := GetIndice (n^.varientF.listOfSons, i) ;
9171 IF isRecordField (q)
9172 THEN
9173 IF NOT q^.recordfieldF.tag
9174 THEN
9175 doRecordFieldC (p, q) ;
9176 outText (p, ";\n")
9177 END
9178 ELSIF isVarientField (q)
9179 THEN
9180 doVarientFieldC (p, q)
9181 ELSE
9182 HALT
9183 END ;
9184 INC (i)
9185 END ;
9186 p := outKc (p, "}")
9187 END doVarientC ;
9188
9189
9190 (*
9191 doRecordC -
9192 *)
9193
9194 PROCEDURE doRecordC (p: pretty; n: node; VAR m: node) ;
9195 VAR
9196 i, h: CARDINAL ;
9197 f : node ;
9198 BEGIN
9199 assert (isRecord (n)) ;
9200 outText (p, "struct") ;
9201 setNeedSpace (p) ;
9202 p := outKc (p, "{") ;
9203 i := LowIndice (n^.recordF.listOfSons) ;
9204 h := HighIndice (n^.recordF.listOfSons) ;
9205 setindent (p, getcurpos (p) + indentation) ;
9206 outText (p, "\n") ;
9207 WHILE i<=h DO
9208 f := GetIndice (n^.recordF.listOfSons, i) ;
9209 IF isRecordField (f)
9210 THEN
9211 IF NOT f^.recordfieldF.tag
9212 THEN
9213 doRecordFieldC (p, f) ;
9214 outText (p, ";\n")
9215 END
9216 ELSIF isVarient (f)
9217 THEN
9218 doVarientC (p, f) ;
9219 outText (p, ";\n")
9220 ELSIF isVarientField (f)
9221 THEN
9222 doVarientFieldC (p, f)
9223 END ;
9224 INC (i)
9225 END ;
9226 p := outKc (p, "}") ;
9227 setNeedSpace (p)
9228 END doRecordC ;
9229
9230
9231 (*
9232 isBitset -
9233 *)
9234
9235 PROCEDURE isBitset (n: node) : BOOLEAN ;
9236 BEGIN
9237 RETURN n = bitsetN
9238 END isBitset ;
9239
9240
9241 (*
9242 isNegative - returns TRUE if expression, n, is negative.
9243 *)
9244
9245 PROCEDURE isNegative (n: node) : BOOLEAN ;
9246 BEGIN
9247 (* --fixme-- needs to be completed. *)
9248 RETURN FALSE
9249 END isNegative ;
9250
9251
9252 (*
9253 doSubrangeC -
9254 *)
9255
9256 PROCEDURE doSubrangeC (p: pretty; n: node) ;
9257 BEGIN
9258 assert (isSubrange (n)) ;
9259 IF isNegative (n^.subrangeF.low)
9260 THEN
9261 outText (p, "int") ; setNeedSpace (p)
9262 ELSE
9263 outText (p, "unsigned int") ; setNeedSpace (p)
9264 END
9265 END doSubrangeC ;
9266
9267
9268 (*
9269 doSetC - generates a C type which holds the set.
9270 Currently we only support sets of size WORD.
9271 *)
9272
9273 PROCEDURE doSetC (p: pretty; n: node) ;
9274 BEGIN
9275 assert (isSet (n)) ;
9276 outText (p, "unsigned int") ; setNeedSpace (p)
9277 END doSetC ;
9278
9279
9280 (*
9281 doTypeC -
9282 *)
9283
9284 PROCEDURE doTypeC (p: pretty; n: node; VAR m: node) ;
9285 BEGIN
9286 IF n=NIL
9287 THEN
9288 outText (p, "void")
9289 ELSIF isCDataTypes (n)
9290 THEN
9291 doCDataTypesC (p, n)
9292 ELSIF isBase (n)
9293 THEN
9294 doBaseC (p, n)
9295 ELSIF isSystem (n)
9296 THEN
9297 doSystemC (p, n)
9298 ELSIF isEnumeration (n)
9299 THEN
9300 doEnumerationC (p, n)
9301 ELSIF isType (n)
9302 THEN
9303 doFQNameC (p, n)
9304 ELSIF isProcType (n)
9305 THEN
9306 doProcTypeC (p, n, m)
9307 ELSIF isArray (n)
9308 THEN
9309 doArrayC (p, n)
9310 ELSIF isRecord (n)
9311 THEN
9312 doRecordC (p, n, m)
9313 ELSIF isPointer (n)
9314 THEN
9315 doPointerC (p, n, m)
9316 ELSIF isSubrange (n)
9317 THEN
9318 doSubrangeC (p, n)
9319 ELSIF isSet (n)
9320 THEN
9321 doSetC (p, n)
9322 ELSIF isCDataTypes (n)
9323 THEN
9324 doCDataTypesC (p, n)
9325 ELSE
9326 metaError1 ('expecting a type symbol rather than a {%1DMd} {%1DMa}', n) ;
9327 flushErrors ;
9328 errorAbort0 ('terminating compilation')
9329 END
9330 END doTypeC ;
9331
9332
9333 (*
9334 doArrayNameC - it displays the array declaration (it might be an unbounded).
9335 *)
9336
9337 PROCEDURE doArrayNameC (p: pretty; n: node) ;
9338 BEGIN
9339 doTypeNameC (p, getType (n)) ; setNeedSpace (p) ; outText (p, "*")
9340 END doArrayNameC ;
9341
9342
9343 (*
9344 doRecordNameC - emit the C/C++ record name <name of n>"_r".
9345 *)
9346
9347 PROCEDURE doRecordNameC (p: pretty; n: node) ;
9348 VAR
9349 s: String ;
9350 BEGIN
9351 s := getFQstring (n) ;
9352 s := ConCat (s, Mark (InitString ("_r"))) ;
9353 outTextS (p, s) ;
9354 s := KillString (s)
9355 END doRecordNameC ;
9356
9357
9358 (*
9359 doPointerNameC - emit the C/C++ pointer type <name of n>*.
9360 *)
9361
9362 PROCEDURE doPointerNameC (p: pretty; n: node) ;
9363 BEGIN
9364 doTypeNameC (p, getType (n)) ; setNeedSpace (p) ; outText (p, "*")
9365 END doPointerNameC ;
9366
9367
9368 (*
9369 doTypeNameC -
9370 *)
9371
9372 PROCEDURE doTypeNameC (p: pretty; n: node) ;
9373 VAR
9374 t: String ;
9375 BEGIN
9376 IF n=NIL
9377 THEN
9378 outText (p, "void") ;
9379 setNeedSpace (p)
9380 ELSIF n = charStarN
9381 THEN
9382 outText (p, "char *") ;
9383 setNeedSpace (p)
9384 ELSIF n = constCharStarN
9385 THEN
9386 outText (p, "const char *") ;
9387 setNeedSpace (p)
9388 ELSIF isBase (n)
9389 THEN
9390 doBaseC (p, n)
9391 ELSIF isSystem (n)
9392 THEN
9393 doSystemC (p, n)
9394 ELSIF isEnumeration (n)
9395 THEN
9396 print (p, "is enumeration type name required\n")
9397 ELSIF isType (n)
9398 THEN
9399 doFQNameC (p, n) ;
9400 ELSIF isProcType (n)
9401 THEN
9402 doFQNameC (p, n) ;
9403 outText (p, "_t")
9404 ELSIF isArray (n)
9405 THEN
9406 doArrayNameC (p, n)
9407 ELSIF isRecord (n)
9408 THEN
9409 doRecordNameC (p, n)
9410 ELSIF isPointer (n)
9411 THEN
9412 doPointerNameC (p, n)
9413 ELSIF isSubrange (n)
9414 THEN
9415 doSubrangeC (p, n)
9416 ELSE
9417 print (p, "is type unknown required\n")
9418 END
9419 END doTypeNameC ;
9420
9421
9422 (*
9423 isExternal - returns TRUE if symbol, n, was declared in another module.
9424 *)
9425
9426 PROCEDURE isExternal (n: node) : BOOLEAN ;
9427 VAR
9428 s: node ;
9429 BEGIN
9430 s := getScope (n) ;
9431 RETURN (s # NIL) AND isDef (s) AND
9432 ((isImp (getMainModule ()) AND (s # lookupDef (getSymName (getMainModule ())))) OR
9433 isModule (getMainModule ()))
9434 END isExternal ;
9435
9436
9437 (*
9438 doOpaqueModifier - adds postfix __opaque providing n uses an opaque type which is
9439 not represented by ( void * ). n is a non type node which might
9440 be using an opaque type. For example a var or param node.
9441 *)
9442
9443 PROCEDURE doOpaqueModifier (p: pretty; n: node) ;
9444 BEGIN
9445 assert (NOT isType (n)) ;
9446 IF isImp (getCurrentModule ()) AND nodeUsesOpaque (n) AND (NOT getNodeOpaqueVoidStar (n))
9447 THEN
9448 outText (doP, '__opaque')
9449 END
9450 END doOpaqueModifier ;
9451
9452
9453 (*
9454 doDeclareVarC -
9455 *)
9456
9457 PROCEDURE doDeclareVarC (n: node) ;
9458 VAR
9459 type,
9460 s : node ;
9461 BEGIN
9462 s := NIL ;
9463 type := getType (n) ;
9464 doTypeC (doP, type, s) ;
9465 doOpaqueModifier (doP, n) ;
9466 setNeedSpace (doP) ;
9467 doFQDNameC (doP, n, FALSE) ;
9468 print (doP, ";\n")
9469 END doDeclareVarC ;
9470
9471
9472 (*
9473 doVarC - output a variable declaration. Note that we do not generate
9474 a declaration if we are translating the implementation module
9475 and a variable is exported as the variable will be in the .h
9476 file to avoid all -Wodr issues.
9477 *)
9478
9479 PROCEDURE doVarC (n: node) ;
9480 BEGIN
9481 IF isDef (getMainModule ())
9482 THEN
9483 print (doP, "EXTERN") ; setNeedSpace (doP) ;
9484 doDeclareVarC (n)
9485 ELSIF (NOT isExported (n)) AND (NOT isLocal (n))
9486 THEN
9487 print (doP, "static") ; setNeedSpace (doP) ;
9488 doDeclareVarC (n)
9489 ELSIF getExtendedOpaque ()
9490 THEN
9491 (* --fixme-- need to revisit extended opaque. *)
9492 IF isExternal (n)
9493 THEN
9494 (* different module declared this variable, therefore it is extern. *)
9495 print (doP, "extern") ; setNeedSpace (doP)
9496 END ;
9497 doDeclareVarC (n)
9498 ELSIF isLocal (n)
9499 THEN
9500 doDeclareVarC (n)
9501 END
9502 END doVarC ;
9503
9504
9505 (*
9506 doExternCP -
9507 *)
9508
9509 PROCEDURE doExternCP (p: pretty) ;
9510 BEGIN
9511 IF lang = ansiCP
9512 THEN
9513 outText (p, 'extern "C"') ; setNeedSpace (p)
9514 END
9515 END doExternCP ;
9516
9517
9518 (*
9519 doProcedureCommentText -
9520 *)
9521
9522 PROCEDURE doProcedureCommentText (p: pretty; s: String) ;
9523 BEGIN
9524 (* remove \n from the start of the comment. *)
9525 WHILE (DynamicStrings.Length (s) > 0) AND (DynamicStrings.char (s, 0) = lf) DO
9526 s := DynamicStrings.Slice (s, 1, 0)
9527 END ;
9528 outTextS (p, s)
9529 END doProcedureCommentText ;
9530
9531
9532 (*
9533 doProcedureComment -
9534 *)
9535
9536 PROCEDURE doProcedureComment (p: pretty; s: String) ;
9537 BEGIN
9538 IF s # NIL
9539 THEN
9540 outText (p, '\n/*\n') ;
9541 doProcedureCommentText (p, s) ;
9542 outText (p, '*/\n\n')
9543 END
9544 END doProcedureComment ;
9545
9546
9547 (*
9548 doProcedureHeadingC -
9549 *)
9550
9551 PROCEDURE doProcedureHeadingC (n: node; prototype: BOOLEAN) ;
9552 VAR
9553 s : String ;
9554 i, h: CARDINAL ;
9555 p, q: node ;
9556 BEGIN
9557 assert (isProcedure (n)) ;
9558 s := getFQstring (n) ;
9559 IF EqualArray (s, 'M2Quads_BuildAssignment')
9560 THEN
9561 localstop
9562 END ;
9563 s := KillString (s) ;
9564 noSpace (doP) ;
9565 IF isDef (getMainModule ())
9566 THEN
9567 doProcedureComment (doP, getContent (n^.procedureF.defComment)) ;
9568 outText (doP, "EXTERN") ; setNeedSpace (doP)
9569 ELSIF isExported (n)
9570 THEN
9571 doProcedureComment (doP, getContent (n^.procedureF.modComment)) ;
9572 doExternCP (doP)
9573 ELSE
9574 doProcedureComment (doP, getContent (n^.procedureF.modComment)) ;
9575 outText (doP, "static") ; setNeedSpace (doP)
9576 END ;
9577 q := NIL ;
9578 doTypeC (doP, n^.procedureF.returnType, q) ;
9579 (*
9580 IF NOT isExported (n)
9581 THEN
9582 doTypeNameModifier (doP, n^.procedureF.returnType)
9583 END ;
9584 *)
9585 doOpaqueModifier (doP, n) ;
9586 setNeedSpace (doP) ;
9587 doFQDNameC (doP, n, FALSE) ;
9588 setNeedSpace (doP) ;
9589 outText (doP, "(") ;
9590 i := LowIndice (n^.procedureF.parameters) ;
9591 h := HighIndice (n^.procedureF.parameters) ;
9592 WHILE i <= h DO
9593 p := GetIndice (n^.procedureF.parameters, i) ;
9594 doParameterC (doP, p) ;
9595 noSpace (doP) ;
9596 IF i < h
9597 THEN
9598 print (doP, ",") ; setNeedSpace (doP)
9599 END ;
9600 INC (i)
9601 END ;
9602 IF h=0
9603 THEN
9604 outText (doP, "void")
9605 END ;
9606 print (doP, ")") ;
9607 IF n^.procedureF.noreturn AND prototype AND (NOT getSuppressNoReturn ())
9608 THEN
9609 setNeedSpace (doP) ;
9610 outText (doP, "__attribute__ ((noreturn))")
9611 END
9612 END doProcedureHeadingC ;
9613
9614
9615 (*
9616 checkDeclareUnboundedParamCopyC -
9617 *)
9618
9619 PROCEDURE checkDeclareUnboundedParamCopyC (p: pretty; n: node) : BOOLEAN ;
9620 VAR
9621 t : node ;
9622 i, c: CARDINAL ;
9623 l : wlist ;
9624 seen: BOOLEAN ;
9625 BEGIN
9626 seen := FALSE ;
9627 t := getType (n) ;
9628 l := n^.paramF.namelist^.identlistF.names ;
9629 IF isArray (t) AND isUnbounded (t) AND (l#NIL)
9630 THEN
9631 t := getType (t) ;
9632 c := wlists.noOfItemsInList (l) ;
9633 i := 1 ;
9634 WHILE i <= c DO
9635 doTypeNameC (p, t) ;
9636 setNeedSpace (p) ;
9637 doNamesC (p, wlists.getItemFromList (l, i)) ;
9638 outText (p, '[_');
9639 doNamesC (p, wlists.getItemFromList (l, i)) ;
9640 outText (p, '_high+1];\n');
9641 seen := TRUE ;
9642 INC (i)
9643 END
9644 END ;
9645 RETURN seen
9646 END checkDeclareUnboundedParamCopyC ;
9647
9648
9649 (*
9650 checkUnboundedParamCopyC -
9651 *)
9652
9653 PROCEDURE checkUnboundedParamCopyC (p: pretty; n: node) ;
9654 VAR
9655 t, s: node ;
9656 i, c: CARDINAL ;
9657 l : wlist ;
9658 BEGIN
9659 t := getType (n) ;
9660 l := n^.paramF.namelist^.identlistF.names ;
9661 IF isArray (t) AND isUnbounded (t) AND (l#NIL)
9662 THEN
9663 c := wlists.noOfItemsInList (l) ;
9664 i := 1 ;
9665 t := getType (t) ;
9666 s := skipType (t) ;
9667 WHILE i <= c DO
9668 keyc.useMemcpy ;
9669 outText (p, 'memcpy (') ;
9670 doNamesC (p, wlists.getItemFromList (l, i)) ;
9671 outText (p, ',') ;
9672 setNeedSpace (p) ;
9673 doNamesC (p, wlists.getItemFromList (l, i)) ;
9674 outText (p, '_, ') ;
9675 IF (s = charN) OR (s = byteN) OR (s = locN)
9676 THEN
9677 outText (p, '_') ;
9678 doNamesC (p, wlists.getItemFromList (l, i)) ;
9679 outText (p, '_high+1);\n')
9680 ELSE
9681 outText (p, '(_') ;
9682 doNamesC (p, wlists.getItemFromList (l, i)) ;
9683 outText (p, '_high+1)') ;
9684 setNeedSpace (p) ;
9685 doMultiplyBySize (p, t) ;
9686 outText (p, ');\n')
9687 END ;
9688 INC (i)
9689 END
9690 END
9691 END checkUnboundedParamCopyC ;
9692
9693
9694 (*
9695 doUnboundedParamCopyC -
9696 *)
9697
9698 PROCEDURE doUnboundedParamCopyC (p: pretty; n: node) ;
9699 VAR
9700 i, h: CARDINAL ;
9701 q : node ;
9702 seen: BOOLEAN ;
9703 BEGIN
9704 assert (isProcedure (n)) ;
9705 i := LowIndice (n^.procedureF.parameters) ;
9706 h := HighIndice (n^.procedureF.parameters) ;
9707 seen := FALSE ;
9708 WHILE i <= h DO
9709 q := GetIndice (n^.procedureF.parameters, i) ;
9710 IF isParam (q)
9711 THEN
9712 seen := checkDeclareUnboundedParamCopyC (p, q) OR seen
9713 END ;
9714 INC (i)
9715 END ;
9716 IF seen
9717 THEN
9718 outText (p, "\n") ;
9719 outText (p, "/* make a local copy of each unbounded array. */\n") ;
9720 i := LowIndice (n^.procedureF.parameters) ;
9721 WHILE i <= h DO
9722 q := GetIndice (n^.procedureF.parameters, i) ;
9723 IF isParam (q)
9724 THEN
9725 checkUnboundedParamCopyC (p, q)
9726 END ;
9727 INC (i)
9728 END
9729 END
9730 END doUnboundedParamCopyC ;
9731
9732
9733 (*
9734 doPrototypeC -
9735 *)
9736
9737 PROCEDURE doPrototypeC (n: node) ;
9738 BEGIN
9739 IF NOT isExported (n)
9740 THEN
9741 keyc.enterScope (n) ;
9742 doProcedureHeadingC (n, TRUE) ;
9743 print (doP, ";\n") ;
9744 keyc.leaveScope (n)
9745 END
9746 END doPrototypeC ;
9747
9748
9749 (*
9750 addTodo - adds, n, to the todo list.
9751 *)
9752
9753 PROCEDURE addTodo (n: node) ;
9754 BEGIN
9755 IF (n#NIL) AND
9756 (NOT alists.isItemInList (globalGroup^.partialQ, n)) AND
9757 (NOT alists.isItemInList (globalGroup^.doneQ, n))
9758 THEN
9759 assert (NOT isVarient (n)) ;
9760 assert (NOT isVarientField (n)) ;
9761 assert (NOT isDef (n)) ;
9762 alists.includeItemIntoList (globalGroup^.todoQ, n)
9763 END
9764 END addTodo ;
9765
9766
9767 (*
9768 addVariablesTodo -
9769 *)
9770
9771 PROCEDURE addVariablesTodo (n: node) ;
9772 BEGIN
9773 IF isVar (n)
9774 THEN
9775 IF n^.varF.isParameter OR n^.varF.isVarParameter
9776 THEN
9777 addDone (n) ;
9778 addTodo (getType (n))
9779 ELSE
9780 addTodo (n)
9781 END
9782 END
9783 END addVariablesTodo ;
9784
9785
9786 (*
9787 addTypesTodo -
9788 *)
9789
9790 PROCEDURE addTypesTodo (n: node) ;
9791 BEGIN
9792 IF isUnbounded (n)
9793 THEN
9794 addDone (n)
9795 ELSE
9796 addTodo (n)
9797 END
9798 END addTypesTodo ;
9799
9800
9801 (*
9802 tempName -
9803 *)
9804
9805 PROCEDURE tempName () : String ;
9806 BEGIN
9807 INC (tempCount) ;
9808 RETURN Sprintf1 (InitString ("_T%d"), tempCount) ;
9809 END tempName ;
9810
9811
9812 (*
9813 makeIntermediateType -
9814 *)
9815
9816 PROCEDURE makeIntermediateType (s: String; p: node) : node ;
9817 VAR
9818 n: Name ;
9819 o: node ;
9820 BEGIN
9821 n := makekey (DynamicStrings.string (s)) ;
9822 enterScope (getScope (p)) ;
9823 o := p ;
9824 p := makeType (makekey (DynamicStrings.string (s))) ;
9825 putType (p, o) ;
9826 putTypeInternal (p) ;
9827 leaveScope ;
9828 RETURN p
9829 END makeIntermediateType ;
9830
9831
9832 (*
9833 simplifyType -
9834 *)
9835
9836 PROCEDURE simplifyType (l: alist; VAR p: node) ;
9837 VAR
9838 s: String ;
9839 BEGIN
9840 IF (p#NIL) AND (isRecord (p) OR isArray (p) OR isProcType (p)) AND (NOT isUnbounded (p))
9841 THEN
9842 s := tempName () ;
9843 p := makeIntermediateType (s, p) ;
9844 s := KillString (s) ;
9845 simplified := FALSE
9846 END ;
9847 simplifyNode (l, p)
9848 END simplifyType ;
9849
9850
9851 (*
9852 simplifyVar -
9853 *)
9854
9855 PROCEDURE simplifyVar (l: alist; n: node) ;
9856 VAR
9857 i, t: CARDINAL ;
9858 v,
9859 d, o: node ;
9860 BEGIN
9861 assert (isVar (n)) ;
9862 o := n^.varF.type ;
9863 simplifyType (l, n^.varF.type) ;
9864 IF o # n^.varF.type
9865 THEN
9866 (* simplification has occurred, make sure that all other variables of this type
9867 use the new type. *)
9868 d := n^.varF.decl ;
9869 assert (isVarDecl (d)) ;
9870 t := wlists.noOfItemsInList (d^.vardeclF.names) ;
9871 i := 1 ;
9872 WHILE i<=t DO
9873 v := lookupInScope (n^.varF.scope, wlists.getItemFromList (d^.vardeclF.names, i)) ;
9874 assert (isVar (v)) ;
9875 v^.varF.type := n^.varF.type ;
9876 INC (i)
9877 END
9878 END
9879 END simplifyVar ;
9880
9881
9882 (*
9883 simplifyRecord -
9884 *)
9885
9886 PROCEDURE simplifyRecord (l: alist; n: node) ;
9887 VAR
9888 i, t: CARDINAL ;
9889 q : node ;
9890 BEGIN
9891 i := LowIndice (n^.recordF.listOfSons) ;
9892 t := HighIndice (n^.recordF.listOfSons) ;
9893 WHILE i<=t DO
9894 q := GetIndice (n^.recordF.listOfSons, i) ;
9895 simplifyNode (l, q) ;
9896 INC (i)
9897 END
9898 END simplifyRecord ;
9899
9900
9901 (*
9902 simplifyVarient -
9903 *)
9904
9905 PROCEDURE simplifyVarient (l: alist; n: node) ;
9906 VAR
9907 i, t: CARDINAL ;
9908 q : node ;
9909 BEGIN
9910 simplifyNode (l, n^.varientF.tag) ;
9911 i := LowIndice (n^.varientF.listOfSons) ;
9912 t := HighIndice (n^.varientF.listOfSons) ;
9913 WHILE i<=t DO
9914 q := GetIndice (n^.varientF.listOfSons, i) ;
9915 simplifyNode (l, q) ;
9916 INC (i)
9917 END
9918 END simplifyVarient ;
9919
9920
9921 (*
9922 simplifyVarientField -
9923 *)
9924
9925 PROCEDURE simplifyVarientField (l: alist; n: node) ;
9926 VAR
9927 i, t: CARDINAL ;
9928 q : node ;
9929 BEGIN
9930 i := LowIndice (n^.varientfieldF.listOfSons) ;
9931 t := HighIndice (n^.varientfieldF.listOfSons) ;
9932 WHILE i<=t DO
9933 q := GetIndice (n^.varientfieldF.listOfSons, i) ;
9934 simplifyNode (l, q) ;
9935 INC (i)
9936 END
9937 END simplifyVarientField ;
9938
9939
9940 (*
9941 doSimplifyNode -
9942 *)
9943
9944 PROCEDURE doSimplifyNode (l: alist; n: node) ;
9945 BEGIN
9946 IF n=NIL
9947 THEN
9948 (* nothing. *)
9949 ELSIF isType (n)
9950 THEN
9951 (* no need to simplify a type. *)
9952 simplifyNode (l, getType (n))
9953 ELSIF isVar (n)
9954 THEN
9955 simplifyVar (l, n)
9956 ELSIF isRecord (n)
9957 THEN
9958 simplifyRecord (l, n)
9959 ELSIF isRecordField (n)
9960 THEN
9961 simplifyType (l, n^.recordfieldF.type)
9962 ELSIF isArray (n)
9963 THEN
9964 simplifyType (l, n^.arrayF.type)
9965 ELSIF isVarient (n)
9966 THEN
9967 simplifyVarient (l, n)
9968 ELSIF isVarientField (n)
9969 THEN
9970 simplifyVarientField (l, n)
9971 ELSIF isPointer (n)
9972 THEN
9973 simplifyType (l, n^.pointerF.type)
9974 END
9975 END doSimplifyNode ;
9976
9977
9978 (*
9979 simplifyNode -
9980 *)
9981
9982 PROCEDURE simplifyNode (l: alist; n: node) ;
9983 BEGIN
9984 IF NOT alists.isItemInList (l, n)
9985 THEN
9986 alists.includeItemIntoList (l, n) ;
9987 doSimplifyNode (l, n)
9988 END
9989 END simplifyNode ;
9990
9991
9992 (*
9993 doSimplify -
9994 *)
9995
9996 PROCEDURE doSimplify (n: node) ;
9997 VAR
9998 l: alist ;
9999 BEGIN
10000 l := alists.initList () ;
10001 simplifyNode (l, n) ;
10002 alists.killList (l)
10003 END doSimplify ;
10004
10005
10006 (*
10007 simplifyTypes -
10008 *)
10009
10010 PROCEDURE simplifyTypes (s: scopeT) ;
10011 BEGIN
10012 REPEAT
10013 simplified := TRUE ;
10014 ForeachIndiceInIndexDo (s.types, doSimplify) ;
10015 ForeachIndiceInIndexDo (s.variables, doSimplify)
10016 UNTIL simplified
10017 END simplifyTypes ;
10018
10019
10020 (*
10021 outDeclsDefC -
10022 *)
10023
10024 PROCEDURE outDeclsDefC (p: pretty; n: node) ;
10025 VAR
10026 s: scopeT ;
10027 BEGIN
10028 s := n^.defF.decls ;
10029 simplifyTypes (s) ;
10030 includeConstType (s) ;
10031
10032 doP := p ;
10033
10034 topologicallyOut (doConstC, doTypesC, doVarC,
10035 outputPartial,
10036 doNone, doCompletePartialC, doNone) ;
10037
10038 (* try and output types, constants before variables and procedures. *)
10039 includeDefVarProcedure (n) ;
10040
10041 topologicallyOut (doConstC, doTypesC, doVarC,
10042 outputPartial,
10043 doNone, doCompletePartialC, doNone) ;
10044
10045 ForeachIndiceInIndexDo (s.procedures, doPrototypeC)
10046 END outDeclsDefC ;
10047
10048
10049 (*
10050 includeConstType -
10051 *)
10052
10053 PROCEDURE includeConstType (s: scopeT) ;
10054 BEGIN
10055 ForeachIndiceInIndexDo (s.constants, addTodo) ;
10056 ForeachIndiceInIndexDo (s.types, addTypesTodo)
10057 END includeConstType ;
10058
10059
10060 (*
10061 includeVarProcedure -
10062 *)
10063
10064 PROCEDURE includeVarProcedure (s: scopeT) ;
10065 BEGIN
10066 ForeachIndiceInIndexDo (s.procedures, addTodo) ;
10067 ForeachIndiceInIndexDo (s.variables, addVariablesTodo)
10068 END includeVarProcedure ;
10069
10070
10071 (*
10072 includeVar -
10073 *)
10074
10075 PROCEDURE includeVar (s: scopeT) ;
10076 BEGIN
10077 ForeachIndiceInIndexDo (s.variables, addTodo)
10078 END includeVar ;
10079
10080
10081 (*
10082 includeExternals -
10083 *)
10084
10085 PROCEDURE includeExternals (n: node) ;
10086 VAR
10087 l: alist ;
10088 BEGIN
10089 l := alists.initList () ;
10090 visitNode (l, n, addExported) ;
10091 alists.killList (l)
10092 END includeExternals ;
10093
10094
10095 (*
10096 checkSystemInclude -
10097 *)
10098
10099 PROCEDURE checkSystemInclude (n: node) ;
10100 BEGIN
10101
10102 END checkSystemInclude ;
10103
10104
10105 (*
10106 addExported -
10107 *)
10108
10109 PROCEDURE addExported (n: node) ;
10110 VAR
10111 s: node ;
10112 BEGIN
10113 s := getScope (n) ;
10114 IF (s # NIL) AND isDef (s) AND (s # defModule)
10115 THEN
10116 IF isType (n) OR isVar (n) OR isConst (n)
10117 THEN
10118 addTodo (n)
10119 END
10120 END
10121 END addExported ;
10122
10123
10124 (*
10125 addExternal - only adds, n, if this symbol is external to the
10126 implementation module and is not a hidden type.
10127 *)
10128
10129 PROCEDURE addExternal (n: node) ;
10130 BEGIN
10131 IF (getScope (n) = defModule) AND isType (n) AND
10132 isTypeHidden (n) AND (NOT getExtendedOpaque ())
10133 THEN
10134 (* do nothing. *)
10135 ELSIF NOT isDef (n)
10136 THEN
10137 addTodo (n)
10138 END
10139 END addExternal ;
10140
10141
10142 (*
10143 includeDefConstType -
10144 *)
10145
10146 PROCEDURE includeDefConstType (n: node) ;
10147 VAR
10148 d: node ;
10149 BEGIN
10150 IF isImp (n)
10151 THEN
10152 defModule := lookupDef (getSymName (n)) ;
10153 IF defModule#NIL
10154 THEN
10155 simplifyTypes (defModule^.defF.decls) ;
10156 includeConstType (defModule^.defF.decls) ;
10157 foreachNodeDo (defModule^.defF.decls.symbols, addExternal)
10158 END
10159 END
10160 END includeDefConstType ;
10161
10162
10163 (*
10164 runIncludeDefConstType -
10165 *)
10166
10167 PROCEDURE runIncludeDefConstType (n: node) ;
10168 VAR
10169 d: node ;
10170 BEGIN
10171 IF isDef (n)
10172 THEN
10173 simplifyTypes (n^.defF.decls) ;
10174 includeConstType (n^.defF.decls) ;
10175 foreachNodeDo (n^.defF.decls.symbols, addExternal)
10176 END
10177 END runIncludeDefConstType ;
10178
10179
10180 (*
10181 joinProcedures - copies procedures from definition module,
10182 d, into implementation module, i.
10183 *)
10184
10185 PROCEDURE joinProcedures (i, d: node) ;
10186 VAR
10187 h, j: CARDINAL ;
10188 BEGIN
10189 assert (isDef (d)) ;
10190 assert (isImp (i)) ;
10191 j := 1 ;
10192 h := HighIndice (d^.defF.decls.procedures) ;
10193 WHILE j<=h DO
10194 IncludeIndiceIntoIndex (i^.impF.decls.procedures,
10195 GetIndice (d^.defF.decls.procedures, j)) ;
10196 INC (j)
10197 END
10198 END joinProcedures ;
10199
10200
10201 (*
10202 includeDefVarProcedure -
10203 *)
10204
10205 PROCEDURE includeDefVarProcedure (n: node) ;
10206 VAR
10207 d: node ;
10208 BEGIN
10209 IF isImp (n)
10210 THEN
10211 defModule := lookupDef (getSymName (n)) ;
10212 IF defModule#NIL
10213 THEN
10214 (*
10215 includeVar (defModule^.defF.decls) ;
10216 simplifyTypes (defModule^.defF.decls) ;
10217 *)
10218 joinProcedures (n, defModule)
10219 END
10220 ELSIF isDef (n)
10221 THEN
10222 includeVar (n^.defF.decls) ;
10223 simplifyTypes (n^.defF.decls)
10224 END
10225 END includeDefVarProcedure ;
10226
10227
10228 (*
10229 foreachModuleDo -
10230 *)
10231
10232 PROCEDURE foreachModuleDo (n: node; p: performOperation) ;
10233 BEGIN
10234 foreachDefModuleDo (p) ;
10235 foreachModModuleDo (p)
10236 END foreachModuleDo ;
10237
10238
10239 (*
10240 outDeclsImpC -
10241 *)
10242
10243 PROCEDURE outDeclsImpC (p: pretty; s: scopeT) ;
10244 BEGIN
10245 simplifyTypes (s) ;
10246 includeConstType (s) ;
10247
10248 doP := p ;
10249
10250 topologicallyOut (doConstC, doTypesC, doVarC,
10251 outputPartial,
10252 doNone, doCompletePartialC, doNone) ;
10253
10254 (* try and output types, constants before variables and procedures. *)
10255 includeVarProcedure (s) ;
10256
10257 topologicallyOut (doConstC, doTypesC, doVarC,
10258 outputPartial,
10259 doNone, doCompletePartialC, doNone) ;
10260
10261 END outDeclsImpC ;
10262
10263
10264 (*
10265 doStatementSequenceC -
10266 *)
10267
10268 PROCEDURE doStatementSequenceC (p: pretty; s: node) ;
10269 VAR
10270 i, h: CARDINAL ;
10271 BEGIN
10272 assert (isStatementSequence (s)) ;
10273 h := HighIndice (s^.stmtF.statements) ;
10274 i := 1 ;
10275 WHILE i<=h DO
10276 doStatementsC (p, GetIndice (s^.stmtF.statements, i)) ;
10277 INC (i)
10278 END
10279 END doStatementSequenceC ;
10280
10281
10282 (*
10283 isStatementSequenceEmpty -
10284 *)
10285
10286 PROCEDURE isStatementSequenceEmpty (s: node) : BOOLEAN ;
10287 BEGIN
10288 assert (isStatementSequence (s)) ;
10289 RETURN HighIndice (s^.stmtF.statements) = 0
10290 END isStatementSequenceEmpty ;
10291
10292
10293 (*
10294 isSingleStatement - returns TRUE if the statement sequence, s, has
10295 only one statement.
10296 *)
10297
10298 PROCEDURE isSingleStatement (s: node) : BOOLEAN ;
10299 VAR
10300 h: CARDINAL ;
10301 BEGIN
10302 assert (isStatementSequence (s)) ;
10303 h := HighIndice (s^.stmtF.statements) ;
10304 IF (h = 0) OR (h > 1)
10305 THEN
10306 RETURN FALSE
10307 END ;
10308 s := GetIndice (s^.stmtF.statements, 1) ;
10309 RETURN (NOT isStatementSequence (s)) OR isSingleStatement (s)
10310 END isSingleStatement ;
10311
10312
10313 (*
10314 doCommentC -
10315 *)
10316
10317 PROCEDURE doCommentC (p: pretty; s: node) ;
10318 VAR
10319 c: String ;
10320 BEGIN
10321 IF s # NIL
10322 THEN
10323 assert (isComment (s)) ;
10324 IF NOT isProcedureComment (s^.commentF.content)
10325 THEN
10326 IF isAfterComment (s^.commentF.content)
10327 THEN
10328 setNeedSpace (p) ;
10329 outText (p, " /* ")
10330 ELSE
10331 outText (p, "/* ")
10332 END ;
10333 c := getContent (s^.commentF.content) ;
10334 c := RemoveWhitePrefix (RemoveWhitePostfix (c)) ;
10335 outTextS (p, c) ;
10336 outText (p, " */\n")
10337 END
10338 END
10339 END doCommentC ;
10340
10341
10342 (*
10343 doAfterCommentC - emit an after comment, c, or a newline if, c, is empty.
10344 *)
10345
10346 PROCEDURE doAfterCommentC (p: pretty; c: node) ;
10347 BEGIN
10348 IF c = NIL
10349 THEN
10350 outText (p, "\n")
10351 ELSE
10352 doCommentC (p, c)
10353 END
10354 END doAfterCommentC ;
10355
10356
10357 (*
10358 doReturnC - issue a return statement and also place in an after comment if one exists.
10359 *)
10360
10361 PROCEDURE doReturnC (p: pretty; s: node) ;
10362 VAR
10363 type: node ;
10364 BEGIN
10365 assert (isReturn (s)) ;
10366 doCommentC (p, s^.returnF.returnComment.body) ;
10367 outText (p, "return") ;
10368 IF (s^.returnF.scope#NIL) AND (s^.returnF.exp#NIL)
10369 THEN
10370 setNeedSpace (p) ;
10371 IF (NOT isProcedure (s^.returnF.scope)) OR (getType (s^.returnF.scope)=NIL)
10372 THEN
10373 metaError1 ('{%1DMad} has no return type', s^.returnF.scope) ;
10374 ELSE
10375 IF isProcedure (s^.returnF.scope) AND nodeUsesOpaque (s^.returnF.scope)
10376 THEN
10377 forceCastOpaque (p, s^.returnF.scope, s^.returnF.exp,
10378 getNodeOpaqueVoidStar (s^.returnF.scope))
10379 ELSE
10380 doExprCastC (p, s^.returnF.exp, getType (s^.returnF.scope))
10381 END
10382 END
10383 END ;
10384 outText (p, ";") ;
10385 doAfterCommentC (p, s^.returnF.returnComment.after)
10386 END doReturnC ;
10387
10388
10389 (*
10390 isZtypeEquivalent -
10391 *)
10392
10393 PROCEDURE isZtypeEquivalent (type: node) : BOOLEAN ;
10394 BEGIN
10395 CASE type^.kind OF
10396
10397 cardinal,
10398 longcard,
10399 shortcard,
10400 integer,
10401 longint,
10402 shortint,
10403 ztype : RETURN TRUE
10404
10405 ELSE
10406 RETURN FALSE
10407 END
10408 END isZtypeEquivalent ;
10409
10410
10411 (*
10412 isEquivalentType - returns TRUE if type1 and type2 are equivalent.
10413 *)
10414
10415 PROCEDURE isEquivalentType (type1, type2: node) : BOOLEAN ;
10416 BEGIN
10417 type1 := skipType (type1) ;
10418 type2 := skipType (type2) ;
10419 RETURN ((type1 = type2) OR
10420 (isZtypeEquivalent (type1) AND isZtypeEquivalent (type2)))
10421 END isEquivalentType ;
10422
10423
10424 (*
10425 doExprCastC - build a cast if necessary.
10426 *)
10427
10428 PROCEDURE doExprCastC (p: pretty; e, type: node) ;
10429 VAR
10430 stype: node ;
10431 BEGIN
10432 stype := skipType (type) ;
10433 IF (NOT isEquivalentType (type, getExprType (e))) AND
10434 (NOT ((e^.kind = nil) AND (isPointer (stype) OR (stype^.kind = address))))
10435 THEN
10436 IF lang = ansiCP
10437 THEN
10438 (* potentially a cast is required. *)
10439 IF isPointer (type) OR (type = addressN)
10440 THEN
10441 outText (p, 'static_cast<') ;
10442 doTypeNameC (p, type) ;
10443 noSpace (p) ;
10444 outText (p, '> (') ;
10445 doExprC (p, e) ;
10446 outText (p, ')') ;
10447 RETURN
10448 ELSE
10449 outText (p, 'static_cast<') ;
10450 IF isProcType (skipType (type))
10451 THEN
10452 doTypeNameC (p, type) ;
10453 outText (p, "_t")
10454 ELSE
10455 doTypeNameC (p, type)
10456 END ;
10457 noSpace (p) ;
10458 outText (p, '> (') ;
10459 doExprC (p, e) ;
10460 outText (p, ')') ;
10461 RETURN
10462 END
10463 END
10464 END ;
10465 doExprC (p, e)
10466 END doExprCastC ;
10467
10468
10469 (*
10470 requiresUnpackProc - returns TRUE if either the expr is a procedure or the proctypes differ.
10471 *)
10472
10473 PROCEDURE requiresUnpackProc (s: node) : BOOLEAN ;
10474 BEGIN
10475 assert (isAssignment (s)) ;
10476 RETURN isProcedure (s^.assignmentF.expr) OR
10477 (skipType (getType (s^.assignmentF.des)) # skipType (getType (s^.assignmentF.expr)))
10478 END requiresUnpackProc ;
10479
10480
10481 (*
10482 forceCastOpaque -
10483 *)
10484
10485 PROCEDURE forceCastOpaque (p: pretty; des, expr: node; toVoidStar: BOOLEAN) ;
10486 BEGIN
10487 IF nodeUsesOpaque (expr)
10488 THEN
10489 flushOpaque (p, expr, getNodeOpaqueVoidStar (des))
10490 ELSE
10491 forceReintCastOpaque (p, des, expr, toVoidStar)
10492 END
10493 END forceCastOpaque ;
10494
10495
10496 (*
10497 forceReintCastOpaque -
10498 *)
10499
10500 PROCEDURE forceReintCastOpaque (p: pretty; des, expr: node; toVoidStar: BOOLEAN) ;
10501 VAR
10502 type: node ;
10503 BEGIN
10504 type := getType (des) ;
10505 IF toVoidStar
10506 THEN
10507 (* next is true cast to void * opaque type. *)
10508 outText (p, 'static_cast<') ;
10509 doTypeNameC (p, type) ;
10510 noSpace (p) ;
10511 outText (p, '> (') ;
10512 doExprC (p, expr) ;
10513 outText (p, ')')
10514 ELSE
10515 (* next is false cast to __opaque opaque type. *)
10516 outText (p, 'static_cast<') ;
10517 doTypeNameC (p, type) ;
10518 outText (p, '__opaque') ;
10519 noSpace (p) ;
10520 outText (p, '> (') ;
10521 doExprC (p, expr) ;
10522 outText (p, ')') ;
10523 END
10524 END forceReintCastOpaque ;
10525
10526
10527 (*
10528 doUnConstCastUnbounded - if node n type is an unbounded array then
10529 use const_cast to remove the const parameter
10530 to allow the unbounded array to be modified.
10531 *)
10532
10533 PROCEDURE doUnConstCastUnbounded (p: pretty; n: node) ;
10534 VAR
10535 type, v: node ;
10536 BEGIN
10537 IF isArrayRef (n)
10538 THEN
10539 IF isVar (n^.arrayrefF.array)
10540 THEN
10541 v := n^.arrayrefF.array ;
10542 IF (v^.varF.isParameter OR v^.varF.isVarParameter) AND
10543 isUnbounded (getType (v))
10544 THEN
10545 type := getType (v) ;
10546 outText (p, " /* const_cast<") ;
10547 doTypeNameC (p, type) ;
10548 outText (p, "> is needed */ ") ;
10549 END
10550 END
10551 END
10552 END doUnConstCastUnbounded ;
10553
10554
10555 (*
10556 doAssignmentC -
10557 *)
10558
10559 PROCEDURE doAssignmentC (p: pretty; s: node) ;
10560 BEGIN
10561 assert (isAssignment (s)) ;
10562 doCommentC (p, s^.assignmentF.assignComment.body) ;
10563 IF debugOpaque
10564 THEN
10565 outText (p, " /* des: */ ") ;
10566 dumpOpaqueState (s^.assignmentF.des) ;
10567 outText (p, " /* expr: */ ") ;
10568 dumpOpaqueState (s^.assignmentF.expr)
10569 END ;
10570 s^.assignmentF.des := doExprCup (p, s^.assignmentF.des,
10571 requiresUnpackProc (s), TRUE) ;
10572 IF debugOpaque
10573 THEN
10574 outText (p, "\n /* after doExprCup des: */ ") ;
10575 dumpOpaqueState (s^.assignmentF.des) ;
10576 outText (p, "\n")
10577 END ;
10578 setNeedSpace (p) ;
10579 outText (p, "=") ;
10580 setNeedSpace (p) ;
10581 IF nodeUsesOpaque (s^.assignmentF.des)
10582 THEN
10583 forceCastOpaque (p, s^.assignmentF.des, s^.assignmentF.expr,
10584 getNodeOpaqueVoidStar (s^.assignmentF.des))
10585 ELSE
10586 IF debugOpaque
10587 THEN
10588 outText (p, " /* no opaque des seen */ ")
10589 END ;
10590 doExprCastC (p, s^.assignmentF.expr, getType (s^.assignmentF.des))
10591 END ;
10592 outText (p, ";") ;
10593 doAfterCommentC (p, s^.assignmentF.assignComment.after)
10594 END doAssignmentC ;
10595
10596
10597 (*
10598 containsStatement -
10599 *)
10600
10601 PROCEDURE containsStatement (s: node) : BOOLEAN ;
10602 BEGIN
10603 RETURN (s # NIL) AND isStatementSequence (s) AND (NOT isStatementSequenceEmpty (s))
10604 END containsStatement ;
10605
10606
10607 (*
10608 doCompoundStmt -
10609 *)
10610
10611 PROCEDURE doCompoundStmt (p: pretty; s: node) ;
10612 BEGIN
10613 IF (s = NIL) OR (isStatementSequence (s) AND isStatementSequenceEmpty (s))
10614 THEN
10615 p := pushPretty (p) ;
10616 setindent (p, getindent (p) + indentationC) ;
10617 outText (p, "{} /* empty. */\n") ;
10618 p := popPretty (p)
10619 ELSIF isStatementSequence (s) AND isSingleStatement (s) AND (NOT forceCompoundStatement)
10620 THEN
10621 p := pushPretty (p) ;
10622 setindent (p, getindent (p) + indentationC) ;
10623 doStatementSequenceC (p, s) ;
10624 p := popPretty (p)
10625 ELSE
10626 p := pushPretty (p) ;
10627 setindent (p, getindent (p) + indentationC) ;
10628 outText (p, "{\n") ;
10629 p := pushPretty (p) ;
10630 setindent (p, getindent (p) + indentationC) ;
10631 doStatementSequenceC (p, s) ;
10632 p := popPretty (p) ;
10633 outText (p, "}\n") ;
10634 p := popPretty (p)
10635 END
10636 END doCompoundStmt ;
10637
10638
10639 (*
10640 doElsifC -
10641 *)
10642
10643 PROCEDURE doElsifC (p: pretty; s: node) ;
10644 BEGIN
10645 assert (isElsif (s)) ;
10646 outText (p, "else if") ;
10647 setNeedSpace (p) ;
10648 outText (p, "(") ;
10649 doExprC (p, s^.elsifF.expr) ;
10650 outText (p, ")\n") ;
10651 assert ((s^.elsifF.else = NIL) OR (s^.elsifF.elsif = NIL)) ;
10652 IF forceCompoundStatement OR
10653 (hasIfAndNoElse (s^.elsifF.then) AND
10654 ((s^.elsifF.else # NIL) OR (s^.elsifF.elsif # NIL)))
10655 THEN
10656 (* avoid dangling else. *)
10657 p := pushPretty (p) ;
10658 setindent (p, getindent (p) + indentationC) ;
10659 outText (p, "{\n") ;
10660 p := pushPretty (p) ;
10661 setindent (p, getindent (p) + indentationC) ;
10662 outText (p, "/* avoid dangling else. */\n") ;
10663 doStatementSequenceC (p, s^.elsifF.then) ;
10664 p := popPretty (p) ;
10665 outText (p, "}\n") ;
10666 p := popPretty (p)
10667 ELSE
10668 doCompoundStmt (p, s^.elsifF.then)
10669 END ;
10670 IF containsStatement (s^.elsifF.else)
10671 THEN
10672 outText (p, "else\n") ;
10673 IF forceCompoundStatement
10674 THEN
10675 (* avoid dangling else. *)
10676 p := pushPretty (p) ;
10677 setindent (p, getindent (p) + indentationC) ;
10678 outText (p, "{\n") ;
10679 p := pushPretty (p) ;
10680 setindent (p, getindent (p) + indentationC) ;
10681 outText (p, "/* avoid dangling else. */\n") ;
10682 doStatementSequenceC (p, s^.elsifF.else) ;
10683 p := popPretty (p) ;
10684 outText (p, "}\n") ;
10685 p := popPretty (p)
10686 ELSE
10687 doCompoundStmt (p, s^.elsifF.else)
10688 END
10689 ELSIF (s^.elsifF.elsif#NIL) AND isElsif (s^.elsifF.elsif)
10690 THEN
10691 doElsifC (p, s^.elsifF.elsif)
10692 END
10693 END doElsifC ;
10694
10695
10696 (*
10697 noIfElse -
10698 *)
10699
10700 PROCEDURE noIfElse (n: node) : BOOLEAN ;
10701 BEGIN
10702 RETURN (n # NIL) AND isIf (n) AND (n^.ifF.else = NIL) AND (n^.ifF.elsif = NIL)
10703 END noIfElse ;
10704
10705
10706 (*
10707 noIfElseChained - returns TRUE if, n, is an IF statement which
10708 has no associated ELSE statement. An IF with an
10709 ELSIF is also checked for no ELSE and will result
10710 in a return value of TRUE.
10711 *)
10712
10713 PROCEDURE noIfElseChained (n: node) : BOOLEAN ;
10714 VAR
10715 e: node ;
10716 BEGIN
10717 IF n # NIL
10718 THEN
10719 IF isIf (n)
10720 THEN
10721 IF n^.ifF.else # NIL
10722 THEN
10723 (* we do have an else, continue to check this statement. *)
10724 RETURN hasIfAndNoElse (n^.ifF.else)
10725 ELSIF n^.ifF.elsif = NIL
10726 THEN
10727 (* neither else or elsif. *)
10728 RETURN TRUE
10729 ELSE
10730 (* test elsif for lack of else. *)
10731 e := n^.ifF.elsif ;
10732 assert (isElsif (e)) ;
10733 RETURN noIfElseChained (e)
10734 END
10735 ELSIF isElsif (n)
10736 THEN
10737 IF n^.elsifF.else # NIL
10738 THEN
10739 (* we do have an else, continue to check this statement. *)
10740 RETURN hasIfAndNoElse (n^.elsifF.else)
10741 ELSIF n^.elsifF.elsif = NIL
10742 THEN
10743 (* neither else or elsif. *)
10744 RETURN TRUE
10745 ELSE
10746 (* test elsif for lack of else. *)
10747 e := n^.elsifF.elsif ;
10748 assert (isElsif (e)) ;
10749 RETURN noIfElseChained (e)
10750 END
10751 END
10752 END ;
10753 RETURN FALSE
10754 END noIfElseChained ;
10755
10756
10757 (*
10758 hasIfElse -
10759 *)
10760
10761 PROCEDURE hasIfElse (n: node) : BOOLEAN ;
10762 BEGIN
10763 IF n # NIL
10764 THEN
10765 IF isStatementSequence (n)
10766 THEN
10767 IF isStatementSequenceEmpty (n)
10768 THEN
10769 RETURN FALSE
10770 ELSIF isSingleStatement (n)
10771 THEN
10772 n := GetIndice (n^.stmtF.statements, 1) ;
10773 RETURN isIfElse (n)
10774 END
10775 END
10776 END ;
10777 RETURN FALSE
10778 END hasIfElse ;
10779
10780
10781 (*
10782 isIfElse -
10783 *)
10784
10785 PROCEDURE isIfElse (n: node) : BOOLEAN ;
10786 BEGIN
10787 RETURN (n # NIL) AND isIf (n) AND ((n^.ifF.else # NIL) OR (n^.ifF.elsif # NIL))
10788 END isIfElse ;
10789
10790
10791 (*
10792 hasIfAndNoElse - returns TRUE if statement, n, is a single statement
10793 which is an IF and it has no else statement.
10794 *)
10795
10796 PROCEDURE hasIfAndNoElse (n: node) : BOOLEAN ;
10797 BEGIN
10798 IF n # NIL
10799 THEN
10800 IF isStatementSequence (n)
10801 THEN
10802 IF isStatementSequenceEmpty (n)
10803 THEN
10804 RETURN FALSE
10805 ELSIF isSingleStatement (n)
10806 THEN
10807 n := GetIndice (n^.stmtF.statements, 1) ;
10808 RETURN hasIfAndNoElse (n)
10809 ELSE
10810 n := GetIndice (n^.stmtF.statements, HighIndice (n^.stmtF.statements)) ;
10811 RETURN hasIfAndNoElse (n)
10812 END
10813 ELSIF isElsif (n) OR isIf (n)
10814 THEN
10815 RETURN noIfElseChained (n)
10816 END
10817 END ;
10818 RETURN FALSE
10819 END hasIfAndNoElse ;
10820
10821
10822 (*
10823 doIfC - issue an if statement and also place in an after comment if one exists.
10824 The if statement might contain an else or elsif which are also handled.
10825 *)
10826
10827 PROCEDURE doIfC (p: pretty; s: node) ;
10828 BEGIN
10829 assert (isIf (s)) ;
10830 doCommentC (p, s^.ifF.ifComment.body) ;
10831 outText (p, "if") ;
10832 setNeedSpace (p) ;
10833 outText (p, "(") ;
10834 doExprC (p, s^.ifF.expr) ;
10835 outText (p, ")") ;
10836 doAfterCommentC (p, s^.ifF.ifComment.after) ;
10837 IF hasIfAndNoElse (s^.ifF.then) AND
10838 ((s^.ifF.else # NIL) OR (s^.ifF.elsif # NIL))
10839 THEN
10840 (* avoid dangling else. *)
10841 p := pushPretty (p) ;
10842 setindent (p, getindent (p) + indentationC) ;
10843 outText (p, "{\n") ;
10844 p := pushPretty (p) ;
10845 setindent (p, getindent (p) + indentationC) ;
10846 outText (p, "/* avoid dangling else. */\n") ;
10847 doStatementSequenceC (p, s^.ifF.then) ;
10848 p := popPretty (p) ;
10849 outText (p, "}\n") ;
10850 p := popPretty (p)
10851 ELSIF noIfElse (s) AND hasIfElse (s^.ifF.then)
10852 THEN
10853 (* gcc does not like legal non dangling else, as it is poor style.
10854 So we will avoid getting a warning. *)
10855 p := pushPretty (p) ;
10856 setindent (p, getindent (p) + indentationC) ;
10857 outText (p, "{\n") ;
10858 p := pushPretty (p) ;
10859 setindent (p, getindent (p) + indentationC) ;
10860 outText (p, "/* avoid gcc warning by using compound statement even if not strictly necessary. */\n") ;
10861 doStatementSequenceC (p, s^.ifF.then) ;
10862 p := popPretty (p) ;
10863 outText (p, "}\n") ;
10864 p := popPretty (p)
10865 ELSE
10866 doCompoundStmt (p, s^.ifF.then)
10867 END ;
10868 assert ((s^.ifF.else = NIL) OR (s^.ifF.elsif = NIL)) ;
10869 IF containsStatement (s^.ifF.else)
10870 THEN
10871 doCommentC (p, s^.ifF.elseComment.body) ;
10872 outText (p, "else") ;
10873 doAfterCommentC (p, s^.ifF.elseComment.after) ;
10874 doCompoundStmt (p, s^.ifF.else)
10875 ELSIF (s^.ifF.elsif#NIL) AND isElsif (s^.ifF.elsif)
10876 THEN
10877 doCommentC (p, s^.ifF.elseComment.body) ;
10878 doCommentC (p, s^.ifF.elseComment.after) ;
10879 doElsifC (p, s^.ifF.elsif)
10880 END ;
10881 doCommentC (p, s^.ifF.endComment.after) ;
10882 doCommentC (p, s^.ifF.endComment.body)
10883 END doIfC ;
10884
10885
10886 (*
10887 doForIncCP -
10888 *)
10889
10890 PROCEDURE doForIncCP (p: pretty; s: node) ;
10891 VAR
10892 t: node ;
10893 BEGIN
10894 assert (isFor (s)) ;
10895 t := skipType (getType (s^.forF.des)) ;
10896 IF isEnumeration (t)
10897 THEN
10898 IF s^.forF.increment = NIL
10899 THEN
10900 doExprC (p, s^.forF.des) ;
10901 outText (p, "= static_cast<") ;
10902 doTypeNameC (p, getType (s^.forF.des)) ;
10903 noSpace (p) ;
10904 outText (p, ">(static_cast<int>(") ;
10905 doExprC (p, s^.forF.des) ;
10906 outText (p, "+1))")
10907 ELSE
10908 doExprC (p, s^.forF.des) ;
10909 outText (p, "= static_cast<") ;
10910 doTypeNameC (p, getType (s^.forF.des)) ;
10911 noSpace (p) ;
10912 outText (p, ">(static_cast<int>(") ;
10913 doExprC (p, s^.forF.des) ;
10914 outText (p, "+") ;
10915 doExprC (p, s^.forF.increment) ;
10916 outText (p, "))")
10917 END
10918 ELSE
10919 doForIncC (p, s)
10920 END
10921 END doForIncCP ;
10922
10923
10924 (*
10925 doForIncC -
10926 *)
10927
10928 PROCEDURE doForIncC (p: pretty; s: node) ;
10929 BEGIN
10930 IF s^.forF.increment = NIL
10931 THEN
10932 doExprC (p, s^.forF.des) ;
10933 outText (p, "++")
10934 ELSE
10935 doExprC (p, s^.forF.des) ;
10936 outText (p, "=") ;
10937 doExprC (p, s^.forF.des) ;
10938 outText (p, "+") ;
10939 doExprC (p, s^.forF.increment)
10940 END
10941 END doForIncC ;
10942
10943
10944 (*
10945 doForInc -
10946 *)
10947
10948 PROCEDURE doForInc (p: pretty; s: node) ;
10949 BEGIN
10950 IF lang = ansiCP
10951 THEN
10952 doForIncCP (p, s)
10953 ELSE
10954 doForIncC (p, s)
10955 END
10956 END doForInc ;
10957
10958
10959 (*
10960 doForC -
10961 *)
10962
10963 PROCEDURE doForC (p: pretty; s: node) ;
10964 BEGIN
10965 assert (isFor (s)) ;
10966 outText (p, "for (") ;
10967 doExprC (p, s^.forF.des) ;
10968 outText (p, "=") ;
10969 doExprC (p, s^.forF.start) ;
10970 outText (p, ";") ;
10971 setNeedSpace (p) ;
10972 doExprC (p, s^.forF.des) ;
10973 outText (p, "<=") ;
10974 doExprC (p, s^.forF.end) ;
10975 outText (p, ";") ;
10976 setNeedSpace (p) ;
10977 doForInc (p, s) ;
10978 outText (p, ")\n") ;
10979 doCompoundStmt (p, s^.forF.statements)
10980 END doForC ;
10981
10982
10983 (*
10984 doRepeatC -
10985 *)
10986
10987 PROCEDURE doRepeatC (p: pretty; s: node) ;
10988 BEGIN
10989 assert (isRepeat (s)) ;
10990 doCommentC (p, s^.repeatF.repeatComment.body) ;
10991 outText (p, "do {") ;
10992 doAfterCommentC (p, s^.repeatF.repeatComment.after) ;
10993 p := pushPretty (p) ;
10994 setindent (p, getindent (p) + indentationC) ;
10995 doStatementSequenceC (p, s^.repeatF.statements) ;
10996 doCommentC (p, s^.repeatF.untilComment.body) ;
10997 p := popPretty (p) ;
10998 outText (p, "} while (! (") ;
10999 doExprC (p, s^.repeatF.expr) ;
11000 outText (p, "));") ;
11001 doAfterCommentC (p, s^.repeatF.untilComment.after)
11002 END doRepeatC ;
11003
11004
11005 (*
11006 doWhileC -
11007 *)
11008
11009 PROCEDURE doWhileC (p: pretty; s: node) ;
11010 BEGIN
11011 assert (isWhile (s)) ;
11012 doCommentC (p, s^.whileF.doComment.body) ;
11013 outText (p, "while (") ;
11014 doExprC (p, s^.whileF.expr) ;
11015 outText (p, ")") ;
11016 doAfterCommentC (p, s^.whileF.doComment.after) ;
11017 doCompoundStmt (p, s^.whileF.statements) ;
11018 doCommentC (p, s^.whileF.endComment.body) ;
11019 doCommentC (p, s^.whileF.endComment.after)
11020 END doWhileC ;
11021
11022
11023 (*
11024 doFuncHighC -
11025 *)
11026
11027 PROCEDURE doFuncHighC (p: pretty; a: node) ;
11028 VAR
11029 s, n: node ;
11030 BEGIN
11031 IF isLiteral (a) AND (getType (a) = charN)
11032 THEN
11033 outCard (p, 0)
11034 ELSIF isString (a)
11035 THEN
11036 outCard (p, a^.stringF.length-2)
11037 ELSIF isConst (a) AND isString (a^.constF.value)
11038 THEN
11039 doFuncHighC (p, a^.constF.value)
11040 ELSIF isUnbounded (getType (a))
11041 THEN
11042 outText (p, '_') ;
11043 outTextN (p, getSymName (a)) ;
11044 outText (p, '_high')
11045 ELSIF isArray (skipType (getType (a)))
11046 THEN
11047 n := skipType (getType (a)) ;
11048 s := n^.arrayF.subr ;
11049 IF isZero (getMin (s))
11050 THEN
11051 doExprC (p, getMax (s))
11052 ELSE
11053 outText (p, '(') ;
11054 doExprC (p, getMax (s)) ;
11055 doSubtractC (p, getMin (s)) ;
11056 outText (p, ')')
11057 END
11058 ELSE
11059 (* output sizeof (a) in bytes for the high. *)
11060 outText (p, '(sizeof') ;
11061 setNeedSpace (p) ;
11062 outText (p, '(') ;
11063 doExprC (p, a) ;
11064 outText (p, ')-1)')
11065 END
11066 END doFuncHighC ;
11067
11068
11069 (*
11070 doMultiplyBySize -
11071 *)
11072
11073 PROCEDURE doMultiplyBySize (p: pretty; a: node) ;
11074 BEGIN
11075 IF (a # charN) AND (a # byteN) AND (a # locN)
11076 THEN
11077 setNeedSpace (p) ;
11078 outText (p, '* sizeof (') ;
11079 doTypeNameC (p, a) ;
11080 noSpace (p) ;
11081 outText (p, ')')
11082 END
11083 END doMultiplyBySize ;
11084
11085
11086 (*
11087 doTotype -
11088 *)
11089
11090 PROCEDURE doTotype (p: pretty; a, t: node) ;
11091 BEGIN
11092 IF (NOT isString (a)) AND (NOT isLiteral (a))
11093 THEN
11094 IF isVar (a)
11095 THEN
11096 IF (a^.varF.isParameter OR a^.varF.isVarParameter) AND
11097 isUnbounded (getType (a)) AND (skipType (getType (getType (a))) = skipType (getType (t)))
11098 THEN
11099 (* do not multiply by size as the existing high value is correct. *)
11100 RETURN
11101 END ;
11102 a := getType (a) ;
11103 IF isArray (a)
11104 THEN
11105 doMultiplyBySize (p, skipType (getType (a)))
11106 END
11107 END
11108 END ;
11109 IF t = wordN
11110 THEN
11111 setNeedSpace (p) ;
11112 outText (p, '/ sizeof (') ;
11113 doTypeNameC (p, wordN) ;
11114 noSpace (p) ;
11115 outText (p, ')')
11116 END
11117 END doTotype ;
11118
11119
11120 (*
11121 doFuncUnbounded -
11122 *)
11123
11124 PROCEDURE doFuncUnbounded (p: pretty; actual, formalParam, formal, func: node) ;
11125 VAR
11126 h: node ;
11127 s: String ;
11128 BEGIN
11129 assert (isUnbounded (formal)) ;
11130 outText (p, '(') ;
11131 IF (lang = ansiCP) AND isParam (formalParam)
11132 THEN
11133 outText (p, "const") ;
11134 setNeedSpace (p)
11135 END ;
11136 doTypeC (p, getType (formal), formal) ;
11137 setNeedSpace (p) ;
11138 outText (p, '*)') ;
11139 setNeedSpace (p) ;
11140 IF isLiteral (actual) AND (getType (actual) = charN)
11141 THEN
11142 outText (p, '"\0') ;
11143 s := InitStringCharStar (keyToCharStar (actual^.literalF.name)) ;
11144 s := DynamicStrings.Slice (DynamicStrings.Mark (s), 0, -1) ;
11145 outTextS (p, s) ;
11146 outText (p, '"') ;
11147 s := KillString (s)
11148 ELSIF isString (actual)
11149 THEN
11150 outCstring (p, actual, TRUE)
11151 ELSIF isConst (actual)
11152 THEN
11153 actual := resolveString (actual) ;
11154 assert (isString (actual)) ;
11155 outCstring (p, actual, TRUE)
11156 ELSIF isFuncCall (actual)
11157 THEN
11158 IF getExprType (actual) = NIL
11159 THEN
11160 metaError3 ('there is no return type to the procedure function {%3ad} which is being passed as the parameter {%1ad} to {%2ad}', formal, func, actual)
11161 ELSE
11162 outText (p, '&') ;
11163 doExprC (p, actual)
11164 END
11165 ELSIF isUnbounded (getType (actual))
11166 THEN
11167 doFQNameC (p, actual)
11168 (* doExprC (p, actual). *)
11169 ELSE
11170 outText (p, '&') ;
11171 doExprC (p, actual) ;
11172 IF isArray (skipType (getType (actual)))
11173 THEN
11174 outText (p, '.array[0]')
11175 END
11176 END ;
11177 IF NOT (enableDefForCStrings AND isDefForC (getScope (func)))
11178 THEN
11179 outText (p, ',') ;
11180 setNeedSpace (p) ;
11181 doFuncHighC (p, actual) ;
11182 doTotype (p, actual, formal)
11183 END
11184 END doFuncUnbounded ;
11185
11186
11187 (*
11188 doProcedureParamC -
11189 *)
11190
11191 PROCEDURE doProcedureParamC (p: pretty; actual, formal: node) ;
11192 BEGIN
11193 IF isForC (formal)
11194 THEN
11195 outText (p, '(') ;
11196 doFQNameC (p, getType (formal)) ;
11197 outText (p, "_C") ;
11198 outText (p, ')') ;
11199 setNeedSpace (p) ;
11200 doExprC (p, actual)
11201 ELSE
11202 outText (p, '(') ;
11203 doTypeNameC (p, getType (formal)) ;
11204 outText (p, ')') ;
11205 setNeedSpace (p) ;
11206 outText (p, '{') ;
11207 outText (p, '(') ;
11208 doFQNameC (p, getType (formal)) ;
11209 outText (p, '_t)') ;
11210 setNeedSpace (p) ;
11211 doExprC (p, actual) ;
11212 outText (p, '}')
11213 END
11214 END doProcedureParamC ;
11215
11216
11217 (*
11218 doAdrExprC -
11219 *)
11220
11221 PROCEDURE doAdrExprC (p: pretty; n: node) ;
11222 BEGIN
11223 IF isDeref (n)
11224 THEN
11225 (* No point in issuing & ( * n ). *)
11226 doExprC (p, n^.unaryF.arg)
11227 ELSIF isVar (n) AND n^.varF.isVarParameter
11228 THEN
11229 (* No point in issuing & ( * n ). *)
11230 doFQNameC (p, n)
11231 ELSE
11232 outText (p, '&') ;
11233 doExprC (p, n)
11234 END
11235 END doAdrExprC ;
11236
11237
11238 (*
11239 typePair -
11240 *)
11241
11242 PROCEDURE typePair (a, b, x, y: node) : BOOLEAN ;
11243 BEGIN
11244 RETURN ((a = x) AND (b = y)) OR ((a = y) AND (b = x))
11245 END typePair ;
11246
11247
11248 (*
11249 needsCast - return TRUE if the actual type parameter needs to be cast to
11250 the formal type.
11251 *)
11252
11253 PROCEDURE needsCast (at, ft: node) : BOOLEAN ;
11254 BEGIN
11255 at := skipType (at) ;
11256 ft := skipType (ft) ;
11257 IF (at = nilN) OR (at^.kind = nil) OR
11258 (at = ft) OR
11259 typePair (at, ft, cardinalN, wordN) OR
11260 typePair (at, ft, cardinalN, ztypeN) OR
11261 typePair (at, ft, integerN, ztypeN) OR
11262 typePair (at, ft, longcardN, ztypeN) OR
11263 typePair (at, ft, shortcardN, ztypeN) OR
11264 typePair (at, ft, longintN, ztypeN) OR
11265 typePair (at, ft, shortintN, ztypeN) OR
11266 typePair (at, ft, realN, rtypeN) OR
11267 typePair (at, ft, longrealN, rtypeN) OR
11268 typePair (at, ft, shortrealN, rtypeN)
11269 THEN
11270 RETURN FALSE
11271 ELSE
11272 RETURN TRUE
11273 END
11274 END needsCast ;
11275
11276
11277 (*
11278 castDestType - emit the destination type ft
11279 *)
11280
11281 PROCEDURE castDestType (p: pretty; formal, ft: node) ;
11282 BEGIN
11283 doTypeNameC (p, ft) ;
11284 IF isVarParam (formal)
11285 THEN
11286 outText (p, '*')
11287 END
11288 END castDestType ;
11289
11290
11291 (*
11292 identifyPointer -
11293 *)
11294
11295 PROCEDURE identifyPointer (type: node) : node ;
11296 BEGIN
11297 IF isPointer (type)
11298 THEN
11299 IF skipType (getType (type)) = charN
11300 THEN
11301 RETURN charStarN
11302 ELSIF (skipType (getType (type)) = byteN) OR
11303 (skipType (getType (type)) = locN)
11304 THEN
11305 RETURN addressN
11306 END
11307 END ;
11308 RETURN type
11309 END identifyPointer ;
11310
11311
11312 (*
11313 castPointer - provides a six way cast between ADDRESS (ie void * ),
11314 char * and const char *.
11315 *)
11316
11317 PROCEDURE castPointer (p: pretty; actual, formal, at, ft: node) : CARDINAL ;
11318 VAR
11319 sat, sft: node ;
11320 parenth : CARDINAL ;
11321 BEGIN
11322 parenth := 0 ;
11323 IF at # ft
11324 THEN
11325 sat := identifyPointer (skipType (at)) ;
11326 sft := identifyPointer (skipType (ft)) ;
11327 IF sat = addressN
11328 THEN
11329 IF sft = charStarN
11330 THEN
11331 outText (p, 'reinterpret_cast <') ;
11332 castDestType (p, formal, ft) ;
11333 outText (p, '>')
11334 ELSIF sft = constCharStarN
11335 THEN
11336 outText (p, 'const_cast <') ;
11337 castDestType (p, formal, ft) ;
11338 outText (p, '> (static_cast <') ;
11339 doTypeNameC (p, charStarN) ;
11340 outText (p, '>') ;
11341 INC (parenth)
11342 ELSE
11343 outText (p, 'reinterpret_cast <') ;
11344 castDestType (p, formal, ft) ;
11345 outText (p, '>')
11346 END
11347 ELSIF sat = charStarN
11348 THEN
11349 IF sft = addressN
11350 THEN
11351 outText (p, 'reinterpret_cast <') ;
11352 castDestType (p, formal, ft) ;
11353 outText (p, '>')
11354 ELSIF sft = constCharStarN
11355 THEN
11356 outText (p, 'const_cast <') ;
11357 castDestType (p, formal, ft) ;
11358 outText (p, '>')
11359 ELSE
11360 outText (p, 'reinterpret_cast <') ;
11361 castDestType (p, formal, ft) ;
11362 outText (p, '>')
11363 END
11364 ELSIF sat = constCharStarN
11365 THEN
11366 IF sft = addressN
11367 THEN
11368 outText (p, 'static_cast <') ;
11369 castDestType (p, formal, ft) ;
11370 outText (p, '> (const_cast <') ;
11371 doTypeNameC (p, charStarN) ;
11372 outText (p, '>') ;
11373 INC (parenth)
11374 ELSIF sft = charStarN
11375 THEN
11376 outText (p, 'const_cast <') ;
11377 castDestType (p, formal, ft) ;
11378 outText (p, '>')
11379 ELSE
11380 outText (p, 'reinterpret_cast <') ;
11381 castDestType (p, formal, ft) ;
11382 outText (p, '>')
11383 END
11384 ELSE
11385 outText (p, 'reinterpret_cast <') ;
11386 castDestType (p, formal, ft) ;
11387 outText (p, '>')
11388 END ;
11389 setNeedSpace (p) ;
11390 outText (p, '(') ;
11391 INC (parenth)
11392 END ;
11393 RETURN parenth
11394 END castPointer ;
11395
11396
11397 (*
11398 checkSystemCast - checks to see if we are passing to/from
11399 a system generic type (WORD, BYTE, ADDRESS)
11400 and if so emit a cast. It returns the number of
11401 open parenthesis.
11402 *)
11403
11404 PROCEDURE checkSystemCast (p: pretty; actual, formal: node) : CARDINAL ;
11405 VAR
11406 at, ft: node ;
11407 BEGIN
11408 at := getExprType (actual) ;
11409 ft := getType (formal) ;
11410 IF needsCast (at, ft)
11411 THEN
11412 IF lang = ansiCP
11413 THEN
11414 IF isString (actual) AND isCDataType (skipType (ft))
11415 THEN
11416 (* Nothing to do. *)
11417 RETURN 0
11418 ELSIF isString (actual) AND (skipType (ft) = addressN)
11419 THEN
11420 outText (p, "const_cast<void*> (static_cast<const void*> (") ;
11421 RETURN 2
11422 ELSIF isPointer (skipType (ft)) OR (skipType (ft) = addressN) OR
11423 isCDataType (skipType (ft))
11424 THEN
11425 IF actual = nilN
11426 THEN
11427 IF isVarParam (formal)
11428 THEN
11429 metaError1 ('NIL is being passed to a VAR parameter {%1DMad}', formal)
11430 END ;
11431 (* NULL is compatible with pointers/address. *)
11432 RETURN 0
11433 ELSE
11434 RETURN castPointer (p, actual, formal, at, ft)
11435 END
11436 ELSE
11437 outText (p, 'static_cast<') ;
11438 doTypeNameC (p, ft) ;
11439 IF isVarParam (formal)
11440 THEN
11441 outText (p, '*')
11442 END ;
11443 noSpace (p) ;
11444 outText (p, '> (')
11445 END ;
11446 RETURN 1
11447 ELSE
11448 outText (p, '(') ;
11449 doTypeNameC (p, ft) ;
11450 IF isVarParam (formal)
11451 THEN
11452 outText (p, '*')
11453 END ;
11454 noSpace (p) ;
11455 outText (p, ')') ;
11456 setNeedSpace (p)
11457 END
11458 END ;
11459 RETURN 0
11460 END checkSystemCast ;
11461
11462
11463 (*
11464 emitN -
11465 *)
11466
11467 PROCEDURE emitN (p: pretty; a: ARRAY OF CHAR; n: CARDINAL) ;
11468 BEGIN
11469 WHILE n>0 DO
11470 outText (p, a) ;
11471 DEC (n)
11472 END
11473 END emitN ;
11474
11475
11476 (*
11477 isForC - return true if node n is a varparam, param or procedure
11478 which was declared inside a definition module for "C".
11479 *)
11480
11481 PROCEDURE isForC (n: node) : BOOLEAN ;
11482 BEGIN
11483 IF isVarParam (n)
11484 THEN
11485 RETURN n^.varparamF.isForC
11486 ELSIF isParam (n)
11487 THEN
11488 RETURN n^.paramF.isForC
11489 ELSIF isProcedure (n)
11490 THEN
11491 RETURN n^.procedureF.isForC
11492 END ;
11493 RETURN FALSE
11494 END isForC ;
11495
11496
11497 (*
11498 isDefForCNode - return TRUE if node n was declared inside a definition module for "C".
11499 *)
11500
11501 PROCEDURE isDefForCNode (n: node) : BOOLEAN ;
11502 VAR
11503 name: Name ;
11504 BEGIN
11505 WHILE (n # NIL) AND (NOT (isImp (n) OR isDef (n) OR isModule (n))) DO
11506 n := getScope (n)
11507 END ;
11508 IF (n # NIL) AND isImp (n)
11509 THEN
11510 name := getSymName (n) ;
11511 n := lookupDef (name) ;
11512 END ;
11513 RETURN (n # NIL) AND isDef (n) AND isDefForC (n)
11514 END isDefForCNode ;
11515
11516
11517 (*
11518 doFuncVarParam - detect whether the formal uses an opaque and ensure that the address of
11519 the actual parameter is cast to the formal type.
11520 *)
11521
11522 PROCEDURE doFuncVarParam (p: pretty; actual, formal: node) ;
11523 VAR
11524 type: node ;
11525 BEGIN
11526 IF nodeUsesOpaque (formal) AND
11527 getNodeOpaqueFlushNecessary (actual, getNodeOpaqueVoidStar (formal))
11528 THEN
11529 type := getType (formal) ;
11530 outText (p, 'reinterpret_cast<') ;
11531 IF getNodeOpaqueVoidStar (formal)
11532 THEN
11533 doTypeNameC (p, type) ;
11534 setNeedSpace (p) ;
11535 outText (p, '*> (&') ;
11536 doExprC (p, actual) ;
11537 outText (p, ')') ;
11538 actual := makeOpaqueCast (actual, TRUE)
11539 ELSE
11540 doTypeNameC (p, type) ;
11541 noSpace (p) ;
11542 outText (p, '__opaque *> (&') ;
11543 doExprC (p, actual) ;
11544 outText (p, ')') ;
11545 actual := makeOpaqueCast (actual, FALSE)
11546 END
11547 ELSE
11548 doAdrExprC (p, actual)
11549 END
11550 END doFuncVarParam ;
11551
11552
11553 (*
11554 doFuncParamC -
11555 *)
11556
11557 PROCEDURE doFuncParamC (p: pretty; actual, formal, func: node) ;
11558 VAR
11559 ft, at: node ;
11560 lbr : CARDINAL ;
11561 BEGIN
11562 IF formal = NIL
11563 THEN
11564 doExprC (p, actual)
11565 ELSE
11566 ft := skipType (getType (formal)) ;
11567 IF isUnbounded (ft)
11568 THEN
11569 doFuncUnbounded (p, actual, formal, ft, func)
11570 ELSE
11571 IF isAProcType (ft) AND isProcedure (actual)
11572 THEN
11573 IF isVarParam (formal)
11574 THEN
11575 metaError1 ('{%1MDad} cannot be passed as a VAR parameter', actual)
11576 ELSE
11577 doProcedureParamC (p, actual, formal)
11578 END
11579 ELSIF (getType (actual) # NIL) AND isProcType (skipType (getType (actual))) AND isAProcType (ft) AND isForC (formal)
11580 THEN
11581 IF isVarParam (formal)
11582 THEN
11583 metaError2 ('{%1MDad} cannot be passed as a VAR parameter to the definition for C module as the parameter requires a cast to the formal type {%2MDtad}',
11584 actual, formal)
11585 ELSE
11586 outText (p, '(') ;
11587 doFQNameC (p, getType (formal)) ;
11588 outText (p, "_C") ;
11589 outText (p, ')') ;
11590 setNeedSpace (p) ;
11591 doExprC (p, actual) ;
11592 outText (p, ".proc")
11593 END
11594 ELSIF (getType (actual) # NIL) AND isProcType (skipType (getType (actual))) AND (getType (actual) # getType (formal))
11595 THEN
11596 IF isVarParam (formal)
11597 THEN
11598 metaError2 ('{%1MDad} cannot be passed as a VAR parameter as the parameter requires a cast to the formal type {%2MDtad}',
11599 actual, formal)
11600 ELSE
11601 doCastC (p, getType (formal), actual)
11602 END
11603 ELSE
11604 IF isVarParam (formal)
11605 THEN
11606 lbr := checkSystemCast (p, actual, formal) ;
11607 doFuncVarParam (p, actual, formal) ;
11608 emitN (p, ")", lbr)
11609 ELSE
11610 IF nodeUsesOpaque (formal)
11611 THEN
11612 forceCastOpaque (p, formal, actual,
11613 getNodeOpaqueVoidStar (formal))
11614 ELSE
11615 lbr := checkSystemCast (p, actual, formal) ;
11616 doExprC (p, actual) ;
11617 emitN (p, ")", lbr)
11618 END
11619 END
11620 END
11621 END
11622 END
11623 END doFuncParamC ;
11624
11625
11626 (*
11627 getNthParamType - return the type of parameter, i, in list, l.
11628 If the parameter is a vararg NIL is returned.
11629 *)
11630
11631 PROCEDURE getNthParamType (l: Index; i: CARDINAL) : node ;
11632 VAR
11633 p: node ;
11634 BEGIN
11635 p := getNthParam (l, i) ;
11636 IF p # NIL
11637 THEN
11638 RETURN getType (p)
11639 END ;
11640 RETURN NIL
11641 END getNthParamType ;
11642
11643
11644 (*
11645 getNthParam - return the parameter, i, in list, l.
11646 If the parameter is a vararg NIL is returned.
11647 *)
11648
11649 PROCEDURE getNthParam (l: Index; i: CARDINAL) : node ;
11650 VAR
11651 p : node ;
11652 j, k, h: CARDINAL ;
11653 BEGIN
11654 IF l # NIL
11655 THEN
11656 j := LowIndice (l) ;
11657 h := HighIndice (l) ;
11658 WHILE j <= h DO
11659 p := GetIndice (l, j) ;
11660 IF isParam (p)
11661 THEN
11662 k := identListLen (p^.paramF.namelist)
11663 ELSIF isVarParam (p)
11664 THEN
11665 k := identListLen (p^.varparamF.namelist)
11666 ELSE
11667 assert (isVarargs (p)) ;
11668 RETURN NIL
11669 END ;
11670 IF i <= k
11671 THEN
11672 RETURN p
11673 ELSE
11674 DEC (i, k) ;
11675 INC (j)
11676 END
11677 END
11678 END ;
11679 RETURN NIL
11680 END getNthParam ;
11681
11682
11683 (*
11684 doFuncArgsC -
11685 *)
11686
11687 PROCEDURE doFuncArgsC (p: pretty; s: node; l: Index; needParen: BOOLEAN) ;
11688 VAR
11689 actual, formal: node ;
11690 i, n : CARDINAL ;
11691 BEGIN
11692 IF needParen
11693 THEN
11694 outText (p, "(")
11695 END ;
11696 IF s^.funccallF.args # NIL
11697 THEN
11698 i := 1 ;
11699 n := expListLen (s^.funccallF.args) ;
11700 WHILE i<=n DO
11701 actual := getExpList (s^.funccallF.args, i) ;
11702 formal := getNthParam (l, i) ;
11703 doFuncParamC (p, actual, formal, s^.funccallF.function) ;
11704 IF i<n
11705 THEN
11706 outText (p, ",") ;
11707 setNeedSpace (p)
11708 END ;
11709 INC (i)
11710 END
11711 END ;
11712 IF needParen
11713 THEN
11714 noSpace (p) ;
11715 outText (p, ")")
11716 END
11717 END doFuncArgsC ;
11718
11719
11720 (*
11721 doProcTypeArgsC -
11722 *)
11723
11724 PROCEDURE doProcTypeArgsC (p: pretty; s: node; args: Index; needParen: BOOLEAN) ;
11725 VAR
11726 a, b: node ;
11727 i, n: CARDINAL ;
11728 BEGIN
11729 IF needParen
11730 THEN
11731 outText (p, "(")
11732 END ;
11733 IF s^.funccallF.args # NIL
11734 THEN
11735 i := 1 ;
11736 n := expListLen (s^.funccallF.args) ;
11737 WHILE i<=n DO
11738 a := getExpList (s^.funccallF.args, i) ;
11739 b := GetIndice (args, i) ;
11740 doFuncParamC (p, a, b, s^.funccallF.function) ;
11741 IF i<n
11742 THEN
11743 outText (p, ",") ;
11744 setNeedSpace (p)
11745 END ;
11746 INC (i)
11747 END
11748 END ;
11749 IF needParen
11750 THEN
11751 noSpace (p) ;
11752 outText (p, ")")
11753 END
11754 END doProcTypeArgsC ;
11755
11756
11757 (*
11758 doAdrArgC -
11759 *)
11760
11761 PROCEDURE doAdrArgC (p: pretty; n: node) ;
11762 BEGIN
11763 IF isDeref (n)
11764 THEN
11765 (* & and * cancel each other out. *)
11766 doExprC (p, n^.unaryF.arg)
11767 ELSIF isVar (n) AND (n^.varF.isVarParameter)
11768 THEN
11769 (* & and * cancel each other out. *)
11770 outTextN (p, getSymName (n)) (* --fixme-- does the caller need to cast it? *)
11771 ELSIF isString (n) OR (isArray (getType (n)) AND isUnbounded (getType (n)))
11772 THEN
11773 IF lang = ansiCP
11774 THEN
11775 outText (p, "const_cast<void*> (static_cast<const void*>") ;
11776 outText (p, "(") ;
11777 doExprC (p, n) ;
11778 outText (p, "))")
11779 ELSE
11780 doExprC (p, n)
11781 END
11782 ELSE
11783 outText (p, "&") ;
11784 doExprC (p, n)
11785 END
11786 END doAdrArgC ;
11787
11788
11789 (*
11790 doAdrC -
11791 *)
11792
11793 PROCEDURE doAdrC (p: pretty; n: node) ;
11794 BEGIN
11795 assert (isUnary (n)) ;
11796 doAdrArgC (p, n^.unaryF.arg)
11797 END doAdrC ;
11798
11799
11800 (*
11801 doInc -
11802 *)
11803
11804 PROCEDURE doInc (p: pretty; n: node) ;
11805 BEGIN
11806 assert (isIntrinsic (n)) ;
11807 IF lang = ansiCP
11808 THEN
11809 doIncDecCP (p, n, "+")
11810 ELSE
11811 doIncDecC (p, n, "+=")
11812 END
11813 END doInc ;
11814
11815
11816 (*
11817 doDec -
11818 *)
11819
11820 PROCEDURE doDec (p: pretty; n: node) ;
11821 BEGIN
11822 assert (isIntrinsic (n)) ;
11823 IF lang = ansiCP
11824 THEN
11825 doIncDecCP (p, n, "-")
11826 ELSE
11827 doIncDecC (p, n, "-=")
11828 END
11829 END doDec ;
11830
11831
11832 (*
11833 doIncDecC -
11834 *)
11835
11836 PROCEDURE doIncDecC (p: pretty; n: node; op: ARRAY OF CHAR) ;
11837 BEGIN
11838 assert (isIntrinsic (n)) ;
11839 IF n^.intrinsicF.args # NIL
11840 THEN
11841 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
11842 setNeedSpace (p) ;
11843 outText (p, op) ;
11844 setNeedSpace (p) ;
11845 IF expListLen (n^.intrinsicF.args) = 1
11846 THEN
11847 outText (p, '1')
11848 ELSE
11849 doExprC (p, getExpList (n^.intrinsicF.args, 2))
11850 END
11851 END
11852 END doIncDecC ;
11853
11854
11855 (*
11856 doIncDecCP -
11857 *)
11858
11859 PROCEDURE doIncDecCP (p: pretty; n: node; op: ARRAY OF CHAR) ;
11860 VAR
11861 lhs,
11862 type: node ;
11863 BEGIN
11864 assert (isIntrinsic (n)) ;
11865 IF n^.intrinsicF.args # NIL
11866 THEN
11867 lhs := getExpList (n^.intrinsicF.args, 1) ;
11868 doExprC (p, lhs) ;
11869 setNeedSpace (p) ;
11870 type := getType (lhs) ;
11871 IF isPointer (type) OR (type = addressN)
11872 THEN
11873 (* cast to (char * ) and then back again after the arithmetic is complete. *)
11874 outText (p, "=") ;
11875 setNeedSpace (p) ;
11876 outText (p, 'reinterpret_cast<') ;
11877 doTypeNameC (p, type) ;
11878 noSpace (p) ;
11879 outText (p, '> (reinterpret_cast<char *> (') ;
11880 doExprC (p, lhs) ;
11881 noSpace (p) ;
11882 outText (p, ')') ;
11883 outText (p, op) ;
11884 IF expListLen (n^.intrinsicF.args) = 1
11885 THEN
11886 outText (p, '1')
11887 ELSE
11888 doExprC (p, getExpList (n^.intrinsicF.args, 2))
11889 END ;
11890 outText (p, ')')
11891 ELSIF isEnumeration (skipType (type))
11892 THEN
11893 outText (p, "= static_cast<") ;
11894 doTypeNameC (p, type) ;
11895 noSpace (p) ;
11896 outText (p, ">(static_cast<int>(") ;
11897 doExprC (p, lhs) ;
11898 outText (p, ")") ;
11899 outText (p, op) ;
11900 IF expListLen (n^.intrinsicF.args) = 1
11901 THEN
11902 outText (p, '1')
11903 ELSE
11904 doExprC (p, getExpList (n^.intrinsicF.args, 2))
11905 END ;
11906 outText (p, ")")
11907 ELSE
11908 outText (p, op) ;
11909 outText (p, "=") ;
11910 setNeedSpace (p) ;
11911 IF expListLen (n^.intrinsicF.args) = 1
11912 THEN
11913 outText (p, '1')
11914 ELSE
11915 doExprC (p, getExpList (n^.intrinsicF.args, 2))
11916 END
11917 END
11918 END
11919 END doIncDecCP ;
11920
11921
11922 (*
11923 doInclC -
11924 *)
11925
11926 PROCEDURE doInclC (p: pretty; n: node) ;
11927 VAR
11928 lo: node ;
11929 BEGIN
11930 assert (isIntrinsic (n)) ;
11931 IF n^.intrinsicF.args # NIL
11932 THEN
11933 IF expListLen (n^.intrinsicF.args) = 2
11934 THEN
11935 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
11936 lo := getSetLow (getExpList (n^.intrinsicF.args, 1)) ;
11937 setNeedSpace (p) ;
11938 outText (p, '|=') ;
11939 setNeedSpace (p) ;
11940 outText (p, '(1') ;
11941 setNeedSpace (p) ;
11942 outText (p, '<<') ;
11943 setNeedSpace (p) ;
11944 outText (p, '(') ;
11945 doExprC (p, getExpList (n^.intrinsicF.args, 2)) ;
11946 doSubtractC (p, lo) ;
11947 setNeedSpace (p) ;
11948 outText (p, '))')
11949 ELSE
11950 HALT (* metaError0 ('expecting two parameters to INCL') *)
11951 END
11952 END
11953 END doInclC ;
11954
11955
11956 (*
11957 doExclC -
11958 *)
11959
11960 PROCEDURE doExclC (p: pretty; n: node) ;
11961 VAR
11962 lo: node ;
11963 BEGIN
11964 assert (isIntrinsic (n)) ;
11965 IF n^.intrinsicF.args # NIL
11966 THEN
11967 IF expListLen (n^.intrinsicF.args) = 2
11968 THEN
11969 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
11970 lo := getSetLow (getExpList (n^.intrinsicF.args, 1)) ;
11971 setNeedSpace (p) ;
11972 outText (p, '&=') ;
11973 setNeedSpace (p) ;
11974 outText (p, '(~(1') ;
11975 setNeedSpace (p) ;
11976 outText (p, '<<') ;
11977 setNeedSpace (p) ;
11978 outText (p, '(') ;
11979 doExprC (p, getExpList (n^.intrinsicF.args, 2)) ;
11980 doSubtractC (p, lo) ;
11981 setNeedSpace (p) ;
11982 outText (p, ')))')
11983 ELSE
11984 HALT (* metaError0 ('expecting two parameters to EXCL') *)
11985 END
11986 END
11987 END doExclC ;
11988
11989
11990 (*
11991 doNewC -
11992 *)
11993
11994 PROCEDURE doNewC (p: pretty; n: node) ;
11995 VAR
11996 t: node ;
11997 BEGIN
11998 assert (isIntrinsic (n)) ;
11999 IF n^.intrinsicF.args = NIL
12000 THEN
12001 HALT
12002 ELSE
12003 IF expListLen (n^.intrinsicF.args) = 1
12004 THEN
12005 keyc.useStorage ;
12006 outText (p, 'Storage_ALLOCATE') ;
12007 setNeedSpace (p) ;
12008 outText (p, '((void **)') ;
12009 setNeedSpace (p) ;
12010 outText (p, '&') ;
12011 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
12012 outText (p, ',') ;
12013 setNeedSpace (p) ;
12014 t := skipType (getType (getExpList (n^.intrinsicF.args, 1))) ;
12015 IF isPointer (t)
12016 THEN
12017 t := getType (t) ;
12018 outText (p, 'sizeof') ;
12019 setNeedSpace (p) ;
12020 outText (p, '(') ;
12021 doTypeNameC (p, t) ;
12022 noSpace (p) ;
12023 outText (p, '))')
12024 ELSE
12025 metaError1 ('expecting a pointer type variable as the argument to NEW, rather than {%1ad}', t)
12026 END
12027 END
12028 END
12029 END doNewC ;
12030
12031
12032 (*
12033 doDisposeC -
12034 *)
12035
12036 PROCEDURE doDisposeC (p: pretty; n: node) ;
12037 VAR
12038 t: node ;
12039 BEGIN
12040 assert (isIntrinsic (n)) ;
12041 IF n^.intrinsicF.args = NIL
12042 THEN
12043 HALT
12044 ELSE
12045 IF expListLen (n^.intrinsicF.args) = 1
12046 THEN
12047 keyc.useStorage ;
12048 outText (p, 'Storage_DEALLOCATE') ;
12049 setNeedSpace (p) ;
12050 outText (p, '((void **)') ;
12051 setNeedSpace (p) ;
12052 outText (p, '&') ;
12053 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
12054 outText (p, ',') ;
12055 setNeedSpace (p) ;
12056 t := skipType (getType (getExpList (n^.intrinsicF.args, 1))) ;
12057 IF isPointer (t)
12058 THEN
12059 t := getType (t) ;
12060 outText (p, 'sizeof') ;
12061 setNeedSpace (p) ;
12062 outText (p, '(') ;
12063 doTypeNameC (p, t) ;
12064 noSpace (p) ;
12065 outText (p, '))')
12066 ELSE
12067 metaError1 ('expecting a pointer type variable as the argument to DISPOSE, rather than {%1ad}', t)
12068 END
12069 ELSE
12070 HALT (* metaError0 ('expecting a single parameter to DISPOSE') *)
12071 END
12072 END
12073 END doDisposeC ;
12074
12075
12076 (*
12077 doCapC -
12078 *)
12079
12080 PROCEDURE doCapC (p: pretty; n: node) ;
12081 BEGIN
12082 assert (isUnary (n)) ;
12083 IF n^.unaryF.arg = NIL
12084 THEN
12085 HALT (* metaError0 ('expecting a single parameter to CAP') *)
12086 ELSE
12087 keyc.useCtype ;
12088 IF getGccConfigSystem ()
12089 THEN
12090 outText (p, 'TOUPPER')
12091 ELSE
12092 outText (p, 'toupper')
12093 END ;
12094 setNeedSpace (p) ;
12095 outText (p, '(') ;
12096 doExprC (p, n^.unaryF.arg) ;
12097 outText (p, ')')
12098 END
12099 END doCapC ;
12100
12101
12102 (*
12103 doLengthC -
12104 *)
12105
12106 PROCEDURE doLengthC (p: pretty; n: node) ;
12107 BEGIN
12108 assert (isUnary (n)) ;
12109 IF n^.unaryF.arg = NIL
12110 THEN
12111 HALT (* metaError0 ('expecting a single parameter to LENGTH') *)
12112 ELSE
12113 keyc.useM2RTS ;
12114 outText (p, 'M2RTS_Length') ;
12115 setNeedSpace (p) ;
12116 outText (p, '(') ;
12117 doExprC (p, n^.unaryF.arg) ;
12118 outText (p, ',') ;
12119 setNeedSpace (p) ;
12120 doFuncHighC (p, n^.unaryF.arg) ;
12121 outText (p, ')')
12122 END
12123 END doLengthC ;
12124
12125
12126 (*
12127 doAbsC -
12128 *)
12129
12130 PROCEDURE doAbsC (p: pretty; n: node) ;
12131 VAR
12132 t: node ;
12133 BEGIN
12134 assert (isUnary (n)) ;
12135 IF n^.unaryF.arg = NIL
12136 THEN
12137 HALT
12138 ELSE
12139 t := getExprType (n)
12140 END ;
12141 IF t = longintN
12142 THEN
12143 keyc.useLabs ;
12144 outText (p, "labs")
12145 ELSIF t = integerN
12146 THEN
12147 keyc.useAbs ;
12148 outText (p, "abs")
12149 ELSIF t = realN
12150 THEN
12151 keyc.useFabs ;
12152 outText (p, "fabs")
12153 ELSIF t = longrealN
12154 THEN
12155 keyc.useFabsl ;
12156 outText (p, "fabsl")
12157 ELSIF t = cardinalN
12158 THEN
12159 (* do nothing. *)
12160 ELSE
12161 HALT
12162 END ;
12163 setNeedSpace (p) ;
12164 outText (p, "(") ;
12165 doExprC (p, n^.unaryF.arg) ;
12166 outText (p, ")")
12167 END doAbsC ;
12168
12169
12170 (*
12171 doValC -
12172 *)
12173
12174 PROCEDURE doValC (p: pretty; n: node) ;
12175 BEGIN
12176 assert (isBinary (n)) ;
12177 outText (p, '(') ;
12178 doTypeNameC (p, n^.binaryF.left) ;
12179 outText (p, ')') ;
12180 setNeedSpace (p) ;
12181 outText (p, '(') ;
12182 doExprC (p, n^.binaryF.right) ;
12183 outText (p, ')')
12184 END doValC ;
12185
12186
12187 (*
12188 doMinC -
12189 *)
12190
12191 PROCEDURE doMinC (p: pretty; n: node) ;
12192 VAR
12193 t: node ;
12194 BEGIN
12195 assert (isUnary (n)) ;
12196 t := getExprType (n^.unaryF.arg) ;
12197 doExprC (p, getMin (t)) ;
12198 END doMinC ;
12199
12200
12201 (*
12202 doMaxC -
12203 *)
12204
12205 PROCEDURE doMaxC (p: pretty; n: node) ;
12206 VAR
12207 t: node ;
12208 BEGIN
12209 assert (isUnary (n)) ;
12210 t := getExprType (n^.unaryF.arg) ;
12211 doExprC (p, getMax (t)) ;
12212 END doMaxC ;
12213
12214
12215 (*
12216 isIntrinsic - returns if, n, is an intrinsic procedure.
12217 The intrinsic functions are represented as unary and binary nodes.
12218 *)
12219
12220 PROCEDURE isIntrinsic (n: node) : BOOLEAN ;
12221 BEGIN
12222 CASE n^.kind OF
12223
12224 unreachable,
12225 throw,
12226 inc,
12227 dec,
12228 incl,
12229 excl,
12230 new,
12231 dispose,
12232 halt : RETURN TRUE
12233
12234 ELSE
12235 RETURN FALSE
12236 END
12237 END isIntrinsic ;
12238
12239
12240 (*
12241 doHalt -
12242 *)
12243
12244 PROCEDURE doHalt (p: pretty; n: node) ;
12245 BEGIN
12246 assert (n^.kind = halt) ;
12247 IF (n^.intrinsicF.args = NIL) OR (expListLen (n^.intrinsicF.args) = 0)
12248 THEN
12249 outText (p, 'M2RTS_HALT') ;
12250 setNeedSpace (p) ;
12251 outText (p, '(-1)')
12252 ELSIF expListLen (n^.intrinsicF.args) = 1
12253 THEN
12254 outText (p, 'M2RTS_HALT') ;
12255 setNeedSpace (p) ;
12256 outText (p, '(') ;
12257 doExprC (p, getExpList (n^.intrinsicF.args, 1)) ;
12258 outText (p, ')')
12259 END
12260 END doHalt ;
12261
12262
12263 (*
12264 doCreal - emit the appropriate creal function.
12265 *)
12266
12267 PROCEDURE doCreal (p: pretty; t: node) ;
12268 BEGIN
12269 CASE t^.kind OF
12270
12271 complex : keyc.useComplex ;
12272 outText (p, "creal") |
12273 longcomplex : keyc.useComplex ;
12274 outText (p, "creall") |
12275 shortcomplex: keyc.useComplex ;
12276 outText (p, "crealf")
12277
12278 END
12279 END doCreal ;
12280
12281
12282 (*
12283 doCimag - emit the appropriate cimag function.
12284 *)
12285
12286 PROCEDURE doCimag (p: pretty; t: node) ;
12287 BEGIN
12288 CASE t^.kind OF
12289
12290 complex : keyc.useComplex ;
12291 outText (p, "cimag") |
12292 longcomplex : keyc.useComplex ;
12293 outText (p, "cimagl") |
12294 shortcomplex: keyc.useComplex ;
12295 outText (p, "cimagf")
12296
12297 END
12298 END doCimag ;
12299
12300
12301 (*
12302 doReC -
12303 *)
12304
12305 PROCEDURE doReC (p: pretty; n: node) ;
12306 VAR
12307 t: node ;
12308 BEGIN
12309 assert (n^.kind = re) ;
12310 IF n^.unaryF.arg # NIL
12311 THEN
12312 t := getExprType (n^.unaryF.arg)
12313 ELSE
12314 HALT
12315 END ;
12316 doCreal (p, t) ;
12317 setNeedSpace (p) ;
12318 outText (p, '(') ;
12319 doExprC (p, n^.unaryF.arg) ;
12320 outText (p, ')')
12321 END doReC ;
12322
12323
12324 (*
12325 doImC -
12326 *)
12327
12328 PROCEDURE doImC (p: pretty; n: node) ;
12329 VAR
12330 t: node ;
12331 BEGIN
12332 assert (n^.kind = im) ;
12333 IF n^.unaryF.arg # NIL
12334 THEN
12335 t := getExprType (n^.unaryF.arg)
12336 ELSE
12337 HALT
12338 END ;
12339 doCimag (p, t) ;
12340 setNeedSpace (p) ;
12341 outText (p, '(') ;
12342 doExprC (p, n^.unaryF.arg) ;
12343 outText (p, ')')
12344 END doImC ;
12345
12346
12347 (*
12348 doCmplx -
12349 *)
12350
12351 PROCEDURE doCmplx (p: pretty; n: node) ;
12352 BEGIN
12353 assert (isBinary (n)) ;
12354 keyc.useComplex ;
12355 setNeedSpace (p) ;
12356 outText (p, '(') ;
12357 doExprC (p, n^.binaryF.left) ;
12358 outText (p, ')') ;
12359 setNeedSpace (p) ;
12360 outText (p, '+') ;
12361 setNeedSpace (p) ;
12362 outText (p, '(') ;
12363 doExprC (p, n^.binaryF.right) ;
12364 setNeedSpace (p) ;
12365 outText (p, '*') ;
12366 setNeedSpace (p) ;
12367 outText (p, 'I') ;
12368 outText (p, ')')
12369 END doCmplx ;
12370
12371
12372 (*
12373 doIntrinsicC -
12374 *)
12375
12376 PROCEDURE doIntrinsicC (p: pretty; n: node) ;
12377 BEGIN
12378 assert (isIntrinsic (n)) ;
12379 doCommentC (p, n^.intrinsicF.intrinsicComment.body) ;
12380 CASE n^.kind OF
12381
12382 unreachable: doUnreachableC (p, n) |
12383 throw : doThrowC (p, n) |
12384 halt : doHalt (p, n) |
12385 inc : doInc (p, n) |
12386 dec : doDec (p, n) |
12387 incl : doInclC (p, n) |
12388 excl : doExclC (p, n) |
12389 new : doNewC (p, n) |
12390 dispose : doDisposeC (p, n)
12391
12392 END ;
12393 outText (p, ";") ;
12394 doAfterCommentC (p, n^.intrinsicF.intrinsicComment.after)
12395 END doIntrinsicC ;
12396
12397
12398 (*
12399 isIntrinsicFunction - returns true if, n, is an instrinsic function.
12400 *)
12401
12402 PROCEDURE isIntrinsicFunction (n: node) : BOOLEAN ;
12403 BEGIN
12404 CASE n^.kind OF
12405
12406 val,
12407 adr,
12408 size,
12409 tsize,
12410 float,
12411 trunc,
12412 ord,
12413 chr,
12414 cap,
12415 abs,
12416 high,
12417 length,
12418 min,
12419 max,
12420 re,
12421 im,
12422 cmplx: RETURN TRUE
12423
12424 ELSE
12425 RETURN FALSE
12426 END
12427 END isIntrinsicFunction ;
12428
12429
12430 (*
12431 doSizeC -
12432 *)
12433
12434 PROCEDURE doSizeC (p: pretty; n: node) ;
12435 BEGIN
12436 assert (isUnary (n)) ;
12437 outText (p, "sizeof (") ;
12438 doExprC (p, n^.unaryF.arg) ;
12439 outText (p, ")")
12440 END doSizeC ;
12441
12442
12443 (*
12444 doConvertC -
12445 *)
12446
12447 PROCEDURE doConvertC (p: pretty; n: node; conversion: ARRAY OF CHAR) ;
12448 VAR
12449 s: String ;
12450 BEGIN
12451 s := InitString (conversion) ;
12452 doConvertSC (p, n, s) ;
12453 s := KillString (s)
12454 END doConvertC ;
12455
12456
12457 (*
12458 doConvertSC -
12459 *)
12460
12461 PROCEDURE doConvertSC (p: pretty; n: node; conversion: String) ;
12462 BEGIN
12463 assert (isUnary (n)) ;
12464 setNeedSpace (p) ;
12465 outText (p, "((") ;
12466 outTextS (p, conversion) ;
12467 outText (p, ")") ;
12468 setNeedSpace (p) ;
12469 outText (p, "(") ;
12470 doExprC (p, n^.unaryF.arg) ;
12471 outText (p, "))")
12472 END doConvertSC ;
12473
12474
12475 (* not needed?
12476 val: doValC (p, n) |
12477 adr: doAdrC (p, n) |
12478 size,
12479 tsize: doSizeC (p, n) |
12480 float: doConvertC (p, n, "(double)") |
12481 trunc: doConvertC (p, n, "(int)") |
12482 ord: doConvertC (p, n, "(unsigned int)") |
12483 chr: doConvertC (p, n, "(char)") |
12484 cap: doCapC (p, n) |
12485 abs: doAbsC (p, n) |
12486 high: doFuncHighC (p, n^.unaryF.arg, 1)) |
12487 length: doLengthC (p, n) |
12488 min: doMinC (p, n) |
12489 max: doMaxC (p, n) |
12490 throw: doThrowC (p, n) |
12491 re: doReC (p, n) |
12492 im: doImC (p, n) |
12493 cmplx: doCmplx (p, n)
12494 *)
12495
12496
12497 (*
12498 getFunction - return the function associate with funccall node n.
12499 *)
12500
12501 PROCEDURE getFunction (n: node) : node ;
12502 BEGIN
12503 assert (isFuncCall (n)) ;
12504 CASE n^.kind OF
12505
12506 funccall: RETURN n^.funccallF.function
12507
12508 ELSE
12509 HALT
12510 END
12511 END getFunction ;
12512
12513
12514 (*
12515 getFuncFromExpr -
12516 *)
12517
12518 PROCEDURE getFuncFromExpr (n: node) : node ;
12519 BEGIN
12520 n := skipType (getType (n)) ;
12521 WHILE (n # procN) AND (NOT isProcType (n)) DO
12522 n := skipType (getType (n))
12523 END ;
12524 RETURN n
12525 END getFuncFromExpr ;
12526
12527
12528 (*
12529 doFuncExprC -
12530 *)
12531
12532 PROCEDURE doFuncExprC (p: pretty; n: node) ;
12533 VAR
12534 t: node ;
12535 BEGIN
12536 assert (isFuncCall (n)) ;
12537 IF isProcedure (n^.funccallF.function)
12538 THEN
12539 doFQDNameC (p, n^.funccallF.function, TRUE) ;
12540 setNeedSpace (p) ;
12541 doFuncArgsC (p, n, n^.funccallF.function^.procedureF.parameters, TRUE)
12542 ELSE
12543 outText (p, "(*") ;
12544 doExprC (p, n^.funccallF.function) ;
12545 outText (p, ".proc") ;
12546 outText (p, ")") ;
12547 t := getFuncFromExpr (n^.funccallF.function) ;
12548 setNeedSpace (p) ;
12549 IF t = procN
12550 THEN
12551 doProcTypeArgsC (p, n, NIL, TRUE)
12552 ELSE
12553 assert (isProcType (t)) ;
12554 doProcTypeArgsC (p, n, t^.proctypeF.parameters, TRUE)
12555 END
12556 END
12557 END doFuncExprC ;
12558
12559
12560 (*
12561 doFuncCallC -
12562 *)
12563
12564 PROCEDURE doFuncCallC (p: pretty; n: node) ;
12565 BEGIN
12566 doCommentC (p, n^.funccallF.funccallComment.body) ;
12567 doFuncExprC (p, n) ;
12568 outText (p, ";") ;
12569 doAfterCommentC (p, n^.funccallF.funccallComment.after)
12570 END doFuncCallC ;
12571
12572
12573 (*
12574 doCaseStatementC -
12575 *)
12576
12577 PROCEDURE doCaseStatementC (p: pretty; n: node; needBreak: BOOLEAN) ;
12578 BEGIN
12579 p := pushPretty (p) ;
12580 setindent (p, getindent (p) + indentationC) ;
12581 doStatementSequenceC (p, n) ;
12582 IF needBreak
12583 THEN
12584 outText (p, "break;\n")
12585 END ;
12586 p := popPretty (p)
12587 END doCaseStatementC ;
12588
12589
12590 (*
12591 doExceptionC -
12592 *)
12593
12594 PROCEDURE doExceptionC (p: pretty; a: ARRAY OF CHAR; n: node) ;
12595 VAR
12596 w: CARDINAL ;
12597 BEGIN
12598 w := getDeclaredMod (n) ;
12599 outText (p, a) ;
12600 setNeedSpace (p) ;
12601 outText (p, '("') ;
12602 outTextS (p, findFileNameFromToken (w, 0)) ;
12603 outText (p, '",') ;
12604 setNeedSpace (p) ;
12605 outCard (p, tokenToLineNo (w, 0)) ;
12606 outText (p, ',') ;
12607 setNeedSpace (p) ;
12608 outCard (p, tokenToColumnNo (w, 0)) ;
12609 outText (p, ');\n') ;
12610 outText (p, '__builtin_unreachable ();\n')
12611 END doExceptionC ;
12612
12613
12614 (*
12615 doExceptionCP -
12616 *)
12617
12618 PROCEDURE doExceptionCP (p: pretty; a: ARRAY OF CHAR; n: node) ;
12619 VAR
12620 w: CARDINAL ;
12621 BEGIN
12622 w := getDeclaredMod (n) ;
12623 outText (p, a) ;
12624 setNeedSpace (p) ;
12625 outText (p, '("') ;
12626 outTextS (p, findFileNameFromToken (w, 0)) ;
12627 outText (p, '",') ;
12628 setNeedSpace (p) ;
12629 outCard (p, tokenToLineNo (w, 0)) ;
12630 outText (p, ',') ;
12631 setNeedSpace (p) ;
12632 outCard (p, tokenToColumnNo (w, 0)) ;
12633 outText (p, ');\n') ;
12634 outText (p, '__builtin_unreachable ();\n')
12635 END doExceptionCP ;
12636
12637
12638 (*
12639 doException -
12640 *)
12641
12642 PROCEDURE doException (p: pretty; a: ARRAY OF CHAR; n: node) ;
12643 BEGIN
12644 keyc.useException ;
12645 IF lang = ansiCP
12646 THEN
12647 doExceptionCP (p, a, n)
12648 ELSE
12649 doExceptionC (p, a, n)
12650 END
12651 END doException ;
12652
12653
12654 (*
12655 doRangeListC -
12656 *)
12657
12658 PROCEDURE doRangeListC (p: pretty; c: node) ;
12659 VAR
12660 r : node ;
12661 i, h: CARDINAL ;
12662 BEGIN
12663 assert (isCaseList (c)) ;
12664 i := 1 ;
12665 h := HighIndice (c^.caselistF.rangePairs) ;
12666 WHILE i<=h DO
12667 r := GetIndice (c^.caselistF.rangePairs, i) ;
12668 assert ((r^.rangeF.hi = NIL) OR (r^.rangeF.lo = r^.rangeF.hi)) ;
12669 outText (p, "case") ;
12670 setNeedSpace (p) ;
12671 doExprC (p, r^.rangeF.lo) ;
12672 outText (p, ":\n") ;
12673 INC (i)
12674 END
12675 END doRangeListC ;
12676
12677
12678 (*
12679 doRangeIfListC -
12680 *)
12681
12682 PROCEDURE doRangeIfListC (p: pretty; e, c: node) ;
12683 VAR
12684 r : node ;
12685 i, h: CARDINAL ;
12686 BEGIN
12687 assert (isCaseList (c)) ;
12688 i := 1 ;
12689 h := HighIndice (c^.caselistF.rangePairs) ;
12690 WHILE i<=h DO
12691 r := GetIndice (c^.caselistF.rangePairs, i) ;
12692 IF (r^.rangeF.lo # r^.rangeF.hi) AND (r^.rangeF.hi # NIL)
12693 THEN
12694 outText (p, "((") ;
12695 doExprC (p, e) ;
12696 outText (p, ")") ;
12697 setNeedSpace (p) ;
12698 outText (p, ">=") ;
12699 setNeedSpace (p) ;
12700 doExprC (p, r^.rangeF.lo) ;
12701 outText (p, ")") ;
12702 setNeedSpace (p) ;
12703 outText (p, "&&") ;
12704 setNeedSpace (p) ;
12705 outText (p, "((") ;
12706 doExprC (p, e) ;
12707 outText (p, ")") ;
12708 setNeedSpace (p) ;
12709 outText (p, "<=") ;
12710 setNeedSpace (p) ;
12711 doExprC (p, r^.rangeF.hi) ;
12712 outText (p, ")")
12713 ELSE
12714 outText (p, "((") ;
12715 doExprC (p, e) ;
12716 outText (p, ")") ;
12717 setNeedSpace (p) ;
12718 outText (p, "==") ;
12719 setNeedSpace (p) ;
12720 doExprC (p, r^.rangeF.lo) ;
12721 outText (p, ")")
12722 END ;
12723 IF i<h
12724 THEN
12725 setNeedSpace (p) ;
12726 outText (p, "||") ;
12727 setNeedSpace (p)
12728 END ;
12729 INC (i)
12730 END
12731 END doRangeIfListC ;
12732
12733
12734 (*
12735 doCaseLabels -
12736 *)
12737
12738 PROCEDURE doCaseLabels (p: pretty; n: node; needBreak: BOOLEAN) ;
12739 BEGIN
12740 assert (isCaseLabelList (n)) ;
12741 doRangeListC (p, n^.caselabellistF.caseList) ;
12742 p := pushPretty (p) ;
12743 setindent (p, getindent (p) + indentationC) ;
12744 doStatementSequenceC (p, n^.caselabellistF.statements) ;
12745 IF needBreak
12746 THEN
12747 outText (p, "break;\n\n")
12748 END ;
12749 p := popPretty (p)
12750 END doCaseLabels ;
12751
12752
12753 (*
12754 doCaseLabelListC -
12755 *)
12756
12757 PROCEDURE doCaseLabelListC (p: pretty; n: node; haveElse: BOOLEAN) ;
12758 VAR
12759 i, h: CARDINAL ;
12760 c : node ;
12761 BEGIN
12762 assert (isCase (n)) ;
12763 i := 1 ;
12764 h := HighIndice (n^.caseF.caseLabelList) ;
12765 WHILE i<=h DO
12766 c := GetIndice (n^.caseF.caseLabelList, i) ;
12767 doCaseLabels (p, c, (i<h) OR haveElse OR caseException) ;
12768 INC (i)
12769 END
12770 END doCaseLabelListC ;
12771
12772
12773 (*
12774 doCaseIfLabels -
12775 *)
12776
12777 PROCEDURE doCaseIfLabels (p: pretty; e, n: node;
12778 i, h: CARDINAL) ;
12779 BEGIN
12780 assert (isCaseLabelList (n)) ;
12781 IF i > 1
12782 THEN
12783 outText (p, "else") ;
12784 setNeedSpace (p) ;
12785 END ;
12786 outText (p, "if") ;
12787 setNeedSpace (p) ;
12788 outText (p, "(") ;
12789 doRangeIfListC (p, e, n^.caselabellistF.caseList) ;
12790 outText (p, ")\n") ;
12791 IF h = 1
12792 THEN
12793 doCompoundStmt (p, n^.caselabellistF.statements)
12794 ELSE
12795 outText (p, "{\n") ;
12796 doStatementSequenceC (p, n^.caselabellistF.statements) ;
12797 outText (p, "}\n")
12798 END
12799 END doCaseIfLabels ;
12800
12801
12802 (*
12803 doCaseIfLabelListC -
12804 *)
12805
12806 PROCEDURE doCaseIfLabelListC (p: pretty; n: node) ;
12807 VAR
12808 i, h: CARDINAL ;
12809 c : node ;
12810 BEGIN
12811 assert (isCase (n)) ;
12812 i := 1 ;
12813 h := HighIndice (n^.caseF.caseLabelList) ;
12814 WHILE i<=h DO
12815 c := GetIndice (n^.caseF.caseLabelList, i) ;
12816 doCaseIfLabels (p, n^.caseF.expression, c, i, h) ;
12817 INC (i)
12818 END
12819 END doCaseIfLabelListC ;
12820
12821
12822 (*
12823 doCaseElseC -
12824 *)
12825
12826 PROCEDURE doCaseElseC (p: pretty; n: node) ;
12827 BEGIN
12828 assert (isCase (n)) ;
12829 IF n^.caseF.else = NIL
12830 THEN
12831 IF caseException
12832 THEN
12833 outText (p, "\ndefault:\n") ;
12834 p := pushPretty (p) ;
12835 setindent (p, getindent (p) + indentationC) ;
12836 doException (p, 'CaseException', n) ;
12837 p := popPretty (p)
12838 END
12839 ELSE
12840 outText (p, "\ndefault:\n") ;
12841 doCaseStatementC (p, n^.caseF.else, TRUE)
12842 END
12843 END doCaseElseC ;
12844
12845
12846 (*
12847 doCaseIfElseC -
12848 *)
12849
12850 PROCEDURE doCaseIfElseC (p: pretty; n: node) ;
12851 BEGIN
12852 assert (isCase (n)) ;
12853 IF n^.caseF.else = NIL
12854 THEN
12855 IF TRUE
12856 THEN
12857 outText (p, "\n") ;
12858 outText (p, "else {\n") ;
12859 p := pushPretty (p) ;
12860 setindent (p, getindent (p) + indentationC) ;
12861 doException (p, 'CaseException', n) ;
12862 p := popPretty (p) ;
12863 outText (p, "}\n")
12864 END
12865 ELSE
12866 outText (p, "\n") ;
12867 outText (p, "else {\n") ;
12868 doCaseStatementC (p, n^.caseF.else, FALSE) ;
12869 outText (p, "}\n")
12870 END
12871 END doCaseIfElseC ;
12872
12873
12874 (*
12875 canUseSwitchCaseLabels - returns TRUE if all the case labels are
12876 single values and not ranges.
12877 *)
12878
12879 PROCEDURE canUseSwitchCaseLabels (n: node) : BOOLEAN ;
12880 VAR
12881 i, h: CARDINAL ;
12882 r, l: node ;
12883 BEGIN
12884 assert (isCaseLabelList (n)) ;
12885 l := n^.caselabellistF.caseList ;
12886 i := 1 ;
12887 h := HighIndice (l^.caselistF.rangePairs) ;
12888 WHILE i<=h DO
12889 r := GetIndice (l^.caselistF.rangePairs, i) ;
12890 IF (r^.rangeF.hi # NIL) AND (r^.rangeF.lo # r^.rangeF.hi)
12891 THEN
12892 RETURN FALSE
12893 END ;
12894 INC (i)
12895 END ;
12896 RETURN TRUE
12897 END canUseSwitchCaseLabels ;
12898
12899
12900 (*
12901 canUseSwitch - returns TRUE if the case statement can be implement
12902 by a switch statement. This will be TRUE if all case
12903 selectors are single values rather than ranges.
12904 *)
12905
12906 PROCEDURE canUseSwitch (n: node) : BOOLEAN ;
12907 VAR
12908 i, h: CARDINAL ;
12909 c : node ;
12910 BEGIN
12911 assert (isCase (n)) ;
12912 i := 1 ;
12913 h := HighIndice (n^.caseF.caseLabelList) ;
12914 WHILE i<=h DO
12915 c := GetIndice (n^.caseF.caseLabelList, i) ;
12916 IF NOT canUseSwitchCaseLabels (c)
12917 THEN
12918 RETURN FALSE
12919 END ;
12920 INC (i)
12921 END ;
12922 RETURN TRUE
12923 END canUseSwitch ;
12924
12925
12926 (*
12927 doCaseC -
12928 *)
12929
12930 PROCEDURE doCaseC (p: pretty; n: node) ;
12931 VAR
12932 i: CARDINAL ;
12933 BEGIN
12934 assert (isCase (n)) ;
12935 IF canUseSwitch (n)
12936 THEN
12937 i := getindent (p) ;
12938 outText (p, "switch") ;
12939 setNeedSpace (p) ;
12940 outText (p, "(") ;
12941 doExprC (p, n^.caseF.expression) ;
12942 p := pushPretty (p) ;
12943 outText (p, ")") ;
12944 setindent (p, i + indentationC) ;
12945 outText (p, "\n{\n") ;
12946 p := pushPretty (p) ;
12947 setindent (p, getindent (p) + indentationC) ;
12948 doCaseLabelListC (p, n, n^.caseF.else # NIL) ;
12949 doCaseElseC (p, n) ;
12950 p := popPretty (p) ;
12951 outText (p, "}\n") ;
12952 p := popPretty (p)
12953 ELSE
12954 doCaseIfLabelListC (p, n) ;
12955 doCaseIfElseC (p, n)
12956 END
12957 END doCaseC ;
12958
12959
12960 (*
12961 doLoopC -
12962 *)
12963
12964 PROCEDURE doLoopC (p: pretty; s: node) ;
12965 BEGIN
12966 assert (isLoop (s)) ;
12967 outText (p, 'for (;;)\n') ;
12968 outText (p, "{\n") ;
12969 p := pushPretty (p) ;
12970 setindent (p, getindent (p) + indentationC) ;
12971 doStatementSequenceC (p, s^.loopF.statements) ;
12972 p := popPretty (p) ;
12973 outText (p, "}\n")
12974 END doLoopC ;
12975
12976
12977 (*
12978 doExitC -
12979 *)
12980
12981 PROCEDURE doExitC (p: pretty; s: node) ;
12982 BEGIN
12983 assert (isExit (s)) ;
12984 outText (p, "/* exit. */\n")
12985 END doExitC ;
12986
12987
12988 (*
12989 doStatementsC -
12990 *)
12991
12992 PROCEDURE doStatementsC (p: pretty; s: node) ;
12993 BEGIN
12994 IF s = NIL
12995 THEN
12996 (* do nothing. *)
12997 ELSIF isStatementSequence (s)
12998 THEN
12999 doStatementSequenceC (p, s)
13000 ELSIF isComment (s)
13001 THEN
13002 doCommentC (p, s)
13003 ELSIF isExit (s)
13004 THEN
13005 doExitC (p, s)
13006 ELSIF isReturn (s)
13007 THEN
13008 doReturnC (p, s)
13009 ELSIF isAssignment (s)
13010 THEN
13011 doAssignmentC (p, s)
13012 ELSIF isIf (s)
13013 THEN
13014 doIfC (p, s)
13015 ELSIF isFor (s)
13016 THEN
13017 doForC (p, s)
13018 ELSIF isRepeat (s)
13019 THEN
13020 doRepeatC (p, s)
13021 ELSIF isWhile (s)
13022 THEN
13023 doWhileC (p, s)
13024 ELSIF isIntrinsic (s)
13025 THEN
13026 doIntrinsicC (p, s)
13027 ELSIF isFuncCall (s)
13028 THEN
13029 doFuncCallC (p, s)
13030 ELSIF isCase (s)
13031 THEN
13032 doCaseC (p, s)
13033 ELSIF isLoop (s)
13034 THEN
13035 doLoopC (p, s)
13036 ELSIF isExit (s)
13037 THEN
13038 doExitC (p, s)
13039 ELSE
13040 HALT (* need to handle another s^.kind. *)
13041 END
13042 END doStatementsC ;
13043
13044
13045 PROCEDURE localstop ;
13046 END localstop ;
13047
13048
13049 (*
13050 doLocalVarC -
13051 *)
13052
13053 PROCEDURE doLocalVarC (p: pretty; s: scopeT) ;
13054 BEGIN
13055 includeVarProcedure (s) ;
13056 debugLists ;
13057 topologicallyOut (doConstC, doTypesC, doVarC,
13058 outputPartial,
13059 doNone, doCompletePartialC, doNone)
13060 END doLocalVarC ;
13061
13062
13063 (*
13064 doLocalConstTypesC -
13065 *)
13066
13067 PROCEDURE doLocalConstTypesC (p: pretty; s: scopeT) ;
13068 BEGIN
13069 simplifyTypes (s) ;
13070 includeConstType (s) ;
13071
13072 doP := p ;
13073
13074 topologicallyOut (doConstC, doTypesC, doVarC,
13075 outputPartial,
13076 doNone, doCompletePartialC, doNone) ;
13077
13078 END doLocalConstTypesC ;
13079
13080
13081 (*
13082 addParamDone -
13083 *)
13084
13085 PROCEDURE addParamDone (n: node) ;
13086 BEGIN
13087 IF isVar (n) AND n^.varF.isParameter
13088 THEN
13089 addDone (n) ;
13090 addDone (getType (n))
13091 END
13092 END addParamDone ;
13093
13094
13095 (*
13096 includeParameters -
13097 *)
13098
13099 PROCEDURE includeParameters (n: node) ;
13100 BEGIN
13101 assert (isProcedure (n)) ;
13102 ForeachIndiceInIndexDo (n^.procedureF.decls.variables, addParamDone)
13103 END includeParameters ;
13104
13105
13106 (*
13107 isHalt -
13108 *)
13109
13110 PROCEDURE isHalt (n: node) : BOOLEAN ;
13111 BEGIN
13112 RETURN n^.kind = halt
13113 END isHalt ;
13114
13115
13116 (*
13117 isReturnOrHalt -
13118 *)
13119
13120 PROCEDURE isReturnOrHalt (n: node) : BOOLEAN ;
13121 BEGIN
13122 RETURN isHalt (n) OR isReturn (n)
13123 END isReturnOrHalt ;
13124
13125
13126 (*
13127 isLastStatementReturn -
13128 *)
13129
13130 PROCEDURE isLastStatementReturn (n: node) : BOOLEAN ;
13131 BEGIN
13132 RETURN isLastStatement (n, isReturnOrHalt)
13133 END isLastStatementReturn ;
13134
13135
13136 (*
13137 isLastStatementSequence -
13138 *)
13139
13140 PROCEDURE isLastStatementSequence (n: node; q: isNodeF) : BOOLEAN ;
13141 VAR
13142 h : CARDINAL ;
13143 BEGIN
13144 assert (isStatementSequence (n)) ;
13145 h := HighIndice (n^.stmtF.statements) ;
13146 IF h > 0
13147 THEN
13148 RETURN isLastStatement (GetIndice (n^.stmtF.statements, h), q)
13149 END ;
13150 RETURN FALSE
13151 END isLastStatementSequence ;
13152
13153
13154 (*
13155 isLastStatementIf -
13156 *)
13157
13158 PROCEDURE isLastStatementIf (n: node; q: isNodeF) : BOOLEAN ;
13159 VAR
13160 ret: BOOLEAN ;
13161 BEGIN
13162 assert (isIf (n)) ;
13163 ret := TRUE ;
13164 IF (n^.ifF.elsif # NIL) AND ret
13165 THEN
13166 ret := isLastStatement (n^.ifF.elsif, q)
13167 END ;
13168 IF (n^.ifF.then # NIL) AND ret
13169 THEN
13170 ret := isLastStatement (n^.ifF.then, q)
13171 END ;
13172 IF (n^.ifF.else # NIL) AND ret
13173 THEN
13174 ret := isLastStatement (n^.ifF.else, q)
13175 END ;
13176 RETURN ret
13177 END isLastStatementIf ;
13178
13179
13180 (*
13181 isLastStatementElsif -
13182 *)
13183
13184 PROCEDURE isLastStatementElsif (n: node; q: isNodeF) : BOOLEAN ;
13185 VAR
13186 ret: BOOLEAN ;
13187 BEGIN
13188 assert (isElsif (n)) ;
13189 ret := TRUE ;
13190 IF (n^.elsifF.elsif # NIL) AND ret
13191 THEN
13192 ret := isLastStatement (n^.elsifF.elsif, q)
13193 END ;
13194 IF (n^.elsifF.then # NIL) AND ret
13195 THEN
13196 ret := isLastStatement (n^.elsifF.then, q)
13197 END ;
13198 IF (n^.elsifF.else # NIL) AND ret
13199 THEN
13200 ret := isLastStatement (n^.elsifF.else, q)
13201 END ;
13202 RETURN ret
13203 END isLastStatementElsif ;
13204
13205
13206 (*
13207 isLastStatementCase -
13208 *)
13209
13210 PROCEDURE isLastStatementCase (n: node; q: isNodeF) : BOOLEAN ;
13211 VAR
13212 ret : BOOLEAN ;
13213 i, h: CARDINAL ;
13214 c : node ;
13215 BEGIN
13216 ret := TRUE ;
13217 assert (isCase (n)) ;
13218 i := 1 ;
13219 h := HighIndice (n^.caseF.caseLabelList) ;
13220 WHILE i<=h DO
13221 c := GetIndice (n^.caseF.caseLabelList, i) ;
13222 assert (isCaseLabelList (c)) ;
13223 ret := ret AND isLastStatement (c^.caselabellistF.statements, q) ;
13224 INC (i)
13225 END ;
13226 IF n^.caseF.else # NIL
13227 THEN
13228 ret := ret AND isLastStatement (n^.caseF.else, q)
13229 END ;
13230 RETURN ret
13231 END isLastStatementCase ;
13232
13233
13234 (*
13235 isLastStatement - returns TRUE if the last statement in, n, is, q.
13236 *)
13237
13238 PROCEDURE isLastStatement (n: node; q: isNodeF) : BOOLEAN ;
13239 VAR
13240 ret: BOOLEAN ;
13241 BEGIN
13242 IF n = NIL
13243 THEN
13244 RETURN FALSE
13245 ELSIF isStatementSequence (n)
13246 THEN
13247 RETURN isLastStatementSequence (n, q)
13248 ELSIF isProcedure (n)
13249 THEN
13250 assert (isProcedure (n)) ;
13251 RETURN isLastStatement (n^.procedureF.beginStatements, q)
13252 ELSIF isIf (n)
13253 THEN
13254 RETURN isLastStatementIf (n, q)
13255 ELSIF isElsif (n)
13256 THEN
13257 RETURN isLastStatementElsif (n, q)
13258 ELSIF isCase (n)
13259 THEN
13260 RETURN isLastStatementCase (n, q)
13261 ELSIF q (n)
13262 THEN
13263 RETURN TRUE
13264 END ;
13265 RETURN FALSE
13266 END isLastStatement ;
13267
13268
13269 (*
13270 doProcedureC -
13271 *)
13272
13273 PROCEDURE doProcedureC (n: node) ;
13274 VAR
13275 s: CARDINAL ;
13276 BEGIN
13277 outText (doP, "\n") ;
13278 includeParameters (n) ;
13279
13280 keyc.enterScope (n) ;
13281
13282 doProcedureHeadingC (n, FALSE) ;
13283 outText (doP, "\n") ;
13284 doP := outKc (doP, "{\n") ;
13285 s := getcurline (doP) ;
13286 doLocalConstTypesC (doP, n^.procedureF.decls) ;
13287 doLocalVarC (doP, n^.procedureF.decls) ;
13288 doUnboundedParamCopyC (doP, n) ;
13289
13290 IF s # getcurline (doP)
13291 THEN
13292 outText (doP, "\n")
13293 END ;
13294
13295 doStatementsC (doP, n^.procedureF.beginStatements) ;
13296 IF n^.procedureF.returnType # NIL
13297 THEN
13298 IF returnException
13299 THEN
13300 IF isLastStatementReturn (n)
13301 THEN
13302 outText (doP, "/* static analysis guarentees a RETURN statement will be used before here. */\n") ;
13303 outText (doP, "__builtin_unreachable ();\n") ;
13304 ELSE
13305 doException (doP, 'ReturnException', n)
13306 END
13307 END
13308 END ;
13309 doP := outKc (doP, "}\n") ;
13310 keyc.leaveScope (n)
13311 END doProcedureC ;
13312
13313
13314 (*
13315 outProceduresC -
13316 *)
13317
13318 PROCEDURE outProceduresC (p: pretty; s: scopeT) ;
13319 BEGIN
13320 doP := p ;
13321 IF debugDecl
13322 THEN
13323 printf ("seen %d procedures\n", HighIndice (s.procedures))
13324 END ;
13325
13326 ForeachIndiceInIndexDo (s.procedures, doProcedureC)
13327 END outProceduresC ;
13328
13329
13330 (*
13331 output -
13332 *)
13333
13334 PROCEDURE output (n: node; c, t, v: nodeProcedure) ;
13335 BEGIN
13336 IF isConst (n)
13337 THEN
13338 c (n)
13339 ELSIF isVar (n)
13340 THEN
13341 v (n)
13342 ELSE
13343 t (n)
13344 END
13345 END output ;
13346
13347
13348 (*
13349 allDependants -
13350 *)
13351
13352 PROCEDURE allDependants (n: node) : dependentState ;
13353 VAR
13354 l: alist ;
13355 s: dependentState ;
13356 BEGIN
13357 l := alists.initList () ;
13358 s := walkDependants (l, n) ;
13359 alists.killList (l) ;
13360 RETURN s
13361 END allDependants ;
13362
13363
13364 (*
13365 walkDependants -
13366 *)
13367
13368 PROCEDURE walkDependants (l: alist; n: node) : dependentState ;
13369 BEGIN
13370 IF (n=NIL) OR alists.isItemInList (globalGroup^.doneQ, n)
13371 THEN
13372 RETURN completed
13373 ELSIF alists.isItemInList (l, n)
13374 THEN
13375 RETURN recursive
13376 ELSE
13377 alists.includeItemIntoList (l, n) ;
13378 RETURN doDependants (l, n)
13379 END
13380 END walkDependants ;
13381
13382
13383 (*
13384 walkType -
13385 *)
13386
13387 PROCEDURE walkType (l: alist; n: node) : dependentState ;
13388 VAR
13389 t: node ;
13390 BEGIN
13391 t := getType (n) ;
13392 IF alists.isItemInList (globalGroup^.doneQ, t)
13393 THEN
13394 RETURN completed
13395 ELSIF alists.isItemInList (globalGroup^.partialQ, t)
13396 THEN
13397 RETURN blocked
13398 ELSE
13399 queueBlocked (t) ;
13400 RETURN blocked
13401 END
13402 END walkType ;
13403
13404
13405 (*
13406 db -
13407 *)
13408
13409 PROCEDURE db (a: ARRAY OF CHAR; n: node) ;
13410 BEGIN
13411 IF getDebugTopological ()
13412 THEN
13413 outText (doP, a) ;
13414 IF n#NIL
13415 THEN
13416 outTextS (doP, gen (n))
13417 END
13418 END
13419 END db ;
13420
13421
13422 (*
13423 dbt -
13424 *)
13425
13426 PROCEDURE dbt (a: ARRAY OF CHAR) ;
13427 BEGIN
13428 IF getDebugTopological ()
13429 THEN
13430 outText (doP, a)
13431 END
13432 END dbt ;
13433
13434
13435 (*
13436 dbs -
13437 *)
13438
13439 PROCEDURE dbs (s: dependentState; n: node) ;
13440 BEGIN
13441 IF getDebugTopological ()
13442 THEN
13443 CASE s OF
13444
13445 completed: outText (doP, '{completed ') |
13446 blocked : outText (doP, '{blocked ') |
13447 partial : outText (doP, '{partial ') |
13448 recursive: outText (doP, '{recursive ')
13449
13450 END ;
13451 IF n#NIL
13452 THEN
13453 outTextS (doP, gen (n))
13454 END ;
13455 outText (doP, '}\n')
13456 END
13457 END dbs ;
13458
13459
13460 (*
13461 dbq -
13462 *)
13463
13464 PROCEDURE dbq (n: node) ;
13465 BEGIN
13466 IF getDebugTopological ()
13467 THEN
13468 IF alists.isItemInList (globalGroup^.todoQ, n)
13469 THEN
13470 db ('{T', n) ; outText (doP, '}')
13471 ELSIF alists.isItemInList (globalGroup^.partialQ, n)
13472 THEN
13473 db ('{P', n) ; outText (doP, '}')
13474 ELSIF alists.isItemInList (globalGroup^.doneQ, n)
13475 THEN
13476 db ('{D', n) ; outText (doP, '}')
13477 END
13478 END
13479 END dbq ;
13480
13481
13482 (*
13483 walkRecord -
13484 *)
13485
13486 PROCEDURE walkRecord (l: alist; n: node) : dependentState ;
13487 VAR
13488 s : dependentState ;
13489 o,
13490 i, t: CARDINAL ;
13491 q : node ;
13492 BEGIN
13493 i := LowIndice (n^.recordF.listOfSons) ;
13494 t := HighIndice (n^.recordF.listOfSons) ;
13495 db ('\nwalking ', n) ; o := getindent (doP) ; setindent (doP, getcurpos (doP)+3) ;
13496 dbq (n) ;
13497 WHILE i<=t DO
13498 q := GetIndice (n^.recordF.listOfSons, i) ;
13499 db ('', q) ;
13500 IF isRecordField (q) AND q^.recordfieldF.tag
13501 THEN
13502 (* do nothing as it is a tag selector processed in the varient. *)
13503 ELSE
13504 s := walkDependants (l, q) ;
13505 IF s#completed
13506 THEN
13507 dbs (s, q) ;
13508 addTodo (n) ;
13509 dbq (n) ;
13510 db ('\n', NIL) ;
13511 setindent (doP, o) ;
13512 RETURN s
13513 END
13514 END ;
13515 INC (i)
13516 END ;
13517 db ('{completed', n) ; dbt ('}\n') ;
13518 setindent (doP, o) ;
13519 RETURN completed
13520 END walkRecord ;
13521
13522
13523 (*
13524 walkVarient -
13525 *)
13526
13527 PROCEDURE walkVarient (l: alist; n: node) : dependentState ;
13528 VAR
13529 s : dependentState ;
13530 i, t: CARDINAL ;
13531 q : node ;
13532 BEGIN
13533 db ('\nwalking', n) ;
13534 s := walkDependants (l, n^.varientF.tag) ;
13535 IF s#completed
13536 THEN
13537 dbs (s, n^.varientF.tag) ;
13538 dbq (n^.varientF.tag) ;
13539 db ('\n', NIL) ;
13540 RETURN s
13541 END ;
13542 i := LowIndice (n^.varientF.listOfSons) ;
13543 t := HighIndice (n^.varientF.listOfSons) ;
13544 WHILE i<=t DO
13545 q := GetIndice (n^.varientF.listOfSons, i) ;
13546 db ('', q) ;
13547 s := walkDependants (l, q) ;
13548 IF s#completed
13549 THEN
13550 dbs (s, q) ;
13551 db ('\n', NIL) ;
13552 RETURN s
13553 END ;
13554 INC (i)
13555 END ;
13556 db ('{completed', n) ; dbt ('}\n') ;
13557 RETURN completed
13558 END walkVarient ;
13559
13560
13561 (*
13562 queueBlocked -
13563 *)
13564
13565 PROCEDURE queueBlocked (n: node) ;
13566 BEGIN
13567 IF NOT (alists.isItemInList (globalGroup^.doneQ, n) OR
13568 alists.isItemInList (globalGroup^.partialQ, n))
13569 THEN
13570 addTodo (n)
13571 END
13572 END queueBlocked ;
13573
13574
13575 (*
13576 walkVar -
13577 *)
13578
13579 PROCEDURE walkVar (l: alist; n: node) : dependentState ;
13580 VAR
13581 t: node ;
13582 BEGIN
13583 t := getType (n) ;
13584 IF alists.isItemInList (globalGroup^.doneQ, t)
13585 THEN
13586 RETURN completed
13587 ELSE
13588 queueBlocked (t) ;
13589 RETURN blocked
13590 END
13591 END walkVar ;
13592
13593
13594 (*
13595 walkEnumeration -
13596 *)
13597
13598 PROCEDURE walkEnumeration (l: alist; n: node) : dependentState ;
13599 VAR
13600 s : dependentState ;
13601 i, t: CARDINAL ;
13602 q : node ;
13603 BEGIN
13604 i := LowIndice (n^.enumerationF.listOfSons) ;
13605 t := HighIndice (n^.enumerationF.listOfSons) ;
13606 s := completed ;
13607 WHILE i<=t DO
13608 q := GetIndice (n^.enumerationF.listOfSons, i) ;
13609 s := walkDependants (l, q) ;
13610 IF s#completed
13611 THEN
13612 RETURN s
13613 END ;
13614 INC (i)
13615 END ;
13616 RETURN s
13617 END walkEnumeration ;
13618
13619
13620 (*
13621 walkSubrange -
13622 *)
13623
13624 PROCEDURE walkSubrange (l: alist; n: node) : dependentState ;
13625 VAR
13626 s: dependentState ;
13627 BEGIN
13628 WITH n^.subrangeF DO
13629 s := walkDependants (l, low) ;
13630 IF s#completed
13631 THEN
13632 RETURN s
13633 END ;
13634 s := walkDependants (l, high) ;
13635 IF s#completed
13636 THEN
13637 RETURN s
13638 END ;
13639 s := walkDependants (l, type) ;
13640 IF s#completed
13641 THEN
13642 RETURN s
13643 END
13644 END ;
13645 RETURN completed
13646 END walkSubrange ;
13647
13648
13649 (*
13650 walkSubscript -
13651 *)
13652
13653 PROCEDURE walkSubscript (l: alist; n: node) : dependentState ;
13654 VAR
13655 s: dependentState ;
13656 BEGIN
13657 WITH n^.subscriptF DO
13658 s := walkDependants (l, expr) ;
13659 IF s#completed
13660 THEN
13661 RETURN s
13662 END ;
13663 s := walkDependants (l, type) ;
13664 IF s#completed
13665 THEN
13666 RETURN s
13667 END
13668 END ;
13669 RETURN completed
13670 END walkSubscript ;
13671
13672
13673 (*
13674 walkPointer -
13675 *)
13676
13677 PROCEDURE walkPointer (l: alist; n: node) : dependentState ;
13678 VAR
13679 t: node ;
13680 BEGIN
13681 (* if the type of, n, is done or partial then we can output pointer. *)
13682 t := getType (n) ;
13683 IF alists.isItemInList (globalGroup^.partialQ, t) OR
13684 alists.isItemInList (globalGroup^.doneQ, t)
13685 THEN
13686 (* pointer to partial can always generate a complete type. *)
13687 RETURN completed
13688 END ;
13689 RETURN walkType (l, n)
13690 END walkPointer ;
13691
13692
13693 (*
13694 walkArray -
13695 *)
13696
13697 PROCEDURE walkArray (l: alist; n: node) : dependentState ;
13698 VAR
13699 s: dependentState ;
13700 BEGIN
13701 WITH n^.arrayF DO
13702 (*
13703 s := walkDependants (l, type) ;
13704 IF s#completed
13705 THEN
13706 RETURN s
13707 END ;
13708 *)
13709 (* an array can only be declared if its data type has already been emitted. *)
13710 IF NOT alists.isItemInList (globalGroup^.doneQ, type)
13711 THEN
13712 s := walkDependants (l, type) ;
13713 queueBlocked (type) ;
13714 IF s=completed
13715 THEN
13716 (* downgrade the completed to partial as it has not yet been written. *)
13717 RETURN partial
13718 ELSE
13719 RETURN s
13720 END
13721 END ;
13722 RETURN walkDependants (l, subr)
13723 END
13724 END walkArray ;
13725
13726
13727 (*
13728 walkConst -
13729 *)
13730
13731 PROCEDURE walkConst (l: alist; n: node) : dependentState ;
13732 VAR
13733 s: dependentState ;
13734 BEGIN
13735 WITH n^.constF DO
13736 s := walkDependants (l, type) ;
13737 IF s#completed
13738 THEN
13739 RETURN s
13740 END ;
13741 s := walkDependants (l, value) ;
13742 IF s#completed
13743 THEN
13744 RETURN s
13745 END
13746 END ;
13747 RETURN completed
13748 END walkConst ;
13749
13750
13751 (*
13752 walkVarParam -
13753 *)
13754
13755 PROCEDURE walkVarParam (l: alist; n: node) : dependentState ;
13756 VAR
13757 t: node ;
13758 BEGIN
13759 t := getType (n) ;
13760 IF alists.isItemInList (globalGroup^.partialQ, t)
13761 THEN
13762 (* parameter can be issued from a partial. *)
13763 RETURN completed
13764 END ;
13765 RETURN walkDependants (l, t)
13766 END walkVarParam ;
13767
13768
13769 (*
13770 walkParam -
13771 *)
13772
13773 PROCEDURE walkParam (l: alist; n: node) : dependentState ;
13774 VAR
13775 t: node ;
13776 BEGIN
13777 t := getType (n) ;
13778 IF alists.isItemInList (globalGroup^.partialQ, t)
13779 THEN
13780 (* parameter can be issued from a partial. *)
13781 RETURN completed
13782 END ;
13783 RETURN walkDependants (l, t)
13784 END walkParam ;
13785
13786
13787 (*
13788 walkOptarg -
13789 *)
13790
13791 PROCEDURE walkOptarg (l: alist; n: node) : dependentState ;
13792 VAR
13793 t: node ;
13794 BEGIN
13795 t := getType (n) ;
13796 IF alists.isItemInList (globalGroup^.partialQ, t)
13797 THEN
13798 (* parameter can be issued from a partial. *)
13799 RETURN completed
13800 END ;
13801 RETURN walkDependants (l, t)
13802 END walkOptarg ;
13803
13804
13805 (*
13806 walkRecordField -
13807 *)
13808
13809 PROCEDURE walkRecordField (l: alist; n: node) : dependentState ;
13810 VAR
13811 t: node ;
13812 s: dependentState ;
13813 BEGIN
13814 assert (isRecordField (n)) ;
13815 t := getType (n) ;
13816 IF alists.isItemInList (globalGroup^.partialQ, t)
13817 THEN
13818 dbs (partial, n) ;
13819 RETURN partial
13820 ELSIF alists.isItemInList (globalGroup^.doneQ, t)
13821 THEN
13822 dbs (completed, n) ;
13823 RETURN completed
13824 ELSE
13825 addTodo (t) ;
13826 dbs (blocked, n) ;
13827 dbq (n) ;
13828 dbq (t) ;
13829 (* s := walkDependants (l, t) *)
13830 RETURN blocked
13831 END
13832 END walkRecordField ;
13833
13834
13835 (*
13836 walkVarientField -
13837 *)
13838
13839 PROCEDURE walkVarientField (l: alist; n: node) : dependentState ;
13840 VAR
13841 s : dependentState ;
13842 i, t: CARDINAL ;
13843 q : node ;
13844 BEGIN
13845 i := LowIndice (n^.varientfieldF.listOfSons) ;
13846 t := HighIndice (n^.varientfieldF.listOfSons) ;
13847 s := completed ;
13848 WHILE i<=t DO
13849 q := GetIndice (n^.varientfieldF.listOfSons, i) ;
13850 s := walkDependants (l, q) ;
13851 IF s#completed
13852 THEN
13853 dbs (s, n) ;
13854 RETURN s
13855 END ;
13856 INC (i)
13857 END ;
13858 n^.varientfieldF.simple := (t <= 1) ;
13859 dbs (s, n) ;
13860 RETURN s
13861 END walkVarientField ;
13862
13863
13864 (*
13865 walkEnumerationField -
13866 *)
13867
13868 PROCEDURE walkEnumerationField (l: alist; n: node) : dependentState ;
13869 BEGIN
13870 RETURN completed
13871 END walkEnumerationField ;
13872
13873
13874 (*
13875 walkSet -
13876 *)
13877
13878 PROCEDURE walkSet (l: alist; n: node) : dependentState ;
13879 BEGIN
13880 RETURN walkDependants (l, getType (n))
13881 END walkSet ;
13882
13883
13884 (*
13885 walkProcType -
13886 *)
13887
13888 PROCEDURE walkProcType (l: alist; n: node) : dependentState ;
13889 VAR
13890 s: dependentState ;
13891 t: node ;
13892 BEGIN
13893 t := getType (n) ;
13894 IF alists.isItemInList (globalGroup^.partialQ, t)
13895 THEN
13896 (* proctype can be generated from partial types. *)
13897 ELSE
13898 s := walkDependants (l, t) ;
13899 IF s#completed
13900 THEN
13901 RETURN s
13902 END
13903 END ;
13904 RETURN walkParameters (l, n^.proctypeF.parameters)
13905 END walkProcType ;
13906
13907
13908 (*
13909 walkProcedure -
13910 *)
13911
13912 PROCEDURE walkProcedure (l: alist; n: node) : dependentState ;
13913 VAR
13914 s: dependentState ;
13915 BEGIN
13916 s := walkDependants (l, getType (n)) ;
13917 IF s#completed
13918 THEN
13919 RETURN s
13920 END ;
13921 RETURN walkParameters (l, n^.procedureF.parameters)
13922 END walkProcedure ;
13923
13924
13925 (*
13926 walkParameters -
13927 *)
13928
13929 PROCEDURE walkParameters (l: alist; p: Index) : dependentState ;
13930 VAR
13931 s : dependentState ;
13932 i, h: CARDINAL ;
13933 q : node ;
13934 BEGIN
13935 i := LowIndice (p) ;
13936 h := HighIndice (p) ;
13937 WHILE i<=h DO
13938 q := GetIndice (p, i) ;
13939 s := walkDependants (l, q) ;
13940 IF s#completed
13941 THEN
13942 RETURN s
13943 END ;
13944 INC (i)
13945 END ;
13946 RETURN completed
13947 END walkParameters ;
13948
13949
13950 (*
13951 walkFuncCall -
13952 *)
13953
13954 PROCEDURE walkFuncCall (l: alist; n: node) : dependentState ;
13955 BEGIN
13956 RETURN completed
13957 END walkFuncCall ;
13958
13959
13960 (*
13961 walkUnary -
13962 *)
13963
13964 PROCEDURE walkUnary (l: alist; n: node) : dependentState ;
13965 VAR
13966 s: dependentState ;
13967 BEGIN
13968 WITH n^.unaryF DO
13969 s := walkDependants (l, arg) ;
13970 IF s#completed
13971 THEN
13972 RETURN s
13973 END ;
13974 RETURN walkDependants (l, resultType)
13975 END
13976 END walkUnary ;
13977
13978
13979 (*
13980 walkBinary -
13981 *)
13982
13983 PROCEDURE walkBinary (l: alist; n: node) : dependentState ;
13984 VAR
13985 s: dependentState ;
13986 BEGIN
13987 WITH n^.binaryF DO
13988 s := walkDependants (l, left) ;
13989 IF s#completed
13990 THEN
13991 RETURN s
13992 END ;
13993 s := walkDependants (l, right) ;
13994 IF s#completed
13995 THEN
13996 RETURN s
13997 END ;
13998 RETURN walkDependants (l, resultType)
13999 END
14000 END walkBinary ;
14001
14002
14003 (*
14004 walkComponentRef -
14005 *)
14006
14007 PROCEDURE walkComponentRef (l: alist; n: node) : dependentState ;
14008 VAR
14009 s: dependentState ;
14010 BEGIN
14011 WITH n^.componentrefF DO
14012 s := walkDependants (l, rec) ;
14013 IF s#completed
14014 THEN
14015 RETURN s
14016 END ;
14017 s := walkDependants (l, field) ;
14018 IF s#completed
14019 THEN
14020 RETURN s
14021 END ;
14022 RETURN walkDependants (l, resultType)
14023 END
14024 END walkComponentRef ;
14025
14026
14027 (*
14028 walkPointerRef -
14029 *)
14030
14031 PROCEDURE walkPointerRef (l: alist; n: node) : dependentState ;
14032 VAR
14033 s: dependentState ;
14034 BEGIN
14035 WITH n^.pointerrefF DO
14036 s := walkDependants (l, ptr) ;
14037 IF s#completed
14038 THEN
14039 RETURN s
14040 END ;
14041 s := walkDependants (l, field) ;
14042 IF s#completed
14043 THEN
14044 RETURN s
14045 END ;
14046 RETURN walkDependants (l, resultType)
14047 END
14048 END walkPointerRef ;
14049
14050
14051 (*
14052 walkSetValue -
14053 *)
14054
14055 PROCEDURE walkSetValue (l: alist; n: node) : dependentState ;
14056 VAR
14057 s : dependentState ;
14058 i, j: CARDINAL ;
14059 BEGIN
14060 assert (isSetValue (n)) ;
14061 WITH n^.setvalueF DO
14062 s := walkDependants (l, type) ;
14063 IF s#completed
14064 THEN
14065 RETURN s
14066 END ;
14067 i := LowIndice (values) ;
14068 j := HighIndice (values) ;
14069 WHILE i <= j DO
14070 s := walkDependants (l, GetIndice (values, i)) ;
14071 IF s#completed
14072 THEN
14073 RETURN s
14074 END ;
14075 INC (i)
14076 END
14077 END ;
14078 RETURN completed
14079 END walkSetValue ;
14080
14081
14082 (*
14083 doDependants - return the dependentState depending upon whether
14084 all dependants have been declared.
14085 *)
14086
14087 PROCEDURE doDependants (l: alist; n: node) : dependentState ;
14088 BEGIN
14089 WITH n^ DO
14090 CASE kind OF
14091
14092 throw, (* --fixme-- *)
14093 varargs,
14094 address,
14095 loc,
14096 byte,
14097 word,
14098 csizet,
14099 cssizet,
14100 cofft,
14101 cardinal64,
14102 (* base types. *)
14103 boolean,
14104 char,
14105 cardinal,
14106 longcard,
14107 shortcard,
14108 integer,
14109 longint,
14110 shortint,
14111 real,
14112 longreal,
14113 shortreal,
14114 bitset,
14115 ztype,
14116 rtype,
14117 complex,
14118 longcomplex,
14119 shortcomplex,
14120 proc : RETURN completed |
14121 (* language features and compound type attributes. *)
14122 type : RETURN walkType (l, n) |
14123 record : RETURN walkRecord (l, n) |
14124 varient : RETURN walkVarient (l, n) |
14125 var : RETURN walkVar (l, n) |
14126 enumeration : RETURN walkEnumeration (l, n) |
14127 subrange : RETURN walkSubrange (l, n) |
14128 pointer : RETURN walkPointer (l, n) |
14129 array : RETURN walkArray (l, n) |
14130 string : RETURN completed |
14131 const : RETURN walkConst (l, n) |
14132 literal : RETURN completed |
14133 varparam : RETURN walkVarParam (l, n) |
14134 param : RETURN walkParam (l, n) |
14135 optarg : RETURN walkOptarg (l, n) |
14136 recordfield : RETURN walkRecordField (l, n) |
14137 varientfield : RETURN walkVarientField (l, n) |
14138 enumerationfield: RETURN walkEnumerationField (l, n) |
14139 set : RETURN walkSet (l, n) |
14140 proctype : RETURN walkProcType (l, n) |
14141 subscript : RETURN walkSubscript (l, n) |
14142 (* blocks. *)
14143 procedure : RETURN walkProcedure (l, n) |
14144 def,
14145 imp,
14146 module,
14147 (* statements. *)
14148 loop,
14149 while,
14150 for,
14151 repeat,
14152 if,
14153 elsif,
14154 assignment : HALT |
14155 (* expressions. *)
14156 componentref : RETURN walkComponentRef (l, n) |
14157 pointerref : RETURN walkPointerRef (l, n) |
14158 not,
14159 abs,
14160 min,
14161 max,
14162 chr,
14163 cap,
14164 ord,
14165 float,
14166 trunc,
14167 high : RETURN walkUnary (l, n) |
14168 cast,
14169 val,
14170 plus,
14171 sub,
14172 div,
14173 mod,
14174 mult,
14175 divide : RETURN walkBinary (l, n) |
14176 constexp,
14177 neg,
14178 adr,
14179 size,
14180 tsize,
14181 deref : RETURN walkUnary (l, n) |
14182 equal,
14183 notequal,
14184 less,
14185 greater,
14186 greequal,
14187 lessequal : RETURN walkBinary (l, n) |
14188 funccall : RETURN walkFuncCall (l, n) |
14189 setvalue : RETURN walkSetValue (l, n)
14190
14191 END
14192 END
14193 END doDependants ;
14194
14195
14196 (*
14197 tryComplete - returns TRUE if node, n, can be and was completed.
14198 *)
14199
14200 PROCEDURE tryComplete (n: node; c, t, v: nodeProcedure) : BOOLEAN ;
14201 BEGIN
14202 IF isEnumeration (n)
14203 THEN
14204 (* can always emit enumerated types. *)
14205 output (n, c, t, v) ;
14206 RETURN TRUE
14207 ELSIF isType (n) AND isTypeHidden (n) AND (getType (n)=NIL)
14208 THEN
14209 (* can always emit hidden types. *)
14210 outputHidden (n) ;
14211 RETURN TRUE
14212 ELSIF allDependants (n) = completed
14213 THEN
14214 output (n, c, t, v) ;
14215 RETURN TRUE
14216 END ;
14217 RETURN FALSE
14218 END tryComplete ;
14219
14220
14221 (*
14222 tryCompleteFromPartial -
14223 *)
14224
14225 PROCEDURE tryCompleteFromPartial (n: node; t: nodeProcedure) : BOOLEAN ;
14226 BEGIN
14227 IF isType (n) AND (getType (n)#NIL) AND isPointer (getType (n)) AND (allDependants (getType (n)) = completed)
14228 THEN
14229 (* alists.includeItemIntoList (globalGroup^.partialQ, getType (n)) ; *)
14230 outputHiddenComplete (n) ;
14231 RETURN TRUE
14232 ELSIF allDependants (n) = completed
14233 THEN
14234 t (n) ;
14235 RETURN TRUE
14236 END ;
14237 RETURN FALSE
14238 END tryCompleteFromPartial ;
14239
14240
14241 (*
14242 visitIntrinsicFunction -
14243 *)
14244
14245 PROCEDURE visitIntrinsicFunction (v: alist; n: node; p: nodeProcedure) ;
14246 BEGIN
14247 assert (isIntrinsicFunction (n)) ;
14248 CASE n^.kind OF
14249
14250 val,
14251 cmplx: WITH n^.binaryF DO
14252 visitNode (v, left, p) ;
14253 visitNode (v, right, p) ;
14254 visitNode (v, resultType, p)
14255 END |
14256 length,
14257 adr,
14258 size,
14259 tsize,
14260 float,
14261 trunc,
14262 ord,
14263 chr,
14264 cap,
14265 abs,
14266 high,
14267 min,
14268 max,
14269 re,
14270 im : WITH n^.unaryF DO
14271 visitNode (v, arg, p) ;
14272 visitNode (v, resultType, p)
14273 END
14274
14275 END
14276 END visitIntrinsicFunction ;
14277
14278
14279 (*
14280 visitUnary -
14281 *)
14282
14283 PROCEDURE visitUnary (v: alist; n: node; p: nodeProcedure) ;
14284 BEGIN
14285 assert (isUnary (n)) ;
14286 WITH n^.unaryF DO
14287 visitNode (v, arg, p) ;
14288 visitNode (v, resultType, p)
14289 END
14290 END visitUnary ;
14291
14292
14293 (*
14294 visitBinary -
14295 *)
14296
14297 PROCEDURE visitBinary (v: alist; n: node; p: nodeProcedure) ;
14298 BEGIN
14299 WITH n^.binaryF DO
14300 visitNode (v, left, p) ;
14301 visitNode (v, right, p) ;
14302 visitNode (v, resultType, p)
14303 END
14304 END visitBinary ;
14305
14306
14307 (*
14308 visitBoolean -
14309 *)
14310
14311 PROCEDURE visitBoolean (v: alist; n: node; p: nodeProcedure) ;
14312 BEGIN
14313 visitNode (v, falseN, p) ;
14314 visitNode (v, trueN, p)
14315 END visitBoolean ;
14316
14317
14318 (*
14319 visitScope -
14320 *)
14321
14322 PROCEDURE visitScope (v: alist; n: node; p: nodeProcedure) ;
14323 BEGIN
14324 IF mustVisitScope
14325 THEN
14326 visitNode (v, n, p)
14327 END
14328 END visitScope ;
14329
14330
14331 (*
14332 visitType -
14333 *)
14334
14335 PROCEDURE visitType (v: alist; n: node; p: nodeProcedure) ;
14336 BEGIN
14337 assert (isType (n)) ;
14338 visitNode (v, n^.typeF.type, p) ;
14339 visitScope (v, n^.typeF.scope, p)
14340 END visitType ;
14341
14342
14343 (*
14344 visitIndex -
14345 *)
14346
14347 PROCEDURE visitIndex (v: alist; i: Index; p: nodeProcedure) ;
14348 VAR
14349 j, h: CARDINAL ;
14350 BEGIN
14351 j := 1 ;
14352 h := HighIndice (i) ;
14353 WHILE j <= h DO
14354 visitNode (v, GetIndice (i, j), p) ;
14355 INC (j)
14356 END
14357 END visitIndex ;
14358
14359
14360 (*
14361 visitRecord -
14362 *)
14363
14364 PROCEDURE visitRecord (v: alist; n: node; p: nodeProcedure) ;
14365 BEGIN
14366 assert (isRecord (n)) ;
14367 visitScope (v, n^.recordF.scope, p) ;
14368 visitIndex (v, n^.recordF.listOfSons, p)
14369 END visitRecord ;
14370
14371
14372 (*
14373 visitVarient -
14374 *)
14375
14376 PROCEDURE visitVarient (v: alist; n: node; p: nodeProcedure) ;
14377 BEGIN
14378 assert (isVarient (n)) ;
14379 visitIndex (v, n^.varientF.listOfSons, p) ;
14380 visitNode (v, n^.varientF.varient, p) ;
14381 visitNode (v, n^.varientF.tag, p) ;
14382 visitScope (v, n^.varientF.scope, p)
14383 END visitVarient ;
14384
14385
14386 (*
14387 visitVar -
14388 *)
14389
14390 PROCEDURE visitVar (v: alist; n: node; p: nodeProcedure) ;
14391 BEGIN
14392 assert (isVar (n)) ;
14393 visitNode (v, n^.varF.type, p) ;
14394 visitNode (v, n^.varF.decl, p) ;
14395 visitScope (v, n^.varF.scope, p)
14396 END visitVar ;
14397
14398
14399 (*
14400 visitEnumeration -
14401 *)
14402
14403 PROCEDURE visitEnumeration (v: alist; n: node; p: nodeProcedure) ;
14404 BEGIN
14405 assert (isEnumeration (n)) ;
14406 visitIndex (v, n^.enumerationF.listOfSons, p) ;
14407 visitScope (v, n^.enumerationF.scope, p)
14408 END visitEnumeration ;
14409
14410
14411 (*
14412 visitSubrange -
14413 *)
14414
14415 PROCEDURE visitSubrange (v: alist; n: node; p: nodeProcedure) ;
14416 BEGIN
14417 assert (isSubrange (n)) ;
14418 visitNode (v, n^.subrangeF.low, p) ;
14419 visitNode (v, n^.subrangeF.high, p) ;
14420 visitNode (v, n^.subrangeF.type, p) ;
14421 visitScope (v, n^.subrangeF.scope, p)
14422 END visitSubrange ;
14423
14424
14425 (*
14426 visitPointer -
14427 *)
14428
14429 PROCEDURE visitPointer (v: alist; n: node; p: nodeProcedure) ;
14430 BEGIN
14431 assert (isPointer (n)) ;
14432 visitNode (v, n^.pointerF.type, p) ;
14433 visitScope (v, n^.pointerF.scope, p)
14434 END visitPointer ;
14435
14436
14437 (*
14438 visitArray -
14439 *)
14440
14441 PROCEDURE visitArray (v: alist; n: node; p: nodeProcedure) ;
14442 BEGIN
14443 assert (isArray (n)) ;
14444 visitNode (v, n^.arrayF.subr, p) ;
14445 visitNode (v, n^.arrayF.type, p) ;
14446 visitScope (v, n^.arrayF.scope, p)
14447 END visitArray ;
14448
14449
14450 (*
14451 visitConst -
14452 *)
14453
14454 PROCEDURE visitConst (v: alist; n: node; p: nodeProcedure) ;
14455 BEGIN
14456 assert (isConst (n)) ;
14457 visitNode (v, n^.constF.type, p) ;
14458 visitNode (v, n^.constF.value, p) ;
14459 visitScope (v, n^.constF.scope, p)
14460 END visitConst ;
14461
14462
14463 (*
14464 visitVarParam -
14465 *)
14466
14467 PROCEDURE visitVarParam (v: alist; n: node; p: nodeProcedure) ;
14468 BEGIN
14469 assert (isVarParam (n)) ;
14470 visitNode (v, n^.varparamF.namelist, p) ;
14471 visitNode (v, n^.varparamF.type, p) ;
14472 visitScope (v, n^.varparamF.scope, p)
14473 END visitVarParam ;
14474
14475
14476 (*
14477 visitParam -
14478 *)
14479
14480 PROCEDURE visitParam (v: alist; n: node; p: nodeProcedure) ;
14481 BEGIN
14482 assert (isParam (n)) ;
14483 visitNode (v, n^.paramF.namelist, p) ;
14484 visitNode (v, n^.paramF.type, p) ;
14485 visitScope (v, n^.paramF.scope, p)
14486 END visitParam ;
14487
14488
14489 (*
14490 visitOptarg -
14491 *)
14492
14493 PROCEDURE visitOptarg (v: alist; n: node; p: nodeProcedure) ;
14494 BEGIN
14495 assert (isOptarg (n)) ;
14496 visitNode (v, n^.optargF.namelist, p) ;
14497 visitNode (v, n^.optargF.type, p) ;
14498 visitNode (v, n^.optargF.init, p) ;
14499 visitScope (v, n^.optargF.scope, p)
14500 END visitOptarg ;
14501
14502
14503 (*
14504 visitRecordField -
14505 *)
14506
14507 PROCEDURE visitRecordField (v: alist; n: node; p: nodeProcedure) ;
14508 BEGIN
14509 assert (isRecordField (n)) ;
14510 visitNode (v, n^.recordfieldF.type, p) ;
14511 visitNode (v, n^.recordfieldF.parent, p) ;
14512 visitNode (v, n^.recordfieldF.varient, p) ;
14513 visitScope (v, n^.recordfieldF.scope, p)
14514 END visitRecordField ;
14515
14516
14517 (*
14518 visitVarientField -
14519 *)
14520
14521 PROCEDURE visitVarientField (v: alist; n: node; p: nodeProcedure) ;
14522 BEGIN
14523 assert (isVarientField (n)) ;
14524 visitNode (v, n^.varientfieldF.parent, p) ;
14525 visitNode (v, n^.varientfieldF.varient, p) ;
14526 visitIndex (v, n^.varientfieldF.listOfSons, p) ;
14527 visitScope (v, n^.varientfieldF.scope, p)
14528 END visitVarientField ;
14529
14530
14531 (*
14532 visitEnumerationField -
14533 *)
14534
14535 PROCEDURE visitEnumerationField (v: alist; n: node; p: nodeProcedure) ;
14536 BEGIN
14537 assert (isEnumerationField (n)) ;
14538 visitNode (v, n^.enumerationfieldF.type, p) ;
14539 visitScope (v, n^.enumerationfieldF.scope, p)
14540 END visitEnumerationField ;
14541
14542
14543 (*
14544 visitSet -
14545 *)
14546
14547 PROCEDURE visitSet (v: alist; n: node; p: nodeProcedure) ;
14548 BEGIN
14549 assert (isSet (n)) ;
14550 visitNode (v, n^.setF.type, p) ;
14551 visitScope (v, n^.setF.scope, p)
14552 END visitSet ;
14553
14554
14555 (*
14556 visitProcType -
14557 *)
14558
14559 PROCEDURE visitProcType (v: alist; n: node; p: nodeProcedure) ;
14560 BEGIN
14561 assert (isProcType (n)) ;
14562 visitIndex (v, n^.proctypeF.parameters, p) ;
14563 visitNode (v, n^.proctypeF.optarg, p) ;
14564 visitNode (v, n^.proctypeF.returnType, p) ;
14565 visitScope (v, n^.proctypeF.scope, p)
14566 END visitProcType ;
14567
14568
14569 (*
14570 visitSubscript -
14571 *)
14572
14573 PROCEDURE visitSubscript (v: alist; n: node; p: nodeProcedure) ;
14574 BEGIN
14575 (*
14576 assert (isSubscript (n)) ;
14577 visitNode (v, n^.subscriptF.type, p) ;
14578 visitNode (v, n^.subscriptF.expr, p)
14579 *)
14580 END visitSubscript ;
14581
14582
14583 (*
14584 visitDecls -
14585 *)
14586
14587 PROCEDURE visitDecls (v: alist; s: scopeT; p: nodeProcedure) ;
14588 BEGIN
14589 visitIndex (v, s.constants, p) ;
14590 visitIndex (v, s.types, p) ;
14591 visitIndex (v, s.procedures, p) ;
14592 visitIndex (v, s.variables, p)
14593 END visitDecls ;
14594
14595
14596 (*
14597 visitProcedure -
14598 *)
14599
14600 PROCEDURE visitProcedure (v: alist; n: node; p: nodeProcedure) ;
14601 BEGIN
14602 assert (isProcedure (n)) ;
14603 visitDecls (v, n^.procedureF.decls, p) ;
14604 visitScope (v, n^.procedureF.scope, p) ;
14605 visitIndex (v, n^.procedureF.parameters, p) ;
14606 visitNode (v, n^.procedureF.optarg, p) ;
14607 visitNode (v, n^.procedureF.returnType, p) ;
14608 visitNode (v, n^.procedureF.beginStatements, p)
14609 END visitProcedure ;
14610
14611
14612 (*
14613 visitDef -
14614 *)
14615
14616 PROCEDURE visitDef (v: alist; n: node; p: nodeProcedure) ;
14617 BEGIN
14618 assert (isDef (n)) ;
14619 visitDecls (v, n^.defF.decls, p)
14620 END visitDef ;
14621
14622
14623 (*
14624 visitImp -
14625 *)
14626
14627 PROCEDURE visitImp (v: alist; n: node; p: nodeProcedure) ;
14628 BEGIN
14629 assert (isImp (n)) ;
14630 visitDecls (v, n^.impF.decls, p) ;
14631 visitNode (v, n^.impF.beginStatements, p) ;
14632 visitNode (v, n^.impF.finallyStatements, p)
14633 (* --fixme-- do we need to visit definitionModule? *)
14634 END visitImp ;
14635
14636
14637 (*
14638 visitModule -
14639 *)
14640
14641 PROCEDURE visitModule (v: alist; n: node; p: nodeProcedure) ;
14642 BEGIN
14643 assert (isModule (n)) ;
14644 visitDecls (v, n^.moduleF.decls, p) ;
14645 visitNode (v, n^.moduleF.beginStatements, p) ;
14646 visitNode (v, n^.moduleF.finallyStatements, p)
14647 END visitModule ;
14648
14649
14650 (*
14651 visitLoop -
14652 *)
14653
14654 PROCEDURE visitLoop (v: alist; n: node; p: nodeProcedure) ;
14655 BEGIN
14656 assert (isLoop (n)) ;
14657 visitNode (v, n^.loopF.statements, p)
14658 END visitLoop ;
14659
14660
14661 (*
14662 visitWhile -
14663 *)
14664
14665 PROCEDURE visitWhile (v: alist; n: node; p: nodeProcedure) ;
14666 BEGIN
14667 assert (isWhile (n)) ;
14668 visitNode (v, n^.whileF.expr, p) ;
14669 visitNode (v, n^.whileF.statements, p)
14670 END visitWhile ;
14671
14672
14673 (*
14674 visitRepeat -
14675 *)
14676
14677 PROCEDURE visitRepeat (v: alist; n: node; p: nodeProcedure) ;
14678 BEGIN
14679 assert (isRepeat (n)) ;
14680 visitNode (v, n^.repeatF.expr, p) ;
14681 visitNode (v, n^.repeatF.statements, p)
14682 END visitRepeat ;
14683
14684
14685 (*
14686 visitCase -
14687 *)
14688
14689 PROCEDURE visitCase (v: alist; n: node; p: nodeProcedure) ;
14690 BEGIN
14691 assert (isCase (n)) ;
14692 visitNode (v, n^.caseF.expression, p) ;
14693 visitIndex (v, n^.caseF.caseLabelList, p) ;
14694 visitNode (v, n^.caseF.else, p)
14695 END visitCase ;
14696
14697
14698 (*
14699 visitCaseLabelList -
14700 *)
14701
14702 PROCEDURE visitCaseLabelList (v: alist; n: node; p: nodeProcedure) ;
14703 BEGIN
14704 assert (isCaseLabelList (n)) ;
14705 visitNode (v, n^.caselabellistF.caseList, p) ;
14706 visitNode (v, n^.caselabellistF.statements, p)
14707 END visitCaseLabelList ;
14708
14709
14710 (*
14711 visitCaseList -
14712 *)
14713
14714 PROCEDURE visitCaseList (v: alist; n: node; p: nodeProcedure) ;
14715 BEGIN
14716 assert (isCaseList (n)) ;
14717 visitIndex (v, n^.caselistF.rangePairs, p)
14718 END visitCaseList ;
14719
14720
14721 (*
14722 visitRange -
14723 *)
14724
14725 PROCEDURE visitRange (v: alist; n: node; p: nodeProcedure) ;
14726 BEGIN
14727 assert (isRange (n)) ;
14728 visitNode (v, n^.rangeF.lo, p) ;
14729 visitNode (v, n^.rangeF.hi, p)
14730 END visitRange ;
14731
14732
14733 (*
14734 visitIf -
14735 *)
14736
14737 PROCEDURE visitIf (v: alist; n: node; p: nodeProcedure) ;
14738 BEGIN
14739 assert (isIf (n)) ;
14740 visitNode (v, n^.ifF.expr, p) ;
14741 visitNode (v, n^.ifF.elsif, p) ;
14742 visitNode (v, n^.ifF.then, p) ;
14743 visitNode (v, n^.ifF.else, p)
14744 END visitIf ;
14745
14746
14747 (*
14748 visitElsif -
14749 *)
14750
14751 PROCEDURE visitElsif (v: alist; n: node; p: nodeProcedure) ;
14752 BEGIN
14753 assert (isElsif (n)) ;
14754 visitNode (v, n^.elsifF.expr, p) ;
14755 visitNode (v, n^.elsifF.elsif, p) ;
14756 visitNode (v, n^.elsifF.then, p) ;
14757 visitNode (v, n^.elsifF.else, p)
14758 END visitElsif ;
14759
14760
14761 (*
14762 visitFor -
14763 *)
14764
14765 PROCEDURE visitFor (v: alist; n: node; p: nodeProcedure) ;
14766 BEGIN
14767 assert (isFor (n)) ;
14768 visitNode (v, n^.forF.des, p) ;
14769 visitNode (v, n^.forF.start, p) ;
14770 visitNode (v, n^.forF.end, p) ;
14771 visitNode (v, n^.forF.increment, p) ;
14772 visitNode (v, n^.forF.statements, p)
14773 END visitFor ;
14774
14775
14776 (*
14777 visitAssignment -
14778 *)
14779
14780 PROCEDURE visitAssignment (v: alist; n: node; p: nodeProcedure) ;
14781 BEGIN
14782 assert (isAssignment (n)) ;
14783 visitNode (v, n^.assignmentF.des, p) ;
14784 visitNode (v, n^.assignmentF.expr, p)
14785 END visitAssignment ;
14786
14787
14788 (*
14789 visitComponentRef -
14790 *)
14791
14792 PROCEDURE visitComponentRef (v: alist; n: node; p: nodeProcedure) ;
14793 BEGIN
14794 assert (isComponentRef (n)) ;
14795 visitNode (v, n^.componentrefF.rec, p) ;
14796 visitNode (v, n^.componentrefF.field, p) ;
14797 visitNode (v, n^.componentrefF.resultType, p)
14798 END visitComponentRef ;
14799
14800
14801 (*
14802 visitPointerRef -
14803 *)
14804
14805 PROCEDURE visitPointerRef (v: alist; n: node; p: nodeProcedure) ;
14806 BEGIN
14807 assert (isPointerRef (n)) ;
14808 visitNode (v, n^.pointerrefF.ptr, p) ;
14809 visitNode (v, n^.pointerrefF.field, p) ;
14810 visitNode (v, n^.pointerrefF.resultType, p)
14811 END visitPointerRef ;
14812
14813
14814 (*
14815 visitArrayRef -
14816 *)
14817
14818 PROCEDURE visitArrayRef (v: alist; n: node; p: nodeProcedure) ;
14819 BEGIN
14820 assert (isArrayRef (n)) ;
14821 visitNode (v, n^.arrayrefF.array, p) ;
14822 visitNode (v, n^.arrayrefF.index, p) ;
14823 visitNode (v, n^.arrayrefF.resultType, p)
14824 END visitArrayRef ;
14825
14826
14827 (*
14828 visitFunccall -
14829 *)
14830
14831 PROCEDURE visitFunccall (v: alist; n: node; p: nodeProcedure) ;
14832 BEGIN
14833 assert (isFuncCall (n)) ;
14834 visitNode (v, n^.funccallF.function, p) ;
14835 visitNode (v, n^.funccallF.args, p) ;
14836 visitNode (v, n^.funccallF.type, p)
14837 END visitFunccall ;
14838
14839
14840 (*
14841 visitVarDecl -
14842 *)
14843
14844 PROCEDURE visitVarDecl (v: alist; n: node; p: nodeProcedure) ;
14845 BEGIN
14846 assert (isVarDecl (n)) ;
14847 visitNode (v, n^.vardeclF.type, p) ;
14848 visitScope (v, n^.vardeclF.scope, p)
14849 END visitVarDecl ;
14850
14851
14852 (*
14853 visitExplist -
14854 *)
14855
14856 PROCEDURE visitExplist (v: alist; n: node; p: nodeProcedure) ;
14857 BEGIN
14858 assert (isExpList (n)) ;
14859 visitIndex (v, n^.explistF.exp, p)
14860 END visitExplist ;
14861
14862
14863 (*
14864 visitExit -
14865 *)
14866
14867 PROCEDURE visitExit (v: alist; n: node; p: nodeProcedure) ;
14868 BEGIN
14869 assert (isExit (n)) ;
14870 visitNode (v, n^.exitF.loop, p)
14871 END visitExit ;
14872
14873
14874 (*
14875 visitReturn -
14876 *)
14877
14878 PROCEDURE visitReturn (v: alist; n: node; p: nodeProcedure) ;
14879 BEGIN
14880 assert (isReturn (n)) ;
14881 visitNode (v, n^.returnF.exp, p)
14882 END visitReturn ;
14883
14884
14885 (*
14886 visitStmtSeq -
14887 *)
14888
14889 PROCEDURE visitStmtSeq (v: alist; n: node; p: nodeProcedure) ;
14890 BEGIN
14891 assert (isStatementSequence (n)) ;
14892 visitIndex (v, n^.stmtF.statements, p)
14893 END visitStmtSeq ;
14894
14895
14896 (*
14897 visitVarargs -
14898 *)
14899
14900 PROCEDURE visitVarargs (v: alist; n: node; p: nodeProcedure) ;
14901 BEGIN
14902 assert (isVarargs (n)) ;
14903 visitScope (v, n^.varargsF.scope, p)
14904 END visitVarargs ;
14905
14906
14907 (*
14908 visitSetValue -
14909 *)
14910
14911 PROCEDURE visitSetValue (v: alist; n: node; p: nodeProcedure) ;
14912 BEGIN
14913 assert (isSetValue (n)) ;
14914 visitNode (v, n^.setvalueF.type, p) ;
14915 visitIndex (v, n^.setvalueF.values, p)
14916 END visitSetValue ;
14917
14918
14919 (*
14920 visitIntrinsic -
14921 *)
14922
14923 PROCEDURE visitIntrinsic (v: alist; n: node; p: nodeProcedure) ;
14924 BEGIN
14925 assert (isIntrinsic (n)) ;
14926 visitNode (v, n^.intrinsicF.args, p)
14927 END visitIntrinsic ;
14928
14929
14930 (*
14931 visitDependants - helper procedure function called from visitNode.
14932 node n has just been visited, this procedure will
14933 visit node, n, dependants.
14934 *)
14935
14936 PROCEDURE visitDependants (v: alist; n: node; p: nodeProcedure) ;
14937 BEGIN
14938 assert (n # NIL) ;
14939 assert (alists.isItemInList (v, n)) ;
14940 CASE n^.kind OF
14941
14942 explist : visitExplist (v, n, p) |
14943 funccall : visitFunccall (v, n, p) |
14944 exit : visitExit (v, n, p) |
14945 return : visitReturn (v, n, p) |
14946 stmtseq : visitStmtSeq (v, n, p) |
14947 comment : |
14948 length : visitIntrinsicFunction (v, n, p) |
14949 unreachable,
14950 throw,
14951 halt,
14952 new,
14953 dispose,
14954 inc,
14955 dec,
14956 incl,
14957 excl : visitIntrinsic (v, n, p) |
14958 boolean : visitBoolean (v, n, p) |
14959 nil,
14960 false,
14961 true : |
14962 varargs : visitVarargs (v, n, p) |
14963 address,
14964 loc,
14965 byte,
14966 word,
14967 csizet,
14968 cssizet,
14969 cofft,
14970 cardinal64,
14971 (* base types. *)
14972 char,
14973 cardinal,
14974 longcard,
14975 shortcard,
14976 integer,
14977 longint,
14978 shortint,
14979 real,
14980 longreal,
14981 shortreal,
14982 bitset,
14983 ztype,
14984 rtype,
14985 complex,
14986 longcomplex,
14987 shortcomplex,
14988 proc : |
14989 (* language features and compound type attributes. *)
14990 type : visitType (v, n, p) |
14991 record : visitRecord (v, n, p) |
14992 varient : visitVarient (v, n, p) |
14993 var : visitVar (v, n, p) |
14994 enumeration : visitEnumeration (v, n, p) |
14995 subrange : visitSubrange (v, n, p) |
14996 pointer : visitPointer (v, n, p) |
14997 array : visitArray (v, n, p) |
14998 string : |
14999 const : visitConst (v, n, p) |
15000 literal : |
15001 varparam : visitVarParam (v, n, p) |
15002 param : visitParam (v, n, p) |
15003 optarg : visitOptarg (v, n, p) |
15004 recordfield : visitRecordField (v, n, p) |
15005 varientfield : visitVarientField (v, n, p) |
15006 enumerationfield: visitEnumerationField (v, n, p) |
15007 set : visitSet (v, n, p) |
15008 proctype : visitProcType (v, n, p) |
15009 subscript : visitSubscript (v, n, p) |
15010 (* blocks. *)
15011 procedure : visitProcedure (v, n, p) |
15012 def : visitDef (v, n, p) |
15013 imp : visitImp (v, n, p) |
15014 module : visitModule (v, n, p) |
15015 (* statements. *)
15016 loop : visitLoop (v, n, p) |
15017 while : visitWhile (v, n, p) |
15018 for : visitFor (v, n, p) |
15019 repeat : visitRepeat (v, n, p) |
15020 case : visitCase (v, n, p) |
15021 caselabellist : visitCaseLabelList (v, n, p) |
15022 caselist : visitCaseList (v, n, p) |
15023 range : visitRange (v, n, p) |
15024 if : visitIf (v, n, p) |
15025 elsif : visitElsif (v, n, p) |
15026 assignment : visitAssignment (v, n, p) |
15027 (* expressions. *)
15028 componentref : visitComponentRef (v, n, p) |
15029 pointerref : visitPointerRef (v, n, p) |
15030 arrayref : visitArrayRef (v, n, p) |
15031 cmplx,
15032 equal,
15033 notequal,
15034 less,
15035 greater,
15036 greequal,
15037 lessequal,
15038 and,
15039 or,
15040 in,
15041 cast,
15042 val,
15043 plus,
15044 sub,
15045 div,
15046 mod,
15047 mult,
15048 divide : visitBinary (v, n, p) |
15049 re : visitUnary (v, n, p) |
15050 im : visitUnary (v, n, p) |
15051 abs : visitUnary (v, n, p) |
15052 chr : visitUnary (v, n, p) |
15053 cap : visitUnary (v, n, p) |
15054 high : visitUnary (v, n, p) |
15055 ord : visitUnary (v, n, p) |
15056 float : visitUnary (v, n, p) |
15057 trunc : visitUnary (v, n, p) |
15058 not : visitUnary (v, n, p) |
15059 neg : visitUnary (v, n, p) |
15060 adr : visitUnary (v, n, p) |
15061 size : visitUnary (v, n, p) |
15062 tsize : visitUnary (v, n, p) |
15063 min : visitUnary (v, n, p) |
15064 max : visitUnary (v, n, p) |
15065 constexp : visitUnary (v, n, p) |
15066 deref : visitUnary (v, n, p) |
15067 identlist : |
15068 vardecl : visitVarDecl (v, n, p) |
15069 setvalue : visitSetValue (v, n, p)
15070
15071 END
15072 END visitDependants ;
15073
15074
15075 (*
15076 visitNode - visits node, n, if it is not already in the alist, v.
15077 It calls p(n) if the node is unvisited.
15078 *)
15079
15080 PROCEDURE visitNode (v: alist; n: node; p: nodeProcedure) ;
15081 BEGIN
15082 IF (n#NIL) AND (NOT alists.isItemInList (v, n))
15083 THEN
15084 alists.includeItemIntoList (v, n) ;
15085 p (n) ;
15086 visitDependants (v, n, p)
15087 END
15088 END visitNode ;
15089
15090
15091 (*
15092 genKind - returns a string depending upon the kind of node, n.
15093 *)
15094
15095 PROCEDURE genKind (n: node) : String ;
15096 BEGIN
15097 CASE n^.kind OF
15098
15099 (* types, no need to generate a kind string as it it contained in the name. *)
15100 nil,
15101 true,
15102 false,
15103 address,
15104 loc,
15105 byte,
15106 word,
15107 csizet,
15108 cssizet,
15109 cofft,
15110 cardinal64,
15111 char,
15112 cardinal,
15113 longcard,
15114 shortcard,
15115 integer,
15116 longint,
15117 shortint,
15118 real,
15119 longreal,
15120 shortreal,
15121 bitset,
15122 boolean,
15123 proc,
15124 ztype,
15125 rtype,
15126 complex,
15127 longcomplex,
15128 shortcomplex : RETURN NIL |
15129
15130 (* language features and compound type attributes. *)
15131 type : RETURN InitString ('type') |
15132 record : RETURN InitString ('record') |
15133 varient : RETURN InitString ('varient') |
15134 var : RETURN InitString ('var') |
15135 enumeration : RETURN InitString ('enumeration') |
15136 subrange : RETURN InitString ('subrange') |
15137 array : RETURN InitString ('array') |
15138 subscript : RETURN InitString ('subscript') |
15139 string : RETURN InitString ('string') |
15140 const : RETURN InitString ('const') |
15141 literal : RETURN InitString ('literal') |
15142 varparam : RETURN InitString ('varparam') |
15143 param : RETURN InitString ('param') |
15144 varargs : RETURN InitString ('varargs') |
15145 pointer : RETURN InitString ('pointer') |
15146 recordfield : RETURN InitString ('recordfield') |
15147 varientfield : RETURN InitString ('varientfield') |
15148 enumerationfield: RETURN InitString ('enumerationfield') |
15149 set : RETURN InitString ('set') |
15150 proctype : RETURN InitString ('proctype') |
15151 (* blocks. *)
15152 procedure : RETURN InitString ('procedure') |
15153 def : RETURN InitString ('def') |
15154 imp : RETURN InitString ('imp') |
15155 module : RETURN InitString ('module') |
15156 (* statements. *)
15157 loop : RETURN InitString ('loop') |
15158 while : RETURN InitString ('while') |
15159 for : RETURN InitString ('for') |
15160 repeat : RETURN InitString ('repeat') |
15161 assignment : RETURN InitString ('assignment') |
15162 if : RETURN InitString ('if') |
15163 elsif : RETURN InitString ('elsif') |
15164 (* expressions. *)
15165 constexp : RETURN InitString ('constexp') |
15166 neg : RETURN InitString ('neg') |
15167 cast : RETURN InitString ('cast') |
15168 val : RETURN InitString ('val') |
15169 plus : RETURN InitString ('plus') |
15170 sub : RETURN InitString ('sub') |
15171 div : RETURN InitString ('div') |
15172 mod : RETURN InitString ('mod') |
15173 mult : RETURN InitString ('mult') |
15174 divide : RETURN InitString ('divide') |
15175 adr : RETURN InitString ('adr') |
15176 size : RETURN InitString ('size') |
15177 tsize : RETURN InitString ('tsize') |
15178 chr : RETURN InitString ('chr') |
15179 ord : RETURN InitString ('ord') |
15180 float : RETURN InitString ('float') |
15181 trunc : RETURN InitString ('trunc') |
15182 high : RETURN InitString ('high') |
15183 componentref : RETURN InitString ('componentref') |
15184 pointerref : RETURN InitString ('pointerref') |
15185 arrayref : RETURN InitString ('arrayref') |
15186 deref : RETURN InitString ('deref') |
15187 equal : RETURN InitString ('equal') |
15188 notequal : RETURN InitString ('notequal') |
15189 less : RETURN InitString ('less') |
15190 greater : RETURN InitString ('greater') |
15191 greequal : RETURN InitString ('greequal') |
15192 lessequal : RETURN InitString ('lessequal') |
15193 lsl : RETURN InitString ('lsl') |
15194 lsr : RETURN InitString ('lsr') |
15195 lor : RETURN InitString ('lor') |
15196 land : RETURN InitString ('land') |
15197 lnot : RETURN InitString ('lnot') |
15198 lxor : RETURN InitString ('lxor') |
15199 and : RETURN InitString ('and') |
15200 or : RETURN InitString ('or') |
15201 not : RETURN InitString ('not') |
15202 identlist : RETURN InitString ('identlist') |
15203 vardecl : RETURN InitString ('vardecl')
15204
15205 END ;
15206 HALT
15207 END genKind ;
15208
15209
15210 (*
15211 gen - generate a small string describing node, n.
15212 *)
15213
15214 PROCEDURE gen (n: node) : String ;
15215 VAR
15216 s: String ;
15217 d: CARDINAL ;
15218 BEGIN
15219 d := VAL (CARDINAL, VAL (LONGCARD, n)) ;
15220 s := Sprintf1 (InitString ('< %d '), d) ; (* use 0x%x once FormatStrings has been released. *)
15221 s := ConCat (s, genKind (n)) ;
15222 s := ConCat (s, InitString (' ')) ;
15223 s := ConCat (s, getFQstring (n)) ;
15224 s := ConCat (s, InitString (' >')) ;
15225 RETURN s
15226 END gen ;
15227
15228
15229 (*
15230 dumpQ -
15231 *)
15232
15233 PROCEDURE dumpQ (q: ARRAY OF CHAR; l: alist) ;
15234 VAR
15235 m : String ;
15236 n : node ;
15237 d,
15238 h, i: CARDINAL ;
15239 BEGIN
15240 m := Sprintf0 (InitString ('Queue ')) ;
15241 m := KillString (WriteS (StdOut, m)) ;
15242 m := Sprintf0 (InitString (q)) ;
15243 m := KillString (WriteS (StdOut, m)) ;
15244 m := Sprintf0 (InitString ('\n')) ;
15245 m := KillString (WriteS (StdOut, m)) ;
15246 i := 1 ;
15247 h := alists.noOfItemsInList (l) ;
15248 WHILE i<=h DO
15249 n := alists.getItemFromList (l, i) ;
15250 m := KillString (WriteS (StdOut, gen (n))) ;
15251 INC (i)
15252 END ;
15253 m := Sprintf0 (InitString ('\n')) ;
15254 m := KillString (WriteS (StdOut, m))
15255 END dumpQ ;
15256
15257
15258 (*
15259 dumpLists -
15260 *)
15261
15262 PROCEDURE dumpLists ;
15263 BEGIN
15264 IF getDebugTopological () AND FALSE
15265 THEN
15266 dumpQ ('todo', globalGroup^.todoQ) ;
15267 dumpQ ('partial', globalGroup^.partialQ) ;
15268 dumpQ ('done', globalGroup^.doneQ)
15269 END
15270 END dumpLists ;
15271
15272
15273 (*
15274 outputHidden -
15275 *)
15276
15277 PROCEDURE outputHidden (n: node) ;
15278 BEGIN
15279 outText (doP, "#if !defined (") ; doFQNameC (doP, n) ; outText (doP, "_D)\n") ;
15280 outText (doP, "# define ") ; doFQNameC (doP, n) ; outText (doP, "_D\n") ;
15281 outText (doP, " typedef void *") ; doFQNameC (doP, n) ; outText (doP, ";\n") ;
15282 outText (doP, "#endif\n\n")
15283 END outputHidden ;
15284
15285
15286 (*
15287 outputHiddenComplete -
15288 *)
15289
15290 PROCEDURE outputHiddenComplete (n: node) ;
15291 VAR
15292 t: node ;
15293 BEGIN
15294 assert (isType (n)) ;
15295 t := getType (n) ;
15296 assert (isPointer (t)) ;
15297 outText (doP, "#define ") ; doFQNameC (doP, n) ; outText (doP, "_D\n") ;
15298 outText (doP, "typedef ") ; doTypeNameC (doP, getType (t)) ;
15299 setNeedSpace (doP) ; outText (doP, "*") ; doFQNameC (doP, n) ; outText (doP, ";\n")
15300 END outputHiddenComplete ;
15301
15302
15303 (*
15304 tryPartial -
15305 *)
15306 (*
15307 PROCEDURE tryPartial (n: node; pt: nodeProcedure) : BOOLEAN ;
15308 VAR
15309 q : node ;
15310 seenPointer: BOOLEAN ;
15311 BEGIN
15312 IF (n#NIL) AND isType (n)
15313 THEN
15314 seenPointer := FALSE ;
15315 q := getType (n) ;
15316 WHILE isPointer (q) DO
15317 seenPointer := TRUE ;
15318 q := getType (q)
15319 END ;
15320 IF q # NIL
15321 THEN
15322 IF isRecord (q) OR isProcType (q)
15323 THEN
15324 pt (n) ;
15325 addTodo (q) ;
15326 RETURN TRUE
15327 ELSIF isArray (q) AND (seenPointer OR
15328 alists.isItemInList (globalGroup^.doneQ, getType (q)))
15329 THEN
15330 pt (n) ;
15331 addTodo (q) ;
15332 RETURN TRUE
15333 ELSIF isType (q) AND seenPointer
15334 THEN
15335 pt (n) ;
15336 addTodo (q) ;
15337 RETURN TRUE
15338 END
15339 END
15340 END ;
15341 RETURN FALSE
15342 END tryPartial ;
15343 *)
15344
15345
15346 (*
15347 tryPartial -
15348 *)
15349
15350 PROCEDURE tryPartial (n: node; pt: nodeProcedure) : BOOLEAN ;
15351 VAR
15352 q: node ;
15353 BEGIN
15354 IF (n#NIL) AND isType (n)
15355 THEN
15356 q := getType (n) ;
15357 WHILE isPointer (q) DO
15358 q := getType (q)
15359 END ;
15360 IF q # NIL
15361 THEN
15362 IF isRecord (q) OR isProcType (q)
15363 THEN
15364 pt (n) ;
15365 addTodo (q) ;
15366 RETURN TRUE
15367 ELSIF isArray (q)
15368 THEN
15369 pt (n) ;
15370 addTodo (q) ;
15371 RETURN TRUE
15372 END
15373 END
15374 END ;
15375 RETURN FALSE
15376 END tryPartial ;
15377
15378
15379 (*
15380 outputPartialRecordArrayProcType -
15381 *)
15382
15383 PROCEDURE outputPartialRecordArrayProcType (n, q: node; indirection: CARDINAL) ;
15384 VAR
15385 s: String ;
15386 BEGIN
15387 outText (doP, "typedef struct") ; setNeedSpace (doP) ;
15388 s := getFQstring (n) ;
15389 IF isRecord (q)
15390 THEN
15391 s := ConCat (s, Mark (InitString ("_r")))
15392 ELSIF isArray (q)
15393 THEN
15394 s := ConCat (s, Mark (InitString ("_a")))
15395 ELSIF isProcType (q)
15396 THEN
15397 s := ConCat (s, Mark (InitString ("_p")))
15398 END ;
15399 outTextS (doP, s) ;
15400 setNeedSpace (doP) ;
15401 s := KillString (s) ;
15402 WHILE indirection>0 DO
15403 outText (doP, "*") ;
15404 DEC (indirection)
15405 END ;
15406 doFQNameC (doP, n) ;
15407 outText (doP, ";\n\n")
15408 END outputPartialRecordArrayProcType ;
15409
15410
15411 (*
15412 outputPartial -
15413 *)
15414
15415 PROCEDURE outputPartial (n: node) ;
15416 VAR
15417 q : node ;
15418 indirection: CARDINAL ;
15419 BEGIN
15420 q := getType (n) ;
15421 indirection := 0 ;
15422 WHILE isPointer (q) DO
15423 q := getType (q) ;
15424 INC (indirection)
15425 END ;
15426 outputPartialRecordArrayProcType (n, q, indirection)
15427 END outputPartial ;
15428
15429
15430 (*
15431 tryOutputTodo -
15432 *)
15433
15434 PROCEDURE tryOutputTodo (c, t, v, pt: nodeProcedure) ;
15435 VAR
15436 i, n: CARDINAL ;
15437 d : node ;
15438 BEGIN
15439 i := 1 ;
15440 n := alists.noOfItemsInList (globalGroup^.todoQ) ;
15441 WHILE i<=n DO
15442 d := alists.getItemFromList (globalGroup^.todoQ, i) ;
15443 IF tryComplete (d, c, t, v)
15444 THEN
15445 alists.removeItemFromList (globalGroup^.todoQ, d) ;
15446 addDone (d) ;
15447 i := 1
15448 ELSIF tryPartial (d, pt)
15449 THEN
15450 alists.removeItemFromList (globalGroup^.todoQ, d) ;
15451 alists.includeItemIntoList (globalGroup^.partialQ, d) ;
15452 i := 1
15453 ELSE
15454 INC (i)
15455 END ;
15456 n := alists.noOfItemsInList (globalGroup^.todoQ)
15457 END
15458 END tryOutputTodo ;
15459
15460
15461 (*
15462 tryOutputPartial -
15463 *)
15464
15465 PROCEDURE tryOutputPartial (t: nodeProcedure) ;
15466 VAR
15467 i, n: CARDINAL ;
15468 d : node ;
15469 BEGIN
15470 i := 1 ;
15471 n := alists.noOfItemsInList (globalGroup^.partialQ) ;
15472 WHILE i<=n DO
15473 d := alists.getItemFromList (globalGroup^.partialQ, i) ;
15474 IF tryCompleteFromPartial (d, t)
15475 THEN
15476 alists.removeItemFromList (globalGroup^.partialQ, d) ;
15477 addDone (d) ;
15478 i := 1 ;
15479 DEC (n)
15480 ELSE
15481 INC (i)
15482 END
15483 END
15484 END tryOutputPartial ;
15485
15486
15487 (*
15488 debugList -
15489 *)
15490
15491 PROCEDURE debugList (listName, symName: ARRAY OF CHAR; l: alist) ;
15492 VAR
15493 i, h: CARDINAL ;
15494 n : node ;
15495 BEGIN
15496 h := alists.noOfItemsInList (l) ;
15497 IF h>0
15498 THEN
15499 i := 1 ;
15500 REPEAT
15501 n := alists.getItemFromList (l, i) ;
15502 dbg (listName, symName, n) ;
15503 INC (i)
15504 UNTIL i > h
15505 END
15506 END debugList ;
15507
15508
15509 (*
15510 debugLists -
15511 *)
15512
15513 PROCEDURE debugLists ;
15514 BEGIN
15515 IF getDebugTopological ()
15516 THEN
15517 debugList ('todo', 'decl_node', globalGroup^.todoQ) ;
15518 debugList ('partial', 'decl_node', globalGroup^.partialQ) ;
15519 debugList ('done', 'decl_node', globalGroup^.doneQ)
15520 END
15521 END debugLists ;
15522
15523
15524 (*
15525 addEnumConst -
15526 *)
15527
15528 PROCEDURE addEnumConst (n: node) ;
15529 VAR
15530 s: String ;
15531 BEGIN
15532 IF isConst (n) OR isEnumeration (n)
15533 THEN
15534 addTodo (n)
15535 END
15536 END addEnumConst ;
15537
15538
15539 (*
15540 populateTodo -
15541 *)
15542
15543 PROCEDURE populateTodo (p: nodeProcedure) ;
15544 VAR
15545 n : node ;
15546 i, h: CARDINAL ;
15547 l : alist ;
15548 BEGIN
15549 h := alists.noOfItemsInList (globalGroup^.todoQ) ;
15550 i := 1 ;
15551 WHILE i <= h DO
15552 n := alists.getItemFromList (globalGroup^.todoQ, i) ;
15553 l := alists.initList () ;
15554 visitNode (l, n, p) ;
15555 alists.killList (l) ;
15556 h := alists.noOfItemsInList (globalGroup^.todoQ) ;
15557 INC (i)
15558 END
15559 END populateTodo ;
15560
15561
15562 (*
15563 topologicallyOut - keep trying to resolve the todoQ and partialQ
15564 until there is no change from the global group.
15565 *)
15566
15567 PROCEDURE topologicallyOut (c, t, v, tp,
15568 pc, pt, pv: nodeProcedure) ;
15569 VAR
15570 before: group ;
15571 BEGIN
15572 populateTodo (addEnumConst) ;
15573 before := NIL ;
15574 REPEAT
15575 before := dupGroup (before) ; (* Get a copy of the globalGroup and free before. *)
15576 dumpLists ;
15577 tryOutputTodo (c, t, v, tp) ;
15578 dumpLists ;
15579 tryOutputPartial (pt)
15580 UNTIL equalGroup (before, globalGroup) ;
15581 killGroup (before) ;
15582 dumpLists ;
15583 debugLists
15584 END topologicallyOut ;
15585
15586
15587 (*
15588 scaffoldStatic -
15589 *)
15590
15591 PROCEDURE scaffoldStatic (p: pretty; n: node) ;
15592 BEGIN
15593 outText (p, "\n") ;
15594 doExternCP (p) ;
15595 outText (p, "void") ;
15596 setNeedSpace (p) ;
15597 outText (p, "_M2_") ;
15598 doFQNameC (p, n) ;
15599 outText (p, "_init") ;
15600 setNeedSpace (p) ;
15601 outText (p, "(__attribute__((unused)) int argc,") ;
15602 setNeedSpace (p) ;
15603 outText (p, "__attribute__((unused)) char *argv[],") ;
15604 setNeedSpace (p) ;
15605 outText (p, "__attribute__((unused)) char *envp[])\n");
15606 p := outKc (p, "{\n") ;
15607 doStatementsC (p, n^.impF.beginStatements) ;
15608 p := outKc (p, "}\n") ;
15609 outText (p, "\n") ;
15610 doExternCP (p) ;
15611 outText (p, "void") ;
15612 setNeedSpace (p) ;
15613 outText (p, "_M2_") ;
15614 doFQNameC (p, n) ;
15615 outText (p, "_fini") ;
15616 setNeedSpace (p) ;
15617 outText (p, "(__attribute__((unused)) int argc,") ;
15618 setNeedSpace (p) ;
15619 outText (p, "__attribute__((unused)) char *argv[],") ;
15620 setNeedSpace (p) ;
15621 outText (p, "__attribute__((unused)) char *envp[])\n");
15622 p := outKc (p, "{\n") ;
15623 doStatementsC (p, n^.impF.finallyStatements) ;
15624 p := outKc (p, "}\n")
15625 END scaffoldStatic ;
15626
15627
15628 (*
15629 emitCtor -
15630 *)
15631
15632 PROCEDURE emitCtor (p: pretty; n: node) ;
15633 VAR
15634 s: String ;
15635 BEGIN
15636 outText (p, "\n") ;
15637 outText (p, "static void") ;
15638 setNeedSpace (p) ;
15639 outText (p, "ctorFunction ()\n") ;
15640 doFQNameC (p, n) ;
15641 p := outKc (p, "{\n") ;
15642 outText (p, 'M2RTS_RegisterModule ("') ;
15643 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
15644 prints (p, s) ;
15645 outText (p, '",\n') ;
15646 outText (p, 'init, fini, dependencies);\n') ;
15647 p := outKc (p, "}\n\n") ;
15648 p := outKc (p, "struct ") ;
15649 prints (p, s) ;
15650 p := outKc (p, "_module_m2 { ") ;
15651 prints (p, s) ;
15652 p := outKc (p, "_module_m2 (); ~") ;
15653 prints (p, s) ;
15654 p := outKc (p, "_module_m2 (); } global_module_") ;
15655 prints (p, s) ;
15656 outText (p, ';\n\n') ;
15657 prints (p, s) ;
15658 p := outKc (p, "_module_m2::") ;
15659 prints (p, s) ;
15660 p := outKc (p, "_module_m2 ()\n") ;
15661 p := outKc (p, "{\n") ;
15662 outText (p, 'M2RTS_RegisterModule ("') ;
15663 prints (p, s) ;
15664 outText (p, '", init, fini, dependencies);') ;
15665 p := outKc (p, "}\n") ;
15666 prints (p, s) ;
15667 p := outKc (p, "_module_m2::~") ;
15668 prints (p, s) ;
15669 p := outKc (p, "_module_m2 ()\n") ;
15670 p := outKc (p, "{\n") ;
15671 p := outKc (p, "}\n") ;
15672 s := KillString (s)
15673 END emitCtor ;
15674
15675
15676 (*
15677 scaffoldDynamic -
15678 *)
15679
15680 PROCEDURE scaffoldDynamic (p: pretty; n: node) ;
15681 BEGIN
15682 outText (p, "\n") ;
15683 doExternCP (p) ;
15684 outText (p, "void") ;
15685 setNeedSpace (p) ;
15686 outText (p, "_M2_") ;
15687 doFQNameC (p, n) ;
15688 outText (p, "_init") ;
15689 setNeedSpace (p) ;
15690 outText (p, "(__attribute__((unused)) int argc,") ;
15691 setNeedSpace (p) ;
15692 outText (p, "__attribute__((unused)) char *argv[],") ;
15693 setNeedSpace (p) ;
15694 outText (p, "__attribute__((unused)) char *envp[])\n") ;
15695 p := outKc (p, "{\n") ;
15696 doStatementsC (p, n^.impF.beginStatements) ;
15697 p := outKc (p, "}\n") ;
15698 outText (p, "\n") ;
15699 doExternCP (p) ;
15700 outText (p, "void") ;
15701 setNeedSpace (p) ;
15702 outText (p, "_M2_") ;
15703 doFQNameC (p, n) ;
15704 outText (p, "_fini") ;
15705 setNeedSpace (p) ;
15706 outText (p, "(__attribute__((unused)) int argc,") ;
15707 setNeedSpace (p) ;
15708 outText (p, "__attribute__((unused)) char *argv[],") ;
15709 setNeedSpace (p) ;
15710 outText (p, "__attribute__((unused)) char *envp[])\n") ;
15711 p := outKc (p, "{\n") ;
15712 doStatementsC (p, n^.impF.finallyStatements) ;
15713 p := outKc (p, "}\n") ;
15714 emitCtor (p, n)
15715 END scaffoldDynamic ;
15716
15717
15718 (*
15719 scaffoldMain -
15720 *)
15721
15722 PROCEDURE scaffoldMain (p: pretty; n: node) ;
15723 VAR
15724 s: String ;
15725 BEGIN
15726 outText (p, "int\n") ;
15727 outText (p, "main") ;
15728 setNeedSpace (p) ;
15729 outText (p, "(int argc, char *argv[], char *envp[])\n") ;
15730 p := outKc (p, "{\n") ;
15731 outText (p, "M2RTS_ConstructModules (") ;
15732 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
15733 prints (p, s) ;
15734 outText (p, ", argc, argv, envp);\n");
15735 outText (p, "M2RTS_DeconstructModules (") ;
15736 prints (p, s) ;
15737 outText (p, ", argc, argv, envp);\n");
15738 outText (p, "return 0;") ;
15739 p := outKc (p, "}\n") ;
15740 s := KillString (s)
15741 END scaffoldMain ;
15742
15743
15744 (*
15745 outImpInitC - emit the init/fini functions and main function if required.
15746 *)
15747
15748 PROCEDURE outImpInitC (p: pretty; n: node) ;
15749 BEGIN
15750 IF getScaffoldDynamic ()
15751 THEN
15752 scaffoldDynamic (p, n)
15753 ELSE
15754 scaffoldStatic (p, n)
15755 END ;
15756 IF getScaffoldMain ()
15757 THEN
15758 scaffoldMain (p, n)
15759 END
15760 END outImpInitC ;
15761
15762
15763 (*
15764 runSimplifyTypes -
15765 *)
15766
15767 PROCEDURE runSimplifyTypes (n: node) ;
15768 BEGIN
15769 IF isImp (n)
15770 THEN
15771 simplifyTypes (n^.impF.decls)
15772 ELSIF isModule (n)
15773 THEN
15774 simplifyTypes (n^.moduleF.decls)
15775 ELSIF isDef (n)
15776 THEN
15777 simplifyTypes (n^.defF.decls)
15778 END
15779 END runSimplifyTypes ;
15780
15781
15782 (*
15783 outDefC -
15784 *)
15785
15786 PROCEDURE outDefC (p: pretty; n: node) ;
15787 VAR
15788 s: String ;
15789 BEGIN
15790 assert (isDef (n)) ;
15791 outputFile := mcStream.openFrag (1) ; (* first fragment. *)
15792 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
15793 print (p, "/* do not edit automatically generated by mc from ") ;
15794 prints (p, s) ; print (p, ". */\n") ;
15795 writeGPLheader (outputFile) ;
15796 doCommentC (p, n^.defF.com.body) ;
15797 print (p, "\n\n#if !defined (_") ; prints (p, s) ; print (p, "_H)\n") ;
15798 print (p, "# define _") ; prints (p, s) ; print (p, "_H\n\n") ;
15799
15800 keyc.genConfigSystem (p) ;
15801
15802 print (p, "# ifdef __cplusplus\n") ;
15803 print (p, 'extern "C" {\n') ;
15804 print (p, "# endif\n") ;
15805
15806 outputFile := mcStream.openFrag (3) ; (* third fragment. *)
15807
15808 doP := p ;
15809 ForeachIndiceInIndexDo (n^.defF.importedModules, doIncludeC) ;
15810
15811 print (p, "\n") ;
15812 print (p, "# if defined (_") ; prints (p, s) ; print (p, "_C)\n") ;
15813 print (p, "# define EXTERN\n") ;
15814 print (p, "# else\n") ;
15815 print (p, '# define EXTERN extern\n') ;
15816 print (p, "# endif\n\n") ;
15817
15818 outDeclsDefC (p, n) ;
15819 runPrototypeDefC (n) ;
15820
15821 print (p, "# ifdef __cplusplus\n") ;
15822 print (p, "}\n") ;
15823 print (p, "# endif\n") ;
15824
15825 print (p, "\n") ;
15826 print (p, "# undef EXTERN\n") ;
15827 print (p, "#endif\n") ;
15828
15829 outputFile := mcStream.openFrag (2) ; (* second fragment. *)
15830 keyc.genDefs (p) ;
15831
15832 s := KillString (s)
15833 END outDefC ;
15834
15835
15836 (*
15837 runPrototypeExported -
15838 *)
15839
15840 PROCEDURE runPrototypeExported (n: node) ;
15841 BEGIN
15842 IF isExported (n)
15843 THEN
15844 keyc.enterScope (n) ;
15845 doProcedureHeadingC (n, TRUE) ;
15846 print (doP, ";\n") ;
15847 keyc.leaveScope (n)
15848 END
15849 END runPrototypeExported ;
15850
15851
15852 (*
15853 runPrototypeDefC -
15854 *)
15855
15856 PROCEDURE runPrototypeDefC (n: node) ;
15857 BEGIN
15858 IF isDef (n)
15859 THEN
15860 ForeachIndiceInIndexDo (n^.defF.decls.procedures, runPrototypeExported)
15861 END
15862 END runPrototypeDefC ;
15863
15864
15865 (*
15866 outImpC -
15867 *)
15868
15869 PROCEDURE outImpC (p: pretty; n: node) ;
15870 VAR
15871 s : String ;
15872 defModule: node ;
15873 BEGIN
15874 assert (isImp (n)) ;
15875 outputFile := mcStream.openFrag (1) ; (* first fragment. *)
15876 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
15877 print (p, "/* do not edit automatically generated by mc from ") ;
15878 prints (p, s) ; print (p, ". */\n") ;
15879 writeGPLheader (outputFile) ;
15880 doCommentC (p, n^.impF.com.body) ;
15881 outText (p, "\n") ;
15882 outputFile := mcStream.openFrag (3) ; (* third fragment. *)
15883 IF getExtendedOpaque ()
15884 THEN
15885 doP := p ;
15886 (* ForeachIndiceInIndexDo (n^.impF.importedModules, doIncludeC) ; *)
15887
15888 includeExternals (n) ;
15889 foreachModuleDo (n, runSimplifyTypes) ;
15890 printf ("/* --extended-opaque seen therefore no #include will be used and everything will be declared in full. */\n") ;
15891 foreachDefModuleDo (runIncludeDefConstType) ;
15892 includeDefVarProcedure (n) ;
15893 outDeclsImpC (p, n^.impF.decls) ;
15894 foreachDefModuleDo (runPrototypeDefC)
15895 ELSE
15896 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
15897 (* Inform the source that this code belongs to the implementation module. *)
15898 print (p, "#define _") ; prints (p, s) ; print (p, "_C\n\n") ;
15899 (* Include the definition module for any opaque types. *)
15900 print (doP, '#include "') ;
15901 prints (p, getHPrefix ()) ;
15902 prints (p, s) ;
15903 print (p, '.h"\n') ;
15904 s := KillString (s) ;
15905
15906 doP := p ;
15907 ForeachIndiceInIndexDo (n^.impF.importedModules, doIncludeC) ;
15908 print (p, "\n") ;
15909 includeDefConstType (n) ;
15910 includeDefVarProcedure (n) ;
15911 outDeclsImpC (p, n^.impF.decls) ;
15912
15913 defModule := lookupDef (getSymName (n)) ;
15914 IF defModule # NIL
15915 THEN
15916 runPrototypeDefC (defModule)
15917 END
15918 END ;
15919
15920 ForeachIndiceInIndexDo (n^.impF.decls.procedures, doPrototypeC) ;
15921
15922 outProceduresC (p, n^.impF.decls) ;
15923 outImpInitC (p, n) ;
15924
15925 outputFile := mcStream.openFrag (2) ; (* second fragment. *)
15926 keyc.genConfigSystem (p) ;
15927 keyc.genDefs (p)
15928 END outImpC ;
15929
15930
15931 (*
15932 outDeclsModuleC -
15933 *)
15934
15935 PROCEDURE outDeclsModuleC (p: pretty; s: scopeT) ;
15936 BEGIN
15937 simplifyTypes (s) ;
15938 includeConstType (s) ;
15939
15940 doP := p ;
15941
15942 topologicallyOut (doConstC, doTypesC, doVarC,
15943 outputPartial,
15944 doNone, doCompletePartialC, doNone) ;
15945
15946 (* try and output types, constants before variables and procedures. *)
15947 includeVarProcedure (s) ;
15948
15949 topologicallyOut (doConstC, doTypesC, doVarC,
15950 outputPartial,
15951 doNone, doCompletePartialC, doNone) ;
15952
15953 ForeachIndiceInIndexDo (s.procedures, doPrototypeC)
15954 END outDeclsModuleC ;
15955
15956
15957 (*
15958 outModuleInitC -
15959 *)
15960
15961 PROCEDURE outModuleInitC (p: pretty; n: node) ;
15962 BEGIN
15963 outText (p, "\n") ;
15964 doExternCP (p) ;
15965 outText (p, "void") ;
15966 setNeedSpace (p) ;
15967 outText (p, "_M2_") ;
15968 doFQNameC (p, n) ;
15969 outText (p, "_init") ;
15970 setNeedSpace (p) ;
15971 outText (p, "(__attribute__((unused)) int argc") ;
15972 outText (p, ",__attribute__((unused)) char *argv[]") ;
15973 outText (p, ",__attribute__((unused)) char *envp[])\n");
15974 p := outKc (p, "{\n") ;
15975 doStatementsC (p, n^.moduleF.beginStatements) ;
15976 p := outKc (p, "}\n") ;
15977 outText (p, "\n") ;
15978 doExternCP (p) ;
15979 outText (p, "void") ;
15980 setNeedSpace (p) ;
15981 outText (p, "_M2_") ;
15982 doFQNameC (p, n) ;
15983 outText (p, "_fini") ;
15984 setNeedSpace (p) ;
15985 outText (p, "(__attribute__((unused)) int argc") ;
15986 outText (p, ",__attribute__((unused)) char *argv[]") ;
15987 outText (p, ",__attribute__((unused)) char *envp[])\n");
15988 p := outKc (p, "{\n") ;
15989 doStatementsC (p, n^.moduleF.finallyStatements) ;
15990 p := outKc (p, "}\n")
15991 END outModuleInitC ;
15992
15993
15994 (*
15995 outModuleC -
15996 *)
15997
15998 PROCEDURE outModuleC (p: pretty; n: node) ;
15999 VAR
16000 s: String ;
16001 BEGIN
16002 assert (isModule (n)) ;
16003 outputFile := mcStream.openFrag (1) ; (* first fragment. *)
16004 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
16005 print (p, "/* do not edit automatically generated by mc from ") ;
16006 prints (p, s) ; print (p, ". */\n") ;
16007 writeGPLheader (outputFile) ;
16008 doCommentC (p, n^.moduleF.com.body) ;
16009 outText (p, "\n") ;
16010 outputFile := mcStream.openFrag (3) ; (* third fragment. *)
16011 IF getExtendedOpaque ()
16012 THEN
16013 doP := p ;
16014 includeExternals (n) ;
16015 foreachModuleDo (n, runSimplifyTypes) ;
16016 printf ("/* --extended-opaque seen therefore no #include will be used and everything will be declared in full. */\n") ;
16017 foreachDefModuleDo (runIncludeDefConstType) ;
16018 outDeclsModuleC (p, n^.moduleF.decls) ;
16019 foreachDefModuleDo (runPrototypeDefC)
16020 ELSE
16021 doP := p ;
16022 ForeachIndiceInIndexDo (n^.moduleF.importedModules, doIncludeC) ;
16023 print (p, "\n") ;
16024 outDeclsModuleC (p, n^.moduleF.decls)
16025 END ;
16026
16027 ForeachIndiceInIndexDo (n^.moduleF.decls.procedures, doPrototypeC) ;
16028
16029 outProceduresC (p, n^.moduleF.decls) ;
16030 outModuleInitC (p, n) ;
16031
16032 outputFile := mcStream.openFrag (2) ; (* second fragment. *)
16033 keyc.genConfigSystem (p) ;
16034 keyc.genDefs (p)
16035 END outModuleC ;
16036
16037
16038 (*
16039 outC -
16040 *)
16041
16042 PROCEDURE outC (p: pretty; n: node) ;
16043 BEGIN
16044 keyc.enterScope (n) ;
16045 IF isDef (n)
16046 THEN
16047 outDefC (p, n)
16048 ELSIF isImp (n)
16049 THEN
16050 outImpC (p, n)
16051 ELSIF isModule (n)
16052 THEN
16053 outModuleC (p, n)
16054 ELSE
16055 HALT
16056 END ;
16057 keyc.leaveScope (n)
16058 END outC ;
16059
16060
16061 (*
16062 doIncludeM2 - include modules in module, n.
16063 *)
16064
16065 PROCEDURE doIncludeM2 (n: node) ;
16066 VAR
16067 s: String ;
16068 BEGIN
16069 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
16070 print (doP, 'IMPORT ') ;
16071 prints (doP, s) ;
16072 print (doP, ' ;\n') ;
16073 s := KillString (s) ;
16074
16075 IF isDef (n)
16076 THEN
16077 foreachNodeDo (n^.defF.decls.symbols, addDone)
16078 ELSIF isImp (n)
16079 THEN
16080 foreachNodeDo (n^.impF.decls.symbols, addDone)
16081 ELSIF isModule (n)
16082 THEN
16083 foreachNodeDo (n^.moduleF.decls.symbols, addDone)
16084 END
16085 END doIncludeM2 ;
16086
16087
16088 (*
16089 doConstM2 -
16090 *)
16091
16092 PROCEDURE doConstM2 (n: node) ;
16093 BEGIN
16094 print (doP, "CONST\n") ;
16095 doFQNameC (doP, n) ;
16096 setNeedSpace (doP) ;
16097 doExprC (doP, n^.constF.value) ;
16098 print (doP, '\n')
16099 END doConstM2 ;
16100
16101
16102 (*
16103 doProcTypeM2 -
16104 *)
16105
16106 PROCEDURE doProcTypeM2 (p: pretty; n: node) ;
16107 BEGIN
16108 outText (p, "proc type to do..")
16109 END doProcTypeM2 ;
16110
16111
16112 (*
16113 doRecordFieldM2 -
16114 *)
16115
16116 PROCEDURE doRecordFieldM2 (p: pretty; f: node) ;
16117 BEGIN
16118 doNameM2 (p, f) ;
16119 outText (p, ":") ;
16120 setNeedSpace (p) ;
16121 doTypeM2 (p, getType (f)) ;
16122 setNeedSpace (p)
16123 END doRecordFieldM2 ;
16124
16125
16126 (*
16127 doVarientFieldM2 -
16128 *)
16129
16130 PROCEDURE doVarientFieldM2 (p: pretty; n: node) ;
16131 VAR
16132 i, t: CARDINAL ;
16133 q : node ;
16134 BEGIN
16135 assert (isVarientField (n)) ;
16136 doNameM2 (p, n) ;
16137 outText (p, ":") ;
16138 setNeedSpace (p) ;
16139 i := LowIndice (n^.varientfieldF.listOfSons) ;
16140 t := HighIndice (n^.varientfieldF.listOfSons) ;
16141 WHILE i<=t DO
16142 q := GetIndice (n^.varientfieldF.listOfSons, i) ;
16143 IF isRecordField (q)
16144 THEN
16145 doRecordFieldM2 (p, q) ;
16146 outText (p, ";\n")
16147 ELSIF isVarient (q)
16148 THEN
16149 doVarientM2 (p, q) ;
16150 outText (p, ";\n")
16151 ELSE
16152 HALT
16153 END ;
16154 INC (i)
16155 END
16156 END doVarientFieldM2 ;
16157
16158
16159 (*
16160 doVarientM2 -
16161 *)
16162
16163 PROCEDURE doVarientM2 (p: pretty; n: node) ;
16164 VAR
16165 i, t: CARDINAL ;
16166 q : node ;
16167 BEGIN
16168 assert (isVarient (n)) ;
16169 outText (p, "CASE") ; setNeedSpace (p) ;
16170 IF n^.varientF.tag # NIL
16171 THEN
16172 IF isRecordField (n^.varientF.tag)
16173 THEN
16174 doRecordFieldM2 (p, n^.varientF.tag)
16175 ELSIF isVarientField (n^.varientF.tag)
16176 THEN
16177 doVarientFieldM2 (p, n^.varientF.tag)
16178 ELSE
16179 HALT
16180 END
16181 END ;
16182 setNeedSpace (p) ;
16183 outText (p, "OF\n") ;
16184 i := LowIndice (n^.varientF.listOfSons) ;
16185 t := HighIndice (n^.varientF.listOfSons) ;
16186 WHILE i<=t DO
16187 q := GetIndice (n^.varientF.listOfSons, i) ;
16188 IF isRecordField (q)
16189 THEN
16190 IF NOT q^.recordfieldF.tag
16191 THEN
16192 doRecordFieldM2 (p, q) ;
16193 outText (p, ";\n")
16194 END
16195 ELSIF isVarientField (q)
16196 THEN
16197 doVarientFieldM2 (p, q)
16198 ELSE
16199 HALT
16200 END ;
16201 INC (i)
16202 END ;
16203 outText (p, "END") ; setNeedSpace (p)
16204 END doVarientM2 ;
16205
16206
16207 (*
16208 doRecordM2 -
16209 *)
16210
16211 PROCEDURE doRecordM2 (p: pretty; n: node) ;
16212 VAR
16213 i, h: CARDINAL ;
16214 f : node ;
16215 BEGIN
16216 assert (isRecord (n)) ;
16217 p := outKm2 (p, "RECORD") ;
16218 i := LowIndice (n^.recordF.listOfSons) ;
16219 h := HighIndice (n^.recordF.listOfSons) ;
16220 outText (p, "\n") ;
16221 WHILE i<=h DO
16222 f := GetIndice (n^.recordF.listOfSons, i) ;
16223 IF isRecordField (f)
16224 THEN
16225 IF NOT f^.recordfieldF.tag
16226 THEN
16227 doRecordFieldM2 (p, f) ;
16228 outText (p, ";\n")
16229 END
16230 ELSIF isVarient (f)
16231 THEN
16232 doVarientM2 (p, f) ;
16233 outText (p, ";\n")
16234 ELSIF isVarientField (f)
16235 THEN
16236 doVarientFieldM2 (p, f)
16237 END ;
16238 INC (i)
16239 END ;
16240 p := outKm2 (p, "END") ; setNeedSpace (p)
16241 END doRecordM2 ;
16242
16243
16244 (*
16245 doPointerM2 -
16246 *)
16247
16248 PROCEDURE doPointerM2 (p: pretty; n: node) ;
16249 BEGIN
16250 outText (p, "POINTER TO") ;
16251 setNeedSpace (doP) ;
16252 doTypeM2 (p, getType (n)) ;
16253 setNeedSpace (p) ;
16254 outText (p, ";\n")
16255 END doPointerM2 ;
16256
16257
16258 (*
16259 doTypeAliasM2 -
16260 *)
16261
16262 PROCEDURE doTypeAliasM2 (p: pretty; n: node) ;
16263 BEGIN
16264 doTypeNameC (p, n) ;
16265 setNeedSpace (p) ;
16266 outText (doP, "=") ;
16267 setNeedSpace (p) ;
16268 doTypeM2 (p, getType (n)) ;
16269 setNeedSpace (p) ;
16270 outText (p, "\n")
16271 END doTypeAliasM2 ;
16272
16273
16274 (*
16275 doEnumerationM2 -
16276 *)
16277
16278 PROCEDURE doEnumerationM2 (p: pretty; n: node) ;
16279 VAR
16280 i, h: CARDINAL ;
16281 s : node ;
16282 t : String ;
16283 BEGIN
16284 outText (p, "(") ;
16285 i := LowIndice (n^.enumerationF.listOfSons) ;
16286 h := HighIndice (n^.enumerationF.listOfSons) ;
16287 WHILE i <= h DO
16288 s := GetIndice (n^.enumerationF.listOfSons, i) ;
16289 doFQNameC (p, s) ;
16290 IF i < h
16291 THEN
16292 outText (p, ",") ; setNeedSpace (p)
16293 END ;
16294 INC (i)
16295 END ;
16296 outText (p, ")")
16297 END doEnumerationM2 ;
16298
16299
16300 (*
16301 doBaseM2 -
16302 *)
16303
16304 PROCEDURE doBaseM2 (p: pretty; n: node) ;
16305 BEGIN
16306 CASE n^.kind OF
16307
16308 char,
16309 cardinal,
16310 longcard,
16311 shortcard,
16312 integer,
16313 longint,
16314 shortint,
16315 complex,
16316 longcomplex,
16317 shortcomplex,
16318 real,
16319 longreal,
16320 shortreal,
16321 bitset,
16322 boolean,
16323 proc : doNameM2 (p, n)
16324
16325 END ;
16326 setNeedSpace (p)
16327 END doBaseM2 ;
16328
16329
16330 (*
16331 doSystemM2 -
16332 *)
16333
16334 PROCEDURE doSystemM2 (p: pretty; n: node) ;
16335 BEGIN
16336 CASE n^.kind OF
16337
16338 address,
16339 loc,
16340 byte,
16341 word,
16342 csizet,
16343 cssizet,
16344 cofft,
16345 cardinal64: doNameM2 (p, n)
16346
16347 END
16348 END doSystemM2 ;
16349
16350
16351 (*
16352 doTypeM2 -
16353 *)
16354
16355 PROCEDURE doTypeM2 (p: pretty; n: node) ;
16356 BEGIN
16357 IF isBase (n)
16358 THEN
16359 doBaseM2 (p, n)
16360 ELSIF isSystem (n)
16361 THEN
16362 doSystemM2 (p, n)
16363 ELSIF isType (n)
16364 THEN
16365 doTypeAliasM2 (p, n)
16366 ELSIF isProcType (n)
16367 THEN
16368 doProcTypeM2 (p, n)
16369 ELSIF isPointer (n)
16370 THEN
16371 doPointerM2 (p, n)
16372 ELSIF isEnumeration (n)
16373 THEN
16374 doEnumerationM2 (p, n)
16375 ELSIF isRecord (n)
16376 THEN
16377 doRecordM2 (p, n)
16378 END
16379 END doTypeM2 ;
16380
16381
16382 (*
16383 doTypesM2 -
16384 *)
16385
16386 PROCEDURE doTypesM2 (n: node) ;
16387 VAR
16388 m: node ;
16389 BEGIN
16390 outText (doP, "TYPE\n") ;
16391 doTypeM2 (doP, n)
16392 END doTypesM2 ;
16393
16394
16395 (*
16396 doVarM2 -
16397 *)
16398
16399 PROCEDURE doVarM2 (n: node) ;
16400 BEGIN
16401 assert (isVar (n)) ;
16402 doNameC (doP, n) ;
16403 outText (doP, ":") ;
16404 setNeedSpace (doP) ;
16405 doTypeM2 (doP, getType (n)) ;
16406 setNeedSpace (doP) ;
16407 outText (doP, ";\n")
16408 END doVarM2 ;
16409
16410
16411 (*
16412 doVarsM2 -
16413 *)
16414
16415 PROCEDURE doVarsM2 (n: node) ;
16416 VAR
16417 m: node ;
16418 BEGIN
16419 outText (doP, "VAR\n") ;
16420 doVarM2 (n)
16421 END doVarsM2 ;
16422
16423
16424 (*
16425 doTypeNameM2 -
16426 *)
16427
16428 PROCEDURE doTypeNameM2 (p: pretty; n: node) ;
16429 BEGIN
16430 doNameM2 (p, n)
16431 END doTypeNameM2 ;
16432
16433
16434 (*
16435 doParamM2 -
16436 *)
16437
16438 PROCEDURE doParamM2 (p: pretty; n: node) ;
16439 VAR
16440 ptype: node ;
16441 i : Name ;
16442 c, t : CARDINAL ;
16443 l : wlist ;
16444 BEGIN
16445 assert (isParam (n)) ;
16446 ptype := getType (n) ;
16447 IF n^.paramF.namelist = NIL
16448 THEN
16449 doTypeNameM2 (p, ptype)
16450 ELSE
16451 assert (isIdentList (n^.paramF.namelist)) ;
16452 l := n^.paramF.namelist^.identlistF.names ;
16453 IF l=NIL
16454 THEN
16455 doTypeNameM2 (p, ptype)
16456 ELSE
16457 t := wlists.noOfItemsInList (l) ;
16458 c := 1 ;
16459 WHILE c <= t DO
16460 i := wlists.getItemFromList (l, c) ;
16461 setNeedSpace (p) ;
16462 doNamesC (p, i) ;
16463 IF c<t
16464 THEN
16465 outText (p, ',') ; setNeedSpace (p)
16466 END ;
16467 INC (c)
16468 END ;
16469 outText (p, ':') ; setNeedSpace (p) ;
16470 doTypeNameM2 (p, ptype)
16471 END
16472 END
16473 END doParamM2 ;
16474
16475
16476 (*
16477 doVarParamM2 -
16478 *)
16479
16480 PROCEDURE doVarParamM2 (p: pretty; n: node) ;
16481 VAR
16482 ptype: node ;
16483 i : Name ;
16484 c, t : CARDINAL ;
16485 l : wlist ;
16486 BEGIN
16487 assert (isVarParam (n)) ;
16488 outText (p, 'VAR') ; setNeedSpace (p) ;
16489 ptype := getType (n) ;
16490 IF n^.varparamF.namelist = NIL
16491 THEN
16492 doTypeNameM2 (p, ptype)
16493 ELSE
16494 assert (isIdentList (n^.varparamF.namelist)) ;
16495 l := n^.varparamF.namelist^.identlistF.names ;
16496 IF l=NIL
16497 THEN
16498 doTypeNameM2 (p, ptype)
16499 ELSE
16500 t := wlists.noOfItemsInList (l) ;
16501 c := 1 ;
16502 WHILE c <= t DO
16503 i := wlists.getItemFromList (l, c) ;
16504 setNeedSpace (p) ;
16505 doNamesC (p, i) ;
16506 IF c<t
16507 THEN
16508 outText (p, ',') ; setNeedSpace (p)
16509 END ;
16510 INC (c)
16511 END ;
16512 outText (p, ':') ; setNeedSpace (p) ;
16513 doTypeNameM2 (p, ptype)
16514 END
16515 END
16516 END doVarParamM2 ;
16517
16518
16519 (*
16520 doParameterM2 -
16521 *)
16522
16523 PROCEDURE doParameterM2 (p: pretty; n: node) ;
16524 BEGIN
16525 IF isParam (n)
16526 THEN
16527 doParamM2 (p, n)
16528 ELSIF isVarParam (n)
16529 THEN
16530 doVarParamM2 (p, n)
16531 ELSIF isVarargs (n)
16532 THEN
16533 print (p, "...")
16534 END
16535 END doParameterM2 ;
16536
16537
16538 (*
16539 doPrototypeM2 -
16540 *)
16541
16542 PROCEDURE doPrototypeM2 (n: node) ;
16543 VAR
16544 i, h: CARDINAL ;
16545 p : node ;
16546 BEGIN
16547 assert (isProcedure (n)) ;
16548 noSpace (doP) ;
16549
16550 doNameM2 (doP, n) ;
16551 setNeedSpace (doP) ;
16552 outText (doP, "(") ;
16553 i := LowIndice (n^.procedureF.parameters) ;
16554 h := HighIndice (n^.procedureF.parameters) ;
16555 WHILE i <= h DO
16556 p := GetIndice (n^.procedureF.parameters, i) ;
16557 doParameterM2 (doP, p) ;
16558 noSpace (doP) ;
16559 IF i < h
16560 THEN
16561 print (doP, ";") ; setNeedSpace (doP)
16562 END ;
16563 INC (i)
16564 END ;
16565 outText (doP, ")") ;
16566 IF n^.procedureF.returnType#NIL
16567 THEN
16568 setNeedSpace (doP) ;
16569 outText (doP, ":") ;
16570 doTypeM2 (doP, n^.procedureF.returnType) ; setNeedSpace (doP)
16571 END ;
16572 outText (doP, ";\n")
16573 END doPrototypeM2 ;
16574
16575
16576 (*
16577 outputPartialM2 - just writes out record, array, and proctypes.
16578 No need for forward declarations in Modula-2
16579 but we need to keep topological sort happy.
16580 So when asked to output partial we emit the
16581 full type for these types and then do nothing
16582 when trying to complete partial to full.
16583 *)
16584
16585 PROCEDURE outputPartialM2 (n: node) ;
16586 VAR
16587 q: node ;
16588 BEGIN
16589 q := getType (n) ;
16590 IF isRecord (q)
16591 THEN
16592 doTypeM2 (doP, n)
16593 ELSIF isArray (q)
16594 THEN
16595 doTypeM2 (doP, n)
16596 ELSIF isProcType (q)
16597 THEN
16598 doTypeM2 (doP, n)
16599 END
16600 END outputPartialM2 ;
16601
16602
16603 (*
16604 outDeclsDefM2 -
16605 *)
16606
16607 PROCEDURE outDeclsDefM2 (p: pretty; s: scopeT) ;
16608 BEGIN
16609 simplifyTypes (s) ;
16610 includeConstType (s) ;
16611
16612 doP := p ;
16613
16614 topologicallyOut (doConstM2, doTypesM2, doVarsM2,
16615 outputPartialM2,
16616 doNothing, doNothing, doNothing) ;
16617
16618 includeVarProcedure (s) ;
16619
16620 topologicallyOut (doConstM2, doTypesM2, doVarsM2,
16621 outputPartialM2,
16622 doNothing, doNothing, doNothing) ;
16623
16624 ForeachIndiceInIndexDo (s.procedures, doPrototypeM2)
16625 END outDeclsDefM2 ;
16626
16627
16628 (*
16629 outDefM2 -
16630 *)
16631
16632 PROCEDURE outDefM2 (p: pretty; n: node) ;
16633 VAR
16634 s: String ;
16635 BEGIN
16636 s := InitStringCharStar (keyToCharStar (getSource (n))) ;
16637 print (p, "(* automatically created by mc from ") ; prints (p, s) ; print (p, ". *)\n\n") ;
16638 s := KillString (s) ;
16639 s := InitStringCharStar (keyToCharStar (getSymName (n))) ;
16640 print (p, "DEFINITION MODULE ") ; prints (p, s) ; print (p, " ;\n\n") ;
16641
16642 doP := p ;
16643 ForeachIndiceInIndexDo (n^.defF.importedModules, doIncludeM2) ;
16644
16645 print (p, "\n") ;
16646
16647 outDeclsDefM2 (p, n^.defF.decls) ;
16648
16649 print (p, "\n") ;
16650 print (p, "END ") ;
16651 prints (p, s) ;
16652 print (p, ".\n") ;
16653 s := KillString (s)
16654 END outDefM2 ;
16655
16656
16657 (*
16658 outDeclsImpM2 -
16659 *)
16660
16661 PROCEDURE outDeclsImpM2 (p: pretty; s: scopeT) ;
16662 BEGIN
16663 simplifyTypes (s) ;
16664 includeConstType (s) ;
16665
16666 doP := p ;
16667
16668 topologicallyOut (doConstM2, doTypesM2, doVarM2,
16669 outputPartialM2,
16670 doNothing, doNothing, doNothing) ;
16671
16672 includeVarProcedure (s) ;
16673
16674 topologicallyOut (doConstM2, doTypesM2, doVarsM2,
16675 outputPartialM2,
16676 doNothing, doNothing, doNothing) ;
16677
16678 outText (p, "\n") ;
16679 ForeachIndiceInIndexDo (s.procedures, doPrototypeC)
16680 END outDeclsImpM2 ;
16681
16682
16683 (*
16684 outImpM2 -
16685 *)
16686
16687 PROCEDURE outImpM2 (p: pretty; n: node) ;
16688 VAR
16689 s: String ;
16690 BEGIN
16691 s := InitStringCharStar (keyToCharStar (getSource (n))) ;
16692 print (p, "(* automatically created by mc from ") ; prints (p, s) ; print (p, ". *)\n\n") ;
16693 print (p, "IMPLEMENTATION MODULE ") ; prints (p, s) ; print (p, " ;\n\n") ;
16694
16695 doP := p ;
16696 ForeachIndiceInIndexDo (n^.impF.importedModules, doIncludeM2) ;
16697 print (p, "\n") ;
16698
16699 includeDefConstType (n) ;
16700 outDeclsImpM2 (p, n^.impF.decls) ;
16701
16702 print (p, "\n") ;
16703 print (p, "END ") ;
16704 prints (p, s) ;
16705 print (p, ".\n") ;
16706
16707 s := KillString (s)
16708 END outImpM2 ;
16709
16710
16711 (*
16712 outModuleM2 -
16713 *)
16714
16715 PROCEDURE outModuleM2 (p: pretty; n: node) ;
16716 BEGIN
16717
16718 END outModuleM2 ;
16719
16720
16721 (*
16722 outM2 -
16723 *)
16724
16725 PROCEDURE outM2 (p: pretty; n: node) ;
16726 BEGIN
16727 IF isDef (n)
16728 THEN
16729 outDefM2 (p, n)
16730 ELSIF isImp (n)
16731 THEN
16732 outImpM2 (p, n)
16733 ELSIF isModule (n)
16734 THEN
16735 outModuleM2 (p, n)
16736 ELSE
16737 HALT
16738 END
16739 END outM2 ;
16740
16741
16742 (*
16743 out - walks the tree of node declarations for the main module
16744 and writes the output to the outputFile specified in
16745 mcOptions. It outputs the declarations in the language
16746 specified above.
16747 *)
16748
16749 PROCEDURE out ;
16750 VAR
16751 p: pretty ;
16752 BEGIN
16753 openOutput ;
16754 p := initPretty (write, writeln) ;
16755 CASE lang OF
16756
16757 ansiC : outC (p, getMainModule ()) |
16758 ansiCP: outC (p, getMainModule ()) |
16759 pim4 : outM2 (p, getMainModule ())
16760
16761 END ;
16762 closeOutput
16763 END out ;
16764
16765
16766 (*
16767 setLangC -
16768 *)
16769
16770 PROCEDURE setLangC ;
16771 BEGIN
16772 lang := ansiC
16773 END setLangC ;
16774
16775
16776 (*
16777 setLangCP -
16778 *)
16779
16780 PROCEDURE setLangCP ;
16781 BEGIN
16782 lang := ansiCP ;
16783 keyc.cp
16784 END setLangCP ;
16785
16786
16787 (*
16788 setLangM2 -
16789 *)
16790
16791 PROCEDURE setLangM2 ;
16792 BEGIN
16793 lang := pim4
16794 END setLangM2 ;
16795
16796
16797 (*
16798 addDone - adds node, n, to the doneQ.
16799 *)
16800
16801 PROCEDURE addDone (n: node) ;
16802 VAR
16803 s: String ;
16804 BEGIN
16805 alists.includeItemIntoList (globalGroup^.doneQ, n) ;
16806 IF isVar (n) OR isParameter (n)
16807 THEN
16808 initNodeOpaqueState (n)
16809 END ;
16810 debugLists
16811 END addDone ;
16812
16813
16814 (*
16815 addDoneDef - adds node, n, to the doneQ providing
16816 it is not an opaque of the main module we are compiling.
16817 *)
16818
16819 PROCEDURE addDoneDef (n: node) ;
16820 BEGIN
16821 IF isDef (n)
16822 THEN
16823 addDone (n) ;
16824 RETURN
16825 END ;
16826 IF FALSE AND (lookupImp (getSymName (getScope (n))) = getMainModule ())
16827 THEN
16828 metaError1 ('cyclic dependancy found between another module using {%1ad} from the definition module of the implementation main being compiled, use the --extended-opaque option to compile', n) ;
16829 flushErrors ;
16830 errorAbort0 ('terminating compilation')
16831 ELSIF isType (n) AND isDeclInImp (n)
16832 THEN
16833 (* Ignore an opaque type which is declared in this implementation module as it
16834 will be fully declared in C/C++ with the __opaque postfix. Whereas the
16835 void * non prefixed typedef will be declared in the .h file. *)
16836 ELSE
16837 addDone (n)
16838 END
16839 END addDoneDef ;
16840
16841
16842 (*
16843 dbgAdd -
16844 *)
16845
16846 PROCEDURE dbgAdd (l: alist; n: node) : node ;
16847 BEGIN
16848 IF n#NIL
16849 THEN
16850 alists.includeItemIntoList (l, n)
16851 END ;
16852 RETURN n
16853 END dbgAdd ;
16854
16855
16856 (*
16857 dbgType -
16858 *)
16859
16860 PROCEDURE dbgType (l: alist; n: node) ;
16861 VAR
16862 t: node ;
16863 BEGIN
16864 t := dbgAdd (l, getType (n)) ;
16865 out1 ("<%s type", n) ;
16866 IF t = NIL
16867 THEN
16868 out0 (", type = NIL\n")
16869 ELSE
16870 out1 (", type = %s>\n", t)
16871 END
16872 END dbgType ;
16873
16874
16875 (*
16876 dbgPointer -
16877 *)
16878
16879 PROCEDURE dbgPointer (l: alist; n: node) ;
16880 VAR
16881 t: node ;
16882 BEGIN
16883 t := dbgAdd (l, getType (n)) ;
16884 out1 ("<%s pointer", n) ;
16885 out1 (" to %s>\n", t)
16886 END dbgPointer ;
16887
16888
16889 (*
16890 dbgRecord -
16891 *)
16892
16893 PROCEDURE dbgRecord (l: alist; n: node) ;
16894 VAR
16895 i, t: CARDINAL ;
16896 q : node ;
16897 BEGIN
16898 out1 ("<%s record:\n", n) ;
16899 i := LowIndice (n^.recordF.listOfSons) ;
16900 t := HighIndice (n^.recordF.listOfSons) ;
16901 WHILE i<=t DO
16902 q := GetIndice (n^.recordF.listOfSons, i) ;
16903 IF isRecordField (q)
16904 THEN
16905 out1 (" <recordfield %s", q)
16906 ELSIF isVarientField (q)
16907 THEN
16908 out1 (" <varientfield %s", q)
16909 ELSIF isVarient (q)
16910 THEN
16911 out1 (" <varient %s", q)
16912 ELSE
16913 HALT
16914 END ;
16915 q := dbgAdd (l, getType (q)) ;
16916 out1 (": %s>\n", q) ;
16917 INC (i)
16918 END ;
16919 outText (doP, ">\n")
16920 END dbgRecord ;
16921
16922
16923 (*
16924 dbgVarient -
16925 *)
16926
16927 PROCEDURE dbgVarient (l: alist; n: node) ;
16928 VAR
16929 i, t: CARDINAL ;
16930 q : node ;
16931 BEGIN
16932 out1 ("<%s varient: ", n) ;
16933 out1 ("tag %s", n^.varientF.tag) ;
16934 q := getType (n^.varientF.tag) ;
16935 IF q=NIL
16936 THEN
16937 outText (doP, "\n")
16938 ELSE
16939 out1 (": %s\n", q) ;
16940 q := dbgAdd (l, q)
16941 END ;
16942 i := LowIndice (n^.varientF.listOfSons) ;
16943 t := HighIndice (n^.varientF.listOfSons) ;
16944 WHILE i<=t DO
16945 q := GetIndice (n^.varientF.listOfSons, i) ;
16946 IF isRecordField (q)
16947 THEN
16948 out1 (" <recordfield %s", q)
16949 ELSIF isVarientField (q)
16950 THEN
16951 out1 (" <varientfield %s", q)
16952 ELSIF isVarient (q)
16953 THEN
16954 out1 (" <varient %s", q)
16955 ELSE
16956 HALT
16957 END ;
16958 q := dbgAdd (l, getType (q)) ;
16959 out1 (": %s>\n", q) ;
16960 INC (i)
16961 END ;
16962 outText (doP, ">\n")
16963 END dbgVarient ;
16964
16965
16966 (*
16967 dbgEnumeration -
16968 *)
16969
16970 PROCEDURE dbgEnumeration (l: alist; n: node) ;
16971 VAR
16972 e : node ;
16973 i, h: CARDINAL ;
16974 BEGIN
16975 outText (doP, "< enumeration ") ;
16976 i := LowIndice (n^.enumerationF.listOfSons) ;
16977 h := HighIndice (n^.enumerationF.listOfSons) ;
16978 WHILE i<=h DO
16979 e := GetIndice (n^.enumerationF.listOfSons, i) ;
16980 out1 ("%s, ", e) ;
16981 INC (i)
16982 END ;
16983 outText (doP, ">\n")
16984 END dbgEnumeration ;
16985
16986
16987 (*
16988 dbgVar -
16989 *)
16990
16991 PROCEDURE dbgVar (l: alist; n: node) ;
16992 VAR
16993 t: node ;
16994 BEGIN
16995 t := dbgAdd (l, getType (n)) ;
16996 out1 ("<%s var", n) ;
16997 out1 (", type = %s>\n", t)
16998 END dbgVar ;
16999
17000
17001 (*
17002 dbgSubrange -
17003 *)
17004
17005 PROCEDURE dbgSubrange (l: alist; n: node) ;
17006 BEGIN
17007 IF n^.subrangeF.low = NIL
17008 THEN
17009 out1 ('%s', n^.subrangeF.type)
17010 ELSE
17011 out1 ('[%s', n^.subrangeF.low) ;
17012 out1 ('..%s]', n^.subrangeF.high)
17013 END
17014 END dbgSubrange ;
17015
17016
17017 (*
17018 dbgArray -
17019 *)
17020
17021 PROCEDURE dbgArray (l: alist; n: node) ;
17022 VAR
17023 t: node ;
17024 BEGIN
17025 t := dbgAdd (l, getType (n)) ;
17026 out1 ("<%s array ", n) ;
17027 IF n^.arrayF.subr # NIL
17028 THEN
17029 dbgSubrange (l, n^.arrayF.subr)
17030 END ;
17031 out1 (" of %s>\n", t)
17032 END dbgArray ;
17033
17034
17035 (*
17036 doDbg -
17037 *)
17038
17039 PROCEDURE doDbg (l: alist; n: node) ;
17040 BEGIN
17041 IF n=NIL
17042 THEN
17043 (* do nothing. *)
17044 ELSIF isSubrange (n)
17045 THEN
17046 dbgSubrange (l, n)
17047 ELSIF isType (n)
17048 THEN
17049 dbgType (l, n)
17050 ELSIF isRecord (n)
17051 THEN
17052 dbgRecord (l, n)
17053 ELSIF isVarient (n)
17054 THEN
17055 dbgVarient (l, n)
17056 ELSIF isEnumeration (n)
17057 THEN
17058 dbgEnumeration (l, n)
17059 ELSIF isPointer (n)
17060 THEN
17061 dbgPointer (l, n)
17062 ELSIF isArray (n)
17063 THEN
17064 dbgArray (l, n)
17065 ELSIF isVar (n)
17066 THEN
17067 dbgVar (l, n)
17068 END
17069 END doDbg ;
17070
17071
17072 (*
17073 dbg -
17074 *)
17075
17076 PROCEDURE dbg (listName, symName: ARRAY OF CHAR; n: node) ;
17077 VAR
17078 l: alist ;
17079 o: pretty ;
17080 f: File ;
17081 s: String ;
17082 i: CARDINAL ;
17083 BEGIN
17084 o := doP ;
17085 f := outputFile ;
17086 outputFile := StdOut ;
17087 doP := initPretty (write, writeln) ;
17088
17089 l := alists.initList () ;
17090 alists.includeItemIntoList (l, n) ;
17091 i := 1 ;
17092 REPEAT
17093 n := alists.getItemFromList (l, i) ;
17094 IF isType (n)
17095 THEN
17096 s := getFQstring (n) ;
17097 IF EqualArray (s, symName)
17098 THEN
17099 out0 ("list ") ;
17100 out0 (listName) ;
17101 out0 (": ") ;
17102 doDbg (l, n)
17103 END ;
17104 s := KillString (s)
17105 END ;
17106 INC (i)
17107 UNTIL i>alists.noOfItemsInList (l) ;
17108 doP := o ;
17109 outputFile := f
17110 END dbg ;
17111
17112
17113 (*
17114 makeStatementSequence - create and return a statement sequence node.
17115 *)
17116
17117 PROCEDURE makeStatementSequence () : node ;
17118 VAR
17119 n: node ;
17120 BEGIN
17121 n := newNode (stmtseq) ;
17122 n^.stmtF.statements := InitIndex (1) ;
17123 RETURN n
17124 END makeStatementSequence ;
17125
17126
17127 (*
17128 addStatement - adds node, n, as a statement to statememt sequence, s.
17129 *)
17130
17131 PROCEDURE addStatement (s: node; n: node) ;
17132 BEGIN
17133 IF n#NIL
17134 THEN
17135 assert (isStatementSequence (s)) ;
17136 PutIndice (s^.stmtF.statements, HighIndice (s^.stmtF.statements) + 1, n) ;
17137 IF isIntrinsic (n) AND (n^.intrinsicF.postUnreachable)
17138 THEN
17139 n^.intrinsicF.postUnreachable := FALSE ;
17140 addStatement (s, makeIntrinsicProc (unreachable, 0, NIL))
17141 END
17142 END
17143 END addStatement ;
17144
17145
17146 (*
17147 isStatementSequence - returns TRUE if node, n, is a statement sequence.
17148 *)
17149
17150 PROCEDURE isStatementSequence (n: node) : BOOLEAN ;
17151 BEGIN
17152 RETURN n^.kind = stmtseq
17153 END isStatementSequence ;
17154
17155
17156 (*
17157 addGenericBody - adds comment node to funccall, return, assignment
17158 nodes.
17159 *)
17160
17161 PROCEDURE addGenericBody (n, c: node);
17162 BEGIN
17163 CASE n^.kind OF
17164
17165 unreachable,
17166 throw,
17167 halt,
17168 new,
17169 dispose,
17170 inc,
17171 dec,
17172 incl,
17173 excl : n^.intrinsicF.intrinsicComment.body := c |
17174 funccall : n^.funccallF.funccallComment.body := c |
17175 return : n^.returnF.returnComment.body := c |
17176 assignment: n^.assignmentF.assignComment.body := c |
17177 module : n^.moduleF.com.body := c |
17178 def : n^.defF.com.body := c |
17179 imp : n^.impF.com.body := c
17180
17181 ELSE
17182 END
17183 END addGenericBody;
17184
17185
17186 (*
17187 addGenericAfter - adds comment node to funccall, return, assignment
17188 nodes.
17189 *)
17190
17191 PROCEDURE addGenericAfter (n, c: node);
17192 BEGIN
17193 CASE n^.kind OF
17194
17195 unreachable,
17196 throw,
17197 halt,
17198 new,
17199 dispose,
17200 inc,
17201 dec,
17202 incl,
17203 excl : n^.intrinsicF.intrinsicComment.after := c |
17204 funccall : n^.funccallF.funccallComment.after := c |
17205 return : n^.returnF.returnComment.after := c |
17206 assignment: n^.assignmentF.assignComment.after := c |
17207 module : n^.moduleF.com.after := c |
17208 def : n^.defF.com.after := c |
17209 imp : n^.impF.com.after := c
17210
17211 ELSE
17212 END
17213 END addGenericAfter ;
17214
17215
17216 (*
17217 addCommentBody - adds a body comment to a statement sequence node.
17218 *)
17219
17220 PROCEDURE addCommentBody (n: node) ;
17221 VAR
17222 b: commentDesc ;
17223 BEGIN
17224 IF n # NIL
17225 THEN
17226 b := getBodyComment () ;
17227 IF b # NIL
17228 THEN
17229 addGenericBody (n, makeCommentS (b))
17230 END
17231 END
17232 END addCommentBody ;
17233
17234
17235 (*
17236 addCommentAfter - adds an after comment to a statement sequence node.
17237 *)
17238
17239 PROCEDURE addCommentAfter (n: node) ;
17240 VAR
17241 a: commentDesc ;
17242 BEGIN
17243 IF n # NIL
17244 THEN
17245 a := getAfterComment () ;
17246 IF a # NIL
17247 THEN
17248 addGenericAfter (n, makeCommentS (a))
17249 END
17250 END
17251 END addCommentAfter ;
17252
17253
17254 (*
17255 addIfComments - adds the, body, and, after, comments to if node, n.
17256 *)
17257
17258 PROCEDURE addIfComments (n: node; body, after: node) ;
17259 BEGIN
17260 assert (isIf (n)) ;
17261 n^.ifF.ifComment.after := after ;
17262 n^.ifF.ifComment.body := body
17263 END addIfComments ;
17264
17265
17266 (*
17267 addElseComments - adds the, body, and, after, comments to an, if, or an elsif, node, n.
17268 *)
17269
17270 PROCEDURE addElseComments (n: node; body, after: node) ;
17271 BEGIN
17272 assert (isIf (n) OR isElsif (n)) ;
17273 IF isIf (n)
17274 THEN
17275 n^.ifF.elseComment.after := after ;
17276 n^.ifF.elseComment.body := body
17277 ELSE
17278 n^.elsifF.elseComment.after := after ;
17279 n^.elsifF.elseComment.body := body
17280 END
17281 END addElseComments ;
17282
17283
17284 (*
17285 addIfEndComments - adds the, body, and, after, comments to an, if, node, n.
17286 *)
17287
17288 PROCEDURE addIfEndComments (n: node; body, after: node) ;
17289 BEGIN
17290 assert (isIf (n)) ;
17291 n^.ifF.endComment.after := after ;
17292 n^.ifF.endComment.body := body
17293 END addIfEndComments ;
17294
17295
17296 (*
17297 makeReturn - creates and returns a return node.
17298 *)
17299
17300 PROCEDURE makeReturn () : node ;
17301 VAR
17302 type,
17303 n : node ;
17304 BEGIN
17305 n := newNode (return) ;
17306 n^.returnF.exp := NIL ;
17307 IF isProcedure (getDeclScope ())
17308 THEN
17309 n^.returnF.scope := getDeclScope ()
17310 ELSE
17311 n^.returnF.scope := NIL
17312 END ;
17313 initPair (n^.returnF.returnComment) ;
17314 RETURN n
17315 END makeReturn ;
17316
17317
17318 (*
17319 isReturn - returns TRUE if node, n, is a return.
17320 *)
17321
17322 PROCEDURE isReturn (n: node) : BOOLEAN ;
17323 BEGIN
17324 assert (n # NIL) ;
17325 RETURN n^.kind = return
17326 END isReturn ;
17327
17328
17329 (*
17330 putReturn - assigns node, e, as the expression on the return node.
17331 *)
17332
17333 PROCEDURE putReturn (n: node; e: node) ;
17334 BEGIN
17335 assert (isReturn (n)) ;
17336 n^.returnF.exp := e
17337 END putReturn ;
17338
17339
17340 (*
17341 makeWhile - creates and returns a while node.
17342 *)
17343
17344 PROCEDURE makeWhile () : node ;
17345 VAR
17346 n: node ;
17347 BEGIN
17348 n := newNode (while) ;
17349 n^.whileF.expr := NIL ;
17350 n^.whileF.statements := NIL ;
17351 initPair (n^.whileF.doComment) ;
17352 initPair (n^.whileF.endComment) ;
17353 RETURN n
17354 END makeWhile ;
17355
17356
17357 (*
17358 putWhile - places an expression, e, and statement sequence, s, into the while
17359 node, n.
17360 *)
17361
17362 PROCEDURE putWhile (n: node; e, s: node) ;
17363 BEGIN
17364 assert (isWhile (n)) ;
17365 n^.whileF.expr := e ;
17366 n^.whileF.statements := s
17367 END putWhile ;
17368
17369
17370 (*
17371 isWhile - returns TRUE if node, n, is a while.
17372 *)
17373
17374 PROCEDURE isWhile (n: node) : BOOLEAN ;
17375 BEGIN
17376 RETURN n^.kind = while
17377 END isWhile ;
17378
17379
17380 (*
17381 addWhileDoComment - adds body and after comments to while node, w.
17382 *)
17383
17384 PROCEDURE addWhileDoComment (w: node; body, after: node) ;
17385 BEGIN
17386 assert (isWhile (w)) ;
17387 w^.whileF.doComment.after := after ;
17388 w^.whileF.doComment.body := body
17389 END addWhileDoComment ;
17390
17391
17392 (*
17393 addWhileEndComment - adds body and after comments to the end of a while node, w.
17394 *)
17395
17396 PROCEDURE addWhileEndComment (w: node; body, after: node) ;
17397 BEGIN
17398 assert (isWhile (w)) ;
17399 w^.whileF.endComment.after := after ;
17400 w^.whileF.endComment.body := body
17401 END addWhileEndComment ;
17402
17403
17404 (*
17405 makeAssignment - creates and returns an assignment node.
17406 The designator is, d, and expression, e.
17407 *)
17408
17409 PROCEDURE makeAssignment (d, e: node) : node ;
17410 VAR
17411 n: node ;
17412 BEGIN
17413 n := newNode (assignment) ;
17414 n^.assignmentF.des := d ;
17415 n^.assignmentF.expr := e ;
17416 initPair (n^.assignmentF.assignComment) ;
17417 RETURN n
17418 END makeAssignment ;
17419
17420
17421 (*
17422 isAssignment -
17423 *)
17424
17425 PROCEDURE isAssignment (n: node) : BOOLEAN ;
17426 BEGIN
17427 RETURN n^.kind = assignment
17428 END isAssignment ;
17429
17430
17431 (*
17432 putBegin - assigns statements, s, to be the normal part in
17433 block, b. The block may be a procedure or module,
17434 or implementation node.
17435 *)
17436
17437 PROCEDURE putBegin (b: node; s: node) ;
17438 BEGIN
17439 assert (isImp (b) OR isProcedure (b) OR isModule (b)) ;
17440 CASE b^.kind OF
17441
17442 imp : b^.impF.beginStatements := s |
17443 module : b^.moduleF.beginStatements := s |
17444 procedure: b^.procedureF.beginStatements := s
17445
17446 END
17447 END putBegin ;
17448
17449
17450 (*
17451 putFinally - assigns statements, s, to be the final part in
17452 block, b. The block may be a module
17453 or implementation node.
17454 *)
17455
17456 PROCEDURE putFinally (b: node; s: node) ;
17457 BEGIN
17458 assert (isImp (b) OR isProcedure (b) OR isModule (b)) ;
17459 CASE b^.kind OF
17460
17461 imp : b^.impF.finallyStatements := s |
17462 module : b^.moduleF.finallyStatements := s
17463
17464 END
17465 END putFinally ;
17466
17467
17468 (*
17469 makeExit - creates and returns an exit node.
17470 *)
17471
17472 PROCEDURE makeExit (l: node; n: CARDINAL) : node ;
17473 VAR
17474 e: node ;
17475 BEGIN
17476 assert (isLoop (l)) ;
17477 e := newNode (exit) ;
17478 e^.exitF.loop := l ;
17479 l^.loopF.labelno := n ;
17480 RETURN e
17481 END makeExit ;
17482
17483
17484 (*
17485 isExit - returns TRUE if node, n, is an exit.
17486 *)
17487
17488 PROCEDURE isExit (n: node) : BOOLEAN ;
17489 BEGIN
17490 assert (n # NIL) ;
17491 RETURN n^.kind = exit
17492 END isExit ;
17493
17494
17495 (*
17496 makeLoop - creates and returns a loop node.
17497 *)
17498
17499 PROCEDURE makeLoop () : node ;
17500 VAR
17501 l: node ;
17502 BEGIN
17503 l := newNode (loop) ;
17504 l^.loopF.statements := NIL ;
17505 l^.loopF.labelno := 0 ;
17506 RETURN l
17507 END makeLoop ;
17508
17509
17510 (*
17511 putLoop - places statement sequence, s, into loop, l.
17512 *)
17513
17514 PROCEDURE putLoop (l, s: node) ;
17515 BEGIN
17516 assert (isLoop (l)) ;
17517 l^.loopF.statements := s
17518 END putLoop ;
17519
17520
17521 (*
17522 isLoop - returns TRUE if, n, is a loop node.
17523 *)
17524
17525 PROCEDURE isLoop (n: node) : BOOLEAN ;
17526 BEGIN
17527 assert (n # NIL) ;
17528 RETURN n^.kind = loop
17529 END isLoop ;
17530
17531
17532 (*
17533 makeComment - creates and returns a comment node.
17534 *)
17535
17536 PROCEDURE makeComment (a: ARRAY OF CHAR) : node ;
17537 VAR
17538 c: commentDesc ;
17539 s: String ;
17540 BEGIN
17541 c := initComment (TRUE) ;
17542 s := InitString (a) ;
17543 addText (c, DynamicStrings.string (s)) ;
17544 s := KillString (s) ;
17545 RETURN makeCommentS (c)
17546 END makeComment ;
17547
17548
17549 (*
17550 makeCommentS - creates and returns a comment node.
17551 *)
17552
17553 PROCEDURE makeCommentS (c: commentDesc) : node ;
17554 VAR
17555 n: node ;
17556 BEGIN
17557 IF c = NIL
17558 THEN
17559 RETURN NIL
17560 ELSE
17561 n := newNode (comment) ;
17562 n^.commentF.content := c ;
17563 RETURN n
17564 END
17565 END makeCommentS ;
17566
17567
17568 (*
17569 isComment - returns TRUE if node, n, is a comment.
17570 *)
17571
17572 PROCEDURE isComment (n: node) : BOOLEAN ;
17573 BEGIN
17574 assert (n # NIL) ;
17575 RETURN n^.kind = comment
17576 END isComment ;
17577
17578
17579 (*
17580 initPair - initialise the commentPair, c.
17581 *)
17582
17583 PROCEDURE initPair (VAR c: commentPair) ;
17584 BEGIN
17585 c.after := NIL ;
17586 c.body := NIL
17587 END initPair ;
17588
17589
17590 (*
17591 makeIf - creates and returns an if node. The if node
17592 will have expression, e, and statement sequence, s,
17593 as the then component.
17594 *)
17595
17596 PROCEDURE makeIf (e, s: node) : node ;
17597 VAR
17598 n: node ;
17599 BEGIN
17600 n := newNode (if) ;
17601 n^.ifF.expr := e ;
17602 n^.ifF.then := s ;
17603 n^.ifF.else := NIL ;
17604 n^.ifF.elsif := NIL ;
17605 initPair (n^.ifF.ifComment) ;
17606 initPair (n^.ifF.elseComment) ;
17607 initPair (n^.ifF.endComment) ;
17608 RETURN n
17609 END makeIf ;
17610
17611
17612 (*
17613 isIf - returns TRUE if, n, is an if node.
17614 *)
17615
17616 PROCEDURE isIf (n: node) : BOOLEAN ;
17617 BEGIN
17618 RETURN n^.kind = if
17619 END isIf ;
17620
17621
17622 (*
17623 makeElsif - creates and returns an elsif node.
17624 This node has an expression, e, and statement
17625 sequence, s.
17626 *)
17627
17628 PROCEDURE makeElsif (i, e, s: node) : node ;
17629 VAR
17630 n: node ;
17631 BEGIN
17632 n := newNode (elsif) ;
17633 n^.elsifF.expr := e ;
17634 n^.elsifF.then := s ;
17635 n^.elsifF.elsif := NIL ;
17636 n^.elsifF.else := NIL ;
17637 initPair (n^.elsifF.elseComment) ;
17638 assert (isIf (i) OR isElsif (i)) ;
17639 IF isIf (i)
17640 THEN
17641 i^.ifF.elsif := n ;
17642 assert (i^.ifF.else = NIL)
17643 ELSE
17644 i^.elsifF.elsif := n ;
17645 assert (i^.elsifF.else = NIL)
17646 END ;
17647 RETURN n
17648 END makeElsif ;
17649
17650
17651 (*
17652 isElsif - returns TRUE if node, n, is an elsif node.
17653 *)
17654
17655 PROCEDURE isElsif (n: node) : BOOLEAN ;
17656 BEGIN
17657 RETURN n^.kind = elsif
17658 END isElsif ;
17659
17660
17661 (*
17662 putElse - the else is grafted onto the if/elsif node, i,
17663 and the statement sequence will be, s.
17664 *)
17665
17666 PROCEDURE putElse (i, s: node) ;
17667 BEGIN
17668 assert (isIf (i) OR isElsif (i)) ;
17669 IF isIf (i)
17670 THEN
17671 assert (i^.ifF.elsif = NIL) ;
17672 assert (i^.ifF.else = NIL) ;
17673 i^.ifF.else := s
17674 ELSE
17675 assert (i^.elsifF.elsif = NIL) ;
17676 assert (i^.elsifF.else = NIL) ;
17677 i^.elsifF.else := s
17678 END
17679 END putElse ;
17680
17681
17682 (*
17683 makeFor - creates and returns a for node.
17684 *)
17685
17686 PROCEDURE makeFor () : node ;
17687 VAR
17688 n: node ;
17689 BEGIN
17690 n := newNode (for) ;
17691 n^.forF.des := NIL ;
17692 n^.forF.start := NIL ;
17693 n^.forF.end := NIL ;
17694 n^.forF.increment := NIL ;
17695 n^.forF.statements := NIL ;
17696 RETURN n
17697 END makeFor ;
17698
17699
17700 (*
17701 isFor - returns TRUE if node, n, is a for node.
17702 *)
17703
17704 PROCEDURE isFor (n: node) : BOOLEAN ;
17705 BEGIN
17706 assert (n # NIL) ;
17707 RETURN n^.kind = for
17708 END isFor ;
17709
17710
17711 (*
17712 putFor - assigns the fields of the for node with
17713 ident, i,
17714 start, s,
17715 end, e,
17716 increment, i,
17717 statements, sq.
17718 *)
17719
17720 PROCEDURE putFor (f, i, s, e, b, sq: node) ;
17721 BEGIN
17722 assert (isFor (f)) ;
17723 f^.forF.des := i ;
17724 f^.forF.start := s ;
17725 f^.forF.end := e ;
17726 f^.forF.increment := b ;
17727 f^.forF.statements := sq
17728 END putFor ;
17729
17730
17731 (*
17732 makeRepeat - creates and returns a repeat node.
17733 *)
17734
17735 PROCEDURE makeRepeat () : node ;
17736 VAR
17737 n: node ;
17738 BEGIN
17739 n := newNode (repeat) ;
17740 n^.repeatF.expr := NIL ;
17741 n^.repeatF.statements := NIL ;
17742 initPair (n^.repeatF.repeatComment) ;
17743 initPair (n^.repeatF.untilComment) ;
17744 RETURN n
17745 END makeRepeat ;
17746
17747
17748 (*
17749 isRepeat - returns TRUE if node, n, is a repeat node.
17750 *)
17751
17752 PROCEDURE isRepeat (n: node) : BOOLEAN ;
17753 BEGIN
17754 assert (n # NIL) ;
17755 RETURN n^.kind = repeat
17756 END isRepeat ;
17757
17758
17759 (*
17760 putRepeat - places statements, s, and expression, e, into
17761 repeat statement, n.
17762 *)
17763
17764 PROCEDURE putRepeat (n, s, e: node) ;
17765 BEGIN
17766 n^.repeatF.expr := e ;
17767 n^.repeatF.statements := s
17768 END putRepeat ;
17769
17770
17771 (*
17772 addRepeatComment - adds body and after comments to repeat node, r.
17773 *)
17774
17775 PROCEDURE addRepeatComment (r: node; body, after: node) ;
17776 BEGIN
17777 assert (isRepeat (r)) ;
17778 r^.repeatF.repeatComment.after := after ;
17779 r^.repeatF.repeatComment.body := body
17780 END addRepeatComment ;
17781
17782
17783 (*
17784 addUntilComment - adds body and after comments to the until section of a repeat node, r.
17785 *)
17786
17787 PROCEDURE addUntilComment (r: node; body, after: node) ;
17788 BEGIN
17789 assert (isRepeat (r)) ;
17790 r^.repeatF.untilComment.after := after ;
17791 r^.repeatF.untilComment.body := body
17792 END addUntilComment ;
17793
17794
17795 (*
17796 makeCase - builds and returns a case statement node.
17797 *)
17798
17799 PROCEDURE makeCase () : node ;
17800 VAR
17801 n: node ;
17802 BEGIN
17803 n := newNode (case) ;
17804 n^.caseF.expression := NIL ;
17805 n^.caseF.caseLabelList := InitIndex (1) ;
17806 n^.caseF.else := NIL ;
17807 RETURN n
17808 END makeCase ;
17809
17810
17811 (*
17812 isCase - returns TRUE if node, n, is a case statement.
17813 *)
17814
17815 PROCEDURE isCase (n: node) : BOOLEAN ;
17816 BEGIN
17817 assert (n # NIL) ;
17818 RETURN n^.kind = case
17819 END isCase ;
17820
17821
17822 (*
17823 putCaseExpression - places expression, e, into case statement, n.
17824 n is returned.
17825 *)
17826
17827 PROCEDURE putCaseExpression (n: node; e: node) : node ;
17828 BEGIN
17829 assert (isCase (n)) ;
17830 n^.caseF.expression := e ;
17831 RETURN n
17832 END putCaseExpression ;
17833
17834
17835 (*
17836 putCaseElse - places else statement, e, into case statement, n.
17837 n is returned.
17838 *)
17839
17840 PROCEDURE putCaseElse (n: node; e: node) : node ;
17841 BEGIN
17842 assert (isCase (n)) ;
17843 n^.caseF.else := e ;
17844 RETURN n
17845 END putCaseElse ;
17846
17847
17848 (*
17849 putCaseStatement - places a caselist, l, and associated
17850 statement sequence, s, into case statement, n.
17851 n is returned.
17852 *)
17853
17854 PROCEDURE putCaseStatement (n: node; l: node; s: node) : node ;
17855 BEGIN
17856 assert (isCase (n)) ;
17857 assert (isCaseList (l)) ;
17858 IncludeIndiceIntoIndex (n^.caseF.caseLabelList, makeCaseLabelList (l, s)) ;
17859 RETURN n
17860 END putCaseStatement ;
17861
17862
17863 (*
17864 makeCaseLabelList - creates and returns a caselabellist node.
17865 *)
17866
17867 PROCEDURE makeCaseLabelList (l, s: node) : node ;
17868 VAR
17869 n: node ;
17870 BEGIN
17871 n := newNode (caselabellist) ;
17872 n^.caselabellistF.caseList := l ;
17873 n^.caselabellistF.statements := s ;
17874 RETURN n
17875 END makeCaseLabelList ;
17876
17877
17878 (*
17879 isCaseLabelList - returns TRUE if, n, is a caselabellist.
17880 *)
17881
17882 PROCEDURE isCaseLabelList (n: node) : BOOLEAN ;
17883 BEGIN
17884 assert (n # NIL) ;
17885 RETURN n^.kind = caselabellist
17886 END isCaseLabelList ;
17887
17888
17889 (*
17890 makeCaseList - creates and returns a case statement node.
17891 *)
17892
17893 PROCEDURE makeCaseList () : node ;
17894 VAR
17895 n: node ;
17896 BEGIN
17897 n := newNode (caselist) ;
17898 n^.caselistF.rangePairs := InitIndex (1) ;
17899 RETURN n
17900 END makeCaseList ;
17901
17902
17903 (*
17904 isCaseList - returns TRUE if, n, is a case list.
17905 *)
17906
17907 PROCEDURE isCaseList (n: node) : BOOLEAN ;
17908 BEGIN
17909 assert (n # NIL) ;
17910 RETURN n^.kind = caselist
17911 END isCaseList ;
17912
17913
17914 (*
17915 putCaseRange - places the case range lo..hi into caselist, n.
17916 *)
17917
17918 PROCEDURE putCaseRange (n: node; lo, hi: node) : node ;
17919 BEGIN
17920 assert (isCaseList (n)) ;
17921 IncludeIndiceIntoIndex (n^.caselistF.rangePairs, makeRange (lo, hi)) ;
17922 RETURN n
17923 END putCaseRange ;
17924
17925
17926 (*
17927 makeRange - creates and returns a case range.
17928 *)
17929
17930 PROCEDURE makeRange (lo, hi: node) : node ;
17931 VAR
17932 n: node ;
17933 BEGIN
17934 n := newNode (range) ;
17935 n^.rangeF.lo := lo ;
17936 n^.rangeF.hi := hi ;
17937 RETURN n
17938 END makeRange ;
17939
17940
17941 (*
17942 isRange - returns TRUE if node, n, is a range.
17943 *)
17944
17945 PROCEDURE isRange (n: node) : BOOLEAN ;
17946 BEGIN
17947 assert (n # NIL) ;
17948 RETURN n^.kind = range
17949 END isRange ;
17950
17951
17952 (*
17953 dupExplist -
17954 *)
17955
17956 PROCEDURE dupExplist (n: node) : node ;
17957 VAR
17958 m: node ;
17959 i: CARDINAL ;
17960 BEGIN
17961 assert (isExpList (n)) ;
17962 m := makeExpList () ;
17963 i := LowIndice (n^.explistF.exp) ;
17964 WHILE i <= HighIndice (n^.explistF.exp) DO
17965 putExpList (m, dupExpr (GetIndice (n^.explistF.exp, i))) ;
17966 INC (i)
17967 END ;
17968 RETURN m
17969 END dupExplist ;
17970
17971
17972 (*
17973 dupArrayref -
17974 *)
17975
17976 PROCEDURE dupArrayref (n: node) : node ;
17977 BEGIN
17978 assert (isArrayRef (n)) ;
17979 RETURN makeArrayRef (dupExpr (n^.arrayrefF.array), dupExpr (n^.arrayrefF.index))
17980 END dupArrayref ;
17981
17982
17983 (*
17984 dupPointerref -
17985 *)
17986
17987 PROCEDURE dupPointerref (n: node) : node ;
17988 BEGIN
17989 assert (isPointerRef (n)) ;
17990 RETURN makePointerRef (dupExpr (n^.pointerrefF.ptr), dupExpr (n^.pointerrefF.field))
17991 END dupPointerref ;
17992
17993
17994 (*
17995 dupComponentref -
17996 *)
17997
17998 PROCEDURE dupComponentref (n: node) : node ;
17999 BEGIN
18000 assert (isComponentRef (n)) ;
18001 RETURN doMakeComponentRef (dupExpr (n^.componentrefF.rec), dupExpr (n^.componentrefF.field))
18002 END dupComponentref ;
18003
18004
18005 (*
18006 dupBinary -
18007 *)
18008
18009 PROCEDURE dupBinary (n: node) : node ;
18010 BEGIN
18011 (* assert (isBinary (n)) ; *)
18012 RETURN makeBinary (n^.kind,
18013 dupExpr (n^.binaryF.left), dupExpr (n^.binaryF.right),
18014 n^.binaryF.resultType)
18015 END dupBinary ;
18016
18017
18018 (*
18019 dupUnary -
18020 *)
18021
18022 PROCEDURE dupUnary (n: node) : node ;
18023 BEGIN
18024 (* assert (isUnary (n)) ; *)
18025 RETURN makeUnary (n^.kind, dupExpr (n^.unaryF.arg), n^.unaryF.resultType)
18026 END dupUnary ;
18027
18028
18029 (*
18030 dupFunccall -
18031 *)
18032
18033 PROCEDURE dupFunccall (n: node) : node ;
18034 VAR
18035 m: node ;
18036 BEGIN
18037 assert (isFuncCall (n)) ;
18038 m := makeFuncCall (dupExpr (n^.funccallF.function), dupExpr (n^.funccallF.args)) ;
18039 m^.funccallF.type := n^.funccallF.type ;
18040 assignNodeOpaqueCastState (m, n) ;
18041 RETURN m
18042 END dupFunccall ;
18043
18044
18045 (*
18046 dupSetValue -
18047 *)
18048
18049 PROCEDURE dupSetValue (n: node) : node ;
18050 VAR
18051 m: node ;
18052 i: CARDINAL ;
18053 BEGIN
18054 m := newNode (setvalue) ;
18055 m^.setvalueF.type := n^.setvalueF.type ;
18056 i := LowIndice (n^.setvalueF.values) ;
18057 WHILE i <= HighIndice (n^.setvalueF.values) DO
18058 m := putSetValue (m, dupExpr (GetIndice (n^.setvalueF.values, i))) ;
18059 INC (i)
18060 END ;
18061 RETURN m
18062 END dupSetValue ;
18063
18064
18065 (*
18066 dupExpr - duplicate the expression nodes, it does not duplicate
18067 variables, literals, constants but only the expression
18068 operators (including function calls and parameter lists).
18069 *)
18070
18071 PROCEDURE dupExpr (n: node) : node ;
18072 BEGIN
18073 IF n = NIL
18074 THEN
18075 RETURN NIL
18076 ELSE
18077 RETURN doDupExpr (n)
18078 END
18079 END dupExpr ;
18080
18081
18082 (*
18083 doDupExpr -
18084 *)
18085
18086 PROCEDURE doDupExpr (n: node) : node ;
18087 BEGIN
18088 assert (n # NIL) ;
18089 CASE n^.kind OF
18090
18091 explist : RETURN dupExplist (n) |
18092 exit,
18093 return,
18094 stmtseq,
18095 comment : HALT | (* should not be duplicating code. *)
18096 length : HALT | (* length should have been converted into unary. *)
18097 (* base constants. *)
18098 nil,
18099 true,
18100 false,
18101 (* system types. *)
18102 address,
18103 loc,
18104 byte,
18105 word,
18106 csizet,
18107 cssizet,
18108 cofft,
18109 cardinal64,
18110 (* base types. *)
18111 boolean,
18112 proc,
18113 char,
18114 integer,
18115 cardinal,
18116 longcard,
18117 shortcard,
18118 longint,
18119 shortint,
18120 real,
18121 longreal,
18122 shortreal,
18123 bitset,
18124 ztype,
18125 rtype,
18126 complex,
18127 longcomplex,
18128 shortcomplex : RETURN n |
18129 (* language features and compound type attributes. *)
18130 type,
18131 record,
18132 varient,
18133 var,
18134 enumeration,
18135 subrange,
18136 subscript,
18137 array,
18138 string,
18139 const,
18140 literal,
18141 varparam,
18142 param,
18143 varargs,
18144 optarg,
18145 pointer,
18146 recordfield,
18147 varientfield,
18148 enumerationfield,
18149 set,
18150 proctype : RETURN n |
18151 (* blocks. *)
18152 procedure,
18153 def,
18154 imp,
18155 module : RETURN n |
18156 (* statements. *)
18157 loop,
18158 while,
18159 for,
18160 repeat,
18161 case,
18162 caselabellist,
18163 caselist,
18164 range,
18165 if,
18166 elsif,
18167 assignment : RETURN n |
18168 (* expressions. *)
18169 arrayref : RETURN dupArrayref (n) |
18170 pointerref : RETURN dupPointerref (n) |
18171 componentref : RETURN dupComponentref (n) |
18172 cmplx,
18173 and,
18174 or,
18175 equal,
18176 notequal,
18177 less,
18178 greater,
18179 greequal,
18180 lessequal,
18181 cast,
18182 val,
18183 plus,
18184 sub,
18185 div,
18186 mod,
18187 mult,
18188 divide,
18189 in : RETURN dupBinary (n) |
18190 re,
18191 im,
18192 constexp,
18193 deref,
18194 abs,
18195 chr,
18196 cap,
18197 high,
18198 float,
18199 trunc,
18200 ord,
18201 not,
18202 neg,
18203 adr,
18204 size,
18205 tsize,
18206 min,
18207 max : RETURN dupUnary (n) |
18208 identlist : RETURN n |
18209 vardecl : RETURN n |
18210 funccall : RETURN dupFunccall (n) |
18211 setvalue : RETURN dupSetValue (n)
18212
18213 END
18214 END doDupExpr ;
18215
18216
18217 (*
18218 setNoReturn - sets noreturn field inside procedure.
18219 *)
18220
18221 PROCEDURE setNoReturn (n: node; value: BOOLEAN) ;
18222 BEGIN
18223 assert (n#NIL) ;
18224 assert (isProcedure (n)) ;
18225 IF n^.procedureF.noreturnused AND (n^.procedureF.noreturn # value)
18226 THEN
18227 metaError1 ('{%1DMad} definition module and implementation module have different <* noreturn *> attributes', n) ;
18228 END ;
18229 n^.procedureF.noreturn := value ;
18230 n^.procedureF.noreturnused := TRUE
18231 END setNoReturn ;
18232
18233
18234 (*
18235 makeSystem -
18236 *)
18237
18238 PROCEDURE makeSystem ;
18239 BEGIN
18240 systemN := lookupDef (makeKey ('SYSTEM')) ;
18241
18242 addressN := makeBase (address) ;
18243 locN := makeBase (loc) ;
18244 byteN := makeBase (byte) ;
18245 wordN := makeBase (word) ;
18246 csizetN := makeBase (csizet) ;
18247 cssizetN := makeBase (cssizet) ;
18248 cofftN := makeBase (cofft) ;
18249 cardinal64N := makeBase (cardinal64) ;
18250
18251 adrN := makeBase (adr) ;
18252 tsizeN := makeBase (tsize) ;
18253 throwN := makeBase (throw) ;
18254
18255 enterScope (systemN) ;
18256 addressN := addToScope (addressN) ;
18257 locN := addToScope (locN) ;
18258 byteN := addToScope (byteN) ;
18259 wordN := addToScope (wordN) ;
18260 csizetN := addToScope (csizetN) ;
18261 cssizetN := addToScope (cssizetN) ;
18262 cofftN := addToScope (cofftN) ;
18263 cardinal64N := addToScope (cardinal64N) ;
18264 adrN := addToScope (adrN) ;
18265 tsizeN := addToScope (tsizeN) ;
18266 throwN := addToScope (throwN) ;
18267
18268 assert (sizeN#NIL) ; (* assumed to be built already. *)
18269 sizeN := addToScope (sizeN) ; (* also export size from system. *)
18270 leaveScope ;
18271
18272 addDone (addressN) ;
18273 addDone (locN) ;
18274 addDone (byteN) ;
18275 addDone (wordN) ;
18276 addDone (csizetN) ;
18277 addDone (cssizetN) ;
18278 addDone (cofftN) ;
18279 addDone (cardinal64N)
18280 END makeSystem ;
18281
18282
18283 (*
18284 makeM2rts -
18285 *)
18286
18287 PROCEDURE makeM2rts ;
18288 BEGIN
18289 m2rtsN := lookupDef (makeKey ('M2RTS'))
18290 END makeM2rts ;
18291
18292
18293 (*
18294 makeBitnum -
18295 *)
18296
18297 PROCEDURE makeBitnum () : node ;
18298 VAR
18299 b: node ;
18300 BEGIN
18301 b := newNode (subrange) ;
18302 b^.subrangeF.type := NIL ;
18303 b^.subrangeF.scope := NIL ;
18304 b^.subrangeF.low := lookupConst (b, makeKey ('0')) ;
18305 b^.subrangeF.high := lookupConst (b, makeKey ('31')) ;
18306 RETURN b
18307 END makeBitnum ;
18308
18309
18310 (*
18311 makeBaseSymbols -
18312 *)
18313
18314 PROCEDURE makeBaseSymbols ;
18315 BEGIN
18316 baseSymbols := initTree () ;
18317
18318 booleanN := makeBase (boolean) ;
18319 charN := makeBase (char) ;
18320 procN := makeBase (proc) ;
18321 cardinalN := makeBase (cardinal) ;
18322 longcardN := makeBase (longcard) ;
18323 shortcardN := makeBase (shortcard) ;
18324 integerN := makeBase (integer) ;
18325 longintN := makeBase (longint) ;
18326 shortintN := makeBase (shortint) ;
18327 bitsetN := makeBase (bitset) ;
18328 bitnumN := makeBitnum () ;
18329 ztypeN := makeBase (ztype) ;
18330 rtypeN := makeBase (rtype) ;
18331 complexN := makeBase (complex) ;
18332 longcomplexN := makeBase (longcomplex) ;
18333 shortcomplexN := makeBase (shortcomplex) ;
18334 realN := makeBase (real) ;
18335 longrealN := makeBase (longreal) ;
18336 shortrealN := makeBase (shortreal) ;
18337
18338 nilN := makeBase (nil) ;
18339 trueN := makeBase (true) ;
18340 falseN := makeBase (false) ;
18341
18342 sizeN := makeBase (size) ;
18343 minN := makeBase (min) ;
18344 maxN := makeBase (max) ;
18345 floatN := makeBase (float) ;
18346 truncN := makeBase (trunc) ;
18347 ordN := makeBase (ord) ;
18348 valN := makeBase (val) ;
18349 chrN := makeBase (chr) ;
18350 capN := makeBase (cap) ;
18351 absN := makeBase (abs) ;
18352 newN := makeBase (new) ;
18353 disposeN := makeBase (dispose) ;
18354 lengthN := makeBase (length) ;
18355 incN := makeBase (inc) ;
18356 decN := makeBase (dec) ;
18357 inclN := makeBase (incl) ;
18358 exclN := makeBase (excl) ;
18359 highN := makeBase (high) ;
18360 imN := makeBase (im) ;
18361 reN := makeBase (re) ;
18362 cmplxN := makeBase (cmplx) ;
18363
18364 putSymKey (baseSymbols, makeKey ('BOOLEAN'), booleanN) ;
18365 putSymKey (baseSymbols, makeKey ('PROC'), procN) ;
18366 putSymKey (baseSymbols, makeKey ('CHAR'), charN) ;
18367 putSymKey (baseSymbols, makeKey ('CARDINAL'), cardinalN) ;
18368 putSymKey (baseSymbols, makeKey ('SHORTCARD'), shortcardN) ;
18369 putSymKey (baseSymbols, makeKey ('LONGCARD'), longcardN) ;
18370 putSymKey (baseSymbols, makeKey ('INTEGER'), integerN) ;
18371 putSymKey (baseSymbols, makeKey ('LONGINT'), longintN) ;
18372 putSymKey (baseSymbols, makeKey ('SHORTINT'), shortintN) ;
18373 putSymKey (baseSymbols, makeKey ('BITSET'), bitsetN) ;
18374 putSymKey (baseSymbols, makeKey ('REAL'), realN) ;
18375 putSymKey (baseSymbols, makeKey ('SHORTREAL'), shortrealN) ;
18376 putSymKey (baseSymbols, makeKey ('LONGREAL'), longrealN) ;
18377 putSymKey (baseSymbols, makeKey ('COMPLEX'), complexN) ;
18378 putSymKey (baseSymbols, makeKey ('LONGCOMPLEX'), longcomplexN) ;
18379 putSymKey (baseSymbols, makeKey ('SHORTCOMPLEX'), shortcomplexN) ;
18380
18381 putSymKey (baseSymbols, makeKey ('NIL'), nilN) ;
18382 putSymKey (baseSymbols, makeKey ('TRUE'), trueN) ;
18383 putSymKey (baseSymbols, makeKey ('FALSE'), falseN) ;
18384 putSymKey (baseSymbols, makeKey ('SIZE'), sizeN) ;
18385 putSymKey (baseSymbols, makeKey ('MIN'), minN) ;
18386 putSymKey (baseSymbols, makeKey ('MAX'), maxN) ;
18387 putSymKey (baseSymbols, makeKey ('FLOAT'), floatN) ;
18388 putSymKey (baseSymbols, makeKey ('TRUNC'), truncN) ;
18389 putSymKey (baseSymbols, makeKey ('ORD'), ordN) ;
18390 putSymKey (baseSymbols, makeKey ('VAL'), valN) ;
18391 putSymKey (baseSymbols, makeKey ('CHR'), chrN) ;
18392 putSymKey (baseSymbols, makeKey ('CAP'), capN) ;
18393 putSymKey (baseSymbols, makeKey ('ABS'), absN) ;
18394 putSymKey (baseSymbols, makeKey ('NEW'), newN) ;
18395 putSymKey (baseSymbols, makeKey ('DISPOSE'), disposeN) ;
18396 putSymKey (baseSymbols, makeKey ('LENGTH'), lengthN) ;
18397 putSymKey (baseSymbols, makeKey ('INC'), incN) ;
18398 putSymKey (baseSymbols, makeKey ('DEC'), decN) ;
18399 putSymKey (baseSymbols, makeKey ('INCL'), inclN) ;
18400 putSymKey (baseSymbols, makeKey ('EXCL'), exclN) ;
18401 putSymKey (baseSymbols, makeKey ('HIGH'), highN) ;
18402 putSymKey (baseSymbols, makeKey ('CMPLX'), cmplxN) ;
18403 putSymKey (baseSymbols, makeKey ('RE'), reN) ;
18404 putSymKey (baseSymbols, makeKey ('IM'), imN) ;
18405
18406 addDone (booleanN) ;
18407 addDone (charN) ;
18408 addDone (cardinalN) ;
18409 addDone (longcardN) ;
18410 addDone (shortcardN) ;
18411 addDone (integerN) ;
18412 addDone (longintN) ;
18413 addDone (shortintN) ;
18414 addDone (bitsetN) ;
18415 addDone (bitnumN) ;
18416 addDone (ztypeN) ;
18417 addDone (rtypeN) ;
18418 addDone (realN) ;
18419 addDone (longrealN) ;
18420 addDone (shortrealN) ;
18421 addDone (complexN) ;
18422 addDone (longcomplexN) ;
18423 addDone (shortcomplexN) ;
18424 addDone (procN) ;
18425 addDone (nilN) ;
18426 addDone (trueN) ;
18427 addDone (falseN)
18428
18429 END makeBaseSymbols ;
18430
18431
18432 (*
18433 makeBuiltins -
18434 *)
18435
18436 PROCEDURE makeBuiltins ;
18437 BEGIN
18438 bitsperunitN := makeLiteralInt (makeKey ('8')) ;
18439 bitsperwordN := makeLiteralInt (makeKey ('32')) ;
18440 bitspercharN := makeLiteralInt (makeKey ('8')) ;
18441 unitsperwordN := makeLiteralInt (makeKey ('4')) ;
18442
18443 addDone (bitsperunitN) ;
18444 addDone (bitsperwordN) ;
18445 addDone (bitspercharN) ;
18446 addDone (unitsperwordN)
18447 END makeBuiltins ;
18448
18449
18450 (*
18451 makeCDataTypes - assign the charStarN and constCharStarN to NIL.
18452 *)
18453
18454 PROCEDURE makeCDataTypes ;
18455 VAR
18456 CdatatypesN: node ;
18457 BEGIN
18458 CdatatypesN := lookupDef (makeKey ('CDataTypes')) ;
18459 enterScope (CdatatypesN) ;
18460 charStarN := makePointer (charN) ;
18461 constCharStarN := makePointer (charN) ;
18462 leaveScope
18463 END makeCDataTypes ;
18464
18465
18466 (*
18467 init -
18468 *)
18469
18470 PROCEDURE init ;
18471 BEGIN
18472 lang := ansiC ;
18473 outputFile := StdOut ;
18474 doP := initPretty (write, writeln) ;
18475 freeGroup := NIL ;
18476 globalGroup := initGroup () ;
18477 modUniverse := initTree () ;
18478 defUniverse := initTree () ;
18479 modUniverseI := InitIndex (1) ;
18480 defUniverseI := InitIndex (1) ;
18481 scopeStack := InitIndex (1) ;
18482 makeBaseSymbols ;
18483 makeSystem ;
18484 makeBuiltins ;
18485 makeM2rts ;
18486 outputState := punct ;
18487 tempCount := 0 ;
18488 mustVisitScope := FALSE ;
18489 makeCDataTypes
18490 END init ;
18491
18492
18493 BEGIN
18494 init
18495 END decl.