1 /*
   2  * Copyright (c) 2012, 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.amd64;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.CanOmitFrame;
  26 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  27 import static org.graalvm.compiler.core.common.GraalOptions.ZapStackOnMethodEntry;
  28 import static jdk.vm.ci.amd64.AMD64.r10;
  29 import static jdk.vm.ci.amd64.AMD64.rax;
  30 import static jdk.vm.ci.amd64.AMD64.rsp;
  31 import static jdk.vm.ci.code.ValueUtil.asRegister;
  32 
  33 import org.graalvm.compiler.asm.Assembler;
  34 import org.graalvm.compiler.asm.Label;
  35 import org.graalvm.compiler.asm.amd64.AMD64Address;
  36 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
  37 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  38 import org.graalvm.compiler.code.CompilationResult;
  39 import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules;
  40 import org.graalvm.compiler.core.common.LIRKind;
  41 import org.graalvm.compiler.core.common.CompilationIdentifier;
  42 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
  43 import org.graalvm.compiler.core.target.Backend;
  44 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  45 import org.graalvm.compiler.hotspot.HotSpotDataBuilder;
  46 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  47 import org.graalvm.compiler.hotspot.HotSpotHostBackend;
  48 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
  49 import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
  50 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
  51 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  52 import org.graalvm.compiler.hotspot.stubs.Stub;
  53 import org.graalvm.compiler.lir.LIR;
  54 import org.graalvm.compiler.lir.amd64.AMD64Call;
  55 import org.graalvm.compiler.lir.amd64.AMD64FrameMap;
  56 import org.graalvm.compiler.lir.amd64.AMD64FrameMapBuilder;
  57 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  58 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
  59 import org.graalvm.compiler.lir.asm.DataBuilder;
  60 import org.graalvm.compiler.lir.asm.FrameContext;
  61 import org.graalvm.compiler.lir.framemap.FrameMap;
  62 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  63 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  64 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  65 import org.graalvm.compiler.nodes.StructuredGraph;
  66 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  67 import org.graalvm.compiler.options.OptionValues;
  68 import org.graalvm.util.EconomicSet;
  69 
  70 import jdk.vm.ci.amd64.AMD64;
  71 import jdk.vm.ci.amd64.AMD64Kind;
  72 import jdk.vm.ci.code.CallingConvention;
  73 import jdk.vm.ci.code.Register;
  74 import jdk.vm.ci.code.RegisterConfig;
  75 import jdk.vm.ci.code.StackSlot;
  76 import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
  77 import jdk.vm.ci.hotspot.HotSpotSentinelConstant;
  78 import jdk.vm.ci.meta.JavaKind;
  79 import jdk.vm.ci.meta.JavaType;
  80 import jdk.vm.ci.meta.ResolvedJavaMethod;
  81 
  82 /**
  83  * HotSpot AMD64 specific backend.
  84  */
  85 public class AMD64HotSpotBackend extends HotSpotHostBackend {
  86 
  87     public AMD64HotSpotBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
  88         super(config, runtime, providers);
  89     }
  90 
  91     @Override
  92     public FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig) {
  93         RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
  94         return new AMD64FrameMapBuilder(newFrameMap(registerConfigNonNull), getCodeCache(), registerConfigNonNull);
  95     }
  96 
  97     @Override
  98     public FrameMap newFrameMap(RegisterConfig registerConfig) {
  99         return new AMD64FrameMap(getCodeCache(), registerConfig, this);
 100     }
 101 
 102     @Override
 103     public LIRGeneratorTool newLIRGenerator(LIRGenerationResult lirGenRes) {
 104         return new AMD64HotSpotLIRGenerator(getProviders(), config, lirGenRes);
 105     }
 106 
 107     @Override
 108     public LIRGenerationResult newLIRGenerationResult(CompilationIdentifier compilationId, LIR lir, FrameMapBuilder frameMapBuilder, StructuredGraph graph, Object stub) {
 109         return new HotSpotLIRGenerationResult(compilationId, lir, frameMapBuilder, makeCallingConvention(graph, (Stub) stub), stub);
 110     }
 111 
 112     @Override
 113     public NodeLIRBuilderTool newNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool lirGen) {
 114         return new AMD64HotSpotNodeLIRBuilder(graph, lirGen, new AMD64NodeMatchRules(lirGen));
 115     }
 116 
 117     @Override
 118     protected void bangStackWithOffset(CompilationResultBuilder crb, int bangOffset) {
 119         AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
 120         int pos = asm.position();
 121         asm.movl(new AMD64Address(rsp, -bangOffset), AMD64.rax);
 122         assert asm.position() - pos >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
 123     }
 124 
 125     /**
 126      * The size of the instruction used to patch the verified entry point of an nmethod when the
 127      * nmethod is made non-entrant or a zombie (e.g. during deopt or class unloading). The first
 128      * instruction emitted at an nmethod's verified entry point must be at least this length to
 129      * ensure mt-safe patching.
 130      */
 131     public static final int PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE = 5;
 132 
 133     /**
 134      * Emits code at the verified entry point and return point(s) of a method.
 135      */
 136     class HotSpotFrameContext implements FrameContext {
 137 
 138         final boolean isStub;
 139         final boolean omitFrame;
 140 
 141         HotSpotFrameContext(boolean isStub, boolean omitFrame) {
 142             this.isStub = isStub;
 143             this.omitFrame = omitFrame;
 144         }
 145 
 146         @Override
 147         public boolean hasFrame() {
 148             return !omitFrame;
 149         }
 150 
 151         @Override
 152         public void enter(CompilationResultBuilder crb) {
 153             FrameMap frameMap = crb.frameMap;
 154             int frameSize = frameMap.frameSize();
 155             AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
 156             if (omitFrame) {
 157                 if (!isStub) {
 158                     asm.nop(PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE);
 159                 }
 160             } else {
 161                 int verifiedEntryPointOffset = asm.position();
 162                 if (!isStub) {
 163                     emitStackOverflowCheck(crb);
 164                     // assert asm.position() - verifiedEntryPointOffset >=
 165                     // PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
 166                 }
 167                 if (!isStub && asm.position() == verifiedEntryPointOffset) {
 168                     asm.subqWide(rsp, frameSize);
 169                     assert asm.position() - verifiedEntryPointOffset >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
 170                 } else {
 171                     asm.decrementq(rsp, frameSize);
 172                 }
 173                 if (ZapStackOnMethodEntry.getValue(crb.getOptions())) {
 174                     final int intSize = 4;
 175                     for (int i = 0; i < frameSize / intSize; ++i) {
 176                         asm.movl(new AMD64Address(rsp, i * intSize), 0xC1C1C1C1);
 177                     }
 178                 }
 179                 assert frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
 180             }
 181         }
 182 
 183         @Override
 184         public void leave(CompilationResultBuilder crb) {
 185             if (!omitFrame) {
 186                 AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
 187                 assert crb.frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
 188 
 189                 int frameSize = crb.frameMap.frameSize();
 190                 asm.incrementq(rsp, frameSize);
 191             }
 192         }
 193     }
 194 
 195     @Override
 196     protected Assembler createAssembler(FrameMap frameMap) {
 197         return new AMD64MacroAssembler(getTarget());
 198     }
 199 
 200     @Override
 201     public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult lirGenRen, FrameMap frameMap, CompilationResult compilationResult, CompilationResultBuilderFactory factory) {
 202         // Omit the frame if the method:
 203         // - has no spill slots or other slots allocated during register allocation
 204         // - has no callee-saved registers
 205         // - has no incoming arguments passed on the stack
 206         // - has no deoptimization points
 207         // - makes no foreign calls (which require an aligned stack)
 208         HotSpotLIRGenerationResult gen = (HotSpotLIRGenerationResult) lirGenRen;
 209         LIR lir = gen.getLIR();
 210         assert gen.getDeoptimizationRescueSlot() == null || frameMap.frameNeedsAllocating() : "method that can deoptimize must have a frame";
 211         OptionValues options = lir.getOptions();
 212         boolean omitFrame = CanOmitFrame.getValue(options) && !frameMap.frameNeedsAllocating() && !lir.hasArgInCallerFrame() && !gen.hasForeignCall();
 213 
 214         Stub stub = gen.getStub();
 215         Assembler masm = createAssembler(frameMap);
 216         HotSpotFrameContext frameContext = new HotSpotFrameContext(stub != null, omitFrame);
 217         DataBuilder dataBuilder = new HotSpotDataBuilder(getCodeCache().getTarget());
 218         CompilationResultBuilder crb = factory.createBuilder(getCodeCache(), getForeignCalls(), frameMap, masm, dataBuilder, frameContext, options, compilationResult);
 219         crb.setTotalFrameSize(frameMap.totalFrameSize());
 220         crb.setMaxInterpreterFrameSize(gen.getMaxInterpreterFrameSize());
 221         StackSlot deoptimizationRescueSlot = gen.getDeoptimizationRescueSlot();
 222         if (deoptimizationRescueSlot != null && stub == null) {
 223             crb.compilationResult.setCustomStackAreaOffset(deoptimizationRescueSlot);
 224         }
 225 
 226         if (stub != null) {
 227             EconomicSet<Register> destroyedCallerRegisters = gatherDestroyedCallerRegisters(lir);
 228             updateStub(stub, destroyedCallerRegisters, gen.getCalleeSaveInfo(), frameMap);
 229         }
 230 
 231         return crb;
 232     }
 233 
 234     @Override
 235     public void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner) {
 236         AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
 237         FrameMap frameMap = crb.frameMap;
 238         RegisterConfig regConfig = frameMap.getRegisterConfig();
 239         Label verifiedEntry = new Label();
 240 
 241         // Emit the prefix
 242         emitCodePrefix(installedCodeOwner, crb, asm, regConfig, verifiedEntry);
 243 
 244         // Emit code for the LIR
 245         emitCodeBody(installedCodeOwner, crb, lir);
 246 
 247         // Emit the suffix
 248         emitCodeSuffix(installedCodeOwner, crb, asm, frameMap);
 249 
 250         // Profile assembler instructions
 251         profileInstructions(lir, crb);
 252     }
 253 
 254     /**
 255      * Emits the code prior to the verified entry point.
 256      *
 257      * @param installedCodeOwner see {@link Backend#emitCode}
 258      */
 259     public void emitCodePrefix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, RegisterConfig regConfig, Label verifiedEntry) {
 260         HotSpotProviders providers = getProviders();
 261         if (installedCodeOwner != null && !installedCodeOwner.isStatic()) {
 262             crb.recordMark(config.MARKID_UNVERIFIED_ENTRY);
 263             CallingConvention cc = regConfig.getCallingConvention(HotSpotCallingConventionType.JavaCallee, null, new JavaType[]{providers.getMetaAccess().lookupJavaType(Object.class)}, this);
 264             Register inlineCacheKlass = rax; // see definition of IC_Klass in
 265                                              // c1_LIRAssembler_x86.cpp
 266             Register receiver = asRegister(cc.getArgument(0));
 267             AMD64Address src = new AMD64Address(receiver, config.hubOffset);
 268 
 269             if (config.useCompressedClassPointers) {
 270                 Register register = r10;
 271                 AMD64HotSpotMove.decodeKlassPointer(crb, asm, register, providers.getRegisters().getHeapBaseRegister(), src, config);
 272                 if (GeneratePIC.getValue(crb.getOptions())) {
 273                     asm.movq(providers.getRegisters().getHeapBaseRegister(), asm.getPlaceholder(-1));
 274                     crb.recordMark(config.MARKID_NARROW_OOP_BASE_ADDRESS);
 275                 } else {
 276                     if (config.narrowKlassBase != 0) {
 277                         // The heap base register was destroyed above, so restore it
 278                         asm.movq(providers.getRegisters().getHeapBaseRegister(), config.narrowOopBase);
 279                     }
 280                 }
 281                 asm.cmpq(inlineCacheKlass, register);
 282             } else {
 283                 asm.cmpq(inlineCacheKlass, src);
 284             }
 285             AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(IC_MISS_HANDLER), ConditionFlag.NotEqual);
 286         }
 287 
 288         asm.align(config.codeEntryAlignment);
 289         crb.recordMark(config.MARKID_OSR_ENTRY);
 290         asm.bind(verifiedEntry);
 291         crb.recordMark(config.MARKID_VERIFIED_ENTRY);
 292 
 293         if (GeneratePIC.getValue(crb.getOptions())) {
 294             // Check for method state
 295             HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
 296             if (!frameContext.isStub) {
 297                 crb.recordInlineDataInCodeWithNote(new HotSpotSentinelConstant(LIRKind.value(AMD64Kind.QWORD), JavaKind.Long), HotSpotConstantLoadAction.MAKE_NOT_ENTRANT);
 298                 asm.movq(AMD64.rax, asm.getPlaceholder(-1));
 299                 asm.testq(AMD64.rax, AMD64.rax);
 300                 AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(WRONG_METHOD_HANDLER), ConditionFlag.NotZero);
 301             }
 302         }
 303     }
 304 
 305     /**
 306      * Emits the code which starts at the verified entry point.
 307      *
 308      * @param installedCodeOwner see {@link Backend#emitCode}
 309      */
 310     public void emitCodeBody(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, LIR lir) {
 311         crb.emit(lir);
 312     }
 313 
 314     /**
 315      * @param installedCodeOwner see {@link Backend#emitCode}
 316      */
 317     public void emitCodeSuffix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, FrameMap frameMap) {
 318         HotSpotProviders providers = getProviders();
 319         HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
 320         if (!frameContext.isStub) {
 321             HotSpotForeignCallsProvider foreignCalls = providers.getForeignCalls();
 322             crb.recordMark(config.MARKID_EXCEPTION_HANDLER_ENTRY);
 323             AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(EXCEPTION_HANDLER), null, false, null);
 324             crb.recordMark(config.MARKID_DEOPT_HANDLER_ENTRY);
 325             AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(DEOPTIMIZATION_HANDLER), null, false, null);
 326         } else {
 327             // No need to emit the stubs for entries back into the method since
 328             // it has no calls that can cause such "return" entries
 329 
 330             if (frameContext.omitFrame) {
 331                 // Cannot access slots in caller's frame if my frame is omitted
 332                 assert !frameMap.accessesCallerFrame();
 333             }
 334         }
 335     }
 336 
 337     @Override
 338     public RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo) {
 339         RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
 340         return new AMD64HotSpotRegisterAllocationConfig(registerConfigNonNull, allocationRestrictedTo);
 341     }
 342 
 343     @Override
 344     public EconomicSet<Register> translateToCallerRegisters(EconomicSet<Register> calleeRegisters) {
 345         return calleeRegisters;
 346     }
 347 }