Skip to content

Commit f52661f

Browse files
Fix JBMC left shift operator
According to JLS 15.19 Shift Operators, a bit mask is applied to the second operand depending on the size of the first.
1 parent 96de90f commit f52661f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

jbmc/regression/jbmc/shift1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
shift1.foo
33

44
^EXIT=0$

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
15251525
else if(bytecode == patternt("?shl"))
15261526
{
15271527
PRECONDITION(op.size() == 2 && results.size() == 1);
1528-
results[0]=shl_exprt(op[0], op[1]);
1528+
results = convert_shl(statement, op, results);
15291529
}
15301530
else if(bytecode == patternt("?shr"))
15311531
{
@@ -2749,6 +2749,23 @@ exprt::operandst &java_bytecode_convert_methodt::convert_cmp(
27492749
return results;
27502750
}
27512751

2752+
exprt::operandst &java_bytecode_convert_methodt::convert_shl(
2753+
const irep_idt &statement,
2754+
const exprt::operandst &op,
2755+
exprt::operandst &results) const
2756+
{
2757+
const typet type = java_type_from_char(statement[0]);
2758+
2759+
const std::size_t width = get_bytecode_type_width(type);
2760+
2761+
// According to JLS 15.19 Shift Operators
2762+
// a mask 0b11111 is applied for int and 0b111111 for long.
2763+
exprt mask = from_integer(width - 1, op[1].type());
2764+
2765+
results[0] = shl_exprt(op[0], bitand_exprt(op[1], mask));
2766+
return results;
2767+
}
2768+
27522769
exprt::operandst &java_bytecode_convert_methodt::convert_ushr(
27532770
const irep_idt &statement,
27542771
const exprt::operandst &op,

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ class java_bytecode_convert_methodt:public messaget
418418
const source_locationt &location,
419419
method_offsett address);
420420

421+
exprt::operandst &convert_shl(
422+
const irep_idt &statement,
423+
const exprt::operandst &op,
424+
exprt::operandst &results) const;
425+
421426
exprt::operandst &convert_ushr(
422427
const irep_idt &statement,
423428
const exprt::operandst &op,

0 commit comments

Comments
 (0)