1 /*
   2  * Copyright (c) 2009, 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.nodes;
  24 
  25 import static org.graalvm.compiler.nodeinfo.InputType.Extension;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
  28 
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.IterableNodeType;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodes.memory.MemoryMapNode;
  34 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  35 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  36 
  37 import jdk.vm.ci.code.TargetDescription;
  38 import jdk.vm.ci.meta.JavaKind;
  39 
  40 @NodeInfo(cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
  41 public final class ReturnNode extends ControlSinkNode implements LIRLowerable, IterableNodeType {
  42 
  43     public static final NodeClass<ReturnNode> TYPE = NodeClass.create(ReturnNode.class);
  44     @OptionalInput ValueNode result;
  45     @OptionalInput(Extension) MemoryMapNode memoryMap;
  46 
  47     public ValueNode result() {
  48         return result;
  49     }
  50 
  51     public ReturnNode(ValueNode result) {
  52         this(result, null);
  53     }
  54 
  55     public ReturnNode(ValueNode result, MemoryMapNode memoryMap) {
  56         super(TYPE, StampFactory.forVoid());
  57         this.result = result;
  58         this.memoryMap = memoryMap;
  59     }
  60 
  61     @Override
  62     public void generate(NodeLIRBuilderTool gen) {
  63         assert verifyReturn(gen.getLIRGeneratorTool().target());
  64         if (result == null) {
  65             gen.getLIRGeneratorTool().emitReturn(JavaKind.Void, null);
  66         } else {
  67             gen.getLIRGeneratorTool().emitReturn(result.getStackKind(), gen.operand(result));
  68         }
  69     }
  70 
  71     public void setMemoryMap(MemoryMapNode memoryMap) {
  72         updateUsages(this.memoryMap, memoryMap);
  73         this.memoryMap = memoryMap;
  74     }
  75 
  76     public MemoryMapNode getMemoryMap() {
  77         return memoryMap;
  78     }
  79 
  80     private boolean verifyReturn(TargetDescription target) {
  81         if (graph().method() != null) {
  82             JavaKind actual = result == null ? JavaKind.Void : result.getStackKind();
  83             JavaKind expected = graph().method().getSignature().getReturnKind().getStackKind();
  84             if (actual == target.wordJavaKind && expected == JavaKind.Object) {
  85                 // OK, we're compiling a snippet that returns a Word
  86                 return true;
  87             }
  88             assert actual == expected : "return kind doesn't match: actual " + actual + ", expected: " + expected;
  89         }
  90         return true;
  91     }
  92 }