1 /*
   2  * Copyright (c) 2012, 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.sparc;
  26 
  27 import static jdk.vm.ci.sparc.SPARC.sp;
  28 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.ENABLE_STACK_RESERVED_ZONE;
  29 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.THROW_DELAYED_STACKOVERFLOW_ERROR;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
  31 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  32 
  33 import org.graalvm.compiler.asm.Label;
  34 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  35 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  36 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  37 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
  38 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
  39 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  40 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
  41 import org.graalvm.compiler.lir.LIRInstructionClass;
  42 import org.graalvm.compiler.lir.Opcode;
  43 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  44 import org.graalvm.compiler.lir.sparc.SPARCCall;
  45 import org.graalvm.compiler.lir.sparc.SPARCControlFlow.ReturnOp;
  46 
  47 import jdk.vm.ci.code.CallingConvention;
  48 import jdk.vm.ci.code.Register;
  49 import jdk.vm.ci.code.RegisterValue;
  50 import jdk.vm.ci.meta.Value;
  51 
  52 /**
  53  * Returns from a function.
  54  */
  55 @Opcode("RETURN")
  56 final class SPARCHotSpotReturnOp extends SPARCHotSpotEpilogueOp {
  57     public static final LIRInstructionClass<SPARCHotSpotReturnOp> TYPE = LIRInstructionClass.create(SPARCHotSpotReturnOp.class);
  58     public static final SizeEstimate SIZE = SizeEstimate.create(2);
  59 
  60     @Use({REG, ILLEGAL}) protected Value value;
  61     @Use({REG, ILLEGAL}) protected Value safepointPollAddress;
  62     private final boolean requiresReservedStackAccessCheck;
  63     private final boolean isStub;
  64     private final GraalHotSpotVMConfig config;
  65     private final Register thread;
  66 
  67     SPARCHotSpotReturnOp(Value value, boolean isStub, GraalHotSpotVMConfig config, Register thread, Value safepointPoll, boolean requiresReservedStackAccessCheck) {
  68         super(TYPE, SIZE);
  69         this.value = value;
  70         this.isStub = isStub;
  71         this.config = config;
  72         this.thread = thread;
  73         this.safepointPollAddress = safepointPoll;
  74         this.requiresReservedStackAccessCheck = requiresReservedStackAccessCheck;
  75     }
  76 
  77     @Override
  78     public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  79         if (!isStub) {
  80             if (requiresReservedStackAccessCheck) {
  81                 try (ScratchRegister sc = masm.getScratchRegister()) {
  82                     HotSpotForeignCallsProvider foreignCalls = (HotSpotForeignCallsProvider) crb.foreignCalls;
  83                     Label noReserved = new Label();
  84                     Register scratch = sc.getRegister();
  85                     masm.ldx(new SPARCAddress(thread, config.javaThreadReservedStackActivationOffset), scratch);
  86                     masm.compareBranch(sp, scratch, SPARCAssembler.ConditionFlag.LessUnsigned, SPARCAssembler.CC.Xcc, noReserved, SPARCAssembler.BranchPredict.PREDICT_TAKEN, null);
  87                     ForeignCallLinkage enableStackReservedZone = foreignCalls.lookupForeignCall(ENABLE_STACK_RESERVED_ZONE);
  88                     CallingConvention cc = enableStackReservedZone.getOutgoingCallingConvention();
  89                     assert cc.getArgumentCount() == 1;
  90                     Register arg0 = ((RegisterValue) cc.getArgument(0)).getRegister();
  91                     masm.mov(thread, arg0);
  92                     SPARCCall.directCall(crb, masm, enableStackReservedZone, scratch, null);
  93                     masm.restoreWindow();
  94                     SPARCCall.indirectJmp(crb, masm, scratch, foreignCalls.lookupForeignCall(THROW_DELAYED_STACKOVERFLOW_ERROR));
  95                     masm.bind(noReserved);
  96                 }
  97             }
  98             // Every non-stub compile method must have a poll before the return.
  99             SPARCHotSpotSafepointOp.emitCode(crb, masm, config, true, null, thread, safepointPollAddress);
 100         }
 101         ReturnOp.emitCodeHelper(crb, masm);
 102     }
 103 }