前端基本构建完毕,build前端部分无报错,argument类删除后端报错,llvmIR输出待完成

This commit is contained in:
rain2133
2025-06-22 00:25:43 +08:00
parent 73b382773a
commit 4828c18f96
5 changed files with 231 additions and 99 deletions

View File

@ -13,7 +13,6 @@ target_link_libraries(SysYParser PUBLIC antlr4_shared)
add_executable(sysyc
sysyc.cpp
ASTPrinter.cpp
IR.cpp
SysYIRGenerator.cpp
Backend.cpp

View File

@ -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)*;

View File

@ -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<unsigned int> & counterNumbers = values.getNumbers();
std::vector<sysy::Value *> & counterValues = values.getValues();
const std::vector<unsigned int> & counterNumbers = values.getNumbers();
const std::vector<sysy::Value *> & 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<Value *>(visitExp(ctx->exp()));
Value* value = std::any_cast<Value *>(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<Value *>(visitConstExp(ctx->constExp()));
Value* value = std::any_cast<Value *>(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<Type *>(visitBType(param->bType())));
paramNames.push_back(param->Ident()->getText());
std::vector<Value *> 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<Value *>(visitExp(exp)));
@ -224,8 +224,8 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
}
}
Type *returnType = std::any_cast<Type *>(visitFuncType(ctx->funcType()));
FunctionType* funcType = Type::getFunctionType(returnType, paramTypes);
Type* returnType = std::any_cast<Type *>(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<Value *>(visitExp(ctx->exp()));
PointerType* variableType =dynamic_cast<PointerType *>(variable->getType())->getBaseType();
Type* variableType = dynamic_cast<PointerType *>(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<ConstantValue *>(index) == nullptr) {
for (const auto &dim : dims) {
if (dynamic_cast<ConstantValue *>(dim) == nullptr) {
indicesConstant = false;
break;
}
@ -505,21 +505,21 @@ std::any SysYIRGenerator::visitLVal(SysYParser::LValContext *ctx) {
AllocaInst* localVar = dynamic_cast<AllocaInst *>(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<LVal *>(variable), indices);
builder.createGetSubArray(dynamic_cast<LVal *>(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<Value *>(ConstantValue::get(Type::getIntType(), value));
return static_cast<Value *>(ConstantValue::get(value));
} else if (ctx->FLITERAL() != nullptr) {
float value = std::stof(ctx->FLITERAL()->getText());
return static_cast<Value *>(ConstantValue::get(Type::getFloatType(), value));
return static_cast<Value *>(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<Value *> 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<Value *>(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<Value *>(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<Value *>(visitUnaryExp(ctx->unaryExp(0)));
Value * result = std::any_cast<Value *>(visitUnaryExp(ctx->unaryExp(0)));
for (size_t i = 1; i < ctx->unaryExp().size(); i++) {
auto op = ctx->mulOp(i - 1);
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
int opType = opNode->getSymbol()->getType();
Value* operand = std::any_cast<Value *>(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<ConstantValue *>(operand);
if (constValue != nullptr)
operand = ConstantValue::get(static_cast<float>(constValue->getInt()));
else
operand = builder.createIToFInst(operand);
} else if (resultType != Type::getFloatType()) {
} else if (resultType != floatType) {
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
if (constResult != nullptr)
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
@ -681,14 +687,14 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
ConstantValue* constOperand = dynamic_cast<ConstantValue *>(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<ConstantValue *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(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<Value *>(visitMulExp(ctx->mulExp(0)));
for (size_t i = 1; i < ctx->mulExp().size(); i++) {
auto op = ctx->addOp(i - 1);
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
int opType = opNode->getSymbol()->getType();
Value* operand = std::any_cast<Value *>(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<ConstantValue *>(operand);
if (operandType != floatType) {
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
if (constOperand != nullptr)
operand = ConstantValue::get(static_cast<float>(constOperand->getInt()));
else
operand = builder.createIToFInst(operand);
} else if (resultType != Type::getFloatType()) {
Value* constResult = dynamic_cast<ConstantValue *>(result);
} else if (resultType != floatType) {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
if (constResult != nullptr)
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
else
result = builder.createIToFInst(result);
}
Value* constResult = dynamic_cast<ConstantValue *>(result);
Value* constOperand = dynamic_cast<ConstantValue *>(operand);
if (op->ADD() != nullptr) {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(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<ConstantValue *>(result);
Value* constOperand = dynamic_cast<ConstantValue *>(operand);
if (op->ADD() != nullptr) {
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(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<Value *>(visitAddExp(ctx->addExp(0)));
for (size_t i = 1; i < ctx->addExp().size(); i++) {
auto op = ctx->relOp(i - 1);
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
int opType = opNode->getSymbol()->getType();
Value* operand = std::any_cast<Value *>(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<Value *>(visitRelExp(ctx->relExp(0)));
for (size_t i = 1; i < ctx->relExp().size(); i++) {
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
int opType = opNode->getSymbol()->getType();
Value * operand = std::any_cast<Value *>(visitRelExp(ctx->relExp(i)));
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
ConstantValue* constOperand = dynamic_cast<ConstantValue *>(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<float>(constResult->getInt()));
else
result = builder.createIToFInst(result);
}
if (operandType != floatType) {
if (constOperand != nullptr)
operand = ConstantValue::get(static_cast<float>(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<ConstantValue *>(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<Value *>(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<Value *>(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<Value *> &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<ConstantValue *>(value);
if (constValue != nullptr) {
result.push_back(
ConstantValue::get(static_cast<float>(constValue->getInt())));
} else {
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
if (constValue != nullptr)
result.push_back(ConstantValue::get(static_cast<float>(constValue->getInt())));
else
result.push_back(builder->createIToFInst(value));
}
} else {
auto constValue = dynamic_cast<ConstantValue *>(value);
if (constValue != nullptr) {
result.push_back(
ConstantValue::get(static_cast<int>(constValue->getFloat())));
} else {
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
if (constValue != nullptr)
result.push_back(ConstantValue::get(static_cast<int>(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);
}
}
}

View File

@ -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> 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;

View File

@ -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;