feat: complete Lab3 instruction selection and assembly generation

This commit is contained in:
2026-04-25 14:30:22 +08:00
parent 979d271ebe
commit 0b0bc04be3
13 changed files with 1078 additions and 160 deletions

View File

@@ -88,7 +88,7 @@ ir::ConstantValue* IRGenImpl::EvalConstExpr(SysYParser::ExpContext& expr) {
return static_cast<ir::ConstantValue*>(module_.GetContext().GetConstInt(value));
}
return static_cast<ir::ConstantValue*>(
module_.GetContext().GetConstFloat(std::stof(ctx->number()->FLITERAL()->getText())));
module_.GetContext().GetConstFloat(static_cast<float>(std::stod(ctx->number()->FLITERAL()->getText()))));
}
std::any visitLValueExp(SysYParser::LValueExpContext* ctx) override {
@@ -105,7 +105,17 @@ ir::ConstantValue* IRGenImpl::EvalConstExpr(SysYParser::ExpContext& expr) {
throw std::runtime_error(
FormatError("irgen", "常量缺少标量初始化表达式"));
}
return Eval(*const_def->initValue()->exp());
auto* init = Eval(*const_def->initValue()->exp());
auto* decl = dynamic_cast<SysYParser::ConstDeclContext*>(const_def->parent);
bool is_float = (decl && decl->btype() && decl->btype()->FLOAT());
if (!is_float && init->GetType()->IsFloat()) {
init = module_.GetContext().GetConstInt(
static_cast<int>(static_cast<ir::ConstantFloat*>(init)->GetValue()));
} else if (is_float && init->GetType()->IsInt32()) {
init = module_.GetContext().GetConstFloat(
static_cast<float>(static_cast<ir::ConstantInt*>(init)->GetValue()));
}
return init;
}
throw std::runtime_error(
FormatError("irgen", "全局/常量表达式必须是编译期常量"));