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