Add List.append implementation for generic64

This commit is contained in:
Ahmad Sattar 2023-01-25 19:32:15 +01:00
parent 9b525143cb
commit 0adb38beca
No known key found for this signature in database
GPG key ID: 6EAC5AB400A49604
3 changed files with 96 additions and 5 deletions

View file

@ -1429,6 +1429,80 @@ impl<
self.free_symbol(&Symbol::DEV_TMP4);
}
fn build_list_append_unsafe(
&mut self,
dst: &Symbol,
args: &'a [Symbol],
arg_layouts: &[InLayout<'a>],
ret_layout: &InLayout<'a>,
) {
let list = args[0];
let list_layout = arg_layouts[0];
let elem = args[1];
let elem_layout = arg_layouts[1];
// Have to pass the input element by pointer, so put it on the stack and load it's address.
self.storage_manager
.ensure_symbol_on_stack(&mut self.buf, &elem);
let (new_elem_offset, _) = self.storage_manager.stack_offset_and_size(&elem);
// Load address of output element into register.
let reg = self
.storage_manager
.claim_general_reg(&mut self.buf, &Symbol::DEV_TMP);
ASM::add_reg64_reg64_imm32(&mut self.buf, reg, CC::BASE_PTR_REG, new_elem_offset);
// Load element_witdh argument (usize).
let u64_layout = Layout::U64;
let elem_stack_size = self.layout_interner.stack_size(elem_layout);
self.load_literal(
&Symbol::DEV_TMP2,
&u64_layout,
&Literal::Int((elem_stack_size as i128).to_ne_bytes()),
);
// Setup the return location.
let base_offset = self
.storage_manager
.claim_stack_area(dst, self.layout_interner.stack_size(*ret_layout));
let lowlevel_args = bumpalo::vec![
in self.env.arena;
list,
// element
Symbol::DEV_TMP,
// element_width
Symbol::DEV_TMP2
];
let lowlevel_arg_layouts = bumpalo::vec![
in self.env.arena;
list_layout,
u64_layout,
u64_layout,
];
self.build_fn_call(
&Symbol::DEV_TMP3,
bitcode::LIST_APPEND_UNSAFE.to_string(),
&lowlevel_args,
&lowlevel_arg_layouts,
&ret_layout,
);
self.free_symbol(&Symbol::DEV_TMP);
self.free_symbol(&Symbol::DEV_TMP2);
// Return list value from fn call
self.storage_manager.copy_symbol_to_stack_offset(
self.layout_interner,
&mut self.buf,
base_offset,
&Symbol::DEV_TMP3,
&ret_layout,
);
self.free_symbol(&Symbol::DEV_TMP3);
}
fn build_list_get_unsafe(
&mut self,
dst: &Symbol,

View file

@ -695,6 +695,14 @@ trait Backend<'a> {
);
self.build_list_reserve(sym, args, arg_layouts, ret_layout)
}
LowLevel::ListAppendUnsafe => {
debug_assert_eq!(
2,
args.len(),
"ListAppendUnsafe: expected to have exactly two arguments"
);
self.build_list_append_unsafe(sym, args, arg_layouts, ret_layout)
}
LowLevel::ListGetUnsafe => {
debug_assert_eq!(
2,
@ -776,7 +784,7 @@ trait Backend<'a> {
self.build_eq(sym, &args[0], &Symbol::DEV_TMP, &arg_layouts[0]);
self.free_symbol(&Symbol::DEV_TMP)
}
Symbol::LIST_GET | Symbol::LIST_SET | Symbol::LIST_REPLACE => {
Symbol::LIST_GET | Symbol::LIST_SET | Symbol::LIST_REPLACE | Symbol::LIST_APPEND => {
// TODO: This is probably simple enough to be worth inlining.
let layout_id = LayoutIds::default().get(func_sym, ret_layout);
let fn_name = self.symbol_to_string(func_sym, layout_id);
@ -952,6 +960,15 @@ trait Backend<'a> {
ret_layout: &InLayout<'a>,
);
/// build_list_append_unsafe returns a new list with a given element appended.
fn build_list_append_unsafe(
&mut self,
dst: &Symbol,
args: &'a [Symbol],
arg_layouts: &[InLayout<'a>],
ret_layout: &InLayout<'a>,
);
/// build_list_get_unsafe loads the element from the list at the index.
fn build_list_get_unsafe(
&mut self,