Skip to content

Commit 6229e47

Browse files
committed
Fix shift operations to mask shift amount per JVM spec
1 parent 4614aba commit 6229e47

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

jvm_rust/src/interpreter.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,13 @@ impl Interpreter {
662662
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
663663
let value1: i32 = stack_frame.operand_stack.pop().unwrap().into();
664664

665-
stack_frame.operand_stack.push(JavaValue::Int(value1 << value2));
665+
stack_frame.operand_stack.push(JavaValue::Int(value1 << (value2 & 0x1f)));
666666
}
667667
Opcode::Ishr => {
668668
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
669669
let value1: i32 = stack_frame.operand_stack.pop().unwrap().into();
670670

671-
stack_frame.operand_stack.push(JavaValue::Int(value1 >> value2));
671+
stack_frame.operand_stack.push(JavaValue::Int(value1 >> (value2 & 0x1f)));
672672
}
673673
Opcode::Isub => {
674674
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
@@ -680,7 +680,7 @@ impl Interpreter {
680680
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
681681
let value1: i32 = stack_frame.operand_stack.pop().unwrap().into();
682682

683-
stack_frame.operand_stack.push(JavaValue::Int(((value1 as u32) >> (value2 as u32)) as _));
683+
stack_frame.operand_stack.push(JavaValue::Int(((value1 as u32) >> ((value2 as u32) & 0x1f)) as _));
684684
}
685685
Opcode::Ixor => {
686686
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
@@ -768,13 +768,13 @@ impl Interpreter {
768768
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
769769
let value1: i64 = stack_frame.operand_stack.pop().unwrap().into();
770770

771-
stack_frame.operand_stack.push(JavaValue::Long(value1 << value2));
771+
stack_frame.operand_stack.push(JavaValue::Long(value1 << (value2 & 0x3f)));
772772
}
773773
Opcode::Lshr => {
774774
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
775775
let value1: i64 = stack_frame.operand_stack.pop().unwrap().into();
776776

777-
stack_frame.operand_stack.push(JavaValue::Long(value1 >> value2));
777+
stack_frame.operand_stack.push(JavaValue::Long(value1 >> (value2 & 0x3f)));
778778
}
779779
Opcode::Lsub => {
780780
let value2: i64 = stack_frame.operand_stack.pop().unwrap().into();
@@ -786,7 +786,7 @@ impl Interpreter {
786786
let value2: i32 = stack_frame.operand_stack.pop().unwrap().into();
787787
let value1: i64 = stack_frame.operand_stack.pop().unwrap().into();
788788

789-
stack_frame.operand_stack.push(JavaValue::Long(((value1 as u64) >> (value2 as u64)) as _));
789+
stack_frame.operand_stack.push(JavaValue::Long(((value1 as u64) >> ((value2 as u64) & 0x3f)) as _));
790790
}
791791
Opcode::Lxor => {
792792
let value2: i64 = stack_frame.operand_stack.pop().unwrap().into();

0 commit comments

Comments
 (0)