Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variable code generation #170

Merged
merged 15 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct DeclNode // NOLINT(cppcoreguidelines-special-member-functions)

std::string id;
std::unique_ptr<Type> type;
bool is_global{false};
};

/// @note This is an abstract class.
Expand All @@ -86,6 +87,7 @@ struct ExprNode // NOLINT(cppcoreguidelines-special-member-functions)

std::unique_ptr<Type> type =
std::make_unique<PrimType>(PrimitiveType::kUnknown);
bool is_global{false};
};

/// @brief A designator node is used to explicitly reference a member for
Expand Down
79 changes: 61 additions & 18 deletions src/llvm_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,39 +163,82 @@ void LLVMIRGenerator::Visit(const DeclStmtNode& decl_stmt) {

void LLVMIRGenerator::Visit(const VarDeclNode& decl) {
auto var_type = builder_helper_.GetLLVMType(*(decl.type));
auto addr = builder_.CreateAlloca(var_type);
if (decl.init) {
decl.init->Accept(*this);
auto val = val_recorder.ValOfPrevExpr();
builder_.CreateStore(val, addr);
if (decl.is_global) {
auto global = module_.getOrInsertGlobal(decl.id, var_type);
auto global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
llvm::Constant* const_val = nullptr;
if (decl.init) {
decl.init->Accept(*this);
auto val = val_recorder.ValOfPrevExpr();
const_val = llvm::dyn_cast<llvm::Constant>(val);
assert(const_val);
} else {
const_val = llvm::ConstantInt::get(builder_.getInt32Ty(), 0, true);
}
global_var->setInitializer(const_val);
id_to_val[decl.id] = global_var;
} else {
auto addr = builder_.CreateAlloca(var_type);
if (decl.init) {
decl.init->Accept(*this);
auto val = val_recorder.ValOfPrevExpr();
builder_.CreateStore(val, addr);
}
id_to_val[decl.id] = addr;
}
id_to_val[decl.id] = addr;
}

void LLVMIRGenerator::Visit(const ArrDeclNode& arr_decl) {
auto arr_type = builder_helper_.GetLLVMType(*(arr_decl.type));
auto base_addr = builder_.CreateAlloca(arr_type);
id_to_val[arr_decl.id] = base_addr;
auto type = builder_helper_.GetLLVMType(*(arr_decl.type));
if (arr_decl.is_global) {
auto global_arr = module_.getOrInsertGlobal(arr_decl.id, type);
id_to_val[arr_decl.id] = global_arr;
} else {
auto addr = builder_.CreateAlloca(type);
id_to_val[arr_decl.id] = addr;
}

auto arr_decl_type = dynamic_cast<ArrType*>(arr_decl.type.get());
for (auto i = std::size_t{0}, e = arr_decl_type->len(); i < e; ++i) {
if (i < arr_decl.init_list.size()) {
// This vector stores the initialize values for a global array.
std::vector<llvm::Constant*> arr_elems{};
leewei05 marked this conversation as resolved.
Show resolved Hide resolved
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);
}

auto res_addr =
builder_.CreateConstInBoundsGEP2_32(arr_type, base_addr, 0, i);

if (i < arr_decl.init_list.size()) {
if (i < init_len) {
auto init_val = val_recorder.ValOfPrevExpr();
builder_.CreateStore(init_val, res_addr);
if (arr_decl.is_global) {
auto const_val = llvm::dyn_cast<llvm::Constant>(init_val);
arr_elems.push_back(const_val);
} else {
auto res_addr = builder_.CreateConstInBoundsGEP2_32(
type, id_to_val.at(arr_decl.id), 0, i);
builder_.CreateStore(init_val, res_addr);
}
} else {
// set remaining elements as 0
auto zero = llvm::ConstantInt::get(builder_.getInt32Ty(), 0, true);
builder_.CreateStore(zero, res_addr);
// A global array is always initialized to 0,
// but a local array remains uninitialized if no values are provided.
if (arr_decl.is_global) {
Lai-YT marked this conversation as resolved.
Show resolved Hide resolved
arr_elems.push_back(zero);
} 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);
}
leewei05 marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (arr_decl.is_global) {
auto arr_type = llvm::dyn_cast<llvm::ArrayType>(type);
auto arr_init = llvm::ConstantArray::get(arr_type, arr_elems);
auto global_arr = module_.getGlobalVariable(arr_decl.id);
global_arr->setInitializer(arr_init);
}
}

void LLVMIRGenerator::Visit(const RecordDeclNode& record_decl) {
Expand Down
Loading
Loading