Commit c938ad6f authored by zygzagZ's avatar zygzagZ

Fix lazy bool evaluation

parent 8256c661
......@@ -406,6 +406,8 @@ void Compiler::generatePhiXCHG(const map<VariablePtr, VariablePtr> &varMap) {
void Compiler::generateQJump(QJump &q) {
auto thisBlock = q.block;
auto tgBlock = q.target->block;
assert(thisBlock && tgBlock);
thisBlock->append(tgBlock);
const auto &map = tgBlock->getPhiMapping(thisBlock);
generatePhiXCHG(map);
append("JMP", getBlockLabelText(q.target));
......@@ -435,6 +437,7 @@ void Compiler::generateQJumpCond(QJumpCond &q) {
auto thisBlock = q.block;
auto tgBlock = q.target->block;
assert(thisBlock && tgBlock);
thisBlock->append(tgBlock);
const auto &map = tgBlock->getPhiMapping(thisBlock);
for (const auto &phiVars : map) {
if ((phiVars.first->info && phiVars.second->info && phiVars.first->info != phiVars.second->info) ||
......
......@@ -24,6 +24,7 @@ public:
shared_ptr<QPhi> phi;
void append(const BasicBlockPtr& after) {
assert(after);
if (find(out.begin(), out.end(), after) != out.end()) return;
out.push_back(after);
after->in.push_back(shared_from_this());
after->phi->phi.emplace_back();
......
......@@ -201,8 +201,9 @@ void QuadrupleGenerator::visitEAdd(EAdd *p) {
void QuadrupleGenerator::visitERel(ERel *p) {
auto l = evalExpr(p->expr_1);
auto r = evalExpr(p->expr_2);
lastVar = alloc(l);
p->relop_->accept(this);
lastVar = alloc(l);
addQuad<QAssign>(lastVar, l, op, r);
if ((lastVar->constExpr = l->constExpr && r->constExpr)) {
if (op == Op::LT)
lastVar->val = l->val < r->val;
......@@ -216,14 +217,13 @@ void QuadrupleGenerator::visitERel(ERel *p) {
lastVar->val = l->val >= r->val;
else if (op == Op::GT)
lastVar->val = l->val > r->val;
addQuad<QAssign>(lastVar, l, op, r);
} else {
addLastVarCondJump(l, op, r);
}
}
void QuadrupleGenerator::visitEAnd(EAnd *p) {
auto labelAfter = make_shared<QLabel>("and_else");
auto labelAfter = make_shared<QLabel>("and_end");
auto l = evalJump(p->expr_1, nullptr, labelFalse);
if (!l) return;
if (l->constExpr) {
......@@ -266,7 +266,7 @@ void QuadrupleGenerator::visitEAnd(EAnd *p) {
}
void QuadrupleGenerator::visitEOr(EOr *p) {
auto labelAfter = make_shared<QLabel>("or_else");
auto labelAfter = make_shared<QLabel>("or_end");
auto l = evalJump(p->expr_1, labelTrue, nullptr);
if (!l) return;
if (l->constExpr) {
......@@ -290,19 +290,16 @@ void QuadrupleGenerator::visitEOr(EOr *p) {
lastVar = r->val ? r : l;
}
} else {
auto rightValBlock = block;
flushBasicBlock();
auto rightValBlock = flushBasicBlock();
addQuad(labelAfter);
rightValBlock->append(block);
leftValBlock->append(block);
lastVar = alloc();
if (!labelTrue || !labelFalse) {
lastVar = alloc();
block->addPhi(rightValBlock, lastVar, r);
block->addPhi(leftValBlock, lastVar, l);
} else {
lastVar = nullptr;
}
}
}
......
......@@ -108,17 +108,11 @@ private:
if (labelTrue && labelFalse) {
addQuad<QJumpCond>(labelTrue, l, op, r);
addQuad<QJump>(labelFalse);
return;
} else if (labelTrue) {
addQuad<QJumpCond>(labelTrue, l, op, r);
lastVar = alloc(0);
} else if (labelFalse) {
addQuad<QJumpCond>(labelFalse, l, op.neg(), r);
lastVar = alloc(1);
} else if (lastVar == l || lastVar == r) return;
// return needed because otherwise we get multiple lastVar assignments
// assign needed because otherwise some vars never have assignments
addQuad<QAssign>(lastVar, l, op, r);
}
}
QuadruplePtr lastQuad;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment