暂存旧符号表结构定义,TODO.md中添加相关说明
This commit is contained in:
1
TODO.md
1
TODO.md
@@ -8,6 +8,7 @@
|
|||||||
- `IRBuilder`:构建指令和基本块的工具类(你们正在实现的部分)
|
- `IRBuilder`:构建指令和基本块的工具类(你们正在实现的部分)
|
||||||
|
|
||||||
### 2. **中端必要优化(最小集合)**
|
### 2. **中端必要优化(最小集合)**
|
||||||
|
常量传播
|
||||||
| 优化阶段 | 关键作用 | 是否必须 |
|
| 优化阶段 | 关键作用 | 是否必须 |
|
||||||
|-------------------|----------------------------------|----------|
|
|-------------------|----------------------------------|----------|
|
||||||
| `Mem2Reg` | 消除冗余内存访问,转换为SSA形式 | ✅ 核心 |
|
| `Mem2Reg` | 消除冗余内存访问,转换为SSA形式 | ✅ 核心 |
|
||||||
|
|||||||
62
olddef.h
Normal file
62
olddef.h
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
class SymbolTable{
|
||||||
|
private:
|
||||||
|
enum Kind
|
||||||
|
{
|
||||||
|
kModule,
|
||||||
|
kFunction,
|
||||||
|
kBlock,
|
||||||
|
};
|
||||||
|
|
||||||
|
std::forward_list<std::pair<Kind, std::unordered_map<std::string, Value*>>> Scopes;
|
||||||
|
|
||||||
|
public:
|
||||||
|
struct ModuleScope {
|
||||||
|
SymbolTable& tables_ref;
|
||||||
|
ModuleScope(SymbolTable& tables) : tables_ref(tables) {
|
||||||
|
tables.enter(kModule);
|
||||||
|
}
|
||||||
|
~ModuleScope() { tables_ref.exit(); }
|
||||||
|
};
|
||||||
|
struct FunctionScope {
|
||||||
|
SymbolTable& tables_ref;
|
||||||
|
FunctionScope(SymbolTable& tables) : tables_ref(tables) {
|
||||||
|
tables.enter(kFunction);
|
||||||
|
}
|
||||||
|
~FunctionScope() { tables_ref.exit(); }
|
||||||
|
};
|
||||||
|
struct BlockScope {
|
||||||
|
SymbolTable& tables_ref;
|
||||||
|
BlockScope(SymbolTable& tables) : tables_ref(tables) {
|
||||||
|
tables.enter(kBlock);
|
||||||
|
}
|
||||||
|
~BlockScope() { tables_ref.exit(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
SymbolTable() = default;
|
||||||
|
|
||||||
|
bool isModuleScope() const { return Scopes.front().first == kModule; }
|
||||||
|
bool isFunctionScope() const { return Scopes.front().first == kFunction; }
|
||||||
|
bool isBlockScope() const { return Scopes.front().first == kBlock; }
|
||||||
|
Value *lookup(const std::string &name) const {
|
||||||
|
for (auto &scope : Scopes) {
|
||||||
|
auto iter = scope.second.find(name);
|
||||||
|
if (iter != scope.second.end())
|
||||||
|
return iter->second;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto insert(const std::string &name, Value *value) {
|
||||||
|
assert(not Scopes.empty());
|
||||||
|
return Scopes.front().second.emplace(name, value);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
void enter(Kind kind) {
|
||||||
|
Scopes.emplace_front();
|
||||||
|
Scopes.front().first = kind;
|
||||||
|
}
|
||||||
|
void exit() {
|
||||||
|
Scopes.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user