src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/MetadataBuilder.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2016, 2019, 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
51436
091c0d22e735 8206992: Update Graal
iveresov
parents: 47668
diff changeset
    24
091c0d22e735 8206992: Update Graal
iveresov
parents: 47668
diff changeset
    25
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    26
package jdk.tools.jaotc;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    27
55293
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    28
import static jdk.tools.jaotc.AOTCompiledClass.getType;
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    29
import static jdk.tools.jaotc.AOTCompiledClass.metadataName;
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    30
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    31
import java.util.ArrayList;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    32
import java.util.List;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    33
55293
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    34
import org.graalvm.compiler.code.CompilationResult;
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    35
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
    36
import org.graalvm.compiler.hotspot.HotSpotGraalServices;
55293
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
    37
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    38
import jdk.tools.jaotc.binformat.BinaryContainer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    39
import jdk.tools.jaotc.binformat.ByteContainer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    40
import jdk.tools.jaotc.binformat.GotSymbol;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    41
import jdk.tools.jaotc.utils.NativeOrderOutputStream;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    42
import jdk.vm.ci.code.StackSlot;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    43
import jdk.vm.ci.code.site.DataPatch;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    44
import jdk.vm.ci.code.site.Infopoint;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    45
import jdk.vm.ci.code.site.Mark;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    46
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    47
import jdk.vm.ci.hotspot.HotSpotMetaData;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    48
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 42650
diff changeset
    49
