From 7313d247776e250d72a1eaf1341bd9e9c8a0fb71 Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Wed, 11 Oct 2023 17:36:24 +0200 Subject: [PATCH] bugfix local params --- src/opcodes.rs | 68 +++++++++++++++++++++++------------------------ src/vm.rs | 13 +++++---- tests/Main.class | Bin 322 -> 476 bytes tests/Main.java | 4 ++- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/opcodes.rs b/src/opcodes.rs index 5f8e6be..b2854a9 100644 --- a/src/opcodes.rs +++ b/src/opcodes.rs @@ -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 ICONST_M1: u8 = 2; //(0x2) Push int constant -1 -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_2: u8 = 5; //(0x5) Push int constant 2 -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_5: u8 = 8; //(0x8) Push int constant 5 +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_1: u8 = 4; // (0x4) Push int constant 1 +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_4: u8 = 7; // (0x7) Push int constant 4 +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 @@ -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 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 LLOAD:u8 = 22; // (0x16) Load long 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 ALOAD:u8 = 25; //0x19 Load reference 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 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_1: u8 = 27; // (0x1b) Load int from local variable 1 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_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 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_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_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_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 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_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 DALOAD: u8 = 49; // (0x31) Load double 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 SALOAD: u8 = 53; // (0x34) Load short from array pub const ISTORE: u8 = 54; // (0x36) Store int 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 DSTORE: u8 = 57; // (0x39) store double in local variable -pub const ASTORE :u8 = 58; // (0x3a) -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_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 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_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 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_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 ASTORE: u8 = 58; // (0x3a) +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_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 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_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 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_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 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_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 LASTORE: u8 = 80; // (0x50) Store into long 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 BASTORE:u8 = 84; // (0x54) Store into byte or boolean array -pub const CASTORE:u8 = 85; // (0x55) Store into char array -pub const SASTORE:u8 = 86; // (0x56) Store into short 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 SASTORE: u8 = 86; // (0x56) Store into short array 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_x1: u8 = 90; // (0x5a) Duplicate the top operand stack value and insert two values down diff --git a/src/vm.rs b/src/vm.rs index 57d782b..5ad9aa4 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -219,19 +219,19 @@ impl Vm { } 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()); + self.local_stack().push_arc(local_params[n].as_ref().unwrap().clone()); } 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 => { - 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 => { - 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 => { - 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 { self.array_load()?; @@ -357,7 +357,7 @@ impl Vm { //TODO implement all opcodes _ => { 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, } -//TODO refs with lifetime fn get_signature_for_invoke(cp: Rc>, index: u16) -> Option { 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) { diff --git a/tests/Main.class b/tests/Main.class index 0be53597f9eb2b086fffe7ab7f9e1cd73351a26f..5b773bcd0f801c15768ecdf073de223b094e644a 100644 GIT binary patch delta 335 zcmZvWy-osA5QV>cS-9-F0)i;0p!f^HgiZ@%LqSDhLSka)#%u^?Sqv^FRz3qS;5%r6 zi6$17K7+k)VVotFPBJrR=A1K`5B-%m`uur)2aefya3~m-9W({2T}Nh3(Po9!7?m8# ztQpn~8`FcU#saRw?gS$*YWrU3QlVNRs z3i(dzqD&R()9>tMMsP|nk~Ud#fBdQFwQS38=!+K;-DcjJk$fiAoMC39pXQd>p2w1E RktRt-j+HQSWJz|K{4Y+cHwOR! delta 192 zcmYL@D-Oa?5Jk^>{XAM)D5Zcxp!ft`dc*kLU7I9LiI~h;h}dugvg; zwWU!=OnNBOR8)z2s~U9^qv%Ta+ohZ$A{K9>%NB+BiQd&Xse8x!BlZ(;1@HvuiP!Xh Df%6l8 diff --git a/tests/Main.java b/tests/Main.java index e6ef7a1..20c7d49 100644 --- a/tests/Main.java +++ b/tests/Main.java @@ -1,6 +1,8 @@ public class Main { public static void main(String[] args){ - new FloatBean().setValue(42F); + FloatBean f = new FloatBean(); + f.setValue(42F); + System.out.println(f.getValue()); } }