Skip to content

Commit

Permalink
Minus expression is not correctly handled for now in codebox backend,…
Browse files Browse the repository at this point in the history
… so use the -1*exp workaround, improve MinusInst handling in LLVM backend.
  • Loading branch information
sletz committed Oct 23, 2023
1 parent 5b710f8 commit bb89cd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions compiler/generator/codebox/codebox_instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions compiler/generator/llvm/llvm_instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bb89cd1

Please sign in to comment.