Commit 726a943e authored by zygzagZ's avatar zygzagZ

Quad refactor

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