1 /*
   2  * Copyright (c) 2014, 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 org.graalvm.compiler.lir.gen;
  24 
  25 import jdk.vm.ci.code.RegisterConfig;
  26 import org.graalvm.compiler.core.common.CompilationIdentifier;
  27 import org.graalvm.compiler.core.common.CompilationIdentifier.Verbosity;
  28 import org.graalvm.compiler.debug.DebugContext;
  29 import org.graalvm.compiler.lir.LIR;
  30 import org.graalvm.compiler.lir.LIRInstruction;
  31 import org.graalvm.compiler.lir.framemap.FrameMap;
  32 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  33 import org.graalvm.util.EconomicMap;
  34 import org.graalvm.util.Equivalence;
  35 
  36 import jdk.vm.ci.code.CallingConvention;
  37 
  38 public class LIRGenerationResult {
  39 
  40     private final LIR lir;
  41     private final FrameMapBuilder frameMapBuilder;
  42     private FrameMap frameMap;
  43     private final CallingConvention callingConvention;
  44     /**
  45      * Records whether the code being generated makes at least one foreign call.
  46      */
  47     private boolean hasForeignCall;
  48     /**
  49      * Unique identifier of this compilation.
  50      */
  51     private CompilationIdentifier compilationId;
  52 
  53     /**
  54      * Stores comments about a {@link LIRInstruction} , e.g., which phase created it.
  55      */
  56     private EconomicMap<LIRInstruction, String> comments;
  57 
  58     public LIRGenerationResult(CompilationIdentifier compilationId, LIR lir, FrameMapBuilder frameMapBuilder, CallingConvention callingConvention) {
  59         this.lir = lir;
  60         this.frameMapBuilder = frameMapBuilder;
  61         this.callingConvention = callingConvention;
  62         this.compilationId = compilationId;
  63     }
  64 
  65     /**
  66      * Adds a comment to a {@link LIRInstruction}. Existing comments are replaced.
  67      */
  68     public final void setComment(LIRInstruction op, String comment) {
  69         DebugContext debug = lir.getDebug();
  70         if (debug.isDumpEnabled(DebugContext.BASIC_LEVEL)) {
  71             if (comments == null) {
  72                 comments = EconomicMap.create(Equivalence.IDENTITY);
  73             }
  74             comments.put(op, comment);
  75         }
  76     }
  77 
  78     /**
  79      * Gets the comment attached to a {@link LIRInstruction}.
  80      */
  81     public final String getComment(LIRInstruction op) {
  82         if (comments == null) {
  83             return null;
  84         }
  85         return comments.get(op);
  86     }
  87 
  88     /**
  89      * Returns the incoming calling convention for the parameters of the method that is compiled.
  90      */
  91     public CallingConvention getCallingConvention() {
  92         return callingConvention;
  93     }
  94 
  95     /**
  96      * Returns the {@link FrameMapBuilder} for collecting the information to build a
  97      * {@link FrameMap}.
  98      *
  99      * This method can only be used prior calling {@link #buildFrameMap}.
 100      */
 101     public final FrameMapBuilder getFrameMapBuilder() {
 102         assert frameMap == null : "getFrameMapBuilder() can only be used before calling buildFrameMap()!";
 103         return frameMapBuilder;
 104     }
 105 
 106     /**
 107      * Creates a {@link FrameMap} out of the {@link FrameMapBuilder}. This method should only be
 108      * called once. After calling it, {@link #getFrameMapBuilder()} can no longer be used.
 109      *
 110      * @see FrameMapBuilder#buildFrameMap
 111      */
 112     public void buildFrameMap() {
 113         assert frameMap == null : "buildFrameMap() can only be called once!";
 114         frameMap = frameMapBuilder.buildFrameMap(this);
 115     }
 116 
 117     /**
 118      * Returns the {@link FrameMap} associated with this {@link LIRGenerationResult}.
 119      *
 120      * This method can only be called after {@link #buildFrameMap}.
 121      */
 122     public FrameMap getFrameMap() {
 123         assert frameMap != null : "getFrameMap() can only be used after calling buildFrameMap()!";
 124         return frameMap;
 125     }
 126 
 127     public final RegisterConfig getRegisterConfig() {
 128         return frameMapBuilder.getRegisterConfig();
 129     }
 130 
 131     public LIR getLIR() {
 132         return lir;
 133     }
 134 
 135     /**
 136      * Determines whether the code being generated makes at least one foreign call.
 137      */
 138     public boolean hasForeignCall() {
 139         return hasForeignCall;
 140     }
 141 
 142     public final void setForeignCall(boolean hasForeignCall) {
 143         this.hasForeignCall = hasForeignCall;
 144     }
 145 
 146     public String getCompilationUnitName() {
 147         if (compilationId == null || compilationId == CompilationIdentifier.INVALID_COMPILATION_ID) {
 148             return "<unknown>";
 149         }
 150         return compilationId.toString(Verbosity.NAME);
 151     }
 152 
 153     /**
 154      * Returns a unique identifier of the current compilation.
 155      */
 156     public CompilationIdentifier getCompilationId() {
 157         return compilationId;
 158     }
 159 }