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