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 org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  28 import static jdk.vm.ci.code.ValueUtil.asRegister;
  29 import static jdk.vm.ci.sparc.SPARC.g0;
  30 import static jdk.vm.ci.sparc.SPARC.l7;
  31 import static jdk.vm.ci.sparc.SPARC.sp;
  32 
  33 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  34 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CC;
  35 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag;
  36 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  37 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
  38 import org.graalvm.compiler.lir.LIRInstructionClass;
  39 import org.graalvm.compiler.lir.Opcode;
  40 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  41 
  42 import jdk.vm.ci.code.Register;
  43 import jdk.vm.ci.meta.AllocatableValue;
  44 
  45 /**
  46  * Sets up the arguments for an exception handler in the callers frame, removes the current frame
  47  * and jumps to the handler.
  48  */
  49 @Opcode("JUMP_TO_EXCEPTION_HANDLER_IN_CALLER")
  50 final class SPARCHotSpotJumpToExceptionHandlerInCallerOp extends SPARCHotSpotEpilogueOp {
  51 
  52     public static final LIRInstructionClass<SPARCHotSpotJumpToExceptionHandlerInCallerOp> TYPE = LIRInstructionClass.create(SPARCHotSpotJumpToExceptionHandlerInCallerOp.class);
  53     public static final SizeEstimate SIZE = SizeEstimate.create(5);
  54 
  55     @Use(REG) AllocatableValue handlerInCallerPc;
  56     @Use(REG) AllocatableValue exception;
  57     @Use(REG) AllocatableValue exceptionPc;
  58     private final Register thread;
  59     private final int isMethodHandleReturnOffset;
  60 
  61     SPARCHotSpotJumpToExceptionHandlerInCallerOp(AllocatableValue handlerInCallerPc, AllocatableValue exception, AllocatableValue exceptionPc, int isMethodHandleReturnOffset, Register thread) {
  62         super(TYPE, SIZE);
  63         this.handlerInCallerPc = handlerInCallerPc;
  64         this.exception = exception;
  65         this.exceptionPc = exceptionPc;
  66         this.isMethodHandleReturnOffset = isMethodHandleReturnOffset;
  67         this.thread = thread;
  68     }
  69 
  70     @Override
  71     public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  72         // Restore SP from L7 if the exception PC is a method handle call site.
  73         SPARCAddress dst = new SPARCAddress(thread, isMethodHandleReturnOffset);
  74         try (ScratchRegister scratch = masm.getScratchRegister()) {
  75             Register scratchReg = scratch.getRegister();
  76             masm.lduw(dst, scratchReg);
  77             masm.cmp(scratchReg, scratchReg);
  78             masm.movcc(ConditionFlag.NotZero, CC.Icc, l7, sp);
  79         }
  80         masm.jmpl(asRegister(handlerInCallerPc), 0, g0);
  81         leaveFrame(crb); // Delay slot
  82     }
  83 }