1 /*
   2  * Copyright (c) 2011, 2012, 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 com.oracle.graal.lir.asm;
  24 
  25 import static com.oracle.graal.api.code.ValueUtil.*;
  26 
  27 import java.util.*;
  28 
  29 import com.oracle.graal.api.code.*;
  30 import com.oracle.graal.api.meta.*;
  31 import com.oracle.graal.asm.*;
  32 import com.oracle.graal.debug.*;
  33 import com.oracle.graal.graph.*;
  34 import com.oracle.graal.lir.*;
  35 
  36 public class TargetMethodAssembler {
  37 
  38     private static class ExceptionInfo {
  39 
  40         public final int codeOffset;
  41         public final LabelRef exceptionEdge;
  42 
  43         public ExceptionInfo(int pcOffset, LabelRef exceptionEdge) {
  44             this.codeOffset = pcOffset;
  45             this.exceptionEdge = exceptionEdge;
  46         }
  47     }
  48 
  49     public final AbstractAssembler asm;
  50     public final CompilationResult compilationResult;
  51     public final TargetDescription target;
  52     public final CodeCacheProvider runtime;
  53     public final FrameMap frameMap;
  54 
  55     /**
  56      * The object that emits code for managing a method's frame. If null, no frame is used by the
  57      * method.
  58      */
  59     public final FrameContext frameContext;
  60 
  61     private List<ExceptionInfo> exceptionInfoList;
  62 
  63     public TargetMethodAssembler(TargetDescription target, CodeCacheProvider runtime, FrameMap frameMap, AbstractAssembler asm, FrameContext frameContext, CompilationResult compilationResult) {
  64         this.target = target;
  65         this.runtime = runtime;
  66         this.frameMap = frameMap;
  67         this.asm = asm;
  68         this.compilationResult = compilationResult;
  69         this.frameContext = frameContext;
  70     }
  71 
  72     public void setFrameSize(int frameSize) {
  73         compilationResult.setFrameSize(frameSize);
  74     }
  75 
  76     private static final CompilationResult.Mark[] NO_REFS = {};
  77 
  78     public CompilationResult.Mark recordMark(Object id) {
  79         return compilationResult.recordMark(asm.codeBuffer.position(), id, NO_REFS);
  80     }
  81 
  82     public CompilationResult.Mark recordMark(Object id, CompilationResult.Mark... references) {
  83         return compilationResult.recordMark(asm.codeBuffer.position(), id, references);
  84     }
  85 
  86     public void blockComment(String s) {
  87         compilationResult.addAnnotation(new CompilationResult.CodeComment(asm.codeBuffer.position(), s));
  88     }
  89 
  90     public CompilationResult finishTargetMethod(Object name, boolean isStub) {
  91         // Install code, data and frame size
  92         compilationResult.setTargetCode(asm.codeBuffer.close(false), asm.codeBuffer.position());
  93 
  94         // Record exception handlers if they exist
  95         if (exceptionInfoList != null) {
  96             for (ExceptionInfo ei : exceptionInfoList) {
  97                 int codeOffset = ei.codeOffset;
  98                 compilationResult.recordExceptionHandler(codeOffset, ei.exceptionEdge.label().position());
  99             }
 100         }
 101 
 102         Debug.metric("TargetMethods").increment();
 103         Debug.metric("CodeBytesEmitted").add(compilationResult.getTargetCodeSize());
 104         Debug.metric("SafepointsEmitted").add(compilationResult.getSafepoints().size());
 105         Debug.metric("DataPatches").add(compilationResult.getDataReferences().size());
 106         Debug.metric("ExceptionHandlersEmitted").add(compilationResult.getExceptionHandlers().size());
 107         Debug.log("Finished target method %s, isStub %b", name, isStub);
 108         return compilationResult;
 109     }
 110 
 111     public void recordExceptionHandlers(int pcOffset, LIRFrameState info) {
 112         if (info != null) {
 113             if (info.exceptionEdge != null) {
 114                 if (exceptionInfoList == null) {
 115                     exceptionInfoList = new ArrayList<>(4);
 116                 }
 117                 exceptionInfoList.add(new ExceptionInfo(pcOffset, info.exceptionEdge));
 118             }
 119         }
 120     }
 121 
 122     public void recordImplicitException(int pcOffset, LIRFrameState info) {
 123         // record an implicit exception point
 124         if (info != null) {
 125             compilationResult.recordSafepoint(pcOffset, info.debugInfo());
 126             assert info.exceptionEdge == null;
 127         }
 128     }
 129 
 130     public void recordDirectCall(int posBefore, int posAfter, InvokeTarget callTarget, LIRFrameState info) {
 131         DebugInfo debugInfo = info != null ? info.debugInfo() : null;
 132         compilationResult.recordCall(posBefore, posAfter - posBefore, callTarget, debugInfo, true);
 133     }
 134 
 135     public void recordIndirectCall(int posBefore, int posAfter, InvokeTarget callTarget, LIRFrameState info) {
 136         DebugInfo debugInfo = info != null ? info.debugInfo() : null;
 137         compilationResult.recordCall(posBefore, posAfter - posBefore, callTarget, debugInfo, false);
 138     }
 139 
 140     public void recordSafepoint(int pos, LIRFrameState info) {
 141         // safepoints always need debug info
 142         DebugInfo debugInfo = info.debugInfo();
 143         compilationResult.recordSafepoint(pos, debugInfo);
 144     }
 145 
 146     public AbstractAddress recordDataReferenceInCode(Constant data, int alignment, boolean inlined) {
 147         assert data != null;
 148         int pos = asm.codeBuffer.position();
 149         Debug.log("Data reference in code: pos = %d, data = %s", pos, data.toString());
 150         compilationResult.recordDataReference(pos, data, alignment, inlined);
 151         return asm.getPlaceholder();
 152     }
 153 
 154     /**
 155      * Returns the integer value of any constant that can be represented by a 32-bit integer value,
 156      * including long constants that fit into the 32-bit range.
 157      */
 158     public int asIntConst(Value value) {
 159         assert (value.getKind().getStackKind() == Kind.Int || value.getKind() == Kind.Long) && isConstant(value);
 160         Constant constant = (Constant) value;
 161         assert !runtime.needsDataPatch(constant) : constant + " should be in a DataPatch";
 162         long c = constant.asLong();
 163         if (!NumUtil.isInt(c)) {
 164             throw GraalInternalError.shouldNotReachHere();
 165         }
 166         return (int) c;
 167     }
 168 
 169     /**
 170      * Returns the address of a float constant that is embedded as a data references into the code.
 171      */
 172     public AbstractAddress asFloatConstRef(Value value) {
 173         return asFloatConstRef(value, 4);
 174     }
 175 
 176     public AbstractAddress asFloatConstRef(Value value, int alignment) {
 177         assert value.getKind() == Kind.Float && isConstant(value);
 178         return recordDataReferenceInCode((Constant) value, alignment, false);
 179     }
 180 
 181     /**
 182      * Returns the address of a double constant that is embedded as a data references into the code.
 183      */
 184     public AbstractAddress asDoubleConstRef(Value value) {
 185         return asDoubleConstRef(value, 8);
 186     }
 187 
 188     public AbstractAddress asDoubleConstRef(Value value, int alignment) {
 189         assert value.getKind() == Kind.Double && isConstant(value);
 190         return recordDataReferenceInCode((Constant) value, alignment, false);
 191     }
 192 
 193     /**
 194      * Returns the address of a long constant that is embedded as a data references into the code.
 195      */
 196     public AbstractAddress asLongConstRef(Value value) {
 197         assert value.getKind() == Kind.Long && isConstant(value);
 198         return recordDataReferenceInCode((Constant) value, 8, false);
 199     }
 200 
 201     public AbstractAddress asIntAddr(Value value) {
 202         assert value.getKind() == Kind.Int;
 203         return asAddress(value);
 204     }
 205 
 206     public AbstractAddress asLongAddr(Value value) {
 207         assert value.getKind() == Kind.Long;
 208         return asAddress(value);
 209     }
 210 
 211     public AbstractAddress asObjectAddr(Value value) {
 212         assert value.getKind() == Kind.Object;
 213         return asAddress(value);
 214     }
 215 
 216     public AbstractAddress asFloatAddr(Value value) {
 217         assert value.getKind() == Kind.Float;
 218         return asAddress(value);
 219     }
 220 
 221     public AbstractAddress asDoubleAddr(Value value) {
 222         assert value.getKind() == Kind.Double;
 223         return asAddress(value);
 224     }
 225 
 226     public AbstractAddress asAddress(Value value) {
 227         assert isStackSlot(value);
 228         StackSlot slot = asStackSlot(value);
 229         return asm.makeAddress(frameMap.registerConfig.getFrameRegister(), frameMap.offsetForStackSlot(slot));
 230     }
 231 }