Fixed for loop

master
Apoorva Ranade 4 years ago
parent e6230c71a4
commit a23d39eb02

@ -212,13 +212,16 @@ public class CodeGenImpl extends CodeGenBase
private int getRegister(){ //return a handle of a vacant register private int getRegister(){ //return a handle of a vacant register
for(int i = 0; i < 10; ++i) for(int i = 0; i < 10; ++i)
if(registerAvilMap[i]) if(registerAvilMap[i])
{
registerAvilMap[i]=false;
return i; return i;
}
for(int i = 0; i < 10; ++i) for(int i = 0; i < 10; ++i)
registerAvilMap[i] = true; //freeall; registerAvilMap[i] = true; //freeall;
return 0; return 0;
} }
private void freeRegister(int handle){ //handle used to free the register private void freeRegister(int handle){ //handle used to free the register
registerAvilMap[handle] = false; registerAvilMap[handle] = true;
} }
private int getParamLocationOffset(int index, int paramSize) { private int getParamLocationOffset(int index, int paramSize) {
@ -987,6 +990,7 @@ public class CodeGenImpl extends CodeGenBase
{ {
Label startLoop = generateLocalLabel(); Label startLoop = generateLocalLabel();
Label endLoop = generateLocalLabel(); Label endLoop = generateLocalLabel();
Label temp = generateLocalLabel();
r=getRegister(); r=getRegister();
regs.add(r); regs.add(r);
Register iden = registerPool[r]; Register iden = registerPool[r];
@ -997,15 +1001,28 @@ public class CodeGenImpl extends CodeGenBase
r=getRegister(); r=getRegister();
regs.add(r); regs.add(r);
Register ln = registerPool[r]; Register ln = registerPool[r];
backend.emitBNEZ(Register.A0, temp, "Ensure not none");
backend.emitJ(errorNone, "Empty List");
backend.emitLocalLabel(temp, "Continue execution for for-loop");
backend.emitMV(l,Register.A0,"Location of list"); backend.emitMV(l,Register.A0,"Location of list");
r=getRegister(); r=getRegister();
regs.add(r); regs.add(r);
Register iter = registerPool[r]; Register iter = registerPool[r];
backend.emitSW(l, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitMV(iter, Register.ZERO, "Initialize index variable"); backend.emitMV(iter, Register.ZERO, "Initialize index variable");
backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitLocalLabel(startLoop, "Start for loop"); backend.emitLocalLabel(startLoop, "Start for loop");
sp_off--;
backend.emitLW(iter, Register.FP, -sp_off*wordSize, "Load index from stack");
backend.emitLW(l, Register.FP, -(sp_off-1)*wordSize, "Store list location from stack");
backend.emitLW(ln, l, getAttrOffset(listClass, "__len__"), "Get attribute __len__"); backend.emitLW(ln, l, getAttrOffset(listClass, "__len__"), "Get attribute __len__");
backend.emitBGE(iter, ln, endLoop, "Jump to end loop if counter exceeds length"); backend.emitBGEU(iter, ln, endLoop, "Jump to end loop if counter exceeds length");
backend.emitADDI(iden, iter, 4, "Compute list element offset in words"); backend.emitADDI(iter, iter, 1, "Increment counter");
backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitADDI(iden, iter, 3, "Compute list element offset in words");
backend.emitLI(ln, wordSize, "Word size in bytes"); backend.emitLI(ln, wordSize, "Word size in bytes");
backend.emitMUL(iden, iden, ln, "Compute list element offset in bytes"); backend.emitMUL(iden, iden, ln, "Compute list element offset in bytes");
backend.emitADD(iden, l, iden, "Pointer to list element"); backend.emitADD(iden, l, iden, "Pointer to list element");
@ -1024,7 +1041,6 @@ public class CodeGenImpl extends CodeGenBase
} }
for(Stmt stmt:node.body) for(Stmt stmt:node.body)
stmt.dispatch(this); stmt.dispatch(this);
backend.emitADDI(iter, iter, 1, "Increment counter");
backend.emitJ(startLoop, "Jump to beginning of loop"); backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of for loop"); backend.emitLocalLabel(endLoop, "End of for loop");
} }

@ -2,4 +2,4 @@
echo "Running all test cases inside src/test/data/pa3/sample/ folder)" echo "Running all test cases inside src/test/data/pa3/sample/ folder)"
java -cp "chocopy-ref.jar:target/assignment.jar" chocopy.ChocoPy --pass=rrs --test --run --dir src/test/data/pa3/sample/ java -cp "chocopy-ref.jar:target/assignment.jar" chocopy.ChocoPy --pass=rrs --test --run --dir src/test/data/pa3/sample/ --maxCycles 65536

Loading…
Cancel
Save