Debian Codegraph @

gcc/m2/gm2-compiler/M2Range.mod

raw
1 (* M2Range.mod exports procedures which maintain the range checking.
2
3 Copyright (C) 2008-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 M2Range ;
23
24
25 FROM SymbolTable IMPORT NulSym, GetLowestType, PutReadQuad, RemoveReadQuad,
26 IsVar, IsConst, PushValue, GetSubrange, GetType,
27 IsSubrange, GetSymName, IsTemporary, IsSet,
28 IsRecord, IsPointer, IsArray, IsProcType, IsConstLit,
29 IsAModula2Type, IsUnbounded, IsEnumeration, GetMode,
30 IsConstString, MakeConstLit, SkipType, IsProcedure,
31 IsParameter, GetDeclaredMod, IsVarParamAny, GetNthParam,
32 ModeOfAddr ;
33
34 FROM SYSTEM IMPORT ADDRESS ;
35 FROM m2tree IMPORT debug_tree ;
36
37 FROM m2linemap IMPORT ErrorAt, GetFilenameFromLocation, GetColumnNoFromLocation, GetLineNoFromLocation ;
38
39 FROM m2type IMPORT GetMinFrom, GetMaxFrom,
40 GetIntegerType, GetTreeType,
41 GetPointerType,
42 AddStatement ;
43
44 FROM m2statement IMPORT BuildProcedureCallTree, BuildIfThenElseEnd, BuildIfThenDoEnd ;
45
46 FROM m2expr IMPORT CompareTrees, BuildSub, BuildAdd, GetIntegerZero, GetIntegerOne,
47 BuildAddr, BuildIndirect, BuildGreaterThan, BuildLessThan,
48 BuildGreaterThanOrEqual,
49 GetPointerZero, BuildNegate, BuildEqualTo, BuildLessThanOrEqual,
50 IsTrue, IsFalse, TreeOverflow ;
51
52 FROM m2convert IMPORT BuildConvert ;
53 FROM m2statement IMPORT BuildParam ;
54 FROM m2decl IMPORT BuildStringConstant, BuildIntegerConstant ;
55 FROM m2builtins IMPORT BuiltInIsfinite ;
56
57 FROM M2Debug IMPORT Assert ;
58 FROM Indexing IMPORT Index, InitIndex, InBounds, PutIndice, GetIndice ;
59 FROM Storage IMPORT ALLOCATE ;
60 FROM M2ALU IMPORT PushIntegerTree, PushInt, ConvertToInt, Equ, Gre, Less, GreEqu ;
61 FROM M2Options IMPORT VariantValueChecking, CaseEnumChecking, GetPIM, StrictTypeAssignment ;
62
63 FROM M2Error IMPORT Error, InternalError, ErrorFormat0, ErrorFormat1, ErrorFormat2, FlushErrors,
64 GetAnnounceScope ;
65
66 FROM M2ColorString IMPORT quoteOpen, quoteClose ;
67
68 FROM M2MetaError IMPORT MetaError1, MetaError2, MetaError3,
69 MetaErrorT0, MetaErrorT1, MetaErrorT2, MetaErrorT3,
70 MetaErrorsT1, MetaErrorsT2, MetaErrorsT3, MetaErrorsT4,
71 MetaErrorStringT1, MetaErrorStringT2, MetaErrorStringT3,
72 MetaString3 ;
73
74 FROM M2LexBuf IMPORT UnknownTokenNo, GetTokenNo, FindFileNameFromToken,
75 TokenToLineNo, TokenToColumnNo, TokenToLocation, MakeVirtual2Tok ;
76
77 FROM StrIO IMPORT WriteString, WriteLn ;
78 FROM NumberIO IMPORT WriteCard ;
79 FROM M2GCCDeclare IMPORT TryDeclareConstant, DeclareConstructor ;
80 FROM M2Quads IMPORT QuadOperator, PutQuad, SubQuad, WriteOperand ;
81 FROM SymbolConversion IMPORT GccKnowsAbout, Mod2Gcc ;
82 FROM Lists IMPORT List ;
83 FROM NameKey IMPORT Name, MakeKey, KeyToCharStar ;
84 FROM StdIO IMPORT Write ;
85 FROM DynamicStrings IMPORT String, string, Length, InitString, ConCat, ConCatChar, Mark, InitStringCharStar, KillString ;
86 FROM M2GenGCC IMPORT GetHighFromUnbounded, StringToChar, LValueToGenericPtr, ZConstToTypedConst ;
87 FROM M2System IMPORT Address, Word, Loc, Byte, IsWordN, IsRealN, IsComplexN ;
88 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2 ;
89
90 FROM M2Check IMPORT ParameterTypeCompatible, ExpressionTypeCompatible, AssignmentTypeCompatible ;
91
92 FROM M2Base IMPORT Nil, IsRealType, GetBaseTypeMinMax,
93 Cardinal, Integer, ZType, IsComplexType,
94 IsExpressionCompatible,
95 IsParameterCompatible,
96 ExceptionAssign,
97 ExceptionReturn,
98 ExceptionInc, ExceptionDec,
99 ExceptionIncl, ExceptionExcl,
100 ExceptionShift, ExceptionRotate,
101 ExceptionStaticArray, ExceptionDynamicArray,
102 ExceptionForLoopBegin, ExceptionForLoopTo, ExceptionForLoopEnd,
103 ExceptionPointerNil, ExceptionNoReturn, ExceptionCase,
104 ExceptionNonPosDiv, ExceptionNonPosMod,
105 ExceptionZeroDiv, ExceptionZeroRem,
106 ExceptionWholeValue, ExceptionRealValue,
107 ExceptionParameterBounds,
108 ExceptionNo ;
109
110 FROM M2CaseList IMPORT CaseBoundsResolved, OverlappingCaseBounds,
111 WriteCase, MissingCaseBounds, TypeCaseBounds,
112 MissingCaseStatementBounds ;
113
114 TYPE
115 TypeOfRange = (assignment, returnassignment, subrangeassignment,
116 inc, dec, incl, excl, shift, rotate,
117 typeindrx, typeexpr, typeassign, typeparam,
118 typereturn,
119 paramassign,
120 staticarraysubscript,
121 dynamicarraysubscript,
122 forloopbegin, forloopto, forloopend,
123 pointernil, noreturn, noelse,
124 casebounds,
125 wholenonposdiv, wholenonposmod,
126 wholezerodiv, wholezerorem, none) ;
127
128 Range = POINTER TO RECORD
129 type : TypeOfRange ;
130 des,
131 expr, expr2,
132 byconst,
133 desLowestType,
134 exprLowestType: CARDINAL ;
135 procedure : CARDINAL ;
136 paramNo : CARDINAL ;
137 isLeftValue : BOOLEAN ; (* is des an LValue,
138 only used in pointernil *)
139 dimension : CARDINAL ;
140 caseList : CARDINAL ;
141 destok,
142 exprtok,
143 expr2tok,
144 byconsttok,
145 tokenNo : CARDINAL ;
146 incrementquad : CARDINAL ; (* Increment quad used in FOR the loop. *)
147 errorReported : BOOLEAN ; (* error message reported yet? *)
148 strict : BOOLEAN ; (* is it a comparison expression? *)
149 isin : BOOLEAN ; (* expression created by IN operator? *)
150 cancelled : BOOLEAN ; (* Has this range been cancelled? *)
151 dependantid : CARDINAL ; (* The associated dependant range test. *)
152 END ;
153
154
155 VAR
156 TopOfRange: CARDINAL ;
157 RangeIndex: Index ;
158 BreakRange: CARDINAL ;
159
160
161 PROCEDURE gdbhook ;
162 END gdbhook ;
163
164
165 (*
166 BreakWhenRangeCreated - to be called interactively by gdb.
167 *)
168
169 PROCEDURE BreakWhenRangeCreated (r: CARDINAL) ;
170 BEGIN
171 BreakRange := r
172 END BreakWhenRangeCreated ;
173
174
175 (*
176 CheckBreak - if sym = BreakRange then call gdbhook.
177 *)
178
179 PROCEDURE CheckBreak (r: CARDINAL) ;
180 BEGIN
181 IF BreakRange = r
182 THEN
183 gdbhook
184 END
185 END CheckBreak ;
186
187
188 (*
189 OverlapsRange - returns TRUE if a1..a2 overlaps with b1..b2.
190 *)
191
192 PROCEDURE OverlapsRange (a1, a2, b1, b2: tree) : BOOLEAN ;
193 BEGIN
194 (* RETURN( ((a1<=b2) AND (a2>=b1)) ) *)
195 RETURN( (CompareTrees(a1, b2)<=0) AND (CompareTrees(a2, b1)>=0) )
196 END OverlapsRange ;
197
198
199 (*
200 IsGreater - returns TRUE if a>b.
201 *)
202
203 PROCEDURE IsGreater (a, b: tree) : BOOLEAN ;
204 BEGIN
205 RETURN( CompareTrees(a, b)>0 )
206 END IsGreater ;
207
208
209 (*
210 IsGreaterOrEqual - returns TRUE if a>=b.
211 *)
212
213 PROCEDURE IsGreaterOrEqual (a, b: tree) : BOOLEAN ;
214 BEGIN
215 RETURN( CompareTrees(a, b)>=0 )
216 END IsGreaterOrEqual ;
217
218
219 (*
220 IsEqual - returns TRUE if a=b.
221 *)
222
223 PROCEDURE IsEqual (a, b: tree) : BOOLEAN ;
224 BEGIN
225 RETURN( CompareTrees(a, b)=0 )
226 END IsEqual ;
227
228
229 (*
230 IsGreaterOrEqualConversion - tests whether t>=e.
231 *)
232
233 PROCEDURE IsGreaterOrEqualConversion (location: location_t; l: CARDINAL; d, e: CARDINAL) : BOOLEAN ;
234 BEGIN
235 IF GetType(d)=NulSym
236 THEN
237 IF GetType(e)=NulSym
238 THEN
239 RETURN( IsGreaterOrEqual(Mod2Gcc(l), LValueToGenericPtr(location, e)) )
240 ELSE
241 RETURN( IsGreaterOrEqual(BuildConvert(location, Mod2Gcc(SkipType(GetType(e))), Mod2Gcc(l), FALSE),
242 LValueToGenericPtr(location, e)) )
243 END
244 ELSE
245 RETURN( IsGreaterOrEqual(BuildConvert(location, Mod2Gcc(SkipType(GetType(d))), Mod2Gcc(l), FALSE),
246 LValueToGenericPtr(location, e)) )
247 END
248 END IsGreaterOrEqualConversion ;
249
250
251 (*
252 IsEqualConversion - returns TRUE if a=b.
253 *)
254
255 PROCEDURE IsEqualConversion (l: CARDINAL; d, e: CARDINAL) : BOOLEAN ;
256 VAR
257 location: location_t ;
258 BEGIN
259 location := TokenToLocation(GetDeclaredMod(l)) ;
260 IF GetType(d)=NulSym
261 THEN
262 IF GetType(e)=NulSym
263 THEN
264 RETURN( IsEqual(Mod2Gcc(l), LValueToGenericPtr(location, e)) )
265 ELSE
266 RETURN( IsEqual(BuildConvert(location, Mod2Gcc(SkipType(GetType(e))), Mod2Gcc(l), FALSE),
267 LValueToGenericPtr(location, e)) )
268 END
269 ELSE
270 RETURN( IsEqual(BuildConvert(location, Mod2Gcc(SkipType(GetType(d))), Mod2Gcc(l), FALSE),
271 LValueToGenericPtr(location, e)) )
272 END
273 END IsEqualConversion ;
274
275
276 (*
277 lookupExceptionHandler -
278 *)
279
280 PROCEDURE lookupExceptionHandler (type: TypeOfRange) : CARDINAL ;
281 BEGIN
282 CASE type OF
283
284 assignment : RETURN( ExceptionAssign ) |
285 returnassignment : RETURN( ExceptionReturn ) |
286 subrangeassignment : InternalError ('not expecting this case value') |
287 inc : RETURN( ExceptionInc ) |
288 dec : RETURN( ExceptionDec ) |
289 incl : RETURN( ExceptionIncl ) |
290 excl : RETURN( ExceptionExcl ) |
291 shift : RETURN( ExceptionShift ) |
292 rotate : RETURN( ExceptionRotate ) |
293 typeassign,
294 typeparam,
295 typeexpr,
296 typeindrx : InternalError ('not expecting this case value') |
297 paramassign : RETURN( ExceptionParameterBounds ) |
298 staticarraysubscript : RETURN( ExceptionStaticArray ) |
299 dynamicarraysubscript: RETURN( ExceptionDynamicArray ) |
300 forloopbegin : RETURN( ExceptionForLoopBegin ) |
301 forloopto : RETURN( ExceptionForLoopTo ) |
302 forloopend : RETURN( ExceptionForLoopEnd ) |
303 pointernil : RETURN( ExceptionPointerNil ) |
304 noreturn : RETURN( ExceptionNoReturn ) |
305 noelse : RETURN( ExceptionCase ) |
306 casebounds : InternalError ('not expecting this case value') |
307 wholenonposdiv : RETURN( ExceptionNonPosDiv ) |
308 wholenonposmod : RETURN( ExceptionNonPosMod ) |
309 wholezerodiv : RETURN( ExceptionZeroDiv ) |
310 wholezerorem : RETURN( ExceptionZeroRem ) |
311 none : RETURN( ExceptionNo )
312
313 ELSE
314 InternalError ('enumeration value unknown')
315 END
316 END lookupExceptionHandler ;
317
318
319 (*
320 InitRange - returns a new range item.
321 *)
322
323 PROCEDURE InitRange () : CARDINAL ;
324 VAR
325 r: CARDINAL ;
326 p: Range ;
327 BEGIN
328 INC(TopOfRange) ;
329 r := TopOfRange ;
330 NEW(p) ;
331 IF p=NIL
332 THEN
333 InternalError ('out of memory error')
334 ELSE
335 CheckBreak (r) ;
336 WITH p^ DO
337 type := none ;
338 des := NulSym ;
339 expr := NulSym ;
340 expr2 := NulSym ;
341 byconst := NulSym ;
342 desLowestType := NulSym ;
343 exprLowestType := NulSym ;
344 isLeftValue := FALSE ; (* ignored in all cases other *)
345 dimension := 0 ;
346 caseList := 0 ;
347 tokenNo := UnknownTokenNo ; (* than pointernil *)
348 destok := UnknownTokenNo ;
349 exprtok := UnknownTokenNo ;
350 expr2tok := UnknownTokenNo ;
351 byconsttok := UnknownTokenNo ;
352 incrementquad := 0 ;
353 errorReported := FALSE ;
354 cancelled := FALSE ;
355 dependantid := 0
356 END ;
357 PutIndice(RangeIndex, r, p)
358 END ;
359 RETURN( r )
360 END InitRange ;
361
362
363 (*
364 reportedError - returns whether this is the first time this error has been
365 reported.
366 *)
367
368 PROCEDURE reportedError (r: CARDINAL) : BOOLEAN ;
369 VAR
370 p: Range ;
371 BEGIN
372 p := GetIndice (RangeIndex, r) ;
373 RETURN p^.errorReported
374 END reportedError ;
375
376
377 (*
378 setReported - assigns errorReported to TRUE.
379 *)
380
381 PROCEDURE setReported (r: CARDINAL) ;
382 VAR
383 p: Range ;
384 BEGIN
385 p := GetIndice (RangeIndex, r) ;
386 p^.errorReported := TRUE
387 END setReported ;
388
389
390 (*
391 PutRangeForIncrement - places incrementquad into the range record.
392 *)
393
394 PROCEDURE PutRangeForIncrement (range: CARDINAL; incrementquad: CARDINAL) ;
395 VAR
396 p: Range ;
397 BEGIN
398 p := GetIndice (RangeIndex, range) ;
399 p^.incrementquad := incrementquad
400 END PutRangeForIncrement ;
401
402
403 (*
404 PutRange - initializes contents of, p, to
405 d, e and their lowest types.
406 It also fills in the current token no
407 and returns, p.
408 *)
409
410 PROCEDURE PutRange (tokno: CARDINAL; p: Range; t: TypeOfRange; d, e: CARDINAL) : Range ;
411 BEGIN
412 WITH p^ DO
413 type := t ;
414 des := d ;
415 expr := e ;
416 desLowestType := GetLowestType (d) ;
417 exprLowestType := GetLowestType (e) ;
418 tokenNo := tokno ;
419 strict := FALSE ;
420 isin := FALSE
421 END ;
422 RETURN p
423 END PutRange ;
424
425
426 (*
427 PutRangeDesExpr2 - initializes contents of, p, to
428 des, expr1 and their lowest types.
429 It also fills in the token numbers for
430 des, expr, expr2 and returns, p.
431 *)
432
433 PROCEDURE PutRangeDesExpr2 (p: Range; t: TypeOfRange;
434 des, destok,
435 expr1, expr1tok,
436 expr2, expr2tok,
437 byconst, byconsttok: CARDINAL) : Range ;
438 BEGIN
439 p^.des := des ;
440 p^.destok := destok ;
441 p^.expr := expr1 ;
442 p^.exprtok := expr1tok ;
443 p^.expr2 := expr2 ;
444 p^.expr2tok := expr2tok ;
445 p^.byconst := byconst ;
446 p^.byconsttok := byconsttok ;
447 WITH p^ DO
448 type := t ;
449 desLowestType := GetLowestType (des) ;
450 exprLowestType := GetLowestType (expr1) ;
451 strict := FALSE ;
452 isin := FALSE
453 END ;
454 RETURN p
455 END PutRangeDesExpr2 ;
456
457
458 (*
459 chooseTokenPos - returns, tokenpos, if it is not the unknown location, otherwise
460 it returns GetTokenNo.
461 *)
462
463 PROCEDURE chooseTokenPos (tokenpos: CARDINAL) : CARDINAL ;
464 BEGIN
465 IF tokenpos = UnknownTokenNo
466 THEN
467 RETURN GetTokenNo ()
468 ELSE
469 RETURN tokenpos
470 END
471 END chooseTokenPos ;
472
473
474 (*
475 PutRangeNoLow - initializes contents of, p. It
476 does not set lowest types as they may be
477 unknown at this point.
478 *)
479
480 PROCEDURE PutRangeNoLow (tokpos: CARDINAL; p: Range; t: TypeOfRange; d, e: CARDINAL) : Range ;
481 BEGIN
482 WITH p^ DO
483 type := t ;
484 des := d ;
485 expr := e ;
486 desLowestType := NulSym ;
487 exprLowestType := NulSym ;
488 isLeftValue := FALSE ;
489 tokenNo := chooseTokenPos (tokpos) ;
490 strict := FALSE ;
491 isin := FALSE
492 END ;
493 RETURN p
494 END PutRangeNoLow ;
495
496
497 (*
498 PutRangeExpr - initializes contents of, p. It
499 does not set lowest types as they may be
500 unknown at this point.
501 *)
502
503 PROCEDURE PutRangeExpr (tokpos: CARDINAL; p: Range; t: TypeOfRange;
504 d, e: CARDINAL; strict, isin: BOOLEAN) : Range ;
505 BEGIN
506 WITH p^ DO
507 type := t ;
508 des := d ;
509 expr := e ;
510 desLowestType := NulSym ;
511 exprLowestType := NulSym ;
512 isLeftValue := FALSE ;
513 tokenNo := chooseTokenPos (tokpos) ;
514 END ;
515 p^.strict := strict ;
516 p^.isin := isin ;
517 RETURN p
518 END PutRangeExpr ;
519
520
521 (*
522 PutRangePointer - initializes contents of, p, to
523 d, isLeft and their lowest types.
524 It also fills in the current token no
525 and returns, p.
526 *)
527
528 PROCEDURE PutRangePointer (tokpos: CARDINAL;
529 p: Range; d: CARDINAL; isLeft: BOOLEAN) : Range ;
530 BEGIN
531 WITH p^ DO
532 type := pointernil ;
533 des := d ;
534 expr := NulSym ;
535 desLowestType := GetLowestType(GetType(d)) ;
536 exprLowestType := NulSym ;
537 isLeftValue := isLeft ;
538 tokenNo := tokpos ;
539 strict := FALSE ;
540 isin := FALSE
541 END ;
542 RETURN p
543 END PutRangePointer ;
544
545
546 (*
547 PutRangeNoEval - initializes contents of, p, to a non evaluation
548 runtime check such as a no else clause or
549 no return found in function call.
550 *)
551
552 PROCEDURE PutRangeNoEval (p: Range; t: TypeOfRange) : Range ;
553 BEGIN
554 WITH p^ DO
555 type := t ;
556 tokenNo := GetTokenNo ()
557 END ;
558 RETURN p
559 END PutRangeNoEval ;
560
561
562 (*
563 PutRange - initializes contents of, p, to
564 d, e and its lowest type.
565 It also fills in the current token no
566 and returns, p.
567 *)
568
569 PROCEDURE PutRangeUnary (tokno: CARDINAL; p: Range; t: TypeOfRange; d, e: CARDINAL) : Range ;
570 BEGIN
571 WITH p^ DO
572 type := t ;
573 des := d ;
574 expr := e ;
575 desLowestType := GetLowestType(d) ;
576 exprLowestType := NulSym ;
577 isLeftValue := FALSE ;
578 tokenNo := chooseTokenPos (tokno) ;
579 strict := FALSE ;
580 isin := FALSE
581 END ;
582 RETURN( p )
583 END PutRangeUnary ;
584
585
586 (*
587 PutRangeParam - initializes contents of, p, to contain the parameter
588 type checking information.
589 It also fills in the current token no
590 and returns, p.
591 *)
592
593 PROCEDURE PutRangeParam (tokno: CARDINAL; p: Range; t: TypeOfRange; proc: CARDINAL;
594 paramno: CARDINAL; formal, actual: CARDINAL;
595 depRangeId: CARDINAL) : Range ;
596 BEGIN
597 WITH p^ DO
598 type := t ;
599 des := formal ;
600 expr := actual ;
601 desLowestType := NulSym ;
602 exprLowestType := NulSym ;
603 procedure := proc ;
604 paramNo := paramno ;
605 isLeftValue := FALSE ;
606 tokenNo := tokno ;
607 strict := FALSE ;
608 isin := FALSE ;
609 dependantid := depRangeId
610 END ;
611 RETURN p
612 END PutRangeParam ;
613
614
615 (*
616 PutRangeArraySubscript - initializes contents of, p, to
617 d, e and their lowest types. It also
618 assigns, dim.
619 It also fills in the current token no
620 and returns, p.
621 *)
622
623 PROCEDURE PutRangeArraySubscript (p: Range; t: TypeOfRange;
624 d, e: CARDINAL; dim: CARDINAL) : Range ;
625 BEGIN
626 WITH p^ DO
627 type := t ;
628 des := d ;
629 expr := e ;
630 desLowestType := GetLowestType(d) ;
631 exprLowestType := GetLowestType(e) ;
632 dimension := dim ;
633 tokenNo := GetTokenNo () ;
634 strict := FALSE ;
635 isin := FALSE
636 END ;
637 RETURN p
638 END PutRangeArraySubscript ;
639
640
641 (*
642 InitAssignmentRangeCheck - returns a range check node which
643 remembers the information necessary
644 so that a range check for des := expr
645 can be generated later on.
646 *)
647
648 PROCEDURE InitAssignmentRangeCheck (tokno: CARDINAL;
649 des, expr: CARDINAL;
650 destok, exprtok: CARDINAL) : CARDINAL ;
651 VAR
652 r: CARDINAL ;
653 p: Range ;
654 BEGIN
655 r := InitRange () ;
656 p := GetIndice (RangeIndex, r) ;
657 Assert (PutRange (tokno, p, assignment, des, expr) # NIL) ;
658 p^.destok := destok ;
659 p^.exprtok := exprtok ;
660 RETURN r
661 END InitAssignmentRangeCheck ;
662
663
664 (*
665 InitReturnRangeCheck - returns a range check node which
666 remembers the information necessary
667 so that a range check for RETURN e
668 from procedure, d, can be generated later on.
669 *)
670
671 PROCEDURE InitReturnRangeCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
672 VAR
673 r: CARDINAL ;
674 BEGIN
675 r := InitRange () ;
676 Assert (PutRange (tokno, GetIndice (RangeIndex, r), returnassignment, d, e) # NIL) ;
677 RETURN r
678 END InitReturnRangeCheck ;
679
680
681 (*
682 InitSubrangeRangeCheck - returns a range check node which
683 remembers the information necessary
684 so that a range check for d := e
685 can be generated later on.
686 *)
687
688 PROCEDURE InitSubrangeRangeCheck (d, e: CARDINAL) : CARDINAL ;
689 VAR
690 r: CARDINAL ;
691 BEGIN
692 r := InitRange () ;
693 Assert (PutRange (GetTokenNo (), GetIndice (RangeIndex, r), subrangeassignment, d, e) # NIL) ;
694 RETURN r
695 END InitSubrangeRangeCheck ;
696
697
698 (*
699 InitStaticArraySubscriptRangeCheck - returns a range check node which
700 remembers the information necessary
701 so that a range check for d[e]
702 can be generated later on.
703 *)
704
705 PROCEDURE InitStaticArraySubscriptRangeCheck (d, e, dim: CARDINAL) : CARDINAL ;
706 VAR
707 r: CARDINAL ;
708 BEGIN
709 r := InitRange () ;
710 Assert (PutRangeArraySubscript (GetIndice (RangeIndex, r), staticarraysubscript, d, e, dim) # NIL) ;
711 RETURN r
712 END InitStaticArraySubscriptRangeCheck ;
713
714
715 (*
716 InitDynamicArraySubscriptRangeCheck - returns a range check node which
717 remembers the information necessary
718 so that a range check for d[e]
719 can be generated later on.
720 *)
721
722 PROCEDURE InitDynamicArraySubscriptRangeCheck (d, e, dim: CARDINAL) : CARDINAL ;
723 VAR
724 r: CARDINAL ;
725 BEGIN
726 r := InitRange () ;
727 Assert (PutRangeArraySubscript (GetIndice (RangeIndex, r), dynamicarraysubscript, d, e, dim) # NIL) ;
728 RETURN r
729 END InitDynamicArraySubscriptRangeCheck ;
730
731
732 (*
733 InitIncRangeCheck - returns a range check node which
734 remembers the information necessary
735 so that a range check for INC(d, e)
736 can be generated later on.
737 *)
738
739 PROCEDURE InitIncRangeCheck (d, e: CARDINAL) : CARDINAL ;
740 VAR
741 r: CARDINAL ;
742 BEGIN
743 r := InitRange () ;
744 Assert (PutRange (GetTokenNo (), GetIndice (RangeIndex, r), inc, d, e) # NIL) ;
745 RETURN r
746 END InitIncRangeCheck ;
747
748
749 (*
750 InitDecRangeCheck - returns a range check node which
751 remembers the information necessary
752 so that a range check for DEC(d, e)
753 can be generated later on.
754 *)
755
756 PROCEDURE InitDecRangeCheck (d, e: CARDINAL) : CARDINAL ;
757 VAR
758 r: CARDINAL ;
759 BEGIN
760 r := InitRange () ;
761 Assert (PutRange (GetTokenNo (), GetIndice (RangeIndex, r), dec, d, e) # NIL) ;
762 RETURN r
763 END InitDecRangeCheck ;
764
765
766 (*
767 InitInclCheck - checks to see that bit, e, is type compatible with
768 e and also in range.
769 *)
770
771 PROCEDURE InitInclCheck (d, e: CARDINAL) : CARDINAL ;
772 VAR
773 r: CARDINAL ;
774 BEGIN
775 r := InitRange () ;
776 Assert (PutRangeNoLow (GetTokenNo (), GetIndice (RangeIndex, r), incl, d, e) # NIL) ;
777 RETURN r
778 END InitInclCheck ;
779
780
781 (*
782 InitExclCheck - checks to see that bit, e, is type compatible with
783 e and also in range.
784 *)
785
786 PROCEDURE InitExclCheck (d, e: CARDINAL) : CARDINAL ;
787 VAR
788 r: CARDINAL ;
789 BEGIN
790 r := InitRange () ;
791 Assert (PutRangeNoLow (GetTokenNo (), GetIndice (RangeIndex, r), excl, d, e) # NIL) ;
792 RETURN r
793 END InitExclCheck ;
794
795
796 (*
797 InitShiftCheck - checks to see that bit, e, is type compatible with
798 d and also in range.
799 *)
800
801 PROCEDURE InitShiftCheck (d, e: CARDINAL) : CARDINAL ;
802 VAR
803 r: CARDINAL ;
804 BEGIN
805 r := InitRange () ;
806 Assert (PutRangeNoLow (GetTokenNo (), GetIndice (RangeIndex, r), shift, d, e) # NIL) ;
807 RETURN r
808 END InitShiftCheck ;
809
810
811 (*
812 InitRotateCheck - checks to see that bit, e, is type compatible with
813 d and also in range.
814 *)
815
816 PROCEDURE InitRotateCheck (d, e: CARDINAL) : CARDINAL ;
817 VAR
818 r: CARDINAL ;
819 BEGIN
820 r := InitRange () ;
821 Assert (PutRangeNoLow (GetTokenNo (), GetIndice (RangeIndex, r), rotate, d, e) # NIL) ;
822 RETURN r
823 END InitRotateCheck ;
824
825
826 (*
827 InitTypesAssignmentCheck - checks to see that the types of d and e
828 are assignment compatible.
829 *)
830
831 PROCEDURE InitTypesAssignmentCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
832 VAR
833 r: CARDINAL ;
834 BEGIN
835 r := InitRange () ;
836 Assert (PutRangeNoLow (tokno, GetIndice (RangeIndex, r), typeassign, d, e) # NIL) ;
837 RETURN r
838 END InitTypesAssignmentCheck ;
839
840
841 (*
842 InitTypesIndrXCheck - checks to see that the types of d and e
843 are assignment compatible. The type checking
844 will dereference *e during the type check.
845 d = *e.
846 *)
847
848 PROCEDURE InitTypesIndrXCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
849 VAR
850 r: CARDINAL ;
851 BEGIN
852 r := InitRange () ;
853 Assert (PutRangeNoLow (tokno, GetIndice (RangeIndex, r), typeindrx, d, e) # NIL) ;
854 RETURN r
855 END InitTypesIndrXCheck ;
856
857
858 (*
859 InitTypesReturnTypeCheck - checks to see that the types of des and func
860 are assignment compatible.
861 *)
862
863 PROCEDURE InitTypesReturnTypeCheck (tokno: CARDINAL; func, val: CARDINAL) : CARDINAL ;
864 VAR
865 r: CARDINAL ;
866 BEGIN
867 r := InitRange () ;
868 Assert (PutRangeNoLow (tokno, GetIndice (RangeIndex, r), typereturn, func, val) # NIL) ;
869 RETURN r
870 END InitTypesReturnTypeCheck ;
871
872
873 (*
874 InitTypesParameterCheck - checks to see that the types of, d,
875 and, e, are parameter compatible.
876 *)
877
878 PROCEDURE InitTypesParameterCheck (tokno: CARDINAL;
879 proc: CARDINAL; paramno: CARDINAL;
880 formal, actual: CARDINAL;
881 depRangeId: CARDINAL) : CARDINAL ;
882 VAR
883 r: CARDINAL ;
884 BEGIN
885 r := InitRange () ;
886 Assert (PutRangeParam (tokno, GetIndice (RangeIndex, r), typeparam, proc,
887 paramno, formal, actual, depRangeId) # NIL) ;
888 RETURN r
889 END InitTypesParameterCheck ;
890
891
892 (*
893 PutRangeParamAssign - initializes contents of, p, to contain the parameter
894 type checking information.
895 It also fills in the current token no
896 and returns, p.
897 *)
898
899 PROCEDURE PutRangeParamAssign (tokno: CARDINAL; p: Range; t: TypeOfRange; proc: CARDINAL;
900 i: CARDINAL; formal, actual: CARDINAL; parentRangeId: CARDINAL) : Range ;
901 BEGIN
902 WITH p^ DO
903 type := t ;
904 des := formal ;
905 expr := actual ;
906 desLowestType := GetLowestType (des) ;
907 exprLowestType := GetLowestType (expr) ;
908 procedure := proc ;
909 paramNo := i ;
910 dimension := i ;
911 isLeftValue := FALSE ;
912 tokenNo := tokno ;
913 dependantid := parentRangeId
914 END ;
915 RETURN( p )
916 END PutRangeParamAssign ;
917
918
919 (*
920 InitParameterRangeCheck - checks to see that the types of, d, and, e,
921 are parameter compatible.
922 *)
923
924 PROCEDURE InitParameterRangeCheck (tokno: CARDINAL; proc: CARDINAL; paramno: CARDINAL;
925 formal, actual: CARDINAL; parentRangeId: CARDINAL) : CARDINAL ;
926 VAR
927 r: CARDINAL ;
928 BEGIN
929 r := InitRange () ;
930 Assert (PutRangeParamAssign (tokno, GetIndice (RangeIndex, r), paramassign, proc,
931 paramno, formal, actual, parentRangeId) # NIL) ;
932 RETURN r
933 END InitParameterRangeCheck ;
934
935
936 (*
937 InitTypesExpressionCheck - checks to see that the types of, d, and, e,
938 are expression compatible.
939 *)
940
941 PROCEDURE InitTypesExpressionCheck (tokno: CARDINAL; d, e: CARDINAL; strict, isin: BOOLEAN) : CARDINAL ;
942 VAR
943 r: CARDINAL ;
944 BEGIN
945 r := InitRange() ;
946 Assert (PutRangeExpr (tokno, GetIndice (RangeIndex, r), typeexpr, d, e, strict, isin) # NIL) ;
947 RETURN r
948 END InitTypesExpressionCheck ;
949
950
951 (*
952 InitForLoopBeginRangeCheck - returns a range check node which
953 remembers the information necessary
954 so that a range check for
955 FOR des := expr1 TO expr2 DO
956 can be generated later on. expr2 is
957 only used to type check with des.
958 *)
959
960 PROCEDURE InitForLoopBeginRangeCheck (des, destok,
961 expr1, expr1tok,
962 expr2, expr2tok,
963 byconst, byconsttok: CARDINAL) : CARDINAL ;
964 VAR
965 r: CARDINAL ;
966 BEGIN
967 r := InitRange () ;
968 Assert (PutRangeDesExpr2 (GetIndice (RangeIndex, r), forloopbegin,
969 des, destok,
970 expr1, expr1tok,
971 expr2, expr2tok,
972 byconst, byconsttok) # NIL) ;
973 RETURN r
974 END InitForLoopBeginRangeCheck ;
975
976
977 (*
978 InitForLoopToRangeCheck - returns a range check node which
979 remembers the information necessary
980 so that a range check for FOR d := e TO .. DO
981 can be generated later on.
982 *)
983
984 PROCEDURE InitForLoopToRangeCheck (d, e: CARDINAL) : CARDINAL ;
985 VAR
986 r: CARDINAL ;
987 BEGIN
988 r := InitRange () ;
989 Assert (PutRange (GetTokenNo (), GetIndice (RangeIndex, r), forloopto, d, e) # NIL) ;
990 RETURN r
991 END InitForLoopToRangeCheck ;
992
993
994 (*
995 InitForLoopEndRangeCheck - returns a range check node which
996 remembers the information necessary
997 so that a range check for
998 INC or DEC(d, e)
999 can be generated later on.
1000 *)
1001
1002 PROCEDURE InitForLoopEndRangeCheck (d, e: CARDINAL) : CARDINAL ;
1003 VAR
1004 r: CARDINAL ;
1005 BEGIN
1006 r := InitRange () ;
1007 Assert (PutRange (GetTokenNo (), GetIndice (RangeIndex, r), forloopend, d, e) # NIL) ;
1008 RETURN r
1009 END InitForLoopEndRangeCheck ;
1010
1011
1012 (*
1013 InitPointerRangeCheck - creates a pointer # NIL check.
1014 *)
1015
1016 PROCEDURE InitPointerRangeCheck (tokno: CARDINAL;
1017 d: CARDINAL; isLeft: BOOLEAN) : CARDINAL ;
1018 VAR
1019 r: CARDINAL ;
1020 BEGIN
1021 r := InitRange () ;
1022 Assert (PutRangePointer (tokno, GetIndice (RangeIndex, r), d, isLeft) # NIL) ;
1023 RETURN r
1024 END InitPointerRangeCheck ;
1025
1026
1027 (*
1028 InitNoReturnRangeCheck - creates a check held in the function
1029 to detect the absence of a RETURN
1030 statement at runtime.
1031 *)
1032
1033 PROCEDURE InitNoReturnRangeCheck () : CARDINAL ;
1034 VAR
1035 r: CARDINAL ;
1036 BEGIN
1037 r := InitRange () ;
1038 Assert (PutRangeNoEval (GetIndice(RangeIndex, r), noreturn) # NIL) ;
1039 RETURN r
1040 END InitNoReturnRangeCheck ;
1041
1042
1043 (*
1044 InitNoElseRangeCheck - creates a check held at the end of
1045 a CASE statement without an ELSE
1046 clause to detect its absence
1047 at runtime.
1048 *)
1049
1050 PROCEDURE InitNoElseRangeCheck () : CARDINAL ;
1051 VAR
1052 r: CARDINAL ;
1053 BEGIN
1054 r := InitRange () ;
1055 Assert (PutRangeNoEval (GetIndice (RangeIndex, r), noelse) # NIL) ;
1056 RETURN r
1057 END InitNoElseRangeCheck ;
1058
1059
1060 (*
1061 InitWholeNonPosDivCheck - creates a check expression for non positive
1062 or zero 2nd operand to division.
1063 *)
1064
1065 PROCEDURE InitWholeNonPosDivCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
1066 VAR
1067 r: CARDINAL ;
1068 BEGIN
1069 r := InitRange () ;
1070 Assert (PutRangeUnary (tokno, GetIndice (RangeIndex, r), wholenonposdiv, d, e) # NIL) ;
1071 RETURN r
1072 END InitWholeNonPosDivCheck ;
1073
1074
1075 (*
1076 InitWholeNonPosModCheck - creates a check expression for non positive
1077 or zero 2nd operand to modulus.
1078 *)
1079
1080 PROCEDURE InitWholeNonPosModCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
1081 VAR
1082 r: CARDINAL ;
1083 BEGIN
1084 r := InitRange () ;
1085 Assert (PutRangeUnary (tokno, GetIndice (RangeIndex, r), wholenonposmod, d, e) # NIL) ;
1086 RETURN r
1087 END InitWholeNonPosModCheck ;
1088
1089
1090 (*
1091 InitWholeZeroDivisionCheck - creates a check expression for zero 2nd
1092 operand for division.
1093 *)
1094
1095 PROCEDURE InitWholeZeroDivisionCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
1096 VAR
1097 r: CARDINAL ;
1098 BEGIN
1099 r := InitRange () ;
1100 Assert (PutRangeUnary (tokno, GetIndice (RangeIndex, r), wholezerodiv, d, e) # NIL) ;
1101 RETURN r
1102 END InitWholeZeroDivisionCheck ;
1103
1104
1105 (*
1106 InitWholeZeroRemainderCheck - creates a check expression for zero 2nd
1107 operand for remainder.
1108 *)
1109
1110 PROCEDURE InitWholeZeroRemainderCheck (tokno: CARDINAL; d, e: CARDINAL) : CARDINAL ;
1111 VAR
1112 r: CARDINAL ;
1113 BEGIN
1114 r := InitRange () ;
1115 Assert (PutRangeUnary (tokno, GetIndice (RangeIndex, r), wholezerorem, d, e) # NIL) ;
1116 RETURN r
1117 END InitWholeZeroRemainderCheck ;
1118
1119
1120 (*
1121 FoldNil - attempts to fold the pointer against nil comparison.
1122 *)
1123
1124 PROCEDURE FoldNil (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1125 VAR
1126 p: Range ;
1127 BEGIN
1128 p := GetIndice(RangeIndex, r) ;
1129 WITH p^ DO
1130 TryDeclareConstant (tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1131 IF GccKnowsAbout (des) AND IsConst (des)
1132 THEN
1133 PushValue (des) ;
1134 PushValue (Nil) ;
1135 IF Equ (tokenno)
1136 THEN
1137 MetaErrorT1 (tokenNo,
1138 'attempting to dereference a pointer {%1Wa} whose value will be NIL',
1139 des) ;
1140 PutQuad (q, ErrorOp, NulSym, NulSym, r)
1141 ELSE
1142 SubQuad (q)
1143 END
1144 END
1145 END
1146 END FoldNil ;
1147
1148
1149 (*
1150 GetMinMax - returns TRUE if we know the max and min of m2type.
1151 *)
1152
1153 PROCEDURE GetMinMax (tokenno: CARDINAL; type: CARDINAL; VAR min, max: tree) : BOOLEAN ;
1154 VAR
1155 minC, maxC: CARDINAL ;
1156 location : location_t ;
1157 BEGIN
1158 location := TokenToLocation(tokenno) ;
1159 Assert (IsAModula2Type (type)) ;
1160 IF GccKnowsAbout(type) AND (NOT IsPointer(type)) AND
1161 (NOT IsArray(type)) AND (NOT IsRecord(type)) AND
1162 (NOT IsRecord(type)) AND (NOT IsUnbounded(type)) AND
1163 (NOT IsProcType(type)) AND (NOT IsRealType(type)) AND
1164 (NOT IsRealN(type)) AND (NOT IsComplexType(type)) AND
1165 (NOT IsComplexN(type)) AND
1166 (type#Address) AND (NOT IsSet(type)) AND
1167 (type#Word) AND (type#Loc) AND (type#Byte) AND (NOT IsWordN(type))
1168 THEN
1169 IF IsSubrange(type)
1170 THEN
1171 GetSubrange(type, maxC, minC) ;
1172 max := Mod2Gcc(maxC) ;
1173 min := Mod2Gcc(minC)
1174 ELSIF IsEnumeration(type)
1175 THEN
1176 GetBaseTypeMinMax(type, minC, maxC) ;
1177 max := Mod2Gcc(maxC) ;
1178 min := Mod2Gcc(minC)
1179 ELSE
1180 max := GetMaxFrom(location, Mod2Gcc(type)) ;
1181 min := GetMinFrom(location, Mod2Gcc(type))
1182 END ;
1183 max := BuildConvert (location, Mod2Gcc(type), max, FALSE) ;
1184 Assert (NOT TreeOverflow (max)) ;
1185 min := BuildConvert (location, Mod2Gcc(type), min, FALSE) ;
1186 Assert (NOT TreeOverflow (min)) ;
1187 RETURN TRUE
1188 ELSE
1189 RETURN FALSE
1190 END
1191 END GetMinMax ;
1192
1193
1194 (*
1195 OutOfRange - returns TRUE if expr lies outside min..max.
1196 *)
1197
1198 PROCEDURE OutOfRange (tokenno: CARDINAL;
1199 min: tree;
1200 expr: CARDINAL;
1201 max: tree;
1202 type: CARDINAL) : BOOLEAN ;
1203 BEGIN
1204 IF TreeOverflow (min)
1205 THEN
1206 WriteString ("overflow detected in min\n"); WriteLn ;
1207 debug_tree (min)
1208 END ;
1209 IF TreeOverflow (max)
1210 THEN
1211 WriteString ("overflow detected in max\n"); WriteLn ;
1212 debug_tree (max)
1213 END ;
1214 IF TreeOverflow (max)
1215 THEN
1216 WriteString ("overflow detected in expr\n"); WriteLn ;
1217 debug_tree (StringToChar (Mod2Gcc (expr), type, expr));
1218 END ;
1219 PushIntegerTree (StringToChar (Mod2Gcc (expr), type, expr)) ;
1220 PushIntegerTree (min) ;
1221 IF Less (tokenno)
1222 THEN
1223 RETURN TRUE
1224 END ;
1225 PushIntegerTree (StringToChar (Mod2Gcc (expr), type, expr)) ;
1226 PushIntegerTree (max) ;
1227 IF Gre (tokenno)
1228 THEN
1229 RETURN TRUE
1230 END ;
1231 RETURN FALSE
1232 END OutOfRange ;
1233
1234
1235 (*
1236 HandlerExists -
1237 *)
1238
1239 PROCEDURE HandlerExists (r: CARDINAL) : BOOLEAN ;
1240 VAR
1241 p: Range ;
1242 BEGIN
1243 p := GetIndice(RangeIndex, r) ;
1244 WITH p^ DO
1245 CASE type OF
1246
1247 assignment : RETURN( ExceptionAssign#NulSym ) |
1248 returnassignment : RETURN( ExceptionReturn#NulSym ) |
1249 subrangeassignment : InternalError ('not expecting this case value') |
1250 inc : RETURN( ExceptionInc#NulSym ) |
1251 dec : RETURN( ExceptionDec#NulSym ) |
1252 incl : RETURN( ExceptionIncl#NulSym ) |
1253 excl : RETURN( ExceptionExcl#NulSym ) |
1254 shift : RETURN( ExceptionShift#NulSym ) |
1255 rotate : RETURN( ExceptionRotate#NulSym ) |
1256 typereturn,
1257 typeassign,
1258 typeparam,
1259 typeexpr,
1260 typeindrx : RETURN( FALSE ) |
1261 paramassign : RETURN( ExceptionParameterBounds#NulSym ) |
1262 staticarraysubscript : RETURN( ExceptionStaticArray#NulSym ) |
1263 dynamicarraysubscript: RETURN( ExceptionDynamicArray#NulSym ) |
1264 forloopbegin : RETURN( ExceptionForLoopBegin#NulSym ) |
1265 forloopto : RETURN( ExceptionForLoopTo#NulSym ) |
1266 forloopend : RETURN( ExceptionForLoopEnd#NulSym ) |
1267 pointernil : RETURN( ExceptionPointerNil#NulSym ) |
1268 noreturn : RETURN( ExceptionNoReturn#NulSym ) |
1269 noelse : RETURN( ExceptionCase#NulSym ) |
1270 casebounds : RETURN( FALSE ) |
1271 wholenonposdiv : RETURN( ExceptionNonPosDiv#NulSym ) |
1272 wholenonposmod : RETURN( ExceptionNonPosMod#NulSym ) |
1273 wholezerodiv : RETURN( ExceptionZeroDiv#NulSym ) |
1274 wholezerorem : RETURN( ExceptionZeroRem#NulSym ) |
1275 none : RETURN( FALSE )
1276
1277 ELSE
1278 InternalError ('enumeration value unknown')
1279 END
1280 END
1281 END HandlerExists ;
1282
1283
1284 (*
1285 FoldAssignment - attempts to fold the range violation checks.
1286 It does not issue errors on type violations as that
1287 is performed by FoldTypeAssign.
1288 *)
1289
1290 PROCEDURE FoldAssignment (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1291 VAR
1292 p : Range ;
1293 min, max: tree ;
1294 BEGIN
1295 p := GetIndice (RangeIndex, r) ;
1296 WITH p^ DO
1297 TryDeclareConstant (exprtok, expr) ;
1298 IF desLowestType # NulSym
1299 THEN
1300 IF AssignmentTypeCompatible (tokenno, "", des, expr, FALSE)
1301 THEN
1302 IF GccKnowsAbout (expr) AND IsConst (expr) AND
1303 GetMinMax (tokenno, desLowestType, min, max)
1304 THEN
1305 IF OutOfRange (tokenno, min, expr, max, desLowestType)
1306 THEN
1307 MetaErrorT2 (tokenNo,
1308 'attempting to assign a value {%2Wa} to a designator {%1a} which will exceed the range of type {%1tad}',
1309 des, expr) ;
1310 PutQuad (q, ErrorOp, NulSym, NulSym, r)
1311 ELSE
1312 SubQuad (q)
1313 END
1314 END
1315 ELSE
1316 (* We do not issue an error if these types are incompatible here
1317 as this is done by FoldTypeAssign. *)
1318 SubQuad (q)
1319 END
1320 END
1321 END
1322 END FoldAssignment ;
1323
1324
1325 (*
1326 CheckCancelled - check to see if the range has been cancelled and if so remove quad.
1327 *)
1328
1329 (*
1330 PROCEDURE CheckCancelled (range: CARDINAL; quad: CARDINAL) ;
1331 BEGIN
1332 IF IsCancelled (range)
1333 THEN
1334 SubQuad (quad)
1335 END
1336 END CheckCancelled ;
1337 *)
1338
1339
1340 (*
1341 IsCancelled - return the cancelled flag associated with range.
1342 *)
1343
1344 PROCEDURE IsCancelled (range: CARDINAL) : BOOLEAN ;
1345 VAR
1346 p: Range ;
1347 BEGIN
1348 p := GetIndice (RangeIndex, range) ;
1349 WITH p^ DO
1350 IF cancelled
1351 THEN
1352 RETURN TRUE
1353 END ;
1354 IF (dependantid # 0) AND IsCancelled (dependantid)
1355 THEN
1356 cancelled := TRUE
1357 END ;
1358 RETURN cancelled
1359 END
1360 END IsCancelled ;
1361
1362
1363 (*
1364 Cancel - set the cancelled flag in range.
1365 *)
1366
1367 PROCEDURE Cancel (range: CARDINAL) ;
1368 VAR
1369 p: Range ;
1370 BEGIN
1371 IF range # 0
1372 THEN
1373 p := GetIndice (RangeIndex, range) ;
1374 WITH p^ DO
1375 IF NOT cancelled
1376 THEN
1377 cancelled := TRUE ;
1378 Cancel (dependantid)
1379 END
1380 END
1381 END
1382 END Cancel ;
1383
1384
1385 (*
1386 FoldParameterAssign -
1387 *)
1388
1389 PROCEDURE FoldParameterAssign (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1390 VAR
1391 p : Range ;
1392 min, max: tree ;
1393 BEGIN
1394 p := GetIndice(RangeIndex, r) ;
1395 WITH p^ DO
1396 TryDeclareConstant (tokenNo, expr) ;
1397 IF desLowestType#NulSym
1398 THEN
1399 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1400 GetMinMax(tokenno, desLowestType, min, max)
1401 THEN
1402 IF OutOfRange(tokenno, min, expr, max, desLowestType)
1403 THEN
1404 (* this is safer to treat as an error, rather than a warning
1405 otherwise the paramater might be widened
1406 (if it is a constant). *)
1407 MetaErrorT3(tokenNo,
1408 'the {%3EN} actual parameter {%2a} will exceed the range of formal parameter type {%1tad}',
1409 des, expr, dimension) ;
1410 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1411 ELSE
1412 SubQuad(q)
1413 END
1414 END
1415 END
1416 END
1417 END FoldParameterAssign ;
1418
1419
1420 (*
1421 FoldReturn - do we know this is reachable, if so generate an error message.
1422 *)
1423
1424 PROCEDURE FoldReturn (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1425 VAR
1426 p : Range ;
1427 min, max: tree ;
1428 BEGIN
1429 p := GetIndice(RangeIndex, r) ;
1430 WITH p^ DO
1431 TryDeclareConstant (tokenNo, expr) ;
1432 IF desLowestType#NulSym
1433 THEN
1434 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1435 GetMinMax(tokenno, desLowestType, min, max)
1436 THEN
1437 IF OutOfRange(tokenno, min, expr, max, desLowestType)
1438 THEN
1439 MetaErrorT2(tokenNo,
1440 'attempting to return {%2Wa} from a procedure function {%1a} which will exceed exceed the range of type {%1tad}',
1441 des, expr) ;
1442 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1443 ELSE
1444 SubQuad(q)
1445 END
1446 END
1447 END
1448 END
1449 END FoldReturn ;
1450
1451
1452 (*
1453 FoldInc -
1454 *)
1455
1456 PROCEDURE FoldInc (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1457 VAR
1458 p : Range ;
1459 t, min, max: tree ;
1460 location : location_t ;
1461 BEGIN
1462 location := TokenToLocation(tokenno) ;
1463 p := GetIndice(RangeIndex, r) ;
1464 WITH p^ DO
1465 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1466 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1467 IF desLowestType#NulSym
1468 THEN
1469 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1470 GetMinMax(tokenno, desLowestType, min, max)
1471 THEN
1472 IF OutOfRange(tokenno, GetIntegerZero(location), expr, max, desLowestType)
1473 THEN
1474 MetaErrorT2(tokenNo,
1475 'operand to INC {%2Wa} exceeds the range of type {%1ts} of the designator {%1a}',
1476 des, expr) ;
1477 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1478 ELSIF GccKnowsAbout(des) AND IsConst(des) AND GccKnowsAbout(desLowestType)
1479 THEN
1480 t := BuildSub(location,
1481 max,
1482 BuildConvert(location, Mod2Gcc(desLowestType), Mod2Gcc(expr), FALSE),
1483 FALSE) ;
1484 PushIntegerTree(Mod2Gcc(des)) ;
1485 PushIntegerTree(t) ;
1486 IF Gre(tokenNo)
1487 THEN
1488 MetaErrorT1(tokenNo,
1489 'the designator to INC {%1Wa} will exceed the range of type {%1ts}',
1490 des) ;
1491 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1492 ELSE
1493 (* range check is unnecessary *)
1494 SubQuad(q)
1495 END
1496 END
1497 END
1498 END
1499 END
1500 END FoldInc ;
1501
1502
1503 (*
1504 FoldDec -
1505 *)
1506
1507 PROCEDURE FoldDec (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1508 VAR
1509 p : Range ;
1510 t, min, max: tree ;
1511 location : location_t ;
1512 BEGIN
1513 location := TokenToLocation(tokenno) ;
1514 p := GetIndice(RangeIndex, r) ;
1515 WITH p^ DO
1516 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1517 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1518 IF desLowestType#NulSym
1519 THEN
1520 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1521 GetMinMax(tokenno, desLowestType, min, max)
1522 THEN
1523 IF OutOfRange(tokenno, GetIntegerZero(location), expr, max, desLowestType)
1524 THEN
1525 MetaErrorT2(tokenNo,
1526 'operand to DEC {%2Wa} exceeds the range of type {%1ts} of the designator {%1a}',
1527 des, expr) ;
1528 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1529 ELSIF GccKnowsAbout(des) AND IsConst(des) AND GccKnowsAbout(desLowestType)
1530 THEN
1531 t := BuildSub(location,
1532 BuildConvert(location, Mod2Gcc(desLowestType), Mod2Gcc(expr), FALSE),
1533 min,
1534 FALSE) ;
1535 PushIntegerTree(Mod2Gcc(des)) ;
1536 PushIntegerTree(t) ;
1537 IF Less(tokenNo)
1538 THEN
1539 MetaErrorT1(tokenNo,
1540 'the designator to DEC {%1Wa} will exceed the range of type {%1ts}',
1541 des) ;
1542 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1543 ELSE
1544 (* range check is unnecessary *)
1545 SubQuad(q)
1546 END
1547 END
1548 END
1549 END
1550 END
1551 END FoldDec ;
1552
1553
1554 (*
1555 CheckSetAndBit - returns TRUE if des is a set type and expr is compatible with des.
1556 *)
1557
1558 PROCEDURE CheckSetAndBit (tokenno: CARDINAL;
1559 des, expr: CARDINAL;
1560 name: ARRAY OF CHAR) : BOOLEAN ;
1561 VAR
1562 s: String ;
1563 BEGIN
1564 IF IsSet(des)
1565 THEN
1566 IF IsExpressionCompatible(GetType(des), GetType(expr))
1567 THEN
1568 RETURN( TRUE )
1569 ELSE
1570 s := ConCat(ConCat(InitString('operands to '),
1571 Mark(InitString(name))),
1572 Mark(InitString(' {%1Etsd:{%2tsd:{%1tsd} and {%2tsd}}} are incompatible'))) ;
1573 MetaErrorStringT2(tokenno, s, des, expr) ;
1574 FlushErrors
1575 END
1576 ELSE
1577 s := ConCat(ConCat(InitString('first operand to '),
1578 Mark(InitString(name))),
1579 Mark(InitString(' is not a set {%1Etasd}'))) ;
1580 MetaErrorStringT1(tokenno, s, des) ;
1581 FlushErrors
1582 END ;
1583 RETURN( FALSE )
1584 END CheckSetAndBit ;
1585
1586
1587 (*
1588 CheckSet - returns TRUE if des is a set type and expr is compatible with INTEGER.
1589 *)
1590
1591 PROCEDURE CheckSet (tokenno: CARDINAL;
1592 des, expr: CARDINAL;
1593 name: ARRAY OF CHAR) : BOOLEAN ;
1594 VAR
1595 s: String ;
1596 BEGIN
1597 IF IsSet(des)
1598 THEN
1599 IF IsParameterCompatible(Integer, GetType(expr))
1600 THEN
1601 RETURN( TRUE )
1602 ELSE
1603 s := ConCat(ConCat(InitString('operands to '),
1604 Mark(InitString(name))),
1605 Mark(InitString(' {%1Etsd:{%2tsd:{%1tsd} and {%2tsd}}} are incompatible'))) ;
1606 MetaErrorStringT2(tokenno, s, des, expr) ;
1607 FlushErrors
1608 END
1609 ELSE
1610 s := ConCat(ConCat(InitString('first operand to '),
1611 Mark(InitString(name))),
1612 Mark(InitString(' is not a set {%1Etasd}'))) ;
1613 MetaErrorStringT1(tokenno, s, des) ;
1614 FlushErrors
1615 END ;
1616 RETURN( FALSE )
1617 END CheckSet ;
1618
1619
1620 (*
1621 FoldIncl - folds an INCL statement if the operands are constant.
1622 *)
1623
1624 PROCEDURE FoldIncl (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1625 VAR
1626 p : Range ;
1627 min, max: tree ;
1628 BEGIN
1629 p := GetIndice(RangeIndex, r) ;
1630 WITH p^ DO
1631 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1632 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1633 desLowestType := SkipType(GetType(des)) ;
1634 IF desLowestType#NulSym
1635 THEN
1636 IF CheckSetAndBit(tokenno, desLowestType, expr, "INCL")
1637 THEN
1638 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1639 GetMinMax(tokenno, desLowestType, min, max)
1640 THEN
1641 IF OutOfRange(tokenno, min, expr, max, desLowestType)
1642 THEN
1643 MetaErrorT2(tokenNo,
1644 'operand to INCL {%2Wa} exceeds the range of type {%1tasa}',
1645 des, expr) ;
1646 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1647 ELSE
1648 (* range check is unnecessary *)
1649 SubQuad(q)
1650 END
1651 END
1652 END
1653 END
1654 END
1655 END FoldIncl ;
1656
1657
1658 (*
1659 FoldExcl - folds an EXCL statement if the operands are constant.
1660 *)
1661
1662 PROCEDURE FoldExcl (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1663 VAR
1664 p : Range ;
1665 min, max: tree ;
1666 BEGIN
1667 p := GetIndice(RangeIndex, r) ;
1668 WITH p^ DO
1669 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1670 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1671 desLowestType := SkipType(GetType(des)) ;
1672 IF desLowestType#NulSym
1673 THEN
1674 IF CheckSetAndBit(tokenno, desLowestType, expr, "EXCL")
1675 THEN
1676 IF GccKnowsAbout(expr) AND IsConst(expr) AND
1677 GetMinMax(tokenno, desLowestType, min, max)
1678 THEN
1679 IF OutOfRange(tokenno, min, expr, max, desLowestType)
1680 THEN
1681 MetaErrorT2(tokenNo,
1682 'operand to EXCL {%2Wa} exceeds the range of type {%1tasa}',
1683 des, expr) ;
1684 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1685 ELSE
1686 (* range check is unnecessary *)
1687 SubQuad(q)
1688 END
1689 END
1690 END
1691 END
1692 END
1693 END FoldExcl ;
1694
1695
1696 (*
1697 FoldShift - folds an SHIFT test statement if the operands are constant.
1698 *)
1699
1700 PROCEDURE FoldShift (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1701 VAR
1702 ofType : CARDINAL ;
1703 p : Range ;
1704 shiftMin,
1705 shiftMax,
1706 min, max: tree ;
1707 location : location_t ;
1708 BEGIN
1709 location := TokenToLocation(tokenno) ;
1710 p := GetIndice(RangeIndex, r) ;
1711 WITH p^ DO
1712 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1713 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1714 desLowestType := SkipType(GetType(des)) ;
1715 IF desLowestType#NulSym
1716 THEN
1717 IF CheckSet(tokenno, desLowestType, expr, "SHIFT")
1718 THEN
1719 ofType := SkipType(GetType(desLowestType)) ;
1720 IF GccKnowsAbout(ofType) AND
1721 GccKnowsAbout(expr) AND IsConst(expr) AND
1722 GetMinMax(tokenno, ofType, min, max)
1723 THEN
1724 min := BuildConvert(location, GetIntegerType(), min, FALSE) ;
1725 max := BuildConvert(location, GetIntegerType(), max, FALSE) ;
1726 shiftMax := BuildAdd(location, BuildSub(location, max, min, FALSE),
1727 GetIntegerOne(location),
1728 FALSE) ;
1729 shiftMin := BuildNegate(location, shiftMax, FALSE) ;
1730 IF OutOfRange(tokenno, shiftMin, expr, shiftMax, desLowestType)
1731 THEN
1732 MetaErrorT2(tokenNo,
1733 'operand to SHIFT {%2Wa} exceeds the range of type {%1tasa}',
1734 des, expr) ;
1735 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1736 ELSE
1737 (* range check is unnecessary *)
1738 SubQuad(q)
1739 END
1740 END
1741 END
1742 END
1743 END
1744 END FoldShift ;
1745
1746
1747 (*
1748 FoldRotate - folds a ROTATE test statement if the operands are constant.
1749 *)
1750
1751 PROCEDURE FoldRotate (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
1752 VAR
1753 ofType : CARDINAL ;
1754 p : Range ;
1755 rotateMin,
1756 rotateMax,
1757 min, max : tree ;
1758 location : location_t ;
1759 BEGIN
1760 location := TokenToLocation(tokenno) ;
1761 p := GetIndice(RangeIndex, r) ;
1762 WITH p^ DO
1763 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
1764 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
1765 desLowestType := SkipType(GetType(des)) ;
1766 IF desLowestType#NulSym
1767 THEN
1768 IF CheckSet(tokenno, desLowestType, expr, "ROTATE")
1769 THEN
1770 ofType := SkipType(GetType(desLowestType)) ;
1771 IF GccKnowsAbout(ofType) AND
1772 GccKnowsAbout(expr) AND IsConst(expr) AND
1773 GetMinMax(tokenno, ofType, min, max)
1774 THEN
1775 min := BuildConvert(location, GetIntegerType(), min, FALSE) ;
1776 max := BuildConvert(location, GetIntegerType(), max, FALSE) ;
1777 rotateMax := BuildAdd(location,
1778 BuildSub(location, max, min, FALSE),
1779 GetIntegerOne(location),
1780 FALSE) ;
1781 rotateMin := BuildNegate(location, rotateMax, FALSE) ;
1782 IF OutOfRange(tokenno, rotateMin, expr, rotateMax, desLowestType)
1783 THEN
1784 MetaErrorT2(tokenNo,
1785 'operand to ROTATE {%2Wa} exceeds the range of type {%1tasa}',
1786 des, expr) ;
1787 PutQuad(q, ErrorOp, NulSym, NulSym, r)
1788 ELSE
1789 (* range check is unnecessary *)
1790 SubQuad(q)
1791 END
1792 END
1793 END
1794 END
1795 END
1796 END FoldRotate ;
1797
1798
1799 (*
1800 FoldTypeReturnFunc - checks to see that val can be returned from func.
1801 *)
1802
1803 PROCEDURE FoldTypeReturnFunc (q: CARDINAL; tokenNo: CARDINAL; func, val: CARDINAL; r: CARDINAL) ;
1804 VAR
1805 valType,
1806 returnType: CARDINAL ;
1807 BEGIN
1808 returnType := GetType (func) ;
1809 IF returnType = NulSym
1810 THEN
1811 IF NOT reportedError (r)
1812 THEN
1813 MetaErrorsT2 (tokenNo,
1814 'procedure {%1Da} is not a procedure function',
1815 '{%2ad} cannot be returned from {%1Da}',
1816 func, val) ;
1817 SubQuad(q)
1818 END
1819 ELSE
1820 valType := val ;
1821 IF IsVar (val) AND (GetMode (val) = LeftValue)
1822 THEN
1823 valType := GetType (val)
1824 END ;
1825 IF AssignmentTypeCompatible (tokenNo, "", returnType, valType, FALSE)
1826 THEN
1827 SubQuad (q)
1828 ELSE
1829 IF NOT reportedError (r)
1830 THEN
1831 MetaErrorsT2 (tokenNo,
1832 'the return type {%1Etad} used in procedure {%1Da}',
1833 'is incompatible with the returned expression {%1ad}}',
1834 func, val) ;
1835 setReported (r) ;
1836 FlushErrors
1837 END
1838 END
1839 END
1840 END FoldTypeReturnFunc ;
1841
1842
1843 (*
1844 FoldTypeAssign -
1845 *)
1846
1847 PROCEDURE FoldTypeAssign (q: CARDINAL; tokenNo: CARDINAL; des, expr: CARDINAL; r: CARDINAL) ;
1848 BEGIN
1849 IF NOT reportedError (r)
1850 THEN
1851 IF AssignmentTypeCompatible (tokenNo,
1852 'assignment designator {%1Ea} {%1ta:of type {%1ta}}' +
1853 ' cannot be assigned with' +
1854 ' {%2ad: a {%2td} {%2ad}}{!%2ad: {%2ad} of type {%2tad}}',
1855 des, expr, TRUE)
1856 THEN
1857 SubQuad (q)
1858 ELSE
1859 setReported (r) ;
1860 FlushErrors
1861 END
1862 END
1863 END FoldTypeAssign ;
1864
1865
1866 (*
1867 FoldTypeIndrX - check to see that des = *expr is type compatible.
1868 *)
1869
1870 PROCEDURE FoldTypeIndrX (q: CARDINAL; tokenNo: CARDINAL; des, expr: CARDINAL; r: CARDINAL) ;
1871 VAR
1872 desType,
1873 exprType: CARDINAL ;
1874 BEGIN
1875 (* Need to skip over a variable or temporary in des and expr so
1876 long as expr is not a procedure. In the case of des = *expr,
1877 both expr and des will be variables due to the property of
1878 indirection. *)
1879 desType := GetType (des) ;
1880 IF IsProcedure (expr)
1881 THEN
1882 (* Must not GetType for a procedure as it gives the return type. *)
1883 exprType := expr
1884 ELSE
1885 exprType := GetType (expr)
1886 END ;
1887 IF AssignmentTypeCompatible (tokenNo, "", GetType (des), GetType (expr), FALSE)
1888 THEN
1889 SubQuad(q)
1890 ELSE
1891 IF NOT reportedError (r)
1892 THEN
1893 IF IsProcedure (des)
1894 THEN
1895 MetaErrorsT2 (tokenNo,
1896 'the return type {%1Etad} declared in procedure {%1Da}',
1897 'is incompatible with the returned expression {%2ad}}',
1898 des, expr) ;
1899 ELSE
1900 MetaErrorT3 (tokenNo,
1901 'assignment designator {%1Ea} {%1ta:of type {%1ta}}' +
1902 ' {%1d:is a {%1d}} and expression {%2a} {%3ad:of type' +
1903 ' {%3ad}} are incompatible',
1904 des, expr, exprType)
1905 END ;
1906 setReported (r) ;
1907 FlushErrors
1908 END
1909 END
1910 END FoldTypeIndrX ;
1911
1912
1913 (*
1914 FoldTypeParam - performs a parameter check between actual and formal.
1915 The quad is removed if the check succeeds.
1916 *)
1917
1918 PROCEDURE FoldTypeParam (q: CARDINAL; tokenNo: CARDINAL;
1919 formal, actual, procedure: CARDINAL;
1920 paramNo: CARDINAL;
1921 depRangeId: CARDINAL) ;
1922 VAR
1923 compatible: BOOLEAN ;
1924 BEGIN
1925 compatible := FALSE ;
1926 IF IsVarParamAny (procedure, paramNo)
1927 THEN
1928 (* Expression type compatibility rules for pass by reference parameters. *)
1929 compatible := ParameterTypeCompatible (tokenNo,
1930 '{%4EN} parameter failure due to expression incompatibility between actual parameter {%3ad} and the {%4N} formal {%2ad} parameter in procedure {%1ad}',
1931 procedure, formal, actual, paramNo, TRUE)
1932 ELSIF GetPIM ()
1933 THEN
1934 (* Assignment type compatibility rules for pass by value PIM parameters. *)
1935 compatible := ParameterTypeCompatible (tokenNo,
1936 '{%4EN} parameter failure due to assignment incompatibility between actual parameter {%3ad} and the {%4N} formal {%2ad} parameter in procedure {%1ad}',
1937 procedure, formal, actual, paramNo, FALSE)
1938 ELSE
1939 compatible := ParameterTypeCompatible (tokenNo,
1940 '{%4EN} parameter failure due to parameter incompatibility between actual parameter {%3ad} and the {%4N} formal {%2ad} parameter in procedure {%1ad}',
1941 procedure, formal, actual, paramNo, FALSE)
1942 END ;
1943 IF compatible
1944 THEN
1945 SubQuad(q)
1946 ELSE
1947 Cancel (depRangeId)
1948 END
1949 END FoldTypeParam ;
1950
1951
1952 (*
1953 FoldTypeExpr -
1954 *)
1955
1956 PROCEDURE FoldTypeExpr (q: CARDINAL; tokenNo: CARDINAL; left, right: CARDINAL; strict, isin: BOOLEAN; r: CARDINAL) ;
1957 BEGIN
1958 IF (left # NulSym) AND (right # NulSym) AND (NOT reportedError (r))
1959 THEN
1960 IF ExpressionTypeCompatible (tokenNo,
1961 'expression of type {%1Etad} is incompatible with type {%2tad}',
1962 left, right, strict, isin)
1963 THEN
1964 SubQuad(q)
1965 ELSE
1966 setReported (r)
1967 END
1968 END
1969 END FoldTypeExpr ;
1970
1971
1972 (*
1973 CodeTypeAssign -
1974 *)
1975
1976 PROCEDURE CodeTypeAssign (tokenNo: CARDINAL; des, expr: CARDINAL; r: CARDINAL) ;
1977 BEGIN
1978 IF NOT AssignmentTypeCompatible (tokenNo, "", des, expr, FALSE)
1979 THEN
1980 IF NOT reportedError (r)
1981 THEN
1982 MetaErrorT2 (tokenNo,
1983 'assignment designator {%1Ea} {%1ta:of type {%1ta}} {%1d:is a {%1d}} and expression {%2a} {%2tad:of type {%2tad}} are incompatible',
1984 des, expr)
1985 END ;
1986 setReported (r)
1987 END
1988 END CodeTypeAssign ;
1989
1990
1991 (*
1992 CodeTypeReturnFunc -
1993 *)
1994
1995 PROCEDURE CodeTypeReturnFunc (tokenNo: CARDINAL; func, val: CARDINAL; r: CARDINAL) ;
1996 VAR
1997 valType,
1998 returnType: CARDINAL ;
1999 BEGIN
2000 returnType := GetType (func) ;
2001 IF returnType = NulSym
2002 THEN
2003 IF NOT reportedError (r)
2004 THEN
2005 MetaErrorsT2 (tokenNo,
2006 'procedure {%1Da} is not a procedure function',
2007 '{%2ad} cannot be returned from {%1Da}',
2008 func, val) ;
2009 END
2010 ELSE
2011 valType := val ;
2012 IF IsVar (val) AND (GetMode (val) = LeftValue)
2013 THEN
2014 valType := GetType (val)
2015 END ;
2016 IF NOT AssignmentTypeCompatible (tokenNo, "", returnType, valType, FALSE)
2017 THEN
2018 IF NOT reportedError (r)
2019 THEN
2020 MetaErrorsT2 (tokenNo,
2021 'the return type {%1Etad} used in procedure function {%1Da}',
2022 'is incompatible with the returned expression {%2EUa} {%2tad:of type {%2tad}}',
2023 func, val)
2024 END
2025 END
2026 END
2027 END CodeTypeReturnFunc ;
2028
2029
2030 (*
2031 CodeTypeIndrX - checks that des = *expr is type compatible and generates an error if they
2032 are not compatible. It skips over the LValue type so that to allow
2033 the error messages to pick up the source variable name rather than
2034 a temporary name or vague name 'expression'.
2035 *)
2036
2037 PROCEDURE CodeTypeIndrX (tokenNo: CARDINAL; des, expr: CARDINAL; r: CARDINAL) ;
2038 BEGIN
2039 IF NOT AssignmentTypeCompatible (tokenNo, "", GetType (des), GetType (expr), FALSE)
2040 THEN
2041 IF NOT reportedError (r)
2042 THEN
2043 IF IsProcedure (des)
2044 THEN
2045 MetaErrorsT2 (tokenNo,
2046 'the return type {%1Etad} declared in procedure {%1Da}',
2047 'is incompatible with the returned expression {%2EUa} {%2tad:of type {%2tad}}',
2048 des, expr) ;
2049 ELSE
2050 MetaErrorT2 (tokenNo,
2051 'assignment designator {%1Ea} {%1ta:of type {%1ta}}' +
2052 ' {%1d:is a {%1d}} and expression {%2a}' +
2053 ' {%2tad:of type {%2tad}} are incompatible',
2054 des, expr)
2055 END ;
2056 setReported (r)
2057 END
2058 (* FlushErrors *)
2059 END
2060 END CodeTypeIndrX ;
2061
2062
2063 (*
2064 CodeTypeParam -
2065 *)
2066
2067 PROCEDURE CodeTypeParam (tokenNo: CARDINAL; formal, actual, procedure: CARDINAL; paramNo: CARDINAL) ;
2068 BEGIN
2069 IF NOT ParameterTypeCompatible (tokenNo,
2070 '{%4EN} type failure between actual {%3ad} and the formal {%2ad}',
2071 procedure, formal, actual, paramNo, IsVarParamAny (procedure, paramNo))
2072 THEN
2073 END
2074 END CodeTypeParam ;
2075
2076
2077 (*
2078 CodeTypeExpr -
2079 *)
2080
2081 PROCEDURE CodeTypeExpr (tokenNo: CARDINAL; left, right: CARDINAL; strict, isin: BOOLEAN; r: CARDINAL) ;
2082 BEGIN
2083 IF NOT reportedError (r)
2084 THEN
2085 IF ExpressionTypeCompatible (tokenNo,
2086 'expression of type {%1Etad} is incompatible with type {%2tad}',
2087 left, right, strict, isin)
2088 THEN
2089 setReported (r)
2090 END
2091 END
2092 END CodeTypeExpr ;
2093
2094
2095 (*
2096 FoldTypeCheck - folds a type check. This is a no-op and it used
2097 for checking types which are resolved post pass 3.
2098 *)
2099
2100 PROCEDURE FoldTypeCheck (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2101 VAR
2102 p: Range ;
2103 BEGIN
2104 p := GetIndice(RangeIndex, r) ;
2105 WITH p^ DO
2106 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
2107 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2108 (* TryDeclareConstructor(q, expr) ; *)
2109 IF (GccKnowsAbout(des) OR (IsParameter(des) AND GccKnowsAbout(GetType(des)))) AND
2110 GccKnowsAbout(expr)
2111 THEN
2112 CASE type OF
2113
2114 typeassign: FoldTypeAssign (q, tokenNo, des, expr, r) |
2115 typeparam : FoldTypeParam (q, tokenNo, des, expr, procedure, paramNo, r) |
2116 typeexpr : FoldTypeExpr (q, tokenNo, des, expr, strict, isin, r) |
2117 typeindrx : FoldTypeIndrX (q, tokenNo, des, expr, r) |
2118 typereturn: FoldTypeReturnFunc (q, tokenNo, des, expr, r)
2119
2120 ELSE
2121 InternalError ('not expecting to reach this point')
2122 END
2123 END
2124 END
2125 END FoldTypeCheck ;
2126
2127
2128 (*
2129 CodeTypeCheck - folds a type check. This is a no-op and it used
2130 for checking types which are resolved post pass 3.
2131 It does assume that both, des, and, expr, have been
2132 resolved at this point.
2133 *)
2134
2135 PROCEDURE CodeTypeCheck (tokenno: CARDINAL; r: CARDINAL) ;
2136 VAR
2137 p: Range ;
2138 BEGIN
2139 p := GetIndice(RangeIndex, r) ;
2140 WITH p^ DO
2141 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
2142 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2143 (* TryDeclareConstructor(0, expr) ; *)
2144 IF (GccKnowsAbout(des) OR (IsParameter(des) AND GccKnowsAbout(GetType(des)))) AND
2145 GccKnowsAbout(expr)
2146 THEN
2147 CASE type OF
2148
2149 typeassign: CodeTypeAssign (tokenNo, des, expr, r) |
2150 typeparam : CodeTypeParam (tokenNo, des, expr, procedure, paramNo) |
2151 typeexpr : CodeTypeExpr (tokenNo, des, expr, strict, isin, r) |
2152 typeindrx : CodeTypeIndrX (tokenNo, des, expr, r) |
2153 typereturn: CodeTypeReturnFunc (tokenNo, des, expr, r)
2154
2155 ELSE
2156 InternalError ('not expecting to reach this point')
2157 END
2158 ELSE
2159 InternalError ('expecting des and expr to be resolved')
2160 END
2161 END
2162 END CodeTypeCheck ;
2163
2164
2165 (*
2166 ForLoopBeginTypeCompatible - check for designator assignment compatibility with
2167 expr1 and designator expression compatibility with expr2.
2168 FOR des := expr1 TO expr2 BY byconst DO
2169 END
2170 It generates composite tokens if the tokens are on
2171 the same source line.
2172 *)
2173
2174 PROCEDURE ForLoopBeginTypeCompatible (p: Range) : BOOLEAN ;
2175 VAR
2176 combinedtok: CARDINAL ;
2177 success : BOOLEAN ;
2178 BEGIN
2179 success := TRUE ;
2180 WITH p^ DO
2181 combinedtok := MakeVirtual2Tok (destok, exprtok) ;
2182 IF NOT AssignmentTypeCompatible (combinedtok, "", des, expr, TRUE)
2183 THEN
2184 MetaErrorT2 (combinedtok,
2185 'type incompatibility between {%1Et} and {%2t} detected during the assignment of the designator {%1a} to the first expression {%2a} in the {%kFOR} loop',
2186 des, expr) ;
2187 success := FALSE
2188 END ;
2189 combinedtok := MakeVirtual2Tok (destok, expr2tok) ;
2190 IF NOT ExpressionTypeCompatible (combinedtok, "", des, expr2, TRUE, FALSE)
2191 THEN
2192 MetaErrorT2 (combinedtok,
2193 'type expression incompatibility between {%1Et} and {%2t} detected when comparing the designator {%1a} against the second expression {%2a} in the {%kFOR} loop',
2194 des, expr2) ;
2195 success := FALSE
2196 END ;
2197 combinedtok := MakeVirtual2Tok (destok, byconsttok) ;
2198 IF NOT ExpressionTypeCompatible (combinedtok, "", des, byconst, TRUE, FALSE)
2199 THEN
2200 MetaErrorT2 (combinedtok,
2201 'type expression incompatibility between {%1Et} and {%2t} detected between the designator {%1a} and the {%kBY} constant expression {%2a} in the {%kFOR} loop',
2202 des, byconst) ;
2203 success := FALSE
2204 END ;
2205 IF (NOT success) AND (incrementquad # 0)
2206 THEN
2207 (* Avoid a subsequent generic type check error. *)
2208 SubQuad (incrementquad)
2209 END
2210 END ;
2211 RETURN success
2212 END ForLoopBeginTypeCompatible ;
2213
2214
2215 (*
2216 FoldForLoopBegin -
2217 *)
2218
2219 PROCEDURE FoldForLoopBegin (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2220 VAR
2221 p : Range ;
2222 min, max: tree ;
2223 BEGIN
2224 p := GetIndice(RangeIndex, r) ;
2225 WITH p^ DO
2226 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2227 IF desLowestType#NulSym
2228 THEN
2229 IF GccKnowsAbout(expr) AND IsConst(expr) AND
2230 GetMinMax(tokenno, desLowestType, min, max)
2231 THEN
2232 IF NOT ForLoopBeginTypeCompatible (p)
2233 THEN
2234 SubQuad (q)
2235 ELSIF OutOfRange (tokenno, min, expr, max, desLowestType)
2236 THEN
2237 MetaErrorT2 (tokenNo,
2238 'attempting to assign a value {%2Wa} to a FOR loop designator {%1a} which will exceed the range of type {%1tad}',
2239 des, expr) ;
2240 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2241 ELSE
2242 SubQuad (q)
2243 END
2244 END
2245 END
2246 END
2247 END FoldForLoopBegin ;
2248
2249
2250 (*
2251 FoldForLoopTo -
2252 *)
2253
2254 PROCEDURE FoldForLoopTo (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2255 VAR
2256 p : Range ;
2257 min, max: tree ;
2258 BEGIN
2259 p := GetIndice(RangeIndex, r) ;
2260 WITH p^ DO
2261 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2262 IF desLowestType#NulSym
2263 THEN
2264 IF GccKnowsAbout(expr) AND IsConst(expr) AND
2265 GetMinMax(tokenno, desLowestType, min, max)
2266 THEN
2267 IF OutOfRange(tokenno, min, expr, max, desLowestType)
2268 THEN
2269 MetaErrorT2(tokenNo,
2270 'final value in FOR loop will exceed type range {%1Wtasa} of designator {%2a}',
2271 des, expr) ;
2272 PutQuad(q, ErrorOp, NulSym, NulSym, r)
2273 ELSE
2274 SubQuad(q)
2275 END
2276 END
2277 END
2278 END
2279 END FoldForLoopTo ;
2280
2281
2282 (*
2283 FoldStaticArraySubscript -
2284 *)
2285
2286 PROCEDURE FoldStaticArraySubscript (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2287 VAR
2288 p : Range ;
2289 min, max: tree ;
2290 BEGIN
2291 p := GetIndice (RangeIndex, r) ;
2292 WITH p^ DO
2293 TryDeclareConstant (tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
2294 TryDeclareConstant (tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2295 IF desLowestType#NulSym
2296 THEN
2297 IF GccKnowsAbout (expr) AND IsConst (expr) AND
2298 GetMinMax (tokenno, desLowestType, min, max)
2299 THEN
2300 IF OutOfRange (tokenno, min, expr, max, desLowestType)
2301 THEN
2302 MetaErrorT3 (tokenNo,
2303 'index {%2Wa} out of range found while attempting to access an element of a static array {%1a} in the {%3N} array subscript',
2304 des, expr, dimension) ;
2305 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2306 ELSE
2307 (* range check is unnecessary *)
2308 SubQuad (q)
2309 END
2310 END
2311 END
2312 END
2313 END FoldStaticArraySubscript ;
2314
2315
2316 (*
2317 FoldDynamicArraySubscript -
2318 *)
2319
2320 PROCEDURE FoldDynamicArraySubscript (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2321 VAR
2322 p : Range ;
2323 location: location_t ;
2324 BEGIN
2325 location := TokenToLocation(tokenno) ;
2326 p := GetIndice(RangeIndex, r) ;
2327 WITH p^ DO
2328 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2329 IF desLowestType#NulSym
2330 THEN
2331 IF GccKnowsAbout(expr) AND IsConst(expr)
2332 THEN
2333 IF IsGreater(GetIntegerZero(location), BuildConvert(location, GetIntegerType(), Mod2Gcc(expr), FALSE))
2334 THEN
2335 MetaErrorT3(tokenNo,
2336 'index {%2Wa} out of range found while attempting to access an element of a dynamic array {%1a} in the {%3N} array subscript',
2337 des, expr, dimension) ;
2338 PutQuad(q, ErrorOp, NulSym, NulSym, r)
2339 ELSE
2340 (* cannot fold high bounds, so leave that for the runtime *)
2341 END
2342 END
2343 END
2344 END
2345 END FoldDynamicArraySubscript ;
2346
2347
2348 (*
2349 FoldCaseBounds -
2350 *)
2351
2352 PROCEDURE FoldCaseBounds (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2353 VAR
2354 p : Range ;
2355 errorGenerated: BOOLEAN ;
2356 BEGIN
2357 p := GetIndice(RangeIndex, r) ;
2358 WITH p^ DO
2359 IF CaseBoundsResolved(tokenno, caseList)
2360 THEN
2361 errorGenerated := FALSE ;
2362 IF TypeCaseBounds (caseList)
2363 THEN
2364 (* nothing to do *)
2365 END ;
2366 IF OverlappingCaseBounds(caseList)
2367 THEN
2368 PutQuad(q, ErrorOp, NulSym, NulSym, r) ;
2369 errorGenerated := TRUE
2370 END ;
2371 IF VariantValueChecking AND MissingCaseBounds(tokenno, caseList)
2372 THEN
2373 IF NOT errorGenerated
2374 THEN
2375 PutQuad(q, ErrorOp, NulSym, NulSym, r) ;
2376 errorGenerated := TRUE
2377 END
2378 END ;
2379 IF CaseEnumChecking AND MissingCaseStatementBounds (tokenno, caseList)
2380 THEN
2381 IF NOT errorGenerated
2382 THEN
2383 PutQuad(q, ErrorOp, NulSym, NulSym, r) ;
2384 errorGenerated := TRUE
2385 END
2386 END ;
2387 IF NOT errorGenerated
2388 THEN
2389 SubQuad(q)
2390 END
2391 END
2392 END
2393 END FoldCaseBounds ;
2394
2395
2396 (*
2397 CodeCaseBounds - attempts to resolve whether the case bounds are legal.
2398 This should resolve at compile time as all case bounds
2399 must be constants. We introduce a CodeCaseBounds as it
2400 might be possible that constants have just been declared
2401 during the code generation of this function.
2402 *)
2403
2404 PROCEDURE CodeCaseBounds (tokenno: CARDINAL; caseList: CARDINAL) ;
2405 BEGIN
2406 IF CaseBoundsResolved (tokenno, caseList)
2407 THEN
2408 IF TypeCaseBounds (caseList)
2409 THEN
2410 (* nothing to do *)
2411 END ;
2412 IF OverlappingCaseBounds (caseList)
2413 THEN
2414 (* nothing to do *)
2415 END ;
2416 IF MissingCaseBounds (tokenno, caseList)
2417 THEN
2418 (* nothing to do *)
2419 END ;
2420 IF CaseEnumChecking AND MissingCaseStatementBounds (tokenno, caseList)
2421 THEN
2422 (* nothing to do *)
2423 END
2424 ELSE
2425 MetaErrorT0 (tokenno, '{%E}the CASE statement ranges must be constants')
2426 END
2427 END CodeCaseBounds ;
2428
2429
2430 (*
2431 MakeAndDeclareConstLit - creates a constant of value and declares it to GCC.
2432 *)
2433
2434 PROCEDURE MakeAndDeclareConstLit (tokenno: CARDINAL; value: Name; type: CARDINAL) : CARDINAL ;
2435 VAR
2436 constant: CARDINAL ;
2437 BEGIN
2438 constant := MakeConstLit (tokenno, value, type) ;
2439 TryDeclareConstant (tokenno, constant) ; (* use quad tokenno, rather than the range tokenNo *)
2440 Assert (GccKnowsAbout (constant)) ;
2441 RETURN constant
2442 END MakeAndDeclareConstLit ;
2443
2444
2445 (*
2446 FoldNonPosDiv - attempts to fold the bound checking for a divide expression.
2447 *)
2448
2449 PROCEDURE FoldNonPosDiv (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2450 VAR
2451 p : Range ;
2452 zero: CARDINAL ;
2453 BEGIN
2454 p := GetIndice(RangeIndex, r) ;
2455 WITH p^ DO
2456 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2457 IF GccKnowsAbout(expr) AND IsConst(expr)
2458 THEN
2459 zero := MakeAndDeclareConstLit (tokenno, MakeKey ('0'), ZType) ;
2460 IF IsGreaterOrEqualConversion (TokenToLocation (tokenno), zero, des, expr)
2461 THEN
2462 MetaErrorT2 (tokenNo,
2463 'the divisor {%2Wa} in this division expression is less than or equal to zero, this will cause an exception to be raised before the result is assigned to the designator {%1a}',
2464 des, expr) ;
2465 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2466 END
2467 END
2468 END
2469 END FoldNonPosDiv ;
2470
2471
2472 (*
2473 FoldNonPosMod - attempts to fold the bound checking for a modulus expression.
2474 *)
2475
2476 PROCEDURE FoldNonPosMod (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2477 VAR
2478 p : Range ;
2479 zero: CARDINAL ;
2480 BEGIN
2481 p := GetIndice(RangeIndex, r) ;
2482 WITH p^ DO
2483 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2484 IF GccKnowsAbout(expr) AND IsConst(expr)
2485 THEN
2486 zero := MakeAndDeclareConstLit (tokenno, MakeKey('0'), ZType) ;
2487 IF IsGreaterOrEqualConversion (TokenToLocation(tokenno), zero, des, expr)
2488 THEN
2489 MetaErrorT2 (tokenNo,
2490 'the divisor {%2Wa} in this modulus expression is less than or equal to zero, this will cause an exception to be raised before the result is assigned to the designator {%1a}',
2491 des, expr) ;
2492 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2493 END
2494 END
2495 END
2496 END FoldNonPosMod ;
2497
2498
2499 (*
2500 FoldZeroDiv -
2501 *)
2502
2503 PROCEDURE FoldZeroDiv (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2504 VAR
2505 p : Range ;
2506 zero: CARDINAL ;
2507 BEGIN
2508 p := GetIndice(RangeIndex, r) ;
2509 WITH p^ DO
2510 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2511 IF GccKnowsAbout(expr) AND IsConst(expr)
2512 THEN
2513 zero := MakeAndDeclareConstLit (tokenno, MakeKey ('0'), ZType) ;
2514 IF IsEqualConversion (zero, des, expr)
2515 THEN
2516 MetaErrorT2 (tokenNo,
2517 'the divisor {%2Wa} in this division expression is equal to zero, this will cause an exception to be raised before the result is assigned to the designator {%1a}',
2518 des, expr) ;
2519 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2520 END
2521 END
2522 END
2523 END FoldZeroDiv ;
2524
2525
2526 (*
2527 FoldZeroRem -
2528 *)
2529
2530 PROCEDURE FoldZeroRem (tokenno: CARDINAL; q: CARDINAL; r: CARDINAL) ;
2531 VAR
2532 p : Range ;
2533 zero: CARDINAL ;
2534 BEGIN
2535 p := GetIndice(RangeIndex, r) ;
2536 WITH p^ DO
2537 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
2538 IF GccKnowsAbout(expr) AND IsConst(expr)
2539 THEN
2540 zero := MakeAndDeclareConstLit (tokenno, MakeKey ('0'), ZType) ;
2541 IF IsEqualConversion (zero, des, expr)
2542 THEN
2543 MetaErrorT2 (tokenNo,
2544 'the divisor {%2Wa} in this remainder expression is equal to zero, this will cause an exception to be raised before the result is assigned to the designator {%1a}',
2545 des, expr) ;
2546 PutQuad (q, ErrorOp, NulSym, NulSym, r)
2547 END
2548 END
2549 END
2550 END FoldZeroRem ;
2551
2552
2553 (*
2554 FoldRangeCheck - attempts to resolve the range check.
2555 If it evaluates to true then
2556 it is replaced by an ErrorOp
2557 elsif it evaluates to false then
2558 it is removed
2559 else
2560 it is left alone
2561 *)
2562
2563 PROCEDURE FoldRangeCheck (tokenno: CARDINAL; quad: CARDINAL; range: CARDINAL) ;
2564 BEGIN
2565 IF IsCancelled (range)
2566 THEN
2567 SubQuad (quad)
2568 ELSE
2569 FoldRangeCheckLower (tokenno, quad, range)
2570 END
2571 END FoldRangeCheck ;
2572
2573
2574 (*
2575 FoldRangeCheckLower - call the appropriate Fold procedure depending upon the type
2576 of range.
2577 *)
2578
2579 PROCEDURE FoldRangeCheckLower (tokenno: CARDINAL; quad: CARDINAL; range: CARDINAL) ;
2580 VAR
2581 p: Range ;
2582 BEGIN
2583 p := GetIndice(RangeIndex, range) ;
2584 WITH p^ DO
2585 CASE type OF
2586
2587 assignment : FoldAssignment(tokenno, quad, range) |
2588 returnassignment : FoldReturn(tokenno, quad, range) |
2589 (* subrangeassignment : | unused currently *)
2590 inc : FoldInc(tokenno, quad, range) |
2591 dec : FoldDec(tokenno, quad, range) |
2592 incl : FoldIncl(tokenno, quad, range) |
2593 excl : FoldExcl(tokenno, quad, range) |
2594 shift : FoldShift(tokenno, quad, range) |
2595 rotate : FoldRotate(tokenno, quad, range) |
2596 typereturn,
2597 typeassign,
2598 typeparam,
2599 typeexpr,
2600 typeindrx : FoldTypeCheck (tokenno, quad, range) |
2601 paramassign : FoldParameterAssign(tokenno, quad, range) |
2602 staticarraysubscript : FoldStaticArraySubscript(tokenno, quad, range) |
2603 dynamicarraysubscript: FoldDynamicArraySubscript(tokenno, quad, range) |
2604 forloopbegin : FoldForLoopBegin(tokenno, quad, range) |
2605 forloopto : FoldForLoopTo(tokenno, quad, range) |
2606 forloopend : RETURN (* unable to fold anything at this point, des, will be variable *) |
2607 pointernil : FoldNil(tokenno, quad, range) |
2608 noreturn : RETURN (* nothing to fold *) |
2609 noelse : RETURN (* nothing to fold *) |
2610 casebounds : FoldCaseBounds(tokenno, quad, range) |
2611 wholenonposdiv : FoldNonPosDiv(tokenno, quad, range) |
2612 wholenonposmod : FoldNonPosMod(tokenno, quad, range) |
2613 wholezerodiv : FoldZeroDiv(tokenno, quad, range) |
2614 wholezerorem : FoldZeroRem(tokenno, quad, range) |
2615 none : SubQuad(quad)
2616
2617 ELSE
2618 InternalError ('unexpected case')
2619 END
2620 END
2621 END FoldRangeCheckLower ;
2622
2623
2624 (*
2625 DeReferenceLValue - returns a Tree which is either ModGcc(expr)
2626 or Mod2Gcc ( *expr) depending whether, expr,
2627 is an LValue.
2628 *)
2629
2630 PROCEDURE DeReferenceLValue (tokenno: CARDINAL; expr: CARDINAL) : tree ;
2631 VAR
2632 e : tree ;
2633 location: location_t ;
2634 BEGIN
2635 location := TokenToLocation(tokenno) ;
2636 e := Mod2Gcc(expr) ;
2637 IF GetMode(expr)=LeftValue
2638 THEN
2639 e := BuildIndirect(location, e, Mod2Gcc(GetType(expr)))
2640 END ;
2641 RETURN( e )
2642 END DeReferenceLValue ;
2643
2644
2645 (*
2646 BuildStringParam - builds a C style string parameter which will be passed
2647 as an ADDRESS type.
2648 *)
2649
2650 PROCEDURE BuildStringParam (tokenno: CARDINAL; s: String) ;
2651 BEGIN
2652 BuildStringParamLoc (TokenToLocation(tokenno), s)
2653 END BuildStringParam ;
2654
2655
2656 (*
2657 BuildStringParamLoc - builds a C style string parameter which will be passed
2658 as an ADDRESS type.
2659 *)
2660
2661 PROCEDURE BuildStringParamLoc (location: location_t; s: String) ;
2662 BEGIN
2663 BuildParam (location,
2664 BuildConvert (location, Mod2Gcc (Address),
2665 BuildAddr (location, BuildStringConstant (string(s), Length(s)),
2666 FALSE), FALSE))
2667 END BuildStringParamLoc ;
2668
2669
2670 (*
2671 CodeErrorCheck - returns a Tree calling the approprate exception handler.
2672 *)
2673
2674 PROCEDURE CodeErrorCheck (r: CARDINAL; function, message: String) : tree ;
2675 VAR
2676 filename: String ;
2677 line,
2678 column : CARDINAL ;
2679 p : Range ;
2680 f : tree ;
2681 location: location_t ;
2682 BEGIN
2683 IF HandlerExists (r)
2684 THEN
2685 IF message = NIL
2686 THEN
2687 message := GetRangeErrorMessage (r)
2688 END ;
2689 message := FillInParameters (r, message) ;
2690 p := GetIndice (RangeIndex, r) ;
2691 WITH p^ DO
2692 filename := FindFileNameFromToken (tokenNo, 0) ;
2693 line := TokenToLineNo (tokenNo, 0) ;
2694 column := TokenToColumnNo (tokenNo, 0) ;
2695 location := TokenToLocation (tokenNo) ;
2696 f := Mod2Gcc (lookupExceptionHandler (type)) ;
2697 BuildStringParam (tokenNo, message) ;
2698 BuildStringParam (tokenNo, function) ;
2699 BuildParam (location, BuildIntegerConstant (column)) ;
2700 BuildParam (location, BuildIntegerConstant (line)) ;
2701 BuildStringParam (tokenNo, filename) ;
2702 RETURN BuildProcedureCallTree (location, f, NIL)
2703 END
2704 ELSE
2705 RETURN NIL
2706 END
2707 END CodeErrorCheck ;
2708
2709
2710 (*
2711 IssueWarning - issue a warning. The compiler knows that this basic block can be reached
2712 and we are in scope, function.
2713 *)
2714
2715 PROCEDURE IssueWarning (function: String; r: CARDINAL) ;
2716 VAR
2717 p: Range ;
2718 s: String ;
2719 BEGIN
2720 p := GetIndice(RangeIndex, r) ;
2721 WITH p^ DO
2722 CASE type OF
2723
2724 assignment : s := InitString('if the assignment is ever executed then the designator {%1Wa} will exceed the type range {%1ts:of {%1ts}}') |
2725 returnassignment : s := InitString('if the value {%2Wa} is returned from procedure function {%1Wa} then it will exceed the type range {%1ts:of {%1ts}}') |
2726 subrangeassignment : InternalError ('not expecting this case value') |
2727 inc : s := InitString('if the INC is ever executed the expression {%2Wa} will cause an overflow error for the designator {%1a} as it exceeds the type range {%1ts:of {%1ts}}') |
2728 dec : s := InitString('if the DEC is ever executed the expression {%2Wa} will cause an underflow error for the designator {%1a} as it exceeds the type range {%1ts:of {%1ts}}') |
2729 incl : s := InitString('the expression {%2Wa} given in the INCL exceeds the type range {%1ts} of the designator {%1a}') |
2730 excl : s := InitString('the expression {%2Wa} given in the EXCL exceeds the type range {%1ts} of the designator {%1a}') |
2731 shift : s := InitString('the expression {%2Wa} given in the second parameter to SHIFT exceeds the type range {%1ts} of the first parameter {%1a}') |
2732 rotate : s := InitString('the expression {%2Wa} given in the second parameter to ROTATE exceeds the type range {%1ts} of the first parameter {%1a}') |
2733 typeassign : s := InitString('') |
2734 typeparam : s := InitString('') |
2735 typeexpr : s := InitString('') |
2736 paramassign : s := InitString('if this call is executed then the actual parameter {%2Wa} will be out of range of the {%3N} formal parameter {%1a}') |
2737 staticarraysubscript : s := InitString('if this access to the static array {%1Wa:{%2a:{%1a}[{%2a}]}} is ever made then the index will be out of bounds in the {%3N} array subscript') |
2738 dynamicarraysubscript: s := InitString('if this access to the dynamic array {%1Wa:{%2a:{%1a}[{%2a}]}} is ever made then the index will be out of bounds in the {%3N} array subscript') |
2739 forloopbegin : s := InitString('if the assignment in this FOR loop is ever executed then the designator {%1Wa} will be exceed the type range {%1ts:of {%1ts}}') |
2740 forloopto : s := InitString('the final value {%2Wa} in this FOR loop will be out of bounds {%1ts:of type {%1ts}} if ever executed') |
2741 forloopend : s := InitString('the FOR loop will cause the designator {%1Wa} to be out of bounds when the BY value {%2a} is added') |
2742 pointernil : s := InitString('if this pointer value {%1Wa} is ever dereferenced it will cause an exception') |
2743 noreturn : s := InitString('{%1W:}this function will exit without executing a RETURN statement') |
2744 noelse : s := InitString('{%1W:}this CASE statement does not have an ELSE statement') |
2745 casebounds : s := InitString('{%1W:}this CASE statement has overlapping ranges') |
2746 wholenonposdiv : s := InitString('this division expression {%2Wa} will cause an exception as this divisor is less than or equal to zero') |
2747 wholenonposmod : s := InitString('this modulus expression {%2Wa} will cause an exception as this divisor is less than or equal to zero') |
2748 wholezerodiv : s := InitString('this division expression {%2Wa} will cause an exception as the divisor is zero') |
2749 wholezerorem : s := InitString('this remainder expression {%2Wa} will cause an exception as the divisor is zero') |
2750 none : InternalError ('unexpected value')
2751
2752 ELSE
2753 InternalError ('enumeration value unknown')
2754 END ;
2755 s := ConCat (s, Mark (InitString (' in ('))) ;
2756 s := ConCat (s, function) ;
2757 s := ConCatChar (s, ')') ;
2758 MetaErrorStringT3 (tokenNo, s, des, expr, dimension) ;
2759 (* FlushErrors *)
2760 END
2761 END IssueWarning ;
2762
2763
2764 (*
2765 CodeErrorCheckLoc - generate a runtime error message positioned at location
2766 and in function. If function is NIL then the error scope
2767 is used.
2768 *)
2769
2770 PROCEDURE CodeErrorCheckLoc (location: location_t;
2771 function, message: ConstCharStar; func: CARDINAL) : tree ;
2772 VAR
2773 scope,
2774 errorMessage: String ;
2775 t : tree ;
2776 filename : String ;
2777 line,
2778 column : CARDINAL ;
2779 BEGIN
2780 IF func = NulSym
2781 THEN
2782 RETURN NIL
2783 ELSE
2784 t := Mod2Gcc (func) ;
2785 IF t # NIL
2786 THEN
2787 filename := InitStringCharStar (GetFilenameFromLocation (location)) ;
2788 Assert (message # NIL) ;
2789 errorMessage := InitStringCharStar (message) ;
2790 column := GetColumnNoFromLocation (location) ;
2791 line := GetLineNoFromLocation (location) ;
2792 BuildStringParamLoc (location, errorMessage) ;
2793 IF function = NIL
2794 THEN
2795 scope := GetAnnounceScope (filename, NIL)
2796 ELSE
2797 scope := quoteOpen (InitString ('')) ;
2798 scope := ConCat (scope, Mark (InitStringCharStar (function))) ;
2799 scope := ConCat (InitString ("procedure "), quoteClose (scope))
2800 END ;
2801 BuildStringParamLoc (location, scope) ;
2802 BuildParam (location, BuildIntegerConstant (column)) ;
2803 BuildParam (location, BuildIntegerConstant (line)) ;
2804 BuildStringParamLoc (location, filename) ;
2805 t := BuildProcedureCallTree (location, t, NIL) ;
2806 (*
2807 filename := KillString (filename) ;
2808 scope := KillString (scope) ;
2809 errorMessage := KillString (errorMessage)
2810 *)
2811 END ;
2812 RETURN t
2813 END
2814 END CodeErrorCheckLoc ;
2815
2816
2817 (*
2818 IssueWarningLoc -
2819 *)
2820
2821 PROCEDURE IssueWarningLoc (location: location_t; message: ConstCharStar) ;
2822 VAR
2823 s: String ;
2824 BEGIN
2825 s := InitString ("numerical overflow detected when performing ") ;
2826 s := ConCat (s, Mark (InitStringCharStar (message))) ;
2827 ErrorAt (location, string (s)) ;
2828 s := KillString (s)
2829 END IssueWarningLoc ;
2830
2831
2832 (*
2833 BuildIfCallWholeHandlerLoc - return a Tree containing a runtime test whether, condition, is true.
2834 *)
2835
2836 PROCEDURE BuildIfCallWholeHandlerLoc (location: location_t; condition: tree;
2837 scope, message: ConstCharStar) : tree ;
2838 BEGIN
2839 RETURN BuildIfCallHandlerLoc (location, condition, scope, message, ExceptionWholeValue)
2840 END BuildIfCallWholeHandlerLoc ;
2841
2842
2843 (*
2844 BuildIfCallRealHandlerLoc - return a Tree containing a runtime test whether, condition, is true.
2845 *)
2846
2847 PROCEDURE BuildIfCallRealHandlerLoc (location: location_t; condition: tree;
2848 scope, message: ConstCharStar) : tree ;
2849 BEGIN
2850 RETURN BuildIfCallHandlerLoc (location, condition, scope, message, ExceptionRealValue)
2851 END BuildIfCallRealHandlerLoc ;
2852
2853
2854 (*
2855 BuildIfCallHandlerLoc - return a Tree containing a runtime test whether, condition, is true.
2856 *)
2857
2858 PROCEDURE BuildIfCallHandlerLoc (location: location_t; condition: tree;
2859 scope, message: ConstCharStar; func: CARDINAL) : tree ;
2860 BEGIN
2861 IF IsTrue (condition)
2862 THEN
2863 IssueWarningLoc (location, message)
2864 END ;
2865 RETURN BuildIfThenDoEnd (condition, CodeErrorCheckLoc (location, scope, message, func))
2866 END BuildIfCallHandlerLoc ;
2867
2868
2869 (*
2870 BuildIfCallHandler -
2871 *)
2872
2873 PROCEDURE BuildIfCallHandler (condition: tree; r: CARDINAL;
2874 function, message: String; warning: BOOLEAN) : tree ;
2875 BEGIN
2876 IF warning AND IsTrue (condition)
2877 THEN
2878 IssueWarning (function, r)
2879 END ;
2880 RETURN BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message))
2881 END BuildIfCallHandler ;
2882
2883
2884 (*
2885 RangeCheckReal -
2886 *)
2887
2888 PROCEDURE RangeCheckReal (p: Range; r: CARDINAL; function, message: String) ;
2889 VAR
2890 e,
2891 condition: tree ;
2892 location : location_t ;
2893 BEGIN
2894 WITH p^ DO
2895 location := TokenToLocation (tokenNo) ;
2896 e := DeReferenceLValue (tokenNo, expr) ;
2897 condition := BuildEqualTo (location,
2898 BuiltInIsfinite (location, e),
2899 GetIntegerZero (location)) ;
2900 AddStatement (location, BuildIfCallHandler (condition, r, function, message, TRUE)) ;
2901 END
2902 END RangeCheckReal ;
2903
2904
2905 (*
2906 RangeCheckOrdinal -
2907 *)
2908
2909 PROCEDURE RangeCheckOrdinal (p: Range; r: CARDINAL; function, message: String) ;
2910 VAR
2911 condition,
2912 desMin, desMax,
2913 exprMin, exprMax: tree ;
2914 location : location_t ;
2915 BEGIN
2916 WITH p^ DO
2917 location := TokenToLocation(tokenNo) ;
2918 IF GetMinMax(tokenNo, exprLowestType, exprMin, exprMax) AND
2919 GetMinMax(tokenNo, desLowestType, desMin, desMax)
2920 THEN
2921 IF OverlapsRange(desMin, desMax, exprMin, exprMax)
2922 THEN
2923 IF IsGreater(desMin, exprMin)
2924 THEN
2925 condition := BuildLessThan(location, DeReferenceLValue(tokenNo, expr), BuildConvert(location, Mod2Gcc(exprLowestType), desMin, FALSE)) ;
2926 AddStatement(location, BuildIfCallHandler(condition, r, function, message, TRUE))
2927 END ;
2928 IF IsGreater(exprMax, desMax)
2929 THEN
2930 condition := BuildGreaterThan(location, DeReferenceLValue(tokenNo, expr), BuildConvert(location, Mod2Gcc(exprLowestType), desMax, FALSE)) ;
2931 AddStatement(location, BuildIfCallHandler(condition, r, function, message, TRUE))
2932 END
2933 ELSE
2934 MetaErrorStringT3 (tokenNo, message, des, expr, paramNo)
2935 END
2936 END
2937 END
2938 END RangeCheckOrdinal ;
2939
2940
2941 (*
2942 DoCodeAssignmentExprType -
2943 *)
2944
2945 PROCEDURE DoCodeAssignmentExprType (p: Range;
2946 r: CARDINAL; function, message: String) ;
2947 BEGIN
2948 WITH p^ DO
2949 IF GccKnowsAbout(desLowestType) AND
2950 GccKnowsAbout(exprLowestType)
2951 THEN
2952 IF IsRealType(desLowestType) AND IsRealType(exprLowestType)
2953 THEN
2954 RangeCheckReal (p, r, function, message)
2955 ELSE
2956 RangeCheckOrdinal (p, r, function, message)
2957 END
2958 ELSE
2959 InternalError ('should have resolved these types')
2960 END
2961 END
2962 END DoCodeAssignmentExprType ;
2963
2964
2965 (*
2966 DoCodeAssignmentWithoutExprType -
2967 *)
2968
2969 PROCEDURE DoCodeAssignmentWithoutExprType (p: Range;
2970 r: CARDINAL; function, message: String) ;
2971 VAR
2972 condition,
2973 desMin, desMax: tree ;
2974 location : location_t ;
2975 BEGIN
2976 WITH p^ DO
2977 location := TokenToLocation(tokenNo) ;
2978 IF GccKnowsAbout(desLowestType)
2979 THEN
2980 IF GetMinMax(tokenNo, desLowestType, desMin, desMax)
2981 THEN
2982 condition := BuildLessThan(location,
2983 BuildConvert(location, Mod2Gcc(desLowestType),
2984 DeReferenceLValue(tokenNo, expr), FALSE),
2985 desMin) ;
2986 AddStatement(location, BuildIfCallHandler(condition, r, function, message, TRUE)) ;
2987 condition := BuildGreaterThan(location,
2988 BuildConvert(location, Mod2Gcc(desLowestType),
2989 DeReferenceLValue(tokenNo, expr), FALSE),
2990 desMax) ;
2991 AddStatement(location, BuildIfCallHandler(condition, r, function, message, TRUE))
2992 END
2993 ELSE
2994 InternalError ('should have resolved this type')
2995 END
2996 END
2997 END DoCodeAssignmentWithoutExprType ;
2998
2999
3000 (*
3001 DoCodeAssignment -
3002 *)
3003
3004 PROCEDURE DoCodeAssignment (tokenno: CARDINAL; r: CARDINAL;
3005 function, message: String) ;
3006 VAR
3007 p: Range ;
3008 BEGIN
3009 p := GetIndice(RangeIndex, r) ;
3010 WITH p^ DO
3011 TryDeclareConstant(tokenNo, des) ;
3012 TryDeclareConstant(tokenNo, expr) ;
3013 DeclareConstructor(tokenno, 0, expr) ;
3014 IF desLowestType#NulSym
3015 THEN
3016 Assert(GccKnowsAbout(expr)) ;
3017 IF exprLowestType=NulSym
3018 THEN
3019 DoCodeAssignmentWithoutExprType (p, r, function, message)
3020 ELSE
3021 DoCodeAssignmentExprType (p, r, function, message)
3022 END
3023 END
3024 END
3025 END DoCodeAssignment ;
3026
3027
3028 (*
3029 CodeAssignment -
3030 *)
3031
3032 PROCEDURE CodeAssignment (tokenno: CARDINAL;
3033 r: CARDINAL; function, message: String) ;
3034 BEGIN
3035 DoCodeAssignment (tokenno, r, function, message)
3036 END CodeAssignment ;
3037
3038
3039 (*
3040 CodeParameterAssign -
3041 *)
3042
3043 PROCEDURE CodeParameterAssign (tokenno: CARDINAL;
3044 r: CARDINAL; function, message: String) ;
3045 BEGIN
3046 DoCodeAssignment (tokenno, r, function, message)
3047 END CodeParameterAssign ;
3048
3049
3050 (*
3051 CodeReturn -
3052 *)
3053
3054 PROCEDURE CodeReturn (tokenno: CARDINAL;
3055 r: CARDINAL; function, message: String) ;
3056 BEGIN
3057 DoCodeAssignment (tokenno, r, function, message)
3058 END CodeReturn ;
3059
3060
3061 (*
3062 IfOutsideLimitsDo -
3063 *)
3064
3065 PROCEDURE IfOutsideLimitsDo (tokenno: CARDINAL; min, expr, max: tree; r: CARDINAL;
3066 function, message: String) ;
3067 VAR
3068 condition: tree ;
3069 location : location_t ;
3070 BEGIN
3071 location := TokenToLocation (tokenno) ;
3072 condition := BuildGreaterThan (location, min, expr) ;
3073 AddStatement (location, BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message))) ;
3074 condition := BuildLessThan (location, max, expr) ;
3075 AddStatement (location, BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message)))
3076 END IfOutsideLimitsDo ;
3077
3078
3079 (*
3080 CodeInc -
3081 *)
3082
3083 PROCEDURE CodeInc (tokenno: CARDINAL;
3084 r: CARDINAL; function, message: String) ;
3085 VAR
3086 p : Range ;
3087 t, condition,
3088 e,
3089 desMin, desMax: tree ;
3090 location : location_t ;
3091 BEGIN
3092 location := TokenToLocation(tokenno) ;
3093 p := GetIndice(RangeIndex, r) ;
3094 WITH p^ DO
3095 TryDeclareConstant(tokenNo, des) ;
3096 TryDeclareConstant(tokenNo, expr) ;
3097 IF desLowestType#NulSym
3098 THEN
3099 IF GccKnowsAbout(expr) AND GccKnowsAbout(desLowestType)
3100 THEN
3101 IF GetMinMax(tokenno, desLowestType, desMin, desMax)
3102 THEN
3103 e := BuildConvert(location, GetTreeType(desMin), DeReferenceLValue(tokenno, expr), FALSE) ;
3104 IfOutsideLimitsDo(tokenNo,
3105 BuildConvert(location, GetTreeType(desMin), GetIntegerZero(location), FALSE),
3106 e, desMax, r, function, message) ;
3107 t := BuildSub(location,
3108 desMax,
3109 BuildConvert(location, Mod2Gcc(desLowestType), e, FALSE),
3110 FALSE) ;
3111 condition := BuildGreaterThan(location, Mod2Gcc(des), t) ;
3112 AddStatement (location, BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message)))
3113 END
3114 ELSE
3115 InternalError ('should have resolved these types')
3116 END
3117 END
3118 END
3119 END CodeInc ;
3120
3121
3122 (*
3123 CodeDec -
3124 *)
3125
3126 PROCEDURE CodeDec (tokenno: CARDINAL;
3127 r: CARDINAL; function, message: String) ;
3128 VAR
3129 p : Range ;
3130 t, condition,
3131 e,
3132 desMin, desMax: tree ;
3133 location : location_t ;
3134 BEGIN
3135 location := TokenToLocation(tokenno) ;
3136 p := GetIndice(RangeIndex, r) ;
3137 WITH p^ DO
3138 TryDeclareConstant(tokenNo, des) ;
3139 TryDeclareConstant(tokenNo, expr) ;
3140 IF desLowestType#NulSym
3141 THEN
3142 IF GccKnowsAbout(expr) AND GccKnowsAbout(desLowestType)
3143 THEN
3144 IF GetMinMax(tokenno, desLowestType, desMin, desMax)
3145 THEN
3146 e := BuildConvert(location, GetTreeType(desMin), DeReferenceLValue(tokenno, expr), FALSE) ;
3147 IfOutsideLimitsDo(tokenNo,
3148 BuildConvert(location, GetTreeType(desMin), GetIntegerZero(location), FALSE),
3149 e, desMax, r, function, message) ;
3150 t := BuildSub(location, BuildConvert(location, Mod2Gcc(desLowestType), e, FALSE),
3151 desMin,
3152 FALSE) ;
3153 condition := BuildLessThan(location, Mod2Gcc(des), t) ;
3154 AddStatement (location, BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message)))
3155 END
3156 ELSE
3157 InternalError ('should have resolved these types')
3158 END
3159 END
3160 END
3161 END CodeDec ;
3162
3163
3164 (*
3165 CodeInclExcl -
3166 *)
3167
3168 PROCEDURE CodeInclExcl (tokenno: CARDINAL;
3169 r: CARDINAL; function, message: String) ;
3170 VAR
3171 p : Range ;
3172 e,
3173 desMin, desMax: tree ;
3174 location : location_t ;
3175 BEGIN
3176 location := TokenToLocation(tokenno) ;
3177 p := GetIndice(RangeIndex, r) ;
3178 WITH p^ DO
3179 TryDeclareConstant(tokenNo, des) ;
3180 TryDeclareConstant(tokenNo, expr) ;
3181 desLowestType := SkipType(GetType(des)) ;
3182 IF desLowestType#NulSym
3183 THEN
3184 IF GccKnowsAbout(expr) AND GccKnowsAbout(desLowestType)
3185 THEN
3186 IF GetMinMax(tokenno, desLowestType, desMin, desMax)
3187 THEN
3188 e := BuildConvert(location, GetTreeType(desMin), DeReferenceLValue(tokenno, expr), FALSE) ;
3189 IfOutsideLimitsDo(tokenNo, desMin, e, desMax, r, function, message)
3190 (* this should not be used for incl/excl as des is a set type
3191 t := BuildSub(location,
3192 desMax,
3193 BuildConvert(location, Mod2Gcc(desLowestType), e, FALSE),
3194 FALSE) ;
3195 condition := BuildGreaterThan(Mod2Gcc(des), t) ;
3196 AddStatement(location, BuildIfThenDoEnd(condition, CodeErrorCheck(r, function, message)))
3197 *)
3198 END
3199 ELSE
3200 InternalError ('should have resolved these types')
3201 END
3202 END
3203 END
3204 END CodeInclExcl ;
3205
3206
3207 (*
3208 CodeShiftRotate - ensure that the bit shift is within the range
3209 -(MAX(set)-MIN(set)+1)..(MAX(set)-MIN(set)+1)
3210 *)
3211
3212 PROCEDURE CodeShiftRotate (tokenno: CARDINAL;
3213 r: CARDINAL; function, message: String) ;
3214 VAR
3215 ofType : CARDINAL ;
3216 p : Range ;
3217 e,
3218 shiftMin, shiftMax,
3219 desMin, desMax : tree ;
3220 location : location_t ;
3221 BEGIN
3222 p := GetIndice(RangeIndex, r) ;
3223 WITH p^ DO
3224 TryDeclareConstant(tokenNo, des) ;
3225 TryDeclareConstant(tokenNo, expr) ;
3226 desLowestType := SkipType(GetType(des)) ;
3227 IF desLowestType#NulSym
3228 THEN
3229 ofType := SkipType(GetType(desLowestType)) ;
3230 IF GccKnowsAbout(expr) AND GccKnowsAbout(ofType)
3231 THEN
3232 IF GetMinMax(tokenno, ofType, desMin, desMax)
3233 THEN
3234 location := TokenToLocation(tokenNo) ;
3235 desMin := BuildConvert(location, GetIntegerType(), desMin, FALSE) ;
3236 desMax := BuildConvert(location, GetIntegerType(), desMax, FALSE) ;
3237 shiftMax := BuildAdd(location,
3238 BuildSub(location, desMax, desMin, FALSE),
3239 GetIntegerOne(location),
3240 FALSE) ;
3241 shiftMin := BuildNegate(location, shiftMax, FALSE) ;
3242 e := BuildConvert(location, GetIntegerType(), DeReferenceLValue(tokenno, expr), FALSE) ;
3243 IfOutsideLimitsDo(tokenNo, shiftMin, e, shiftMax, r, function, message)
3244 END
3245 ELSE
3246 InternalError ('should have resolved these types')
3247 END
3248 END
3249 END
3250 END CodeShiftRotate ;
3251
3252
3253 (*
3254 CodeStaticArraySubscript -
3255 *)
3256
3257 PROCEDURE CodeStaticArraySubscript (tokenno: CARDINAL;
3258 r: CARDINAL; function, message: String) ;
3259 VAR
3260 p : Range ;
3261 desMin, desMax: tree ;
3262 location : location_t ;
3263 BEGIN
3264 location := TokenToLocation (tokenno) ;
3265 p := GetIndice (RangeIndex, r) ;
3266 WITH p^ DO
3267 TryDeclareConstant (tokenNo, expr) ;
3268 IF GccKnowsAbout (expr) AND GccKnowsAbout (desLowestType)
3269 THEN
3270 IF GetMinMax (tokenno, desLowestType, desMin, desMax)
3271 THEN
3272 IfOutsideLimitsDo (tokenno, desMin,
3273 BuildConvert (location, GetTreeType (desMin), DeReferenceLValue (tokenno, expr), FALSE),
3274 desMax, r, function, message)
3275 ELSE
3276 InternalError ('should have resolved the bounds of the static array')
3277 END
3278 ELSE
3279 InternalError ('should have resolved these types')
3280 END
3281 END
3282 END CodeStaticArraySubscript ;
3283
3284
3285 (*
3286 CodeDynamicArraySubscript -
3287 *)
3288
3289 PROCEDURE CodeDynamicArraySubscript (tokenno: CARDINAL;
3290 r: CARDINAL; function, message: String) ;
3291 VAR
3292 UnboundedType: CARDINAL ;
3293 p : Range ;
3294 high, e : tree ;
3295 location : location_t ;
3296 BEGIN
3297 location := TokenToLocation(tokenno) ;
3298 p := GetIndice(RangeIndex, r) ;
3299 WITH p^ DO
3300 TryDeclareConstant(tokenNo, expr) ;
3301 Assert(IsVar(des)) ;
3302 IF GccKnowsAbout(expr) AND GccKnowsAbout(des)
3303 THEN
3304 UnboundedType := GetType(des) ;
3305 Assert(IsUnbounded(UnboundedType)) ;
3306 high := BuildConvert(location, GetIntegerType(), GetHighFromUnbounded(location, dimension, des), FALSE) ;
3307 e := BuildConvert(location, GetIntegerType(), DeReferenceLValue(tokenno, expr), FALSE) ;
3308 IfOutsideLimitsDo(tokenNo, GetIntegerZero(location), e, high, r, function, message)
3309 ELSE
3310 InternalError ('should have resolved these types')
3311 END
3312 END
3313 END CodeDynamicArraySubscript ;
3314
3315
3316 (*
3317 CodeForLoopBegin -
3318 *)
3319
3320 PROCEDURE CodeForLoopBegin (tokenno: CARDINAL;
3321 r: CARDINAL; function, message: String) ;
3322 BEGIN
3323 IF ForLoopBeginTypeCompatible (GetIndice (RangeIndex, r))
3324 THEN
3325 DoCodeAssignment(tokenno, r, function, message)
3326 END
3327 END CodeForLoopBegin ;
3328
3329
3330 (*
3331 CodeForLoopTo -
3332 *)
3333
3334 PROCEDURE CodeForLoopTo (tokenno: CARDINAL;
3335 r: CARDINAL; function, message: String) ;
3336 BEGIN
3337 DoCodeAssignment(tokenno, r, function, message)
3338 END CodeForLoopTo ;
3339
3340
3341 (*
3342 Pseudo template code for CodeLoopEnd:
3343
3344 PROCEDURE CheckCardinalInteger (des: CARDINAL; inc: INTEGER) ;
3345 VAR
3346 room,
3347 lg : CARDINAL ;
3348 BEGIN
3349 IF inc>=0
3350 THEN
3351 IF des>=0
3352 THEN
3353 lg := VAL(CARDINAL, inc) ;
3354 room := MAX(CARDINAL)-des ;
3355 IF lg>room
3356 THEN
3357 printf("increment exceeds range at end of FOR loop\n") ;
3358 exit (2)
3359 END
3360 ELSE
3361 (* inc can never cause an overflow given its type *)
3362 END
3363 ELSE
3364 (* inc < 0 *)
3365 IF des>VAL(CARDINAL, MAX(INTEGER))
3366 THEN
3367 (* inc can never cause an underflow given its range *)
3368 ELSE
3369 (* des <= MAX(INTEGER) *)
3370 IF des=MIN(INTEGER)
3371 THEN
3372 printf("increment exceeds range at end of FOR loop\n") ;
3373 exit (4)
3374 ELSE
3375 IF inc=MIN(INTEGER)
3376 THEN
3377 IF des=0
3378 THEN
3379 printf("increment exceeds range at end of FOR loop\n") ;
3380 exit (5)
3381 END
3382 ELSE
3383 lg := VAL(CARDINAL, -inc) ;
3384 IF lg>des
3385 THEN
3386 printf("increment exceeds range at end of FOR loop\n") ;
3387 exit (5)
3388 END
3389 END
3390 END
3391 END
3392 END
3393 END CheckCardinalInteger ;
3394
3395
3396 PROCEDURE CheckCardinalCardinal (des: CARDINAL; inc: CARDINAL) ;
3397 BEGIN
3398 IF MAX(CARDINAL)-des<inc
3399 THEN
3400 printf("increment exceeds range at end of FOR loop\n") ;
3401 exit (2)
3402 END
3403 END CheckCardinalCardinal ;
3404 *)
3405
3406
3407 (*
3408 SameTypesCodeForLoopEnd - the trivial case.
3409 *)
3410
3411 PROCEDURE SameTypesCodeForLoopEnd (tokenNo: CARDINAL; r: CARDINAL; function, message: String;
3412 p: Range; dmax: tree) ;
3413 VAR
3414 inc,
3415 room,
3416 statement,
3417 condition: tree ;
3418 location : location_t ;
3419 BEGIN
3420 location := TokenToLocation(tokenNo) ;
3421 WITH p^ DO
3422 inc := DeReferenceLValue(tokenNo, expr) ;
3423 room := BuildSub(location, dmax, Mod2Gcc(des), FALSE) ;
3424 condition := BuildLessThan(location, room, inc) ;
3425 statement := BuildIfCallHandler(condition, r, function, message, IsTrue(condition)) ;
3426 AddStatement(location, statement)
3427 END
3428 END SameTypesCodeForLoopEnd ;
3429
3430
3431 (*
3432 DiffTypesSameForLoopEnd - remember that lowestType will map onto an int, or unsigned int
3433 of appropriate size.
3434 *)
3435
3436 PROCEDURE DiffTypesCodeForLoopEnd (tokenNo: CARDINAL; r: CARDINAL; function, message: String;
3437 p: Range; dmax, emin, emax: tree) ;
3438 VAR
3439 location : location_t ;
3440 desoftypee,
3441 inc,
3442 room,
3443 c1, c2, c3,
3444 c4, c5, c6,
3445 c7, c8,
3446 s1, s2, s3,
3447 s4, s5, s6,
3448 s7, s8,
3449 lg1, lg2,
3450 dz, ez : tree ;
3451 BEGIN
3452 location := TokenToLocation(tokenNo) ;
3453 WITH p^ DO
3454 inc := DeReferenceLValue(tokenNo, expr) ;
3455 ez := BuildConvert(location, Mod2Gcc(exprLowestType), GetIntegerZero(location), FALSE) ;
3456 dz := BuildConvert(location, Mod2Gcc(desLowestType), GetIntegerZero(location), FALSE) ;
3457
3458 c1 := BuildGreaterThanOrEqual(location, inc, ez) ;
3459 (* if (inc >= 0) [c1] *)
3460 c2 := BuildGreaterThanOrEqual(location, Mod2Gcc(des), dz) ;
3461 (* if (des >= 0) [c2] *)
3462 lg1 := BuildConvert(location, Mod2Gcc(desLowestType), inc, FALSE) ;
3463 room := BuildSub(location, dmax, Mod2Gcc(des), FALSE) ;
3464 c3 := BuildGreaterThan(location, lg1, room) ; (* [c3] *)
3465 (* WarnIf(IsTrue(c1) AND IsTrue(c2) AND IsTrue(c3), function, message) ; --implement me-- *)
3466
3467 s3 := BuildIfCallHandler(c3, r, function, message, FALSE) ;
3468 s2 := BuildIfThenDoEnd(c2, s3) ;
3469
3470 (* else *)
3471 (* (* inc < 0 *) [s4] *)
3472 (* if (des <= val(desLowestType, emax) [c4] *)
3473 c4 := BuildLessThanOrEqual(location, Mod2Gcc(des), BuildConvert(location, Mod2Gcc(desLowestType), emax, FALSE)) ;
3474 (* (* des <= MAX(exprLowestType) *) *)
3475 desoftypee := BuildConvert(location, Mod2Gcc(exprLowestType), Mod2Gcc(des), FALSE) ;
3476 c5 := BuildEqualTo(location, desoftypee, emin) ; (* [c5] *)
3477 s5 := BuildIfCallHandler(c5, r, function, message, FALSE) ;
3478 (* if des = emin *)
3479 (* error [s5] *)
3480 (* end *)
3481 c6 := BuildEqualTo(location, inc, emin) ; (* [c6] *)
3482 (* if inc = emin *)
3483 (* if des = 0 [c7] *)
3484 c7 := BuildEqualTo(location, Mod2Gcc(des), dz) ;
3485 s7 := BuildIfCallHandler(c7, r, function, message, FALSE) ;
3486
3487 (* end *)
3488 (* else *)
3489 (* lg2 = VAL(desLowestType, -inc) [s8] *)
3490 lg2 := BuildConvert(location, Mod2Gcc(desLowestType), BuildNegate(location, inc, FALSE), FALSE) ;
3491 (* if lg2 > des *)
3492 (* error *)
3493 c8 := BuildGreaterThan(location, lg2, Mod2Gcc(des)) ;
3494 s8 := BuildIfCallHandler(c8, r, function, message, FALSE) ;
3495 (* end *)
3496 (* end *)
3497 (* end *)
3498 (* end *)
3499 (* end *)
3500 END ;
3501
3502 s6 := BuildIfThenElseEnd(c6, s7, s8) ;
3503 s4 := BuildIfThenElseEnd(c4, s5, s6) ;
3504 s1 := BuildIfThenElseEnd(c1, s2, s4) ;
3505 AddStatement(location, s1)
3506
3507 END DiffTypesCodeForLoopEnd ;
3508
3509
3510 (*
3511 CodeForLoopEnd - checks to see that des := des + expr does not overflow.
3512 This is called at the end of the for loop. It is more complex
3513 than it initially seems as des and expr might be different types.
3514 *)
3515
3516 PROCEDURE CodeForLoopEnd (tokenno: CARDINAL;
3517 r: CARDINAL; function, message: String) ;
3518 VAR
3519 isCard : BOOLEAN ;
3520 p : Range ;
3521 dmin, dmax,
3522 emin, emax: tree ;
3523 BEGIN
3524 p := GetIndice(RangeIndex, r) ;
3525 WITH p^ DO
3526 TryDeclareConstant(tokenno, des) ; (* use quad tokenno, rather than the range tokenNo *)
3527 TryDeclareConstant(tokenno, expr) ; (* use quad tokenno, rather than the range tokenNo *)
3528 IF desLowestType#NulSym
3529 THEN
3530 Assert(GccKnowsAbout(expr)) ;
3531 IF GccKnowsAbout(desLowestType) AND
3532 GetMinMax(tokenno, desLowestType, dmin, dmax) AND
3533 GccKnowsAbout(exprLowestType) AND
3534 GetMinMax(tokenno, exprLowestType, emin, emax)
3535 THEN
3536 PushIntegerTree(dmin) ;
3537 PushInt(0) ;
3538 isCard := GreEqu(tokenno) ;
3539 IF (desLowestType=exprLowestType) AND isCard
3540 THEN
3541 SameTypesCodeForLoopEnd(tokenno, r, function, message, p, dmax)
3542 ELSE
3543 DiffTypesCodeForLoopEnd(tokenno, r, function, message, p, dmax, emin, emax)
3544 END
3545 END
3546 END
3547 END
3548 END CodeForLoopEnd ;
3549
3550
3551 (*
3552 CodeNil -
3553 *)
3554
3555 PROCEDURE CodeNil (r: CARDINAL; function, message: String) ;
3556 VAR
3557 p : Range ;
3558 condition, t: tree ;
3559 location : location_t ;
3560 BEGIN
3561 p := GetIndice(RangeIndex, r) ;
3562 WITH p^ DO
3563 TryDeclareConstant(tokenNo, des) ;
3564 (*
3565 IF GetMode(des)=LeftValue
3566 THEN
3567 (* t := BuildIndirect(Mod2Gcc(des), Mod2Gcc(GetType(des))) *)
3568 ELSE
3569 t := Mod2Gcc(des)
3570 END ;
3571 *)
3572 t := Mod2Gcc(des) ;
3573 location := TokenToLocation(tokenNo) ;
3574 condition := BuildEqualTo(location, BuildConvert(location, GetPointerType(), t, FALSE), GetPointerZero(location)) ;
3575 AddStatement(location, BuildIfCallHandler(condition, r, function, message, TRUE))
3576 END
3577 END CodeNil ;
3578
3579
3580 (*
3581 CodeWholeNonPos - generates range check code for expr<=0.
3582 *)
3583
3584 PROCEDURE CodeWholeNonPos (tokenno: CARDINAL;
3585 r: CARDINAL; function, message: String) ;
3586 VAR
3587 zero : CARDINAL ;
3588 p : Range ;
3589 condition,
3590 e : tree ;
3591 location : location_t ;
3592 BEGIN
3593 p := GetIndice(RangeIndex, r) ;
3594 WITH p^ DO
3595 TryDeclareConstant (tokenNo, expr) ;
3596 IF GccKnowsAbout (expr)
3597 THEN
3598 location := TokenToLocation (tokenno) ;
3599 e := ZConstToTypedConst (LValueToGenericPtr(location, expr), expr, des) ;
3600 zero := MakeAndDeclareConstLit (tokenno, MakeKey('0'), ZType) ;
3601 condition := BuildLessThanOrEqual (location, e, Mod2Gcc (zero)) ;
3602 AddStatement (location, BuildIfThenDoEnd (condition, CodeErrorCheck (r, function, message)))
3603 ELSE
3604 InternalError ('should have resolved expr')
3605 END
3606 END
3607 END CodeWholeNonPos ;
3608
3609
3610 (*
3611 CodeWholeZero - generates range check code for expr=0.
3612 *)
3613
3614 PROCEDURE CodeWholeZero (tokenno: CARDINAL;
3615 r: CARDINAL; function, message: String) ;
3616 VAR
3617 zero : CARDINAL ;
3618 p : Range ;
3619 condition,
3620 e : tree ;
3621 location : location_t ;
3622 BEGIN
3623 p := GetIndice(RangeIndex, r) ;
3624 WITH p^ DO
3625 TryDeclareConstant(tokenNo, expr) ;
3626 IF GccKnowsAbout(expr)
3627 THEN
3628 location := TokenToLocation(tokenno) ;
3629 e := ZConstToTypedConst(LValueToGenericPtr(location, expr), expr, des) ;
3630 zero := MakeAndDeclareConstLit (tokenno, MakeKey('0'), ZType) ;
3631 condition := BuildEqualTo(location,
3632 e, BuildConvert(location, GetTreeType(e), Mod2Gcc(zero), FALSE)) ;
3633 AddStatement(location, BuildIfThenDoEnd(condition, CodeErrorCheck(r, function, message)))
3634 ELSE
3635 InternalError ('should have resolved expr')
3636 END
3637 END
3638 END CodeWholeZero ;
3639
3640
3641 (*
3642 InitCaseBounds - creates a case bound range check.
3643 *)
3644
3645 PROCEDURE InitCaseBounds (b: CARDINAL) : CARDINAL ;
3646 VAR
3647 p: Range ;
3648 r: CARDINAL ;
3649 BEGIN
3650 r := InitRange() ;
3651 p := PutRangeNoEval(GetIndice(RangeIndex, r), casebounds) ;
3652 p^.caseList := b ;
3653 RETURN( r )
3654 END InitCaseBounds ;
3655
3656
3657 (*
3658 FillInParameters -
3659 *)
3660
3661 PROCEDURE FillInParameters (r: CARDINAL; s: String) : String ;
3662 VAR
3663 p: Range ;
3664 BEGIN
3665 p := GetIndice (RangeIndex, r) ;
3666 WITH p^ DO
3667 CASE type OF
3668
3669 assignment : s := MetaString3 (s, des, expr, dimension) |
3670 returnassignment : s := MetaString3 (s, des, expr, dimension) |
3671 subrangeassignment : InternalError ('unexpected case') |
3672 inc : s := MetaString3 (s, des, expr, dimension) |
3673 dec : s := MetaString3 (s, des, expr, dimension) |
3674 incl : s := MetaString3 (s, des, expr, dimension) |
3675 excl : s := MetaString3 (s, des, expr, dimension) |
3676 shift : s := MetaString3 (s, des, expr, dimension) |
3677 rotate : s := MetaString3 (s, des, expr, dimension) |
3678 typeassign : |
3679 typeparam : |
3680 typeexpr : |
3681 paramassign : s := MetaString3 (s, des, expr, paramNo) |
3682 staticarraysubscript : s := MetaString3 (s, des, expr, dimension) |
3683 dynamicarraysubscript: s := MetaString3 (s, des, expr, dimension) |
3684 forloopbegin : s := MetaString3 (s, des, expr, dimension) |
3685 forloopto : s := MetaString3 (s, des, expr, dimension) |
3686 forloopend : s := MetaString3 (s, des, expr, dimension) |
3687 pointernil : s := MetaString3 (s, des, expr, dimension) |
3688 noreturn : s := MetaString3 (s, des, expr, dimension) |
3689 noelse : s := MetaString3 (s, des, expr, dimension) |
3690 casebounds : s := MetaString3 (s, des, expr, dimension) |
3691 wholenonposdiv : s := MetaString3 (s, des, expr, dimension) |
3692 wholenonposmod : s := MetaString3 (s, des, expr, dimension) |
3693 wholezerodiv : s := MetaString3 (s, des, expr, dimension) |
3694 wholezerorem : s := MetaString3 (s, des, expr, dimension) |
3695 none : |
3696
3697 ELSE
3698 InternalError ('unexpected case')
3699 END
3700 END ;
3701 RETURN s
3702 END FillInParameters ;
3703
3704
3705 (*
3706 GetRangeErrorMessage - returns a specific error message for the range, r.
3707 It assumes the 3 parameters to be supplied on the MetaError
3708 parameter list are: dest, expr, paramNo or dimension.
3709
3710 XYZ
3711 'the initial assignment to {%1a} at the start of the FOR loop will cause a range error, as the type range of {%1taD} does not overlap with {%2tad}')
3712 'the final TO value {%2a} of the FOR loop will cause a range error with the iterator variable {%1a}')
3713 *)
3714
3715 PROCEDURE GetRangeErrorMessage (r: CARDINAL) : String ;
3716 VAR
3717 p: Range ;
3718 s: String ;
3719 BEGIN
3720 p := GetIndice (RangeIndex, r) ;
3721 WITH p^ DO
3722 CASE type OF
3723
3724 assignment : s := InitString ('assignment will cause a range error, as the runtime instance value of {%1tad} does not overlap with the type {%2tad}') |
3725 returnassignment : s := InitString ('attempting to return {%2Wa} from a procedure function {%1a} which will exceed exceed the range of type {%1tad}') |
3726 subrangeassignment : InternalError ('unexpected case') |
3727 inc : s := InitString ('if the INC is ever executed the expression {%2Wa} will cause an overflow error for the designator {%1a} as it exceeds the type range {%1ts:of {%1ts}}') |
3728 dec : s := InitString ('if the DEC is ever executed the expression {%2Wa} will cause an underflow error for the designator {%1a} as it exceeds the type range {%1ts:of {%1ts}}') |
3729 incl : s := InitString ('the expression {%2Wa} given in the INCL exceeds the type range {%1ts} of the designator {%1a}') |
3730 excl : s := InitString ('the expression {%2Wa} given in the EXCL exceeds the type range {%1ts} of the designator {%1a}') |
3731 shift : s := InitString ('the expression {%2Wa} given in the second parameter to SHIFT exceeds the type range {%1ts} of the first parameter {%1a}') |
3732 rotate : s := InitString ('the expression {%2Wa} given in the second parameter to ROTATE exceeds the type range {%1ts} of the first parameter {%1a}') |
3733 typeassign : s := NIL |
3734 typeparam : s := NIL |
3735 typeexpr : s := NIL |
3736 typeindrx : s := InitString ('assignment between designator {%1ad} and {%2ad} is incompatible') |
3737 typereturn : s := InitString ('the value {%2ad} returned from procedure function {%1a} is type incompatible, expecting {%1tad} rather than a {%2tad}') |
3738 paramassign : s := InitString('if this call is executed then the actual parameter {%2Wa} will be out of range of the {%3N} formal parameter {%1a}') |
3739 staticarraysubscript : s := InitString('if this access to the static array {%1Wa:{%2a:{%1a}[{%2a}]}} is ever made then the index will be out of bounds in the {%3N} array subscript') |
3740 dynamicarraysubscript: s := InitString('if this access to the dynamic array {%1Wa:{%2a:{%1a}[{%2a}]}} is ever made then the index will be out of bounds in the {%3N} array subscript') |
3741 forloopbegin : s := InitString('if the assignment in this FOR loop is ever executed then the designator {%1Wa} will be exceed the type range {%1ts:of {%1ts}}') |
3742 forloopto : s := InitString('the final value {%2Wa} in this FOR loop will be out of bounds {%1ts:of type {%1ts}} if ever executed') |
3743 forloopend : s := InitString('the FOR loop will cause the designator {%1Wa} to be out of bounds when the BY value {%2a} is added') |
3744 pointernil : s := InitString('if this pointer value {%1Wa} is ever dereferenced it will cause an exception') |
3745 noreturn : s := InitString('{%1W:}this function will exit without executing a RETURN statement') |
3746 noelse : s := InitString('{%1W:}this CASE statement does not have an ELSE statement') |
3747 casebounds : s := InitString('{%1W:}this CASE statement has overlapping ranges') |
3748 wholenonposdiv : s := InitString('this division expression {%2Wa} will cause an exception as this divisor is less than or equal to zero') |
3749 wholenonposmod : s := InitString('this modulus expression {%2Wa} will cause an exception as this divisor is less than or equal to zero') |
3750 wholezerodiv : s := InitString('this division expression {%2Wa} will cause an exception as the divisor is zero') |
3751 wholezerorem : s := InitString('this remainder expression {%2Wa} will cause an exception as the divisor is zero') |
3752 none : s := NIL
3753
3754 ELSE
3755 InternalError ('unexpected case')
3756 END
3757 END ;
3758 RETURN s
3759 END GetRangeErrorMessage ;
3760
3761
3762 (*
3763 CodeRangeCheck - returns a Tree representing the code for a
3764 range test defined by, r.
3765 *)
3766
3767 PROCEDURE CodeRangeCheck (r: CARDINAL; function: String) ;
3768 VAR
3769 p : Range ;
3770 message: String ;
3771 BEGIN
3772 p := GetIndice (RangeIndex, r) ;
3773 message := GetRangeErrorMessage (r) ;
3774 WITH p^ DO
3775 CASE type OF
3776
3777 assignment : CodeAssignment (tokenNo, r, function, message) |
3778 returnassignment : CodeReturn (tokenNo, r, function, message) |
3779 subrangeassignment : InternalError ('unexpected case') |
3780 inc : CodeInc (tokenNo, r, function, message) |
3781 dec : CodeDec (tokenNo, r, function, message) |
3782 incl,
3783 excl : CodeInclExcl (tokenNo, r, function, message) |
3784 shift,
3785 rotate : CodeShiftRotate (tokenNo, r, function, message) |
3786 typeassign,
3787 typeparam,
3788 typeexpr,
3789 typeindrx,
3790 typereturn : CodeTypeCheck (tokenNo, r) |
3791 staticarraysubscript : CodeStaticArraySubscript (tokenNo, r, function, message) |
3792 dynamicarraysubscript: CodeDynamicArraySubscript (tokenNo, r, function, message) |
3793 forloopbegin : CodeForLoopBegin (tokenNo, r, function, message) |
3794 forloopto : CodeForLoopTo (tokenNo, r, function, message) |
3795 forloopend : CodeForLoopEnd (tokenNo, r, function, message) |
3796 pointernil : CodeNil (r, function, message) |
3797 noreturn : AddStatement (TokenToLocation (tokenNo), CodeErrorCheck (r, function, message)) |
3798 noelse : AddStatement (TokenToLocation (tokenNo), CodeErrorCheck (r, function, message)) |
3799 casebounds : CodeCaseBounds (tokenNo, caseList) |
3800 wholenonposdiv : CodeWholeNonPos (tokenNo, r, function, message) |
3801 wholenonposmod : CodeWholeNonPos (tokenNo, r, function, message) |
3802 wholezerodiv : CodeWholeZero (tokenNo, r, function, message) |
3803 wholezerorem : CodeWholeZero (tokenNo, r, function, message) |
3804 paramassign : CodeParameterAssign (tokenNo, r, function, message) |
3805 none :
3806
3807 ELSE
3808 InternalError ('unexpected case')
3809 END
3810 END
3811 END CodeRangeCheck ;
3812
3813
3814 (*
3815 AddVarRead - checks to see whether symbol, Sym, is
3816 a variable or a parameter and if so it
3817 then adds this quadruple to the variable
3818 list.
3819 *)
3820 (*
3821 PROCEDURE AddVarRead (sym: CARDINAL; quadNo: CARDINAL) ;
3822 BEGIN
3823 IF (sym#NulSym) AND IsVar(sym)
3824 THEN
3825 PutReadQuad(sym, GetMode(sym), quadNo)
3826 END
3827 END AddVarRead ;
3828 *)
3829
3830
3831 (*
3832 SubVarRead - checks to see whether symbol, Sym, is
3833 a variable or a parameter and if so it
3834 then removes this quadruple from the
3835 variable list.
3836 *)
3837
3838 (*
3839 PROCEDURE SubVarRead (sym: CARDINAL; quadNo: CARDINAL) ;
3840 BEGIN
3841 IF (sym#NulSym) AND IsVar(sym)
3842 THEN
3843 RemoveReadQuad(sym, GetMode(sym), quadNo)
3844 END
3845 END SubVarRead ;
3846 *)
3847
3848
3849 (*
3850 CheckRangeAddVariableRead - ensures that any references to reading
3851 variables used by this range check, r,
3852 at this, quadNo, are recorded in the
3853 symbol table.
3854 *)
3855
3856 (*
3857 PROCEDURE CheckRangeAddVariableRead (r: CARDINAL; quadNo: CARDINAL) ;
3858 VAR
3859 p: Range ;
3860 BEGIN
3861 p := GetIndice(RangeIndex, r) ;
3862 WITH p^ DO
3863 (* AddVarRead(des, quadNo) ; *)
3864 (* AddVarRead(expr, quadNo) *)
3865 END
3866 END CheckRangeAddVariableRead ;
3867 *)
3868
3869
3870 (*
3871 CheckRangeRemoveVariableRead - ensures that any references to reading
3872 variable at this quadNo are removed from
3873 the symbol table.
3874 *)
3875
3876 (*
3877 PROCEDURE CheckRangeRemoveVariableRead (r: CARDINAL; quadNo: CARDINAL) ;
3878 VAR
3879 p: Range ;
3880 BEGIN
3881 p := GetIndice(RangeIndex, r) ;
3882 WITH p^ DO
3883 (* SubVarRead(des, quadNo) ; *)
3884 (* SubVarRead(expr, quadNo) *)
3885 END
3886 END CheckRangeRemoveVariableRead ;
3887 *)
3888
3889
3890 (*
3891 WriteRangeCheck - displays debugging information about range, r.
3892 *)
3893
3894 PROCEDURE WriteRangeCheck (r: CARDINAL) ;
3895 VAR
3896 p: Range ;
3897 BEGIN
3898 p := GetIndice(RangeIndex, r) ;
3899 WITH p^ DO
3900 WriteString ('range ') ;
3901 WriteCard (r, 0) ;
3902 WriteString (' ') ;
3903 IF cancelled
3904 THEN
3905 WriteString ('cancelled ')
3906 END ;
3907 IF dependantid # 0
3908 THEN
3909 WriteString ('dep ') ;
3910 WriteCard (dependantid, 0) ;
3911 WriteString (' ')
3912 END ;
3913 CASE type OF
3914
3915 assignment : WriteString('assignment (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3916 returnassignment : WriteString('returnassignment (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3917 subrangeassignment : WriteString('subrangeassignment(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3918 inc : WriteString('inc(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3919 dec : WriteString('dec(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3920 incl : WriteString('incl(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3921 excl : WriteString('excl(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3922 shift : WriteString('shift(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3923 rotate : WriteString('rotate(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3924 typeexpr : WriteString('expr compatible (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3925 typeassign : WriteString('assignment compatible (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3926 typeindrx : WriteString('indrx compatible (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3927 typereturn : WriteString('return compatible (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3928 typeparam : WriteString('parameter compatible (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3929 paramassign : WriteString('parameter range (') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3930 staticarraysubscript : WriteString('staticarraysubscript(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3931 dynamicarraysubscript: WriteString('dynamicarraysubscript(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3932 forloopbegin : WriteString('forloopbegin(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3933 forloopto : WriteString('forloopto(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3934 forloopend : WriteString('forloopend(') ; WriteOperand(des) ; WriteString(', ') ; WriteOperand(expr) |
3935 pointernil : WriteString('pointernil(') ; WriteOperand(des) |
3936 noreturn : WriteString('noreturn(') |
3937 noelse : WriteString('noelse(') |
3938 casebounds : WriteString('casebounds(') ; WriteCase(caseList) |
3939 wholenonposdiv : WriteString('wholenonposdiv(') ; WriteOperand(expr) |
3940 wholenonposmod : WriteString('wholenonposmod(') ; WriteOperand(expr) |
3941 wholezerodiv : WriteString('wholezerodiv(') ; WriteOperand(expr) |
3942 wholezerorem : WriteString('wholezerorem(') ; WriteOperand(expr) |
3943 none : WriteString('none(') |
3944
3945 ELSE
3946 InternalError ('unknown case')
3947 END ;
3948 Write(')')
3949 END
3950 END WriteRangeCheck ;
3951
3952
3953 (*
3954 Init - initializes the modules global variables.
3955 *)
3956
3957 PROCEDURE Init ;
3958 BEGIN
3959 TopOfRange := 0 ;
3960 RangeIndex := InitIndex(1) ;
3961 BreakWhenRangeCreated (0) ; (* Disable the intereactive range watch. *)
3962 (* To examine the range when it is created run cc1gm2 from gdb
3963 and set a break point on gdbhook.
3964 (gdb) break gdbhook
3965 (gdb) run
3966 Now below interactively call BreakWhenRangeCreated with the symbol
3967 under investigation. *)
3968 gdbhook ;
3969 (* Now is the time to interactively call gdb, for example:
3970 (gdb) print BreakWhenRangeCreated (1234)
3971 (gdb) cont
3972 and you will arrive at gdbhook when this symbol is created. *)
3973 END Init ;
3974
3975
3976 BEGIN
3977 Init
3978 END M2Range.