treat local const array as global

This commit is contained in:
ridethepig 2023-06-08 17:30:58 +08:00
parent 5c1278280a
commit 7a74685838
2 changed files with 6 additions and 15 deletions

View File

@ -101,6 +101,7 @@ private:
std::shared_ptr<BasicBlock> current_bb = {};
std::vector<int> *arr_dim_list = nullptr;
int arr_dim_index = 0;
int local_const_arr_cnt = 0;
struct loop_record {
BasicBlockPtr_t cond, body, next;
int id;

View File

@ -150,21 +150,11 @@ std::any Visitor::visitConstDef(SysyParser::ConstDefContext *ctx) {
if (_scope_tab.get_level()) {
// local const array
// Keep a pointer to the base address, alloca_ = &array, *alloca_ = &array[0]
auto alloca_ = build_InstAlloca(const_name, array_type, _state.current_bb);
_scope_tab.push_name(const_name, alloca_);
// first dim base, ptr = *alloca_ = &array[0]
auto base_ptr = build_InstGEP(alloca_, {CONST0, CONST0}, _state.current_bb);
// get the first element's base, final ptr = &a[0][0]...[0]
for (int i = 1; i < dim_list.size(); ++i) {
base_ptr = build_InstGEP(base_ptr, {CONST0, CONST0}, _state.current_bb);
}
// we store the elements in 1-dim shape
build_InstStore(array_value[0] ? array_value[0] : CONST0, base_ptr, _state.current_bb);
for (int i = 1; i < array_value.size(); ++i) {
auto ptr = build_InstGEP(base_ptr, {ConstantInt::make_shared(i)}, _state.current_bb);
build_InstStore(array_value[i] ? array_value[i] : CONST0, ptr, _state.current_bb);
}
auto const_arr = ConstantArr::make_shared("const_arr", array_value, array_type);
auto glob_name = fmt::format("__const.{}.{}{}", _state.current_func->name, const_name, _state.local_const_arr_cnt++);
auto global_var = GlobalVar::make_shared(glob_name, array_type, const_arr, true);
module.global_var_list.push_back(global_var);
_scope_tab.push_name(const_name, global_var);
}
else {
// global const array