Skip to content

Commit

Permalink
Update comments and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 authored and Lai-YT committed Jul 7, 2024
1 parent 6e743d3 commit 5aef2d1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/llvm_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ 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.
// This vector stores the initialize values for a global array.
std::vector<llvm::Constant*> arr_elems{};
for (auto i = std::size_t{0}, e = arr_decl_type->len(),
init_len = arr_decl.init_list.size();
Expand All @@ -220,13 +220,12 @@ void LLVMIRGenerator::Visit(const ArrDeclNode& arr_decl) {
builder_.CreateStore(init_val, res_addr);
}
} else {
// set remaining elements as 0
auto zero = llvm::ConstantInt::get(builder_.getInt32Ty(), 0, true);
// A global array is always initialized to 0,
// but a local array remains uninitialized if no values are provided.
if (arr_decl.is_global) {
arr_elems.push_back(zero);
} 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.
} else if (!arr_decl.is_global && init_len != 0) {
auto res_addr = builder_.CreateConstInBoundsGEP2_32(
type, id_to_val.at(arr_decl.id), 0, i);
builder_.CreateStore(zero, res_addr);
Expand Down
8 changes: 3 additions & 5 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,8 @@ void QbeIrGenerator::Visit(const ArrDeclNode& arr_decl) {
global_var_init_vals.clear();
auto arr_size = arr_type->len();
auto init_len = arr_decl.init_list.size();
// The predicate of this loop guarantees that it doesn't go out of bound if
// the number of initialized elements is less than the array declaration
// size.
for (auto i = std::size_t{0}; i < arr_size && i < init_len; ++i) {
assert(init_len <= arr_size);
for (auto i = std::size_t{0}; i < init_len; ++i) {
auto& arr_init = arr_decl.init_list.at(i);
arr_init->Accept(*this);
Write_("{}", GenerateQBEInit(global_var_init_vals.at(i)));
Expand Down Expand Up @@ -263,7 +261,7 @@ void QbeIrGenerator::Visit(const ArrDeclNode& arr_decl) {
// 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) {
i < e && init_len != 0; ++i) {
if (i < init_len) {
auto& arr_init = arr_decl.init_list.at(i);
arr_init->Accept(*this);
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,6 +22,7 @@ int main() {
__builtin_print(e[1]);
__builtin_print(e[2]);

// NOTE: Local scope array should not be 0-initialized, and the generated IR has to be checked manually.
int f[2];
return 0;
}

0 comments on commit 5aef2d1

Please sign in to comment.