[midend]修复错误的RelExp类型转换
This commit is contained in:
@@ -33,6 +33,9 @@ namespace sysy {
|
|||||||
|
|
||||||
Value *SysYIRGenerator::promoteType(Value *value, Type *targetType) {
|
Value *SysYIRGenerator::promoteType(Value *value, Type *targetType) {
|
||||||
//如果是常量则直接返回相应的值
|
//如果是常量则直接返回相应的值
|
||||||
|
if (targetType == nullptr) {
|
||||||
|
return value; // 如果值为空,那就不需要转换
|
||||||
|
}
|
||||||
ConstantInteger* constInt = dynamic_cast<ConstantInteger *>(value);
|
ConstantInteger* constInt = dynamic_cast<ConstantInteger *>(value);
|
||||||
ConstantFloating *constFloat = dynamic_cast<ConstantFloating *>(value);
|
ConstantFloating *constFloat = dynamic_cast<ConstantFloating *>(value);
|
||||||
if (constInt) {
|
if (constInt) {
|
||||||
@@ -385,9 +388,9 @@ Value* SysYIRGenerator::computeExp(SysYParser::ExpContext *ctx, Type* targetType
|
|||||||
BinaryExpLenStack.push_back(0); // 进入新的层次时Push 0
|
BinaryExpLenStack.push_back(0); // 进入新的层次时Push 0
|
||||||
visitAddExp(ctx->addExp());
|
visitAddExp(ctx->addExp());
|
||||||
|
|
||||||
if(targetType == nullptr) {
|
// if(targetType == nullptr) {
|
||||||
targetType = Type::getIntType(); // 默认目标类型为int
|
// targetType = Type::getIntType(); // 默认目标类型为int
|
||||||
}
|
// }
|
||||||
|
|
||||||
compute();
|
compute();
|
||||||
// 最后一个Value应该是最终结果
|
// 最后一个Value应该是最终结果
|
||||||
@@ -428,9 +431,9 @@ Value* SysYIRGenerator::computeAddExp(SysYParser::AddExpContext *ctx, Type* targ
|
|||||||
visitMulExp(ctx->mulExp(i));
|
visitMulExp(ctx->mulExp(i));
|
||||||
// BinaryValueStack.push_back(operand);
|
// BinaryValueStack.push_back(operand);
|
||||||
}
|
}
|
||||||
if(targetType == nullptr) {
|
// if(targetType == nullptr) {
|
||||||
targetType = Type::getIntType(); // 默认目标类型为int
|
// targetType = Type::getIntType(); // 默认目标类型为int
|
||||||
}
|
// }
|
||||||
// 根据后缀表达式的逻辑计算
|
// 根据后缀表达式的逻辑计算
|
||||||
compute();
|
compute();
|
||||||
// 最后一个Value应该是最终结果
|
// 最后一个Value应该是最终结果
|
||||||
@@ -1620,13 +1623,13 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
|
||||||
Value* result = computeAddExp(ctx->addExp(0), Type::getIntType());
|
Value* result = computeAddExp(ctx->addExp(0));
|
||||||
|
|
||||||
for (int i = 1; i < ctx->addExp().size(); i++) {
|
for (int i = 1; i < ctx->addExp().size(); i++) {
|
||||||
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
|
||||||
int opType = opNode->getSymbol()->getType();
|
int opType = opNode->getSymbol()->getType();
|
||||||
|
|
||||||
Value* operand = computeAddExp(ctx->addExp(i), Type::getIntType());
|
Value* operand = computeAddExp(ctx->addExp(i));
|
||||||
|
|
||||||
Type* resultType = result->getType();
|
Type* resultType = result->getType();
|
||||||
Type* operandType = operand->getType();
|
Type* operandType = operand->getType();
|
||||||
|
|||||||
Reference in New Issue
Block a user