1 (* M2ALU.def gcc implementation of the M2ALU module.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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/>. *)
22 DEFINITION MODULE M2ALU ;
28 Date : Tue Jul 11 09:14:28 2000
29 Description: Handles all values in the Modula-2 compiler and all
30 manipulations of values. All values are mapped onto
34 FROM NameKey IMPORT Name ;
35 FROM m2tree IMPORT Tree ;
36 FROM M2GCCDeclare IMPORT WalkAction, IsAction ;
38 EXPORT QUALIFIED PtrToValue,
45 IsValueTypeConstructor,
49 PushIntegerTree, PopIntegerTree,
50 PushSetTree, PopSetTree,
51 PushRealTree, PopRealTree,
52 PushComplexTree, PopComplexTree,
65 IsSolved, IsValueConst,
66 PutConstructorSolved, EvaluateValue, TryEvaluateValue,
68 IsNulSet, IsGenericNulSet, PushNulSet, AddBitRange, AddBit, SubBit,
70 SetDifference, SetSymmetricDifference,
71 SetNegate, SetShift, SetRotate,
74 DivFloor, ModFloor, DivTrunc, ModTrunc,
75 Equ, NotEqu, Less, Gre, LessEqu, GreEqu,
76 GetValue, GetRange, ConstructSetConstant, BuildRange,
77 IsConstructorDependants, WalkConstructorDependants,
78 AddField, AddElements,
80 PushEmptyConstructor, PushEmptyArray, PushEmptyRecord,
83 IsValueAndTreeKnown, CheckOrResetOverflow ;
90 InitValue - initializes and returns a value container.
93 PROCEDURE InitValue () : PtrToValue ;
97 IsValueTypeNone - returns TRUE if the value on the top stack has no value.
100 PROCEDURE IsValueTypeNone () : BOOLEAN ;
104 IsValueTypeInteger - returns TRUE if the value on the top stack is an integer.
107 PROCEDURE IsValueTypeInteger () : BOOLEAN ;
111 IsValueTypeReal - returns TRUE if the value on the top stack is a real.
114 PROCEDURE IsValueTypeReal () : BOOLEAN ;
118 IsValueTypeComplex - returns TRUE if the value on the top stack is a complex.
121 PROCEDURE IsValueTypeComplex () : BOOLEAN ;
125 IsValueTypeSet - returns TRUE if the value on the top stack is a set.
128 PROCEDURE IsValueTypeSet () : BOOLEAN ;
132 IsValueTypeConstructor - returns TRUE if the value on the top
133 stack is a constructor.
136 PROCEDURE IsValueTypeConstructor () : BOOLEAN ;
140 IsValueTypeArray - returns TRUE if the value on the top
144 PROCEDURE IsValueTypeArray () : BOOLEAN ;
148 IsValueTypeRecord - returns TRUE if the value on the top
152 PROCEDURE IsValueTypeRecord () : BOOLEAN ;
156 GetSetValueType - returns the set type on top of the ALU stack.
159 PROCEDURE GetSetValueType () : CARDINAL ;
163 PushIntegerTree - pushes a gcc tree value onto the ALU stack.
166 PROCEDURE PushIntegerTree (t: Tree) ;
170 PopIntegerTree - pops a gcc tree value from the ALU stack.
173 PROCEDURE PopIntegerTree () : Tree ;
177 PushRealTree - pushes a gcc tree value onto the ALU stack.
180 PROCEDURE PushRealTree (t: Tree) ;
184 PopRealTree - pops a gcc tree value from the ALU stack.
187 PROCEDURE PopRealTree () : Tree ;
191 PushComplexTree - pushes a gcc tree value onto the ALU stack.
194 PROCEDURE PushComplexTree (t: Tree) ;
198 PopComplexTree - pops a gcc tree value from the ALU stack.
201 PROCEDURE PopComplexTree () : Tree ;
205 PushSetTree - pushes a gcc tree onto the ALU stack.
206 The tree, t, is expected to contain a
207 word value. It is converted into a set
208 type (sym). Bit 0 maps onto MIN(sym).
211 PROCEDURE PushSetTree (tokenno: CARDINAL;
212 t: Tree; sym: CARDINAL) ;
216 PopSetTree - pops a gcc tree from the ALU stack.
219 PROCEDURE PopSetTree (tokenno: CARDINAL) : Tree ;
223 PopConstructorTree - returns a tree containing the compound literal.
226 PROCEDURE PopConstructorTree (tokenno: CARDINAL) : Tree ;
230 PushFrom - pushes a copy of the contents of, v, onto stack.
233 PROCEDURE PushFrom (v: PtrToValue) ;
237 PopInto - pops the top element from the stack and places it into, v.
240 PROCEDURE PopInto (v: PtrToValue) ;
244 PushCard - pushes a cardinal onto the stack.
247 PROCEDURE PushCard (c: CARDINAL) ;
251 PushInt - pushes an integer onto the stack.
254 PROCEDURE PushInt (i: INTEGER) ;
258 PushChar - pushes a char onto the stack.
261 PROCEDURE PushChar (c: CHAR) ;
265 PopChar - returns the value from the stack in a character.
268 PROCEDURE PopChar (tokenno: CARDINAL) : CHAR ;
272 PushString - pushes the numerical value of the string onto the stack.
275 PROCEDURE PushString (tokenno: CARDINAL; s: Name; issueError: BOOLEAN) ;
279 CoerseLongRealToCard - performs a coersion between a REAL to a CARDINAL
282 PROCEDURE CoerseLongRealToCard ;
286 ConvertRealToInt - converts a REAL into an INTEGER
289 PROCEDURE ConvertRealToInt ;
293 ConvertToInt - converts the value into an INTEGER. This should be used
294 if we are computing the number of elements in a char set to
298 PROCEDURE ConvertToInt ;
302 ConvertToType - converts the top of stack to type, t.
305 PROCEDURE ConvertToType (t: CARDINAL) ;
309 IsSolved - returns true if the memory cell indicated by v
313 PROCEDURE IsSolved (v: PtrToValue) : BOOLEAN ;
317 PutConstructorSolved - records that this constructor is solved.
320 PROCEDURE PutConstructorSolved (sym: CARDINAL) ;
324 EvaluateValue - attempts to evaluate the symbol, sym, value.
327 PROCEDURE EvaluateValue (sym: CARDINAL) ;
331 TryEvaluateValue - attempts to evaluate the symbol, sym, value.
334 PROCEDURE TryEvaluateValue (sym: CARDINAL) ;
338 Add - adds the top two elements on the stack.
347 |------------| +------------+
348 | Op2 | | Op2 + Op1 |
349 |------------| |------------|
356 Sub - subtracts the top two elements on the stack.
365 |------------| +------------+
366 | Op2 | | Op2 - Op1 |
367 |------------| |------------|
374 Mult - multiplies the top two elements on the stack.
383 |------------| +------------+
384 | Op2 | | Op2 * Op1 |
385 |------------| |------------|
392 DivFloor - divides the top two elements on the stack.
401 |------------| +--------------+
402 | Op2 | | Op2 DIV Op1 |
403 |------------| |--------------|
410 ModFloor - modulus of the top two elements on the stack.
419 |------------| +--------------+
420 | Op2 | | Op2 MOD Op1 |
421 |------------| |--------------|
428 DivTrunc - divides the top two elements on the stack.
437 |------------| +--------------+
438 | Op2 | | Op2 DIV Op1 |
439 |------------| |--------------|
446 ModTrunc - modulus of the top two elements on the stack.
455 |------------| +--------------+
456 | Op2 | | Op2 MOD Op1 |
457 |------------| |--------------|
464 Equ - returns true if the top two elements on the stack
481 PROCEDURE Equ (tokenno: CARDINAL) : BOOLEAN ;
485 NotEqu - returns true if the top two elements on the stack
502 PROCEDURE NotEqu (tokenno: CARDINAL) : BOOLEAN ;
506 Less - returns true if Op2 < Op1.
522 PROCEDURE Less (tokenno: CARDINAL) : BOOLEAN ;
526 Gre - returns true if Op2 > Op1
542 PROCEDURE Gre (tokenno: CARDINAL) : BOOLEAN ;
546 LessEqu - returns true if Op2 <= Op1
562 PROCEDURE LessEqu (tokenno: CARDINAL) : BOOLEAN ;
566 GreEqu - returns true if Op2 >= Op1
583 PROCEDURE GreEqu (tokenno: CARDINAL) : BOOLEAN ;
587 IsNulSet - returns TRUE if the top element is the nul set constant, {}.
590 PROCEDURE IsNulSet () : BOOLEAN ;
594 IsGenericNulSet - returns TRUE if the top element is the generic nul set constant, {}.
597 PROCEDURE IsGenericNulSet () : BOOLEAN ;
601 PushNulSet - pushes an empty set {} onto the ALU stack. The subrange type used
602 to construct the set is defined by, type. If this is NulSym then
603 the set is generic and compatible with all sets.
612 Ptr -> |------------|
616 PROCEDURE PushNulSet (settype: CARDINAL) ;
620 AddBitRange - adds the range op1..op2 to the underlying set.
624 +------------+ +------------+
626 |------------| |------------|
629 PROCEDURE AddBitRange (tokenno: CARDINAL; op1, op2: CARDINAL) ;
633 AddBit - adds the bit op1 to the underlying set. INCL(Set, op1)
637 +------------+ +------------+
639 |------------| |------------|
642 PROCEDURE AddBit (tokenno: CARDINAL; op1: CARDINAL) ;
646 SubBit - removes a bit op1 from the underlying set. EXCL(Set, Op1)
651 |------------| +------------+
653 |------------| |------------|
656 PROCEDURE SubBit (tokenno: CARDINAL; op1: CARDINAL) ;
660 SetIn - returns true if the Op1 IN Set
676 PROCEDURE SetIn (tokenno: CARDINAL; Op1: CARDINAL) : BOOLEAN ;
680 SetOr - performs an inclusive OR of the top two sets on the stack.
689 |------------| +------------+
690 | Op2 | | Op2 + Op1 |
691 |------------| |------------|
695 PROCEDURE SetOr (tokenno: CARDINAL) ;
699 SetAnd - performs a set AND the top two sets on the stack.
708 |------------| +------------+
709 | Op2 | | Op2 * Op1 |
710 |------------| |------------|
713 PROCEDURE SetAnd (tokenno: CARDINAL) ;
717 SetDifference - performs a set difference of the top two elements on the stack.
718 For each member in the set
719 if member in Op2 and not member in Op1
728 |------------| +-------------------+
729 | Op2 | | Op2 and (not Op1) |
730 |------------| |-------------------|
733 PROCEDURE SetDifference (tokenno: CARDINAL) ;
737 SetSymmetricDifference - performs a set difference of the top two sets on the stack.
746 |------------| +------------+
747 | Op2 | | Op2 - Op1 |
748 |------------| |------------|
751 PROCEDURE SetSymmetricDifference (tokenno: CARDINAL) ;
755 SetNegate - negates the top set on the stack.
758 +-----------+ +------------+
760 |-----------| |------------|
763 PROCEDURE SetNegate (tokenno: CARDINAL) ;
767 SetShift - if op1 is positive
782 |------------| +------------+
784 |------------| |------------|
788 PROCEDURE SetShift (tokenno: CARDINAL) ;
792 SetRotate - if op1 is positive
794 result := ROTATERIGHT(op2, op1)
796 result := ROTATELEFT(op2, op1)
807 |------------| +------------+
809 |------------| |------------|
812 PROCEDURE SetRotate (tokenno: CARDINAL) ;
816 GetValue - returns and pops the value from the top of stack.
819 PROCEDURE GetValue (tokenno: CARDINAL) : PtrToValue ;
823 GetRange - returns TRUE if range number, n, exists in the value, v.
824 A non empty set is defined by having 1..N ranges
827 PROCEDURE GetRange (v: PtrToValue; n: CARDINAL; VAR low, high: CARDINAL) : BOOLEAN ;
831 ConstructSetConstant - builds a struct of integers which represents the
835 PROCEDURE ConstructSetConstant (tokenno: CARDINAL; v: PtrToValue) : Tree ;
839 BuildRange - returns a integer sized constant which represents the
843 PROCEDURE BuildRange (tokenno: CARDINAL; e1, e2: Tree) : Tree ;
847 IsConstructorDependants - return TRUE if returned if all
848 q(dependants) of, sym, return TRUE.
851 PROCEDURE IsConstructorDependants (sym: CARDINAL; q: IsAction) : BOOLEAN ;
855 WalkConstructorDependants - walk the constructor, sym, calling
856 p for each dependant.
859 PROCEDURE WalkConstructorDependants (sym: CARDINAL; p: WalkAction) ;
863 IsValueAndTreeKnown - returns TRUE if the value is known and the gcc tree
873 |------------| +------------+
876 PROCEDURE IsValueAndTreeKnown () : BOOLEAN ;
880 CheckOrResetOverflow - tests to see whether the tree, t, has caused
881 an overflow error and if so it generates an
885 PROCEDURE CheckOrResetOverflow (tokenno: CARDINAL; t: Tree; check: BOOLEAN) ;
889 AddElements - adds the elements, el BY, n, to the array constant.
893 +------------+ +------------+
895 |------------| |------------|
899 PROCEDURE AddElements (tokenno: CARDINAL; el, n: CARDINAL) ;
903 AddField - adds the field op1 to the underlying constructor.
907 +------------+ +------------+
909 |------------| |------------|
913 PROCEDURE AddField (tokenno: CARDINAL; op1: CARDINAL) ;
917 PushEmptyConstructor - pushes an empty constructor {} onto the ALU stack.
918 This is expected to be filled in by subsequent
919 calls to AddElements, AddRange or AddField.
928 Ptr -> |------------|
932 PROCEDURE PushEmptyConstructor (constype: CARDINAL) ;
936 PushEmptyArray - pushes an empty array {} onto the ALU stack.
937 This is expected to be filled in by subsequent
938 calls to AddElements.
947 Ptr -> |------------|
951 PROCEDURE PushEmptyArray (arraytype: CARDINAL) ;
955 PushEmptyRecord - pushes an empty record {} onto the ALU stack.
956 This is expected to be filled in by subsequent
966 Ptr -> |------------|
970 PROCEDURE PushEmptyRecord (recordtype: CARDINAL) ;
974 ChangeToConstructor - change the top of stack value to a constructor, type.
977 PROCEDURE ChangeToConstructor (tokenno: CARDINAL; constype: CARDINAL) ;
981 IsValueConst - returns true if the memory cell indicated by v
982 is only defined by constants. For example
983 no variables are used in the constructor.
986 PROCEDURE IsValueConst (v: PtrToValue) : BOOLEAN ;
990 PushTypeOfTree - pushes tree, gcc, to the stack and records the
994 PROCEDURE PushTypeOfTree (sym: CARDINAL; gcc: Tree) ;