comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLInvokeNode.java @ 13943:89ac75425681

SL: small cleanups
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 12 Feb 2014 10:30:42 -0800
parents graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLCallNode.java@afd6fa5e8229
children a08b8694f556
comparison
equal deleted inserted replaced
13942:1ee27cd07ed0 13943:89ac75425681
1 /*
2 * Copyright (c) 2013, 2014, 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 com.oracle.truffle.sl.nodes.call;
24
25 import com.oracle.truffle.api.*;
26 import com.oracle.truffle.api.dsl.*;
27 import com.oracle.truffle.api.frame.*;
28 import com.oracle.truffle.api.nodes.*;
29 import com.oracle.truffle.sl.nodes.*;
30 import com.oracle.truffle.sl.runtime.*;
31
32 /**
33 * The node for function invocation in SL. Since SL has first class functions, the
34 * {@link SLFunction target function} can be computed by an {@link #functionNode arbitrary
35 * expression}. This node is responsible for evaluating this expression, as well as evaluating the
36 * {@link #argumentNodes arguments}. The actual dispatch is then delegated to a chain of
37 * {@link SLAbstractDispatchNode}s that form a polymorphic inline cache.
38 */
39 @NodeInfo(shortName = "invoke")
40 public final class SLInvokeNode extends SLExpressionNode {
41
42 public static SLInvokeNode create(SLExpressionNode function, SLExpressionNode[] arguments) {
43 return new SLInvokeNode(function, arguments, new SLUninitializedDispatchNode());
44 }
45
46 @Child protected SLExpressionNode functionNode;
47 @Children protected final SLExpressionNode[] argumentNodes;
48 @Child protected SLAbstractDispatchNode dispatchNode;
49
50 private SLInvokeNode(SLExpressionNode functionNode, SLExpressionNode[] argumentNodes, SLAbstractDispatchNode dispatchNode) {
51 this.functionNode = adoptChild(functionNode);
52 this.argumentNodes = adoptChildren(argumentNodes);
53 this.dispatchNode = adoptChild(dispatchNode);
54 }
55
56 @Override
57 @ExplodeLoop
58 public Object executeGeneric(VirtualFrame frame) {
59 SLFunction function = evaluateFunction(frame);
60
61 /*
62 * The number of arguments is constant for one invoke node. During compilation, the loop is
63 * unrolled and the execute methods of all arguments are inlined. This is triggered by the
64 * ExplodeLoop annotation on the method. The compiler assertion below illustrates that the
65 * array length is really constant.
66 */
67 CompilerAsserts.compilationConstant(argumentNodes.length);
68
69 Object[] argumentValues = new Object[argumentNodes.length];
70 for (int i = 0; i < argumentNodes.length; i++) {
71 argumentValues[i] = argumentNodes[i].executeGeneric(frame);
72 }
73 SLArguments arguments = new SLArguments(argumentValues);
74
75 return dispatchNode.executeDispatch(frame, function, arguments);
76 }
77
78 private SLFunction evaluateFunction(VirtualFrame frame) {
79 try {
80 /*
81 * The function node must evaluate to a SLFunction value, so we call
82 * function-specialized method.
83 */
84 return functionNode.executeFunction(frame);
85 } catch (UnexpectedResultException ex) {
86 /*
87 * The function node evaluated to a non-function result. This is a type error in the SL
88 * program. We report it with the same exception that Truffle DSL generated nodes use to
89 * report type errors.
90 */
91 throw new UnsupportedSpecializationException(this, new Node[]{functionNode}, ex.getResult());
92 }
93 }
94 }