-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGeneration.cpp
913 lines (815 loc) · 28.4 KB
/
CodeGeneration.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
//
// Created by tmpark on 3/4/16.
//
#include "CodeGeneration.h"
#include "Helper.h"
#include <bitset>
CodeGeneration :: CodeGeneration(std::unordered_map<std::string,vector<shared_ptr<BasicBlock>>> functionList){
this->functionList = functionList;
loc = 0;
numOfGlobalVar = 0;
}
void CodeGeneration::doCodeGen()
{
Parser *parser = Parser :: instance();
//Extract main block
vector<shared_ptr<BasicBlock>> blockList = functionList.at(GLOBAL_SCOPE_NAME);
//Prolog for main
//get NumOfGlobalVar;
shared_ptr<BasicBlock> firstBlock = blockList.at(0);
startLocOfBlock.insert({firstBlock->getBlockNum(),loc});
SymTable globalSymTable = parser->getSymTable(GLOBAL_SCOPE_NAME);
numOfGlobalVar = globalSymTable.getNumOfVar();
int globalVarSize = globalSymTable.getLocalVarSize();
VIRTUAL_IN_STACK = LOCAL_IN_STACK - globalVarSize;
int numOfVRegs = parser->getNumOfVirtualRegs(GLOBAL_SCOPE_NAME);
assembleAndPutCode(OP_ADD,REG_SP,0,REG_GP);//Initialize SP as GP
assembleAndPutCode(OP_ADD,REG_FP,0,REG_GP);//Initialize FP as GP
//Move Stack pointer to reserve slots for face return, global variable, and, virtual regs
assembleAndPutCode(OP_ADDI,REG_SP,REG_SP,-4*(1 + globalVarSize + numOfVRegs));
genCodeForBlock(GLOBAL_SCOPE_NAME,firstBlock);
for(auto function : functionList)
{
if(function.first == GLOBAL_SCOPE_NAME)
continue;
string functionName = function.first;
blockList = function.second;
firstBlock = blockList.at(0);
startLocOfBlock.insert({firstBlock->getBlockNum(),loc});
//Fuction Prolog
//push previous fp
int R_a = REG_FP;
int R_b = REG_SP;
int R_c = -4;
assembleAndPutCode(OP_PSH, R_a, R_b, R_c);
//FP value become SP
R_c = 0;//Register 0
assembleAndPutCode(OP_ADD, R_a, R_b, R_c);
//Push return address
R_a = REG_RET;
int valC = -4;
assembleAndPutCode(OP_PSH, R_a, R_b, valC);
//Move SP to the amount of the size of all variable;
SymTable symTable = parser->getSymTable(functionName);
int localVarSize = symTable.getLocalVarSize();
VIRTUAL_IN_STACK = LOCAL_IN_STACK - localVarSize;
if(localVarSize != 0)
assembleAndPutCode(OP_ADDI,REG_SP,REG_SP,-4*(localVarSize));//Move Stack pointer to reserve slots for variable
//Move SP to the amount of virtual register
numOfVRegs = parser->getNumOfVirtualRegs(functionName);
if(numOfVRegs != 0)
assembleAndPutCode(OP_ADDI,REG_SP,REG_SP,-4*numOfVRegs);//Move Stack pointer to reserve slots for virtual register
//Register that will be used is stored and initialize that as 0
vector<int> regUsedList = symTable.regUsedList;
for(auto regUsed : regUsedList)
{
R_a = regUsed;
R_b = REG_SP;
valC = -4;
assembleAndPutCode(OP_PSH, R_a, R_b, valC);
assembleAndPutCode(OP_ADD, R_a, REG_0, REG_0);
}
for(auto symIter : symTable.varSymbolList)
{
shared_ptr<Symbol> sym = symIter.second;
SymType symType = sym->getSymType();
if(symType == sym_param)
{
int loc = sym->getBaseAddr()*4;
int paramIndex = sym->getParamIndex();
int allocatedReg = sym->getRegNo(functionName);
if(allocatedReg != -1)
{
//regTable[allocatedReg] = true;
R_a = allocatedReg;
if(paramIndex < 3)
{
R_b = REG_PARAM + paramIndex;
R_c = 0;
assembleAndPutCode(OP_ADD,R_a,R_b,R_c);
}
else
{
R_b = REG_FP;
int cVal = loc;
assembleAndPutCode(OP_LDW,R_a,R_b,cVal);
}
}
}
}
//For global variable register allocation that will be used
for(auto symIter : globalSymTable.varSymbolList)
{
shared_ptr<Symbol> sym = symIter.second;
if(sym->getSymType() == sym_var)
{
int loc = sym->getBaseAddr()*4;
int allocatedReg = sym->getRegNo(functionName);
if(allocatedReg != -1)
{
//regTable[allocatedReg] = true;
R_a = allocatedReg;
R_b = REG_GP;
int cVal = loc;
assembleAndPutCode(OP_LDW,R_a,R_b,cVal);
}
}
}
//Real function Body
genCodeForBlock(functionName,firstBlock);
endLocOfFunc.insert({functionName,loc});
//Function Epilog
//if there is global variable defined, store it so that main also can get the changed value
for(auto globalVarIter : symTable.definedGlobalVal)
{
string globalVarName = globalVarIter.first;
DefinedInfo defInfo = globalVarIter.second;
Kind defKind = defInfo.getKind();
shared_ptr<Symbol> globalVarSym = globalSymTable.varSymbolList.at(globalVarName);
int loc = globalVarSym->getBaseAddr()*4;
if(defKind == constKind)
{
int cVal = defInfo.getConst();
assembleAndPutCode(OP_ADDI,REG_PROXY,0,cVal);
R_a = REG_PROXY;
}
else if(defKind == varKind)
{
shared_ptr<Symbol> definedVarSym = defInfo.getVarSym();
R_a = definedVarSym->getRegNo(functionName);
}
else if(defKind == instKind)
{
shared_ptr<IRFormat> definedInst = defInfo.getInst();
R_a = definedInst->getRegNo();
}
R_b = REG_GP;
int cVal = loc;
assembleAndPutCode(OP_STW,R_a,R_b,cVal);
}
//Return back registers' for caller
while(!regUsedList.empty())
{
R_a = regUsedList.back();
R_b = REG_SP;
R_c = 4;
assembleAndPutCode(OP_POP,R_a,R_b,R_c);
regUsedList.pop_back();
}
if(numOfVRegs != 0)
assembleAndPutCode(OP_ADDI,REG_SP,REG_SP,4*numOfVRegs);//Pop the virtual registers
if(localVarSize != 0)
assembleAndPutCode(OP_ADDI,REG_SP,REG_SP,4*(localVarSize));//Pop the local variable
R_a = REG_RET;
R_b = REG_SP;
valC = 4;
//get the return address back
assembleAndPutCode(OP_POP, R_a, R_b, valC);
//get the previous FP back
R_a = REG_FP;
assembleAndPutCode(OP_POP, R_a, R_b, valC);
//Remove Parameter
R_a = REG_SP;
R_b = REG_SP;
valC = symTable.getNumOfParam()*4;
if(valC !=0)
assembleAndPutCode(OP_ADDI, R_a, R_b, valC);
//Return!
assembleAndPutCode(OP_RET,REG_RET);
}
//Fixup
for(auto fixInfo : locationTobeFixed)
{
int fixLoc = fixInfo.first;
Result operandToFix = fixInfo.second;
int blockTobeFixed = operandToFix.getBlockNo();
int newLoc;
if(operandToFix.getJumpLoc() > 0)
newLoc = jumpLocOfBlock.at(blockTobeFixed);
else
newLoc = startLocOfBlock.at(blockTobeFixed);
buf[fixLoc] = buf[fixLoc] & 0xFFFF0000;
buf[fixLoc] = buf[fixLoc] | newLoc-fixLoc & 0x0000FFFF;
}
for(auto fixInfo : returnTobeFixed)
{
int fixLoc = fixInfo.first;
string functionTobeFixed = fixInfo.second;
int newLoc = endLocOfFunc.at(functionTobeFixed);
buf[fixLoc] = buf[fixLoc] & 0xFFFF0000;
buf[fixLoc] = buf[fixLoc] | newLoc-fixLoc & 0x0000FFFF;
}
/*
for(int i = 0 ; i < loc ; i++)
{
cout << bitset<32>(buf.at(i)) << endl;
}*/
}
void CodeGeneration::genCodeForBlock(string functionName, shared_ptr<BasicBlock> currentBlock)
{
startLocOfBlock.insert({currentBlock->getBlockNum(),loc});
//Generate Each code
for(auto code : currentBlock->edgeCodes)
{
insertCode(functionName,code);
}
if(!currentBlock->edgeCodes.empty())
jumpLocOfBlock.insert({currentBlock->getBlockNum(),loc});
for(auto code : currentBlock->irCodes)
{
insertCode(functionName,code);
}
//NextBlock
for(auto childBlock : currentBlock->DTForwardEdges)
genCodeForBlock(functionName,childBlock);
}
void CodeGeneration::insertCode(string functionName, shared_ptr<IRFormat> code)
{
if(code->isElimiated())
return;
IROP irOp = code->getIROP();
size_t numOfIROperand = code->operands.size();
//end, read, writeNL case
if(numOfIROperand == 0)
{
if(irOp == IR_end)
{
int opCode = OP_RET;
assembleAndPutCode(opCode,0);
}
else if(irOp == IR_read)
{
int opCode = OP_RDD;
int instRegNum = code->getRegNo();
assembleAndPutCode(opCode,instRegNum);
}
else if(irOp == IR_writeNL)
{
int opCode = OP_WRL;
assembleAndPutCode(opCode);
}
return;
}
bool isFirstOperandConst = false;
Result firstOperand = code->operands.at(0);
if(firstOperand.getKind() == constKind)
isFirstOperandConst = true;
if(numOfIROperand == 1)
{
if(irOp == IR_load)
{
bool arrayLoad = firstOperand.isArrayInst();
string stringName = firstOperand.getVariableName();
int R_a = code->getRegNo();
shared_ptr<IRFormat> actualCode = firstOperand.getInst();
firstOperand = actualCode->operands.at(0);
int R_b = firstOperand.getReg(functionName);
Result secondOperand = actualCode->operands.at(1);
if(arrayLoad) {
Parser *parser = Parser::instance();
SymTable symTable = parser->getSymTable(functionName);
auto symIter = symTable.varSymbolList.find(stringName);
int baseReg;
int maxMemSize;
if (symIter == symTable.varSymbolList.end())//which means global
{
baseReg = REG_GP;
symTable = parser->getSymTable(GLOBAL_SCOPE_NAME);
shared_ptr<Symbol> sym = symTable.varSymbolList.at(stringName);
maxMemSize = sym->getVarSize() * 4;
if(secondOperand.getKind() == constKind)
{
assembleAndPutCode(OP_ADDI, REG_TEMP, REG_0, secondOperand.getConst());
assembleAndPutCode(OP_CHKI, REG_TEMP, maxMemSize);
}
else
assembleAndPutCode(OP_CHKI, secondOperand.getReg(functionName), maxMemSize);
}
else {
shared_ptr<Symbol> sym = symIter->second;
baseReg = REG_FP;
maxMemSize = sym->getVarSize() * 4;
if(secondOperand.getKind() == constKind)
{
assembleAndPutCode(OP_ADDI, REG_TEMP, REG_0, secondOperand.getConst());
assembleAndPutCode(OP_CHKI, REG_TEMP, maxMemSize);
}
else
assembleAndPutCode(OP_CHKI, secondOperand.getReg(functionName), maxMemSize);
}
}
if(secondOperand.getKind() == constKind)
assembleAndPutCode(OP_LDW,R_a,R_b,secondOperand.getConst());
else
assembleAndPutCode(OP_LDX,R_a,R_b,secondOperand.getReg(functionName));
}
else if(irOp == IR_bra) //should distinguish whether it is Normal jump or func call
{
//Function call
if(firstOperand.isDiffFuncLoc())
{
int firstOperandVal = REG_RET;
int opCode = OP_BSR;
locationTobeFixed.insert({loc,firstOperand});
assembleAndPutCode(opCode,firstOperandVal);
}
else if(firstOperand.getKind() == instKind){//not be used
}
else if(firstOperand.getKind() == regKind && firstOperand.isReturnAddr()) //return
{
int firstOperandVal = firstOperand.getReg(functionName);
returnTobeFixed.insert({loc,functionName});
assembleAndPutCode(OP_BSR,firstOperandVal);
}
else//General unconditional branch
{
int firstOperandVal = firstOperand.getBlockNo();
int opCode = OP_BEQ;
locationTobeFixed.insert({loc,firstOperand});
assembleAndPutCode(opCode,0,firstOperandVal);
}
}
else if(irOp == IR_write)
{
int outputReg;
if(isFirstOperandConst)
{
assembleAndPutCode(OP_ADDI,REG_TEMP,0,firstOperand.getConst());
outputReg = REG_TEMP;
}
else
outputReg = firstOperand.getReg(functionName);
assembleAndPutCode(OP_WRD,outputReg);
}
return;
}
bool isSecondOperandConst = false;
Result secondOperand = code->operands.at(1);
if(secondOperand.getKind() == constKind)
isSecondOperandConst = true;
if(numOfIROperand > 1)
{
//kinds of adds
if(irOp == IR_add || irOp == IR_sub || irOp == IR_mul || irOp == IR_div || irOp == IR_cmp)
{
int destReg = code->getRegNo();
int firstOperandReg;
int secondOperandVal;
OPFORMAT opFormat;
if(isFirstOperandConst) {
assembleAndPutCode(OP_ADDI, REG_TEMP, 0, firstOperand.getConst());
firstOperandReg = REG_TEMP;
}
else
firstOperandReg = firstOperand.getReg(functionName);
if(isSecondOperandConst) {
opFormat = OP_F1;
secondOperandVal = secondOperand.getConst();
}
else {
opFormat = OP_F2;
secondOperandVal = secondOperand.getReg(functionName);
}
int opCode = irToOp(irOp,opFormat);
assembleAndPutCode(opCode,destReg,firstOperandReg,secondOperandVal);
}
else if(irOp == IR_store)
{
int R_a;
if(isFirstOperandConst) {
assembleAndPutCode(OP_ADDI, REG_TEMP, 0, firstOperand.getConst());
R_a = REG_TEMP;
}
else
R_a = firstOperand.getReg(functionName);
bool arrayStore = secondOperand.isArrayInst();
string stringName = secondOperand.getVariableName();
shared_ptr<IRFormat> actualCode = secondOperand.getInst();
firstOperand = actualCode->operands.at(0);
secondOperand = actualCode->operands.at(1);
if(arrayStore) {
Parser *parser = Parser::instance();
SymTable symTable = parser->getSymTable(functionName);
auto symIter = symTable.varSymbolList.find(stringName);
int baseReg;
int maxMemSize;
if (symIter == symTable.varSymbolList.end())//which means global
{
baseReg = REG_GP;
symTable = parser->getSymTable(GLOBAL_SCOPE_NAME);
shared_ptr<Symbol> sym = symTable.varSymbolList.at(stringName);
maxMemSize = sym->getVarSize() * 4;
if(secondOperand.getKind() == constKind)
{
assembleAndPutCode(OP_ADDI, REG_TEMP, REG_0, secondOperand.getConst());
assembleAndPutCode(OP_CHKI, REG_TEMP, maxMemSize);
}
else
assembleAndPutCode(OP_CHKI, secondOperand.getReg(functionName), maxMemSize);
}
else {
shared_ptr<Symbol> sym = symIter->second;
baseReg = REG_FP;
maxMemSize = sym->getVarSize() * 4;
if(secondOperand.getKind() == constKind)
{
assembleAndPutCode(OP_ADDI, REG_TEMP, REG_0, secondOperand.getConst());
assembleAndPutCode(OP_CHKI, REG_TEMP, maxMemSize);
}
else
assembleAndPutCode(OP_CHKI, secondOperand.getReg(functionName), maxMemSize);
}
}
int R_b = firstOperand.getReg(functionName);
if(secondOperand.getKind() == constKind)
assembleAndPutCode(OP_STW,R_a,R_b,secondOperand.getConst());
else
assembleAndPutCode(OP_STX,R_a,R_b,secondOperand.getReg(functionName));
}
else if(irOp == IR_move)
{
int firstOperandReg;
int secondOperandReg = secondOperand.getReg(functionName);
int destReg = secondOperandReg;
if(isFirstOperandConst) {
assembleAndPutCode(OP_ADDI, REG_TEMP, 0, firstOperand.getConst());
firstOperandReg = REG_TEMP;
}
else
firstOperandReg = firstOperand.getReg(functionName);
int opCode = OP_ADD;
assembleAndPutCode(opCode,destReg,0,firstOperandReg);
}
else if(irOp == IR_bne || irOp == IR_beq || irOp == IR_ble || irOp == IR_blt || irOp == IR_bge|| irOp == IR_bgt)
{
int firstOperandReg = firstOperand.getReg(functionName);
int secondOperandVal = secondOperand.getBlockNo();
OPFORMAT opFormat = OP_F1;
int opCode = irToOp(irOp,opFormat);
locationTobeFixed.insert({loc,secondOperand});
assembleAndPutCode(opCode,firstOperandReg,secondOperandVal);
}
else if(irOp == IR_miu)
{
int secondOperandReg = secondOperand.getReg(functionName);
if(secondOperandReg == REG_SP)//should push to the stack
{
int R_a;
int R_b = secondOperandReg;
if(isFirstOperandConst) {
int val_c = firstOperand.getConst();
assembleAndPutCode(OP_ADDI, REG_TEMP, 0, val_c);
R_a = REG_TEMP;
}
else
R_a = firstOperand.getReg(functionName);
assembleAndPutCode(OP_PSH, R_a, R_b, -4);
}
else//go to parameter register or return val reg
{
int R_a = secondOperandReg;
int R_b = 0;
int R_c;
if(isFirstOperandConst) {
int val_c = firstOperand.getConst();
assembleAndPutCode(OP_ADDI, REG_TEMP, R_b, val_c);
R_c = REG_TEMP;
}
else
R_c = firstOperand.getReg(functionName);
assembleAndPutCode(OP_ADD, R_a, R_b, R_c);
//Even though parameters are inserted in reg, reserve the space
if(secondOperandReg < REG_PARAM + NUM_OF_PARAM_REGS && secondOperandReg >= REG_PARAM)
assembleAndPutCode(OP_ADDI, REG_SP,REG_SP,-4);
}
}
return;
}
}
void CodeGeneration::assembleAndPutCode(int op)
{
OpCode opCode = (OpCode)op;
if(opCode != OP_WRL)
cerr << "There is only one opcode for no operand : WRL" << endl;
PutF1(op,0,0,0);
}
void CodeGeneration::storeForVirtualReg(int virReg, int proxyIndex)
{
int vRegIndex = virReg - REG_VIRTUAL;
int R_a = REG_PROXY + proxyIndex;
int R_b = REG_FP;
int valC = (VIRTUAL_IN_STACK - vRegIndex-1)*4;
int valC_1 = valC;
int valC_2 = valC;
if(valC > pow(2,15) || valC < -pow(2,15)) //+ overflow
{
int rest = valC & 1;
valC_1 = valC >> 1;
valC_2 = valC_1;
valC_2 = valC_2 + rest;
PutF1(OP_ADDI,REG_TEMP + 1,REG_0,valC_1);
PutF1(OP_ADDI,REG_TEMP + 1,REG_TEMP + 1,valC_2);
PutF1(OP_STX,R_a,R_b,REG_TEMP + 1);
}//make half and half(if odd number arg3_2 is 1 more)
else
PutF1(OP_STW,R_a,R_b,valC);
}
void CodeGeneration::loadForVirtualReg(int virReg, int proxyIndex)
{
int vRegIndex = virReg - REG_VIRTUAL;
int R_a = REG_PROXY + proxyIndex;
int R_b = REG_FP;
int valC = (VIRTUAL_IN_STACK - vRegIndex -1)*4;
int valC_1 = valC;
int valC_2 = valC;
if(valC > pow(2,15) || valC < -pow(2,15)) //+ overflow
{
int rest = valC & 1;
valC_1 = valC >> 1;
valC_2 = valC_1;
valC_2 = valC_2 + rest;
PutF1(OP_ADDI,REG_TEMP + 1,REG_0,valC_1);
PutF1(OP_ADDI,REG_TEMP + 1,REG_TEMP + 1,valC_2);
PutF1(OP_LDX,R_a,R_b,REG_TEMP + 1);
}//make half and half(if odd number arg3_2 is 1 more)
else
PutF1(OP_LDW,R_a,R_b,valC);
}
void CodeGeneration::assembleAndPutCode(int op, int arg1)
{
OpCode opCode = (OpCode)op;
switch (opCode) {
// F1 Format
case OP_BSR:
PutF1(op,0,0,arg1);
break;
case OP_RDD:
if(arg1 > 31) //Virtual Register
{
PutF1(op,REG_PROXY,0,0);
storeForVirtualReg(arg1,0);
}
else
PutF1(op,arg1,0,0);
break;
case OP_WRD:
case OP_WRH:
if(arg1 > 31)
{
loadForVirtualReg(arg1,0);
arg1 = REG_PROXY;
}
PutF1(op,0,arg1,0);
break;
// F2 Format
case OP_RET:
PutF2(op,0,0,arg1);
break;
// F3 Format
case OP_JSR:
PutF3(op,arg1);
break;
default:
cerr << "This opcode is not for one argument" << endl;
}
}
void CodeGeneration::assembleAndPutCode(int op, int arg1, int arg2)
{
OpCode opCode = (OpCode)op;
switch (opCode) {
// F1 Format
case OP_CHKI:
if(arg1 > 31)
{
loadForVirtualReg(arg1, 0);
arg1 = REG_PROXY;
}
PutF1(op,arg1,0,arg2);
break;
case OP_BEQ:
case OP_BNE:
case OP_BLT:
case OP_BGE:
case OP_BLE:
case OP_BGT:
if(arg1 > 31)
{
PutF1(op,REG_PROXY,0,arg2);
storeForVirtualReg(arg1,0);
}
else
PutF1(op,arg1,0,arg2);
break;
// F2 Format
case OP_CHK:
if(arg1 > 31) {
loadForVirtualReg(arg1, 0);
arg1 = REG_PROXY;
}
if(arg2 > 31)
{
loadForVirtualReg(arg2, 1);
arg2 = REG_PROXY + 1;
}
PutF2(op,arg1,0,arg2);
break;
default:
cerr << "This opcode is not for two arguments" << endl;
}
}
void CodeGeneration::assembleAndPutCode(int op, int arg1, int arg2, int arg3)
{
int arg3_1 = arg3;
int arg3_2 = arg3;
if(arg3 > pow(2,15) || arg3 < -pow(2,15)) //+ overflow
{
int rest = arg3 & 1;
arg3_1 = arg3 >> 1;
arg3_2 = arg3_1;
arg3_2 = arg3_2 + rest;
}//make half and half(if odd number arg3_2 is 1 more)
OpCode opCode = (OpCode)op;
switch (opCode) {
// F1 Format
case OP_ADDI:
case OP_SUBI:
case OP_MULI:
case OP_DIVI:
case OP_MODI:
case OP_CMPI:
case OP_ORI:
case OP_ANDI:
case OP_BICI:
case OP_XORI:
case OP_LSHI:
case OP_ASHI:
case OP_LDW:
case OP_POP:
if(arg2 > 31)
{
loadForVirtualReg(arg2, 0);
arg2 = REG_PROXY;
}
if(arg1 > 31)
{
if(arg3 > pow(2,15) || arg3 < -pow(2,15)) //+ overflow
{
PutF1(OP_ADDI,REG_PROXY+1,REG_0,arg3_1);
PutF1(OP_ADDI,REG_PROXY+1,REG_PROXY+1,arg3_2);
PutF1(op-16,REG_PROXY,arg2,REG_PROXY+1);
}
else
PutF1(op,REG_PROXY,arg2,arg3);
storeForVirtualReg(arg1,0);
}
else
{
if(arg3 > pow(2,15) || arg3 < -pow(2,15)) //+ overflow
{
PutF1(OP_ADDI,REG_PROXY+1,REG_0,arg3_1);
PutF1(OP_ADDI,REG_PROXY+1,REG_PROXY+1,arg3_2);
PutF1(op-16,arg1,arg2,REG_PROXY+1);
}
else
PutF1(op,arg1,arg2,arg3);
}
break;
case OP_STW:
case OP_PSH:
if(arg1 > 31)
{
loadForVirtualReg(arg1, 0);
arg1 = REG_PROXY;
}
if(arg2 > 31)
{
loadForVirtualReg(arg2, 1);
arg2 = REG_PROXY + 1;
}
if(arg3 > pow(2,15) || arg3 < -pow(2,15)) //+ overflow
{
//No cases until now
}
PutF1(op,arg1,arg2,arg3);
break;
// F2 Format
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_MOD:
case OP_CMP:
case OP_OR:
case OP_AND:
case OP_BIC:
case OP_XOR:
case OP_LSH:
case OP_ASH:
case OP_LDX:
if(arg2 > 31)
{
loadForVirtualReg(arg2, 0);
arg2 = REG_PROXY;
}
if(arg3 > 31)
{
loadForVirtualReg(arg3, 1);
arg3 = REG_PROXY+1;
}
if(arg1 > 31)
{
PutF1(op,REG_PROXY,arg2,arg3);
storeForVirtualReg(arg1,0);
}
else
PutF2(op,arg1,arg2,arg3);
break;
case OP_STX:
if(arg1 > 31)
{
loadForVirtualReg(arg1, 0);
arg1 = REG_PROXY;
}
if(arg2 > 31)
{
loadForVirtualReg(arg2, 1);
arg2 = REG_PROXY + 1;
}
PutF1(op,arg1,arg2,arg3);
break;
default:
cerr << "This opcode is not for three arguments" << endl;
}
}
void CodeGeneration::PutF1(int op, int a, int b, int c) {
buf.at(loc) = op << 26 |
a << 21 |
b << 16 |
c & 0x0000FFFF;
loc++;
}
void CodeGeneration::PutF2(int op, int a, int b, int c) {
buf.at(loc) = op << 26 |
a << 21 |
b << 16 |
c & 0x0000001F;
loc++;
}
void CodeGeneration::PutF3(int op, int c) {
buf.at(loc) = op << 26 |
c & 0x03FFFFFF;
loc++;
}
void CodeGeneration::writeOutCode(const string &binaryFolder,const string &sourceFileName)
{
string formatName = ".out";
RC rc = -1;
string fileName = binaryFolder + sourceFileName + formatName;
destroyFile(fileName);
rc = createFile(fileName);
if (rc == -1)
return;
rc = openFile(fileName);
if (rc == -1) {
destroyFile(fileName);
return;
}
fileStream.write((char*)buf.data(),loc*sizeof(int32_t));
fileStream.close();
return;
}
RC CodeGeneration::createFile(const string &fileName)
{
destroyFile(fileName);
const char* fileName_char = fileName.c_str();
fstream file_to_create;
file_to_create.open(fileName_char, fstream::out | fstream:: binary); //Create a file (do not use in when creating a file)
if(file_to_create.is_open())
{
file_to_create.close();
return 1;
}
return -1;
}
RC CodeGeneration::openFile(const std::string &fileName) {
fileStream.open(fileName.c_str(), std::fstream::in | std::fstream::out | fstream::binary);
if(!fileStream.is_open())
{
return -1;
}
return 1;
}
RC CodeGeneration::destroyFile(const string &fileName)
{
const char* fileName_char = fileName.c_str();
RC success = remove(fileName_char); //delete file
if(success == 0)
{
return 0; //successful
}
return -1; //A file still exists
}
RC CodeGeneration::closeFile() {
fileStream.close();
return 1;
}