Skip to content

Commit

Permalink
Fixed post inc and dec to not use a temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
QFSW committed Mar 26, 2019
1 parent e6bdb55 commit 48bef36
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/ast/operators/unaryOperators/unaryPostDecrement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ void UnaryPostDecrement::printC(std::ostream &os) const

void UnaryPostDecrement::generateIL(std::vector<Instr> &instrs, ILContext &context, std::string destReg) const
{
getOperand()->generateIL(instrs, context, "$t0");
instrs.push_back(Instr("mov", destReg, "$t0"));
instrs.push_back(Instr("dec", "$t0"));
std::string storageLabel = context.makeName("dec_t");
getOperand()->generateIL(instrs, context, storageLabel);
instrs.push_back(Instr("mov", destReg, storageLabel));
instrs.push_back(Instr("dec", storageLabel));
LValuePtr lvalue = Utils::tryCast<LValue>(getOperand(), "Illegal ++: " + toString() + ". LHS of an increment must be an lvalue");
lvalue->generateLValueStoreIL(instrs, context, "$t0");
lvalue->generateLValueStoreIL(instrs, context, storageLabel);
}

bool UnaryPostDecrement::isConstant() const { return false; }
Expand Down
9 changes: 5 additions & 4 deletions src/ast/operators/unaryOperators/unaryPostIncrement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ void UnaryPostIncrement::printC(std::ostream &os) const

void UnaryPostIncrement::generateIL(std::vector<Instr> &instrs, ILContext &context, std::string destReg) const
{
getOperand()->generateIL(instrs, context, "$t0");
instrs.push_back(Instr("mov", destReg, "$t0"));
instrs.push_back(Instr("inc", "$t0"));
std::string storageLabel = context.makeName("inc_t");
getOperand()->generateIL(instrs, context, storageLabel);
instrs.push_back(Instr("mov", destReg, storageLabel));
instrs.push_back(Instr("inc", storageLabel));
LValuePtr lvalue = Utils::tryCast<LValue>(getOperand(), "Illegal ++: " + toString() + ". LHS of an increment must be an lvalue");
lvalue->generateLValueStoreIL(instrs, context, "$t0");
lvalue->generateLValueStoreIL(instrs, context, storageLabel);
}

bool UnaryPostIncrement::isConstant() const { return false; }
Expand Down

0 comments on commit 48bef36

Please sign in to comment.