Debian Codegraph @

gcc/m2/gm2-compiler/M2ALU.mod

raw
1 (* M2ALU.mod gcc implementation of the M2ALU module.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2ALU ;
23
24 (*
25 Title : M2ALU.mod
26 Author : Gaius Mulley
27 System : UNIX (gm2)
28 Date : Mon Jul 10 12:04:50 2000
29 Description: gcc implementation of the M2ALU module, this module provides an interface
30 between some of the Modula-2 front end optimization routines and tree
31 construction required so that efficient trees can be passed to gcc's
32 backend. M2ALU allows constant expressions to be calculated.
33 *)
34
35 FROM ASCII IMPORT nul ;
36 FROM SYSTEM IMPORT WORD, ADDRESS ;
37 FROM NameKey IMPORT KeyToCharStar, MakeKey, CharKey ;
38 FROM M2Error IMPORT InternalError, FlushErrors ;
39 FROM M2Debug IMPORT Assert ;
40 FROM Storage IMPORT ALLOCATE ;
41 FROM StringConvert IMPORT ostoi, bstoi, stoi, hstoi ;
42 FROM M2GCCDeclare IMPORT GetTypeMin, GetTypeMax, CompletelyResolved, DeclareConstant ;
43 FROM M2GenGCC IMPORT PrepareCopyString, StringToChar ;
44 FROM M2Bitset IMPORT Bitset ;
45 FROM SymbolConversion IMPORT Mod2Gcc, GccKnowsAbout ;
46 FROM M2Printf IMPORT printf0, printf2 ;
47 FROM M2Base IMPORT MixTypes, GetBaseTypeMinMax, Char, IsRealType, IsComplexType, ZType ;
48 FROM DynamicStrings IMPORT String, InitString, Mark, ConCat, Slice, InitStringCharStar, KillString, InitStringChar, string ;
49 FROM M2LexBuf IMPORT TokenToLineNo, FindFileNameFromToken, TokenToLocation ;
50 FROM M2MetaError IMPORT MetaError0, MetaError1, MetaError2, MetaErrorStringT0,
51 MetaErrorT0, MetaErrorT1, MetaErrorT2, MetaErrorT3 ;
52
53 FROM SymbolTable IMPORT NulSym, IsEnumeration, IsSubrange, IsValueSolved, PushValue,
54 ForeachFieldEnumerationDo, MakeTemporary, PutVar, PopValue, GetType,
55 MakeConstLit, GetArraySubscript,
56 IsSet, SkipType, IsRecord, IsArray, IsConst, IsConstructor,
57 IsConstString, SkipTypeAndSubrange, GetDeclaredMod,
58 GetSubrange, GetSymName, GetNth, GetString, GetStringLength,
59 ModeOfAddr ;
60
61 IMPORT DynamicStrings ;
62
63 FROM gcctypes IMPORT location_t, tree ;
64 FROM m2linemap IMPORT UnknownLocation ;
65
66 FROM m2expr IMPORT BuildAdd, BuildSub, BuildMult,
67 BuildDivTrunc, BuildModTrunc, BuildDivFloor, BuildModFloor,
68 BuildLSL, BuildLSR,
69 BuildLogicalOr, BuildLogicalAnd, BuildSymmetricDifference,
70 GetWordOne, GetCardinalZero, TreeOverflow, RemoveOverflow,
71 GetCstInteger ;
72
73 FROM m2decl IMPORT GetBitsPerBitset, BuildIntegerConstant, BuildConstLiteralNumber ;
74 FROM m2misc IMPORT DebugTree ;
75
76 FROM m2type IMPORT RealToTree, Constructor, GetIntegerType, GetLongRealType,
77 BuildStartSetConstructor, BuildSetConstructorElement, BuildEndSetConstructor,
78 BuildRecordConstructorElement, BuildEndRecordConstructor, BuildStartRecordConstructor,
79 BuildNumberOfArrayElements, BuildCharConstant, BuildCharConstantChar,
80 BuildArrayConstructorElement, BuildStartArrayConstructor, BuildEndArrayConstructor,
81 GetM2CharType ;
82
83 FROM m2convert IMPORT ConvertConstantAndCheck, ToWord, ToInteger, ToCardinal, ToBitset ;
84 FROM m2block IMPORT RememberConstant ;
85
86 FROM m2expr IMPORT GetPointerZero, GetIntegerZero, GetIntegerOne,
87 CompareTrees, FoldAndStrip, AreRealOrComplexConstantsEqual, AreConstantsEqual ;
88
89
90 TYPE
91 cellType = (none, integer, real, complex, set, constructor, array, record) ;
92
93
94 CONST
95 Debugging = FALSE ;
96 DebugGarbage = TRUE ;
97
98 TYPE
99 listOfRange = POINTER TO rList ;
100 rList = RECORD
101 low, high: CARDINAL ; (* symbol table *)
102 next : listOfRange ;
103 END ;
104
105 listOfFields = POINTER TO fList ;
106 fList = RECORD
107 field : CARDINAL ; (* symbol table *)
108 next : listOfFields ;
109 END ;
110
111 listOfElements = POINTER TO eList ;
112 eList = RECORD
113 element : CARDINAL ; (* symbol table *)
114 by : CARDINAL ; (* symbol table *)
115 next : listOfElements ;
116 END ;
117
118 PtrToValue = POINTER TO cell ;
119 cell = RECORD
120 location : location_t ;
121 areAllConstants,
122 solved : BOOLEAN ;
123 constructorType: CARDINAL ;
124 next : PtrToValue ;
125 numberValue : tree ;
126
127 CASE type: cellType OF
128
129 none,
130 integer, real,
131 complex : |
132 set : setValue : listOfRange |
133 constructor,
134 record : fieldValues: listOfFields |
135 array : arrayValues: listOfElements
136
137 END
138 END ;
139
140 DoSetProcedure = PROCEDURE (CARDINAL, listOfRange, listOfRange) : listOfRange ;
141
142
143 VAR
144 ElementFreeList : listOfElements ;
145 FieldFreeList : listOfFields ;
146 RangeFreeList : listOfRange ;
147 FreeList,
148 TopOfStack : PtrToValue ;
149 EnumerationValue: tree ;
150 EnumerationField: CARDINAL ;
151 CurrentTokenNo : CARDINAL ;
152 (* WatchedValue : PtrToValue ; *)
153
154
155 (*
156 New - allocate a PtrToValue. Firstly check the FreeList, if empty call upon New.
157 *)
158
159 PROCEDURE New () : PtrToValue ;
160 VAR
161 v: PtrToValue ;
162 BEGIN
163 IF FreeList=NIL
164 THEN
165 NEW (v)
166 ELSE
167 v := FreeList ;
168 FreeList := FreeList^.next
169 END ;
170 WITH v^ DO
171 numberValue := NIL
172 END ;
173 RETURN InitRecord (v)
174 END New ;
175
176
177 (*
178 InitRecord - initialize the non variant fields of, v. Return v.
179 *)
180
181 PROCEDURE InitRecord (v: PtrToValue) : PtrToValue ;
182 BEGIN
183 WITH v^ DO
184 location := UnknownLocation () ;
185 areAllConstants := FALSE ;
186 solved := FALSE ;
187 constructorType := NulSym ;
188 numberValue := NIL
189 END ;
190 RETURN v
191 END InitRecord ;
192
193
194 (*
195 NewRange - assigns, v, to a new area of memory.
196 *)
197
198 PROCEDURE NewRange (VAR v: listOfRange) ;
199 BEGIN
200 IF RangeFreeList=NIL
201 THEN
202 NEW(v) ;
203 IF v=NIL
204 THEN
205 InternalError ('out of memory error')
206 END
207 ELSE
208 v := RangeFreeList ;
209 RangeFreeList := RangeFreeList^.next
210 END
211 END NewRange ;
212
213
214 (*
215 DisposeRange - adds the list, v, to the free list.
216 *)
217
218 PROCEDURE DisposeRange (VAR v: listOfRange) ;
219 VAR
220 r: listOfRange ;
221 BEGIN
222 IF v#NIL
223 THEN
224 r := v ;
225 WHILE (r#NIL) AND (r^.next#NIL) DO
226 r := r^.next
227 END ;
228 IF r#NIL
229 THEN
230 r^.next := RangeFreeList
231 END ;
232 RangeFreeList := v ;
233 v := NIL
234 END
235 END DisposeRange ;
236
237
238 (*
239 IsOnFieldFreeList - returns TRUE if, r, is on the FieldFreeList.
240 *)
241
242 PROCEDURE IsOnFieldFreeList (r: listOfFields) : BOOLEAN ;
243 VAR
244 s: listOfFields ;
245 BEGIN
246 s := FieldFreeList ;
247 WHILE s#NIL DO
248 IF s=r
249 THEN
250 RETURN( TRUE )
251 ELSE
252 s := s^.next
253 END
254 END ;
255 RETURN( FALSE )
256 END IsOnFieldFreeList ;
257
258
259 (*
260 IsOnElementFreeList - returns TRUE if, r, is on the ElementFreeList.
261 *)
262
263 PROCEDURE IsOnElementFreeList (r: listOfElements) : BOOLEAN ;
264 VAR
265 s: listOfElements ;
266 BEGIN
267 s := ElementFreeList ;
268 WHILE s#NIL DO
269 IF s=r
270 THEN
271 RETURN( TRUE )
272 ELSE
273 s := s^.next
274 END
275 END ;
276 RETURN( FALSE )
277 END IsOnElementFreeList ;
278
279
280 (*
281 DisposeFields - adds the list, v, to the free list.
282 *)
283
284 PROCEDURE DisposeFields (VAR v: listOfFields) ;
285 VAR
286 r: listOfFields ;
287 BEGIN
288 IF v#NIL
289 THEN
290 r := v ;
291 WHILE r^.next#NIL DO
292 Assert(NOT IsOnFieldFreeList(r)) ;
293 r := r^.next
294 END ;
295 r^.next := FieldFreeList ;
296 FieldFreeList := v ;
297 v := NIL
298 END
299 END DisposeFields ;
300
301
302 (*
303 NewField - adds the list, v, to the free list.
304 *)
305
306 PROCEDURE NewField (VAR v: listOfFields) ;
307 BEGIN
308 IF FieldFreeList=NIL
309 THEN
310 NEW(v) ;
311 IF v=NIL
312 THEN
313 InternalError ('out of memory error')
314 END
315 ELSE
316 v := FieldFreeList ;
317 FieldFreeList := FieldFreeList^.next
318 END
319 END NewField ;
320
321
322 (*
323 NewElement - returns a new element record.
324 *)
325
326 PROCEDURE NewElement (VAR e: listOfElements) ;
327 BEGIN
328 IF ElementFreeList=NIL
329 THEN
330 NEW(e) ;
331 IF e=NIL
332 THEN
333 InternalError ('out of memory error')
334 END
335 ELSE
336 e := ElementFreeList ;
337 ElementFreeList := ElementFreeList^.next
338 END
339 END NewElement ;
340
341
342 (*
343 DisposeElements - returns the list, e, to the free list.
344 *)
345
346 PROCEDURE DisposeElements (VAR e: listOfElements) ;
347 VAR
348 r: listOfElements ;
349 BEGIN
350 IF e#NIL
351 THEN
352 r := e ;
353 WHILE r^.next#NIL DO
354 Assert(NOT IsOnElementFreeList(r)) ;
355 r := r^.next
356 END ;
357 r^.next := ElementFreeList ;
358 ElementFreeList := e ;
359 e := NIL
360 END
361 END DisposeElements ;
362
363
364 (*
365 CheckNotAlreadyOnFreeList - checks to see whether, v, is already on the free list
366 and aborts if this is the case.
367 *)
368
369 PROCEDURE CheckNotAlreadyOnFreeList (v: PtrToValue) ;
370 VAR
371 l: PtrToValue ;
372 BEGIN
373 IF DebugGarbage
374 THEN
375 l := FreeList ;
376 WHILE l#NIL DO
377 IF l=v
378 THEN
379 InternalError ('value is already on the free list')
380 END ;
381 l := l^.next
382 END
383 END
384 END CheckNotAlreadyOnFreeList ;
385
386
387 (*
388 CheckNotOnStack - checks to see whether, v, is already on the stack
389 and aborts if this is the case.
390 *)
391
392 PROCEDURE CheckNotOnStack (v: PtrToValue) ;
393 VAR
394 l: PtrToValue ;
395 BEGIN
396 IF DebugGarbage
397 THEN
398 l := TopOfStack ;
399 WHILE l#NIL DO
400 IF l=v
401 THEN
402 InternalError ('value is already on the stack')
403 END ;
404 l := l^.next
405 END
406 END
407 END CheckNotOnStack ;
408
409
410 (*
411 Dispose - place, v, onto the FreeList.
412 *)
413
414 PROCEDURE Dispose (v: PtrToValue) ;
415 BEGIN
416 CheckNotAlreadyOnFreeList(v) ;
417 CheckNotOnStack(v) ;
418 CASE v^.type OF
419
420 set : DisposeRange(v^.setValue) |
421 constructor,
422 record : DisposeFields(v^.fieldValues) |
423 array : DisposeElements(v^.arrayValues)
424
425 ELSE
426 END ;
427 v^.next := FreeList ;
428 FreeList := v
429 END Dispose ;
430
431
432 (*
433 AddRange - returns a ListOfRange which is prepended to the front of the current list.
434 *)
435
436 PROCEDURE AddRange (head: listOfRange; l, h: CARDINAL) : listOfRange ;
437 VAR
438 r: listOfRange ;
439 BEGIN
440 NewRange(r) ;
441 WITH r^ DO
442 low := l ;
443 high := h ;
444 next := head
445 END ;
446 RETURN( r )
447 END AddRange ;
448
449
450 (*
451 DupRange - duplicates and returns the list, t.
452 *)
453
454 PROCEDURE DupRange (r: listOfRange) : listOfRange ;
455 VAR
456 s: listOfRange ;
457 BEGIN
458 s := NIL ;
459 WHILE r#NIL DO
460 s := AddRange(s, r^.low, r^.high) ;
461 r := r^.next
462 END ;
463 RETURN( s )
464 END DupRange ;
465
466
467 (*
468 InitValue - initializes and returns a memory cell.
469 *)
470
471 PROCEDURE InitValue () : PtrToValue ;
472 VAR
473 v: PtrToValue ;
474 BEGIN
475 v := New() ;
476 IF v=NIL
477 THEN
478 InternalError ('out of memory error')
479 ELSE
480 WITH v^ DO
481 location := UnknownLocation () ;
482 type := none ;
483 areAllConstants := TRUE ;
484 solved := FALSE ;
485 next := NIL ;
486 constructorType := NulSym
487 END ;
488 RETURN( v )
489 END
490 END InitValue ;
491
492
493 (*
494 IsValueTypeNone - returns TRUE if the value on the top stack has no value.
495 *)
496
497 PROCEDURE IsValueTypeNone () : BOOLEAN ;
498 VAR
499 v: PtrToValue ;
500 BEGIN
501 v := Pop() ;
502 WITH v^ DO
503 IF type=none
504 THEN
505 Push(v) ;
506 RETURN( TRUE )
507 ELSE
508 Push(v) ;
509 RETURN( FALSE )
510 END
511 END
512 END IsValueTypeNone ;
513
514
515 (*
516 IsValueTypeInteger - returns TRUE if the value on the top stack is an integer.
517 *)
518
519 PROCEDURE IsValueTypeInteger () : BOOLEAN ;
520 VAR
521 v: PtrToValue ;
522 BEGIN
523 v := Pop() ;
524 WITH v^ DO
525 IF type=integer
526 THEN
527 Push(v) ;
528 RETURN( TRUE )
529 ELSE
530 Push(v) ;
531 RETURN( FALSE )
532 END
533 END
534 END IsValueTypeInteger ;
535
536
537 (*
538 IsValueTypeReal - returns TRUE if the value on the top stack is a real.
539 *)
540
541 PROCEDURE IsValueTypeReal () : BOOLEAN ;
542 VAR
543 v: PtrToValue ;
544 BEGIN
545 v := Pop() ;
546 WITH v^ DO
547 IF type=real
548 THEN
549 Push(v) ;
550 RETURN( TRUE )
551 ELSE
552 Push(v) ;
553 RETURN( FALSE )
554 END
555 END
556 END IsValueTypeReal ;
557
558
559 (*
560 IsValueTypeComplex - returns TRUE if the value on the top stack is a complex.
561 *)
562
563 PROCEDURE IsValueTypeComplex () : BOOLEAN ;
564 VAR
565 v: PtrToValue ;
566 BEGIN
567 v := Pop() ;
568 WITH v^ DO
569 IF type=complex
570 THEN
571 Push(v) ;
572 RETURN( TRUE )
573 ELSE
574 Push(v) ;
575 RETURN( FALSE )
576 END
577 END
578 END IsValueTypeComplex ;
579
580
581 (*
582 IsValueTypeSet - returns TRUE if the value on the top stack is a set.
583 *)
584
585 PROCEDURE IsValueTypeSet () : BOOLEAN ;
586 VAR
587 v: PtrToValue ;
588 BEGIN
589 v := Pop() ;
590 WITH v^ DO
591 IF type=set
592 THEN
593 Push(v) ;
594 RETURN( TRUE )
595 ELSE
596 Push(v) ;
597 RETURN( FALSE )
598 END
599 END
600 END IsValueTypeSet ;
601
602
603 (*
604 IsValueTypeConstructor - returns TRUE if the value on the top
605 stack is a constructor.
606 *)
607
608 PROCEDURE IsValueTypeConstructor () : BOOLEAN ;
609 VAR
610 v: PtrToValue ;
611 BEGIN
612 v := Pop() ;
613 WITH v^ DO
614 IF type=constructor
615 THEN
616 Push(v) ;
617 RETURN( TRUE )
618 ELSE
619 Push(v) ;
620 RETURN( FALSE )
621 END
622 END
623 END IsValueTypeConstructor ;
624
625
626 (*
627 IsValueTypeArray - returns TRUE if the value on the top stack is
628 an array.
629 *)
630
631 PROCEDURE IsValueTypeArray () : BOOLEAN ;
632 VAR
633 v: PtrToValue ;
634 BEGIN
635 v := Pop() ;
636 WITH v^ DO
637 IF type=array
638 THEN
639 Push(v) ;
640 RETURN( TRUE )
641 ELSE
642 Push(v) ;
643 RETURN( FALSE )
644 END
645 END
646 END IsValueTypeArray ;
647
648
649 (*
650 IsValueTypeRecord - returns TRUE if the value on the top stack is
651 a record.
652 *)
653
654 PROCEDURE IsValueTypeRecord () : BOOLEAN ;
655 VAR
656 v: PtrToValue ;
657 BEGIN
658 v := Pop() ;
659 WITH v^ DO
660 IF type=record
661 THEN
662 Push(v) ;
663 RETURN( TRUE )
664 ELSE
665 Push(v) ;
666 RETURN( FALSE )
667 END
668 END
669 END IsValueTypeRecord ;
670
671
672 (*
673 GetSetValueType - returns the set type on top of the ALU stack.
674 *)
675
676 PROCEDURE GetSetValueType () : CARDINAL ;
677 VAR
678 v: PtrToValue ;
679 BEGIN
680 v := Pop() ;
681 Push(v) ;
682 WITH v^ DO
683 IF type=set
684 THEN
685 RETURN( constructorType )
686 ELSE
687 InternalError ('expecting set type')
688 END
689 END
690 END GetSetValueType ;
691
692
693 (*
694 PushIntegerTree - pushes a gcc tree value onto the ALU stack.
695 *)
696
697 PROCEDURE PushIntegerTree (t: tree) ;
698 VAR
699 v: PtrToValue ;
700 BEGIN
701 v := InitValue() ;
702 WITH v^ DO
703 type := integer ;
704 numberValue := t ;
705 areAllConstants := TRUE ;
706 solved := TRUE
707 END ;
708 Push(v)
709 END PushIntegerTree ;
710
711
712 (*
713 PopIntegerTree - pops a gcc tree value from the ALU stack.
714 *)
715
716 PROCEDURE PopIntegerTree () : tree ;
717 VAR
718 v: PtrToValue ;
719 t: tree ;
720 BEGIN
721 v := Pop() ;
722 WITH v^ DO
723 IF type=integer
724 THEN
725 t := numberValue
726 ELSE
727 InternalError ('expecting type of constant to be a whole number')
728 END
729 END ;
730 Dispose(v) ;
731 RETURN( t )
732 END PopIntegerTree ;
733
734
735 (*
736 PushRealTree - pushes a gcc tree value onto the ALU stack.
737 *)
738
739 PROCEDURE PushRealTree (t: tree) ;
740 VAR
741 v: PtrToValue ;
742 BEGIN
743 v := New() ;
744 WITH v^ DO
745 type := real ;
746 numberValue := t ;
747 areAllConstants := TRUE ;
748 solved := TRUE
749 END ;
750 Push(v)
751 END PushRealTree ;
752
753
754 (*
755 PopRealTree - pops a gcc tree value from the ALU stack.
756 *)
757
758 PROCEDURE PopRealTree () : tree ;
759 VAR
760 v: PtrToValue ;
761 t: tree ;
762 BEGIN
763 v := Pop() ;
764 WITH v^ DO
765 IF type=real
766 THEN
767 t := numberValue
768 ELSE
769 InternalError ('expecting type of constant to be a real number')
770 END
771 END ;
772 Dispose(v) ;
773 RETURN( t )
774 END PopRealTree ;
775
776
777 (*
778 PushComplexTree - pushes a gcc tree value onto the ALU stack.
779 *)
780
781 PROCEDURE PushComplexTree (t: tree) ;
782 VAR
783 v: PtrToValue ;
784 BEGIN
785 v := New() ;
786 WITH v^ DO
787 type := complex ;
788 numberValue := t ;
789 areAllConstants := TRUE ;
790 solved := TRUE
791 END ;
792 Push(v)
793 END PushComplexTree ;
794
795
796 (*
797 PopComplexTree - pops a gcc tree value from the ALU stack.
798 *)
799
800 PROCEDURE PopComplexTree () : tree ;
801 VAR
802 v: PtrToValue ;
803 t: tree ;
804 BEGIN
805 v := Pop() ;
806 WITH v^ DO
807 IF type=complex
808 THEN
809 t := numberValue
810 ELSE
811 InternalError ('expecting type of constant to be a complex number')
812 END
813 END ;
814 Dispose(v) ;
815 RETURN( t )
816 END PopComplexTree ;
817
818
819 (*
820 PushSetTree - pushes a gcc tree onto the ALU stack.
821 The tree, t, is expected to contain a
822 word value. It is converted into a set
823 type (sym). Bit 0 maps onto MIN(sym).
824 *)
825
826 PROCEDURE PushSetTree (tokenno: CARDINAL;
827 t: tree; sym: CARDINAL) ;
828 VAR
829 v: PtrToValue ;
830 c,
831 i: INTEGER ;
832 r: listOfRange ;
833 l: location_t ;
834 BEGIN
835 l := TokenToLocation(tokenno) ;
836 r := NIL ;
837 i := 0 ;
838 WHILE (i<GetBitsPerBitset()) AND
839 (CompareTrees(GetIntegerZero(l), t)#0) DO
840 IF CompareTrees(GetIntegerOne(l),
841 BuildLogicalAnd(l, t, GetIntegerOne(l), FALSE))=0
842 THEN
843 PushCard(i) ;
844 c := Val(tokenno, SkipType(sym), PopIntegerTree()) ;
845 DeclareConstant(tokenno, c) ;
846 r := AddRange(r, c, c)
847 END ;
848 t := BuildLSR(l, t, GetIntegerOne(l), FALSE) ;
849 INC(i)
850 END ;
851 SortElements(tokenno, r) ;
852 CombineElements(tokenno, r) ;
853 v := New() ;
854 WITH v^ DO
855 location := l ;
856 type := set ;
857 constructorType := sym ;
858 areAllConstants := FALSE ;
859 solved := FALSE ;
860 setValue := r
861 END ;
862 Eval(tokenno, v) ;
863 Push(v)
864 END PushSetTree ;
865
866
867 (*
868 PopSetTree - pops a gcc tree from the ALU stack.
869 *)
870
871 PROCEDURE PopSetTree (tokenno: CARDINAL) : tree ;
872 VAR
873 v: PtrToValue ;
874 t: tree ;
875 BEGIN
876 v := Pop() ;
877 WITH v^ DO
878 IF type=set
879 THEN
880 Eval(tokenno, v) ;
881 IF NOT v^.solved
882 THEN
883 InternalError ('the set has not been resolved')
884 END ;
885 IF NOT v^.areAllConstants
886 THEN
887 InternalError ('the set must only contain constants')
888 END ;
889 t := ConstructSetConstant(tokenno, v)
890 ELSE
891 InternalError ('expecting type of constant to be a set')
892 END
893 END ;
894 Dispose(v) ;
895 RETURN( t )
896 END PopSetTree ;
897
898
899 (*
900 PopConstructorTree - returns a tree containing the compound literal.
901 *)
902
903 PROCEDURE PopConstructorTree (tokenno: CARDINAL) : tree ;
904 VAR
905 v: PtrToValue ;
906 t: tree ;
907 BEGIN
908 v := Pop() ;
909 WITH v^ DO
910 Eval(tokenno, v) ;
911 IF NOT v^.solved
912 THEN
913 InternalError ('the constructor has not been resolved')
914 END ;
915 CASE type OF
916
917 constructor: InternalError('expecting constructor to be resolved into specific type') |
918 array : t := ConstructArrayConstant(tokenno, v) |
919 record : t := ConstructRecordConstant(tokenno, v) |
920 set : t := ConstructSetConstant(tokenno, v)
921
922 ELSE
923 InternalError ('expecting type to be a constructor')
924 END
925 END ;
926 Dispose(v) ;
927 RETURN( t )
928 END PopConstructorTree ;
929
930
931 (*
932 Pop - pops and returns top element from the stack.
933 *)
934
935 PROCEDURE Pop () : PtrToValue ;
936 VAR
937 v: PtrToValue ;
938 BEGIN
939 IF TopOfStack=NIL
940 THEN
941 InternalError ('stack underflow error')
942 ELSE
943 v := TopOfStack ;
944 TopOfStack := TopOfStack^.next
945 END ;
946 CheckNotAlreadyOnFreeList(v) ;
947 RETURN( v )
948 END Pop ;
949
950
951 (*
952 Push - pushes the value onto the stack.
953 *)
954
955 PROCEDURE Push (v: PtrToValue) ;
956 BEGIN
957 CheckNotAlreadyOnFreeList(v) ;
958 CheckNotOnStack(v) ;
959 v^.next := TopOfStack ;
960 TopOfStack := v
961 END Push ;
962
963
964 (*
965 Reduce - remove the top element of the stack.
966 *)
967
968 PROCEDURE Reduce ;
969 BEGIN
970 Dispose (Pop ())
971 END Reduce ;
972
973
974 (*
975 PrintValue - debugging procedure to display the value on the top of the stack.
976 *)
977
978 PROCEDURE PrintValue ;
979 VAR
980 v: PtrToValue ;
981 BEGIN
982 v := Pop() ;
983 WITH v^ DO
984 IF type=integer
985 THEN
986 DebugTree(numberValue)
987 END
988 END ;
989 Push(v)
990 END PrintValue ;
991
992
993 (*
994 DupFields - duplicates the field list in order.
995 *)
996
997 PROCEDURE DupFields (f: listOfFields) : listOfFields ;
998 VAR
999 p, q, l: listOfFields ;
1000 BEGIN
1001 p := NIL ;
1002 l := NIL ;
1003 WHILE f#NIL DO
1004 NewField(q) ;
1005 IF p=NIL
1006 THEN
1007 p := q
1008 END ;
1009 q^.field := f^.field ;
1010 q^.next := NIL ;
1011 IF l#NIL
1012 THEN
1013 l^.next := q
1014 END ;
1015 l := q ;
1016 f := f^.next
1017 END ;
1018 RETURN( p )
1019 END DupFields ;
1020
1021
1022 (*
1023 DupElements - duplicates the array list in order.
1024 *)
1025
1026 PROCEDURE DupElements (f: listOfElements) : listOfElements ;
1027 VAR
1028 p, q, l: listOfElements ;
1029 BEGIN
1030 p := NIL ;
1031 l := NIL ;
1032 WHILE f#NIL DO
1033 NewElement(q) ;
1034 IF p=NIL
1035 THEN
1036 p := q
1037 END ;
1038 q^.element := f^.element ;
1039 q^.by := f^.by ;
1040 q^.next := NIL ;
1041 IF l#NIL
1042 THEN
1043 l^.next := q
1044 END ;
1045 l := q ;
1046 f := f^.next
1047 END ;
1048 RETURN( p )
1049 END DupElements ;
1050
1051
1052 (*
1053 PushFrom - pushes a copy of the contents of, v, onto stack.
1054 *)
1055
1056 PROCEDURE PushFrom (v: PtrToValue) ;
1057 VAR
1058 t: PtrToValue ;
1059 BEGIN
1060 CheckNotAlreadyOnFreeList(v) ;
1061 t := New() ; (* as it is a copy *)
1062 t^ := v^ ;
1063 CASE v^.type OF
1064
1065 set : t^.setValue := DupRange(v^.setValue) |
1066 constructor,
1067 record : t^.fieldValues := DupFields(v^.fieldValues) |
1068 array : t^.arrayValues := DupElements(v^.arrayValues)
1069
1070 ELSE
1071 END ;
1072 Push(t)
1073 END PushFrom ;
1074
1075
1076 (*
1077 PopInto - pops the top element from the stack and places it into, v.
1078 *)
1079
1080 PROCEDURE PopInto (v: PtrToValue) ;
1081 VAR
1082 t: PtrToValue ;
1083 BEGIN
1084 t := Pop() ;
1085 v^ := t^ ;
1086 CASE v^.type OF
1087
1088 set : t^.setValue := NIL |
1089 record,
1090 constructor: t^.fieldValues := NIL |
1091 array : t^.arrayValues := NIL |
1092 none,
1093 integer,
1094 real,
1095 complex : v^.numberValue := RememberConstant(FoldAndStrip(t^.numberValue))
1096
1097 ELSE
1098 InternalError ('not expecting this value')
1099 END ;
1100 Dispose(t)
1101 END PopInto ;
1102
1103
1104 (*
1105 PushCard - pushes a cardinal onto the stack.
1106 *)
1107
1108 PROCEDURE PushCard (c: CARDINAL) ;
1109 VAR
1110 v: PtrToValue ;
1111 BEGIN
1112 v := New() ;
1113 WITH v^ DO
1114 type := integer ;
1115 numberValue := BuildIntegerConstant(INTEGER(c)) ;
1116 areAllConstants := TRUE ;
1117 solved := TRUE
1118 END ;
1119 Push(v)
1120 END PushCard ;
1121
1122
1123 (*
1124 PushInt - pushes an integer onto the stack.
1125 *)
1126
1127 PROCEDURE PushInt (i: INTEGER) ;
1128 VAR
1129 v: PtrToValue ;
1130 BEGIN
1131 v := New() ;
1132 WITH v^ DO
1133 type := integer ;
1134 numberValue := BuildIntegerConstant(i) ;
1135 areAllConstants := TRUE ;
1136 solved := TRUE
1137 END ;
1138 Push(v)
1139 END PushInt ;
1140
1141
1142 (*
1143 PushChar - pushes a char onto the stack.
1144 *)
1145
1146 PROCEDURE PushChar (c: CHAR) ;
1147 VAR
1148 v: PtrToValue ;
1149 BEGIN
1150 v := New() ;
1151 WITH v^ DO
1152 type := integer ;
1153 numberValue := BuildIntegerConstant(ORD(c)) ;
1154 areAllConstants := TRUE ;
1155 solved := TRUE
1156 END ;
1157 Push(v)
1158 END PushChar ;
1159
1160
1161 (*
1162 PopChar - pops a char from the stack.
1163 *)
1164
1165 PROCEDURE PopChar (tokenno: CARDINAL) : CHAR ;
1166 VAR
1167 v : PtrToValue ;
1168 ch: CHAR ;
1169 BEGIN
1170 v := Pop () ;
1171 ch := 0C ;
1172 WITH v^ DO
1173 IF type = integer
1174 THEN
1175 ch := VAL (CHAR, GetCstInteger (numberValue))
1176 ELSE
1177 MetaErrorT0 (tokenno, '{%E}cannot convert constant to a CHAR')
1178 END
1179 END ;
1180 Push (v) ;
1181 RETURN ch
1182 END PopChar ;
1183
1184
1185 (*
1186 IsReal - returns TRUE if a is a REAL number.
1187 *)
1188
1189 PROCEDURE IsReal (a: DynamicStrings.String) : BOOLEAN ;
1190 BEGIN
1191 RETURN( DynamicStrings.Index(a, '.', 0)#-1 )
1192 END IsReal ;
1193
1194
1195 (*
1196 PushString - pushes the numerical value of the string onto the stack.
1197 *)
1198
1199 PROCEDURE PushString (tokenno: CARDINAL; s: Name; issueError: BOOLEAN) ;
1200 VAR
1201 ch : CHAR ;
1202 a, b : DynamicStrings.String ;
1203 length : CARDINAL ;
1204 location: location_t ;
1205 BEGIN
1206 a := DynamicStrings.InitStringCharStar (KeyToCharStar (s)) ;
1207 b := NIL ;
1208 length := DynamicStrings.Length (a) ;
1209 IF length>0
1210 THEN
1211 DEC (length) ;
1212 ch := DynamicStrings.char (a, length) ;
1213 location := TokenToLocation (tokenno) ;
1214 CASE ch OF
1215
1216 'H': (* hexadecimal *)
1217 b := DynamicStrings.Slice (a, 0, -1) ;
1218 PushIntegerTree (BuildConstLiteralNumber (location,
1219 DynamicStrings.string (b),
1220 16, issueError)) |
1221 'A': (* binary *)
1222 b := DynamicStrings.Slice (a, 0, -1) ;
1223 PushIntegerTree (BuildConstLiteralNumber (location,
1224 DynamicStrings.string (b),
1225 2, issueError)) |
1226 'C', (* --fixme-- question:
1227 should we type this as a char rather than an int? *)
1228 'B': (* octal *)
1229 b := DynamicStrings.Slice (a, 0, -1) ;
1230 PushIntegerTree (BuildConstLiteralNumber (location,
1231 DynamicStrings.string (b),
1232 8, issueError))
1233
1234 ELSE
1235 IF IsReal (a)
1236 THEN
1237 PushRealTree (RealToTree (KeyToCharStar (s)))
1238 ELSE
1239 PushIntegerTree (BuildConstLiteralNumber (location, KeyToCharStar (s),
1240 10, issueError))
1241 END
1242 END
1243 ELSE
1244 InternalError ('expecting constant literal')
1245 END ;
1246 a := DynamicStrings.KillString (a) ;
1247 b := DynamicStrings.KillString (b)
1248 END PushString ;
1249
1250
1251 (*
1252 IsSolvedGCC - returns TRUE if the value, sym, is solved.
1253 If TRUE then it also ensures this symbol is
1254 entered into the double book keeping table
1255 for GM2 <-> GCC.
1256 *)
1257
1258 PROCEDURE IsSolvedGCC (sym: CARDINAL) : BOOLEAN ;
1259 BEGIN
1260 IF IsValueSolved(sym)
1261 THEN
1262 IF NOT GccKnowsAbout(sym)
1263 THEN
1264 DeclareConstant(GetDeclaredMod(sym), sym)
1265 END ;
1266 RETURN( TRUE )
1267 ELSE
1268 RETURN( FALSE )
1269 END
1270 END IsSolvedGCC ;
1271
1272
1273 (*
1274 CoerseLongRealToCard - performs a coersion between a REAL to a CARDINAL
1275 *)
1276
1277 PROCEDURE CoerseLongRealToCard ;
1278 VAR
1279 v: PtrToValue ;
1280 BEGIN
1281 v := Pop() ;
1282 WITH v^ DO
1283 IF type=real
1284 THEN
1285 numberValue := ConvertConstantAndCheck(location, GetIntegerType(), numberValue) ;
1286 type := integer ;
1287 areAllConstants := TRUE ;
1288 solved := TRUE
1289 ELSE
1290 InternalError ('expecting a REAL number')
1291 END
1292 END ;
1293 Push(v)
1294 END CoerseLongRealToCard ;
1295
1296
1297 (*
1298 ConvertRealToInt - converts a REAL into an INTEGER
1299 *)
1300
1301 PROCEDURE ConvertRealToInt ;
1302 VAR
1303 v: PtrToValue ;
1304 BEGIN
1305 v := Pop() ;
1306 WITH v^ DO
1307 IF type=real
1308 THEN
1309 numberValue := ConvertConstantAndCheck(location, GetIntegerType(), numberValue) ;
1310 type := integer ;
1311 areAllConstants := TRUE ;
1312 solved := TRUE
1313 ELSE
1314 InternalError ('expecting a REAL number')
1315 END
1316 END ;
1317 Push(v)
1318 END ConvertRealToInt ;
1319
1320
1321 (*
1322 ConvertIntToReal - converts a INTEGER into a LONGREAL
1323 *)
1324
1325 PROCEDURE ConvertIntToReal ;
1326 VAR
1327 v: PtrToValue ;
1328 BEGIN
1329 v := Pop() ;
1330 WITH v^ DO
1331 IF type=integer
1332 THEN
1333 numberValue := ConvertConstantAndCheck(location, GetLongRealType(), numberValue) ;
1334 type := real ;
1335 areAllConstants := TRUE ;
1336 solved := TRUE
1337 ELSE
1338 InternalError ('expecting an INTEGER number')
1339 END
1340 END ;
1341 Push(v)
1342 END ConvertIntToReal ;
1343
1344
1345 (*
1346 ConvertToInt - converts the value into an INTEGER. This should be used
1347 if we are computing the number of elements in a char set to
1348 avoid an overflow.
1349 *)
1350
1351 PROCEDURE ConvertToInt ;
1352 VAR
1353 v: PtrToValue ;
1354 BEGIN
1355 v := Pop() ;
1356 WITH v^ DO
1357 IF type=integer
1358 THEN
1359 numberValue := ConvertConstantAndCheck(location, GetIntegerType(), numberValue) ;
1360 solved := TRUE ;
1361 areAllConstants := TRUE
1362 ELSE
1363 InternalError ('expecting an INTEGER number')
1364 END
1365 END ;
1366 Push(v)
1367 END ConvertToInt ;
1368
1369
1370 (*
1371 ConvertToType - converts the top of stack to type, t.
1372 *)
1373
1374 PROCEDURE ConvertToType (t: CARDINAL) ;
1375 VAR
1376 v: PtrToValue ;
1377 BEGIN
1378 v := Pop() ;
1379 IF t#NulSym
1380 THEN
1381 WITH v^ DO
1382 IF type=integer
1383 THEN
1384 numberValue := ConvertConstantAndCheck(location, Mod2Gcc(t), numberValue) ;
1385 solved := TRUE ;
1386 areAllConstants := TRUE
1387 ELSE
1388 InternalError ('expecting an INTEGER number')
1389 END
1390 END
1391 END ;
1392 Push(v)
1393 END ConvertToType ;
1394
1395
1396
1397 (*
1398 IsSolved - returns true if the memory cell indicated by v
1399 has a known value.
1400 *)
1401
1402 PROCEDURE IsSolved (v: PtrToValue) : BOOLEAN ;
1403 BEGIN
1404 IF v=NIL
1405 THEN
1406 InternalError ('uninitialized value')
1407 ELSE
1408 RETURN( v^.solved )
1409 END
1410 END IsSolved ;
1411
1412
1413 (*
1414 IsValueConst - returns true if the memory cell indicated by v
1415 is only defined by constants. For example
1416 no variables are used in the constructor.
1417 *)
1418
1419 PROCEDURE IsValueConst (v: PtrToValue) : BOOLEAN ;
1420 BEGIN
1421 IF v=NIL
1422 THEN
1423 InternalError ('uninitialized value')
1424 ELSE
1425 RETURN( v^.areAllConstants )
1426 END
1427 END IsValueConst ;
1428
1429
1430 (*
1431 EitherReal - returns TRUE if either, Op1, or, Op2, are Real.
1432 *)
1433
1434 PROCEDURE EitherReal (Op1, Op2: PtrToValue) : BOOLEAN ;
1435 BEGIN
1436 RETURN( (Op1^.type=real) OR (Op2^.type=real) )
1437 END EitherReal ;
1438
1439
1440 (*
1441 EitherComplex - returns TRUE if either, Op1, or, Op2, are Real.
1442 *)
1443
1444 PROCEDURE EitherComplex (Op1, Op2: PtrToValue) : BOOLEAN ;
1445 BEGIN
1446 RETURN( (Op1^.type=complex) OR (Op2^.type=complex) )
1447 END EitherComplex ;
1448
1449
1450 (*
1451 Add - adds the top two elements on the stack.
1452
1453 The Stack:
1454
1455 Entry Exit
1456
1457 Ptr ->
1458 +------------+
1459 | Op1 | <- Ptr
1460 |------------| +------------+
1461 | Op2 | | Op2 + Op1 |
1462 |------------| |------------|
1463 *)
1464
1465 PROCEDURE Addn ;
1466 VAR
1467 Temp,
1468 Op1, Op2: PtrToValue ;
1469 BEGIN
1470 Op1 := Pop() ;
1471 Op2 := Pop() ;
1472 IF EitherReal(Op1, Op2)
1473 THEN
1474 RealAdd(Op1, Op2)
1475 ELSIF EitherComplex(Op1, Op2)
1476 THEN
1477 ComplexAdd(Op1, Op2)
1478 ELSE
1479 Temp := New() ; (* as it is a temp *)
1480 WITH Temp^ DO
1481 location := Op1^.location ;
1482 type := integer ;
1483 numberValue := BuildAdd(location, Op1^.numberValue, Op2^.numberValue, FALSE) ;
1484 solved := TRUE
1485 END ;
1486 Push(Temp)
1487 END ;
1488 Dispose(Op1) ;
1489 Dispose(Op2)
1490 END Addn ;
1491
1492
1493 (*
1494 RealAdd - adds two numbers. One of which is a Real.
1495 *)
1496
1497 PROCEDURE RealAdd (Op1, Op2: PtrToValue) ;
1498 VAR
1499 Temp: PtrToValue ;
1500 BEGIN
1501 IF Op1^.type=integer
1502 THEN
1503 Push(Op1) ;
1504 ConvertIntToReal ;
1505 Op1 := Pop()
1506 END ;
1507 IF Op2^.type=integer
1508 THEN
1509 Push(Op2) ;
1510 ConvertIntToReal ;
1511 Op2 := Pop()
1512 END ;
1513 Temp := New() ;
1514 WITH Temp^ DO
1515 location := Op1^.location ;
1516 numberValue := BuildAdd(location, Op1^.numberValue, Op2^.numberValue, FALSE) ;
1517 type := real ;
1518 solved := TRUE
1519 END ;
1520 Push(Temp)
1521 END RealAdd ;
1522
1523
1524 (*
1525 ComplexAdd - adds two complex numbers.
1526 *)
1527
1528 PROCEDURE ComplexAdd (Op1, Op2: PtrToValue) ;
1529 VAR
1530 Temp: PtrToValue ;
1531 BEGIN
1532 IF (Op1^.type=complex) AND (Op2^.type=complex)
1533 THEN
1534 Temp := New() ;
1535 WITH Temp^ DO
1536 location := Op1^.location ;
1537 numberValue := BuildAdd(location, Op1^.numberValue, Op2^.numberValue, FALSE) ;
1538 type := complex ;
1539 solved := TRUE
1540 END ;
1541 Push(Temp)
1542 ELSE
1543 InternalError ('expecting both operands to be of type COMPLEX')
1544 END
1545 END ComplexAdd ;
1546
1547
1548 (*
1549 Sub - subtracts the top two elements on the stack.
1550
1551 The Stack:
1552
1553 Entry Exit
1554
1555 Ptr ->
1556 +------------+
1557 | Op1 | <- Ptr
1558 |------------| +------------+
1559 | Op2 | | Op2 - Op1 |
1560 |------------| |------------|
1561 *)
1562
1563 PROCEDURE Sub ;
1564 VAR
1565 Temp,
1566 Op1, Op2: PtrToValue ;
1567 BEGIN
1568 Op1 := Pop() ;
1569 Op2 := Pop() ;
1570 IF EitherReal(Op1, Op2)
1571 THEN
1572 RealSub(Op1, Op2)
1573 ELSIF EitherComplex(Op1, Op2)
1574 THEN
1575 ComplexSub(Op1, Op2)
1576 ELSE
1577 Temp := New() ; (* as it is a temp *)
1578 WITH Temp^ DO
1579 location := Op1^.location ;
1580 type := integer ;
1581 numberValue := BuildSub(location, Op2^.numberValue, Op1^.numberValue, TRUE) ;
1582 solved := TRUE
1583 END ;
1584 Push(Temp)
1585 END ;
1586 Dispose(Op1) ;
1587 Dispose(Op2)
1588 END Sub ;
1589
1590
1591 (*
1592 RealSub - subtracts two numbers. One of which is a Real.
1593 *)
1594
1595 PROCEDURE RealSub (Op1, Op2: PtrToValue) ;
1596 VAR
1597 Temp: PtrToValue ;
1598 BEGIN
1599 IF Op1^.type=integer
1600 THEN
1601 Push(Op1) ;
1602 ConvertIntToReal ;
1603 Op1 := Pop()
1604 END ;
1605 IF Op2^.type=integer
1606 THEN
1607 Push(Op2) ;
1608 ConvertIntToReal ;
1609 Op2 := Pop()
1610 END ;
1611 Temp := New() ;
1612 WITH Temp^ DO
1613 location := Op1^.location ;
1614 numberValue := BuildSub(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1615 type := real ;
1616 solved := TRUE
1617 END ;
1618 Push(Temp)
1619 END RealSub ;
1620
1621
1622 (*
1623 ComplexSub - subtracts two complex numbers.
1624 *)
1625
1626 PROCEDURE ComplexSub (Op1, Op2: PtrToValue) ;
1627 VAR
1628 Temp: PtrToValue ;
1629 BEGIN
1630 IF (Op1^.type=complex) AND (Op2^.type=complex)
1631 THEN
1632 Temp := New() ;
1633 WITH Temp^ DO
1634 location := Op1^.location ;
1635 numberValue := BuildSub(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1636 type := complex ;
1637 solved := TRUE
1638 END ;
1639 Push(Temp)
1640 ELSE
1641 InternalError ('expecting both operands to be of type COMPLEX')
1642 END
1643 END ComplexSub ;
1644
1645
1646 (*
1647 Mult - multiplies the top two elements on the stack.
1648
1649 The Stack:
1650
1651 Entry Exit
1652
1653 Ptr ->
1654 +------------+
1655 | Op1 | <- Ptr
1656 |------------| +------------+
1657 | Op2 | | Op2 * Op1 |
1658 |------------| |------------|
1659 *)
1660
1661 PROCEDURE Multn ;
1662 VAR
1663 Temp,
1664 Op1, Op2: PtrToValue ;
1665 BEGIN
1666 Op1 := Pop() ;
1667 Op2 := Pop() ;
1668 IF EitherReal(Op1, Op2)
1669 THEN
1670 RealMult(Op1, Op2)
1671 ELSIF EitherComplex(Op1, Op2)
1672 THEN
1673 ComplexMult(Op1, Op2)
1674 ELSE
1675 Temp := New() ; (* as it is a temp *)
1676 WITH Temp^ DO
1677 location := Op1^.location ;
1678 type := integer ;
1679 numberValue := BuildMult(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1680 solved := TRUE
1681 END ;
1682 Push(Temp)
1683 END ;
1684 Dispose(Op1) ;
1685 Dispose(Op2)
1686 END Multn ;
1687
1688
1689 (*
1690 RealMult - multiplies two numbers. One of which is a Real.
1691 *)
1692
1693 PROCEDURE RealMult (Op1, Op2: PtrToValue) ;
1694 VAR
1695 Temp: PtrToValue ;
1696 BEGIN
1697 IF Op1^.type=integer
1698 THEN
1699 Push(Op1) ;
1700 ConvertIntToReal ;
1701 Op1 := Pop()
1702 END ;
1703 IF Op2^.type=integer
1704 THEN
1705 Push(Op2) ;
1706 ConvertIntToReal ;
1707 Op2 := Pop()
1708 END ;
1709 Temp := New() ; (* as it is a temp *)
1710 WITH Temp^ DO
1711 location := Op1^.location ;
1712 numberValue := BuildMult(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1713 type := real ;
1714 solved := TRUE
1715 END ;
1716 Push(Temp)
1717 END RealMult ;
1718
1719
1720 (*
1721 ComplexMult - multiplies two complex numbers.
1722 *)
1723
1724 PROCEDURE ComplexMult (Op1, Op2: PtrToValue) ;
1725 VAR
1726 Temp: PtrToValue ;
1727 BEGIN
1728 IF (Op1^.type=complex) AND (Op2^.type=complex)
1729 THEN
1730 Temp := New() ;
1731 WITH Temp^ DO
1732 location := Op1^.location ;
1733 numberValue := BuildMult(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1734 type := complex ;
1735 solved := TRUE
1736 END ;
1737 Push(Temp)
1738 ELSE
1739 InternalError ('expecting both operands to be of type COMPLEX')
1740 END
1741 END ComplexMult ;
1742
1743
1744 (*
1745 DivTrunc - divides the top two elements on the stack.
1746
1747 The Stack:
1748
1749 Entry Exit
1750
1751 Ptr ->
1752 +------------+
1753 | Op1 | <- Ptr
1754 |------------| +--------------+
1755 | Op2 | | Op2 DIV Op1 |
1756 |------------| |--------------|
1757 *)
1758
1759 PROCEDURE DivTrunc ;
1760 VAR
1761 Temp,
1762 Op1, Op2: PtrToValue ;
1763 BEGIN
1764 Op1 := Pop() ;
1765 Op2 := Pop() ;
1766 IF EitherReal(Op1, Op2)
1767 THEN
1768 RealDiv(Op1, Op2)
1769 ELSIF EitherComplex(Op1, Op2)
1770 THEN
1771 ComplexDiv(Op1, Op2)
1772 ELSE
1773 Temp := New() ; (* as it is a temp *)
1774 WITH Temp^ DO
1775 location := Op1^.location ;
1776 type := integer ;
1777 numberValue := BuildDivTrunc(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1778 solved := TRUE
1779 END ;
1780 Push(Temp)
1781 END ;
1782 Dispose(Op1) ;
1783 Dispose(Op2)
1784 END DivTrunc ;
1785
1786
1787 (*
1788 DivFloor - divides the top two elements on the stack.
1789
1790 The Stack:
1791
1792 Entry Exit
1793
1794 Ptr ->
1795 +------------+
1796 | Op1 | <- Ptr
1797 |------------| +--------------+
1798 | Op2 | | Op2 DIV Op1 |
1799 |------------| |--------------|
1800 *)
1801
1802 PROCEDURE DivFloor ;
1803 VAR
1804 Temp,
1805 Op1, Op2: PtrToValue ;
1806 BEGIN
1807 Op1 := Pop() ;
1808 Op2 := Pop() ;
1809 IF EitherReal(Op1, Op2)
1810 THEN
1811 RealDiv(Op1, Op2)
1812 ELSIF EitherComplex(Op1, Op2)
1813 THEN
1814 ComplexDiv(Op1, Op2)
1815 ELSE
1816 Temp := New() ; (* as it is a temp *)
1817 WITH Temp^ DO
1818 location := Op1^.location ;
1819 type := integer ;
1820 numberValue := BuildDivFloor(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1821 solved := TRUE
1822 END ;
1823 Push(Temp)
1824 END ;
1825 Dispose(Op1) ;
1826 Dispose(Op2)
1827 END DivFloor ;
1828
1829
1830 (*
1831 RealDiv - divides two numbers. One of which is a Real.
1832 *)
1833
1834 PROCEDURE RealDiv (Op1, Op2: PtrToValue) ;
1835 VAR
1836 Temp: PtrToValue ;
1837 BEGIN
1838 IF Op1^.type=integer
1839 THEN
1840 Push(Op1) ;
1841 ConvertIntToReal ;
1842 Op1 := Pop()
1843 END ;
1844 IF Op2^.type=integer
1845 THEN
1846 Push(Op2) ;
1847 ConvertIntToReal ;
1848 Op2 := Pop()
1849 END ;
1850 Temp := New() ; (* as it is a temp *)
1851 WITH Temp^ DO
1852 location := Op1^.location ;
1853 numberValue := BuildDivTrunc(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1854 type := real ;
1855 solved := TRUE
1856 END ;
1857 Push(Temp)
1858 END RealDiv ;
1859
1860
1861 (*
1862 ComplexDiv - divides two complex numbers.
1863 *)
1864
1865 PROCEDURE ComplexDiv (Op1, Op2: PtrToValue) ;
1866 VAR
1867 Temp: PtrToValue ;
1868 BEGIN
1869 IF (Op1^.type=complex) AND (Op2^.type=complex)
1870 THEN
1871 Temp := New() ;
1872 WITH Temp^ DO
1873 location := Op1^.location ;
1874 numberValue := BuildDivTrunc(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1875 type := complex ;
1876 solved := TRUE
1877 END ;
1878 Push(Temp)
1879 ELSE
1880 InternalError ('expecting both operands to be of type COMPLEX')
1881 END
1882 END ComplexDiv ;
1883
1884
1885 (*
1886 ModFloor - modulus of the top two elements on the stack.
1887
1888 The Stack:
1889
1890 Entry Exit
1891
1892 Ptr ->
1893 +------------+
1894 | Op1 | <- Ptr
1895 |------------| +--------------+
1896 | Op2 | | Op2 MOD Op1 |
1897 |------------| |--------------|
1898 *)
1899
1900 PROCEDURE ModFloor ;
1901 VAR
1902 Temp,
1903 Op1, Op2: PtrToValue ;
1904 BEGIN
1905 Op1 := Pop() ;
1906 Op2 := Pop() ;
1907 IF EitherReal(Op1, Op2)
1908 THEN
1909 MetaError0 ('cannot perform {%EkMOD} on REAL types')
1910 ELSIF EitherComplex(Op1, Op2)
1911 THEN
1912 MetaError0 ('cannot perform {%EkMOD} on COMPLEX types')
1913 ELSE
1914 Temp := New() ; (* as it is a temp *)
1915 WITH Temp^ DO
1916 location := Op1^.location ;
1917 type := integer ;
1918 numberValue := BuildModFloor(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1919 solved := TRUE
1920 END ;
1921 Push(Temp)
1922 END ;
1923 Dispose(Op1) ;
1924 Dispose(Op2)
1925 END ModFloor ;
1926
1927
1928 (*
1929 ModTrunc - modulus of the top two elements on the stack.
1930
1931 The Stack:
1932
1933 Entry Exit
1934
1935 Ptr ->
1936 +------------+
1937 | Op1 | <- Ptr
1938 |------------| +--------------+
1939 | Op2 | | Op2 MOD Op1 |
1940 |------------| |--------------|
1941 *)
1942
1943 PROCEDURE ModTrunc ;
1944 VAR
1945 Temp,
1946 Op1, Op2: PtrToValue ;
1947 BEGIN
1948 Op1 := Pop() ;
1949 Op2 := Pop() ;
1950 IF EitherReal(Op1, Op2)
1951 THEN
1952 MetaError0 ('cannot perform {%EkMOD} on REAL types')
1953 ELSIF EitherComplex(Op1, Op2)
1954 THEN
1955 MetaError0 ('cannot perform {%EkMOD} on COMPLEX types')
1956 ELSE
1957 Temp := New() ; (* as it is a temp *)
1958 WITH Temp^ DO
1959 location := Op1^.location ;
1960 type := integer ;
1961 numberValue := BuildModTrunc(location, Op2^.numberValue, Op1^.numberValue, FALSE) ;
1962 solved := TRUE
1963 END ;
1964 Push(Temp)
1965 END ;
1966 Dispose(Op1) ;
1967 Dispose(Op2)
1968 END ModTrunc ;
1969
1970
1971 (*
1972 AreSetsEqual - returns TRUE if sets, op1, and, op2, contain the same
1973 members.
1974 *)
1975
1976 PROCEDURE AreSetsEqual (tokenno: CARDINAL; op1, op2: PtrToValue) : BOOLEAN ;
1977 VAR
1978 low1, low2,
1979 high1, high2: CARDINAL ;
1980 i : CARDINAL ;
1981 BEGIN
1982 i := 1 ;
1983 Eval(tokenno, op1) ;
1984 Eval(tokenno, op2) ;
1985 IF NOT (op1^.solved AND op2^.solved)
1986 THEN
1987 InternalError ('can only compare set values when they are known')
1988 END ;
1989 LOOP
1990 IF GetRange(op1, i, low1, high1)
1991 THEN
1992 IF GetRange(op2, i, low2, high2)
1993 THEN
1994 PushValue(low1) ;
1995 PushValue(low2) ;
1996 IF NotEqu(tokenno)
1997 THEN
1998 RETURN( FALSE )
1999 END ;
2000 PushValue(high1) ;
2001 PushValue(high2) ;
2002 IF NotEqu(tokenno)
2003 THEN
2004 RETURN( FALSE )
2005 END ;
2006 INC(i)
2007 ELSE
2008 (* op2 is out of ranges, but op1 still has >= 1 range left *)
2009 RETURN( FALSE )
2010 END
2011 ELSE
2012 IF GetRange(op2, i, low2, high2)
2013 THEN
2014 (* op1 is out of ranges, but op2 still has >= 1 range left *)
2015 RETURN( FALSE )
2016 ELSE
2017 (* both out of ranges and they were the same *)
2018 RETURN( TRUE )
2019 END
2020 END
2021 END
2022 END AreSetsEqual ;
2023
2024
2025 (*
2026 Equ - returns true if the top two elements on the stack
2027 are identical.
2028
2029 The Stack:
2030
2031 Entry Exit
2032
2033 Ptr ->
2034 +------------+
2035 | Op1 |
2036 |------------|
2037 | Op2 |
2038 |------------| Empty
2039
2040 RETURN( Op2 = Op1 )
2041 *)
2042
2043 PROCEDURE Equ (tokenno: CARDINAL) : BOOLEAN ;
2044 VAR
2045 Op1, Op2: PtrToValue ;
2046 result : BOOLEAN ;
2047 BEGIN
2048 Op1 := Pop() ;
2049 Op2 := Pop() ;
2050 IF (Op1^.type=set) AND (Op2^.type=set)
2051 THEN
2052 result := AreSetsEqual(tokenno, Op1, Op2)
2053 ELSIF (Op1^.type=set) OR (Op2^.type=set)
2054 THEN
2055 MetaErrorT0 (tokenno, 'cannot perform a comparison between a number and a set') ;
2056 result := FALSE
2057 ELSE
2058 IF Op1^.type#Op2^.type
2059 THEN
2060 MetaErrorT0 (tokenno, 'cannot perform a comparison between a different type constants') ;
2061 result := FALSE
2062 ELSIF (Op1^.type=complex) OR (Op1^.type=real)
2063 THEN
2064 result := AreRealOrComplexConstantsEqual(Op1^.numberValue, Op2^.numberValue)
2065 ELSE
2066 result := AreConstantsEqual(Op1^.numberValue, Op2^.numberValue)
2067 END
2068 END ;
2069 Dispose(Op1) ;
2070 Dispose(Op2) ;
2071 RETURN( result )
2072 END Equ ;
2073
2074
2075 (*
2076 NotEqu - returns true if the top two elements on the stack
2077 are not identical.
2078
2079 The Stack:
2080
2081 Entry Exit
2082
2083 Ptr ->
2084 +------------+
2085 | Op1 |
2086 |------------|
2087 | Op2 |
2088 |------------| Empty
2089
2090 RETURN( Op2 # Op1 )
2091 *)
2092
2093 PROCEDURE NotEqu (tokenno: CARDINAL) : BOOLEAN ;
2094 BEGIN
2095 RETURN( NOT Equ(tokenno) )
2096 END NotEqu ;
2097
2098
2099 (*
2100 Less - returns true if Op2 < Op1
2101
2102 The Stack:
2103
2104 Entry Exit
2105
2106 Ptr ->
2107 +------------+
2108 | Op1 |
2109 |------------|
2110 | Op2 |
2111 |------------| Empty
2112
2113 RETURN( Op2 < Op1 )
2114 *)
2115
2116 PROCEDURE Less (tokenno: CARDINAL) : BOOLEAN ;
2117 VAR
2118 v1, v2: PtrToValue ;
2119 result: BOOLEAN ;
2120 res : INTEGER ;
2121 BEGIN
2122 v1 := Pop () ;
2123 v2 := Pop () ;
2124 IF (v1^.type = set) AND (v2^.type = set)
2125 THEN
2126 result := NOT IsSuperset (tokenno, v2, v1)
2127 ELSIF (v1^.type = set) OR (v2^.type = set)
2128 THEN
2129 MetaErrorT0 (tokenno, 'cannot perform a comparison between a number and a set') ;
2130 result := FALSE
2131 ELSE
2132 res := CompareTrees (v2^.numberValue, v1^.numberValue) ;
2133 IF res = -1
2134 THEN
2135 result := TRUE
2136 ELSE
2137 result := FALSE
2138 END ;
2139 (* result := (CompareTrees(v2^.numberValue, v1^.numberValue)=-1) *)
2140 END ;
2141 Dispose (v1) ;
2142 Dispose (v2) ;
2143 RETURN result
2144 END Less ;
2145
2146
2147 (*
2148 Gre - returns true if Op2 > Op1
2149
2150 The Stack:
2151
2152 Entry Exit
2153
2154 Ptr ->
2155 +------------+
2156 | Op1 |
2157 |------------|
2158 | Op2 |
2159 |------------| Empty
2160
2161 RETURN( Op2 > Op1 )
2162 *)
2163
2164 PROCEDURE Gre (tokenno: CARDINAL) : BOOLEAN ;
2165 VAR
2166 v1, v2: PtrToValue ;
2167 result: BOOLEAN ;
2168 BEGIN
2169 v1 := Pop() ;
2170 v2 := Pop() ;
2171 IF (v1^.type=set) AND (v2^.type=set)
2172 THEN
2173 result := NOT IsSubset(tokenno, v2, v1)
2174 ELSIF (v1^.type=set) OR (v2^.type=set)
2175 THEN
2176 MetaErrorT0 (tokenno, 'cannot perform a comparison between a number and a set') ;
2177 FlushErrors ;
2178 result := FALSE
2179 ELSE
2180 result := (CompareTrees(v2^.numberValue, v1^.numberValue)=1)
2181 END ;
2182 Dispose(v1) ;
2183 Dispose(v2) ;
2184 RETURN( result )
2185 END Gre ;
2186
2187
2188 (*
2189 IsSubset - returns TRUE if the set as defined by, s1, is a subset of set, s2.
2190 *)
2191
2192 PROCEDURE IsSubset (tokenno: CARDINAL; s1, s2: PtrToValue) : BOOLEAN ;
2193 BEGIN
2194 Push(s1) ;
2195 Push(s2) ;
2196 SetAnd(tokenno) ;
2197 Push(s1) ;
2198 RETURN( Equ(tokenno) )
2199 END IsSubset ;
2200
2201
2202 (*
2203 LessEqu - returns true if Op2<Op1
2204
2205 The Stack:
2206
2207 Entry Exit
2208
2209 Ptr ->
2210 +------------+
2211 | Op1 |
2212 |------------|
2213 | Op2 |
2214 |------------| Empty
2215
2216 RETURN( Op2 <= Op1 )
2217 *)
2218
2219 PROCEDURE LessEqu (tokenno: CARDINAL) : BOOLEAN ;
2220 VAR
2221 v1, v2: PtrToValue ;
2222 result: BOOLEAN ;
2223 BEGIN
2224 v1 := Pop() ;
2225 v2 := Pop() ;
2226 IF (v1^.type=set) AND (v2^.type=set)
2227 THEN
2228 result := IsSubset(tokenno, v2, v1)
2229 ELSIF (v1^.type=set) OR (v2^.type=set)
2230 THEN
2231 MetaErrorT0 (tokenno, 'cannot perform a comparison between a number and a set') ;
2232 FlushErrors ;
2233 result := FALSE
2234 ELSE
2235 result := (CompareTrees(v2^.numberValue, v1^.numberValue)<=0)
2236 END ;
2237 Dispose(v1) ;
2238 Dispose(v2) ;
2239 RETURN( result )
2240 END LessEqu ;
2241
2242
2243 (*
2244 IsSuperset - returns TRUE if the set as defined by, s1, is a superset of set, s2.
2245 *)
2246
2247 PROCEDURE IsSuperset (tokenno: CARDINAL; s1, s2: PtrToValue) : BOOLEAN ;
2248 BEGIN
2249 PushFrom(s1) ;
2250 PushFrom(s2) ;
2251 SetAnd(tokenno) ;
2252 PushFrom(s2) ;
2253 RETURN( Equ(tokenno) )
2254 END IsSuperset ;
2255
2256
2257 (*
2258 GreEqu - returns true if Op2 >= Op1
2259 are not identical.
2260
2261 The Stack:
2262
2263 Entry Exit
2264
2265 Ptr ->
2266 +------------+
2267 | Op1 |
2268 |------------|
2269 | Op2 |
2270 |------------| Empty
2271
2272 RETURN( Op2 >= Op1 )
2273 *)
2274
2275 PROCEDURE GreEqu (tokenno: CARDINAL) : BOOLEAN ;
2276 VAR
2277 v1, v2: PtrToValue ;
2278 result: BOOLEAN ;
2279 BEGIN
2280 v1 := Pop() ;
2281 v2 := Pop() ;
2282 IF (v1^.type=set) AND (v2^.type=set)
2283 THEN
2284 result := IsSuperset(tokenno, v2, v1)
2285 ELSIF (v1^.type=set) OR (v2^.type=set)
2286 THEN
2287 MetaErrorT0 (tokenno, 'cannot perform a comparison between a number and a set') ;
2288 FlushErrors ;
2289 result := FALSE
2290 ELSE
2291 result := (CompareTrees(v2^.numberValue, v1^.numberValue)>=0)
2292 END ;
2293 Dispose(v1) ;
2294 Dispose(v2) ;
2295 RETURN( result )
2296 END GreEqu ;
2297
2298
2299 (*
2300 IsNulSet - returns TRUE if the top element is the nul set constant, {}.
2301 *)
2302
2303 PROCEDURE IsNulSet () : BOOLEAN ;
2304 VAR
2305 v: PtrToValue ;
2306 r: BOOLEAN ;
2307 BEGIN
2308 v := Pop() ;
2309 WITH v^ DO
2310 r := (type=set) AND (setValue=NIL)
2311 END ;
2312 Push(v) ;
2313 RETURN( r )
2314 END IsNulSet ;
2315
2316
2317 (*
2318 IsGenericNulSet - returns TRUE if the top element is the generic nul set constant, {}.
2319 *)
2320
2321 PROCEDURE IsGenericNulSet () : BOOLEAN ;
2322 VAR
2323 v: PtrToValue ;
2324 r: BOOLEAN ;
2325 BEGIN
2326 v := Pop() ;
2327 WITH v^ DO
2328 r := (type=set) AND (setValue=NIL) AND (constructorType=NulSym)
2329 END ;
2330 Push(v) ;
2331 RETURN( r )
2332 END IsGenericNulSet ;
2333
2334
2335 (*
2336 PushNulSet - pushes an empty set {} onto the ALU stack. The subrange type used
2337 to construct the set is defined by, constructorType.
2338 If this is NulSym then
2339 the set is generic and compatible with all sets.
2340
2341 The Stack:
2342
2343 Entry Exit
2344
2345 <- Ptr
2346 +------------+
2347 | {} |
2348 Ptr -> |------------|
2349
2350 *)
2351
2352 PROCEDURE PushNulSet (settype: CARDINAL) ;
2353 VAR
2354 v: PtrToValue ;
2355 BEGIN
2356 v := InitValue() ;
2357 WITH v^ DO
2358 type := set ;
2359 constructorType := settype ;
2360 areAllConstants := TRUE ;
2361 solved := CompletelyResolved(settype) ;
2362 setValue := NIL ;
2363 next := NIL ;
2364 END ;
2365 Push(v)
2366 END PushNulSet ;
2367
2368
2369 (*
2370 PushEmptyConstructor - pushes an empty constructor {} onto the ALU stack.
2371 This is expected to be filled in by subsequent
2372 calls to AddElements, AddRange or AddField.
2373
2374 The Stack:
2375
2376 Entry Exit
2377
2378 <- Ptr
2379 +------------+
2380 | {} |
2381 Ptr -> |------------|
2382
2383 *)
2384
2385 PROCEDURE PushEmptyConstructor (constype: CARDINAL) ;
2386 VAR
2387 v: PtrToValue ;
2388 BEGIN
2389 v := InitValue() ;
2390 WITH v^ DO
2391 type := constructor ;
2392 constructorType := constype ;
2393 areAllConstants := TRUE ;
2394 solved := CompletelyResolved(constype) ;
2395 fieldValues := NIL ;
2396 next := NIL ;
2397 END ;
2398 Push(v)
2399 END PushEmptyConstructor ;
2400
2401
2402 (*
2403 PushEmptyArray - pushes an empty array {} onto the ALU stack.
2404 This is expected to be filled in by subsequent
2405 calls to AddElements.
2406
2407 The Stack:
2408
2409 Entry Exit
2410
2411 <- Ptr
2412 +------------+
2413 | {} |
2414 Ptr -> |------------|
2415
2416 *)
2417
2418 PROCEDURE PushEmptyArray (arraytype: CARDINAL) ;
2419 VAR
2420 v: PtrToValue ;
2421 BEGIN
2422 v := InitValue() ;
2423 WITH v^ DO
2424 type := array ;
2425 constructorType := arraytype ;
2426 areAllConstants := TRUE ;
2427 solved := CompletelyResolved(arraytype) ;
2428 arrayValues := NIL ;
2429 next := NIL ;
2430 END ;
2431 Push(v)
2432 END PushEmptyArray ;
2433
2434
2435 (*
2436 PushEmptyRecord - pushes an empty record {} onto the ALU stack.
2437 This is expected to be filled in by subsequent
2438 calls to AddField.
2439
2440 The Stack:
2441
2442 Entry Exit
2443
2444 <- Ptr
2445 +------------+
2446 | {} |
2447 Ptr -> |------------|
2448
2449 *)
2450
2451 PROCEDURE PushEmptyRecord (recordtype: CARDINAL) ;
2452 VAR
2453 v: PtrToValue ;
2454 BEGIN
2455 v := InitValue() ;
2456 WITH v^ DO
2457 type := record ;
2458 constructorType := recordtype ;
2459 areAllConstants := TRUE ;
2460 solved := CompletelyResolved(recordtype) ;
2461 arrayValues := NIL ;
2462 next := NIL ;
2463 END ;
2464 Push(v)
2465 END PushEmptyRecord ;
2466
2467
2468 (*
2469 AddElements - adds the elements, el BY, n, to the array constant.
2470
2471 Ptr ->
2472 <- Ptr
2473 +------------+ +------------+
2474 | Array | | Array |
2475 |------------| |------------|
2476
2477 *)
2478
2479 PROCEDURE AddElements (tokenno: CARDINAL; el, n: CARDINAL) ;
2480 VAR
2481 v: PtrToValue ;
2482 e: listOfElements ;
2483 BEGIN
2484 v := Pop() ;
2485 v := CoerseTo(tokenno, array, v) ;
2486 IF v^.type=array
2487 THEN
2488 NewElement(e) ;
2489 WITH e^ DO
2490 element := el ;
2491 by := n ;
2492 next := NIL
2493 END ;
2494 AddElementToEnd(v, e) ;
2495 WITH v^ DO
2496 solved := solved AND IsSolvedGCC(el) AND IsSolvedGCC(n)
2497 END
2498 ELSE
2499 InternalError ('expecting array type')
2500 END ;
2501 Push(v)
2502 END AddElements ;
2503
2504
2505 (*
2506 AddElement -
2507
2508 PROCEDURE AddElement (v: listOfElements;
2509 e, b: CARDINAL) : listOfElements ;
2510 VAR
2511 el: listOfElements ;
2512 BEGIN
2513 NEW(el) ;
2514 IF el=NIL
2515 THEN
2516 InternalError ('out of memory')
2517 END ;
2518 (* held in reverse order here *)
2519 WITH el^ DO
2520 element := e ;
2521 by := b ;
2522 next := v^.next
2523 END ;
2524 v^.next := el ;
2525 RETURN( v )
2526 END AddElement ;
2527 *)
2528
2529
2530 (*
2531 cellTypeString - returns a string corresponding to, s.
2532 *)
2533
2534 PROCEDURE cellTypeString (s: cellType) : String ;
2535 BEGIN
2536 CASE s OF
2537
2538 none : RETURN( InitString('none') ) |
2539 integer : RETURN( InitString('integer') ) |
2540 real : RETURN( InitString('real') ) |
2541 complex : RETURN( InitString('complex') ) |
2542 set : RETURN( InitString('set') ) |
2543 constructor: RETURN( InitString('constructor') ) |
2544 array : RETURN( InitString('array') ) |
2545 record : RETURN( InitString('record') )
2546
2547 ELSE
2548 InternalError ('unexpected value of s')
2549 END ;
2550 RETURN( NIL )
2551 END cellTypeString ;
2552
2553
2554 (*
2555 ToSetValue - converts a list of fields into a list of ranges.
2556 In effect it turns a generic constructor into
2557 a set type.
2558 *)
2559
2560 PROCEDURE ToSetValue (f: listOfFields) : listOfRange ;
2561 VAR
2562 g : listOfFields ;
2563 r, s: listOfRange ;
2564 BEGIN
2565 g := f ;
2566 r := NIL ;
2567 WHILE f#NIL DO
2568 NewRange(s) ;
2569 WITH s^ DO
2570 low := f^.field ;
2571 high := low ;
2572 next := r
2573 END ;
2574 IF r=NIL
2575 THEN
2576 r := s
2577 END ;
2578 f := f^.next
2579 END ;
2580 DisposeFields(g) ;
2581 RETURN( r )
2582 END ToSetValue ;
2583
2584
2585 (*
2586 ToArrayValue - converts a list of fields into an array initialiser.
2587 In effect it turns a generic constructor into
2588 an array type.
2589 *)
2590
2591 PROCEDURE ToArrayValue (tok: CARDINAL; f: listOfFields) : listOfElements ;
2592 VAR
2593 g : listOfFields ;
2594 r, s: listOfElements ;
2595 BEGIN
2596 g := f ;
2597 r := NIL ;
2598 WHILE f#NIL DO
2599 NewElement(s) ;
2600 WITH s^ DO
2601 element := f^.field ;
2602 by := MakeConstLit (tok, MakeKey('1'), ZType) ;
2603 next := r
2604 END ;
2605 IF r=NIL
2606 THEN
2607 r := s
2608 END ;
2609 f := f^.next
2610 END ;
2611 DisposeFields(g) ;
2612 RETURN( r )
2613 END ToArrayValue ;
2614
2615
2616 (*
2617 ChangeToConstructor - change the top of stack value to a constructor, type.
2618 (Constructor, Set, Array or Record).
2619 *)
2620
2621 PROCEDURE ChangeToConstructor (tokenno: CARDINAL; constype: CARDINAL) ;
2622 VAR
2623 v: PtrToValue ;
2624 BEGIN
2625 IF IsValueTypeConstructor() OR IsValueTypeSet() OR
2626 IsValueTypeArray() OR IsValueTypeRecord()
2627 THEN
2628 RETURN
2629 ELSIF IsValueTypeNone()
2630 THEN
2631 v := Pop() ;
2632 WITH v^ DO
2633 type := constructor ;
2634 constructorType := constype ;
2635 solved := CompletelyResolved(constype) ;
2636 fieldValues := NIL ;
2637 next := NIL ;
2638 END ;
2639 IF IsSet(SkipType(constype))
2640 THEN
2641 v := CoerseTo(tokenno, set, v)
2642 ELSIF IsRecord(SkipType(constype))
2643 THEN
2644 v := CoerseTo(tokenno, record, v)
2645 ELSIF IsArray(SkipType(constype))
2646 THEN
2647 v := CoerseTo(tokenno, array, v)
2648 END ;
2649 Push(v)
2650 ELSE
2651 InternalError('cannot change constant to a constructor type')
2652 END
2653 END ChangeToConstructor ;
2654
2655
2656 (*
2657 CoerseTo - attempts to coerses a cellType, v, into, type, t.
2658 Normally this will be a generic constructors converting
2659 into set or array.
2660 *)
2661
2662 PROCEDURE CoerseTo (tokenno: CARDINAL;
2663 t: cellType; v: PtrToValue) : PtrToValue ;
2664 VAR
2665 s1, s2, s3: DynamicStrings.String ;
2666 BEGIN
2667 WITH v^ DO
2668 IF t=type
2669 THEN
2670 RETURN( v )
2671 ELSIF (type=constructor) AND (t=set)
2672 THEN
2673 type := set ;
2674 setValue := ToSetValue(fieldValues) ;
2675 RETURN( v )
2676 ELSIF (type=constructor) AND (t=array)
2677 THEN
2678 type := array ;
2679 arrayValues := ToArrayValue (tokenno, fieldValues) ;
2680 RETURN( v )
2681 ELSIF (type=constructor) AND (t=record)
2682 THEN
2683 (* nothing to do other than change tag *)
2684 type := record ;
2685 RETURN( v )
2686 ELSE
2687 s1 := cellTypeString (t) ;
2688 s2 := cellTypeString (type) ;
2689 s3 := ConCat(InitString('cannot mix construction of a '),
2690 Mark(ConCat(Mark(s1),
2691 Mark(ConCat(InitString(' with a '),
2692 (Mark(s2))))))) ;
2693 MetaErrorStringT0 (tokenno, s3) ;
2694 RETURN( v )
2695 END
2696 END
2697 END CoerseTo ;
2698
2699
2700 (*
2701 SetNegate - negates the top set on the stack.
2702
2703 Ptr -> <- Ptr
2704 +-----------+ +------------+
2705 | Set | | Set |
2706 |-----------| |------------|
2707 *)
2708
2709 PROCEDURE SetNegate (tokenno: CARDINAL) ;
2710 VAR
2711 min,
2712 max : CARDINAL ;
2713 r, s: listOfRange ;
2714 v : PtrToValue ;
2715 i : CARDINAL ;
2716 BEGIN
2717 v := Pop() ;
2718 Eval(tokenno, v) ;
2719 IF v^.constructorType=NulSym
2720 THEN
2721 MetaError0 ('cannot negate a generic set, set should be prefixed by a simple type')
2722 END ;
2723 r := NIL ;
2724 min := GetTypeMin(GetType(v^.constructorType)) ;
2725 max := GetTypeMax(GetType(v^.constructorType)) ;
2726 i := min ;
2727 s := v^.setValue ;
2728 IF Debugging
2729 THEN
2730 printf0('attempting to negate set\n') ;
2731 DisplayElements(s)
2732 END ;
2733 WHILE s#NIL DO
2734 PushValue(s^.low) ;
2735 PushValue(min) ;
2736 IF Gre(tokenno)
2737 THEN
2738 PushValue(i) ;
2739 PushValue(max) ;
2740 IF LessEqu(tokenno)
2741 THEN
2742 r := AddRange(r, i, DupConst(tokenno, s^.low, -1))
2743 END
2744 END ;
2745 PushValue(s^.high) ;
2746 PushValue(max) ;
2747 IF Less(tokenno)
2748 THEN
2749 i := DupConst(tokenno, s^.high, 1) ;
2750 s := s^.next
2751 ELSE
2752 s := NIL
2753 END
2754 END ;
2755 IF Debugging
2756 THEN
2757 printf0('negated set so far\n') ;
2758 DisplayElements(r)
2759 END ;
2760 DisposeRange(v^.setValue) ;
2761 PushValue(i) ;
2762 PushValue(max) ;
2763 IF LessEqu(tokenno)
2764 THEN
2765 r := AddRange(r, i, max)
2766 END ;
2767 IF Debugging
2768 THEN
2769 printf0('final negated set value\n') ;
2770 DisplayElements(r)
2771 END ;
2772 WITH v^ DO
2773 solved := FALSE ;
2774 setValue := r ;
2775 END ;
2776 Eval(tokenno, v) ;
2777 Push(v)
2778 END SetNegate ;
2779
2780
2781 (*
2782 AddBitRange - adds the range op1..op2 to the underlying set.
2783
2784 Ptr ->
2785 <- Ptr
2786 +------------+ +------------+
2787 | Set | | Set |
2788 |------------| |------------|
2789
2790 *)
2791
2792 PROCEDURE AddBitRange (tokenno: CARDINAL; op1, op2: CARDINAL) ;
2793 VAR
2794 v: PtrToValue ;
2795 BEGIN
2796 v := Pop() ;
2797 v := CoerseTo(tokenno, set, v) ;
2798 IF v^.type=set
2799 THEN
2800 WITH v^ DO
2801 setValue := AddRange(setValue, op1, op2) ;
2802 solved := solved AND IsSolvedGCC(op1) AND IsSolvedGCC(op2) ;
2803 areAllConstants := areAllConstants AND IsConst(op1) AND IsConst(op2)
2804 END
2805 END ;
2806 Push(v)
2807 END AddBitRange ;
2808
2809
2810 (*
2811 AddBit - adds the bit op1 to the underlying set. INCL(Set, op1)
2812
2813 Ptr ->
2814 <- Ptr
2815 +------------+ +------------+
2816 | Set | | Set |
2817 |------------| |------------|
2818 *)
2819
2820 PROCEDURE AddBit (tokenno: CARDINAL; op1: CARDINAL) ;
2821 BEGIN
2822 AddBitRange(tokenno, op1, op1)
2823 END AddBit ;
2824
2825
2826 (*
2827 AddElementToEnd - appends, e, to the end of list, v.
2828 *)
2829
2830 PROCEDURE AddElementToEnd (v: PtrToValue; e: listOfElements) ;
2831 VAR
2832 a: listOfElements ;
2833 BEGIN
2834 IF v^.arrayValues=NIL
2835 THEN
2836 v^.arrayValues := e
2837 ELSE
2838 a := v^.arrayValues ;
2839 WHILE a^.next#NIL DO
2840 a := a^.next
2841 END ;
2842 a^.next := e
2843 END
2844 END AddElementToEnd ;
2845
2846
2847 (*
2848 AddFieldToEnd - appends, f, to the end of list, v.
2849 *)
2850
2851 PROCEDURE AddFieldToEnd (v: PtrToValue; f: listOfFields) ;
2852 VAR
2853 a: listOfFields ;
2854 BEGIN
2855 IF v^.fieldValues=NIL
2856 THEN
2857 v^.fieldValues := f
2858 ELSE
2859 a := v^.fieldValues ;
2860 WHILE a^.next#NIL DO
2861 a := a^.next
2862 END ;
2863 a^.next := f
2864 END
2865 END AddFieldToEnd ;
2866
2867
2868 (*
2869 AddField - adds the field op1 to the underlying constructor.
2870
2871 Ptr ->
2872 <- Ptr
2873 +------------+ +------------+
2874 | const | | const |
2875 |------------| |------------|
2876
2877 *)
2878
2879 PROCEDURE AddField (tokenno: CARDINAL; op1: CARDINAL) ;
2880 VAR
2881 v: PtrToValue ;
2882 f: listOfFields ;
2883 e: listOfElements ;
2884 BEGIN
2885 v := Pop() ;
2886 CASE v^.type OF
2887
2888 set : Push(v) ;
2889 AddBit(tokenno, op1) ;
2890 RETURN |
2891 array : WITH v^ DO
2892 solved := solved AND IsSolvedGCC(op1) ;
2893 areAllConstants := areAllConstants AND IsConst(op1)
2894 END ;
2895 NewElement(e) ;
2896 WITH e^ DO
2897 element := op1 ;
2898 by := MakeConstLit (tokenno, MakeKey('1'), ZType) ;
2899 next := NIL
2900 END ;
2901 AddElementToEnd(v, e) |
2902 constructor,
2903 record : WITH v^ DO
2904 solved := solved AND IsSolvedGCC(op1) ;
2905 areAllConstants := areAllConstants AND IsConst(op1)
2906 END ;
2907 NewField(f) ;
2908 WITH f^ DO
2909 field := op1 ;
2910 next := NIL
2911 END ;
2912 AddFieldToEnd(v, f)
2913
2914 ELSE
2915 InternalError ('not expecting this constant type')
2916 END ;
2917 Push(v)
2918 END AddField ;
2919
2920
2921 (*
2922 ElementsSolved - returns TRUE if all ranges in the set have been solved.
2923 *)
2924
2925 PROCEDURE ElementsSolved (tokenno: CARDINAL; r: listOfRange) : BOOLEAN ;
2926 BEGIN
2927 WHILE r#NIL DO
2928 WITH r^ DO
2929 IF NOT IsConst (low)
2930 THEN
2931 MetaErrorT1 (tokenno, 'a constant set can only contain constant set elements, {%1Ead} is not a constant',
2932 low)
2933 END ;
2934 IF (high # low) AND (NOT IsConst (high))
2935 THEN
2936 MetaErrorT1 (tokenno, 'a constant set can only contain constant set elements, {%1Ead} is not a constant',
2937 high)
2938 END ;
2939 IF NOT (IsSolvedGCC(low) AND IsSolvedGCC(high))
2940 THEN
2941 RETURN( FALSE )
2942 END
2943 END ;
2944 r := r^.next
2945 END ;
2946 RETURN( TRUE )
2947 END ElementsSolved ;
2948
2949
2950 (*
2951 ArrayElementsSolved - returns TRUE if all ranges in the set have been solved.
2952 *)
2953
2954 PROCEDURE ArrayElementsSolved (e: listOfElements) : BOOLEAN ;
2955 BEGIN
2956 WHILE e#NIL DO
2957 WITH e^ DO
2958 IF NOT (IsSolvedGCC(element) AND IsSolvedGCC(by))
2959 THEN
2960 RETURN( FALSE )
2961 END
2962 END ;
2963 e := e^.next
2964 END ;
2965 RETURN( TRUE )
2966 END ArrayElementsSolved ;
2967
2968
2969 (*
2970 EvalFieldValues - returns TRUE if all fields in the record have been solved.
2971 *)
2972
2973 PROCEDURE EvalFieldValues (e: listOfFields) : BOOLEAN ;
2974 BEGIN
2975 WHILE e#NIL DO
2976 WITH e^ DO
2977 IF IsConst(field)
2978 THEN
2979 IF NOT IsSolvedGCC(field)
2980 THEN
2981 RETURN( FALSE )
2982 END
2983 ELSE
2984 (* RETURN( FALSE ) *)
2985 END
2986 END ;
2987 e := e^.next
2988 END ;
2989 RETURN( TRUE )
2990 END EvalFieldValues ;
2991
2992
2993 (*
2994 Swap - swaps the contents of, i, and, j.
2995 *)
2996
2997 PROCEDURE Swap (i, j: listOfRange) ;
2998 VAR
2999 t: CARDINAL ;
3000 BEGIN
3001 t := i^.low ;
3002 i^.low := j^.low ;
3003 j^.low := t ;
3004
3005 t := i^.high ;
3006 i^.high := j^.high ;
3007 j^.high := t
3008 END Swap ;
3009
3010
3011 (*
3012 DisplayElements -
3013 *)
3014
3015 PROCEDURE DisplayElements (i: listOfRange) ;
3016 BEGIN
3017 WHILE i#NIL DO
3018 PushValue(i^.low) ;
3019 PrintValue ;
3020 Reduce ;
3021 PushValue(i^.high) ;
3022 PrintValue ;
3023 Reduce ;
3024 i := i^.next
3025 END
3026 END DisplayElements ;
3027
3028
3029 (*
3030 SortElements - sorts the list as defined by, h, into ascending range order.
3031 The low element is the sort key.
3032 *)
3033
3034 PROCEDURE SortElements (tokenno: CARDINAL; h: listOfRange) ;
3035 VAR
3036 i, j, k: listOfRange ;
3037 BEGIN
3038 i := h ;
3039 WHILE i#NIL DO
3040 j := i ;
3041 k := i^.next ;
3042 WHILE k#NIL DO
3043 PushValue(k^.low) ;
3044 ConvertToInt ;
3045 PushValue(j^.low) ;
3046 ConvertToInt ;
3047 IF Less(tokenno)
3048 THEN
3049 j := k ;
3050 END ;
3051 k := k^.next
3052 END ;
3053 Swap(i, j) ;
3054 i := i^.next
3055 END
3056 END SortElements ;
3057
3058
3059 (*
3060 CombineElements - given a sorted list determine whether there is any
3061 overlap in the low..high bounds. If overlap exists
3062 then remove it.
3063 *)
3064
3065 PROCEDURE CombineElements (tokenno: CARDINAL; r: listOfRange) ;
3066 VAR
3067 t, j: listOfRange ;
3068 BEGIN
3069 WHILE r#NIL DO
3070 j := r^.next ;
3071 WHILE j#NIL DO
3072 PushValue(r^.high) ;
3073 ConvertToInt ;
3074 PushCard(1) ;
3075 Addn ;
3076 PushValue(j^.low) ;
3077 ConvertToInt ;
3078 IF GreEqu(tokenno)
3079 THEN
3080 r^.high := j^.high ;
3081 t := j^.next ;
3082 r^.next := j^.next ;
3083 j^.next := NIL ;
3084 DisposeRange(j) ;
3085 j := t
3086 ELSE
3087 j := NIL
3088 END
3089 END ;
3090 r := r^.next
3091 END
3092 END CombineElements ;
3093
3094
3095 (*
3096 EvalSetValues - returns TRUE if all elements in this set have been resolved.
3097 *)
3098
3099 PROCEDURE EvalSetValues (tokenno: CARDINAL; r: listOfRange) : BOOLEAN ;
3100 BEGIN
3101 IF ElementsSolved (tokenno, r)
3102 THEN
3103 SortElements(tokenno, r) ;
3104 CombineElements(tokenno, r) ;
3105 RETURN( TRUE )
3106 ELSE
3107 RETURN( FALSE )
3108 END
3109 END EvalSetValues ;
3110
3111
3112 (*
3113 Eval - attempts to solve a constructor type.
3114 *)
3115
3116 PROCEDURE Eval (tokenno: CARDINAL; v: PtrToValue) ;
3117 BEGIN
3118 CheckNotAlreadyOnFreeList(v) ;
3119 WITH v^ DO
3120 IF NOT solved
3121 THEN
3122 IF IsSet(SkipType(constructorType))
3123 THEN
3124 v := CoerseTo(tokenno, set, v)
3125 ELSIF IsRecord(SkipType(constructorType))
3126 THEN
3127 v := CoerseTo(tokenno, record, v)
3128 ELSIF IsArray(SkipType(constructorType))
3129 THEN
3130 v := CoerseTo(tokenno, array, v)
3131 END ;
3132 areAllConstants := DefinedByConstants(v) ;
3133 CASE type OF
3134
3135 set : Assert((constructorType=NulSym) OR IsSet(SkipType(constructorType))) ;
3136 solved := CompletelyResolved(constructorType) AND EvalSetValues(tokenno, setValue) |
3137 array : Assert((constructorType=NulSym) OR IsArray(SkipType(constructorType))) ;
3138 solved := CompletelyResolved(constructorType) AND ArrayElementsSolved(arrayValues) |
3139 record: Assert((constructorType=NulSym) OR IsRecord(SkipType(constructorType))) ;
3140 solved := CompletelyResolved(constructorType) AND EvalFieldValues(fieldValues)
3141
3142 ELSE
3143 (* do nothing *)
3144 END
3145 END
3146 END
3147 END Eval ;
3148
3149
3150 (*
3151 WalkSetValueDependants -
3152 *)
3153
3154 PROCEDURE WalkSetValueDependants (r: listOfRange; p: WalkAction) ;
3155 BEGIN
3156 WHILE r#NIL DO
3157 WITH r^ DO
3158 p(low) ;
3159 p(high)
3160 END ;
3161 r := r^.next
3162 END
3163 END WalkSetValueDependants ;
3164
3165
3166 (*
3167 IsSetValueDependants -
3168 *)
3169
3170 PROCEDURE IsSetValueDependants (r: listOfRange; q: IsAction) : BOOLEAN ;
3171 VAR
3172 result: BOOLEAN ;
3173 BEGIN
3174 result := TRUE ;
3175 WHILE r#NIL DO
3176 WITH r^ DO
3177 IF NOT q(low)
3178 THEN
3179 result := FALSE
3180 END ;
3181 IF NOT q(high)
3182 THEN
3183 result := FALSE
3184 END
3185 END ;
3186 r := r^.next
3187 END ;
3188 RETURN( result )
3189 END IsSetValueDependants ;
3190
3191
3192 (*
3193 WalkFieldValueDependants -
3194 *)
3195
3196 PROCEDURE WalkFieldValueDependants (f: listOfFields; p: WalkAction) ;
3197 BEGIN
3198 WHILE f#NIL DO
3199 WITH f^ DO
3200 p(field)
3201 END ;
3202 f := f^.next
3203 END
3204 END WalkFieldValueDependants ;
3205
3206
3207 (*
3208 IsFieldValueDependants -
3209 *)
3210
3211 PROCEDURE IsFieldValueDependants (f: listOfFields; q: IsAction) : BOOLEAN ;
3212 VAR
3213 result: BOOLEAN ;
3214 BEGIN
3215 result := TRUE ;
3216 WHILE f#NIL DO
3217 WITH f^ DO
3218 IF NOT q(field)
3219 THEN
3220 result := FALSE
3221 END
3222 END ;
3223 f := f^.next
3224 END ;
3225 RETURN( result )
3226 END IsFieldValueDependants ;
3227
3228
3229 (*
3230 WalkArrayValueDependants -
3231 *)
3232
3233 PROCEDURE WalkArrayValueDependants (a: listOfElements; p: WalkAction) ;
3234 BEGIN
3235 WHILE a#NIL DO
3236 WITH a^ DO
3237 p(element) ;
3238 p(by)
3239 END ;
3240 a := a^.next
3241 END
3242 END WalkArrayValueDependants ;
3243
3244
3245 (*
3246 IsArrayValueDependants -
3247 *)
3248
3249 PROCEDURE IsArrayValueDependants (a: listOfElements; q: IsAction) : BOOLEAN ;
3250 VAR
3251 result: BOOLEAN ;
3252 BEGIN
3253 result := TRUE ;
3254 WHILE a#NIL DO
3255 WITH a^ DO
3256 IF NOT q(element)
3257 THEN
3258 result := FALSE
3259 END ;
3260 IF NOT q(by)
3261 THEN
3262 result := FALSE
3263 END
3264 END ;
3265 a := a^.next
3266 END ;
3267 RETURN( result )
3268 END IsArrayValueDependants ;
3269
3270
3271 (*
3272 IsConstructorDependants - return TRUE if all q(dependants) of,
3273 sym, return TRUE.
3274 *)
3275
3276 PROCEDURE IsConstructorDependants (sym: CARDINAL; q: IsAction) : BOOLEAN ;
3277 VAR
3278 v : PtrToValue ;
3279 typeResult,
3280 result : BOOLEAN ;
3281 BEGIN
3282 PushValue(sym) ;
3283 IF IsValueTypeNone()
3284 THEN
3285 v := Pop() ;
3286 result := FALSE
3287 ELSE
3288 v := Pop() ;
3289 WITH v^ DO
3290 typeResult := q(constructorType) ;
3291 CASE type OF
3292
3293 none : result := FALSE |
3294 set : result := IsSetValueDependants(setValue, q) |
3295 constructor,
3296 record : result := IsFieldValueDependants(fieldValues, q) |
3297 array : result := IsArrayValueDependants(arrayValues, q)
3298
3299 ELSE
3300 InternalError ('not expecting this type')
3301 END ;
3302 result := result AND typeResult
3303 END
3304 END ;
3305 RETURN( result )
3306 END IsConstructorDependants ;
3307
3308
3309 (*
3310 WalkConstructorDependants - walk the constructor, sym, calling
3311 p for each dependant.
3312 *)
3313
3314 PROCEDURE WalkConstructorDependants (sym: CARDINAL; p: WalkAction) ;
3315 VAR
3316 v: PtrToValue ;
3317 BEGIN
3318 PushValue(sym) ;
3319 IF IsValueTypeNone()
3320 THEN
3321 v := Pop()
3322 ELSE
3323 v := Pop() ;
3324 WITH v^ DO
3325 p(constructorType) ;
3326 CASE type OF
3327
3328 none : |
3329 set : WalkSetValueDependants(setValue, p) |
3330 constructor,
3331 record : WalkFieldValueDependants(fieldValues, p) |
3332 array : WalkArrayValueDependants(arrayValues, p)
3333
3334 ELSE
3335 InternalError ('not expecting this type')
3336 END
3337 END
3338 END
3339 END WalkConstructorDependants ;
3340
3341
3342 (*
3343 PutConstructorSolved - records that this constructor is solved.
3344 *)
3345
3346 PROCEDURE PutConstructorSolved (sym: CARDINAL) ;
3347 VAR
3348 v: PtrToValue ;
3349 BEGIN
3350 PushValue(sym) ;
3351 v := Pop() ;
3352 v^.solved := TRUE ;
3353 Push(v) ;
3354 PopValue(sym)
3355 END PutConstructorSolved ;
3356
3357
3358 (*
3359 EvaluateValue - attempts to evaluate the symbol, sym, value.
3360 *)
3361
3362 PROCEDURE EvaluateValue (sym: CARDINAL) ;
3363 VAR
3364 v: PtrToValue ;
3365 BEGIN
3366 PushValue(sym) ;
3367 v := Pop() ;
3368 Eval(GetDeclaredMod(sym), v) ;
3369 Push(v) ;
3370 PopValue(sym)
3371 END EvaluateValue ;
3372
3373
3374 (*
3375 TryEvaluateValue - attempts to evaluate the symbol, sym, value.
3376 *)
3377
3378 PROCEDURE TryEvaluateValue (sym: CARDINAL) ;
3379 VAR
3380 v: PtrToValue ;
3381 BEGIN
3382 PushValue(sym) ;
3383 v := Pop() ;
3384 WITH v^ DO
3385 CASE type OF
3386
3387 set, array, record: IF v^.constructorType=NulSym
3388 THEN
3389 (* must wait *)
3390 RETURN
3391 ELSE
3392 Eval(GetDeclaredMod(sym), v)
3393 END
3394
3395 ELSE
3396 (* nothing to do *)
3397 END ;
3398 IF solved
3399 THEN
3400 Push(v) ;
3401 PopValue(sym)
3402 END
3403 END
3404 END TryEvaluateValue ;
3405
3406
3407 (*
3408 DefinedByConstants - returns TRUE if the value, v, is defined by constants.
3409 It assigns, v^.areAllConstants, with the result.
3410 *)
3411
3412 PROCEDURE DefinedByConstants (v: PtrToValue) : BOOLEAN ;
3413 BEGIN
3414 WITH v^ DO
3415 CASE type OF
3416
3417 none,
3418 integer,
3419 real,
3420 complex : areAllConstants := TRUE |
3421 set : areAllConstants := rangeConstant(setValue) |
3422 constructor,
3423 record : areAllConstants := fieldsConstant(fieldValues) |
3424 array : areAllConstants := arrayConstant(arrayValues)
3425
3426 ELSE
3427 InternalError ('unexpected type')
3428 END ;
3429 RETURN( areAllConstants )
3430 END
3431 END DefinedByConstants ;
3432
3433
3434 (*
3435 rangeConstant - returns TRUE if all the range entities are constant.
3436 *)
3437
3438 PROCEDURE rangeConstant (r: listOfRange) : BOOLEAN ;
3439 BEGIN
3440 WHILE r#NIL DO
3441 IF (NOT IsConst(r^.low)) OR (NOT IsConst(r^.high))
3442 THEN
3443 RETURN( FALSE )
3444 END ;
3445 r := r^.next ;
3446 END ;
3447 RETURN( TRUE )
3448 END rangeConstant ;
3449
3450
3451 (*
3452 fieldsConstant - returns TRUE if all the field entities are constant.
3453 *)
3454
3455 PROCEDURE fieldsConstant (f: listOfFields) : BOOLEAN ;
3456 BEGIN
3457 WHILE f#NIL DO
3458 IF NOT IsConst(f^.field)
3459 THEN
3460 RETURN( FALSE )
3461 END ;
3462 f := f^.next
3463 END ;
3464 RETURN( TRUE )
3465 END fieldsConstant ;
3466
3467
3468 (*
3469 arrayConstant - returns TRUE if the, element, and, by, components
3470 of an array constructor are constant.
3471 *)
3472
3473 PROCEDURE arrayConstant (e: listOfElements) : BOOLEAN ;
3474 BEGIN
3475 WHILE e#NIL DO
3476 IF (NOT IsConst(e^.element)) AND (NOT IsConst(e^.by))
3477 THEN
3478 RETURN( FALSE )
3479 END ;
3480 e := e^.next
3481 END ;
3482 RETURN( TRUE )
3483 END arrayConstant ;
3484
3485
3486 (*
3487 FindValueEnum -
3488 *)
3489
3490 PROCEDURE FindValueEnum (field: WORD) ;
3491 BEGIN
3492 PushValue(field) ;
3493 PushIntegerTree(EnumerationValue) ;
3494 IF Equ(CurrentTokenNo)
3495 THEN
3496 EnumerationField := field
3497 END
3498 END FindValueEnum ;
3499
3500
3501 (*
3502 Val - returns a GCC symbol enumeration or a GCC constant which has, value, and which is
3503 of type, type.
3504 *)
3505
3506 PROCEDURE Val (tokenno: CARDINAL; type: CARDINAL; value: tree) : CARDINAL ;
3507 VAR
3508 sym: CARDINAL ;
3509 BEGIN
3510 IF IsEnumeration(type)
3511 THEN
3512 EnumerationField := NulSym ;
3513 EnumerationValue := value ;
3514 CurrentTokenNo := tokenno ;
3515 ForeachFieldEnumerationDo(type, FindValueEnum) ;
3516 IF EnumerationField=NulSym
3517 THEN
3518 InternalError ('enumeration value exceeds range')
3519 END ;
3520 RETURN( EnumerationField )
3521 ELSE
3522 sym := MakeTemporary(tokenno, ImmediateValue) ;
3523 PutVar(sym, type) ;
3524 CheckOverflow(tokenno, value) ;
3525 PushIntegerTree(value) ;
3526 PopValue(sym) ;
3527 RETURN( sym )
3528 END
3529 END Val ;
3530
3531
3532 (*
3533 DupConst - duplicates and returns a constant, sym, but adds, offset to its value.
3534 *)
3535
3536 PROCEDURE DupConst (tokenno: CARDINAL; sym: CARDINAL; offset: INTEGER) : CARDINAL ;
3537 BEGIN
3538 PushValue(sym) ;
3539 PushInt(offset) ;
3540 Addn ;
3541 RETURN( Val(tokenno, GetType(sym), PopIntegerTree()) )
3542 END DupConst ;
3543
3544
3545 (*
3546 DupConstAndAdd - duplicates and returns a constant, sym,
3547 but adds the symbol, extra.
3548 *)
3549
3550 PROCEDURE DupConstAndAdd (tokenno: CARDINAL;
3551 sym: CARDINAL; extra: tree) : CARDINAL ;
3552 BEGIN
3553 PushValue(sym) ;
3554 PushIntegerTree(extra) ;
3555 Addn ;
3556 RETURN( Val(tokenno, GetType(sym), PopIntegerTree()) )
3557 END DupConstAndAdd ;
3558
3559
3560 (*
3561 DupConstAndAddMod - duplicates and returns a constant, sym,
3562 but adds the symbol, extra, and ensures that
3563 the result in within limits: min..max using
3564 modulo arithmetic.
3565 *)
3566
3567 PROCEDURE DupConstAndAddMod (tokenno: CARDINAL;
3568 sym: CARDINAL; extra: tree;
3569 l, h: CARDINAL) : CARDINAL ;
3570 BEGIN
3571 (* result := (((sym-l) + extra) MOD (h-l)) + l) *)
3572 PushValue(sym) ;
3573 PushValue(l) ;
3574 Sub ;
3575 PushIntegerTree(extra) ;
3576 Addn ;
3577 PushValue(h) ;
3578 PushValue(l) ;
3579 Sub ;
3580 ModTrunc ;
3581 PushValue(l) ;
3582 Addn ;
3583 RETURN( Val(tokenno, GetType(sym), PopIntegerTree()) )
3584 END DupConstAndAddMod ;
3585
3586
3587 (*
3588 Remove - removes, v, from list, h.
3589 *)
3590
3591 PROCEDURE Remove (VAR h: listOfRange; v: listOfRange) ;
3592 VAR
3593 i: listOfRange ;
3594 BEGIN
3595 IF h=v
3596 THEN
3597 h := h^.next
3598 ELSE
3599 i := h ;
3600 WHILE (i#NIL) AND (i^.next#v) DO
3601 i := i^.next
3602 END ;
3603 IF i=NIL
3604 THEN
3605 InternalError ('expecting v to be on the list')
3606 ELSE
3607 i := v^.next
3608 END
3609 END ;
3610 v^.next := NIL ;
3611 DisposeRange(v)
3612 END Remove ;
3613
3614
3615 (*
3616 RemoveBit - remove bit, op1, from range, v, on list, h.
3617 *)
3618
3619 PROCEDURE RemoveBit (tokenno: CARDINAL; VAR h: listOfRange; v: listOfRange; op1: CARDINAL) ;
3620 BEGIN
3621 WITH v^ DO
3622 PushValue(low) ;
3623 PushValue(high) ;
3624 IF Equ(tokenno)
3625 THEN
3626 (* single bit in this range *)
3627 PushValue(low) ;
3628 PushValue(op1) ;
3629 IF Equ(tokenno)
3630 THEN
3631 (* remove entry *)
3632 Remove(h, v) ;
3633 RETURN
3634 END
3635 ELSE
3636 (* is op1 equal to low? *)
3637 PushValue(op1) ;
3638 PushValue(low) ;
3639 IF Equ(tokenno)
3640 THEN
3641 low := DupConst(tokenno, low, 1)
3642 ELSE
3643 PushValue(op1) ;
3644 PushValue(high) ;
3645 IF Equ(tokenno)
3646 THEN
3647 high := DupConst(tokenno, high, -1)
3648 ELSE
3649 high := DupConst(tokenno, op1, -1) ;
3650 h := AddRange(h, DupConst(tokenno, op1, 1), high) ;
3651 SortElements(tokenno, h)
3652 END
3653 END
3654 END
3655 END
3656 END RemoveBit ;
3657
3658
3659 (*
3660 PerformSubBit -
3661 *)
3662
3663 PROCEDURE PerformSubBit (tokenno: CARDINAL; VAR h: listOfRange; op1: CARDINAL) ;
3664 VAR
3665 v: listOfRange ;
3666 BEGIN
3667 v := h ;
3668 WHILE v#NIL DO
3669 WITH v^ DO
3670 PushValue(low) ;
3671 PushValue(op1) ;
3672 IF LessEqu(tokenno)
3673 THEN
3674 PushValue(op1) ;
3675 PushValue(high) ;
3676 IF LessEqu(tokenno)
3677 THEN
3678 RemoveBit(tokenno, h, v, op1) ;
3679 RETURN
3680 END
3681 END
3682 END ;
3683 v := v^.next
3684 END
3685 END PerformSubBit ;
3686
3687
3688 (*
3689 SubBit - removes a bit op1 from the underlying set. EXCL(Set, Op1)
3690
3691 Ptr ->
3692 <- Ptr
3693 +------------+ +------------+
3694 | Set | | Set |
3695 |------------| |------------|
3696 *)
3697
3698 PROCEDURE SubBit (tokenno: CARDINAL; op1: CARDINAL) ;
3699 VAR
3700 v: PtrToValue ;
3701 BEGIN
3702 v := Pop() ;
3703 IF v^.type=set
3704 THEN
3705 Eval(tokenno, v) ;
3706 WITH v^ DO
3707 IF solved
3708 THEN
3709 IF IsSolvedGCC(op1)
3710 THEN
3711 PerformSubBit(tokenno, setValue, op1) ;
3712 solved := FALSE
3713 ELSE
3714 InternalError ('can only subtract a bit from a set when the bit value is known')
3715 END
3716 ELSE
3717 InternalError ('can only subtract a bit from a set when the set value is known')
3718 END
3719 END ;
3720 Eval(tokenno, v)
3721 ELSE
3722 InternalError ('expecting set type constant')
3723 END ;
3724 Push(v)
3725 END SubBit ;
3726
3727
3728 (*
3729 PerformSetIn - returns TRUE if op1 is in set.
3730 *)
3731
3732 PROCEDURE PerformSetIn (tokenno: CARDINAL; op1: CARDINAL; h: listOfRange) : BOOLEAN ;
3733 BEGIN
3734 WHILE h#NIL DO
3735 WITH h^ DO
3736 PushValue(op1) ;
3737 ConvertToInt ;
3738 PushValue(low) ;
3739 ConvertToInt ;
3740 IF GreEqu(tokenno)
3741 THEN
3742 PushValue(op1) ;
3743 PushValue(high) ;
3744 IF LessEqu(tokenno)
3745 THEN
3746 RETURN( TRUE )
3747 END
3748 ELSE
3749 (* op1 is smaller than this and all subsequent ranges *)
3750 RETURN( FALSE )
3751 END
3752 END ;
3753 h := h^.next
3754 END ;
3755 RETURN( FALSE )
3756 END PerformSetIn ;
3757
3758
3759 (*
3760 SetIn - returns true if Op2 IN Op1
3761
3762 The Stack:
3763
3764 Entry Exit
3765
3766 Ptr ->
3767 +------------+
3768 | Set |
3769 |------------| Empty
3770
3771 RETURN( Op1 IN Set )
3772 *)
3773
3774 PROCEDURE SetIn (tokenno: CARDINAL; Op1: CARDINAL) : BOOLEAN ;
3775 VAR
3776 Set : PtrToValue ;
3777 result: BOOLEAN ;
3778 BEGIN
3779 Set := Pop() ;
3780 IF Set^.type#set
3781 THEN
3782 InternalError ('expecting ALU operand to be a set')
3783 END ;
3784 Eval(tokenno, Set) ;
3785 IF IsSolvedGCC(Op1) AND Set^.solved
3786 THEN
3787 result := PerformSetIn(tokenno, Op1, Set^.setValue)
3788 ELSE
3789 InternalError ('one or more operands have not been resolved')
3790 END ;
3791 Dispose(Set) ;
3792 RETURN( result )
3793 END SetIn ;
3794
3795
3796 (*
3797 SetOp - perform the function doOp on the top two elements of the stack.
3798 *)
3799
3800 PROCEDURE SetOp (tokenno: CARDINAL; doOp: DoSetProcedure) ;
3801 VAR
3802 Result,
3803 Set1, Set2: PtrToValue ;
3804 BEGIN
3805 Set1 := Pop() ;
3806 Set2 := Pop() ;
3807 Eval(tokenno, Set1) ;
3808 Eval(tokenno, Set2) ;
3809 IF NOT (Set1^.solved AND Set2^.solved)
3810 THEN
3811 InternalError ('one or more operands have not been resolved')
3812 END ;
3813 IF Set1^.type#set
3814 THEN
3815 InternalError ('expecting type of constant to be a set')
3816 END ;
3817 IF Set2^.type#set
3818 THEN
3819 InternalError ('expecting type of constant to be a set')
3820 END ;
3821 Result := New() ;
3822 WITH Result^ DO
3823 type := set ;
3824 setValue := doOp(tokenno, Set1^.setValue, Set2^.setValue) ;
3825 constructorType := MixTypes(Set1^.constructorType,
3826 Set2^.constructorType, tokenno) ;
3827 solved := FALSE
3828 END ;
3829 (* Set1 and Set2 have given their range lists to the Result *)
3830 Set1^.setValue := NIL ;
3831 Set2^.setValue := NIL ;
3832 Eval(tokenno, Result) ;
3833 Push(Result) ;
3834 Dispose(Set1) ;
3835 Dispose(Set2)
3836 END SetOp ;
3837
3838
3839 (*
3840 PerformOr - performs a logical OR between the two ranges.
3841 The ranges, r1, r2, are destroyed.
3842 *)
3843
3844 PROCEDURE PerformOr (tokenno: CARDINAL; r1, r2: listOfRange) : listOfRange ;
3845 VAR
3846 i: listOfRange ;
3847 BEGIN
3848 i := r1 ;
3849 WHILE (i#NIL) AND (i^.next#NIL) DO
3850 i := i^.next
3851 END ;
3852 IF i=NIL
3853 THEN
3854 r1 := r2
3855 ELSE
3856 i^.next := r2
3857 END ;
3858 SortElements(tokenno, r1) ;
3859 CombineElements(tokenno, r1) ;
3860 RETURN( r1 )
3861 END PerformOr ;
3862
3863
3864 (*
3865 SetOr - performs an inclusive OR of the top two elements on the stack.
3866
3867 The Stack:
3868
3869 Entry Exit
3870
3871 Ptr ->
3872 +------------+
3873 | Set1 | <- Ptr
3874 |------------| +------------+
3875 | Set2 | | Set1 + Set2|
3876 |------------| |------------|
3877
3878 *)
3879
3880 PROCEDURE SetOr (tokenno: CARDINAL) ;
3881 BEGIN
3882 SetOp(tokenno, PerformOr)
3883 END SetOr ;
3884
3885
3886 (*
3887 Min - returns the symbol which has the least value.
3888 *)
3889
3890 PROCEDURE Min (tokenno: CARDINAL; a, b: CARDINAL) : CARDINAL ;
3891 BEGIN
3892 PushValue(a) ;
3893 ConvertToInt ;
3894 PushValue(b) ;
3895 ConvertToInt ;
3896 IF Less(tokenno)
3897 THEN
3898 RETURN( a )
3899 ELSE
3900 RETURN( b )
3901 END
3902 END Min ;
3903
3904
3905 (*
3906 Max - returns the symbol which has the greatest value.
3907 *)
3908
3909 PROCEDURE Max (tokenno: CARDINAL; a, b: CARDINAL) : CARDINAL ;
3910 BEGIN
3911 PushValue(a) ;
3912 ConvertToInt ;
3913 PushValue(b) ;
3914 ConvertToInt ;
3915 IF Gre(tokenno)
3916 THEN
3917 RETURN( a )
3918 ELSE
3919 RETURN( b )
3920 END
3921 END Max ;
3922
3923
3924 (*
3925 IsRangeIntersection - returns TRUE if ranges, r1, and, r2, intersect.
3926 *)
3927
3928 PROCEDURE IsRangeIntersection (tokenno: CARDINAL; r1, r2: listOfRange) : BOOLEAN ;
3929 BEGIN
3930 IF (r1=NIL) OR (r2=NIL)
3931 THEN
3932 RETURN( FALSE )
3933 ELSE
3934 (* easier to prove NOT outside limits *)
3935 PushValue(r1^.low) ;
3936 ConvertToInt ;
3937 PushValue(r2^.high) ;
3938 ConvertToInt ;
3939 IF Gre(tokenno)
3940 THEN
3941 RETURN( FALSE )
3942 ELSE
3943 PushValue(r1^.high) ;
3944 ConvertToInt ;
3945 PushValue(r2^.low) ;
3946 ConvertToInt ;
3947 IF Less(tokenno)
3948 THEN
3949 RETURN( FALSE )
3950 ELSE
3951 RETURN( TRUE )
3952 END
3953 END
3954 END
3955 END IsRangeIntersection ;
3956
3957
3958 (*
3959 IsRangeLess - returns TRUE if r1^.low is < r2^.low
3960 *)
3961
3962 PROCEDURE IsRangeLess (tokenno: CARDINAL; r1, r2: listOfRange) : BOOLEAN ;
3963 BEGIN
3964 IF (r1=NIL) OR (r2=NIL)
3965 THEN
3966 InternalError ('not expecting NIL ranges')
3967 END ;
3968 PushValue(r1^.high) ;
3969 ConvertToInt ;
3970 PushValue(r2^.low) ;
3971 ConvertToInt ;
3972 RETURN( Less(tokenno) )
3973 END IsRangeLess ;
3974
3975
3976 (*
3977 MinTree - returns the tree symbol which has the least value.
3978 *)
3979
3980 PROCEDURE MinTree (tokenno: CARDINAL; a, b: tree) : tree ;
3981 BEGIN
3982 PushIntegerTree(a) ;
3983 ConvertToInt ;
3984 PushIntegerTree(b) ;
3985 ConvertToInt ;
3986 IF Less(tokenno)
3987 THEN
3988 RETURN( a )
3989 ELSE
3990 RETURN( b )
3991 END
3992 END MinTree ;
3993
3994
3995 (*
3996 MaxTree - returns the symbol which has the greatest value.
3997 *)
3998
3999 PROCEDURE MaxTree (tokenno: CARDINAL; a, b: tree) : tree ;
4000 BEGIN
4001 PushIntegerTree(a) ;
4002 ConvertToInt ;
4003 PushIntegerTree(b) ;
4004 ConvertToInt ;
4005 IF Gre(tokenno)
4006 THEN
4007 RETURN( a )
4008 ELSE
4009 RETURN( b )
4010 END
4011 END MaxTree ;
4012
4013
4014 (*
4015 IsIntersectionTree - returns TRUE if ranges, a..b, and, c..d, intersect.
4016 *)
4017
4018 PROCEDURE IsIntersectionTree (tokenno: CARDINAL; a, b, c, d: tree) : BOOLEAN ;
4019 BEGIN
4020 (* easier to prove NOT outside limits *)
4021 PushIntegerTree(a) ;
4022 ConvertToInt ;
4023 PushIntegerTree(d) ;
4024 ConvertToInt ;
4025 IF Gre(tokenno)
4026 THEN
4027 RETURN( FALSE )
4028 ELSE
4029 PushIntegerTree(b) ;
4030 ConvertToInt ;
4031 PushIntegerTree(c) ;
4032 ConvertToInt ;
4033 IF Less(tokenno)
4034 THEN
4035 RETURN( FALSE )
4036 ELSE
4037 RETURN( TRUE )
4038 END
4039 END
4040 END IsIntersectionTree ;
4041
4042
4043 (*
4044 SubTree - returns the tree value containing (a-b)
4045 *)
4046
4047 PROCEDURE SubTree (a, b: tree) : tree ;
4048 BEGIN
4049 PushIntegerTree(a) ;
4050 PushIntegerTree(b) ;
4051 Sub ;
4052 RETURN( PopIntegerTree() )
4053 END SubTree ;
4054
4055
4056 (*
4057 PerformAnd - performs a logical AND between the two ranges.
4058 The ranges, r1, r2, are unaltered.
4059 *)
4060
4061 PROCEDURE PerformAnd (tokenno: CARDINAL; r1, r2: listOfRange) : listOfRange ;
4062 VAR
4063 r: listOfRange ;
4064 BEGIN
4065 r := NIL ;
4066 WHILE (r1#NIL) AND (r2#NIL) DO
4067 IF IsRangeIntersection(tokenno, r1, r2)
4068 THEN
4069 r := AddRange(r, Max(tokenno, r1^.low, r2^.low), Min(tokenno, r1^.high, r2^.high)) ;
4070 IF r^.high=r1^.high
4071 THEN
4072 r1 := r1^.next
4073 ELSE
4074 r2 := r2^.next
4075 END
4076 ELSIF IsRangeLess(tokenno, r1, r2)
4077 THEN
4078 (* move r1 onto the next range *)
4079 r1 := r1^.next
4080 ELSE
4081 (* move r2 onto the next range *)
4082 r2 := r2^.next
4083 END
4084 END ;
4085 RETURN( r )
4086 END PerformAnd ;
4087
4088
4089 (*
4090 SetAnd - performs a set AND the top two elements on the stack.
4091
4092 The Stack:
4093
4094 Entry Exit
4095
4096 Ptr ->
4097 +------------+
4098 | Op1 | <- Ptr
4099 |------------| +------------+
4100 | Op2 | | Op2 * Op1 |
4101 |------------| |------------|
4102 *)
4103
4104 PROCEDURE SetAnd (tokenno: CARDINAL) ;
4105 BEGIN
4106 SetOp(tokenno, PerformAnd)
4107 END SetAnd ;
4108
4109
4110 (*
4111 SetDifference - performs a set difference of the top two elements on the stack.
4112 For each member in the set
4113 if member in Op2 and not member in Op1
4114
4115 The Stack:
4116
4117 Entry Exit
4118
4119 Ptr ->
4120 +------------+
4121 | Op1 | <- Ptr
4122 |------------| +-------------------+
4123 | Op2 | | Op2 and (not Op1) |
4124 |------------| |-------------------|
4125 *)
4126
4127 PROCEDURE SetDifference (tokenno: CARDINAL) ;
4128 VAR
4129 Set1, Set2: PtrToValue ;
4130 BEGIN
4131 Set1 := Pop() ;
4132 Set2 := Pop() ;
4133 Eval(tokenno, Set1) ;
4134 Eval(tokenno, Set2) ;
4135 IF NOT (Set1^.solved AND Set2^.solved)
4136 THEN
4137 InternalError ('one or more operands have not been resolved')
4138 END ;
4139 IF Set1^.setValue=NIL
4140 THEN
4141 (* null set, return Set2 *)
4142 Push(Set1) ;
4143 ELSE
4144 Push(Set1) ;
4145 SetNegate(tokenno) ;
4146 Push(Set2) ;
4147 SetAnd(tokenno)
4148 END
4149 END SetDifference ;
4150
4151
4152 (*
4153 SetSymmetricDifference - performs a set difference of the top two elements on the stack.
4154
4155 The Stack:
4156
4157 Entry Exit
4158
4159 Ptr ->
4160 +------------+
4161 | Op1 | <- Ptr
4162 |------------| +-------------+
4163 | Op2 | | Op2 xor Op1 |
4164 |------------| |-------------|
4165 *)
4166
4167 PROCEDURE SetSymmetricDifference (tokenno: CARDINAL) ;
4168 VAR
4169 Set1, Set2: PtrToValue ;
4170 BEGIN
4171 Set1 := Pop() ;
4172 Set2 := Pop() ;
4173 Eval(tokenno, Set1) ;
4174 Eval(tokenno, Set2) ;
4175 IF NOT (Set1^.solved AND Set2^.solved)
4176 THEN
4177 InternalError ('one or more operands have not been resolved')
4178 END ;
4179 IF Set1^.setValue=NIL
4180 THEN
4181 Dispose(Set1) ;
4182 Push(Set2)
4183 ELSIF Set2^.setValue=NIL
4184 THEN
4185 Dispose(Set2) ;
4186 Push(Set1)
4187 ELSE
4188 (* Set1 or Set2 and (not (Set1 and Set2)) *)
4189 PushFrom(Set1) ;
4190 PushFrom(Set2) ;
4191 SetAnd(tokenno) ;
4192 SetNegate(tokenno) ;
4193 Push(Set1) ;
4194 Push(Set2) ;
4195 SetOr(tokenno) ;
4196 SetAnd(tokenno)
4197 END
4198 END SetSymmetricDifference ;
4199
4200
4201 (*
4202 SetShift - if op1 is positive
4203 then
4204 result := op2 << op1
4205 else
4206 result := op2 >> op1
4207 fi
4208
4209
4210 The Stack:
4211
4212 Entry Exit
4213
4214 Ptr ->
4215 +------------+
4216 | Op1 | <- Ptr
4217 |------------| +------------+
4218 | Op2 | | result |
4219 |------------| |------------|
4220
4221 *)
4222
4223 PROCEDURE SetShift (tokenno: CARDINAL) ;
4224 VAR
4225 res,
4226 Shift,
4227 Set : PtrToValue ;
4228 n : CARDINAL ;
4229 r1, r2 : CARDINAL ;
4230 BEGIN
4231 IF NOT IsValueTypeInteger()
4232 THEN
4233 InternalError ('expecting integer type')
4234 END ;
4235 Shift := Pop() ;
4236 IF NOT IsValueTypeSet()
4237 THEN
4238 InternalError ('expecting set type')
4239 END ;
4240 Set := Pop() ;
4241 Eval(tokenno, Set) ;
4242 IF NOT Set^.solved
4243 THEN
4244 InternalError ('set has not been resolved')
4245 END ;
4246 IF Set^.setValue=NIL
4247 THEN
4248 Push(Set)
4249 ELSE
4250 res := New() ;
4251 res^ := Set^ ;
4252 WITH res^ DO
4253 setValue := NIL ;
4254 n := 1 ;
4255 WHILE GetRange(Set, n, r1, r2) DO
4256 setValue := AddRange(setValue,
4257 DupConstAndAdd(tokenno, r1, Shift),
4258 DupConstAndAdd(tokenno, r2, Shift)) ;
4259 INC(n)
4260 END ;
4261 Push(res) ;
4262 IF constructorType#NulSym
4263 THEN
4264 PushNulSet(constructorType) ;
4265 SetNegate(tokenno) ;
4266 SetAnd(tokenno)
4267 END
4268 END ;
4269 Dispose(Set)
4270 END
4271 END SetShift ;
4272
4273
4274 (*
4275 SetRotate - if op1 is positive
4276 then
4277 result := ROTATERIGHT(op2, op1)
4278 else
4279 result := ROTATELEFT(op2, op1)
4280 fi
4281
4282
4283 The Stack:
4284
4285 Entry Exit
4286
4287 Ptr ->
4288 +------------+
4289 | Op1 | <- Ptr
4290 |------------| +------------+
4291 | Op2 | | result |
4292 |------------| |------------|
4293 *)
4294
4295 PROCEDURE SetRotate (tokenno: CARDINAL) ;
4296 VAR
4297 res,
4298 Rotate,
4299 Set : PtrToValue ;
4300 n : CARDINAL ;
4301 l, h,
4302 type,
4303 r1, r2 : CARDINAL ;
4304 BEGIN
4305 IF NOT IsValueTypeInteger()
4306 THEN
4307 InternalError ('expecting integer type')
4308 END ;
4309 Rotate := Pop() ;
4310 IF NOT IsValueTypeSet()
4311 THEN
4312 InternalError ('expecting set type')
4313 END ;
4314 Set := Pop() ;
4315 Eval(tokenno, Set) ;
4316 IF NOT Set^.solved
4317 THEN
4318 InternalError ('set has not been resolved')
4319 END ;
4320 IF Set^.setValue=NIL
4321 THEN
4322 Push(Set)
4323 ELSE
4324 type := Set^.constructorType ;
4325 IF type=NulSym
4326 THEN
4327 MetaErrorT0 (tokenno, 'cannot perform a ROTATE on a generic set') ;
4328 Push(Set) ;
4329 RETURN
4330 END ;
4331 l := GetTypeMin(type) ;
4332 h := GetTypeMax(type) ;
4333 res := New() ;
4334 res^ := Set^ ;
4335 WITH res^ DO
4336 setValue := NIL ;
4337 n := 1 ;
4338 WHILE GetRange(Set, n, r1, r2) DO
4339 setValue := AddRange(setValue,
4340 DupConstAndAddMod(tokenno, r1, Rotate, l, h),
4341 DupConstAndAddMod(tokenno, r2, Rotate, l, h)) ;
4342 INC(n)
4343 END
4344 END ;
4345 Push(res) ;
4346 Dispose(Set)
4347 END
4348 END SetRotate ;
4349
4350
4351 (*
4352 GetValue - returns and pops the value from the top of stack.
4353 *)
4354
4355 PROCEDURE GetValue (tokenno: CARDINAL) : PtrToValue ;
4356 VAR
4357 v: PtrToValue ;
4358 BEGIN
4359 v := Pop() ;
4360 Eval(tokenno, v) ;
4361 RETURN( v )
4362 END GetValue ;
4363
4364
4365 (*
4366 GetRange - returns TRUE if range number, n, exists in the value, v.
4367 A non empty set is defined by having 1..N ranges
4368 *)
4369
4370 PROCEDURE GetRange (v: PtrToValue; n: CARDINAL; VAR low, high: CARDINAL) : BOOLEAN ;
4371 VAR
4372 l: listOfRange ;
4373 BEGIN
4374 WITH v^ DO
4375 IF type#set
4376 THEN
4377 InternalError ('expecting set constant')
4378 END ;
4379 l := setValue ;
4380 WHILE n>1 DO
4381 IF l=NIL
4382 THEN
4383 RETURN( FALSE )
4384 END ;
4385 l := l^.next ;
4386 DEC(n)
4387 END ;
4388 IF l=NIL
4389 THEN
4390 RETURN( FALSE )
4391 END ;
4392 low := l^.low ;
4393 high := l^.high
4394 END ;
4395 RETURN( TRUE )
4396 END GetRange ;
4397
4398
4399 (*
4400 BuildStructBitset - v is the PtrToValue.
4401 low and high are the limits of the subrange.
4402 *)
4403
4404 PROCEDURE BuildStructBitset (tokenno: CARDINAL; v: PtrToValue; low, high: tree) : tree ;
4405 VAR
4406 BitsInSet : tree ;
4407 bpw : CARDINAL ;
4408 cons : Constructor ;
4409 BEGIN
4410 PushIntegerTree(low) ;
4411 ConvertToInt ;
4412 low := PopIntegerTree() ;
4413 PushIntegerTree(high) ;
4414 ConvertToInt ;
4415 high := PopIntegerTree() ;
4416 bpw := GetBitsPerBitset() ;
4417
4418 PushIntegerTree(high) ;
4419 PushIntegerTree(low) ;
4420 Sub ;
4421 PushCard(1) ;
4422 Addn ;
4423 BitsInSet := PopIntegerTree() ;
4424
4425 cons := BuildStartSetConstructor(Mod2Gcc(v^.constructorType)) ;
4426
4427 PushIntegerTree(BitsInSet) ;
4428 PushCard(0) ;
4429 WHILE Gre(tokenno) DO
4430 PushIntegerTree(BitsInSet) ;
4431 PushCard(bpw-1) ;
4432 IF GreEqu(tokenno)
4433 THEN
4434 PushIntegerTree(low) ;
4435 PushCard(bpw-1) ;
4436 Addn ;
4437
4438 BuildSetConstructorElement(cons, BuildBitset(tokenno, v, low, PopIntegerTree())) ;
4439
4440 PushIntegerTree(low) ;
4441 PushCard(bpw) ;
4442 Addn ;
4443 low := PopIntegerTree() ;
4444 PushIntegerTree(BitsInSet) ;
4445 PushCard(bpw) ;
4446 Sub ;
4447 BitsInSet := PopIntegerTree()
4448 ELSE
4449 (* printf2('range is %a..%a\n', GetSymName(low), GetSymName(high)) ; *)
4450
4451 BuildSetConstructorElement(cons, BuildBitset(tokenno, v, low, high)) ;
4452
4453 PushCard(0) ;
4454 BitsInSet := PopIntegerTree()
4455 END ;
4456 PushIntegerTree(BitsInSet) ;
4457 PushCard(0)
4458 END ;
4459 RETURN( BuildEndSetConstructor(cons) )
4460 END BuildStructBitset ;
4461
4462
4463 (*
4464 ConstructLargeOrSmallSet - generates a constant representing the set value of the symbol, sym.
4465 We manufacture the constant by using a initialization
4466 structure of cardinals.
4467
4468 { (cardinal), (cardinal) etc }
4469 *)
4470
4471 PROCEDURE ConstructLargeOrSmallSet (tokenno: CARDINAL; v: PtrToValue; low, high: CARDINAL) : tree ;
4472 BEGIN
4473 PushValue(high) ;
4474 ConvertToInt ;
4475 PushValue(low) ;
4476 ConvertToInt ;
4477 Sub ;
4478 PushCard(GetBitsPerBitset()) ;
4479 IF Less(tokenno)
4480 THEN
4481 (* small set *)
4482 RETURN( BuildBitset(tokenno, v, Mod2Gcc(low), Mod2Gcc(high)) )
4483 ELSE
4484 (* large set *)
4485 RETURN( BuildStructBitset(tokenno, v, Mod2Gcc(low), Mod2Gcc(high)) )
4486 END
4487 END ConstructLargeOrSmallSet ;
4488
4489
4490 (*
4491 ConstructSetConstant - builds a struct of integers which represents the
4492 set const as defined by, v.
4493 *)
4494
4495 PROCEDURE ConstructSetConstant (tokenno: CARDINAL; v: PtrToValue) : tree ;
4496 VAR
4497 n1, n2 : Name ;
4498 gccsym : tree ;
4499 baseType,
4500 high, low: CARDINAL ;
4501 BEGIN
4502 WITH v^ DO
4503 IF constructorType=NulSym
4504 THEN
4505 InternalError ('set type must be known in order to generate a constant')
4506 ELSE
4507 baseType := SkipType(GetType(constructorType)) ;
4508 IF Debugging
4509 THEN
4510 n1 := GetSymName(constructorType) ;
4511 n2 := GetSymName(baseType) ;
4512 printf2('ConstructSetConstant of type %a and baseType %a\n', n1, n2)
4513 END ;
4514 IF IsSubrange(baseType)
4515 THEN
4516 GetSubrange(baseType, high, low) ;
4517 gccsym := ConstructLargeOrSmallSet(tokenno, v, low, high)
4518 ELSE
4519 gccsym := ConstructLargeOrSmallSet(tokenno, v, GetTypeMin(baseType), GetTypeMax(baseType))
4520 END ;
4521 RETURN( gccsym )
4522 END
4523 END
4524 END ConstructSetConstant ;
4525
4526
4527 (*
4528 ConvertConstToType - returns a Tree containing an initialiser,
4529 init, ready to be assigned to a record or
4530 array constructor.
4531 *)
4532
4533 PROCEDURE ConvertConstToType (tokenno: CARDINAL; field: CARDINAL; init: CARDINAL) : tree ;
4534 VAR
4535 initT,
4536 nBytes: tree ;
4537 BEGIN
4538 IF IsConstString(init) AND IsArray(SkipType(GetType(field))) AND
4539 (SkipTypeAndSubrange(GetType(GetType(field)))=Char)
4540 THEN
4541 IF NOT PrepareCopyString (tokenno, nBytes, initT, init, GetType (field))
4542 THEN
4543 MetaErrorT2 (tokenno,
4544 'string constant {%1Ea} is too large to be assigned to the {%2d} {%2a}',
4545 init, field)
4546 END ;
4547 RETURN initT
4548 ELSE
4549 RETURN( ConvertConstantAndCheck(TokenToLocation(tokenno), Mod2Gcc(GetType(field)), Mod2Gcc(init)) )
4550 END
4551 END ConvertConstToType ;
4552
4553
4554 (*
4555 ConstructRecordConstant - builds a struct initializer, as defined by, v.
4556 *)
4557
4558 PROCEDURE ConstructRecordConstant (tokenno: CARDINAL; v: PtrToValue) : tree ;
4559 VAR
4560 n1, n2 : Name ;
4561 i,
4562 Field,
4563 baseType : CARDINAL ;
4564 cons : Constructor ;
4565 BEGIN
4566 WITH v^ DO
4567 IF constructorType=NulSym
4568 THEN
4569 InternalError ('record type must be known in order to generate a constant')
4570 ELSE
4571 baseType := SkipType(constructorType) ;
4572 IF Debugging
4573 THEN
4574 n1 := GetSymName(constructorType) ;
4575 n2 := GetSymName(baseType) ;
4576 printf2('ConstructRecordConstant of type %a and baseType %a\n', n1, n2)
4577 END ;
4578 cons := BuildStartRecordConstructor(Mod2Gcc(baseType)) ;
4579 i := 1 ;
4580 REPEAT
4581 Field := GetNth(baseType, i) ;
4582 IF Field#NulSym
4583 THEN
4584 IF GccKnowsAbout(GetType(Field))
4585 THEN
4586 BuildRecordConstructorElement(cons, ConvertConstToType(tokenno, Field, GetConstructorField(v, i)))
4587 ELSE
4588 MetaErrorT0 (tokenno, 'trying to construct a compound literal and using a record field which does not exist')
4589 END
4590 END ;
4591 INC(i)
4592 UNTIL Field=NulSym ;
4593 RETURN( BuildEndRecordConstructor(cons) )
4594 END
4595 END
4596 END ConstructRecordConstant ;
4597
4598
4599 (*
4600 GetConstructorField - returns a symbol containing the constructor field, i.
4601 *)
4602
4603 PROCEDURE GetConstructorField (v: PtrToValue; i: CARDINAL) : CARDINAL ;
4604 VAR
4605 j: CARDINAL ;
4606 f: listOfFields ;
4607 BEGIN
4608 WITH v^ DO
4609 IF type#record
4610 THEN
4611 InternalError ('constructor type must be a record in order to push a field')
4612 ELSE
4613 IF constructorType=NulSym
4614 THEN
4615 InternalError ('constructor type must be a record in order to push a field')
4616 ELSE
4617 j := 1 ;
4618 f := fieldValues ;
4619 WHILE (j<i) AND (f#NIL) DO
4620 f := f^.next ;
4621 INC(j)
4622 END ;
4623 IF f=NIL
4624 THEN
4625 MetaError1 ('the {%1EN} element does not exist in the constant compound literal', i) ;
4626 RETURN( NulSym )
4627 ELSE
4628 RETURN( f^.field )
4629 END
4630 END
4631 END
4632 END
4633 END GetConstructorField ;
4634
4635
4636 (*
4637 GetConstructorElement - returns a symbol containing the array constructor element, i.
4638 *)
4639
4640 PROCEDURE GetConstructorElement (tokenno: CARDINAL; v: PtrToValue; i: CARDINAL) : CARDINAL ;
4641 VAR
4642 j: tree ;
4643 e: listOfElements ;
4644 BEGIN
4645 WITH v^ DO
4646 IF type#array
4647 THEN
4648 InternalError ('constructor type must be an array')
4649 ELSE
4650 IF constructorType=NulSym
4651 THEN
4652 InternalError ('constructor type must be an array')
4653 ELSE
4654 PushCard(i) ;
4655 j := PopIntegerTree() ;
4656 e := arrayValues ;
4657 WHILE e#NIL DO
4658 PushValue(e^.by) ;
4659 PushIntegerTree(j) ;
4660 IF GreEqu(tokenno)
4661 THEN
4662 RETURN( e^.element )
4663 END ;
4664 PushIntegerTree(j) ;
4665 ConvertToInt ;
4666 PushValue(e^.by) ;
4667 ConvertToInt ;
4668 Sub ;
4669 j := PopIntegerTree() ;
4670 e := e^.next
4671 END ;
4672 IF e=NIL
4673 THEN
4674 IF IsArray(SkipType(constructorType)) AND (GetType(SkipType(constructorType))=Char)
4675 THEN
4676 RETURN MakeConstLit (tokenno, MakeKey('0'), Char)
4677 ELSE
4678 MetaErrorT2 (tokenno,
4679 'the {%1EN} element does not exist in the {%2ad} array declaration used by the compound literal', i, constructorType) ;
4680 END
4681 END
4682 END
4683 END
4684 END ;
4685 RETURN NulSym
4686 END GetConstructorElement ;
4687
4688
4689 (*
4690 IsString - returns TRUE if sym is an ARRAY [..] OF CHAR
4691 *)
4692
4693 PROCEDURE IsString (sym: CARDINAL) : BOOLEAN ;
4694 BEGIN
4695 RETURN (IsArray(sym) AND (SkipType(GetType(sym))=Char)) OR
4696 IsConstString(sym) OR
4697 (IsConst(sym) AND (SkipType(GetType(sym))=Char))
4698 END IsString ;
4699
4700
4701 (*
4702 StringFitsArray -
4703 *)
4704
4705 PROCEDURE StringFitsArray (arrayType, el: CARDINAL; tokenno: CARDINAL) : BOOLEAN ;
4706 VAR
4707 location: location_t ;
4708 BEGIN
4709 location := TokenToLocation(tokenno) ;
4710 PushIntegerTree(BuildNumberOfArrayElements(location, Mod2Gcc(arrayType))) ;
4711 IF IsConstString(el)
4712 THEN
4713 PushCard(GetStringLength(tokenno, el))
4714 ELSIF IsConst(el) AND (SkipType(GetType(el))=Char) AND IsValueSolved(el)
4715 THEN
4716 PushCard(1)
4717 ELSE
4718 PushCard(0) ;
4719 MetaError1 ('cannot build a string using {%1Ead}', el)
4720 END ;
4721 RETURN GreEqu(tokenno)
4722 END StringFitsArray ;
4723
4724
4725 (*
4726 GetArrayLimits -
4727 *)
4728
4729 PROCEDURE GetArrayLimits (array: CARDINAL; VAR low, high: CARDINAL) ;
4730 VAR
4731 Subscript,
4732 Subrange : CARDINAL ;
4733 BEGIN
4734 Subscript := GetArraySubscript(array) ;
4735 Subrange := SkipType(GetType(Subscript)) ;
4736 IF IsEnumeration(Subrange)
4737 THEN
4738 GetBaseTypeMinMax(Subrange, low, high)
4739 ELSE
4740 GetSubrange(Subrange, high, low)
4741 END
4742 END GetArrayLimits ;
4743
4744
4745 (*
4746 InitialiseArrayOfCharWithString -
4747 *)
4748
4749 PROCEDURE InitialiseArrayOfCharWithString (tokenno: CARDINAL; cons: ADDRESS;
4750 el, baseType, arrayType: CARDINAL) : tree ;
4751 VAR
4752 isChar : BOOLEAN ;
4753 s, letter: String ;
4754 i, l : CARDINAL ;
4755 high, low: CARDINAL ;
4756 value,
4757 indice : tree ;
4758 location : location_t ;
4759 BEGIN
4760 location := TokenToLocation(tokenno) ;
4761 GetArrayLimits(baseType, low, high) ;
4762 l := 0 ;
4763 s := NIL ;
4764 IF IsConstString(el)
4765 THEN
4766 isChar := FALSE ;
4767 s := InitStringCharStar(KeyToCharStar(GetString(el))) ;
4768 l := GetStringLength(tokenno, el)
4769 ELSIF IsConst(el) AND (SkipType(GetType(el))=Char) AND IsValueSolved(el)
4770 THEN
4771 isChar := TRUE
4772 ELSE
4773 MetaError1 ('cannot build a string using {%1Ead}', el) ;
4774 isChar := FALSE
4775 END ;
4776 i := 0 ;
4777 REPEAT
4778 PushValue(low) ;
4779 PushCard(i) ;
4780 Addn ;
4781 indice := PopIntegerTree() ;
4782 letter := NIL ;
4783 IF isChar
4784 THEN
4785 isChar := FALSE ;
4786 PushValue(el) ;
4787 value := PopIntegerTree()
4788 ELSIF i<l
4789 THEN
4790 IF i+1<l
4791 THEN
4792 letter := Slice(s, i, i+1)
4793 ELSE
4794 letter := Slice(s, i, 0)
4795 END ;
4796 value := BuildCharConstant(location, string(letter)) ;
4797 ELSE
4798 letter := InitStringChar(nul) ;
4799 value := BuildCharConstant(location, string(letter))
4800 END ;
4801 value := ConvertConstantAndCheck(location, Mod2Gcc(arrayType), value) ;
4802 letter := KillString(letter) ;
4803 BuildArrayConstructorElement(cons, value, indice) ;
4804 PushValue(low) ;
4805 PushCard(i) ;
4806 Addn ;
4807 PushValue(high) ;
4808 INC(i)
4809 UNTIL GreEqu(tokenno) ;
4810 s := KillString(s) ;
4811 IF NOT StringFitsArray(baseType, el, tokenno)
4812 THEN
4813 MetaError2 ('string {%1Ea} is too large to fit into array {%2ad}', el, baseType)
4814 END ;
4815 (*
4816 IF v#NIL
4817 THEN
4818 el := GetConstructorElement(tokenno, v, 2) ;
4819 IF el#NulSym
4820 THEN
4821 MetaError1('not allowed to have multiple strings to initialise an array of characters {%1Ua}', el)
4822 END
4823 END ;
4824 *)
4825 RETURN( BuildEndArrayConstructor(cons) )
4826 END InitialiseArrayOfCharWithString ;
4827
4828
4829 (*
4830 CheckElementString -
4831 *)
4832
4833 PROCEDURE CheckElementString (el, arrayType: CARDINAL; tokenno: CARDINAL) : tree ;
4834 VAR
4835 cons: ADDRESS ;
4836 BEGIN
4837 IF IsString(arrayType) AND IsString(el)
4838 THEN
4839 cons := BuildStartArrayConstructor(Mod2Gcc(arrayType)) ;
4840 RETURN( InitialiseArrayOfCharWithString(tokenno, cons, el, arrayType, SkipType(GetType(arrayType))) )
4841 ELSE
4842 RETURN( Mod2Gcc(el) )
4843 END
4844 END CheckElementString ;
4845
4846
4847 (*
4848 InitialiseArrayWith -
4849 *)
4850
4851 PROCEDURE InitialiseArrayWith (tokenno: CARDINAL; cons: ADDRESS;
4852 v: PtrToValue; el, high, low, arrayType: CARDINAL) : tree ;
4853 VAR
4854 location: location_t ;
4855 i : CARDINAL ;
4856 indice,
4857 value : tree ;
4858 BEGIN
4859 location := TokenToLocation (tokenno) ;
4860 i := 0 ;
4861 WHILE el#NulSym DO
4862 PushValue (low) ;
4863 ConvertToInt ;
4864 PushInt (i) ;
4865 Addn ;
4866 indice := PopIntegerTree () ;
4867 value := CheckElementString (el, arrayType, tokenno) ;
4868 IF value = NIL
4869 THEN
4870 MetaErrorT0 (tokenno, '{%W}too few characters found when trying to construct a compound literal array') ;
4871 value := GetCardinalZero (location)
4872 END ;
4873 value := ConvertConstantAndCheck (location, Mod2Gcc (arrayType), value) ;
4874 BuildArrayConstructorElement (cons, value, indice) ;
4875 PushValue (low) ;
4876 ConvertToInt ;
4877 PushInt (i) ;
4878 Addn ;
4879 PushValue (high) ;
4880 ConvertToInt ;
4881 IF GreEqu (tokenno)
4882 THEN
4883 RETURN BuildEndArrayConstructor (cons)
4884 END ;
4885 INC (i) ;
4886 el := GetConstructorElement (tokenno, v, i+1)
4887 END ;
4888 RETURN BuildEndArrayConstructor (cons)
4889 END InitialiseArrayWith ;
4890
4891
4892 (*
4893 CheckGetCharFromString - return TRUE if a char from the position arrayIndex in the list of
4894 constDecl elements can be extracted. The character is returned
4895 in value.
4896 *)
4897
4898 PROCEDURE CheckGetCharFromString (location: location_t;
4899 tokenno: CARDINAL ;
4900 constDecl: PtrToValue;
4901 consType: CARDINAL ;
4902 arrayIndex: CARDINAL;
4903 VAR value: tree) : BOOLEAN ;
4904 VAR
4905 elementIndex: CARDINAL ;
4906 element : CARDINAL ;
4907 offset,
4908 totalLength : CARDINAL ;
4909 key : Name ;
4910 BEGIN
4911 totalLength := 0 ;
4912 elementIndex := 1 ;
4913 REPEAT
4914 element := GetConstructorElement (tokenno, constDecl, elementIndex) ;
4915 offset := totalLength ;
4916 IF IsConstString (element)
4917 THEN
4918 INC (totalLength, GetStringLength (tokenno, element)) ;
4919 IF totalLength > arrayIndex
4920 THEN
4921 key := GetString (element) ;
4922 DEC (arrayIndex, offset) ;
4923 value := BuildCharConstantChar (location, CharKey (key, arrayIndex)) ;
4924 RETURN TRUE
4925 END
4926 ELSIF IsConst (element) AND (SkipType (GetType (element)) = Char) AND IsValueSolved (element)
4927 THEN
4928 INC (totalLength) ;
4929 IF totalLength > arrayIndex
4930 THEN
4931 PushValue (element) ;
4932 value := ConvertConstantAndCheck (location, GetM2CharType (), PopIntegerTree ()) ;
4933 RETURN TRUE
4934 END
4935 ELSE
4936 INC (totalLength) ;
4937 IF totalLength > arrayIndex
4938 THEN
4939 MetaErrorT3 (tokenno,
4940 'expecting {%kCHAR} datatype and not {%1Ea} a {%1tad} in the {%2N} component of the {%3a} {%3d}',
4941 element, arrayIndex, consType) ;
4942 value := GetCardinalZero (location) ;
4943 RETURN FALSE
4944 END
4945 END ;
4946 INC (elementIndex)
4947 UNTIL element = NulSym ;
4948 value := GetCardinalZero (location) ;
4949 MetaErrorT2 (tokenno,
4950 'unable to obtain a {%kCHAR} at the {%1EN} position in {%2ad}',
4951 arrayIndex, consType) ;
4952 RETURN FALSE
4953 END CheckGetCharFromString ;
4954
4955
4956 (*
4957 InitialiseArrayOfCharWith -
4958 *)
4959
4960 PROCEDURE InitialiseArrayOfCharWith (tokenno: CARDINAL; cons: ADDRESS;
4961 constDecl: PtrToValue;
4962 el, high, low, consType, arrayType: CARDINAL) : tree ;
4963 VAR
4964 location : location_t ;
4965 arrayIndex: CARDINAL ; (* arrayIndex is the char position index of the final string. *)
4966 indice,
4967 value : tree ;
4968 BEGIN
4969 location := TokenToLocation (tokenno) ;
4970 arrayIndex := 0 ;
4971 WHILE el#NulSym DO
4972 PushValue (low) ;
4973 ConvertToInt ;
4974 PushInt (arrayIndex) ;
4975 Addn ;
4976 indice := PopIntegerTree () ;
4977 IF NOT CheckGetCharFromString (location, tokenno, constDecl, consType, arrayIndex, value)
4978 THEN
4979 (*
4980 MetaErrorT2 (tokenno,
4981 'unable to obtain a {%kCHAR} at the {%1EN} position in {%2ad}',
4982 arrayIndex, consType) ;
4983 *)
4984 END ;
4985 value := ConvertConstantAndCheck (location, Mod2Gcc (arrayType), value) ;
4986 BuildArrayConstructorElement (cons, value, indice) ;
4987 PushValue (low) ;
4988 ConvertToInt ;
4989 PushInt (arrayIndex) ;
4990 Addn ;
4991 PushValue (high) ;
4992 ConvertToInt ;
4993 IF GreEqu (tokenno)
4994 THEN
4995 RETURN BuildEndArrayConstructor (cons)
4996 END ;
4997 INC (arrayIndex)
4998 END ;
4999 RETURN BuildEndArrayConstructor (cons)
5000 END InitialiseArrayOfCharWith ;
5001
5002
5003 (*
5004 ConstructArrayConstant - builds a struct initializer, as defined by, v.
5005 *)
5006
5007 PROCEDURE ConstructArrayConstant (tokenno: CARDINAL; v: PtrToValue) : tree ;
5008 VAR
5009 n1, n2 : Name ;
5010 el1, el2,
5011 baseType,
5012 arrayType,
5013 high, low : CARDINAL ;
5014 cons : ADDRESS ;
5015 BEGIN
5016 WITH v^ DO
5017 IF constructorType=NulSym
5018 THEN
5019 InternalError ('array type must be known in order to generate a constant')
5020 ELSE
5021 baseType := SkipType(constructorType) ;
5022 IF Debugging
5023 THEN
5024 n1 := GetSymName(constructorType) ;
5025 n2 := GetSymName(baseType) ;
5026 printf2 ('ConstructArrayConstant of type %a and baseType %a\n', n1, n2)
5027 END ;
5028 cons := BuildStartArrayConstructor(Mod2Gcc(baseType)) ;
5029
5030 GetArrayLimits(baseType, low, high) ;
5031 arrayType := GetType(baseType) ;
5032
5033 el1 := GetConstructorElement(tokenno, v, 1) ;
5034 el2 := GetConstructorElement(tokenno, v, 2) ;
5035 IF (el2 = NulSym) AND IsString(baseType) AND IsString(el1)
5036 THEN
5037 (* constructorType is ARRAY [low..high] OF CHAR and using a string to initialise it *)
5038 RETURN InitialiseArrayOfCharWithString (tokenno, cons, el1, baseType, arrayType)
5039 ELSIF SkipType(arrayType)=Char
5040 THEN
5041 RETURN InitialiseArrayOfCharWith (tokenno, cons, v, el1, high, low, baseType, arrayType)
5042 ELSE
5043 RETURN InitialiseArrayWith (tokenno, cons, v, el1, high, low, arrayType)
5044 END
5045 END
5046 END
5047 END ConstructArrayConstant ;
5048
5049
5050 (*
5051 BuildRange - returns a integer sized constant which represents the
5052 value {e1..e2}.
5053 *)
5054
5055 PROCEDURE BuildRange (tokenno: CARDINAL; e1, e2: tree) : tree ;
5056 VAR
5057 c, i, t : tree ;
5058 location: location_t ;
5059 BEGIN
5060 location := TokenToLocation(tokenno) ;
5061 PushIntegerTree(e1) ;
5062 PushIntegerTree(e2) ;
5063 IF Gre(tokenno)
5064 THEN
5065 c := e1 ;
5066 e1 := e2 ;
5067 e2 := c
5068 END ;
5069 t := tree(NIL) ;
5070 PushIntegerTree(e1) ;
5071 i := PopIntegerTree() ;
5072 REPEAT
5073 IF t=tree(NIL)
5074 THEN
5075 t := BuildLSL(location, GetWordOne(location), ToWord(location, i), FALSE)
5076 ELSE
5077 t := BuildLogicalOr(location, t, BuildLSL(location, GetWordOne(location), ToWord(location, i), FALSE), FALSE)
5078 END ;
5079 PushIntegerTree(i) ;
5080 PushIntegerTree(GetIntegerOne(location)) ;
5081 Addn ;
5082 i := PopIntegerTree() ;
5083 PushIntegerTree(i) ;
5084 PushIntegerTree(e2) ;
5085 UNTIL Gre(tokenno) ;
5086 RETURN( t )
5087 END BuildRange ;
5088
5089
5090 (*
5091 BuildBitset - given a set, v, construct the bitmask for its
5092 constant value which lie in the range low..high.
5093 *)
5094
5095 PROCEDURE BuildBitset (tokenno: CARDINAL;
5096 v: PtrToValue; low, high: tree) : tree ;
5097 VAR
5098 tl, th,
5099 t : tree ;
5100 n : CARDINAL ;
5101 r1, r2 : CARDINAL ;
5102 location: location_t ;
5103 BEGIN
5104 location := TokenToLocation(tokenno) ;
5105 low := ToInteger(location, low) ;
5106 high := ToInteger(location, high) ;
5107 n := 1 ;
5108 t := GetCardinalZero(location) ;
5109 WHILE GetRange(v, n, r1, r2) DO
5110 PushValue(r1) ;
5111 tl := ToInteger(location, PopIntegerTree()) ;
5112 PushValue(r2) ;
5113 th := ToInteger(location, PopIntegerTree()) ;
5114 IF IsIntersectionTree(tokenno, tl, th, low, high)
5115 THEN
5116 tl := ToCardinal(location, SubTree(MaxTree(tokenno, tl, low), low)) ;
5117 th := ToCardinal(location, SubTree(MinTree(tokenno, th, high), low)) ;
5118 t := BuildLogicalOr(location, t, BuildRange(tokenno, tl, th), FALSE)
5119 END ;
5120 INC(n)
5121 END ;
5122 RETURN( ToBitset(location, t) )
5123 END BuildBitset ;
5124
5125
5126 (*
5127 IsValueAndTreeKnown - returns TRUE if the value is known and the gcc tree
5128 is defined.
5129
5130 The Stack:
5131
5132 Entry Exit
5133
5134 Ptr ->
5135 +------------+
5136 | Op1 | <- Ptr
5137 |------------| +------------+
5138 *)
5139
5140 PROCEDURE IsValueAndTreeKnown () : BOOLEAN ;
5141 VAR
5142 v: PtrToValue ;
5143 BEGIN
5144 v := Pop() ;
5145 IF v#NIL
5146 THEN
5147 WITH v^ DO
5148 IF solved
5149 THEN
5150 CASE type OF
5151
5152 integer,
5153 real,
5154 complex: IF numberValue=NIL
5155 THEN
5156 Dispose(v) ;
5157 RETURN( FALSE )
5158 END
5159 ELSE
5160 END
5161 ELSE
5162 Dispose(v) ;
5163 RETURN( FALSE )
5164 END
5165 END ;
5166 Dispose(v)
5167 END ;
5168 RETURN( TRUE )
5169 END IsValueAndTreeKnown ;
5170
5171
5172 (*
5173 CheckOverflow - tests to see whether the tree, t, has caused
5174 an overflow error and if so it generates an
5175 error message.
5176 *)
5177
5178 PROCEDURE CheckOverflow (tokenno: CARDINAL; t: tree) ;
5179 BEGIN
5180 IF TreeOverflow (t)
5181 THEN
5182 MetaErrorT0 (tokenno, 'constant overflow error') ;
5183 FlushErrors
5184 END
5185 END CheckOverflow ;
5186
5187
5188 (*
5189 CheckOrResetOverflow - tests to see whether the tree, t, has caused
5190 an overflow error and if so it generates an
5191 error message.
5192 *)
5193
5194 PROCEDURE CheckOrResetOverflow (tokenno: CARDINAL; t: tree; check: BOOLEAN) ;
5195 BEGIN
5196 IF check
5197 THEN
5198 CheckOverflow (tokenno, t)
5199 ELSE
5200 t := RemoveOverflow (t)
5201 END
5202 END CheckOrResetOverflow ;
5203
5204
5205 (*
5206 PushGCCArrayTree - pushes a gcc tree value onto the ALU stack.
5207 *)
5208
5209 PROCEDURE PushGCCArrayTree (gcc: tree; t: CARDINAL) ;
5210 VAR
5211 v: PtrToValue ;
5212 BEGIN
5213 v := New() ;
5214 WITH v^ DO
5215 constructorType := t ;
5216 type := array ;
5217 numberValue := gcc ;
5218 arrayValues := NIL ;
5219 areAllConstants := TRUE ;
5220 solved := TRUE
5221 END ;
5222 Push(v)
5223 END PushGCCArrayTree ;
5224
5225
5226 (*
5227 PushGCCSetTree - pushes a gcc tree value onto the ALU stack.
5228 *)
5229
5230 PROCEDURE PushGCCSetTree (gcc: tree; t: CARDINAL) ;
5231 VAR
5232 v: PtrToValue ;
5233 BEGIN
5234 v := New() ;
5235 WITH v^ DO
5236 constructorType := t ;
5237 type := set ;
5238 numberValue := gcc ;
5239 setValue := NIL ;
5240 areAllConstants := TRUE ;
5241 solved := TRUE
5242 END ;
5243 Push(v)
5244 END PushGCCSetTree ;
5245
5246
5247 (*
5248 PushGCCRecordTree - pushes a gcc tree value onto the ALU stack.
5249 *)
5250
5251 PROCEDURE PushGCCRecordTree (gcc: tree; t: CARDINAL) ;
5252 VAR
5253 v: PtrToValue ;
5254 BEGIN
5255 v := New() ;
5256 WITH v^ DO
5257 constructorType := t ;
5258 type := record ;
5259 numberValue := gcc ;
5260 fieldValues := NIL ;
5261 areAllConstants := TRUE ;
5262 solved := TRUE
5263 END ;
5264 Push(v)
5265 END PushGCCRecordTree ;
5266
5267
5268 (*
5269 PushTypeOfTree - pushes tree, gcc, to the stack and records the
5270 front end type.
5271 *)
5272
5273 PROCEDURE PushTypeOfTree (sym: CARDINAL; gcc: tree) ;
5274 VAR
5275 t: CARDINAL ;
5276 BEGIN
5277 t := SkipType(GetType(sym)) ;
5278 IF t=NulSym
5279 THEN
5280 PushIntegerTree(gcc)
5281 ELSIF IsComplexType(t)
5282 THEN
5283 PushComplexTree(gcc)
5284 ELSIF IsArray(t)
5285 THEN
5286 PushGCCArrayTree(gcc, t)
5287 ELSIF IsSet(t)
5288 THEN
5289 PushGCCSetTree(gcc, t)
5290 ELSIF IsRecord(t)
5291 THEN
5292 PushGCCRecordTree(gcc, t)
5293 ELSIF IsRealType(t)
5294 THEN
5295 PushRealTree(gcc)
5296 ELSE
5297 PushIntegerTree(gcc)
5298 END
5299 END PushTypeOfTree ;
5300
5301
5302 (*
5303 Init - initialises the stack and the free list.
5304 *)
5305
5306 PROCEDURE Init ;
5307 BEGIN
5308 FreeList := NIL ;
5309 TopOfStack := NIL ;
5310 RangeFreeList := NIL ;
5311 FieldFreeList := NIL ;
5312 ElementFreeList := NIL
5313 END Init ;
5314
5315
5316 BEGIN
5317 Init
5318 END M2ALU.
5319 (*
5320 * Local variables:
5321 * compile-command: "gm2 -c -g -I.:../gm2-libs:../gm2-libs-ch:../gm2-libiberty/ M2ALU.mod"
5322 * End:
5323 *)