From 063a9f049ea7210deccc9e1edd79c3d4c24f7250 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 23 May 2019 15:26:35 +0300 Subject: [PATCH] C: less code for virtual calls --- .../c/generate/CodeGenerationVisitor.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java index 86fe439f0..3b7bcb476 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java @@ -352,14 +352,18 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { @Override public void visit(VariableExpr expr) { pushLocation(expr.getLocation()); - if (expr.getIndex() == 0) { - writer.print("teavm_this_"); - } else { - writer.print("teavm_local_" + expr.getIndex()); - } + writer.print(getVariableName(expr.getIndex())); popLocation(expr.getLocation()); } + private String getVariableName(int index) { + if (index == 0) { + return "teavm_this_"; + } else { + return "teavm_local_" + index; + } + } + @Override public void visit(SubscriptExpr expr) { pushLocation(expr.getLocation()); @@ -504,12 +508,21 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { return; } - String receiver = allocTemporaryVariable(CVariableType.PTR); - writer.print("((").print(receiver).print(" = "); - arguments.get(0).acceptVisitor(this); + Expr receiverArg = arguments.get(0); + boolean closingParenthesis = false; + 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); - writer.print("), TEAVM_METHOD(") + writer.print("TEAVM_METHOD(") .print(receiver).print(", ") .print(names.forClassClass(vtableClass)).print(", ") .print(names.forVirtualMethod(reference.getDescriptor())) @@ -518,9 +531,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { writer.print(", "); arguments.get(i).acceptVisitor(this); } - writer.print("))"); - - freeTemporaryVariable(CVariableType.PTR); + writer.print(")"); + if (closingParenthesis) { + writer.print(")"); + freeTemporaryVariable(CVariableType.PTR); + } } private void generateNoMethodCall(MethodReference reference, List arguments) {