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