lot more opcodes

This commit is contained in:
Sander Hautvast 2023-10-02 21:25:56 +02:00
parent e92f6e270f
commit 3540f48f21
3 changed files with 89 additions and 48 deletions

View file

@ -11,7 +11,7 @@ pub struct Object {
// hashcode: i32, // hashcode: i32,
pub class: Rc<Class>, pub class: Rc<Class>,
pub data: HashMap<u16, Arc<UnsafeCell<Value>>>, //TODO optimize pub data: HashMap<u16, Arc<UnsafeCell<Value>>>, //TODO optimize
} }//arrays
unsafe impl Send for Object {} unsafe impl Send for Object {}

View file

@ -1,31 +1,37 @@
pub const NOP:u8 = 0; //(0x0) Do nothing pub const NOP:u8 = 0; //(0x0) Do nothing
pub const ACONST_NULL: u8 = 1; // (0x01) Push null pub const ACONST_NULL: u8 = 1; // (0x01) Push null
pub const ICONST_M1: u8 = 2; //(0x2) Push int constant -1 pub const ICONST_M1: u8 = 2; //(0x2) Push int constant -1
pub const ICONST_0: u8 = 3; //(0x2) Push int constant 0 pub const ICONST_0: u8 = 3; //(0x3) Push int constant 0
pub const ICONST_1: u8 = 4; //(0x2) Push int constant 1 pub const ICONST_1: u8 = 4; //(0x4) Push int constant 1
pub const ICONST_2: u8 = 5; //(0x2) Push int constant 2 pub const ICONST_2: u8 = 5; //(0x5) Push int constant 2
pub const ICONST_3: u8 = 6; //(0x2) Push int constant 3 pub const ICONST_3: u8 = 6; //(0x6) Push int constant 3
pub const ICONST_4: u8 = 7; //(0x2) Push int constant 4 pub const ICONST_4: u8 = 7; //(0x7) Push int constant 4
pub const ICONST_5: u8 = 8; //(0x2) Push int constant 5 pub const ICONST_5: u8 = 8; //(0x8) Push int constant 5
pub const LCONST_0: u8 = 9; // (0x9) Push long constant 0
pub const LCONST_1: u8 = 10; // (0xa) Push long constant 1
pub const FCONST_0: u8 = 11; // (0xb) Push float 0 pub const FCONST_0: u8 = 11; // (0xb) Push float 0
pub const FCONST_1: u8 = 12; // (0xc) Push float 1 pub const FCONST_1: u8 = 12; // (0xc) Push float 1
pub const FCONST_2: u8 = 13; // (0xd) Push float 2 pub const FCONST_2: u8 = 13; // (0xd) Push float 2
// pub const dconst_0:u8 = 14; // (0xe) push double 0 pub const DCONST_0 :u8 = 14; // (0xe) push double 0
// pub const dconst_1:u8 = 15; // (0xf) push double 1 pub const DCONST_1 :u8 = 15; // (0xe) push double 1
// TODO turn all into references
pub const BIPUSH: u8 = 16; // (0x10) Push byte pub const BIPUSH: u8 = 16; // (0x10) Push byte
pub const SIPUSH: u8 = 17; // (0x11) Push short
pub const LDC: u8 = 18; // (0x12) Push item from run-time pub constant pool pub const LDC: u8 = 18; // (0x12) Push item from run-time pub constant pool
pub const LDC_W: u8 = 19; // (0x13) Push item from run-time constant pool (wide index) pub const LDC_W: u8 = 19; // (0x13) Push item from run-time constant pool (wide index)
pub const LDC2_W: u8 = 20; // (0x14) Push long or double from run-time constant pool (wide index) pub const LDC2_W: u8 = 20; // (0x14) Push long or double from run-time constant pool (wide index)
// pub const fload:u8 = 23; // (0x17) Load float from local variable pub const ILOAD: u8 = 21; // (0x15) Load int from local variable
// pub const dload:u8 = 24; // (0x18) load double from local variable pub const LLOAD:u8 = 22; // (0x16) Load long from local variable
// pub const aload:u8 = 25; //0x19 pub const FLOAD:u8 = 23; // (0x16) Load float from local variable
// pub const DLOAD:u8 = 24; // (0x18) load double from local variable
pub const ALOAD:u8 = 25; //0x19 Load reference from local variable
pub const ILOAD_0: u8 = 26; // (0x1a) Load int from local variable 0 pub const ILOAD_0: u8 = 26; // (0x1a) Load int from local variable 0
pub const ILOAD_1: u8 = 27; // (0x1a) Load int from local variable 1 pub const ILOAD_1: u8 = 27; // (0x1b) Load int from local variable 1
pub const ILOAD_2: u8 = 28; // (0x1a) Load int from local variable 2 pub const ILOAD_2: u8 = 28; // (0x1c) Load int from local variable 2
pub const ILOAD_3: u8 = 29; // (0x1a) Load int from local variable 3 pub const ILOAD_3: u8 = 29; // (0x1d) Load int from local variable 3
pub const LLOAD_0: u8 = 30; // (0x1e) Load long from local variable 0
pub const LLOAD_1: u8 = 31; // (0x1f) Load long from local variable 1
pub const LLOAD_2: u8 = 32; // (0x20) Load long from local variable 2
pub const LLOAD_3: u8 = 33; // (0x21) Load long from local variable 3
pub const FLOAD_0: u8 = 34; // (0x22) Load float from local variable 0 pub const FLOAD_0: u8 = 34; // (0x22) Load float from local variable 0
pub const FLOAD_1: u8 = 35; // (0x23) Load float from local variable 1 pub const FLOAD_1: u8 = 35; // (0x23) Load float from local variable 1
pub const FLOAD_2: u8 = 36; // (0x24) Load float from local variable 2 pub const FLOAD_2: u8 = 36; // (0x24) Load float from local variable 2
@ -34,16 +40,19 @@ pub const DLOAD_0:u8 = 38; // (0x26) Load double from local variable 0
pub const DLOAD_1:u8 = 39; // (0x27) Load double from local variable 1 pub const DLOAD_1:u8 = 39; // (0x27) Load double from local variable 1
pub const DLOAD_2:u8 = 40; // (0x28) Load double from local variable 2 pub const DLOAD_2:u8 = 40; // (0x28) Load double from local variable 2
pub const DLOAD_3:u8 = 41; // (0x29) Load double from local variable 3 pub const DLOAD_3:u8 = 41; // (0x29) Load double from local variable 3
pub const ALOAD_0: u8 = 42; // (0x2a) pub const ALOAD_0: u8 = 42; // (0x2a) Load reference from local variable 0
pub const ALOAD_1: u8 = 43;// (0x2a) pub const ALOAD_1: u8 = 43; // (0x2b) Load reference from local variable 1
pub const ALOAD_2: u8 = 44;// (0x2b) pub const ALOAD_2: u8 = 44; // (0x2c) Load reference from local variable 2
pub const ALOAD_3: u8 = 45;// (0x2c) pub const ALOAD_3: u8 = 45; // (0x2d) Load reference from local variable 3
pub const IALOAD: u8 = 46; // (0x2e) Load int from array
// pub const faload: u8 = 48; // (0x30) Load float from array pub const LALOAD: u8 = 47; // (0x2f) Load long from array
// pub const daload:u8 = 49; // (0x31) load double from array pub const FALOAD: u8 = 48; // (0x30) Load float from array
// pub const aaload: u8 = 50; // (0x3d) pub const DALOAD: u8 = 49; // (0x31) Load double from array
// pub const baload: u8 = 51; //(0x33) pub const AALOAD: u8 = 50; // (0x3d) Load reference from array
// pub const caload: u8 = 52; // (0x34) pub const BALOAD: u8 = 51; //(0x33) Load byte or boolean from array
pub const CALOAD: u8 = 52; // (0x34) Load char from array
pub const SALOAD: u8 = 53; // (0x34) Load short from array
pub const ISTORE: u8 = 54; // (0x36) Store int into local variable
// //
// pub const fstore: u8 = 56; // (0x38) Store float into local variable // pub const fstore: u8 = 56; // (0x38) Store float into local variable
// pub const dstore: u8 = 57; // (0x39) store double in local variable // pub const dstore: u8 = 57; // (0x39) store double in local variable

View file

@ -124,41 +124,57 @@ impl Vm {
println!("opcode {} ", opcode); println!("opcode {} ", opcode);
match opcode { match opcode {
ACONST_NULL => { ACONST_NULL => {
self.local_stack().push(Value::Null) self.local_stack().push(Value::Null);
} }
ICONST_M1 => { ICONST_M1 => {
self.local_stack().push(Value::I32(-1)) self.local_stack().push(Value::I32(-1));
} }
ICONST_0 => { ICONST_0 => {
self.local_stack().push(Value::I32(0)) self.local_stack().push(Value::I32(0));
} }
ICONST_1 => { ICONST_1 => {
self.local_stack().push(Value::I32(1)) self.local_stack().push(Value::I32(1));
} }
ICONST_2 => { ICONST_2 => {
self.local_stack().push(Value::I32(2)) self.local_stack().push(Value::I32(2));
} }
ICONST_3 => { ICONST_3 => {
self.local_stack().push(Value::I32(3)) self.local_stack().push(Value::I32(3));
} }
ICONST_4 => { ICONST_4 => {
self.local_stack().push(Value::I32(4)) self.local_stack().push(Value::I32(4));
} }
ICONST_5 => { ICONST_5 => {
self.local_stack().push(Value::I32(5)) self.local_stack().push(Value::I32(5));
}
LCONST_0 => {
self.local_stack().push(Value::I64(0));
}
LCONST_1 => {
self.local_stack().push(Value::I64(1));
} }
FCONST_0 => { FCONST_0 => {
self.local_stack().push(Value::F32(0.0)) self.local_stack().push(Value::F32(0.0));
} }
FCONST_1 => { FCONST_1 => {
self.local_stack().push(Value::F32(1.0)) self.local_stack().push(Value::F32(1.0));
} }
FCONST_2 => { FCONST_2 => {
self.local_stack().push(Value::F32(2.0)) self.local_stack().push(Value::F32(2.0));
}
DCONST_0 => {
self.local_stack().push(Value::F64(0.0));
}
DCONST_1 => {
self.local_stack().push(Value::F64(1.0));
}
SIPUSH => {
let s = read_u16(&code.opcodes, pc) as i32;
self.local_stack().push(Value::I32(s));
} }
BIPUSH => { BIPUSH => {
let c = read_u8(&code.opcodes, pc); let c = read_u8(&code.opcodes, pc) as i32;
self.local_stack().push(Value::I32(c as i32)); self.local_stack().push(Value::I32(c));
} }
LDC => { LDC => {
let cp_index = read_u8(&code.opcodes, pc) as u16; let cp_index = read_u8(&code.opcodes, pc) as u16;
@ -200,18 +216,34 @@ impl Vm {
} }
} }
} }
ILOAD_0 | FLOAD_0 | DLOAD_0 | ALOAD_0 => { ILOAD | LLOAD | FLOAD | DLOAD | ALOAD => { // omitting the type checks so far
let n = read_u8(&code.opcodes, pc) as usize;
self.local_stack().push_arc(args[n].clone());
}
ILOAD_0 | LLOAD_0 | FLOAD_0 | DLOAD_0 | ALOAD_0 => {
self.local_stack().push_arc(args[0].clone()); self.local_stack().push_arc(args[0].clone());
} }
ILOAD_1 | FLOAD_1 | DLOAD_1 | ALOAD_1 => { ILOAD_1 | LLOAD_1 | FLOAD_1 | DLOAD_1 | ALOAD_1 => {
self.local_stack().push_arc(args[1].clone()); self.local_stack().push_arc(args[1].clone());
} }
ILOAD_2 | FLOAD_2 | DLOAD_2 | ALOAD_2 => { ILOAD_2 | LLOAD_2 | FLOAD_2 | DLOAD_2 | ALOAD_2 => {
self.local_stack().push_arc(args[2].clone()); self.local_stack().push_arc(args[2].clone());
} }
ILOAD_3 | FLOAD_3 | DLOAD_3 | ALOAD_3 => { ILOAD_3 | LLOAD_3 | FLOAD_3 | DLOAD_3 | ALOAD_3 => {
self.local_stack().push_arc(args[3].clone()); self.local_stack().push_arc(args[3].clone());
} }
IALOAD | LALOAD | FALOAD | DALOAD | AALOAD | BALOAD | CALOAD | SALOAD => {
// TODO implement arrays
// let index = self.local_stack().pop()?;
// let array_ref = self.local_stack().pop()?;
}
ISTORE =>{
let index = read_u8(&code.opcodes, pc);
let value = self.local_stack().pop()?;
if let Value::I32(int) = value {
// TODO local vars
}
}
POP => { POP => {
self.local_stack().pop().expect("Stack empty"); self.local_stack().pop().expect("Stack empty");
} }