[lab2]some bugs fixed

This commit is contained in:
Lixuanwang
2025-03-19 20:59:50 +08:00
parent 57ccf6f0e3
commit ed44877164

View File

@ -1,3 +1,4 @@
// SysYIRGenerator.cpp
#include "SysYIRGenerator.h"
#include <iomanip>
@ -32,10 +33,9 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
std::string type = ctx->bType()->getText();
for (auto constDef : ctx->constDef()) {
std::string varName = constDef->Ident()->getText();
symbolTable[varName].second = type;
std::string llvmType = getLLVMType(type);
std::string allocaName = getNextTemp();
symbolTable[varName] = {allocaName, llvmType};
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
if (constDef->constInitVal()) {
@ -43,6 +43,7 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
}
symbolTable[varName] = {allocaName, llvmType};
}
return nullptr;
}
@ -50,7 +51,6 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext* ctx) {
std::string type = ctx->bType()->getText();
for (auto varDef : ctx->varDef()) {
symbolTable[varDef->Ident()->getText()].second = type;
varDef->accept(this);
}
return nullptr;
@ -61,7 +61,7 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext* ctx) {
std::string type = symbolTable[varName].second;
std::string llvmType = getLLVMType(type);
std::string allocaName = getNextTemp();
symbolTable[varName] = {allocaName, llvmType};
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
if (ctx->ASSIGN()) {
@ -69,6 +69,7 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext* ctx) {
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
}
symbolTable[varName] = {allocaName, llvmType};
return nullptr;
}
@ -86,18 +87,10 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx) {
if (i > 0) irStream << ", ";
std::string paramType = getLLVMType(params[i]->bType()->getText());
irStream << paramType << " noundef %" << i;
symbolTable[params[i]->Ident()->getText()] = {"%" + std::to_string(i), paramType};
}
}
irStream << ") #0 {\nentry:\n";
if (ctx->funcFParams()) {
auto params = ctx->funcFParams()->funcFParam();
for (size_t i = 0; i < params.size(); ++i) {
std::string paramName = params[i]->Ident()->getText();
std::string paramType = getLLVMType(params[i]->bType()->getText());
symbolTable[paramName] = {"%" + std::to_string(i), paramType};
}
}
irStream << ") #0 {\n";
ctx->blockStmt()->accept(this);
@ -108,7 +101,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx) {
irStream << " ret " << currentReturnType << " 0\n";
}
}
irStream << "}\n\n";
irStream << "}\n";
return nullptr;
}
@ -122,8 +115,7 @@ std::any SysYIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext* ctx) {
std::any SysYIRGenerator::visitStmt(SysYParser::StmtContext* ctx) {
if (ctx->lValue() && ctx->ASSIGN()) {
std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
std::string varName = ctx->lValue()->Ident()->getText();
std::string lhsType = symbolTable[varName].second;
std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
std::string rhs = std::any_cast<std::string>(ctx->exp()->accept(this));
irStream << " store " << lhsType << " " << rhs << ", " << lhsType
<< "* " << lhsAlloca << ", align 4\n";
@ -141,17 +133,17 @@ std::any SysYIRGenerator::visitStmt(SysYParser::StmtContext* ctx) {
std::string falseLabel = "if.else." + std::to_string(tempCounter);
std::string mergeLabel = "if.end." + std::to_string(tempCounter++);
irStream << " br i1 " << cond << ", label %" << trueLabel << ", label %" << falseLabel << "\n\n";
irStream << " br i1 " << cond << ", label %" << trueLabel << ", label %" << falseLabel << "\n";
irStream << trueLabel << ":\n";
ctx->stmt(0)->accept(this);
irStream << " br label %" << mergeLabel << "\n\n";
irStream << " br label %" << mergeLabel << "\n";
irStream << falseLabel << ":\n";
if (ctx->ELSE()) {
ctx->stmt(1)->accept(this);
}
irStream << " br label %" << mergeLabel << "\n\n";
irStream << " br label %" << mergeLabel << "\n";
irStream << mergeLabel << ":\n";
}
@ -207,14 +199,12 @@ std::any SysYIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext* ctx) {
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 += args[i];
}
irStream << " " << temp << " = call " << currentReturnType << " @" << funcName << "(" << argList << ")\n";
return temp;
}
@ -307,14 +297,11 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
std::string endLabel = "land.end." + std::to_string(tempCounter++);
std::string temp = getNextTemp();
irStream << " " << temp << " = and i1 " << left << ", 1\n";
irStream << " br i1 " << temp << ", label %" << falseLabel << ", label %" << endLabel << "\n\n";
irStream << " br label %" << falseLabel << "\n";
irStream << falseLabel << ":\n";
std::string right = std::any_cast<std::string>(eqExps[i]->accept(this));
irStream << " " << temp << " = and i1 " << temp << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n\n";
irStream << " " << temp << " = and i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
left = temp;
}
@ -329,14 +316,11 @@ std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
std::string endLabel = "lor.end." + std::to_string(tempCounter++);
std::string temp = getNextTemp();
irStream << " " << temp << " = or i1 " << left << ", 0\n";
irStream << " br i1 " << temp << ", label %" << trueLabel << ", label %" << endLabel << "\n\n";
irStream << " br label %" << trueLabel << "\n";
irStream << trueLabel << ":\n";
std::string right = std::any_cast<std::string>(lAndExps[i]->accept(this));
irStream << " " << temp << " = or i1 " << temp << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n\n";
irStream << " " << temp << " = or i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
left = temp;
}