This commit is contained in:
ladev789
2025-05-29 19:25:46 +08:00
parent 09d67fdaf1
commit 1c799bd04f
3 changed files with 80 additions and 52 deletions

View File

@ -217,7 +217,7 @@ std::any LLVMIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext* ctx) {
} }
return nullptr; return nullptr;
} }
std::any visitAssignStmt(SysYParser::AssignStmtContext *ctx) std::any LLVMIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx)
{ {
std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this)); std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second; std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
@ -240,7 +240,7 @@ std::any visitAssignStmt(SysYParser::AssignStmtContext *ctx)
return nullptr; return nullptr;
} }
std::any visitIfStmt(SysYParser::IfStmtContext *ctx) std::any LLVMIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx)
{ {
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this)); std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
std::string trueLabel = "if.then." + std::to_string(tempCounter); std::string trueLabel = "if.then." + std::to_string(tempCounter);
@ -263,7 +263,7 @@ std::any visitIfStmt(SysYParser::IfStmtContext *ctx)
return nullptr; return nullptr;
} }
std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx) std::any LLVMIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx)
{ {
std::string loop_cond = "while.cond." + std::to_string(tempCounter); std::string loop_cond = "while.cond." + std::to_string(tempCounter);
std::string loop_body = "while.body." + std::to_string(tempCounter); std::string loop_body = "while.body." + std::to_string(tempCounter);
@ -276,7 +276,7 @@ std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx)
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this)); std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
irStream << " br i1 " << cond << ", label %" << loop_body << ", label %" << loop_end << "\n"; irStream << " br i1 " << cond << ", label %" << loop_body << ", label %" << loop_end << "\n";
irStream << loop_body << ":\n"; irStream << loop_body << ":\n";
ctx->stmt(0)->accept(this); ctx->stmt()->accept(this);
irStream << " br label %" << loop_cond << "\n"; irStream << " br label %" << loop_cond << "\n";
irStream << loop_end << ":\n"; irStream << loop_end << ":\n";
@ -284,7 +284,7 @@ std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx)
return nullptr; return nullptr;
} }
std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx) std::any LLVMIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx)
{ {
if (loopStack.empty()) { if (loopStack.empty()) {
throw std::runtime_error("Break statement outside of a loop."); throw std::runtime_error("Break statement outside of a loop.");
@ -293,7 +293,7 @@ std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx)
return nullptr; return nullptr;
} }
std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx) std::any LLVMIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext *ctx)
{ {
if (loopStack.empty()) { if (loopStack.empty()) {
throw std::runtime_error("Continue statement outside of a loop."); throw std::runtime_error("Continue statement outside of a loop.");
@ -302,6 +302,18 @@ std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx)
return nullptr; return nullptr;
} }
std::any LLVMIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx)
{
hasReturn = true;
if (ctx->exp()) {
std::string value = std::any_cast<std::string>(ctx->exp()->accept(this));
irStream << " ret " << currentReturnType << " " << value << "\n";
} else {
irStream << " ret void\n";
}
return nullptr;
}
// std::any LLVMIRGenerator::visitStmt(SysYParser::StmtContext* ctx) { // std::any LLVMIRGenerator::visitStmt(SysYParser::StmtContext* ctx) {
// if (ctx->lValue() && ctx->ASSIGN()) { // if (ctx->lValue() && ctx->ASSIGN()) {
// std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this)); // std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
@ -385,23 +397,34 @@ std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx)
std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) { std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
std::string varName = ctx->Ident()->getText(); std::string varName = ctx->Ident()->getText();
return symbolTable[varName].first; std::string allocaPtr = symbolTable[varName].first;
std::string type = symbolTable[varName].second;
std::string temp = getNextTemp();
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
tmpTable[temp] = type;
return temp;
// std::string varName = ctx->Ident()->getText();
// return symbolTable[varName].first;
} }
std::any LLVMIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) { // std::any LLVMIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
if (ctx->lValue()) { // if (ctx->lValue()) {
std::string allocaPtr = std::any_cast<std::string>(ctx->lValue()->accept(this)); // std::string allocaPtr = std::any_cast<std::string>(ctx->lValue()->accept(this));
std::string varName = ctx->lValue()->Ident()->getText(); // std::string varName = ctx->lValue()->Ident()->getText();
std::string type = symbolTable[varName].second; // std::string type = symbolTable[varName].second;
std::string temp = getNextTemp(); // std::string temp = getNextTemp();
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n"; // irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
tmpTable[temp] = type; // tmpTable[temp] = type;
return temp; // return temp;
} else if (ctx->exp()) { // } else if (ctx->exp()) {
return ctx->exp()->accept(this); // return ctx->exp()->accept(this);
} else { // } else {
return ctx->number()->accept(this); // return ctx->number()->accept(this);
} // }
// }
std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimaryExpContext* ctx) {
return visitChildren(ctx);
} }
std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) { std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
@ -413,7 +436,7 @@ std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
return ""; return "";
} }
std::any LLVMIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext* ctx) { std::any LLVMIRGenerator::visitUnExp(SysYParser::UnExpContext* ctx) {
if (ctx->unaryOp()) { if (ctx->unaryOp()) {
std::string operand = std::any_cast<std::string>(ctx->unaryExp()->accept(this)); std::string operand = std::any_cast<std::string>(ctx->unaryExp()->accept(this));
std::string op = ctx->unaryOp()->getText(); std::string op = ctx->unaryOp()->getText();
@ -426,25 +449,27 @@ std::any LLVMIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext* ctx) {
irStream << " " << temp << " = xor " << type << " " << operand << ", 1\n"; irStream << " " << temp << " = xor " << type << " " << operand << ", 1\n";
} }
return temp; return temp;
} else if (ctx->Ident()) {
std::string funcName = ctx->Ident()->getText();
std::vector<std::string> args;
if (ctx->funcRParams()) {
for (auto argCtx : ctx->funcRParams()->exp()) {
args.push_back(std::any_cast<std::string>(argCtx->accept(this)));
}
}
std::string temp = getNextTemp();
std::string argList = "";
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) argList += ", ";
argList +=tmpTable[args[i]] + " noundef " + args[i];
}
irStream << " " << temp << " = call " << currentReturnType << " @" << funcName << "(" << argList << ")\n";
tmpTable[temp] = currentReturnType;
return temp;
} }
return ctx->primaryExp()->accept(this); return ctx->unaryExp()->accept(this);
}
std::any LLVMIRGenerator::visitCall(SysYParser::CallContext *ctx)
{
std::string funcName = ctx->Ident()->getText();
std::vector<std::string> args;
if (ctx->funcRParams()) {
for (auto argCtx : ctx->funcRParams()->exp()) {
args.push_back(std::any_cast<std::string>(argCtx->accept(this)));
}
}
std::string temp = getNextTemp();
std::string argList = "";
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) argList += ", ";
argList +=tmpTable[args[i]] + " noundef " + args[i];
}
irStream << " " << temp << " = call " << currentReturnType << " @" << funcName << "(" << argList << ")\n";
tmpTable[temp] = currentReturnType;
return temp;
} }
std::any LLVMIRGenerator::visitMulExp(SysYParser::MulExpContext* ctx) { std::any LLVMIRGenerator::visitMulExp(SysYParser::MulExpContext* ctx) {

View File

@ -41,11 +41,12 @@ private:
std::any visitVarDef(SysYParser::VarDefContext* ctx); std::any visitVarDef(SysYParser::VarDefContext* ctx);
std::any visitFuncDef(SysYParser::FuncDefContext* ctx); std::any visitFuncDef(SysYParser::FuncDefContext* ctx);
std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx); std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx);
std::any visitStmt(SysYParser::StmtContext* ctx); // std::any visitStmt(SysYParser::StmtContext* ctx);
std::any visitLValue(SysYParser::LValueContext* ctx); std::any visitLValue(SysYParser::LValueContext* ctx);
std::any visitPrimaryExp(SysYParser::PrimaryExpContext* ctx); std::any visitPrimExp(SysYParser::PrimaryExpContext* ctx);
std::any visitNumber(SysYParser::NumberContext* ctx); std::any visitNumber(SysYParser::NumberContext* ctx);
std::any visitUnaryExp(SysYParser::UnaryExpContext* ctx); std::any visitCall(SysYParser::CallContext *ctx);
std::any visitUnExp(SysYParser::UnExpContext* ctx);
std::any visitMulExp(SysYParser::MulExpContext* ctx); std::any visitMulExp(SysYParser::MulExpContext* ctx);
std::any visitAddExp(SysYParser::AddExpContext* ctx); std::any visitAddExp(SysYParser::AddExpContext* ctx);
std::any visitRelExp(SysYParser::RelExpContext* ctx); std::any visitRelExp(SysYParser::RelExpContext* ctx);

View File

@ -78,24 +78,26 @@ int main(int argc, char **argv) {
} }
// visit AST to generate IR // visit AST to generate IR
SysYIRGenerator generator;
generator.visitCompUnit(moduleAST);
auto moduleIR = generator.get();
if (argStopAfter == "ir") { if (argStopAfter == "ir") {
SysYIRGenerator generator;
generator.visitCompUnit(moduleAST);
auto moduleIR = generator.get();
moduleIR->print(cout); moduleIR->print(cout);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else if (argStopAfter == "llvmir") { } else if (argStopAfter == "llvmir") {
LLVMIRGenerator llvmirGenerator; LLVMIRGenerator llvmirGenerator;
llvmirGenerator.generateIR(moduleAST); // 使用公共接口生成 IR
cout << llvmirGenerator.getIR(); cout << llvmirGenerator.getIR();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
// generate assembly // // generate assembly
CodeGen codegen(moduleIR); // CodeGen codegen(moduleIR);
string asmCode = codegen.code_gen(); // string asmCode = codegen.code_gen();
cout << asmCode << endl; // cout << asmCode << endl;
if (argStopAfter == "asm") // if (argStopAfter == "asm")
return EXIT_SUCCESS; // return EXIT_SUCCESS;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }