-
Notifications
You must be signed in to change notification settings - Fork 2
/
m2em.mod
2271 lines (2057 loc) · 78.7 KB
/
m2em.mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*$Segment M2EM*)
(*$RangeCheck+*)
(*$OverflowCheck+*)
IMPLEMENTATION MODULE M2EM; (* Hermann Seiler 19.2.86 / 10.6.86 / 28.4.89 *)
FROM M2Lib IMPORT aTerminateStatus;
FROM M2DM IMPORT
ObjPtr, StrPtr, ParPtr, PDPtr, KeyPtr,
ObjClass, StrForm, Standard, ConstValue, PDesc,
Structure, aAdr, aSize, nilchk,
notyp, undftyp, booltyp, chartyp, cardtyp, cardinttyp,
inttyp, bitstyp, lcardtyp, dbltyp, realtyp, lrltyp,
proctyp, stringtyp, addrtyp, wordtyp, bytetyp,
WordSize, MaxChar, MaxCard, MinInt, MaxInt, MinDouble, MaxDouble;
FROM M2SM IMPORT
Enter, Symbol, Mark, id;
FROM M2HM IMPORT
byte, word, long, quad, StackTop, stackPointer,
Condition, Release, PushAdr, DRegister, dontLoseIndex,
WidType, ItemMode, ItSet, Item, curLev, GenTerminate,
LongVal, WordVal, SimpleC, SignedT, SimpleT, RealT,
Isz, SetregMd, SetconMd, SetglbMd, SetstkMd, SetDregMd,
InvertCC, GenHalt, Branch, PushVal,
LoadAdr, LoadD, LoadX, Move, MoveAdr,
Neg1, Abs1, Cap1, Tst1, Com1, Inc1, Dec1,
Add2, Sub2, And2, Or2, Eor2, Div2, Mod2,
Mul2, Cmp2, In2, ShiType, Shi2, Ash2,
ConvertTyp, ConIndex, VarIndex, GetHigh, Normalize,
CheckHigh, CheckClimit, CheckRange, CheckDbltoSingle,
direction, CheckCPUUsage, CallExt, LoadVal, GetReg, RegAdr, Unlock;
FROM M2FM IMPORT
FMonad, FDyad;
FROM M2LM IMPORT AddBytesToObjectFile, FixLink, MergedLinks, PutOp,
PutDPReference, PutLongReference, PutLinkReference, newLink,
PutStackReference;
FROM M2Sets IMPORT SCEqual, SCLessEqual, SCSlash, SCTimes, SCPlus, SCMinus,
SCClearSet, SCSetBit, SCSetBitRange, SCIn, SCComplementSet;
FROM M2TM IMPORT Find;
FROM M2OMF IMPORT GenGlobalOp;
FROM SYSTEM IMPORT
WORD, ADR, SHIFT;
FROM W65C816 IMPORT JSL, PEA, SEP, REP, LDA, IMMEDIATE, IMMEDIATE3, PHA,
TAY, LSR, STA, DIRECT, TSC, CLC, ADC, TAX, TYA, And, CPY, DEY, BEQ,
ASL, BNE, ABSLONGBYX, STX, TXA, CPX, DEX, DIRINDBYYLG, LDY, PHY, INCACC,
DIRINDLONG, PLA, ORA, PLX, STKRELATIVE;
VAR
mask : ARRAY [ 0 .. 32 ] OF LONGINT;
wtab : ARRAY [ 0..wtabmax ] OF
RECORD
witem, mitem : Item;
END;
PROCEDURE IncAdr(VAR adr: aAdr; s : INTEGER);
BEGIN
IF (adr >= 0) & (s <= MaxInt - adr) OR
(adr < 0) & (s >= MinInt - adr) THEN adr := adr + s
ELSE Mark(211) (* address underflow/overflow *)
END;
END IncAdr;
PROCEDURE AllocVar(obj : ObjPtr; VAR adr: aAdr);
VAR
s: INTEGER;
BEGIN
(* obj^.class = Var *)
WITH obj^ DO
s := VAL(INTEGER, typ^.size);
vadr := adr;
IncAdr(adr, s);
END (*WITH*);
END AllocVar;
PROCEDURE AllocPar(par : ParPtr; VAR adr: aAdr);
CONST
PointerSize = 4;
VAR
s: INTEGER;
BEGIN
WITH par^ DO
s := VAL(INTEGER, typ^.size);
(* negative allocation for parameters *)
IF (typ^.form = Array) & typ^.dyn THEN
(* s = size of formal type for dynamic array parameter *)
IncAdr(adr, -s);
ELSIF varpar THEN
IncAdr(adr, -PointerSize);
ELSE
IncAdr(adr, -s);
END;
END; (*WITH*)
END AllocPar;
PROCEDURE AllocFld(obj : ObjPtr; VAR adr: aAdr);
VAR
s: INTEGER;
BEGIN
(* obj^.class = Field *)
WITH obj^ DO
s := VAL(INTEGER, typ^.size);
offset := adr;
IncAdr(adr, s)
END (*WITH*);
END AllocFld;
PROCEDURE SRTest(VAR x : Item);
BEGIN
WITH x DO
IF typ^.form = Range THEN typ := typ^.RBaseTyp END;
END (*WITH*);
END SRTest;
PROCEDURE setCC(VAR x : Item; fcc : Condition);
(* transform all modes to 'cocMd' : *)
BEGIN
Release(x);
WITH x DO
typ := booltyp; mode := cocMd; CC := fcc;
Tjmp := 0; Fjmp := 0;
END;
END setCC;
PROCEDURE SetBaseLimits(VAR x : Item; VAR lo, hi : INTEGER);
BEGIN
(* x.typ^.form = Set *)
WITH x DO
typ := typ^.SBaseTyp;
IF typ^.form = Enum THEN
lo := 0;
hi := typ^.NofConst - 1;
ELSIF typ^.form = Range THEN
lo := typ^.min;
hi := typ^.max;
typ := typ^.RBaseTyp;
ELSIF typ^.form = Char THEN
lo := 0;
hi := 255;
ELSE (* bitset *)
lo := 0;
hi := 15;
END;
END (*WITH*);
END SetBaseLimits;
PROCEDURE ConnectToWith(VAR x : Item; flev : CARDINAL);
BEGIN
IF (flev > 0) & (flev <= wtabmax) & (flev <= wlev) THEN
(* caution : typ of item is changed. *)
x := wtab[flev].witem;
ELSE
Release(x);
SetglbMd(x, 0, undftyp);
x.name := 1;
END;
END ConnectToWith;
PROCEDURE GenItem(VAR x : Item; y, scope : ObjPtr);
VAR
yy: ObjPtr;
BEGIN
IF y <> NIL THEN
yy := y;
WITH x DO
typ := y^.typ;
CASE y^.class OF
Const : mode := conMd;
val := y^.conval;
| Typ : mode := typMd;
| Var : indir := y^.varpar;
mod := y^.vmod;
lev := y^.vlev;
adr := y^.vadr;
off := 0;
isParam := y^.vpar;
name := y^.name;
indexed := FALSE;
parent := y^.parent;
register := erNone;
notpop := FALSE;
IF mod = 0 THEN
IF lev = 0 THEN (* global *)
mode := glbMd;
ELSE (* local *)
mode := locMd;
END;
ELSE (* external *)
mode := glbMd;
END;
IF (y^.typ^.form = Array) AND
(y^.typ^.dyn) THEN
indir := TRUE;
END;
| Field : ConnectToWith(x, scope^.name);
typ := y^.typ;
IF mode < conMd THEN
off := off + y^.offset;
END;
| Proc : mode := procMd;
proc := yy;
typ := notyp;
| Tool : mode := toolMd;
proc := yy;
typ := notyp;
| Module : SetglbMd(x, 0, typ);
name := 1;
Mark(107);
| Temp : SetglbMd(x, 0, undftyp);
name := 1;
Mark(50);
END (*CASE*);
END (*WITH*);
ELSE
SetglbMd(x, 0, undftyp);
x.name := 1;
Mark(50);
END;
END GenItem;
PROCEDURE GenIndex(VAR x, y : Item);
(*
OPERATION:
Index the array 'x' by 'y'.
The resulting index is placed on the stack, and 'x' is set to indicate that
it's value is to be obtained using an index.
*)
VAR
i: LONGINT;
up, low: LONGINT;
elsize: CARDINAL;
inxt, elet: StrPtr;
z: Item;
BEGIN
SRTest(y);
IF x.typ^.form = Array THEN
elet := x.typ^.ElemTyp;
elsize := elet^.size;
IF x.typ^.dyn THEN
(* dynamic array always descriptor indirect : *)
IF (y.typ = inttyp) OR
(y.typ = cardtyp) OR
(y.typ = cardinttyp) THEN
z := x;
GetHigh(z); (* inhibit change of x *)
(* the minimal width of an index must be word! *)
IF (y.typ^.size = VAL(aSize, 2)) THEN
y.typ := inttyp;
ELSE
Mark(109);
Release(y);
END;
CheckHigh(y, z);
Release(z);
VarIndex(x,y,elsize);
ELSE
Mark(109);
END;
ELSE (* not dyn *)
up := VAL(LONGINT, 0);
low := VAL(LONGINT, 0);
WITH x.typ^.IndexTyp^ DO
IF form = Range THEN
inxt := RBaseTyp;
up := max;
low := min;
ELSIF form = Enum THEN
inxt := x.typ^.IndexTyp;
up := NofConst;
low := VAL(LONGINT, 0);
END;
END (*WITH*);
IF SimpleC(y) THEN
(* constant index : *)
i := LongVal(y);
IF inxt <> y.typ THEN
IF (i < VAL(LONGINT, 0)) OR
((inxt = cardtyp) & (y.typ <> inttyp)) OR
((inxt = inttyp) & (y.typ <> cardtyp)) THEN
Mark(109);
END;
END;
IF ((low <= i) & (i <= up)) &
((low >= VAL(LONGINT, 0)) OR (i <= (VAL(LONGINT, MaxCard) + low))) THEN
i := i - low; (* normalize index to 0 *)
ELSE
Mark(108);
i := VAL(LONGINT, 0);
END;
(* now i >= 0 ! : *)
IF (elsize = 0) OR
(i <= (VAL(LONGINT, MaxCard DIV elsize))) THEN
i := VAL(LONGINT, elsize) * i;
ELSE
Mark(108);
i := VAL(LONGINT, 0);
END;
ConIndex(x, i);
ELSE
(* variable index : *)
IF inxt <> y.typ THEN
IF ((inxt <> cardtyp) OR (y.typ <> inttyp)) &
((inxt <> inttyp) OR (y.typ <> cardtyp)) &
(y.typ <> dbltyp) & (y.typ <> cardinttyp) THEN
Mark(109);
END;
END;
(* the width of an index must be word! *)
IF (y.typ^.size = VAL(aSize, 2)) THEN
IF low < 0 THEN
y.typ := inttyp;
ELSE
y.typ := cardtyp;
END;
ELSIF inxt^.form = Enum THEN
LoadX(y, word);
y.typ := cardtyp;
ELSE
Mark(109);
END;
Normalize(y,low);
(*
After normalization, the index may be considered to be a cardinal,
since we now have an array with bounds: 0 <= y <= (up - low).
By turning it into a cardinal, we only end up doing unsigned
comparisons, which is correct, considering that it is in theory
possible to have an upper bound that is > 32767 (which is -ve for
an integer).
*)
y.typ := cardtyp;
IF (low >= VAL(LONGINT, 0)) OR (up <= low + VAL(LONGINT, MaxCard)) THEN
i := up - low;
ELSE
Mark(106);
i := VAL(LONGINT, 0);
END;
(* Ensure that 'y' is within the correct range *)
CheckClimit(y, i);
(* now generate actual index, and place it on the stack *)
VarIndex(x, y, elsize);
END (*variable index*);
END (*not dyn*);
x.typ := elet;
ELSE (*form <> Array*)
Mark(109);
Release(y);
END;
END GenIndex;
PROCEDURE GenField(VAR x : Item; f : ObjPtr);
BEGIN
WITH x DO
(* typ^.form = Record *)
IF (f <> NIL) & (f^.class = Field) THEN
off := off + f^.offset;
typ := f^.typ;
ELSE
Mark(110);
Release(x);
SetglbMd(x, 0, undftyp);
name := 1;
END;
END (*WITH*);
END GenField;
PROCEDURE GenDeRef(VAR x : Item);
BEGIN
WITH x DO
IF (typ^.form = Pointer) OR (typ = addrtyp) THEN
IF (mode < conMd) & NOT(indir) & NOT(indexed) THEN
indir := TRUE;
adr := adr + off;
off := 0;
ELSIF (mode <> conMd) THEN
LoadD(x);
indir := TRUE; (* LoadVal changes mode to regMd, and indir=FALSE *)
off := 0;
ELSE
Mark(111); (* illegal dereferencing *)
END;
IF typ = addrtyp THEN
typ := bytetyp;
ELSE
typ := typ^.PBaseTyp;
END;
ELSE
Mark(111); (* illegal dereferencing *)
END;
END (*WITH*);
END GenDeRef;
PROCEDURE GenNeg(VAR x : Item);
VAR f : StrForm; lv : LONGINT;
BEGIN SRTest(x);
WITH x DO
f := typ^.form;
IF SimpleC(x) OR ((mode = conMd) & RealT(x)) THEN (* V2.6 *)
lv := LongVal(x);
IF (f = Int) OR ((f = Card) & (lv <= VAL(LONGINT, 32767))) THEN
IF WordVal(x) > MIN(INTEGER) THEN
SetconMd(x, -lv, inttyp);
ELSE
Mark(201);
END;
ELSIF f = Double THEN
IF lv > MIN(LONGINT) THEN
SetconMd(x, -lv, dbltyp)
ELSE
Mark(201);
END;
ELSIF f = Real THEN
val.R := - val.R; (* V2.6 *)
ELSIF f = LongReal THEN (* V2.6 *)
val.X := - val.X; (* V2.6 *)
ELSE Mark(112);
END;
ELSE (* x not a constant *)
IF (f = Int) OR (f = Double) THEN Neg1(x)
ELSIF (f = CardInt) THEN Neg1(x); typ := inttyp (* V2.6 *)
ELSIF (f = Real) OR (f = LongReal) THEN FMonad(NonStand,x)
ELSE Mark(112);
END;
END;
END (*WITH*);
END GenNeg;
PROCEDURE GenNot(VAR x : Item);
VAR t : CARDINAL;
BEGIN SRTest(x);
WITH x DO
IF typ^.form = Bool THEN
IF mode = conMd THEN
val.B := NOT(val.B);
SetconMd(x, VAL(LONGINT, ORD(val.B)), typ);
ELSIF mode = cocMd THEN
CC := InvertCC(CC);
t := Tjmp; Tjmp := Fjmp; Fjmp := t;
ELSE
Tst1(x);
setCC(x,NE);
END;
ELSE
Mark(113);
END;
END (*WITH*);
END GenNot;
PROCEDURE GenAnd(VAR x : Item);
BEGIN SRTest(x);
WITH x DO
IF mode = cocMd THEN
Branch(CC, Fjmp, Tjmp);
FixLink(Tjmp);
ELSIF (typ^.form = Bool) & (mode <> conMd) THEN
Tst1(x);
setCC(x,EQ);
Branch(CC, Fjmp, Tjmp);
FixLink(Tjmp);
ELSIF typ^.form <> Bool THEN
Mark(122);
Release(x);
SetconMd(x, 0, booltyp);
END;
END (*WITH*);
END GenAnd;
PROCEDURE GenOr(VAR x : Item);
BEGIN SRTest(x);
WITH x DO
IF mode = cocMd THEN
Branch(InvertCC(CC), Tjmp, Fjmp);
FixLink(Fjmp);
ELSIF (typ^.form = Bool) & (mode <> conMd) THEN
Tst1(x);
setCC(x,NE);
Branch(CC, Tjmp, Fjmp);
FixLink(Fjmp);
ELSIF typ^.form <> Bool THEN
Mark(125);
Release(x);
SetconMd(x, 1, booltyp);
END;
END (*WITH*);
END GenOr;
PROCEDURE GenSingSet(VAR x, e : Item);
VAR
s: StrPtr;
y: Item;
v: INTEGER;
lo: INTEGER;
hi: INTEGER;
i: INTEGER;
szx: WidType;
loop: CARDINAL;
endOfLoop: CARDINAL;
xadr: DRegister;
savexadr: aAdr;
BEGIN (* x.typ^.form = Set *)
SRTest(e);
Isz(x, szx);
s := x.typ; (* hold original type of set *)
SetBaseLimits(x,lo,hi);
IF (e.typ = x.typ) OR (* V2.6 *)
(((e.typ=inttyp) OR (e.typ=cardinttyp)) & (s = bitstyp)) THEN (* V2.6 *)
IF SimpleC(e) THEN (* e is constant *)
v := WordVal(e);
IF (v >= lo) & (v <= hi) THEN
WITH x DO
mode := conMd;
typ := s;
val.FS.setSize := s^.size;
SCClearSet(val.FS);
SCSetBit(val.FS, v);
END;
ELSE
WITH x DO
mode := conMd;
typ := s;
val.FS.setSize := s^.size;
SCClearSet(val.FS);
END;
Mark(202);
END
ELSE (* e is expression *)
Mark(418);
END;
ELSE
Mark(116);
END;
Release(e);
END GenSingSet;
PROCEDURE GenSet(VAR x, e1, e2 : Item);
VAR
s: StrPtr;
y: Item;
lo, hi, v1, v2: INTEGER;
lv: LONGINT;
szx: WidType;
BEGIN (* x.typ^.form = Set *)
SRTest(e1);
SRTest(e2);
s := x.typ; (* hold original type of set *)
SetBaseLimits(x,lo,hi);
Isz(x, szx);
IF (e1.typ = x.typ) & (e2.typ = x.typ) OR
( ((e1.typ = inttyp) OR (e1.typ = cardinttyp)) & (* V2.6 *)
((e2.typ = inttyp) OR (e2.typ = cardinttyp)) & (* V2.6 *)
(s = bitstyp) ) THEN (* V2.6 *)
IF SimpleC(e1) & SimpleC(e2) THEN
(* constant set-constructor : *)
v1 := WordVal(e1);
v2 := WordVal(e2);
IF ((lo <= v2) & (v2 <= hi)) & (v1 <= v2) & (hi <= 255) THEN
WITH x DO
typ := s;
mode := conMd;
val.FS.setSize := s^.size;
SCClearSet(val.FS);
SCSetBitRange(val.FS, v1, v2);
END;
ELSE
SetconMd(x, 0, s);
Mark(202);
END
ELSE
Mark(417);
END;
ELSE
Mark(116);
END;
Release(e1);
Release(e2);
END GenSet;
PROCEDURE GenIn(VAR x, y : Item);
VAR
f: StrForm;
s: StrPtr;
v, lo, hi: INTEGER;
b: BOOLEAN;
BEGIN
SRTest(x);
f := x.typ^.form;
s := y.typ; (* hold original type of set *)
IF ((Bool <= f) & (f <= Enum)) & (y.typ^.form = Set) THEN
SetBaseLimits(y,lo,hi);
IF (x.typ = y.typ) OR (x.typ = inttyp) & (s = bitstyp) (* V2.6 *)
OR (x.typ = cardinttyp) & (s = bitstyp) THEN (* V2.6 *)
y.typ := s; (* recover type of set *)
IF SimpleC(x) THEN (* constant element x *)
v := WordVal(x);
IF (v >= lo) & (v <= hi) & (hi <= 255) THEN
IF y.mode = conMd THEN (* constant set y *)
b := SCIn(v, y.val.FS);
SetconMd(x, VAL(LONGINT, ORD(b)), booltyp);
ELSE (* variable set y *)
In2(x,y);
setCC(x,EQ);
END;
ELSE
SetconMd(x, 0, booltyp);
Mark(202);
END;
ELSE (* variable element x *)
CheckRange(x,lo,hi,0);
In2(x,y);
setCC(x,EQ);
END;
ELSE
Release(x);
SetconMd(x, 0, booltyp);
Mark(114);
END;
ELSE
Release(x);
SetconMd(x, 0, booltyp);
Mark(115);
END;
x.typ := booltyp;
Release(y);
END GenIn;
PROCEDURE GenWith(VAR x : Item; VAR fadr: aAdr);
VAR
xt: StrPtr;
BEGIN
INC(wlev);
IF wlev > wtabmax THEN
Mark(236);
Release(x);
RETURN;
END;
xt := x.typ; (* holds original type of record *)
WITH wtab[wlev] DO
x.typ := xt;
Release(x);
PushAdr(x);
x.notpop := TRUE;
x.indir := TRUE;
witem := x;
END (*WITH wtab*);
END GenWith;
PROCEDURE GenWith2;
BEGIN
IF wlev > 0 THEN
IF wlev <= wtabmax THEN
WITH wtab[wlev] DO
IF witem.notpop THEN
StackTop(4); (* Pull the address back off the stack *)
END;
END (*WITH wtab*);
END;
DEC(wlev);
END;
END GenWith2;
PROCEDURE InitM2EM;
VAR k : CARDINAL; exp : LONGINT;
BEGIN
wlev := 0;
exp := 0;
mask[0] := 0;
mask[32] := -1;
FOR k := 1 TO 31 DO
exp := exp + exp + VAL(LONGINT, 1);
mask[k] := exp;
END;
END InitM2EM;
PROCEDURE GenOp(op : Symbol; VAR x, y : Item);
VAR f, g : StrForm; c : Condition;
xval, yval : ConstValue; xx : Item;
BEGIN
SRTest(x); f := x.typ^.form;
SRTest(y); g := y.typ^.form;
IF x.typ <> y.typ THEN
IF ((f = Int) OR (f = CardInt)) & (g = Card) & (y.mode = conMd)
& (y.val.C <= MaxInt) THEN y.typ := x.typ
ELSIF (f = Card) & ((g = Int) OR (g = CardInt)) & (x.mode = conMd)
& (x.val.C <= MaxInt) THEN x.typ := y.typ; f := Int
ELSIF (f = Double) & (g = LCard) & (x.mode = conMd) THEN (* V2.6zz*)
x.typ := y.typ; f := LCard (* V2.6zz*)
ELSIF (f = LCard) & (g = Double) & (y.mode = conMd) THEN (* V2.6zz*)
y.typ := x.typ (* V2.6zz*)
ELSIF (x.typ = addrtyp) & (g = Pointer) THEN f := Pointer
ELSIF (g = CardInt) & ((f = Int) OR (f = Card)) THEN
g := f; y.typ := x.typ
ELSIF (f = CardInt) & ((g = Int) OR (g = Card)) THEN
f := g; x.typ := y.typ
ELSIF ((f = LCard) OR (f = Double)) &
((y.mode = conMd) & ((g = Int) OR (g = Card) OR (g = CardInt))) THEN
g := f;
SetconMd(y, LongVal(y), x.typ);
ELSIF ((g = LCard) OR (g = Double)) &
((x.mode = conMd) & ((f = Int) OR (f = Card) OR (f = CardInt))) THEN
f := g;
SetconMd(x, LongVal(x), y.typ);
ELSIF ((f <> Pointer) OR (y.typ <> addrtyp)) &
((f <> Double) OR (g <> Double)) THEN Mark(117);
END;
END;
IF (x.mode = conMd) & (y.mode = conMd) THEN (* V2.6 *)
xval := x.val; yval := y.val;
CASE op OF
times: IF f = Card THEN
IF (xval.C = 0) OR (yval.C <= MaxCard DIV xval.C) THEN
xval.C := xval.C * yval.C;
ELSE
Mark(203);
END;
ELSIF f = Int THEN
IF (xval.I = 0) OR
((y.val.I <> MIN(INTEGER)) & (x.val.I <> MIN(INTEGER)) &
(ABS(yval.I) <= MaxInt DIV ABS(xval.I))) THEN
xval.I := xval.I * yval.I;
ELSE
Mark(203);
END;
(* The next three components of the if statement have been changed to *)
(* avoid a possible divide by zero problem. Instead of assuming that *)
(* the compiler compiling this code doesn't go past the first true *)
(* check in an if statement, we must separate the (val = 0) condition *)
(* from the rest of the conditions. *)
ELSIF f = Double THEN
IF (xval.D = VAL(LONGINT, 0)) THEN
xval.D := xval.D * yval.D;
ELSIF ((y.val.D <> MIN(LONGINT)) &
(x.val.D <> MIN(LONGINT)) &
(ABS(yval.D) <= MaxDouble DIV ABS(xval.D))) THEN
xval.D := xval.D * yval.D;
ELSE
Mark(203);
END
ELSIF f = Real THEN
IF (xval.R = 0.0) THEN
xval.R := xval.R * yval.R;
ELSIF (ABS(xval.R) <= FLOAT(1)) OR
(ABS(yval.R) <= ABS(MAX(REAL)) / ABS(xval.R)) THEN
xval.R := xval.R * yval.R;
ELSE Mark(203)
END
ELSIF f = LongReal THEN (* V2.6 *)
IF (xval.X = FLOATD(0)) THEN
xval.X := xval.X * yval.X; (* V2.6 *)
ELSIF (ABS(xval.X) <= FLOATD(1)) OR (* V2.6 *)
(ABS(yval.X) <= ABS(MAX(LONGREAL))
/ ABS(xval.X)) THEN (* V2.6 *)
xval.X := xval.X * yval.X; (* V2.6 *)
ELSE Mark(203) (* V2.6 *)
END (* V2.6 *)
ELSIF f = Set THEN
SCTimes(xval.FS, yval.FS);
ELSE Mark(118)
END;
| slash: IF f = Real THEN (* V2.6E*)
IF (ABS(yval.R) >= FLOAT(1)) OR ((yval.R <> 0.0) & (* V2.6E*)
(ABS(xval.R) <= ABS(yval.R) * ABS(MAX(REAL)))) THEN
xval.R := xval.R / yval.R;
ELSE Mark(204)
END
ELSIF f = LongReal THEN (* V2.6 *)
IF (ABS(yval.X) >= FLOATD(1)) OR ((yval.X <> FLOATD(0)) & (* V2.6 *)
(ABS(xval.X) <= ABS(yval.X) * ABS(MAX(LONGREAL)))) THEN (* V2.6 *)
xval.X := xval.X / yval.X; (* V2.6 *)
ELSE Mark(204) (* V2.6 *)
END (* V2.6 *)
ELSIF f = Set THEN
SCSlash(xval.FS, yval.FS);
ELSE Mark(119)
END;
| div: IF f = Card THEN
IF yval.C > 0 THEN
xval.C := xval.C DIV yval.C;
ELSE Mark(205)
END
ELSIF f = Int THEN
IF yval.I <> 0 THEN
xval.I := xval.I DIV yval.I;
ELSE Mark(205)
END
ELSIF f = Double THEN
IF yval.D <> VAL(LONGINT, 0) THEN
xval.D := xval.D DIV yval.D;
ELSE Mark(205)
END
ELSE Mark(120)
END;
| rem: IF (f = Int) OR (f = Card) OR (f = Double) THEN (* V2.6 *)
Mark(200) (* V2.6 *)
ELSIF (f = Real) OR (f = LongReal) THEN (* V2.6 *)
Mark(200) (* V2.6 *)
ELSE Mark(121) (* V2.6 *)
END; (* V2.6 *)
| mod: IF f = Card THEN
IF yval.C > 0 THEN
xval.C := xval.C MOD yval.C;
ELSE Mark(205)
END
ELSIF f = Int THEN
IF (xval.I >= 0) & (yval.I > 0) THEN
xval.I := xval.I MOD yval.I;
ELSE Mark(205)
END
ELSIF f = Double THEN
IF (xval.D >= VAL(LONGINT, 0)) & (yval.D > VAL(LONGINT, 0)) THEN
xval.D := xval.D MOD yval.D;
ELSE Mark(205)
END
ELSIF (f = Real) OR (f = LongReal) THEN (* V2.6 *)
Mark(200) (* V2.6 *)
ELSE Mark(121)
END;
| and: IF f = Bool THEN xval.B := xval.B AND yval.B
ELSE Mark(122)
END;
| plus: IF f = Card THEN
IF yval.C <= MaxCard - xval.C THEN
xval.C := xval.C + yval.C;
ELSE Mark(206)
END
ELSIF f = Int THEN
IF (xval.I >= 0) & (yval.I <= MaxInt - xval.I) OR
(xval.I < 0) & (yval.I >= MIN(INTEGER) - xval.I) THEN
xval.I := xval.I + yval.I;
ELSE Mark(206)
END
ELSIF f = Real THEN
IF (xval.R >= 0.0) & (yval.R <= ABS(MAX(REAL)) - xval.R) OR
(xval.R < 0.0) & (yval.R >= MIN(REAL) - xval.R) THEN
xval.R := xval.R + yval.R;
ELSE Mark(206)
END
ELSIF f = LongReal THEN (* V2.6 *)
IF (xval.X >= FLOATD(0)) & (yval.X <= ABS(MAX(LONGREAL)) - xval.X) OR
(xval.X < FLOATD(0)) & (yval.X >= MIN(LONGREAL) - xval.X) THEN
xval.X := xval.X + yval.X; (* V2.6 *)
ELSE Mark(206) (* V2.6 *)
END (* V2.6 *)
ELSIF f = Set THEN
SCPlus(xval.FS, yval.FS);
ELSIF (f = Double) OR (f = LCard) THEN
xval.D := xval.D + yval.D;
ELSE Mark(123)
END;
| minus: IF f = Card THEN
IF yval.C <= xval.C THEN xval.C := xval.C - yval.C
ELSIF yval.C - xval.C <= MaxInt THEN
xval.I := - VAL(INTEGER, yval.C - xval.C);
x.typ := inttyp; (* typ is changed! *)
ELSE Mark(207)
END
ELSIF f = Int THEN
IF (xval.I >= 0) &
((yval.I >= 0) OR (xval.I <= MaxInt + yval.I)) OR
(xval.I < 0) &
((yval.I < 0) OR (xval.I >= MIN(INTEGER) + yval.I)) THEN
xval.I := xval.I - yval.I;
ELSE Mark(207)
END
ELSIF f = Real THEN
IF (xval.R >= 0.0) &
((yval.R >= 0.0) OR (xval.R <= ABS(MAX(REAL)) + yval.R)) THEN
xval.R := xval.R - yval.R;
ELSIF (xval.R < 0.0) &
((yval.R < 0.0) OR (xval.R >= MIN(REAL) + yval.R)) THEN
xval.R := xval.R - yval.R;
ELSE Mark(207)
END
ELSIF f = LongReal THEN (* V2.6 *)
IF (xval.X >= FLOATD(0)) & (* V2.6 *)
((yval.X >= FLOATD(0)) OR (xval.X <= ABS(MAX(LONGREAL)) + yval.X)) THEN
xval.X := xval.X - yval.X; (* V2.6 *)
ELSIF (xval.X < FLOATD(0)) & (* V2.6 *)
((yval.X < FLOATD(0)) OR (xval.X >= MIN(LONGREAL) + yval.X)) THEN
xval.X := xval.X - yval.X; (* V2.6 *)
ELSE Mark(207) (* V2.6 *)
END (* V2.6 *)
ELSIF f = Set THEN
SCMinus(xval.FS, yval.FS);
ELSIF (f = Double) OR (f = LCard) THEN
xval.D := xval.D - yval.D;
ELSE Mark(124)
END;
| or: IF f = Bool THEN xval.B := xval.B OR yval.B
ELSE Mark(125)
END;
| eql: IF f = Card THEN xval.B := xval.C = yval.C;
ELSIF f = Int THEN xval.B := xval.I = yval.I;
ELSIF f = Real THEN xval.B := xval.R = yval.R;
ELSIF f = Bool THEN xval.B := xval.B = yval.B;
ELSIF f = Char THEN xval.B := xval.Ch = yval.Ch;
ELSIF f = Double THEN xval.B := xval.D = yval.D;
ELSIF f = Set THEN xval.B := SCEqual(xval.FS, yval.FS);
ELSIF f = Enum THEN xval.B := xval.Ch = yval.Ch; (* V2.6 *)
ELSIF f = LongReal THEN xval.B := xval.X = yval.X; (* V2.6 *)
ELSE Mark(126)
END;
x.typ := booltyp;
| neq: IF f = Card THEN xval.B := xval.C <> yval.C;
ELSIF f = Int THEN xval.B := xval.I <> yval.I;
ELSIF f = Real THEN xval.B := xval.R <> yval.R;
ELSIF f = Bool THEN xval.B := xval.B <> yval.B;
ELSIF f = Char THEN xval.B := xval.Ch <> yval.Ch;
ELSIF f = Double THEN xval.B := xval.D <> yval.D;
ELSIF f = Set THEN xval.B := NOT SCEqual(xval.FS, yval.FS);
ELSIF f = Enum THEN xval.B := xval.Ch <> yval.Ch; (* V2.6 *)
ELSIF f = LongReal THEN xval.B := xval.X <> yval.X; (* V2.6 *)
ELSE Mark(126)
END;
x.typ := booltyp;
| lss: IF f = Card THEN xval.B := xval.C < yval.C;
ELSIF f = Int THEN xval.B := xval.I < yval.I;
ELSIF f = Real THEN xval.B := xval.R < yval.R;
ELSIF f = Bool THEN xval.B := xval.B < yval.B;
ELSIF f = Char THEN xval.B := xval.Ch < yval.Ch;
ELSIF f = Double THEN xval.B := xval.D < yval.D;
ELSIF f = Enum THEN xval.B := xval.Ch < yval.Ch; (* V2.6 *)
ELSIF f = LongReal THEN xval.B := xval.X < yval.X; (* V2.6 *)
ELSE Mark(126)
END;
x.typ := booltyp;
| leq: IF f = Card THEN xval.B := xval.C <= yval.C;
ELSIF f = Int THEN xval.B := xval.I <= yval.I;
ELSIF f = Real THEN xval.B := xval.R <= yval.R;
ELSIF f = Bool THEN xval.B := xval.B <= yval.B;
ELSIF f = Char THEN xval.B := xval.Ch <= yval.Ch;
ELSIF f = Double THEN xval.B := xval.D <= yval.D;
ELSIF f = Set THEN xval.B := SCLessEqual(xval.FS, yval.FS);
ELSIF f = Enum THEN xval.B := xval.Ch <= yval.Ch; (* V2.6 *)
ELSIF f = LongReal THEN xval.B := xval.X <= yval.X; (* V2.6 *)
ELSE Mark(126)
END;
x.typ := booltyp;