Commit 69a63946 authored by zygzagZ's avatar zygzagZ

Fix jump code generation

parent b00ae3ce
......@@ -144,9 +144,7 @@ void QuadrupleGenerator::visitNeg(Neg *p) {
}
void QuadrupleGenerator::visitNot(Not *p) {
swap(labelTrue, labelFalse);
auto var = evalExpr(p->expr_);
swap(labelTrue, labelFalse);
auto var = evalJump(p->expr_, labelFalse, labelTrue);
lastVar = alloc();
addQuad<QAssign>(lastVar, Op::Not, var);
if ((lastVar->constExpr = var->constExpr)) {
......@@ -187,8 +185,8 @@ void QuadrupleGenerator::visitEAdd(EAdd *p) {
}
void QuadrupleGenerator::visitERel(ERel *p) {
auto l = evalJump(p->expr_1, nullptr, nullptr);
auto r = evalJump(p->expr_2, nullptr, nullptr);
auto l = evalExpr(p->expr_1);
auto r = evalExpr(p->expr_2);
lastVar = alloc();
p->relop_->accept(this);
if ((lastVar->constExpr = l->constExpr && r->constExpr)) {
......@@ -268,7 +266,7 @@ void QuadrupleGenerator::visitEOr(EOr *p) {
flushBasicBlock();
leftValBlock->append(block);
auto r = evalExpr(p->expr_2);
auto r = evalJump(p->expr_2, labelTrue, labelFalse);
if (!r) return;
if (r->constExpr) {
......@@ -327,7 +325,7 @@ void QuadrupleGenerator::visitEIndexAcc(EIndexAcc *p) {
}
void QuadrupleGenerator::visitEClsMmbr(EClsMmbr *p) {
auto l = evalExpr(p->expr_);
auto l = evalJump(p->expr_, labelTrue, labelFalse);
auto var = p->pident_->var.lock();
assert(var);
// it cannot be function, as function might only be used in EApp and are handled directly there
......@@ -457,6 +455,8 @@ void QuadrupleGenerator::assign(Expr* lval, VariablePtr val) {
} else {
// local variable - only assign
addQuad<QAssign>(dest, Op::Copy, val);
if ((dest->constExpr = val->constExpr))
dest->val = val->val;
}
}
......@@ -637,4 +637,4 @@ void QuadrupleGenerator::visitForEach(ForEach *p) {
auto tab = evalExpr(p->expr_);
// throw ParseError("ForEach is yet unimplemented!", p);
// TODO: implement
}
\ No newline at end of file
}
......@@ -41,30 +41,27 @@ private:
return alloc(info);
}
VariablePtr evalExpr(Visitable *expr) {
VariablePtr evalJump(Visitable *expr, shared_ptr<QLabel> tLabel, shared_ptr<QLabel> fLabel) {
if (!expr) throw runtime_error("No expr to eval");
lineno = expr->lineno;
lastVar = nullptr;
swap(labelTrue, tLabel);
swap(labelFalse ,fLabel);
try {
expr->accept(this);
} catch (const ParseError &err) {
throw ParseError(err, expr);
}
if (!lastVar) throw ParseError("No variable found", expr);
swap(labelTrue, tLabel);
swap(labelFalse ,fLabel);
auto ret = lastVar;
lastVar = nullptr;
return ret;
}
VariablePtr evalJump(Visitable *expr, shared_ptr<QLabel> tLabel, shared_ptr<QLabel> fLabel) {
if (!expr) throw runtime_error("No expr to eval");
lineno = expr->lineno;
swap(labelTrue, tLabel);
swap(labelFalse ,fLabel);
auto ret = evalExpr(expr);
swap(labelTrue, tLabel);
swap(labelFalse ,fLabel);
return ret;
inline VariablePtr evalExpr(Visitable *expr) {
return evalJump(expr, nullptr, nullptr);
}
VariablePtr alloc(const VarInfoPtr& info) {
......
......@@ -10,7 +10,7 @@
class Variable : std::enable_shared_from_this<Variable> {
public:
Variable() = default;
explicit Variable(VarInfoPtr info) : info(std::move(info)) {};
explicit Variable(VarInfoPtr i) : info(std::move(i)), type(info->type) {};
explicit Variable(int constVal) : type(new Int), constExpr(true), val(constVal) {};
explicit Variable(std::string symbolName) : name(std::move(symbolName)) {};
VarInfoPtr info;
......
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