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 float value of any constant that can be represented by a 32-bit float value 171 */ 172 public float asFloatConst(Value value) { 173 assert (value.getKind().getStackKind() == Kind.Float && isConstant(value)); 174 Constant constant = (Constant) value; 175 assert !runtime.needsDataPatch(constant) : constant + " should be in a DataPatch"; 176 return constant.asFloat(); 177 } 178 179 /** 180 * Returns the long value of any constant that can be represented by a 64-bit long value 181 */ 182 public long asLongConst(Value value) { 183 assert (value.getKind().getStackKind() == Kind.Long && isConstant(value)); 184 Constant constant = (Constant) value; 185 assert !runtime.needsDataPatch(constant) : constant + " should be in a DataPatch"; 186 return constant.asLong(); 187 } 188 189 /** 190 * Returns the double value of any constant that can be represented by a 64-bit float value 191 */ 192 public double asDoubleConst(Value value) { 193 assert (value.getKind().getStackKind() == Kind.Double && isConstant(value)); 194 Constant constant = (Constant) value; 195 assert !runtime.needsDataPatch(constant) : constant + " should be in a DataPatch"; 196 return constant.asDouble(); 197 } 198 199 /** 200 * Returns the address of a float constant that is embedded as a data references into the code. 201 */ 202 public AbstractAddress asFloatConstRef(Value value) { 203 return asFloatConstRef(value, 4); 204 } 205 206 public AbstractAddress asFloatConstRef(Value value, int alignment) { 207 assert value.getKind() == Kind.Float && isConstant(value); 208 return recordDataReferenceInCode((Constant) value, alignment, false); 209 } 210 211 /** 212 * Returns the address of a double constant that is embedded as a data references into the code. 213 */ 214 public AbstractAddress asDoubleConstRef(Value value) { 215 return asDoubleConstRef(value, 8); 216 } 217 218 public AbstractAddress asDoubleConstRef(Value value, int alignment) { 219 assert value.getKind() == Kind.Double && isConstant(value); 220 return recordDataReferenceInCode((Constant) value, alignment, false); 221 } 222 223 /** 224 * Returns the address of a long constant that is embedded as a data references into the code. 225 */ 226 public AbstractAddress asLongConstRef(Value value) { 227 assert value.getKind() == Kind.Long && isConstant(value); 228 return recordDataReferenceInCode((Constant) value, 8, false); 229 } 230 231 public AbstractAddress asIntAddr(Value value) { 232 assert value.getKind() == Kind.Int; 233 return asAddress(value); 234 } 235 236 public AbstractAddress asLongAddr(Value value) { 237 assert value.getKind() == Kind.Long; 238 return asAddress(value); 239 } 240 241 public AbstractAddress asObjectAddr(Value value) { 242 assert value.getKind() == Kind.Object; 243 return asAddress(value); 244 } 245 246 public AbstractAddress asFloatAddr(Value value) { 247 assert value.getKind() == Kind.Float; 248 return asAddress(value); 249 } 250 251 public AbstractAddress asDoubleAddr(Value value) { 252 assert value.getKind() == Kind.Double; 253 return asAddress(value); 254 } 255 256 public AbstractAddress asAddress(Value value) { 257 assert isStackSlot(value); 258 StackSlot slot = asStackSlot(value); 259 return asm.makeAddress(frameMap.registerConfig.getFrameRegister(), frameMap.offsetForStackSlot(slot)); 260 } 261 }