src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/Main.java
author dlong
Tue, 24 Apr 2018 09:04:57 -0700
changeset 49873 26ebfe8ce852
parent 47668 fc4cfca10556
child 51206 14b870bda24f
permissions -rw-r--r--
8199755: Update Graal Reviewed-by: kvn
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.PrintWriter;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    31
import java.text.MessageFormat;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    32
import java.util.List;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    33
import java.util.ListIterator;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    34
import java.util.Set;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    35
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    36
import jdk.tools.jaotc.binformat.BinaryContainer;
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    37
import jdk.tools.jaotc.Options.Option;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    38
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
    39
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    40
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler;
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
    41
import org.graalvm.compiler.debug.DebugContext;
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
    42
import org.graalvm.compiler.debug.DebugContext.Activation;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    43
import org.graalvm.compiler.hotspot.CompilerConfigurationFactory;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    44
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    45
import org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    46
import org.graalvm.compiler.hotspot.HotSpotGraalOptionValues;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    47
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    48
import org.graalvm.compiler.hotspot.HotSpotHostBackend;
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
    49
import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    50
import org.graalvm.compiler.java.GraphBuilderPhase;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    51
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
    52
import org.graalvm.compiler.options.OptionValues;
43405
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    53
import org.graalvm.compiler.phases.BasePhase;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    54
import org.graalvm.compiler.phases.PhaseSuite;
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
    55
