From 3332aa47c1e7f518d559648ef45e78ab91f8dba1 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Fri, 5 Feb 2021 23:39:53 -0500 Subject: [PATCH] [rt] More misc fixes --- rt/src/int.rs | 2 +- rt/src/lam.rs | 20 +++++++++++++++++--- rt/src/lib.rs | 11 +++-------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/rt/src/int.rs b/rt/src/int.rs index f6148b4..afe2f00 100644 --- a/rt/src/int.rs +++ b/rt/src/int.rs @@ -25,5 +25,5 @@ pub unsafe extern "C" fn ivy_check_int(obj: Obj) { #[no_mangle] pub unsafe extern "C" fn ivy_make_int(value: i64) -> Obj { // @TODO: Handle 64 bit values rather than 63 bit integers - Obj { int: value << 1 } + Obj { int: value << 1 | 1 } } diff --git a/rt/src/lam.rs b/rt/src/lam.rs index 67f2f9e..0c1ad82 100644 --- a/rt/src/lam.rs +++ b/rt/src/lam.rs @@ -1,4 +1,4 @@ -use crate::{sys, trace, Obj, ObjTag}; +use crate::{sys, trace, Obj, ObjHeader, ObjTag}; use std::sync::atomic::AtomicU32; #[repr(C)] @@ -34,7 +34,7 @@ pub unsafe extern "C" fn ivy_make_lam( tag: ObjTag::Lam, _pad: [0; 1], upvars, - rc: AtomicU32::new(0), + rc: AtomicU32::new(1), func, params, filled: 0, @@ -46,6 +46,21 @@ pub unsafe extern "C" fn ivy_make_lam( Obj { box_lam } } +#[no_mangle] +pub unsafe extern "C" fn ivy_clone_lam(lam: &ObjLam) -> *mut ObjLam { + let size = lam.size(); + let data = sys::malloc(size); + core::ptr::copy(lam as *const _ as *const u8, data, size); + let box_hdr = data as *mut ObjHeader; + *(*box_hdr).rc.get_mut() = 1; + trace!( + "COPY {:016x} {:016x}", + lam as *const _ as usize, + box_hdr as usize + ); + data as *mut ObjLam +} + #[no_mangle] pub unsafe extern "C" fn ivy_app(fun: Obj, arg: Obj) -> Obj { ivy_app_mut(crate::ivy_clone(fun), arg) @@ -84,7 +99,6 @@ pub unsafe extern "C" fn ivy_app_mut(fun: Obj, arg: Obj) -> Obj { (lam.func)(lam) } else { trace!("UPD8 {:016x}", fun.int); - fun.incref(); fun } } diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 684171e..e76fcd7 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -87,14 +87,7 @@ pub unsafe extern "C" fn ivy_clone(obj: Obj) -> Obj { unimplemented!("copying boxed integers") } Some(ObjTag::Lam) => { - let lam = &*obj.box_lam; - let size = lam.size(); - let data = sys::malloc(size); - core::ptr::copy(obj.box_lam as *const u8, data, size); - let box_hdr = data as *mut ObjHeader; - *(*box_hdr).rc.get_mut() = 0; - trace!("COPY {:016x} {:016x}", obj.int, box_hdr as usize); - let box_lam = data as *mut ObjLam; + let box_lam = lam::ivy_clone_lam(&*obj.box_lam); Obj { box_lam } } } @@ -120,6 +113,7 @@ impl Obj { } unsafe fn incref(self) { + trace!("INC {:016x}", self.int); if !self.is_box() { return; } @@ -131,6 +125,7 @@ impl Obj { } unsafe fn decref(self) { + trace!("DEC {:016x}", self.int); if !self.is_box() { return; } -- 2.43.2