hotspot/src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/Main.java
author kvn
Thu, 22 Jun 2017 15:26:34 -0700
changeset 46557 eb30f7891bae
parent 46344 694c102fd8ed
child 46640 70bdce04c59b
permissions -rw-r--r--
8181113: jaotc options lost after 8177046 Reviewed-by: iveresov, dlong
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     1
/*
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     4
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     8
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    13
 * accompanied this code).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    14
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    18
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    21
 * questions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    22
 */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    23
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    24
package jdk.tools.jaotc;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    25
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    26
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    27
import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    28
import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    29
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    30
import java.io.BufferedReader;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    31
import java.io.File;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    32
import java.io.FileNotFoundException;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    33
import java.io.FileReader;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    34
import java.io.FileWriter;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    35
import java.io.IOException;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    36
import java.io.InputStream;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    37
import java.io.InputStreamReader;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    38
import java.io.PrintWriter;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    39
import java.lang.management.ManagementFactory;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    40
import java.lang.management.MemoryUsage;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    41
import java.nio.file.Path;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    42
import java.nio.file.Paths;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    43
import java.text.MessageFormat;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    44
import java.util.ArrayList;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    45
import java.util.Date;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    46
import java.util.HashSet;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    47
import java.util.LinkedList;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    48
import java.util.List;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    49
import java.util.ListIterator;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    50
import java.util.Set;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    51
import java.util.stream.Stream;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    52
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    53
import jdk.tools.jaotc.binformat.BinaryContainer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    54
import jdk.tools.jaotc.binformat.ByteContainer;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    55
import jdk.tools.jaotc.collect.*;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    56
import jdk.tools.jaotc.collect.classname.ClassNameSourceProvider;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    57
import jdk.tools.jaotc.collect.directory.DirectorySourceProvider;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    58
import jdk.tools.jaotc.collect.jar.JarSourceProvider;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
    59
