1 /*
2  * Copyright (c) 2012, 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.replacements;
26 
27 import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProviderImpl.LOAD_AND_CLEAR_EXCEPTION;
28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.EXCEPTION_OOP_LOCATION;
29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.EXCEPTION_PC_LOCATION;
30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.readExceptionOop;
31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.registerAsWord;
32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.writeExceptionOop;
33 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.writeExceptionPc;
34 import static org.graalvm.compiler.hotspot.replacements.HotspotSnippetsOptions.LoadExceptionObjectInVM;
35 import static org.graalvm.compiler.nodes.PiNode.piCastToSnippetReplaceeStamp;
36 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
37 
38 import org.graalvm.compiler.api.replacements.Snippet;
39 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
40 import org.graalvm.compiler.core.common.type.Stamp;
41 import org.graalvm.compiler.debug.DebugHandlersFactory;
42 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
43 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
44 import org.graalvm.compiler.hotspot.word.HotSpotWordTypes;
45 import org.graalvm.compiler.nodes.StructuredGraph;
46 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
47 import org.graalvm.compiler.nodes.java.LoadExceptionObjectNode;
48 import org.graalvm.compiler.nodes.spi.LoweringTool;
49 import org.graalvm.compiler.options.OptionValues;
50 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
51 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
52 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
53 import org.graalvm.compiler.replacements.Snippets;
54 import org.graalvm.compiler.replacements.nodes.ReadRegisterNode;
55 import org.graalvm.compiler.word.Word;
56 import jdk.internal.vm.compiler.word.WordFactory;
57 
58 import jdk.vm.ci.code.BytecodeFrame;
59 import jdk.vm.ci.code.Register;
60 import jdk.vm.ci.code.TargetDescription;
61 import jdk.vm.ci.meta.ResolvedJavaType;
62 
63 /**
64  * Snippet for loading the exception object at the start of an exception dispatcher.
65  * <p>
66  * The frame state upon entry to an exception handler is such that it is a
67  * {@link BytecodeFrame#rethrowException rethrow exception} state and the stack contains exactly the
68  * exception object (per the JVM spec) to rethrow. This means that the code generated for this node
69  * must not cause a deoptimization as the runtime/interpreter would not have a valid location to
70  * find the exception object to be rethrown.
71  */
72 public class LoadExceptionObjectSnippets implements Snippets {
73 
74     @Snippet
loadException(@onstantParameter Register threadRegister)75     public static Object loadException(@ConstantParameter Register threadRegister) {
76         Word thread = registerAsWord(threadRegister);
77         Object exception = readExceptionOop(thread);
78         writeExceptionOop(thread, null);
79         writeExceptionPc(thread, WordFactory.zero());
80         return piCastToSnippetReplaceeStamp(exception);
81     }
82 
83     public static class Templates extends AbstractTemplates {
84 
85         private final SnippetInfo loadException = snippet(LoadExceptionObjectSnippets.class, "loadException", EXCEPTION_OOP_LOCATION, EXCEPTION_PC_LOCATION);
86         private final HotSpotWordTypes wordTypes;
87 
Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target)88         public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) {
89             super(options, factories, providers, providers.getSnippetReflection(), target);
90             this.wordTypes = providers.getWordTypes();
91         }
92 
lower(LoadExceptionObjectNode loadExceptionObject, HotSpotRegistersProvider registers, LoweringTool tool)93         public void lower(LoadExceptionObjectNode loadExceptionObject, HotSpotRegistersProvider registers, LoweringTool tool) {
94             StructuredGraph graph = loadExceptionObject.graph();
95             if (LoadExceptionObjectInVM.getValue(graph.getOptions())) {
96                 ResolvedJavaType wordType = providers.getMetaAccess().lookupJavaType(Word.class);
97                 Stamp stamp = wordTypes.getWordStamp(wordType);
98                 ReadRegisterNode thread = graph.add(new ReadRegisterNode(stamp, registers.getThreadRegister(), true, false));
99                 graph.addBeforeFixed(loadExceptionObject, thread);
100                 ForeignCallNode loadExceptionC = graph.add(new ForeignCallNode(LOAD_AND_CLEAR_EXCEPTION, thread));
101                 loadExceptionC.setStateAfter(loadExceptionObject.stateAfter());
102                 graph.replaceFixedWithFixed(loadExceptionObject, loadExceptionC);
103             } else {
104                 Arguments args = new Arguments(loadException, loadExceptionObject.graph().getGuardsStage(), tool.getLoweringStage());
105                 args.addConst("threadRegister", registers.getThreadRegister());
106                 template(loadExceptionObject, args).instantiate(providers.getMetaAccess(), loadExceptionObject, DEFAULT_REPLACER, args);
107             }
108         }
109     }
110 }
111