[IR Gen] debugging SIGSEGV
This commit is contained in:
@ -533,6 +533,7 @@ std::any LLVMIRGenerator::visitUnExp(SysYParser::UnExpContext* ctx) {
|
||||
}
|
||||
return ctx->unaryExp()->accept(this);
|
||||
}
|
||||
|
||||
std::any LLVMIRGenerator::visitCall(SysYParser::CallContext *ctx)
|
||||
{
|
||||
std::string funcName = ctx->Ident()->getText();
|
||||
|
||||
@ -180,7 +180,7 @@ std::any LLVMIRGenerator::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
|
||||
symbolTable[varName] = {allocaName, llvmType};
|
||||
tmpTable[allocaName] = llvmType;
|
||||
|
||||
// 局部常量(SysY IR)
|
||||
// 局部常量(SysY IR)TODO:这里可能有bug,AI在犯蠢
|
||||
sysy::IRBuilder builder(currentIRBlock);
|
||||
auto allocaInst = builder.createAllocaInst(irType, {}, varName);
|
||||
builder.createStoreInst(initValue, allocaInst);
|
||||
@ -230,9 +230,9 @@ std::any LLVMIRGenerator::visitVarDef(SysYParser::VarDefContext* ctx) {
|
||||
|
||||
symbolTable[varName] = {allocaName, llvmType};
|
||||
tmpTable[allocaName] = llvmType;
|
||||
irSymbolTable[varName] = allocaInst;
|
||||
irTmpTable[allocaName] = allocaInst;
|
||||
builder.createStoreInst(initValue, allocaInst);
|
||||
irSymbolTable[varName] = allocaInst;//TODO:这里没看懂在干嘛
|
||||
irTmpTable[allocaName] = allocaInst;//TODO:这里没看懂在干嘛
|
||||
builder.createStoreInst(initValue, allocaInst);//TODO:这里没看懂在干嘛
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -342,6 +342,7 @@ std::any LLVMIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
|
||||
rhs = ss.str();
|
||||
} catch (...) {
|
||||
// 如果 rhs 不是字面量,假设已正确处理
|
||||
throw std::runtime_error("Invalid float literal: " + rhs);
|
||||
}
|
||||
}
|
||||
irStream << " store " << lhsType << " " << rhs << ", " << lhsType
|
||||
@ -485,9 +486,7 @@ std::any LLVMIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
||||
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 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();
|
||||
@ -509,6 +508,9 @@ std::any LLVMIRGenerator::visitPrimExp(SysYParser::PrimExpContext* ctx) {
|
||||
} 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.");
|
||||
}
|
||||
}
|
||||
@ -555,6 +557,36 @@ std::any LLVMIRGenerator::visitString(SysYParser::StringContext* ctx) {
|
||||
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;
|
||||
@ -588,34 +620,6 @@ std::any LLVMIRGenerator::visitCall(SysYParser::CallContext* ctx) {
|
||||
return temp;
|
||||
}
|
||||
|
||||
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::visitMulExp(SysYParser::MulExpContext* ctx) {
|
||||
auto unaryExps = ctx->unaryExp();
|
||||
std::string left = std::any_cast<std::string>(unaryExps[0]->accept(this));
|
||||
|
||||
Reference in New Issue
Block a user