Commit 69a63946 authored by zygzagZ's avatar zygzagZ

Fix jump code generation

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