diff --git a/compiler/generator/codebox/codebox_instructions.hh b/compiler/generator/codebox/codebox_instructions.hh index a962ce6d4c..552eb35013 100644 --- a/compiler/generator/codebox/codebox_instructions.hh +++ b/compiler/generator/codebox/codebox_instructions.hh @@ -539,6 +539,13 @@ class CodeboxInstVisitor : public TextInstVisitor { } } + // Simply multiply the value by -1 here + virtual void visit(MinusInst* inst) + { + Typed::VarType type = TypingVisitor::getType(inst->fInst); + InstBuilder::genMul(InstBuilder::genTypedNum(type, -1.), inst->fInst)->accept(this); + } + virtual void visit(BinopInst* inst) { Typed::VarType type1 = TypingVisitor::getType(inst->fInst1); diff --git a/compiler/generator/llvm/llvm_instructions.hh b/compiler/generator/llvm/llvm_instructions.hh index 172539dd56..5493efdb3f 100644 --- a/compiler/generator/llvm/llvm_instructions.hh +++ b/compiler/generator/llvm/llvm_instructions.hh @@ -789,13 +789,27 @@ class LLVMInstVisitor : public InstVisitor, public LLVMTypeHelper { virtual void visit(Int64NumInst* inst) { fCurValue = genInt64(inst->fNum); } - // Simply multiply the value by -1 here + //====== + // Unop + //====== + virtual void visit(MinusInst* inst) { inst->fInst->accept(this); - fCurValue = generateBinop(kMul, genTypedNum(getCurType(), -1.), fCurValue); + if (getCurType() == getInt32Ty()) { + fCurValue = fBuilder->CreateNeg(fCurValue); + } else if (getCurType() == getFloatTy() || getCurType() == getDoubleTy()) { + fCurValue = fBuilder->CreateFNeg(fCurValue); + } else { + // Simply multiply the value by -1 here + fCurValue = generateBinop(kMul, genTypedNum(getCurType(), -1.), fCurValue); + } } + //======= + // Binop + //======= + virtual void visit(BinopInst* inst) { // Keep result of first arg compilation