From 57a39a156a7eb946a8e174837e1e4cf561ea63a9 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 31 Jul 2015 17:20:21 +0300 Subject: [PATCH] Emit neg and not --- .../teavm/model/emit/ConditionEmitter.java | 4 ++++ .../org/teavm/model/emit/ValueEmitter.java | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/teavm-core/src/main/java/org/teavm/model/emit/ConditionEmitter.java b/teavm-core/src/main/java/org/teavm/model/emit/ConditionEmitter.java index ea8101e83..b3da0fa5d 100644 --- a/teavm-core/src/main/java/org/teavm/model/emit/ConditionEmitter.java +++ b/teavm-core/src/main/java/org/teavm/model/emit/ConditionEmitter.java @@ -45,4 +45,8 @@ public class ConditionEmitter { ForkEmitter newFork = fork.or(block, otherEmitter.fork); return new ConditionEmitter(pe, newFork); } + + public ConditionEmitter not() { + return new ConditionEmitter(pe, fork.not()); + } } diff --git a/teavm-core/src/main/java/org/teavm/model/emit/ValueEmitter.java b/teavm-core/src/main/java/org/teavm/model/emit/ValueEmitter.java index 65b9b1457..7421d84a8 100644 --- a/teavm-core/src/main/java/org/teavm/model/emit/ValueEmitter.java +++ b/teavm-core/src/main/java/org/teavm/model/emit/ValueEmitter.java @@ -45,6 +45,7 @@ import org.teavm.model.instructions.IntegerSubtype; import org.teavm.model.instructions.InvocationType; import org.teavm.model.instructions.InvokeInstruction; import org.teavm.model.instructions.IsInstanceInstruction; +import org.teavm.model.instructions.NegateInstruction; import org.teavm.model.instructions.NumericOperandType; import org.teavm.model.instructions.PutElementInstruction; import org.teavm.model.instructions.PutFieldInstruction; @@ -119,6 +120,27 @@ public class ValueEmitter { return pe; } + public ValueEmitter neg() { + if (!(type instanceof ValueType.Primitive)) { + throw new EmitException("Can't negate non-primitive: " + type); + } + + ValueEmitter value = this; + PrimitiveType type = ((ValueType.Primitive) this.type).getKind(); + IntegerSubtype subtype = convertToIntegerSubtype(type); + if (subtype != null) { + value = value.castToInteger(subtype); + type = PrimitiveType.INTEGER; + } + + ValueEmitter result = pe.newVar(ValueType.primitive(type)); + NegateInstruction insn = new NegateInstruction(convertToNumeric(type)); + insn.setOperand(value.variable); + insn.setReceiver(result.variable); + pe.addInstruction(insn); + return result; + } + static class Pair { ValueEmitter first; ValueEmitter second;