final class MetadataBuilder {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    50
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    51
    private final DataBuilder dataBuilder;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    52
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    53
    private final BinaryContainer binaryContainer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    54
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    55
    MetadataBuilder(DataBuilder dataBuilder) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    56
        this.dataBuilder = dataBuilder;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    57
        this.binaryContainer = dataBuilder.getBinaryContainer();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    58
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    59
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    60
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    61
     * Process compiled methods and create method metadata.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    62
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    63
    void processMetadata(List<AOTCompiledClass> classes, AOTCompiledClass stubCompiledCode) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    64
        for (AOTCompiledClass c : classes) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    65
            processMetadataClass(c);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    66
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    67
        processMetadataClass(stubCompiledCode);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    68
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    69
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    70
    private void processMetadataClass(AOTCompiledClass c) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    71
        processInfopointsAndMarks(c);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    72
        createMethodMetadata(c);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    73
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    74
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    75
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    76
     * Add metadata for each of the compiled methods in {@code compiledClass} to read-only section
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    77
     * of {@code binaryContainer}.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    78
     *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    79
     * @param compiledClass AOT Graal compilation result
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    80
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    81
    private void createMethodMetadata(AOTCompiledClass compiledClass) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    82
        HotSpotGraalRuntimeProvider runtime = dataBuilder.getBackend().getRuntime();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    83
        ByteContainer methodMetadataContainer = binaryContainer.getMethodMetadataContainer();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    84
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    85
        // For each of the compiled java methods, create records holding information about them.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    86
        for (CompiledMethodInfo methodInfo : compiledClass.getCompiledMethods()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    87
            // Get the current offset in the methodmetadata container.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    88
            final int startOffset = methodMetadataContainer.getByteStreamSize();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    89
            assert startOffset % 8 == 0 : "Must be aligned on 8";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    90
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    91
            methodInfo.setMetadataOffset(startOffset);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    92
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    93
            HotSpotCompiledCode compiledMethod = methodInfo.compiledCode();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    94
            // pc and scope description
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    95
            HotSpotMetaData metaData = new HotSpotMetaData(runtime.getTarget(), compiledMethod);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    96
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    97
            byte[] pcDesc = metaData.pcDescBytes();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    98
            byte[] scopeDesc = metaData.scopesDescBytes();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    99
            byte[] relocationInfo = metaData.relocBytes();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   100
            byte[] oopMapInfo = metaData.oopMaps();
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   101
            byte[] implicitExceptionBytes = HotSpotGraalServices.getImplicitExceptionBytes(metaData);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   102
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   103
            // create a global symbol at this position for this method
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   104
            NativeOrderOutputStream metadataStream = new NativeOrderOutputStream();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   105
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   106
            // get the code size
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   107
            int codeSize = methodInfo.getCodeSize();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   108
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   109
            // get code offsets
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   110
            CodeOffsets co = CodeOffsets.buildFrom(methodInfo.getCompilationResult().getMarks());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   111
            int unverifiedEntry = co.entry();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   112
            int verifiedEntry = co.verifiedEntry();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   113
            int exceptionHandler = co.exceptionHandler();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   114
            int deoptHandler = co.deoptHandler();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   115
            int frameSize = methodInfo.getCompilationResult().getTotalFrameSize();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   116
            StackSlot deoptRescueSlot = methodInfo.getCompilationResult().getCustomStackArea();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   117
            int origPcOffset = deoptRescueSlot != null ? deoptRescueSlot.getOffset(frameSize) : -1;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   118
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   119
            // get stubs offset
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   120
            int stubsOffset = methodInfo.getStubsOffset();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   121
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   122
            int offset = addMetadataEntries(binaryContainer, metaData, methodInfo);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   123
            methodInfo.setMetadataGotOffset(offset);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   124
            methodInfo.setMetadataGotSize(metaData.metadataEntries().length);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   125
            int unsafeAccess = methodInfo.getCompilationResult().hasUnsafeAccess() ? 1 : 0;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   126
            try {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   127
                // calculate total size of the container
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   128
                NativeOrderOutputStream.PatchableInt totalSize = metadataStream.patchableInt();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   129
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   130
                // @formatter:off
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   131
                metadataStream.putInt(codeSize).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   132
                               putInt(unverifiedEntry).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   133
                               putInt(verifiedEntry).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   134
                               putInt(exceptionHandler).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   135
                               putInt(deoptHandler).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   136
                               putInt(stubsOffset).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   137
                               putInt(frameSize).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   138
                               putInt(origPcOffset).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   139
                               putInt(unsafeAccess);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   140
                // @formatter:on
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   141
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   142
                NativeOrderOutputStream.PatchableInt pcDescOffset = metadataStream.patchableInt();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   143
                NativeOrderOutputStream.PatchableInt scopeOffset = metadataStream.patchableInt();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   144
                NativeOrderOutputStream.PatchableInt relocationOffset = metadataStream.patchableInt();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   145
                NativeOrderOutputStream.PatchableInt exceptionOffset = metadataStream.patchableInt();
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   146
                NativeOrderOutputStream.PatchableInt implictTableOffset = null;
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   147
                if (implicitExceptionBytes != null) {
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   148
                    implictTableOffset = metadataStream.patchableInt();
55293
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
   149
                }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   150
                NativeOrderOutputStream.PatchableInt oopMapOffset = metadataStream.patchableInt();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   151
                metadataStream.align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   152
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   153
                pcDescOffset.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   154
                metadataStream.put(pcDesc).align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   155
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   156
                scopeOffset.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   157
                metadataStream.put(scopeDesc).align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   158
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   159
                relocationOffset.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   160
                metadataStream.put(relocationInfo).align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   161
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   162
                exceptionOffset.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   163
                metadataStream.put(metaData.exceptionBytes()).align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   164
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   165
                if (implicitExceptionBytes != null) {
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   166
                    implictTableOffset.set(metadataStream.position());
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 55293
diff changeset
   167
                    metadataStream.put(implicitExceptionBytes).align(8);
55293
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
   168
                }
d19dc5b10fbb 8209626: [JVMCI] Use implicit exception table for dispatch and printing
never
parents: 52910
diff changeset
   169
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   170
                // oopmaps should be last
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   171
                oopMapOffset.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   172
                metadataStream.put(oopMapInfo).align(8);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   173
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   174
                totalSize.set(metadataStream.position());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   175
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   176
                byte[] data = metadataStream.array();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   177
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   178
                methodMetadataContainer.appendBytes(data, 0, data.length);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   179
            } catch (Exception e) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   180
                throw new InternalError("Exception occurred during compilation of " + methodInfo.getMethodInfo().getSymbolName(), e);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   181
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   182
            methodInfo.clearCompileData(); // Clear unused anymore compilation data
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   183
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   184
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   185
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   186
    private static int addMetadataEntries(BinaryContainer binaryContainer, HotSpotMetaData metaData, CompiledMethodInfo methodInfo) {
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   187
        Object[] metaDataEntries = metaData.metadataEntries();
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   188
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   189
        if (metaDataEntries.length == 0) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   190
            return 0;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   191
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   192
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   193
        int metadataGotSlotsStart = binaryContainer.getMetadataGotContainer().getByteStreamSize(); // binaryContainer.reserveMetadataGOTSlots(metaDataEntries.length);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   194
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   195
        for (int index = 0; index < metaDataEntries.length; index++) {
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   196
            Object ref = metaDataEntries[index];
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   197
            String name = metadataName(ref);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   198
            // Create GOT cells for klasses referenced in metadata
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   199
            addMetadataEntry(binaryContainer, name);
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   200
            // We should already have added entries for this klass
47668
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   201
            assert AOTCompiledClass.getAOTKlassData(getType(ref)) != null;
fc4cfca10556 8132547: [AOT] support invokedynamic instructions
dlong
parents: 47216
diff changeset
   202
            assert methodInfo.getDependentKlassData(getType(ref)) != null;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   203
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   204
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   205
        return metadataGotSlotsStart;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   206
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   207
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   208
    private static void addMetadataEntry(BinaryContainer binaryContainer, String name) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   209
        int stringOffset = binaryContainer.addMetaspaceName(name);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   210
        binaryContainer.addMetadataGotEntry(stringOffset);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   211
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   212
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   213
    /**
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   214
     * Process {@link Infopoint}s, {@link Mark}s and {@link DataPatch}es generated by the compiler
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   215
     * to create all needed binary section constructs.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   216
     *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   217
     * @param compiledClass compilation result
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   218
     */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   219
    private void processInfopointsAndMarks(AOTCompiledClass compiledClass) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   220
        ArrayList<CompiledMethodInfo> compiledMethods = compiledClass.getCompiledMethods();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   221
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   222
        MarkProcessor markProcessor = new MarkProcessor(dataBuilder);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   223
        DataPatchProcessor dataPatchProcessor = new DataPatchProcessor(dataBuilder);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   224
        InfopointProcessor infopointProcessor = new InfopointProcessor(dataBuilder);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   225
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   226
        for (CompiledMethodInfo methodInfo : compiledMethods) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   227
            CompilationResult compilationResult = methodInfo.getCompilationResult();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   228
            String targetSymbol = "state.M" + methodInfo.getCodeId();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   229
            String gotName = "got." + targetSymbol;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   230
            GotSymbol symbol = binaryContainer.getMethodStateContainer().createGotSymbol(gotName);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   231
            assert (symbol.getIndex() == methodInfo.getCodeId()) : "wrong offset";
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   232
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   233
            for (Infopoint infoPoint : compilationResult.getInfopoints()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   234
                infopointProcessor.process(methodInfo, infoPoint);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   235
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   236
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   237
            for (Mark mark : compilationResult.getMarks()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   238
                markProcessor.process(methodInfo, mark);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   239
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   240
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   241
            for (DataPatch dataPatch : compilationResult.getDataPatches()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   242
                dataPatchProcessor.process(methodInfo, dataPatch);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   243
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   244
        }
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
}