1 /*
   2  * Copyright (c) 2015, 2016, 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.aarch64;
  24 
  25 import static org.graalvm.compiler.hotspot.HotSpotBackend.EXCEPTION_HANDLER_IN_CALLER;
  26 import static jdk.vm.ci.aarch64.AArch64.lr;
  27 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  28 import static jdk.vm.ci.hotspot.aarch64.AArch64HotSpotRegisterConfig.fp;
  29 import static jdk.vm.ci.hotspot.aarch64.AArch64HotSpotRegisterConfig.inlineCacheRegister;
  30 import static jdk.vm.ci.hotspot.aarch64.AArch64HotSpotRegisterConfig.metaspaceMethodRegister;
  31 
  32 import org.graalvm.compiler.core.aarch64.AArch64NodeLIRBuilder;
  33 import org.graalvm.compiler.core.aarch64.AArch64NodeMatchRules;
  34 import org.graalvm.compiler.core.common.LIRKind;
  35 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
  36 import org.graalvm.compiler.core.gen.DebugInfoBuilder;
  37 import org.graalvm.compiler.debug.Debug;
  38 import org.graalvm.compiler.hotspot.HotSpotDebugInfoBuilder;
  39 import org.graalvm.compiler.hotspot.HotSpotLIRGenerator;
  40 import org.graalvm.compiler.hotspot.HotSpotLockStack;
  41 import org.graalvm.compiler.hotspot.HotSpotNodeLIRBuilder;
  42 import org.graalvm.compiler.hotspot.nodes.DirectCompareAndSwapNode;
  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.aarch64.AArch64Move.CompareAndSwapOp;
  49 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  50 import org.graalvm.compiler.nodes.BreakpointNode;
  51 import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  52 import org.graalvm.compiler.nodes.DirectCallTargetNode;
  53 import org.graalvm.compiler.nodes.FullInfopointNode;
  54 import org.graalvm.compiler.nodes.IndirectCallTargetNode;
  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 import jdk.vm.ci.meta.ValueKind;
  74 
  75 /**
  76  * LIR generator specialized for AArch64 HotSpot.
  77  */
  78 public class AArch64HotSpotNodeLIRBuilder extends AArch64NodeLIRBuilder implements HotSpotNodeLIRBuilder {
  79 
  80     public AArch64HotSpotNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool gen, AArch64NodeMatchRules nodeMatchRules) {
  81         super(graph, gen, nodeMatchRules);
  82         assert gen instanceof AArch64HotSpotLIRGenerator;
  83         assert getDebugInfoBuilder() instanceof HotSpotDebugInfoBuilder;
  84         ((AArch64HotSpotLIRGenerator) gen).setDebugInfoBuilder(((HotSpotDebugInfoBuilder) getDebugInfoBuilder()));
  85     }
  86 
  87     @Override
  88     protected DebugInfoBuilder createDebugInfoBuilder(StructuredGraph graph, NodeValueMap nodeValueMap) {
  89         HotSpotLockStack lockStack = new HotSpotLockStack(gen.getResult().getFrameMapBuilder(), LIRKind.value(AArch64Kind.QWORD));
  90         return new HotSpotDebugInfoBuilder(nodeValueMap, lockStack, (HotSpotLIRGenerator) gen);
  91     }
  92 
  93     private AArch64HotSpotLIRGenerator getGen() {
  94         return (AArch64HotSpotLIRGenerator) gen;
  95     }
  96 
  97     @Override
  98     protected void emitPrologue(StructuredGraph graph) {
  99         CallingConvention incomingArguments = gen.getResult().getCallingConvention();
 100         Value[] params = new Value[incomingArguments.getArgumentCount() + 2];
 101         for (int i = 0; i < incomingArguments.getArgumentCount(); i++) {
 102             params[i] = incomingArguments.getArgument(i);
 103             if (isStackSlot(params[i])) {
 104                 StackSlot slot = ValueUtil.asStackSlot(params[i]);
 105                 if (slot.isInCallerFrame() && !gen.getResult().getLIR().hasArgInCallerFrame()) {
 106                     gen.getResult().getLIR().setHasArgInCallerFrame();
 107                 }
 108             }
 109         }
 110         params[params.length - 2] = fp.asValue(LIRKind.value(AArch64Kind.QWORD));
 111         params[params.length - 1] = lr.asValue(LIRKind.value(AArch64Kind.QWORD));
 112 
 113         gen.emitIncomingValues(params);
 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         Variable scratch = gen.newVariable(LIRKind.value(getGen().target().arch.getWordKind()));
 126         append(new AArch64HotSpotSafepointOp(info, getGen().config, 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             Debug.log("Ignoring InfopointNode for AFTER_BCI");
 178         } else {
 179             super.visitFullInfopointNode(i);
 180         }
 181     }
 182 
 183     @Override
 184     public void visitDirectCompareAndSwap(DirectCompareAndSwapNode x) {
 185         AllocatableValue address = gen.asAllocatable(operand(x.getAddress()));
 186         AllocatableValue cmpValue = gen.asAllocatable(operand(x.expectedValue()));
 187         AllocatableValue newValue = gen.asAllocatable(operand(x.newValue()));
 188         ValueKind<?> kind = cmpValue.getValueKind();
 189         assert kind.equals(newValue.getValueKind());
 190 
 191         Variable result = gen.newVariable(newValue.getValueKind());
 192         Variable scratch = gen.newVariable(LIRKind.value(AArch64Kind.DWORD));
 193         append(new CompareAndSwapOp(result, cmpValue, newValue, address, scratch));
 194         setResult(x, result);
 195     }
 196 
 197     @Override
 198     public void visitBreakpointNode(BreakpointNode node) {
 199         JavaType[] sig = new JavaType[node.arguments().size()];
 200         for (int i = 0; i < sig.length; i++) {
 201             sig[i] = node.arguments().get(i).stamp().javaType(gen.getMetaAccess());
 202         }
 203 
 204         Value[] parameters = visitInvokeArguments(gen.getResult().getFrameMapBuilder().getRegisterConfig().getCallingConvention(HotSpotCallingConventionType.JavaCall, null, sig, gen),
 205                         node.arguments());
 206         append(new AArch64BreakpointOp(parameters));
 207     }
 208 }