-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegenr.py
2560 lines (2409 loc) · 78 KB
/
codegenr.py
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
#!/bin/python
# PyOberon 0.1 - Oberon 07 compiler (re-)written in Python
# Copyright (C) 2016 John "The Blue Wizard" Rogers
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# PyOberon code generator for RISC 5
# NW 31.5.2015 code generator in Oberon-07 for RISC
# # MODULE ORG;
# Note: The file naming convention here is this: codegen[CPU]{size}.py
# where CPU is currently "r" for RISC 0/5 as originally written in
# ORG.Mod, and "rv" for RISC-V/G, and "size" is either "32" (default;
# can be omitted) and "64". There is no 64-bit version of RISC 0/5,
# so there won't be a Python version for it.
# # IMPORT SYSTEM, Files, ORS, ORB;
import scanner, symbols
# Code generator for Oberon compiler for RISC 5 processor.
# Procedural interface to Parser OSP; result in array "code".
# Procedure Close writes code-files
# CONST
WordSize = 4
StkOrg0 = -64
VarOrg0 = 0 # for RISC-0 only
# dedicated registers
MT = 12
SB = 13
SP = 14
LNK = 15
# maxCode = 8000
maxStrx = 2400
maxTD = 120
C24 = 0x1000000
# internal item modes
Reg = 10
RegI = 11
Cond = 12
# frequently used opcodes
U = 0x2000
V = 0x1000
Mov = 0
Lsl = 1
Asr = 2
Ror = 3
And = 4
Ann = 5
Ior = 6
Xor = 7
Add = 8
Sub = 9
Cmp = 9
Mul = 10
Div = 11
Fad = 12
Fsb = 13
Fml = 14
Fdv = 15
Ldr = 8
Str = 10
BR = 0
BLR = 1
BC = 2
BL = 3
MI = 0
PL = 8
EQ = 1
NE = 9
LT = 5
GE = 13
LE = 6
GT = 14
# # TYPE Item* = RECORD
# # mode*: INTEGER;
# # type*: ORB.Type;
# # a*, b*, r: LONGINT;
# # rdo*: BOOLEAN (*read only*)
# # 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
# # VAR pc*, varsize: LONGINT; (*program counter, data index*)
# # tdx, strx: LONGINT;
# # entry: LONGINT; (*main entry point*)
# # RH: LONGINT; (*available registers R[0] ... R[H-1]*)
# # curSB: LONGINT; (*current static base in SB*)
# # frame: LONGINT; (*frame offset changed in SaveRegs and RestoreRegs*)
# # fixorgP, fixorgD, fixorgT: LONGINT; (*origins of lists of locations to be fixed up by loader*)
# # check: BOOLEAN; (*emit run-time checks*)
# # version: INTEGER; (* 0 = RISC-0, 1 = RISC-5 *)
# # relmap: ARRAY 6 OF INTEGER; (*condition codes for relations*)
# # code: ARRAY maxCode OF LONGINT;
# # data: ARRAY maxTD OF LONGINT; (*type descriptors*)
# # str: ARRAY maxStrx OF CHAR;
relmap = [1, 9, 5, 6, 14, 13]
# instruction assemblers according to formats
# # PROCEDURE Put0(op, a, b, c: LONGINT);
def Put0(op, a, b, c):
# # BEGIN (*emit format-0 instruction*)
# emit format-0 instruction
# # code[pc] := ((a*10H + b) * 10H + op) * 10000H + c; INC(pc)
code.append(((a*0x10 + b) * 0x10 + op) * 0x10000 + c)
# # END Put0;
# # PROCEDURE Put1(op, a, b, im: LONGINT);
def Put1(op, a, b, im):
# # BEGIN (*emit format-1 instruction, -10000H <= im < 10000H*)
# emit format-1 instruction, -0x10000 <= im < 0x10000
# # IF im < 0 THEN INC(op, V) END ;
if im < 0:
op += V
# # code[pc] := (((a+40H) * 10H + b) * 10H + op) * 10000H + (im MOD 10000H); INC(pc)
code.append((((a+0x40) * 0x10 + b) * 0x10 + op) * 0x10000 + (im & 0xFFFF))
# END Put1;
# # PROCEDURE Put1a(op, a, b, im: LONGINT);
def Put1a(op, a, b, im):
# # BEGIN (*same as Pu1, but with range test -10000H <= im < 10000H*)
# same as Put1, but with range test -0x10000 <= im < 0x10000
# # IF (im >= -10000H) & (im <= 0FFFFH) THEN Put1(op, a, b, im)
if (im >= -0x10000) and (im <= 0xFFFF):
Put1(op, a, b, im)
# # ELSE Put1(Mov+U, RH, 0, im DIV 10000H);
else:
Put1(Mov+U, RH, 0, im/0x10000)
# # IF im MOD 10000H # 0 THEN Put1(Ior, RH, RH, im MOD 10000H) END ;
if (im & 0xFFFF) != 0:
Put1(Ior, RH, RH, im & 0xFFFF)
# # Put0(op, a, b, RH)
Put0(op, a, b, RH)
# # END
# # END Put1a;
# # PROCEDURE Put2(op, a, b, off: LONGINT);
def Put2(op, a, b, off):
# # BEGIN (*emit load/store instruction*)
# emit load/store instruction
# # code[pc] := ((op * 10H + a) * 10H + b) * 100000H + (off MOD 100000H); INC(pc)
code.append(((op * 0x10 + a) * 0x10 + b) * 0x100000 + (off & 0xFFFFF))
# # END Put2;
# # PROCEDURE Put3(op, cond, off: LONGINT);
def Put3(op, cond, off):
# # BEGIN (*emit branch instruction*)
# emit branch instruction
# # code[pc] := ((op+12) * 10H + cond) * 1000000H + (off MOD 1000000H); INC(pc)
code.append(((op+12) * 0x10 + cond) * 0x1000000 + (off & 0xFFFFFF))
# # END Put3;
# # PROCEDURE incR;
def incR():
global RH
# # BEGIN
# # IF RH < MT-1 THEN INC(RH) ELSE ORS.Mark("register stack overflow") END
if RH < MT-1:
RH += 1
else:
print "register stack overflow"
# # END incR;
# # PROCEDURE CheckRegs*;
def CheckRegs():
global RH
# # BEGIN
# # IF RH # 0 THEN ORS.Mark("Reg Stack"); RH := 0 END ;
if RH != 0:
print "Reg Stack"
RH = 0
# # IF pc >= maxCode - 40 THEN ORS.Mark("Program too long") END
if len(code) >= maxCode - 40: # FIXME: should it be made obsolete?
print "Program too long"
# # END CheckRegs;
# # PROCEDURE SetCC(VAR x: Item; n: LONGINT);
def SetCC(x, n):
# # BEGIN x.mode := Cond; x.a := 0; x.b := 0; x.r := n
x.mode = Cond
x.a = 0
x.b = 0
x.r = n
# # END SetCC;
# # PROCEDURE Trap(cond, num: LONGINT);
def Trap(cond, num):
# # BEGIN num := ORS.Pos()*100H + num*10H + MT; Put3(BLR, cond, num)
num = num*0x10 + MT # FIXME: I elect not to use .Pos for now, due to technicalities
Put3(BLR, cond, num)
# # END Trap;
# handling of forward reference, fixups of branch addresses and constant tables
# # PROCEDURE negated(cond: LONGINT): LONGINT;
def negated(cond):
# # BEGIN
# # IF cond < 8 THEN cond := cond+8 ELSE cond := cond-8 END ;
if cond < 8:
cond += 8
else:
cond -= 8
# # RETURN cond
return cond
# # END negated;
# # PROCEDURE invalSB;
def invalSB():
global curSB
# # BEGIN curSB := 1
curSB = 1
# # END invalSB;
# # PROCEDURE fix(at, with: LONGINT);
def fix(at, _with):
# # BEGIN code[at] := code[at] DIV C24 * C24 + (with MOD C24)
code[at] = code[at] # FIXME
# # END fix;
# # PROCEDURE FixLink*(L: LONGINT);
def FixLink(L):
# # VAR L1: LONGINT;
# # BEGIN invalSB;
invalSB()
# # WHILE L # 0 DO L1 := code[L] MOD 40000H; fix(L, pc-L-1); L := L1 END
while L != 0:
L1 = code[L] & 0x3FFFF
fix(L, len(code)-L-1)
L = L1
# # END FixLink;
# # PROCEDURE FixLinkWith(L0, dst: LONGINT);
def FixLinkWith(L0, dst):
# # VAR L1: LONGINT;
# # BEGIN
# # WHILE L0 # 0 DO
while L0 != 0:
# # L1 := code[L0] MOD C24;
L1 = code[L0] & 0x3FFFF # FIXME
# # code[L0] := code[L0] DIV C24 * C24 + ((dst - L0 - 1) MOD C24); L0 := L1
code[L0] = code[L0] # FIXME
L0 = L1
# # END
# # END FixLinkWith;
# # PROCEDURE merged(L0, L1: LONGINT): LONGINT;
def merged(L0, L1):
# # VAR L2, L3: LONGINT;
# # BEGIN
# # IF L0 # 0 THEN L3 := L0;
if L0 != 0:
L3 = L0
# # REPEAT L2 := L3; L3 := code[L2] MOD 40000H UNTIL L3 = 0;
while True:
L2 = L3
L3 = code[L2] & 0x3FFFF
if L3 == 0:
break
# # code[L2] := code[L2] + L1; L1 := L0
code[L2] += L1
L1 = L0
# # END ;
# # RETURN L1
return L1
# # END merged;
# loading of operands and addresses into registers
# # PROCEDURE GetSB(base: LONGINT);
def GetSB(base):
global fixorgD, curSB
# # BEGIN
# # IF (version # 0) & ((base # curSB) OR (base # 0)) THEN
if (version != 0) and ((base != curSB) or (base != 0)):
# # Put2(Ldr, SB, -base, pc-fixorgD); fixorgD := pc-1; curSB := base
Put2(Ldr, SB, -base, len(code)-fixorgD)
fixorgD = len(code)-1
curSB = base
# # END
# # END GetSB;
# # PROCEDURE NilCheck;
def NilCheck():
# # BEGIN IF check THEN Trap(EQ, 4) END
if check:
Trap(EQ, 4)
# # END NilCheck;
# # PROCEDURE load(VAR x: Item);
def load(x):
# # VAR op: LONGINT;
# # BEGIN
# # IF x.type.size = 1 THEN op := Ldr+1 ELSE op := Ldr END ;
if x.type.size == 1:
op = Ldr+1
else:
op = Ldr
# # IF x.mode # Reg THEN
if x.mode == Reg:
return
# # IF x.mode = ORB.Const THEN
if x.mode == symbols.Const:
# # IF x.type.form = ORB.Proc THEN
if x.type.form == symbols.Proc:
# # IF x.r > 0 THEN ORS.Mark("not allowed")
if x.r > 0:
print "not allowed"
# # ELSIF x.r = 0 THEN Put3(BL, 7, 0); Put1a(Sub, RH, LNK, pc*4 - x.a)
if x.r == 0:
Put3(BL, 7, 0)
Put1a(Sub, RH, LNK, len(code)*4 - x.a)
# # ELSE GetSB(x.r); Put1(Add, RH, SB, x.a + 100H) (*mark as progbase-relative*)
else:
GetSB(x.r)
Put1(Add, RH, SB, x.a + 0x100) # mark as progbase-relative
# # END
# # ELSIF (x.a <= 0FFFFH) & (x.a >= -10000H) THEN Put1(Mov, RH, 0, x.a)
elif (x.a <= 0xFFFF) and (x.a >= -0x10000):
Put1(Mov, RH, 0, x.a)
# # ELSE Put1(Mov+U, RH, 0, x.a DIV 10000H MOD 10000H);
else:
Put1(Mov+U, RH, 0, x.a DIV 0x10000 MOD 0x10000)
# # IF x.a MOD 10000H # 0 THEN Put1(Ior, RH, RH, x.a MOD 10000H) END
if (x.a & 0xFFFF) != 0:
Put1(Ior, RH, RH, x.a & 0xFFFF)
# # END ;
# # x.r := RH; incR
x.r = RH
incR()
# # ELSIF x.mode = ORB.Var THEN
elif x.mode == symbols.Var:
# # IF x.r > 0 THEN (*local*) Put2(op, RH, SP, x.a + frame)
if x.r > 0: # local
Put2(op, RH, SP, x.a + frame)
# # ELSE GetSB(x.r); Put2(op, RH, SB, x.a)
else:
GetSB(x.r)
Put2(op, RH, SB, x.a)
# # END ;
# # x.r := RH; incR
x.r = RH
incR()
# # ELSIF x.mode = ORB.Par THEN Put2(Ldr, RH, SP, x.a + frame); Put2(op, RH, RH, x.b); x.r := RH; incR
elif x.mode == symbols.Par:
Put2(Ldr, RH, SP, x.a + frame)
Put2(op, RH, RH, x.b)
x.r = RH
incR()
# # ELSIF x.mode = RegI THEN Put2(op, x.r, x.r, x.a)
elif x.mode == RegI:
Put2(op, x.r, x.r, x.a)
# # ELSIF x.mode = Cond THEN
elif x.mode == Cond:
# # Put3(BC, negated(x.r), 2);
Put3(BC, negated(x.r), 2)
# # FixLink(x.b); Put1(Mov, RH, 0, 1); Put3(BC, 7, 1);
FixLink(x.b)
Put1(Mov, RH, 0, 1)
Put3(BC, 7, 1)
# # FixLink(x.a); Put1(Mov, RH, 0, 0); x.r := RH; incR
FixLink(x.a)
Put1(Mov, RH, 0, 0)
x.r = RH
incR()
# # END ;
# # x.mode := Reg
x.mode = Reg
# # END
# # END load;
# # PROCEDURE loadAdr(VAR x: Item);
def loadAdr(x):
# # BEGIN
# # IF x.mode = ORB.Var THEN
if x.mode == symbols.Var:
# # IF x.r > 0 THEN (*local*) Put1a(Add, RH, SP, x.a + frame)
if x.r > 0: # local
Put1a(Add, RH, SP, x.a + frame)
# # ELSE GetSB(x.r); Put1a(Add, RH, SB, x.a)
else:
GetSB(x.r)
Put1a(Add, RH, SB, x.a)
# # END ;
# # x.r := RH; incR
x.r = RH
incR()
# # ELSIF x.mode = ORB.Par THEN Put2(Ldr, RH, SP, x.a + frame);
elif x.mode == symbols.Par:
# # IF x.b # 0 THEN Put1a(Add, RH, RH, x.b) END ;
if x.b != 0:
Put1a(Add, RH, RH, x.b)
# # x.r := RH; incR
x.r = RH
incR()
# # ELSIF x.mode = RegI THEN
elif x.mode == RegI:
# # IF x.a # 0 THEN Put1a(Add, x.r, x.r, x.a) END
if x.a != 0:
Put1a(Add, x.r, x.r, x.a)
# # ELSE ORS.Mark("address error")
else:
print "address error"
# # END ;
# # x.mode := Reg
x.mode = Reg
# # END loadAdr;
# # PROCEDURE loadCond(VAR x: Item);
def loadCond(x):
global RH
# # BEGIN
# # IF x.type.form = ORB.Bool THEN
if x.type.form == symbols.Bool:
# # IF x.mode = ORB.Const THEN x.r := 15 - x.a*8
if x.mode == symbols.Const:
x.r = 15 - x.a*8
# # ELSE load(x);
else:
load(x)
# # IF code[pc-1] DIV 40000000H # -2 THEN Put1(Cmp, x.r, x.r, 0) END ;
if code[-1] / 0x40000000 != -2:
Put1(Cmp, x.r, x.r, 0)
# # x.r := NE; DEC(RH)
x.r = NE
RH -= 1
# # END ;
# # x.mode := Cond; x.a := 0; x.b := 0
x.mode = Cond
x.a = 0
x.b = 0
# # ELSE ORS.Mark("not Boolean?")
else:
print "not Boolean?"
# # END
# # END loadCond;
# # PROCEDURE loadTypTagAdr(T: ORB.Type);
def loadTypTagAdr(T):
# # VAR x: Item;
# # BEGIN x.mode := ORB.Var; x.a := T.len; x.r := -T.mno; loadAdr(x)
x.mode = symbols.Var
x.a = T._len
x.r = -T.mno
loadAdr(x)
# # END loadTypTagAdr;
# # PROCEDURE loadStringAdr(VAR x: Item);
def loadStringAdr(x):
# # BEGIN GetSB(0); Put1a(Add, RH, SB, varsize+x.a); x.mode := Reg; x.r := RH; incR
GetSB(0)
Put1a(Add, RH, SB, varsize+x.a)
x.mode = Reg
x.r = RH
incR()
# # 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);
def MakeConstItem(x, typ, val):
# # BEGIN x.mode := ORB.Const; x.type := typ; x.a := val
x.mode = symbols.Const
x.type = typ
x.a = val
# # END MakeConstItem;
# # PROCEDURE MakeRealItem*(VAR x: Item; val: REAL);
def MakeRealItem(x, val):
# # BEGIN x.mode := ORB.Const; x.type := ORB.realType; x.a := SYSTEM.VAL(LONGINT, val)
x.mode = symbols.Const
x.type = symbols.realType
x.a = SYSTEM.VAL(LONGINT, val) # FIXME
# # END MakeRealItem;
# FIXME
# # PROCEDURE MakeStringItem*(VAR x: Item; len: LONGINT); (*copies string from ORS-buffer to ORG-string array*)
def MakeStringItem(x, slen): #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;
x.mode = symbols.Const
x.type = symbols.strType
x.a = strx
x.b = slen
i = 0
# # IF strx + len + 4 < maxStrx THEN
if strx + slen + 4 < maxStrx:
# # WHILE len > 0 DO str[strx] := ORS.str[i]; INC(strx); INC(i); DEC(len) END ;
while slen > 0:
_str[strx] = scanner._str[i]
strx += 1
i += 1
slen -= 1
# # WHILE strx MOD 4 # 0 DO str[strx] := 0X; INC(strx) END
while strx % 4 != 0:
_str[strx] = '\0'
strx += 1
# # ELSE ORS.Mark("too many strings")
else:
print "too many strings"
# # END
# # END MakeStringItem;
# # PROCEDURE MakeItem*(VAR x: Item; y: ORB.Object; curlev: LONGINT);
def MakeItem(x, y, curlev):
# # BEGIN x.mode := y.class; x.type := y.type; x.a := y.val; x.rdo := y.rdo;
x.mode = y.klass
x.type = y.type
x.a = y.val
x.rdo = y.rdo
# # IF y.class = ORB.Par THEN x.b := 0
if y.klass == symbols.Par:
x.b = 0
# # ELSIF y.class = ORB.Typ THEN x.a := y.type.len; x.r := -y.lev
elif y.klass == symbols.Typ:
x.a = y.type._len
x.r := -y.lev
# # ELSIF (y.class = ORB.Const) & (y.type.form = ORB.String) THEN x.b := y.lev (*len*)
elif (y.klass == symbols.Const) and (y.type.form == symbols.String):
x.b = y.lev # len
# # ELSE x.r := y.lev
else:
x.r = y.lev
# # END ;
# # IF (y.lev > 0) & (y.lev # curlev) & (y.class # ORB.Const) THEN ORS.Mark("level error, not accessible") END
if (y.lev > 0) and (y.lev != curlev) and (y.klass != symbols.Const):
print "level error, not accessible"
# # END MakeItem;
# Code generation for Selectors, Variables, Constants
# # PROCEDURE Field*(VAR x: Item; y: ORB.Object); (* x := x.y *)
def Field(x, y): # x := x.y
# # BEGIN;
# # IF x.mode = ORB.Var THEN
if x.mode == symbols.Var:
# # IF x.r >= 0 THEN x.a := x.a + y.val
if x.r >= 0:
x.a += y.val
# # ELSE loadAdr(x); x.mode := RegI; x.a := y.val
else:
loadAdr(x)
x.mode = RegI
x.a = y.val
# # END
# # ELSIF x.mode = RegI THEN x.a := x.a + y.val
elif x.mode == RegI:
x.a += y.val
# # ELSIF x.mode = ORB.Par THEN x.b := x.b + y.val
elif x.mode == symbols.Par:
x.b += y.val
# # END
# # END Field;
# # PROCEDURE Index*(VAR x, y: Item); (* x := x[y] *)
def Index(x, y): # x := x[y]
global RH
# # VAR s, lim: LONGINT;
# # BEGIN s := x.type.base.size; lim := x.type.len;
s = x.type.base.size
lim = x.type._len
# # IF (y.mode = ORB.Const) & (lim >= 0) THEN
if (y.mode = symbols.Const) and (lim >= 0):
# # IF (y.a < 0) OR (y.a >= lim) THEN ORS.Mark("bad index") END ;
if (y.a < 0) OR (y.a >= lim):
print "bad index"
# # IF x.mode IN {ORB.Var, RegI} THEN x.a := y.a * s + x.a
if x.mode in (symbols.Var, RegI):
x.a += y.a * s
# # ELSIF x.mode = ORB.Par THEN x.b := y.a * s + x.b
elif x.mode == symbols.Par:
x.b += y.a * s
# # END
# # ELSE load(y);
else:
load(y)
# # IF check THEN (*check array bounds*)
if check(): # check array bounds
# # IF lim >= 0 THEN Put1a(Cmp, RH, y.r, lim)
if lim >= 0:
Put1a(Cmp, RH, y.r, lim)
# # ELSE (*open array*)
else: # open array
# # IF x.mode IN {ORB.Var, ORB.Par} THEN Put2(Ldr, RH, SP, x.a+4+frame); Put0(Cmp, RH, y.r, RH)
if x.mode in (symbols.Var, symbols.Par):
Put2(Ldr, RH, SP, x.a+4+frame)
Put0(Cmp, RH, y.r, RH)
# # ELSE ORS.Mark("error in Index")
else:
print "error in Index"
# # END
# # END ;
# # Trap(10, 1) (*BCC*)
Trap(10, 1) # BCC
# # END ;
# # IF s = 4 THEN Put1(Lsl, y.r, y.r, 2) ELSIF s > 1 THEN Put1a(Mul, y.r, y.r, s) END ;
if s == 4:
Put1(Lsl, y.r, y.r, 2)
elif s > 1:
Put1a(Mul, y.r, y.r, s)
# # IF x.mode = ORB.Var THEN
if x.mode == symbols.Var:
# # IF x.r > 0 THEN Put0(Add, y.r, SP, y.r); INC(x.a, frame)
if x.r > 0:
Put0(Add, y.r, SP, y.r)
x.a += frame
# # ELSE GetSB(x.r);
else:
GetSB(x.r)
# # IF x.r = 0 THEN Put0(Add, y.r, SB, y.r)
if x.r == 0:
Put0(Add, y.r, SB, y.r)
# # ELSE Put1a(Add, RH, SB, x.a); Put0(Add, y.r, RH, y.r); x.a := 0
else:
Put1a(Add, RH, SB, x.a)
Put0(Add, y.r, RH, y.r)
x.a = 0
# # END
# # END ;
# # x.r := y.r; x.mode := RegI
x.r = y.r
x.mode = RegI
# # ELSIF x.mode = ORB.Par THEN
elif x.mode == symbols.Par:
# # Put2(Ldr, RH, SP, x.a + frame);
Put2(Ldr, RH, SP, x.a + frame)
# # Put0(Add, y.r, RH, y.r); x.mode := RegI; x.r := y.r; x.a := x.b
Put0(Add, y.r, RH, y.r)
x.mode = RegI
x.r = y.r
x.a = x.b
# # ELSIF x.mode = RegI THEN Put0(Add, x.r, x.r, y.r); DEC(RH)
elif x.mode == RegI:
Put0(Add, x.r, x.r, y.r)
RH -= 1
# # END
# # END
# # END Index;
# # PROCEDURE DeRef*(VAR x: Item);
def DeRef(x):
# # BEGIN
# # IF x.mode = ORB.Var THEN
if x.mode == symbols.Var:
# # IF x.r > 0 THEN (*local*) Put2(Ldr, RH, SP, x.a + frame) ELSE GetSB(x.r); Put2(Ldr, RH, SB, x.a) END ;
if x.r > 0: # local
Put2(Ldr, RH, SP, x.a + frame)
else:
GetSB(x.r)
Put2(Ldr, RH, SB, x.a)
# # NilCheck; x.r := RH; incR
NilCheck()
x.r = RH
incR()
# # ELSIF x.mode = ORB.Par THEN
elif x.mode == symbols.Par:
# # Put2(Ldr, RH, SP, x.a + frame); Put2(Ldr, RH, RH, x.b); NilCheck; x.r := RH; incR
Put2(Ldr, RH, SP, x.a + frame)
Put2(Ldr, RH, RH, x.b)
NilCheck()
x.r = RH
incR()
# # ELSIF x.mode = RegI THEN Put2(Ldr, x.r, x.r, x.a); NilCheck
elif x.mode == RegI:
Put2(Ldr, x.r, x.r, x.a)
NilCheck()
# # ELSIF x.mode # Reg THEN ORS.Mark("bad mode in DeRef")
elif x.mode != Reg:
print "bad mode in DeRef"
# # END ;
# # x.mode := RegI; x.a := 0; x.b := 0
x.mode = RegI
x.a = 0
x.b = 0
# # END DeRef;
# # PROCEDURE Q(T: ORB.Type; VAR dcw: LONGINT);
def Q(T, dcw):
# # BEGIN (*one entry of type descriptor extension table*)
# one entry of type descriptor extension table
# # IF T.base # NIL THEN
if T.base != None:
# # Q(T.base, dcw); data[dcw] := (T.mno*1000H + T.len) * 1000H + dcw - fixorgT;
Q(T.base, dcw)
data[dcw] = (T.mno*0x1000 + T._len) * 0x1000 + dcw - fixorgT
# # fixorgT := dcw; INC(dcw)
fixorgT = dcw
dcw += 1
# # END
# # END Q;
# # PROCEDURE FindPtrFlds(typ: ORB.Type; off: LONGINT; VAR dcw: LONGINT);
def FindPtrFlds(typ, off, dcw):
# # VAR fld: ORB.Object; i, s: LONGINT;
# # BEGIN
# # IF (typ.form = ORB.Pointer) OR (typ.form = ORB.NilTyp) THEN data[dcw] := off; INC(dcw)
if (typ.form == symbols.Pointer) or (typ.form == symbols.NilTyp):
data[dcw] = off
dcw += 1
# # ELSIF typ.form = ORB.Record THEN
elif typ.form == symbols.Record:
# # fld := typ.dsc;
fld = typ.dsc
# # WHILE fld # NIL DO FindPtrFlds(fld.type, fld.val + off, dcw); fld := fld.next END
while fld != None:
FindPtrFlds(fld.type, fld.val + off, dcw)
fld := fld.next
# # ELSIF typ.form = ORB.Array THEN
elif typ.form == symbols.Array:
# # s := typ.base.size;
s = typ.base.size
# # FOR i := 0 TO typ.len-1 DO FindPtrFlds(typ.base, i*s + off, dcw) END
for i in range(typ._len):
FindPtrFlds(typ.base, i*s + off, dcw)
# # END
# # END FindPtrFlds;
# # PROCEDURE BuildTD*(T: ORB.Type; VAR dc: LONGINT);
def BuildTD(T, dc):
# # VAR dcw, k, s: LONGINT; (*dcw = word address*)
# # BEGIN dcw := dc DIV 4; s := T.size; (*convert size for heap allocation*)
dcw = dc / 4 # dcw = word address
s = T.size # convert size for heap allocation
# # IF s <= 24 THEN s := 32 ELSIF s <= 56 THEN s := 64 ELSIF s <= 120 THEN s := 128
if s <= 24:
s = 32
elif s <= 56:
s = 64
elif s <= 120:
s := 128
# # ELSE s := (s+263) DIV 256 * 256
else:
s = (s+263) / 256 * 256
# # END ;
# # T.len := dc; data[dcw] := s; INC(dcw);
T.len = dc
data[dcw] = s
dcw += 1
# # k := T.nofpar; (*extension level!*)
k = T.nofpar # extension level!
# # IF k > 3 THEN ORS.Mark("ext level too large")
if k > 3:
print "ext level too large"
# # ELSE Q(T, dcw);
else:
Q(T, dcw)
# # WHILE k < 3 DO data[dcw] := -1; INC(dcw); INC(k) END
while k < 3:
data[dcw] = -1
dcw += 1
k += 1
# # END ;
# # FindPtrFlds(T, 0, dcw); data[dcw] := -1; INC(dcw); tdx := dcw; dc := dcw*4;
FindPtrFlds(T, 0, dcw)
data[dcw] = -1
dcw += 1
tdx = dcw
dc = dcw*4
# # IF tdx >= maxTD THEN ORS.Mark("too many record types"); tdx := 0 END
if tdx >= maxTD:
print "too many record types")
tdx = 0
# # END BuildTD;
# # PROCEDURE TypeTest*(VAR x: Item; T: ORB.Type; varpar, isguard: BOOLEAN);
def TypeTest(x, T, varpar, isguard):
global RH
# # VAR pc0: LONGINT;
# # BEGIN (*fetch tag into RH*)
# fetch tag into RH
# # IF varpar THEN Put2(Ldr, RH, SP, x.a+4+frame)
if varpar:
Put2(Ldr, RH, SP, x.a+4+frame)
# # ELSE load(x);
else:
load(x)
# # pc0 := pc; Put3(BC, EQ, 0); (*NIL belongs to every pointer type*)
pc0 = len(code)
Put3(BC, EQ, 0) # NIL belongs to every pointer type
# # Put2(Ldr, RH, x.r, -8)
Put2(Ldr, RH, x.r, -8)
# # END ;
# # Put2(Ldr, RH, RH, T.nofpar*4); incR;
Put2(Ldr, RH, RH, T.nofpar*4)
incR()
# # loadTypTagAdr(T); (*tag of T*)
loadTypTagAdr(T) # tag of T
# # Put0(Cmp, RH-1, RH-1, RH-2); DEC(RH, 2);
Put0(Cmp, RH-1, RH-1, RH-2)
RH -= 2
# # IF ~varpar THEN fix(pc0, pc - pc0 - 1) END ;
if !varpar:
fix(pc0, len(code) - pc0 - 1)
# # IF isguard THEN
if isguard:
# # IF check THEN Trap(NE, 2) END
if check:
Trap(NE, 2)
# # ELSE SetCC(x, EQ);
else:
SetCC(x, EQ)
# # IF ~varpar THEN DEC(RH) END
if !varpar:
RH -= 1
# # END
# # END TypeTest;
# Code generation for Boolean operators
# # PROCEDURE Not*(VAR x: Item); (* x := ~x *)
def Not(x): # x := ~x
# # VAR t: LONGINT;
# # BEGIN
# # IF x.mode # Cond THEN loadCond(x) END ;
if x.mode != Cond:
loadCond(x)
# # x.r := negated(x.r); t := x.a; x.a := x.b; x.b := t
x.r = negated(x.r)
t = x.a
x.a = x.b
x.b := t
# # END Not;
# # PROCEDURE And1*(VAR x: Item); (* x := x & *)
def And1(x): # x := x &
# # BEGIN
# # IF x.mode # Cond THEN loadCond(x) END ;
if x.mode != Cond:
loadCond(x)
# # Put3(BC, negated(x.r), x.a); x.a := pc-1; FixLink(x.b); x.b := 0
Put3(BC, negated(x.r), x.a)
x.a = len(code)-1
FixLink(x.b)
x.b = 0
# # END And1;
# # PROCEDURE And2*(VAR x, y: Item);
def And2(x, y):
# # BEGIN
# # IF y.mode # Cond THEN loadCond(y) END ;
if y.mode != Cond:
loadCond(y)
# # x.a := merged(y.a, x.a); x.b := y.b; x.r := y.r
x.a = merged(y.a, x.a)
x.b = y.b
x.r = y.r
# # END And2;
# # PROCEDURE Or1*(VAR x: Item); (* x := x OR *)
def Or1(x): # x := x OR
# # BEGIN
# # IF x.mode # Cond THEN loadCond(x) END ;
if x.mode != Cond:
loadCond(x)
# # Put3(BC, x.r, x.b); x.b := pc-1; FixLink(x.a); x.a := 0
Put3(BC, x.r, x.b)
x.b = len(code)-1
FixLink(x.a)
x.a = 0
# # END Or1;
# # PROCEDURE Or2*(VAR x, y: Item);
def Or2(x, y):
# # BEGIN
# # IF y.mode # Cond THEN loadCond(y) END ;
if y.mode != Cond:
loadCond(y)
# # x.a := y.a; x.b := merged(y.b, x.b); x.r := y.r
x.a = y.a
x.b = merged(y.b, x.b)
x.r := y.r
# # END Or2;
# Code generation for arithmetic operators
# # PROCEDURE Neg*(VAR x: Item); (* x := -x *)
def Neg(x): # x := -x
# # BEGIN
# # IF x.type.form = ORB.Int THEN
if x.type.form == symbols.Int:
# # IF x.mode = ORB.Const THEN x.a := -x.a
if x.mode == symbols.Const:
x.a := -x.a
# # ELSE load(x); Put1(Mov, RH, 0, 0); Put0(Sub, x.r, RH, x.r)
else:
load(x)
Put1(Mov, RH, 0, 0)
Put0(Sub, x.r, RH, x.r)
# # END
# # ELSIF x.type.form = ORB.Real THEN
elif x.type.form == symbols.Real:
# # IF x.mode = ORB.Const THEN x.a := x.a + 7FFFFFFFH + 1
if x.mode == symbols.Const:
x.a += 0x7FFFFFFF + 1 # FIXME
# # ELSE load(x); Put1(Mov, RH, 0, 0); Put0(Fsb, x.r, RH, x.r)
else:
load(x)
Put1(Mov, RH, 0, 0)
Put0(Fsb, x.r, RH, x.r)
# # END
# # ELSE (*form = Set*)
else: # form = Set
# # IF x.mode = ORB.Const THEN x.a := -x.a-1
if x.mode == symbols.Const:
x.a = -x.a-1
# # ELSE load(x); Put1(Xor, x.r, x.r, -1)
else:
load(x)
Put1(Xor, x.r, x.r, -1)
# # END
# # END
# # END Neg;
# # PROCEDURE AddOp*(op: LONGINT; VAR x, y: Item); (* x := x +- y *)
def AddOp(op, x, y): # x := x +- y
global RH
# # BEGIN
# # IF op = ORS.plus THEN
if op == ORS.plus:
# # IF (x.mode = ORB.Const) & (y.mode = ORB.Const) THEN x.a := x.a + y.a
if (x.mode == symbols.Const) and (y.mode == symbols.Const):
x.a += y.a
# # ELSIF y.mode = ORB.Const THEN load(x);
elif y.mode == symbols.Const:
load(x)
# # IF y.a # 0 THEN Put1a(Add, x.r, x.r, y.a) END
if y.a != 0:
Put1a(Add, x.r, x.r, y.a)
# # ELSE load(x); load(y); Put0(Add, RH-2, x.r, y.r); DEC(RH); x.r := RH-1
else:
load(x)
load(y)
Put0(Add, RH-2, x.r, y.r)
RH -= 1
x.r = RH-1
# # END
# # ELSE (*op = ORS.minus*)
else: # op = ORS.minus
# # IF (x.mode = ORB.Const) & (y.mode = ORB.Const) THEN x.a := x.a - y.a
if (x.mode == symbols.Const) and (y.mode == symbols.Const):
x.a -= y.a
# # ELSIF y.mode = ORB.Const THEN load(x);
load(x)
# # IF y.a # 0 THEN Put1a(Sub, x.r, x.r, y.a) END
if y.a != 0:
Put1a(Sub, x.r, x.r, y.a)
# # ELSE load(x); load(y); Put0(Sub, RH-2, x.r, y.r); DEC(RH); x.r := RH-1
else:
load(x)
load(y)
Put0(Sub, RH-2, x.r, y.r)
RH -= 1
x.r = RH-1
# # END
# # END
# # END AddOp;
# # PROCEDURE log2(m: LONGINT; VAR e: LONGINT): LONGINT;
def log2(m, e): # FIXME
# # BEGIN e := 0;
e = 0
# # WHILE ~ODD(m) DO m := m DIV 2; INC(e) END ;
while (m % 2) == 0:
m /= 2
e += 1
# # RETURN m