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