|
|
@ -769,12 +769,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
backend.emitOR(Register.A0, Register.A0, Register.T0, "OR operation");
|
|
|
|
backend.emitOR(Register.A0, Register.A0, Register.T0, "OR operation");
|
|
|
|
backend.emitLocalLabel(label, "Next step after OR");
|
|
|
|
backend.emitLocalLabel(label, "Next step after OR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*Maybe NA
|
|
|
|
|
|
|
|
else if(operator.equals("is"))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
backend.emitXOR(Register.A0, Register.A0, Register.T0, comment);
|
|
|
|
|
|
|
|
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
backend.emitJAL(errorNI, "Operator not implemented for boolean operands");
|
|
|
|
backend.emitJAL(errorNI, "Operator not implemented for boolean operands");
|
|
|
@ -792,7 +786,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
|
|
|
|
backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
|
|
|
|
sp_off -= 2;
|
|
|
|
sp_off -= 2;
|
|
|
|
return Register.A0;
|
|
|
|
return Register.A0;
|
|
|
|
//addLists();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
backend.emitJAL(errorNI, "Operator not implemented for list operands");
|
|
|
|
backend.emitJAL(errorNI, "Operator not implemented for list operands");
|
|
|
@ -808,7 +801,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
|
|
|
|
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
|
|
|
|
backend.emitJAL(streqlLabel, "Invoke method:streql");
|
|
|
|
backend.emitJAL(streqlLabel, "Invoke method:streql");
|
|
|
|
sp_off -= 2;
|
|
|
|
sp_off -= 2;
|
|
|
|
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
|
|
|
|
backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(operator.equals("!="))
|
|
|
|
else if(operator.equals("!="))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -866,8 +859,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Register analyze(Identifier node)
|
|
|
|
public Register analyze(Identifier node)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//backend.emitLW(T6, FP, 0, "Inside Identifier: ");
|
|
|
|
|
|
|
|
System.out.println(node.getInferredType());
|
|
|
|
|
|
|
|
Identifier targetID = (Identifier)node;
|
|
|
|
Identifier targetID = (Identifier)node;
|
|
|
|
Object var = getVar(targetID);
|
|
|
|
Object var = getVar(targetID);
|
|
|
|
if(var instanceof StackVarRuntimeInfo){
|
|
|
|
if(var instanceof StackVarRuntimeInfo){
|
|
|
@ -881,11 +872,9 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
return Register.A0;
|
|
|
|
return Register.A0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Register analyze(WhileStmt node)
|
|
|
|
public Register analyze(WhileStmt node)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
System.out.println("Inside WhileStmt: ");
|
|
|
|
|
|
|
|
Label startLoop = generateLocalLabel();
|
|
|
|
Label startLoop = generateLocalLabel();
|
|
|
|
backend.emitLocalLabel(startLoop, "Beginning of while loop");
|
|
|
|
backend.emitLocalLabel(startLoop, "Beginning of while loop");
|
|
|
|
Register result = node.condition.dispatch(this);
|
|
|
|
Register result = node.condition.dispatch(this);
|
|
|
@ -933,18 +922,13 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
return Register.A0;
|
|
|
|
return Register.A0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Register analyze(IntegerLiteral node) {
|
|
|
|
public Register analyze(IntegerLiteral node)
|
|
|
|
System.out.println("Inside IntegerLiteral: " + node.value);
|
|
|
|
{
|
|
|
|
//backend.emitLW(T6, FP, 0, "Inside IntegerLiteral: " + node.value);
|
|
|
|
|
|
|
|
backend.emitLI(A0, node.value, "Load integer literal " + node.value);
|
|
|
|
backend.emitLI(A0, node.value, "Load integer literal " + node.value);
|
|
|
|
|
|
|
|
|
|
|
|
return A0;
|
|
|
|
return A0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Register analyze(ForStmt node)
|
|
|
|
public Register analyze(ForStmt node)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -1011,7 +995,6 @@ 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];
|
|
|
@ -1031,7 +1014,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
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");
|
|
|
|
backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
|
|
|
|
incSp(1);
|
|
|
|
incSp(1);
|
|
|
|
//backend.emitADDI(Register.SP, Register.SP, -2*wordSize, "Set SP to last argument");
|
|
|
|
|
|
|
|
backend.emitLocalLabel(startLoop, "Start for loop");
|
|
|
|
backend.emitLocalLabel(startLoop, "Start for loop");
|
|
|
|
backend.emitLW(iter, Register.FP, -(sp_off-1)*wordSize, "Load index from stack");
|
|
|
|
backend.emitLW(iter, Register.FP, -(sp_off-1)*wordSize, "Load index from stack");
|
|
|
|
backend.emitLW(l, Register.FP, -(sp_off-2)*wordSize, "Load string location from stack");
|
|
|
|
backend.emitLW(l, Register.FP, -(sp_off-2)*wordSize, "Load string location from stack");
|
|
|
@ -1067,7 +1049,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
System.out.println("Iterable not a list or a string");
|
|
|
|
backend.emitJAL(errorNI, "Operator not implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int n:regs)
|
|
|
|
for(int n:regs)
|
|
|
|
freeRegister(n);
|
|
|
|
freeRegister(n);
|
|
|
@ -1124,7 +1106,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
backend.emitLBU(T0, T0, 0, "Load character");
|
|
|
|
backend.emitLBU(T0, T0, 0, "Load character");
|
|
|
|
backend.emitSLLI(A0, T0, 2, "A0 = T0 * 4");
|
|
|
|
backend.emitSLLI(A0, T0, 2, "A0 = T0 * 4");
|
|
|
|
backend.emitSLLI(T0, T0, 4, "T0 = T0 * 16");
|
|
|
|
backend.emitSLLI(T0, T0, 4, "T0 = T0 * 16");
|
|
|
|
backend.emitADD(T0, T0, A0, " T0*20 = offset to all chars");
|
|
|
|
backend.emitADD(T0, T0, A0, "T0*20 = offset to all chars");
|
|
|
|
backend.emitLA(A0, allCharsLabel, "All chars");
|
|
|
|
backend.emitLA(A0, allCharsLabel, "All chars");
|
|
|
|
backend.emitADD(A0, A0, T0, "get the char");
|
|
|
|
backend.emitADD(A0, A0, T0, "get the char");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1172,7 +1154,8 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
* error and exit the program. For example, to throw an OOB error: backend.emitJ(errorOob, "Go
|
|
|
|
* error and exit the program. For example, to throw an OOB error: backend.emitJ(errorOob, "Go
|
|
|
|
* to out-of-bounds error and abort");
|
|
|
|
* to out-of-bounds error and abort");
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected void emitCustomCode() {
|
|
|
|
protected void emitCustomCode()
|
|
|
|
|
|
|
|
{
|
|
|
|
emitStdFunc("concat");
|
|
|
|
emitStdFunc("concat");
|
|
|
|
emitStdFunc("conslist");
|
|
|
|
emitStdFunc("conslist");
|
|
|
|
emitStdFunc("strcat");
|
|
|
|
emitStdFunc("strcat");
|
|
|
@ -1190,7 +1173,8 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
|
|
|
|
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
|
|
|
|
private void emitErrorFunc(Label errLabel, String msg) {
|
|
|
|
private void emitErrorFunc(Label errLabel, String msg)
|
|
|
|
|
|
|
|
{
|
|
|
|
backend.emitGlobalLabel(errLabel);
|
|
|
|
backend.emitGlobalLabel(errLabel);
|
|
|
|
if(errLabel==errorOob)
|
|
|
|
if(errLabel==errorOob)
|
|
|
|
backend.emitLI(A0, ERROR_OOB, "Exit code for: " + msg);
|
|
|
|
backend.emitLI(A0, ERROR_OOB, "Exit code for: " + msg);
|
|
|
|