Skip to content

Commit 41f9711

Browse files
Merge pull request #5590 from peterschrammel/fix-jbmc-shl
Fix shl operator in JBMC
2 parents bd6acb6 + f52661f commit 41f9711

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed
753 Bytes
Binary file not shown.

jbmc/regression/jbmc/shift1/shift1.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
class shift1
22
{
3-
public static void main(String[] args)
3+
public static void foo(int c, long x)
44
{
5-
assert (5 << 1) == 10;
6-
assert (5 >> 1) == 2;
7-
assert (-4 >> 1) == -2;
8-
assert (-4 >>> 1) == 2147483646;
5+
switch (c)
6+
{
7+
case 0:
8+
if (x == 5L) assert ((int)x << 1) == 10;
9+
break;
10+
case 1:
11+
if (x == 5L) assert ((int)x >> 1) == 2;
12+
break;
13+
case 2:
14+
if (x == -4L) assert ((int)x >> 1) == -2;
15+
break;
16+
case 3:
17+
if (x == -4L) assert ((int)x >>> 1) == 2147483646;
18+
break;
19+
case 4:
20+
if (x == 1L) assert ((int)x << 0) == 1;
21+
break;
22+
case 5:
23+
if (x == 1L) assert ((int)x << 31) == -2147483648;
24+
break;
25+
case 6:
26+
if (x == 1L) assert ((int)x << 32) == 1;
27+
break;
28+
case 7:
29+
if (x == 1L) assert ((int)x << 33) == 2;
30+
break;
31+
case 8:
32+
if (x == 1L) assert (x << 0) == 1L;
33+
break;
34+
case 9:
35+
if (x == 1L) assert (x << 63) == -9223372036854775808L;
36+
break;
37+
case 10:
38+
if (x == 1L) assert (x << 64) == 1L;
39+
break;
40+
default:
41+
if (x == 1L) assert (x << 65) == 2L;
42+
break;
43+
}
944
}
1045
}
11-

jbmc/regression/jbmc/shift1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CORE
2-
shift1
2+
shift1.foo
33

44
^EXIT=0$
55
^SIGNAL=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)