-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm2hm.mod
6032 lines (5174 loc) · 150 KB
/
m2hm.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 M2HMPart1*)
(*$RangeCheck+*)
(*$OverflowCheck+*)
IMPLEMENTATION MODULE M2HM;
(* M2HM-implementation for the W65C816 processor. *)
FROM M2DM IMPORT
ObjPtr, StrPtr, StrForm, ConstValue, PDesc,
Structure, aAdr, mainmod, aSize,
notyp, undftyp, booltyp, chartyp, cardtyp,
inttyp, bitstyp, dbltyp, realtyp, lrltyp,
proctyp, stringtyp, addrtyp, wordtyp, bytetyp,
WordSize, MinInt, MaxCard, stackchk, nilchk,
rngchk, ovflchk, WorkingStorage, DirectPageSize;
FROM M2FM IMPORT
LoadF, ExtendedSize;
FROM M2Lib IMPORT
aProcessDescriptor, aTerminateStatus, LoWORD, HighWORD, LongWORD;
FROM M2LM IMPORT
aSymbol, GetModuleKey,
IdToSymbol, AddBytesToObjectFile, localStringList, PutLongReference,
GetVariableName, GetModuleName, newLink, FixLink, AllocChar,
PutLinkReference, GetProcedureName, GetStringName, PutOp,
globalStringList, PutDPReference, CDAName, CDAShutdown, Stacksize,
NDAOpen, NDAClose, NDAAction, NDAPeriod, NDAMask, NDAMenuline,
PutStackReference, PutDisplayReference, CDEVName;
FROM M2OMF IMPORT GenGlobalOp, PutByte, ReferenceLabel, PutLabelText,
EndSegment, aSegmentKind, aSegmentAttribute, aSegmentAttributeSet,
NewSegmentHeader, GenLocalOp, objFile, PutByteConstant,
IncrementSegmentLength;
FROM M2Sets IMPORT SCClearSet;
FROM M2SM IMPORT
Symbol, Mark, aDirective, aDirectiveSet, Directives;
FROM Strings IMPORT Concat, Length;
FROM SYSTEM IMPORT
ADR, BYTE, WORD, SHIFT, TSIZE;
FROM W65C816 IMPORT TSC, CLC, SBC, ABSLONG, BPL, SEP, REP, PHA, PHX, BCC, BEQ,
BNE, BCS, BVS, BVC, BMI, BRL, LDA, IMMEDIATE, IMMEDIATE3, BRA, PHK, PER, PEA,
JMP, JABS, PHB, TAY, INCACC, TCS, TYA, ADC, LDY, DIRECT, LDX, DIRINDBYYLG,
TAX, DEY, DIRINDLONG, PLA, STY, TXY, TYX, And, DIRECTBYX, TDC, ABSLONGBYX,
PLX, STKRELATIVE, STX, TXA, PLY, ASL, ROL, LSR, SEC, ROR, ORA, EOR, CMP,
CPX, DECACC, DEX, INX, JSL, RTS, PHD, PLD, DIRINDIRECT, DIRINDBYY, STKRELBYY,
JML, JABSLIND, JABSL, BITIMMED, STA, DIRECTBYY, TCD, PLB, PHP, PLP, PHY;
IMPORT W65C816;
CONST
(* WD65C816 instruction mnemonics. *)
(* ______________________________ *)
(* Left shift constants. *)
LS3 = 10B; LS4 = 20B; LS5 = 40B; LS6 = 100B;
LS7 = 200B; LS8 = 400B; LS9 = 1000B; LS10 = 2000B;
LS11 = 4000B; LS12 = 10000B;
TYPE
aCpuReg = (YReg, XReg, AReg, AddToA);
VAR
mask : ARRAY [ 0 .. 32 ] OF LONGINT;
hightyp : StrPtr;
registers : DRegSet;
lockItem : POINTER TO Item;
bitMask : ARRAY [0..15] OF CARDINAL;
(*$RangeCheck-*)
PROCEDURE Locked(reg: DRegister): BOOLEAN;
BEGIN
RETURN (reg IN registers);
END Locked;
PROCEDURE Lock(reg: DRegister);
BEGIN
IF Locked(reg) THEN
Mark(178);
ELSE
INCL(registers, reg);
IF reg = erQuad1 THEN
Lock(er6);
Lock(er7);
ELSIF reg = erQuad2 THEN
Lock(er1);
Lock(er2);
END;
END;
END Lock;
PROCEDURE Unlock(reg: DRegister);
BEGIN
IF Locked(reg) THEN
EXCL(registers, reg);
IF reg = erQuad1 THEN
Unlock(er6);
Unlock(er7);
ELSIF reg = erQuad2 THEN
Unlock(er1);
Unlock(er2);
ELSIF reg = erCPU THEN
lockItem := NIL;
END;
END;
END Unlock;
PROCEDURE Release(VAR x: Item);
BEGIN
WITH x DO
IF mode IN ItSet{DregMd, regMd} THEN
Unlock(register);
END;
IF (mode < conMd) AND indexed THEN
Unlock(indexReg);
END;
END;
END Release;
(* $RangeCheck+*)
PROCEDURE RegAdr(reg: DRegister): aAdr;
BEGIN
CASE reg OF
erNone: RETURN 0; (* long *)
|
er1: RETURN 8; (* long *)
|
er2: RETURN 12; (* long *)
|
er3: RETURN 16; (* word *)
|
er4: RETURN 18; (* word *)
|
er5: RETURN 20; (* word *)
|
er6: RETURN 0; (* long *)
|
er7: RETURN 4; (* long *)
|
erQuad1: RETURN 0; (* quadword *)
|
erQuad2: RETURN 8; (* quadword *)
END;
END RegAdr;
PROCEDURE GetReg(size: WidType; VAR reg: DRegister);
BEGIN
reg := erNone;
IF size = long THEN
IF Locked(er1) THEN
IF Locked(er2) THEN
IF Locked(er6) THEN
IF Locked(er7) THEN
Mark(185);
ELSE
reg := er7;
END;
ELSE
reg := er6;
END;
ELSE
reg := er2;
END;
ELSE
reg := er1;
END;
ELSIF size = quad THEN
IF Locked(er1) AND Locked(er2) THEN
IF Locked(er6) AND Locked(er7) THEN
Mark(186);
ELSE
reg := erQuad1;
END;
ELSE
reg := erQuad2;
END;
ELSE
IF Locked(er3) THEN
IF Locked(er4) THEN
IF Locked(er5) THEN
Mark(184);
ELSE
reg := er5;
END;
ELSE
reg := er4;
END;
ELSE
reg := er3;
END;
END;
IF reg <> erNone THEN
Lock(reg);
END;
END GetReg;
PROCEDURE ProcessorID(VAR id: Processor);
BEGIN
id := "WD65C816";
END ProcessorID;
PROCEDURE IncAdr(incval: CARDINAL);
VAR
skip: CARDINAL;
BEGIN
IF incval <> 0 THEN
newLink(skip);
IF incval = 1 THEN
PutOp(INCACC, 1);
PutLinkReference(BNE, skip, 2);
ELSE
PutOp(CLC, 1);
PutDPReference(ADC+IMMEDIATE, incval, 3);
PutLinkReference(BCC, skip, 2);
END;
PutOp(INX, 1);
FixLink(skip);
END;
END IncAdr;
PROCEDURE GenModulaEntryCode;
(*
OPERATION:
Will generate the entry code for a modula style procedure call.
As much as possible the library routines will be written in modula
and linked in.
To call these routines, the compiler must generate code that pushes the
parameters onto the stack correctly. It must then call this routine,
and then finally, it should generate a JSL to the specified routine.
*)
BEGIN
END GenModulaEntryCode;
PROCEDURE GenStackCheck(SPLoaded: BOOLEAN);
VAR
noOverflow: CARDINAL;
BEGIN
IF stackchk THEN
IF NOT SPLoaded THEN
PutOp(TSC, 1);
END;
newLink(noOverflow);
PutOp(SEC, 1);
GenGlobalOp(SBC+ABSLONG, 'M2Lib_StackBottom', 0, 0, 3);
AddBytesToObjectFile(4);
PutLinkReference(BPL, noOverflow, 2);
GenTerminate(tsStackOverflow);
FixLink(noOverflow);
END;
END GenStackCheck;
PROCEDURE GenNILCheck(reg: DRegister; onStack: BOOLEAN);
VAR
notNIL: CARDINAL;
NILptr: CARDINAL;
tmpReg: DRegister;
BEGIN
IF nilchk THEN
newLink(notNIL);
newLink(NILptr);
IF onStack THEN
(*
For the moment, assume that the erCPU registers are clear, and that the
address is on the top of the stack.
*)
PutStackReference(LDA+STKRELATIVE, 1);
PutStackReference(ORA+STKRELATIVE, 3);
PutLinkReference(BEQ, NILptr, 2);
PutLinkReference(BRA, notNIL, 2);
ELSIF reg = erCPU THEN
GetReg(word, tmpReg);
PutDPReference(STA+DIRECT, RegAdr(tmpReg), 2);
PutOp(TXA, 1);
PutDPReference(ORA+DIRECT, RegAdr(tmpReg), 2);
PutLinkReference(BEQ, NILptr, 2);
PutDPReference(LDA+DIRECT, RegAdr(tmpReg), 2);
PutLinkReference(BRA, notNIL, 2);
Unlock(tmpReg);
ELSE
PutDPReference(LDA+DIRECT, RegAdr(reg), 2);
PutDPReference(ORA+DIRECT, RegAdr(reg)+2, 2);
PutLinkReference(BNE, notNIL, 2);
END;
FixLink(NILptr);
GenTerminate(tsAddressOverflow);
FixLink(notNIL);
END;
END GenNILCheck;
PROCEDURE ShortM;
BEGIN
PutDPReference(SEP, 20H, 2); (* Short accumulator and memory *)
END ShortM;
PROCEDURE LongM;
BEGIN
PutDPReference(REP, 20H, 2); (* Long accumulator and memory *)
END LongM;
PROCEDURE CheckCPUUsage(VAR x: Item);
(*
OPERATION:
If the CPU registers are locked, then push the item that has them locked
onto the stack, and release them.
*)
VAR
szx: WidType;
BEGIN
IF Locked(erCPU) THEN (* CPU registers are in use *)
IF NOT errorInProc THEN
IF (lockItem <> NIL) AND
(ADR(x) <> lockItem) THEN
Isz(lockItem^, szx);
IF szx = byte THEN
ShortM;
PutOp(PHA, 1);
LongM;
ELSIF szx = word THEN
PutOp(PHA, 1);
ELSIF szx = long THEN
PutOp(PHX, 1);
PutOp(PHA, 1);
ELSE
Mark(401);
END;
INC(stackPointer, lockItem^.typ^.size);
SetstkMd(lockItem^, lockItem^.typ);
Unlock(erCPU);
END;
ELSE
Unlock(erCPU);
END;
END;
END CheckCPUUsage;
PROCEDURE LoadParameters(VAR x, y: Item; xIntoReal: BOOLEAN);
(*
OPERATION:
This procedure should be used by any low level code generation routines
that manipulate two Items. It will load one parameter into a DRegister,
and the other into the real registers.
The calling procedure may specify which parameter gets to reside in the
real registers using the third parameter.
*)
BEGIN
IF xIntoReal THEN
IF x.mode = regMd THEN
LoadD(x);
END;
LoadD(y);
LoadVal(x, erCPU);
ELSE
IF y.mode = regMd THEN
LoadD(y);
END;
LoadD(x);
LoadVal(y, erCPU);
END;
END LoadParameters;
PROCEDURE Assert(ok : BOOLEAN);
BEGIN
IF NOT ok THEN Mark(299) END;
END Assert;
PROCEDURE SignedT(VAR x : Item) : BOOLEAN;
(* is x a signed type ? *)
(* Note : Real/LongReal excluded! *)
VAR
s : StrPtr;
BEGIN
s := x.typ; (* let x.typ unchanged *)
IF s^.form = Range THEN
s := s^.RBaseTyp;
END;
RETURN (s = inttyp) OR (s = dbltyp);
END SignedT;
PROCEDURE SimpleT(VAR x : Item) : BOOLEAN;
(* is x a simple type of size *)
(* byte/word/long ? *)
(* Note : Real/LongReal excluded! *)
VAR
f: StrForm;
s: StrPtr;
sz: CARDINAL;
szx: WidType;
BEGIN
s := x.typ; (* let x.typ unchanged *)
IF s^.form = Range THEN
s := s^.RBaseTyp;
END;
f := s^.form;
sz := VAL(CARDINAL, s^.size);
Isz(x, szx);
RETURN ((f = Set) AND (szx <> quad))
OR
((sz IN {1,2,3,4}) AND ((f <= Double) OR
(f = Pointer) OR
(f = ProcTyp) OR
(f = Opaque)));
END SimpleT;
PROCEDURE RealT(VAR x : Item) : BOOLEAN;
(* is x a floating-point-type ? *)
(* (REAL or LONGREAL) *)
(* Note: floating-point-types are *)
(* NOT considered as simple *)
VAR
s: StrPtr;
BEGIN
s := x.typ; (* let x.typ unchanged *)
RETURN (s = realtyp) OR (s = lrltyp);
END RealT;
PROCEDURE SimpleC(VAR x : Item) : BOOLEAN;
(* is x a simple constant of size *)
(* byte/word/long ? *)
(* Note : Real/LongReal excluded! *)
BEGIN
RETURN (x.mode = conMd) & SimpleT(x);
END SimpleC;
PROCEDURE LongVal(VAR x : Item) : LONGINT;
VAR
r: LONGINT;
BEGIN
r := 0;
WITH x DO
Assert(mode = conMd);
IF typ^.form = Range THEN
typ := typ^.RBaseTyp;
END;
CASE typ^.form OF
Undef : IF typ^.size = VAL(aSize, 1) THEN
r := VAL(LONGINT, val.Ch); (* V2.6 *)
ELSIF typ^.size = VAL(aSize, 2) THEN
r := VAL(LONGINT, val.C); (* V2.6 *)
ELSE
r := val.U;
END; (* V2.6 *)
| Bool : r := VAL(LONGINT, val.B); (* V2.6 *)
| Char : r := VAL(LONGINT, val.Ch); (* V2.6 *)
| Card, CardInt: r := VAL(LONGINT, val.C); (* V2.6 *)
| Int : r := VAL(LONGINT, val.I); (* V2.6 *)
| Enum : r := VAL(LONGINT, val.Ch); (* V2.6 *)
| Set : r := LongWORD(VAL(CARDINAL, val.FS.set[0]),
VAL(CARDINAL, val.FS.set[1]));
| LCard,Double : r := val.D; (* V2.6 *)
| Real : r := val.D; (* V2.6 - P.E. Should be val.R *)
ELSE r := val.D; (* String, etc. *) (* V2.6 *)
END;
END; (*WITH*)
RETURN r;
END LongVal;
PROCEDURE WordVal(VAR x : Item) : INTEGER;
VAR
r: INTEGER;
BEGIN r := 0;
WITH x DO
Assert(mode = conMd);
IF typ^.form = Range THEN
typ := typ^.RBaseTyp;
END;
CASE typ^.form OF
Undef : IF typ^.size = VAL(aSize, 1) THEN
r := ORD(val.Ch); (* V2.6 *)
ELSIF typ^.size = VAL(aSize, 2) THEN
r := VAL(INTEGER,val.C); (* V2.6 *)
ELSE
r := VAL(INTEGER, val.U);
END; (* V2.6 *)
| Bool : r := ORD(val.B); (* V2.6 *)
| Char : r := ORD(val.Ch); (* V2.6 *)
| Card, CardInt: r := val.I; (* V2.6 - P.E. Should be VAL(INTEGER, val.C) *)
| Int : r := val.I; (* V2.6 *)
| Enum : r := ORD(val.Ch); (* V2.6 *)
| Set : r := VAL(INTEGER, val.FS.set[0]);
| LCard,Double : r := VAL(INTEGER, val.D); (* V2.6 *)
| Real : r := VAL(INTEGER, val.D); (* V2.6 - P.E. Should be val.R *)
ELSE r := VAL(INTEGER, val.D); (* String, etc. *) (* V2.6 *)
END;
END; (*WITH*)
RETURN r;
END WordVal;
PROCEDURE Isz(VAR x : Item; VAR fsz : WidType);
(* instruction size for item x : byte/word/long. *)
(* Note : callable only for simple types ! *)
VAR
s: CARDINAL;
BEGIN
(*
It's possible that due to some error being detected that an item may not
be set up correctly, so allow for it.
*)
IF x.typ^.size <= MAX(CARDINAL) THEN
s := x.typ^.size;
ELSE
s := 0; (* causes error to be displayed *)
END;
IF s = 1 THEN
fsz := byte;
ELSIF s = 2 THEN
fsz := word;
ELSIF s = 4 THEN
fsz := long;
ELSIF x.typ^.form = Set THEN
fsz := setsized;
ELSIF s = 8 THEN
fsz := quad;
ELSE
fsz := long;
Mark(238); (* invalid instruction size *)
END;
END Isz;
PROCEDURE SetglbMd(VAR x : Item; fadr: aAdr; ftyp : StrPtr);
(* setup of an item designating a global variable *)
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
mode := glbMd;
mod := 0;
lev := 0;
adr := fadr;
off := 0;
isParam := FALSE;
indir := FALSE;
name := 0;
notpop := FALSE;
indexed := FALSE;
indexReg := erNone;
register := erNone;
notpop := FALSE;
parent := NIL;
END (*WITH*);
END SetglbMd;
PROCEDURE SetlocMd(VAR x : Item; fadr: aAdr; ftyp : StrPtr);
(* setup of an item which is a local variable *)
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
mode := locMd;
mod := 0;
lev := curLev;
adr := fadr;
off := 0;
isParam := FALSE;
indir := FALSE;
name := 0;
notpop := FALSE;
indexed := FALSE;
indexReg := erNone;
register := erNone;
notpop := FALSE;
parent := NIL;
END (*WITH*);
END SetlocMd;
PROCEDURE SetconMd(VAR x : Item; fval : LONGINT; ftyp : StrPtr);
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
IF typ^.form = Range THEN
typ := typ^.RBaseTyp;
END;
mode := conMd;
val.U := VAL(LONGINT, 0);
CASE typ^.form OF
(*
If something is undefined, then an error must have occured. Therefor,
it shouldn't matter what the constant is. For safety's sake, we will
set it to zero.
*)
Undef : IF typ^.size = VAL(aSize, 1) THEN
val.Ch := VAL(CHAR, fval);
ELSIF typ^.size = VAL(aSize, 2) THEN
val.C := VAL(CARDINAL, fval);
ELSE
val.U := fval;
END;
| Bool : val.B := VAL(BOOLEAN, fval); (* V2.6 *)
| Char : val.Ch := VAL(CHAR, fval); (* V2.6 *)
| Card, (* V2.6 *)
CardInt : val.C := VAL(CARDINAL, fval); (* V2.6 *)
| Int : val.I := VAL(INTEGER, fval); (* V2.6 *)
| Enum : val.Ch := VAL(CHAR, fval); (* V2.6 *)
| LCard : val.D := fval; (* V2.6 *)
| Double : val.D := fval; (* V2.6 *)
| Real : val.R := VAL(REAL, fval); (* V2.6 *)
| Set : val.FS.setSize := typ^.size;
SCClearSet(val.FS);
val.FS.set[0] := VAL(BITSET, LoWORD(fval));
val.FS.set[1] := VAL(BITSET, HighWORD(fval));
ELSE val.D := fval; (* String, etc. V2.6 *)
END;
END (*WITH*);
END SetconMd;
PROCEDURE SetregMd(VAR x : Item; ftyp : StrPtr);
(* setup of an item designating a register based variable
note that variables located in registers are usually one of:
* A result from a library routine
* A dummy variable used at compile time
Variables less than 3 bytes long are placed in the accumulator.
Variables 4 bytes in length have the high word in the X register, and
the low word in the accumulator.
*)
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
mode := regMd;
mod := 0;
lev := 0;
adr := 0;
off := 0;
register := erCPU;
isParam := FALSE;
indir := FALSE;
name := 0;
notpop := FALSE;
indexed := FALSE;
indexReg := erNone;
notpop := FALSE;
parent := NIL;
END (*WITH*);
IF lockItem <> ADR(x) THEN
Lock(erCPU);
lockItem := ADR(x);
END;
END SetregMd;
PROCEDURE SetstkMd(VAR x : Item; ftyp : StrPtr);
(* setup of an item designating a variable located on the stack *)
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
mode := stkMd;
mod := 0;
lev := curLev;
adr := stackPointer;
off := 0;
isParam := FALSE;
indir := FALSE;
name := 0;
notpop := FALSE;
indexed := FALSE;
indexReg := erNone;
register := erNone;
notpop := FALSE;
parent := NIL;
END (*WITH*);
END SetstkMd;
PROCEDURE SetDregMd(VAR x : Item; reg: DRegister; ftyp : StrPtr);
(*
setup of an item designating a variable located in one of the fake data
registers
*)
BEGIN
WITH x DO
IF ftyp <> NIL THEN
typ := ftyp;
ELSE
typ := undftyp;
END;
mode := DregMd;
mod := 0;
lev := curLev;
off := 0;
register := reg;
isParam := FALSE;
indir := FALSE;
name := 0;
notpop := FALSE;
indexed := FALSE;
indexReg := erNone;
notpop := FALSE;
parent := NIL;
adr := RegAdr(reg);
END (*WITH*);
END SetDregMd;
PROCEDURE Branch(cond : Condition; VAR tJmp, fJmp : CARDINAL);
(* jump forward, build chain. *)
VAR
l1: CARDINAL;
BEGIN
IF cond <> T THEN
IF fJmp = 0 THEN
newLink(fJmp);
END;
CASE cond OF
T, F:
|
HI: PutLinkReference(BCC, fJmp, 2);
PutLinkReference(BEQ, fJmp, 2);
|
LS: newLink(l1);
PutLinkReference(BCC, l1, 2);
PutLinkReference(BNE, fJmp, 2);
FixLink(l1);
|
CC: PutLinkReference(BCS, fJmp, 2);
|
CS: PutLinkReference(BCC, fJmp, 2);
|
NE: PutLinkReference(BEQ, fJmp, 2);
|
EQ: PutLinkReference(BNE, fJmp, 2);
|
VC: PutLinkReference(BVS, fJmp, 2);
|
VS: PutLinkReference(BVC, fJmp, 2);
|
PL: PutLinkReference(BMI, fJmp, 2);
|
MI: newLink(l1);
PutLinkReference(BEQ, l1, 2);
PutLinkReference(BCS, fJmp, 2);
FixLink(l1);
|
GE: PutLinkReference(BCC, fJmp, 2);
|
LT: PutLinkReference(BCS, fJmp, 2);
|
GT: PutLinkReference(BCC, fJmp, 2);
PutLinkReference(BEQ, fJmp, 2);
|
LE: newLink(l1);
PutLinkReference(BEQ, l1, 2);
PutLinkReference(BCS, fJmp, 2);
FixLink(l1);
|
FF: (* for true *)
|
FEQ: (* for neq *)
PutLinkReference(BEQ, fJmp, 2);
|
FGT: (* for not gtr *)
PutLinkReference(BEQ, fJmp, 2);
|
FGE: (* for not geq *)
PutLinkReference(BEQ, fJmp, 2);
|
FLT: (* for not lss *)
PutLinkReference(BNE, fJmp, 2);
|
FLE: (* for not leq *)
PutLinkReference(BNE, fJmp, 2);
|
FGL: (* for not ? *)
|
FGLE: (* for not ? *)
|
FNGLE:(* for ? *)
|
FNGL: (* for ? *)
|
FNLE: (* for leq *)
PutLinkReference(BEQ, fJmp, 2);
|
FNLT: (* for lss *)
PutLinkReference(BEQ, fJmp, 2);
|
FNGE: (* for geq *)
PutLinkReference(BNE, fJmp, 2);
|
FNGT: (* for gtr *)
PutLinkReference(BNE, fJmp, 2);
|
FNE: (* for eql *)
PutLinkReference(BNE, fJmp, 2);
|
FT: (* for false *)
END;
END;
IF tJmp = 0 THEN
newLink(tJmp);
END;
IF (cond <> F) AND (cond <> FF) THEN
PutLinkReference(BRL, tJmp, 3);
END;
END Branch;
PROCEDURE BranchWith(cond : Condition; VAR tJmp, fJmp, wJmp: CARDINAL);
(* jump forward, build chain. *)
VAR
l1: CARDINAL;
BEGIN
IF cond <> T THEN
IF fJmp = 0 THEN
newLink(fJmp);
END;
CASE cond OF
T, F:
|
HI: PutLinkReference(BCC, fJmp, 2);
PutLinkReference(BEQ, fJmp, 2);
|
LS: newLink(l1);
PutLinkReference(BCC, l1, 2);
PutLinkReference(BNE, fJmp, 2);
FixLink(l1);
|
CC: PutLinkReference(BCS, fJmp, 2);
|
CS: PutLinkReference(BCC, fJmp, 2);
|
NE: PutLinkReference(BEQ, fJmp, 2);
|
EQ: PutLinkReference(BNE, fJmp, 2);
|
VC: PutLinkReference(BVS, fJmp, 2);
|
VS: PutLinkReference(BVC, fJmp, 2);
|
PL: PutLinkReference(BMI, fJmp, 2);
|
MI: newLink(l1);
PutLinkReference(BEQ, l1, 2);
PutLinkReference(BCS, fJmp, 2);
FixLink(l1);
|
GE: PutLinkReference(BCC, fJmp, 2);
|
LT: PutLinkReference(BCS, fJmp, 2);
|
GT: PutLinkReference(BCC, fJmp, 2);
PutLinkReference(BEQ, fJmp, 2);
|
LE: newLink(l1);
PutLinkReference(BEQ, l1, 2);
PutLinkReference(BCS, fJmp, 2);
FixLink(l1);
|
FF: (* for true *)
|
FEQ: (* for neq *)
PutLinkReference(BEQ, fJmp, 2);
|
FGT: (* for not gtr *)
PutLinkReference(BEQ, fJmp, 2);
|
FGE: (* for not geq *)
PutLinkReference(BEQ, fJmp, 2);
|
FLT: (* for not lss *)
PutLinkReference(BNE, fJmp, 2);
|
FLE: (* for not leq *)
PutLinkReference(BNE, fJmp, 2);
|
FGL: (* for not ? *)
|
FGLE: (* for not ? *)
|
FNGLE:(* for ? *)
|
FNGL: (* for ? *)
|
FNLE: (* for leq *)
PutLinkReference(BEQ, fJmp, 2);
|
FNLT: (* for lss *)
PutLinkReference(BEQ, fJmp, 2);
|
FNGE: (* for geq *)
PutLinkReference(BNE, fJmp, 2);
|
FNGT: (* for gtr *)
PutLinkReference(BNE, fJmp, 2);
|
FNE: (* for eql *)
PutLinkReference(BNE, fJmp, 2);
|
FT: (* for false *)
END;
END;
FixLink(wJmp);
IF tJmp = 0 THEN
newLink(tJmp);
END;
IF (cond <> F) AND (cond <> FF) THEN
PutLinkReference(BRL, tJmp, 3);
END;
END BranchWith;
PROCEDURE LoadCC(VAR x : Item);
(* convert from 'cocMd' to 'regMd' while *)
(* generating conditional code. *)
VAR
done: CARDINAL;
BEGIN
WITH x DO
IF NOT errorInProc THEN
Assert( mode = cocMd );
IF (Fjmp = 0) AND (Tjmp = 0) AND (CC < HI) THEN
IF InvertCC(CC) = F THEN
PutDPReference(LDA+IMMEDIATE, 0, 3);
ELSE
PutDPReference(LDA+IMMEDIATE, 1, 3);
END;
ELSE
newLink(done);
Branch(CC, Fjmp, Tjmp);
IF CC <> T THEN
FixLink(Tjmp);
END;
PutDPReference(LDA+IMMEDIATE, 1, 3);
PutLinkReference(BRA, done, 2);
FixLink(Fjmp);
PutDPReference(LDA+IMMEDIATE, 0, 3);
FixLink(done);
END;
END;
(* transform 'cocMd' to 'regMd' *)
SetregMd(x, booltyp);
END (*WITH*);
END LoadCC;
PROCEDURE ExternalCall(mno, pno : CARDINAL; pname: CARDINAL);
(* call of the external procedure #pno in module #mno. *)