Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java @ 21557:31fc2fce38f3
Merge.
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 27 May 2015 13:32:18 +0200 |
parents | b1530a6cce8c 99e3f4c5c853 |
children | a880844225e4 |
comparison
equal
deleted
inserted
replaced
21556:48c1ebd24120 | 21557:31fc2fce38f3 |
---|---|
22 */ | 22 */ |
23 package com.oracle.truffle.sl; | 23 package com.oracle.truffle.sl; |
24 | 24 |
25 import java.io.*; | 25 import java.io.*; |
26 import java.math.*; | 26 import java.math.*; |
27 import java.net.*; | |
28 import java.util.*; | |
27 import java.util.Scanner; | 29 import java.util.Scanner; |
28 | 30 |
29 import com.oracle.truffle.api.*; | 31 import com.oracle.truffle.api.*; |
30 import com.oracle.truffle.api.dsl.*; | 32 import com.oracle.truffle.api.dsl.*; |
31 import com.oracle.truffle.api.instrument.*; | 33 import com.oracle.truffle.api.instrument.*; |
32 import com.oracle.truffle.api.nodes.*; | 34 import com.oracle.truffle.api.nodes.*; |
33 import com.oracle.truffle.api.source.*; | 35 import com.oracle.truffle.api.source.*; |
34 import com.oracle.truffle.api.tools.*; | 36 import com.oracle.truffle.api.tools.*; |
35 import com.oracle.truffle.api.vm.*; | 37 import com.oracle.truffle.api.vm.*; |
38 import com.oracle.truffle.api.vm.TruffleVM.Symbol; | |
36 import com.oracle.truffle.sl.builtins.*; | 39 import com.oracle.truffle.sl.builtins.*; |
37 import com.oracle.truffle.sl.factory.*; | 40 import com.oracle.truffle.sl.factory.*; |
38 import com.oracle.truffle.sl.nodes.*; | 41 import com.oracle.truffle.sl.nodes.*; |
39 import com.oracle.truffle.sl.nodes.call.*; | 42 import com.oracle.truffle.sl.nodes.call.*; |
40 import com.oracle.truffle.sl.nodes.controlflow.*; | 43 import com.oracle.truffle.sl.nodes.controlflow.*; |
130 * <em>default printer</em>. | 133 * <em>default printer</em>. |
131 * | 134 * |
132 */ | 135 */ |
133 @TruffleLanguage.Registration(name = "sl", mimeType = "application/x-sl") | 136 @TruffleLanguage.Registration(name = "sl", mimeType = "application/x-sl") |
134 public class SLMain extends TruffleLanguage { | 137 public class SLMain extends TruffleLanguage { |
138 private static SLMain LAST; | |
139 private static List<NodeFactory<? extends SLBuiltinNode>> builtins = Collections.emptyList(); | |
135 private final SLContext context; | 140 private final SLContext context; |
136 | 141 |
137 public SLMain(Env env) { | 142 public SLMain(Env env) { |
138 super(env); | 143 super(env); |
139 this.context = SLContextFactory.create(new BufferedReader(new InputStreamReader(System.in)), new PrintWriter(System.out)); | 144 context = SLContextFactory.create(new BufferedReader(env().stdIn()), new PrintWriter(env().stdOut(), true)); |
145 LAST = this; | |
146 for (NodeFactory<? extends SLBuiltinNode> builtin : builtins) { | |
147 context.installBuiltin(builtin); | |
148 } | |
140 } | 149 } |
141 | 150 |
142 /* Demonstrate per-type tabulation of node execution counts */ | 151 /* Demonstrate per-type tabulation of node execution counts */ |
143 private static boolean nodeExecCounts = false; | 152 private static boolean nodeExecCounts = false; |
144 /* Demonstrate per-line tabulation of STATEMENT node execution counts */ | 153 /* Demonstrate per-line tabulation of STATEMENT node execution counts */ |
148 | 157 |
149 /** | 158 /** |
150 * The main entry point. Use the mx command "mx sl" to run it with the correct class path setup. | 159 * The main entry point. Use the mx command "mx sl" to run it with the correct class path setup. |
151 */ | 160 */ |
152 public static void main(String[] args) throws IOException { | 161 public static void main(String[] args) throws IOException { |
153 TruffleVM vm = TruffleVM.create(); | 162 TruffleVM vm = TruffleVM.newVM().build(); |
154 assert vm.getLanguages().containsKey("application/x-sl"); | 163 assert vm.getLanguages().containsKey("application/x-sl"); |
155 | 164 |
156 int repeats = 1; | 165 int repeats = 1; |
157 if (args.length >= 2) { | 166 if (args.length >= 2) { |
158 repeats = Integer.parseInt(args[1]); | 167 repeats = Integer.parseInt(args[1]); |
159 } | 168 } |
160 | 169 |
170 if (args.length == 0) { | |
171 vm.eval("application/x-sl", new InputStreamReader(System.in)); | |
172 } else { | |
173 vm.eval(new File(args[0]).toURI()); | |
174 } | |
175 Symbol main = vm.findGlobalSymbol("main"); | |
176 if (main == null) { | |
177 throw new SLException("No function main() defined in SL source file."); | |
178 } | |
161 while (repeats-- > 0) { | 179 while (repeats-- > 0) { |
162 if (args.length == 0) { | 180 main.invoke(null); |
163 vm.eval("application/x-sl", new InputStreamReader(System.in)); | |
164 } else { | |
165 vm.eval(new File(args[0]).toURI()); | |
166 } | |
167 } | 181 } |
168 } | 182 } |
169 | 183 |
170 /** | 184 /** |
171 * Parse and run the specified SL source. Factored out in a separate method so that it can also | 185 * Parse and run the specified SL source. Factored out in a separate method so that it can also |
172 * be used by the unit test harness. | 186 * be used by the unit test harness. |
173 */ | 187 */ |
174 public static long run(SLContext context, Source source, PrintWriter logOutput, int repeats) { | 188 public static long run(TruffleVM context, URI source, PrintWriter logOutput, PrintWriter out, int repeats, List<NodeFactory<? extends SLBuiltinNode>> currentBuiltins) throws IOException { |
189 builtins = currentBuiltins; | |
190 | |
175 if (logOutput != null) { | 191 if (logOutput != null) { |
176 logOutput.println("== running on " + Truffle.getRuntime().getName()); | 192 logOutput.println("== running on " + Truffle.getRuntime().getName()); |
177 // logOutput.println("Source = " + source.getCode()); | 193 // logOutput.println("Source = " + source.getCode()); |
178 } | 194 } |
179 | 195 |
198 coverageTracker = new CoverageTracker(); | 214 coverageTracker = new CoverageTracker(); |
199 coverageTracker.install(); | 215 coverageTracker.install(); |
200 } | 216 } |
201 | 217 |
202 /* Parse the SL source file. */ | 218 /* Parse the SL source file. */ |
203 Parser.parseSL(context, source); | 219 Object result = context.eval(source); |
220 if (result != null) { | |
221 out.println(result); | |
222 } | |
204 | 223 |
205 /* Lookup our main entry point, which is per definition always named "main". */ | 224 /* Lookup our main entry point, which is per definition always named "main". */ |
206 SLFunction main = context.getFunctionRegistry().lookup("main"); | 225 Symbol main = context.findGlobalSymbol("main"); |
207 if (main.getCallTarget() == null) { | 226 if (main == null) { |
208 throw new SLException("No function main() defined in SL source file."); | 227 throw new SLException("No function main() defined in SL source file."); |
209 } | 228 } |
210 | 229 |
211 /* Change to true if you want to see the AST on the console. */ | 230 /* Change to true if you want to see the AST on the console. */ |
212 boolean printASTToLog = false; | 231 boolean printASTToLog = false; |
213 /* Change to true if you want to see source attribution for the AST to the console */ | 232 /* Change to true if you want to see source attribution for the AST to the console */ |
214 boolean printSourceAttributionToLog = false; | 233 boolean printSourceAttributionToLog = false; |
215 /* Change to dump the AST to IGV over the network. */ | 234 /* Change to dump the AST to IGV over the network. */ |
216 boolean dumpASTToIGV = false; | 235 boolean dumpASTToIGV = false; |
217 | 236 |
218 printScript("before execution", context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); | 237 printScript("before execution", LAST.context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); |
219 long totalRuntime = 0; | 238 long totalRuntime = 0; |
220 try { | 239 try { |
221 for (int i = 0; i < repeats; i++) { | 240 for (int i = 0; i < repeats; i++) { |
222 long start = System.nanoTime(); | 241 long start = System.nanoTime(); |
223 /* Call the main entry point, without any arguments. */ | 242 /* Call the main entry point, without any arguments. */ |
224 try { | 243 try { |
225 Object result = main.getCallTarget().call(); | 244 result = main.invoke(null); |
226 if (result != SLNull.SINGLETON) { | 245 if (result != SLNull.SINGLETON) { |
227 context.getOutput().println(result); | 246 out.println(result); |
228 } | 247 } |
229 } catch (UnsupportedSpecializationException ex) { | 248 } catch (UnsupportedSpecializationException ex) { |
230 context.getOutput().println(formatTypeError(ex)); | 249 out.println(formatTypeError(ex)); |
250 } catch (SLUndefinedFunctionException ex) { | |
251 out.println(String.format("Undefined function: %s", ex.getFunctionName())); | |
231 } | 252 } |
232 long end = System.nanoTime(); | 253 long end = System.nanoTime(); |
233 totalRuntime += end - start; | 254 totalRuntime += end - start; |
234 | 255 |
235 if (logOutput != null && repeats > 1) { | 256 if (logOutput != null && repeats > 1) { |
236 logOutput.println("== iteration " + (i + 1) + ": " + ((end - start) / 1000000) + " ms"); | 257 logOutput.println("== iteration " + (i + 1) + ": " + ((end - start) / 1000000) + " ms"); |
237 } | 258 } |
238 } | 259 } |
239 | 260 |
240 } finally { | 261 } finally { |
241 printScript("after execution", context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); | 262 printScript("after execution", LAST.context, logOutput, printASTToLog, printSourceAttributionToLog, dumpASTToIGV); |
242 } | 263 } |
243 if (nodeExecCounter != null) { | 264 if (nodeExecCounter != null) { |
244 nodeExecCounter.print(System.out); | 265 nodeExecCounter.print(System.out); |
245 nodeExecCounter.dispose(); | 266 nodeExecCounter.dispose(); |
246 } | 267 } |
305 StringBuilder result = new StringBuilder(); | 326 StringBuilder result = new StringBuilder(); |
306 result.append("Type error"); | 327 result.append("Type error"); |
307 if (ex.getNode() != null && ex.getNode().getSourceSection() != null) { | 328 if (ex.getNode() != null && ex.getNode().getSourceSection() != null) { |
308 SourceSection ss = ex.getNode().getSourceSection(); | 329 SourceSection ss = ex.getNode().getSourceSection(); |
309 if (ss != null && !(ss instanceof NullSourceSection)) { | 330 if (ss != null && !(ss instanceof NullSourceSection)) { |
310 result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn()); | 331 result.append(" at ").append(ss.getSource().getShortName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn()); |
311 } | 332 } |
312 } | 333 } |
313 result.append(": operation"); | 334 result.append(": operation"); |
314 if (ex.getNode() != null) { | 335 if (ex.getNode() != null) { |
315 NodeInfo nodeInfo = SLContext.lookupNodeInfo(ex.getNode().getClass()); | 336 NodeInfo nodeInfo = SLContext.lookupNodeInfo(ex.getNode().getClass()); |
354 return null; | 375 return null; |
355 } | 376 } |
356 | 377 |
357 @Override | 378 @Override |
358 protected Object findExportedSymbol(String globalName) { | 379 protected Object findExportedSymbol(String globalName) { |
380 for (SLFunction f : context.getFunctionRegistry().getFunctions()) { | |
381 if (globalName.equals(f.getName())) { | |
382 return f; | |
383 } | |
384 } | |
359 return null; | 385 return null; |
360 } | 386 } |
361 | 387 |
362 @Override | 388 @Override |
363 protected Object getLanguageGlobal() { | 389 protected Object getLanguageGlobal() { |
364 return null; | 390 return context; |
365 } | 391 } |
366 | 392 |
367 @Override | 393 @Override |
368 protected boolean isObjectOfLanguage(Object object) { | 394 protected boolean isObjectOfLanguage(Object object) { |
369 return false; | 395 return object instanceof SLFunction; |
370 } | 396 } |
371 | 397 |
372 } | 398 } |