aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2022-06-12 17:17:12 +0100
committerGitHub <noreply@github.com>2022-06-12 17:17:12 +0100
commita8bd3402674963f903d415f2fd6fcd52fe12139d (patch)
treefc5158fbf90110eb4ebad095de06a033599f3eb2 /src
parentca1f419dc28c2fb611b03afbe9661b3335554a40 (diff)
parent865d88dd56a8b048e861787e048f364a4579da2b (diff)
Merge pull request #1395 from hdooley/master
factor out alloca generation into a helper
Diffstat (limited to 'src')
-rw-r--r--src/llvm_backend.hpp2
-rw-r--r--src/llvm_backend_const.cpp8
-rw-r--r--src/llvm_backend_general.cpp22
3 files changed, 19 insertions, 13 deletions
diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp
index a460b1a23..1507e287d 100644
--- a/src/llvm_backend.hpp
+++ b/src/llvm_backend.hpp
@@ -461,6 +461,8 @@ void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module,
lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t);
bool lb_is_expr_untyped_const(Ast *expr);
+LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const* name = "");
+
void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment);
void lb_emit_init_context(lbProcedure *p, lbAddr addr);
diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp
index bd76400de..00452eb50 100644
--- a/src/llvm_backend_const.cpp
+++ b/src/llvm_backend_const.cpp
@@ -410,12 +410,10 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
// NOTE(bill, 2020-06-08): This is a bit of a hack but a "constant" slice needs
// its backing data on the stack
lbProcedure *p = m->curr_procedure;
- LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
-
LLVMTypeRef llvm_type = lb_type(m, t);
- array_data = LLVMBuildAlloca(p->builder, llvm_type, "");
- LLVMSetAlignment(array_data, 16); // TODO(bill): Make this configurable
- LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
+
+ array_data = llvm_alloca(p, llvm_type, 16);
+
LLVMBuildStore(p->builder, backing_array.value, array_data);
{
diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp
index 0866e3687..21ca3f23e 100644
--- a/src/llvm_backend_general.cpp
+++ b/src/llvm_backend_general.cpp
@@ -221,6 +221,17 @@ LLVMValueRef llvm_one(lbModule *m) {
return LLVMConstInt(lb_type(m, t_i32), 1, false);
}
+LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const* name) {
+ LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
+
+ LLVMValueRef val = LLVMBuildAlloca(p->builder, llvm_type, name);
+ LLVMSetAlignment(val, cast(unsigned int)alignment);
+
+ LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
+
+ return val;
+}
+
lbValue lb_zero(lbModule *m, Type *t) {
lbValue v = {};
v.value = LLVMConstInt(lb_type(m, t), 0, false);
@@ -2300,13 +2311,11 @@ general_end:;
return loaded_val;
} else {
GB_ASSERT(p->decl_block != p->curr_block);
- LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
- LLVMValueRef ptr = LLVMBuildAlloca(p->builder, dst_type, "");
- LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
i64 max_align = gb_max(lb_alignof(src_type), lb_alignof(dst_type));
max_align = gb_max(max_align, 4);
- LLVMSetAlignment(ptr, cast(unsigned)max_align);
+
+ LLVMValueRef ptr = llvm_alloca(p, dst_type, max_align);
LLVMValueRef nptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(src_type, 0), "");
LLVMBuildStore(p->builder, val, nptr);
@@ -2689,16 +2698,13 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p
}
LLVMTypeRef llvm_type = lb_type(p->module, type);
- LLVMValueRef ptr = LLVMBuildAlloca(p->builder, llvm_type, name);
unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(llvm_type));
if (is_type_matrix(type)) {
alignment *= 2; // NOTE(bill): Just in case
}
- LLVMSetAlignment(ptr, alignment);
-
- LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
+ LLVMValueRef ptr = llvm_alloca(p, llvm_type, alignment, name);
if (!zero_init && !force_no_init) {
// If there is any padding of any kind, just zero init regardless of zero_init parameter