Skip to content

Commit

Permalink
Remove SysState
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Aug 26, 2024
1 parent c77d0dc commit ac129d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 107 deletions.
3 changes: 1 addition & 2 deletions vm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::process::exit;
use std::sync::{Arc, Mutex};
use crate::vm::{VM, Value, MemBlock};
use crate::asm::{Assembler};
use crate::sys::{SysState};
use crate::utils::{thousands_sep};

/// Command-line options
Expand Down Expand Up @@ -150,7 +149,7 @@ fn main()
}

let vm = result.unwrap();
let mut mutex = SysState::get_mutex(vm);
let mut mutex = Arc::new(Mutex::new(vm));
let ret_val = run_program(&mut mutex);

#[cfg(feature = "count_insns")]
Expand Down
4 changes: 4 additions & 0 deletions vm/src/sys/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ impl AudioCallback for AudioCB
/// the Send trait, and so can't be referenced from another thread
static mut DEVICE: Option<AudioDevice<AudioCB>> = None;



/*
// NOTE: this can only be called from the main thread since it uses SDL
// However, it creates a new thread to generate audio sample, this thread
// could be given a reference to another VM instance
Expand Down Expand Up @@ -104,3 +107,4 @@ pub fn audio_open_output(vm: &mut VM, sample_rate: Value, num_channels: Value, f
// TODO: return the device_id (u32)
Value::from(0)
}
*/
112 changes: 21 additions & 91 deletions vm/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,106 +92,36 @@ pub fn get_sdl_context() -> &'static mut sdl2::Sdl
}
}

pub struct SysState
/// Get the syscall with a given index
pub fn get_syscall(const_idx: u16) -> SysCallFn
{
/// Map of indices to syscall functions
syscalls: [Option<SysCallFn>; SYSCALL_TBL_LEN],

/// Weak reference to a mutex for the VM
mutex: Weak<Mutex<VM>>,



}

