This commit is contained in:
2026-05-20 15:48:03 -06:00
parent 541add786d
commit 4b2c026d75
4 changed files with 60 additions and 14 deletions

1
runtime/Cargo.lock generated
View File

@@ -41,6 +41,7 @@ name = "gyehoek"
version = "0.1.0"
dependencies = [
"bdwgc-alloc",
"libc",
]
[[package]]

View File

@@ -12,6 +12,7 @@ crate-type = ["staticlib"]
bdwgc-alloc = { version = "0.6.13"
, default-features = false
, features = ["cmake"] }
libc = "0.2.186"
[patch.crates-io]
bdwgc-alloc = { git = 'https://git.deertopia.net/msyds/bdwgc-rust.git' }

30
runtime/src/gc.rs Normal file
View File

@@ -0,0 +1,30 @@
use libc::{c_int, c_void, size_t};
#[link(name = "gc", kind = "static")]
unsafe extern "C" {
fn GC_allow_register_threads ();
fn GC_alloc_lock ();
fn GC_alloc_unlock ();
fn GC_free (ptr: *mut c_void);
// fn GC_get_stack_base (stack_base: *mut GcStackBase) -> c_int;
fn GC_init ();
fn GC_malloc (size: size_t) -> *mut c_void;
fn GC_realloc (ptr: *mut c_void, size: size_t) -> *mut c_void;
// fn GC_register_my_thread
// (stack_base: *const GcStackBase) -> c_int;
// fn GC_set_stackbottom
// (thread: *const c_void, stack_bottom: *const GcStackBase);
fn GC_unregister_my_thread ();
fn GC_gcollect ();
fn GC_register_finalizer (
ptr: *const c_void,
finalizer: extern "C" fn (*mut c_void, *mut c_void),
client_data: *const c_void,
opt_old_finalizer: *const c_void,
opt_old_client_data: *const c_void,
) -> *mut c_void;
}
pub unsafe fn malloc<T> (size: usize) -> *mut T {
unsafe { GC_malloc (size) as *mut T }
}

View File

@@ -1,21 +1,27 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
use std::{io::{stdout, Write}};
mod gc;
// use crate::gc;
// use bdwgc_alloc:Allocator;
const scm_tc2_int : u64 = 2;
const scm_tc3_cons : u64 = 0;
const scm_tc7_weak_set : u64 = 0x55;
const scm_tc7_symbol : u64 = 0x05;
const scm_tc7_string : u64 = 0x15;
pub const scm_tc2_int : u64 = 2;
pub const scm_tc3_cons : u64 = 0;
pub const scm_tc7_weak_set : u64 = 0x55;
pub const scm_tc7_symbol : u64 = 0x05;
pub const scm_tc7_string : u64 = 0x15;
const scm_false : SCM = scm_pack (0b00100);
const scm_true : SCM = scm_pack (0b01100);
const scm_eol : SCM = scm_pack (0b10100);
pub const scm_false : SCM = scm_pack (0b00100);
pub const scm_true : SCM = scm_pack (0b01100);
pub const scm_eol : SCM = scm_pack (0b10100);
type scm_bits = u64;
pub type scm_bits = u64;
#[repr(C)]
#[derive(Clone, Copy)]
struct SCM {
pub struct SCM {
n : scm_bits
}
@@ -37,6 +43,14 @@ fn scm_pack_pointer (x : *const scm_bits) -> SCM {
fn scm_words (ty: scm_bits, n: usize) -> SCM {
unsafe {
let r = gc::malloc (n * size_of::<scm_bits> ()) as *mut scm_bits;
*r = ty;
return scm_pack_pointer (r);
}
}
fn scm_cell_object (x: SCM, n: usize) -> SCM {
let p = scm_unpack_pointer (x) as *mut SCM;
unsafe {
@@ -90,13 +104,13 @@ pub extern "C" fn scm_write (x: SCM) -> SCM {
let ty = if is_immediate (x) { "immediate" } else { "heap object" };
print! ("#<{ty} {:#016x}>", scm_unpack (x));
}
stdout ().flush ();
let _ = stdout ().flush ();
return scm_pack (0);
}
#[unsafe(no_mangle)]
pub extern "C" fn scm_from_utf8 (s: &str) -> SCM {
println! ("scm_from_utf8_string");
pub extern "C" fn scm_from_utf8 (s: *const u8, len: usize) -> SCM {
let r = scm_words (scm_tc7_string, 3);
return scm_pack (0);
}