import jdk.tools.jaotc.collect.module.ModuleSourceProvider;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    60
import jdk.tools.jaotc.utils.Timer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    61
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    62
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    63
import org.graalvm.compiler.hotspot.CompilerConfigurationFactory;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    64
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    65
import org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    66
import org.graalvm.compiler.hotspot.HotSpotGraalOptionValues;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    67
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    68
import org.graalvm.compiler.hotspot.HotSpotHostBackend;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    69
import org.graalvm.compiler.java.GraphBuilderPhase;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    70
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    71
import org.graalvm.compiler.options.OptionValues;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    72
import org.graalvm.compiler.phases.BasePhase;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    73
import org.graalvm.compiler.phases.PhaseSuite;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    74
import org.graalvm.compiler.phases.tiers.HighTierContext;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    75
import org.graalvm.compiler.runtime.RuntimeProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    76
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    77
import jdk.vm.ci.meta.MetaAccessProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    78
import jdk.vm.ci.meta.ResolvedJavaMethod;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    79
import jdk.vm.ci.meta.ResolvedJavaType;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    80
import jdk.vm.ci.runtime.JVMCI;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    81
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    82
public class Main implements LogPrinter {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    83
    static class BadArgs extends Exception {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    84
        private static final long serialVersionUID = 1L;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    85
        final String key;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    86
        final Object[] args;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    87
        boolean showUsage;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    88
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    89
        BadArgs(String key, Object... args) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    90
            super(MessageFormat.format(key, args));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    91
            this.key = key;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    92
            this.args = args;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    93
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    94
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    95
        BadArgs showUsage(boolean b) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    96
            showUsage = b;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    97
            return this;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    98
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    99
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   100
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   101
    abstract static class Option {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   102
        final String help;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   103
        final boolean hasArg;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   104
        final String[] aliases;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   105
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   106
        Option(String help, boolean hasArg, String... aliases) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   107
            this.help = help;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   108
            this.hasArg = hasArg;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   109
            this.aliases = aliases;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   110
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   111
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   112
        boolean isHidden() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   113
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   114
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   115
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   116
        boolean matches(String opt) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   117
            for (String a : aliases) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   118
                if (a.equals(opt)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   119
                    return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   120
                } else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   121
                    return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   122
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   123
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   124
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   125
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   126
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   127
        boolean ignoreRest() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   128
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   129
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   130
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   131
        abstract void process(Main task, String opt, String arg) throws BadArgs;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   132
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   133
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   134
    static Option[] recognizedOptions = {new Option("  --output <file>            Output file name", true, "--output") {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   135
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   136
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   137
            String name = arg;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   138
            task.options.outputName = name;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   139
        }
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   140
    }, new Option("  --class-name <class names> List of classes to compile", true, "--class-name", "--classname") {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   141
        @Override
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   142
        void process(Main task, String opt, String arg) {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   143
            task.options.files.addAll(ClassSearch.makeList(ClassNameSourceProvider.TYPE, arg));
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   144
        }
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   145
    }, new Option("  --jar <jarfiles>           List of jar files to compile", true, "--jar") {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   146
        @Override
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   147
        void process(Main task, String opt, String arg) {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   148
            task.options.files.addAll(ClassSearch.makeList(JarSourceProvider.TYPE, arg));
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   149
        }
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   150
    }, new Option("  --module <modules>         List of modules to compile", true, "--module") {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   151
        @Override
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   152
        void process(Main task, String opt, String arg) {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   153
            task.options.files.addAll(ClassSearch.makeList(ModuleSourceProvider.TYPE, arg));
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   154
        }
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   155
    }, new Option("  --directory <dirs>         List of directories where to search for files to compile", true, "--directory") {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   156
        @Override
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   157
        void process(Main task, String opt, String arg) {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   158
            task.options.files.addAll(ClassSearch.makeList(DirectorySourceProvider.TYPE, arg));
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   159
        }
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   160
    }, new Option("  --search-path <dirs>       List of directories where to search for specified files", true, "--search-path") {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   161
        @Override
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   162
        void process(Main task, String opt, String arg) {
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   163
            String[] elements = arg.split(":");
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   164
            task.options.searchPath.add(elements);
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   165
        }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   166
    }, new Option("  --compile-commands <file>  Name of file with compile commands", true, "--compile-commands") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   167
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   168
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   169
            task.options.methodList = arg;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   170
        }
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   171
    }, new Option("  --compile-for-tiered       Generate profiling code for tiered compilation", false, "--compile-for-tiered") {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   172
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   173
        void process(Main task, String opt, String arg) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   174
            task.options.tiered = true;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   175
        }
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   176
    }, new Option("  --compile-with-assertions  Compile with java assertions", false, "--compile-with-assertions") {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   177
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   178
        void process(Main task, String opt, String arg) {
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   179
            task.options.compileWithAssertions = true;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   180
        }
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   181
    }, new Option("  --compile-threads <number> Number of compilation threads to be used", true, "--compile-threads", "--threads") {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   182
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   183
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   184
            int threads = Integer.parseInt(arg);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   185
            final int available = Runtime.getRuntime().availableProcessors();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   186
            if (threads <= 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   187
                task.warning("invalid number of threads specified: {0}, using: {1}", threads, available);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   188
                threads = available;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   189
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   190
            if (threads > available) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   191
                task.warning("too many threads specified: {0}, limiting to: {1}", threads, available);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   192
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   193
            task.options.threads = Integer.min(threads, available);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   194
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   195
    }, new Option("  --ignore-errors            Ignores all exceptions thrown during class loading", false, "--ignore-errors") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   196
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   197
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   198
            task.options.ignoreClassLoadingErrors = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   199
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   200
    }, new Option("  --exit-on-error            Exit on compilation errors", false, "--exit-on-error") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   201
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   202
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   203
            task.options.exitOnError = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   204
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   205
    }, new Option("  --info                     Print information during compilation", false, "--info") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   206
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   207
        void process(Main task, String opt, String arg) throws BadArgs {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   208
            task.options.info = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   209
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   210
    }, new Option("  --verbose                  Print verbose information", false, "--verbose") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   211
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   212
        void process(Main task, String opt, String arg) throws BadArgs {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   213
            task.options.info = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   214
            task.options.verbose = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   215
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   216
    }, new Option("  --debug                    Print debug information", false, "--debug") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   217
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   218
        void process(Main task, String opt, String arg) throws BadArgs {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   219
            task.options.info = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   220
            task.options.verbose = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   221
            task.options.debug = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   222
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   223
    }, new Option("  --help                     Print this usage message", false, "--help") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   224
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   225
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   226
            task.options.help = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   227
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   228
    }, new Option("  --version                  Version information", false, "--version") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   229
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   230
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   231
            task.options.version = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   232
        }
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   233
    }, new Option("  --linker-path              Full path to linker executable", true, "--linker-path") {
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   234
        @Override
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   235
        void process(Main task, String opt, String arg) {
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   236
            task.options.linkerpath = arg;
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   237
        }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   238
    }, new Option("  -J<flag>                   Pass <flag> directly to the runtime system", false, "-J") {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   239
        @Override
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   240
        void process(Main task, String opt, String arg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   241
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   242
    }};
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   243
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   244
    public static class Options {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   245
        public List<SearchFor> files = new LinkedList<>();
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   246
        public String outputName = "unnamed.so";
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   247
        public String methodList;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   248
        public List<ClassSource> sources = new ArrayList<>();
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   249
        public String linkerpath = null;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   250
        public SearchPath searchPath = new SearchPath();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   251
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   252
        /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   253
         * We don't see scaling beyond 16 threads.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   254
         */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   255
        private static final int COMPILER_THREADS = 16;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   256
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   257
        public int threads = Integer.min(COMPILER_THREADS, Runtime.getRuntime().availableProcessors());
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   258
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   259
        public boolean ignoreClassLoadingErrors;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   260
        public boolean exitOnError;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   261
        public boolean info;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   262
        public boolean verbose;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   263
        public boolean debug;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   264
        public boolean help;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   265
        public boolean version;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   266
        public boolean compileWithAssertions;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   267
        public boolean tiered;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   268
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   269
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   270
    /* package */final Options options = new Options();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   271
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   272
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   273
     * Logfile.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   274
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   275
    private static FileWriter logFile = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   276
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   277
    private static final int EXIT_OK = 0;        // No errors.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   278
    private static final int EXIT_CMDERR = 2;    // Bad command-line arguments and/or switches.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   279
    private static final int EXIT_ABNORMAL = 4;  // Terminated abnormally.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   280
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   281
    private static final String PROGNAME = "jaotc";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   282
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   283
    private static final String JVM_VERSION = System.getProperty("java.runtime.version");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   284
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   285
    public static void main(String[] args) throws Exception {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   286
        Main t = new Main();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   287
        final int exitCode = t.run(args);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   288
        System.exit(exitCode);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   289
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   290
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   291
    private int run(String[] args) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   292
        if (log == null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   293
            log = new PrintWriter(System.out);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   294
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   295
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   296
        try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   297
            handleOptions(args);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   298
            if (options.help) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   299
                showHelp();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   300
                return EXIT_OK;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   301
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   302
            if (options.version) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   303
                showVersion();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   304
                return EXIT_OK;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   305
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   306
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   307
            printlnInfo("Compiling " + options.outputName + "...");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   308
            final long start = System.currentTimeMillis();
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   309
            if (!run()) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   310
                return EXIT_ABNORMAL;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   311
            }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   312
            final long end = System.currentTimeMillis();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   313
            printlnInfo("Total time: " + (end - start) + " ms");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   314
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   315
            return EXIT_OK;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   316
        } catch (BadArgs e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   317
            reportError(e.key, e.args);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   318
            if (e.showUsage) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   319
                showUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   320
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   321
            return EXIT_CMDERR;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   322
        } catch (Exception e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   323
            e.printStackTrace();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   324
            return EXIT_ABNORMAL;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   325
        } finally {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   326
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   327
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   328
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   329
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   330
    private static String humanReadableByteCount(long bytes) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   331
        int unit = 1024;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   332
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   333
        if (bytes < unit) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   334
            return bytes + " B";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   335
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   336
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   337
        int exp = (int) (Math.log(bytes) / Math.log(unit));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   338
        char pre = "KMGTPE".charAt(exp - 1);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   339
        return String.format("%.1f %cB", bytes / Math.pow(unit, exp), pre);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   340
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   341
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   342
    void printMemoryUsage() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   343
        if (options.verbose) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   344
            MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   345
            float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   346
            log.format(" [used: %-7s, comm: %-7s, freeRatio ~= %.1f%%]",
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   347
                            humanReadableByteCount(memusage.getUsed()),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   348
                            humanReadableByteCount(memusage.getCommitted()),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   349
                            freeratio * 100);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   350
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   351
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   352
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   353
    /**
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   354
     * Visual Studio supported versions Search Order is: VS2013, VS2015, VS2012
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   355
     */
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   356
    public enum VSVERSIONS {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   357
        VS2013("VS120COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64\\link.exe"),
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   358
        VS2015("VS140COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\link.exe"),
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   359
        VS2012("VS110COMNTOOLS", "C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\bin\\amd64\\link.exe");
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   360
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   361
        private final String envvariable;
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   362
        private final String wkp;
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   363
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   364
        VSVERSIONS(String envvariable, String wellknownpath) {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   365
            this.envvariable = envvariable;
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   366
            this.wkp = wellknownpath;
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   367
        }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   368
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   369
        String EnvVariable() {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   370
            return envvariable;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   371
        }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   372
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   373
        String WellKnownPath() {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   374
            return wkp;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   375
        }
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   376
    }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   377
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   378
    /**
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   379
     * Search for Visual Studio link.exe Search Order is: VS2013, VS2015, VS2012
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   380
     */
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   381
    private static String getWindowsLinkPath() {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   382
        String link = "\\VC\\bin\\amd64\\link.exe";
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   383
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   384
        /**
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   385
         * First try searching the paths pointed to by the VS environment variables.
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   386
         */
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   387
        for (VSVERSIONS vs : VSVERSIONS.values()) {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   388
            String vspath = System.getenv(vs.EnvVariable());
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   389
            if (vspath != null) {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   390
                File commonTools = new File(vspath);
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   391
                File vsRoot = commonTools.getParentFile().getParentFile();
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   392
                File linkPath = new File(vsRoot, link);
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   393
                if (linkPath.exists())
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   394
                    return linkPath.getPath();
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   395
            }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   396
        }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   397
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   398
        /**
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   399
         * If we didn't find via the VS environment variables, try the well known paths
46267
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   400
         */
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   401
        for (VSVERSIONS vs : VSVERSIONS.values()) {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   402
            String wkp = vs.WellKnownPath();
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   403
            if (new File(wkp).exists()) {
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   404
                return wkp;
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   405
            }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   406
        }
dde19b81b7fd 8174203: Enable AOT Jtreg tests on Windows x86_64
bobv
parents: 46260
diff changeset
   407
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   408
        return null;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   409
    }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   410
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   411
    @SuppressWarnings("try")
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   412
    private boolean run() throws Exception {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   413
        openLog();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   414
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   415
        try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   416
            CompilationSpec compilationRestrictions = collectSpecifiedMethods();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   417
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   418
            Set<Class<?>> classesToCompile = new HashSet<>();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   419
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   420
            try (Timer t = new Timer(this, "")) {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   421
                FileSupport fileSupport = new FileSupport();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   422
                ClassSearch lookup = new ClassSearch();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   423
                lookup.addProvider(new ModuleSourceProvider());
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   424
                lookup.addProvider(new ClassNameSourceProvider(fileSupport));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   425
                lookup.addProvider(new JarSourceProvider());
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   426
                lookup.addProvider(new DirectorySourceProvider(fileSupport));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   427
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   428
                List<LoadedClass> found = null;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   429
                try {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   430
                    found = lookup.search(options.files, options.searchPath);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   431
                } catch (InternalError e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   432
                    reportError(e);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   433
                    return false;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   434
                }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   435
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   436
                for (LoadedClass loadedClass : found) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   437
                    classesToCompile.add(loadedClass.getLoadedClass());
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   438
                }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   439
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   440
                printInfo(classesToCompile.size() + " classes found");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   441
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   442
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   443
            OptionValues graalOptions = HotSpotGraalOptionValues.HOTSPOT_OPTIONS;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   444
            // Setting -Dgraal.TieredAOT overrides --compile-for-tiered
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   445
            if (!TieredAOT.hasBeenSet(graalOptions)) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   446
                graalOptions = new OptionValues(graalOptions, TieredAOT, options.tiered);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   447
            }
46557
eb30f7891bae 8181113: jaotc options lost after 8177046
kvn
parents: 46344
diff changeset
   448
            graalOptions = new OptionValues(graalOptions, GeneratePIC, true, ImmutableCode, true);
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   449
            GraalJVMCICompiler graalCompiler = HotSpotGraalCompilerFactory.createCompiler(JVMCI.getRuntime(), graalOptions, CompilerConfigurationFactory.selectFactory(null, graalOptions));
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   450
            HotSpotGraalRuntimeProvider runtime = (HotSpotGraalRuntimeProvider) graalCompiler.getGraalRuntime();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   451
            HotSpotHostBackend backend = (HotSpotHostBackend) runtime.getCapability(RuntimeProvider.class).getHostBackend();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   452
            MetaAccessProvider metaAccess = backend.getProviders().getMetaAccess();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   453
            GraalFilters filters = new GraalFilters(metaAccess);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   454
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   455
            List<AOTCompiledClass> classes;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   456
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   457
            try (Timer t = new Timer(this, "")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   458
                classes = collectMethodsToCompile(classesToCompile, compilationRestrictions, filters, metaAccess);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   459
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   460
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   461
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   462
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   463
                printMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   464
                compilationRestrictions = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   465
                classesToCompile = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   466
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   467
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   468
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   469
            AOTBackend aotBackend = new AOTBackend(this, graalOptions, backend, filters);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   470
            AOTCompiler compiler = new AOTCompiler(this, graalOptions, aotBackend, options.threads);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   471
            classes = compiler.compileClasses(classes);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   472
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   473
            GraalHotSpotVMConfig graalHotSpotVMConfig = runtime.getVMConfig();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   474
            PhaseSuite<HighTierContext> graphBuilderSuite = aotBackend.getGraphBuilderSuite();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   475
            ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class);
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   476
            GraphBuilderConfiguration graphBuilderConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   477
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   478
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   479
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   480
                printMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   481
                aotBackend = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   482
                compiler = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   483
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   484
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   485
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   486
            BinaryContainer binaryContainer = new BinaryContainer(graalOptions, graalHotSpotVMConfig, graphBuilderConfig, JVM_VERSION);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   487
            DataBuilder dataBuilder = new DataBuilder(this, backend, classes, binaryContainer);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   488
            dataBuilder.prepareData();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   489
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   490
            // Print information about section sizes
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   491
            printContainerInfo(binaryContainer.getHeaderContainer().getContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   492
            printContainerInfo(binaryContainer.getConfigContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   493
            printContainerInfo(binaryContainer.getKlassesOffsetsContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   494
            printContainerInfo(binaryContainer.getMethodsOffsetsContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   495
            printContainerInfo(binaryContainer.getKlassesDependenciesContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   496
            printContainerInfo(binaryContainer.getStubsOffsetsContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   497
            printContainerInfo(binaryContainer.getMethodMetadataContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   498
            printContainerInfo(binaryContainer.getCodeContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   499
            printContainerInfo(binaryContainer.getCodeSegmentsContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   500
            printContainerInfo(binaryContainer.getConstantDataContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   501
            printContainerInfo(binaryContainer.getMetaspaceGotContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   502
            printContainerInfo(binaryContainer.getMetadataGotContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   503
            printContainerInfo(binaryContainer.getMethodStateContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   504
            printContainerInfo(binaryContainer.getOopGotContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   505
            printContainerInfo(binaryContainer.getMetaspaceNamesContainer());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   506
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   507
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   508
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   509
                printMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   510
                backend = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   511
                for (AOTCompiledClass aotCompClass : classes) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   512
                    aotCompClass.clear();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   513
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   514
                classes.clear();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   515
                classes = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   516
                dataBuilder = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   517
                binaryContainer.freeMemory();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   518
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   519
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   520
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   521
            String name = options.outputName;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   522
            String objectFileName = name;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   523
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   524
            // [TODO] The jtregs tests expect .so extension so don't
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   525
            // override with platform specific file extension until the
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   526
            // tests are fixed.
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   527
            String libraryFileName = name;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   528
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   529
            String linkerCmd;
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   530
            String linkerPath;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   531
            String osName = System.getProperty("os.name");
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   532
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   533
            if (name.endsWith(".so")) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   534
                objectFileName = name.substring(0, name.length() - ".so".length());
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   535
            } else if (name.endsWith(".dylib")) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   536
                objectFileName = name.substring(0, name.length() - ".dylib".length());
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   537
            } else if (name.endsWith(".dll")) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   538
                objectFileName = name.substring(0, name.length() - ".dll".length());
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   539
            }
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   540
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   541
            switch (osName) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   542
                case "Linux":
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   543
                    // libraryFileName = options.outputName + ".so";
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   544
                    objectFileName = objectFileName + ".o";
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   545
                    linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   546
                    linkerCmd = linkerPath + " -shared -z noexecstack -o " + libraryFileName + " " + objectFileName;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   547
                    break;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   548
                case "SunOS":
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   549
                    // libraryFileName = options.outputName + ".so";
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   550
                    objectFileName = objectFileName + ".o";
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   551
                    linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   552
                    linkerCmd = linkerPath + " -shared -o " + libraryFileName + " " + objectFileName;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   553
                    break;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   554
                case "Mac OS X":
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   555
                    // libraryFileName = options.outputName + ".dylib";
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   556
                    objectFileName = objectFileName + ".o";
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   557
                    linkerPath = (options.linkerpath != null) ? options.linkerpath : "ld";
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   558
                    linkerCmd = linkerPath + " -dylib -o " + libraryFileName + " " + objectFileName;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   559
                    break;
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   560
                default:
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   561
                    if (osName.startsWith("Windows")) {
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   562
                        // libraryFileName = options.outputName + ".dll";
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   563
                        objectFileName = objectFileName + ".obj";
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   564
                        linkerPath = (options.linkerpath != null) ? options.linkerpath : getWindowsLinkPath();
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   565
                        if (linkerPath == null) {
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   566
                            throw new InternalError("Can't locate Microsoft Visual Studio amd64 link.exe");
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   567
                        }
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   568
                        linkerCmd = linkerPath + " /DLL /OPT:NOREF /NOLOGO /NOENTRY" + " /OUT:" + libraryFileName + " " + objectFileName;
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   569
                        break;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   570
                    } else
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   571
                        throw new InternalError("Unsupported platform: " + osName);
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   572
            }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   573
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   574
            try (Timer t = new Timer(this, "Creating binary: " + objectFileName)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   575
                binaryContainer.createBinary(objectFileName, JVM_VERSION);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   576
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   577
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   578
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   579
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   580
                printMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   581
                binaryContainer = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   582
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   583
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   584
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   585
            try (Timer t = new Timer(this, "Creating shared library: " + libraryFileName)) {
46275
d27ab535d88c 8174863: AOT: jaotc should provide an option to specify the path to the platform linker
bobv
parents: 46267
diff changeset
   586
                Process p = Runtime.getRuntime().exec(linkerCmd);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   587
                final int exitCode = p.waitFor();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   588
                if (exitCode != 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   589
                    InputStream stderr = p.getErrorStream();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   590
                    BufferedReader br = new BufferedReader(new InputStreamReader(stderr));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   591
                    Stream<String> lines = br.lines();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   592
                    StringBuilder sb = new StringBuilder();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   593
                    lines.iterator().forEachRemaining(e -> sb.append(e));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   594
                    throw new InternalError(sb.toString());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   595
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   596
                File objFile = new File(objectFileName);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   597
                if (objFile.exists()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   598
                    if (!objFile.delete()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   599
                        throw new InternalError("Failed to delete " + objectFileName + " file");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   600
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   601
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   602
                // Make non-executable for all.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   603
                File libFile = new File(libraryFileName);
46260
5de61384fba6 8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents: 42650
diff changeset
   604
                if (libFile.exists() && !osName.startsWith("Windows")) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   605
                    if (!libFile.setExecutable(false, false)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   606
                        throw new InternalError("Failed to change attribute for " + libraryFileName + " file");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   607
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   608
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   609
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   610
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   611
            printVerbose("Final memory  ");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   612
            printMemoryUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   613
            printlnVerbose("");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   614
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   615
        } finally {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   616
            closeLog();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   617
        }
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   618
        return true;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   619
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   620
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   621
    private void addMethods(AOTCompiledClass aotClass, ResolvedJavaMethod[] methods, CompilationSpec compilationRestrictions, GraalFilters filters) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   622
        for (ResolvedJavaMethod m : methods) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   623
            addMethod(aotClass, m, compilationRestrictions, filters);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   624
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   625
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   626
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   627
    private void addMethod(AOTCompiledClass aotClass, ResolvedJavaMethod method, CompilationSpec compilationRestrictions, GraalFilters filters) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   628
        // Don't compile native or abstract methods.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   629
        if (!method.hasBytecodes()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   630
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   631
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   632
        if (!compilationRestrictions.shouldCompileMethod(method)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   633
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   634
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   635
        if (!filters.shouldCompileMethod(method)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   636
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   637
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   638
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   639
        aotClass.addMethod(method);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   640
        printlnVerbose("  added " + method.getName() + method.getSignature().toMethodDescriptor());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   641
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   642
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   643
    private void printContainerInfo(ByteContainer container) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   644
        printlnVerbose(container.getContainerName() + ": " + container.getByteStreamSize() + " bytes");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   645
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   646
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   647
    PrintWriter log;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   648
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   649
    private void handleOptions(String[] args) throws BadArgs {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   650
        if (args.length == 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   651
            options.help = true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   652
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   653
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   654
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   655
        // Make checkstyle happy.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   656
        int i = 0;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   657
        for (; i < args.length; i++) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   658
            String arg = args[i];
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   659
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   660
            if (arg.charAt(0) == '-') {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   661
                Option option = getOption(arg);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   662
                String param = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   663
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   664
                if (option.hasArg) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   665
                    if (arg.startsWith("--") && arg.indexOf('=') > 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   666
                        param = arg.substring(arg.indexOf('=') + 1, arg.length());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   667
                    } else if (i + 1 < args.length) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   668
                        param = args[++i];
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   669
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   670
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   671
                    if (param == null || param.isEmpty() || param.charAt(0) == '-') {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   672
                        throw new BadArgs("missing argument for option: {0}", arg).showUsage(true);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   673
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   674
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   675
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   676
                option.process(this, arg, param);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   677
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   678
                if (option.ignoreRest()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   679
                    break;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   680
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   681
            } else {
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   682
                options.files.add(new SearchFor(arg));
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   683
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   684
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   685
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   686
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   687
    private static Option getOption(String name) throws BadArgs {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   688
        for (Option o : recognizedOptions) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   689
            if (o.matches(name)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   690
                return o;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   691
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   692
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   693
        throw new BadArgs("unknown option: {0}", name).showUsage(true);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   694
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   695
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   696
    public void printInfo(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   697
        if (options.info) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   698
            log.print(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   699
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   700
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   701
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   702
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   703
    public void printlnInfo(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   704
        if (options.info) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   705
            log.println(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   706
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   707
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   708
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   709
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   710
    public void printVerbose(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   711
        if (options.verbose) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   712
            log.print(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   713
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   714
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   715
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   716
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   717
    public void printlnVerbose(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   718
        if (options.verbose) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   719
            log.println(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   720
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   721
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   722
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   723
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   724
    public void printDebug(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   725
        if (options.debug) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   726
            log.print(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   727
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   728
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   729
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   730
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   731
    public void printlnDebug(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   732
        if (options.debug) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   733
            log.println(message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   734
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   735
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   736
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   737
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   738
    public void printError(String message) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   739
        log.println("Error: " + message);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   740
        log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   741
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   742
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   743
    private void reportError(Throwable e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   744
        log.println("Error: " + e.getMessage());
43974
309b569feebb 8175052: [AOT] jaotc does not accept file name with .class
kvn
parents: 43479
diff changeset
   745
        if (options.info) {
309b569feebb 8175052: [AOT] jaotc does not accept file name with .class
kvn
parents: 43479
diff changeset
   746
            e.printStackTrace(log);
309b569feebb 8175052: [AOT] jaotc does not accept file name with .class
kvn
parents: 43479
diff changeset
   747
        }
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   748
        log.flush();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   749
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   750
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   751
    private void reportError(String key, Object... args) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   752
        printError(MessageFormat.format(key, args));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   753
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   754
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   755
    private void warning(String key, Object... args) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   756
        log.println("Warning: " + MessageFormat.format(key, args));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   757
        log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   758
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   759
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   760
    private void showUsage() {
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   761
        log.println("Usage: " + PROGNAME + " <options> list");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   762
        log.println("use --help for a list of possible options");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   763
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   764
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   765
    private void showHelp() {
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   766
        log.println("Usage: " + PROGNAME + " <options> list");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   767
        log.println();
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   768
        log.println("  list       A : separated list of class names, modules, jar files");
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   769
        log.println("             or directories which contain class files.");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   770
        log.println();
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   771
        log.println("where options include:");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   772
        for (Option o : recognizedOptions) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   773
            String name = o.aliases[0].substring(1); // there must always be at least one name
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   774
            name = name.charAt(0) == '-' ? name.substring(1) : name;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   775
            if (o.isHidden() || name.equals("h")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   776
                continue;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   777
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   778
            log.println(o.help);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   779
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   780
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   781
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   782
    private void showVersion() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   783
        log.println(PROGNAME + " " + JVM_VERSION);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   784
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   785
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   786
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   787
     * Collect all method we should compile.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   788
     *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   789
     * @return array list of AOT classes which have compiled methods.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   790
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   791
    private List<AOTCompiledClass> collectMethodsToCompile(Set<Class<?>> classesToCompile, CompilationSpec compilationRestrictions, GraalFilters filters, MetaAccessProvider metaAccess) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   792
        int total = 0;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   793
        int count = 0;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   794
        List<AOTCompiledClass> classes = new ArrayList<>();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   795
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   796
        for (Class<?> c : classesToCompile) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   797
            ResolvedJavaType resolvedJavaType = metaAccess.lookupJavaType(c);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   798
            if (filters.shouldCompileAnyMethodInClass(resolvedJavaType)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   799
                AOTCompiledClass aotClass = new AOTCompiledClass(resolvedJavaType);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   800
                printlnVerbose(" Scanning " + c.getName());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   801
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   802
                // Constructors
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   803
                try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   804
                    ResolvedJavaMethod[] ctors = resolvedJavaType.getDeclaredConstructors();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   805
                    addMethods(aotClass, ctors, compilationRestrictions, filters);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   806
                    total += ctors.length;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   807
                } catch (Throwable e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   808
                    // If we are running in JCK mode we ignore all exceptions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   809
                    if (options.ignoreClassLoadingErrors) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   810
                        printError(c.getName() + ": " + e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   811
                    } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   812
                        throw new InternalError(e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   813
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   814
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   815
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   816
                // Methods
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   817
                try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   818
                    ResolvedJavaMethod[] methods = resolvedJavaType.getDeclaredMethods();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   819
                    addMethods(aotClass, methods, compilationRestrictions, filters);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   820
                    total += methods.length;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   821
                } catch (Throwable e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   822
                    // If we are running in JCK mode we ignore all exceptions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   823
                    if (options.ignoreClassLoadingErrors) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   824
                        printError(c.getName() + ": " + e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   825
                    } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   826
                        throw new InternalError(e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   827
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   828
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   829
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   830
                // Class initializer
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   831
                try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   832
                    ResolvedJavaMethod clinit = resolvedJavaType.getClassInitializer();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   833
                    if (clinit != null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   834
                        addMethod(aotClass, clinit, compilationRestrictions, filters);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   835
                        total++;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   836
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   837
                } catch (Throwable e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   838
                    // If we are running in JCK mode we ignore all exceptions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   839
                    if (options.ignoreClassLoadingErrors) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   840
                        printError(c.getName() + ": " + e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   841
                    } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   842
                        throw new InternalError(e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   843
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   844
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   845
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   846
                // Found any methods to compile? Add the class.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   847
                if (aotClass.hasMethods()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   848
                    classes.add(aotClass);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   849
                    count += aotClass.getMethodCount();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   850
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   851
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   852
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   853
        printInfo(total + " methods total, " + count + " methods to compile");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   854
        return classes;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   855
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   856
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   857
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   858
     * If a file with compilation limitations is specified using the java property
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   859
     * jdk.tools.jaotc.compile.method.list, read the file's contents and collect the restrictions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   860
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   861
    private CompilationSpec collectSpecifiedMethods() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   862
        CompilationSpec compilationRestrictions = new CompilationSpec();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   863
        String methodListFileName = options.methodList;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   864
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   865
        if (methodListFileName != null && !methodListFileName.equals("")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   866
            try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   867
                FileReader methListFile = new FileReader(methodListFileName);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   868
                BufferedReader readBuf = new BufferedReader(methListFile);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   869
                String line = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   870
                while ((line = readBuf.readLine()) != null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   871
                    String trimmedLine = line.trim();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   872
                    if (!trimmedLine.startsWith("#")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   873
                        String[] components = trimmedLine.split(" ");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   874
                        if (components.length == 2) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   875
                            String directive = components[0];
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   876
                            String pattern = components[1];
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   877
                            switch (directive) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   878
                                case "compileOnly":
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   879
                                    compilationRestrictions.addCompileOnlyPattern(pattern);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   880
                                    break;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   881
                                case "exclude":
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   882
                                    compilationRestrictions.addExcludePattern(pattern);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   883
                                    break;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   884
                                default:
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   885
                                    System.out.println("Unrecognized command " + directive + ". Ignoring\n\t" + line + "\n encountered in " + methodListFileName);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   886
                            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   887
                        } else {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   888
                            if (!trimmedLine.equals("")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   889
                                System.out.println("Ignoring malformed line:\n\t " + line + "\n");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   890
                            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   891
                        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   892
                    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   893
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   894
                readBuf.close();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   895
            } catch (FileNotFoundException e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   896
                throw new InternalError("Unable to open method list file: " + methodListFileName, e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   897
            } catch (IOException e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   898
                throw new InternalError("Unable to read method list file: " + methodListFileName, e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   899
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   900
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   901
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   902
        return compilationRestrictions;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   903
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   904
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   905
    private static void openLog() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   906
        int v = Integer.getInteger("jdk.tools.jaotc.logCompilation", 0);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   907
        if (v == 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   908
            logFile = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   909
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   910
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   911
        // Create log file in current directory
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   912
        String fileName = "aot_compilation" + new Date().getTime() + ".log";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   913
        Path logFilePath = Paths.get("./", fileName);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   914
        String logFileName = logFilePath.toString();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   915
        try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   916
            // Create file to which we do not append
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   917
            logFile = new FileWriter(logFileName, false);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   918
        } catch (IOException e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   919
            System.out.println("Unable to open logfile :" + logFileName + "\nNo logs will be created");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   920
            logFile = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   921
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   922
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   923
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   924
    public static void writeLog(String str) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   925
        if (logFile != null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   926
            try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   927
                logFile.write(str + "\n");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   928
                logFile.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   929
            } catch (IOException e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   930
                // Print to console
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   931
                System.out.println(str + "\n");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   932
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   933
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   934
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   935
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   936
    public static void closeLog() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   937
        if (logFile != null) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   938
            try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   939
                logFile.close();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   940
            } catch (IOException e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   941
                // Do nothing
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   942
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   943
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   944
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   945
}