Skip to content

Commit ca297cc

Browse files
允许有print的函数内联
1 parent 918a861 commit ca297cc

File tree

5 files changed

+110
-36
lines changed

5 files changed

+110
-36
lines changed

GrammarAnalyzer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ void GrammarAnalyzer::scanSentence() {
13141314
}
13151315

13161316
void GrammarAnalyzer::printSentence() {
1317-
inlineable = false;
1317+
//inlineable = false;
13181318
if (lex.sym().type != PRINTFTK) {
13191319
f.handleFault(lex.lineNumber(), "缺少printf标识符");
13201320
throw 0;

MidCodeFrameWork.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ vector<MidCode>MidCodeFramework::inlinedSimpleFunction(string functionName
131131
case MIDGOTO:
132132
case MIDBNZ:
133133
case MIDBZ:
134-
case MIDPRINTCHAR:
135-
case MIDPRINTINT:
136-
case MIDPRINTSTRING:
137134
case MIDREADCHAR:
138135
case MIDREADINTEGER:
139136
cout << "bug @ inlinedFunction" << endl;
@@ -207,6 +204,8 @@ vector<MidCode>MidCodeFramework::inlinedSimpleFunction(string functionName
207204
}
208205
case MIDNEGATE:
209206
case MIDASSIGN:
207+
case MIDPRINTCHAR:
208+
case MIDPRINTINT:
210209
{
211210
SymbolEntry* targetEntry = MidCode::table->getSymbolById(c.target);
212211
if (c.target < 0 || targetEntry->scope != "") {
@@ -229,6 +228,8 @@ vector<MidCode>MidCodeFramework::inlinedSimpleFunction(string functionName
229228
res.push_back(newMidCode);
230229
break;
231230
}
231+
232+
case MIDPRINTSTRING:
232233
case MIDNOP:
233234
res.push_back(newMidCode);
234235
break;

MipsTranslator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ int MipsTranslator::loadOperand(int var, int isImmediate
370370
if (entry->type == TYPEINT || entry->type == TYPECHAR || entry->type == TYPETMP
371371
||entry->type==TYPEINTCONST||entry->type==TYPECHARCONST) {
372372
//此处建立了机制防止未初始化的内存被加载进入寄存器,可以节省访存
373-
if (!(var < 0 && globalVariable.find(var) == globalVariable.end()
373+
if (!(/*var < 0 &&*/ globalVariable.find(var) == globalVariable.end()
374374
&&loaded.find(var)==loaded.end())) {
375375
int bias = entry->addr;
376376
out << "lw " << name[res[0]] << "," << bias << "($sp)";
@@ -648,6 +648,7 @@ void MipsTranslator::translate(MidCode c) {
648648
}
649649
case MIDDIV:
650650
{
651+
651652
vector<int>conflictVar;
652653
if (!c.isImmediate1) { conflictVar.push_back(c.operand1); }
653654
if (!c.isImmediate2) { conflictVar.push_back(c.operand2); }

debug.txt

+44-19
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,24 @@ n=RET
235235
syscall-4 5 !=
236236
syscall-1 n
237237
syscall-11 10
238-
push 5
239-
push 10
240-
call swap
238+
#TMP98=5
239+
#TMP99=10
240+
syscall-4 x =
241+
syscall-1 #TMP98
242+
syscall-11 10
243+
syscall-4 y =
244+
syscall-1 #TMP99
245+
syscall-11 10
246+
#TMP97=#TMP98
247+
#TMP98=#TMP99
248+
#TMP99=#TMP97
249+
syscall-4 SWAP x =
250+
syscall-1 #TMP98
251+
syscall-11 10
252+
syscall-4 SWAP y =
253+
syscall-1 #TMP99
254+
syscall-11 10
255+
#TMP95=#TMP96
241256
call complete_flower_num
242257

243258
====Block0====
@@ -779,22 +794,23 @@ n=RET
779794
syscall-4 5 !=
780795
syscall-1 n
781796
syscall-11 10
782-
push 5
783-
push 10
784-
call swap
785-
prev:[43,]
786-
next:[45,]
787-
use:[]
788-
def:[n,]
789-
activeIn:[]
790-
activeOut:[]
791-
==============
792-
====Block45====
797+
syscall-4 x =
798+
syscall-1 5
799+
syscall-11 10
800+
syscall-4 y =
801+
syscall-1 10
802+
syscall-11 10
803+
syscall-4 SWAP x =
804+
syscall-1 10
805+
syscall-11 10
806+
syscall-4 SWAP y =
807+
syscall-1 5
808+
syscall-11 10
793809
call complete_flower_num
794-
prev:[44,]
810+
prev:[43,]
795811
next:[]
796812
use:[]
797-
def:[]
813+
def:[n,]
798814
activeIn:[]
799815
activeOut:[]
800816
==============
@@ -1004,9 +1020,18 @@ n=RET
10041020
syscall-4 5 !=
10051021
syscall-1 n
10061022
syscall-11 10
1007-
push 5
1008-
push 10
1009-
call swap
1023+
syscall-4 x =
1024+
syscall-1 5
1025+
syscall-11 10
1026+
syscall-4 y =
1027+
syscall-1 10
1028+
syscall-11 10
1029+
syscall-4 SWAP x =
1030+
syscall-1 10
1031+
syscall-11 10
1032+
syscall-4 SWAP y =
1033+
syscall-1 5
1034+
syscall-11 10
10101035
call complete_flower_num
10111036

10121037

mips.txt

+59-12
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ syscall #printchar
111111
move $a0,$3
112112
# syscall-11 10
113113

114-
lw $t0,0($sp) #load variabletemp
115114
move $t0,$a0#temp=x
116115

117116
move $a0,$a1#x=y
@@ -525,7 +524,6 @@ li $t1,10 # load immediate 10
525524
div $s3,$t1
526525
mflo $t0##TMP91 = h / 10
527526

528-
lw $t2,716($sp) #load variablex2
529527
mul $t2,$t0,10#x2 = #TMP91 * 10
530528

531529
bne $t2,$s3,label$23
@@ -584,7 +582,6 @@ addiu $sp,$sp,-56
584582
jal factorial
585583
#call factorial
586584

587-
lw $t0,0($sp) #load variablen
588585
move $t0,$v0#n=RET
589586

590587
la $a0,string$16
@@ -602,15 +599,65 @@ li $v0,11
602599
syscall #printchar
603600
# syscall-11 10
604601

605-
li $t1,5 # load immediate 5
606-
move $a0,$t1
607-
sw $t1,-8($sp)
608-
li $t2,10 # load immediate 10
609-
move $a1,$t2
610-
sw $t2,-4($sp)
611-
addiu $sp,$sp,-48
612-
jal swap
613-
#call swap
602+
la $a0,string$0
603+
li $v0,4
604+
syscall #printstring
605+
# syscall-4 x =
606+
607+
li $a0,5
608+
li $v0,1
609+
syscall #printint
610+
# syscall-1 5
611+
612+
li $a0,10
613+
li $v0,11
614+
syscall #printchar
615+
# syscall-11 10
616+
617+
la $a0,string$1
618+
li $v0,4
619+
syscall #printstring
620+
# syscall-4 y =
621+
622+
li $a0,10
623+
li $v0,1
624+
syscall #printint
625+
# syscall-1 10
626+
627+
li $a0,10
628+
li $v0,11
629+
syscall #printchar
630+
# syscall-11 10
631+
632+
la $a0,string$2
633+
li $v0,4
634+
syscall #printstring
635+
# syscall-4 SWAP x =
636+
637+
li $a0,10
638+
li $v0,1
639+
syscall #printint
640+
# syscall-1 10
641+
642+
li $a0,10
643+
li $v0,11
644+
syscall #printchar
645+
# syscall-11 10
646+
647+
la $a0,string$3
648+
li $v0,4
649+
syscall #printstring
650+
# syscall-4 SWAP y =
651+
652+
li $a0,5
653+
li $v0,1
654+
syscall #printint
655+
# syscall-1 5
656+
657+
li $a0,10
658+
li $v0,11
659+
syscall #printchar
660+
# syscall-11 10
614661

615662
addiu $sp,$sp,-760
616663
jal complete_flower_num

0 commit comments

Comments
 (0)