Skip to content
Snippets Groups Projects
Unverified Commit 2948129d authored by Shivane Sabharwal's avatar Shivane Sabharwal
Browse files

add allocProtoType

parent 64c5899d
No related branches found
No related tags found
No related merge requests found
Pipeline #293 passed with warnings with stages
in 11 minutes and 35 seconds
......@@ -175,6 +175,7 @@ public class CodeGenImpl extends CodeGenBase {
backend.emitMV(tmpReg, SP, "Return STR OBJ address.");
}
}
// Every function needs a register manager.
......@@ -406,6 +407,24 @@ public class CodeGenImpl extends CodeGenBase {
private registerManager regMgr = new registerManager();
/* This method allocates object on the heap using the prototype
* Return register containing address */
private RiscVBackend.Register allocPrototype(SymbolType typ, RiscVBackend.Register objReg) {
RiscVBackend.Register storeAddr = regMgr.borrowOneTmp();
// Convention: if typ is INT_TYPE then objReg contains the literal value, not a pointer.
if (typ.equals(SymbolType.INT_TYPE)) {
backend.emitLA(A0, intClass.getPrototypeLabel(), "Load prototype address");
backend.emitJAL(objectAllocLabel, "Allocate int");
backend.emitSW(objReg, A0, 3 * backend.getWordSize(), "Set __int__ value");
backend.emitMV(storeAddr, A0, "Store address of new allocation in register");
return storeAddr;
} else if (typ.equals(SymbolType.STR_TYPE)) {
backend.emitLA(A0, strClass.getPrototypeLabel(), "Load prototype address");
backend.emitJAL(objectAllocLabel, "Allocate string");
}
return null;
}
/** An analyzer for the function described by FUNCINFO0, which is null
* for the top level. */
StmtAnalyzer(FuncInfo funcInfo0) {
......@@ -533,6 +552,31 @@ public class CodeGenImpl extends CodeGenBase {
return null;
}
@Override
public RiscVBackend.Register analyze(UnaryExpr node) {
RiscVBackend.Register operandReg = node.operand.dispatch(this);
switch (node.operator) {
case "-":
RiscVBackend.Register savedOpAddr = regMgr.borrowOneTmp();
RiscVBackend.Register savedResult = regMgr.borrowOnePersist();
backend.emitMV(savedOpAddr, operandReg, "Save operand addr in register");
if (node.operand instanceof IntegerLiteral) {
backend.emitLW(operandReg, operandReg, 3 * backend.getWordSize(), "Load operand value");
} else if (node.operand instanceof Identifier) {
backend.emitLW(operandReg, operandReg, 0, "Load operand value");
}
backend.emitSUB(savedResult, ZERO, operandReg, "Unary Operator-::INT save in same operand reg");
RiscVBackend.Register heapObject = allocPrototype(SymbolType.INT_TYPE, savedResult);
regMgr.returnOne(operandReg);
regMgr.returnOne(savedOpAddr);
regMgr.returnOne(savedResult);
return heapObject;
}
return null;
}
@Override
public RiscVBackend.Register analyze(Identifier node) {
SymbolInfo id = sym.get(node.name);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment