bugfix local params

This commit is contained in:
Sander Hautvast 2023-10-11 17:36:24 +02:00
parent db14df8bde
commit 7313d24777
4 changed files with 43 additions and 42 deletions

View file

@ -1,12 +1,12 @@
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; //(0x3) Push int constant 0 pub const ICONST_0: u8 = 3; // (0x3) Push int constant 0
pub const ICONST_1: u8 = 4; //(0x4) Push int constant 1 pub const ICONST_1: u8 = 4; // (0x4) Push int constant 1
pub const ICONST_2: u8 = 5; //(0x5) Push int constant 2 pub const ICONST_2: u8 = 5; // (0x5) Push int constant 2
pub const ICONST_3: u8 = 6; //(0x6) Push int constant 3 pub const ICONST_3: u8 = 6; // (0x6) Push int constant 3
pub const ICONST_4: u8 = 7; //(0x7) Push int constant 4 pub const ICONST_4: u8 = 7; // (0x7) Push int constant 4
pub const ICONST_5: u8 = 8; //(0x8) 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_0: u8 = 9; // (0x9) Push long constant 0
pub const LCONST_1: u8 = 10; // (0xa) Push long constant 1 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
@ -20,10 +20,10 @@ 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 ILOAD: u8 = 21; // (0x15) Load int from local variable pub const ILOAD: u8 = 21; // (0x15) Load int from local variable
pub const LLOAD:u8 = 22; // (0x16) Load long from local variable pub const LLOAD: u8 = 22; // (0x16) Load long from local variable
pub const FLOAD:u8 = 23; // (0x16) Load float from local variable pub const FLOAD: u8 = 23; // (0x16) Load float from local variable
pub const DLOAD:u8 = 24; // (0x18) load double 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 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; // (0x1b) 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; // (0x1c) Load int from local variable 2 pub const ILOAD_2: u8 = 28; // (0x1c) Load int from local variable 2
@ -36,10 +36,10 @@ 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
pub const FLOAD_3: u8 = 37; // (0x25) Load float from local variable 3 pub const FLOAD_3: u8 = 37; // (0x25) Load float from local variable 3
pub const DLOAD_0:u8 = 38; // (0x26) Load double from local variable 0 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) Load reference from local variable 0 pub const ALOAD_0: u8 = 42; // (0x2a) Load reference from local variable 0
pub const ALOAD_1: u8 = 43; // (0x2b) Load reference from local variable 1 pub const ALOAD_1: u8 = 43; // (0x2b) Load reference from local variable 1
pub const ALOAD_2: u8 = 44; // (0x2c) Load reference from local variable 2 pub const ALOAD_2: u8 = 44; // (0x2c) Load reference from local variable 2
@ -49,26 +49,26 @@ pub const LALOAD: u8 = 47; // (0x2f) Load long from array
pub const FALOAD: u8 = 48; // (0x30) Load float from array pub const FALOAD: u8 = 48; // (0x30) Load float from array
pub const DALOAD: u8 = 49; // (0x31) Load double from array pub const DALOAD: u8 = 49; // (0x31) Load double from array
pub const AALOAD: u8 = 50; // (0x3d) Load reference from array pub const AALOAD: u8 = 50; // (0x3d) Load reference from array
pub const BALOAD: u8 = 51; //(0x33) Load byte or boolean from array pub const BALOAD: u8 = 51; // (0x33) Load byte or boolean from array
pub const CALOAD: u8 = 52; // (0x34) Load char from array pub const CALOAD: u8 = 52; // (0x34) Load char from array
pub const SALOAD: u8 = 53; // (0x34) Load short 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 ISTORE: u8 = 54; // (0x36) Store int into local variable
pub const LSTORE: u8 = 55; // (0x37) Store long into local variable pub const LSTORE: u8 = 55; // (0x37) Store long 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
pub const ASTORE :u8 = 58; // (0x3a) pub const ASTORE: u8 = 58; // (0x3a)
pub const ISTORE_0:u8 = 59; // (0x3b) Store int into local variable 0 pub const ISTORE_0: u8 = 59; // (0x3b) Store int into local variable 0
pub const ISTORE_1:u8 = 60; // (0x3c) Store int into local variable 1 pub const ISTORE_1: u8 = 60; // (0x3c) Store int into local variable 1
pub const ISTORE_2:u8 = 61; // (0x3d) Store int into local variable 2 pub const ISTORE_2: u8 = 61; // (0x3d) Store int into local variable 2
pub const ISTORE_3:u8 = 62; // (0x3e) Store int into local variable 3 pub const ISTORE_3: u8 = 62; // (0x3e) Store int into local variable 3
pub const LSTORE_0:u8 = 63; // (0x3f) Store long into local variable 0 pub const LSTORE_0: u8 = 63; // (0x3f) Store long into local variable 0
pub const LSTORE_1:u8 = 64; // (0x40) Store long into local variable 1 pub const LSTORE_1: u8 = 64; // (0x40) Store long into local variable 1
pub const LSTORE_2:u8 = 65; // (0x41) Store long into local variable 2 pub const LSTORE_2: u8 = 65; // (0x41) Store long into local variable 2
pub const LSTORE_3:u8 = 66; // (0x42) Store long into local variable 3 pub const LSTORE_3: u8 = 66; // (0x42) Store long into local variable 3
pub const FSTORE_0:u8 = 67; // (0x43) Store float into local variable 0 pub const FSTORE_0: u8 = 67; // (0x43) Store float into local variable 0
pub const FSTORE_1:u8 = 68; // (0x44) Store float into local variable 1 pub const FSTORE_1: u8 = 68; // (0x44) Store float into local variable 1
pub const FSTORE_2:u8 = 69; // (0x45) Store float into local variable 2 pub const FSTORE_2: u8 = 69; // (0x45) Store float into local variable 2
pub const FSTORE_3:u8 = 70; // (0x46) Store float into local variable 3 pub const FSTORE_3: u8 = 70; // (0x46) Store float into local variable 3
pub const DSTORE_0: u8 = 71; // (0x47) store double in local variable 0 pub const DSTORE_0: u8 = 71; // (0x47) store double in local variable 0
pub const DSTORE_1: u8 = 72; // (0x48) store double in local variable 1 pub const DSTORE_1: u8 = 72; // (0x48) store double in local variable 1
pub const DSTORE_2: u8 = 73; // (0x49) store double in local variable 2 pub const DSTORE_2: u8 = 73; // (0x49) store double in local variable 2
@ -80,11 +80,11 @@ pub const ASTORE_3: u8 = 78; // (0x4e)
pub const IASTORE: u8 = 79; // (0x4f) Store into int array pub const IASTORE: u8 = 79; // (0x4f) Store into int array
pub const LASTORE: u8 = 80; // (0x50) Store into long array pub const LASTORE: u8 = 80; // (0x50) Store into long array
pub const FASTORE: u8 = 81; // (0x51) Store into float array pub const FASTORE: u8 = 81; // (0x51) Store into float array
pub const DASTORE: u8 = 82; //(0x52) store into double array pub const DASTORE: u8 = 82; // (0x52) store into double array
pub const AASTORE: u8 = 83; // (0x53) Store into object array pub const AASTORE: u8 = 83; // (0x53) Store into object array
pub const BASTORE:u8 = 84; // (0x54) Store into byte or boolean array pub const BASTORE: u8 = 84; // (0x54) Store into byte or boolean array
pub const CASTORE:u8 = 85; // (0x55) Store into char array pub const CASTORE: u8 = 85; // (0x55) Store into char array
pub const SASTORE:u8 = 86; // (0x56) Store into short array pub const SASTORE: u8 = 86; // (0x56) Store into short array
pub const POP: u8 = 87; // (0x57) Pop the top operand stack value pub const POP: u8 = 87; // (0x57) Pop the top operand stack value
pub const DUP: u8 = 89; // (0x59) duplicate the top operand stack value pub const DUP: u8 = 89; // (0x59) duplicate the top operand stack value
// pub const dup_x1: u8 = 90; // (0x5a) Duplicate the top operand stack value and insert two values down // pub const dup_x1: u8 = 90; // (0x5a) Duplicate the top operand stack value and insert two values down

View file

@ -219,19 +219,19 @@ impl Vm {
} }
ILOAD | LLOAD | FLOAD | DLOAD | ALOAD => { // omitting the type checks so far ILOAD | LLOAD | FLOAD | DLOAD | ALOAD => { // omitting the type checks so far
let n = read_u8(&code.opcodes, pc) as usize; let n = read_u8(&code.opcodes, pc) as usize;
self.local_stack().push_arc(args[n].clone()); self.local_stack().push_arc(local_params[n].as_ref().unwrap().clone());
} }
ILOAD_0 | LLOAD_0 | FLOAD_0 | DLOAD_0 | ALOAD_0 => { ILOAD_0 | LLOAD_0 | FLOAD_0 | DLOAD_0 | ALOAD_0 => {
self.local_stack().push_arc(args[0].clone()); self.local_stack().push_arc(local_params[0].as_ref().unwrap().clone());
} }
ILOAD_1 | LLOAD_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(local_params[1].as_ref().unwrap().clone());
} }
ILOAD_2 | LLOAD_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(local_params[2].as_ref().unwrap().clone());
} }
ILOAD_3 | LLOAD_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(local_params[3].as_ref().unwrap().clone());
} }
IALOAD | LALOAD | FALOAD | DALOAD | AALOAD | BALOAD | CALOAD | SALOAD => unsafe { IALOAD | LALOAD | FALOAD | DALOAD | AALOAD | BALOAD | CALOAD | SALOAD => unsafe {
self.array_load()?; self.array_load()?;
@ -357,7 +357,7 @@ impl Vm {
//TODO implement all opcodes //TODO implement all opcodes
_ => { _ => {
panic!("opcode not implemented {:?}", self.stack) panic!("opcode not implemented {:?}", self.stack)
//TODO implement proper stacktraces //TODO implement proper --stacktraces-- error handling
} }
} }
} }
@ -493,7 +493,6 @@ struct MethodSignature {
num_args: usize, num_args: usize,
} }
//TODO refs with lifetime
fn get_signature_for_invoke(cp: Rc<HashMap<u16, CpEntry>>, index: u16) -> Option<Invocation> { fn get_signature_for_invoke(cp: Rc<HashMap<u16, CpEntry>>, index: u16) -> Option<Invocation> {
if let CpEntry::MethodRef(class_index, name_and_type_index) = cp.get(&index).unwrap() { if let CpEntry::MethodRef(class_index, name_and_type_index) = cp.get(&index).unwrap() {
if let Some(method_signature) = get_name_and_type(Rc::clone(&cp), *name_and_type_index) { if let Some(method_signature) = get_name_and_type(Rc::clone(&cp), *name_and_type_index) {

Binary file not shown.

View file

@ -1,6 +1,8 @@
public class Main { public class Main {
public static void main(String[] args){ public static void main(String[] args){
new FloatBean().setValue(42F); FloatBean f = new FloatBean();
f.setValue(42F);
System.out.println(f.getValue());
} }
} }