import org.graalvm.compiler.phases.tiers.HighTierContext;
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
    56
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    57
import org.graalvm.compiler.runtime.RuntimeProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    58
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    59
import jdk.vm.ci.meta.MetaAccessProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    60
import jdk.vm.ci.meta.ResolvedJavaMethod;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    61
import jdk.vm.ci.runtime.JVMCI;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    62
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    63
public final class Main {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    64
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    65
    final Options options = new Options();
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    66
    private PrintWriter log;
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    67
    LogPrinter printer;
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    68
    GraalFilters filters;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    69
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    70
    private static final int EXIT_OK = 0;        // No errors.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    71
    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
    72
    private static final int EXIT_ABNORMAL = 4;  // Terminated abnormally.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    73
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    74
    private static final String PROGNAME = "jaotc";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    75
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    76
    private static final String JVM_VERSION = System.getProperty("java.runtime.version");
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    77
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    78
    public static void main(String[] args) throws Exception {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    79
        Main t = new Main();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    80
        final int exitCode = t.run(args);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    81
        System.exit(exitCode);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    82
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    83
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    84
    private int run(String[] args) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    85
        log = new PrintWriter(System.out);
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    86
        printer = new LogPrinter(this, log);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    87
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    88
        try {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    89
            Options.handleOptions(this, args);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    90
            if (options.help) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    91
                showHelp();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    92
                return EXIT_OK;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    93
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    94
            if (options.version) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    95
                showVersion();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    96
                return EXIT_OK;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    97
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    98
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
    99
            printer.printlnInfo("Compiling " + options.outputName + "...");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   100
            final long start = System.currentTimeMillis();
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   101
            if (!run()) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   102
                return EXIT_ABNORMAL;
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   103
            }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   104
            final long end = System.currentTimeMillis();
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   105
            printer.printlnInfo("Total time: " + (end - start) + " ms");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   106
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   107
            return EXIT_OK;
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   108
        } catch (Options.BadArgs e) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   109
            printer.reportError(e.key, e.args);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   110
            if (e.showUsage) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   111
                showUsage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   112
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   113
            return EXIT_CMDERR;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   114
        } catch (Exception e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   115
            e.printStackTrace();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   116
            return EXIT_ABNORMAL;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   117
        } finally {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   118
            log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   119
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   120
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   121
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   122
    @SuppressWarnings("try")
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   123
    private boolean run() throws Exception {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   124
        LogPrinter.openLog();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   125
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   126
        try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   127
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   128
            final Linker linker = new Linker(this);
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   129
            final String objectFileName = linker.objFile();
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   130
            final Collector collector = new Collector(this);
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   131
            Set<Class<?>> classesToCompile;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   132
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   133
            try (Timer t = new Timer(this, "")) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   134
                classesToCompile = collector.collectClassesToCompile();
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   135
                printer.printInfo(classesToCompile.size() + " classes found");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   136
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   137
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   138
            OptionValues graalOptions = HotSpotGraalOptionValues.HOTSPOT_OPTIONS;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   139
            // Setting -Dgraal.TieredAOT overrides --compile-for-tiered
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   140
            if (!TieredAOT.hasBeenSet(graalOptions)) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   141
                graalOptions = new OptionValues(graalOptions, TieredAOT, options.tiered);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   142
            }
46557
eb30f7891bae 8181113: jaotc options lost after 8177046
kvn
parents: 46344
diff changeset
   143
            graalOptions = new OptionValues(graalOptions, GeneratePIC, true, ImmutableCode, true);
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47668
diff changeset
   144
            GraalJVMCICompiler graalCompiler = HotSpotGraalCompilerFactory.createCompiler("JAOTC", JVMCI.getRuntime(), graalOptions, CompilerConfigurationFactory.selectFactory(null, graalOptions));
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   145
            HotSpotGraalRuntimeProvider runtime = (HotSpotGraalRuntimeProvider) graalCompiler.getGraalRuntime();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   146
            HotSpotHostBackend backend = (HotSpotHostBackend) runtime.getCapability(RuntimeProvider.class).getHostBackend();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   147
            MetaAccessProvider metaAccess = backend.getProviders().getMetaAccess();
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   148
            filters = new GraalFilters(metaAccess);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   149
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   150
            List<AOTCompiledClass> classes;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   151
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   152
            try (Timer t = new Timer(this, "")) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   153
                classes = collector.collectMethodsToCompile(classesToCompile, metaAccess);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   154
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   155
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   156
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   157
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   158
                printer.printMemoryUsage();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   159
                classesToCompile = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   160
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   161
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   162
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   163
            AOTDynamicTypeStore dynoStore = new AOTDynamicTypeStore();
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   164
            AOTCompiledClass.setDynamicTypeStore(dynoStore);
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   165
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   166
            AOTBackend aotBackend = new AOTBackend(this, graalOptions, backend, new HotSpotInvokeDynamicPlugin(dynoStore));
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
   167
            SnippetReflectionProvider snippetReflection = aotBackend.getProviders().getSnippetReflection();
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   168
            AOTCompiler compiler = new AOTCompiler(this, graalOptions, aotBackend, options.threads);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   169
            classes = compiler.compileClasses(classes);
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
            GraalHotSpotVMConfig graalHotSpotVMConfig = runtime.getVMConfig();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   172
            PhaseSuite<HighTierContext> graphBuilderSuite = aotBackend.getGraphBuilderSuite();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   173
            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
   174
            GraphBuilderConfiguration graphBuilderConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
1fab8e6866d7 8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents: 42650
diff changeset
   175
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   176
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   177
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   178
                printer.printMemoryUsage();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   179
                aotBackend = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   180
                compiler = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   181
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   182
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   183
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 46327
diff changeset
   184
            BinaryContainer binaryContainer = new BinaryContainer(graalOptions, graalHotSpotVMConfig, graphBuilderConfig, JVM_VERSION);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   185
            DataBuilder dataBuilder = new DataBuilder(this, backend, classes, binaryContainer);
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
   186
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
   187
            try (DebugContext debug = DebugContext.create(graalOptions, new GraalDebugHandlersFactory(snippetReflection)); Activation a = debug.activate()) {
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
   188
                dataBuilder.prepareData(debug);
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46557
diff changeset
   189
            }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   190
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   191
            // Print information about section sizes
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   192
            printer.containersInfo(binaryContainer);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   193
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   194
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   195
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   196
                printer.printMemoryUsage();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   197
                backend = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   198
                for (AOTCompiledClass aotCompClass : classes) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   199
                    aotCompClass.clear();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   200
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   201
                classes.clear();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   202
                classes = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   203
                dataBuilder = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   204
                binaryContainer.freeMemory();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   205
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   206
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   207
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   208
            try (Timer t = new Timer(this, "Creating binary: " + objectFileName)) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   209
                binaryContainer.createBinary(objectFileName);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   210
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   211
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   212
            // Free memory!
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   213
            try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   214
                printer.printMemoryUsage();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   215
                binaryContainer = null;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   216
                System.gc();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   217
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   218
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   219
            try (Timer t = new Timer(this, "Creating shared library: " + linker.libFile())) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   220
                linker.link();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   221
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   222
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   223
            printer.printVerbose("Final memory  ");
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   224
            printer.printMemoryUsage();
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   225
            printer.printlnVerbose("");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   226
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   227
        } finally {
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   228
            LogPrinter.closeLog();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   229
        }
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents: 43405
diff changeset
   230
        return true;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   231
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   232
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   233
    void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   234
        String methodName = JavaMethodInfo.uniqueMethodName(resolvedMethod);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   235
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   236
        if (options.debug) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   237
            printer.printError("Failed compilation: " + methodName + ": " + e);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   238
        }
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   239
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   240
        // Ignore some exceptions when meta-compiling Graal.
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   241
        if (GraalFilters.shouldIgnoreException(e)) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   242
            return;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   243
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   244
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   245
        LogPrinter.writeLog("Failed compilation of method " + methodName + message);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   246
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   247
        if (!options.debug) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   248
            printer.printError("Failed compilation: " + methodName + ": " + e);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   249
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   250
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   251
        if (options.verbose) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   252
            e.printStackTrace(log);
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   253
        }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   254
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   255
        if (options.exitOnError) {
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   256
            System.exit(1);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   257
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   258
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   259
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   260
    void warning(String key, Object... args) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   261
        log.println("Warning: " + MessageFormat.format(key, args));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   262
        log.flush();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   263
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   264
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   265
    private void showUsage() {
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   266
        log.println("Usage: " + PROGNAME + " <options> list");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   267
        log.println("use --help for a list of possible options");
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   268
        log.flush();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   269
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   270
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   271
    private void showHelp() {
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   272
        log.println("Usage: " + PROGNAME + " <options> list");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   273
        log.println();
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   274
        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
   275
        log.println("             or directories which contain class files.");
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   276
        log.println();
43479
67507b173e81 8173158: [AOT] fix typo in jaotc --help output
kvn
parents: 43464
diff changeset
   277
        log.println("where options include:");
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   278
        for (Option o : Options.recognizedOptions) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   279
            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
   280
            name = name.charAt(0) == '-' ? name.substring(1) : name;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   281
            if (o.isHidden() || name.equals("h")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   282
                continue;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   283
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   284
            log.println(o.help);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   285
        }
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46804
diff changeset
   286
        log.flush();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   287
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   288
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   289
    private void showVersion() {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   290
        log.println(PROGNAME + " " + JVM_VERSION);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   291
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   292
}