-
Notifications
You must be signed in to change notification settings - Fork 0
/
386.ORG.Mod.txt
2223 lines (2046 loc) · 81.3 KB
/
386.ORG.Mod.txt
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
MODULE ORG; (* SVP, 25.7.2024 Oberon compiler; code generator for Intel 386/387 *)
IMPORT SYSTEM, Files, ORS, ORB;
(*Code generator for Oberon compiler for Intel 386/387 processor.
Procedural interface to Parser OSAP; result in array "code".
Procedure Close writes code-files*)
CONST WordSize* = 4;
CodeAlign = 128;
(*limits*)
maxCode = 40000; maxStrx = 3000; maxTD = 160;
(*register values*)
AX = 0; CX = 1; DX = 2; BX = 3; SP = 4; BP = 5; SI = 6; DI = 7;
NoReg = -1; off0 = 31; off8 = 30; off32 = 29; anyoffset = {off0, off8, off32};
reservedRegs = {SP, BP};
regs = {AX..DI} - reservedRegs;
ParOff = 8; TagOff = -4;
(*internal item modes*)
Reg = 10; RegI = 11; Cond = 12; FStk = 13; RegX = 14; RegJ = 15; RegY = 16;
(*frequently used opcodes*)
JccD32 = 0F80H; JccD8 = 70H; JmpD32 = 0E9H; JmpD8 = 0EBH;
(*condition codes*)
ccB = 2; ccAE = 3; ccBE = 6; ccA = 7; (*unsigned*)
ccL = 12; ccGE = 13; ccLE = 14; ccG = 15; (*signed*)
ccE = 4; ccNE = 5; ccS = 8; ccNS = 9; ccO = 0; ccNO = 1; ccP = 10; ccNP = 11;
ccTrue = -1; ccFalse = -2;
(*fixup types*)
FixCodeRel = 1; FixCode = 2; FixVar = 3;
TYPE Item* = RECORD
mode*: INTEGER;
type*: ORB.Type;
a*, b*, r: LONGINT;
rdo*: BOOLEAN; (*read only*)
scale, index: INTEGER;
fixup, mno, vno: INTEGER
END ;
(* Item forms and meaning of fields:
mode r a b
--------------------------------
Const - value (proc adr) (immediate value)
Var base off - (direct adr)
Par - off0 off1 (indirect adr)
Reg regno
RegI regno off -
Cond cond Fchain Tchain
FStk - - -
RegX regno off - (scale*index+base)
RegJ regno off - (RegI with fixup)
RegY regno off - (RegX with fixup) *)
VAR pc*, varsize: LONGINT; (*program counter, data index*)
tdx, strx: LONGINT;
entry: LONGINT; (*main entry point*)
FH: LONGINT; (*available fpu registers ST[0] ... ST[H-1]*)
HReg: LONGINT;
parblocksize, procA, procB: LONGINT;
fixorgP, fixorgD, fixorgT: LONGINT; (*origins of lists of locations to be fixed up by loader*)
check: BOOLEAN; (*emit run-time checks*)
version: INTEGER; (* 10 = Intel 386/387 *)
usedRegs: SET;
relmap, frelmap: ARRAY 6 OF INTEGER; (*condition codes for relations*)
code: ARRAY maxCode OF BYTE;
data: ARRAY maxTD OF LONGINT; (*type descriptors*)
str: ARRAY maxStrx OF CHAR;
(*instruction assemblers according to formats*)
PROCEDURE ThisByte(a: INTEGER): BYTE;
BEGIN RETURN code[a]
END ThisByte;
PROCEDURE ThisShort(a: INTEGER): INTEGER;
BEGIN RETURN ORD(code[a+1] * 256 + code[a])
END ThisShort;
PROCEDURE ThisWord(a: INTEGER): INTEGER;
BEGIN RETURN ORD((((code[a+3] * 256) + code[a+2]) * 256 + code[a+1]) * 256 + code[a])
END ThisWord;
PROCEDURE ThisStrWord(a: INTEGER): INTEGER;
BEGIN RETURN ((ORD(str[a+3])*256 + ORD(str[a+2]))*256 + ORD(str[a+1]))*256 + ORD(str[a])
END ThisStrWord;
PROCEDURE PutByte(a, x: INTEGER);
BEGIN code[a] := x
END PutByte;
PROCEDURE PutShort(a, x: INTEGER);
BEGIN PutByte(a, x); PutByte(a+1, x DIV 256)
END PutShort;
PROCEDURE PutWord(a, x: INTEGER);
BEGIN PutByte(a, x); PutByte(a+1, x DIV 256); PutByte(a+2, x DIV 65536); PutByte(a+3, x DIV 16777216)
END PutWord;
PROCEDURE PutStrWord(a, x: INTEGER);
BEGIN str[a] := CHR(x); str[a+1] := CHR(x DIV 100H); str[a+2] := CHR(x DIV 10000H); str[a+3] := CHR(x DIV 1000000H);
END PutStrWord;
PROCEDURE GenByte(x: INTEGER);
BEGIN PutByte(pc, x); INC(pc)
END GenByte;
PROCEDURE GenShort(x: INTEGER);
BEGIN PutShort(pc, x); INC(pc, 2)
END GenShort;
PROCEDURE GenWord(x: INTEGER);
BEGIN PutWord(pc, x); INC(pc, 4)
END GenWord;
PROCEDURE Gen1(op: INTEGER); (*prefix/opcode*)
(*emit multiple bytes*)
VAR i: INTEGER;
BEGIN i := 3;
WHILE (i > 0) & (ASR(op, i*8) MOD 256 = 0) DO DEC(i) END;
WHILE i >= 0 DO GenByte(ASR(op, i*8)); DEC(i) END
END Gen1;
PROCEDURE Gen1word(op, word: INTEGER);
BEGIN Gen1(op); GenWord(word)
END Gen1word;
PROCEDURE Gen2(op, r, reg: INTEGER); (*op r,reg*)
(*emit opcode with ModR/M reg/reg*)
BEGIN
ASSERT(r IN {AX..DI});
ASSERT(reg IN {AX..DI});
Gen1(op); GenByte(3 * 64 + r * 8 + reg)
END Gen2;
PROCEDURE Gen3(op, reg, base, offset: INTEGER; hint: SET); (*op reg,[base+offset]*)
(*emit opcode with ModR/M reg/mem*)
VAR mod, size: INTEGER;
BEGIN
ASSERT(reg IN {AX..DI});
ASSERT((base = NoReg) OR (base IN {AX..DI}));
ASSERT(hint * {off0, off8, off32} # {});
IF base = NoReg THEN ASSERT(off32 IN hint); mod := 0; base := BP; size := 4 (*mod=0 + base=BP => no-base + offset32*)
ELSIF (offset = 0) & (base # BP) & (off0 IN hint) THEN mod := 0; size := 0 (*no-offset*)
ELSIF (offset >= -128) & (offset <= 127) & (off8 IN hint) THEN mod := 1; size := 1 (*offset8*)
ELSE ASSERT(off32 IN hint); mod := 2; size := 4 (*offset32*)
END;
IF base = SP THEN (* [esp+offset] via SIB *)
Gen1(op); GenByte(mod * 64 + reg * 8 + SP); GenByte(0 * 64 + SP * 8 + SP)
ELSE (* [base+offset] *)
Gen1(op); GenByte(mod * 64 + reg * 8 + base)
END;
IF size = 1 THEN GenByte(offset)
ELSIF size = 4 THEN GenWord(offset)
END
END Gen3;
PROCEDURE Gen4(op, reg, scale, index, base, offset: INTEGER; hint: SET); (* op reg,[scale*index+base+offset] *)
(*same as Gen3, but with scale/index*)
VAR mod, size: INTEGER;
BEGIN
ASSERT(reg IN {AX..DI});
ASSERT(scale IN {0, 1, 2, 4, 8});
ASSERT((index = NoReg) OR (index IN {AX..DI} - {SP}));
ASSERT((base = NoReg) OR (base IN {AX..DI}));
IF (scale = 0) OR (index = NoReg) THEN (* [base+offset] *)
Gen3(op, reg, base, offset, hint);
ELSE (* [scale*index+base+offset] *)
IF base = NoReg THEN ASSERT(off32 IN hint); mod := 0; base := BP; size := 4 (* mod=0 + base=BP => no-base + offset32 *)
ELSIF (offset = 0) & (base # BP) & (off0 IN hint) THEN mod := 0; size := 0 (* no-offset *)
ELSIF (offset >= -128) & (offset <= 127) & (off8 IN hint) THEN mod := 1; size := 1 (* offset8 *)
ELSE ASSERT(off32 IN hint); mod := 2; size := 4 (* offset32 *)
END;
IF scale = 1 THEN scale := 0
ELSIF scale = 2 THEN scale := 1
ELSIF scale = 4 THEN scale := 2
ELSE scale := 3
END;
Gen1(op); GenByte(mod * 64 + reg * 8 + SP); GenByte(scale * 64 + index * 8 + base);
IF size = 1 THEN GenByte(offset)
ELSIF size = 4 THEN GenWord(offset)
END
END
END Gen4;
PROCEDURE incF;
BEGIN
IF FH < 8 THEN INC(FH) ELSE ORS.Mark("fpu stack overflow") END
END incF;
PROCEDURE decF;
BEGIN
IF FH > 0 THEN DEC(FH) ELSE ORS.Mark("fpu stack underflow") END
END decF;
PROCEDURE GetReg(hint: SET): INTEGER;
VAR reg: INTEGER; excl: SET;
BEGIN excl := usedRegs + reservedRegs;
IF HReg # NoReg THEN EXCL(hint, HReg) END;
reg := AX; WHILE (reg <= DI) & ((reg IN excl) OR ~(reg IN hint)) DO INC(reg) END;
IF reg > DI THEN
reg := AX; WHILE (reg <= DI) & (reg IN excl) DO INC(reg) END;
IF reg > DI THEN ORS.Mark("too many regs in use"); reg := AX END;
END;
INCL(usedRegs, reg);
IF reg = HReg THEN HReg := NoReg END;
RETURN reg
END GetReg;
PROCEDURE GetReg1(hint, excl: SET): INTEGER;
VAR reg: INTEGER;
BEGIN
reg := GetReg(hint - excl);
IF reg IN excl THEN
ORS.Mark("too many regs in use"); reg := AX
END;
RETURN reg
END GetReg1;
PROCEDURE FreeReg(reg: INTEGER);
BEGIN
IF ~(reg IN reservedRegs) & (reg IN usedRegs) THEN EXCL(usedRegs, reg)
ELSE ORS.Mark("FreeReg")
END
END FreeReg;
PROCEDURE FreeItem(VAR x: Item);
BEGIN
IF x.mode = FStk THEN decF
ELSIF x.mode IN {Reg, RegI, RegJ, RegX, RegY} THEN
IF (x.r # NoReg) & ~(x.r IN reservedRegs) THEN FreeReg(x.r) END;
IF (x.mode IN {RegX, RegY}) & (x.index # NoReg) & ~(x.index IN reservedRegs) THEN FreeReg(x.index) END
END
END FreeItem;
PROCEDURE SaveReg(reg: INTEGER; workset: SET; VAR info: INTEGER);
VAR temp: INTEGER; set: SET;
BEGIN ASSERT(reg IN {AX..DI}); ASSERT(reg IN workset);
IF reg IN usedRegs THEN
temp := GetReg1(regs, reservedRegs + usedRegs + workset);
Gen2(8BH, temp, reg); info := temp (*mov i,reg*)
ELSE INCL(usedRegs, reg); info := reg
END
END SaveReg;
PROCEDURE RestoreReg(reg, info: INTEGER);
BEGIN ASSERT(reg IN {AX..DI}); ASSERT(info IN {AX..DI});
IF reg # info THEN
Gen2(8BH, reg, info) (* mov i,reg *)
END;
FreeReg(info)
END RestoreReg;
PROCEDURE CheckRegs*;
BEGIN
IF FH # 0 THEN ORS.Mark("FPU Stack"); FH := 0 END ;
IF usedRegs # reservedRegs THEN ORS.Mark("Reg Stack"); usedRegs := reservedRegs END ;
IF pc >= maxCode - 40 THEN ORS.Mark("program too long") END ;
END CheckRegs;
PROCEDURE SetCC(VAR x: Item; n: LONGINT);
BEGIN x.mode := Cond; x.a := 0; x.b := 0; x.r := n
END SetCC;
PROCEDURE Trap(cond, num: LONGINT);
VAR pc0, disp, op: INTEGER; x: Item;
BEGIN
IF cond # ccFalse THEN
IF cond # ccTrue THEN
IF ODD(cond) THEN op := JccD8 + cond - 1 ELSE op := JccD8 + cond + 1 END;
Gen1(op); pc0 := pc; GenByte(0)
END;
Gen1word(68H, ORS.Pos() * 100H + num); (*push c*)
Gen3(0FFH, 2, NoReg, 0, {off32}); (*call [MTOrg]*)
disp := (pc - 4) - fixorgD;
IF disp < 4096 THEN PutWord(pc - 4, -1 * 10000H + disp); fixorgD := pc - 4
ELSE ORS.Mark("fixup impossible")
END;
IF cond # ccTrue THEN PutByte(pc0, pc - (pc0 + 1)) END
END
END Trap;
PROCEDURE Fix (type, mno, vno: INTEGER);
(*fixorgD format: offset 16 bit + mno 4 bit + disp 12 bit*)
(*fixorgP format: offset 14 bit + mno 4 bit + disp 14 bit*)
VAR disp: INTEGER; error, rel: BOOLEAN;
BEGIN
IF type # 0 THEN
IF type IN {FixCode, FixCodeRel} THEN
rel := type = FixCodeRel;
disp := (pc - 4) - fixorgP;
IF mno = 0 THEN (*dword-aligned procedure address*)
ASSERT(vno MOD 4 = 0); vno := vno DIV 4; error := rel OR (vno >= 16384)
ELSE error := vno >= 8192;
IF rel THEN vno := -vno END
END;
IF ~error & (disp < 16384) THEN
PutWord(pc - 4, vno * 40000H + (-mno) * 4000H + disp); fixorgP := pc - 4;
ELSE ORS.Mark("fixup impossible")
END
ELSIF type = FixVar THEN
disp := (pc - 4) - fixorgD;
IF (disp < 4096) & (vno < 0FFFFH) THEN
PutWord(pc - 4, vno * 10000H + (-mno) * 1000H + disp); fixorgD := pc - 4
ELSE ORS.Mark("fixup impossible")
END
ELSE ORS.Mark("invalid fixup type")
END
END
END Fix;
PROCEDURE PrepFixup (offset, fixup, mno, vno: INTEGER; VAR newoffset, newvno: INTEGER);
BEGIN
IF (fixup # 0) & (mno = 0) THEN
INC(offset, vno);
IF offset >= 0 THEN
IF offset < 0FFFFH THEN vno := offset; offset := 0
ELSE DEC(offset, 0FFFEH); vno := 0FFFEH
END
ELSE vno := 0
END
END;
newoffset := offset; newvno := vno
END PrepFixup;
(*handling of forward reference, fixups of branch addresses and constant tables*)
PROCEDURE negated(cond: LONGINT): LONGINT;
BEGIN
IF ODD(cond) THEN DEC(cond) ELSE INC(cond) END;
RETURN cond
END negated;
PROCEDURE reverse(op: INTEGER): INTEGER;
BEGIN
IF op = ORS.lss THEN op := ORS.gtr
ELSIF op = ORS.leq THEN op := ORS.geq
ELSIF op = ORS.gtr THEN op := ORS.lss
ELSIF op = ORS.geq THEN op := ORS.leq
END;
RETURN op
END reverse;
PROCEDURE FixOne*(at: LONGINT);
BEGIN PutWord(at, pc - (at + 4))
END FixOne;
PROCEDURE FixLink*(L: LONGINT);
VAR L1: LONGINT;
BEGIN
WHILE L # 0 DO L1 := ThisWord(L); PutWord(L, pc - (L + 4)); L := L1 END;
END FixLink;
PROCEDURE FixLinkWith(L0, dst: LONGINT);
VAR L1: LONGINT;
BEGIN
WHILE L0 # 0 DO
L1 := ThisWord(L0);
PutWord(L0, dst - (L0 + 4)); L0 := L1
END
END FixLinkWith;
PROCEDURE merged(L0, L1: LONGINT): LONGINT;
VAR L2, L3: LONGINT;
BEGIN
IF L0 # 0 THEN L3 := L0;
REPEAT L2 := L3; L3 := ThisWord(L2) UNTIL L3 = 0;
PutWord(L2, L1); L1 := L0
END ;
RETURN L1
END merged;
PROCEDURE GenJmpLink (cond, next: INTEGER; VAR link: INTEGER);
BEGIN
IF cond # ccFalse THEN
IF cond = ccTrue THEN Gen1(JmpD32) ELSE Gen1(JccD32 + cond) END;
link := pc; GenWord(next)
ELSE link := next
END
END GenJmpLink;
PROCEDURE GenJmp (cond, L: INTEGER);
VAR disp: INTEGER;
BEGIN
IF cond # ccFalse THEN
disp := L - (pc + 2);
IF (disp >= -128) & (disp <= 127) THEN
IF cond = ccTrue THEN Gen1(JmpD8) ELSE Gen1(JccD8 + cond) END;
GenByte(disp)
ELSE
IF cond = ccTrue THEN Gen1(JmpD32) ELSE Gen1(JccD32 + cond) END;
GenWord(L - (pc + 4))
END
END
END GenJmp;
(* loading of operands and addresses into registers *)
PROCEDURE Gen4fixup (op, subop, scale, index, base, offset, fixup, mno, vno: INTEGER);
VAR temp: INTEGER;
BEGIN
PrepFixup(offset, fixup, mno, vno, offset, vno);
IF offset # 0 THEN
temp := GetReg(regs);
Gen4(8DH, temp, scale, index, base, 0, {off32}); Fix(fixup, mno, vno); (*lea*)
Gen4(op, subop, 0, NoReg, temp, offset, anyoffset);
FreeReg(temp)
ELSE Gen4(op, subop, scale, index, base, 0, {off32}); Fix(fixup, mno, vno);
END
END Gen4fixup;
PROCEDURE Gen3fixup (op, subop, base, offset, fixup, mno, vno: INTEGER);
BEGIN Gen4fixup(op, subop, 0, NoReg, base, offset, fixup, mno, vno)
END Gen3fixup;
PROCEDURE Gen3v(op, reg: INTEGER; VAR x: Item);
VAR base: INTEGER;
BEGIN
IF (x.mode = ORB.Const) & (x.type.form = ORB.Proc) THEN
IF x.r > 0 THEN ORS.Mark("not allowed")
ELSE Gen3fixup(op, reg, NoReg, 0, FixCode, x.r, x.a)
END
ELSIF x.mode = ORB.Var THEN
IF x.r > 0 THEN Gen3(op, reg, BP, x.a, anyoffset)
ELSE Gen3fixup(op, reg, NoReg, 0, FixVar, x.r, x.a)
END
ELSIF x.mode = ORB.Par THEN
base := GetReg(regs);
Gen3(8BH, base, BP, x.a, anyoffset); (* mov reg,PTR[ebp+offset] *)
Gen3(op, reg, base, x.b, anyoffset); (* op reg,[base+offset2] *)
FreeReg(base)
ELSIF x.mode = Reg THEN
Gen2(op, reg, x.r)
ELSIF x.mode = RegI THEN
Gen3(op, reg, x.r, x.a, anyoffset)
ELSIF x.mode = RegJ THEN
Gen3fixup(op, reg, x.r, x.a, x.fixup, x.mno, x.vno)
ELSIF x.mode = RegX THEN
Gen4(op, reg, x.scale, x.index, x.r, x.a, anyoffset)
ELSIF x.mode = RegY THEN
Gen4fixup(op, reg, x.scale, x.index, x.r, x.a, x.fixup, x.mno, x.vno)
ELSE ORS.Mark("bad address")
END;
END Gen3v;
PROCEDURE movmem(VAR x: Item; r: INTEGER; freeReg: BOOLEAN);
VAR temp, offset, vno: INTEGER;
BEGIN
IF x.mode = Reg THEN
Gen3v(89H, r, x) (*mov x,r*)
ELSE
IF (x.type.size = 1) & (r IN {SP..DI}) THEN (*must be AL, BL, CL, DL*)
temp := GetReg1({AX}, {SP..DI}); Gen2(8BH, temp, r);
IF freeReg THEN FreeReg(r) END;
r := temp; freeReg := TRUE
END;
IF x.mode IN {RegJ, RegY} THEN
PrepFixup(x.a, x.fixup, x.mno, x.vno, offset, vno)
END;
IF (r = AX) &
(x.mode IN {RegI, RegJ, RegX, RegY}) & (x.r = NoReg) &
(~(x.mode IN {RegX, RegY}) OR (x.index = NoReg) OR (x.scale = 0)) &
(~(x.mode IN {RegJ, RegY}) OR (offset = 0) OR (x.fixup = 0)) THEN
IF x.type.size = 1 THEN Gen1word(0A2H, x.a) ELSE Gen1word(0A3H, x.a) END; (*mov [x],eax*)
IF x.mode IN {RegJ, RegY} THEN Fix(x.fixup, x.mno, vno) END
ELSIF x.type.size = 1 THEN Gen3v(88H, r, x) (*mov byte[x],r*)
ELSE Gen3v(89H, r, x) (*mov dword[x],r*)
END
END;
IF freeReg THEN FreeReg(r) END
END movmem;
PROCEDURE movmemcon(VAR x: Item; val: INTEGER);
BEGIN
IF x.mode = Reg THEN Gen1word(0B8H + x.r, val) (*mov x,imm32*)
ELSIF x.type.size = 1 THEN Gen3v(0C6H, 0, x); GenByte(val) (*mov byte[x],imm8*)
ELSE Gen3v(0C7H, 0, x); GenWord(val) (*mov dword[x],imm32*)
END
END movmemcon;
PROCEDURE mov(r: INTEGER; VAR x: Item);
VAR op, offset, vno: INTEGER;
BEGIN
ASSERT(r IN {AX..DI});
IF x.type.size = 1 THEN op := 0FB6H ELSE op := 8BH END; (* movxz r32,rm / mov r32,rm *)
IF x.mode = ORB.Const THEN (* mov reg,imm32 *)
IF x.type.form = ORB.Proc THEN
IF x.r > 0 THEN ORS.Mark("not allowed")
ELSE
PrepFixup(0, FixCode, x.r, x.a, offset, vno);
IF offset = 0 THEN Gen1word(0B8H + r, 0); Fix(FixCode, x.r, vno) (*mov r,imm*)
ELSE Gen3fixup(op, r, NoReg, offset, FixCode, x.r, vno) (*mov*)
END
END
ELSE Gen1word(0B8H + r, x.a)
END
ELSIF x.mode = ORB.Var THEN
IF x.r > 0 THEN Gen3(op, r, BP, x.a, anyoffset) (* local *)
ELSE
PrepFixup(0, FixVar, x.r, x.a, offset, vno);
IF (r = AX) & (x.type.size = 4) & (offset = 0) THEN Gen1word(0A1H, 0); Fix(FixVar, x.r, vno) (*mov r,[a]*)
ELSE Gen3fixup(op, r, NoReg, offset, FixVar, x.r, vno)
END
END
ELSIF x.mode = ORB.Par THEN
Gen3(8BH, r, BP, x.a, anyoffset); (* mov r32,[ebp+adr] *)
Gen3(op, r, r, x.b, anyoffset); (* mov r,[r+offset] *)
ELSIF x.mode = Reg THEN
IF r # x.r THEN
Gen2(8BH, r, x.r) (* mov r,x *)
END
ELSIF x.mode = RegI THEN
IF (r = AX) & (x.r = NoReg) & (x.type.size = 4) THEN Gen1word(0A1H, x.a)
ELSE Gen3(op, r, x.r, x.a, anyoffset)
END;
ELSIF x.mode = RegJ THEN
PrepFixup(x.a, x.fixup, x.mno, x.vno, offset, vno);
IF (r = AX) & (x.r = NoReg) & (x.type.size = 4) & (offset = 0) THEN Gen1word(0A1H, 0); Fix(x.fixup, x.mno, vno)
ELSE Gen3v(op, r, x)
END
ELSIF x.mode = RegX THEN
IF (r = AX) & (x.r = NoReg) & (x.index = NoReg) & (x.type.size = 4) THEN Gen1word(0A1H, x.a)
ELSE Gen4(op, r, x.scale, x.index, x.r, x.a, anyoffset)
END
ELSIF x.mode = RegY THEN
PrepFixup(x.a, x.fixup, x.mno, x.vno, offset, vno);
IF (r = AX) & (x.r = NoReg) & ((x.index = NoReg) OR (x.scale = 0)) & (x.type.size = 4) & (offset = 0) THEN Gen1word(0A1H, 0); Fix(x.fixup, x.mno, vno)
ELSE Gen3v(op, r, x)
END
ELSIF x.mode = Cond THEN
IF (x.r = ccTrue) OR (x.r = ccFalse) THEN
FixLink(x.a); FixLink(x.b);
IF x.r = ccTrue THEN op := 1 ELSE op := 0 END;
Gen1word(0B8H + r, op); (*mov r,imm32*)
ELSIF (x.a = 0) & (x.b = 0) THEN
Gen1word(0B8H + r, 0); (*mov r,0*)
Gen2(0F90H + x.r, 0, r) (*setcc r*)
ELSE
Gen1(JccD8 + negated(x.r)); GenByte(7);
FixLink(x.b); Gen1word(0B8H + r, 1); Gen1(JmpD8); GenByte(5);
FixLink(x.a); Gen1word(0B8H + r, 0);
END
ELSIF x.mode = FStk THEN
Gen3(8DH, SP, SP, -4, anyoffset); (* lea esp,[esp-4] *)
IF x.type.form = ORB.Real THEN Gen3(0D9H, 2, SP, 0, anyoffset) (* fst float32[esp] *)
ELSE Gen3(0DBH, 2, SP, 0, anyoffset) (* fist int32[esp] *)
END;
Gen1(58H + r); (* pop r *)
ELSE ORS.Mark("mov error")
END
END mov;
PROCEDURE lea(r: INTEGER; VAR x: Item);
VAR offset, vno: INTEGER;
BEGIN
ASSERT(r IN {AX..DI});
IF (x.mode = ORB.Const) & (x.type.form = ORB.Proc) THEN
IF x.r > 0 THEN ORS.Mark("not allowed")
ELSE
PrepFixup(0, FixCode, x.r, x.a, offset, vno);
IF offset = 0 THEN Gen1word(0B8H + r, 0); Fix(FixCode, x.r, vno) (*mov reg,adr*)
ELSE Gen3fixup(8DH, r, NoReg, offset, FixCode, x.r, vno) (*lea*)
END
END
ELSIF x.mode = ORB.Var THEN
IF x.r > 0 THEN Gen3(8DH, r, BP, x.a, anyoffset) (* lea reg,[ebp+offset] *)
ELSE
PrepFixup(0, FixVar, x.r, x.a, offset, vno);
IF offset = 0 THEN Gen1word(0B8H + r, 0); Fix(FixVar, x.r, vno) (*mov reg,adr*)
ELSE Gen3fixup(8DH, r, NoReg, offset, FixVar, x.r, vno)
END
END
ELSIF x.mode = ORB.Par THEN
Gen3(8BH, r, BP, x.a, anyoffset); (* mov reg,PTR[ebp+offset1] *)
IF x.b # 0 THEN Gen3(8DH, r, r, x.b, anyoffset) END (* lea reg,[reg+offset2] *)
ELSIF x.mode = RegI THEN
IF x.r = NoReg THEN Gen1word(0B8H + r, x.a) (*mov r,imm*)
ELSIF x.a = 0 THEN Gen2(8BH, r, x.r) (*mov r,x*)
ELSE Gen3(8DH, r, x.r, x.a, anyoffset) (* lea reg,[reg+offset] *)
END
ELSIF x.mode = RegJ THEN
PrepFixup(x.a, x.fixup, x.mno, x.vno, offset, vno);
IF x.r = NoReg THEN Gen1word(0B8H + r, 0) (*mov r,imm*)
ELSE Gen3(8DH, r, x.r, 0, {off32}) (*lea r,[reg+adr]*)
END;
Fix(x.fixup, x.mno, vno);
IF offset # 0 THEN Gen3(8DH, r, r, offset, anyoffset) END (*lea r,[r+offset]*)
ELSIF x.mode = RegX THEN
IF (x.r = NoReg) & ((x.index = NoReg) OR (x.scale = 0)) THEN
Gen1word(0B8H + r, x.a) (*mov r,imm*)
ELSIF (x.a = 0) & ((x.index = NoReg) OR (x.scale = 0)) THEN
IF r # x.r THEN Gen2(8BH, r, x.r) END (*mov r,xr*)
ELSE Gen4(8DH, r, x.scale, x.index, x.r, x.a, anyoffset)
END
ELSIF x.mode = RegY THEN
PrepFixup(x.a, x.fixup, x.mno, x.vno, offset, vno);
IF (x.r = NoReg) & ((x.index = NoReg) OR (x.scale = 0)) THEN
Gen1word(0B8H + r, 0) (*mov r,imm*)
ELSE
Gen4(8DH, r, x.scale, x.index, x.r, 0, {off32}) (*lea r,[scale*index+r+adr]*)
END;
Fix(x.fixup, x.mno, vno);
IF offset # 0 THEN Gen3(8DH, r, r, offset, anyoffset) END (*lea r,[r+offset]*)
ELSE ORS.Mark("address error")
END
END lea;
PROCEDURE simplify(VAR x: Item);
BEGIN
IF x.mode = ORB.Var THEN
IF x.r > 0 THEN x.mode := RegI; x.r := BP
ELSE
x.fixup := FixVar; x.mno := x.r; x.vno := x.a;
x.mode := RegJ; x.r := NoReg; x.a := 0
END
ELSIF x.mode = RegJ THEN
IF x.fixup = 0 THEN x.mode := RegI END
ELSIF x.mode = RegY THEN
IF x.fixup = 0 THEN x.mode := RegX END
END
END simplify;
PROCEDURE load(VAR x: Item; hint: SET);
VAR r: LONGINT;
BEGIN
IF x.mode # Reg THEN
IF x.mode IN {RegI, RegJ} THEN
IF (x.r # NoReg) & ~(x.r IN reservedRegs) & ((x.r IN hint) OR (hint - usedRegs = {})) THEN r := x.r
ELSE r := GetReg(hint)
END;
mov(r, x);
IF (x.r # NoReg) & (x.r # r) & ~(x.r IN reservedRegs) THEN FreeReg(x.r) END
ELSIF x.mode IN {RegX, RegY} THEN
IF (x.r # NoReg) & ~(x.r IN reservedRegs) & ((x.r IN hint) OR (hint - usedRegs = {})) THEN r := x.r
ELSIF (x.index # NoReg) & ~(x.index IN reservedRegs) & ((x.index IN hint) OR (hint - usedRegs = {})) THEN r := x.index
ELSE r := GetReg(hint)
END;
mov(r, x);
IF (x.r # NoReg) & (x.r = r) & ~(x.index IN reservedRegs) THEN FreeReg(x.index) END;
IF (x.index # NoReg) & (x.index = r) & ~(x.r IN reservedRegs) THEN FreeReg(x.r) END
ELSIF x.mode = FStk THEN
r := GetReg(hint);
Gen3(8DH, SP, SP, -4, anyoffset); (* lea esp,[esp-4] *)
IF x.type.form = ORB.Real THEN Gen3(0D9H, 3, SP, 0, anyoffset) (* fstp float32[esp] *)
ELSE Gen3(0DBH, 3, SP, 0, anyoffset) (* fistp int32[esp] *)
END;
Gen1(58H + r); (* pop r *)
decF
ELSE r := GetReg(hint); mov(r, x)
END;
x.mode := Reg; x.r := r
END
END load;
PROCEDURE loadAdr(VAR x: Item; hint: SET);
VAR reg: INTEGER;
BEGIN
IF x.mode IN {RegI, RegJ} THEN
IF (x.r # NoReg) & ~(x.r IN reservedRegs) & ((x.r IN hint) OR (hint - usedRegs = {})) THEN reg := x.r ELSE reg := GetReg(hint) END;
lea(reg, x);
IF (x.r # NoReg) & (x.r # reg) & ~(x.r IN reservedRegs) THEN FreeReg(x.r) END
ELSIF x.mode IN {RegX, RegY} THEN
IF (x.r # NoReg) & ~(x.r IN reservedRegs) & ((x.r IN hint) OR (hint - usedRegs = {})) THEN reg := x.r
ELSIF (x.index # NoReg) & ~(x.index IN reservedRegs) & ((x.index IN hint) OR (hint - usedRegs = {})) THEN reg := x.index
ELSE reg := GetReg(hint)
END;
lea(reg, x);
IF (x.r # NoReg) & (x.r = reg) & ~(x.index IN reservedRegs) THEN FreeReg(x.index) END;
IF (x.index # NoReg) & (x.index = reg) & ~(x.r IN reservedRegs) THEN FreeReg(x.r) END
ELSIF x.mode # Reg THEN
reg := GetReg(hint); lea(reg, x)
ELSE ORS.Mark("address error"); reg := AX
END;
x.mode := Reg; x.r := reg
END loadAdr;
PROCEDURE cmp(VAR x, y: Item);
VAR op, reg, offset, vno: INTEGER;
BEGIN
IF (x.mode = Reg) & (y.mode = ORB.Const) & (y.a = 0) THEN
Gen2(85H, x.r, x.r) (* test r32,r32 *)
ELSIF (x.mode = Reg) & (y.mode = Reg) THEN
Gen3v(3BH, x.r, y) (* cmp reg32,r/m32 *)
ELSIF (x.mode = Reg) & (y.mode IN {ORB.Var, ORB.Par, RegI, RegJ, RegX, RegY}) THEN
IF y.type.size = 1 THEN
IF x.r IN {SP..DI} THEN
reg := GetReg1(regs, {SP..DI}); mov(reg, x); Gen3v(3AH, reg, y); FreeReg(reg) (*cmp reg8,r/m8*)
ELSE Gen3v(3AH, x.r, y) (* cmp reg8,r/m8 *)
END
ELSE Gen3v(3BH, x.r, y) (* cmp reg32,r/m32 *)
END
ELSIF (x.mode IN {ORB.Var, ORB.Par, RegI, RegJ, RegX, RegY}) & (y.mode = Reg) THEN
IF x.type.size = 1 THEN
IF y.r IN {SP..DI} THEN
reg := GetReg1(regs, {SP..DI}); mov(reg, y); Gen3v(38H, reg, x); FreeReg(reg) (*cmp r/m8,reg8*)
ELSE Gen3v(38H, y.r, x)
END
ELSE Gen3v(39H, y.r, x) (* cmp r/m32,reg32 *)
END
ELSIF (x.mode IN {ORB.Var, ORB.Par, Reg, RegI, RegJ, RegX, RegY}) & (y.mode = ORB.Const) THEN
IF y.type.form = ORB.Proc THEN
PrepFixup(0, FixCode, y.r, y.a, offset, vno);
IF offset = 0 THEN
IF (x.mode = Reg) & (x.r = AX) THEN Gen1(3DH) (*cmp eax,imm32*)
ELSE Gen3v(81H, 7, x) (*cmp r/m32,imm32*)
END;
GenWord(0); Fix(FixCode, y.r, vno)
ELSE reg := GetReg(regs); mov(reg, y); Gen3v(39H, reg, x); FreeReg(reg) (*cmp r/m32,reg32*)
END
ELSIF x.mode = Reg THEN
IF (y.a >= -128) & (y.a <= 127) THEN
IF (x.type.size = 1) & ~(x.r IN {SP..DI}) THEN
IF x.r = AX THEN Gen1(3CH); GenByte(y.a) (* cmp al,imm8 *)
ELSE Gen2(80H, 7, x.r); GenByte(y.a) (* cmp reg8,imm8 *)
END
ELSE Gen2(83H, 7, x.r); GenByte(y.a) (* cmp reg32,imm8 *)
END
ELSIF x.r = AX THEN Gen1word(3DH, y.a) (* cmp eax,imm32 *)
ELSE Gen2(81H, 7, x.r); GenWord(y.a) (* cmp reg32,imm32 *)
END
ELSIF x.type.size = 1 THEN
IF (y.a >= -128) & (y.a <= 127) THEN Gen3v(80H, 7, x); GenByte(y.a) (* cmp r/m8,imm8 *)
ELSE reg := GetReg(regs); mov(reg, x); Gen2(81H, 7, x.r); GenWord(y.a); FreeReg(reg) (* cmp reg32,imm32 *)
END
ELSE
IF (y.a >= -128) & (y.a <= 127) THEN Gen3v(83H, 7, x); GenByte(y.a) (* cmp r/m32,imm8 *)
ELSE Gen3v(81H, 7, x); GenWord(y.a) (* cmp r/m32,imm32 *)
END
END
ELSE ORS.Mark("invalid cmp mode")
END
END cmp;
PROCEDURE cmpcon(VAR x: Item; y: INTEGER);
VAR z: Item;
BEGIN z.mode := ORB.Const; z.type := x.type; z.a := y; cmp(x, z)
END cmpcon;
PROCEDURE loadMemReg(VAR x: Item; targetSize: INTEGER; hint: SET);
BEGIN
IF (x.type.size < targetSize) OR (x.mode = ORB.Const) & (x.type.form # ORB.Proc) OR
~(x.mode IN {ORB.Var, ORB.Par, RegI, RegJ, Reg, RegX, RegY}) THEN
load(x, hint)
END
END loadMemReg;
PROCEDURE push(VAR x: Item);
VAR offset, vno: INTEGER;
BEGIN
IF x.mode = ORB.Const THEN (*push imm*)
IF x.type.form = ORB.Proc THEN
PrepFixup(0, FixCode, x.r, x.a, offset, vno);
IF offset = 0 THEN Gen1word(68H, 0); Fix(FixCode, x.r, vno)
ELSE Gen3v(0FFH, 6, x)
END
ELSIF (x.a >= -128) & (x.a <= 127) THEN Gen1(6AH); GenByte(x.a)
ELSE Gen1word(68H, x.a)
END
ELSIF x.mode = FStk THEN
Gen3(8DH, SP, SP, -4, anyoffset); (*lea esp,[esp-4]*)
IF x.type.form = ORB.Real THEN Gen3(0D9H, 3, SP, 0, anyoffset) (* fstp float32[esp] *)
ELSE Gen3(0DBH, 3, SP, 0, anyoffset) (*fistp int32[esp]*)
END;
decF
ELSE
loadMemReg(x, 4, regs);
IF x.mode = Reg THEN Gen1(50H + x.r) (*push reg32*)
ELSE Gen3v(0FFH, 6, x) (*push m32*)
END;
FreeItem(x)
END
END push;
PROCEDURE MakeConstVarItem (VAR x: Item; typ: ORB.Type; val: INTEGER);
VAR a: INTEGER;
BEGIN a := 0;
WHILE (a < strx) & (ThisStrWord(a) # val) DO INC(a, 4) END; (*find existing value*)
IF a >= strx THEN
IF a + 4 < maxStrx THEN PutStrWord(a, val); INC(strx, 4)
ELSE ORS.Mark("too many strings")
END
END;
x.mode := ORB.Var; x.type := typ; x.r := 0; x.a := varsize + a
END MakeConstVarItem;
PROCEDURE loadFloat(VAR x: Item);
VAR op, mode, form, base: INTEGER;
BEGIN mode := x.mode; form := x.type.form;
IF mode # FStk THEN
IF form = ORB.Real THEN op := 0D9H ELSE op := 0DBH END; (*m32fp/m32int*)
IF (mode = ORB.Const) & (form IN {ORB.Byte..ORB.Set}) THEN
IF x.a = 0 THEN Gen1(0D9EEH) (*fldz*)
ELSIF (form = ORB.Real) & (x.a = 3F800000H) THEN Gen1(0D9E8H) (*fld1*)
ELSIF (form # ORB.Real) & (x.a = 1) THEN Gen1(0D9E8H) (*fld1*)
ELSE MakeConstVarItem(x, x.type, x.a); Gen3v(op, 0, x) (*fld*)
END
ELSIF mode IN {ORB.Var, ORB.Par, RegI, RegJ, RegX, RegY} THEN
Gen3v(op, 0, x); FreeItem(x) (*fld*)
ELSE
push(x);
Gen3(op, 0, SP, 0, anyoffset); (*fld [esp]*)
Gen3(8DH, SP, SP, 4, anyoffset); (*lea esp,[esp+4]*)
END;
x.mode := FStk; x.type := ORB.realType; incF
END
END loadFloat;
PROCEDURE loadCond(VAR x: Item);
BEGIN
IF x.type.form = ORB.Bool THEN
IF x.mode = ORB.Const THEN
IF x.a = 0 THEN x.r := ccFalse ELSE x.r := ccTrue END
ELSE loadMemReg(x, x.type.size, {AX}); cmpcon(x, 0); FreeItem(x); x.r := ccNE
END;
x.mode := Cond; x.a := 0; x.b := 0
ELSE ORS.Mark("not Boolean?")
END
END loadCond;
PROCEDURE loadTypTagAdr(VAR x: Item; T: ORB.Type; hint: SET);
BEGIN x.mode := ORB.Var; x.a := T.len; x.r := -T.mno; loadAdr(x, hint)
END loadTypTagAdr;
PROCEDURE loadStringAdr(VAR x: Item; hint: SET);
VAR reg: INTEGER;
BEGIN
reg := GetReg(hint);
Gen1word(0B8H + reg, 0); Fix(FixVar, 0, varsize + x.a);
x.mode := Reg; x.r := reg
END loadStringAdr;
(* Items: Conversion from constants or from Objects on the Heap to Items on the Stack*)
PROCEDURE MakeConstItem*(VAR x: Item; typ: ORB.Type; val: LONGINT);
BEGIN x.mode := ORB.Const; x.type := typ; x.a := val
END MakeConstItem;
PROCEDURE MakeRealItem*(VAR x: Item; val: REAL);
BEGIN x.mode := ORB.Const; x.type := ORB.realType; x.a := SYSTEM.VAL(LONGINT, val)
END MakeRealItem;
PROCEDURE MakeStringItem*(VAR x: Item; len: LONGINT); (*copies string from ORS-buffer to ORG-string array*)
VAR i: LONGINT;
BEGIN x.mode := ORB.Const; x.type := ORB.strType; x.a := strx; x.b := len; i := 0;
IF strx + len + 4 < maxStrx THEN
WHILE len > 0 DO str[strx] := ORS.str[i]; INC(strx); INC(i); DEC(len) END ;
WHILE strx MOD 4 # 0 DO str[strx] := 0X; INC(strx) END
ELSE ORS.Mark("too many strings")
END
END MakeStringItem;
PROCEDURE MakeItem*(VAR x: Item; y: ORB.Object; curlev: LONGINT);
BEGIN x.mode := y.class; x.type := y.type; x.a := y.val; x.rdo := y.rdo;
IF y.class = ORB.Par THEN x.b := 0
ELSIF (y.class = ORB.Const) & (y.type.form = ORB.String) THEN x.b := y.lev (*len*) ;
ELSE x.r := y.lev
END ;
IF (y.lev > 0) & (y.lev # curlev) & (y.class # ORB.Const) THEN ORS.Mark("not accessible ") END
END MakeItem;
(* Code generation for basic operations *)
PROCEDURE xchg(a, b: INTEGER);
VAR t: INTEGER;
BEGIN
IF a # b THEN
IF b = AX THEN b := a; a := AX END; (* preffer eax as dst *)
IF a = AX THEN Gen1(90H + b) (* xchg eax,RB *)
ELSE Gen2(87H, a, b) (* xchg RA,RB *)
END
END
END xchg;
PROCEDURE shift(fct: INTEGER; VAR x, y: Item; hint: SET);
VAR op, r: INTEGER; usecx: BOOLEAN;
BEGIN
IF fct = 0 THEN op := 4 (* shl *) ELSIF fct = 1 THEN op := 7 (* sar *) ELSE op := 1 (* ror *) END;
IF y.mode = ORB.Const THEN
IF y.a # 0 THEN
load(x, hint);
IF fct = 0 THEN (* shl *)
IF y.a = 1 THEN Gen4(8DH, x.r, 2, x.r, NoReg, 0, anyoffset) (* lea x,[x*2] *)
ELSIF y.a = 2 THEN Gen4(8DH, x.r, 4, x.r, NoReg, 0, anyoffset) (* lea x,[x*4] *)
ELSIF y.a = 3 THEN Gen4(8DH, x.r, 8, x.r, NoReg, 0, anyoffset) (* lea x,[x*8] *)
ELSE Gen2(0C1H, op, x.r); GenByte(y.a) (* shl r32,imm8 *)
END
ELSIF y.a = 1 THEN Gen2(0D1H, op, x.r) (* shl r32,1 *)
ELSE Gen2(0C1H, op, x.r); GenByte(y.a) (* shl r32,imm8 *)
END
END
ELSE
load(x, hint - {CX}); load(y, {CX});
usecx := FALSE;
IF y.r # CX THEN
IF x.r = CX THEN xchg(y.r, x.r); r := y.r; y.r := x.r; y.r := r
ELSE usecx := TRUE; xchg(y.r, CX)
END
END;
Gen2(0D3H, op, x.r); (* shl x,cl *)
IF usecx THEN xchg(CX, y.r) END;
FreeReg(y.r)
END
END shift;
PROCEDURE addop(op: INTEGER; VAR x, y: Item; hint: SET; fse: BOOLEAN);
VAR subop, val: INTEGER;
BEGIN
IF (x.mode = ORB.Const) & (y.mode = ORB.Const) THEN
IF op = ORS.plus THEN x.a := x.a + y.a ELSE x.a := x.a - y.a END
ELSIF y.mode = ORB.Const THEN
IF fse THEN (*force side effects*)
IF (y.a >= -128) & (y.a <= 127) THEN
load(x, hint);
IF op = ORS.plus THEN Gen2(83H, 0, x.r) ELSE Gen2(83H, 5, x.r) END;
GenByte(y.a)
ELSE load(x, {AX});
IF x.r = AX THEN
IF op = ORS.plus THEN Gen1word(5H, y.a)
ELSE Gen1word(2DH, y.a)
END
ELSE
IF op = ORS.plus THEN Gen2(81H, 0, x.r); GenWord(y.a)
ELSE Gen2(81H, 5, x.r); GenWord(y.a)
END
END
END
ELSE
IF op = ORS.plus THEN val := y.a ELSE val := -y.a END;
load(x, hint); Gen3(8DH, x.r, x.r, val, anyoffset) (* lea reg,[reg+val] *)
END
ELSE
load(x, hint); loadMemReg(y, 4, hint);
IF op = ORS.plus THEN
IF ~fse & (y.mode = Reg) THEN Gen4(8DH, x.r, 1, x.r, y.r, 0, anyoffset) (* lea x,[x+r] *)
ELSE Gen3v(03H, x.r, y)
END
ELSE Gen3v(2BH, x.r, y)
END;
FreeItem(y)
END
END addop;
PROCEDURE log2(m: LONGINT; VAR e: LONGINT): LONGINT;
BEGIN e := 0;
WHILE ~ODD(m) DO m := m DIV 2; INC(e) END ;
RETURN m
END log2;
PROCEDURE mul(VAR x, y: Item; hint: SET);
VAR e, reg: INTEGER; temp: Item;
BEGIN
IF (x.mode = ORB.Const) & (y.mode = ORB.Const) THEN x.a := x.a * y.a
ELSIF (x.mode = ORB.Const) & (x.a = 0) OR (y.mode = ORB.Const) & (y.a = 0) THEN
FreeItem(x); FreeItem(y); x.mode := ORB.Const; x.a := 0
ELSIF (x.mode = ORB.Const) & (x.a = 1) THEN x := y
ELSIF (y.mode = ORB.Const) & (y.a = 1) THEN (* nop *)
ELSIF (x.mode = ORB.Const) OR (y.mode = ORB.Const) THEN
IF y.mode # ORB.Const THEN temp := x; x := y; y := temp END;
IF (y.mode = ORB.Const) & (y.a >= 2) & (log2(y.a, e) = 1) THEN
load(x, hint); y.a := e; shift(0, x, y, hint) (* shl x,y *)
ELSE (* imul reg32,r/m32,imm *)
loadMemReg(x, 4, hint);
IF x.mode IN {Reg, RegI, RegJ, RegX, RegY} THEN
IF (y.a >= -128) & (y.a < 128) THEN Gen3v(6BH, x.r, x); GenByte(y.a)
ELSE Gen3v(69H, x.r, x); GenWord(y.a)
END;
x.mode := Reg
ELSE (* imul reg32,mem,imm *)
reg := GetReg(hint);
IF (y.a >= -128) & (y.a < 128) THEN Gen3v(6BH, reg, x); GenByte(y.a)
ELSE Gen3v(69H, reg, x); GenWord(y.a)
END;
x.mode := Reg; x.r := reg
END
END
ELSE (* imul reg32,r/m32 *)
load(x, hint); loadMemReg(y, 4, hint);
Gen3v(0FAFH, x.r, y);
FreeItem(y)
END
END mul;
PROCEDURE sop(op: INTEGER; VAR x, y: Item; hint: SET);
VAR xset, yset: SET; yint: INTEGER; temp: Item;
BEGIN
IF (x.mode = ORB.Const) & (y.mode = ORB.Const) THEN
xset := SYSTEM.VAL(SET, x.a); yset := SYSTEM.VAL(SET, y.a);
IF op = ORS.plus THEN xset := xset + yset
ELSIF op = ORS.minus THEN xset := xset - yset
ELSIF op = ORS.times THEN xset := xset * yset
ELSE (*op = ORS.rdiv*) xset := xset / yset
END;
x.a := SYSTEM.VAL(INTEGER, xset)
ELSIF (x.mode = ORB.Const) OR (y.mode = ORB.Const) THEN
IF x.mode = ORB.Const THEN
load(y, hint); yint := x.a;