diff --git a/core/src/main/java/org/teavm/model/instructions/AssignInstruction.java b/core/src/main/java/org/teavm/model/instructions/AssignInstruction.java index 247ff7bb8..94ef34e20 100644 --- a/core/src/main/java/org/teavm/model/instructions/AssignInstruction.java +++ b/core/src/main/java/org/teavm/model/instructions/AssignInstruction.java @@ -18,10 +18,6 @@ package org.teavm.model.instructions; import org.teavm.model.Instruction; import org.teavm.model.Variable; -/** - * - * @author Alexey Andreev - */ public class AssignInstruction extends Instruction { private Variable assignee; private Variable receiver; diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java index 59b22e707..c1e91e688 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java @@ -147,7 +147,10 @@ public class CompositeMethodGenerator { returnBlockIndex = program.basicBlockCount() - 1; for (int i = capturedValues.size(); i < template.variableCount(); ++i) { - program.createVariable(); + VariableReader variable = template.variableAt(i); + Variable variableCopy = program.createVariable(); + variableCopy.setDebugName(variable.getDebugName()); + variableCopy.setLabel(variable.getLabel()); } // Pre-create phis @@ -305,10 +308,11 @@ public class CompositeMethodGenerator { add(insn); return insn.getReceiver(); } else if (value instanceof ValueImpl) { - return varContext.emitVariable((ValueImpl) value, new CallLocation(MetaprogrammingImpl.templateMethod, - location)); + Variable result = varContext.emitVariable((ValueImpl) value, + new CallLocation(MetaprogrammingImpl.templateMethod, location)); + return coalesce(result); } else if (value instanceof LazyValueImpl) { - return lazy((LazyValueImpl) value); + return coalesce(lazy((LazyValueImpl) value)); } else if (value instanceof ReflectFieldImpl) { ReflectFieldImpl reflectField = (ReflectFieldImpl) value; diagnostics.error(new CallLocation(MetaprogrammingImpl.templateMethod, location), @@ -356,7 +360,7 @@ public class CompositeMethodGenerator { if (result instanceof ValueImpl) { return ((ValueImpl) result).innerValue; } else if (result instanceof LazyValueImpl) { - return lazy((LazyValueImpl) result); + return lazy((LazyValueImpl) result); } else if (result != null) { throw new IllegalStateException("Unknown value type: " + result.getClass().getName()); } else { @@ -364,6 +368,16 @@ public class CompositeMethodGenerator { } } + private Variable coalesce(Variable var) { + if (var == null) { + NullConstantInstruction nullInsn = new NullConstantInstruction(); + nullInsn.setReceiver(program.createVariable()); + var = nullInsn.getReceiver(); + add(nullInsn); + } + return var; + } + Variable box(Variable var, ValueType type) { if (type instanceof ValueType.Primitive) { switch (((ValueType.Primitive) type).getKind()) { @@ -805,10 +819,12 @@ public class CompositeMethodGenerator { if (type == InvocationType.VIRTUAL && instance != null) { if (method.getClassName().equals(Value.class.getName())) { if (method.getName().equals("get")) { - AssignInstruction insn = new AssignInstruction(); - insn.setReceiver(var(receiver)); - insn.setAssignee(var(instance)); - add(insn); + if (receiver != null) { + AssignInstruction insn = new AssignInstruction(); + insn.setReceiver(var(receiver)); + insn.setAssignee(var(instance)); + add(insn); + } return; } else { diagnostics.error(new CallLocation(MetaprogrammingImpl.templateMethod, location),