Debian Codegraph @

gcc/m2/gm2-compiler/M2SymInit.mod

raw
1 (* M2SymInit.mod records initialization state for variables.
2
3 Copyright (C) 2001-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 M2SymInit ;
23
24 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
25 FROM M2Debug IMPORT Assert ;
26 FROM M2Printf IMPORT printf0, printf1, printf2, printf3, printf4 ;
27 FROM libc IMPORT printf ;
28 FROM NameKey IMPORT Name, NulName, KeyToCharStar, MakeKey ;
29 FROM M2Base IMPORT Nil ;
30
31 FROM M2Options IMPORT UninitVariableChecking, UninitVariableConditionalChecking,
32 CompilerDebugging ;
33
34 FROM M2MetaError IMPORT MetaErrorT1, MetaErrorStringT1, MetaErrorStringT2 ;
35 FROM M2LexBuf IMPORT UnknownTokenNo ;
36 FROM DynamicStrings IMPORT String, InitString, Mark, ConCat, InitString ;
37 FROM M2Error IMPORT InternalError ;
38
39 FROM M2BasicBlock IMPORT BasicBlock,
40 InitBasicBlocks, InitBasicBlocksFromRange,
41 KillBasicBlocks, FreeBasicBlocks,
42 ForeachBasicBlockDo,
43 GetBasicBlockStart, GetBasicBlockEnd ;
44
45 IMPORT Indexing ;
46 FROM Indexing IMPORT Index ;
47
48 FROM Lists IMPORT List, InitList, GetItemFromList, PutItemIntoList,
49 IsItemInList, IncludeItemIntoList, NoOfItemsInList,
50 RemoveItemFromList, ForeachItemInListDo, KillList, DuplicateList ;
51
52 FROM SymbolTable IMPORT NulSym, ModeOfAddr, IsVar, IsRecord, GetSType,
53 ProcedureKind, GetNthParam, NoOfParam,
54 GetNth, IsRecordField, IsSet, IsArray, IsProcedure,
55 GetVarScope, IsVarAParam, IsComponent, GetMode,
56 VarCheckReadInit, VarInitState, PutVarInitialized,
57 PutVarFieldInitialized, GetVarFieldInitialized,
58 IsConst, IsConstString, NoOfParamAny, IsVarParamAny,
59 ForeachLocalSymDo, ForeachParamSymDo,
60 IsTemporary, ModeOfAddr,
61 IsReallyPointer, IsUnbounded,
62 IsVarient, IsFieldVarient, GetVarient,
63 IsVarArrayRef, GetSymName,
64 IsType, IsPointer, IsTuple,
65 GetParameterShadowVar, IsParameter, GetLType,
66 GetParameterHeapVar, GetVarDeclTok ;
67
68 FROM M2Quads IMPORT QuadOperator, GetQuadOtok, GetQuad, GetNextQuad,
69 IsNewLocalVar, IsReturn, IsKillLocalVar, IsConditional,
70 IsUnConditional, IsBackReference, IsCall, IsGoto,
71 GetM2OperatorDesc, Opposite, DisplayQuadRange,
72 GetQuadTrash ;
73
74 FROM M2Printf IMPORT printf0, printf1, printf2 ;
75 FROM M2GCCDeclare IMPORT PrintSym ;
76
77
78 CONST
79 Debugging = FALSE ;
80
81 TYPE
82 descType = (scalar, record) ;
83
84 InitDesc = POINTER TO RECORD
85 sym, type : CARDINAL ;
86 initialized: BOOLEAN ;
87 CASE kind: descType OF
88
89 scalar: |
90 record: rec: recordDesc |
91
92 END
93 END ;
94
95 recordDesc = RECORD
96 fieldDesc: Indexing.Index ;
97 END ;
98
99 symAlias = POINTER TO RECORD
100 keySym,
101 alias : CARDINAL ;
102 next : symAlias ;
103 END ;
104
105 bbEntry = POINTER TO RECORD
106 start, end: CARDINAL ;
107 (* Is this the first bb? *)
108 first,
109 (* Does it end with a call? *)
110 endCall,
111 (* Does it end with a goto? *)
112 endGoto,
113 (* Does it end with a conditional? *)
114 endCond,
115 (* Does it form part of a loop? *)
116 topOfLoop : BOOLEAN ;
117 trashQuad,
118 indexBB,
119 nextQuad,
120 condQuad,
121 nextBB,
122 condBB : CARDINAL ;
123 next : bbEntry ;
124 END ;
125
126 VAR
127 IndirectArray,
128 LArray : Indexing.Index ;
129 freeList : symAlias ;
130 bbArray : Indexing.Index ;
131 bbFreeList : bbEntry ;
132 ignoreList,
133 errorList : List ; (* Ensure that we only generate one set of warnings per token. *)
134
135
136 (*
137 PrintSymInit -
138 *)
139
140 PROCEDURE PrintSymInit (desc: InitDesc) ;
141 VAR
142 i, n: CARDINAL ;
143 BEGIN
144 printf ("sym %d: type %d ", desc^.sym, desc^.type) ;
145 IF desc^.kind = scalar
146 THEN
147 printf ("scalar")
148 ELSE
149 printf ("record")
150 END ;
151 IF NOT desc^.initialized
152 THEN
153 printf (" not")
154 END ;
155 printf (" initialized\n") ;
156 IF (desc^.type # NulSym) AND IsRecord (desc^.type)
157 THEN
158 i := 1 ;
159 n := Indexing.HighIndice (desc^.rec.fieldDesc) ;
160 WHILE i <= n DO
161 PrintSymInit (Indexing.GetIndice (desc^.rec.fieldDesc, i)) ;
162 INC (i)
163 END
164 END
165 END PrintSymInit ;
166
167
168 PROCEDURE InitSymInit () : InitDesc ;
169 VAR
170 id: InitDesc ;
171 BEGIN
172 NEW (id) ;
173 WITH id^ DO
174 sym := NulSym ;
175 type := NulSym ;
176 initialized := TRUE ;
177 kind := scalar
178 END ;
179 RETURN id
180 END InitSymInit ;
181
182
183 PROCEDURE KillSymInit (VAR desc: InitDesc) ;
184 BEGIN
185 WITH desc^ DO
186 CASE kind OF
187
188 record: KillFieldDesc (rec.fieldDesc)
189
190 ELSE
191 END
192 END ;
193 DISPOSE (desc) ;
194 desc := NIL
195 END KillSymInit ;
196
197
198 PROCEDURE ConfigSymInit (desc: InitDesc; sym: CARDINAL) ;
199 BEGIN
200 IF IsVar (sym) OR IsRecordField (sym)
201 THEN
202 desc^.sym := sym ;
203 desc^.type := GetSType (sym) ;
204 desc^.initialized := FALSE ;
205 (* An unknown symbol will have no type. *)
206 IF desc^.type = NulSym
207 THEN
208 desc^.kind := scalar ;
209 desc^.initialized := TRUE (* For now we don't attempt to handle array types. *)
210 ELSE
211 IF IsRecord (desc^.type)
212 THEN
213 desc^.kind := record ;
214 desc^.rec.fieldDesc := Indexing.InitIndex (1) ;
215 PopulateFields (desc, desc^.type)
216 ELSE
217 desc^.kind := scalar ;
218 IF IsArray (desc^.type)
219 THEN
220 desc^.initialized := TRUE (* For now we don't attempt to handle array types. *)
221 END
222 END
223 END
224 END
225 END ConfigSymInit ;
226
227
228 (*
229 KillFieldDesc -
230 *)
231
232 PROCEDURE KillFieldDesc (VAR fielddesc: Indexing.Index) ;
233 VAR
234 i, h: CARDINAL ;
235 id : InitDesc ;
236 BEGIN
237 i := 1 ;
238 h := Indexing.HighIndice (fielddesc) ;
239 WHILE i <= h DO
240 id := Indexing.GetIndice (fielddesc, i) ;
241 KillSymInit (id) ;
242 INC (i)
243 END ;
244 fielddesc := Indexing.KillIndex (fielddesc)
245 END KillFieldDesc ;
246
247
248 (*
249 PopulateFields -
250 *)
251
252 PROCEDURE PopulateFields (desc: InitDesc; recsym: CARDINAL) ;
253 VAR
254 field,
255 i : CARDINAL ;
256 fdesc: InitDesc ;
257 BEGIN
258 Assert (IsRecord (recsym)) ;
259 i := 1 ;
260 REPEAT
261 field := GetNth (recsym, i) ;
262 IF field # NulSym
263 THEN
264 fdesc := InitSymInit () ;
265 ConfigSymInit (fdesc, field) ;
266 Indexing.IncludeIndiceIntoIndex (desc^.rec.fieldDesc, fdesc) ;
267 INC (i)
268 END
269 UNTIL field = NulSym
270 END PopulateFields ;
271
272
273 PROCEDURE SetInitialized (desc: InitDesc) ;
274 BEGIN
275 desc^.initialized := TRUE
276 END SetInitialized ;
277
278
279 PROCEDURE GetInitialized (desc: InitDesc) : BOOLEAN ;
280 BEGIN
281 IF NOT desc^.initialized
282 THEN
283 IF IsRecord (desc^.type)
284 THEN
285 TrySetInitialized (desc)
286 END
287 END ;
288 IF Debugging
289 THEN
290 PrintSymInit (desc)
291 END ;
292 RETURN desc^.initialized
293 END GetInitialized ;
294
295
296 PROCEDURE GetFieldDesc (desc: InitDesc; field: CARDINAL) : InitDesc ;
297 VAR
298 fsym,
299 i : CARDINAL ;
300 BEGIN
301 IF IsRecord (desc^.type)
302 THEN
303 i := 1 ;
304 REPEAT
305 fsym := GetNth (desc^.type, i) ;
306 IF field = fsym
307 THEN
308 RETURN Indexing.GetIndice (desc^.rec.fieldDesc, i)
309 END ;
310 INC (i)
311 UNTIL fsym = NulSym
312 END ;
313 RETURN NIL
314 END GetFieldDesc ;
315
316
317 PROCEDURE SetFieldInitialized (desc: InitDesc; fieldlist: List) : BOOLEAN ;
318 BEGIN
319 RETURN SetFieldInitializedNo (desc, fieldlist, 1)
320 END SetFieldInitialized ;
321
322
323 (*
324 TrySetInitialized -
325 *)
326
327 PROCEDURE TrySetInitialized (desc: InitDesc) ;
328 VAR
329 i, h : CARDINAL ;
330 fdesc: InitDesc ;
331 BEGIN
332 h := Indexing.HighIndice (desc^.rec.fieldDesc) ;
333 i := 1 ;
334 WHILE i <= h DO
335 fdesc := Indexing.GetIndice (desc^.rec.fieldDesc, i) ;
336 IF NOT fdesc^.initialized
337 THEN
338 RETURN
339 END ;
340 INC (i)
341 END ;
342 desc^.initialized := TRUE
343 END TrySetInitialized ;
344
345
346 (*
347 SetFieldInitializedNo -
348 *)
349
350 PROCEDURE SetFieldInitializedNo (desc: InitDesc;
351 fieldlist: List; level: CARDINAL) : BOOLEAN ;
352 VAR
353 nsym : CARDINAL ;
354 fdesc: InitDesc ;
355 BEGIN
356 IF level > NoOfItemsInList (fieldlist)
357 THEN
358 RETURN FALSE
359 ELSE
360 nsym := GetItemFromList (fieldlist, level) ;
361 fdesc := GetFieldDesc (desc, nsym) ;
362 IF fdesc = NIL
363 THEN
364 RETURN FALSE
365 ELSIF level = NoOfItemsInList (fieldlist)
366 THEN
367 SetInitialized (fdesc) ;
368 TrySetInitialized (desc) ;
369 RETURN desc^.initialized
370 ELSE
371 IF SetFieldInitializedNo (fdesc, fieldlist, level + 1)
372 THEN
373 END ;
374 TrySetInitialized (desc) ;
375 RETURN desc^.initialized
376 END
377 END
378 END SetFieldInitializedNo ;
379
380
381 PROCEDURE GetFieldInitialized (desc: InitDesc; fieldlist: List) : BOOLEAN ;
382 BEGIN
383 RETURN GetFieldInitializedNo (desc, fieldlist, 1)
384 END GetFieldInitialized ;
385
386
387 PROCEDURE GetFieldInitializedNo (desc: InitDesc;
388 fieldlist: List; level: CARDINAL) : BOOLEAN ;
389 VAR
390 nsym : CARDINAL ;
391 fdesc: InitDesc ;
392 BEGIN
393 IF desc^.initialized
394 THEN
395 RETURN TRUE
396 ELSIF level > NoOfItemsInList (fieldlist)
397 THEN
398 RETURN FALSE
399 ELSE
400 nsym := GetItemFromList (fieldlist, level) ;
401 fdesc := GetFieldDesc (desc, nsym) ;
402 IF fdesc = NIL
403 THEN
404 (* The pointer variable maybe uninitialized and hence we cannot
405 find the record variable. *)
406 RETURN FALSE
407 ELSIF fdesc^.initialized
408 THEN
409 RETURN TRUE
410 ELSE
411 RETURN GetFieldInitializedNo (fdesc, fieldlist, level + 1)
412 END
413 END
414 END GetFieldInitializedNo ;
415
416
417 (*
418 IsGlobalVar -
419 *)
420
421 PROCEDURE IsGlobalVar (sym: CARDINAL) : BOOLEAN ;
422 BEGIN
423 RETURN IsVar (sym) AND (NOT IsProcedure (GetVarScope (sym)))
424 END IsGlobalVar ;
425
426
427 (*
428 IsLocalVar -
429
430 PROCEDURE IsLocalVar (procsym, varsym: CARDINAL) : BOOLEAN ;
431 BEGIN
432 RETURN IsVar (varsym) AND (GetVarScope (varsym) = procsym)
433 END IsLocalVar ;
434 *)
435
436
437 (*
438 RecordFieldContainsVarient -
439 *)
440
441 PROCEDURE RecordFieldContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
442 BEGIN
443 Assert (IsRecordField (sym)) ;
444 IF doContainsVariant (GetSType (sym), visited)
445 THEN
446 RETURN TRUE
447 END ;
448 RETURN GetVarient (sym) # NulSym
449 END RecordFieldContainsVarient ;
450
451
452 (*
453 RecordContainsVarient -
454 *)
455
456 PROCEDURE RecordContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
457 VAR
458 i,
459 fieldsym: CARDINAL ;
460 BEGIN
461 Assert (IsRecord (sym)) ;
462 i := 1 ;
463 REPEAT
464 fieldsym := GetNth (sym, i) ;
465 IF fieldsym # NulSym
466 THEN
467 IF IsRecordField (fieldsym)
468 THEN
469 IF RecordFieldContainsVarient (fieldsym, visited)
470 THEN
471 RETURN TRUE
472 END
473 ELSIF IsVarient (fieldsym)
474 THEN
475 RETURN TRUE
476 END ;
477 INC (i)
478 END
479 UNTIL fieldsym = NulSym ;
480 RETURN FALSE
481 END RecordContainsVarient ;
482
483
484 (*
485 VarContainsVarient -
486 *)
487
488 PROCEDURE VarContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
489 BEGIN
490 Assert (IsVar (sym)) ;
491 RETURN doContainsVariant (GetSType (sym), visited)
492 END VarContainsVarient ;
493
494
495 (*
496 TypeContainsVarient -
497 *)
498
499 PROCEDURE TypeContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
500 BEGIN
501 Assert (IsType (sym)) ;
502 RETURN doContainsVariant (GetSType (sym), visited)
503 END TypeContainsVarient ;
504
505
506 (*
507 ArrayContainsVarient -
508 *)
509
510 PROCEDURE ArrayContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
511 BEGIN
512 Assert (IsArray (sym)) ;
513 RETURN doContainsVariant (GetSType (sym), visited)
514 END ArrayContainsVarient ;
515
516
517 (*
518 PointerContainsVarient -
519 *)
520
521 PROCEDURE PointerContainsVarient (sym: CARDINAL; visited: List) : BOOLEAN ;
522 BEGIN
523 Assert (IsPointer (sym)) ;
524 RETURN doContainsVariant (GetSType (sym), visited)
525 END PointerContainsVarient ;
526
527
528 (*
529 doContainsVariant -
530 *)
531
532 PROCEDURE doContainsVariant (sym: CARDINAL; visited: List) : BOOLEAN ;
533 BEGIN
534 IF (sym # NulSym) AND (NOT IsItemInList (visited, sym))
535 THEN
536 IncludeItemIntoList (visited, sym) ;
537 IF IsVar (sym)
538 THEN
539 RETURN VarContainsVarient (sym, visited)
540 ELSIF IsRecord (sym)
541 THEN
542 RETURN RecordContainsVarient (sym, visited)
543 ELSIF IsPointer (sym)
544 THEN
545 RETURN PointerContainsVarient (sym, visited)
546 ELSIF IsArray (sym)
547 THEN
548 RETURN ArrayContainsVarient (sym, visited)
549 ELSIF IsType (sym)
550 THEN
551 RETURN TypeContainsVarient (sym, visited)
552 END
553 END ;
554 RETURN FALSE
555 END doContainsVariant ;
556
557
558 (*
559 ContainsVariant - returns TRUE if type sym contains a variant record.
560 *)
561
562 PROCEDURE ContainsVariant (sym: CARDINAL) : BOOLEAN ;
563 VAR
564 visited: List ;
565 result : BOOLEAN ;
566 BEGIN
567 InitList (visited) ;
568 result := doContainsVariant (sym, visited) ;
569 KillList (visited) ;
570 RETURN result
571 END ContainsVariant ;
572
573
574 (*
575 IssueConditional -
576 *)
577
578 PROCEDURE IssueConditional (quad: CARDINAL; conditional: BOOLEAN) ;
579 VAR
580 op : QuadOperator ;
581 op1, op2, op3 : CARDINAL ;
582 op1tok, op2tok, op3tok, qtok: CARDINAL ;
583 constExpr, overflowChecking : BOOLEAN ;
584 s : String ;
585 BEGIN
586 GetQuadOtok (quad, qtok, op, op1, op2, op3,
587 overflowChecking, constExpr,
588 op1tok, op2tok, op3tok) ;
589 IF IsUniqueWarning (qtok)
590 THEN
591 op1tok := DefaultTokPos (op1tok, qtok) ;
592 op2tok := DefaultTokPos (op2tok, qtok) ;
593 op3tok := DefaultTokPos (op3tok, qtok) ;
594 IF NOT conditional
595 THEN
596 op := Opposite (op)
597 END ;
598 s := InitString ('depending upon the result of {%1Oad} ') ;
599 s := ConCat (s, Mark (GetM2OperatorDesc (op))) ;
600 s := ConCat (s, InitString (' {%2ad}')) ;
601 MetaErrorStringT2 (qtok, s, op1, op2)
602 END
603 END IssueConditional ;
604
605
606 (*
607 GenerateNoteFlow -
608 *)
609
610 PROCEDURE GenerateNoteFlow (n: CARDINAL; warning: BOOLEAN) ;
611 VAR
612 i : CARDINAL ;
613 ip1Ptr,
614 iPtr : bbEntry ;
615 BEGIN
616 IF NOT warning
617 THEN
618 (* Only issue flow messages for non warnings. *)
619 i := 1 ;
620 WHILE i <= n DO
621 iPtr := Indexing.GetIndice (bbArray, i) ;
622 IF iPtr^.endCond
623 THEN
624 IF i < n
625 THEN
626 ip1Ptr := Indexing.GetIndice (bbArray, i+1) ;
627 IssueConditional (iPtr^.end, iPtr^.condBB = ip1Ptr^.indexBB)
628 END
629 END ;
630 INC (i)
631 END
632 END
633 END GenerateNoteFlow ;
634
635
636 (*
637 IssueWarning - issue a warning or note at tok location.
638 *)
639
640 PROCEDURE IssueWarning (tok: CARDINAL;
641 before, after: ARRAY OF CHAR;
642 sym: CARDINAL; warning: BOOLEAN) ;
643 VAR
644 s: String ;
645 BEGIN
646 s := InitString (before) ;
647 IF warning
648 THEN
649 s := ConCat (s, Mark (InitString ('{%1Wad}')))
650 ELSE
651 s := ConCat (s, Mark (InitString ('{%1Oad}')))
652 END ;
653 s := ConCat (s, Mark (InitString (after))) ;
654 MetaErrorStringT1 (tok, s, sym)
655 END IssueWarning ;
656
657
658 (*
659 IsUniqueWarning - return TRUE if a warning has not been issued at tok.
660 It remembers tok and subsequent calls will always return FALSE.
661 *)
662
663 PROCEDURE IsUniqueWarning (tok: CARDINAL) : BOOLEAN ;
664 BEGIN
665 IF NOT IsItemInList (errorList, tok)
666 THEN
667 IncludeItemIntoList (errorList, tok) ;
668 RETURN TRUE
669 ELSE
670 RETURN FALSE
671 END
672 END IsUniqueWarning ;
673
674
675 (*
676 CheckDeferredRecordAccess -
677 *)
678
679 PROCEDURE CheckDeferredRecordAccess (tok: CARDINAL;
680 sym: CARDINAL;
681 canDereference, warning: BOOLEAN;
682 i: CARDINAL) ;
683 VAR
684 unique: BOOLEAN ;
685 BEGIN
686 IF IsVar (sym)
687 THEN
688 IF Debugging
689 THEN
690 Trace ("CheckDeferredRecordAccess %d\n", sym) ;
691 PrintSym (sym) ;
692 IF canDereference
693 THEN
694 printf1 ("checkReadInit (%d, true)\n", sym)
695 ELSE
696 printf1 ("checkReadInit (%d, false)\n", sym)
697 END
698 END ;
699 IF IsExempt (sym)
700 THEN
701 Trace ("checkReadInit sym is a parameter or not a local variable (%d)", sym) ;
702 (* We assume parameters have been initialized. *)
703 PutVarInitialized (sym, LeftValue) ;
704 PutVarInitialized (sym, RightValue)
705 (* SetVarInitialized (sym, TRUE) *)
706 ELSIF IsUnbounded (GetSType (sym))
707 THEN
708 SetVarInitialized (sym, TRUE, tok)
709 ELSIF IsComponent (sym)
710 THEN
711 Trace ("checkReadInit IsComponent (%d) is true)", sym) ;
712 IF (NOT GetVarComponentInitialized (sym, tok)) AND IsUniqueWarning (tok)
713 THEN
714 GenerateNoteFlow (i, warning) ;
715 IssueWarning (tok,
716 'attempting to access ',
717 ' before it has been initialized',
718 sym, warning)
719 END
720 ELSIF (GetMode (sym) = LeftValue) AND canDereference
721 THEN
722 Trace ("checkReadInit GetMode (%d) = LeftValue and canDereference (LeftValue and RightValue VarCheckReadInit)", sym) ;
723 unique := TRUE ;
724 IF NOT VarCheckReadInit (sym, LeftValue)
725 THEN
726 unique := IsUniqueWarning (tok) ;
727 IF unique
728 THEN
729 GenerateNoteFlow (i, warning) ;
730 IssueWarning (tok,
731 'attempting to access the address of ',
732 ' before it has been initialized',
733 sym, warning)
734 END
735 END ;
736 IF NOT VarCheckReadInit (sym, RightValue)
737 THEN
738 IF unique
739 THEN
740 GenerateNoteFlow (i, warning) ;
741 IssueWarning (tok,
742 'attempting to access ', ' before it has been initialized',
743 sym, warning)
744 END
745 END
746 ELSE
747 Trace ("checkReadInit call VarCheckReadInit using GetMode (%d)", sym) ;
748 IF (NOT VarCheckReadInit (sym, GetMode (sym))) AND IsUniqueWarning (tok)
749 THEN
750 GenerateNoteFlow (i, warning) ;
751 IssueWarning (tok,
752 'attempting to access ',
753 ' before it has been initialized',
754 sym, warning)
755 END
756 END
757 END
758 END CheckDeferredRecordAccess ;
759
760
761 (*
762 SetVarUninitialized - resets variable init state.
763 *)
764
765 PROCEDURE SetVarUninitialized (sym: CARDINAL) ;
766 BEGIN
767 IF IsVar (sym)
768 THEN
769 IF NOT IsUnbounded (GetSType (sym))
770 THEN
771 VarInitState (sym)
772 END
773 END
774 END SetVarUninitialized ;
775
776
777 (*
778 ComponentFindVar -
779 *)
780
781 PROCEDURE ComponentFindVar (sym: CARDINAL;
782 VAR lvalue: BOOLEAN;
783 tok: CARDINAL) : CARDINAL ;
784 VAR
785 nsym,
786 i : CARDINAL ;
787 BEGIN
788 i := 1 ;
789 REPEAT
790 nsym := GetNth (sym, i) ;
791 lvalue := GetMode (nsym) = LeftValue ;
792 nsym := getLAlias (nsym) ;
793 IF nsym = Nil
794 THEN
795 MetaErrorT1 (tok,
796 "attempting to dereference {%1Wad} which will be a {%kNIL} pointer",
797 sym) ;
798 RETURN NulSym
799 ELSIF (nsym # NulSym) AND IsVar (nsym)
800 THEN
801 IF (nsym # sym) AND IsComponent (nsym)
802 THEN
803 RETURN ComponentFindVar (nsym, lvalue, tok)
804 ELSE
805 RETURN nsym
806 END
807 END ;
808 INC (i)
809 UNTIL nsym = NulSym ;
810 RETURN NulSym
811 END ComponentFindVar ;
812
813
814 (*
815 ComponentCreateFieldList - builds a list of fields accessed by the component var.
816 Each item in the list will be a field of incremental levels
817 though a nested record. It is not a list of fields
818 at the same level.
819
820 foo = RECORD
821 v: RECORD
822 x, y: CARDINAL ;
823 END ;
824 w: CARDINAL ;
825 END ;
826
827 { v, x } for example and not { v, w }
828 *)
829
830 PROCEDURE ComponentCreateFieldList (sym: CARDINAL) : List ;
831 VAR
832 lst: List ;
833 BEGIN
834 InitList (lst) ;
835 IF IsVar (sym) AND IsComponent (sym)
836 THEN
837 ComponentBuildFieldList (lst, sym)
838 END ;
839 RETURN lst
840 END ComponentCreateFieldList ;
841
842
843 PROCEDURE ComponentBuildFieldList (lst: List; sym: CARDINAL) ;
844 VAR
845 i, nsym: CARDINAL ;
846 BEGIN
847 i := 1 ;
848 REPEAT
849 nsym := GetNth (sym, i) ;
850 IF nsym # NulSym
851 THEN
852 IF IsComponent (nsym)
853 THEN
854 ComponentBuildFieldList (lst, nsym)
855 ELSIF IsRecordField (nsym)
856 THEN
857 IncludeItemIntoList (lst, nsym)
858 END ;
859 INC (i)
860 END
861 UNTIL nsym = NulSym
862 END ComponentBuildFieldList ;
863
864
865 (*
866 deRefComponent -
867 *)
868
869 PROCEDURE deRefComponent (component: CARDINAL; lvalue: BOOLEAN;
870 sym: CARDINAL; tok: CARDINAL) : CARDINAL ;
871 BEGIN
872 IF lvalue
873 THEN
874 RETURN getContent (component, sym, tok)
875 ELSE
876 RETURN component
877 END
878 END deRefComponent ;
879
880
881 (*
882 SetVarComponentInitialized -
883 *)
884
885 PROCEDURE SetVarComponentInitialized (sym: CARDINAL; tok: CARDINAL) ;
886 VAR
887 lvalue: BOOLEAN ;
888 i, n,
889 fsym,
890 vsym : CARDINAL ;
891 lst : List ;
892 BEGIN
893 vsym := ComponentFindVar (sym, lvalue, tok) ;
894 vsym := deRefComponent (vsym, lvalue, sym, tok) ;
895 IF vsym # NulSym
896 THEN
897 IF Debugging
898 THEN
899 printf0 ("*************** vsym is: ") ;
900 PrintSym (vsym)
901 END ;
902 (* Build list accessing the field. *)
903 lst := ComponentCreateFieldList (sym) ;
904 IF Debugging
905 THEN
906 printf2 ("sym = %d, vsym = %d, fields:", sym, vsym)
907 END ;
908 (* Now mark this field in the record variable as initialized. *)
909 IF PutVarFieldInitialized (vsym, RightValue, lst)
910 THEN
911 IF Debugging
912 THEN
913 i := 1 ;
914 n := NoOfItemsInList (lst) ;
915 WHILE i <= n DO
916 fsym := GetItemFromList (lst, i) ;
917 printf1 (" %d", fsym) ;
918 INC (i)
919 END ;
920 printf0 (" is initialized\n")
921 END
922 ELSIF Debugging
923 THEN
924 printf0 (" vsym is not a var\n")
925 END ;
926 KillList (lst)
927 END
928 END SetVarComponentInitialized ;
929
930
931 (*
932 GetVarComponentInitialized -
933 *)
934
935 PROCEDURE GetVarComponentInitialized (sym: CARDINAL; tok: CARDINAL) : BOOLEAN ;
936 VAR
937 lvalue,
938 init : BOOLEAN ;
939 component,
940 vsym : CARDINAL ;
941 lst : List ;
942 BEGIN
943 component := ComponentFindVar (sym, lvalue, tok) ;
944 IF IsItemInList (ignoreList, component) OR IsExempt (component)
945 THEN
946 RETURN TRUE
947 ELSE
948 init := FALSE ;
949 vsym := deRefComponent (component, lvalue, sym, tok) ;
950 IF vsym # NulSym
951 THEN
952 IF IsExempt (vsym)
953 THEN
954 init := TRUE
955 ELSE
956 (* Create list representing how the field is accessed. *)
957 lst := ComponentCreateFieldList (sym) ;
958 (* Now obtain the mark indicating whether this field was initialized. *)
959 init := GetVarFieldInitialized (vsym, RightValue, lst) ;
960 KillList (lst)
961 END
962 END ;
963 RETURN init
964 END
965 END GetVarComponentInitialized ;
966
967
968 (*
969 Trace -
970 *)
971
972 PROCEDURE Trace (message: ARRAY OF CHAR; sym: CARDINAL) ;
973 BEGIN
974 IF Debugging
975 THEN
976 printf1 (message, sym) ;
977 printf0 ("\n")
978 END
979 END Trace ;
980
981
982 (*
983 SetVarInitialized - if the variable has a left mode and can be dereferenced
984 then set the left and right initialization state.
985 *)
986
987 PROCEDURE SetVarInitialized (sym: CARDINAL; canDereference: BOOLEAN;
988 tok: CARDINAL) ;
989 BEGIN
990 IF IsVar (sym)
991 THEN
992 RemoveItemFromList (ignoreList, sym) ;
993 IF IsComponent (sym)
994 THEN
995 Trace ("SetVarInitialized sym %d is a component and calling SetVarComponentInitialized", sym);
996 SetVarComponentInitialized (sym, tok)
997 ELSIF (GetMode (sym) = LeftValue) AND canDereference
998 THEN
999 Trace ("SetVarInitialized sym %d is LeftValue and canDeference and calling PutVarInitialized LeftValue and RightValue", sym);
1000 PutVarInitialized (sym, LeftValue) ;
1001 PutVarInitialized (sym, RightValue)
1002 ELSE
1003 Trace ("SetVarInitialized sym %d calling PutVarInitialized with its mode", sym);
1004 PutVarInitialized (sym, GetMode (sym))
1005 END ;
1006 IF Debugging
1007 THEN
1008 PrintSym (sym)
1009 END
1010 END
1011 END SetVarInitialized ;
1012
1013
1014 (*
1015 doGetVarInitialized -
1016 *)
1017
1018 PROCEDURE doGetVarInitialized (sym: CARDINAL; tok: CARDINAL) : BOOLEAN ;
1019 BEGIN
1020 IF IsVar (sym)
1021 THEN
1022 IF IsUnbounded (GetSType (sym))
1023 THEN
1024 RETURN TRUE
1025 ELSIF IsComponent (sym)
1026 THEN
1027 RETURN GetVarComponentInitialized (sym, tok)
1028 END ;
1029 RETURN VarCheckReadInit (sym, GetMode (sym))
1030 END ;
1031 RETURN IsConst (sym) AND IsConstString (sym)
1032 END doGetVarInitialized ;
1033
1034
1035 (*
1036 GetVarInitialized -
1037 *)
1038
1039 PROCEDURE GetVarInitialized (sym: CARDINAL; tok: CARDINAL) : BOOLEAN ;
1040 VAR
1041 init: BOOLEAN ;
1042 BEGIN
1043 init := doGetVarInitialized (sym, tok) ;
1044 IF Debugging
1045 THEN
1046 IF init
1047 THEN
1048 Trace ("GetVarInitialized (sym = %d) returning TRUE", sym)
1049 ELSE
1050 Trace ("GetVarInitialized (sym = %d) returning FALSE", sym)
1051 END
1052 END ;
1053 RETURN init
1054 END GetVarInitialized ;
1055
1056
1057 (*
1058 IsExempt - returns TRUE if sym is a global variable or a parameter or
1059 a variable with a variant record type.
1060 *)
1061
1062 PROCEDURE IsExempt (sym: CARDINAL) : BOOLEAN ;
1063 BEGIN
1064 RETURN (sym # NulSym) AND IsVar (sym) AND
1065 (IsGlobalVar (sym) OR
1066 (* (IsVarAParam (sym) AND (GetMode (sym) = LeftValue)) OR *)
1067 ContainsVariant (sym) OR
1068 IsArray (GetSType (sym)) OR IsSet (GetSType (sym)) OR
1069 IsUnbounded (GetSType (sym)) OR IsVarArrayRef (sym) OR
1070 IsItemInList (ignoreList, sym))
1071 END IsExempt ;
1072
1073
1074 (*
1075 CheckBinary -
1076 *)
1077
1078 PROCEDURE CheckBinary (op1tok, op1,
1079 op2tok, op2,
1080 op3tok, op3: CARDINAL; warning: BOOLEAN;
1081 i: CARDINAL) ;
1082 BEGIN
1083 CheckDeferredRecordAccess (op2tok, op2, FALSE, warning, i) ;
1084 CheckDeferredRecordAccess (op3tok, op3, FALSE, warning, i) ;
1085 SetVarInitialized (op1, FALSE, op1tok)
1086 END CheckBinary ;
1087
1088
1089 (*
1090 CheckUnary -
1091 *)
1092
1093 PROCEDURE CheckUnary (lhstok, lhs,
1094 rhstok, rhs: CARDINAL; warning: BOOLEAN;
1095 i: CARDINAL) ;
1096 BEGIN
1097 CheckDeferredRecordAccess (rhstok, rhs, FALSE, warning, i) ;
1098 SetVarInitialized (lhs, FALSE, lhstok)
1099 END CheckUnary ;
1100
1101
1102 (*
1103 CheckXIndr -
1104 *)
1105
1106 PROCEDURE CheckXIndr (lhstok, lhs, type,
1107 rhstok, rhs: CARDINAL; warning: BOOLEAN;
1108 i: CARDINAL) ;
1109 VAR
1110 lst : List ;
1111 content: CARDINAL ;
1112 BEGIN
1113 CheckDeferredRecordAccess (rhstok, rhs, FALSE, warning, i) ;
1114 CheckDeferredRecordAccess (lhstok, lhs, FALSE, warning, i) ;
1115 (* Now see if we know what lhs is pointing to and set fields if necessary. *)
1116 content := getContent (getLAlias (lhs), lhs, lhstok) ;
1117 IF (content # NulSym) AND (content # lhs) AND (GetSType (content) = type)
1118 THEN
1119 IF IsReallyPointer (rhs)
1120 THEN
1121 SetupLAlias (content, rhs)
1122 END ;
1123 IF IsRecord (type)
1124 THEN
1125 (* Set all fields of content as initialized. *)
1126 SetVarInitialized (content, FALSE, lhstok)
1127 ELSE
1128 (* Set only the field assigned in vsym as initialized. *)
1129 lst := ComponentCreateFieldList (rhs) ;
1130 IF PutVarFieldInitialized (content, RightValue, lst)
1131 THEN
1132 END ;
1133 KillList (lst)
1134 END
1135 END
1136 END CheckXIndr ;
1137
1138
1139 (*
1140 CheckIndrX -
1141 *)
1142
1143 PROCEDURE CheckIndrX (lhstok, lhs, rhstok, rhs: CARDINAL;
1144 warning: BOOLEAN;
1145 i: CARDINAL) ;
1146 VAR
1147 content: CARDINAL ;
1148 BEGIN
1149 CheckDeferredRecordAccess (rhstok, rhs, FALSE, warning, i) ;
1150 content := getContent (getLAlias (rhs), rhs, rhstok) ;
1151 IF content = NulSym
1152 THEN
1153 IncludeItemIntoList (ignoreList, lhs)
1154 ELSE
1155 CheckDeferredRecordAccess (rhstok, content, TRUE, warning, i) ;
1156 SetVarInitialized (lhs, VarCheckReadInit (content, RightValue), lhstok) ;
1157 IF IsReallyPointer (content)
1158 THEN
1159 SetupLAlias (lhs, content)
1160 END
1161 END
1162 END CheckIndrX ;
1163
1164
1165 (*
1166 CheckRecordField -
1167 *)
1168
1169 PROCEDURE CheckRecordField (op1: CARDINAL) ;
1170 BEGIN
1171 PutVarInitialized (op1, LeftValue)
1172 END CheckRecordField ;
1173
1174
1175 (*
1176 CheckLastForIterator -
1177 *)
1178
1179 PROCEDURE CheckLastForIterator (op1tok: CARDINAL; op1: CARDINAL;
1180 op2tok: CARDINAL; op2: CARDINAL;
1181 warning: BOOLEAN; i: CARDINAL) ;
1182 BEGIN
1183 SetVarInitialized (op1, FALSE, op1tok) ;
1184 Assert (IsTuple (op2)) ;
1185 CheckDeferredRecordAccess (op2tok, GetNth (op2, 1), FALSE, warning, i) ;
1186 CheckDeferredRecordAccess (op2tok, GetNth (op2, 2), FALSE, warning, i) ;
1187 END CheckLastForIterator ;
1188
1189
1190 (*
1191 CheckBecomes -
1192 *)
1193
1194 PROCEDURE CheckBecomes (destok, des, exprtok, expr: CARDINAL;
1195 warning: BOOLEAN; i: CARDINAL) ;
1196 VAR
1197 lvalue: BOOLEAN ;
1198 lst : List ;
1199 vsym : CARDINAL ;
1200 BEGIN
1201 CheckDeferredRecordAccess (exprtok, expr, FALSE, warning, i) ;
1202 SetupLAlias (des, expr) ;
1203 SetVarInitialized (des, FALSE, destok) ;
1204 (* Now see if we know what lhs is pointing to and set fields if necessary. *)
1205 IF IsComponent (des)
1206 THEN
1207 vsym := ComponentFindVar (des, lvalue, destok) ;
1208 vsym := deRefComponent (vsym, lvalue, des, destok) ;
1209 IF vsym # NulSym
1210 THEN
1211 (* Set only the field assigned in vsym as initialized. *)
1212 lst := ComponentCreateFieldList (des) ;
1213 IF PutVarFieldInitialized (vsym, RightValue, lst)
1214 THEN
1215 END ;
1216 KillList (lst)
1217 END
1218 END
1219 END CheckBecomes ;
1220
1221
1222 (*
1223 CheckComparison -
1224 *)
1225
1226 PROCEDURE CheckComparison (op1tok, op1, op2tok, op2: CARDINAL;
1227 warning: BOOLEAN; i: CARDINAL) ;
1228 BEGIN
1229 CheckDeferredRecordAccess (op1tok, op1, FALSE, warning, i) ;
1230 CheckDeferredRecordAccess (op2tok, op2, FALSE, warning, i)
1231 END CheckComparison ;
1232
1233
1234 (*
1235 CheckAddr -
1236 *)
1237
1238 PROCEDURE CheckAddr (ptrtok, ptr, contenttok, content: CARDINAL) ;
1239 BEGIN
1240 SetVarInitialized (ptr, GetVarInitialized (content, contenttok), ptrtok) ;
1241 SetupIndr (ptr, content)
1242 END CheckAddr ;
1243
1244
1245 (*
1246 DefaultTokPos -
1247 *)
1248
1249 PROCEDURE DefaultTokPos (preferredPos, defaultPos: CARDINAL) : CARDINAL ;
1250 BEGIN
1251 IF preferredPos = UnknownTokenNo
1252 THEN
1253 RETURN defaultPos
1254 END ;
1255 RETURN preferredPos
1256 END DefaultTokPos ;
1257
1258
1259 (*
1260 stop -
1261 *)
1262
1263 PROCEDURE stop ;
1264 END stop ;
1265
1266
1267 (*
1268 CheckReadBeforeInitQuad -
1269 *)
1270
1271 PROCEDURE CheckReadBeforeInitQuad (procSym: CARDINAL; quad: CARDINAL;
1272 warning: BOOLEAN; i: CARDINAL) : BOOLEAN ;
1273 VAR
1274 op : QuadOperator ;
1275 op1, op2, op3 : CARDINAL ;
1276 op1tok, op2tok, op3tok, qtok: CARDINAL ;
1277 constExpr, overflowChecking : BOOLEAN ;
1278 BEGIN
1279 IF quad = 3140
1280 THEN
1281 stop
1282 END ;
1283 IF Debugging
1284 THEN
1285 printf1 ("CheckReadBeforeInitQuad (quad %d)\n", quad) ;
1286 DumpAliases ;
1287 ForeachLocalSymDo (procSym, PrintSym) ;
1288 printf0 ("***********************************\n")
1289 END ;
1290 GetQuadOtok (quad, qtok, op, op1, op2, op3,
1291 overflowChecking, constExpr,
1292 op1tok, op2tok, op3tok) ;
1293 op1tok := DefaultTokPos (op1tok, qtok) ;
1294 op2tok := DefaultTokPos (op2tok, qtok) ;
1295 op3tok := DefaultTokPos (op3tok, qtok) ;
1296 CASE op OF
1297
1298 (* Jumps, calls and branches. *)
1299 IfInOp,
1300 IfNotInOp,
1301 IfEquOp,
1302 IfNotEquOp,
1303 IfLessOp,
1304 IfLessEquOp,
1305 IfGreOp,
1306 IfGreEquOp : CheckComparison (op1tok, op1, op2tok, op2, warning, i) |
1307 LastForIteratorOp : CheckLastForIterator (op1tok, op1, op2tok, op2,
1308 warning, i) ;
1309 Assert (IsConst (op3)) |
1310 TryOp,
1311 ReturnOp,
1312 CallOp,
1313 KillLocalVarOp,
1314 RetryOp,
1315 GotoOp : RETURN TRUE | (* End of basic block. *)
1316
1317 (* Variable references. *)
1318
1319 InclOp,
1320 ExclOp : CheckDeferredRecordAccess (op1tok, op1, FALSE, warning, i) ;
1321 CheckDeferredRecordAccess (op1tok, op1, TRUE, warning, i) ;
1322 CheckDeferredRecordAccess (op3tok, op3, FALSE, warning, i) |
1323 NegateOp : CheckUnary (op1tok, op1, op3tok, op3, warning, i) |
1324 BecomesOp : CheckBecomes (op1tok, op1, op3tok, op3, warning, i) |
1325 UnboundedOp,
1326 FunctValueOp,
1327 StandardFunctionOp,
1328 HighOp,
1329 SizeOp : SetVarInitialized (op1, FALSE, op1tok) |
1330 AddrOp : CheckAddr (op1tok, op1, op3tok, op3) |
1331 ReturnValueOp : SetVarInitialized (op1, FALSE, op1tok) |
1332 NewLocalVarOp : SetParameterVariablesInitialized (op3) |
1333 ParamOp : CheckDeferredRecordAccess (op2tok, op2, FALSE, warning, i) ;
1334 CheckDeferredRecordAccess (op3tok, op3, FALSE, warning, i) ;
1335 IF (op1 > 0) AND (op1 <= NoOfParamAny (op2)) AND
1336 IsVarParamAny (op2, op1)
1337 THEN
1338 SetVarInitialized (op3, TRUE, op3tok)
1339 END |
1340 ArrayOp : CheckDeferredRecordAccess (op3tok, op3, FALSE, warning, i) ;
1341 SetVarInitialized (op1, TRUE, op1tok) |
1342 RecordFieldOp : CheckRecordField (op1) |
1343 LogicalShiftOp,
1344 LogicalRotateOp,
1345 LogicalOrOp,
1346 LogicalAndOp,
1347 LogicalXorOp,
1348 LogicalDiffOp,
1349 CoerceOp,
1350 ConvertOp,
1351 CastOp,
1352 AddOp,
1353 ArithAddOp,
1354 SubOp,
1355 MultOp,
1356 DivM2Op,
1357 ModM2Op,
1358 ModFloorOp,
1359 DivCeilOp,
1360 ModCeilOp,
1361 DivFloorOp,
1362 ModTruncOp,
1363 DivTruncOp : CheckBinary (op1tok, op1, op2tok, op2, op3tok, op3, warning, i) |
1364 XIndrOp : CheckXIndr (op1tok, op1, op2, op3tok, op3, warning, i) |
1365 IndrXOp : CheckIndrX (op1tok, op1, op3tok, op3, warning, i) |
1366 SaveExceptionOp : SetVarInitialized (op1, FALSE, op1tok) |
1367 RestoreExceptionOp: CheckDeferredRecordAccess (op1tok, op1, FALSE, warning, i) |
1368
1369 SubrangeLowOp,
1370 SubrangeHighOp : InternalError ('quadruples should have been resolved') |
1371 ElementSizeOp,
1372 BuiltinConstOp, (* Nothing to do, it is assigning a constant to op1 (also a const). *)
1373 BuiltinTypeInfoOp, (* Likewise assigning op1 (const) with a type. *)
1374 StringConvertCnulOp,
1375 StringConvertM2nulOp,
1376 StringLengthOp,
1377 ProcedureScopeOp,
1378 InitEndOp,
1379 InitStartOp,
1380 FinallyStartOp,
1381 FinallyEndOp,
1382 CatchBeginOp,
1383 CatchEndOp,
1384 ThrowOp,
1385 StartDefFileOp,
1386 StartModFileOp,
1387 EndFileOp,
1388 CodeOnOp,
1389 CodeOffOp,
1390 ProfileOnOp,
1391 ProfileOffOp,
1392 OptimizeOnOp,
1393 OptimizeOffOp,
1394 InlineOp,
1395 LineNumberOp,
1396 StatementNoteOp,
1397 SavePriorityOp,
1398 RestorePriorityOp,
1399 RangeCheckOp,
1400 ModuleScopeOp,
1401 ErrorOp,
1402 DummyOp,
1403 OptParamOp,
1404 InitAddressOp : |
1405
1406 END ;
1407 RETURN FALSE
1408 END CheckReadBeforeInitQuad ;
1409
1410
1411 (*
1412 SetParameterVariablesInitialized - sets all shadow variables for parameters as
1413 initialized.
1414 *)
1415
1416 PROCEDURE SetParameterVariablesInitialized (procSym: CARDINAL) ;
1417 BEGIN
1418 ForeachLocalSymDo (procSym, SetVarUninitialized) ;
1419 ForeachParamSymDo (procSym, SetVarLRInitialized) ;
1420 END SetParameterVariablesInitialized ;
1421
1422
1423 (*
1424 FilterCheckReadBeforeInitQuad -
1425 *)
1426
1427 PROCEDURE FilterCheckReadBeforeInitQuad (procSym: CARDINAL; start: CARDINAL;
1428 warning: BOOLEAN;
1429 i: CARDINAL) : BOOLEAN ;
1430 VAR
1431 Op : QuadOperator ;
1432 Op1, Op2, Op3: CARDINAL ;
1433 BEGIN
1434 GetQuad (start, Op, Op1, Op2, Op3) ;
1435 IF (Op # RangeCheckOp) AND (Op # StatementNoteOp)
1436 THEN
1437 RETURN CheckReadBeforeInitQuad (procSym, start, warning, i)
1438 END ;
1439 RETURN FALSE
1440 END FilterCheckReadBeforeInitQuad ;
1441
1442
1443 (*
1444 CheckReadBeforeInitFirstBasicBlock -
1445 *)
1446
1447 PROCEDURE CheckReadBeforeInitFirstBasicBlock (procSym: CARDINAL;
1448 start, end: CARDINAL;
1449 warning: BOOLEAN;
1450 i: CARDINAL) ;
1451 BEGIN
1452 LOOP
1453 IF FilterCheckReadBeforeInitQuad (procSym, start, warning, i)
1454 THEN
1455 END ;
1456 IF start = end
1457 THEN
1458 RETURN
1459 ELSE
1460 start := GetNextQuad (start)
1461 END
1462 END
1463 END CheckReadBeforeInitFirstBasicBlock ;
1464
1465
1466 (*
1467 bbArrayKill -
1468 *)
1469
1470 PROCEDURE bbArrayKill ;
1471 VAR
1472 i, h : CARDINAL ;
1473 bbPtr: bbEntry ;
1474 BEGIN
1475 h := Indexing.HighIndice (bbArray) ;
1476 i := 1 ;
1477 WHILE i <= h DO
1478 bbPtr := Indexing.GetIndice (bbArray, i) ;
1479 bbPtr^.next := bbFreeList ;
1480 bbFreeList := bbPtr ;
1481 INC (i)
1482 END ;
1483 bbArray := Indexing.KillIndex (bbArray)
1484 END bbArrayKill ;
1485
1486
1487 (*
1488 DumpBBEntry -
1489 *)
1490
1491 PROCEDURE DumpBBEntry (bbPtr: bbEntry; procSym: CARDINAL) ;
1492 BEGIN
1493 printf4 ("bb %d: scope %d: quads: %d .. %d",
1494 bbPtr^.indexBB, procSym, bbPtr^.start, bbPtr^.end) ;
1495 IF bbPtr^.first
1496 THEN
1497 printf0 (" first")
1498 END ;
1499 IF bbPtr^.endCall
1500 THEN
1501 printf0 (" endcall")
1502 END ;
1503 IF bbPtr^.endGoto
1504 THEN
1505 printf0 (" endgoto")
1506 END ;
1507 IF bbPtr^.endCond
1508 THEN
1509 printf0 (" endcond")
1510 END ;
1511 IF bbPtr^.topOfLoop
1512 THEN
1513 printf0 (" topofloop")
1514 END ;
1515 IF bbPtr^.condBB # 0
1516 THEN
1517 printf1 (" cond %d", bbPtr^.condBB)
1518 END ;
1519 IF bbPtr^.nextBB # 0
1520 THEN
1521 printf1 (" next %d", bbPtr^.nextBB)
1522 END ;
1523 printf0 ("\n")
1524 END DumpBBEntry ;
1525
1526
1527 (*
1528 DumpBBArray -
1529 *)
1530
1531 PROCEDURE DumpBBArray (procSym: CARDINAL) ;
1532 VAR
1533 bbPtr: bbEntry ;
1534 i, n : CARDINAL ;
1535 BEGIN
1536 i := 1 ;
1537 n := Indexing.HighIndice (bbArray) ;
1538 WHILE i <= n DO
1539 bbPtr := Indexing.GetIndice (bbArray, i) ;
1540 DumpBBEntry (bbPtr, procSym) ;
1541 INC (i)
1542 END ;
1543 i := 1 ;
1544 WHILE i <= n DO
1545 bbPtr := Indexing.GetIndice (bbArray, i) ;
1546 printf4 ("bb %d: scope %d: quads: %d .. %d\n",
1547 bbPtr^.indexBB, procSym, bbPtr^.start, bbPtr^.end) ;
1548 DisplayQuadRange (procSym, bbPtr^.start, bbPtr^.end) ;
1549 INC (i)
1550 END
1551 END DumpBBArray ;
1552
1553
1554 (*
1555 DumpBBSequence -
1556 *)
1557
1558 PROCEDURE DumpBBSequence (lst: List) ;
1559 VAR
1560 arrayindex,
1561 listindex, n: CARDINAL ;
1562 BEGIN
1563 n := NoOfItemsInList (lst) ;
1564 listindex := 1 ;
1565 printf0 ("=============\n");
1566 printf0 (" checking sequence:");
1567 WHILE listindex <= n DO
1568 arrayindex := GetItemFromList (lst, listindex) ;
1569 printf2 (" lst[%d] -> %d", listindex, arrayindex) ;
1570 INC (listindex)
1571 END ;
1572 printf0 ("\n")
1573 END DumpBBSequence ;
1574
1575
1576 (*
1577 trashParam -
1578 *)
1579
1580 PROCEDURE trashParam (trashQuad: CARDINAL) ;
1581 VAR
1582 op : QuadOperator ;
1583 op1, proc, param, paramValue : CARDINAL ;
1584 op1tok, op2tok, paramtok, qtok: CARDINAL ;
1585 constExpr, overflowChecking : BOOLEAN ;
1586 heapValue, ptrToHeap : CARDINAL ;
1587 BEGIN
1588 IF trashQuad # 0
1589 THEN
1590 GetQuadOtok (trashQuad, qtok, op, op1, proc, param,
1591 overflowChecking, constExpr,
1592 op1tok, op2tok, paramtok) ;
1593 heapValue := GetQuadTrash (trashQuad) ;
1594 IF Debugging
1595 THEN
1596 printf1 ("heapValue = %d\n", heapValue)
1597 END ;
1598 IF heapValue # NulSym
1599 THEN
1600 SetVarInitialized (param, FALSE, paramtok) ;
1601 paramValue := getLAlias (param) ;
1602 ptrToHeap := getContent (paramValue, param, paramtok) ;
1603 IF ptrToHeap # NulSym
1604 THEN
1605 IF IsDeallocate (proc)
1606 THEN
1607 SetupLAlias (ptrToHeap, Nil) ;
1608 SetVarInitialized (ptrToHeap, FALSE, paramtok)
1609 ELSE
1610 SetupIndr (ptrToHeap, heapValue) ;
1611 SetVarInitialized (ptrToHeap, TRUE, paramtok)
1612 END
1613 END
1614 END
1615 END ;
1616 DumpAliases
1617 END trashParam ;
1618
1619
1620 (*
1621 SetVarLRInitialized - this sets up an alias between the parameter
1622 value and the pointer for the case:
1623
1624 procedure foo (var shadow: PtrToType) ;
1625
1626 which allows shadow to be statically analyzed
1627 once it is re-assigned.
1628 *)
1629
1630 PROCEDURE SetVarLRInitialized (param: CARDINAL) ;
1631 VAR
1632 heap,
1633 shadow: CARDINAL ;
1634 BEGIN
1635 Assert (IsParameter (param)) ;
1636 shadow := GetParameterShadowVar (param) ;
1637 IF shadow # NulSym
1638 THEN
1639 IncludeItemIntoList (ignoreList, shadow)
1640 END ;
1641 heap := GetParameterHeapVar (param) ;
1642 IF (shadow # NulSym) AND (heap # NulSym)
1643 THEN
1644 PutVarInitialized (shadow, GetMode (shadow)) ;
1645 PutVarInitialized (heap, GetMode (heap)) ;
1646 SetupIndr (shadow, heap) ;
1647 IncludeItemIntoList (ignoreList, heap)
1648 END
1649 END SetVarLRInitialized ;
1650
1651
1652 (*
1653 TestBBSequence -
1654 *)
1655
1656 PROCEDURE TestBBSequence (procSym: CARDINAL; lst: List) ;
1657 VAR
1658 bbPtr : bbEntry ;
1659 bbi,
1660 i, n : CARDINAL ;
1661 warning: BOOLEAN ; (* Should we issue a warning rather than a note? *)
1662 BEGIN
1663 IF Debugging
1664 THEN
1665 DumpBBSequence (lst)
1666 END ;
1667 initBlock ;
1668 ForeachLocalSymDo (procSym, SetVarUninitialized) ;
1669 ForeachParamSymDo (procSym, SetVarLRInitialized) ;
1670 n := NoOfItemsInList (lst) ;
1671 i := 1 ;
1672 warning := TRUE ;
1673 WHILE i <= n DO
1674 bbi := GetItemFromList (lst, i) ;
1675 bbPtr := Indexing.GetIndice (bbArray, bbi) ;
1676 CheckReadBeforeInitFirstBasicBlock (procSym,
1677 bbPtr^.start, bbPtr^.end,
1678 warning, i) ;
1679 IF bbPtr^.endCond
1680 THEN
1681 (* Check to see if we are moving into an conditional block in which case
1682 we will issue a note. *)
1683 warning := FALSE
1684 ELSIF bbPtr^.endCall AND (bbPtr^.trashQuad # 0)
1685 THEN
1686 trashParam (bbPtr^.trashQuad)
1687 END ;
1688 INC (i)
1689 END ;
1690 killBlock
1691 END TestBBSequence ;
1692
1693
1694 (*
1695 CreateBBPermultations -
1696 *)
1697
1698 PROCEDURE CreateBBPermultations (procSym: CARDINAL; i: CARDINAL; lst: List) ;
1699 VAR
1700 duplst: List ;
1701 iPtr : bbEntry ;
1702 BEGIN
1703 IF i = 0
1704 THEN
1705 TestBBSequence (procSym, lst)
1706 ELSE
1707 iPtr := Indexing.GetIndice (bbArray, i) ;
1708 IF iPtr^.topOfLoop
1709 THEN
1710 TestBBSequence (procSym, lst)
1711 ELSE
1712 duplst := DuplicateList (lst) ;
1713 IncludeItemIntoList (duplst, i) ;
1714 IF iPtr^.endCall AND (iPtr^.trashQuad = 0)
1715 THEN
1716 TestBBSequence (procSym, duplst)
1717 ELSIF iPtr^.endGoto
1718 THEN
1719 CreateBBPermultations (procSym, iPtr^.nextBB, duplst)
1720 ELSIF UninitVariableConditionalChecking AND iPtr^.endCond
1721 THEN
1722 CreateBBPermultations (procSym, iPtr^.nextBB, duplst) ;
1723 CreateBBPermultations (procSym, iPtr^.condBB, duplst)
1724 ELSIF iPtr^.endCond
1725 THEN
1726 TestBBSequence (procSym, duplst)
1727 ELSE
1728 (* Fall through. *)
1729 CreateBBPermultations (procSym, iPtr^.nextBB, duplst)
1730 END ;
1731 KillList (duplst)
1732 END
1733 END
1734 END CreateBBPermultations ;
1735
1736
1737 (*
1738 ScopeBlockVariableAnalysis - checks to see whether a variable is
1739 read before it has been initialized.
1740 *)
1741
1742 PROCEDURE ScopeBlockVariableAnalysis (Scope: CARDINAL;
1743 Start, End: CARDINAL) ;
1744 VAR
1745 bb : BasicBlock ;
1746 lst: List ;
1747 BEGIN
1748 IF UninitVariableChecking
1749 THEN
1750 bbArray := Indexing.InitIndex (1) ;
1751 bb := InitBasicBlocksFromRange (Scope, Start, End) ;
1752 ForeachBasicBlockDo (bb, AppendEntry) ;
1753 KillBasicBlocks (bb) ;
1754 GenerateCFG ;
1755 IF Scope # NulSym
1756 THEN
1757 InitList (lst) ;
1758 IF Debugging
1759 THEN
1760 DumpBBArray (Scope) ;
1761 IF UninitVariableConditionalChecking
1762 THEN
1763 printf0 ("UninitVariableConditionalChecking is TRUE\n")
1764 END
1765 END ;
1766 CreateBBPermultations (Scope, 1, lst) ;
1767 KillList (lst)
1768 END ;
1769 bbArrayKill
1770 END
1771 END ScopeBlockVariableAnalysis ;
1772
1773
1774 (*
1775 GetOp3 -
1776 *)
1777
1778 PROCEDURE GetOp3 (quad: CARDINAL) : CARDINAL ;
1779 VAR
1780 op: QuadOperator ;
1781 op1, op2, op3: CARDINAL ;
1782 BEGIN
1783 GetQuad (quad, op, op1, op2, op3) ;
1784 RETURN op3
1785 END GetOp3 ;
1786
1787
1788 (*
1789 getBBindex - return the basic block index which starts with quad.
1790 *)
1791
1792 PROCEDURE getBBindex (quad: CARDINAL) : CARDINAL ;
1793 VAR
1794 iPtr : bbEntry ;
1795 i, high: CARDINAL ;
1796 BEGIN
1797 i := 1 ;
1798 high := Indexing.HighIndice (bbArray) ;
1799 WHILE i <= high DO
1800 iPtr := Indexing.GetIndice (bbArray, i) ;
1801 IF iPtr^.start = quad
1802 THEN
1803 RETURN iPtr^.indexBB
1804 END ;
1805 INC (i)
1806 END ;
1807 RETURN 0
1808 END getBBindex ;
1809
1810
1811 (*
1812 GenerateCFG -
1813 *)
1814
1815 PROCEDURE GenerateCFG ;
1816 VAR
1817 iPtr : bbEntry ;
1818 next,
1819 i, high: CARDINAL ;
1820 BEGIN
1821 i := 1 ;
1822 high := Indexing.HighIndice (bbArray) ;
1823 WHILE i <= high DO
1824 iPtr := Indexing.GetIndice (bbArray, i) ;
1825 IF IsKillLocalVar (iPtr^.end) OR IsReturn (iPtr^.end)
1826 THEN
1827 (* Nothing to do as we have reached the end of this scope. *)
1828 ELSE
1829 next := GetNextQuad (iPtr^.end) ;
1830 iPtr^.nextQuad := next ;
1831 iPtr^.nextBB := getBBindex (next) ;
1832 IF iPtr^.endCond
1833 THEN
1834 iPtr^.condQuad := GetOp3 (iPtr^.end) ;
1835 iPtr^.condBB := getBBindex (iPtr^.condQuad)
1836 END
1837 END ;
1838 INC (i)
1839 END
1840 END GenerateCFG ;
1841
1842
1843 (*
1844 NewEntry -
1845 *)
1846
1847 PROCEDURE NewEntry () : bbEntry ;
1848 VAR
1849 bbPtr: bbEntry ;
1850 BEGIN
1851 IF bbFreeList = NIL
1852 THEN
1853 NEW (bbPtr)
1854 ELSE
1855 bbPtr := bbFreeList ;
1856 bbFreeList := bbFreeList^.next
1857 END ;
1858 RETURN bbPtr
1859 END NewEntry ;
1860
1861
1862 (*
1863 IsAllocate - return TRUE is sym is ALLOCATE.
1864 *)
1865
1866 PROCEDURE IsAllocate (sym: CARDINAL) : BOOLEAN ;
1867 BEGIN
1868 RETURN IsProcedure (sym) AND (GetSymName (sym) = MakeKey('ALLOCATE'))
1869 END IsAllocate ;
1870
1871
1872 (*
1873 IsDeallocate - return TRUE is sym is DEALLOCATE.
1874 *)
1875
1876 PROCEDURE IsDeallocate (sym: CARDINAL) : BOOLEAN ;
1877 BEGIN
1878 RETURN IsProcedure (sym) AND (GetSymName (sym) = MakeKey('DEALLOCATE'))
1879 END IsDeallocate ;
1880
1881
1882 (*
1883 DetectTrash -
1884 *)
1885
1886 PROCEDURE DetectTrash (bbPtr: bbEntry) ;
1887 VAR
1888 i : CARDINAL ;
1889 op : QuadOperator ;
1890 op1, op2, op3: CARDINAL ;
1891 BEGIN
1892 IF bbPtr^.endCall
1893 THEN
1894 i := bbPtr^.start ;
1895 LOOP
1896 GetQuad (i, op, op1, op2, op3) ;
1897 IF (op = ParamOp) AND (op1 = 1) AND (IsAllocate (op2) OR IsDeallocate (op2))
1898 THEN
1899 bbPtr^.trashQuad := i
1900 END ;
1901 IF i = bbPtr^.end
1902 THEN
1903 RETURN
1904 END ;
1905 i := GetNextQuad (i)
1906 END
1907 END
1908 END DetectTrash ;
1909
1910
1911 (*
1912 AppendEntry -
1913 *)
1914
1915 PROCEDURE AppendEntry (bb: BasicBlock) ;
1916 VAR
1917 bbPtr: bbEntry ;
1918 high : CARDINAL ;
1919 BEGIN
1920 high := Indexing.HighIndice (bbArray) ;
1921 bbPtr := NewEntry () ;
1922 WITH bbPtr^ DO
1923 start := GetBasicBlockStart (bb) ;
1924 end := GetBasicBlockEnd (bb) ;
1925 first := high = 0 ;
1926 endCall := IsCall (end) ;
1927 endGoto := IsGoto (end) ;
1928 endCond := IsConditional (end) ;
1929 topOfLoop := IsBackReference (start) ;
1930 trashQuad := 0 ;
1931 indexBB := high + 1 ;
1932 nextQuad := 0 ;
1933 condQuad := 0 ;
1934 nextBB := 0 ;
1935 condBB := 0 ;
1936 next := NIL
1937 END ;
1938 DetectTrash (bbPtr) ;
1939 Indexing.PutIndice (bbArray, high + 1, bbPtr)
1940 END AppendEntry ;
1941
1942
1943 (*
1944 DumpAlias -
1945 *)
1946
1947 PROCEDURE DumpAlias (array: Index; aliasIndex: CARDINAL) ;
1948 VAR
1949 sa: symAlias ;
1950 BEGIN
1951 sa := Indexing.GetIndice (array, aliasIndex) ;
1952 printf2 ("keySym = %d: alias = %d\n", sa^.keySym, sa^.alias)
1953 END DumpAlias ;
1954
1955
1956 (*
1957 doDumpAliases -
1958 *)
1959
1960 PROCEDURE doDumpAliases (array: Index) ;
1961 VAR
1962 i, n: CARDINAL ;
1963 BEGIN
1964 i := 1 ;
1965 n := Indexing.HighIndice (array) ;
1966 WHILE i <= n DO
1967 DumpAlias (array, i) ;
1968 INC (i)
1969 END
1970 END doDumpAliases ;
1971
1972
1973 (*
1974 DumpAliases -
1975 *)
1976
1977 PROCEDURE DumpAliases ;
1978 BEGIN
1979 IF Debugging
1980 THEN
1981 printf0 ("LArray\n") ;
1982 doDumpAliases (LArray) ;
1983 printf0 ("IndirectArray\n") ;
1984 doDumpAliases (IndirectArray)
1985 END
1986 END DumpAliases ;
1987
1988
1989 (*
1990 newAlias -
1991 *)
1992
1993 PROCEDURE newAlias () : symAlias ;
1994 VAR
1995 sa: symAlias ;
1996 BEGIN
1997 IF freeList = NIL
1998 THEN
1999 NEW (sa)
2000 ELSE
2001 sa := freeList ;
2002 freeList := freeList^.next
2003 END ;
2004 RETURN sa
2005 END newAlias ;
2006
2007
2008 (*
2009 initAlias -
2010 *)
2011
2012 PROCEDURE initAlias (sym: CARDINAL) : symAlias ;
2013 VAR
2014 sa: symAlias ;
2015 BEGIN
2016 sa := newAlias () ;
2017 WITH sa^ DO
2018 keySym := sym ;
2019 alias := NulSym ;
2020 next := NIL
2021 END ;
2022 RETURN sa
2023 END initAlias ;
2024
2025
2026 (*
2027 killAlias -
2028 *)
2029
2030 PROCEDURE killAlias (sa: symAlias) ;
2031 BEGIN
2032 sa^.next := freeList ;
2033 freeList := sa
2034 END killAlias ;
2035
2036
2037 (*
2038 initBlock -
2039 *)
2040
2041 PROCEDURE initBlock ;
2042 BEGIN
2043 LArray := Indexing.InitIndex (1) ;
2044 IndirectArray := Indexing.InitIndex (1) ;
2045 InitList (ignoreList)
2046 END initBlock ;
2047
2048
2049 (*
2050 killBlock -
2051 *)
2052
2053 PROCEDURE killBlock ;
2054 BEGIN
2055 doKillBlock (LArray) ;
2056 doKillBlock (IndirectArray) ;
2057 KillList (ignoreList)
2058 END killBlock ;
2059
2060
2061 PROCEDURE doKillBlock (VAR array: Index) ;
2062 VAR
2063 i, n: CARDINAL ;
2064 BEGIN
2065 i := 1 ;
2066 n := Indexing.HighIndice (array) ;
2067 WHILE i <= n DO
2068 killAlias (Indexing.GetIndice (array, i)) ;
2069 INC (i)
2070 END ;
2071 array := Indexing.KillIndex (array)
2072 END doKillBlock ;
2073
2074
2075 (*
2076 addAlias -
2077 *)
2078
2079 PROCEDURE addAlias (array: Index; sym: CARDINAL; aliased: CARDINAL) ;
2080 VAR
2081 i, n: CARDINAL ;
2082 sa : symAlias ;
2083 BEGIN
2084 i := 1 ;
2085 n := Indexing.HighIndice (array) ;
2086 WHILE i <= n DO
2087 sa := Indexing.GetIndice (array, i) ;
2088 IF sa^.keySym = sym
2089 THEN
2090 sa^.alias := aliased ;
2091 RETURN
2092 END ;
2093 INC (i)
2094 END ;
2095 sa := initAlias (sym) ;
2096 Indexing.IncludeIndiceIntoIndex (array, sa) ;
2097 sa^.alias := aliased
2098 END addAlias ;
2099
2100
2101 (*
2102 lookupAlias -
2103 *)
2104
2105 PROCEDURE lookupAlias (array: Index; sym: CARDINAL) : symAlias ;
2106 VAR
2107 i, n: CARDINAL ;
2108 sa : symAlias ;
2109 BEGIN
2110 i := 1 ;
2111 n := Indexing.HighIndice (array) ;
2112 WHILE i <= n DO
2113 sa := Indexing.GetIndice (array, i) ;
2114 IF sa^.keySym = sym
2115 THEN
2116 RETURN sa
2117 END ;
2118 INC (i)
2119 END ;
2120 RETURN NIL
2121 END lookupAlias ;
2122
2123
2124 (*
2125 doGetAlias -
2126 *)
2127
2128 PROCEDURE doGetAlias (array: Index; sym: CARDINAL) : CARDINAL ;
2129 VAR
2130 sa: symAlias ;
2131 BEGIN
2132 sa := lookupAlias (array, sym) ;
2133 IF (sa # NIL) AND (sa^.alias # NulSym)
2134 THEN
2135 RETURN sa^.alias
2136 END ;
2137 RETURN NulSym
2138 END doGetAlias ;
2139
2140
2141 (*
2142 getLAlias - attempts to looks up an alias which is not a temporary variable.
2143 *)
2144
2145 PROCEDURE getLAlias (sym: CARDINAL) : CARDINAL ;
2146 VAR
2147 type,
2148 nsym: CARDINAL ;
2149 BEGIN
2150 nsym := sym ;
2151 REPEAT
2152 sym := nsym ;
2153 type := GetSType (sym) ;
2154 IF (IsTemporary (sym) AND (GetMode (sym) = LeftValue)) OR
2155 ((type # NulSym) AND IsReallyPointer (type))
2156 THEN
2157 nsym := doGetAlias (LArray, sym)
2158 ELSE
2159 RETURN sym
2160 END
2161 UNTIL nsym = NulSym ;
2162 RETURN sym
2163 END getLAlias ;
2164
2165
2166 (*
2167 SetupLAlias -
2168 *)
2169
2170 PROCEDURE SetupLAlias (des, exp: CARDINAL) ;
2171 BEGIN
2172 IF (exp = Nil) OR
2173 (IsVar (exp) AND
2174 ((GetMode (des) = LeftValue) OR IsReallyPointer (GetSType (des))))
2175 THEN
2176 addAlias (LArray, des, exp) ;
2177 DumpAliases
2178 END
2179 END SetupLAlias ;
2180
2181
2182 (*
2183 SetupIndr -
2184 *)
2185
2186 PROCEDURE SetupIndr (ptr, content: CARDINAL) ;
2187 BEGIN
2188 addAlias (IndirectArray, ptr, content) ;
2189 END SetupIndr ;
2190
2191
2192 (*
2193 getContent - attempts to return the content pointed to by ptr.
2194 sym is the original symbol and ptr will be the equivalent lvalue.
2195 *)
2196
2197 PROCEDURE getContent (ptr: CARDINAL; sym: CARDINAL; tok: CARDINAL) : CARDINAL ;
2198 BEGIN
2199 IF ptr = Nil
2200 THEN
2201 MetaErrorT1 (tok,
2202 "attempting to dereference {%1Wad} which will be a {%kNIL} pointer",
2203 sym) ;
2204 RETURN NulSym
2205 ELSE
2206 RETURN doGetAlias (IndirectArray, ptr)
2207 END
2208 END getContent ;
2209
2210
2211 (*
2212 init -
2213 *)
2214
2215 PROCEDURE init ;
2216 BEGIN
2217 freeList := NIL ;
2218 bbFreeList := NIL ;
2219 InitList (errorList)
2220 END init ;
2221
2222
2223 BEGIN
2224 init
2225 END M2SymInit.