C: less code for virtual calls

This commit is contained in:
Alexey Andreev 2019-05-23 15:26:35 +03:00
parent 8b3df6f730
commit 063a9f049e

View File

@ -352,14 +352,18 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(VariableExpr expr) { public void visit(VariableExpr expr) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
if (expr.getIndex() == 0) { writer.print(getVariableName(expr.getIndex()));
writer.print("teavm_this_");
} else {
writer.print("teavm_local_" + expr.getIndex());
}
popLocation(expr.getLocation()); popLocation(expr.getLocation());
} }
private String getVariableName(int index) {
if (index == 0) {
return "teavm_this_";
} else {
return "teavm_local_" + index;
}
}
@Override @Override
public void visit(SubscriptExpr expr) { public void visit(SubscriptExpr expr) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
@ -504,12 +508,21 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
return; return;
} }
String receiver = allocTemporaryVariable(CVariableType.PTR); Expr receiverArg = arguments.get(0);
writer.print("((").print(receiver).print(" = "); boolean closingParenthesis = false;
arguments.get(0).acceptVisitor(this); String receiver;
if (receiverArg instanceof VariableExpr) {
receiver = getVariableName(((VariableExpr) receiverArg).getIndex());
} else {
receiver = allocTemporaryVariable(CVariableType.PTR);
writer.print("((").print(receiver).print(" = ");
arguments.get(0).acceptVisitor(this);
writer.print("), ");
closingParenthesis = true;
}
includes.includeClass(vtableClass); includes.includeClass(vtableClass);
writer.print("), TEAVM_METHOD(") writer.print("TEAVM_METHOD(")
.print(receiver).print(", ") .print(receiver).print(", ")
.print(names.forClassClass(vtableClass)).print(", ") .print(names.forClassClass(vtableClass)).print(", ")
.print(names.forVirtualMethod(reference.getDescriptor())) .print(names.forVirtualMethod(reference.getDescriptor()))
@ -518,9 +531,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
writer.print(", "); writer.print(", ");
arguments.get(i).acceptVisitor(this); arguments.get(i).acceptVisitor(this);
} }
writer.print("))"); writer.print(")");
if (closingParenthesis) {
freeTemporaryVariable(CVariableType.PTR); writer.print(")");
freeTemporaryVariable(CVariableType.PTR);
}
} }
private void generateNoMethodCall(MethodReference reference, List<? extends Expr> arguments) { private void generateNoMethodCall(MethodReference reference, List<? extends Expr> arguments) {