impl SysState
{
pub fn new() -> Self
{
let mut sys_state = Self {
syscalls: [None; SYSCALL_TBL_LEN],
mutex: Weak::new(),
//net_state: NetState::default(),
};

sys_state.init_syscalls();

sys_state
}

pub fn get_mutex(vm: VM) -> Arc<Mutex<VM>>
{
// Move the VM into a mutex
let vm_arc = Arc::new(Mutex::new(vm));

// Store a weak reference to the mutex into the sys state
vm_arc.lock().unwrap().sys_state.mutex = Arc::downgrade(&vm_arc);

vm_arc
}

/// Register a syscall implementation
pub fn reg_syscall(&mut self, const_idx: u16, fun: SysCallFn)
{
let desc = SYSCALL_DESCS[const_idx as usize].as_ref().unwrap();

assert!(
fun.argc() == desc.argc,
"{} should accept {} args but implementation has {} params",
desc.name,
desc.argc,
fun.argc()
);

assert!(fun.has_ret() == desc.has_ret);

self.syscalls[const_idx as usize] = Some(fun);
}

/// Get the syscall with a given index
pub fn get_syscall(&self, const_idx: u16) -> SysCallFn
{
if let Some(syscall_fn) = self.syscalls[const_idx as usize] {
return syscall_fn;
}
else
{
panic!("unknown syscall \"{}\"", const_idx);
}
}

fn init_syscalls(&mut self)
{
let mut syscalls = HashMap::<String, SysCallFn>::new();

match const_idx {
// Core VM syscalls
self.reg_syscall(VM_HEAP_SIZE, SysCallFn::Fn0_1(vm_heap_size));
self.reg_syscall(VM_RESIZE_HEAP, SysCallFn::Fn1_1(vm_resize_heap));
self.reg_syscall(MEMSET, SysCallFn::Fn3_0(memset));
self.reg_syscall(MEMSET32, SysCallFn::Fn3_0(memset32));
self.reg_syscall(MEMCPY, SysCallFn::Fn3_0(memcpy));
self.reg_syscall(MEMCMP, SysCallFn::Fn3_1(memcmp));

self.reg_syscall(PRINT_I64, SysCallFn::Fn1_0(print_i64));
self.reg_syscall(PRINT_F32, SysCallFn::Fn1_0(print_f32));
self.reg_syscall(PRINT_STR, SysCallFn::Fn1_0(print_str));
self.reg_syscall(PRINT_ENDL, SysCallFn::Fn0_0(print_endl));
self.reg_syscall(PUTCHAR, SysCallFn::Fn1_1(putchar));
self.reg_syscall(GETCHAR, SysCallFn::Fn0_1(getchar));

VM_HEAP_SIZE => SysCallFn::Fn0_1(vm_heap_size),
VM_RESIZE_HEAP => SysCallFn::Fn1_1(vm_resize_heap),
MEMSET => SysCallFn::Fn3_0(memset),
MEMSET32 => SysCallFn::Fn3_0(memset32),
MEMCPY => SysCallFn::Fn3_0(memcpy),
MEMCMP => SysCallFn::Fn3_1(memcmp),

// 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),

/*
self.reg_syscall(TIME_CURRENT_MS, SysCallFn::Fn0_1(time_current_ms));
//self.reg_syscall(TIME_DELAY_CB, SysCallFn::Fn2_0(time_delay_cb));
self.reg_syscall(WINDOW_CREATE, SysCallFn::Fn4_1(window_create));
self.reg_syscall(WINDOW_DRAW_FRAME, SysCallFn::Fn2_0(window_draw_frame));
self.reg_syscall(AUDIO_OPEN_OUTPUT, SysCallFn::Fn4_1(audio_open_output));
*/

//self.reg_syscall(NET_LISTEN, SysCallFn::Fn2_1(net_listen));
//self.reg_syscall(NET_ACCEPT, SysCallFn::Fn4_1(net_accept));
//self.reg_syscall(NET_READ, SysCallFn::Fn3_1(net_read));
//self.reg_syscall(NET_WRITE, SysCallFn::Fn3_1(net_write));
//self.reg_syscall(NET_CLOSE, SysCallFn::Fn1_0(net_close));
_ => panic!("unknown syscall \"{}\"", const_idx),
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/sys/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sdl2::pixels::PixelFormatEnum;

use std::time::Duration;

use crate::sys::{SysState, get_sdl_context};
use crate::sys::{get_sdl_context};
use crate::vm::{VM, Value};

/// SDL video subsystem
Expand Down
22 changes: 9 additions & 13 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ struct StackFrame
pub struct Thread
{
// Thread id
pub tid: u64,
pub id: u64,

// Parent VM
pub vm: Arc<Mutex<VM>>,
Expand All @@ -526,6 +526,13 @@ pub struct Thread
frames: Vec<StackFrame>,
}

impl Thread
{



}




Expand All @@ -534,9 +541,6 @@ pub struct Thread

pub struct VM
{
// Host system state
pub sys_state: SysState,

// Heap memory space
heap: MemBlock,

Expand All @@ -548,25 +552,17 @@ pub struct VM

// List of stack frames (activation records)
frames: Vec<StackFrame>,

// Count of executed instructions
//#[cfg(feature = "count_insns")]
//insn_count: u64,
}

impl VM
{
pub fn new(mut code: MemBlock, mut heap: MemBlock, syscalls: HashSet<u16>) -> Self
{
// Initialize the system state
let sys_state = SysState::new();

// Resize the code and heap space to a page size multiple
code.resize(code.len());
heap.resize(heap.len());

Self {
sys_state,
code,
heap,
stack: Vec::default(),
Expand Down Expand Up @@ -1474,7 +1470,7 @@ impl VM

Op::syscall => {
let syscall_idx = self.code.read_pc::<u16>(&mut pc);
let syscall_fn = self.sys_state.get_syscall(syscall_idx);
let syscall_fn = get_syscall(syscall_idx);

match syscall_fn
{
Expand Down

0 comments on commit ac129d8

Please sign in to comment.