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

 193         append(new AMD64BreakpointOp(parameters));
 194     }
 195 }
--- EOF ---