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