From 7a74685838ad74dfafa2455250a04a36b30ed728 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Thu, 8 Jun 2023 17:30:58 +0800 Subject: [PATCH] treat local const array as global --- include/visitor.h | 1 + src/visitor.cpp | 20 +++++--------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/include/visitor.h b/include/visitor.h index dbb19cb..cc6e652 100644 --- a/include/visitor.h +++ b/include/visitor.h @@ -101,6 +101,7 @@ private: std::shared_ptr current_bb = {}; std::vector *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; diff --git a/src/visitor.cpp b/src/visitor.cpp index 9cc060f..283cfa1 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -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