1 /* 2 * Copyright (c) 2012, 2015, 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.hotspot.amd64; 24 25 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER; 26 import static jdk.vm.ci.amd64.AMD64.rbp; 27 import static jdk.vm.ci.code.ValueUtil.isStackSlot; 28 29 import org.graalvm.compiler.core.amd64.AMD64NodeLIRBuilder; 30 import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules; 31 import org.graalvm.compiler.core.common.LIRKind; 32 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage; 33 import org.graalvm.compiler.core.gen.DebugInfoBuilder; 34 import org.graalvm.compiler.debug.Debug; 35 import org.graalvm.compiler.hotspot.HotSpotDebugInfoBuilder; 36 import org.graalvm.compiler.hotspot.HotSpotLIRGenerator; 37 import org.graalvm.compiler.hotspot.HotSpotLockStack; 38 import org.graalvm.compiler.hotspot.HotSpotNodeLIRBuilder; 39 import org.graalvm.compiler.hotspot.nodes.DirectCompareAndSwapNode; 40 import org.graalvm.compiler.hotspot.nodes.HotSpotDirectCallTargetNode; 41 import org.graalvm.compiler.hotspot.nodes.HotSpotIndirectCallTargetNode; 42 import org.graalvm.compiler.lir.LIRFrameState; 43 import org.graalvm.compiler.lir.Variable; 44 import org.graalvm.compiler.lir.amd64.AMD64BreakpointOp; 45 import org.graalvm.compiler.lir.amd64.AMD64Move.CompareAndSwapOp; 46 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 47 import org.graalvm.compiler.nodes.BreakpointNode; 48 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; 49 import org.graalvm.compiler.nodes.DirectCallTargetNode; 50 import org.graalvm.compiler.nodes.FullInfopointNode; 51 import org.graalvm.compiler.nodes.IndirectCallTargetNode; 52 import org.graalvm.compiler.nodes.ParameterNode; 53 import org.graalvm.compiler.nodes.SafepointNode; 54 import org.graalvm.compiler.nodes.StructuredGraph; 55 import org.graalvm.compiler.nodes.ValueNode; 56 import org.graalvm.compiler.nodes.spi.NodeValueMap; 57 58 import jdk.vm.ci.amd64.AMD64; 59 import jdk.vm.ci.amd64.AMD64Kind; 60 import jdk.vm.ci.code.BytecodeFrame; 61 import jdk.vm.ci.code.CallingConvention; 62 import jdk.vm.ci.code.Register; 63 import jdk.vm.ci.code.RegisterValue; 64 import jdk.vm.ci.code.StackSlot; 65 import jdk.vm.ci.code.ValueUtil; 66 import jdk.vm.ci.hotspot.HotSpotCallingConventionType; 67 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 68 import jdk.vm.ci.meta.AllocatableValue; 69 import jdk.vm.ci.meta.JavaType; 70 import jdk.vm.ci.meta.Value; 71 72 /** 73 * LIR generator specialized for AMD64 HotSpot. 74 */ 75 public class AMD64HotSpotNodeLIRBuilder extends AMD64NodeLIRBuilder implements HotSpotNodeLIRBuilder { 76 77 public AMD64HotSpotNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool gen, AMD64NodeMatchRules nodeMatchRules) { 78 super(graph, gen, nodeMatchRules); 79 assert gen instanceof AMD64HotSpotLIRGenerator; 80 assert getDebugInfoBuilder() instanceof HotSpotDebugInfoBuilder; 81 ((AMD64HotSpotLIRGenerator) gen).setDebugInfoBuilder(((HotSpotDebugInfoBuilder) getDebugInfoBuilder())); 82 } 83 84 private AMD64HotSpotLIRGenerator getGen() { 85 return (AMD64HotSpotLIRGenerator) gen; 86 } 87 88 @Override 89 protected DebugInfoBuilder createDebugInfoBuilder(StructuredGraph graph, NodeValueMap nodeValueMap) { 90 HotSpotLockStack lockStack = new HotSpotLockStack(gen.getResult().getFrameMapBuilder(), LIRKind.value(AMD64Kind.QWORD)); 91 return new HotSpotDebugInfoBuilder(nodeValueMap, lockStack, (HotSpotLIRGenerator) gen); 92 } 93 94 @Override 95 protected void emitPrologue(StructuredGraph graph) { 96 97 CallingConvention incomingArguments = gen.getResult().getCallingConvention(); 98 99 Value[] params = new Value[incomingArguments.getArgumentCount() + 1]; 100 for (int i = 0; i < params.length - 1; i++) { 101 params[i] = incomingArguments.getArgument(i); 102 if (isStackSlot(params[i])) { 103 StackSlot slot = ValueUtil.asStackSlot(params[i]); 104 if (slot.isInCallerFrame() && !gen.getResult().getLIR().hasArgInCallerFrame()) { 105 gen.getResult().getLIR().setHasArgInCallerFrame(); 106 } 107 } 108 } 109 params[params.length - 1] = rbp.asValue(LIRKind.value(AMD64Kind.QWORD)); 110 111 gen.emitIncomingValues(params); 112 113 getGen().emitSaveRbp(); 114 115 getGen().append(((HotSpotDebugInfoBuilder) getDebugInfoBuilder()).lockStack()); 116 117 for (ParameterNode param : graph.getNodes(ParameterNode.TYPE)) { 118 Value paramValue = params[param.index()]; 119 assert paramValue.getValueKind().equals(getLIRGeneratorTool().getLIRKind(param.stamp())) : paramValue.getValueKind() + " != " + param.stamp(); 120 setResult(param, gen.emitMove(paramValue)); 121 } 122 } 123 124 @Override 125 public void visitSafepointNode(SafepointNode i) { 126 LIRFrameState info = state(i); 127 append(new AMD64HotSpotSafepointOp(info, getGen().config, this)); 128 } 129 130 @Override 131 protected void emitDirectCall(DirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState) { 132 InvokeKind invokeKind = ((HotSpotDirectCallTargetNode) callTarget).invokeKind(); 133 if (invokeKind.isIndirect()) { 134 append(new AMD64HotspotDirectVirtualCallOp(callTarget.targetMethod(), result, parameters, temps, callState, invokeKind, getGen().config)); 135 } else { 136 assert invokeKind.isDirect(); 137 HotSpotResolvedJavaMethod resolvedMethod = (HotSpotResolvedJavaMethod) callTarget.targetMethod(); 138 assert resolvedMethod.isConcrete() : "Cannot make direct call to abstract method."; 139 append(new AMD64HotSpotDirectStaticCallOp(callTarget.targetMethod(), result, parameters, temps, callState, invokeKind, getGen().config)); 140 } 141 } 142 143 @Override 144 protected void emitIndirectCall(IndirectCallTargetNode callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState callState) { 145 if (callTarget instanceof HotSpotIndirectCallTargetNode) { 146 Value metaspaceMethodSrc = operand(((HotSpotIndirectCallTargetNode) callTarget).metaspaceMethod()); 147 Value targetAddressSrc = operand(callTarget.computedAddress()); 148 AllocatableValue metaspaceMethodDst = AMD64.rbx.asValue(metaspaceMethodSrc.getValueKind()); 149 AllocatableValue targetAddressDst = AMD64.rax.asValue(targetAddressSrc.getValueKind()); 150 gen.emitMove(metaspaceMethodDst, metaspaceMethodSrc); 151 gen.emitMove(targetAddressDst, targetAddressSrc); 152 append(new AMD64IndirectCallOp(callTarget.targetMethod(), result, parameters, temps, metaspaceMethodDst, targetAddressDst, callState, getGen().config)); 153 } else { 154 super.emitIndirectCall(callTarget, result, parameters, temps, callState); 155 } 156 } 157 158 @Override 159 public void emitPatchReturnAddress(ValueNode address) { 160 append(new AMD64HotSpotPatchReturnAddressOp(gen.load(operand(address)))); 161 } 162 163 @Override 164 public void emitJumpToExceptionHandlerInCaller(ValueNode handlerInCallerPc, ValueNode exception, ValueNode exceptionPc) { 165 Variable handler = gen.load(operand(handlerInCallerPc)); 166 ForeignCallLinkage linkage = gen.getForeignCalls().lookupForeignCall(EXCEPTION_HANDLER_IN_CALLER); 167 CallingConvention outgoingCc = linkage.getOutgoingCallingConvention(); 168 assert outgoingCc.getArgumentCount() == 2; 169 RegisterValue exceptionFixed = (RegisterValue) outgoingCc.getArgument(0); 170 RegisterValue exceptionPcFixed = (RegisterValue) outgoingCc.getArgument(1); 171 gen.emitMove(exceptionFixed, operand(exception)); 172 gen.emitMove(exceptionPcFixed, operand(exceptionPc)); 173 Register thread = getGen().getProviders().getRegisters().getThreadRegister(); 174 AMD64HotSpotJumpToExceptionHandlerInCallerOp op = new AMD64HotSpotJumpToExceptionHandlerInCallerOp(handler, exceptionFixed, exceptionPcFixed, getGen().config.threadIsMethodHandleReturnOffset, 175 thread); 176 append(op); 177 } 178 179 @Override 180 public void visitFullInfopointNode(FullInfopointNode i) { 181 if (i.getState() != null && i.getState().bci == BytecodeFrame.AFTER_BCI) { 182 Debug.log("Ignoring InfopointNode for AFTER_BCI"); 183 } else { 184 super.visitFullInfopointNode(i); 185 } 186 } 187 188 @Override 189 public void visitDirectCompareAndSwap(DirectCompareAndSwapNode x) { 190 Value expected = gen.loadNonConst(operand(x.expectedValue())); 191 Variable newVal = gen.load(operand(x.newValue())); 192 assert expected.getValueKind().equals(newVal.getValueKind()); 193 194 RegisterValue raxLocal = AMD64.rax.asValue(expected.getValueKind()); 195 gen.emitMove(raxLocal, expected); 196 append(new CompareAndSwapOp((AMD64Kind) expected.getPlatformKind(), raxLocal, getGen().asAddressValue(operand(x.getAddress())), raxLocal, newVal)); 197 198 setResult(x, gen.emitMove(raxLocal)); 199 } 200 201 @Override 202 public void visitBreakpointNode(BreakpointNode node) { 203 JavaType[] sig = new JavaType[node.arguments().size()]; 204 for (int i = 0; i < sig.length; i++) { 205 sig[i] = node.arguments().get(i).stamp().javaType(gen.getMetaAccess()); 206 } 207 208 Value[] parameters = visitInvokeArguments(gen.getResult().getFrameMapBuilder().getRegisterConfig().getCallingConvention(HotSpotCallingConventionType.JavaCall, null, sig, gen), 209 node.arguments()); 210 append(new AMD64BreakpointOp(parameters)); 211 } 212 }