|
|
@ -68,7 +68,7 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
private final Label errorDiv = new Label("error.Div");
|
|
|
|
private final Label errorDiv = new Label("error.Div");
|
|
|
|
/** Index out of bounds. */
|
|
|
|
/** Index out of bounds. */
|
|
|
|
private final Label errorOob = new Label("error.OOB");
|
|
|
|
private final Label errorOob = new Label("error.OOB");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Emits the top level of the program.
|
|
|
|
* Emits the top level of the program.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -158,6 +158,11 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
/** The descriptor for the current function, or null at the top level. */
|
|
|
|
/** The descriptor for the current function, or null at the top level. */
|
|
|
|
private final FuncInfo funcInfo;
|
|
|
|
private final FuncInfo funcInfo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Label of code that exits from block. */
|
|
|
|
|
|
|
|
protected Label elseBlock;
|
|
|
|
|
|
|
|
// Variable to store offset from frame pointer to identify next
|
|
|
|
|
|
|
|
// empty space on stack frame to store variable
|
|
|
|
|
|
|
|
private final int offset = 12;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* An analyzer for the function described by FUNCINFO0, which is null for the
|
|
|
|
* An analyzer for the function described by FUNCINFO0, which is null for the
|
|
|
|
* top level.
|
|
|
|
* top level.
|
|
|
@ -236,38 +241,52 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(BinaryExpr node) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(ExprStmt node) {
|
|
|
|
public Void analyze(ExprStmt node) {
|
|
|
|
node.expr.dispatch(this);
|
|
|
|
node.expr.dispatch(this);
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(ForStmt node) {
|
|
|
|
public Void analyze(IfExpr node) {
|
|
|
|
// control flow
|
|
|
|
node.condition.dispatch(this);
|
|
|
|
return defaultAction(node);
|
|
|
|
Label ln = generateLocalLabel();
|
|
|
|
|
|
|
|
node.thenExpr.dispatch(this);
|
|
|
|
|
|
|
|
backend.emitJ(ln, "Jump to end of if expression");
|
|
|
|
|
|
|
|
backend.emitLocalLabel(elseBlock, "Else part of if expression");
|
|
|
|
|
|
|
|
node.elseExpr.dispatch(this);
|
|
|
|
|
|
|
|
backend.emitLocalLabel(ln, "End of if expression");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(Identifier node) {
|
|
|
|
public Void analyze(IfStmt node) {
|
|
|
|
// statement
|
|
|
|
node.condition.dispatch(this);
|
|
|
|
return defaultAction(node);
|
|
|
|
Label ln = generateLocalLabel();
|
|
|
|
|
|
|
|
for(Stmt s:node.thenBody)
|
|
|
|
|
|
|
|
s.dispatch(this);
|
|
|
|
|
|
|
|
backend.emitJ(ln, "Jump to end of if statement");
|
|
|
|
|
|
|
|
backend.emitLocalLabel(elseBlock, "Else part of if statement");
|
|
|
|
|
|
|
|
for(Stmt s:node.elseBody)
|
|
|
|
|
|
|
|
s.dispatch(this);
|
|
|
|
|
|
|
|
backend.emitLocalLabel(ln, "End of if statement");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(IfExpr node) {
|
|
|
|
public Void analyze(BinaryExpr node) {
|
|
|
|
|
|
|
|
elseBlock = generateLocalLabel();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(ForStmt node) {
|
|
|
|
// control flow
|
|
|
|
// control flow
|
|
|
|
return defaultAction(node);
|
|
|
|
return defaultAction(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(IfStmt node) {
|
|
|
|
public Void analyze(Identifier node) {
|
|
|
|
// control flow
|
|
|
|
// statement
|
|
|
|
return defaultAction(node);
|
|
|
|
return defaultAction(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(IndexExpr node) {
|
|
|
|
public Void analyze(IndexExpr node) {
|
|
|
@ -276,7 +295,6 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(ListExpr node) {
|
|
|
|
public Void analyze(ListExpr node) {
|
|
|
|
// statement
|
|
|
|
// statement
|
|
|
|