pass test1,but test2 segmentation fault
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required (VERSION 3.19)
|
cmake_minimum_required(VERSION 3.19)
|
||||||
|
|
||||||
# cmake_policy(SET CMP0135 OLD)
|
# 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_STANDARD_REQUIRED YES)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# ========== 修改点 1:强制 Debug 模式(调试时使用) ==========
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
message(STATUS "Build type not set, falling back to Debug mode.")
|
message(STATUS "Build type not set, defaulting to Debug mode for GDB support.")
|
||||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
|
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
|
||||||
"Choose the type of build, options are: Debug Release." FORCE)
|
"Choose the type of build, options are: Debug Release RelWithDebInfo." FORCE)
|
||||||
endif(NOT CMAKE_BUILD_TYPE)
|
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 output directories
|
||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
|
||||||
|
|||||||
@@ -112,9 +112,9 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
|
|||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
|
std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
|
||||||
if(ctx->INT())
|
if(ctx->INT())
|
||||||
return Type::getIntType();
|
return Type::getPointerType(Type::getIntType());
|
||||||
else if(ctx->FLOAT())
|
else if(ctx->FLOAT())
|
||||||
return Type::getFloatType();
|
return Type::getPointerType(Type::getFloatType());
|
||||||
std::cerr << "error: unknown type" << ctx->getText() << std::endl;
|
std::cerr << "error: unknown type" << ctx->getText() << std::endl;
|
||||||
return std::any();
|
return std::any();
|
||||||
}
|
}
|
||||||
@@ -127,21 +127,33 @@ std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) {
|
|||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitConstDef(SysYParser::ConstDefContext *ctx){
|
std::any SysYIRGenerator::visitConstDef(SysYParser::ConstDefContext *ctx){
|
||||||
auto name = ctx->Ident()->getText();
|
auto name = ctx->Ident()->getText();
|
||||||
auto type = current_type;
|
Type* type = current_type;
|
||||||
auto init = ctx->constInitVal() ? any_cast<Value*>(ctx->constInitVal()->accept(this)) : (Value *)nullptr;
|
Value* init = ctx->constInitVal() ? any_cast<Value*>(ctx->constInitVal()->accept(this)) : (Value *)nullptr;
|
||||||
|
|
||||||
if (ctx->constExp().empty()){
|
if (ctx->constExp().empty()){
|
||||||
//scalar
|
//scalar
|
||||||
if(init){
|
if(init){
|
||||||
if(symbols_table.isModuleScope()){
|
if(symbols_table.isModuleScope()){
|
||||||
assert(init->isConstant() && "global must be initialized by constant");
|
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);
|
symbols_table.insert(name, global);
|
||||||
|
cout << "add module const " << name ;
|
||||||
|
if(init){
|
||||||
|
cout << " inited by " ;
|
||||||
|
init->print(cout);
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
auto alloca = builder.createAllocaInst(type, {}, name);
|
Value* alloca = builder.createAllocaInst(type, {}, name);
|
||||||
auto store = builder.createStoreInst(init, alloca);
|
Value* store = builder.createStoreInst(init, alloca);
|
||||||
symbols_table.insert(name, alloca);
|
symbols_table.insert(name, alloca);
|
||||||
|
cout << "add local const " << name ;
|
||||||
|
if(init){
|
||||||
|
cout << " inited by " ;
|
||||||
|
init->print(cout);
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -238,7 +250,9 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx){
|
|||||||
entryblock->createArgument(paramTypes[i], paramNames[i]);
|
entryblock->createArgument(paramTypes[i], paramNames[i]);
|
||||||
|
|
||||||
for(auto& arg: entryblock->getArguments())
|
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());
|
builder.setPosition(entryblock, entryblock->end());
|
||||||
|
|
||||||
ctx->blockStmt()->accept(this);
|
ctx->blockStmt()->accept(this);
|
||||||
@@ -253,7 +267,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext* ctx){
|
|||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx){
|
std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx){
|
||||||
cout << "visitVarDecl" << endl;
|
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()){
|
for(auto varDef:ctx->varDef()){
|
||||||
varDef->accept(this);
|
varDef->accept(this);
|
||||||
}
|
}
|
||||||
@@ -266,9 +280,10 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx){
|
|||||||
* varDef: Ident (LBRACK constExp RBRACK)* (ASSIGN initVal)?;
|
* varDef: Ident (LBRACK constExp RBRACK)* (ASSIGN initVal)?;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
|
std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
|
||||||
auto name = ctx->Ident()->getText();
|
const std::string name = ctx->Ident()->getText();
|
||||||
auto type = current_type;
|
Type* type = current_type;
|
||||||
auto init = ctx->initVal() ? any_cast<Value*>(ctx->initVal()->accept(this)) : (Value *)nullptr;
|
Value* init = ctx->initVal() ? any_cast<Value*>(ctx->initVal()->accept(this)) : nullptr;
|
||||||
|
// const std::vector<Value *> dims = {};
|
||||||
|
|
||||||
cout << "vardef: ";
|
cout << "vardef: ";
|
||||||
current_type->print(cout);
|
current_type->print(cout);
|
||||||
@@ -280,23 +295,31 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
|
|||||||
|
|
||||||
if(init)
|
if(init)
|
||||||
assert(init->isConstant() && "global must be initialized by constant");
|
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);
|
symbols_table.insert(name, global);
|
||||||
cout << "add module var " << name << "inited by " ;
|
cout << "add module var " << name ;
|
||||||
init->print(cout);
|
// if(init){
|
||||||
|
// cout << " inited by " ;
|
||||||
|
// init->print(cout);
|
||||||
|
// }
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
auto alloca = builder.createAllocaInst(type, {}, name);
|
|
||||||
auto store = (StoreInst *)nullptr;
|
Value* alloca = builder.createAllocaInst(type, {}, name);
|
||||||
if(init)
|
cout << "creatalloca" << endl;
|
||||||
store = builder.createStoreInst(init, alloca);
|
alloca->print(cout);
|
||||||
|
Value* store = (StoreInst *)nullptr;
|
||||||
|
if(init != nullptr)
|
||||||
|
store = builder.createStoreInst(alloca, init, {}, name);
|
||||||
symbols_table.insert(name, alloca);
|
symbols_table.insert(name, alloca);
|
||||||
cout << "add local var " << name ;
|
// alloca->setName(name);
|
||||||
if(init){
|
cout << "add local var " ;
|
||||||
cout << "inited by " ;
|
alloca->print(cout);
|
||||||
init->print(cout);
|
// if(init){
|
||||||
}
|
// cout << " inited by " ;
|
||||||
|
// init->print(cout);
|
||||||
|
// }
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,7 +336,7 @@ std::any SysYIRGenerator::visitVarDef(SysYParser::VarDefContext *ctx){
|
|||||||
* initVal: exp | LBRACE (initVal (COMMA initVal)*)? RBRACE;
|
* initVal: exp | LBRACE (initVal (COMMA initVal)*)? RBRACE;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitInitVal(SysYParser::InitValContext *ctx) {
|
std::any SysYIRGenerator::visitInitVal(SysYParser::InitValContext *ctx) {
|
||||||
Value* initvalue;
|
Value* initvalue = nullptr;
|
||||||
if(ctx->exp())
|
if(ctx->exp())
|
||||||
initvalue = any_cast<Value*>(ctx->exp()->accept(this));
|
initvalue = any_cast<Value*>(ctx->exp()->accept(this));
|
||||||
else{
|
else{
|
||||||
@@ -424,8 +447,9 @@ std::any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext* ctx) {
|
|||||||
* returnStmt: RETURN exp? SEMICOLON;
|
* returnStmt: RETURN exp? SEMICOLON;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) {
|
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(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();
|
const auto func = builder.getBasicBlock()->getParent();
|
||||||
|
|
||||||
@@ -467,14 +491,11 @@ std::any SysYIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext* ctx
|
|||||||
std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
|
std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
|
||||||
cout << "visitassignstme :\n";
|
cout << "visitassignstme :\n";
|
||||||
auto lvalue = any_cast<Value *>(ctx->lValue()->accept(this));
|
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));
|
auto rvalue = any_cast<Value *>(ctx->exp()->accept(this));
|
||||||
//可能要考虑类型转换例如int a = 1.0
|
//可能要考虑类型转换例如int a = 1.0
|
||||||
builder.createStoreInst(rvalue, lvalue);
|
cout << "getrval" << endl;//rvalue->print(cout);cout << ")\n";
|
||||||
|
builder.createStoreInst(rvalue, lvalue, {}, {});
|
||||||
cout << ') = rval(' << rvalue->getName();
|
|
||||||
|
|
||||||
|
|
||||||
return std::any();
|
return std::any();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,6 +505,7 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext* ctx) {
|
|||||||
* lValue: Ident (LBRACK exp RBRACK)*;
|
* lValue: Ident (LBRACK exp RBRACK)*;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
||||||
|
cout << "visitLValue" << endl;
|
||||||
auto name = ctx->Ident()->getText();
|
auto name = ctx->Ident()->getText();
|
||||||
Value* value = symbols_table.lookup(name);
|
Value* value = symbols_table.lookup(name);
|
||||||
|
|
||||||
@@ -502,10 +524,18 @@ std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext* ctx) {
|
|||||||
return std::any();
|
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) {
|
std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
|
||||||
ConstantValue* res = nullptr;
|
cout << "visitNumber" << endl;
|
||||||
|
Value* res = nullptr;
|
||||||
if (auto iLiteral = ctx->ILITERAL()) {
|
if (auto iLiteral = ctx->ILITERAL()) {
|
||||||
/* 基数 (8, 10, 16) */
|
/* 基数 (8, 10, 16) */
|
||||||
const auto text = iLiteral->getText();
|
const auto text = iLiteral->getText();
|
||||||
@@ -522,6 +552,10 @@ std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
|
|||||||
const auto text = fLiteral->getText();
|
const auto text = fLiteral->getText();
|
||||||
res = ConstantValue::get((float)std::stof(text));
|
res = ConstantValue::get((float)std::stof(text));
|
||||||
}
|
}
|
||||||
|
cout << "number: ";
|
||||||
|
res->print(cout);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,6 +565,7 @@ std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) {
|
|||||||
* call: Ident LPAREN funcRParams? RPAREN;
|
* call: Ident LPAREN funcRParams? RPAREN;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
|
std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
|
||||||
|
cout << "visitCall" << endl;
|
||||||
auto funcName = ctx->Ident()->getText();
|
auto funcName = ctx->Ident()->getText();
|
||||||
auto func = module->getFunction(funcName);
|
auto func = module->getFunction(funcName);
|
||||||
assert(func && "function not found");
|
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)));
|
args.push_back(any_cast<Value*>(exp->accept(this)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto call = builder.createCallInst(func, args);
|
Value* call = builder.createCallInst(func, args);
|
||||||
return call;
|
return call;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -552,6 +587,7 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext* ctx) {
|
|||||||
* unExp: unaryOp unaryExp
|
* unExp: unaryOp unaryExp
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitUnExp(SysYParser::UnExpContext *ctx) {
|
std::any SysYIRGenerator::visitUnExp(SysYParser::UnExpContext *ctx) {
|
||||||
|
cout << "visitUnExp" << endl;
|
||||||
Value* res = nullptr;
|
Value* res = nullptr;
|
||||||
auto op = ctx->unaryOp()->getText();
|
auto op = ctx->unaryOp()->getText();
|
||||||
auto exp = any_cast<Value*>(ctx->unaryExp()->accept(this));
|
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)*
|
* mulExp: unaryExp ((MUL | DIV | MOD) unaryExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||||
|
cout << "visitMulExp" << endl;
|
||||||
Value* res = nullptr;
|
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){
|
if(ctx->unaryExp().size() == 1){
|
||||||
|
cout << "unaryExp().size() = 1\n";
|
||||||
res = lhs;
|
res = lhs;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
cout << "unaryExp().size() > 1\n";
|
||||||
for(size_t i = 1; i < ctx->unaryExp().size(); i++){
|
for(size_t i = 1; i < ctx->unaryExp().size(); i++){
|
||||||
auto unaryexp = ctx->unaryExp(i);
|
Value* rhs = any_cast<Value *>(ctx->unaryExp(i)->accept(this));
|
||||||
auto rhs = any_cast<Value*>(unaryexp->accept(this));
|
|
||||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
||||||
|
|
||||||
if(opNode->getText() == "*"){
|
if(opNode->getText() == "*"){
|
||||||
@@ -606,16 +646,18 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
|||||||
* addExp: mulExp ((ADD | SUB) mulExp)*
|
* addExp: mulExp ((ADD | SUB) mulExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||||
|
cout << "visitAddExp" << endl;
|
||||||
Value* res = nullptr;
|
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){
|
if(ctx->mulExp().size() == 1){
|
||||||
|
cout << "ctx->mulExp().size() = 1\n";
|
||||||
res = lhs;
|
res = lhs;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
for(size_t i = 1; i < ctx->mulExp().size(); i++){
|
for(size_t i = 1; i < ctx->mulExp().size(); i++){
|
||||||
auto mulexp = ctx->mulExp(i);
|
cout << "i = " << i << "\n";
|
||||||
auto rhs = any_cast<Value*>(mulexp->accept(this));
|
Value* rhs = any_cast<Value*>(ctx->mulExp(i)->accept(this));
|
||||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
|
||||||
|
|
||||||
if(opNode->getText() == "+"){
|
if(opNode->getText() == "+"){
|
||||||
res = builder.createAddInst(lhs, rhs, lhs->getName() + "+" + rhs->getName());
|
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());
|
res = builder.createSubInst(lhs, rhs, lhs->getName() + "-" + rhs->getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
lhs = res;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -634,16 +678,16 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
|||||||
* relExp: addExp ((LT | GT | LE | GE) addExp)*
|
* relExp: addExp ((LT | GT | LE | GE) addExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||||
|
cout << "visitRelExp" << endl;
|
||||||
Value* res = nullptr;
|
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){
|
if(ctx->addExp().size() == 1){
|
||||||
res = lhs;
|
res = lhs;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
for(size_t i = 1; i < ctx->addExp().size(); i++){
|
for(size_t i = 1; i < ctx->addExp().size(); i++){
|
||||||
auto addexp = ctx->addExp(i);
|
Value* rhs = any_cast<Value*>(ctx->addExp(i)->accept(this));
|
||||||
auto rhs = any_cast<Value*>(addexp->accept(this));
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
|
||||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
||||||
if(lhs->getType() != rhs->getType()){
|
if(lhs->getType() != rhs->getType()){
|
||||||
std::cerr << "type mismatch:type check not implemented" << std::endl;
|
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)*
|
* eqExp: relExp ((EQ | NEQ) relExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
|
std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext* ctx) {
|
||||||
|
cout << "visitEqExp" << endl;
|
||||||
Value* res = nullptr;
|
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){
|
if(ctx->relExp().size() == 1){
|
||||||
res = lhs;
|
res = lhs;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
for(size_t i = 1; i < ctx->relExp().size(); i++){
|
for(size_t i = 1; i < ctx->relExp().size(); i++){
|
||||||
auto relexp = ctx->relExp(i);
|
Value* rhs = any_cast<Value*>(ctx->relExp(i)->accept(this));
|
||||||
auto rhs = any_cast<Value*>(relexp->accept(this));
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i - 1]);
|
||||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode *>(ctx->children[2 * i + 1]);
|
|
||||||
if(lhs->getType() != rhs->getType()){
|
if(lhs->getType() != rhs->getType()){
|
||||||
std::cerr << "type mismatch:type check not implemented" << std::endl;
|
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)*
|
* lAndExp: eqExp (AND eqExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
|
std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
|
||||||
|
cout << "visitLAndExp" << endl;
|
||||||
auto currentBlock = builder.getBasicBlock();
|
auto currentBlock = builder.getBasicBlock();
|
||||||
// auto trueBlock = currentBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
|
// auto trueBlock = currentBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
|
||||||
auto falseBlock = currentBlock->getParent()->addBasicBlock("falseland" + std::to_string(++falseBlockNum));
|
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;
|
auto trueBlock = currentBlock;
|
||||||
for(size_t i = 1; i < ctx->eqExp().size(); i++){
|
for(size_t i = 1; i < ctx->eqExp().size(); i++){
|
||||||
trueBlock = trueBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
|
trueBlock = trueBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
|
||||||
@@ -746,9 +791,10 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext* ctx) {
|
|||||||
* lOrExp: lAndExp (OR lAndExp)*
|
* lOrExp: lAndExp (OR lAndExp)*
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
|
std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
|
||||||
|
cout << "visitLOrExp" << endl;
|
||||||
auto currentBlock = builder.getBasicBlock();
|
auto currentBlock = builder.getBasicBlock();
|
||||||
auto trueBlock = currentBlock->getParent()->addBasicBlock("trueland" + std::to_string(++trueBlockNum));
|
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;
|
auto falseBlock = currentBlock;
|
||||||
for(size_t i = 1; i < ctx->lAndExp().size(); i++){
|
for(size_t i = 1; i < ctx->lAndExp().size(); i++){
|
||||||
falseBlock = currentBlock->getParent()->addBasicBlock("falseland" + std::to_string(++falseBlockNum));
|
falseBlock = currentBlock->getParent()->addBasicBlock("falseland" + std::to_string(++falseBlockNum));
|
||||||
@@ -771,10 +817,11 @@ std::any SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext* ctx) {
|
|||||||
* constExp: addExp;
|
* constExp: addExp;
|
||||||
*/
|
*/
|
||||||
std::any SysYIRGenerator::visitConstExp(SysYParser::ConstExpContext* ctx) {
|
std::any SysYIRGenerator::visitConstExp(SysYParser::ConstExpContext* ctx) {
|
||||||
|
cout << "visitConstExp" << endl;
|
||||||
ConstantValue* res = nullptr;
|
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)){
|
if(isa<ConstantValue>(value)){
|
||||||
res = dynamic_cast<ConstantValue*>(value);
|
res = dyncast<ConstantValue>(value);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
std::cerr << "error constexp" << ctx->getText() << std::endl;
|
std::cerr << "error constexp" << ctx->getText() << std::endl;
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ private:
|
|||||||
bool isalloca;
|
bool isalloca;
|
||||||
AllocaInst *current_alloca;
|
AllocaInst *current_alloca;
|
||||||
GlobalValue *current_global;
|
GlobalValue *current_global;
|
||||||
Type *current_type;
|
Type* current_type;
|
||||||
int numdims = 0;
|
int numdims = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -118,7 +118,7 @@ public:
|
|||||||
std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override;
|
std::any visitReturnStmt(SysYParser::ReturnStmtContext *ctx) override;
|
||||||
// std::any visitExp(SysYParser::ExpContext *ctx) override;
|
// std::any visitExp(SysYParser::ExpContext *ctx) override;
|
||||||
std::any visitLValue(SysYParser::LValueContext *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 visitParenExp(SysYParser::ParenExpContext *ctx) override;
|
||||||
std::any visitNumber(SysYParser::NumberContext *ctx) override;
|
std::any visitNumber(SysYParser::NumberContext *ctx) override;
|
||||||
// std::any visitString(SysYParser::StringContext *ctx) override;
|
// std::any visitString(SysYParser::StringContext *ctx) override;
|
||||||
|
|||||||
Reference in New Issue
Block a user