[deploy]把Antlr生成的代码直接放到源代码文件夹中

This commit is contained in:
LixuanWang
2025-07-16 10:32:18 +08:00
parent b08b7d9fa6
commit bb1602aa5f
19 changed files with 5314 additions and 3067 deletions

View File

@ -1,11 +1,11 @@
# Generate lexer and parser with ANTLR
list(APPEND CMAKE_MODULE_PATH "${ANTLR_RUNTIME}/cmake")
include(FindANTLR)
antlr_target(SysYGen SysY.g4
LEXER PARSER
OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VISITOR
)
# list(APPEND CMAKE_MODULE_PATH "${ANTLR_RUNTIME}/cmake")
# include(FindANTLR)
# antlr_target(SysYGen SysY.g4
# LEXER PARSER
# OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
# VISITOR
# )
add_library(SysYParser SHARED ${ANTLR_SysYGen_CXX_OUTPUTS})
target_include_directories(SysYParser PUBLIC ${ANTLR_RUNTIME}/runtime/src)
@ -13,9 +13,11 @@ target_link_libraries(SysYParser PUBLIC antlr4_shared)
add_executable(sysyc
sysyc.cpp
SysYLexer.cpp
SysYParser.cpp
SysYVisitor.cpp
IR.cpp
SysYIRGenerator.cpp
# Backend.cpp
SysYIRPrinter.cpp
SysYIROptPre.cpp
SysYIRAnalyser.cpp
@ -24,6 +26,7 @@ add_executable(sysyc
Reg2Mem.cpp
RISCv64Backend.cpp
)
target_include_directories(sysyc PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_options(sysyc PRIVATE -frtti)
target_link_libraries(sysyc PRIVATE SysYParser)

View File

@ -1,674 +0,0 @@
// LLVMIRGenerator.cpp
// TODO类型转换及其检查
// TODOsysy库函数处理
// TODO数组处理
// TODO对while、continue、break的测试
#include "LLVMIRGenerator.h"
#include <iomanip>
using namespace std;
namespace sysy {
std::string LLVMIRGenerator::generateIR(SysYParser::CompUnitContext* unit) {
// 初始化自定义IR数据结构
irModule = std::make_unique<sysy::Module>();
irBuilder = sysy::IRBuilder(); // 初始化IR构建器
tempCounter = 0;
symbolTable.clear();
tmpTable.clear();
globalVars.clear();
inFunction = false;
visitCompUnit(unit);
return irStream.str();
}
std::string LLVMIRGenerator::getNextTemp() {
std::string ret = "%." + std::to_string(tempCounter++);
tmpTable[ret] = "void";
return ret;
}
std::string LLVMIRGenerator::getLLVMType(const std::string& type) {
if (type == "int") return "i32";
if (type == "float") return "float";
if (type.find("[]") != std::string::npos)
return getLLVMType(type.substr(0, type.size()-2)) + "*";
return "i32";
}
sysy::Type* LLVMIRGenerator::getSysYType(const std::string& typeStr) {
if (typeStr == "int") return sysy::Type::getIntType();
if (typeStr == "float") return sysy::Type::getFloatType();
if (typeStr == "void") return sysy::Type::getVoidType();
// 处理指针类型等
return sysy::Type::getIntType();
}
std::any LLVMIRGenerator::visitCompUnit(SysYParser::CompUnitContext* ctx) {
auto type_i32 = Type::getIntType();
auto type_f32 = Type::getFloatType();
auto type_void = Type::getVoidType();
auto type_i32p = Type::getPointerType(type_i32);
auto type_f32p = Type::getPointerType(type_f32);
// 创建运行时库函数
irModule->createFunction("getint", sysy::FunctionType::get(type_i32, {}));
irModule->createFunction("getch", sysy::FunctionType::get(type_i32, {}));
irModule->createFunction("getfloat", sysy::FunctionType::get(type_f32, {}));
//TODO: 添加更多运行时库函数
irStream << "declare i32 @getint()\n";
irStream << "declare i32 @getch()\n";
irStream << "declare float @getfloat()\n";
//TODO: 添加更多运行时库函数的文本IR
for (auto decl : ctx->decl()) {
decl->accept(this);
}
for (auto funcDef : ctx->funcDef()) {
inFunction = true; // 进入函数定义
funcDef->accept(this);
inFunction = false; // 离开函数定义
}
return nullptr;
}
std::any LLVMIRGenerator::visitVarDecl(SysYParser::VarDeclContext* ctx) {
// TODO数组初始化
std::string type = ctx->bType()->getText();
currentVarType = getLLVMType(type);
for (auto varDef : ctx->varDef()) {
if (!inFunction) {
// 全局变量声明
std::string varName = varDef->Ident()->getText();
std::string llvmType = getLLVMType(type);
std::string value = "0"; // 默认值为 0
if (varDef->ASSIGN()) {
value = std::any_cast<std::string>(varDef->initVal()->accept(this));
} else {
std::cout << "[WR-Release-01]Warning: Global variable '" << varName
<< "' is declared without initialization, defaulting to 0.\n";
}
irStream << "@" << varName << " = dso_local global " << llvmType << " " << value << ", align 4\n";
globalVars.push_back(varName); // 记录全局变量
} else {
// 局部变量声明
varDef->accept(this);
}
}
return nullptr;
}
std::any LLVMIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
// TODO数组初始化
std::string type = ctx->bType()->getText();
for (auto constDef : ctx->constDef()) {
if (!inFunction) {
// 全局常量声明
std::string varName = constDef->Ident()->getText();
std::string llvmType = getLLVMType(type);
std::string value = "0"; // 默认值为 0
try {
value = std::any_cast<std::string>(constDef->constInitVal()->accept(this));
} catch (...) {
throw std::runtime_error("[ERR-Release-01]Const value must be initialized upon definition.");
}
// 如果是 float 类型,转换为十六进制表示
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hexValue;
value = ss.str();
} catch (...) {
throw std::runtime_error("[ERR-Release-02]Invalid float literal: " + value);
}
}
irStream << "@" << varName << " = dso_local constant " << llvmType << " " << value << ", align 4\n";
globalVars.push_back(varName); // 记录全局变量
} else {
// 局部常量声明
std::string varName = constDef->Ident()->getText();
std::string llvmType = getLLVMType(type);
std::string allocaName = getNextTemp();
std::string value = "0"; // 默认值为 0
try {
value = std::any_cast<std::string>(constDef->constInitVal()->accept(this));
} catch (...) {
throw std::runtime_error("Const value must be initialized upon definition.");
}
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hexValue;
value = ss.str();
} catch (...) {
throw std::runtime_error("Invalid float literal: " + value);
}
}
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
symbolTable[varName] = {allocaName, llvmType};
tmpTable[allocaName] = llvmType;
}
}
return nullptr;
}
std::any LLVMIRGenerator::visitVarDef(SysYParser::VarDefContext* ctx) {
// TODO数组初始化
std::string varName = ctx->Ident()->getText();
std::string type = currentVarType;
std::string llvmType = getLLVMType(type);
std::string allocaName = getNextTemp();
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
if (ctx->ASSIGN()) {
std::string value = std::any_cast<std::string>(ctx->initVal()->accept(this));
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << (hexValue & (0xffffffffUL << 32));
value = ss.str();
} catch (...) {
throw std::runtime_error("Invalid float literal: " + value);
}
}
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
}
symbolTable[varName] = {allocaName, llvmType};
tmpTable[allocaName] = llvmType;
return nullptr;
}
std::any LLVMIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx) {
currentFunction = ctx->Ident()->getText();
currentReturnType = getLLVMType(ctx->funcType()->getText());
symbolTable.clear();
tmpTable.clear();
tempCounter = 0;
hasReturn = false;
irStream << "define dso_local " << currentReturnType << " @" << currentFunction << "(";
if (ctx->funcFParams()) {
auto params = ctx->funcFParams()->funcFParam();
tempCounter += params.size();
for (size_t i = 0; i < params.size(); ++i) {
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};
tmpTable["%" + std::to_string(i)] = paramType;
}
}
tempCounter++;
irStream << ") #0 {\n";
if (ctx->funcFParams()) {
auto params = ctx->funcFParams()->funcFParam();
for (size_t i = 0; i < params.size(); ++i) {
std::string varName = params[i]->Ident()->getText();
std::string type = params[i]->bType()->getText();
std::string llvmType = getLLVMType(type);
std::string allocaName = getNextTemp();
tmpTable[allocaName] = llvmType;
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
irStream << " store " << llvmType << " " << symbolTable[varName].first << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
symbolTable[varName] = {allocaName, llvmType};
}
}
ctx->blockStmt()->accept(this);
if (!hasReturn) {
if (currentReturnType == "void") {
irStream << " ret void\n";
} else {
irStream << " ret " << currentReturnType << " 0\n";
}
}
irStream << "}\n";
return nullptr;
}
std::any LLVMIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext* ctx) {
for (auto item : ctx->blockItem()) {
item->accept(this);
}
return nullptr;
}
std::any LLVMIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx)
{
std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
std::string rhs = std::any_cast<std::string>(ctx->exp()->accept(this));
if (lhsType == "float") {
try {
double floatValue = std::stod(rhs);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << (hexValue & (0xffffffffUL << 32));
rhs = ss.str();
} catch (...) {
throw std::runtime_error("Invalid float literal: " + rhs);
}
}
irStream << " store " << lhsType << " " << rhs << ", " << lhsType
<< "* " << lhsAlloca << ", align 4\n";
return nullptr;
}
std::any LLVMIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx)
{
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
std::string trueLabel = "if.then." + std::to_string(tempCounter);
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";
irStream << trueLabel << ":\n";
ctx->stmt(0)->accept(this);
irStream << " br label %" << mergeLabel << "\n";
irStream << falseLabel << ":\n";
if (ctx->ELSE()) {
ctx->stmt(1)->accept(this);
}
irStream << " br label %" << mergeLabel << "\n";
irStream << mergeLabel << ":\n";
return nullptr;
}
std::any LLVMIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx)
{
std::string loop_cond = "while.cond." + std::to_string(tempCounter);
std::string loop_body = "while.body." + std::to_string(tempCounter);
std::string loop_end = "while.end." + std::to_string(tempCounter++);
loopStack.push({loop_end, loop_cond});
irStream << " br label %" << loop_cond << "\n";
irStream << loop_cond << ":\n";
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
irStream << " br i1 " << cond << ", label %" << loop_body << ", label %" << loop_end << "\n";
irStream << loop_body << ":\n";
ctx->stmt()->accept(this);
irStream << " br label %" << loop_cond << "\n";
irStream << loop_end << ":\n";
loopStack.pop();
return nullptr;
}
std::any LLVMIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx)
{
if (loopStack.empty()) {
throw std::runtime_error("Break statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().breakLabel << "\n";
return nullptr;
}
std::any LLVMIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext *ctx)
{
if (loopStack.empty()) {
throw std::runtime_error("Continue statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().continueLabel << "\n";
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) {
// if (ctx->lValue() && ctx->ASSIGN()) {
// std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
// std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
// std::string rhs = std::any_cast<std::string>(ctx->exp()->accept(this));
// if (lhsType == "float") {
// try {
// double floatValue = std::stod(rhs);
// uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
// std::stringstream ss;
// ss << "0x" << std::hex << std::uppercase << (hexValue & (0xffffffffUL << 32));
// rhs = ss.str();
// } catch (...) {
// throw std::runtime_error("Invalid float literal: " + rhs);
// }
// }
// irStream << " store " << lhsType << " " << rhs << ", " << lhsType
// << "* " << lhsAlloca << ", align 4\n";
// } else if (ctx->RETURN()) {
// 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";
// }
// } else if (ctx->IF()) {
// std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
// std::string trueLabel = "if.then." + std::to_string(tempCounter);
// 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";
// irStream << trueLabel << ":\n";
// ctx->stmt(0)->accept(this);
// irStream << " br label %" << mergeLabel << "\n";
// irStream << falseLabel << ":\n";
// if (ctx->ELSE()) {
// ctx->stmt(1)->accept(this);
// }
// irStream << " br label %" << mergeLabel << "\n";
// irStream << mergeLabel << ":\n";
// } else if (ctx->WHILE()) {
// std::string loop_cond = "while.cond." + std::to_string(tempCounter);
// std::string loop_body = "while.body." + std::to_string(tempCounter);
// std::string loop_end = "while.end." + std::to_string(tempCounter++);
// loopStack.push({loop_end, loop_cond});
// irStream << " br label %" << loop_cond << "\n";
// irStream << loop_cond << ":\n";
// std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
// irStream << " br i1 " << cond << ", label %" << loop_body << ", label %" << loop_end << "\n";
// irStream << loop_body << ":\n";
// ctx->stmt(0)->accept(this);
// irStream << " br label %" << loop_cond << "\n";
// irStream << loop_end << ":\n";
// loopStack.pop();
// } else if (ctx->BREAK()) {
// if (loopStack.empty()) {
// throw std::runtime_error("Break statement outside of a loop.");
// }
// irStream << " br label %" << loopStack.top().breakLabel << "\n";
// } else if (ctx->CONTINUE()) {
// if (loopStack.empty()) {
// throw std::runtime_error("Continue statement outside of a loop.");
// }
// irStream << " br label %" << loopStack.top().continueLabel << "\n";
// } else if (ctx->blockStmt()) {
// ctx->blockStmt()->accept(this);
// } else if (ctx->exp()) {
// ctx->exp()->accept(this);
// }
// return nullptr;
// }
std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
std::string varName = ctx->Ident()->getText();
return symbolTable[varName].first;
}
// std::any LLVMIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
// if (ctx->lValue()) {
// std::string allocaPtr = std::any_cast<std::string>(ctx->lValue()->accept(this));
// std::string varName = ctx->lValue()->Ident()->getText();
// std::string type = symbolTable[varName].second;
// std::string temp = getNextTemp();
// irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
// tmpTable[temp] = type;
// return temp;
// } else if (ctx->exp()) {
// return ctx->exp()->accept(this);
// } else {
// return ctx->number()->accept(this);
// }
// }
std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimExpContext *ctx){
// irStream << "visitPrimExp\n";
// std::cout << "Type name: " << typeid(*(ctx->primaryExp())).name() << std::endl;
SysYParser::PrimaryExpContext* pExpCtx = ctx->primaryExp();
if (auto* lvalCtx = dynamic_cast<SysYParser::LValContext*>(pExpCtx)) {
std::string allocaPtr = std::any_cast<std::string>(lvalCtx->lValue()->accept(this));
std::string varName = lvalCtx->lValue()->Ident()->getText();
std::string type = symbolTable[varName].second;
std::string temp = getNextTemp();
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
tmpTable[temp] = type;
return temp;
} else if (auto* expCtx = dynamic_cast<SysYParser::ParenExpContext*>(pExpCtx)) {
return expCtx->exp()->accept(this);
} else if (auto* strCtx = dynamic_cast<SysYParser::StrContext*>(pExpCtx)) {
return strCtx->string()->accept(this);
} else if (auto* numCtx = dynamic_cast<SysYParser::NumContext*>(pExpCtx)) {
return numCtx->number()->accept(this);
} else {
// 没有成功转换,说明 ctx->primaryExp() 不是 NumContext 或其他已知类型
// 可能是其他类型的表达式,或者是一个空的 PrimaryExpContext
std::cout << "Unknown primary expression type." << std::endl;
throw std::runtime_error("Unknown primary expression type.");
}
// return visitChildren(ctx);
}
std::any LLVMIRGenerator::visitParenExp(SysYParser::ParenExpContext* ctx) {
return ctx->exp()->accept(this);
}
std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
if (ctx->ILITERAL()) {
return ctx->ILITERAL()->getText();
} else if (ctx->FLITERAL()) {
return ctx->FLITERAL()->getText();
}
return "";
}
std::any LLVMIRGenerator::visitString(SysYParser::StringContext *ctx)
{
if (ctx->STRING()) {
// 处理字符串常量
std::string str = ctx->STRING()->getText();
// 去掉引号
str = str.substr(1, str.size() - 2);
// 转义处理
std::string escapedStr;
for (char c : str) {
if (c == '\\') {
escapedStr += "\\\\";
} else if (c == '"') {
escapedStr += "\\\"";
} else {
escapedStr += c;
}
}
return "\"" + escapedStr + "\"";
}
return ctx->STRING()->getText();
}
std::any LLVMIRGenerator::visitUnExp(SysYParser::UnExpContext* ctx) {
if (ctx->unaryOp()) {
std::string operand = std::any_cast<std::string>(ctx->unaryExp()->accept(this));
std::string op = ctx->unaryOp()->getText();
std::string temp = getNextTemp();
std::string type = operand.substr(0, operand.find(' '));
tmpTable[temp] = type;
if (op == "-") {
irStream << " " << temp << " = sub " << type << " 0, " << operand << "\n";
} else if (op == "!") {
irStream << " " << temp << " = xor " << type << " " << operand << ", 1\n";
}
return temp;
}
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) {
auto unaryExps = ctx->unaryExp();
std::string left = std::any_cast<std::string>(unaryExps[0]->accept(this));
for (size_t i = 1; i < unaryExps.size(); ++i) {
std::string right = std::any_cast<std::string>(unaryExps[i]->accept(this));
std::string op = ctx->children[2*i-1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
if (op == "*") {
irStream << " " << temp << " = mul nsw " << type << " " << left << ", " << right << "\n";
} else if (op == "/") {
irStream << " " << temp << " = sdiv " << type << " " << left << ", " << right << "\n";
} else if (op == "%") {
irStream << " " << temp << " = srem " << type << " " << left << ", " << right << "\n";
}
left = temp;
tmpTable[temp] = type;
}
return left;
}
std::any LLVMIRGenerator::visitAddExp(SysYParser::AddExpContext* ctx) {
auto mulExps = ctx->mulExp();
std::string left = std::any_cast<std::string>(mulExps[0]->accept(this));
for (size_t i = 1; i < mulExps.size(); ++i) {
std::string right = std::any_cast<std::string>(mulExps[i]->accept(this));
std::string op = ctx->children[2*i-1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
if (op == "+") {
irStream << " " << temp << " = add nsw " << type << " " << left << ", " << right << "\n";
} else if (op == "-") {
irStream << " " << temp << " = sub nsw " << type << " " << left << ", " << right << "\n";
}
left = temp;
tmpTable[temp] = type;
}
return left;
}
std::any LLVMIRGenerator::visitRelExp(SysYParser::RelExpContext* ctx) {
auto addExps = ctx->addExp();
std::string left = std::any_cast<std::string>(addExps[0]->accept(this));
for (size_t i = 1; i < addExps.size(); ++i) {
std::string right = std::any_cast<std::string>(addExps[i]->accept(this));
std::string op = ctx->children[2*i-1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
if (op == "<") {
irStream << " " << temp << " = icmp slt " << type << " " << left << ", " << right << "\n";
} else if (op == ">") {
irStream << " " << temp << " = icmp sgt " << type << " " << left << ", " << right << "\n";
} else if (op == "<=") {
irStream << " " << temp << " = icmp sle " << type << " " << left << ", " << right << "\n";
} else if (op == ">=") {
irStream << " " << temp << " = icmp sge " << type << " " << left << ", " << right << "\n";
}
left = temp;
}
return left;
}
std::any LLVMIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
auto relExps = ctx->relExp();
std::string left = std::any_cast<std::string>(relExps[0]->accept(this));
for (size_t i = 1; i < relExps.size(); ++i) {
std::string right = std::any_cast<std::string>(relExps[i]->accept(this));
std::string op = ctx->children[2*i-1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
if (op == "==") {
irStream << " " << temp << " = icmp eq " << type << " " << left << ", " << right << "\n";
} else if (op == "!=") {
irStream << " " << temp << " = icmp ne " << type << " " << left << ", " << right << "\n";
}
left = temp;
}
return left;
}
std::any LLVMIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
auto eqExps = ctx->eqExp();
std::string left = std::any_cast<std::string>(eqExps[0]->accept(this));
for (size_t i = 1; i < eqExps.size(); ++i) {
std::string falseLabel = "land.false." + std::to_string(tempCounter);
std::string endLabel = "land.end." + std::to_string(tempCounter++);
std::string temp = getNextTemp();
irStream << " br label %" << falseLabel << "\n";
irStream << falseLabel << ":\n";
std::string right = std::any_cast<std::string>(eqExps[i]->accept(this));
irStream << " " << temp << " = and i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
left = temp;
}
return left;
}
std::any LLVMIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
auto lAndExps = ctx->lAndExp();
std::string left = std::any_cast<std::string>(lAndExps[0]->accept(this));
for (size_t i = 1; i < lAndExps.size(); ++i) {
std::string trueLabel = "lor.true." + std::to_string(tempCounter);
std::string endLabel = "lor.end." + std::to_string(tempCounter++);
std::string temp = getNextTemp();
irStream << " br label %" << trueLabel << "\n";
irStream << trueLabel << ":\n";
std::string right = std::any_cast<std::string>(lAndExps[i]->accept(this));
irStream << " " << temp << " = or i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
left = temp;
}
return left;
}
}

View File

@ -1,859 +0,0 @@
// LLVMIRGenerator.cpp
// TODO类型转换及其检查
// TODOsysy库函数处理
// TODO数组处理
// TODO对while、continue、break的测试
#include "LLVMIRGenerator_1.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
// namespace sysy {
std::string LLVMIRGenerator::generateIR(SysYParser::CompUnitContext* unit) {
// 初始化 SysY IR 模块
module = std::make_unique<sysy::Module>();
// 清空符号表和临时变量表
symbolTable.clear();
tmpTable.clear();
irSymbolTable.clear();
irTmpTable.clear();
tempCounter = 0;
globalVars.clear();
hasReturn = false;
loopStack = std::stack<LoopLabels>();
inFunction = false;
// 访问编译单元
visitCompUnit(unit);
return irStream.str();
}
std::string LLVMIRGenerator::getNextTemp() {
std::string ret = "%." + std::to_string(tempCounter++);
tmpTable[ret] = "void";
return ret;
}
std::string LLVMIRGenerator::getIRTempName() {
return "%" + std::to_string(tempCounter++);
}
std::string LLVMIRGenerator::getLLVMType(const std::string& type) {
if (type == "int") return "i32";
if (type == "float") return "float";
if (type.find("[]") != std::string::npos)
return getLLVMType(type.substr(0, type.size() - 2)) + "*";
return "i32";
}
sysy::Type* LLVMIRGenerator::getIRType(const std::string& type) {
if (type == "int") return sysy::Type::getIntType();
if (type == "float") return sysy::Type::getFloatType();
if (type == "void") return sysy::Type::getVoidType();
if (type.find("[]") != std::string::npos) {
std::string baseType = type.substr(0, type.size() - 2);
return sysy::Type::getPointerType(getIRType(baseType));
}
return sysy::Type::getIntType(); // 默认 int
}
void LLVMIRGenerator::setIRPosition(sysy::BasicBlock* block) {
currentIRBlock = block;
}
std::any LLVMIRGenerator::visitCompUnit(SysYParser::CompUnitContext* ctx) {
for (auto decl : ctx->decl()) {
decl->accept(this);
}
for (auto funcDef : ctx->funcDef()) {
inFunction = true;
funcDef->accept(this);
inFunction = false;
}
return nullptr;
}
std::any LLVMIRGenerator::visitVarDecl(SysYParser::VarDeclContext* ctx) {
// TODO数组初始化
std::string type = ctx->bType()->getText();
currentVarType = getLLVMType(type);
sysy::Type* irType = sysy::Type::getPointerType(getIRType(type));
for (auto varDef : ctx->varDef()) {
if (!inFunction) {
// 全局变量(文本 IR
std::string varName = varDef->Ident()->getText();
std::string llvmType = getLLVMType(type);
std::string value = "0";
sysy::Value* initValue = nullptr;
if (varDef->ASSIGN()) {
value = std::any_cast<std::string>(varDef->initVal()->accept(this));
if (irTmpTable.find(value) != irTmpTable.end() && isa<sysy::ConstantValue>(irTmpTable[value])) {
initValue = irTmpTable[value];
}
}
if (llvmType == "float" && initValue) {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hexValue;
value = ss.str();
} catch (...) {
throw std::runtime_error("[ERR-Release-02]Invalid float literal: " + value);
}
}
irStream << "@" << varName << " = dso_local global " << llvmType << " " << value << ", align 4\n";
globalVars.push_back(varName);
// 全局变量SysY IR
auto globalValue = module->createGlobalValue(varName, irType, {}, initValue);
irSymbolTable[varName] = globalValue;
} else {
varDef->accept(this);
}
}
return nullptr;
}
std::any LLVMIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
// TODO数组初始化
std::string type = ctx->bType()->getText();
currentVarType = getLLVMType(type);
sysy::Type* irType = sysy::Type::getPointerType(getIRType(type)); // 全局变量为指针类型
for (auto constDef : ctx->constDef()) {
std::string varName = constDef->Ident()->getText();
std::string llvmType = getLLVMType(type);
std::string value = "0";
sysy::Value* initValue = nullptr;
try {
value = std::any_cast<std::string>(constDef->constInitVal()->accept(this));
if (isa<sysy::ConstantValue>(irTmpTable[value])) {
initValue = irTmpTable[value];
}
} catch (...) {
throw std::runtime_error("Const value must be initialized upon definition.");
}
if (!inFunction) {
// 全局常量(文本 IR
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hexValue;
value = ss.str();
} catch (...) {
throw std::runtime_error("[ERR-Release-03]Invalid float literal: " + value);
}
}
irStream << "@" << varName << " = dso_local constant " << llvmType << " " << value << ", align 4\n";
globalVars.push_back(varName);
// 全局常量SysY IR
auto globalValue = module->createGlobalValue(varName, irType, {}, initValue);
irSymbolTable[varName] = globalValue;
} else {
// 局部常量(文本 IR
std::string allocaName = getNextTemp();
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << hexValue;
value = ss.str();
} catch (...) {
throw std::runtime_error("Invalid float literal: " + value);
}
}
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
symbolTable[varName] = {allocaName, llvmType};
tmpTable[allocaName] = llvmType;
// 局部常量SysY IRTODO:这里可能有bugAI在犯蠢
sysy::IRBuilder builder(currentIRBlock);
auto allocaInst = builder.createAllocaInst(irType, {}, varName);
builder.createStoreInst(initValue, allocaInst);
irSymbolTable[varName] = allocaInst;
irTmpTable[allocaName] = allocaInst;
}
}
return nullptr;
}
std::any LLVMIRGenerator::visitVarDef(SysYParser::VarDefContext* ctx) {
// TODO数组初始化
std::string varName = ctx->Ident()->getText();
std::string llvmType = currentVarType;
sysy::Type* irType = sysy::Type::getPointerType(getIRType(currentVarType == "i32" ? "int" : "float"));
std::string allocaName = getNextTemp();
// 局部变量(文本 IR
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
// 局部变量SysY IR
sysy::IRBuilder builder(currentIRBlock);
auto allocaInst = builder.createAllocaInst(irType, {}, varName);
sysy::Value* initValue = nullptr;
if (ctx->ASSIGN()) {
std::string value = std::any_cast<std::string>(ctx->initVal()->accept(this));
if (llvmType == "float") {
try {
double floatValue = std::stod(value);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << (hexValue & (0xffffffffUL << 32));
value = ss.str();
} catch (...) {
throw std::runtime_error("Invalid float literal: " + value);
}
}
irStream << " store " << llvmType << " " << value << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
if (irTmpTable.find(value) != irTmpTable.end()) {
initValue = irTmpTable[value];
}
builder.createStoreInst(initValue, allocaInst);
}
symbolTable[varName] = {allocaName, llvmType};
tmpTable[allocaName] = llvmType;
irSymbolTable[varName] = allocaInst;//TODO:这里没看懂在干嘛
irTmpTable[allocaName] = allocaInst;//TODO:这里没看懂在干嘛
builder.createStoreInst(initValue, allocaInst);//TODO:这里没看懂在干嘛
return nullptr;
}
std::any LLVMIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx) {
currentFunction = ctx->Ident()->getText();
currentReturnType = getLLVMType(ctx->funcType()->getText());
sysy::Type* irReturnType = getIRType(ctx->funcType()->getText());
std::vector<sysy::Type*> paramTypes;
// 清空符号表
symbolTable.clear();
tmpTable.clear();
irSymbolTable.clear();
irTmpTable.clear();
tempCounter = 0;
hasReturn = false;
// 处理函数参数(文本 IR 和 SysY IR
if (ctx->funcFParams()) {
auto params = ctx->funcFParams()->funcFParam();
for (size_t i = 0; i < params.size(); ++i) {
std::string paramType = getLLVMType(params[i]->bType()->getText());
if (i > 0) irStream << ", ";
irStream << paramType << " noundef %" << i;
symbolTable[params[i]->Ident()->getText()] = {"%" + std::to_string(i), paramType};
tmpTable["%" + std::to_string(i)] = paramType;
paramTypes.push_back(getIRType(params[i]->bType()->getText()));
}
tempCounter += params.size();
}
tempCounter++;
// 文本 IR 函数定义
irStream << "define dso_local " << currentReturnType << " @" << currentFunction << "(";
irStream << ") #0 {\n";
// SysY IR 函数定义
sysy::Type* funcType = sysy::Type::getFunctionType(irReturnType, paramTypes);
currentIRFunction = module->createFunction(currentFunction, funcType);
setIRPosition(currentIRFunction->getEntryBlock());
// 处理函数参数分配
if (ctx->funcFParams()) {
auto params = ctx->funcFParams()->funcFParam();
for (size_t i = 0; i < params.size(); ++i) {
std::string varName = params[i]->Ident()->getText();
std::string llvmType = getLLVMType(params[i]->bType()->getText());
sysy::Type* irType = getIRType(params[i]->bType()->getText());
std::string allocaName = getNextTemp();
tmpTable[allocaName] = llvmType;
// 文本 IR 分配
irStream << " " << allocaName << " = alloca " << llvmType << ", align 4\n";
irStream << " store " << llvmType << " %" << i << ", " << llvmType
<< "* " << allocaName << ", align 4\n";
// SysY IR 分配
sysy::IRBuilder builder(currentIRBlock);
auto arg = currentIRBlock->createArgument(irType, varName);
auto allocaInst = builder.createAllocaInst(sysy::Type::getPointerType(irType), {}, varName);
builder.createStoreInst(arg, allocaInst);
symbolTable[varName] = {allocaName, llvmType};
irSymbolTable[varName] = allocaInst;
irTmpTable[allocaName] = allocaInst;
}
}
ctx->blockStmt()->accept(this);
if (!hasReturn) {
if (currentReturnType == "void") {
irStream << " ret void\n";
sysy::IRBuilder builder(currentIRBlock);
builder.createReturnInst();
} else {
irStream << " ret " << currentReturnType << " 0\n";
sysy::IRBuilder builder(currentIRBlock);
builder.createReturnInst(sysy::ConstantValue::get(0));
}
}
irStream << "}\n";
currentIRFunction = nullptr;
currentIRBlock = nullptr;
return nullptr;
}
std::any LLVMIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext* ctx) {
for (auto item : ctx->blockItem()) {
item->accept(this);
}
return nullptr;
}
std::any LLVMIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
std::string lhsAlloca = std::any_cast<std::string>(ctx->lValue()->accept(this));
std::string lhsType = symbolTable[ctx->lValue()->Ident()->getText()].second;
std::string rhs = std::any_cast<std::string>(ctx->exp()->accept(this));
sysy::Value* rhsValue = irTmpTable[rhs];
// 文本 IR
if (lhsType == "float") {
try {
double floatValue = std::stod(rhs);
uint64_t hexValue = reinterpret_cast<uint64_t&>(floatValue);
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << (hexValue & (0xffffffffUL << 32));
rhs = ss.str();
} catch (...) {
// 如果 rhs 不是字面量,假设已正确处理
throw std::runtime_error("Invalid float literal: " + rhs);
}
}
irStream << " store " << lhsType << " " << rhs << ", " << lhsType
<< "* " << lhsAlloca << ", align 4\n";
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
builder.createStoreInst(rhsValue, irSymbolTable[ctx->lValue()->Ident()->getText()]);
return nullptr;
}
std::any LLVMIRGenerator::visitIfStmt(SysYParser::IfStmtContext* ctx) {
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
sysy::Value* condValue = irTmpTable[cond];
std::string trueLabel = "if.then." + std::to_string(tempCounter);
std::string falseLabel = "if.else." + std::to_string(tempCounter);
std::string mergeLabel = "if.end." + std::to_string(tempCounter++);
// SysY IR 基本块
sysy::BasicBlock* thenBlock = currentIRFunction->addBasicBlock(trueLabel);
sysy::BasicBlock* elseBlock = ctx->ELSE() ? currentIRFunction->addBasicBlock(falseLabel) : nullptr;
sysy::BasicBlock* mergeBlock = currentIRFunction->addBasicBlock(mergeLabel);
// 文本 IR
irStream << " br i1 " << cond << ", label %" << trueLabel << ", label %"
<< (ctx->ELSE() ? falseLabel : mergeLabel) << "\n";
// SysY IR 条件分支
sysy::IRBuilder builder(currentIRBlock);
builder.createCondBrInst(condValue, thenBlock, ctx->ELSE() ? elseBlock : mergeBlock, {}, {});
// 处理 then 分支
setIRPosition(thenBlock);
irStream << trueLabel << ":\n";
ctx->stmt(0)->accept(this);
irStream << " br label %" << mergeLabel << "\n";
builder.setPosition(thenBlock, thenBlock->end());
builder.createUncondBrInst(mergeBlock, {});
// 处理 else 分支
if (ctx->ELSE()) {
setIRPosition(elseBlock);
irStream << falseLabel << ":\n";
ctx->stmt(1)->accept(this);
irStream << " br label %" << mergeLabel << "\n";
builder.setPosition(elseBlock, elseBlock->end());
builder.createUncondBrInst(mergeBlock, {});
}
// 合并点
setIRPosition(mergeBlock);
irStream << mergeLabel << ":\n";
return nullptr;
}
std::any LLVMIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext* ctx) {
std::string loopCond = "while.cond." + std::to_string(tempCounter);
std::string loopBody = "while.body." + std::to_string(tempCounter);
std::string loopEnd = "while.end." + std::to_string(tempCounter++);
// SysY IR 基本块
sysy::BasicBlock* condBlock = currentIRFunction->addBasicBlock(loopCond);
sysy::BasicBlock* bodyBlock = currentIRFunction->addBasicBlock(loopBody);
sysy::BasicBlock* endBlock = currentIRFunction->addBasicBlock(loopEnd);
loopStack.push({loopEnd, loopCond, endBlock, condBlock});
// 跳转到条件块
sysy::IRBuilder builder(currentIRBlock);
builder.createUncondBrInst(condBlock, {});
irStream << " br label %" << loopCond << "\n";
// 条件块
setIRPosition(condBlock);
irStream << loopCond << ":\n";
std::string cond = std::any_cast<std::string>(ctx->cond()->accept(this));
sysy::Value* condValue = irTmpTable[cond];
irStream << " br i1 " << cond << ", label %" << loopBody << ", label %" << loopEnd << "\n";
builder.setPosition(condBlock, condBlock->end());
builder.createCondBrInst(condValue, bodyBlock, endBlock, {}, {});
// 循环体
setIRPosition(bodyBlock);
irStream << loopBody << ":\n";
ctx->stmt()->accept(this);
irStream << " br label %" << loopCond << "\n";
builder.setPosition(bodyBlock, bodyBlock->end());
builder.createUncondBrInst(condBlock, {});
// 结束块
setIRPosition(endBlock);
irStream << loopEnd << ":\n";
loopStack.pop();
return nullptr;
}
std::any LLVMIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext* ctx) {
if (loopStack.empty()) {
throw std::runtime_error("Break statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().breakLabel << "\n";
sysy::IRBuilder builder(currentIRBlock);
builder.createUncondBrInst(loopStack.top().irBreakBlock, {});
return nullptr;
}
std::any LLVMIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext* ctx) {
if (loopStack.empty()) {
throw std::runtime_error("Continue statement outside of a loop.");
}
irStream << " br label %" << loopStack.top().continueLabel << "\n";
sysy::IRBuilder builder(currentIRBlock);
builder.createUncondBrInst(loopStack.top().irContinueBlock, {});
return nullptr;
}
std::any LLVMIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
hasReturn = true;
sysy::IRBuilder builder(currentIRBlock);
if (ctx->exp()) {
std::string value = std::any_cast<std::string>(ctx->exp()->accept(this));
sysy::Value* irValue = irTmpTable[value];
irStream << " ret " << currentReturnType << " " << value << "\n";
builder.createReturnInst(irValue);
} else {
irStream << " ret void\n";
builder.createReturnInst();
}
return nullptr;
}
std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
std::string varName = ctx->Ident()->getText();
if (irSymbolTable.find(varName) == irSymbolTable.end()) {
throw std::runtime_error("Undefined variable: " + varName);
}
// 对于 LValue返回分配的指针文本 IR 和 SysY IR 一致)
return symbolTable[varName].first;
}
std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimExpContext* ctx) {
SysYParser::PrimaryExpContext* pExpCtx = ctx->primaryExp();
if (auto* lvalCtx = dynamic_cast<SysYParser::LValContext*>(pExpCtx)) {
std::string allocaPtr = std::any_cast<std::string>(lvalCtx->lValue()->accept(this));
std::string varName = lvalCtx->lValue()->Ident()->getText();
std::string type = symbolTable[varName].second;
std::string temp = getNextTemp();
sysy::Type* irType = getIRType(type == "i32" ? "int" : "float");
// 文本 IR
irStream << " " << temp << " = load " << type << ", " << type << "* " << allocaPtr << ", align 4\n";
tmpTable[temp] = type;
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
auto loadInst = builder.createLoadInst(irSymbolTable[varName], {});
irTmpTable[temp] = loadInst;
return temp;
} else if (auto* expCtx = dynamic_cast<SysYParser::ParenExpContext*>(pExpCtx)) {
return expCtx->exp()->accept(this);
} else if (auto* strCtx = dynamic_cast<SysYParser::StrContext*>(pExpCtx)) {
return strCtx->string()->accept(this);
} else if (auto* numCtx = dynamic_cast<SysYParser::NumContext*>(pExpCtx)) {
return numCtx->number()->accept(this);
} else {
// 没有成功转换,说明 ctx->primaryExp() 不是 NumContext 或其他已知类型
// 可能是其他类型的表达式,或者是一个空的 PrimaryExpContext
std::cout << "Unknown primary expression type." << std::endl;
throw std::runtime_error("Unknown primary expression type.");
}
}
std::any LLVMIRGenerator::visitParenExp(SysYParser::ParenExpContext* ctx) {
return ctx->exp()->accept(this);
}
std::any LLVMIRGenerator::visitNumber(SysYParser::NumberContext* ctx) {
std::string value;
sysy::Value* irValue = nullptr;
if (ctx->ILITERAL()) {
value = ctx->ILITERAL()->getText();
irValue = sysy::ConstantValue::get(std::stoi(value));
} else if (ctx->FLITERAL()) {
value = ctx->FLITERAL()->getText();
irValue = sysy::ConstantValue::get(std::stof(value));
} else {
value = "";
}
std::string temp = getNextTemp();
tmpTable[temp] = ctx->ILITERAL() ? "i32" : "float";
irTmpTable[temp] = irValue;
return value;
}
std::any LLVMIRGenerator::visitString(SysYParser::StringContext* ctx) {
if (ctx->STRING()) {
std::string str = ctx->STRING()->getText();
str = str.substr(1, str.size() - 2);
std::string escapedStr;
for (char c : str) {
if (c == '\\') {
escapedStr += "\\\\";
} else if (c == '"') {
escapedStr += "\\\"";
} else {
escapedStr += c;
}
}
// TODO: SysY IR 暂不支持字符串常量,返回文本 IR 结果
return "\"" + escapedStr + "\"";
}
return ctx->STRING()->getText();
}
std::any LLVMIRGenerator::visitUnExp(SysYParser::UnExpContext* ctx) {
if (ctx->unaryOp()) {
std::string operand = std::any_cast<std::string>(ctx->unaryExp()->accept(this));
sysy::Value* irOperand = irTmpTable[operand];
std::string op = ctx->unaryOp()->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[operand];
sysy::Type* irType = getIRType(type == "i32" ? "int" : "float");
tmpTable[temp] = type;
// 文本 IR
if (op == "-") {
irStream << " " << temp << " = sub " << type << " 0, " << operand << "\n";
} else if (op == "!") {
irStream << " " << temp << " = xor " << type << " " << operand << ", 1\n";
}
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Instruction::Kind kind = (op == "-") ? (type == "i32" ? sysy::Instruction::kNeg : sysy::Instruction::kFNeg)
: sysy::Instruction::kNot;
auto unaryInst = builder.createUnaryInst(kind, irType, irOperand, temp);
irTmpTable[temp] = unaryInst;
return temp;
}
return ctx->unaryExp()->accept(this);
}
std::any LLVMIRGenerator::visitCall(SysYParser::CallContext* ctx) {
std::string funcName = ctx->Ident()->getText();
std::vector<std::string> args;
std::vector<sysy::Value*> irArgs;
if (ctx->funcRParams()) {
for (auto argCtx : ctx->funcRParams()->exp()) {
std::string arg = std::any_cast<std::string>(argCtx->accept(this));
args.push_back(arg);
irArgs.push_back(irTmpTable[arg]);
}
}
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];
}
// 文本 IR
irStream << " " << temp << " = call " << currentReturnType << " @" << funcName << "(" << argList << ")\n";
tmpTable[temp] = currentReturnType;
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Function* callee = module->getFunction(funcName);
if (!callee) {
throw std::runtime_error("Undefined function: " + funcName);
}
auto callInst = builder.createCallInst(callee, irArgs, temp);
irTmpTable[temp] = callInst;
return temp;
}
std::any LLVMIRGenerator::visitMulExp(SysYParser::MulExpContext* ctx) {
auto unaryExps = ctx->unaryExp();
std::string left = std::any_cast<std::string>(unaryExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
sysy::Type* irType = irLeft->getType();
for (size_t i = 1; i < unaryExps.size(); ++i) {
std::string right = std::any_cast<std::string>(unaryExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
std::string op = ctx->children[2 * i - 1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
tmpTable[temp] = type;
// 文本 IR
if (op == "*") {
irStream << " " << temp << " = mul nsw " << type << " " << left << ", " << right << "\n";
} else if (op == "/") {
irStream << " " << temp << " = sdiv " << type << " " << left << ", " << right << "\n";
} else if (op == "%") {
irStream << " " << temp << " = srem " << type << " " << left << ", " << right << "\n";
}
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Instruction::Kind kind;
if (type == "i32") {
if (op == "*") kind = sysy::Instruction::kMul;
else if (op == "/") kind = sysy::Instruction::kDiv;
else kind = sysy::Instruction::kRem;
} else {
if (op == "*") kind = sysy::Instruction::kFMul;
else if (op == "/") kind = sysy::Instruction::kFDiv;
else kind = sysy::Instruction::kFRem;
}
auto binaryInst = builder.createBinaryInst(kind, irType, irLeft, irRight, temp);
irTmpTable[temp] = binaryInst;
left = temp;
irLeft = binaryInst;
}
return left;
}
std::any LLVMIRGenerator::visitAddExp(SysYParser::AddExpContext* ctx) {
auto mulExps = ctx->mulExp();
std::string left = std::any_cast<std::string>(mulExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
sysy::Type* irType = irLeft->getType();
for (size_t i = 1; i < mulExps.size(); ++i) {
std::string right = std::any_cast<std::string>(mulExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
std::string op = ctx->children[2 * i - 1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
tmpTable[temp] = type;
// 文本 IR
if (op == "+") {
irStream << " " << temp << " = add nsw " << type << " " << left << ", " << right << "\n";
} else if (op == "-") {
irStream << " " << temp << " = sub nsw " << type << " " << left << ", " << right << "\n";
}
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Instruction::Kind kind = (type == "i32") ? (op == "+" ? sysy::Instruction::kAdd : sysy::Instruction::kSub)
: (op == "+" ? sysy::Instruction::kFAdd : sysy::Instruction::kFSub);
auto binaryInst = builder.createBinaryInst(kind, irType, irLeft, irRight, temp);
irTmpTable[temp] = binaryInst;
left = temp;
irLeft = binaryInst;
}
return left;
}
std::any LLVMIRGenerator::visitRelExp(SysYParser::RelExpContext* ctx) {
auto addExps = ctx->addExp();
std::string left = std::any_cast<std::string>(addExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
sysy::Type* irType = sysy::Type::getIntType(); // 比较结果为 i1
for (size_t i = 1; i < addExps.size(); ++i) {
std::string right = std::any_cast<std::string>(addExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
std::string op = ctx->children[2 * i - 1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
tmpTable[temp] = "i1";
// 文本 IR
if (op == "<") {
irStream << " " << temp << " = icmp slt " << type << " " << left << ", " << right << "\n";
} else if (op == ">") {
irStream << " " << temp << " = icmp sgt " << type << " " << left << ", " << right << "\n";
} else if (op == "<=") {
irStream << " " << temp << " = icmp sle " << type << " " << left << ", " << right << "\n";
} else if (op == ">=") {
irStream << " " << temp << " = icmp sge " << type << " " << left << ", " << right << "\n";
}
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Instruction::Kind kind;
if (type == "i32") {
if (op == "<") kind = sysy::Instruction::kICmpLT;
else if (op == ">") kind = sysy::Instruction::kICmpGT;
else if (op == "<=") kind = sysy::Instruction::kICmpLE;
else kind = sysy::Instruction::kICmpGE;
} else {
if (op == "<") kind = sysy::Instruction::kFCmpLT;
else if (op == ">") kind = sysy::Instruction::kFCmpGT;
else if (op == "<=") kind = sysy::Instruction::kFCmpLE;
else kind = sysy::Instruction::kFCmpGE;
}
auto cmpInst = builder.createBinaryInst(kind, irType, irLeft, irRight, temp);
irTmpTable[temp] = cmpInst;
left = temp;
irLeft = cmpInst;
}
return left;
}
std::any LLVMIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
auto relExps = ctx->relExp();
std::string left = std::any_cast<std::string>(relExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
sysy::Type* irType = sysy::Type::getIntType(); // 比较结果为 i1
for (size_t i = 1; i < relExps.size(); ++i) {
std::string right = std::any_cast<std::string>(relExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
std::string op = ctx->children[2 * i - 1]->getText();
std::string temp = getNextTemp();
std::string type = tmpTable[left];
tmpTable[temp] = "i1";
// 文本 IR
if (op == "==") {
irStream << " " << temp << " = icmp eq " << type << " " << left << ", " << right << "\n";
} else if (op == "!=") {
irStream << " " << temp << " = icmp ne " << type << " " << left << ", " << right << "\n";
}
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
sysy::Instruction::Kind kind = (type == "i32") ? (op == "==" ? sysy::Instruction::kICmpEQ : sysy::Instruction::kICmpNE)
: (op == "==" ? sysy::Instruction::kFCmpEQ : sysy::Instruction::kFCmpNE);
auto cmpInst = builder.createBinaryInst(kind, irType, irLeft, irRight, temp);
irTmpTable[temp] = cmpInst;
left = temp;
irLeft = cmpInst;
}
return left;
}
std::any LLVMIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
auto eqExps = ctx->eqExp();
std::string left = std::any_cast<std::string>(eqExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
for (size_t i = 1; i < eqExps.size(); ++i) {
std::string falseLabel = "land.false." + std::to_string(tempCounter);
std::string endLabel = "land.end." + std::to_string(tempCounter++);
sysy::BasicBlock* falseBlock = currentIRFunction->addBasicBlock(falseLabel);
sysy::BasicBlock* endBlock = currentIRFunction->addBasicBlock(endLabel);
std::string temp = getNextTemp();
tmpTable[temp] = "i1";
// 文本 IR
irStream << " br i1 " << left << ", label %" << falseLabel << ", label %" << endLabel << "\n";
irStream << falseLabel << ":\n";
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
builder.createCondBrInst(irLeft, falseBlock, endBlock, {}, {});
setIRPosition(falseBlock);
std::string right = std::any_cast<std::string>(eqExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
irStream << " " << temp << " = and i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
// SysY IR 逻辑与(通过基本块实现短路求值)
builder.setPosition(falseBlock, falseBlock->end());
auto andInst = builder.createBinaryInst(sysy::Instruction::kICmpEQ, sysy::Type::getIntType(), irLeft, irRight, temp);
builder.createUncondBrInst(endBlock, {});
irTmpTable[temp] = andInst;
left = temp;
irLeft = andInst;
setIRPosition(endBlock);
}
return left;
}
std::any LLVMIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
auto lAndExps = ctx->lAndExp();
std::string left = std::any_cast<std::string>(lAndExps[0]->accept(this));
sysy::Value* irLeft = irTmpTable[left];
for (size_t i = 1; i < lAndExps.size(); ++i) {
std::string trueLabel = "lor.true." + std::to_string(tempCounter);
std::string endLabel = "lor.end." + std::to_string(tempCounter++);
sysy::BasicBlock* trueBlock = currentIRFunction->addBasicBlock(trueLabel);
sysy::BasicBlock* endBlock = currentIRFunction->addBasicBlock(endLabel);
std::string temp = getNextTemp();
tmpTable[temp] = "i1";
// 文本 IR
irStream << " br i1 " << left << ", label %" << trueLabel << ", label %" << endLabel << "\n";
irStream << trueLabel << ":\n";
// SysY IR
sysy::IRBuilder builder(currentIRBlock);
builder.createCondBrInst(irLeft, trueBlock, endBlock, {}, {});
setIRPosition(trueBlock);
std::string right = std::any_cast<std::string>(lAndExps[i]->accept(this));
sysy::Value* irRight = irTmpTable[right];
irStream << " " << temp << " = or i1 " << left << ", " << right << "\n";
irStream << " br label %" << endLabel << "\n";
irStream << endLabel << ":\n";
// SysY IR 逻辑或(通过基本块实现短路求值)
builder.setPosition(trueBlock, trueBlock->end());
auto orInst = builder.createBinaryInst(sysy::Instruction::kICmpEQ, sysy::Type::getIntType(), irLeft, irRight, temp);
builder.createUncondBrInst(endBlock, {});
irTmpTable[temp] = orInst;
left = temp;
irLeft = orInst;
setIRPosition(endBlock);
}
return left;
}
// } // namespace sysy

File diff suppressed because it is too large Load Diff

View File

@ -1,100 +0,0 @@
#ifndef RISCV32_BACKEND_H
#define RISCV32_BACKEND_H
#include "IR.h"
#include <string>
#include <vector>
#include <map>
#include <set>
#include <memory>
#include <iostream>
#include <functional> // For std::function
namespace sysy {
class RISCv32CodeGen {
public:
enum class PhysicalReg {
ZERO, RA, SP, GP, TP, T0, T1, T2, S0, S1, A0, A1, A2, A3, A4, A5, A6, A7, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, T3, T4, T5, T6,
F0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15,F16, F17, F18, F19, F20, F21, F22, F23, F24, F25, F26, F27, F28, F29, F30, F31
};
// Move DAGNode and RegAllocResult to public section
struct DAGNode {
enum NodeKind { CONSTANT, LOAD, STORE, BINARY, CALL, RETURN, BRANCH, ALLOCA_ADDR, UNARY };
NodeKind kind;
Value* value = nullptr; // For IR Value
std::string inst; // Generated RISC-V instruction(s) for this node
std::string result_vreg; // Virtual register assigned to this node's result
std::vector<DAGNode*> operands;
std::vector<DAGNode*> users; // For debugging and potentially optimizations
DAGNode(NodeKind k) : kind(k) {}
// Debugging / helper
std::string getNodeKindString() const {
switch (kind) {
case CONSTANT: return "CONSTANT";
case LOAD: return "LOAD";
case STORE: return "STORE";
case BINARY: return "BINARY";
case CALL: return "CALL";
case RETURN: return "RETURN";
case BRANCH: return "BRANCH";
case ALLOCA_ADDR: return "ALLOCA_ADDR";
case UNARY: return "UNARY";
default: return "UNKNOWN";
}
}
};
struct RegAllocResult {
std::map<std::string, PhysicalReg> vreg_to_preg; // Virtual register to Physical Register mapping
std::map<Value*, int> stack_map; // Value (AllocaInst) to stack offset
int stack_size = 0; // Total stack frame size for locals and spills
};
RISCv32CodeGen(Module* mod) : module(mod) {}
std::string code_gen();
std::string module_gen();
std::string function_gen(Function* func);
// 修改 basicBlock_gen 的声明,添加 int block_idx 参数
std::string basicBlock_gen(BasicBlock* bb, const RegAllocResult& alloc, int block_idx);
// DAG related
std::vector<std::unique_ptr<DAGNode>> build_dag(BasicBlock* bb);
void select_instructions(DAGNode* node, const RegAllocResult& alloc);
// 改变 emit_instructions 的参数,使其可以直接添加汇编指令到 main ss
void emit_instructions(DAGNode* node, std::stringstream& ss, const RegAllocResult& alloc, std::set<DAGNode*>& emitted_nodes);
// Register Allocation related
std::map<Instruction*, std::set<std::string>> liveness_analysis(Function* func);
std::map<std::string, std::set<std::string>> build_interference_graph(
const std::map<Instruction*, std::set<std::string>>& live_sets);
void color_graph(std::map<std::string, PhysicalReg>& vreg_to_preg,
const std::map<std::string, std::set<std::string>>& interference_graph);
RegAllocResult register_allocation(Function* func);
void eliminate_phi(Function* func); // Phi elimination is typically done before DAG building
// Utility
std::string reg_to_string(PhysicalReg reg);
void print_dag(const std::vector<std::unique_ptr<DAGNode>>& dag, const std::string& bb_name);
private:
static const std::vector<PhysicalReg> allocable_regs;
std::map<Value*, std::string> value_vreg_map; // Maps IR Value* to its virtual register name
Module* module;
int vreg_counter = 0; // Counter for unique virtual register names
int alloca_offset_counter = 0; // Counter for alloca offsets
// 新增一个成员变量来存储当前函数的所有 DAGNode以确保其生命周期贯穿整个函数代码生成
// 这样可以在多个 BasicBlock_gen 调用中访问到完整的 DAG 节点
std::vector<std::unique_ptr<DAGNode>> current_function_dag_nodes;
// 为空标签定义一个伪名称前缀,加上块索引以确保唯一性
const std::string ENTRY_BLOCK_PSEUDO_NAME = "entry_block_";
};
} // namespace sysy
#endif // RISCV32_BACKEND_H

125
src/SysY.interp Normal file

File diff suppressed because one or more lines are too long

73
src/SysY.tokens Normal file
View File

@ -0,0 +1,73 @@
CONST=1
INT=2
FLOAT=3
VOID=4
IF=5
ELSE=6
WHILE=7
BREAK=8
CONTINUE=9
RETURN=10
ADD=11
SUB=12
MUL=13
DIV=14
MOD=15
EQ=16
NE=17
LT=18
LE=19
GT=20
GE=21
AND=22
OR=23
NOT=24
ASSIGN=25
COMMA=26
SEMICOLON=27
LPAREN=28
RPAREN=29
LBRACE=30
RBRACE=31
LBRACK=32
RBRACK=33
Ident=34
ILITERAL=35
FLITERAL=36
STRING=37
WS=38
LINECOMMENT=39
BLOCKCOMMENT=40
'const'=1
'int'=2
'float'=3
'void'=4
'if'=5
'else'=6
'while'=7
'break'=8
'continue'=9
'return'=10
'+'=11
'-'=12
'*'=13
'/'=14
'%'=15
'=='=16
'!='=17
'<'=18
'<='=19
'>'=20
'>='=21
'&&'=22
'||'=23
'!'=24
'='=25
','=26
';'=27
'('=28
')'=29
'{'=30
'}'=31
'['=32
']'=33

7
src/SysYBaseVisitor.cpp Normal file
View File

@ -0,0 +1,7 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#include "SysYBaseVisitor.h"

292
src/SysYLexer.cpp Normal file
View File

@ -0,0 +1,292 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#include "SysYLexer.h"
using namespace antlr4;
using namespace antlr4;
namespace {
struct SysYLexerStaticData final {
SysYLexerStaticData(std::vector<std::string> ruleNames,
std::vector<std::string> channelNames,
std::vector<std::string> modeNames,
std::vector<std::string> literalNames,
std::vector<std::string> symbolicNames)
: ruleNames(std::move(ruleNames)), channelNames(std::move(channelNames)),
modeNames(std::move(modeNames)), literalNames(std::move(literalNames)),
symbolicNames(std::move(symbolicNames)),
vocabulary(this->literalNames, this->symbolicNames) {}
SysYLexerStaticData(const SysYLexerStaticData&) = delete;
SysYLexerStaticData(SysYLexerStaticData&&) = delete;
SysYLexerStaticData& operator=(const SysYLexerStaticData&) = delete;
SysYLexerStaticData& operator=(SysYLexerStaticData&&) = delete;
std::vector<antlr4::dfa::DFA> decisionToDFA;
antlr4::atn::PredictionContextCache sharedContextCache;
const std::vector<std::string> ruleNames;
const std::vector<std::string> channelNames;
const std::vector<std::string> modeNames;
const std::vector<std::string> literalNames;
const std::vector<std::string> symbolicNames;
const antlr4::dfa::Vocabulary vocabulary;
antlr4::atn::SerializedATNView serializedATN;
std::unique_ptr<antlr4::atn::ATN> atn;
};
::antlr4::internal::OnceFlag sysylexerLexerOnceFlag;
#if ANTLR4_USE_THREAD_LOCAL_CACHE
static thread_local
#endif
std::unique_ptr<SysYLexerStaticData> sysylexerLexerStaticData = nullptr;
void sysylexerLexerInitialize() {
#if ANTLR4_USE_THREAD_LOCAL_CACHE
if (sysylexerLexerStaticData != nullptr) {
return;
}
#else
assert(sysylexerLexerStaticData == nullptr);
#endif
auto staticData = std::make_unique<SysYLexerStaticData>(
std::vector<std::string>{
"CONST", "INT", "FLOAT", "VOID", "IF", "ELSE", "WHILE", "BREAK", "CONTINUE",
"RETURN", "ADD", "SUB", "MUL", "DIV", "MOD", "EQ", "NE", "LT", "LE",
"GT", "GE", "AND", "OR", "NOT", "ASSIGN", "COMMA", "SEMICOLON", "LPAREN",
"RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK", "DecDigit", "OctDigit",
"HexDigit", "OctPrefix", "HexPrefix", "NonZeroDecDigit", "Sign", "DecFractional",
"Exponent", "DecFloat", "HexFractional", "BinExponent", "HexFloat",
"ESC", "ALPHA", "ALPHANUM", "NONDIGIT", "Ident", "ILITERAL", "FLITERAL",
"STRING", "WS", "LINECOMMENT", "BLOCKCOMMENT"
},
std::vector<std::string>{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
},
std::vector<std::string>{
"DEFAULT_MODE"
},
std::vector<std::string>{
"", "'const'", "'int'", "'float'", "'void'", "'if'", "'else'", "'while'",
"'break'", "'continue'", "'return'", "'+'", "'-'", "'*'", "'/'", "'%'",
"'=='", "'!='", "'<'", "'<='", "'>'", "'>='", "'&&'", "'||'", "'!'",
"'='", "','", "';'", "'('", "')'", "'{'", "'}'", "'['", "']'"
},
std::vector<std::string>{
"", "CONST", "INT", "FLOAT", "VOID", "IF", "ELSE", "WHILE", "BREAK",
"CONTINUE", "RETURN", "ADD", "SUB", "MUL", "DIV", "MOD", "EQ", "NE",
"LT", "LE", "GT", "GE", "AND", "OR", "NOT", "ASSIGN", "COMMA", "SEMICOLON",
"LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK", "Ident",
"ILITERAL", "FLITERAL", "STRING", "WS", "LINECOMMENT", "BLOCKCOMMENT"
}
);
static const int32_t serializedATNSegment[] = {
4,0,40,416,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,2,6,7,
6,2,7,7,7,2,8,7,8,2,9,7,9,2,10,7,10,2,11,7,11,2,12,7,12,2,13,7,13,2,14,
7,14,2,15,7,15,2,16,7,16,2,17,7,17,2,18,7,18,2,19,7,19,2,20,7,20,2,21,
7,21,2,22,7,22,2,23,7,23,2,24,7,24,2,25,7,25,2,26,7,26,2,27,7,27,2,28,
7,28,2,29,7,29,2,30,7,30,2,31,7,31,2,32,7,32,2,33,7,33,2,34,7,34,2,35,
7,35,2,36,7,36,2,37,7,37,2,38,7,38,2,39,7,39,2,40,7,40,2,41,7,41,2,42,
7,42,2,43,7,43,2,44,7,44,2,45,7,45,2,46,7,46,2,47,7,47,2,48,7,48,2,49,
7,49,2,50,7,50,2,51,7,51,2,52,7,52,2,53,7,53,2,54,7,54,2,55,7,55,2,56,
7,56,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,2,1,2,
1,3,1,3,1,3,1,3,1,3,1,4,1,4,1,4,1,5,1,5,1,5,1,5,1,5,1,6,1,6,1,6,1,6,1,
6,1,6,1,7,1,7,1,7,1,7,1,7,1,7,1,8,1,8,1,8,1,8,1,8,1,8,1,8,1,8,1,8,1,9,
1,9,1,9,1,9,1,9,1,9,1,9,1,10,1,10,1,11,1,11,1,12,1,12,1,13,1,13,1,14,
1,14,1,15,1,15,1,15,1,16,1,16,1,16,1,17,1,17,1,18,1,18,1,18,1,19,1,19,
1,20,1,20,1,20,1,21,1,21,1,21,1,22,1,22,1,22,1,23,1,23,1,24,1,24,1,25,
1,25,1,26,1,26,1,27,1,27,1,28,1,28,1,29,1,29,1,30,1,30,1,31,1,31,1,32,
1,32,1,33,1,33,1,34,1,34,1,35,1,35,1,36,1,36,1,37,1,37,1,37,1,38,1,38,
1,39,1,39,1,40,5,40,241,8,40,10,40,12,40,244,9,40,1,40,1,40,4,40,248,
8,40,11,40,12,40,249,1,40,4,40,253,8,40,11,40,12,40,254,1,40,1,40,3,40,
259,8,40,1,41,1,41,3,41,263,8,41,1,41,4,41,266,8,41,11,41,12,41,267,1,
42,1,42,3,42,272,8,42,1,42,4,42,275,8,42,11,42,12,42,276,1,42,1,42,3,
42,281,8,42,1,43,5,43,284,8,43,10,43,12,43,287,9,43,1,43,1,43,4,43,291,
8,43,11,43,12,43,292,1,43,4,43,296,8,43,11,43,12,43,297,1,43,1,43,3,43,
302,8,43,1,44,1,44,3,44,306,8,44,1,44,4,44,309,8,44,11,44,12,44,310,1,
45,1,45,1,45,1,45,1,45,1,45,4,45,319,8,45,11,45,12,45,320,1,45,1,45,3,
45,325,8,45,1,46,1,46,1,46,1,46,3,46,331,8,46,1,47,1,47,1,48,1,48,1,49,
1,49,1,50,1,50,1,50,5,50,342,8,50,10,50,12,50,345,9,50,1,51,1,51,5,51,
349,8,51,10,51,12,51,352,9,51,1,51,1,51,5,51,356,8,51,10,51,12,51,359,
9,51,1,51,1,51,4,51,363,8,51,11,51,12,51,364,3,51,367,8,51,1,52,1,52,
3,52,371,8,52,1,53,1,53,1,53,5,53,376,8,53,10,53,12,53,379,9,53,1,53,
1,53,1,54,1,54,1,54,1,54,1,55,1,55,1,55,1,55,5,55,391,8,55,10,55,12,55,
394,9,55,1,55,3,55,397,8,55,1,55,1,55,1,55,1,55,1,56,1,56,1,56,1,56,5,
56,407,8,56,10,56,12,56,410,9,56,1,56,1,56,1,56,1,56,1,56,3,377,392,408,
0,57,1,1,3,2,5,3,7,4,9,5,11,6,13,7,15,8,17,9,19,10,21,11,23,12,25,13,
27,14,29,15,31,16,33,17,35,18,37,19,39,20,41,21,43,22,45,23,47,24,49,
25,51,26,53,27,55,28,57,29,59,30,61,31,63,32,65,33,67,0,69,0,71,0,73,
0,75,0,77,0,79,0,81,0,83,0,85,0,87,0,89,0,91,0,93,0,95,0,97,0,99,0,101,
34,103,35,105,36,107,37,109,38,111,39,113,40,1,0,12,1,0,48,57,1,0,48,
55,3,0,48,57,65,70,97,102,2,0,88,88,120,120,1,0,49,57,2,0,43,43,45,45,
2,0,69,69,101,101,2,0,80,80,112,112,2,0,65,90,97,122,3,0,48,57,65,90,
97,122,3,0,65,90,95,95,97,122,3,0,9,10,13,13,32,32,429,0,1,1,0,0,0,0,
3,1,0,0,0,0,5,1,0,0,0,0,7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,0,13,1,0,0,
0,0,15,1,0,0,0,0,17,1,0,0,0,0,19,1,0,0,0,0,21,1,0,0,0,0,23,1,0,0,0,0,
25,1,0,0,0,0,27,1,0,0,0,0,29,1,0,0,0,0,31,1,0,0,0,0,33,1,0,0,0,0,35,1,
0,0,0,0,37,1,0,0,0,0,39,1,0,0,0,0,41,1,0,0,0,0,43,1,0,0,0,0,45,1,0,0,
0,0,47,1,0,0,0,0,49,1,0,0,0,0,51,1,0,0,0,0,53,1,0,0,0,0,55,1,0,0,0,0,
57,1,0,0,0,0,59,1,0,0,0,0,61,1,0,0,0,0,63,1,0,0,0,0,65,1,0,0,0,0,101,
1,0,0,0,0,103,1,0,0,0,0,105,1,0,0,0,0,107,1,0,0,0,0,109,1,0,0,0,0,111,
1,0,0,0,0,113,1,0,0,0,1,115,1,0,0,0,3,121,1,0,0,0,5,125,1,0,0,0,7,131,
1,0,0,0,9,136,1,0,0,0,11,139,1,0,0,0,13,144,1,0,0,0,15,150,1,0,0,0,17,
156,1,0,0,0,19,165,1,0,0,0,21,172,1,0,0,0,23,174,1,0,0,0,25,176,1,0,0,
0,27,178,1,0,0,0,29,180,1,0,0,0,31,182,1,0,0,0,33,185,1,0,0,0,35,188,
1,0,0,0,37,190,1,0,0,0,39,193,1,0,0,0,41,195,1,0,0,0,43,198,1,0,0,0,45,
201,1,0,0,0,47,204,1,0,0,0,49,206,1,0,0,0,51,208,1,0,0,0,53,210,1,0,0,
0,55,212,1,0,0,0,57,214,1,0,0,0,59,216,1,0,0,0,61,218,1,0,0,0,63,220,
1,0,0,0,65,222,1,0,0,0,67,224,1,0,0,0,69,226,1,0,0,0,71,228,1,0,0,0,73,
230,1,0,0,0,75,232,1,0,0,0,77,235,1,0,0,0,79,237,1,0,0,0,81,258,1,0,0,
0,83,260,1,0,0,0,85,280,1,0,0,0,87,301,1,0,0,0,89,303,1,0,0,0,91,324,
1,0,0,0,93,330,1,0,0,0,95,332,1,0,0,0,97,334,1,0,0,0,99,336,1,0,0,0,101,
338,1,0,0,0,103,366,1,0,0,0,105,370,1,0,0,0,107,372,1,0,0,0,109,382,1,
0,0,0,111,386,1,0,0,0,113,402,1,0,0,0,115,116,5,99,0,0,116,117,5,111,
0,0,117,118,5,110,0,0,118,119,5,115,0,0,119,120,5,116,0,0,120,2,1,0,0,
0,121,122,5,105,0,0,122,123,5,110,0,0,123,124,5,116,0,0,124,4,1,0,0,0,
125,126,5,102,0,0,126,127,5,108,0,0,127,128,5,111,0,0,128,129,5,97,0,
0,129,130,5,116,0,0,130,6,1,0,0,0,131,132,5,118,0,0,132,133,5,111,0,0,
133,134,5,105,0,0,134,135,5,100,0,0,135,8,1,0,0,0,136,137,5,105,0,0,137,
138,5,102,0,0,138,10,1,0,0,0,139,140,5,101,0,0,140,141,5,108,0,0,141,
142,5,115,0,0,142,143,5,101,0,0,143,12,1,0,0,0,144,145,5,119,0,0,145,
146,5,104,0,0,146,147,5,105,0,0,147,148,5,108,0,0,148,149,5,101,0,0,149,
14,1,0,0,0,150,151,5,98,0,0,151,152,5,114,0,0,152,153,5,101,0,0,153,154,
5,97,0,0,154,155,5,107,0,0,155,16,1,0,0,0,156,157,5,99,0,0,157,158,5,
111,0,0,158,159,5,110,0,0,159,160,5,116,0,0,160,161,5,105,0,0,161,162,
5,110,0,0,162,163,5,117,0,0,163,164,5,101,0,0,164,18,1,0,0,0,165,166,
5,114,0,0,166,167,5,101,0,0,167,168,5,116,0,0,168,169,5,117,0,0,169,170,
5,114,0,0,170,171,5,110,0,0,171,20,1,0,0,0,172,173,5,43,0,0,173,22,1,
0,0,0,174,175,5,45,0,0,175,24,1,0,0,0,176,177,5,42,0,0,177,26,1,0,0,0,
178,179,5,47,0,0,179,28,1,0,0,0,180,181,5,37,0,0,181,30,1,0,0,0,182,183,
5,61,0,0,183,184,5,61,0,0,184,32,1,0,0,0,185,186,5,33,0,0,186,187,5,61,
0,0,187,34,1,0,0,0,188,189,5,60,0,0,189,36,1,0,0,0,190,191,5,60,0,0,191,
192,5,61,0,0,192,38,1,0,0,0,193,194,5,62,0,0,194,40,1,0,0,0,195,196,5,
62,0,0,196,197,5,61,0,0,197,42,1,0,0,0,198,199,5,38,0,0,199,200,5,38,
0,0,200,44,1,0,0,0,201,202,5,124,0,0,202,203,5,124,0,0,203,46,1,0,0,0,
204,205,5,33,0,0,205,48,1,0,0,0,206,207,5,61,0,0,207,50,1,0,0,0,208,209,
5,44,0,0,209,52,1,0,0,0,210,211,5,59,0,0,211,54,1,0,0,0,212,213,5,40,
0,0,213,56,1,0,0,0,214,215,5,41,0,0,215,58,1,0,0,0,216,217,5,123,0,0,
217,60,1,0,0,0,218,219,5,125,0,0,219,62,1,0,0,0,220,221,5,91,0,0,221,
64,1,0,0,0,222,223,5,93,0,0,223,66,1,0,0,0,224,225,7,0,0,0,225,68,1,0,
0,0,226,227,7,1,0,0,227,70,1,0,0,0,228,229,7,2,0,0,229,72,1,0,0,0,230,
231,5,48,0,0,231,74,1,0,0,0,232,233,5,48,0,0,233,234,7,3,0,0,234,76,1,
0,0,0,235,236,7,4,0,0,236,78,1,0,0,0,237,238,7,5,0,0,238,80,1,0,0,0,239,
241,3,67,33,0,240,239,1,0,0,0,241,244,1,0,0,0,242,240,1,0,0,0,242,243,
1,0,0,0,243,245,1,0,0,0,244,242,1,0,0,0,245,247,5,46,0,0,246,248,3,67,
33,0,247,246,1,0,0,0,248,249,1,0,0,0,249,247,1,0,0,0,249,250,1,0,0,0,
250,259,1,0,0,0,251,253,3,67,33,0,252,251,1,0,0,0,253,254,1,0,0,0,254,
252,1,0,0,0,254,255,1,0,0,0,255,256,1,0,0,0,256,257,5,46,0,0,257,259,
1,0,0,0,258,242,1,0,0,0,258,252,1,0,0,0,259,82,1,0,0,0,260,262,7,6,0,
0,261,263,3,79,39,0,262,261,1,0,0,0,262,263,1,0,0,0,263,265,1,0,0,0,264,
266,3,67,33,0,265,264,1,0,0,0,266,267,1,0,0,0,267,265,1,0,0,0,267,268,
1,0,0,0,268,84,1,0,0,0,269,271,3,81,40,0,270,272,3,83,41,0,271,270,1,
0,0,0,271,272,1,0,0,0,272,281,1,0,0,0,273,275,3,67,33,0,274,273,1,0,0,
0,275,276,1,0,0,0,276,274,1,0,0,0,276,277,1,0,0,0,277,278,1,0,0,0,278,
279,3,83,41,0,279,281,1,0,0,0,280,269,1,0,0,0,280,274,1,0,0,0,281,86,
1,0,0,0,282,284,3,71,35,0,283,282,1,0,0,0,284,287,1,0,0,0,285,283,1,0,
0,0,285,286,1,0,0,0,286,288,1,0,0,0,287,285,1,0,0,0,288,290,5,46,0,0,
289,291,3,71,35,0,290,289,1,0,0,0,291,292,1,0,0,0,292,290,1,0,0,0,292,
293,1,0,0,0,293,302,1,0,0,0,294,296,3,71,35,0,295,294,1,0,0,0,296,297,
1,0,0,0,297,295,1,0,0,0,297,298,1,0,0,0,298,299,1,0,0,0,299,300,5,46,
0,0,300,302,1,0,0,0,301,285,1,0,0,0,301,295,1,0,0,0,302,88,1,0,0,0,303,
305,7,7,0,0,304,306,3,79,39,0,305,304,1,0,0,0,305,306,1,0,0,0,306,308,
1,0,0,0,307,309,3,67,33,0,308,307,1,0,0,0,309,310,1,0,0,0,310,308,1,0,
0,0,310,311,1,0,0,0,311,90,1,0,0,0,312,313,3,75,37,0,313,314,3,87,43,
0,314,315,3,89,44,0,315,325,1,0,0,0,316,318,3,75,37,0,317,319,3,71,35,
0,318,317,1,0,0,0,319,320,1,0,0,0,320,318,1,0,0,0,320,321,1,0,0,0,321,
322,1,0,0,0,322,323,3,89,44,0,323,325,1,0,0,0,324,312,1,0,0,0,324,316,
1,0,0,0,325,92,1,0,0,0,326,327,5,92,0,0,327,331,5,34,0,0,328,329,5,92,
0,0,329,331,5,92,0,0,330,326,1,0,0,0,330,328,1,0,0,0,331,94,1,0,0,0,332,
333,7,8,0,0,333,96,1,0,0,0,334,335,7,9,0,0,335,98,1,0,0,0,336,337,7,10,
0,0,337,100,1,0,0,0,338,343,3,99,49,0,339,342,3,97,48,0,340,342,5,95,
0,0,341,339,1,0,0,0,341,340,1,0,0,0,342,345,1,0,0,0,343,341,1,0,0,0,343,
344,1,0,0,0,344,102,1,0,0,0,345,343,1,0,0,0,346,350,3,77,38,0,347,349,
3,67,33,0,348,347,1,0,0,0,349,352,1,0,0,0,350,348,1,0,0,0,350,351,1,0,
0,0,351,367,1,0,0,0,352,350,1,0,0,0,353,357,3,73,36,0,354,356,3,69,34,
0,355,354,1,0,0,0,356,359,1,0,0,0,357,355,1,0,0,0,357,358,1,0,0,0,358,
367,1,0,0,0,359,357,1,0,0,0,360,362,3,75,37,0,361,363,3,71,35,0,362,361,
1,0,0,0,363,364,1,0,0,0,364,362,1,0,0,0,364,365,1,0,0,0,365,367,1,0,0,
0,366,346,1,0,0,0,366,353,1,0,0,0,366,360,1,0,0,0,367,104,1,0,0,0,368,
371,3,85,42,0,369,371,3,91,45,0,370,368,1,0,0,0,370,369,1,0,0,0,371,106,
1,0,0,0,372,377,5,34,0,0,373,376,3,93,46,0,374,376,9,0,0,0,375,373,1,
0,0,0,375,374,1,0,0,0,376,379,1,0,0,0,377,378,1,0,0,0,377,375,1,0,0,0,
378,380,1,0,0,0,379,377,1,0,0,0,380,381,5,34,0,0,381,108,1,0,0,0,382,
383,7,11,0,0,383,384,1,0,0,0,384,385,6,54,0,0,385,110,1,0,0,0,386,387,
5,47,0,0,387,388,5,47,0,0,388,392,1,0,0,0,389,391,9,0,0,0,390,389,1,0,
0,0,391,394,1,0,0,0,392,393,1,0,0,0,392,390,1,0,0,0,393,396,1,0,0,0,394,
392,1,0,0,0,395,397,5,13,0,0,396,395,1,0,0,0,396,397,1,0,0,0,397,398,
1,0,0,0,398,399,5,10,0,0,399,400,1,0,0,0,400,401,6,55,0,0,401,112,1,0,
0,0,402,403,5,47,0,0,403,404,5,42,0,0,404,408,1,0,0,0,405,407,9,0,0,0,
406,405,1,0,0,0,407,410,1,0,0,0,408,409,1,0,0,0,408,406,1,0,0,0,409,411,
1,0,0,0,410,408,1,0,0,0,411,412,5,42,0,0,412,413,5,47,0,0,413,414,1,0,
0,0,414,415,6,56,0,0,415,114,1,0,0,0,31,0,242,249,254,258,262,267,271,
276,280,285,292,297,301,305,310,320,324,330,341,343,350,357,364,366,370,
375,377,392,396,408,1,6,0,0
};
staticData->serializedATN = antlr4::atn::SerializedATNView(serializedATNSegment, sizeof(serializedATNSegment) / sizeof(serializedATNSegment[0]));
antlr4::atn::ATNDeserializer deserializer;
staticData->atn = deserializer.deserialize(staticData->serializedATN);
const size_t count = staticData->atn->getNumberOfDecisions();
staticData->decisionToDFA.reserve(count);
for (size_t i = 0; i < count; i++) {
staticData->decisionToDFA.emplace_back(staticData->atn->getDecisionState(i), i);
}
sysylexerLexerStaticData = std::move(staticData);
}
}
SysYLexer::SysYLexer(CharStream *input) : Lexer(input) {
SysYLexer::initialize();
_interpreter = new atn::LexerATNSimulator(this, *sysylexerLexerStaticData->atn, sysylexerLexerStaticData->decisionToDFA, sysylexerLexerStaticData->sharedContextCache);
}
SysYLexer::~SysYLexer() {
delete _interpreter;
}
std::string SysYLexer::getGrammarFileName() const {
return "SysY.g4";
}
const std::vector<std::string>& SysYLexer::getRuleNames() const {
return sysylexerLexerStaticData->ruleNames;
}
const std::vector<std::string>& SysYLexer::getChannelNames() const {
return sysylexerLexerStaticData->channelNames;
}
const std::vector<std::string>& SysYLexer::getModeNames() const {
return sysylexerLexerStaticData->modeNames;
}
const dfa::Vocabulary& SysYLexer::getVocabulary() const {
return sysylexerLexerStaticData->vocabulary;
}
antlr4::atn::SerializedATNView SysYLexer::getSerializedATN() const {
return sysylexerLexerStaticData->serializedATN;
}
const atn::ATN& SysYLexer::getATN() const {
return *sysylexerLexerStaticData->atn;
}
void SysYLexer::initialize() {
#if ANTLR4_USE_THREAD_LOCAL_CACHE
sysylexerLexerInitialize();
#else
::antlr4::internal::call_once(sysylexerLexerOnceFlag, sysylexerLexerInitialize);
#endif
}

154
src/SysYLexer.interp Normal file

File diff suppressed because one or more lines are too long

73
src/SysYLexer.tokens Normal file
View File

@ -0,0 +1,73 @@
CONST=1
INT=2
FLOAT=3
VOID=4
IF=5
ELSE=6
WHILE=7
BREAK=8
CONTINUE=9
RETURN=10
ADD=11
SUB=12
MUL=13
DIV=14
MOD=15
EQ=16
NE=17
LT=18
LE=19
GT=20
GE=21
AND=22
OR=23
NOT=24
ASSIGN=25
COMMA=26
SEMICOLON=27
LPAREN=28
RPAREN=29
LBRACE=30
RBRACE=31
LBRACK=32
RBRACK=33
Ident=34
ILITERAL=35
FLITERAL=36
STRING=37
WS=38
LINECOMMENT=39
BLOCKCOMMENT=40
'const'=1
'int'=2
'float'=3
'void'=4
'if'=5
'else'=6
'while'=7
'break'=8
'continue'=9
'return'=10
'+'=11
'-'=12
'*'=13
'/'=14
'%'=15
'=='=16
'!='=17
'<'=18
'<='=19
'>'=20
'>='=21
'&&'=22
'||'=23
'!'=24
'='=25
','=26
';'=27
'('=28
')'=29
'{'=30
'}'=31
'['=32
']'=33

3401
src/SysYParser.cpp Normal file

File diff suppressed because it is too large Load Diff

7
src/SysYVisitor.cpp Normal file
View File

@ -0,0 +1,7 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#include "SysYVisitor.h"

View File

@ -1,78 +0,0 @@
#pragma once
#include "SysYBaseVisitor.h"
#include "SysYParser.h"
#include "IR.h"
#include "IRBuilder.h"
#include <sstream>
#include <map>
#include <vector>
#include <stack>
class LLVMIRGenerator : public SysYBaseVisitor {
public:
sysy::Module* getIRModule() const { return irModule.get(); }
std::string generateIR(SysYParser::CompUnitContext* unit);
std::string getIR() const { return irStream.str(); }
private:
std::unique_ptr<sysy::Module> irModule; // IR数据结构
std::stringstream irStream; // 文本输出流
sysy::IRBuilder irBuilder; // IR构建器
int tempCounter = 0;
std::string currentVarType;
// std::map<std::string, sysy::Value*> symbolTable;
std::map<std::string, std::pair<std::string, std::string>> symbolTable;
std::map<std::string, std::string> tmpTable;
std::vector<std::string> globalVars;
std::string currentFunction;
std::string currentReturnType;
std::vector<std::string> breakStack;
std::vector<std::string> continueStack;
bool hasReturn = false;
struct LoopLabels {
std::string breakLabel; // break跳转的目标标签
std::string continueLabel; // continue跳转的目标标签
};
std::stack<LoopLabels> loopStack; // 用于管理循环的break和continue标签
std::string getNextTemp();
std::string getLLVMType(const std::string&);
sysy::Type* getSysYType(const std::string&);
bool inFunction = false; // 标识当前是否处于函数内部
// 访问方法
std::any visitCompUnit(SysYParser::CompUnitContext* ctx);
std::any visitConstDecl(SysYParser::ConstDeclContext* ctx);
std::any visitVarDecl(SysYParser::VarDeclContext* ctx);
std::any visitVarDef(SysYParser::VarDefContext* ctx);
std::any visitFuncDef(SysYParser::FuncDefContext* ctx);
std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx);
// std::any visitStmt(SysYParser::StmtContext* ctx);
std::any visitLValue(SysYParser::LValueContext* ctx);
std::any visitPrimaryExp(SysYParser::PrimaryExpContext* ctx);
std::any visitPrimExp(SysYParser::PrimExpContext* ctx);
std::any visitParenExp(SysYParser::ParenExpContext* ctx);
std::any visitNumber(SysYParser::NumberContext* ctx);
std::any visitString(SysYParser::StringContext* ctx);
std::any visitCall(SysYParser::CallContext *ctx);
std::any visitUnExp(SysYParser::UnExpContext* ctx);
std::any visitMulExp(SysYParser::MulExpContext* ctx);
std::any visitAddExp(SysYParser::AddExpContext* ctx);
std::any visitRelExp(SysYParser::RelExpContext* ctx);
std::any visitEqExp(SysYParser::EqExpContext* ctx);
std::any visitLAndExp(SysYParser::LAndExpContext* ctx);
std::any visitLOrExp(SysYParser::LOrExpContext* ctx);
std::any visitAssignStmt(SysYParser::AssignStmtContext *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;
// 统一创建二元操作(同时生成数据结构和文本)
sysy::Value* createBinaryOp(SysYParser::ExpContext* lhs,
SysYParser::ExpContext* rhs,
sysy::Instruction::Kind opKind);
};

View File

@ -0,0 +1,196 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#pragma once
#include "antlr4-runtime.h"
#include "SysYVisitor.h"
/**
* This class provides an empty implementation of SysYVisitor, which can be
* extended to create a visitor which only needs to handle a subset of the available methods.
*/
class SysYBaseVisitor : public SysYVisitor {
public:
virtual std::any visitCompUnit(SysYParser::CompUnitContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitDecl(SysYParser::DeclContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitConstDecl(SysYParser::ConstDeclContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitBType(SysYParser::BTypeContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitConstDef(SysYParser::ConstDefContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitConstArrayInitValue(SysYParser::ConstArrayInitValueContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitVarDecl(SysYParser::VarDeclContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitVarDef(SysYParser::VarDefContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitScalarInitValue(SysYParser::ScalarInitValueContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitArrayInitValue(SysYParser::ArrayInitValueContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitFuncType(SysYParser::FuncTypeContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitFuncDef(SysYParser::FuncDefContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitFuncFParams(SysYParser::FuncFParamsContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitFuncFParam(SysYParser::FuncFParamContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitBlockStmt(SysYParser::BlockStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitBlockItem(SysYParser::BlockItemContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitAssignStmt(SysYParser::AssignStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitExpStmt(SysYParser::ExpStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitBlkStmt(SysYParser::BlkStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitIfStmt(SysYParser::IfStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitWhileStmt(SysYParser::WhileStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitBreakStmt(SysYParser::BreakStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitContinueStmt(SysYParser::ContinueStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitExp(SysYParser::ExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitCond(SysYParser::CondContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitLValue(SysYParser::LValueContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitNumber(SysYParser::NumberContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitCall(SysYParser::CallContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitUnaryOp(SysYParser::UnaryOpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitString(SysYParser::StringContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitMulExp(SysYParser::MulExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitAddExp(SysYParser::AddExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitRelExp(SysYParser::RelExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitEqExp(SysYParser::EqExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitLAndExp(SysYParser::LAndExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitLOrExp(SysYParser::LOrExpContext *ctx) override {
return visitChildren(ctx);
}
virtual std::any visitConstExp(SysYParser::ConstExpContext *ctx) override {
return visitChildren(ctx);
}
};

55
src/include/SysYLexer.h Normal file
View File

@ -0,0 +1,55 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#pragma once
#include "antlr4-runtime.h"
class SysYLexer : public antlr4::Lexer {
public:
enum {
CONST = 1, INT = 2, FLOAT = 3, VOID = 4, IF = 5, ELSE = 6, WHILE = 7,
BREAK = 8, CONTINUE = 9, RETURN = 10, ADD = 11, SUB = 12, MUL = 13,
DIV = 14, MOD = 15, EQ = 16, NE = 17, LT = 18, LE = 19, GT = 20, GE = 21,
AND = 22, OR = 23, NOT = 24, ASSIGN = 25, COMMA = 26, SEMICOLON = 27,
LPAREN = 28, RPAREN = 29, LBRACE = 30, RBRACE = 31, LBRACK = 32, RBRACK = 33,
Ident = 34, ILITERAL = 35, FLITERAL = 36, STRING = 37, WS = 38, LINECOMMENT = 39,
BLOCKCOMMENT = 40
};
explicit SysYLexer(antlr4::CharStream *input);
~SysYLexer() override;
std::string getGrammarFileName() const override;
const std::vector<std::string>& getRuleNames() const override;
const std::vector<std::string>& getChannelNames() const override;
const std::vector<std::string>& getModeNames() const override;
const antlr4::dfa::Vocabulary& getVocabulary() const override;
antlr4::atn::SerializedATNView getSerializedATN() const override;
const antlr4::atn::ATN& getATN() const override;
// By default the static state used to implement the lexer is lazily initialized during the first
// call to the constructor. You can call this function if you wish to initialize the static state
// ahead of time.
static void initialize();
private:
// Individual action functions triggered by action() above.
// Individual semantic predicate functions triggered by sempred() above.
};

808
src/include/SysYParser.h Normal file
View File

@ -0,0 +1,808 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#pragma once
#include "antlr4-runtime.h"
class SysYParser : public antlr4::Parser {
public:
enum {
CONST = 1, INT = 2, FLOAT = 3, VOID = 4, IF = 5, ELSE = 6, WHILE = 7,
BREAK = 8, CONTINUE = 9, RETURN = 10, ADD = 11, SUB = 12, MUL = 13,
DIV = 14, MOD = 15, EQ = 16, NE = 17, LT = 18, LE = 19, GT = 20, GE = 21,
AND = 22, OR = 23, NOT = 24, ASSIGN = 25, COMMA = 26, SEMICOLON = 27,
LPAREN = 28, RPAREN = 29, LBRACE = 30, RBRACE = 31, LBRACK = 32, RBRACK = 33,
Ident = 34, ILITERAL = 35, FLITERAL = 36, STRING = 37, WS = 38, LINECOMMENT = 39,
BLOCKCOMMENT = 40
};
enum {
RuleCompUnit = 0, RuleGlobalDecl = 1, RuleDecl = 2, RuleConstDecl = 3,
RuleBType = 4, RuleConstDef = 5, RuleConstInitVal = 6, RuleVarDecl = 7,
RuleVarDef = 8, RuleInitVal = 9, RuleFuncType = 10, RuleFuncDef = 11,
RuleFuncFParams = 12, RuleFuncFParam = 13, RuleBlockStmt = 14, RuleBlockItem = 15,
RuleStmt = 16, RuleExp = 17, RuleCond = 18, RuleLValue = 19, RulePrimaryExp = 20,
RuleNumber = 21, RuleCall = 22, RuleUnaryExp = 23, RuleUnaryOp = 24,
RuleFuncRParams = 25, RuleString = 26, RuleMulExp = 27, RuleAddExp = 28,
RuleRelExp = 29, RuleEqExp = 30, RuleLAndExp = 31, RuleLOrExp = 32,
RuleConstExp = 33
};
explicit SysYParser(antlr4::TokenStream *input);
SysYParser(antlr4::TokenStream *input, const antlr4::atn::ParserATNSimulatorOptions &options);
~SysYParser() override;
std::string getGrammarFileName() const override;
const antlr4::atn::ATN& getATN() const override;
const std::vector<std::string>& getRuleNames() const override;
const antlr4::dfa::Vocabulary& getVocabulary() const override;
antlr4::atn::SerializedATNView getSerializedATN() const override;
class CompUnitContext;
class GlobalDeclContext;
class DeclContext;
class ConstDeclContext;
class BTypeContext;
class ConstDefContext;
class ConstInitValContext;
class VarDeclContext;
class VarDefContext;
class InitValContext;
class FuncTypeContext;
class FuncDefContext;
class FuncFParamsContext;
class FuncFParamContext;
class BlockStmtContext;
class BlockItemContext;
class StmtContext;
class ExpContext;
class CondContext;
class LValueContext;
class PrimaryExpContext;
class NumberContext;
class CallContext;
class UnaryExpContext;
class UnaryOpContext;
class FuncRParamsContext;
class StringContext;
class MulExpContext;
class AddExpContext;
class RelExpContext;
class EqExpContext;
class LAndExpContext;
class LOrExpContext;
class ConstExpContext;
class CompUnitContext : public antlr4::ParserRuleContext {
public:
CompUnitContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<GlobalDeclContext *> globalDecl();
GlobalDeclContext* globalDecl(size_t i);
std::vector<FuncDefContext *> funcDef();
FuncDefContext* funcDef(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
CompUnitContext* compUnit();
class GlobalDeclContext : public antlr4::ParserRuleContext {
public:
GlobalDeclContext(antlr4::ParserRuleContext *parent, size_t invokingState);
GlobalDeclContext() = default;
void copyFrom(GlobalDeclContext *context);
using antlr4::ParserRuleContext::copyFrom;
virtual size_t getRuleIndex() const override;
};
class GlobalConstDeclContext : public GlobalDeclContext {
public:
GlobalConstDeclContext(GlobalDeclContext *ctx);
ConstDeclContext *constDecl();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class GlobalVarDeclContext : public GlobalDeclContext {
public:
GlobalVarDeclContext(GlobalDeclContext *ctx);
VarDeclContext *varDecl();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
GlobalDeclContext* globalDecl();
class DeclContext : public antlr4::ParserRuleContext {
public:
DeclContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
ConstDeclContext *constDecl();
VarDeclContext *varDecl();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
DeclContext* decl();
class ConstDeclContext : public antlr4::ParserRuleContext {
public:
ConstDeclContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *CONST();
BTypeContext *bType();
std::vector<ConstDefContext *> constDef();
ConstDefContext* constDef(size_t i);
antlr4::tree::TerminalNode *SEMICOLON();
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ConstDeclContext* constDecl();
class BTypeContext : public antlr4::ParserRuleContext {
public:
BTypeContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *INT();
antlr4::tree::TerminalNode *FLOAT();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
BTypeContext* bType();
class ConstDefContext : public antlr4::ParserRuleContext {
public:
ConstDefContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *Ident();
antlr4::tree::TerminalNode *ASSIGN();
ConstInitValContext *constInitVal();
std::vector<antlr4::tree::TerminalNode *> LBRACK();
antlr4::tree::TerminalNode* LBRACK(size_t i);
std::vector<ConstExpContext *> constExp();
ConstExpContext* constExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> RBRACK();
antlr4::tree::TerminalNode* RBRACK(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ConstDefContext* constDef();
class ConstInitValContext : public antlr4::ParserRuleContext {
public:
ConstInitValContext(antlr4::ParserRuleContext *parent, size_t invokingState);
ConstInitValContext() = default;
void copyFrom(ConstInitValContext *context);
using antlr4::ParserRuleContext::copyFrom;
virtual size_t getRuleIndex() const override;
};
class ConstScalarInitValueContext : public ConstInitValContext {
public:
ConstScalarInitValueContext(ConstInitValContext *ctx);
ConstExpContext *constExp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class ConstArrayInitValueContext : public ConstInitValContext {
public:
ConstArrayInitValueContext(ConstInitValContext *ctx);
antlr4::tree::TerminalNode *LBRACE();
antlr4::tree::TerminalNode *RBRACE();
std::vector<ConstInitValContext *> constInitVal();
ConstInitValContext* constInitVal(size_t i);
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ConstInitValContext* constInitVal();
class VarDeclContext : public antlr4::ParserRuleContext {
public:
VarDeclContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
BTypeContext *bType();
std::vector<VarDefContext *> varDef();
VarDefContext* varDef(size_t i);
antlr4::tree::TerminalNode *SEMICOLON();
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
VarDeclContext* varDecl();
class VarDefContext : public antlr4::ParserRuleContext {
public:
VarDefContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *Ident();
std::vector<antlr4::tree::TerminalNode *> LBRACK();
antlr4::tree::TerminalNode* LBRACK(size_t i);
std::vector<ConstExpContext *> constExp();
ConstExpContext* constExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> RBRACK();
antlr4::tree::TerminalNode* RBRACK(size_t i);
antlr4::tree::TerminalNode *ASSIGN();
InitValContext *initVal();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
VarDefContext* varDef();
class InitValContext : public antlr4::ParserRuleContext {
public:
InitValContext(antlr4::ParserRuleContext *parent, size_t invokingState);
InitValContext() = default;
void copyFrom(InitValContext *context);
using antlr4::ParserRuleContext::copyFrom;
virtual size_t getRuleIndex() const override;
};
class ArrayInitValueContext : public InitValContext {
public:
ArrayInitValueContext(InitValContext *ctx);
antlr4::tree::TerminalNode *LBRACE();
antlr4::tree::TerminalNode *RBRACE();
std::vector<InitValContext *> initVal();
InitValContext* initVal(size_t i);
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class ScalarInitValueContext : public InitValContext {
public:
ScalarInitValueContext(InitValContext *ctx);
ExpContext *exp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
InitValContext* initVal();
class FuncTypeContext : public antlr4::ParserRuleContext {
public:
FuncTypeContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *VOID();
antlr4::tree::TerminalNode *INT();
antlr4::tree::TerminalNode *FLOAT();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
FuncTypeContext* funcType();
class FuncDefContext : public antlr4::ParserRuleContext {
public:
FuncDefContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
FuncTypeContext *funcType();
antlr4::tree::TerminalNode *Ident();
antlr4::tree::TerminalNode *LPAREN();
antlr4::tree::TerminalNode *RPAREN();
BlockStmtContext *blockStmt();
FuncFParamsContext *funcFParams();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
FuncDefContext* funcDef();
class FuncFParamsContext : public antlr4::ParserRuleContext {
public:
FuncFParamsContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<FuncFParamContext *> funcFParam();
FuncFParamContext* funcFParam(size_t i);
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
FuncFParamsContext* funcFParams();
class FuncFParamContext : public antlr4::ParserRuleContext {
public:
FuncFParamContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
BTypeContext *bType();
antlr4::tree::TerminalNode *Ident();
std::vector<antlr4::tree::TerminalNode *> LBRACK();
antlr4::tree::TerminalNode* LBRACK(size_t i);
std::vector<antlr4::tree::TerminalNode *> RBRACK();
antlr4::tree::TerminalNode* RBRACK(size_t i);
std::vector<ExpContext *> exp();
ExpContext* exp(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
FuncFParamContext* funcFParam();
class BlockStmtContext : public antlr4::ParserRuleContext {
public:
BlockStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *LBRACE();
antlr4::tree::TerminalNode *RBRACE();
std::vector<BlockItemContext *> blockItem();
BlockItemContext* blockItem(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
BlockStmtContext* blockStmt();
class BlockItemContext : public antlr4::ParserRuleContext {
public:
BlockItemContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
DeclContext *decl();
StmtContext *stmt();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
BlockItemContext* blockItem();
class StmtContext : public antlr4::ParserRuleContext {
public:
StmtContext(antlr4::ParserRuleContext *parent, size_t invokingState);
StmtContext() = default;
void copyFrom(StmtContext *context);
using antlr4::ParserRuleContext::copyFrom;
virtual size_t getRuleIndex() const override;
};
class BlkStmtContext : public StmtContext {
public:
BlkStmtContext(StmtContext *ctx);
BlockStmtContext *blockStmt();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class WhileStmtContext : public StmtContext {
public:
WhileStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *WHILE();
antlr4::tree::TerminalNode *LPAREN();
CondContext *cond();
antlr4::tree::TerminalNode *RPAREN();
StmtContext *stmt();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class IfStmtContext : public StmtContext {
public:
IfStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *IF();
antlr4::tree::TerminalNode *LPAREN();
CondContext *cond();
antlr4::tree::TerminalNode *RPAREN();
std::vector<StmtContext *> stmt();
StmtContext* stmt(size_t i);
antlr4::tree::TerminalNode *ELSE();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class AssignStmtContext : public StmtContext {
public:
AssignStmtContext(StmtContext *ctx);
LValueContext *lValue();
antlr4::tree::TerminalNode *ASSIGN();
ExpContext *exp();
antlr4::tree::TerminalNode *SEMICOLON();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class BreakStmtContext : public StmtContext {
public:
BreakStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *BREAK();
antlr4::tree::TerminalNode *SEMICOLON();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class ExpStmtContext : public StmtContext {
public:
ExpStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *SEMICOLON();
ExpContext *exp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class ReturnStmtContext : public StmtContext {
public:
ReturnStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *RETURN();
antlr4::tree::TerminalNode *SEMICOLON();
ExpContext *exp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
class ContinueStmtContext : public StmtContext {
public:
ContinueStmtContext(StmtContext *ctx);
antlr4::tree::TerminalNode *CONTINUE();
antlr4::tree::TerminalNode *SEMICOLON();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
StmtContext* stmt();
class ExpContext : public antlr4::ParserRuleContext {
public:
ExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
AddExpContext *addExp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ExpContext* exp();
class CondContext : public antlr4::ParserRuleContext {
public:
CondContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
LOrExpContext *lOrExp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
CondContext* cond();
class LValueContext : public antlr4::ParserRuleContext {
public:
LValueContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *Ident();
std::vector<antlr4::tree::TerminalNode *> LBRACK();
antlr4::tree::TerminalNode* LBRACK(size_t i);
std::vector<ExpContext *> exp();
ExpContext* exp(size_t i);
std::vector<antlr4::tree::TerminalNode *> RBRACK();
antlr4::tree::TerminalNode* RBRACK(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
LValueContext* lValue();
class PrimaryExpContext : public antlr4::ParserRuleContext {
public:
PrimaryExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *LPAREN();
ExpContext *exp();
antlr4::tree::TerminalNode *RPAREN();
LValueContext *lValue();
NumberContext *number();
StringContext *string();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
PrimaryExpContext* primaryExp();
class NumberContext : public antlr4::ParserRuleContext {
public:
NumberContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *ILITERAL();
antlr4::tree::TerminalNode *FLITERAL();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
NumberContext* number();
class CallContext : public antlr4::ParserRuleContext {
public:
CallContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *Ident();
antlr4::tree::TerminalNode *LPAREN();
antlr4::tree::TerminalNode *RPAREN();
FuncRParamsContext *funcRParams();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
CallContext* call();
class UnaryExpContext : public antlr4::ParserRuleContext {
public:
UnaryExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
PrimaryExpContext *primaryExp();
CallContext *call();
UnaryOpContext *unaryOp();
UnaryExpContext *unaryExp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
UnaryExpContext* unaryExp();
class UnaryOpContext : public antlr4::ParserRuleContext {
public:
UnaryOpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *ADD();
antlr4::tree::TerminalNode *SUB();
antlr4::tree::TerminalNode *NOT();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
UnaryOpContext* unaryOp();
class FuncRParamsContext : public antlr4::ParserRuleContext {
public:
FuncRParamsContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<ExpContext *> exp();
ExpContext* exp(size_t i);
std::vector<antlr4::tree::TerminalNode *> COMMA();
antlr4::tree::TerminalNode* COMMA(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
FuncRParamsContext* funcRParams();
class StringContext : public antlr4::ParserRuleContext {
public:
StringContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *STRING();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
StringContext* string();
class MulExpContext : public antlr4::ParserRuleContext {
public:
MulExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<UnaryExpContext *> unaryExp();
UnaryExpContext* unaryExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> MUL();
antlr4::tree::TerminalNode* MUL(size_t i);
std::vector<antlr4::tree::TerminalNode *> DIV();
antlr4::tree::TerminalNode* DIV(size_t i);
std::vector<antlr4::tree::TerminalNode *> MOD();
antlr4::tree::TerminalNode* MOD(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
MulExpContext* mulExp();
class AddExpContext : public antlr4::ParserRuleContext {
public:
AddExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<MulExpContext *> mulExp();
MulExpContext* mulExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> ADD();
antlr4::tree::TerminalNode* ADD(size_t i);
std::vector<antlr4::tree::TerminalNode *> SUB();
antlr4::tree::TerminalNode* SUB(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
AddExpContext* addExp();
class RelExpContext : public antlr4::ParserRuleContext {
public:
RelExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<AddExpContext *> addExp();
AddExpContext* addExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> LT();
antlr4::tree::TerminalNode* LT(size_t i);
std::vector<antlr4::tree::TerminalNode *> GT();
antlr4::tree::TerminalNode* GT(size_t i);
std::vector<antlr4::tree::TerminalNode *> LE();
antlr4::tree::TerminalNode* LE(size_t i);
std::vector<antlr4::tree::TerminalNode *> GE();
antlr4::tree::TerminalNode* GE(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
RelExpContext* relExp();
class EqExpContext : public antlr4::ParserRuleContext {
public:
EqExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<RelExpContext *> relExp();
RelExpContext* relExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> EQ();
antlr4::tree::TerminalNode* EQ(size_t i);
std::vector<antlr4::tree::TerminalNode *> NE();
antlr4::tree::TerminalNode* NE(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
EqExpContext* eqExp();
class LAndExpContext : public antlr4::ParserRuleContext {
public:
LAndExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<EqExpContext *> eqExp();
EqExpContext* eqExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> AND();
antlr4::tree::TerminalNode* AND(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
LAndExpContext* lAndExp();
class LOrExpContext : public antlr4::ParserRuleContext {
public:
LOrExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
std::vector<LAndExpContext *> lAndExp();
LAndExpContext* lAndExp(size_t i);
std::vector<antlr4::tree::TerminalNode *> OR();
antlr4::tree::TerminalNode* OR(size_t i);
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
LOrExpContext* lOrExp();
class ConstExpContext : public antlr4::ParserRuleContext {
public:
ConstExpContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
AddExpContext *addExp();
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
ConstExpContext* constExp();
// By default the static state used to implement the parser is lazily initialized during the first
// call to the constructor. You can call this function if you wish to initialize the static state
// ahead of time.
static void initialize();
private:
};

112
src/include/SysYVisitor.h Normal file
View File

@ -0,0 +1,112 @@
// Generated from SysY.g4 by ANTLR 4.13.2
#pragma once
#include "antlr4-runtime.h"
#include "SysYParser.h"
/**
* This class defines an abstract visitor for a parse tree
* produced by SysYParser.
*/
class SysYVisitor : public antlr4::tree::AbstractParseTreeVisitor {
public:
/**
* Visit parse trees produced by SysYParser.
*/
virtual std::any visitCompUnit(SysYParser::CompUnitContext *context) = 0;
virtual std::any visitGlobalConstDecl(SysYParser::GlobalConstDeclContext *context) = 0;
virtual std::any visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *context) = 0;
virtual std::any visitDecl(SysYParser::DeclContext *context) = 0;
virtual std::any visitConstDecl(SysYParser::ConstDeclContext *context) = 0;
virtual std::any visitBType(SysYParser::BTypeContext *context) = 0;
virtual std::any visitConstDef(SysYParser::ConstDefContext *context) = 0;
virtual std::any visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *context) = 0;
virtual std::any visitConstArrayInitValue(SysYParser::ConstArrayInitValueContext *context) = 0;
virtual std::any visitVarDecl(SysYParser::VarDeclContext *context) = 0;
virtual std::any visitVarDef(SysYParser::VarDefContext *context) = 0;
virtual std::any visitScalarInitValue(SysYParser::ScalarInitValueContext *context) = 0;
virtual std::any visitArrayInitValue(SysYParser::ArrayInitValueContext *context) = 0;
virtual std::any visitFuncType(SysYParser::FuncTypeContext *context) = 0;
virtual std::any visitFuncDef(SysYParser::FuncDefContext *context) = 0;
virtual std::any visitFuncFParams(SysYParser::FuncFParamsContext *context) = 0;
virtual std::any visitFuncFParam(SysYParser::FuncFParamContext *context) = 0;
virtual std::any visitBlockStmt(SysYParser::BlockStmtContext *context) = 0;
virtual std::any visitBlockItem(SysYParser::BlockItemContext *context) = 0;
virtual std::any visitAssignStmt(SysYParser::AssignStmtContext *context) = 0;
virtual std::any visitExpStmt(SysYParser::ExpStmtContext *context) = 0;
virtual std::any visitBlkStmt(SysYParser::BlkStmtContext *context) = 0;
virtual std::any visitIfStmt(SysYParser::IfStmtContext *context) = 0;
virtual std::any visitWhileStmt(SysYParser::WhileStmtContext *context) = 0;
virtual std::any visitBreakStmt(SysYParser::BreakStmtContext *context) = 0;
virtual std::any visitContinueStmt(SysYParser::ContinueStmtContext *context) = 0;
virtual std::any visitReturnStmt(SysYParser::ReturnStmtContext *context) = 0;
virtual std::any visitExp(SysYParser::ExpContext *context) = 0;
virtual std::any visitCond(SysYParser::CondContext *context) = 0;
virtual std::any visitLValue(SysYParser::LValueContext *context) = 0;
virtual std::any visitPrimaryExp(SysYParser::PrimaryExpContext *context) = 0;
virtual std::any visitNumber(SysYParser::NumberContext *context) = 0;
virtual std::any visitCall(SysYParser::CallContext *context) = 0;
virtual std::any visitUnaryExp(SysYParser::UnaryExpContext *context) = 0;
virtual std::any visitUnaryOp(SysYParser::UnaryOpContext *context) = 0;
virtual std::any visitFuncRParams(SysYParser::FuncRParamsContext *context) = 0;
virtual std::any visitString(SysYParser::StringContext *context) = 0;
virtual std::any visitMulExp(SysYParser::MulExpContext *context) = 0;
virtual std::any visitAddExp(SysYParser::AddExpContext *context) = 0;
virtual std::any visitRelExp(SysYParser::RelExpContext *context) = 0;
virtual std::any visitEqExp(SysYParser::EqExpContext *context) = 0;
virtual std::any visitLAndExp(SysYParser::LAndExpContext *context) = 0;
virtual std::any visitLOrExp(SysYParser::LOrExpContext *context) = 0;
virtual std::any visitConstExp(SysYParser::ConstExpContext *context) = 0;
};