optimize local array init
This commit is contained in:
parent
3073d69806
commit
5c1278280a
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -9,7 +9,7 @@
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${workspaceFolder}/build/sysy",
|
||||
"args": ["../sysytests/functional_2022/21_if_test2.sy", "-S", "-o", "build/21_my.ll", "-O1", "-emit-llvm"],
|
||||
"args": ["../sysytests/functional_2022/34_arr_expr_len.sy", "-S", "-o", "build/21_my.ll", "-O1", "-emit-llvm"],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "3rdparty/easylogging++.h"
|
||||
#include "fmt/core.h"
|
||||
#include <algorithm>
|
||||
#include <any>
|
||||
#include <bitset>
|
||||
|
||||
@ -199,6 +199,7 @@ public:
|
||||
|
||||
class ConstantArr : public Constant {
|
||||
public:
|
||||
//! By default, this value_list is flattened and with size equal to declared size
|
||||
std::vector<ValuePtr_t> value_list;
|
||||
ConstantArr(const std::string &name, const std::vector<ValuePtr_t> &value_list, std::shared_ptr<ArrayType> type)
|
||||
: Constant(name, type), value_list(value_list) {}
|
||||
|
||||
@ -67,7 +67,6 @@ void _sysy_stoptime(int lineno);
|
||||
*/
|
||||
|
||||
Visitor::Visitor(SysyLexer &) {
|
||||
#pragma region RegisterLibFunc
|
||||
auto fparam_i32 = std::make_shared<FParam>("", TypeHelper::TYPE_I32);
|
||||
auto pointer_type = std::make_shared<PointerType>(TypeHelper::TYPE_I32);
|
||||
auto fparam_ptr_i32 = std::make_shared<FParam>("", pointer_type);
|
||||
@ -107,7 +106,9 @@ Visitor::Visitor(SysyLexer &) {
|
||||
func_stoptime->fparam_list = {fparam_i32};
|
||||
_func_tab.push_name("stoptime", func_stoptime);
|
||||
|
||||
#pragma endregion
|
||||
auto func_memset = std::make_shared<Function>("memset", TypeHelper::TYPE_VOID);
|
||||
func_memset->fparam_list = {fparam_ptr_i32, fparam_i32, fparam_i32};
|
||||
_func_tab.push_name("memset", func_memset);
|
||||
}
|
||||
|
||||
std::any Visitor::visitConstDecl(SysyParser::ConstDeclContext *ctx) {
|
||||
@ -235,11 +236,17 @@ std::any Visitor::visitVarDef(SysyParser::VarDefContext *ctx) {
|
||||
for (int i = 1; i < dim_list.size(); ++i) {
|
||||
base_ptr = build_InstGEP(base_ptr, {CONST0, CONST0}, _state.current_bb);
|
||||
}
|
||||
auto inst_call = build_InstCall(
|
||||
_func_tab.get_name("memset").value(),
|
||||
{base_ptr, CONST0, ConstantInt::make_shared(array_value.size() * 4)},
|
||||
_state.current_bb
|
||||
);
|
||||
// TODO: BAAU-2021 calls memset in `libc` (not sysylib) to optimize this process
|
||||
build_InstStore(array_value[0] ? array_value[0] : CONST0, base_ptr, _state.current_bb);
|
||||
if (array_value[0]) build_InstStore(array_value[0], base_ptr, _state.current_bb);
|
||||
for (int i = 1; i < array_value.size(); ++i) {
|
||||
if (!array_value[i]) continue;
|
||||
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);
|
||||
build_InstStore(array_value[i], ptr, _state.current_bb);
|
||||
}
|
||||
}
|
||||
// If there is no init expr, let it be
|
||||
@ -288,18 +295,10 @@ std::any Visitor::visitInitVal(SysyParser::InitValContext *ctx) {
|
||||
}
|
||||
for (auto init_val : ctx->initVal()) {
|
||||
if (init_val->exp()) {
|
||||
if (_state.isGlobalIint) {
|
||||
auto const_value = any_to_Value(visitInitVal(init_val));
|
||||
// should evaluate to const int
|
||||
sysy_assert(Value::is<ConstantInt>(const_value));
|
||||
cur_arr.push_back(const_value);
|
||||
}
|
||||
else {
|
||||
// God knows what it evaluates to
|
||||
auto exp_value = any_to_Value(visitInitVal(init_val));
|
||||
if (_state.isGlobalIint) assert(Value::is<ConstantInt>(exp_value));
|
||||
cur_arr.push_back(exp_value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// evaluate sub-array
|
||||
// before evaluate the new sub-array, first fill up the last one
|
||||
@ -614,7 +613,6 @@ std::any Visitor::visitUnaryExp(SysyParser::UnaryExpContext *ctx) {
|
||||
}
|
||||
else if (ctx->IDENT()) {
|
||||
// Fucntion call
|
||||
// TODO: buildCall/isRealParam
|
||||
// TODO: Handle string & putf()
|
||||
auto func_name = ctx->IDENT()->getText();
|
||||
VLOG(5) << "Calling Func: " << func_name;
|
||||
|
||||
@ -11,8 +11,8 @@
|
||||
|
||||
namespace CompSysY {
|
||||
|
||||
const std::array<std::string, 8> libfunc_list =
|
||||
{"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"};
|
||||
const std::array<std::string, 9> libfunc_list =
|
||||
{"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime", "memset"};
|
||||
|
||||
// On generation, the array is flattened, thus we have to calculate them
|
||||
std::shared_ptr<ConstantArr> gen_arr_hierarchy(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user