comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/FunctionRootNode.java @ 12752:71991b7a0f14

SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Nov 2013 21:34:44 +0100
parents graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/FunctionDefinitionNode.java@82ec5f898e6c
children 652f24858aad
comparison
equal deleted inserted replaced
12712:882a0aadfed6 12752:71991b7a0f14
1 /*
2 * Copyright (c) 2012, 2013, 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;
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.builtins.*;
30 import com.oracle.truffle.sl.runtime.*;
31
32 public final class FunctionRootNode extends RootNode {
33
34 @Child private TypedNode body;
35
36 private final TypedNode uninitializedBody;
37 private final String name;
38 private final boolean alwaysInline;
39
40 private FunctionRootNode(TypedNode body, String name, boolean alwaysInline) {
41 super(null);
42 this.uninitializedBody = NodeUtil.cloneNode(body);
43 this.body = adoptChild(body);
44 this.name = name;
45 this.alwaysInline = alwaysInline;
46 }
47
48 public static CallTarget createBuiltin(SLContext context, NodeFactory<? extends BuiltinNode> factory, String name) {
49 int argumentCount = factory.getExecutionSignature().size();
50 TypedNode[] arguments = new TypedNode[argumentCount];
51 for (int i = 0; i < arguments.length; i++) {
52 arguments[i] = new ReadArgumentNode(i);
53 }
54 BuiltinNode buitinBody = factory.createNode(arguments, context);
55 FunctionRootNode root = new FunctionRootNode(buitinBody, name, true);
56 return Truffle.getRuntime().createCallTarget(root);
57 }
58
59 public static CallTarget createFunction(StatementNode body, FrameDescriptor frameDescriptor, String name, TypedNode returnValue, String[] parameterNames) {
60 FunctionBodyNode bodyContainer = new FunctionBodyNode(frameDescriptor, body, returnValue, parameterNames);
61 FunctionRootNode root = new FunctionRootNode(bodyContainer, name, false);
62 return Truffle.getRuntime().createCallTarget(root, frameDescriptor);
63 }
64
65 @Override
66 public Object execute(VirtualFrame frame) {
67 return body.executeGeneric(frame);
68 }
69
70 public boolean isAlwaysInline() {
71 return alwaysInline;
72 }
73
74 public TypedNode inline(ArgumentsNode clonedArgs) {
75 TypedNode clonedBody = NodeUtil.cloneNode(uninitializedBody);
76 if (clonedBody instanceof BuiltinNode) {
77 return inlineBuiltin(clonedArgs, (BuiltinNode) clonedBody);
78 } else if (clonedBody instanceof FunctionBodyNode) {
79 return inlineFunction(clonedArgs, (FunctionBodyNode) clonedBody);
80 } else {
81 throw new UnsupportedOperationException();
82 }
83 }
84
85 private InlinedFunctionNode inlineFunction(ArgumentsNode clonedArgs, FunctionBodyNode clonedBody) {
86 return new InlinedFunctionNode(getCallTarget(), clonedBody, clonedArgs);
87 }
88
89 private static TypedNode inlineBuiltin(ArgumentsNode clonedArgs, BuiltinNode builtin) {
90 TypedNode[] callerArgs = clonedArgs.getArguments();
91 TypedNode[] builtinArgs = builtin.getArguments();
92 for (int i = 0; i < builtinArgs.length; i++) {
93 if (i < callerArgs.length) {
94 builtinArgs[i].replace(callerArgs[i]);
95 } else {
96 builtinArgs[i].replace(new NullLiteralNode());
97 }
98 }
99 return builtin;
100 }
101
102 public Node getUninitializedBody() {
103 return uninitializedBody;
104 }
105
106 @Override
107 public String toString() {
108 return "function " + name;
109 }
110
111 private static final class InlinedFunctionNode extends TypedNode implements InlinedCallSite {
112
113 @Child private FunctionBodyNode body;
114 @Child private ArgumentsNode arguments;
115
116 private final CallTarget callTarget;
117 private final FrameDescriptor frameDescriptor;
118
119 public InlinedFunctionNode(CallTarget callTarget, FunctionBodyNode body, ArgumentsNode arguments) {
120 this.callTarget = callTarget;
121 this.body = adoptChild(body);
122 this.frameDescriptor = body.getFrameDescriptor();
123 this.arguments = adoptChild(arguments);
124 }
125
126 @Override
127 public Object executeGeneric(VirtualFrame frame) {
128 SLArguments args = new SLArguments(arguments.executeArray(frame));
129 VirtualFrame childFrame = Truffle.getRuntime().createVirtualFrame(frame.pack(), args, frameDescriptor);
130 return body.executeGeneric(childFrame);
131 }
132
133 public CallTarget getCallTarget() {
134 return callTarget;
135 }
136
137 }
138
139 public String getName() {
140 return name;
141 }
142
143 }