1 /*
   2  * Copyright (c) 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.stubs;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall;
  26 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_REGISTERS;
  27 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition.SAFEPOINT;
  28 import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl.REEXECUTABLE;
  29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.clearPendingException;
  30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord;
  31 import static org.graalvm.word.LocationIdentity.any;
  32 
  33 import org.graalvm.compiler.api.replacements.Fold;
  34 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  35 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  36 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  37 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  38 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
  39 import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl;
  40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  41 import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode;
  42 import org.graalvm.compiler.hotspot.word.KlassPointer;
  43 import org.graalvm.compiler.options.OptionValues;
  44 import org.graalvm.compiler.replacements.nodes.CStringConstant;
  45 import org.graalvm.compiler.word.Word;
  46 
  47 import jdk.vm.ci.code.Register;
  48 
  49 /**
  50  * Base class for stubs that create a runtime exception.
  51  */
  52 public class CreateExceptionStub extends SnippetStub {
  53 
  54     protected CreateExceptionStub(String snippetMethodName, OptionValues options, HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
  55         super(snippetMethodName, options, providers, linkage);
  56     }
  57 
  58     @Fold
  59     static String getInternalClassName(Class<?> cls) {
  60         return cls.getName().replace('.', '/');
  61     }
  62 
  63     private static Word classAsCString(Class<?> cls) {
  64         return CStringConstant.cstring(getInternalClassName(cls));
  65     }
  66 
  67     protected static Object createException(Register threadRegister, Class<? extends Throwable> exception) {
  68         Word message = null;
  69         return createException(threadRegister, exception, message);
  70     }
  71 
  72     protected static Object createException(Register threadRegister, Class<? extends Throwable> exception, Word message) {
  73         Word thread = registerAsWord(threadRegister);
  74         throwAndPostJvmtiException(THROW_AND_POST_JVMTI_EXCEPTION, thread, classAsCString(exception), message);
  75         return clearPendingException(thread);
  76     }
  77 
  78     protected static Object createException(Register threadRegister, Class<? extends Throwable> exception, KlassPointer klass) {
  79         Word thread = registerAsWord(threadRegister);
  80         throwKlassExternalNameException(THROW_KLASS_EXTERNAL_NAME_EXCEPTION, thread, classAsCString(exception), klass);
  81         return clearPendingException(thread);
  82     }
  83 
  84     protected static Object createException(Register threadRegister, Class<? extends Throwable> exception, KlassPointer objKlass, KlassPointer targetKlass) {
  85         Word thread = registerAsWord(threadRegister);
  86         throwClassCastException(THROW_CLASS_CAST_EXCEPTION, thread, classAsCString(exception), objKlass, targetKlass);
  87         return clearPendingException(thread);
  88     }
  89 
  90     private static final ForeignCallDescriptor THROW_AND_POST_JVMTI_EXCEPTION = new ForeignCallDescriptor("throw_and_post_jvmti_exception", void.class, Word.class, Word.class, Word.class);
  91     private static final ForeignCallDescriptor THROW_KLASS_EXTERNAL_NAME_EXCEPTION = new ForeignCallDescriptor("throw_klass_external_name_exception", void.class, Word.class, Word.class,
  92                     KlassPointer.class);
  93     private static final ForeignCallDescriptor THROW_CLASS_CAST_EXCEPTION = new ForeignCallDescriptor("throw_class_cast_exception", void.class, Word.class, Word.class, KlassPointer.class,
  94                     KlassPointer.class);
  95 
  96     @NodeIntrinsic(StubForeignCallNode.class)
  97     private static native void throwAndPostJvmtiException(@ConstantNodeParameter ForeignCallDescriptor d, Word thread, Word type, Word message);
  98 
  99     @NodeIntrinsic(StubForeignCallNode.class)
 100     private static native void throwKlassExternalNameException(@ConstantNodeParameter ForeignCallDescriptor d, Word thread, Word type, KlassPointer klass);
 101 
 102     @NodeIntrinsic(StubForeignCallNode.class)
 103     private static native void throwClassCastException(@ConstantNodeParameter ForeignCallDescriptor d, Word thread, Word type, KlassPointer objKlass, KlassPointer targetKlass);
 104 
 105     public static void registerForeignCalls(GraalHotSpotVMConfig c, HotSpotForeignCallsProviderImpl foreignCalls) {
 106         foreignCalls.registerForeignCall(THROW_AND_POST_JVMTI_EXCEPTION, c.throwAndPostJvmtiExceptionAddress, NativeCall, DESTROYS_REGISTERS, SAFEPOINT, REEXECUTABLE, any());
 107         foreignCalls.registerForeignCall(THROW_KLASS_EXTERNAL_NAME_EXCEPTION, c.throwKlassExternalNameExceptionAddress, NativeCall, DESTROYS_REGISTERS, SAFEPOINT, REEXECUTABLE, any());
 108         foreignCalls.registerForeignCall(THROW_CLASS_CAST_EXCEPTION, c.throwClassCastExceptionAddress, NativeCall, DESTROYS_REGISTERS, SAFEPOINT, REEXECUTABLE, any());
 109     }
 110 }