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.code.ValueUtil.asRegister;
  28 
  29 import java.util.ArrayList;
  30 import java.util.EnumSet;
  31 
  32 import jdk.vm.ci.code.CallingConvention;
  33 import jdk.vm.ci.meta.AllocatableValue;
  34 import jdk.vm.ci.meta.Constant;
  35 import jdk.vm.ci.meta.Value;
  36 
  37 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  38 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
  39 import org.graalvm.compiler.lir.LIRFrameState;
  40 import org.graalvm.compiler.lir.LIRInstructionClass;
  41 import org.graalvm.compiler.lir.LIRValueUtil;
  42 import org.graalvm.compiler.lir.ValueProcedure;
  43 import org.graalvm.compiler.lir.aarch64.AArch64LIRInstruction;
  44 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  45 
  46 public final class AArch64HotSpotConstantRetrievalOp extends AArch64LIRInstruction {
  47     public static final LIRInstructionClass<AArch64HotSpotConstantRetrievalOp> TYPE = LIRInstructionClass.create(AArch64HotSpotConstantRetrievalOp.class);
  48 
  49     @Def protected AllocatableValue result;
  50     protected final Constant[] constants;
  51     @Alive protected AllocatableValue[] constantDescriptions;
  52     @Temp protected AllocatableValue[] gotSlotOffsetParameters;
  53     @Temp protected AllocatableValue[] descriptionParameters;
  54     @Temp protected Value[] callTemps;
  55     @State protected LIRFrameState frameState;
  56     private final ForeignCallLinkage callLinkage;
  57     private final Object[] notes;
  58 
  59     private class CollectTemporaries implements ValueProcedure {
  60         ArrayList<Value> values = new ArrayList<>();
  61 
  62         CollectTemporaries() {
  63             forEachTemp(this);
  64         }
  65 
  66         public Value[] asArray() {
  67             Value[] copy = new Value[values.size()];
  68             return values.toArray(copy);
  69         }
  70 
  71         @Override
  72         public Value doValue(Value value, OperandMode mode, EnumSet<OperandFlag> flags) {
  73             values.add(value);
  74             return value;
  75         }
  76     }
  77 
  78     public AArch64HotSpotConstantRetrievalOp(Constant[] constants, AllocatableValue[] constantDescriptions, LIRFrameState frameState, ForeignCallLinkage callLinkage, Object[] notes) {
  79         super(TYPE);
  80         this.constantDescriptions = constantDescriptions;
  81         this.constants = constants;
  82         this.frameState = frameState;
  83         this.notes = notes;
  84         assert constants.length == notes.length;
  85 
  86         // call arguments
  87         CallingConvention callingConvention = callLinkage.getOutgoingCallingConvention();
  88         this.gotSlotOffsetParameters = new AllocatableValue[constants.length];
  89         int argIndex = 0;
  90         for (int i = 0; i < constants.length; i++, argIndex++) {
  91             this.gotSlotOffsetParameters[i] = callingConvention.getArgument(argIndex);
  92         }
  93         this.descriptionParameters = new AllocatableValue[constantDescriptions.length];
  94         for (int i = 0; i < constantDescriptions.length; i++, argIndex++) {
  95             this.descriptionParameters[i] = callingConvention.getArgument(argIndex);
  96         }
  97         this.result = callingConvention.getReturn();
  98 
  99         this.callLinkage = callLinkage;
 100 
 101         // compute registers that are killed by the stub, but are not used as other temps.
 102         this.callTemps = new Value[0];
 103         this.callTemps = LIRValueUtil.subtractRegisters(callLinkage.getTemporaries(), new CollectTemporaries().asArray());
 104     }
 105 
 106     @Override
 107     public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
 108         // metadata_adr
 109         for (int i = 0; i < constants.length; i++) {
 110             crb.recordInlineDataInCodeWithNote(constants[i], notes[i]);
 111             masm.addressOf(asRegister(gotSlotOffsetParameters[i]));
 112         }
 113 
 114         for (int i = 0; i < constantDescriptions.length; i++) {
 115             masm.mov(64, asRegister(descriptionParameters[i]), asRegister(constantDescriptions[i]));
 116         }
 117 
 118         final int before = masm.position();
 119         masm.bl(before);
 120         final int after = masm.position();
 121         crb.recordDirectCall(before, after, callLinkage, frameState);
 122     }
 123 
 124 }