annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @ 16702:2a5ec181dad4

SL: Added instrumentation testing
author David Piorkowski <david.piorkowski@oracle.com>
date Tue, 05 Aug 2014 16:34:08 -0700
parents 7c8ddb4233cd
children 7661cc464239
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
1 /*
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
4 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
8 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
13 * accompanied this code).
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
14 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
18 *
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
21 * questions.
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
22 */
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
23 package com.oracle.truffle.sl.parser;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
24
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
25 import java.math.*;
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
26 import java.util.*;
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
27
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
28 import com.oracle.truffle.api.frame.*;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
29 import com.oracle.truffle.api.nodes.*;
16067
915ebb306fcc Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 15631
diff changeset
30 import com.oracle.truffle.api.source.*;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
31 import com.oracle.truffle.sl.nodes.*;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
32 import com.oracle.truffle.sl.nodes.call.*;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
33 import com.oracle.truffle.sl.nodes.controlflow.*;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
34 import com.oracle.truffle.sl.nodes.expression.*;
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
35 import com.oracle.truffle.sl.nodes.instrument.*;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
36 import com.oracle.truffle.sl.nodes.local.*;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
37 import com.oracle.truffle.sl.runtime.*;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
38
13836
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
39 /**
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
40 * Helper class used by the SL {@link Parser} to create nodes. The code is factored out of the
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
41 * automatically generated parser to keep the attributed grammar of SL small.
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
42 */
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
43 public class SLNodeFactory {
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
44
13836
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
45 /**
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
46 * Local variable names that are visible in the current block. Variables are not visible outside
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
47 * of their defining block, to prevent the usage of undefined variables. Because of that, we can
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
48 * decide during parsing if a name references a local variable or is a function name.
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
49 */
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
50 static class LexicalScope {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
51 protected final LexicalScope outer;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
52 protected final Map<String, FrameSlot> locals;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
53
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
54 public LexicalScope(LexicalScope outer) {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
55 this.outer = outer;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
56 this.locals = new HashMap<>();
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
57 if (outer != null) {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
58 locals.putAll(outer.locals);
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
59 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
60 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
61 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
62
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
63 /* State while parsing a source unit. */
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
64 private final SLContext context;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
65 private final Source source;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
66
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
67 /* State while parsing a function. */
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
68 private int functionStartPos;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
69 private String functionName;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
70 private int functionBodyStartPos; // includes parameter list
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
71 private int parameterCount;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
72 private FrameDescriptor frameDescriptor;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
73 private List<SLStatementNode> methodNodes;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
74
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
75 /* State while parsing a block. */
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
76 private LexicalScope lexicalScope;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
77
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
78 private final SLNodeProber prober;
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
79
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
80 public SLNodeFactory(SLContext context, Source source, SLNodeProber prober) {
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
81 this.context = context;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
82 this.source = source;
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
83 this.prober = prober;
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
84 }
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
85
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
86 public void startFunction(Token nameToken, int bodyStartPos) {
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
87 assert functionStartPos == 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
88 assert functionName == null;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
89 assert functionBodyStartPos == 0;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
90 assert parameterCount == 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
91 assert frameDescriptor == null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
92 assert lexicalScope == null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
93
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
94 functionStartPos = nameToken.charPos;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
95 functionName = nameToken.val;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
96 functionBodyStartPos = bodyStartPos;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9222
diff changeset
97 frameDescriptor = new FrameDescriptor();
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
98 methodNodes = new ArrayList<>();
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
99 startBlock();
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
100 }
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
101
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
102 public void addFormalParameter(Token nameToken) {
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
103 /*
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
104 * Method parameters are assigned to local variables at the beginning of the method. This
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
105 * ensures that accesses to parameters are specialized the same way as local variables are
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
106 * specialized.
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
107 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
108 final SourceSection src = srcFromToken(nameToken);
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
109 final SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
110 methodNodes.add(createAssignment(nameToken, readArg));
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
111 parameterCount++;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
112 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
113
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
114 public void finishFunction(SLStatementNode bodyNode) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
115 methodNodes.add(bodyNode);
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
116 final int bodyEndPos = bodyNode.getSourceSection().getCharEndIndex();
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
117 final SourceSection functionSrc = source.createSection(functionName, functionStartPos, bodyEndPos - functionStartPos);
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
118 final SLStatementNode methodBlock = finishBlock(methodNodes, functionBodyStartPos, bodyEndPos - functionBodyStartPos);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
119 assert lexicalScope == null : "Wrong scoping of blocks in parser";
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
120
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
121 final SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(functionSrc, methodBlock);
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
122 final SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
123
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
124 context.getFunctionRegistry().register(functionName, rootNode);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
125
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
126 functionStartPos = 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
127 functionName = null;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
128 functionBodyStartPos = 0;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
129 parameterCount = 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
130 frameDescriptor = null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
131 lexicalScope = null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
132 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
133
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
134 public void startBlock() {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
135 lexicalScope = new LexicalScope(lexicalScope);
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
136 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
137
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
138 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int startPos, int length) {
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
139 lexicalScope = lexicalScope.outer;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
140
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
141 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size());
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
142 flattenBlocks(bodyNodes, flattenedNodes);
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
143
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
144 final SourceSection src = source.createSection("block", startPos, length);
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
145 return new SLBlockNode(src, flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()]));
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
146 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
147
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
148 private void flattenBlocks(Iterable<? extends Node> bodyNodes, List<SLStatementNode> flattenedNodes) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
149 for (Node n : bodyNodes) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
150 if (n instanceof SLBlockNode) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
151 flattenBlocks(n.getChildren(), flattenedNodes);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
152 } else {
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
153 flattenedNodes.add((SLStatementNode) n);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
154 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
155 }
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
156 }
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
157
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
158 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
159 * Returns a {@link SLBreakNode} for the given token. This node will be instrumented, tagged as
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
160 * a statement and wrapped in an {@link SLStatementWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
161 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
162 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
163 * @param breakToken The token containing the break node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
164 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
165 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
166 * <li>An un-instrumented SLBreakNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
167 * <li>An {@link SLStatementWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
168 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
169 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
170 public SLStatementNode createBreak(Token breakToken) {
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
171 final SLBreakNode breakNode = new SLBreakNode(srcFromToken(breakToken));
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
172 if (prober != null) {
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
173 return prober.probeAsStatement(breakNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
174 }
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
175 return breakNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
176 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
177
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
178 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
179 * Returns a {@link SLContinueNode} for the given token. This node will be instrumented, tagged
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
180 * as a statement and wrapped in an {@link SLStatementWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
181 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
182 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
183 * @param continueToken The token containing the continue node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
184 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
185 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
186 * <li>An un-instrumented SLContinueNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
187 * <li>An {@link SLStatementWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
188 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
189 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
190 public SLStatementNode createContinue(Token continueToken) {
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
191 final SLContinueNode continueNode = new SLContinueNode(srcFromToken(continueToken));
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
192 if (prober != null) {
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
193 return prober.probeAsStatement(continueNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
194 }
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
195 return continueNode;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
196 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
197
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
198 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
199 * Returns a {@link SLWhileNode} for the given token. This node will be instrumented, tagged as
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
200 * a statement and wrapped in an {@link SLStatementWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
201 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
202 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
203 * @param whileToken The token containing the while node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
204 * @param conditionNode The conditional node for this while loop
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
205 * @param bodyNode The body of the while loop
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
206 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
207 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
208 * <li>An un-instrumented SLWhileNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
209 * <li>An {@link SLStatementWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
210 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
211 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
212 public SLStatementNode createWhile(Token whileToken, SLExpressionNode conditionNode, SLStatementNode bodyNode) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
213 final int start = whileToken.charPos;
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
214 final int end = bodyNode.getSourceSection().getCharEndIndex();
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
215 final SLWhileNode whileNode = new SLWhileNode(source.createSection(whileToken.val, start, end - start), conditionNode, bodyNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
216 if (prober != null) {
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
217 return prober.probeAsStatement(whileNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
218 }
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
219 return whileNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
220 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
221
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
222 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
223 * Returns a {@link SLIfNode} for the given token. This node will be instrumented, tagged as a
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
224 * statement and wrapped in an {@link SLStatementWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
225 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
226 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
227 * @param ifToken The token containing the if node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
228 * @param conditionNode The condition node of this if statement
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
229 * @param thenPartNode The then part of the if
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
230 * @param elsePartNode The else part of the if
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
231 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
232 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
233 * <li>An un-instrumented SLIfNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
234 * <li>An {@link SLStatementWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
235 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
236 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
237 public SLStatementNode createIf(Token ifToken, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
238 final int start = ifToken.charPos;
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
239 final int end = elsePartNode == null ? thenPartNode.getSourceSection().getCharEndIndex() : elsePartNode.getSourceSection().getCharEndIndex();
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
240 final SLIfNode ifNode = new SLIfNode(source.createSection(ifToken.val, start, end - start), conditionNode, thenPartNode, elsePartNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
241 if (prober != null) {
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
242 return prober.probeAsStatement(ifNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
243 }
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
244 return ifNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
245 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
246
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
247 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
248 * Returns a {@link SLReturnNode} for the given token. This node will be instrumented, tagged as
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
249 * a statement and wrapped in an {@link SLStatementWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
250 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
251 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
252 * @param t The token containing the return node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
253 * @param valueNode The value of the return
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
254 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
255 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
256 * <li>An un-instrumented SLReturnNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
257 * <li>An {@link SLStatementWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
258 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
259 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
260 public SLStatementNode createReturn(Token t, SLExpressionNode valueNode) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
261 final int start = t.charPos;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
262 final int length = valueNode == null ? t.val.length() : valueNode.getSourceSection().getCharEndIndex() - start;
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
263 final SLReturnNode returnNode = new SLReturnNode(source.createSection(t.val, start, length), valueNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
264 if (prober != null) {
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
265 return prober.probeAsStatement(returnNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
266 }
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
267 return returnNode;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
268 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
269
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
270 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
271 * Returns the corresponding subclass of {@link SLExpressionNode} for binary expressions.
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
272 * </br>These nodes are currently not instrumented.
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
273 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
274 * @param opToken The operator of the binary expression
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
275 * @param leftNode The left node of the expression
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
276 * @param rightNode The right node of the expression
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
277 * @return A subclass of SLExpressionNode for the operation given by opToken.
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
278 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
279 public SLExpressionNode createBinary(Token opToken, SLExpressionNode leftNode, SLExpressionNode rightNode) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
280 int start = leftNode.getSourceSection().getCharIndex();
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
281 int length = rightNode.getSourceSection().getCharEndIndex() - start;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
282 final SourceSection src = source.createSection(opToken.val, start, length);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
283 switch (opToken.val) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
284 case "+":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
285 return SLAddNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
286 case "*":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
287 return SLMulNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
288 case "/":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
289 return SLDivNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
290 case "-":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
291 return SLSubNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
292 case "<":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
293 return SLLessThanNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
294 case "<=":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
295 return SLLessOrEqualNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
296 case ">":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
297 return SLLogicalNotNodeFactory.create(src, SLLessOrEqualNodeFactory.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
298 case ">=":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
299 return SLLogicalNotNodeFactory.create(src, SLLessThanNodeFactory.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
300 case "==":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
301 return SLEqualNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
302 case "!=":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
303 return SLLogicalNotNodeFactory.create(src, SLEqualNodeFactory.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
304 case "&&":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
305 return SLLogicalAndNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
306 case "||":
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
307 return SLLogicalOrNodeFactory.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
308 default:
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
309 throw new RuntimeException("unexpected operation: " + opToken.val);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
310 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
311 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
312
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
313 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
314 * Returns a {@link SLInvokeNode} for the given token. This node will be instrumented, tagged as
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
315 * a call and wrapped in an {@link SLExpressionWrapper} if an {@link SLASTProber} was
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
316 * initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
317 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
318 * @param nameToken The name of the function being called
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
319 * @param parameterNodes The parameters of the function call
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
320 * @param finalToken A token used to determine the end of the sourceSelection for this call
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
321 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
322 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
323 * <li>An un-instrumented SLInvokeNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
324 * <li>An {@link SLExpressionWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
325 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
326 */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
327 public SLExpressionNode createCall(Token nameToken, List<SLExpressionNode> parameterNodes, Token finalToken) {
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
328 final int startPos = nameToken.charPos;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
329 final int endPos = finalToken.charPos + finalToken.val.length();
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
330 final SourceSection src = source.createSection(nameToken.val, startPos, endPos - startPos);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
331 SLExpressionNode functionNode = createRead(nameToken);
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
332 if (prober != null) {
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
333 SLExpressionNode wrappedNode = prober.probeAsCall(functionNode, nameToken.val);
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
334 return SLInvokeNode.create(src, wrappedNode, parameterNodes.toArray(new SLExpressionNode[parameterNodes.size()]));
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
335 }
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
336 return SLInvokeNode.create(src, functionNode, parameterNodes.toArray(new SLExpressionNode[parameterNodes.size()]));
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
337 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
338
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
339 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
340 * Returns a {@link SLWriteLocalVariableNode} for the given token. This node will be
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
341 * instrumented, tagged as an assignment and wrapped in an {@link SLExpressionWrapper} if an
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
342 * {@link SLASTProber} was initialized in this class. ({@link #prober} != null)
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
343 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
344 * @param nameToken The name of the variable being assigned
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
345 * @param valueNode The value to be assigned
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
346 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
347 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
348 * <li>An un-instrumented SLWriteLocalVariableNode if there is no prober</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
349 * <li>An {@link SLExpressionWrapper} instrumenting this node.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
350 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
351 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
352 public SLExpressionNode createAssignment(Token nameToken, SLExpressionNode valueNode) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
353 FrameSlot frameSlot = frameDescriptor.findOrAddFrameSlot(nameToken.val);
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
354 lexicalScope.locals.put(nameToken.val, frameSlot);
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
355 final int start = nameToken.charPos;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
356 final int length = valueNode.getSourceSection().getCharEndIndex() - start;
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
357 if (prober != null) {
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
358 SLWriteLocalVariableNode writeNode = SLWriteLocalVariableNodeFactory.create(source.createSection("=", start, length), valueNode, frameSlot);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
359 final SLExpressionNode wrappedNode = prober.probeAsLocalAssignment(writeNode, nameToken.val);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
360 return wrappedNode;
16595
618d92152d3c SL: Added support for instrumentation.
David Piorkowski <david.piorkowski@oracle.com>
parents: 16512
diff changeset
361 }
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
362 return SLWriteLocalVariableNodeFactory.create(source.createSection("=", start, length), valueNode, frameSlot);
12752
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
363 }
71991b7a0f14 SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents: 12390
diff changeset
364
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
365 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
366 * Returns a {@link SLReadLocalVariableNode} if this read is a local variable or a
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
367 * {@link SLFunctionLiteralNode} if this read is global. In Simple, the only global names are
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
368 * functions. </br> There is currently no instrumentation for this node.
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
369 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
370 * @param nameToken The name of the variable/function being read
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
371 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
372 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
373 * <li>A SLReadLocalVariableNode representing the local variable being read.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
374 * <li>A SLFunctionLiteralNode representing the function definition</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
375 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
376 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
377 public SLExpressionNode createRead(Token nameToken) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
378 final FrameSlot frameSlot = lexicalScope.locals.get(nameToken.val);
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
379 final SourceSection src = srcFromToken(nameToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
380 if (frameSlot != null) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
381 /* Read of a local variable. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
382 return SLReadLocalVariableNodeFactory.create(src, frameSlot);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
383 } else {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
384 /* Read of a global name. In our language, the only global names are functions. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
385 return new SLFunctionLiteralNode(src, context.getFunctionRegistry().lookup(nameToken.val));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
386 }
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
387 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
388
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
389 public SLExpressionNode createStringLiteral(Token literalToken) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
390 /* Remove the trailing and ending " */
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
391 String literal = literalToken.val;
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
392 assert literal.length() >= 2 && literal.startsWith("\"") && literal.endsWith("\"");
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
393 final SourceSection src = srcFromToken(literalToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
394 literal = literal.substring(1, literal.length() - 1);
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
395
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
396 return new SLStringLiteralNode(src, literal);
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
397 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
398
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
399 public SLExpressionNode createNumericLiteral(Token literalToken) {
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
400 final SourceSection src = srcFromToken(literalToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
401 try {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
402 /* Try if the literal is small enough to fit into a long value. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
403 return new SLLongLiteralNode(src, Long.parseLong(literalToken.val));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
404 } catch (NumberFormatException ex) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
405 /* Overflow of long value, so fall back to BigInteger. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
406 return new SLBigIntegerLiteralNode(src, new BigInteger(literalToken.val));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
407 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
408 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
409
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
410 public SLExpressionNode createParenExpression(SLExpressionNode expressionNode, int start, int length) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
411 final SourceSection src = source.createSection("()", start, length);
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
412 return new SLParenExpressionNode(src, expressionNode);
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
413 }
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
414
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
415 /**
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
416 * Creates source description of a single token.
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
417 */
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
418 private SourceSection srcFromToken(Token token) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
419 return source.createSection(token.val, token.charPos, token.val.length());
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
420 }
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
421
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
422 }