pass test1,but test2 segmentation fault

This commit is contained in:
Downright
2025-03-26 11:39:22 +08:00
parent 7f364abffb
commit 9bea0879e0
3 changed files with 116 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.19)
cmake_minimum_required(VERSION 3.19)
# cmake_policy(SET CMP0135 OLD)
@@ -15,11 +15,18 @@ set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS OFF)
# ========== 修改点 1强制 Debug 模式(调试时使用) ==========
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set, falling back to Debug mode.")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: Debug Release." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set, defaulting to Debug mode for GDB support.")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo." FORCE)
endif()
# ========== 修改点 2确保 Debug 模式生成调试信息 ==========
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Debug mode enabled: Adding -g flag for GDB debugging.")
add_compile_options(-g -O0) # -O0 禁用优化,避免干扰调试
endif()
# Set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

View File

@@ -112,9 +112,9 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
*/
std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
if(ctx->INT())
return Type::getIntType();
else if(ctx->FLOAT())
return Type::getFloatType();
return Type::getPointerType(Type::getIntType());
else if(ctx->FLOAT())
return Type::getPointerType(Type::getFloatType());
std::cerr << "error: unknown type" << ctx->getText() << std::endl;
return std::any();
}
@@ -127,21 +127,33 @@ std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
*/
std::any SysYIRGenerator::visitConstDef(SysYParser::ConstDefContext *ctx){
auto name = ctx->Ident()->getText();
auto type = current_type;
auto init = ctx->constInitVal() ? any_cast<Value*>(ctx->constInitVal()->accept(this)) : (Value *)nullptr;
Type* type = current_type;
Value* init = ctx->constInitVal() ? any_cast<Value*>(ctx->constInitVal()->accept(this)) : (Value *)nullptr;
if (ctx->constExp().empty()){
//scalar
if(init){
if(symbols_table.isModuleScope()){
assert(init->isConstant() && "global must be initialized by constant");
auto global = module->createGlobalValue(name, type, {}, init);
Value* global = module->createGlobalValue(name, type, {}, init);
symbols_table.insert(name, global);
cout << "add module const " << name ;
if(init){
cout << " inited by " ;
init->print(cout);
}
cout << '\n';
}
else{
auto alloca = builder.createAllocaInst(type, {}, name);
auto store = builder.createStoreInst(init, alloca);
Value* alloca = builder.createAllocaInst(type, {}, name);
Value* store = builder.createStoreInst(init, alloca);
symbols_table.insert(name, alloca);
cout << "add local const " << name ;
if(init){
cout << " inited by " ;
init->print(cout);
}
cout << '\n';
}
}
else{
@@ -238,7 +250,9 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx){
entryblock->createArgument(paramTypes[i], paramNames[i]);
for(auto& arg: entryblock->getArguments())
symbols_table.insert(arg->getName(), arg.get());
symbols_table.insert(arg->getName(), (Value *)arg.get());
cout << "setposition entryblock" << endl;
builder.setPosition(entryblock, entryblock->end());
ctx->blockStmt()->accept(this);
@@ -253,7 +267,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx){
*/
std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx){
cout << "visitVarDecl" << endl;
current_type = any_cast<Type*>(ctx->bType()->accept(this));
current_type = any_cast<Type *>(ctx->bType()->accept(this));
for(auto varDef:ctx->varDef()){
varDef->accept(this);
}
@@ -266,9 +280,10 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx){
* varDef: Ident (LBRACK constExp RBRACK)* (ASSIGN initVal)?;
*/
std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
auto name = ctx->Ident()->getText();
auto type = current_type;
auto init = ctx->initVal() ? any_cast<Value*>(ctx->initVal()->accept(this)) : (Value *)nullptr;
const std::string name = ctx->Ident()->getText();
Type* type = current_type;
Value* init = ctx->initVal() ? any_cast<Value*>(ctx->initVal()->accept(this)) : nullptr;
// const std::vector<Value *> dims = {};
cout << "vardef: ";
current_type->print(cout);
@@ -280,23 +295,31 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
if(init)
assert(init->isConstant() && "global must be initialized by constant");
auto global = module->createGlobalValue(name, type, {}, init);
Value* global = module->createGlobalValue(name, type, {}, init);
symbols_table.insert(name, global);
cout << "add module var " << name << "inited by " ;
init->print(cout);
cout << "add module var " << name ;
// if(init){
// cout << " inited by " ;
// init->print(cout);
// }
cout << '\n';
}
else{
auto alloca = builder.createAllocaInst(type, {}, name);
auto store = (StoreInst *)nullptr;
if(init)
store = builder.createStoreInst(init, alloca);
Value* alloca = builder.createAllocaInst(type, {}, name);
cout << "creatalloca" << endl;
alloca->print(cout);
Value* store = (StoreInst *)nullptr;
if(init != nullptr)
store = builder.createStoreInst(alloca, init, {}, name);
symbols_table.insert(name, alloca);
cout << "add local var " << name ;
if(init){
cout << "inited by " ;
init->print(cout);
}
// alloca->setName(name);
cout << "add local var " ;
alloca->print(cout);
// if(init){
// cout << " inited by " ;
// init->print(cout);
// }
cout << '\n';
}
}
@@ -313,7 +336,7 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
* initVal: exp | LBRACE (initVal (COMMA initVal)*)? RBRACE;
*/
std::any SysYIRGenerator::visitInitVal(SysYParser::InitValContext *ctx) {
Value* initvalue;
Value* initvalue = nullptr;
if(ctx->exp())
initvalue = any_cast<Value*>(ctx->exp()->accept(this));
else{
@@ -424,8 +447,9 @@ std::any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext* ctx) {
* returnStmt: RETURN exp? SEMICOLON;
*/
std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
cout << "visitReturnStmt" << endl;
// auto value = ctx->exp() ? any_cast_Value(visit(ctx->exp())) : nullptr;
auto value = ctx->exp() ? any_cast<Value*>(ctx->exp()->accept(this)) : nullptr;
Value* value = ctx->exp() ? any_cast<Value*>(ctx->exp()->accept(this)) : nullptr;
const auto func = builder.getBasicBlock()->getParent();
@@ -467,14 +491,11 @@ std::any SysYIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext* ctx
std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
cout << "visitassignstme :\n";
auto lvalue = any_cast<Value *>(ctx->lValue()->accept(this));
cout << 'lval: (' << lvalue->getName() ;
cout << "getlval" << endl;//lvalue->print(cout);cout << ')';
auto rvalue = any_cast<Value *>(ctx->exp()->accept(this));
//可能要考虑类型转换例如int a = 1.0
builder.createStoreInst(rvalue, lvalue);
cout << ') = rval(' << rvalue->getName();
cout << "getrval" << endl;//rvalue->print(cout);cout << ")\n";
builder.createStoreInst(rvalue, lvalue, {}, {});
return std::any();
}
@@ -484,6 +505,7 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
* lValue: Ident (LBRACK exp RBRACK)*;
*/
std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
cout << "visitLValue" << endl;
auto name = ctx->Ident()->getText();
Value* value = symbols_table.lookup(name);
@@ -502,10 +524,18 @@ std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
return std::any();
}
std::any SysYIRGenerator::visitPrimExp(SysYParser::PrimExpContext *ctx){
cout << "visitPrimExp" << endl;
return visitChildren(ctx);
}
// std::any SysYIRGenerator::visitExp(SysYParser::ExpContext* ctx) {
// cout << "visitExp" << endl;
// return ctx->addExp()->accept(this);
// }
std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
ConstantValue* res = nullptr;
cout << "visitNumber" << endl;
Value* res = nullptr;
if (auto iLiteral = ctx->ILITERAL()) {
/* 基数 (8, 10, 16) */
const auto text = iLiteral->getText();
@@ -522,6 +552,10 @@ std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
const auto text = fLiteral->getText();
res = ConstantValue::get((float)std::stof(text));
}
cout << "number: ";
res->print(cout);
cout << endl;
return res;
}
@@ -531,6 +565,7 @@ std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
* call: Ident LPAREN funcRParams? RPAREN;
*/
std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
cout << "visitCall" << endl;
auto funcName = ctx->Ident()->getText();
auto func = module->getFunction(funcName);
assert(func && "function not found");
@@ -542,7 +577,7 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
args.push_back(any_cast<Value*>(exp->accept(this)));
}
}
auto call = builder.createCallInst(func, args);
Value* call = builder.createCallInst(func, args);
return call;
}
@@ -552,6 +587,7 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
* unExp: unaryOp unaryExp
*/
std::any SysYIRGenerator::visitUnExp(SysYParser::UnExpContext *ctx) {
cout << "visitUnExp" << endl;
Value* res = nullptr;
auto op = ctx->unaryOp()->getText();
auto exp = any_cast<Value*>(ctx->unaryExp()->accept(this));
@@ -574,15 +610,19 @@ std::any SysYIRGenerator::visitUnExp(SysYParser::UnExpContext *ctx) {
* mulExp: unaryExp ((MUL | DIV | MOD) unaryExp)*
*/
std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
cout << "visitMulExp" << endl;
Value* res = nullptr;
auto lhs = any_cast<Value*>(ctx->unaryExp(0)->accept(this));
cout << "mulExplhsin\n";
Value* lhs = any_cast<Value *>(ctx->unaryExp(0)->accept(this));
cout << "mulExplhsout\n";
if(ctx->unaryExp().size() == 1){
cout << "unaryExp().size() = 1\n";
res = lhs;
}
else{
cout << "unaryExp().size() > 1\n";
for(size_t i = 1; i < ctx->unaryExp().size(); i++){
auto unaryexp = ctx->unaryExp(i);
auto rhs = any_cast<Value*>(unaryexp->accept(this));
Value* rhs = any_cast<Value *>(ctx->unaryExp(i)->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
if(opNode->getText() == "*"){
@@ -606,16 +646,18 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
* addExp: mulExp ((ADD | SUB) mulExp)*
*/
std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
cout << "visitAddExp" << endl;
Value* res = nullptr;
auto lhs = any_cast<Value*>(ctx->mulExp(0)->accept(this));
Value* lhs = any_cast<Value*>(ctx->mulExp(0)->accept(this));
if(ctx->mulExp().size() == 1){
cout << "ctx->mulExp().size() = 1\n";
res = lhs;
}
else{
for(size_t i = 1; i < ctx->mulExp().size(); i++){
auto mulexp = ctx->mulExp(i);
auto rhs = any_cast<Value*>(mulexp->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
cout << "i = " << i << "\n";
Value* rhs = any_cast<Value*>(ctx->mulExp(i)->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
if(opNode->getText() == "+"){
res = builder.createAddInst(lhs, rhs, lhs->getName() + "+" + rhs->getName());
@@ -624,7 +666,9 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
res = builder.createSubInst(lhs, rhs, lhs->getName() + "-" + rhs->getName());
}
}
lhs = res;
}
return res;
}
@@ -634,16 +678,16 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
* relExp: addExp ((LT | GT | LE | GE) addExp)*
*/
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
cout << "visitRelExp" << endl;
Value* res = nullptr;
auto lhs = any_cast<Value*>(ctx->addExp(0)->accept(this));
Value* lhs = any_cast<Value*>(ctx->addExp(0)->accept(this));
if(ctx->addExp().size() == 1){
res = lhs;
}
else{
for(size_t i = 1; i < ctx->addExp().size(); i++){
auto addexp = ctx->addExp(i);
auto rhs = any_cast<Value*>(addexp->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
Value* rhs = any_cast<Value*>(ctx->addExp(i)->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
if(lhs->getType() != rhs->getType()){
std::cerr << "type mismatch:type check not implemented" << std::endl;
}
@@ -683,16 +727,16 @@ std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
* eqExp: relExp ((EQ | NEQ) relExp)*
*/
std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
cout << "visitEqExp" << endl;
Value* res = nullptr;
auto lhs = any_cast<Value*>(ctx->relExp(0)->accept(this));
Value* lhs = any_cast<Value*>(ctx->relExp(0)->accept(this));
if(ctx->relExp().size() == 1){
res = lhs;
}
else{
for(size_t i = 1; i < ctx->relExp().size(); i++){
auto relexp = ctx->relExp(i);
auto rhs = any_cast<Value*>(relexp->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
Value* rhs = any_cast<Value*>(ctx->relExp(i)->accept(this));
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
if(lhs->getType() != rhs->getType()){
std::cerr << "type mismatch:type check not implemented" << std::endl;
}
@@ -720,10 +764,11 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
* lAndExp: eqExp (AND eqExp)*
*/
std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
cout << "visitLAndExp" << endl;
auto currentBlock = builder.getBasicBlock();
// auto trueBlock = currentBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
auto falseBlock = currentBlock->getParent()->addBasicBlock("falseland" + std::to_string(++falseBlockNum));
auto value = any_cast<Value*>(ctx->eqExp(0)->accept(this));
Value* value = any_cast<Value*>(ctx->eqExp(0)->accept(this));
auto trueBlock = currentBlock;
for(size_t i = 1; i < ctx->eqExp().size(); i++){
trueBlock = trueBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
@@ -746,9 +791,10 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
* lOrExp: lAndExp (OR lAndExp)*
*/
std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
cout << "visitLOrExp" << endl;
auto currentBlock = builder.getBasicBlock();
auto trueBlock = currentBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
auto value = any_cast<Value*>(ctx->lAndExp(0)->accept(this));
Value* value = any_cast<Value*>(ctx->lAndExp(0)->accept(this));
auto falseBlock = currentBlock;
for(size_t i = 1; i < ctx->lAndExp().size(); i++){
falseBlock = currentBlock->getParent()->addBasicBlock("falseland" + std::to_string(++falseBlockNum));
@@ -771,10 +817,11 @@ std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
* constExp: addExp;
*/
std::any SysYIRGenerator::visitConstExp(SysYParser::ConstExpContext* ctx) {
cout << "visitConstExp" << endl;
ConstantValue* res = nullptr;
auto value = any_cast<Value*>(ctx->addExp()->accept(this));
Value* value = any_cast<Value*>(ctx->addExp()->accept(this));
if(isa<ConstantValue>(value)){
res = dynamic_cast<ConstantValue*>(value);
res = dyncast<ConstantValue>(value);
}
else{
std::cerr << "error constexp" << ctx->getText() << std::endl;

View File

@@ -85,7 +85,7 @@ private:
bool isalloca;
AllocaInst *current_alloca;
GlobalValue *current_global;
Type *current_type;
Type* current_type;
int numdims = 0;
public:
@@ -118,7 +118,7 @@ public:
std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override;
// std::any visitExp(SysYParser::ExpContext *ctx) override;
std::any visitLValue(SysYParser::LValueContext *ctx) override;
// std::any visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) override;
std::any visitPrimExp(SysYParser::PrimExpContext *ctx) override;
// std::any visitParenExp(SysYParser::ParenExpContext *ctx) override;
std::any visitNumber(SysYParser::NumberContext *ctx) override;
// std::any visitString(SysYParser::StringContext *ctx) override;