comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/call/SLCallNode.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents 7c418666c6c9
children f9b934e1e172
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
1 /* 1 /*
2 * Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved. 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. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.sl.nodes.call; 23 package com.oracle.truffle.sl.nodes.call;
24 24
25 import com.oracle.truffle.api.dsl.*;
25 import com.oracle.truffle.api.frame.*; 26 import com.oracle.truffle.api.frame.*;
26 import com.oracle.truffle.api.nodes.*; 27 import com.oracle.truffle.api.nodes.*;
27 import com.oracle.truffle.sl.nodes.*; 28 import com.oracle.truffle.sl.nodes.*;
28 import com.oracle.truffle.sl.runtime.*; 29 import com.oracle.truffle.sl.runtime.*;
29 30
31 /**
32 * The node for a function call in SL. Since SL has first class functions, the {@link SLFunction
33 * target function} can be computed by an {@link #functionNode arbitrary expression}. This node is
34 * responsible for evaluating this expression, as well as evaluating the {@link #argumentNodes
35 * arguments}. The actual call dispatch is then delegated to a chain of
36 * {@link SLAbstractDispatchNode}s that form a polymorphic inline cache.
37 */
38 @NodeInfo(shortName = "call")
30 public final class SLCallNode extends SLExpressionNode { 39 public final class SLCallNode extends SLExpressionNode {
31 40
32 public static SLCallNode create(SLExpressionNode function, SLExpressionNode[] arguments) { 41 public static SLCallNode create(SLExpressionNode function, SLExpressionNode[] arguments) {
33 return new SLCallNode(function, arguments, new SLUninitializedCallNode()); 42 return new SLCallNode(function, arguments, new SLUninitializedDispatchNode());
34 } 43 }
35 44
36 @Child protected SLExpressionNode functionNode; 45 @Child protected SLExpressionNode functionNode;
37 @Children protected final SLExpressionNode[] argumentNodes; 46 @Children protected final SLExpressionNode[] argumentNodes;
38 @Child protected SLAbstractDispatchNode dispatchNode; 47 @Child protected SLAbstractDispatchNode dispatchNode;
44 } 53 }
45 54
46 @Override 55 @Override
47 @ExplodeLoop 56 @ExplodeLoop
48 public Object executeGeneric(VirtualFrame frame) { 57 public Object executeGeneric(VirtualFrame frame) {
49 SLFunction function; 58 SLFunction function = evaluateFunction(frame);
50 try {
51 function = functionNode.executeFunction(frame);
52 } catch (UnexpectedResultException e) {
53 throw new UnsupportedOperationException("Call to " + e.getMessage() + " not supported.");
54 }
55 59
56 Object[] argumentValues = new Object[argumentNodes.length]; 60 Object[] argumentValues = new Object[argumentNodes.length];
57 for (int i = 0; i < argumentNodes.length; i++) { 61 for (int i = 0; i < argumentNodes.length; i++) {
58 argumentValues[i] = argumentNodes[i].executeGeneric(frame); 62 argumentValues[i] = argumentNodes[i].executeGeneric(frame);
59 } 63 }
60 SLArguments arguments = new SLArguments(argumentValues); 64 SLArguments arguments = new SLArguments(argumentValues);
61 65
62 return dispatchNode.executeCall(frame, function, arguments); 66 return dispatchNode.executeDispatch(frame, function, arguments);
67 }
68
69 private SLFunction evaluateFunction(VirtualFrame frame) {
70 try {
71 /*
72 * The function node must evaluate to a SLFunction value, so we call
73 * function-specialized method.
74 */
75 return functionNode.executeFunction(frame);
76 } catch (UnexpectedResultException ex) {
77 /*
78 * The function node evaluated to a non-function result. This is a type error in the SL
79 * program. We report it with the same exception that Truffle DSL generated nodes use to
80 * report type errors.
81 */
82 throw new UnsupportedSpecializationException(this, ex.getResult());
83 }
63 } 84 }
64 } 85 }