diff --git a/TODO.md b/TODO.md index c386b0d..aae2f4e 100644 --- a/TODO.md +++ b/TODO.md @@ -8,6 +8,7 @@ - `IRBuilder`:构建指令和基本块的工具类(你们正在实现的部分) ### 2. **中端必要优化(最小集合)** +常量传播 | 优化阶段 | 关键作用 | 是否必须 | |-------------------|----------------------------------|----------| | `Mem2Reg` | 消除冗余内存访问,转换为SSA形式 | ✅ 核心 | diff --git a/olddef.h b/olddef.h new file mode 100644 index 0000000..c7b9522 --- /dev/null +++ b/olddef.h @@ -0,0 +1,62 @@ + +class SymbolTable{ +private: + enum Kind + { + kModule, + kFunction, + kBlock, + }; + + std::forward_list>> 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(); + } + +}; \ No newline at end of file