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 java.util.Set;
  34 
  35 import org.graalvm.compiler.asm.Assembler;
  36 import org.graalvm.compiler.asm.Label;
  37 import org.graalvm.compiler.asm.amd64.AMD64Address;
  38 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
  39 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
  40 import org.graalvm.compiler.code.CompilationResult;
  41 import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules;
  42 import org.graalvm.compiler.core.common.LIRKind;
  43 import org.graalvm.compiler.core.common.CompilationIdentifier;
  44 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
  45 import org.graalvm.compiler.core.target.Backend;
  46 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  47 import org.graalvm.compiler.hotspot.HotSpotDataBuilder;
  48 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  49 import org.graalvm.compiler.hotspot.HotSpotHostBackend;
  50 import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
  51 import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
  52 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
  53 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  54 import org.graalvm.compiler.hotspot.stubs.Stub;
  55 import org.graalvm.compiler.lir.LIR;
  56 import org.graalvm.compiler.lir.amd64.AMD64Call;
  57 import org.graalvm.compiler.lir.amd64.AMD64FrameMap;
  58 import org.graalvm.compiler.lir.amd64.AMD64FrameMapBuilder;
  59 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  60 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
  61 import org.graalvm.compiler.lir.asm.DataBuilder;
  62 import org.graalvm.compiler.lir.asm.FrameContext;
  63 import org.graalvm.compiler.lir.framemap.FrameMap;
  64 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  65 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  66 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  67 import org.graalvm.compiler.nodes.StructuredGraph;
  68 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  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()) {
 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         boolean omitFrame = CanOmitFrame.getValue() && !frameMap.frameNeedsAllocating() && !lir.hasArgInCallerFrame() && !gen.hasForeignCall();
 212 
 213         Stub stub = gen.getStub();
 214         Assembler masm = createAssembler(frameMap);
 215         HotSpotFrameContext frameContext = new HotSpotFrameContext(stub != null, omitFrame);
 216         DataBuilder dataBuilder = new HotSpotDataBuilder(getCodeCache().getTarget());
 217         CompilationResultBuilder crb = factory.createBuilder(getCodeCache(), getForeignCalls(), frameMap, masm, dataBuilder, frameContext, compilationResult);
 218         crb.setTotalFrameSize(frameMap.totalFrameSize());
 219         crb.setMaxInterpreterFrameSize(gen.getMaxInterpreterFrameSize());
 220         StackSlot deoptimizationRescueSlot = gen.getDeoptimizationRescueSlot();
 221         if (deoptimizationRescueSlot != null && stub == null) {
 222             crb.compilationResult.setCustomStackAreaOffset(deoptimizationRescueSlot);
 223         }
 224 
 225         if (stub != null) {
 226             Set<Register> destroyedCallerRegisters = gatherDestroyedCallerRegisters(lir);
 227             updateStub(stub, destroyedCallerRegisters, gen.getCalleeSaveInfo(), frameMap);
 228         }
 229 
 230         return crb;
 231     }
 232 
 233     @Override
 234     public void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner) {
 235         AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
 236         FrameMap frameMap = crb.frameMap;
 237         RegisterConfig regConfig = frameMap.getRegisterConfig();
 238         Label verifiedEntry = new Label();
 239 
 240         // Emit the prefix
 241         emitCodePrefix(installedCodeOwner, crb, asm, regConfig, verifiedEntry);
 242 
 243         // Emit code for the LIR
 244         emitCodeBody(installedCodeOwner, crb, lir);
 245 
 246         // Emit the suffix
 247         emitCodeSuffix(installedCodeOwner, crb, asm, frameMap);
 248 
 249         // Profile assembler instructions
 250         profileInstructions(lir, crb);
 251     }
 252 
 253     /**
 254      * Emits the code prior to the verified entry point.
 255      *
 256      * @param installedCodeOwner see {@link Backend#emitCode}
 257      */
 258     public void emitCodePrefix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, RegisterConfig regConfig, Label verifiedEntry) {
 259         HotSpotProviders providers = getProviders();
 260         if (installedCodeOwner != null && !installedCodeOwner.isStatic()) {
 261             crb.recordMark(config.MARKID_UNVERIFIED_ENTRY);
 262             CallingConvention cc = regConfig.getCallingConvention(HotSpotCallingConventionType.JavaCallee, null, new JavaType[]{providers.getMetaAccess().lookupJavaType(Object.class)}, this);
 263             Register inlineCacheKlass = rax; // see definition of IC_Klass in
 264                                              // c1_LIRAssembler_x86.cpp
 265             Register receiver = asRegister(cc.getArgument(0));
 266             AMD64Address src = new AMD64Address(receiver, config.hubOffset);
 267 
 268             if (config.useCompressedClassPointers) {
 269                 Register register = r10;
 270                 AMD64HotSpotMove.decodeKlassPointer(crb, asm, register, providers.getRegisters().getHeapBaseRegister(), src, config);
 271                 if (GeneratePIC.getValue()) {
 272                     asm.movq(providers.getRegisters().getHeapBaseRegister(), asm.getPlaceholder(-1));
 273                     crb.recordMark(config.MARKID_NARROW_OOP_BASE_ADDRESS);
 274                 } else {
 275                     if (config.narrowKlassBase != 0) {
 276                         // The heap base register was destroyed above, so restore it
 277                         asm.movq(providers.getRegisters().getHeapBaseRegister(), config.narrowOopBase);
 278                     }
 279                 }
 280                 asm.cmpq(inlineCacheKlass, register);
 281             } else {
 282                 asm.cmpq(inlineCacheKlass, src);
 283             }
 284             AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(IC_MISS_HANDLER), ConditionFlag.NotEqual);
 285         }
 286 
 287         asm.align(config.codeEntryAlignment);
 288         crb.recordMark(config.MARKID_OSR_ENTRY);
 289         asm.bind(verifiedEntry);
 290         crb.recordMark(config.MARKID_VERIFIED_ENTRY);
 291 
 292         if (GeneratePIC.getValue()) {
 293             // Check for method state
 294             HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
 295             if (!frameContext.isStub) {
 296                 crb.recordInlineDataInCodeWithNote(new HotSpotSentinelConstant(LIRKind.value(AMD64Kind.QWORD), JavaKind.Long), HotSpotConstantLoadAction.MAKE_NOT_ENTRANT);
 297                 asm.movq(AMD64.rax, asm.getPlaceholder(-1));
 298                 asm.testq(AMD64.rax, AMD64.rax);
 299                 AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(WRONG_METHOD_HANDLER), ConditionFlag.NotZero);
 300             }
 301         }
 302     }
 303 
 304     /**
 305      * Emits the code which starts at the verified entry point.
 306      *
 307      * @param installedCodeOwner see {@link Backend#emitCode}
 308      */
 309     public void emitCodeBody(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, LIR lir) {
 310         crb.emit(lir);
 311     }
 312 
 313     /**
 314      * @param installedCodeOwner see {@link Backend#emitCode}
 315      */
 316     public void emitCodeSuffix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, FrameMap frameMap) {
 317         HotSpotProviders providers = getProviders();
 318         HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
 319         if (!frameContext.isStub) {
 320             HotSpotForeignCallsProvider foreignCalls = providers.getForeignCalls();
 321             crb.recordMark(config.MARKID_EXCEPTION_HANDLER_ENTRY);
 322             AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(EXCEPTION_HANDLER), null, false, null);
 323             crb.recordMark(config.MARKID_DEOPT_HANDLER_ENTRY);
 324             AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(DEOPTIMIZATION_HANDLER), null, false, null);
 325         } else {
 326             // No need to emit the stubs for entries back into the method since
 327             // it has no calls that can cause such "return" entries
 328 
 329             if (frameContext.omitFrame) {
 330                 // Cannot access slots in caller's frame if my frame is omitted
 331                 assert !frameMap.accessesCallerFrame();
 332             }
 333         }
 334     }
 335 
 336     @Override
 337     public RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig) {
 338         RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
 339         return new AMD64HotSpotRegisterAllocationConfig(registerConfigNonNull);
 340     }
 341 
 342     @Override
 343     public Set<Register> translateToCallerRegisters(Set<Register> calleeRegisters) {
 344         return calleeRegisters;
 345     }
 346 }