-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMipsTranslator.cpp
1358 lines (1270 loc) · 44.9 KB
/
MipsTranslator.cpp
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
#include"MipsTranslator.h"
MipsTranslator::MipsTranslator(string name) {
currentFunction = -1;
out.open(name, ios_base::trunc | ios_base::out);
}
void MipsTranslator::generateProgramHeader() {
out << ".data:" << endl;
MidCode::table->dumpMipsCodeHeader(out);
out << ".text:" << endl;
out << "jal main"<<endl;
}
void MipsTranslator::translateFunction(FlowGraph& g) {
//所有s寄存器清空
for (int i = 0; i < GLOBALREG; i++) {
Sstatus[i] = REGFREE;
}
SregisterUser.clear();
//所有A寄存器清除
for (int i = 0; i < 4; i++) {
Astatus[i] = REGFREE;
Auser[i] = -1;
}
//设置当前状态参数
currentFunction = g.functionId;//函数编号
globalVariable = g.globalVariable;//所有跨基本块
tmpVariable = g.tmpVariable;//所有的临时变量
allVariable = g.allVariable;//所有变量
conflictMap.clear();//冲突图
varReg.clear();//重新把寄存器分配全部置-1避免发生分配0号寄存器的事情
for (int i : allVariable) {
varReg[i] = -1;
}
//添加冲突边
for (int i = 0; i < g.conflictEdges.size(); i++) {
if (g.conflictEdges[i][0] != g.conflictEdges[i][1]) {
conflictMap[g.conflictEdges[i][0]].insert(g.conflictEdges[i][1]);
conflictMap[g.conflictEdges[i][1]].insert(g.conflictEdges[i][0]);
}
}
//根据冲突边分配s寄存器
SregisterAlloc();
for (Block* b : g.graph) {
translateBlock(b);//逐块翻译
}
}
void MipsTranslator::translateBlock(Block* b) {
currentBlock = b;//设置当前块
//基本块中所有临时寄存器全部释放
for (int i = 0; i < TMPREG; i++) {
Tstatus[i] = REGFREE;
Tuser[i] = -1;
}
for (int i = 0; i < b->v.size(); i++) {
if (i+1<b->v.size()
&&(b->v[i].op==MIDEQL||b->v[i].op==MIDNEQ|| b->v[i].op == MIDGRE
||b->v[i].op == MIDGEQ|| b->v[i].op == MIDLSS
|| b->v[i].op == MIDLEQ)
&&(b->v[i+1].op==MIDBZ||b->v[i+1].op==MIDBNZ)) {
//针对跳转语句优化
MidCode c1 = b->v[i];
MidCode c2 = b->v[i + 1];
if (c2.operand1 == c1.target && !c2.isImmediate1//保证是同一个变量
&& c2.activeVariable.find(c1.target) == c2.activeVariable.end()
//保证这个条件变量以后再也没用过
) {
i++;
vector<MidCode> res = { c1,c2 };
translate(res,1);
}
else {
translate(b->v[i]);
}
}
else if (b->v[i].op != MIDPUSH) {
translate(b->v[i]);
}
else {
//所有的push语句集中处理
vector<MidCode>res;
while (b->v[i].op == MIDPUSH) {
res.push_back(b->v[i]);
i++;
}
i--;
translate(res,0);
}
}
//这里:暂时是把所有临时变量全部写回了,但是其实没这个必要,稍后可以加以改进
for (int i = 0; i < TMPREG; i++) {
if (Tstatus[i] == REGVAR) {
varReg[Tuser[i]] = -1;
}
Tstatus[i] = REGFREE;
Tuser[i] = -1;
}
}
void MipsTranslator::SregisterAlloc() {
if (globalVariable.size() == 0) {
//若没有跨基本块变量
return;
}
else if (globalVariable.size() == 1) {
//若是只有一个,直接分配16号寄存器
varReg[*(globalVariable.begin())] = 16;
Sstatus[0] = REGVAR;
SregisterUser[16].insert( *(globalVariable.begin()) );
return;
}
else {
vector<int>q;
set<int>var=globalVariable;
map<int, set<int>>m = conflictMap;
while (var.size() > 1) {
int remove = -1;
for (int i : var) {
if (m[i].size() < GLOBALREG) {
//选取一个连接边小于k的并删除
q.push_back(i);
for (int j : m[i]) {
m[j].erase(i);
}
remove = i;
}
}
if (remove != -1) {
var.erase(remove);
}
else {
//若没有找到满足条件的,暂时是从头找一个
//此处如何选择可进行优化
int chosen = *(var.begin());
//cout << "remove var No." << chosen << endl;
for (int j : m[chosen]) {
m[j].erase(chosen);
}
var.erase(chosen);
}
}
varReg[*(globalVariable.begin())] = 16;
Sstatus[0] = REGVAR;
SregisterUser[16].insert(*(globalVariable.begin()));
//剩余的那个直接分 16寄存器
for (int i : q) {
//对q中的每一个节点
for (int j = 0; j < GLOBALREG; j++) {
//检查每一个s寄存器
bool ok = true;
for (int k : SregisterUser[j + 16]) {
if (conflictMap[i].find(k) != conflictMap[i].end()) {
//检查使用该寄存器的变量中有没有和自己冲突的变量
ok = false;//找到了就肯定不行
break;
}
}
if (ok) {
varReg[i] = Sregister[j];
Sstatus[j] = REGVAR;
SregisterUser[j + 16].insert(i);
//cout << "assign reg $s" << j << "to var No." << i << endl;
break;
}
}
}
}
}
vector<int> MipsTranslator::TregisterAlloc(int var, int isImmediate
, vector<int>conflictVar, vector<int> conflictReg) {
return TregisterAlloc(var, isImmediate, conflictVar, conflictReg,nullptr);
}
/*
@param
var:待分配寄存器的变量编号/立即数值,若为-1则意味着分配一个临时寄存器
isImmediate:是否是立即数
conflictVar:与之有冲突的变量:不能把同一指令里使用的其他寄存器分出去
conflictReg:与之有冲突的寄存器编号:也不能把在同一指令里使用的立即数寄存器分出去
activeVariable:输入此时此句的活跃变量(未处理当前句,不过没有影响)
activeVariable为nullptr时是不按照这个活跃变量进行分析
@return
返回值:第一个数是返回的寄存器编号,第二个数是需要写回的变量,如果没有就是-1
在返回时寄存器将已被进行所有注册,只有写回操作需要完成(??为啥这么干来着我忘了)
*/
vector<int> MipsTranslator::TregisterAlloc(int var, int isImmediate
, vector<int>conflictVar, vector<int> conflictReg,set<int>*activeVariable) {
//如果是变量(不是立即数)且已经分配了寄存器,直接返回
if (!isImmediate&&var!=-1&&varReg[var] >0) {
return { varReg[var],-1 };
}
else{
//寻找寄存器,这里寻找寄存器的策略可以优化?
for (int i = 0; i < TMPREG; i++) {
if (Tstatus[i] == REGFREE) {
//如果找到了free状态的直接使用即可
if (!isImmediate && var == -1) {
Tstatus[i] = REGTMP;//改状态
Tuser[i] = -1;//改使用者
}
else if (isImmediate) {
Tstatus[i] = REGOCCUPY;//改状态
Tuser[i] = var;//改使用者
}
else {
Tstatus[i] = REGVAR;
Tuser[i] = var;
varReg[var] = Tregister[i];//登记
}
return { Tregister[i],-1 };
}
}
//此处是,根据活跃变量分析结果寻找一个有变量占用但占用者不活跃的变量
if (nullptr != activeVariable) {
for (int i = 0; i < TMPREG; i++) {
if (Tstatus[i] == REGVAR &&
find(conflictVar.begin(), conflictVar.end(), Tuser[i]) != conflictVar.end()) {
continue;//不分配相关变量的寄存器
}
else if ((Tstatus[i] == REGOCCUPY||Tstatus[i]==REGTMP)&&
find(conflictReg.begin(), conflictReg.end(), Tregister[i]) != conflictReg.end()) {
continue;//不分配相关变量占用的寄存器
}
if (Tstatus[i] == REGVAR &&
activeVariable->find(Tuser[i]) == activeVariable->end()) {
//这是不活跃的变量,不寻找全局变量,因为全局即使不活跃还是需要写回的
SymbolEntry* entry = MidCode::table->getSymbolById(Tuser[i]);
if (entry->scope == "") {
continue;
}
if (!isImmediate && var == -1) {
Tstatus[i] = REGTMP;//改状态
Tuser[i] = -1;//改使用者
}
else if (isImmediate ) {
Tstatus[i] = REGOCCUPY;//改状态
Tuser[i] =var;//改使用者(常数)
}
else {
Tstatus[i] = REGVAR;
Tuser[i] = var;
varReg[var] = Tregister[i];//登记
}
return { Tregister[i],-1 };
}
}
}
//此处可以考虑分配策略可选择进行优化
for (int i = 0; i < TMPREG; i++) {
if (Tstatus[i]==REGVAR&&
find(conflictVar.begin(), conflictVar.end(), Tuser[i]) != conflictVar.end()) {
continue;//不分配相关的寄存器
}
else if ((Tstatus[i] == REGOCCUPY || Tstatus[i] == REGTMP) &&
find(conflictReg.begin(), conflictReg.end(), Tregister[i]) != conflictReg.end()) {
continue;//不分配相关变量占用的寄存器
}
else {
//找到了一个T寄存器
if (Tstatus[i] == REGOCCUPY||Tstatus[i]==REGTMP) {
//若是occupy状态或是临时占用无需写回
if (!isImmediate && var == -1) {
Tstatus[i] = REGTMP;//改状态
Tuser[i] = -1;//改使用者
}
else if (isImmediate) {
Tstatus[i] = REGOCCUPY;//改状态
Tuser[i] = var;//改使用者(常数)
}
else {
Tstatus[i] = REGVAR;
Tuser[i] = var;
varReg[var] = Tregister[i];//登记
}
return { Tregister[i],-1 };
}
else {
//需要写回
int old = Tuser[i];
if (!isImmediate && var == -1) {
Tstatus[i] = REGTMP;//改状态
Tuser[i] = -1;//改使用者
}
else if (isImmediate) {
Tstatus[i] = REGOCCUPY;//改状态
Tuser[i] = var;//改使用者
}
else {
Tstatus[i] = REGVAR;
Tuser[i] = var;
varReg[var] = Tregister[i];//登记
}
varReg[old] = -1;
return{ Tregister[i],old };
}
}
}
cout << "bug at allocating the tmp register";
return { -1,-1 };
}
}
void MipsTranslator::setReport(map<int, vector<int>>_report) {
report = _report;
}
int MipsTranslator::loadOperand(int var, int isImmediate
, vector<int>conflictVar, vector<int> conflictReg, set<int>* activeVariable) {
if (var!=-1&&!isImmediate && varReg[var] >0) {
//是变量且已分配寄存器,直接返回已经分配的结果
return varReg[var];
}
else if (isImmediate) {
//是立即数
bool found = false;
for (int i = 0; i < TMPREG; i++) {
if (Tstatus[i] == REGOCCUPY && Tuser[i] == var) {
return Tregister[i];
}
}
vector<int>res = TregisterAlloc(var, isImmediate, conflictVar
, conflictReg, activeVariable);
if (res[1] != -1) { writeback(res[1], res[0]); }//写回
out << "li " << name[res[0]] << "," << var;
out << " # load immediate " << var;
out << endl;
return res[0];
}
else {
//申请了一个临时寄存器
if (var == -1) {
vector<int>res = TregisterAlloc(var, isImmediate, conflictVar
, conflictReg,activeVariable);
if (res[1] != -1) { writeback(res[1], res[0]); }
return res[0];
}
SymbolEntry* entry = MidCode::table->getSymbolById(var);
vector<int>res;
if (entry->isParameter) {
//如果待分配的变量是参数之一的话只会为他分配本应属于他的a寄存器,如果有的话
SymbolEntry* func = MidCode::table->getSymbolById(currentFunction);
int order = -1;
for (int i = 0; i < 4 && i < func->link->paraNum; i++) {
if (var == func->link->paraIds[i]) {
order = i;
Astatus[i] = REGVAR;
Auser[i] = var;
break;
}
}
if (order != -1) {
int bias = entry->addr;
out << "lw " << name[order+4] << "," << bias << "($sp)";
out << " #load variable " << MidCode::getOperandName(var, false);
out << endl;
return order+4;
}
}
//分配寄存器
res = TregisterAlloc(var, isImmediate, conflictVar, conflictReg, activeVariable);
if (res[1] != -1) { writeback( res[1],res[0] ); }
if (entry->scope != "") {
//是局部变量
if (entry->type == TYPEINT || entry->type == TYPECHAR || entry->type == TYPETMP
||entry->type==TYPEINTCONST||entry->type==TYPECHARCONST) {
//此处建立了机制防止未初始化的内存被加载进入寄存器,可以节省访存
if (!(/*var < 0 &&*/ globalVariable.find(var) == globalVariable.end()
&&loaded.find(var)==loaded.end())) {
int bias = entry->addr;
out << "lw " << name[res[0]] << "," << bias << "($sp)";
out << " #load variable" << MidCode::getOperandName(var, false);
out << endl;
//loaded.insert(var);
}
loaded.insert(var);
}
//在传入变量是数组时返回数组地址,不过这段代码现在应该永远不会被执行
else if (entry->type == TYPEINTARRAY || entry->type == TYPECHARARRAY) {
int bias = entry->addr;
out << "addiu " << name[res[0]] << ",$sp," << bias;
out << "# load address of " << MidCode::getOperandName(var, false);
out << endl;
}
}
else {
//是全局变量
if(entry->type == TYPEINT || entry->type == TYPECHAR
|| entry->type == TYPEINTCONST || entry->type == TYPECHARCONST) {
out << "lw " << name[res[0]] << "," << entry->name;
out << " #load global variable " << entry->name;
out << endl;
}
//在传入变量是数组时返回数组地址,不过这段代码现在应该永远不会被执行
else if (entry->type == TYPEINTARRAY || entry->type == TYPECHARARRAY) {
out << "la " << name[res[0]] << "," << entry->name;
out << " #load address of " << entry->name;
out << endl;
}
}
return res[0];
}
}
/*通用的写回函数*/
void MipsTranslator::writeback(int var,int reg) {
SymbolEntry* entry = MidCode::table->getSymbolById(var);
if ((entry->type == TYPEINT || entry->type == TYPECHAR||entry->type==TYPETMP)
&& entry->scope != "") {
//int char类型的局部变量
int bias = entry->addr;
out << "sw " << name[reg] << "," << bias << "($sp)";
out << "#write back local variable " << MidCode::getOperandName(var,false);
out << endl;
}
else if (entry->scope == "" && (entry->type == TYPEINT || entry->type == TYPECHAR )) {
out << "sw " << name[reg] << "," << entry->name;
out << "#write back global variable " << entry->name;
out << endl;
}
//其他类型不需要写回
}
void MipsTranslator::translate(MidCode c) {
if (c.labelNo != MIDNOLABEL) {
out << "label$" << -c.labelNo << ":" << endl;
}
switch (c.op) {
case MIDFUNC:
{
SymbolEntry* s = MidCode::table->getSymbolById(c.operand1);
out << s->name << ":" << endl;//函数标签
if (s->name != "main") {
//若不是main函数,保存s寄存器,返回地址,不需要下移sp因为call时候会移动的
out << "sw $ra," << report[s->id][0] - report[s->id][1] + 32 << "($sp)";
out << "#save the return value" << endl;
for (int i = 0; i < GLOBALREG; i++) {
if(Sstatus[i]==REGVAR)
out << "sw " << name[i + 16] << "," << report[s->id][0] - report[s->id][1] + i * 4 << "($sp)" << endl;
}
for (int i = 0; i < 4 && i < s->link->paraNum; i++) {
varReg[s->link->paraIds[i]] = Aregister[i];
Astatus[i] = REGVAR;
Auser[i] = s->link->paraIds[i];
}
}
else {
//若是main函数则应下移sp来为局部变量开辟空间
out << "addiu $sp,$sp," << -report[s->id][0] - report[s->id][1] << endl;
}
SubSymbolTable* tmp = MidCode::table->getSubSymbolTableByName(s->name);
//此处在语法分析实现了常量替换的情况下应该可以删掉
/*for (auto& i : tmp->symbolMap) {
if (i.second->type == TYPEINTCONST || i.second->type == TYPECHARCONST) {
//需要为常量进行赋值
out << "li $v1," << i.second->initValue << endl;
out << "sw $v1," << i.second->addr << "($sp)" << endl;
}
}
*/
break;
}
case MIDCALL:
{
SymbolEntry* s = MidCode::table->getSymbolById(c.operand1);
writeBackAfterBlock();//每一个基本块的结束都要进行基本块外的写回操作
for (int i =s->link->paraNum ; i < 4; i++) {
if (Astatus[i] == REGVAR) {
SymbolEntry* tmp = MidCode::table->getSymbolById(Auser[i]);
out << "sw " << name[Aregister[i]] << "," << tmp->addr << "($sp)" << endl;
}
}
out << "addiu $sp,$sp," << -(report[s->id][0] + 36) << endl;
out << "jal " << s->name<<endl;
for (int i = 0; i < 4; i++) {
if (Astatus[i] == REGVAR) {
SymbolEntry* tmp = MidCode::table->getSymbolById(Auser[i]);
out << "lw " << name[Aregister[i]] << "," << tmp->addr << "($sp)" << endl;
}
}
out << "#" << c << endl;
break;
}
case MIDRET:
{
SymbolEntry* func = MidCode::table->getSymbolById(currentFunction);
writeBackAfterBlock();
if (func->name != "main") {
//不是main函数,如果是main函数就没啥需要干的了,向v0寄存器写入返回值
if (c.operand1 != -1 && !c.isImmediate1) {
//返回值不是空且不是立即数
if (varReg[c.operand1] > 0) {
//已经保存在寄存器中
out << "move $v0," << name[varReg[c.operand1]]<<endl;
}
else {
//返回的肯定不能是数组名
SymbolEntry* s = MidCode::table->getSymbolById(c.operand1);
if (s->scope == "") {
out << "lw $v0," << s->name << endl;
}
else {
out << "lw $v0," << s->addr << "($sp)" << endl;
}
}
}
else if (c.isImmediate1) {
out << "li $v0," << c.operand1 << endl;
}
for (int i = 0; i < GLOBALREG; i++) {
if(Sstatus[i]==REGVAR)
out << "lw " << name[i + 16] << "," << report[currentFunction][0] -
report[currentFunction][1] + i * 4 << "($sp)" << endl;
}
out << "lw $ra," << report[currentFunction][0]
- report[currentFunction][1] + 32 << "($sp)" << endl;
out << "addiu $sp,$sp," << report[currentFunction][0] + 36 << endl;
out << "jr $ra" << endl;
}
out << "#" << c << endl;
break;
}
case MIDADD:
{
if (c.isImmediate2) {
//这一段是在第二操作数是立即数时换用对应指令
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {},&(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "addiu " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);//全局变量会无条件立即写回
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) {conflictVar.push_back(c.operand1);}
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "addu " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c<<endl;
specialVarwriteback(c.target, false);
break;
}
case MIDSUB:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "subiu " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "subu " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDMULT:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "mul " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "mul " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDDIV:
{
if (c.isImmediate2&&c.operand2==2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "sra " << name[target] << "," << name[operand1] << "," << 1;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "div " << name[operand1] << "," << name[operand2]<<endl;
out << "mflo " << name[target];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDLSS:
{
if (c.isImmediate2&&c.operand2>=0&&c.operand2<=32767) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "slti " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "slt " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDLEQ:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "sle " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "sle " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDGRE:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "sgt " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "sgt " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDGEQ:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "sge " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "sge " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDEQL:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "seq " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "seq " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDNEQ:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "sne " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "sne " << name[target] << "," << name[operand1] << "," << name[operand2];
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDREM:
{
if (c.isImmediate2) {
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "rem " << name[target] << "," << name[operand1] << "," << c.operand2;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target,operand1 }, &(c.activeVariable));
out << "div " << name[operand1] << "," << name[operand2]<<endl;
out << "mfhi " << name[target]<<endl;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDNEGATE:
{
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
int operand1 = loadOperand(c.operand1, c.isImmediate1, conflictVar,
{ target }, &(c.activeVariable));
out << "subu "<<name[target]<<",$0," << name[operand1] ;
out << "#" << c << endl;
specialVarwriteback(c.target, false);
break;
}
case MIDARRAYGET:
{
vector<int>conflictVar;
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int target = loadOperand(c.target, false, conflictVar, {}, &(c.activeVariable));
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
int operand2 = loadOperand(c.operand2, c.isImmediate2, conflictVar,
{ target }, &(c.activeVariable));
SymbolEntry* s = MidCode::table->getSymbolById(c.operand1);
if (s->scope == "") {
conflictVar.clear();
conflictVar.push_back(c.target);
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }
int tmpreg = loadOperand(-1, false, conflictVar, { target,operand2 }, &(c.activeVariable));