comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java @ 18485:e3c95cbbb50c

Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Sun, 23 Nov 2014 16:07:23 -0800
parents 997bc9764a9a
children 0ef23ff7d5a1
comparison
equal deleted inserted replaced
18484:e97e1f07a3d6 18485:e3c95cbbb50c
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.sl.runtime; 23 package com.oracle.truffle.sl.runtime;
24 24
25 import java.io.*; 25 import java.io.*;
26 import java.util.*;
27 26
28 import com.oracle.truffle.api.*; 27 import com.oracle.truffle.api.*;
29 import com.oracle.truffle.api.dsl.*; 28 import com.oracle.truffle.api.dsl.*;
30 import com.oracle.truffle.api.frame.*; 29 import com.oracle.truffle.api.frame.*;
31 import com.oracle.truffle.api.instrument.*;
32 import com.oracle.truffle.api.nodes.*; 30 import com.oracle.truffle.api.nodes.*;
33 import com.oracle.truffle.api.object.*; 31 import com.oracle.truffle.api.object.*;
34 import com.oracle.truffle.api.source.*; 32 import com.oracle.truffle.api.source.*;
35 import com.oracle.truffle.sl.*; 33 import com.oracle.truffle.sl.*;
36 import com.oracle.truffle.sl.builtins.*; 34 import com.oracle.truffle.sl.builtins.*;
37 import com.oracle.truffle.sl.nodes.*; 35 import com.oracle.truffle.sl.nodes.*;
38 import com.oracle.truffle.sl.nodes.instrument.*;
39 import com.oracle.truffle.sl.nodes.local.*; 36 import com.oracle.truffle.sl.nodes.local.*;
40 import com.oracle.truffle.sl.parser.*; 37 import com.oracle.truffle.sl.parser.*;
41 38
42 /** 39 /**
43 * The run-time state of SL during execution. One context is instantiated before any source code is 40 * The run-time state of SL during execution. One context is instantiated before any source code is
54 51
55 private final BufferedReader input; 52 private final BufferedReader input;
56 private final PrintStream output; 53 private final PrintStream output;
57 private final SLFunctionRegistry functionRegistry; 54 private final SLFunctionRegistry functionRegistry;
58 private final Shape emptyShape; 55 private final Shape emptyShape;
59 private SourceCallback sourceCallback = null;
60 56
61 public SLContext(BufferedReader input, PrintStream output) { 57 public SLContext(BufferedReader input, PrintStream output) {
62 this.input = input; 58 this.input = input;
63 this.output = output; 59 this.output = output;
64 this.functionRegistry = new SLFunctionRegistry(); 60 this.functionRegistry = new SLFunctionRegistry();
68 } 64 }
69 65
70 @Override 66 @Override
71 public String getLanguageShortName() { 67 public String getLanguageShortName() {
72 return "Simple"; 68 return "Simple";
73 }
74
75 @Override
76 public void setSourceCallback(SourceCallback sourceCallback) {
77 this.sourceCallback = sourceCallback;
78 } 69 }
79 70
80 /** 71 /**
81 * Returns the default input, i.e., the source for the {@link SLReadlnBuiltin}. To allow unit 72 * Returns the default input, i.e., the source for the {@link SLReadlnBuiltin}. To allow unit
82 * testing, we do not use {@link System#in} directly. 73 * testing, we do not use {@link System#in} directly.
96 /** 87 /**
97 * Returns the registry of all functions that are currently defined. 88 * Returns the registry of all functions that are currently defined.
98 */ 89 */
99 public SLFunctionRegistry getFunctionRegistry() { 90 public SLFunctionRegistry getFunctionRegistry() {
100 return functionRegistry; 91 return functionRegistry;
101 }
102
103 public SourceCallback getSourceCallback() {
104 return sourceCallback;
105 } 92 }
106 93
107 /** 94 /**
108 * Adds all builtin functions to the {@link SLFunctionRegistry}. This method lists all 95 * Adds all builtin functions to the {@link SLFunctionRegistry}. This method lists all
109 * {@link SLBuiltinNode builtin implementation classes}. 96 * {@link SLBuiltinNode builtin implementation classes}.
156 * this method will remove any previously added instrumentation. 143 * this method will remove any previously added instrumentation.
157 * 144 *
158 * @param source The {@link Source} to execute. 145 * @param source The {@link Source} to execute.
159 */ 146 */
160 public void executeMain(Source source) { 147 public void executeMain(Source source) {
161
162 if (sourceCallback != null) {
163 sourceCallback.startLoading(source);
164 }
165
166 Parser.parseSL(this, source); 148 Parser.parseSL(this, source);
167
168 List<SLFunction> functionList = getFunctionRegistry().getFunctions();
169
170 // Since only functions can be global in SL, this guarantees that we instrument
171 // everything of interest. Parsing must occur before accepting the visitors since
172 // the visitor which creates our instrumentation points expects a complete AST.
173
174 for (SLFunction function : functionList) {
175 RootCallTarget rootCallTarget = function.getCallTarget();
176 rootCallTarget.getRootNode().accept(new SLInstrumenter());
177 }
178
179 if (sourceCallback != null) {
180 sourceCallback.endLoading(source);
181 }
182
183 SLFunction main = getFunctionRegistry().lookup("main"); 149 SLFunction main = getFunctionRegistry().lookup("main");
184 if (main.getCallTarget() == null) { 150 if (main.getCallTarget() == null) {
185 throw new SLException("No function main() defined in SL source file."); 151 throw new SLException("No function main() defined in SL source file.");
186 } 152 }
187 main.getCallTarget().call(); 153 main.getCallTarget().call();