treat local const array as global
This commit is contained in:
parent
5c1278280a
commit
7a74685838
@ -101,6 +101,7 @@ private:
|
|||||||
std::shared_ptr<BasicBlock> current_bb = {};
|
std::shared_ptr<BasicBlock> current_bb = {};
|
||||||
std::vector<int> *arr_dim_list = nullptr;
|
std::vector<int> *arr_dim_list = nullptr;
|
||||||
int arr_dim_index = 0;
|
int arr_dim_index = 0;
|
||||||
|
int local_const_arr_cnt = 0;
|
||||||
struct loop_record {
|
struct loop_record {
|
||||||
BasicBlockPtr_t cond, body, next;
|
BasicBlockPtr_t cond, body, next;
|
||||||
int id;
|
int id;
|
||||||
|
|||||||
@ -150,21 +150,11 @@ std::any Visitor::visitConstDef(SysyParser::ConstDefContext *ctx) {
|
|||||||
|
|
||||||
if (_scope_tab.get_level()) {
|
if (_scope_tab.get_level()) {
|
||||||
// local const array
|
// local const array
|
||||||
// Keep a pointer to the base address, alloca_ = &array, *alloca_ = &array[0]
|
auto const_arr = ConstantArr::make_shared("const_arr", array_value, array_type);
|
||||||
auto alloca_ = build_InstAlloca(const_name, array_type, _state.current_bb);
|
auto glob_name = fmt::format("__const.{}.{}{}", _state.current_func->name, const_name, _state.local_const_arr_cnt++);
|
||||||
_scope_tab.push_name(const_name, alloca_);
|
auto global_var = GlobalVar::make_shared(glob_name, array_type, const_arr, true);
|
||||||
// first dim base, ptr = *alloca_ = &array[0]
|
module.global_var_list.push_back(global_var);
|
||||||
auto base_ptr = build_InstGEP(alloca_, {CONST0, CONST0}, _state.current_bb);
|
_scope_tab.push_name(const_name, global_var);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// global const array
|
// global const array
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user