Skip to content

Commit

Permalink
Rename SysCallFn to HostFn
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Sep 3, 2024
1 parent 947b3a9 commit 236c0ff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
1 change: 0 additions & 1 deletion ncc/examples/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ void textinput(u64 window_id, char ch)

void keydown(u64 window_id, u16 keycode)
{

if(vm_status != VM_STATUS_DONE)
{
if(keycode == KEY_ESCAPE) vm_status = VM_STATUS_HALT;
Expand Down
52 changes: 26 additions & 26 deletions vm/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::constants::*;
/// Note: the in/out arg count should be fixed so
/// that we can JIT syscalls efficiently
#[derive(Copy, Clone)]
pub enum SysCallFn
pub enum HostFn
{
Fn0_0(fn(&mut Thread)),
Fn0_1(fn(&mut Thread) -> Value),
Expand All @@ -33,7 +33,7 @@ pub enum SysCallFn
Fn4_1(fn(&mut Thread, a0: Value, a1: Value, a2: Value, a3: Value) -> Value),
}

impl SysCallFn
impl HostFn
{
fn argc(&self) -> usize
{
Expand Down Expand Up @@ -87,38 +87,38 @@ pub fn get_sdl_context() -> &'static mut sdl2::Sdl
}

/// Get the syscall with a given index
pub fn get_syscall(const_idx: u16) -> SysCallFn
pub fn get_syscall(const_idx: u16) -> HostFn
{
match const_idx {
// Core VM syscalls
VM_HEAP_SIZE => SysCallFn::Fn0_1(vm_heap_size),
VM_GROW_HEAP => SysCallFn::Fn1_1(vm_grow_heap),
MEMSET => SysCallFn::Fn3_0(memset),
MEMSET32 => SysCallFn::Fn3_0(memset32),
MEMCPY => SysCallFn::Fn3_0(memcpy),
MEMCMP => SysCallFn::Fn3_1(memcmp),

THREAD_SPAWN => SysCallFn::Fn1_1(thread_spawn),
THREAD_JOIN => SysCallFn::Fn1_1(thread_join),
THREAD_ID => SysCallFn::Fn0_1(thread_id),
THREAD_SLEEP => SysCallFn::Fn1_0(thread_sleep),
VM_HEAP_SIZE => HostFn::Fn0_1(vm_heap_size),
VM_GROW_HEAP => HostFn::Fn1_1(vm_grow_heap),
MEMSET => HostFn::Fn3_0(memset),
MEMSET32 => HostFn::Fn3_0(memset32),
MEMCPY => HostFn::Fn3_0(memcpy),
MEMCMP => HostFn::Fn3_1(memcmp),

THREAD_SPAWN => HostFn::Fn1_1(thread_spawn),
THREAD_JOIN => HostFn::Fn1_1(thread_join),
THREAD_ID => HostFn::Fn0_1(thread_id),
THREAD_SLEEP => HostFn::Fn1_0(thread_sleep),

// Console I/O
PRINT_I64 => SysCallFn::Fn1_0(print_i64),
PRINT_F32 => SysCallFn::Fn1_0(print_f32),
PRINT_STR => SysCallFn::Fn1_0(print_str),
PRINT_ENDL => SysCallFn::Fn0_0(print_endl),
PUTCHAR => SysCallFn::Fn1_1(putchar),
GETCHAR => SysCallFn::Fn0_1(getchar),
PRINT_I64 => HostFn::Fn1_0(print_i64),
PRINT_F32 => HostFn::Fn1_0(print_f32),
PRINT_STR => HostFn::Fn1_0(print_str),
PRINT_ENDL => HostFn::Fn0_0(print_endl),
PUTCHAR => HostFn::Fn1_1(putchar),
GETCHAR => HostFn::Fn0_1(getchar),

TIME_CURRENT_MS => SysCallFn::Fn0_1(time_current_ms),
TIME_CURRENT_MS => HostFn::Fn0_1(time_current_ms),

WINDOW_CREATE => SysCallFn::Fn4_1(window_create),
WINDOW_DRAW_FRAME => SysCallFn::Fn2_0(window_draw_frame),
WINDOW_POLL_EVENT => SysCallFn::Fn1_1(window_poll_event),
WINDOW_WAIT_EVENT => SysCallFn::Fn1_0(window_wait_event),
WINDOW_CREATE => HostFn::Fn4_1(window_create),
WINDOW_DRAW_FRAME => HostFn::Fn2_0(window_draw_frame),
WINDOW_POLL_EVENT => HostFn::Fn1_1(window_poll_event),
WINDOW_WAIT_EVENT => HostFn::Fn1_0(window_wait_event),

AUDIO_OPEN_OUTPUT => SysCallFn::Fn4_1(audio_open_output),
AUDIO_OPEN_OUTPUT => HostFn::Fn4_1(audio_open_output),

_ => panic!("unknown syscall \"{}\"", const_idx),
}
Expand Down
20 changes: 10 additions & 10 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,63 +1505,63 @@ impl Thread

match syscall_fn
{
SysCallFn::Fn0_0(fun) => {
HostFn::Fn0_0(fun) => {
fun(self)
}

SysCallFn::Fn0_1(fun) => {
HostFn::Fn0_1(fun) => {
let v = fun(self);
self.push(v);
}

SysCallFn::Fn1_0(fun) => {
HostFn::Fn1_0(fun) => {
let a0 = self.pop();
fun(self, a0)
}

SysCallFn::Fn1_1(fun) => {
HostFn::Fn1_1(fun) => {
let a0 = self.pop();
let v = fun(self, a0);
self.push(v);
}

SysCallFn::Fn2_0(fun) => {
HostFn::Fn2_0(fun) => {
let a1 = self.pop();
let a0 = self.pop();
fun(self, a0, a1)
}

SysCallFn::Fn2_1(fun) => {
HostFn::Fn2_1(fun) => {
let a1 = self.pop();
let a0 = self.pop();
let v = fun(self, a0, a1);
self.push(v);
}

SysCallFn::Fn3_0(fun) => {
HostFn::Fn3_0(fun) => {
let a2 = self.pop();
let a1 = self.pop();
let a0 = self.pop();
fun(self, a0, a1, a2)
}

SysCallFn::Fn3_1(fun) => {
HostFn::Fn3_1(fun) => {
let a2 = self.pop();
let a1 = self.pop();
let a0 = self.pop();
let v = fun(self, a0, a1, a2);
self.push(v);
}

SysCallFn::Fn4_0(fun) => {
HostFn::Fn4_0(fun) => {
let a3 = self.pop();
let a2 = self.pop();
let a1 = self.pop();
let a0 = self.pop();
fun(self, a0, a1, a2, a3)
}

SysCallFn::Fn4_1(fun) => {
HostFn::Fn4_1(fun) => {
let a3 = self.pop();
let a2 = self.pop();
let a1 = self.pop();
Expand Down

0 comments on commit 236c0ff

Please sign in to comment.