author | mcimadamore |
Wed, 26 Oct 2016 15:41:25 +0100 | |
changeset 41856 | 13a056e8f16e |
parent 36778 | e04318f39f92 |
permissions | -rw-r--r-- |
23791 | 1 |
/* |
36526 | 2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
23791 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 8025505 |
|
27 |
* @summary Constant folding deficiency |
|
26100 | 28 |
* @library /tools/lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28340
diff
changeset
|
29 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
28340
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.main |
36526 | 31 |
* jdk.jdeps/com.sun.tools.javap |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
32 |
* @build toolbox.ToolBox toolbox.JavapTask |
23791 | 33 |
* @run main ConstFoldTest |
34 |
*/ |
|
35 |
||
36 |
import java.net.URL; |
|
28340 | 37 |
import java.nio.file.Path; |
38 |
import java.nio.file.Paths; |
|
23791 | 39 |
import java.util.List; |
40 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
41 |
import toolbox.JavapTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
42 |
import toolbox.Task; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
43 |
import toolbox.ToolBox; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
44 |
|
23791 | 45 |
public class ConstFoldTest { |
46 |
public static void main(String... args) throws Exception { |
|
47 |
new ConstFoldTest().run(); |
|
48 |
} |
|
49 |
||
50 |
// This is the test case. This class should end up |
|
51 |
// as straight-line code with no conditionals |
|
52 |
class CFTest { |
|
53 |
void m() { |
|
54 |
int x; |
|
55 |
if (1 != 2) x=1; else x=0; |
|
56 |
if (1 == 2) x=1; else x=0; |
|
26100 | 57 |
if ("" != null) x=1; else x=0; |
58 |
if ("" == null) x=1; else x=0; |
|
23791 | 59 |
if (null == null) x=1; else x=0; |
60 |
if (null != null) x=1; else x=0; |
|
61 |
||
62 |
x = 1 != 2 ? 1 : 0; |
|
63 |
x = 1 == 2 ? 1 : 0; |
|
26100 | 64 |
x = "" != null ? 1 : 0; |
65 |
x = "" == null ? 1 : 0; |
|
23791 | 66 |
x = null == null ? 1 : 0; |
67 |
x = null != null ? 1 : 0; |
|
68 |
||
69 |
boolean b; |
|
70 |
b = 1 != 2 && true; |
|
71 |
b = 1 == 2 || true; |
|
26100 | 72 |
b = ("" != null) && true; |
73 |
b = ("" == null) || true; |
|
23791 | 74 |
b = (null == null) && true; |
75 |
b = (null != null) || true; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
// All of the conditionals above should be eliminated. |
|
80 |
// these if* bytecodes should not be seen |
|
81 |
final String regex = "\\sif(?:null|nonnull|eq|ne){1}\\s"; |
|
82 |
||
83 |
void run() throws Exception { |
|
26100 | 84 |
ToolBox tb = new ToolBox(); |
85 |
||
23791 | 86 |
URL url = ConstFoldTest.class.getResource("ConstFoldTest$CFTest.class"); |
28340 | 87 |
Path file = Paths.get(url.toURI()); |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
88 |
List<String> result = new JavapTask(tb) |
26100 | 89 |
.options("-c") |
28340 | 90 |
.classes(file.toString()) |
26100 | 91 |
.run() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
92 |
.write(Task.OutputKind.DIRECT) |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
93 |
.getOutputLines(Task.OutputKind.DIRECT); |
23791 | 94 |
|
26100 | 95 |
List<String> bad_codes = tb.grep(regex, result); |
23791 | 96 |
if (!bad_codes.isEmpty()) { |
97 |
for (String code : bad_codes) |
|
98 |
System.out.println("Bad OpCode Found: " + code); |
|
99 |
throw new Exception("constant folding failed"); |
|
100 |
} |
|
101 |
} |
|
102 |
} |