Skip to content

Commit 5be99f4

Browse files
committed
fix continue: jump to conditions not to "do"
1 parent d175a5b commit 5be99f4

File tree

4 files changed

+38
-91
lines changed

4 files changed

+38
-91
lines changed

MJCompiler/src/rs/ac/bg/etf/pp1/CodeGenerator.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import rs.ac.bg.etf.pp1.ast.DesignatorStatement;
2525
import rs.ac.bg.etf.pp1.ast.DivideOp;
2626
import rs.ac.bg.etf.pp1.ast.DoWhileDummyConditionEnd;
27+
import rs.ac.bg.etf.pp1.ast.DoWhileDummyConditionStart;
2728
import rs.ac.bg.etf.pp1.ast.DoWhileDummyStart;
2829
import rs.ac.bg.etf.pp1.ast.EqualOp;
2930
import rs.ac.bg.etf.pp1.ast.ExprListAddOpTerm;
@@ -982,9 +983,15 @@ public void visit(MulOpFactorList mulOpFactorList) {
982983
// do-while start instruction (for the purpose of returning back to the beginning of the do-while
983984
private Stack<Integer> startDoWhileBlockAddressForPatchingStack = new Stack<Integer>();
984985

985-
// the last instruction in the "then" block is unconditionally jump which will skip whole else part
986+
// break in do-while will immediately stop its execution and jump to the very first instruction after whole do-while statement
987+
// this will save addresses for patch when this address is found
986988
private Stack<List<Integer>> endDoWhileBlockAddressPatchingFromBreakStatementStack = new Stack<List<Integer>>();
987989

990+
// continue in do-while will immediately jump to the condition check (very first instruction of the condition check)
991+
// this will save addresses for patch when this address is found
992+
private Stack<List<Integer>> startDoWhileConditionBlockAddressPatchingFromContinueStatementStack = new Stack<List<Integer>>();
993+
994+
988995
@Override
989996
public void visit(DoWhileDummyStart DoWhileDummyStart) {
990997
// open new do-while scope by pushing new list of addresses for patching on stack
@@ -993,7 +1000,22 @@ public void visit(DoWhileDummyStart DoWhileDummyStart) {
9931000

9941001
// add "do" address
9951002
startDoWhileBlockAddressForPatchingStack.push(Code.pc);
1003+
9961004
endDoWhileBlockAddressPatchingFromBreakStatementStack.push(new ArrayList<Integer>());
1005+
startDoWhileConditionBlockAddressPatchingFromContinueStatementStack.push(new ArrayList<Integer>());
1006+
1007+
}
1008+
1009+
@Override
1010+
public void visit(DoWhileDummyConditionStart DoWhileDummyConditionStart) {
1011+
1012+
// fix all jumps from continue to the conditions check
1013+
1014+
for(int placeForPatch: startDoWhileConditionBlockAddressPatchingFromContinueStatementStack.peek()) {
1015+
Code.fixup(placeForPatch);
1016+
}
1017+
1018+
startDoWhileConditionBlockAddressPatchingFromContinueStatementStack.peek().clear(); // all addresses has been patched so nothing should left
9971019

9981020

9991021
}
@@ -1185,6 +1207,7 @@ public void visit(StatementDoWhile StatementDoWhile) {
11851207
destinationAddressPatchingFromORConditionBlockStack.pop();
11861208
startDoWhileBlockAddressForPatchingStack.pop();
11871209
endDoWhileBlockAddressPatchingFromBreakStatementStack.pop();
1210+
startDoWhileConditionBlockAddressPatchingFromContinueStatementStack.pop();
11881211
}
11891212

11901213

@@ -1205,10 +1228,11 @@ public void visit(StatementBreak StatementBreak) {
12051228
@Override
12061229
public void visit(StatementContinue StatementContinue) {
12071230

1208-
// continue represents unconditionally jump to the first instruction in the (deepest) do-while statement
1209-
1210-
Code.putJump(this.startDoWhileBlockAddressForPatchingStack.peek());
1231+
// continue represents unconditionally jump to the first instruction of the condition in the (deepest) do-while statement
12111232

1233+
Code.putJump(0);
1234+
startDoWhileConditionBlockAddressPatchingFromContinueStatementStack.peek().add(Code.pc - 2); // saved address for patching
1235+
12121236
}
12131237
}
12141238

MJCompiler/test/program.mj

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,18 @@
11
program ABC
22

33
{
4-
bool verify() {
5-
return true;
6-
}
7-
8-
void main() int x, y, n; int inv_max; {
94

10-
if(verify() && x == 0) {
11-
x = 1;
12-
print('t');
13-
print(eol);
14-
} else
15-
x = -1;
5+
void main() int x; int i; {
166

17-
if(x == 1)
18-
y = 2;
19-
else
20-
y = 3;
21-
print('y');
22-
print(y);
23-
print(eol);
24-
25-
if(x > 0 && y < 2)
26-
x = 2;
27-
print('x');
28-
print(x);
29-
print(eol);
30-
31-
if(x >= 0 || y <= 2)
32-
x = 3;
33-
print('x');
34-
print(x);
35-
print(eol);
36-
37-
if(x != 0 && y >= 2 || x == 1)
38-
x = 4;
39-
print('x');
40-
print(x);
41-
print(eol);
42-
43-
if(-x > y) {
44-
inv_max = -x;
45-
print('>');
46-
} else {
47-
if(x == y) {
48-
inv_max = -1;
49-
print('=');
50-
} else {
51-
inv_max = -y;
52-
print('<');
53-
}
54-
}
55-
print(eol);
56-
print('-'); print('m'); print('a'); print('x'); print(inv_max);
57-
58-
print(eol);
59-
print(x);
60-
print(eol);
61-
n = -11;
62-
do {
63-
n++;
64-
65-
if(-5 <= n && n <= -3){
66-
if(n == -5) {
67-
print('p');
68-
} else if(n == -4) {
69-
print('c');
70-
} else {
71-
print('t');
72-
}
7+
i = 1;
8+
do{
9+
if(i % 2 == 0) {
10+
i++;
11+
continue;
7312
}
74-
75-
if(x - 1 + n - 3 == 0) {
76-
print('x');
77-
break;
78-
}
79-
if(n == -2) {
80-
print(' '); print('n'); print('e'); print('g');
81-
print(eol);
82-
continue;
83-
}
84-
print(n);
85-
print(eol);
86-
87-
88-
} while (n <= x);
89-
90-
print(n);
91-
print(eol);
92-
13+
print('i'); print(i); print(eol);
14+
i++;
15+
}while(i<=6);
9316

9417
}
9518
}

MJCompiler/test/program.obj

-377 Bytes
Binary file not shown.

MJCompiler/test/rs/ac/bg/etf/pp1/Compiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void main(String[] args) throws Exception {
3030
Reader br = null;
3131
try {
3232

33-
String mjFileName = "controll_structures";
33+
String mjFileName = "program";
3434

3535
File sourceCode = new File("test/" + mjFileName + ".mj");
3636
// File sourceCode = new File("test/class_extends.mj");

0 commit comments

Comments
 (0)