forked from alexfru/SmallerC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgmips.c
2258 lines (2066 loc) · 60.5 KB
/
cgmips.c
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
/*
Copyright (c) 2012-2015, Alexey Frunze
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************************/
/* */
/* Smaller C */
/* */
/* A simple and small single-pass C compiler ("small C" class). */
/* */
/* MIPS code generator */
/* */
/*****************************************************************************/
// Works around bugs in RetroBSD's as instruction reordering
//#define REORDER_WORKAROUND
STATIC
void GenInit(void)
{
// initialization of target-specific code generator
SizeOfWord = 4;
OutputFormat = FormatSegmented;
CodeHeaderFooter[0] = "\t.text";
DataHeaderFooter[0] = "\t.data";
RoDataHeaderFooter[0] = "\t.rdata";
BssHeaderFooter[0] = "\t.bss";
UseLeadingUnderscores = 0;
#ifdef REORDER_WORKAROUND
FileHeader = "\t.set\tnoreorder";
#else
FileHeader = "\t.set\treorder";
#endif
}
STATIC
int GenInitParams(int argc, char** argv, int* idx)
{
(void)argc;
// initialization of target-specific code generator with parameters
if (!strcmp(argv[*idx], "-v"))
{
// RetroBSD's cc may supply this parameter. Just need to consume it.
return 1;
}
return 0;
}
STATIC
void GenInitFinalize(void)
{
// finalization of initialization of target-specific code generator
}
STATIC
void GenStartCommentLine(void)
{
printf2(" # ");
}
STATIC
void GenWordAlignment(int bss)
{
(void)bss;
printf2("\t.align 2\n");
}
STATIC
void GenLabel(char* Label, int Static)
{
{
if (!Static && GenExterns)
printf2("\t.globl\t%s\n", Label);
printf2("%s:\n", Label);
}
}
STATIC
void GenPrintLabel(char* Label)
{
{
if (isdigit(*Label))
printf2("$L%s", Label);
else
printf2("%s", Label);
}
}
STATIC
void GenNumLabel(int Label)
{
printf2("$L%d:\n", Label);
}
STATIC
void GenPrintNumLabel(int label)
{
printf2("$L%d", label);
}
STATIC
void GenZeroData(unsigned Size, int bss)
{
(void)bss;
printf2("\t.space\t%u\n", truncUint(Size)); // or ".fill size"
}
STATIC
void GenIntData(int Size, int Val)
{
Val = truncInt(Val);
if (Size == 1)
printf2("\t.byte\t%d\n", Val);
else if (Size == 2)
printf2("\t.half\t%d\n", Val);
else if (Size == 4)
printf2("\t.word\t%d\n", Val);
}
STATIC
void GenStartAsciiString(void)
{
printf2("\t.ascii\t");
}
STATIC
void GenAddrData(int Size, char* Label, int ofs)
{
ofs = truncInt(ofs);
if (Size == 1)
printf2("\t.byte\t");
else if (Size == 2)
printf2("\t.half\t");
else if (Size == 4)
printf2("\t.word\t");
GenPrintLabel(Label);
if (ofs)
printf2(" %+d", ofs);
puts2("");
}
#define MipsInstrNop 0x00
#define MipsInstrMov 0x01
#define MipsInstrMfLo 0x02
#define MipsInstrMfHi 0x03
#define MipsInstrLA 0x06
#define MipsInstrLI 0x07
//#define MipsInstrLUI
#define MipsInstrLB 0x08
#define MipsInstrLBU 0x09
#define MipsInstrLH 0x0A
#define MipsInstrLHU 0x0B
#define MipsInstrLW 0x0C
#define MipsInstrSB 0x0D
#define MipsInstrSH 0x0E
#define MipsInstrSW 0x0F
#define MipsInstrAddU 0x10
#define MipsInstrSubU 0x11
#define MipsInstrAnd 0x12
#define MipsInstrOr 0x13
#define MipsInstrXor 0x14
#define MipsInstrNor 0x15
#define MipsInstrSLL 0x16
#define MipsInstrSRL 0x17
#define MipsInstrSRA 0x18
#define MipsInstrMul 0x19
#define MipsInstrDiv 0x1A
#define MipsInstrDivU 0x1B
#define MipsInstrSLT 0x1C
#define MipsInstrSLTU 0x1D
#define MipsInstrJ 0x1E
#define MipsInstrJAL 0x1F
#define MipsInstrBEQ 0x20
#define MipsInstrBNE 0x21
#define MipsInstrBLTZ 0x22
#define MipsInstrBGEZ 0x23
#define MipsInstrBLEZ 0x24
#define MipsInstrBGTZ 0x25
#define MipsInstrSeb 0x26
#define MipsInstrSeh 0x27
STATIC
void GenPrintInstr(int instr, int val)
{
char* p = "";
(void)val;
switch (instr)
{
case MipsInstrNop : p = "nop"; break;
case MipsInstrMov : p = "move"; break;
case MipsInstrMfLo : p = "mflo"; break;
case MipsInstrMfHi : p = "mfhi"; break;
case MipsInstrLA : p = "la"; break;
case MipsInstrLI : p = "li"; break;
// case MipsInstrLUI : p = "lui"; break;
case MipsInstrLB : p = "lb"; break;
case MipsInstrLBU : p = "lbu"; break;
case MipsInstrLH : p = "lh"; break;
case MipsInstrLHU : p = "lhu"; break;
case MipsInstrLW : p = "lw"; break;
case MipsInstrSB : p = "sb"; break;
case MipsInstrSH : p = "sh"; break;
case MipsInstrSW : p = "sw"; break;
case MipsInstrAddU : p = "addu"; break;
case MipsInstrSubU : p = "subu"; break;
case MipsInstrAnd : p = "and"; break;
case MipsInstrOr : p = "or"; break;
case MipsInstrXor : p = "xor"; break;
case MipsInstrNor : p = "nor"; break;
case MipsInstrSLL : p = "sll"; break;
case MipsInstrSRL : p = "srl"; break;
case MipsInstrSRA : p = "sra"; break;
case MipsInstrMul : p = "mul"; break;
case MipsInstrDiv : p = "div"; break;
case MipsInstrDivU : p = "divu"; break;
case MipsInstrSLT : p = "slt"; break;
case MipsInstrSLTU : p = "sltu"; break;
case MipsInstrJ : p = "j"; break;
case MipsInstrJAL : p = "jal"; break;
case MipsInstrBEQ : p = "beq"; break;
case MipsInstrBNE : p = "bne"; break;
case MipsInstrBLTZ : p = "bltz"; break;
case MipsInstrBGEZ : p = "bgez"; break;
case MipsInstrBLEZ : p = "blez"; break;
case MipsInstrBGTZ : p = "bgtz"; break;
case MipsInstrSeb : p = "seb"; break;
case MipsInstrSeh : p = "seh"; break;
}
printf2("\t%s\t", p);
}
#define MipsOpRegZero 0x00
#define MipsOpRegAt 0x01
#define MipsOpRegV0 0x02
#define MipsOpRegV1 0x03
#define MipsOpRegA0 0x04
#define MipsOpRegA1 0x05
#define MipsOpRegA2 0x06
#define MipsOpRegA3 0x07
#define MipsOpRegT0 0x08
#define MipsOpRegT1 0x09
#define MipsOpRegT2 0x0A
#define MipsOpRegT3 0x0B
#define MipsOpRegT4 0x0C
#define MipsOpRegT5 0x0D
#define MipsOpRegT6 0x0E
#define MipsOpRegT7 0x0F
#define MipsOpRegS0 0x10
#define MipsOpRegS1 0x11
#define MipsOpRegS2 0x12
#define MipsOpRegS3 0x13
#define MipsOpRegS4 0x14
#define MipsOpRegS5 0x15
#define MipsOpRegS6 0x16
#define MipsOpRegS7 0x17
#define MipsOpRegT8 0x18
#define MipsOpRegT9 0x19
#define MipsOpRegSp 0x1D
#define MipsOpRegFp 0x1E
#define MipsOpRegRa 0x1F
#define MipsOpIndRegZero 0x20
#define MipsOpIndRegAt 0x21
#define MipsOpIndRegV0 0x22
#define MipsOpIndRegV1 0x23
#define MipsOpIndRegA0 0x24
#define MipsOpIndRegA1 0x25
#define MipsOpIndRegA2 0x26
#define MipsOpIndRegA3 0x27
#define MipsOpIndRegT0 0x28
#define MipsOpIndRegT1 0x29
#define MipsOpIndRegSp 0x3D
#define MipsOpIndRegFp 0x3E
#define MipsOpIndRegRa 0x3F
#define MipsOpConst 0x80
#define MipsOpLabel 0x81
#define MipsOpNumLabel 0x82
#define MipsOpLabelLo 0x83
#define MipsOpIndLocal MipsOpIndRegFp
#define MAX_TEMP_REGS 8 // this many temp registers used beginning with T0 to hold subexpression results
#define TEMP_REG_A MipsOpRegT8 // two temporary registers used for momentary operations, similarly to the AT register
#define TEMP_REG_B MipsOpRegT9
#ifdef REORDER_WORKAROUND
STATIC
void GenNop(void)
{
puts2("\tnop");
}
#endif
STATIC
void GenPrintOperand(int op, int val)
{
if (op >= MipsOpRegZero && op <= MipsOpRegRa)
{
printf2("$%d", op);
}
else if (op >= MipsOpIndRegZero && op <= MipsOpIndRegRa)
{
printf2("%d($%d)", truncInt(val), op - MipsOpIndRegZero);
}
else
{
switch (op)
{
case MipsOpConst: printf2("%d", truncInt(val)); break;
case MipsOpLabelLo:
printf2("%%lo(");
GenPrintLabel(IdentTable + val);
printf2(")($1)");
break;
case MipsOpLabel: GenPrintLabel(IdentTable + val); break;
case MipsOpNumLabel: GenPrintNumLabel(val); break;
default:
//error("WTF!\n");
errorInternal(100);
break;
}
}
}
STATIC
void GenPrintOperandSeparator(void)
{
printf2(", ");
}
STATIC
void GenPrintNewLine(void)
{
puts2("");
}
STATIC
void GenPrintInstr1Operand(int instr, int instrval, int operand, int operandval)
{
GenPrintInstr(instr, instrval);
GenPrintOperand(operand, operandval);
GenPrintNewLine();
#ifdef REORDER_WORKAROUND
if (instr == MipsInstrJ || instr == MipsInstrJAL)
GenNop();
#endif
}
STATIC
void GenPrintInstr2Operands(int instr, int instrval, int operand1, int operand1val, int operand2, int operand2val)
{
if (operand2 == MipsOpConst && operand2val == 0 &&
(instr == MipsInstrAddU || instr == MipsInstrSubU))
return;
GenPrintInstr(instr, instrval);
GenPrintOperand(operand1, operand1val);
GenPrintOperandSeparator();
GenPrintOperand(operand2, operand2val);
GenPrintNewLine();
#ifdef REORDER_WORKAROUND
if (instr == MipsInstrBLTZ || instr == MipsInstrBGEZ ||
instr == MipsInstrBGTZ || instr == MipsInstrBLEZ)
GenNop();
#endif
}
STATIC
void GenPrintInstr3Operands(int instr, int instrval,
int operand1, int operand1val,
int operand2, int operand2val,
int operand3, int operand3val)
{
if (operand3 == MipsOpConst && operand3val == 0 &&
(instr == MipsInstrAddU || instr == MipsInstrSubU) &&
operand1 == operand2)
return;
GenPrintInstr(instr, instrval);
GenPrintOperand(operand1, operand1val);
GenPrintOperandSeparator();
GenPrintOperand(operand2, operand2val);
GenPrintOperandSeparator();
GenPrintOperand(operand3, operand3val);
GenPrintNewLine();
#ifdef REORDER_WORKAROUND
if (instr == MipsInstrBEQ || instr == MipsInstrBNE)
GenNop();
#endif
}
STATIC
void GenExtendRegIfNeeded(int reg, int opSz)
{
if (opSz == -1)
{
#ifdef DONT_USE_SEH
GenPrintInstr3Operands(MipsInstrSLL, 0,
reg, 0,
reg, 0,
MipsOpConst, 24);
GenPrintInstr3Operands(MipsInstrSRA, 0,
reg, 0,
reg, 0,
MipsOpConst, 24);
#else
GenPrintInstr2Operands(MipsInstrSeb, 0,
reg, 0,
reg, 0);
#endif
}
else if (opSz == 1)
{
GenPrintInstr3Operands(MipsInstrAnd, 0,
reg, 0,
reg, 0,
MipsOpConst, 0xFF);
}
else if (opSz == -2)
{
#ifdef DONT_USE_SEH
GenPrintInstr3Operands(MipsInstrSLL, 0,
reg, 0,
reg, 0,
MipsOpConst, 16);
GenPrintInstr3Operands(MipsInstrSRA, 0,
reg, 0,
reg, 0,
MipsOpConst, 16);
#else
GenPrintInstr2Operands(MipsInstrSeh, 0,
reg, 0,
reg, 0);
#endif
}
else if (opSz == 2)
{
GenPrintInstr3Operands(MipsInstrAnd, 0,
reg, 0,
reg, 0,
MipsOpConst, 0xFFFF);
}
}
STATIC
void GenJumpUncond(int label)
{
GenPrintInstr1Operand(MipsInstrJ, 0,
MipsOpNumLabel, label);
}
extern int GenWreg; // GenWreg is defined below
STATIC
void GenJumpIfEqual(int val, int label)
{
GenPrintInstr2Operands(MipsInstrLI, 0,
TEMP_REG_B, 0,
MipsOpConst, val);
GenPrintInstr3Operands(MipsInstrBEQ, 0,
GenWreg, 0,
TEMP_REG_B, 0,
MipsOpNumLabel, label);
}
STATIC
void GenJumpIfZero(int label)
{
#ifndef NO_ANNOTATIONS
printf2(" # JumpIfZero\n");
#endif
GenPrintInstr3Operands(MipsInstrBEQ, 0,
GenWreg, 0,
MipsOpRegZero, 0,
MipsOpNumLabel, label);
}
STATIC
void GenJumpIfNotZero(int label)
{
#ifndef NO_ANNOTATIONS
printf2(" # JumpIfNotZero\n");
#endif
GenPrintInstr3Operands(MipsInstrBNE, 0,
GenWreg, 0,
MipsOpRegZero, 0,
MipsOpNumLabel, label);
}
fpos_t GenPrologPos;
int GenLeaf;
STATIC
void GenWriteFrameSize(void)
{
unsigned size = 8/*RA + FP*/ - CurFxnMinLocalOfs;
printf2("\tsubu\t$29, $29, %10u\n", size); // 10 chars are enough for 32-bit unsigned ints
printf2("\tsw\t$30, %10u($29)\n", size - 8);
printf2("\taddu\t$30, $29, %10u\n", size - 8);
printf2("\t%csw\t$31, 4($30)\n", GenLeaf ? '#' : ' ');
}
STATIC
void GenUpdateFrameSize(void)
{
fpos_t pos;
fgetpos(OutFile, &pos);
fsetpos(OutFile, &GenPrologPos);
GenWriteFrameSize();
fsetpos(OutFile, &pos);
}
STATIC
void GenFxnProlog(void)
{
if (CurFxnParamCntMin && CurFxnParamCntMax)
{
int i, cnt = CurFxnParamCntMax;
if (cnt > 4)
cnt = 4;
for (i = 0; i < cnt; i++)
GenPrintInstr2Operands(MipsInstrSW, 0,
MipsOpRegA0 + i, 0,
MipsOpIndRegSp, 4 * i);
}
GenLeaf = 1; // will be reset to 0 if a call is generated
fgetpos(OutFile, &GenPrologPos);
GenWriteFrameSize();
}
STATIC
void GenGrowStack(int size)
{
if (!size)
return;
GenPrintInstr3Operands(MipsInstrSubU, 0,
MipsOpRegSp, 0,
MipsOpRegSp, 0,
MipsOpConst, size);
}
STATIC
void GenFxnEpilog(void)
{
GenUpdateFrameSize();
if (!GenLeaf)
GenPrintInstr2Operands(MipsInstrLW, 0,
MipsOpRegRa, 0,
MipsOpIndRegFp, 4);
GenPrintInstr2Operands(MipsInstrLW, 0,
MipsOpRegFp, 0,
MipsOpIndRegFp, 0);
GenPrintInstr3Operands(MipsInstrAddU, 0,
MipsOpRegSp, 0,
MipsOpRegSp, 0,
MipsOpConst, 8/*RA + FP*/ - CurFxnMinLocalOfs);
GenPrintInstr1Operand(MipsInstrJ, 0,
MipsOpRegRa, 0);
}
STATIC
int GenMaxLocalsSize(void)
{
return 0x7FFFFFFF;
}
STATIC
int GenGetBinaryOperatorInstr(int tok)
{
switch (tok)
{
case tokPostAdd:
case tokAssignAdd:
case '+':
return MipsInstrAddU;
case tokPostSub:
case tokAssignSub:
case '-':
return MipsInstrSubU;
case '&':
case tokAssignAnd:
return MipsInstrAnd;
case '^':
case tokAssignXor:
return MipsInstrXor;
case '|':
case tokAssignOr:
return MipsInstrOr;
case '<':
case '>':
case tokLEQ:
case tokGEQ:
case tokEQ:
case tokNEQ:
case tokULess:
case tokUGreater:
case tokULEQ:
case tokUGEQ:
return MipsInstrNop;
case '*':
case tokAssignMul:
return MipsInstrMul;
case '/':
case '%':
case tokAssignDiv:
case tokAssignMod:
return MipsInstrDiv;
case tokUDiv:
case tokUMod:
case tokAssignUDiv:
case tokAssignUMod:
return MipsInstrDivU;
case tokLShift:
case tokAssignLSh:
return MipsInstrSLL;
case tokRShift:
case tokAssignRSh:
return MipsInstrSRA;
case tokURShift:
case tokAssignURSh:
return MipsInstrSRL;
default:
//error("Error: Invalid operator\n");
errorInternal(101);
return 0;
}
}
STATIC
void GenPreIdentAccess(int label)
{
printf2("\t.set\tnoat\n\tlui\t$1, %%hi(");
GenPrintLabel(IdentTable + label);
puts2(")");
}
STATIC
void GenPostIdentAccess(void)
{
puts2("\t.set\tat");
}
STATIC
void GenReadIdent(int regDst, int opSz, int label)
{
int instr = MipsInstrLW;
GenPreIdentAccess(label);
if (opSz == -1)
{
instr = MipsInstrLB;
}
else if (opSz == 1)
{
instr = MipsInstrLBU;
}
else if (opSz == -2)
{
instr = MipsInstrLH;
}
else if (opSz == 2)
{
instr = MipsInstrLHU;
}
GenPrintInstr2Operands(instr, 0,
regDst, 0,
MipsOpLabelLo, label);
GenPostIdentAccess();
}
STATIC
void GenReadLocal(int regDst, int opSz, int ofs)
{
int instr = MipsInstrLW;
if (opSz == -1)
{
instr = MipsInstrLB;
}
else if (opSz == 1)
{
instr = MipsInstrLBU;
}
else if (opSz == -2)
{
instr = MipsInstrLH;
}
else if (opSz == 2)
{
instr = MipsInstrLHU;
}
GenPrintInstr2Operands(instr, 0,
regDst, 0,
MipsOpIndRegFp, ofs);
}
STATIC
void GenReadIndirect(int regDst, int regSrc, int opSz)
{
int instr = MipsInstrLW;
if (opSz == -1)
{
instr = MipsInstrLB;
}
else if (opSz == 1)
{
instr = MipsInstrLBU;
}
else if (opSz == -2)
{
instr = MipsInstrLH;
}
else if (opSz == 2)
{
instr = MipsInstrLHU;
}
GenPrintInstr2Operands(instr, 0,
regDst, 0,
regSrc + MipsOpIndRegZero, 0);
}
STATIC
void GenWriteIdent(int regSrc, int opSz, int label)
{
int instr = MipsInstrSW;
GenPreIdentAccess(label);
if (opSz == -1 || opSz == 1)
{
instr = MipsInstrSB;
}
else if (opSz == -2 || opSz == 2)
{
instr = MipsInstrSH;
}
GenPrintInstr2Operands(instr, 0,
regSrc, 0,
MipsOpLabelLo, label);
GenPostIdentAccess();
}
STATIC
void GenWriteLocal(int regSrc, int opSz, int ofs)
{
int instr = MipsInstrSW;
if (opSz == -1 || opSz == 1)
{
instr = MipsInstrSB;
}
else if (opSz == -2 || opSz == 2)
{
instr = MipsInstrSH;
}
GenPrintInstr2Operands(instr, 0,
regSrc, 0,
MipsOpIndRegFp, ofs);
}
STATIC
void GenWriteIndirect(int regDst, int regSrc, int opSz)
{
int instr = MipsInstrSW;
if (opSz == -1 || opSz == 1)
{
instr = MipsInstrSB;
}
else if (opSz == -2 || opSz == 2)
{
instr = MipsInstrSH;
}
GenPrintInstr2Operands(instr, 0,
regSrc, 0,
regDst + MipsOpIndRegZero, 0);
}
STATIC
void GenIncDecIdent(int regDst, int opSz, int label, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokInc)
instr = MipsInstrSubU;
GenReadIdent(regDst, opSz, label);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteIdent(regDst, opSz, label);
GenExtendRegIfNeeded(regDst, opSz);
}
STATIC
void GenIncDecLocal(int regDst, int opSz, int ofs, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokInc)
instr = MipsInstrSubU;
GenReadLocal(regDst, opSz, ofs);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteLocal(regDst, opSz, ofs);
GenExtendRegIfNeeded(regDst, opSz);
}
STATIC
void GenIncDecIndirect(int regDst, int regSrc, int opSz, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokInc)
instr = MipsInstrSubU;
GenReadIndirect(regDst, regSrc, opSz);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteIndirect(regSrc, regDst, opSz);
GenExtendRegIfNeeded(regDst, opSz);
}
STATIC
void GenPostIncDecIdent(int regDst, int opSz, int label, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokPostInc)
instr = MipsInstrSubU;
GenReadIdent(regDst, opSz, label);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteIdent(regDst, opSz, label);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, -1);
GenExtendRegIfNeeded(regDst, opSz);
}
STATIC
void GenPostIncDecLocal(int regDst, int opSz, int ofs, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokPostInc)
instr = MipsInstrSubU;
GenReadLocal(regDst, opSz, ofs);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteLocal(regDst, opSz, ofs);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, -1);
GenExtendRegIfNeeded(regDst, opSz);
}
STATIC
void GenPostIncDecIndirect(int regDst, int regSrc, int opSz, int tok)
{
int instr = MipsInstrAddU;
if (tok != tokPostInc)
instr = MipsInstrSubU;
GenReadIndirect(regDst, regSrc, opSz);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, 1);
GenWriteIndirect(regSrc, regDst, opSz);
GenPrintInstr3Operands(instr, 0,
regDst, 0,
regDst, 0,
MipsOpConst, -1);
GenExtendRegIfNeeded(regDst, opSz);
}
int CanUseTempRegs;
int TempsUsed;
int GenWreg = MipsOpRegV0; // current working register (V0 or Tn or An)
int GenLreg, GenRreg; // left operand register and right operand register after GenPopReg()
/*
General idea behind GenWreg, GenLreg, GenRreg:
- In expressions w/o function calls:
Subexpressions are evaluated in V0, T0, T1, ..., T<MAX_TEMP_REGS-1>. If those registers
aren't enough, the stack is used additionally.
The expression result ends up in V0, which is handy for returning from
functions.
In the process, GenWreg is the current working register and is one of: V0, T0, T1, ... .
All unary operators are evaluated in the current working register.
GenPushReg() and GenPopReg() advance GenWreg as needed when handling binary operators.
GenPopReg() sets GenWreg, GenLreg and GenRreg. GenLreg and GenRreg are the registers
where the left and right operands of a binary operator are.
When the exression runs out of the temporary registers, the stack is used. While it is being
used, GenWreg remains equal to the last temporary register, and GenPopReg() sets GenLreg = TEMP_REG_A.
Hence, after GenPopReg() the operands of the binary operator are always in registers and can be
directly manipulated with.
Following GenPopReg(), binary operator evaluation must take the left and right operands from
GenLreg and GenRreg and write the evaluated result into GenWreg. Care must be taken as GenWreg
will be the same as either GenLreg (when the popped operand comes from T0-T<MAX_TEMP_REGS-1>)
or GenRreg (when the popped operand comes from the stack in TEMP_REG_A).
- In expressions with function calls:
GenWreg is always V0 in subexpressions that aren't function parameters. These subexpressions
get automatically pushed onto the stack as necessary.
GenWreg is always V0 in expressions, where return values from function calls are used as parameters
into other called functions. IOW, this is the case when the function call depth is greater than 1.
Subexpressions in such expressions get automatically pushed onto the stack as necessary.
GenWreg is A0-A3 in subexpressions that are function parameters when the function call depth is 1.
Basically, while a function parameter is evaluated, it's evaluated in the register from where
the called function will take it. This avoids some of unnecessary register copies and stack
manipulations in the most simple and very common cases of function calls.
*/
STATIC
void GenWregInc(int inc)
{
if (inc > 0)
{
// Advance the current working register to the next available temporary register
if (GenWreg == MipsOpRegV0)
GenWreg = MipsOpRegT0;
else
GenWreg++;
}
else
{
// Return to the previous current working register
if (GenWreg == MipsOpRegT0)
GenWreg = MipsOpRegV0;
else
GenWreg--;
}
}
STATIC
void GenPushReg(void)
{
if (CanUseTempRegs && TempsUsed < MAX_TEMP_REGS)
{
GenWregInc(1);
TempsUsed++;
return;
}