comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/SLRootNode.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 533b21375e58
children 5243fe9a3fbc
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
1 /* 1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 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; 23 package com.oracle.truffle.sl.nodes;
24 24
25 import com.oracle.truffle.api.*;
26 import com.oracle.truffle.api.dsl.*;
27 import com.oracle.truffle.api.frame.*; 25 import com.oracle.truffle.api.frame.*;
28 import com.oracle.truffle.api.nodes.*; 26 import com.oracle.truffle.api.nodes.*;
29 import com.oracle.truffle.sl.builtins.*; 27 import com.oracle.truffle.sl.builtins.*;
30 import com.oracle.truffle.sl.nodes.controlflow.*; 28 import com.oracle.truffle.sl.nodes.controlflow.*;
31 import com.oracle.truffle.sl.nodes.local.*;
32 import com.oracle.truffle.sl.runtime.*;
33 29
30 /**
31 * The root of all SL execution trees. It is a Truffle requirement that the tree root extends the
32 * class {@link RootNode}. This class is used for both builtin and user-defined functions. For
33 * builtin functions, the {@link #bodyNode} is a subclass of {@link SLBuiltinNode}. For user-defined
34 * functions, the {@link #bodyNode} is a {@link SLFunctionBodyNode}.
35 */
34 public final class SLRootNode extends RootNode { 36 public final class SLRootNode extends RootNode {
35 37
36 @Child private SLExpressionNode body; 38 /** The function body that is executed, and specialized during execution. */
39 @Child private SLExpressionNode bodyNode;
37 40
38 private final SLExpressionNode uninitializedBody; 41 /**
42 * A copy of the uninitialized body. When performing method inlining, it is beneficial to inline
43 * the unspecialized function body, so that it is specialized in the context of the caller. This
44 * makes the specializations of the inlined function more precise.
45 */
46 private final SLExpressionNode uninitializedBodyNode;
47
48 /** The name of the function, for printing purposes only. */
39 private final String name; 49 private final String name;
40 private final boolean inlineImmediatly;
41 50
42 public static RootCallTarget createFunction(String name, FrameDescriptor frameDescriptor, SLStatementNode body) { 51 public SLRootNode(FrameDescriptor frameDescriptor, SLExpressionNode bodyNode, String name) {
43 SLFunctionBodyNode bodyContainer = new SLFunctionBodyNode(frameDescriptor, body);
44 SLRootNode root = new SLRootNode(frameDescriptor, bodyContainer, name, false);
45 return Truffle.getRuntime().createCallTarget(root);
46 }
47
48 public static RootCallTarget createBuiltin(SLContext context, NodeFactory<? extends SLBuiltinNode> factory, String name) {
49 int argumentCount = factory.getExecutionSignature().size();
50 SLExpressionNode[] arguments = new SLExpressionNode[argumentCount];
51 for (int i = 0; i < arguments.length; i++) {
52 arguments[i] = new SLReadArgumentNode(i);
53 }
54 SLBuiltinNode buitinBody = factory.createNode(arguments, context);
55 SLRootNode root = new SLRootNode(new FrameDescriptor(), buitinBody, name, true);
56 return Truffle.getRuntime().createCallTarget(root);
57 }
58
59 private SLRootNode(FrameDescriptor frameDescriptor, SLExpressionNode body, String name, boolean inlineImmediatly) {
60 super(null, frameDescriptor); 52 super(null, frameDescriptor);
61 this.uninitializedBody = NodeUtil.cloneNode(body); 53 /* Deep copy the body before any specialization occurs during execution. */
62 this.body = adoptChild(body); 54 this.uninitializedBodyNode = NodeUtil.cloneNode(bodyNode);
55 this.bodyNode = adoptChild(bodyNode);
63 this.name = name; 56 this.name = name;
64 this.inlineImmediatly = inlineImmediatly;
65 } 57 }
66 58
67 @Override 59 @Override
68 public Object execute(VirtualFrame frame) { 60 public Object execute(VirtualFrame frame) {
69 return body.executeGeneric(frame); 61 return bodyNode.executeGeneric(frame);
70 }
71
72 public boolean isInlineImmediatly() {
73 return inlineImmediatly;
74 } 62 }
75 63
76 @Override 64 @Override
77 public RootNode inline() { 65 public RootNode inline() {
78 return new SLRootNode(getFrameDescriptor().shallowCopy(), NodeUtil.cloneNode(uninitializedBody), name, inlineImmediatly); 66 return new SLRootNode(getFrameDescriptor().shallowCopy(), NodeUtil.cloneNode(uninitializedBodyNode), name);
79 } 67 }
80 68
81 @Override 69 @Override
82 public int getInlineNodeCount() { 70 public int getInlineNodeCount() {
83 return NodeUtil.countNodes(uninitializedBody); 71 return NodeUtil.countNodes(uninitializedBodyNode);
84 } 72 }
85 73
86 @Override 74 @Override
87 public boolean isInlinable() { 75 public boolean isInlinable() {
88 return true; 76 return true;
89 } 77 }
90 78
91 public Node getUninitializedBody() {
92 return uninitializedBody;
93 }
94
95 @Override 79 @Override
96 public String toString() { 80 public String toString() {
97 return "function " + name; 81 return "root " + name;
98 } 82 }
99 } 83 }