1 /*
   2  * Copyright (c) 2011, 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.lir.amd64;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.asRegister;
  28 import static jdk.vm.ci.code.ValueUtil.isRegister;
  29 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
  31 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  32 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  33 import static org.graalvm.compiler.lir.LIRValueUtil.differentRegisters;
  34 
  35 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
  36 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  37 import org.graalvm.compiler.core.common.LIRKind;
  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.Opcode;
  42 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  43 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool.ZapRegistersAfterInstruction;
  44 
  45 import jdk.vm.ci.amd64.AMD64;
  46 import jdk.vm.ci.amd64.AMD64Kind;
  47 import jdk.vm.ci.code.Register;
  48 import jdk.vm.ci.code.RegisterValue;
  49 import jdk.vm.ci.meta.AllocatableValue;
  50 import jdk.vm.ci.meta.InvokeTarget;
  51 import jdk.vm.ci.meta.ResolvedJavaMethod;
  52 import jdk.vm.ci.meta.Value;
  53 
  54 public class AMD64Call {
  55 
  56     public abstract static class CallOp extends AMD64LIRInstruction {
  57         public static final LIRInstructionClass<CallOp> TYPE = LIRInstructionClass.create(CallOp.class);
  58 
  59         @Def({REG, ILLEGAL}) protected Value result;
  60         @Use({REG, STACK}) protected Value[] parameters;
  61         @Temp({REG, STACK}) protected Value[] temps;
  62         @State protected LIRFrameState state;
  63 
  64         protected CallOp(LIRInstructionClass<? extends CallOp> c, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
  65             super(c);
  66             this.result = result;
  67             this.parameters = parameters;
  68             this.state = state;
  69             this.temps = addStackSlotsToTemporaries(parameters, temps);
  70             assert temps != null;
  71         }
  72 
  73         @Override
  74         public boolean destroysCallerSavedRegisters() {
  75             return true;
  76         }
  77     }
  78 
  79     public abstract static class MethodCallOp extends CallOp {
  80         public static final LIRInstructionClass<MethodCallOp> TYPE = LIRInstructionClass.create(MethodCallOp.class);
  81 
  82         protected final ResolvedJavaMethod callTarget;
  83 
  84         protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
  85             super(c, result, parameters, temps, state);
  86             this.callTarget = callTarget;
  87         }
  88 
  89     }
  90 
  91     @Opcode("CALL_DIRECT")
  92     public static class DirectCallOp extends MethodCallOp {
  93         public static final LIRInstructionClass<DirectCallOp> TYPE = LIRInstructionClass.create(DirectCallOp.class);
  94 
  95         public DirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
  96             this(TYPE, callTarget, result, parameters, temps, state);
  97         }
  98 
  99         protected DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
 100             super(c, callTarget, result, parameters, temps, state);
 101         }
 102 
 103         @Override
 104         public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 105             directCall(crb, masm, callTarget, null, true, state);
 106         }
 107 
 108         public int emitCall(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 109             return directCall(crb, masm, callTarget, null, true, state);
 110         }
 111     }
 112 
 113     @Opcode("CALL_INDIRECT")
 114     public static class IndirectCallOp extends MethodCallOp {
 115         public static final LIRInstructionClass<IndirectCallOp> TYPE = LIRInstructionClass.create(IndirectCallOp.class);
 116 
 117         @Use({REG}) protected Value targetAddress;
 118 
 119         public IndirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress, LIRFrameState state) {
 120             this(TYPE, callTarget, result, parameters, temps, targetAddress, state);
 121         }
 122 
 123         protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress,
 124                         LIRFrameState state) {
 125             super(c, callTarget, result, parameters, temps, state);
 126             this.targetAddress = targetAddress;
 127         }
 128 
 129         @Override
 130         public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 131             indirectCall(crb, masm, asRegister(targetAddress), callTarget, state);
 132         }
 133 
 134         @Override
 135         public void verify() {
 136             super.verify();
 137             assert isRegister(targetAddress) : "The current register allocator cannot handle variables to be used at call sites, it must be in a fixed register for now";
 138         }
 139     }
 140 
 141     public abstract static class ForeignCallOp extends CallOp implements ZapRegistersAfterInstruction {
 142         public static final LIRInstructionClass<ForeignCallOp> TYPE = LIRInstructionClass.create(ForeignCallOp.class);
 143 
 144         protected final ForeignCallLinkage callTarget;
 145 
 146         public ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
 147             super(c, result, parameters, temps, state);
 148             this.callTarget = callTarget;
 149         }
 150 
 151         @Override
 152         public boolean destroysCallerSavedRegisters() {
 153             return callTarget.destroysRegisters();
 154         }
 155     }
 156 
 157     @Opcode("NEAR_FOREIGN_CALL")
 158     public static final class DirectNearForeignCallOp extends ForeignCallOp {
 159         public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
 160 
 161         public DirectNearForeignCallOp(ForeignCallLinkage linkage, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
 162             super(TYPE, linkage, result, parameters, temps, state);
 163         }
 164 
 165         @Override
 166         public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 167             directCall(crb, masm, callTarget, null, false, state);
 168         }
 169     }
 170 
 171     @Opcode("FAR_FOREIGN_CALL")
 172     public static final class DirectFarForeignCallOp extends ForeignCallOp {
 173         public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
 174 
 175         @Temp({REG}) protected AllocatableValue callTemp;
 176 
 177         public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
 178             super(TYPE, callTarget, result, parameters, temps, state);
 179             /*
 180              * The register allocator does not support virtual registers that are used at the call
 181              * site, so use a fixed register.
 182              */
 183             callTemp = AMD64.rax.asValue(LIRKind.value(AMD64Kind.QWORD));
 184             assert differentRegisters(parameters, callTemp);
 185         }
 186 
 187         @Override
 188         public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 189             directCall(crb, masm, callTarget, ((RegisterValue) callTemp).getRegister(), false, state);
 190         }
 191     }
 192 
 193     public static int directCall(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget callTarget, Register scratch, boolean align, LIRFrameState info) {
 194         if (align) {
 195             emitAlignmentForDirectCall(crb, masm);
 196         }
 197         int before = masm.position();
 198         int callPCOffset;
 199         if (scratch != null && !GeneratePIC.getValue(crb.getOptions())) {
 200             // offset might not fit a 32-bit immediate, generate an
 201             // indirect call with a 64-bit immediate
 202             masm.movq(scratch, 0L);
 203             callPCOffset = masm.position();
 204             masm.call(scratch);
 205         } else {
 206             callPCOffset = masm.position();
 207             masm.call();
 208         }
 209         int after = masm.position();
 210         crb.recordDirectCall(before, after, callTarget, info);
 211         crb.recordExceptionHandlers(after, info);
 212         masm.ensureUniquePC();
 213         return callPCOffset;
 214     }
 215 
 216     protected static void emitAlignmentForDirectCall(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
 217         // make sure that the displacement word of the call ends up word aligned
 218         int offset = masm.position();
 219         offset += crb.target.arch.getMachineCodeCallDisplacementOffset();
 220         int modulus = crb.target.wordSize;
 221         if (offset % modulus != 0) {
 222             masm.nop(modulus - offset % modulus);
 223         }
 224     }
 225 
 226     public static int directJmp(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget target) {
 227         return directJmp(crb, masm, target, null);
 228     }
 229 
 230     public static int directJmp(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget target, Register scratch) {
 231         int before = masm.position();
 232         int callPCOffset;
 233         if (scratch != null && !GeneratePIC.getValue(crb.getOptions())) {
 234             // offset might not fit a 32-bit immediate, generate an
 235             // indirect call with a 64-bit immediate
 236             masm.movq(scratch, 0L);
 237             callPCOffset = masm.position();
 238             masm.jmp(scratch);
 239         } else {
 240             callPCOffset = masm.position();
 241             masm.jmp(0, true);
 242         }
 243         int after = masm.position();
 244         crb.recordDirectCall(before, after, target, null);
 245         masm.ensureUniquePC();
 246         return callPCOffset;
 247     }
 248 
 249     public static void directConditionalJmp(CompilationResultBuilder crb, AMD64MacroAssembler masm, InvokeTarget target, ConditionFlag cond) {
 250         int before = masm.position();
 251         masm.jcc(cond, 0, true);
 252         int after = masm.position();
 253         crb.recordDirectCall(before, after, target, null);
 254         masm.ensureUniquePC();
 255     }
 256 
 257     public static int indirectCall(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
 258         int before = masm.position();
 259         masm.call(dst);
 260         int after = masm.position();
 261         crb.recordIndirectCall(before, after, callTarget, info);
 262         crb.recordExceptionHandlers(after, info);
 263         masm.ensureUniquePC();
 264         return before;
 265     }
 266 }