Skip to content

Commit

Permalink
Skip default values for local scope array that doesn't have initializ…
Browse files Browse the repository at this point in the history
…ed values
  • Loading branch information
leewei05 authored and Lai-YT committed Jul 7, 2024
1 parent a8b9e37 commit 6e743d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/llvm_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,15 @@ void LLVMIRGenerator::Visit(const ArrDeclNode& arr_decl) {
auto arr_decl_type = dynamic_cast<ArrType*>(arr_decl.type.get());
// This vector stores the initialize values for an array.
std::vector<llvm::Constant*> arr_elems{};
for (auto i = std::size_t{0}, e = arr_decl_type->len(); i < e; ++i) {
if (i < arr_decl.init_list.size()) {
for (auto i = std::size_t{0}, e = arr_decl_type->len(),
init_len = arr_decl.init_list.size();
i < e; ++i) {
if (i < init_len) {
auto& arr_init = arr_decl.init_list.at(i);
arr_init->Accept(*this);
}

if (i < arr_decl.init_list.size()) {
if (i < init_len) {
auto init_val = val_recorder.ValOfPrevExpr();
if (arr_decl.is_global) {
auto const_val = llvm::dyn_cast<llvm::Constant>(init_val);
Expand All @@ -222,7 +224,9 @@ void LLVMIRGenerator::Visit(const ArrDeclNode& arr_decl) {
auto zero = llvm::ConstantInt::get(builder_.getInt32Ty(), 0, true);
if (arr_decl.is_global) {
arr_elems.push_back(zero);
} else {
} else if (!arr_decl.is_global && 0 < init_len) {
// If array is in local scope and its initialized values is not
// declared, then compiler will not set element values to 0.
auto res_addr = builder_.CreateConstInBoundsGEP2_32(
type, id_to_val.at(arr_decl.id), 0, i);
builder_.CreateStore(zero, res_addr);
Expand Down
12 changes: 9 additions & 3 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,14 @@ void QbeIrGenerator::Visit(const ArrDeclNode& arr_decl) {
arr_decl.type->size());
id_to_num[arr_decl.id] = base_addr_num;

for (auto i = std::size_t{0}, e = arr_type->len(); i < e; ++i) {
if (i < arr_decl.init_list.size()) {
// NOTE: Compiler will not set elements to 0 if `init_len` is 0.
// 6.7.9 Initialization
// 10. If an object that has automatic storage duration is not initialized
// explicitly, its value is indeterminate.
for (auto i = std::size_t{0}, e = arr_type->len(),
init_len = arr_decl.init_list.size();
i < e && 0 < init_len; ++i) {
if (i < init_len) {
auto& arr_init = arr_decl.init_list.at(i);
arr_init->Accept(*this);
}
Expand All @@ -271,7 +277,7 @@ void QbeIrGenerator::Visit(const ArrDeclNode& arr_decl) {
WriteInstr_("{} =l add {}, {}", FuncScopeTemp{res_addr_num},
FuncScopeTemp{base_addr_num}, FuncScopeTemp{offset});

if (i < arr_decl.init_list.size()) {
if (i < init_len) {
int init_val_num = num_recorder.NumOfPrevExpr();
WriteInstr_("storew {}, {}", FuncScopeTemp{init_val_num},
FuncScopeTemp{res_addr_num});
Expand Down
1 change: 1 addition & 0 deletions test/codegen/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ int main() {
__builtin_print(e[1]);
__builtin_print(e[2]);

int f[2];
return 0;
}

0 comments on commit 6e743d3

Please sign in to comment.