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