-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobg.c
More file actions
3011 lines (2785 loc) · 64.5 KB
/
obg.c
File metadata and controls
3011 lines (2785 loc) · 64.5 KB
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
//Oberon7 code generator
//Based on Niklaus Wirth's implementation
//Compile using gcc
//$ gcc -Wall obs.c obt.c obg.c obp.c obc.c
//This code is distributed under the GPL License.
//For more info, check: http://www.gnu.org/copyleft/gpl.html
//15 June 2016: Srinivas Nayak: This file created
//24 June 2016: Srinivas Nayak: Coding started
//10 July 2016: Srinivas Nayak: Code updated with NW's version of 04 July 2016
//Code generator for Oberon7 compiler for x86 processor.
//Procedural interface to Parser; result in array "code".
//Procedure Close writes code-files.
//logical shift left = arithmetic shift left
//logical shift right # arithmetic shift right
//arithmetic shift right inserts sign bit from left side
//other three shifts inserts 0
//C shift operator << on unsigned type is logical shift left
//C shift operator << on signed type is arithmetic shift left
//that means << is indifferent on unsigned and signed type
//C shift operator >> on unsigned type is logical shift right
//C shift operator >> on signed type is arithmetic shift right
//Oberon MUL is equivalent to C logical shift left
//Oberon DIV is equivalent to C arithmetic shift right
//Oberon MOD is equivalent to C bitwise AND
#include "obc.h"
//maximum size of generated code
#define maxCode 8000
//maximum size of string literals
#define maxStrx 2400
//maximum size of type descriptors
#define maxTD 120
int WordSize = 4,
StkOrg0 = -64, VarOrg0 = 0, //for RISC-0 only
MT = 12, SB = 13, SP = 14, LNK = 15, //dedicated registers
C24 = 0x1000000, //24 bit addresses for code
Reg = 10, RegI = 11, Cond = 12, //item modes, class values are from 0 to 8
//frequently used opcodes
//these two sets mode
U = 0x2000, V = 0x1000,
//bitwise operations
Mov = 0, Lsl = 1, Asr = 2, Ror= 3,
//logical operations
And = 4, Ann = 5, Ior = 6, Xor = 7,
//integer arithmetic operations
Add = 8, Sub = 9, Cmp = 9, Mul = 10, Div = 11,
//floating point arithmetic operations
Fad = 12, Fsb = 13, Fml = 14, Fdv = 15,
//load and store operations
Ldr = 8, Str = 10,
//branching operations
BR = 0, BLR = 1, BC = 2, BL = 3,
//condition codes
MI = 0, PL = 8, //to denote positive or negative number
EQ = 1, NE = 9, //only six relational operators exist in language
//CS, CC //so these are not used
//VS, VC
//LS, HI
LT = 5, GE = 13,
LE = 6, GT = 14;
int riscver = 1; //0 = RISC-0, 1 = RISC-5
int entry; //module entry point
int RH; //available registers R[0] ... R[H-1]
int curSB; //current static base in SB
int frame; //frame offset changed in SaveRegs and RestoreRegs
int fixorgP, fixorgD, fixorgT; //origins of lists of locations to be fixed up by loader
int check; //emit run-time checks
int relmap[6]; //condition codes for relations
int code[maxCode]; //contains generated code
int pc; //program counter
int data[maxTD];//contains type descriptors
int tdx; //global type descriptor counter
int varsize; //data index; holds whole size of total variables declared in a module, which gets its value from dc of OBP
char _str[maxStrx]; //coniains all string literals
int strx; //global string length counter
//declarations
void Put0(int op, int a, int b, int c);
void Put1(int op, int a, int b, int im);
void Put1a(int op, int a, int b, int im);
void Put2(int op, int a, int b, int off);
void Put3(int op, int cond, int off);
int Here();
void CheckRegs();
void Header();
void incR();
void initObg();
void invalSB();
void NilCheck();
void Close(char* modid, int key, int nofent);
void Enter(int parblksize, int locblksize, int internal);
void Open(int v);
void SetDataSize(int dc);
int merged(int L0, int L1);
int negated(int cond);
void BJump(int L);
void CBJump(Item* x, int L);
void CFJump(Item* x);
void fix(int at, int with);
void FixLink(int L);
void FixLinkWith(int L0, int dst);
void FJump(int *L);
void GetSB(int base);
void RestoreRegs(int r); //R[0 .. r-1]
void SaveRegs(int r);
void Trap(int cond, int num);
int NofPtrs(Type typ);
void BuildTD(Type T, int* dc);
void FindPtrFlds(Type typ, int off, int *dcw);
void loadTypTagAdr(Type T);
void Q(Type T, int *dcw);
int _log2(int m, int* e);
void Abs(Item* x);
void ADC(Item* x, Item* y);
void AddOp(int op, Item* x, Item *y); // x := x +- y
void Adr(Item* x);
void And1(Item* x); //x := x &
void And2(Item* x, Item* y);
void Assert(Item* x);
void Bit(Item* x, Item* y);
void Call(Item* x, int r);
void Condition(Item* x);
void Copy(Item* x, Item* y , Item* z);
void CopyString(Item*x, Item* y); //x := y
void DeRef(Item* x);
void DivOp(int op, Item* x, Item* y); // x := x op y
void Field(Item* x, Object y); // x := x.y
void Fixup(Item* x);
void Float(Item* x);
void Floor(Item* x);
void For0(Item* x, Item* y);
void For1(Item* x, Item* y, Item* z, Item* w, int* L);
void For2(Item* x, Item* y, Item* w);
void _Get(Item* x, Item* y);
void H(Item* x);
void Include(int inorex, Item* x, Item* y);
void Increment(int upordown, Item* x, Item* y);
void Index(Item* x, Item* y); //x := x[y]
void In(Item* x, Item* y); // x := x IN y
void IntRelation(int op, Item* x, Item* y); // x := x < y
void LDPSR(Item* x);
void LDREG(Item* x, Item* y);
void Led(Item* x);
void Len(Item* x);
void loadAdr(Item* x);
void loadCond(Item* x);
void load(Item* x);
void loadStringAdr(Item* x);
void MakeConstItem(Item* x, Type typ, int val);
void MakeItem(Item* x, Object y, int curlev);
void MakeRealItem(Item* x, float val);
void MakeStringItem(Item* x, int len);
void MulOp(Item* x, Item* y); //x := x * y
void Neg(Item* x); // x := -x
void New(Item* x);
void Not(Item* x); //x := ~x
void Odd(Item* x);
void OpenArrayParam(Item* x);
void Or1(Item* x); //x := x OR
void Or2(Item* x, Item* y);
void Ord(Item* x);
void Pack(Item* x, Item* y);
void PrepCall(Item* x, int* r);
void Put(Item* x, Item* y);
void RealOp(int op, Item* x, Item* y); // x := x op y
void RealRelation(int op, Item* x, Item* y ); // x := x < y
void Register(Item* x);
void Return(int form, Item* x, int size, int internal);
void SBC(Item* x, Item* y);
void SetCC(Item *x, int n);
void _Set(Item* x, Item* y); // x := {x .. y}
void SetOp(int op, Item* x, Item* y); // x := x op y
void Shift(int fct, Item* x, Item* y);
void Singleton(Item* x); // x := {x}
void Store(Item* x, Item* y); //x := y
void StoreStruct(Item* x, Item* y); // x := y, frame = 0
void StringParam(Item* x);
void StringRelation(int op, Item* x, Item* y); // x := x < y
void StrToChar(Item* x );
void _TypeTest(Item* x, Type T, int varpar, int isguard);
void UML(Item* x, Item* y);
void Unpk(Item* x, Item* y);
void ValueParam(Item* x);
void VarParam(Item* x, Type ftype);
void FindPtrs(FILE* R, Type typ, int adr);
void WriteByte(FILE *R, int x);
void WriteInt(FILE *R, int x);
char* instr[16] = {"MOV", "LSL", "ASR", "ROR",
"AND", "ANN", "IOR", "XOR",
"ADD", "SUB", "MUL", "DIV",
"FAD", "FSB", "FML", "FDV"};
char* reg[16] = {"R0", "R1", "R2", "R3",
"R4", "R5", "R6", "R7",
"R8", "R9", "R10", "R11",
"MT", "SB", "SP", "LNK"};
char* condcode[16] = {"MI", "EQ", "CS", "VS",
"LS", "LT", "LE", "TR",
"PL", "NE", "CC", "VC",
"HI", "GE", "GT", "FL"};
//emit Format0 instructions
void Put0(int op, int a, int b, int c)
{
//emit format-0 instruction
code[pc] = ((a*0x10 + b) * 0x10 + op) * 0x10000 + c;
pc++;
//printf("\npos = %d", pos());
//printf("\nPut0: op=%d a=%d b=%d c=%d", op, a, b, c);
//printf("\n%#x\n", code[pc-1]);
print_sym();
print_put0_asm(pc-1, op, a, b, c);
printf("\n");
}
void print_put0_asm(int pc, int op, int a, int b, int c)
{
printf("\ncode[%d]: ", pc);
switch(op)
{
case 0:
{
printf("%s %s, 0, %s", instr[op], reg[a], reg[c]);
break;
}
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
{
printf("%s %s, %s, %s", instr[op], reg[a], reg[b], reg[c]);
break;
}
default:
{
printf("unknown asm");
}
}
}
//emit Format1 instructions when im is 16bit, i.e. -0x10000 <= im < 0x10000
void Put1(int op, int a, int b, int im)
{
int temp_op = op;
if( im < 0 )
{
op = op + V;
}
code[pc] = (((a+0x40) * 0x10 + b) * 0x10 + op) * 0x10000 + (im & 0xFFFF);
pc++;
//printf("\npos = %d", pos());
//printf("\nPut1: op=%d a=%d b=%d im=%d", op, a, b, im);
//printf("\n%#x\n", code[pc-1]);
print_sym();
print_put1_asm(pc-1, temp_op, a, b, im);
printf("\n");
}
void print_put1_asm(int pc, int op, int a, int b, int im)
{
printf("\ncode[%d]: ", pc);
switch(op)
{
case 0:
{
printf("%s %s, 0, %d", instr[op], reg[a], im);
break;
}
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
{
printf("%s %s, %s, %d", instr[op], reg[a], reg[b], im);
break;
}
default:
{
printf("unknown asm");
}
}
}
//emit Format1 instructions when im is 32bit
void Put1a(int op, int a, int b, int im)
{
//if im is 16bit, i.e. -0x10000H <= im < 0x10000
if( (im >= -0x10000) && (im < 0x10000) )
{
Put1(op, a, b, im);
}
else //if im is 32bit
{
Put1(Mov+U, RH, 0, im >> 16); //MSBs of RH = MSBs of im
if( (im & 0xFFFF) != 0 ) //if there is any LSB
{
Put1(Ior, RH, RH, im & 0xFFFF); //RH = RH | LSBs of im
}
Put0(op, a, b, RH); //Ra = Rb op RH
}
}
//emit load/store Format2 instruction
void Put2(int op, int a, int b, int off)
{
code[pc] = ((op * 0x10 + a) * 0x10 + b) * 0x100000 + (off & 0xFFFFF);
pc++;
//printf("\npos = %d", pos());
//printf("\nPut2: op=%d a=%d b=%d off=%d", op, a, b, off);
//printf("\n%#x\n", code[pc-1]);
print_sym();
print_put2_asm(pc-1, op, a, b, off);
printf("\n");
}
void print_put2_asm(int pc, int op, int a, int b, int off)
{
printf("\ncode[%d]: ", pc);
switch(op)
{
case 8:
{
printf("LDW %s, %s, %d", reg[a], reg[b], off);
break;
}
case 9:
{
printf("LDB %s, %s, %d", reg[a], reg[b], off);
break;
}
case 10:
{
printf("STW %s, %s, %d", reg[a], reg[b], off);
break;
}
case 11:
{
printf("STB %s, %s, %d", reg[a], reg[b], off);
break;
}
default:
{
printf("unknown asm");
}
}
}
//emit branch instruction
void Put3(int op, int cond, int off)
{
code[pc] = ((op+12) * 0x10 + cond) * 0x1000000 + (off & 0xFFFFFF);
pc++;
//printf("\npos = %d", pos());
//printf("\nPut3: op=%d cond=%d off=%d", op, cond, off);
//printf("\n%#x\n", code[pc-1]);
print_sym();
print_put3_asm(pc-1, op, cond, off);
printf("\n");
}
void print_put3_asm(int pc, int op, int cond, int off)
{
printf("\ncode[%d]: ", pc);
switch(op)
{
case 0:
{
printf("BR %s, %s", condcode[cond], reg[off]);
break;
}
case 1:
{
printf("BLR %s, %s", condcode[cond], reg[off]);
break;
}
case 2:
{
printf("BC %s, %d", condcode[cond], off);
break;
}
case 3:
{
printf("BL %s, %d", condcode[cond], off);
break;
}
default:
{
printf("unknown asm");
}
}
}
//increament register stack top.
//R0-R11 is treated as a register stack
//R12-R15 are special purpose registers
//MT = 12, SB = 13, SP = 14, LNK = 15
void incR()
{
if( RH < MT-1 )
{
RH++;
}
else
{
Mark("register stack overflow");
}
}
//used in StatSequence() after consuming one statement
void CheckRegs()
{
if( RH != 0 )
{
Mark("Reg Stack");
RH = 0;
}
if( pc >= maxCode - 40 )
{
Mark("program too long");
}
}
//converts Item type to Cond
//here n is processor condition code
void SetCC(Item *x, int n)
{
x->mode = Cond;
x->a = 0;
x->b = 0;
x->r = n;
}
//Module Table address is at MT register
void Trap(int cond, int num)
{
Put3(BLR, cond, pos()*0x100 + num*0x10 + MT);
//pos() value is left shifted 8 bits,
//num left shifted 4 bits
}
//handling of forward reference, fixups of branch addresses and constant tables
//MI becomes PL
//EQ becomes NE
//LT becomes GE
//LE becomes GT
//and vice-versa
int negated(int cond)
{
if( cond < 8 )
{
cond = cond+8;
}
else
{
cond = cond-8;
}
return cond;
}
//invalidate Static Base, which is base address of module variables
void invalSB()
{
curSB = 1;
}
//fix offset at 'code[at]' with 'with'
void fix(int at, int with)
{
code[at] = ((code[at] >> 24) * C24) + (with & 0xFFFFFF);
}
//fix actual address of a forward jump
//in already generated forward jump instruction
void FixLink(int L)
{
int L1;
invalSB();
while( L != 0 ) //in some cases we need loop
{
L1 = code[L] & 0x3FFFF; //why? only 18bits being extracted? RAM size 1MB, addressable by 20bits; branch instruction offset is in words, so 2 bits less.
fix(L, pc-L-1); //why? pc-L-1? see below
L = L1;
}
//at addr L we had inserted BC, 7, off.
//now we are at addr M and got to know that
//our BC should jump to addr M, so we called FixLink.
//when BC is executed at addr L,
//BC will calculate jump addr as L + 1 + off;
//and result should be addr M,
//i.e. L + 1 + off = M.
//since we are at addr M now, our PC = M.
//hence, L + 1 + off = PC;
//i.e. off = PC - L - 1.
}
void FixLinkWith(int L0, int dst)
{
int L1;
while( L0 != 0 )
{
L1 = code[L0] & 0xFFFFFF; //why? 0xFFFFFF? seems it can be 0x3FFFF, see FixLink
code[L0] = ((code[L0] >> 24) * C24) + ((dst - L0 - 1) & 0xFFFFFF); //why? dst-L0-1? see FixLink
L0 = L1;
}
}
//if L0 is 0, {do nothing} return L1
//else when L0 is valid, {merge L0 with L1} and return L0
//how to do merge?
//goto addr L0
//get off from that instruction
//say we got A
//goto addr A
//get off from that instruction
//say we got B
//goto addr B
//get off from that instruction
//say we got C
//goto addr C
//get off from that instruction
//say we got 0
//goto addr C again
//write L1 as off in that instruction
int merged(int L0, int L1)
{
int L2, L3;
if( L0 != 0 )
{
L3 = L0;
do
{
L2 = L3;
L3 = code[L2] & 0x3FFFF;
}
while(!( L3 == 0 ));
code[L2] = code[L2] + L1;
L1 = L0;
}
return L1;
}
// loading of operands and addresses into registers
//base is a -ve number representing imported module
//or 0 meaning this module
void GetSB(int base)
{
if( (riscver != 0) && ((base != curSB) || (base != 0)) ) //why? ((base != curSB) || (base != 0))? for risc-5, this fails only when base = curSB and base = 0, i.e. base = curSB = 0. seems this case is unlikely to happen. so this condition will always pass!
{
Put2(Ldr, SB, -base, pc-fixorgD); //why? -base? because base is either 0 (means this module) or -ve (means some imported module)
fixorgD = pc-1; //why?
curSB = base;
}
}
//null reference trap, equivalent to segfault
void NilCheck()
{
if( check )
{
Trap(EQ, 4); //if Z bit is set
}
}
//***************
//converts Item mode to Reg
//takes care of all Const (including Procedures) but not of string Const
//takes care of Var, Par, RegI, Cond, but not of Fld, Typ
void load(Item* x)
{
int op;
if( x->type->size == 1 )
{
op = Ldr+1; //load byte
}
else
{
op = Ldr; //load word
}
if( x->mode != Reg )
{
if( x->mode == Const )
{
if( x->type->form == Proc )
{
if( x->r > 0 ) //why? what it means?
{
Mark("not allowed");
}
else if( x->r == 0 ) //x->r is 0 means we are calling a procedure of this module
{
Put3(BL, 7, 0); //basically it means go to next instruction while saving next instruction address in LNK register; L: r15:=pc + 1 word and goto pc+1+0; 1 means 1 word, 0 is for 0 word offset
Put1a(Sub, RH, LNK, pc*4 - x->a); //basically it loads procedure address which is address of this instruction (which is just saved into LNK register by above instruction) minus offset found in x->a; L + 1 word: r0:=r15-((L+1)*4 - x->a);
}
else //x->r is -ve means we are calling a procedure of some imported module
{
GetSB(x->r);
Put1(Add, RH, SB, x->a + 0x100); //relative to static base. why? 0x100?
}
}
else if( (x->a <= 0x0FFFF) && (x->a >= -0x10000) ) //16bit value
{
Put1(Mov, RH, 0, x->a);
}
else //32bit value
{
Put1(Mov+U, RH, 0, (x->a >> 16) & 0xFFFF);
if( (x->a & 0xFFFF) != 0 )
{
Put1(Ior, RH, RH, x->a & 0xFFFF);
}
}
x->r = RH;
incR();
}
else if( x->mode == Var )
{
if( x->r > 0 ) //x->r > 0 means it is a local variable to a procedure
{
Put2(op, RH, SP, x->a + frame); //get value from mem[SP+offset]
}
else //x->r = 0 means global variable of this module; x->r < 0 means exported variable of some imported module
{
GetSB(x->r);
Put2(op, RH, SB, x->a); //get value from mem[SB+offset]
}
x->r = RH;
incR();
}
else if( x->mode == Par ) //parameter declared with VAR
{
Put2(Ldr, RH, SP, x->a + frame); //get address into RH
Put2(op, RH, RH, x->b); //get value into RH from mem[RH+0]
x->r = RH;
incR();
}
else if( x->mode == RegI )
{
Put2(op, x->r, x->r, x->a); //get value from mem[x->r + x->a] to x->r
}
else if( x->mode == Cond )
{
Put3(BC, negated(x->r), 2); //if condition is false, jump to last Put1() instruction below. why?
FixLink(x->b); //x->b holds Fjump address
Put1(Mov, RH, 0, 1); //why RH:=R1?
Put3(BC, 7, 1); //jump past this below instruction
FixLink(x->a); //x->a holds Tjump address
Put1(Mov, RH, 0, 0); //why RH:=R0?
x->r = RH;
incR();
}
x->mode = Reg;
}
}
void loadAdr(Item* x)
{
if( x->mode == Var )
{
if( x->r > 0 ) //if local variable of a procedure
{
Put1a(Add, RH, SP, x->a + frame); //RH := SP + offset
}
else
{
GetSB(x->r);
Put1a(Add, RH, SB, x->a); //RH := SB + offset
}
x->r = RH;
incR();
}
else if( x->mode == Par ) //parameter declared with VAR
{
Put2(Ldr, RH, SP, x->a + frame); //get address into RH
if( x->b != 0 ) //why? for Par, what does x->b contain? it contains selector offset
{
Put1a(Add, RH, RH, x->b); //RH := RH + x->b
}
x->r = RH;
incR();
}
else if( x->mode == RegI )
{
if( x->a != 0 )
{
Put1a(Add, x->r, x->r, x->a); //x->r := x->r + x->a
}
}
else
{
Mark("address error");
}
x->mode = Reg;
}
void loadCond(Item* x)
{
if( x->type->form == Bool )
{
if( x->mode == Const )
{
x->r = 15 - x->a*8; //x->r := 7 (true) or 15 (false)
}
else
{
load(x);
if( (code[pc-1] >> 30) != -2 ) //if previous instruction is a format 2 (load or store) instruction
{
Put1(Cmp, x->r, x->r, 0); //x->r := x->r - 0
}
x->r = NE; //not equal to zero
RH--;
}
x->mode = Cond;
x->a = 0;
x->b = 0;
}
else
{
Mark("not Boolean?");
}
}
void loadTypTagAdr(Type T)
{
Item x;
x.mode = Var; //trick to execute a piece of code in loadAdr()
x.a = T->len; //len holds descriptor address for records
x.r = -T->mno; //mno holds positive value of imported module number; becomes negative here
loadAdr(&x);
}
void loadStringAdr(Item* x)
{
GetSB(0); //for current module
Put1a(Add, RH, SB, varsize+x->a); //RH := SB + varsize+x->a
x->mode = Reg;
x->r = RH;
incR();
}
// Items: Conversion from literals or from Objects on the Heap to Items on the Stack
// makes item for INT, CHAR, NIL, FALS, TRU symbols/literals
void MakeConstItem(Item* x, Type typ, int val)
{
x->mode = Const;
x->type = typ;
x->a = val;
}
//makes item for REAL symbols/literals
void MakeRealItem(Item* x, float val)
{
union
{
float f;
int i;
} u;
u.f = val;
x->mode = Const;
x->type = realType;
x->a = u.i; //equivalent to: x->a = val
//type casting from float to int will truncate the value
//so union trick is used to put a float value unchanged into int variable
}
//makes item for constant string literals
//copies string from ORS-buffer str[] to ORG-string array _str[]
void MakeStringItem(Item* x, int len)
{
int i;
x->mode = Const;
x->type = strType;
x->a = strx; //address of this string constant
x->b = len; //length of this string constant as given by ORS, which covers '\0'
i = 0;
if( strx + len + 4 < maxStrx )
{
while( len > 0 ) //copy the string constant and the trailing '\0' as well
{
_str[strx] = str[i];
strx++;
i++;
len--;
}
while( strx % 4 != 0 ) //make strx 4 byte aligned
{
_str[strx] = '\0';
strx++;
}
}
else
{
Mark("too many strings");
}
}
void MakeItem(Item* x, Object y, int curlev)
{
x->mode = y->class;
x->type = y->type;
x->rdo = y->rdo;
if( y->class == Par ) //parameter declared with VAR
{
x->a = y->val;
x->b = 0;
//x->r
}
else if( y->class == Typ )
{
x->a = y->type->len;
//x->b
x->r = -y->lev;
}
else if( (y->class == Const) && (y->type->form == String) )
{
x->a = y->val;
x->b = y->lev; //lev abused for string length
//x->r
}
else
{
x->a = y->val;
//x->b
x->r = y->lev; //lev denotes if a variable is local to procedure
}
if( (y->lev > 0) && (y->lev != curlev) && (y->class != Const) )
{
Mark("level error, not accessible");
}
}
// Code generation for Selectors, Variables, Constants
void Field(Item* x, Object y) // x := x.y
{
if( x->mode == Var )
{
if( x->r >= 0 ) //if local to procedure or global of current module
{
x->a = x->a + y->val; //offset of x + offset of ys
}
else
{
loadAdr(x);
x->mode = RegI;
x->a = y->val;
}
}
else if( x->mode == RegI )
{
x->a = x->a + y->val;
}
else if( x->mode == Par )
{
x->b = x->b + y->val; //x->b holds selector offset
}
}
void Index(Item* x, Item* y) //x := x[y]
{
int s, lim;
s = x->type->base->size; //datatype size
lim = x->type->len; //array size
if( (y->mode == Const) && (lim >= 0) ) //if y is a constant
{
if( (y->a < 0) || (y->a >= lim) ) //check boundary
{
Mark("bad index");
}
if( x->mode == Var || x->mode == RegI )
{
x->a = x->a + (y->a * s);
}
else if( x->mode == Par )
{
x->b = x->b + (y->a * s); //x->b holds selector offset
}
}
else //if y is not a constant
{
load(y); //bring y value in to a register
if( check )//if checking of array bounds allowed
{
if( lim >= 0 )
{
Put1a(Cmp, RH, y->r, lim); //compare y with array size
}
else //open array
{
if( x->mode == Var || x->mode == Par )
{
Put2(Ldr, RH, SP, x->a+4+frame); //x->a offset is for array parameter and (x->a)+4 ofset is for open array lenth
Put0(Cmp, RH, y->r, RH);
}
else
{
Mark("error in Index");
}
}
Trap(10, 1); //10 = CC cond code = if carry bit clear
}
//if datatype size is 4
if( s == 4 )
{
Put1(Lsl, y->r, y->r, 2); //left shift y value by 2 bits
}
else if( s > 1 ) //if datatype size is not 4 but more than 1
{
Put1a(Mul, y->r, y->r, s); //multiply y value with datatype size
}
if( x->mode == Var )
{
if( x->r > 0 ) //if x array is local to procedure
{
Put0(Add, y->r, SP, y->r);
x->a = x->a + frame;
//to point to y th element in array x,
//we have to add SP, x->a and y->r.
//but since we will change Item x's mode to RegI,
//we just add SP and y->r, which will become x->r below
//and we simply adjust x->a adding 'frame'
//remember that RegI means: addr = addr = Reg[r] + a
}
else
{
GetSB(x->r);
if( x->r == 0 ) //if array x is in current module
{
Put0(Add, y->r, SB, y->r); //just add SB and y->r