1 /*
   2  * Copyright (c) 2020, 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.code.ValueUtil.asRegister;
  28 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.DEOPT_BLOB_UNPACK_WITH_EXCEPTION_IN_TLS;
  29 
  30 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  31 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  32 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  33 import org.graalvm.compiler.lir.LIRInstructionClass;
  34 import org.graalvm.compiler.lir.Opcode;
  35 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  36 import org.graalvm.compiler.lir.sparc.SPARCCall;
  37 
  38 import jdk.vm.ci.code.Register;
  39 import jdk.vm.ci.meta.Value;
  40 import jdk.vm.ci.sparc.SPARC;
  41 
  42 /**
  43  * Removes the current frame and tail calls the uncommon trap routine.
  44  */
  45 @Opcode("DEOPT_WITH_EXCEPTION_IN_CALLER")
  46 final class SPARCHotSpotDeoptimizeWithExceptionCallerOp extends SPARCHotSpotEpilogueOp {
  47     public static final LIRInstructionClass<SPARCHotSpotDeoptimizeWithExceptionCallerOp> TYPE = LIRInstructionClass.create(SPARCHotSpotDeoptimizeWithExceptionCallerOp.class);
  48     public static final SizeEstimate SIZE = SizeEstimate.create(32);
  49 
  50     private final GraalHotSpotVMConfig config;
  51     private final Register thread;
  52     @Use(OperandFlag.REG) private Value exception;
  53 
  54     protected SPARCHotSpotDeoptimizeWithExceptionCallerOp(GraalHotSpotVMConfig config, Value exception, Register thread) {
  55         super(TYPE, SIZE);
  56         this.config = config;
  57         this.exception = exception;
  58         this.thread = thread;
  59     }
  60 
  61     @Override
  62     public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  63         Register exc = asRegister(exception);
  64 
  65         // Save exception oop in TLS
  66         masm.stx(exc, new SPARCAddress(thread, config.threadExceptionOopOffset));
  67         // Store original return address in TLS
  68         masm.stx(SPARC.i7, new SPARCAddress(thread, config.threadExceptionPcOffset));
  69 
  70         leaveFrame(crb);
  71 
  72         try (SPARCMacroAssembler.ScratchRegister sc = masm.getScratchRegister()) {
  73             Register scratch = sc.getRegister();
  74             SPARCCall.indirectJmp(crb, masm, scratch, crb.foreignCalls.lookupForeignCall(DEOPT_BLOB_UNPACK_WITH_EXCEPTION_IN_TLS));
  75         }
  76     }
  77 }