1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.hotspot;
  24 
  25 import jdk.vm.ci.code.BytecodeFrame;
  26 import jdk.vm.ci.code.CompiledCode;
  27 import jdk.vm.ci.code.StackSlot;
  28 import jdk.vm.ci.code.site.DataPatch;
  29 import jdk.vm.ci.code.site.Infopoint;
  30 import jdk.vm.ci.code.site.Site;
  31 import jdk.vm.ci.meta.Assumptions.Assumption;
  32 import jdk.vm.ci.meta.ResolvedJavaMethod;
  33 
  34 /**
  35  * A {@link CompiledCode} with additional HotSpot-specific information required for installing the
  36  * code in HotSpot's code cache.
  37  */
  38 public class HotSpotCompiledCode implements CompiledCode {
  39 
  40     /**
  41      * The name of this compilation unit.
  42      */
  43     protected final String name;
  44 
  45     /**
  46      * The buffer containing the emitted machine code.
  47      */
  48     protected final byte[] targetCode;
  49 
  50     /**
  51      * The leading number of bytes in {@link #targetCode} containing the emitted machine code.
  52      */
  53     protected final int targetCodeSize;
  54 
  55     /**
  56      * A list of code annotations describing special sites in {@link #targetCode}.
  57      */
  58     protected final Site[] sites;
  59 
  60     /**
  61      * A list of {@link Assumption} this code relies on.
  62      */
  63     protected final Assumption[] assumptions;
  64 
  65     /**
  66      * The list of the methods whose bytecodes were used as input to the compilation. If
  67      * {@code null}, then the compilation did not record method dependencies. Otherwise, the first
  68      * element of this array is the root method of the compilation.
  69      */
  70     protected final ResolvedJavaMethod[] methods;
  71 
  72     /**
  73      * A list of comments that will be included in code dumps.
  74      */
  75     protected final Comment[] comments;
  76 
  77     /**
  78      * The data section containing serialized constants for the emitted machine code.
  79      */
  80     protected final byte[] dataSection;
  81 
  82     /**
  83      * The minimum alignment of the data section.
  84      */
  85     protected final int dataSectionAlignment;
  86 
  87     /**
  88      * A list of relocations in the {@link #dataSection}.
  89      */
  90     protected final DataPatch[] dataSectionPatches;
  91 
  92     /**
  93      * A flag determining whether this code is immutable and position independent.
  94      */
  95     protected final boolean isImmutablePIC;
  96 
  97     /**
  98      * The total size of the stack frame of this compiled method.
  99      */
 100     protected final int totalFrameSize;
 101 
 102     /**
 103      * The deopt rescue slot. Must be non-null if there is a safepoint in the method.
 104      */
 105     protected final StackSlot deoptRescueSlot;
 106 
 107     public static class Comment {
 108 
 109         public final String text;
 110         public final int pcOffset;
 111 
 112         public Comment(int pcOffset, String text) {
 113             this.text = text;
 114             this.pcOffset = pcOffset;
 115         }
 116     }
 117 
 118     public HotSpotCompiledCode(String name, byte[] targetCode, int targetCodeSize, Site[] sites, Assumption[] assumptions, ResolvedJavaMethod[] methods, Comment[] comments, byte[] dataSection,
 119                     int dataSectionAlignment, DataPatch[] dataSectionPatches, boolean isImmutablePIC, int totalFrameSize, StackSlot deoptRescueSlot) {
 120         this.name = name;
 121         this.targetCode = targetCode;
 122         this.targetCodeSize = targetCodeSize;
 123         this.sites = sites;
 124         this.assumptions = assumptions;
 125         this.methods = methods;
 126 
 127         this.comments = comments;
 128         this.dataSection = dataSection;
 129         this.dataSectionAlignment = dataSectionAlignment;
 130         this.dataSectionPatches = dataSectionPatches;
 131         this.isImmutablePIC = isImmutablePIC;
 132         this.totalFrameSize = totalFrameSize;
 133         this.deoptRescueSlot = deoptRescueSlot;
 134 
 135         assert validateFrames();
 136     }
 137 
 138     public String getName() {
 139         return name;
 140     }
 141 
 142     @Override
 143     public String toString() {
 144         return name;
 145     }
 146 
 147     /**
 148      * Ensure that all the frames passed into the VM are properly formatted with an empty or illegal
 149      * slot following double word slots.
 150      */
 151     private boolean validateFrames() {
 152         for (Site site : sites) {
 153             if (site instanceof Infopoint) {
 154                 Infopoint info = (Infopoint) site;
 155                 if (info.debugInfo != null) {
 156                     BytecodeFrame frame = info.debugInfo.frame();
 157                     assert frame == null || frame.validateFormat();
 158                 }
 159             }
 160         }
 161         return true;
 162     }
 163 }