Clean up op codes.

This commit is contained in:
Jesse Brault 2024-12-28 15:35:35 -06:00
parent 9c4ca23765
commit 7aa93c3986
5 changed files with 42 additions and 118 deletions

View File

@ -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<DvmObject>`.
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]

View File

@ -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;

View File

@ -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<u8>, register: u8) {
code.push(register);
}
pub fn add_load(
code: &mut Vec<u8>,
target_register: u8,
source_register: u8,
field_index: usize
) {
pub fn add_load(code: &mut Vec<u8>, 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<u8>,
target_register: u8,
field_index: usize,
source_register: u8,
) {
pub fn add_store(code: &mut Vec<u8>, target_register: u8, field_index: usize, source_register: u8) {
code.push(STORE);
code.push(target_register);
push_number!(code, field_index);

View File

@ -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;