[IR]重构常量定义,引入undefvalue定义,修改常量方法使用尽量适配旧版
This commit is contained in:
@@ -145,8 +145,8 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
for (size_t i = 0; i < counterNumbers.size(); i++) {
|
||||
|
||||
builder.createMemsetInst(
|
||||
alloca, ConstantValue::get(static_cast<int>(begin)),
|
||||
ConstantValue::get(static_cast<int>(counterNumbers[i])),
|
||||
alloca, ConstantInteger::get(begin),
|
||||
ConstantInteger::get(static_cast<int>(counterNumbers[i])),
|
||||
counterValues[i]);
|
||||
begin += counterNumbers[i];
|
||||
}
|
||||
@@ -218,7 +218,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
paramNames.push_back(param->Ident()->getText());
|
||||
std::vector<Value *> dims = {};
|
||||
if (!param->LBRACK().empty()) {
|
||||
dims.push_back(ConstantValue::get(-1)); // 第一个维度不确定
|
||||
dims.push_back(ConstantInteger::get(-1)); // 第一个维度不确定
|
||||
for (const auto &exp : param->exp()) {
|
||||
dims.push_back(std::any_cast<Value *>(visitExp(exp)));
|
||||
}
|
||||
@@ -247,9 +247,9 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
if(HasReturnInst == false) {
|
||||
// 如果没有return语句,则默认返回0
|
||||
if (returnType != Type::getVoidType()) {
|
||||
Value* returnValue = ConstantValue::get(0);
|
||||
Value* returnValue = ConstantInteger::get(0);
|
||||
if (returnType == Type::getFloatType()) {
|
||||
returnValue = ConstantValue::get(0.0f);
|
||||
returnValue = ConstantFloating::get(0.0f);
|
||||
}
|
||||
builder.createReturnInst(returnValue);
|
||||
} else {
|
||||
@@ -286,9 +286,9 @@ std::any SysYIRGenerator::visitAssignStmt(SysYParser::AssignStmtContext *ctx) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr) {
|
||||
if (variableType == Type::getFloatType()) {
|
||||
value = ConstantValue::get(static_cast<float>(constValue->getInt()));
|
||||
value = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
value = ConstantValue::get(static_cast<int>(constValue->getFloat()));
|
||||
value = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
} else {
|
||||
if (variableType == Type::getFloatType()) {
|
||||
@@ -478,9 +478,9 @@ std::any SysYIRGenerator::visitReturnStmt(SysYParser::ReturnStmtContext *ctx) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(returnValue);
|
||||
if (constValue != nullptr) {
|
||||
if (funcType == Type::getFloatType()) {
|
||||
returnValue = ConstantValue::get(static_cast<float>(constValue->getInt()));
|
||||
returnValue = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
returnValue = ConstantValue::get(static_cast<int>(constValue->getFloat()));
|
||||
returnValue = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
} else {
|
||||
if (funcType == Type::getFloatType()) {
|
||||
@@ -560,10 +560,10 @@ std::any SysYIRGenerator::visitPrimaryExp(SysYParser::PrimaryExpContext *ctx) {
|
||||
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(value));
|
||||
return static_cast<Value *>(ConstantInteger::get(value));
|
||||
} else if (ctx->FLITERAL() != nullptr) {
|
||||
float value = std::stof(ctx->FLITERAL()->getText());
|
||||
return static_cast<Value *>(ConstantValue::get(value));
|
||||
return static_cast<Value *>(ConstantFloating::get(value));
|
||||
}
|
||||
throw std::runtime_error("Unknown number type.");
|
||||
return std::any(); // 不会到达这里
|
||||
@@ -599,9 +599,9 @@ std::any SysYIRGenerator::visitCall(SysYParser::CallContext *ctx) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(args[i]);
|
||||
if (constValue != nullptr) {
|
||||
if (params[i]->getType() == Type::getPointerType(Type::getFloatType())) {
|
||||
args[i] = ConstantValue::get(static_cast<float>(constValue->getInt()));
|
||||
args[i] = ConstantInteger::get(static_cast<float>(constValue->getInt()));
|
||||
} else {
|
||||
args[i] = ConstantValue::get(static_cast<int>(constValue->getFloat()));
|
||||
args[i] = ConstantFloating::get(static_cast<int>(constValue->getFloat()));
|
||||
}
|
||||
} else {
|
||||
if (params[i]->getType() == Type::getPointerType(Type::getFloatType())) {
|
||||
@@ -629,9 +629,9 @@ std::any SysYIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext *ctx) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr) {
|
||||
if (constValue->isFloat()) {
|
||||
result = ConstantValue::get(-constValue->getFloat());
|
||||
result = ConstantFloating::get(-constValue->getFloat());
|
||||
} else {
|
||||
result = ConstantValue::get(-constValue->getInt());
|
||||
result = ConstantInteger::get(-constValue->getInt());
|
||||
}
|
||||
} else if (value != nullptr) {
|
||||
if (value->getType() == Type::getIntType()) {
|
||||
@@ -648,9 +648,9 @@ std::any SysYIRGenerator::visitUnaryExp(SysYParser::UnaryExpContext *ctx) {
|
||||
if (constValue != nullptr) {
|
||||
if (constValue->isFloat()) {
|
||||
result =
|
||||
ConstantValue::get(1 - (constValue->getFloat() != 0.0F ? 1 : 0));
|
||||
ConstantFloating::get(1 - (constValue->getFloat() != 0.0F ? 1 : 0));
|
||||
} else {
|
||||
result = ConstantValue::get(1 - (constValue->getInt() != 0 ? 1 : 0));
|
||||
result = ConstantInteger::get(1 - (constValue->getInt() != 0 ? 1 : 0));
|
||||
}
|
||||
} else if (value != nullptr) {
|
||||
if (value->getType() == Type::getIntType()) {
|
||||
@@ -692,13 +692,13 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
if (operandType != floatType) {
|
||||
ConstantValue * constValue = dynamic_cast<ConstantValue *>(operand);
|
||||
if (constValue != nullptr)
|
||||
operand = ConstantValue::get(static_cast<float>(constValue->getInt()));
|
||||
operand = ConstantFloating::get(static_cast<float>(constValue->getInt()));
|
||||
else
|
||||
operand = builder.createIToFInst(operand);
|
||||
} else if (resultType != floatType) {
|
||||
ConstantValue* constResult = dynamic_cast<ConstantValue *>(result);
|
||||
if (constResult != nullptr)
|
||||
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
else
|
||||
result = builder.createIToFInst(result);
|
||||
}
|
||||
@@ -707,14 +707,14 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
ConstantValue* constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (opType == SysYParser::MUL) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr)) {
|
||||
result = ConstantValue::get(constResult->getFloat() *
|
||||
result = ConstantFloating::get(constResult->getFloat() *
|
||||
constOperand->getFloat());
|
||||
} else {
|
||||
result = builder.createFMulInst(result, operand);
|
||||
}
|
||||
} else if (opType == SysYParser::DIV) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr)) {
|
||||
result = ConstantValue::get(constResult->getFloat() /
|
||||
result = ConstantFloating::get(constResult->getFloat() /
|
||||
constOperand->getFloat());
|
||||
} else {
|
||||
result = builder.createFDivInst(result, operand);
|
||||
@@ -729,17 +729,17 @@ std::any SysYIRGenerator::visitMulExp(SysYParser::MulExpContext *ctx) {
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (opType == SysYParser::MUL) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr))
|
||||
result = ConstantValue::get(constResult->getInt() * constOperand->getInt());
|
||||
result = ConstantInteger::get(constResult->getInt() * constOperand->getInt());
|
||||
else
|
||||
result = builder.createMulInst(result, operand);
|
||||
} else if (opType == SysYParser::DIV) {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr))
|
||||
result = ConstantValue::get(constResult->getInt() / constOperand->getInt());
|
||||
result = ConstantInteger::get(constResult->getInt() / constOperand->getInt());
|
||||
else
|
||||
result = builder.createDivInst(result, operand);
|
||||
} else {
|
||||
if ((constOperand != nullptr) && (constResult != nullptr))
|
||||
result = ConstantValue::get(constResult->getInt() % constOperand->getInt());
|
||||
result = ConstantInteger::get(constResult->getInt() % constOperand->getInt());
|
||||
else
|
||||
result = builder.createRemInst(result, operand);
|
||||
}
|
||||
@@ -767,13 +767,13 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
if (operandType != floatType) {
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (constOperand != nullptr)
|
||||
operand = ConstantValue::get(static_cast<float>(constOperand->getInt()));
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
else
|
||||
operand = builder.createIToFInst(operand);
|
||||
} else if (resultType != floatType) {
|
||||
ConstantValue * constResult = dynamic_cast<ConstantValue *>(result);
|
||||
if (constResult != nullptr)
|
||||
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
|
||||
result = ConstantFloating::get(static_cast<float>(constResult->getInt()));
|
||||
else
|
||||
result = builder.createIToFInst(result);
|
||||
}
|
||||
@@ -782,12 +782,12 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (opType == SysYParser::ADD) {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
result = ConstantValue::get(constResult->getFloat() + constOperand->getFloat());
|
||||
result = ConstantFloating::get(constResult->getFloat() + constOperand->getFloat());
|
||||
else
|
||||
result = builder.createFAddInst(result, operand);
|
||||
} else {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
result = ConstantValue::get(constResult->getFloat() - constOperand->getFloat());
|
||||
result = ConstantFloating::get(constResult->getFloat() - constOperand->getFloat());
|
||||
else
|
||||
result = builder.createFSubInst(result, operand);
|
||||
}
|
||||
@@ -796,12 +796,12 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
||||
ConstantValue * constOperand = dynamic_cast<ConstantValue *>(operand);
|
||||
if (opType == SysYParser::ADD) {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
result = ConstantValue::get(constResult->getInt() + constOperand->getInt());
|
||||
result = ConstantInteger::get(constResult->getInt() + constOperand->getInt());
|
||||
else
|
||||
result = builder.createAddInst(result, operand);
|
||||
} else {
|
||||
if ((constResult != nullptr) && (constOperand != nullptr))
|
||||
result = ConstantValue::get(constResult->getInt() - constOperand->getInt());
|
||||
result = ConstantInteger::get(constResult->getInt() - constOperand->getInt());
|
||||
else
|
||||
result = builder.createSubInst(result, operand);
|
||||
}
|
||||
@@ -833,10 +833,10 @@ std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||
auto operand2 = constOperand->isFloat() ? constOperand->getFloat()
|
||||
: constOperand->getInt();
|
||||
|
||||
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);
|
||||
if (opType == SysYParser::LT) result = ConstantInteger::get(operand1 < operand2 ? 1 : 0);
|
||||
else if (opType == SysYParser::GT) result = ConstantInteger::get(operand1 > operand2 ? 1 : 0);
|
||||
else if (opType == SysYParser::LE) result = ConstantInteger::get(operand1 <= operand2 ? 1 : 0);
|
||||
else if (opType == SysYParser::GE) result = ConstantInteger::get(operand1 >= operand2 ? 1 : 0);
|
||||
else assert(false);
|
||||
|
||||
} else {
|
||||
@@ -848,14 +848,14 @@ std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||
if (resultType == floatType || operandType == floatType) {
|
||||
if (resultType != floatType) {
|
||||
if (constResult != nullptr)
|
||||
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
|
||||
result = ConstantFloating::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()));
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
else
|
||||
operand = builder.createIToFInst(operand);
|
||||
|
||||
@@ -901,8 +901,8 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
|
||||
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);
|
||||
if (opType == SysYParser::EQ) result = ConstantInteger::get(operand1 == operand2 ? 1 : 0);
|
||||
else if (opType == SysYParser::NE) result = ConstantInteger::get(operand1 != operand2 ? 1 : 0);
|
||||
else assert(false);
|
||||
|
||||
} else {
|
||||
@@ -913,13 +913,13 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
|
||||
if (resultType == floatType || operandType == floatType) {
|
||||
if (resultType != floatType) {
|
||||
if (constResult != nullptr)
|
||||
result = ConstantValue::get(static_cast<float>(constResult->getInt()));
|
||||
result = ConstantFloating::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()));
|
||||
operand = ConstantFloating::get(static_cast<float>(constOperand->getInt()));
|
||||
else
|
||||
operand = builder.createIToFInst(operand);
|
||||
}
|
||||
@@ -943,9 +943,9 @@ std::any SysYIRGenerator::visitEqExp(SysYParser::EqExpContext *ctx) {
|
||||
// 如果只有一个关系表达式,则将结果转换为0或1
|
||||
if (constResult != nullptr) {
|
||||
if (constResult->isFloat())
|
||||
result = ConstantValue::get(constResult->getFloat() != 0.0F ? 1 : 0);
|
||||
result = ConstantInteger::get(constResult->getFloat() != 0.0F ? 1 : 0);
|
||||
else
|
||||
result = ConstantValue::get(constResult->getInt() != 0 ? 1 : 0);
|
||||
result = ConstantInteger::get(constResult->getInt() != 0 ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1013,6 +1013,7 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||
ValueCounter &result, IRBuilder *builder) {
|
||||
Value* value = root->getValue();
|
||||
auto &children = root->getChildren();
|
||||
// 类型转换
|
||||
if (value != nullptr) {
|
||||
if (type == value->getType()) {
|
||||
result.push_back(value);
|
||||
@@ -1020,14 +1021,14 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||
if (type == Type::getFloatType()) {
|
||||
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr)
|
||||
result.push_back(ConstantValue::get(static_cast<float>(constValue->getInt())));
|
||||
result.push_back(ConstantFloating::get(static_cast<float>(constValue->getInt())));
|
||||
else
|
||||
result.push_back(builder->createIToFInst(value));
|
||||
|
||||
} else {
|
||||
ConstantValue* constValue = dynamic_cast<ConstantValue *>(value);
|
||||
if (constValue != nullptr)
|
||||
result.push_back(ConstantValue::get(static_cast<int>(constValue->getFloat())));
|
||||
result.push_back(ConstantInteger::get(static_cast<int>(constValue->getFloat())));
|
||||
else
|
||||
result.push_back(builder->createFtoIInst(value));
|
||||
|
||||
@@ -1061,9 +1062,9 @@ void Utils::tree2Array(Type *type, ArrayValueTree *root,
|
||||
int num = blockSize - afterSize + beforeSize;
|
||||
if (num > 0) {
|
||||
if (type == Type::getFloatType())
|
||||
result.push_back(ConstantValue::get(0.0F), num);
|
||||
result.push_back(ConstantFloating::get(0.0F), num);
|
||||
else
|
||||
result.push_back(ConstantValue::get(0), num);
|
||||
result.push_back(ConstantInteger::get(0), num);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1101,7 +1102,7 @@ void Utils::initExternalFunction(Module *pModule, IRBuilder *pBuilder) {
|
||||
funcName, pModule, pBuilder);
|
||||
paramTypes.push_back(Type::getIntType());
|
||||
paramNames.emplace_back("x");
|
||||
paramDims.push_back(std::vector<Value *>{ConstantValue::get(-1)});
|
||||
paramDims.push_back(std::vector<Value *>{ConstantInteger::get(-1)});
|
||||
funcName = "getarray";
|
||||
Utils::createExternalFunction(paramTypes, paramNames, paramDims, returnType,
|
||||
funcName, pModule, pBuilder);
|
||||
@@ -1117,7 +1118,7 @@ void Utils::initExternalFunction(Module *pModule, IRBuilder *pBuilder) {
|
||||
returnType = Type::getIntType();
|
||||
paramTypes.push_back(Type::getFloatType());
|
||||
paramNames.emplace_back("x");
|
||||
paramDims.push_back(std::vector<Value *>{ConstantValue::get(-1)});
|
||||
paramDims.push_back(std::vector<Value *>{ConstantInteger::get(-1)});
|
||||
funcName = "getfarray";
|
||||
Utils::createExternalFunction(paramTypes, paramNames, paramDims, returnType,
|
||||
funcName, pModule, pBuilder);
|
||||
@@ -1141,7 +1142,7 @@ void Utils::initExternalFunction(Module *pModule, IRBuilder *pBuilder) {
|
||||
paramTypes.push_back(Type::getIntType());
|
||||
paramDims.clear();
|
||||
paramDims.emplace_back();
|
||||
paramDims.push_back(std::vector<Value *>{ConstantValue::get(-1)});
|
||||
paramDims.push_back(std::vector<Value *>{ConstantInteger::get(-1)});
|
||||
paramNames.clear();
|
||||
paramNames.emplace_back("n");
|
||||
paramNames.emplace_back("a");
|
||||
@@ -1164,7 +1165,7 @@ void Utils::initExternalFunction(Module *pModule, IRBuilder *pBuilder) {
|
||||
paramTypes.push_back(Type::getFloatType());
|
||||
paramDims.clear();
|
||||
paramDims.emplace_back();
|
||||
paramDims.push_back(std::vector<Value *>{ConstantValue::get(-1)});
|
||||
paramDims.push_back(std::vector<Value *>{ConstantInteger::get(-1)});
|
||||
paramNames.clear();
|
||||
paramNames.emplace_back("n");
|
||||
paramNames.emplace_back("a");
|
||||
|
||||
Reference in New Issue
Block a user