fix vec_insert_many

This commit is contained in:
janis 2025-11-01 00:28:59 +01:00
parent 1adeca36ec
commit dae2e00569
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
4 changed files with 51 additions and 5 deletions

View file

@ -566,18 +566,18 @@ vec_insert_many:
add rsi, rax ; src ptr
mov rdi, rsi
mov rax, [rsp + 18] ; count
mov rax, [rsp + 0x18] ; count
mul rcx ; count * item_size
add rdi, rcx ; dst ptr
add rdi, rax ; dst ptr
mov rdx, r12 ; number of bytes to move
call memmove
mov rdi, rsi
mov rsi, [rsp + 0x10]
mov rdx, [rsp + 0x18] ; count
mov rcx, [rsp + 0x18] ; count
mov rax, [rbx + 24] ; item_size
mul rdx ; count * item_size
mov rax, rdx
mul rcx ; count * item_size
mov rdx, rax
call memcpy
mov rax, [rbx + 8] ; len

View file

@ -19,7 +19,10 @@ unsafe extern "C" {
pub unsafe fn ast_resolve_var_refs(ast: *mut Ast, ctx: *mut SymbolTable, root_index: u64);
pub unsafe fn get_register_name(reg_idx: u8, width: u8, buffer: *mut u8) -> FFISlice;
pub unsafe fn stackvar_cmp(a: *const (u64, u64), b: *const (u64, u64)) -> i32;
pub unsafe fn codegen_allocate_register(ctx: *mut FunctionCtx) -> u8;
pub unsafe fn codegen_function(ast: *const CodegenCtx, func_idx: u64) -> ();
pub unsafe fn codegen_expr(ctx: *const CodegenCtx, function_ctx: &FunctionCtx, expr_idx: u64) -> (u64, bool);
pub unsafe fn vec_insert_many(vec: *mut BlobVec, index: usize, data: *const u8, count: usize);
pub unsafe fn vec_extend(vec: *mut BlobVec, elements: *const u8, count: usize) -> ();
}
@ -50,6 +53,13 @@ pub const TYPE_I32: u8 = 3;
pub const TYPE_U32: u8 = 4;
pub const TYPE_STR: u8 = 5;
pub const TYPE_POINTER: u8 = 6;
pub const OPERAND_REGISTER: u32 = 1;
pub const OPERAND_RBP_OFFSET: u32 = 2;
pub const OPERAND_RSP_OFFSET: u32 = 3;
pub const OPERAND_ADDRESS: u32 = 4;
pub const OPERAND_IMMEDIATE: u32 = 5;
pub const OPERAND_CONSTANT: u32 = 6;
pub const OPERAND_LABEL: u32 = 7;
pub const TOKEN_EOF: u8 = 0;
pub const TOKEN_LET: u8 = 1;
pub const TOKEN_IF: u8 = 2;
@ -201,6 +211,24 @@ pub struct CodegenCtx {
pub text: Vec<u8>,
}
#[repr(C)]
#[derive(Debug)]
pub struct FunctionCtx {
pub current_stack_size: u64,
pub stack_vars: Vec<(u64, u64)>,
pub register_bitset: u128,
pub dirtied_register_bitset: u128,
}
#[repr(C)]
#[derive(Debug)]
pub struct Operand {
pub kind: u8,
pub register_and_width: u8,
pub len: u16,
pub value: u64,
}
#[repr(C)]
#[derive(Debug)]
pub struct BlobVec {

View file

@ -160,6 +160,19 @@ pub mod vec {
}
}
pub fn insert_many(&mut self, index: usize, elements: Box<[T]>) {
unsafe {
let elements =
core::mem::transmute::<Box<[T]>, Box<[core::mem::ManuallyDrop<T>]>>(elements);
super::defs::vec_insert_many(
&mut self.vec,
index,
elements.as_ptr() as *const u8,
elements.len(),
);
}
}
pub fn pop(&mut self) -> Option<T> {
if self.vec.len == 0 {
return None;

View file

@ -113,4 +113,9 @@ fn main() {
let elements = Box::new([1, 2, 3, 4, 5]);
vec.extend(elements);
assert_eq!(vec.as_slice(), &[50, 1, 2, 3, 4, 5]);
// vec insert_many
let elements = Box::new([6, 7, 8]);
vec.insert_many(2, elements);
assert_eq!(vec.as_slice(), &[50, 1, 6, 7, 8, 2, 3, 4, 5]);
}