langtools/test/tools/javac/ConstFoldTest.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 23791 f1cdccbc1040
child 26100 bb7dd001d190
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23791
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     1
/*
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    28
 * @library /tools/javac/lib
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    29
 * @build ToolBox
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    30
 * @run main ConstFoldTest
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    31
 */
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    32
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    33
import java.net.URL;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    34
import java.util.List;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    35
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    36
public class ConstFoldTest {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    38
        new ConstFoldTest().run();
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    39
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    40
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    41
    // This is the test case. This class should end up
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    42
    // as straight-line code with no conditionals
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    43
    class CFTest {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    44
        void m() {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    45
            int x;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    46
            if (1 != 2)       x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    47
            if (1 == 2)       x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    48
            if ("" != null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    49
            if ("" == null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    50
            if (null == null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    51
            if (null != null) x=1; else x=0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    52
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    53
            x = 1 != 2        ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    54
            x = 1 == 2        ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    55
            x = "" != null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    56
            x = "" == null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    57
            x = null == null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    58
            x = null != null  ? 1 : 0;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    59
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    60
            boolean b;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    61
            b = 1 != 2         && true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    62
            b = 1 == 2         || true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    63
            b = ("" != null) && true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    64
            b = ("" == null) || true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    65
            b = (null == null) && true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    66
            b = (null != null) || true;
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    67
        }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    68
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    69
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    70
    // All of the conditionals above should be eliminated.
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    71
    // these if* bytecodes should not be seen
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    72
    final String regex = "\\sif(?:null|nonnull|eq|ne){1}\\s";
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    73
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    74
    void run() throws Exception {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    75
        URL url = ConstFoldTest.class.getResource("ConstFoldTest$CFTest.class");
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    76
        String result = ToolBox.javap(new ToolBox.JavaToolArgs().setAllArgs("-c", url.getFile()));
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    77
        System.out.println(result);
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    78
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    79
        List<String> bad_codes = ToolBox.grep(regex, result, "\n");
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    80
        if (!bad_codes.isEmpty()) {
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    81
            for (String code : bad_codes)
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    82
                System.out.println("Bad OpCode Found: " + code);
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    83
            throw new Exception("constant folding failed");
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    84
        }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    85
    }
f1cdccbc1040 8025505: Constant folding deficiency
pgovereau
parents:
diff changeset
    86
}