Skip to content

Commit

Permalink
Changed instructions to tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
vangroan committed May 5, 2024
1 parent 6308373 commit f564262
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 68 deletions.
54 changes: 18 additions & 36 deletions crates/vuur_vm/src/instruction_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,19 @@ pub enum Op {
I32_GreaterEq,

/// Push a constant int32 value onto the operand stack.
I32_Const {
constant_id: ConstantId,
},
I32_Const_Inline {
arg: Arg24,
},
I32_Const(ConstantId),
I32_Const_Inline(Arg24),

// ------------------------------------------------------------------------
// Variables
Store_Global {
global_id: GlobalId,
},
Load_Global {
global_id: GlobalId,
},
Store_Local {
local_id: LocalId,
},
Load_Local {
local_id: LocalId,
},
Store_Upvalue {
up_value_id: UpValueId,
},
Load_Upvalue {
up_value_id: UpValueId,
},
Store_Global(GlobalId),
Load_Global(GlobalId),
Store_Local(LocalId),
Load_Local(LocalId),
Store_Upvalue(UpValueId),
Load_Upvalue(UpValueId),
/// "Close" the up-value, copying its inner value into its heap slot.
Upvalue_Close {
up_value_id: UpValueId,
},
Upvalue_Close(UpValueId),

// ------------------------------------------------------------------------
// Callables
Expand Down Expand Up @@ -108,15 +90,15 @@ impl Op {
Op::I32_Greater => -1,
Op::I32_LessEq => -1,
Op::I32_GreaterEq => -1,
Op::I32_Const { .. } => 1,
Op::I32_Const_Inline { .. } => 1,
Op::Store_Global { .. } => 0,
Op::Load_Global { .. } => 1,
Op::Store_Local { .. } => 0,
Op::Load_Local { .. } => 1,
Op::Store_Upvalue { .. } => 0,
Op::Load_Upvalue { .. } => 1,
Op::Upvalue_Close { .. } => 0,
Op::I32_Const(_) => 1,
Op::I32_Const_Inline(_) => 1,
Op::Store_Global(_) => 0,
Op::Load_Global(_) => 1,
Op::Store_Local(_) => 0,
Op::Load_Local(_) => 1,
Op::Store_Upvalue(_) => 0,
Op::Load_Upvalue(_) => 1,
Op::Upvalue_Close(_) => 0,
Op::Call_Closure { arity } => -(*arity as isize) + 1,
Op::Call_Method { arity, .. } => -(*arity as isize), // remember receiver
Op::Return => -1,
Expand Down
46 changes: 19 additions & 27 deletions crates/vuur_vm/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::value::{Slot, Value};
use crate::value::{Value};
use crate::{
handle::Handle,
instruction_set::{Arg24, Op},
value::{Closure, ConstantId, GlobalId, LocalId, Module, Program, ScriptFunc},
vm_v2::{Store, VM},
vm_v2::{VM},
};

use std::rc::Rc;
use std::time::{Instant, Duration};

/// Create a recursive fibonacci script function.
fn fibonacci(module: Handle<Module>) -> Rc<ScriptFunc> {
Expand All @@ -19,32 +21,26 @@ fn fibonacci(module: Handle<Module>) -> Rc<ScriptFunc> {
let fib = GlobalId::new(0);
let n = LocalId::new(0);
let code = vec![
Op::Load_Local { local_id: n },
Op::I32_Const_Inline {
arg: Arg24::from_i32(1),
},
Op::Load_Local(n),
Op::I32_Const_Inline(Arg24::from_i32(1)),
Op::I32_LessEq,
Op::Jump_False {
addr: Arg24::from_u32(6),
},
Op::Load_Local { local_id: n },
Op::Load_Local(n),
Op::Return,
// Setup call to fib(n)
Op::Load_Global { global_id: fib },
Op::Load_Global(fib),
// n - 1
Op::Load_Local { local_id: n },
Op::I32_Const_Inline {
arg: Arg24::from_i32(1),
},
Op::Load_Local(n),
Op::I32_Const_Inline(Arg24::from_i32(1)),
Op::I32_Sub,
Op::Call_Closure { arity: 1 },
// Setup call to fib(n)
Op::Load_Global { global_id: fib },
Op::Load_Global(fib),
// n - 2
Op::Load_Local { local_id: n },
Op::I32_Const_Inline {
arg: Arg24::from_i32(2),
},
Op::Load_Local(n),
Op::I32_Const_Inline(Arg24::from_i32(2)),
Op::I32_Sub,
Op::Call_Closure { arity: 1 },
// fib(n - 1) + fib(n - 2)
Expand Down Expand Up @@ -74,17 +70,11 @@ fn test_vm_v2() {

let code = vec![
// func fib(n: Int) -> Int:
Op::Closure(ConstantId::new(0)), // create closure
Op::Store_Global {
global_id: GlobalId::new(0),
}, // Store closure in variable
Op::Closure(ConstantId::new(0)), // create closure
Op::Store_Global(GlobalId::new(0)), // Store closure in variable
// fib(5)
Op::Load_Global {
global_id: GlobalId::new(0),
}, // Load closure from variable
Op::I32_Const_Inline {
arg: Arg24::from_i32(fib_arg_1),
},
Op::Load_Global(GlobalId::new(0)), // Load closure from variable
Op::I32_Const_Inline(Arg24::from_i32(fib_arg_1)),
Op::Call_Closure { arity: 1 },
// Op::I32_Const_Inline {
// arg: Arg24::from_i32(1),
Expand All @@ -110,7 +100,9 @@ fn test_vm_v2() {

// ---------------------------------------------------------------------------------------------
let mut vm = VM::new();
let start = Instant::now();
let value = vm.run_program(&program);
println!("time: {}µs", (Instant::now() - start).as_micros());
println!("{value:?}");
assert_eq!(value.unwrap().into_i32().unwrap(), 55);
}
11 changes: 6 additions & 5 deletions crates/vuur_vm/src/vm_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl VM {
}
}

// #[inline(never)]
pub(crate) fn run_program(&mut self, program: &Program) -> Result<Value, String> {
let module = program.module.clone();
let closure = program.closure.clone();
Expand Down Expand Up @@ -266,34 +267,34 @@ fn run_op_loop(_vm: &mut VM, fiber: &mut Fiber, frame: &mut CallFrame) -> Result
let [a, b] = fiber.pop_slots_2();
fiber.stack.push(Value::Bool(a.into_i32()? <= b.into_i32()?));
}
Op::I32_Const_Inline { arg } => {
Op::I32_Const_Inline(arg) => {
let a = arg.to_i32();
fiber.stack.push(Value::from_i32(a));
}
Op::Store_Global { global_id } => {
Op::Store_Global(global_id) => {
let module = func
.module
.upgrade()
.ok_or_else(|| "function lost reference to its lexical module")?;
let value = fiber.stack.pop().unwrap_or(Value::Nil);
module.borrow_mut().vars.insert(global_id, value);
}
Op::Load_Global { global_id } => {
Op::Load_Global(global_id) => {
let module = func
.module
.upgrade()
.ok_or_else(|| "function lost reference to its lexical module")?;
let value = module.borrow_mut().vars.get(global_id).clone();
fiber.stack.push(value);
}
Op::Store_Local { local_id } => {
Op::Store_Local(local_id) => {
let index = frame.stack_offset + local_id.to_usize();
if index >= fiber.stack.len() {
return Err("operand stack overflow".to_string());
}
fiber.stack[index] = fiber.stack.pop().ok_or_else(|| "operand stack underflow")?;
}
Op::Load_Local { local_id } => {
Op::Load_Local(local_id) => {
let value = fiber
.stack
.get(frame.stack_offset + local_id.to_usize())
Expand Down

0 comments on commit f564262

Please sign in to comment.