mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
refactor allocation
This commit is contained in:
parent
718cb28308
commit
b61f5d02b9
1 changed files with 37 additions and 16 deletions
|
@ -17,7 +17,9 @@ use bumpalo::Bump;
|
||||||
use inkwell::basic_block::BasicBlock;
|
use inkwell::basic_block::BasicBlock;
|
||||||
use inkwell::builder::Builder;
|
use inkwell::builder::Builder;
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
use inkwell::debug_info::{AsDIScope, DICompileUnit, DISubprogram, DebugInfoBuilder};
|
use inkwell::debug_info::{
|
||||||
|
AsDIScope, DICompileUnit, DIFlagsConstants, DISubprogram, DebugInfoBuilder,
|
||||||
|
};
|
||||||
use inkwell::memory_buffer::MemoryBuffer;
|
use inkwell::memory_buffer::MemoryBuffer;
|
||||||
use inkwell::module::{Linkage, Module};
|
use inkwell::module::{Linkage, Module};
|
||||||
use inkwell::passes::{PassManager, PassManagerBuilder};
|
use inkwell::passes::{PassManager, PassManagerBuilder};
|
||||||
|
@ -201,8 +203,6 @@ impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_subprogram(&self, function_name: &str) -> DISubprogram<'ctx> {
|
pub fn new_subprogram(&self, function_name: &str) -> DISubprogram<'ctx> {
|
||||||
use inkwell::debug_info::DIFlagsConstants;
|
|
||||||
|
|
||||||
let dibuilder = self.dibuilder;
|
let dibuilder = self.dibuilder;
|
||||||
let compile_unit = self.compile_unit;
|
let compile_unit = self.compile_unit;
|
||||||
|
|
||||||
|
@ -223,7 +223,7 @@ impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> {
|
||||||
);
|
);
|
||||||
|
|
||||||
dibuilder.create_function(
|
dibuilder.create_function(
|
||||||
/* scope */ compile_unit.as_debug_info_scope(),
|
/* scope */ compile_unit.get_file().as_debug_info_scope(),
|
||||||
/* func name */ function_name,
|
/* func name */ function_name,
|
||||||
/* linkage_name */ None,
|
/* linkage_name */ None,
|
||||||
/* file */ compile_unit.get_file(),
|
/* file */ compile_unit.get_file(),
|
||||||
|
@ -1040,21 +1040,45 @@ pub fn allocate_with_refcount<'a, 'ctx, 'env>(
|
||||||
let builder = env.builder;
|
let builder = env.builder;
|
||||||
let ctx = env.context;
|
let ctx = env.context;
|
||||||
|
|
||||||
let value_type = basic_type_from_layout(env.arena, ctx, layout, env.ptr_bytes);
|
let len_type = env.ptr_int();
|
||||||
let value_bytes = layout.stack_size(env.ptr_bytes);
|
|
||||||
|
|
||||||
|
let value_bytes = layout.stack_size(env.ptr_bytes);
|
||||||
|
let value_bytes_intvalue = len_type.const_int(value_bytes as u64, false);
|
||||||
|
|
||||||
|
let rc1 = crate::llvm::refcounting::refcount_1(ctx, env.ptr_bytes);
|
||||||
|
|
||||||
|
let data_ptr = allocate_with_refcount_help(env, layout, value_bytes_intvalue, rc1);
|
||||||
|
|
||||||
|
// store the value in the pointer
|
||||||
|
builder.build_store(data_ptr, value);
|
||||||
|
|
||||||
|
data_ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn allocate_with_refcount_help<'a, 'ctx, 'env>(
|
||||||
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
layout: &Layout<'a>,
|
||||||
|
number_of_data_bytes: IntValue<'ctx>,
|
||||||
|
initial_refcount: IntValue<'ctx>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let builder = env.builder;
|
||||||
|
let ctx = env.context;
|
||||||
|
|
||||||
|
let value_type = basic_type_from_layout(env.arena, ctx, layout, env.ptr_bytes);
|
||||||
let len_type = env.ptr_int();
|
let len_type = env.ptr_int();
|
||||||
|
|
||||||
let extra_bytes = layout.alignment_bytes(env.ptr_bytes);
|
let extra_bytes = layout.alignment_bytes(env.ptr_bytes);
|
||||||
|
|
||||||
let ptr = {
|
let ptr = {
|
||||||
let len = value_bytes as u64 + extra_bytes as u64;
|
// number of bytes we will allocated
|
||||||
|
let number_of_bytes = builder.build_int_add(
|
||||||
// bytes per element
|
len_type.const_int(extra_bytes as u64, false),
|
||||||
let bytes_len = len_type.const_int(len, false);
|
number_of_data_bytes,
|
||||||
|
"add_extra_bytes",
|
||||||
|
);
|
||||||
|
|
||||||
env.builder
|
env.builder
|
||||||
.build_array_malloc(ctx.i8_type(), bytes_len, "create_ptr")
|
.build_array_malloc(ctx.i8_type(), number_of_bytes, "create_ptr")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
||||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||||
|
@ -1105,11 +1129,8 @@ pub fn allocate_with_refcount<'a, 'ctx, 'env>(
|
||||||
n => unreachable!("invalid extra_bytes {}", n),
|
n => unreachable!("invalid extra_bytes {}", n),
|
||||||
};
|
};
|
||||||
|
|
||||||
let rc1 = crate::llvm::refcounting::refcount_1(ctx, env.ptr_bytes);
|
// let rc1 = crate::llvm::refcounting::refcount_1(ctx, env.ptr_bytes);
|
||||||
refcount_ptr.set_refcount(env, rc1);
|
refcount_ptr.set_refcount(env, initial_refcount);
|
||||||
|
|
||||||
// store the value in the pointer
|
|
||||||
builder.build_store(data_ptr, value);
|
|
||||||
|
|
||||||
data_ptr
|
data_ptr
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue