From 4828c18f96e36c59090ec1c411dd62addaf635ac Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Sun, 22 Jun 2025 00:25:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E5=9F=BA=E6=9C=AC=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E5=AE=8C=E6=AF=95=EF=BC=8Cbuild=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=97=A0=E6=8A=A5=E9=94=99=EF=BC=8Cargument?= =?UTF-8?q?=E7=B1=BB=E5=88=A0=E9=99=A4=E5=90=8E=E7=AB=AF=E6=8A=A5=E9=94=99?= =?UTF-8?q?=EF=BC=8CllvmIR=E8=BE=93=E5=87=BA=E5=BE=85=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 1 - src/SysY.g4 | 7 +- src/SysYIRGenerator.cpp | 306 ++++++++++++++++++++++++---------- src/include/SysYIRGenerator.h | 13 +- src/sysyc.cpp | 3 +- 5 files changed, 231 insertions(+), 99 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e8572c..e8c78f6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,6 @@ target_link_libraries(SysYParser PUBLIC antlr4_shared) add_executable(sysyc sysyc.cpp - ASTPrinter.cpp IR.cpp SysYIRGenerator.cpp Backend.cpp diff --git a/src/SysY.g4 b/src/SysY.g4 index d614ec4..ad74a0c 100644 --- a/src/SysY.g4 +++ b/src/SysY.g4 @@ -159,9 +159,10 @@ primaryExp: LPAREN exp RPAREN | string; number: ILITERAL | FLITERAL; -unaryExp: primaryExp #primExp - | Ident LPAREN (funcRParams)? RPAREN #call - | unaryOp unaryExp #unExp; +call: Ident LPAREN (funcRParams)? RPAREN; +unaryExp: primaryExp + | call + | unaryOp unaryExp; unaryOp: ADD|SUB|NOT; funcRParams: exp (COMMA exp)*; diff --git a/src/SysYIRGenerator.cpp b/src/SysYIRGenerator.cpp index 6abdeff..7e4ed85 100644 --- a/src/SysYIRGenerator.cpp +++ b/src/SysYIRGenerator.cpp @@ -137,8 +137,8 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) { // values.getValues()可能是[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] // 对于每个维度,使用memset将对应的值填充到数组中 // 这里的alloca是一个指向数组的指针 - std::vector & counterNumbers = values.getNumbers(); - std::vector & counterValues = values.getValues(); + const std::vector & counterNumbers = values.getNumbers(); + const std::vector & counterValues = values.getValues(); unsigned begin = 0; for (size_t i = 0; i < counterNumbers.size(); i++) { @@ -160,9 +160,9 @@ std::any SysYIRGenerator::visitBType(SysYParser::BTypeContext *ctx) { } std::any SysYIRGenerator::visitScalarInitValue(SysYParser::ScalarInitValueContext *ctx) { - AllocaInst* alloca = std::any_cast(visitExp(ctx->exp())); + Value* value = std::any_cast(visitExp(ctx->exp())); ArrayValueTree* result = new ArrayValueTree(); - result->setValue(alloca); + result->setValue(value); return result; } @@ -176,9 +176,9 @@ std::any SysYIRGenerator::visitArrayInitValue(SysYParser::ArrayInitValueContext } std::any SysYIRGenerator::visitConstScalarInitValue(SysYParser::ConstScalarInitValueContext *ctx) { - AllocaInst* alloca = std::any_cast(visitConstExp(ctx->constExp())); + Value* value = std::any_cast(visitConstExp(ctx->constExp())); ArrayValueTree* result = new ArrayValueTree(); - result->setValue(alloca); + result->setValue(value); return result; } @@ -214,7 +214,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){ paramTypes.push_back(std::any_cast(visitBType(param->bType()))); paramNames.push_back(param->Ident()->getText()); std::vector dims = {}; - if (param->exp() != nullptr) { + if (!param->LBRACK().empty()) { dims.push_back(ConstantValue::get(-1)); // 第一个维度不确定 for (const auto &exp : param->exp()) { dims.push_back(std::any_cast(visitExp(exp))); @@ -224,8 +224,8 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){ } } - Type *returnType = std::any_cast(visitFuncType(ctx->funcType())); - FunctionType* funcType = Type::getFunctionType(returnType, paramTypes); + Type* returnType = std::any_cast(visitFuncType(ctx->funcType())); + Type* funcType = Type::getFunctionType(returnType, paramTypes); Function* function = module->createFunction(name, funcType); BasicBlock* entry = function->getEntryBlock(); builder.setPosition(entry, entry->end()); @@ -243,7 +243,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){ module->leaveScope(); - return std::any; + return std::any(); } std::any SysYIRGenerator::visitBlockStmt(SysYParser::BlockStmtContext *ctx) { @@ -264,7 +264,7 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) { User* variable = module->getVariable(name); Value* value = std::any_cast(visitExp(ctx->exp())); - PointerType* variableType =dynamic_cast(variable->getType())->getBaseType(); + Type* variableType = dynamic_cast(variable->getType())->getBaseType(); // 左值右值类型不同处理 if (variableType != value->getType()) { @@ -473,12 +473,12 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) { } } } - builder.createRetInst(returnValue); + builder.createReturnInst(returnValue); return std::any(); } -std::any SysYIRGenerator::visitLVal(SysYParser::LValContext *ctx) { +std::any SysYIRGenerator::visitLValue(SysYParser::LValueContext *ctx) { std::string name = ctx->Ident()->getText(); User* variable = module->getVariable(name); @@ -493,8 +493,8 @@ std::any SysYIRGenerator::visitLVal(SysYParser::LValContext *ctx) { } bool indicesConstant = true; - for (const auto &index : indices) { - if (dynamic_cast(index) == nullptr) { + for (const auto &dim : dims) { + if (dynamic_cast(dim) == nullptr) { indicesConstant = false; break; } @@ -505,21 +505,21 @@ std::any SysYIRGenerator::visitLVal(SysYParser::LValContext *ctx) { AllocaInst* localVar = dynamic_cast(variable); if (constVar != nullptr && indicesConstant) { // 如果是常量变量,且索引是常量,则直接获取子数组 - value = constVar->getByIndices(indices); + value = constVar->getByIndices(dims); } else if (module->isInGlobalArea() && (globalVar != nullptr)) { assert(indicesConstant); - value = globalVar->getByIndices(indices); + value = globalVar->getByIndices(dims); } else { - if ((globalVar != nullptr && globalVar->getNumDims() > indices.size()) || - (localVar != nullptr && localVar->getNumDims() > indices.size()) || - (constVar != nullptr && constVar->getNumDims() > indices.size())) { + if ((globalVar != nullptr && globalVar->getNumDims() > dims.size()) || + (localVar != nullptr && localVar->getNumDims() > dims.size()) || + (constVar != nullptr && constVar->getNumDims() > dims.size())) { // value = builder.createLaInst(variable, indices); // 如果变量是全局变量或局部变量,且索引数量小于维度数量,则创建createGetSubArray获取子数组 auto getArrayInst = - builder.createGetSubArray(dynamic_cast(variable), indices); + builder.createGetSubArray(dynamic_cast(variable), dims); value = getArrayInst->getChildArray(); } else { - value = builder.createLoadInst(variable, indices); + value = builder.createLoadInst(variable, dims); } } @@ -529,25 +529,23 @@ std::any SysYIRGenerator::visitLVal(SysYParser::LValContext *ctx) { std::any SysYIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) { if (ctx->exp() != nullptr) return visitExp(ctx->exp()); - if (ctx->lVal() != nullptr) - return visitLVal(ctx->lVal()); + if (ctx->lValue() != nullptr) + return visitLValue(ctx->lValue()); if (ctx->number() != nullptr) return visitNumber(ctx->number()); - // if (ctx->string() != nullptr) { - // std::string str = ctx->string()->getText(); - // str = str.substr(1, str.size() - 2); // 去掉双引号 - // return ConstantValue::get(str); - // } + if (ctx->string() != nullptr) { + cout << "String literal not supported in SysYIRGenerator." << endl; + } return visitNumber(ctx->number()); } std::any SysYIRGenerator::visitNumber(SysYParser::NumberContext *ctx) { if (ctx->ILITERAL() != nullptr) { int value = std::stol(ctx->ILITERAL()->getText(), nullptr, 0); - return static_cast(ConstantValue::get(Type::getIntType(), value)); + return static_cast(ConstantValue::get(value)); } else if (ctx->FLITERAL() != nullptr) { float value = std::stof(ctx->FLITERAL()->getText()); - return static_cast(ConstantValue::get(Type::getFloatType(), value)); + return static_cast(ConstantValue::get(value)); } throw std::runtime_error("Unknown number type."); return std::any(); // 不会到达这里 @@ -557,15 +555,15 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) { std::string funcName = ctx->Ident()->getText(); Function *function = module->getFunction(funcName); if (function == nullptr) { - function = module->getExternalFunction(name); + function = module->getExternalFunction(funcName); if (function == nullptr) { - std::cout << "The function " << name << " no defined." << std::endl; + std::cout << "The function " << funcName << " no defined." << std::endl; assert(function); } } std::vector args = {}; - if (name == "starttime" || name == "stoptime") { + if (funcName == "starttime" || funcName == "stoptime") { // 如果是starttime或stoptime函数 // TODO: 这里需要处理starttime和stoptime函数的参数 // args.emplace_back() @@ -601,7 +599,12 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) { return static_cast(builder.createCallInst(function, args)); } -std::any SysYIRGenerator::visitUnExp(SysYParser::UnExpContext *ctx) { +std::any SysYIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext *ctx) { + if (ctx->primaryExp() != nullptr) + return visitPrimaryExp(ctx->primaryExp()); + if (ctx->call() != nullptr) + return visitCall(ctx->call()); + Value* value = std::any_cast(visitUnaryExp(ctx->unaryExp())); Value* result = value; if (ctx->unaryOp()->SUB() != nullptr) { @@ -654,24 +657,27 @@ std::any SysYIRGenerator::visitFuncRParams(SysYParser::FuncRParamsContext *ctx) std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) { - auto result = std::any_cast(visitUnaryExp(ctx->unaryExp(0))); - + Value * result = std::any_cast(visitUnaryExp(ctx->unaryExp(0))); + for (size_t i = 1; i < ctx->unaryExp().size(); i++) { - auto op = ctx->mulOp(i - 1); + auto opNode = dynamic_cast(ctx->children[2*i-1]); + int opType = opNode->getSymbol()->getType(); + Value* operand = std::any_cast(visitUnaryExp(ctx->unaryExp(i))); Type* resultType = result->getType(); Type* operandType = operand->getType(); + Type* floatType = Type::getFloatType(); - if (resultType == Type::getFloatType() || operandType == Type::getFloatType()) { + if (resultType == floatType || operandType == floatType) { // 如果有一个操作数是浮点数,则将两个操作数都转换为浮点数 - if (operandType != Type::getFloatType()) { + if (operandType != floatType) { ConstantValue * constValue = dynamic_cast(operand); if (constValue != nullptr) operand = ConstantValue::get(static_cast(constValue->getInt())); else operand = builder.createIToFInst(operand); - } else if (resultType != Type::getFloatType()) { + } else if (resultType != floatType) { ConstantValue* constResult = dynamic_cast(result); if (constResult != nullptr) result = ConstantValue::get(static_cast(constResult->getInt())); @@ -681,14 +687,14 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) { ConstantValue* constResult = dynamic_cast(result); ConstantValue* constOperand = dynamic_cast(operand); - if (op->MUL() != nullptr) { + if (opType == SysYParser::MUL) { if ((constOperand != nullptr) && (constResult != nullptr)) { result = ConstantValue::get(constResult->getFloat() * constOperand->getFloat()); } else { result = builder.createFMulInst(result, operand); } - } else if (op->DIV() != nullptr) { + } else if (opType == SysYParser::DIV) { if ((constOperand != nullptr) && (constResult != nullptr)) { result = ConstantValue::get(constResult->getFloat() / constOperand->getFloat()); @@ -703,12 +709,12 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) { } else { ConstantValue * constResult = dynamic_cast(result); ConstantValue * constOperand = dynamic_cast(operand); - if (op->MUL() != nullptr) { + if (opType == SysYParser::MUL) { if ((constOperand != nullptr) && (constResult != nullptr)) result = ConstantValue::get(constResult->getInt() * constOperand->getInt()); else result = builder.createMulInst(result, operand); - } else if (op->DIV() != nullptr) { + } else if (opType == SysYParser::DIV) { if ((constOperand != nullptr) && (constResult != nullptr)) result = ConstantValue::get(constResult->getInt() / constOperand->getInt()); else @@ -730,31 +736,33 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) { Value* result = std::any_cast(visitMulExp(ctx->mulExp(0))); for (size_t i = 1; i < ctx->mulExp().size(); i++) { - auto op = ctx->addOp(i - 1); + auto opNode = dynamic_cast(ctx->children[2*i-1]); + int opType = opNode->getSymbol()->getType(); Value* operand = std::any_cast(visitMulExp(ctx->mulExp(i))); Type* resultType = result->getType(); Type* operandType = operand->getType(); + Type* floatType = Type::getFloatType(); - if (resultType == Type::getFloatType() || operandType == Type::getFloatType()) { + if (resultType == floatType || operandType == floatType) { // 类型转换 - if (operandType != Type::getFloatType()) { - Value* constOperand = dynamic_cast(operand); + if (operandType != floatType) { + ConstantValue * constOperand = dynamic_cast(operand); if (constOperand != nullptr) operand = ConstantValue::get(static_cast(constOperand->getInt())); else operand = builder.createIToFInst(operand); - } else if (resultType != Type::getFloatType()) { - Value* constResult = dynamic_cast(result); + } else if (resultType != floatType) { + ConstantValue * constResult = dynamic_cast(result); if (constResult != nullptr) result = ConstantValue::get(static_cast(constResult->getInt())); else result = builder.createIToFInst(result); } - Value* constResult = dynamic_cast(result); - Value* constOperand = dynamic_cast(operand); - if (op->ADD() != nullptr) { + ConstantValue * constResult = dynamic_cast(result); + ConstantValue * constOperand = dynamic_cast(operand); + if (opType == SysYParser::ADD) { if ((constResult != nullptr) && (constOperand != nullptr)) result = ConstantValue::get(constResult->getFloat() + constOperand->getFloat()); else @@ -766,9 +774,9 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) { result = builder.createFSubInst(result, operand); } } else { - Value* constResult = dynamic_cast(result); - Value* constOperand = dynamic_cast(operand); - if (op->ADD() != nullptr) { + ConstantValue * constResult = dynamic_cast(result); + ConstantValue * constOperand = dynamic_cast(operand); + if (opType == SysYParser::ADD) { if ((constResult != nullptr) && (constOperand != nullptr)) result = ConstantValue::get(constResult->getInt() + constOperand->getInt()); else @@ -785,11 +793,13 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) { return result; } -std:any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { +std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { Value* result = std::any_cast(visitAddExp(ctx->addExp(0))); for (size_t i = 1; i < ctx->addExp().size(); i++) { - auto op = ctx->relOp(i - 1); + auto opNode = dynamic_cast(ctx->children[2*i-1]); + int opType = opNode->getSymbol()->getType(); + Value* operand = std::any_cast(visitAddExp(ctx->addExp(i))); Type* resultType = result->getType(); @@ -805,10 +815,10 @@ std:any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { auto operand2 = constOperand->isFloat() ? constOperand->getFloat() : constOperand->getInt(); - if (op->LT() != nullptr) result = ConstantValue::get(operand1 < operand2 ? 1 : 0); - else if (op->GT() != nullptr) result = ConstantValue::get(operand1 > operand2 ? 1 : 0); - else if (op->LE() != nullptr) result = ConstantValue::get(operand1 <= operand2 ? 1 : 0); - else if (op->GE() != nullptr) result = ConstantValue::get(operand1 >= operand2 ? 1 : 0); + if (opType == SysYParser::LT) result = ConstantValue::get(operand1 < operand2 ? 1 : 0); + else if (opType == SysYParser::GT) result = ConstantValue::get(operand1 > operand2 ? 1 : 0); + else if (opType == SysYParser::LE) result = ConstantValue::get(operand1 <= operand2 ? 1 : 0); + else if (opType == SysYParser::GE) result = ConstantValue::get(operand1 >= operand2 ? 1 : 0); else assert(false); } else { @@ -833,18 +843,18 @@ std:any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { } - if (op->LT() != nullptr) result = builder.createFCmpLTInst(result, operand); - else if (op->GT() != nullptr) result = builder.createFCmpGTInst(result, operand); - else if (op->LE() != nullptr) result = builder.createFCmpLEInst(result, operand); - else if (op->GE() != nullptr) result = builder.createFCmpGEInst(result, operand); + if (opType == SysYParser::LT) result = builder.createFCmpLTInst(result, operand); + else if (opType == SysYParser::GT) result = builder.createFCmpGTInst(result, operand); + else if (opType == SysYParser::LE) result = builder.createFCmpLEInst(result, operand); + else if (opType == SysYParser::GE) result = builder.createFCmpGEInst(result, operand); else assert(false); } else { // 整数处理 - if (op->LT() != nullptr) result = builder.createICmpLTInst(result, operand); - else if (op->GT() != nullptr) result = builder.createICmpGTInst(result, operand); - else if (op->LE() != nullptr) result = builder.createICmpLEInst(result, operand); - else if (op->GE() != nullptr) result = builder.createICmpGEInst(result, operand); + if (opType == SysYParser::LT) result = builder.createICmpLTInst(result, operand); + else if (opType == SysYParser::GT) result = builder.createICmpGTInst(result, operand); + else if (opType == SysYParser::LE) result = builder.createICmpLEInst(result, operand); + else if (opType == SysYParser::GE) result = builder.createICmpGEInst(result, operand); else assert(false); } @@ -855,31 +865,154 @@ std:any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) { } +std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) { + Value * result = std::any_cast(visitRelExp(ctx->relExp(0))); + + for (size_t i = 1; i < ctx->relExp().size(); i++) { + auto opNode = dynamic_cast(ctx->children[2*i-1]); + int opType = opNode->getSymbol()->getType(); + + Value * operand = std::any_cast(visitRelExp(ctx->relExp(i))); + + ConstantValue* constResult = dynamic_cast(result); + ConstantValue* constOperand = dynamic_cast(operand); + + if ((constResult != nullptr) && (constOperand != nullptr)) { + auto operand1 = constResult->isFloat() ? constResult->getFloat() + : constResult->getInt(); + auto operand2 = constOperand->isFloat() ? constOperand->getFloat() + : constOperand->getInt(); + + if (opType == SysYParser::EQ) result = ConstantValue::get(operand1 == operand2 ? 1 : 0); + else if (opType == SysYParser::NE) result = ConstantValue::get(operand1 != operand2 ? 1 : 0); + else assert(false); + + } else { + Type* resultType = result->getType(); + Type* operandType = operand->getType(); + Type* floatType = Type::getFloatType(); + + if (resultType == floatType || operandType == floatType) { + if (resultType != floatType) { + if (constResult != nullptr) + result = ConstantValue::get(static_cast(constResult->getInt())); + else + result = builder.createIToFInst(result); + } + if (operandType != floatType) { + if (constOperand != nullptr) + operand = ConstantValue::get(static_cast(constOperand->getInt())); + else + operand = builder.createIToFInst(operand); + } + + if (opType == SysYParser::EQ) result = builder.createFCmpEQInst(result, operand); + else if (opType == SysYParser::NE) result = builder.createFCmpNEInst(result, operand); + else assert(false); + + } else { + + if (opType == SysYParser::EQ) result = builder.createICmpEQInst(result, operand); + else if (opType == SysYParser::NE) result = builder.createICmpNEInst(result, operand); + else assert(false); + + } + } + } + + if (ctx->relExp().size() == 1) { + ConstantValue * constResult = dynamic_cast(result); + // 如果只有一个关系表达式,则将结果转换为0或1 + if (constResult != nullptr) { + if (constResult->isFloat()) + result = ConstantValue::get(constResult->getFloat() != 0.0F ? 1 : 0); + else + result = ConstantValue::get(constResult->getInt() != 0 ? 1 : 0); + } + } + + return result; +} + +std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){ + std::stringstream labelstring; + BasicBlock *curBlock = builder.getBasicBlock(); + Function *function = builder.getBasicBlock()->getParent(); + BasicBlock *trueBlock = builder.getTrueBlock(); + BasicBlock *falseBlock = builder.getFalseBlock(); + auto conds = ctx->eqExp(); + + for (size_t i = 0; i < conds.size() - 1; i++) { + + labelstring << "AND.L" << builder.getLabelIndex(); + BasicBlock *newtrueBlock = function->addBasicBlock(labelstring.str()); + labelstring.str(""); + + auto cond = std::any_cast(visitEqExp(ctx->eqExp(i))); + builder.createCondBrInst(cond, newtrueBlock, falseBlock, {}, {}); + + BasicBlock::conectBlocks(curBlock, newtrueBlock); + BasicBlock::conectBlocks(curBlock, falseBlock); + + curBlock = newtrueBlock; + builder.setPosition(curBlock, curBlock->end()); + } + + auto cond = std::any_cast(visitEqExp(conds.back())); + builder.createCondBrInst(cond, trueBlock, falseBlock, {}, {}); + + BasicBlock::conectBlocks(curBlock, trueBlock); + BasicBlock::conectBlocks(curBlock, falseBlock); + + return std::any(); +} + +auto SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext *ctx) -> std::any { + std::stringstream labelstring; + BasicBlock *curBlock = builder.getBasicBlock(); + Function *function = curBlock->getParent(); + auto conds = ctx->lAndExp(); + + for (size_t i = 0; i < conds.size() - 1; i++) { + labelstring << "OR.L" << builder.getLabelIndex(); + BasicBlock *newFalseBlock = function->addBasicBlock(labelstring.str()); + labelstring.str(""); + + builder.pushFalseBlock(newFalseBlock); + visitLAndExp(ctx->lAndExp(i)); + builder.popFalseBlock(); + + builder.setPosition(newFalseBlock, newFalseBlock->end()); + } + + visitLAndExp(conds.back()); + + return std::any(); +} + void Utils::tree2Array(Type *type, ArrayValueTree *root, const std::vector &dims, unsigned numDims, ValueCounter &result, IRBuilder *builder) { - auto value = root->getValue(); + Value* value = root->getValue(); auto &children = root->getChildren(); if (value != nullptr) { if (type == value->getType()) { result.push_back(value); } else { if (type == Type::getFloatType()) { - auto constValue = dynamic_cast(value); - if (constValue != nullptr) { - result.push_back( - ConstantValue::get(static_cast(constValue->getInt()))); - } else { + ConstantValue* constValue = dynamic_cast(value); + if (constValue != nullptr) + result.push_back(ConstantValue::get(static_cast(constValue->getInt()))); + else result.push_back(builder->createIToFInst(value)); - } + } else { - auto constValue = dynamic_cast(value); - if (constValue != nullptr) { - result.push_back( - ConstantValue::get(static_cast(constValue->getFloat()))); - } else { + ConstantValue* constValue = dynamic_cast(value); + if (constValue != nullptr) + result.push_back(ConstantValue::get(static_cast(constValue->getFloat()))); + else result.push_back(builder->createFtoIInst(value)); - } + } } return; @@ -909,11 +1042,10 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root, int num = blockSize - afterSize + beforeSize; if (num > 0) { - if (type == Type::getFloatType()) { + if (type == Type::getFloatType()) result.push_back(ConstantValue::get(0.0F), num); - } else { + else result.push_back(ConstantValue::get(0), num); - } } } diff --git a/src/include/SysYIRGenerator.h b/src/include/SysYIRGenerator.h index e203016..445a856 100644 --- a/src/include/SysYIRGenerator.h +++ b/src/include/SysYIRGenerator.h @@ -10,8 +10,6 @@ namespace sysy { -class SysYIRGenerator : public SysYBaseVisitor { - // @brief 用于存储数组值的树结构 // 多位数组本质上是一维数组的嵌套可以用树来表示。 class ArrayValueTree { @@ -55,6 +53,8 @@ public: static void initExternalFunction(Module *pModule, IRBuilder *pBuilder); }; +class SysYIRGenerator : public SysYBaseVisitor { + private: std::unique_ptr module; IRBuilder builder; @@ -108,7 +108,8 @@ public: // std::any visitCond(SysYParser::CondContext *ctx) override; std::any visitLValue(SysYParser::LValueContext *ctx) override; - std::any visitPrimExp(SysYParser::PrimExpContext *ctx) override; + + std::any visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) override; // std::any visitParenExp(SysYParser::ParenExpContext *ctx) override; std::any visitNumber(SysYParser::NumberContext *ctx) override; @@ -116,11 +117,11 @@ public: std::any visitCall(SysYParser::CallContext *ctx) override; - // std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override; + std::any visitUnaryExp(SysYParser::UnaryExpContext *ctx) override; // std::any visitUnaryOp(SysYParser::UnaryOpContext *ctx) override; - std::any visitUnExp(SysYParser::UnExpContext *ctx) override; - + // std::any visitUnExp(SysYParser::UnExpContext *ctx) override; + std::any visitFuncRParams(SysYParser::FuncRParamsContext *ctx) override; std::any visitMulExp(SysYParser::MulExpContext *ctx) override; std::any visitAddExp(SysYParser::AddExpContext *ctx) override; diff --git a/src/sysyc.cpp b/src/sysyc.cpp index 1328f55..40116e1 100644 --- a/src/sysyc.cpp +++ b/src/sysyc.cpp @@ -6,7 +6,6 @@ using namespace std; #include "SysYLexer.h" #include "SysYParser.h" using namespace antlr4; -#include "ASTPrinter.h" #include "Backend.h" #include "SysYIRGenerator.h" #include "LLVMIRGenerator.h" @@ -77,7 +76,7 @@ int main(int argc, char **argv) { SysYIRGenerator generator; generator.visitCompUnit(moduleAST); auto moduleIR = generator.get(); - moduleIR->print(cout); + // moduleIR->print(cout); return EXIT_SUCCESS; } else if (argStopAfter == "llvmir") { LLVMIRGenerator llvmirGenerator;