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