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