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) { ...@@ -406,6 +406,8 @@ void Compiler::generatePhiXCHG(const map<VariablePtr, VariablePtr> &varMap) {
void Compiler::generateQJump(QJump &q) { void Compiler::generateQJump(QJump &q) {
auto thisBlock = q.block; auto thisBlock = q.block;
auto tgBlock = q.target->block; auto tgBlock = q.target->block;
assert(thisBlock && tgBlock);
thisBlock->append(tgBlock);
const auto &map = tgBlock->getPhiMapping(thisBlock); const auto &map = tgBlock->getPhiMapping(thisBlock);
generatePhiXCHG(map); generatePhiXCHG(map);
append("JMP", getBlockLabelText(q.target)); append("JMP", getBlockLabelText(q.target));
...@@ -435,6 +437,7 @@ void Compiler::generateQJumpCond(QJumpCond &q) { ...@@ -435,6 +437,7 @@ void Compiler::generateQJumpCond(QJumpCond &q) {
auto thisBlock = q.block; auto thisBlock = q.block;
auto tgBlock = q.target->block; auto tgBlock = q.target->block;
assert(thisBlock && tgBlock); assert(thisBlock && tgBlock);
thisBlock->append(tgBlock);
const auto &map = tgBlock->getPhiMapping(thisBlock); const auto &map = tgBlock->getPhiMapping(thisBlock);
for (const auto &phiVars : map) { for (const auto &phiVars : map) {
if ((phiVars.first->info && phiVars.second->info && phiVars.first->info != phiVars.second->info) || if ((phiVars.first->info && phiVars.second->info && phiVars.first->info != phiVars.second->info) ||
......
...@@ -24,6 +24,7 @@ public: ...@@ -24,6 +24,7 @@ public:
shared_ptr<QPhi> phi; shared_ptr<QPhi> phi;
void append(const BasicBlockPtr& after) { void append(const BasicBlockPtr& after) {
assert(after); assert(after);
if (find(out.begin(), out.end(), after) != out.end()) return;
out.push_back(after); out.push_back(after);
after->in.push_back(shared_from_this()); after->in.push_back(shared_from_this());
after->phi->phi.emplace_back(); after->phi->phi.emplace_back();
......
...@@ -201,8 +201,9 @@ void QuadrupleGenerator::visitEAdd(EAdd *p) { ...@@ -201,8 +201,9 @@ void QuadrupleGenerator::visitEAdd(EAdd *p) {
void QuadrupleGenerator::visitERel(ERel *p) { void QuadrupleGenerator::visitERel(ERel *p) {
auto l = evalExpr(p->expr_1); auto l = evalExpr(p->expr_1);
auto r = evalExpr(p->expr_2); auto r = evalExpr(p->expr_2);
lastVar = alloc(l);
p->relop_->accept(this); p->relop_->accept(this);
lastVar = alloc(l);
addQuad<QAssign>(lastVar, l, op, r);
if ((lastVar->constExpr = l->constExpr && r->constExpr)) { if ((lastVar->constExpr = l->constExpr && r->constExpr)) {
if (op == Op::LT) if (op == Op::LT)
lastVar->val = l->val < r->val; lastVar->val = l->val < r->val;
...@@ -216,14 +217,13 @@ void QuadrupleGenerator::visitERel(ERel *p) { ...@@ -216,14 +217,13 @@ void QuadrupleGenerator::visitERel(ERel *p) {
lastVar->val = l->val >= r->val; lastVar->val = l->val >= r->val;
else if (op == Op::GT) else if (op == Op::GT)
lastVar->val = l->val > r->val; lastVar->val = l->val > r->val;
addQuad<QAssign>(lastVar, l, op, r);
} else { } else {
addLastVarCondJump(l, op, r); addLastVarCondJump(l, op, r);
} }
} }
void QuadrupleGenerator::visitEAnd(EAnd *p) { 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); auto l = evalJump(p->expr_1, nullptr, labelFalse);
if (!l) return; if (!l) return;
if (l->constExpr) { if (l->constExpr) {
...@@ -266,7 +266,7 @@ void QuadrupleGenerator::visitEAnd(EAnd *p) { ...@@ -266,7 +266,7 @@ void QuadrupleGenerator::visitEAnd(EAnd *p) {
} }
void QuadrupleGenerator::visitEOr(EOr *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); auto l = evalJump(p->expr_1, labelTrue, nullptr);
if (!l) return; if (!l) return;
if (l->constExpr) { if (l->constExpr) {
...@@ -290,19 +290,16 @@ void QuadrupleGenerator::visitEOr(EOr *p) { ...@@ -290,19 +290,16 @@ void QuadrupleGenerator::visitEOr(EOr *p) {
lastVar = r->val ? r : l; lastVar = r->val ? r : l;
} }
} else { } else {
auto rightValBlock = block; auto rightValBlock = flushBasicBlock();
flushBasicBlock();
addQuad(labelAfter); addQuad(labelAfter);
rightValBlock->append(block); rightValBlock->append(block);
leftValBlock->append(block); leftValBlock->append(block);
lastVar = alloc();
if (!labelTrue || !labelFalse) { if (!labelTrue || !labelFalse) {
lastVar = alloc();
block->addPhi(rightValBlock, lastVar, r); block->addPhi(rightValBlock, lastVar, r);
block->addPhi(leftValBlock, lastVar, l); block->addPhi(leftValBlock, lastVar, l);
} else {
lastVar = nullptr;
} }
} }
} }
......
...@@ -108,17 +108,11 @@ private: ...@@ -108,17 +108,11 @@ private:
if (labelTrue && labelFalse) { if (labelTrue && labelFalse) {
addQuad<QJumpCond>(labelTrue, l, op, r); addQuad<QJumpCond>(labelTrue, l, op, r);
addQuad<QJump>(labelFalse); addQuad<QJump>(labelFalse);
return;
} else if (labelTrue) { } else if (labelTrue) {
addQuad<QJumpCond>(labelTrue, l, op, r); addQuad<QJumpCond>(labelTrue, l, op, r);
lastVar = alloc(0);
} else if (labelFalse) { } else if (labelFalse) {
addQuad<QJumpCond>(labelFalse, l, op.neg(), r); 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; 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