From 7aa93c3986a2bf8945eb287653c0e00ee1b4ae7b Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Sat, 28 Dec 2024 15:35:35 -0600 Subject: [PATCH] Clean up op codes. --- doc/dvm.asciidoc | 62 +++------------------------------ src/vm/dvm_value.rs | 2 +- src/vm/mod.rs | 3 ++ src/vm/op_codes.rs | 83 ++++++++++++++++++--------------------------- src/vm/util.rs | 10 ------ 5 files changed, 42 insertions(+), 118 deletions(-) diff --git a/doc/dvm.asciidoc b/doc/dvm.asciidoc index 5d4374e..79227ae 100644 --- a/doc/dvm.asciidoc +++ b/doc/dvm.asciidoc @@ -101,60 +101,9 @@ mov_register( ) ---- -=== Load Byte +=== Load -Loads the target register with the `u8` stored at the offset from the base of the object stored in the source register. - -[source] ----- -load_byte( - target_register: u8, - source_register: u8, - offset: usize -) ----- - -The source register must contain a `DvmValue::Object` at runtime. - -=== Load Int - -Loads the target register with the `i32` stored at the offset from the base of the object stored in the source register. - -[source] ----- -load_int( - target_register: u8, - source_register: u8, - offset: usize -) ----- - -The source register must contain a `DvmValue::Object` at runtime. - -=== Load Long - -Loads the target register with the `i64` stored at the offset from the base of the object stored in the source register. - -[source] ----- -load_long( - target_register: u8, - source_register: u8, - offset: usize -) ----- - -The source register must contain a `DvmValue::Object` at runtime. - -=== Load Double - -=== Load USize - -=== Load Boolean - -=== Load Object - -Loads the target register with the `DvmValue::Object` stored at the offset from the base of the object stored in the +Loads the target register with the `DvmValue` inner value stored in the field at `field_index` in the object in the source register. [source] @@ -162,16 +111,15 @@ source register. load_object( target_register: u8, source_register: u8, - offset: usize + field_index: usize ) ---- -The source register must contain a `DvmValue::Object` at runtime, and the data at the offset from that object's base -must be a valid `Rc`. +The source register must contain a `DvmValue::Object` at runtime. === Store -Stores the value contained in the source register to the offset from the base of the object contained in the target +Stores the value contained in the source register to the field at `field_index` in the object contained in the target register. [source] diff --git a/src/vm/dvm_value.rs b/src/vm/dvm_value.rs index aece25e..aa15d05 100644 --- a/src/vm/dvm_value.rs +++ b/src/vm/dvm_value.rs @@ -30,7 +30,7 @@ impl DvmValue { panic!("Expected DvmValue::Object, but found DvmValue::{:?}", self); } } - + pub fn is_object(&self) -> bool { match self { DvmValue::Object(_) => true, diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 98aa7bd..31964eb 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -274,6 +274,9 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8 let mut iter = byte_code.iter().cloned(); while let Some(op_code) = iter.next() { match op_code { + MOV_BYTE => { + todo!() + } MOV_INT => { let target_register = next_8!(iter, usize); let operand = next_32_le!(iter, u32) as i32; diff --git a/src/vm/op_codes.rs b/src/vm/op_codes.rs index 8374346..b236a32 100644 --- a/src/vm/op_codes.rs +++ b/src/vm/op_codes.rs @@ -1,46 +1,39 @@ -use crate::push_bytes; +pub const MOV_BYTE: u8 = 0x01; +pub const MOV_INT: u8 = 0x02; +pub const MOV_LONG: u8 = 0x03; +pub const MOV_U_SIZE: u8 = 0x04; +pub const MOV_DOUBLE: u8 = 0x05; +pub const MOV_BOOLEAN: u8 = 0x06; +pub const MOV_REGISTER: u8 = 0x07; +pub const MOV_CONST: u8 = 0x08; -/// ## mov(register: u8, operand: u32) -/// - 0: opcode -/// - 1: register -/// - 2..5: operand -pub const MOV_INT: u8 = 0x01; +pub const LOAD: u8 = 0x10; +pub const STORE: u8 = 0x11; -pub const MOV_LONG: u8 = 0x02; -pub const MOV_DOUBLE: u8 = 0x03; +pub const ALLOC: u8 = 0x20; +pub const DEALLOC: u8 = 0x21; +pub const ALLOC_RAW: u8 = 0x22; +pub const DEALLOC_RAW: u8 = 0x23; -/// ## mov(target_register: u8, source_register: u8) -/// 0: opcode -/// 1: target_register -/// 2: source_register -pub const MOV_REGISTER: u8 = 0x04; +pub const PLATFORM_CALL: u8 = 0x30; +pub const INVOKE_FN: u8 = 0x31; +pub const INVOKE_VIRTUAL: u8 = 0x32; +pub const INVOKE_DYNAMIC: u8 = 0x33; -/// ## alloc(register: u8, size: u32) -/// 0: opcode -/// 1: register -/// 2..5: size -pub const ALLOC: u8 = 0x05; +pub const ADD: u8 = 0x40; +pub const SUBTRACT: u8 = 0x41; +pub const MULTIPLY: u8 = 0x42; +pub const DIVIDE: u8 = 0x43; +pub const MODULO: u8 = 0x44; +pub const POWER: u8 = 0x45; -/// ## dealloc(register: u8) -/// 0: opcode -/// 1: register -pub const DEALLOC: u8 = 0x06; - -pub const MOV_CONST: u8 = 0x0b; - -pub const ALLOC_RAW: u8 = 0x0d; -pub const DEALLOC_RAW: u8 = 0x0e; - -pub const PLATFORM_CALL: u8 = 0x10; -pub const INVOKE_FN: u8 = 0x11; -pub const INVOKE_VIRTUAL: u8 = 0x12; -pub const INVOKE_DYNAMIC: u8 = 0x14; - -pub const MULTIPLY: u8 = 0x40; - -pub const LOAD: u8 = 0x50; - -pub const STORE: u8 = 0x60; +macro_rules! push_bytes { + ( $dest: expr, $src: expr ) => { + for b in $src { + $dest.push(b); + } + }; +} macro_rules! push_number { ( $dest: expr, $num: expr ) => { @@ -84,24 +77,14 @@ pub fn add_dealloc(code: &mut Vec, register: u8) { code.push(register); } -pub fn add_load( - code: &mut Vec, - target_register: u8, - source_register: u8, - field_index: usize -) { +pub fn add_load(code: &mut Vec, target_register: u8, source_register: u8, field_index: usize) { code.push(LOAD); code.push(target_register); code.push(source_register); push_number!(code, field_index); } -pub fn add_store( - code: &mut Vec, - target_register: u8, - field_index: usize, - source_register: u8, -) { +pub fn add_store(code: &mut Vec, target_register: u8, field_index: usize, source_register: u8) { code.push(STORE); code.push(target_register); push_number!(code, field_index); diff --git a/src/vm/util.rs b/src/vm/util.rs index f08cfff..b9dbf7d 100644 --- a/src/vm/util.rs +++ b/src/vm/util.rs @@ -22,15 +22,5 @@ macro_rules! get_64_le { }; } -#[macro_export] -macro_rules! push_bytes { - ( $dest: expr, $src: expr ) => { - for b in $src { - $dest.push(b); - } - }; -} - pub use get_32_le; pub use get_64_le; -pub use push_bytes;