|
|
#include "irgen/IRGen.h"
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
#include "SysYParser.h"
|
|
|
#include "ir/IR.h"
|
|
|
#include "utils/Log.h"
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
// 使用 LValContext 而不是 LValueContext
|
|
|
std::string GetLValueName(SysYParser::LValContext& lvalue) {
|
|
|
if (!lvalue.Ident()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "非法左值"));
|
|
|
}
|
|
|
return lvalue.Ident()->getText();
|
|
|
}
|
|
|
|
|
|
int TryGetConstInt(SysYParser::ConstExpContext* ctx) {
|
|
|
// 这里是一个简化的版本,实际上应该调用语义分析的常量求值
|
|
|
// 暂时假设所有常量表达式都是整数常量
|
|
|
// 实际实现需要更复杂的逻辑
|
|
|
|
|
|
// 简化为返回10
|
|
|
return 10;
|
|
|
}
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
// 注意:visitBlock 已经在 IRGenFunc.cpp 中实现,这里不要重复定义
|
|
|
|
|
|
std::any IRGenImpl::visitDecl(SysYParser::DeclContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitDecl: 开始处理声明" << std::endl;
|
|
|
if (!ctx) {
|
|
|
throw std::runtime_error(FormatError("irgen", "缺少变量声明"));
|
|
|
}
|
|
|
|
|
|
// 处理 varDecl
|
|
|
if (auto* varDecl = ctx->varDecl()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitDecl: 处理变量声明" << std::endl;
|
|
|
// 检查类型
|
|
|
if (varDecl->bType() && varDecl->bType()->Int()) {
|
|
|
for (auto* varDef : varDecl->varDef()) {
|
|
|
varDef->accept(this);
|
|
|
}
|
|
|
} else {
|
|
|
throw std::runtime_error(FormatError("irgen", "当前仅支持 int 类型变量"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 处理 constDecl
|
|
|
if (ctx->constDecl()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitDecl: 处理常量声明" << std::endl;
|
|
|
auto* constDecl = ctx->constDecl();
|
|
|
|
|
|
if (constDecl->bType() && constDecl->bType()->Int()) {
|
|
|
for (auto* constDef : constDecl->constDef()) {
|
|
|
constDef->accept(this);
|
|
|
}
|
|
|
} else if (constDecl->bType() && constDecl->bType()->Float()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "float常量暂未实现"));
|
|
|
} else {
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的常量类型"));
|
|
|
}
|
|
|
}
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitDecl: 声明处理完成" << std::endl;
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
// 在 IRGenDecl.cpp 中确保有这个函数
|
|
|
std::any IRGenImpl::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitConstDecl: 开始处理常量声明" << std::endl;
|
|
|
if (!ctx) {
|
|
|
throw std::runtime_error(FormatError("irgen", "非法常量声明"));
|
|
|
}
|
|
|
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitConstDecl: processing constant declaration" << std::endl;
|
|
|
|
|
|
// 检查类型
|
|
|
if (ctx->bType()) {
|
|
|
if (ctx->bType()->Int()) {
|
|
|
// int 类型常量
|
|
|
for (auto* constDef : ctx->constDef()) {
|
|
|
if (constDef) {
|
|
|
constDef->accept(this);
|
|
|
}
|
|
|
}
|
|
|
} else if (ctx->bType()->Float()) {
|
|
|
// float 类型常量(暂不支持)
|
|
|
throw std::runtime_error(FormatError("irgen", "float常量暂未实现"));
|
|
|
} else {
|
|
|
throw std::runtime_error(FormatError("irgen", "未知的常量类型"));
|
|
|
}
|
|
|
} else {
|
|
|
throw std::runtime_error(FormatError("irgen", "常量声明缺少类型"));
|
|
|
}
|
|
|
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitConstDecl: 常量声明处理完成" << std::endl;
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
std::any IRGenImpl::visitConstDef(SysYParser::ConstDefContext* ctx) {
|
|
|
if (!ctx || !ctx->Ident()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "非法常量定义"));
|
|
|
}
|
|
|
|
|
|
std::string const_name = ctx->Ident()->getText();
|
|
|
|
|
|
// 检查是否为数组
|
|
|
bool is_array = !ctx->constExp().empty();
|
|
|
|
|
|
// 获取常量类型(int 或 float)
|
|
|
bool is_float = false;
|
|
|
auto* constDecl = dynamic_cast<SysYParser::ConstDeclContext*>(ctx->parent);
|
|
|
if (constDecl && constDecl->bType()) {
|
|
|
if (constDecl->bType()->Float()) {
|
|
|
is_float = true;
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitConstDef: 常量 " << const_name << " 是 float 类型" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (is_array) {
|
|
|
// 数组常量处理
|
|
|
std::vector<int> dimensions;
|
|
|
for (auto* const_exp : ctx->constExp()) {
|
|
|
int dim_size = TryEvaluateConstInt(const_exp);
|
|
|
if (dim_size <= 0) dim_size = 1;
|
|
|
dimensions.push_back(dim_size);
|
|
|
}
|
|
|
|
|
|
// 创建数组类型
|
|
|
std::shared_ptr<ir::Type> element_type;
|
|
|
if (is_float) {
|
|
|
element_type = ir::Type::GetFloatType();
|
|
|
} else {
|
|
|
element_type = ir::Type::GetInt32Type();
|
|
|
}
|
|
|
|
|
|
auto array_type = ir::Type::GetArrayType(element_type, dimensions);
|
|
|
ir::GlobalValue* global_array = module_.CreateGlobal(const_name, array_type);
|
|
|
|
|
|
// 处理初始化值
|
|
|
std::vector<ir::ConstantValue*> init_consts;
|
|
|
if (auto* const_init_val = ctx->constInitVal()) {
|
|
|
auto result = const_init_val->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
auto init_vec = std::any_cast<std::vector<ir::Value*>>(result);
|
|
|
for (auto* val : init_vec) {
|
|
|
if (is_float) {
|
|
|
if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(val)) {
|
|
|
init_consts.push_back(const_float);
|
|
|
} else if (auto* const_int = dynamic_cast<ir::ConstantInt*>(val)) {
|
|
|
// 整数转浮点
|
|
|
float float_val = static_cast<float>(const_int->GetValue());
|
|
|
init_consts.push_back(builder_.CreateConstFloat(float_val));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
}
|
|
|
} else {
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(val)) {
|
|
|
init_consts.push_back(const_int);
|
|
|
} else if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(val)) {
|
|
|
// 浮点转整数
|
|
|
int int_val = static_cast<int>(const_float->GetValue());
|
|
|
init_consts.push_back(builder_.CreateConstInt(int_val));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
try {
|
|
|
ir::Value* single_val = std::any_cast<ir::Value*>(result);
|
|
|
if (is_float) {
|
|
|
if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(single_val)) {
|
|
|
init_consts.push_back(const_float);
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
}
|
|
|
} else {
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(single_val)) {
|
|
|
init_consts.push_back(const_int);
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
} catch (...) {}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 补0
|
|
|
int total_size = 1;
|
|
|
for (int dim : dimensions) total_size *= dim;
|
|
|
while (init_consts.size() < static_cast<size_t>(total_size)) {
|
|
|
if (is_float) {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
global_array->SetInitializer(init_consts);
|
|
|
global_array->SetConstant(true);
|
|
|
|
|
|
const_storage_map_[ctx] = global_array;
|
|
|
const_global_map_[const_name] = global_array;
|
|
|
|
|
|
} else {
|
|
|
// 标量常量处理
|
|
|
if (!ctx->constInitVal()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "常量缺少初始值"));
|
|
|
}
|
|
|
|
|
|
ir::ConstantValue* const_value = nullptr;
|
|
|
auto* const_init_val = ctx->constInitVal();
|
|
|
|
|
|
if (const_init_val->constExp()) {
|
|
|
// 对于常量表达式,我们可以尝试直接求值
|
|
|
if (is_float) {
|
|
|
// TODO: 实现浮点常量表达式的求值
|
|
|
const_value = module_.GetContext().GetConstFloat(0.0f);
|
|
|
} else {
|
|
|
int value = TryEvaluateConstInt(const_init_val->constExp());
|
|
|
const_value = module_.GetContext().GetConstInt(value);
|
|
|
}
|
|
|
} else {
|
|
|
if (is_float) {
|
|
|
const_value = module_.GetContext().GetConstFloat(0.0f);
|
|
|
} else {
|
|
|
const_value = module_.GetContext().GetConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 存储常量值到映射
|
|
|
const_value_map_[const_name] = const_value;
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
|
|
|
// TO DO:visitVarDef来区分全局和局部变量,并且正确处理数组变量的定义和初始化
|
|
|
std::any IRGenImpl::visitVarDef(SysYParser::VarDefContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitVarDef: 开始处理变量定义" << std::endl;
|
|
|
if (!ctx) {
|
|
|
throw std::runtime_error(FormatError("irgen", "缺少变量定义"));
|
|
|
}
|
|
|
|
|
|
if (!ctx->Ident()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "变量声明缺少名称"));
|
|
|
}
|
|
|
|
|
|
std::string varName = ctx->Ident()->getText();
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitVarDef: 变量名称: " << varName << std::endl;
|
|
|
// 防止同一个变量被多次分配存储空间。
|
|
|
if (storage_map_.find(ctx) != storage_map_.end()) {
|
|
|
throw std::runtime_error(FormatError("irgen", "声明重复生成存储槽位: " + varName));
|
|
|
}
|
|
|
|
|
|
bool is_array = !ctx->constExp().empty();
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitVarDef: 是否为数组: " << (is_array ? "是" : "否") << std::endl;
|
|
|
|
|
|
// 使用 func_ 来判断:func_ == nullptr 表示在全局作用域
|
|
|
if (func_ == nullptr) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitVarDef: 处理全局变量" << std::endl;
|
|
|
// 全局变量处理
|
|
|
return HandleGlobalVariable(ctx, varName, is_array);
|
|
|
} else {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitVarDef: 处理局部变量" << std::endl;
|
|
|
// 局部变量处理
|
|
|
return HandleLocalVariable(ctx, varName, is_array);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
|
|
|
const std::string& varName,
|
|
|
bool is_array) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 开始处理全局变量 " << varName << std::endl;
|
|
|
|
|
|
// 获取变量类型(int 或 float)
|
|
|
bool is_float = false;
|
|
|
auto* varDecl = dynamic_cast<SysYParser::VarDeclContext*>(ctx->parent);
|
|
|
if (varDecl && varDecl->bType()) {
|
|
|
if (varDecl->bType()->Float()) {
|
|
|
is_float = true;
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 变量 " << varName << " 是 float 类型" << std::endl;
|
|
|
} else if (varDecl->bType()->Int()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 变量 " << varName << " 是 int 类型" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (is_array) {
|
|
|
// 全局数组变量
|
|
|
int total_size = 1;
|
|
|
std::vector<int> dimensions;
|
|
|
|
|
|
// 计算总大小
|
|
|
for (auto* const_exp : ctx->constExp()) {
|
|
|
int dim_size = TryEvaluateConstInt(const_exp);
|
|
|
if (dim_size <= 0) {
|
|
|
dim_size = 1;
|
|
|
std::cerr << "[WARNING] HandleGlobalVariable: 无法确定数组维度大小,使用1" << std::endl;
|
|
|
}
|
|
|
dimensions.push_back(dim_size);
|
|
|
total_size *= dim_size;
|
|
|
}
|
|
|
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 数组总大小: " << total_size << std::endl;
|
|
|
|
|
|
// 创建数组类型
|
|
|
std::shared_ptr<ir::Type> element_type;
|
|
|
if (is_float) {
|
|
|
element_type = ir::Type::GetFloatType();
|
|
|
} else {
|
|
|
element_type = ir::Type::GetInt32Type();
|
|
|
}
|
|
|
|
|
|
auto array_type = ir::Type::GetArrayType(element_type, dimensions);
|
|
|
ir::GlobalValue* global_array = module_.CreateGlobal(varName, array_type);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 创建全局数组: " << varName << std::endl;
|
|
|
|
|
|
// 处理初始化值
|
|
|
std::vector<ir::ConstantValue*> init_consts;
|
|
|
if (auto* initVal = ctx->initVal()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 处理初始化值" << std::endl;
|
|
|
auto result = initVal->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
auto init_vec = std::any_cast<std::vector<ir::Value*>>(result);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 获取到初始化值列表, 大小: " << init_vec.size() << std::endl;
|
|
|
for (auto* val : init_vec) {
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(val)) {
|
|
|
init_consts.push_back(const_int);
|
|
|
} else if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(val)) {
|
|
|
init_consts.push_back(const_float);
|
|
|
} else {
|
|
|
// 非常量表达式,使用0
|
|
|
if (is_float) {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
try {
|
|
|
ir::Value* single_val = std::any_cast<ir::Value*>(result);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 获取到单个初始化值" << std::endl;
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(single_val)) {
|
|
|
init_consts.push_back(const_int);
|
|
|
} else if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(single_val)) {
|
|
|
init_consts.push_back(const_float);
|
|
|
} else {
|
|
|
if (is_float) {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
std::cerr << "[WARNING] HandleGlobalVariable: 无法解析数组初始化值" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 如果初始化值不足,补0
|
|
|
while (init_consts.size() < static_cast<size_t>(total_size)) {
|
|
|
if (is_float) {
|
|
|
init_consts.push_back(builder_.CreateConstFloat(0.0f));
|
|
|
} else {
|
|
|
init_consts.push_back(builder_.CreateConstInt(0));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置全局数组的初始化器
|
|
|
if (!init_consts.empty()) {
|
|
|
global_array->SetInitializer(init_consts);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 设置全局数组初始化器" << std::endl;
|
|
|
}
|
|
|
|
|
|
// 存储全局变量引用
|
|
|
storage_map_[ctx] = global_array;
|
|
|
global_map_[varName] = global_array;
|
|
|
|
|
|
} else {
|
|
|
// 全局标量变量
|
|
|
std::shared_ptr<ir::Type> var_type;
|
|
|
if (is_float) {
|
|
|
var_type = ir::Type::GetFloatType();
|
|
|
} else {
|
|
|
var_type = ir::Type::GetInt32Type();
|
|
|
}
|
|
|
|
|
|
ir::GlobalValue* global_var = module_.CreateGlobal(varName, var_type);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 创建全局标量变量: " << varName << std::endl;
|
|
|
|
|
|
// 处理初始化值
|
|
|
ir::ConstantValue* init_value = nullptr;
|
|
|
if (auto* initVal = ctx->initVal()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleGlobalVariable: 处理标量初始化值" << std::endl;
|
|
|
auto result = initVal->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
ir::Value* val = std::any_cast<ir::Value*>(result);
|
|
|
if (is_float) {
|
|
|
if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(val)) {
|
|
|
init_value = const_float;
|
|
|
} else if (auto* const_int = dynamic_cast<ir::ConstantInt*>(val)) {
|
|
|
// 整数转浮点
|
|
|
float float_val = static_cast<float>(const_int->GetValue());
|
|
|
init_value = builder_.CreateConstFloat(float_val);
|
|
|
} else {
|
|
|
init_value = builder_.CreateConstFloat(0.0f);
|
|
|
}
|
|
|
} else {
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(val)) {
|
|
|
init_value = const_int;
|
|
|
} else if (auto* const_float = dynamic_cast<ir::ConstantFloat*>(val)) {
|
|
|
// 浮点转整数
|
|
|
int int_val = static_cast<int>(const_float->GetValue());
|
|
|
init_value = builder_.CreateConstInt(int_val);
|
|
|
} else {
|
|
|
init_value = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
if (is_float) {
|
|
|
init_value = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init_value = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 如果没有初始化值,默认初始化
|
|
|
if (!init_value) {
|
|
|
if (is_float) {
|
|
|
init_value = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init_value = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置全局变量的初始化器
|
|
|
global_var->SetInitializer(init_value);
|
|
|
|
|
|
// 存储全局变量引用
|
|
|
storage_map_[ctx] = global_var;
|
|
|
global_map_[varName] = global_var;
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
// 修改 HandleLocalVariable 函数中的数组处理部分
|
|
|
|
|
|
std::any IRGenImpl::HandleLocalVariable(SysYParser::VarDefContext* ctx,
|
|
|
const std::string& varName,
|
|
|
bool is_array) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleLocalVariable: 开始处理局部变量 " << varName << std::endl;
|
|
|
|
|
|
// 获取变量类型
|
|
|
bool is_float = false;
|
|
|
auto* varDecl = dynamic_cast<SysYParser::VarDeclContext*>(ctx->parent);
|
|
|
if (varDecl && varDecl->bType()) {
|
|
|
if (varDecl->bType()->Float()) {
|
|
|
is_float = true;
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] HandleLocalVariable: 变量 " << varName << " 是 float 类型" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (is_array) {
|
|
|
// 局部数组变量
|
|
|
int total_size = 1;
|
|
|
std::vector<int> dimensions;
|
|
|
|
|
|
// 获取数组维度
|
|
|
for (auto* const_exp : ctx->constExp()) {
|
|
|
try {
|
|
|
int dim_size = TryEvaluateConstInt(const_exp);
|
|
|
if (dim_size <= 0) dim_size = 1;
|
|
|
dimensions.push_back(dim_size);
|
|
|
total_size *= dim_size;
|
|
|
} catch (const std::exception& e) {
|
|
|
std::cerr << "[WARNING] HandleLocalVariable: 无法获取数组维度,使用维度1" << std::endl;
|
|
|
dimensions.push_back(1);
|
|
|
total_size *= 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 创建数组类型
|
|
|
std::shared_ptr<ir::Type> elem_type;
|
|
|
if (is_float) {
|
|
|
elem_type = ir::Type::GetFloatType();
|
|
|
} else {
|
|
|
elem_type = ir::Type::GetInt32Type();
|
|
|
}
|
|
|
|
|
|
// 修正:使用完整的维度列表创建数组类型
|
|
|
auto array_type = ir::Type::GetArrayType(elem_type, dimensions);
|
|
|
|
|
|
// 分配数组内存 - 为每个元素创建独立的 alloca
|
|
|
std::vector<ir::Value*> element_slots;
|
|
|
for (int i = 0; i < total_size; i++) {
|
|
|
ir::AllocaInst* slot;
|
|
|
if (is_float) {
|
|
|
slot = builder_.CreateAllocaFloat(
|
|
|
module_.GetContext().NextTemp() + "_" + varName + "_" + std::to_string(i));
|
|
|
} else {
|
|
|
slot = builder_.CreateAllocaI32(
|
|
|
module_.GetContext().NextTemp() + "_" + varName + "_" + std::to_string(i));
|
|
|
}
|
|
|
element_slots.push_back(slot);
|
|
|
}
|
|
|
|
|
|
// 存储第一个元素的地址作为数组的基地址
|
|
|
storage_map_[ctx] = element_slots[0];
|
|
|
local_var_map_[varName] = element_slots[0];
|
|
|
|
|
|
// 处理初始化
|
|
|
if (auto* initVal = ctx->initVal()) {
|
|
|
auto result = initVal->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
std::vector<ir::Value*> init_values =
|
|
|
std::any_cast<std::vector<ir::Value*>>(result);
|
|
|
|
|
|
// 初始化数组元素
|
|
|
for (size_t i = 0; i < init_values.size() && i < static_cast<size_t>(total_size); i++) {
|
|
|
builder_.CreateStore(init_values[i], element_slots[i]);
|
|
|
}
|
|
|
|
|
|
// 剩余元素初始化为0
|
|
|
for (size_t i = init_values.size(); i < static_cast<size_t>(total_size); i++) {
|
|
|
if (is_float) {
|
|
|
builder_.CreateStore(builder_.CreateConstFloat(0.0f), element_slots[i]);
|
|
|
} else {
|
|
|
builder_.CreateStore(builder_.CreateConstInt(0), element_slots[i]);
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
try {
|
|
|
ir::Value* single_value = std::any_cast<ir::Value*>(result);
|
|
|
// 只初始化第一个元素
|
|
|
builder_.CreateStore(single_value, element_slots[0]);
|
|
|
|
|
|
// 其他元素初始化为0
|
|
|
for (int i = 1; i < total_size; i++) {
|
|
|
if (is_float) {
|
|
|
builder_.CreateStore(builder_.CreateConstFloat(0.0f), element_slots[i]);
|
|
|
} else {
|
|
|
builder_.CreateStore(builder_.CreateConstInt(0), element_slots[i]);
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
// 全部初始化为0
|
|
|
for (int i = 0; i < total_size; i++) {
|
|
|
if (is_float) {
|
|
|
builder_.CreateStore(builder_.CreateConstFloat(0.0f), element_slots[i]);
|
|
|
} else {
|
|
|
builder_.CreateStore(builder_.CreateConstInt(0), element_slots[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
// 没有初始化值,全部初始化为0
|
|
|
for (int i = 0; i < total_size; i++) {
|
|
|
if (is_float) {
|
|
|
builder_.CreateStore(builder_.CreateConstFloat(0.0f), element_slots[i]);
|
|
|
} else {
|
|
|
builder_.CreateStore(builder_.CreateConstInt(0), element_slots[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
// 无初始化,所有元素初始化为0
|
|
|
for (int i = 0; i < total_size; i++) {
|
|
|
if (is_float) {
|
|
|
builder_.CreateStore(builder_.CreateConstFloat(0.0f), element_slots[i]);
|
|
|
} else {
|
|
|
builder_.CreateStore(builder_.CreateConstInt(0), element_slots[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
// 局部标量变量
|
|
|
ir::AllocaInst* slot;
|
|
|
if (is_float) {
|
|
|
slot = builder_.CreateAllocaFloat(module_.GetContext().NextTemp() + "_" + varName);
|
|
|
} else {
|
|
|
slot = builder_.CreateAllocaI32(module_.GetContext().NextTemp() + "_" + varName);
|
|
|
}
|
|
|
|
|
|
storage_map_[ctx] = slot;
|
|
|
local_var_map_[varName] = slot;
|
|
|
|
|
|
// 处理初始化
|
|
|
ir::Value* init = nullptr;
|
|
|
if (auto* initVal = ctx->initVal()) {
|
|
|
auto result = initVal->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
init = std::any_cast<ir::Value*>(result);
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
try {
|
|
|
std::vector<ir::Value*> init_values =
|
|
|
std::any_cast<std::vector<ir::Value*>>(result);
|
|
|
if (!init_values.empty()) {
|
|
|
init = init_values[0];
|
|
|
} else {
|
|
|
if (is_float) {
|
|
|
init = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
if (is_float) {
|
|
|
init = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
if (is_float) {
|
|
|
init = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
if (is_float) {
|
|
|
init = builder_.CreateConstFloat(0.0f);
|
|
|
} else {
|
|
|
init = builder_.CreateConstInt(0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
builder_.CreateStore(init, slot);
|
|
|
}
|
|
|
|
|
|
return {};
|
|
|
}
|
|
|
|
|
|
|
|
|
std::any IRGenImpl::visitInitVal(SysYParser::InitValContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitInitVal: 开始处理初始化值" << std::endl;
|
|
|
if (!ctx) {
|
|
|
throw std::runtime_error(FormatError("irgen", "非法初始化值"));
|
|
|
}
|
|
|
|
|
|
// 如果是单个表达式
|
|
|
if (ctx->exp()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitInitVal: 处理表达式初始化" << std::endl;
|
|
|
return EvalExpr(*ctx->exp());
|
|
|
}
|
|
|
// 如果是聚合初始化(花括号列表)
|
|
|
else if (!ctx->initVal().empty()) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitInitVal: 处理聚合初始化" << std::endl;
|
|
|
// 处理嵌套聚合初始化
|
|
|
return ProcessNestedInitVals(ctx);
|
|
|
}
|
|
|
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] visitInitVal: 空初始化列表" << std::endl;
|
|
|
// 空初始化列表
|
|
|
return std::vector<ir::Value*>{};
|
|
|
}
|
|
|
|
|
|
// 新增:处理嵌套聚合初始化的辅助函数
|
|
|
std::vector<ir::Value*> IRGenImpl::ProcessNestedInitVals(SysYParser::InitValContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] ProcessNestedInitVals: 开始处理嵌套初始化值" << std::endl;
|
|
|
std::vector<ir::Value*> all_values;
|
|
|
|
|
|
for (auto* init_val : ctx->initVal()) {
|
|
|
auto result = init_val->accept(this);
|
|
|
if (result.has_value()) {
|
|
|
try {
|
|
|
// 尝试获取单个值
|
|
|
ir::Value* value = std::any_cast<ir::Value*>(result);
|
|
|
all_values.push_back(value);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] ProcessNestedInitVals: 获取到单个值" << std::endl;
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
try {
|
|
|
// 尝试获取值列表(嵌套情况)
|
|
|
std::vector<ir::Value*> nested_values =
|
|
|
std::any_cast<std::vector<ir::Value*>>(result);
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] ProcessNestedInitVals: 获取到嵌套值列表, 大小: " << nested_values.size() << std::endl;
|
|
|
// 展平嵌套的值
|
|
|
all_values.insert(all_values.end(),
|
|
|
nested_values.begin(), nested_values.end());
|
|
|
} catch (const std::bad_any_cast&) {
|
|
|
// 未知类型
|
|
|
std::cerr << "[ERROR] ProcessNestedInitVals: 不支持的初始化值类型" << std::endl;
|
|
|
throw std::runtime_error(
|
|
|
FormatError("irgen", "不支持的初始化值类型"));
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] ProcessNestedInitVals: 无初始化值结果" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] ProcessNestedInitVals: 共获取 " << all_values.size() << " 个初始化值" << std::endl;
|
|
|
return all_values;
|
|
|
}
|
|
|
|
|
|
int IRGenImpl::TryEvaluateConstInt(SysYParser::ConstExpContext* ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] TryEvaluateConstInt: 开始求值常量表达式" << std::endl;
|
|
|
if (!ctx) {
|
|
|
if (IRGen_DEBUG) std::cerr << "[DEBUG] TryEvaluateConstInt: ctx is null" << std::endl;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 直接访问常量表达式树,计算数值
|
|
|
// 这里需要实现真正的常量求值逻辑
|
|
|
// 简化版本:假设常量表达式是整数常量
|
|
|
|
|
|
if (ctx->addExp()) {
|
|
|
// 尝试从 addExp 求值
|
|
|
return EvaluateConstAddExp(ctx->addExp());
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 添加辅助函数来求值常量表达式
|
|
|
int IRGenImpl::EvaluateConstAddExp(SysYParser::AddExpContext* ctx) {
|
|
|
if (!ctx) return 0;
|
|
|
|
|
|
// 如果没有左操作数,直接求值右操作数
|
|
|
if (!ctx->addExp()) {
|
|
|
return EvaluateConstMulExp(ctx->mulExp());
|
|
|
}
|
|
|
|
|
|
int left = EvaluateConstAddExp(ctx->addExp());
|
|
|
int right = EvaluateConstMulExp(ctx->mulExp());
|
|
|
|
|
|
if (ctx->AddOp()) {
|
|
|
return left + right;
|
|
|
} else if (ctx->SubOp()) {
|
|
|
return left - right;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int IRGenImpl::EvaluateConstMulExp(SysYParser::MulExpContext* ctx) {
|
|
|
if (!ctx) return 0;
|
|
|
|
|
|
// 如果没有左操作数,直接求值右操作数
|
|
|
if (!ctx->mulExp()) {
|
|
|
return EvaluateConstUnaryExp(ctx->unaryExp());
|
|
|
}
|
|
|
|
|
|
int left = EvaluateConstMulExp(ctx->mulExp());
|
|
|
int right = EvaluateConstUnaryExp(ctx->unaryExp());
|
|
|
|
|
|
if (ctx->MulOp()) {
|
|
|
return left * right;
|
|
|
} else if (ctx->DivOp()) {
|
|
|
return left / right;
|
|
|
} else if (ctx->QuoOp()) {
|
|
|
return left % right;
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int IRGenImpl::EvaluateConstUnaryExp(SysYParser::UnaryExpContext* ctx) {
|
|
|
if (!ctx) return 0;
|
|
|
|
|
|
// 基本表达式(数字字面量)
|
|
|
if (ctx->primaryExp()) {
|
|
|
return EvaluateConstPrimaryExp(ctx->primaryExp());
|
|
|
}
|
|
|
|
|
|
// 一元运算
|
|
|
if (ctx->unaryOp() && ctx->unaryExp()) {
|
|
|
int operand = EvaluateConstUnaryExp(ctx->unaryExp());
|
|
|
std::string op = ctx->unaryOp()->getText();
|
|
|
|
|
|
if (op == "+") {
|
|
|
return operand;
|
|
|
} else if (op == "-") {
|
|
|
return -operand;
|
|
|
} else if (op == "!") {
|
|
|
return !operand;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int IRGenImpl::EvaluateConstPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
|
|
|
if (!ctx) return 0;
|
|
|
|
|
|
// 处理数字字面量
|
|
|
if (ctx->DECIMAL_INT()) {
|
|
|
return std::stoi(ctx->DECIMAL_INT()->getText());
|
|
|
}
|
|
|
|
|
|
if (ctx->HEX_INT()) {
|
|
|
std::string hex = ctx->HEX_INT()->getText();
|
|
|
return std::stoi(hex, nullptr, 16);
|
|
|
}
|
|
|
|
|
|
if (ctx->OCTAL_INT()) {
|
|
|
std::string oct = ctx->OCTAL_INT()->getText();
|
|
|
return std::stoi(oct, nullptr, 8);
|
|
|
}
|
|
|
|
|
|
if (ctx->ZERO()) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 处理括号表达式
|
|
|
if (ctx->L_PAREN() && ctx->exp()) {
|
|
|
return EvaluateConstExp(ctx->exp());
|
|
|
}
|
|
|
|
|
|
// 常量标识符(引用其他常量)
|
|
|
if (ctx->lVal()) {
|
|
|
std::string const_name = ctx->lVal()->Ident()->getText();
|
|
|
auto it = const_value_map_.find(const_name);
|
|
|
if (it != const_value_map_.end()) {
|
|
|
if (auto* const_int = dynamic_cast<ir::ConstantInt*>(it->second)) {
|
|
|
return const_int->GetValue();
|
|
|
}
|
|
|
}
|
|
|
// 也可以从语义分析获取常量值
|
|
|
// ...
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int IRGenImpl::EvaluateConstExp(SysYParser::ExpContext* ctx) {
|
|
|
if (!ctx || !ctx->addExp()) return 0;
|
|
|
return EvaluateConstAddExp(ctx->addExp());
|
|
|
} |