更改g4文件,优化IR生成流程

This commit is contained in:
rain2133
2025-06-21 13:44:51 +08:00
parent 30f89bba23
commit c1583e447d
2 changed files with 85 additions and 86 deletions

View File

@ -101,7 +101,10 @@ BLOCKCOMMENT: '/*' .*? '*/' -> skip;
// CompUnit: (CompUnit)? (decl |funcDef);
compUnit: (decl |funcDef)+;
compUnit: (globalDecl |funcDef)+;
globalDecl: constDecl # globalConstDecl
| varDecl # globalVarDecl;
decl: constDecl | varDecl;
@ -111,16 +114,16 @@ bType: INT | FLOAT;
constDef: Ident (LBRACK constExp RBRACK)* ASSIGN constInitVal;
constInitVal: constExp
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE;
constInitVal: constExp # constScalarInitValue
| LBRACE (constInitVal (COMMA constInitVal)*)? RBRACE # constArrayInitValue;
varDecl: bType varDef (COMMA varDef)* SEMICOLON;
varDef: Ident (LBRACK constExp RBRACK)*
| Ident (LBRACK constExp RBRACK)* ASSIGN initVal;
initVal: exp
| LBRACE (initVal (COMMA initVal)*)? RBRACE;
initVal: exp # scalarInitValue
| LBRACE (initVal (COMMA initVal)*)? RBRACE # arrayInitValue;
funcType: VOID | INT | FLOAT;

View File

@ -9,130 +9,126 @@
namespace sysy {
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();
}
};
class SysYIRGenerator : public SysYBaseVisitor {
// @brief 用于存储数组值的树结构
// 多位数组本质上是一维数组的嵌套可以用树来表示。
class ArrayValueTree {
private:
Value *value = nullptr; /// 该节点存储的value
std::vector<std::unique_ptr<ArrayValueTree>> children; /// 子节点列表
public:
ArrayValueTree() = default;
public:
auto getValue() const -> Value * { return value; }
auto getChildren() const
-> const std::vector<std::unique_ptr<ArrayValueTree>> & {
return children;
}
void setValue(Value *newValue) { value = newValue; }
void addChild(ArrayValueTree *newChild) { children.emplace_back(newChild); }
void addChildren(const std::vector<ArrayValueTree *> &newChildren) {
for (const auto &child : newChildren) {
children.emplace_back(child);
}
}
};
class Utils {
public:
// transform a tree of ArrayValueTree to a ValueCounter
static void tree2Array(Type *type, ArrayValueTree *root,
const std::vector<Value *> &dims, unsigned numDims,
ValueCounter &result, IRBuilder *builder);
static void
createExternalFunction(const std::vector<Type *> &paramTypes,
const std::vector<std::string> &paramNames,
const std::vector<std::vector<Value *>> &paramDims,
Type *returnType, const std::string &funcName,
Module *pModule, IRBuilder *pBuilder);
static void initExternalFunction(Module *pModule, IRBuilder *pBuilder);
};
private:
std::unique_ptr<Module> module;
IRBuilder builder;
SymbolTable symbols_table;
int trueBlockNum = 0, falseBlockNum = 0;
int d = 0, n = 0;
vector<int> path;
bool isalloca;
AllocaInst *current_alloca;
GlobalValue *current_global;
Type* current_type;
int numdims = 0;
public:
SysYIRGenerator() = default;
public:
Module *get() const { return module.get(); }
IRBuilder *getBuilder(){ return &builder; }
public:
std::any visitCompUnit(SysYParser::CompUnitContext *ctx) override;
std::any visitDecl(SysYParser::DeclContext *ctx) override;
std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *ctx) override;
std::any visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *ctx) override;
std::any visitDecl(SysYParser::DeclContext *ctx) override ;
std::any visitConstDecl(SysYParser::ConstDeclContext *ctx) override;
std::any visitVarDecl(SysYParser::VarDeclContext *ctx) override;
std::any visitBType(SysYParser::BTypeContext *ctx) override;
std::any visitConstDef(SysYParser::ConstDefContext *ctx) override;
std::any visitConstInitVal(SysYParser::ConstInitValContext *ctx) override;
// std::any visitConstDef(SysYParser::ConstDefContext *ctx) override;
// std::any visitVarDef(SysYParser::VarDefContext *ctx) override;
std::any visitScalarInitValue(SysYParser::ScalarInitValueContext *ctx) override;
std::any visitArrayInitValue(SysYParser::ArrayInitValueContext *ctx) override;
std::any visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *ctx) override;
std::any visitConstArrayInitValue(SysYParser::ConstArrayInitValueContext *ctx) override;
// std::any visitConstInitVal(SysYParser::ConstInitValContext *ctx) override;
std::any visitFuncType(SysYParser::FuncTypeContext* ctx) override;
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override;
std::any visitVarDecl(SysYParser::VarDeclContext *ctx) override;
std::any visitVarDef(SysYParser::VarDefContext *ctx) override;
std::any visitInitVal(SysYParser::InitValContext *ctx) override;
// std::any visitInitVal(SysYParser::InitValContext *ctx) override;
// std::any visitFuncFParam(SysYParser::FuncFParamContext *ctx) override;
// std::any visitFuncFParams(SysYParser::FuncFParamsContext *ctx) override;
std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx) override;
// std::any visitStmt(SysYParser::StmtContext *ctx) override;
std::any visitAssignStmt(SysYParser::AssignStmtContext *ctx) override;
// std::any visitExpStmt(SysYParser::ExpStmtContext *ctx) override;
// std::any visitBlkStmt(SysYParser::BlkStmtContext *ctx) override;
std::any visitIfStmt(SysYParser::IfStmtContext *ctx) override;
std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx) override;
std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx) override;
std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx) override;
std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override;
// std::any visitExp(SysYParser::ExpContext *ctx) override;
// std::any visitCond(SysYParser::CondContext *ctx) override;
std::any visitLValue(SysYParser::LValueContext *ctx) override;
std::any visitPrimExp(SysYParser::PrimExpContext *ctx) override;
// std::any visitParenExp(SysYParser::ParenExpContext *ctx) override;
std::any visitNumber(SysYParser::NumberContext *ctx) override;
// std::any visitString(SysYParser::StringContext *ctx) override;
std::any visitCall(SysYParser::CallContext *ctx) override;
// std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override;
// std::any visitUnaryOp(SysYParser::UnaryOpContext *ctx) override;
std::any visitUnExp(SysYParser::UnExpContext *ctx) override;
// std::any visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override;
std::any visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override;
std::any visitMulExp(SysYParser::MulExpContext *ctx) override;
std::any visitAddExp(SysYParser::AddExpContext *ctx) override;
std::any visitRelExp(SysYParser::RelExpContext *ctx) override;
std::any visitEqExp(SysYParser::EqExpContext *ctx) override;
std::any visitLAndExp(SysYParser::LAndExpContext *ctx) override;
std::any visitLOrExp(SysYParser::LOrExpContext *ctx) override;
std::any visitConstExp(SysYParser::ConstExpContext *ctx) override;
// std::any visitConstExp(SysYParser::ConstExpContext *ctx) override;
}; // class SysYIRGenerator