Commit 726a943e authored by zygzagZ's avatar zygzagZ

Quad refactor

parent 5ea4aec5
...@@ -59,6 +59,8 @@ void Compiler::compileFunction(const FunctionInfoPtr& f) { ...@@ -59,6 +59,8 @@ void Compiler::compileFunction(const FunctionInfoPtr& f) {
RegisterAllocator regGen(quadEnv); RegisterAllocator regGen(quadEnv);
regGen.allocate();
for (const auto& b : quadEnv.blocks) { for (const auto& b : quadEnv.blocks) {
for (const auto& q : b->quads) { for (const auto& q : b->quads) {
buf << q->toString() << endl; buf << q->toString() << endl;
......
...@@ -46,7 +46,13 @@ public: ...@@ -46,7 +46,13 @@ public:
virtual std::string toString() const { return to_string(lineno) + "\t"; }; virtual std::string toString() const { return to_string(lineno) + "\t"; };
virtual bool isFinal() const { return false; } virtual bool isFinal() const { return false; }
virtual void useVariables() {}; virtual void useVariables() {
auto self = shared_from_this();
for (const auto& v : vars()) {
if (v) v->uses.emplace_back(self);
}
};
virtual vector<VariablePtr> vars() const { return {}; };
}; };
class QWriteVar : public Quadruple { class QWriteVar : public Quadruple {
...@@ -56,6 +62,7 @@ public: ...@@ -56,6 +62,7 @@ public:
std::string toString() const override { return Quadruple::toString() + "\t" + (loc ? (loc->name + " := ") : ""); } std::string toString() const override { return Quadruple::toString() + "\t" + (loc ? (loc->name + " := ") : ""); }
void useVariables() override { void useVariables() override {
Quadruple::useVariables();
if (loc) loc->writes.emplace_back(shared_from_this()); if (loc) loc->writes.emplace_back(shared_from_this());
} }
}; };
...@@ -82,10 +89,9 @@ public: ...@@ -82,10 +89,9 @@ public:
else else
return QWriteVar::toString() + args[0]->name + " " + Op::name(op) + " " + args[1]->name; return QWriteVar::toString() + args[0]->name + " " + Op::name(op) + " " + args[1]->name;
} }
void useVariables() override {
QWriteVar::useVariables(); vector<VariablePtr> vars() const override {
auto self = shared_from_this(); return args;
for (const auto& a : args) if (a) a->uses.emplace_back(self);
} }
}; };
...@@ -121,9 +127,11 @@ public: ...@@ -121,9 +127,11 @@ public:
bool isFinal() const override { return true; } bool isFinal() const override { return true; }
void useVariables() override { vector<VariablePtr> vars() const override {
if (left) left->uses.emplace_back(shared_from_this()); auto ret = QJump::vars();
if (right) right->uses.emplace_back(shared_from_this()); if (left) ret.emplace_back(left);
if (right) ret.emplace_back(right);
return ret;
} }
}; };
...@@ -144,8 +152,10 @@ public: ...@@ -144,8 +152,10 @@ public:
} }
} }
void useVariables() override { vector<VariablePtr> vars() const override {
if (param) param->uses.emplace_back(shared_from_this()); auto ret = Quadruple::vars();
if (param) ret.emplace_back(param);
return ret;
} }
}; };
...@@ -165,20 +175,21 @@ public: ...@@ -165,20 +175,21 @@ public:
} }
} }
void useVariables() override { vector<VariablePtr> vars() const override {
QWriteVar::useVariables(); auto ret = QWriteVar::vars();
if (self) self->uses.emplace_back(shared_from_this()); if (self) ret.emplace_back(self);
return ret;
} }
}; };
class QReturn : public Quadruple { class QReturn : public Quadruple {
public: public:
VariablePtr ret; VariablePtr val;
explicit QReturn(VariablePtr ret) : ret(std::move(ret)) {}; explicit QReturn(VariablePtr ret) : val(std::move(ret)) {};
std::string toString() const override { std::string toString() const override {
if (ret) { if (val) {
return Quadruple::toString() + "\treturn " + ret->name; return Quadruple::toString() + "\treturn " + val->name;
} else { } else {
return Quadruple::toString() + "\treturn"; return Quadruple::toString() + "\treturn";
} }
...@@ -186,8 +197,10 @@ public: ...@@ -186,8 +197,10 @@ public:
bool isFinal() const override { return true; } bool isFinal() const override { return true; }
void useVariables() override { vector<VariablePtr> vars() const override {
if (ret) ret->uses.emplace_back(shared_from_this()); auto ret = Quadruple::vars();
if (val) ret.emplace_back(val);
return ret;
} }
}; };
...@@ -205,10 +218,11 @@ public: ...@@ -205,10 +218,11 @@ public:
return ret + access.toString(); return ret + access.toString();
} }
void useVariables() override { vector<VariablePtr> vars() const override {
QWriteVar::useVariables(); auto ret = QWriteVar::vars();
if (access.base) access.base->uses.emplace_back(shared_from_this()); if (access.base) ret.emplace_back(access.base);
if (access.index) access.index->uses.emplace_back(shared_from_this()); if (access.index) ret.emplace_back(access.index);
return ret;
} }
}; };
...@@ -222,10 +236,12 @@ public: ...@@ -222,10 +236,12 @@ public:
std::string toString() const override { return Quadruple::toString() + "\t" + loc.toString() + " := " + val->name; } std::string toString() const override { return Quadruple::toString() + "\t" + loc.toString() + " := " + val->name; }
void useVariables() override { vector<VariablePtr> vars() const override {
if (loc.base) loc.base->uses.emplace_back(shared_from_this()); auto ret = Quadruple::vars();
if (loc.index) loc.index->uses.emplace_back(shared_from_this()); if (loc.base) ret.emplace_back(loc.base);
if (val) val->uses.emplace_back(shared_from_this()); if (loc.index) ret.emplace_back(loc.index);
if (val) ret.emplace_back(val);
return ret;
} }
}; };
...@@ -244,9 +260,10 @@ public: ...@@ -244,9 +260,10 @@ public:
return ret; return ret;
} }
void useVariables() override { vector<VariablePtr> vars() const override {
QWriteVar::useVariables(); auto ret = QWriteVar::vars();
if (count) count->uses.emplace_back(shared_from_this()); if (count) ret.emplace_back(count);
return ret;
} }
}; };
......
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
#include "setOverloads.h" #include "setOverloads.h"
void RegisterAllocator::analyseLive() { void RegisterAllocator::analyseLive() {
for (auto v : vars) { for (const auto& v : vars) {
for (auto q : v->uses) { for (const auto& q : v->uses) {
q->block->flow.use.emplace(v); q->block->flow.use.emplace(v);
} }
for (auto q : v->writes) { for (const auto& q : v->writes) {
q->block->flow.def.emplace(v); q->block->flow.def.emplace(v);
} }
} }
for (const auto& b : blocks) { for (const auto& b : blocks) {
for (const auto& phiMap : b->phi) { for (const auto& phiMap : b->phi) {
for (const auto& phiRow : phiMap) { for (const auto& phiRow : phiMap) {
...@@ -35,4 +36,8 @@ void RegisterAllocator::analyseLive() { ...@@ -35,4 +36,8 @@ void RegisterAllocator::analyseLive() {
} }
if (!changed) break; if (!changed) break;
} }
}
void RegisterAllocator::allocate() {
analyseLive();
} }
\ No newline at end of file
...@@ -14,7 +14,7 @@ class Register : public std::enable_shared_from_this<Register> { ...@@ -14,7 +14,7 @@ class Register : public std::enable_shared_from_this<Register> {
class RegisterAllocator { class RegisterAllocator {
public: public:
RegisterAllocator(const QuadrupleGenerator::Result &r) : blocks(r.blocks), vars(r.vars) {} explicit RegisterAllocator(const QuadrupleGenerator::Result &r) : blocks(r.blocks), vars(r.vars) {}
void processQJump(shared_ptr<QJump> q); void processQJump(shared_ptr<QJump> q);
void processQJumpCond(shared_ptr<QJumpCond> q); void processQJumpCond(shared_ptr<QJumpCond> q);
void processQLabel(shared_ptr<QLabel> q); void processQLabel(shared_ptr<QLabel> q);
...@@ -26,8 +26,9 @@ public: ...@@ -26,8 +26,9 @@ public:
void processQAssign(shared_ptr<QAssign> q); void processQAssign(shared_ptr<QAssign> q);
void processQCall(shared_ptr<QCall> q); void processQCall(shared_ptr<QCall> q);
void analyseLive(); void allocate();
private: private:
void analyseLive();
vector<BasicBlockPtr> blocks; vector<BasicBlockPtr> blocks;
vector<VariablePtr> vars; vector<VariablePtr> vars;
}; };
......
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