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