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