Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLLanguage.java @ 22046:e7c2d36daf72
TruffleLanguage.parse method to convert a source to CallTarget. Basic caching to make sure the code is shared among tenants in one JVM.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 30 Jul 2015 17:36:34 +0200 |
parents | 5bc7f7b867ab |
children | 227ed03852de |
comparison
equal
deleted
inserted
replaced
22045:ffbc7f472438 | 22046:e7c2d36daf72 |
---|---|
54 import com.oracle.truffle.api.nodes.*; | 54 import com.oracle.truffle.api.nodes.*; |
55 import com.oracle.truffle.api.source.*; | 55 import com.oracle.truffle.api.source.*; |
56 import com.oracle.truffle.api.vm.*; | 56 import com.oracle.truffle.api.vm.*; |
57 import com.oracle.truffle.api.vm.TruffleVM.Symbol; | 57 import com.oracle.truffle.api.vm.TruffleVM.Symbol; |
58 import com.oracle.truffle.sl.builtins.*; | 58 import com.oracle.truffle.sl.builtins.*; |
59 import com.oracle.truffle.sl.factory.*; | |
60 import com.oracle.truffle.sl.nodes.*; | 59 import com.oracle.truffle.sl.nodes.*; |
61 import com.oracle.truffle.sl.nodes.call.*; | 60 import com.oracle.truffle.sl.nodes.call.*; |
62 import com.oracle.truffle.sl.nodes.controlflow.*; | 61 import com.oracle.truffle.sl.nodes.controlflow.*; |
63 import com.oracle.truffle.sl.nodes.expression.*; | 62 import com.oracle.truffle.sl.nodes.expression.*; |
64 import com.oracle.truffle.sl.nodes.instrument.*; | 63 import com.oracle.truffle.sl.nodes.instrument.*; |
162 private final SLContext context; | 161 private final SLContext context; |
163 private DebugSupportProvider debugSupport; | 162 private DebugSupportProvider debugSupport; |
164 | 163 |
165 public SLLanguage(Env env) { | 164 public SLLanguage(Env env) { |
166 super(env); | 165 super(env); |
167 context = SLContextFactory.create(this, new BufferedReader(env().stdIn()), new PrintWriter(env().stdOut(), true)); | 166 context = new SLContext(this, new BufferedReader(env().stdIn()), new PrintWriter(env().stdOut(), true)); |
168 LAST = this; | 167 LAST = this; |
169 for (NodeFactory<? extends SLBuiltinNode> builtin : builtins) { | 168 for (NodeFactory<? extends SLBuiltinNode> builtin : builtins) { |
170 context.installBuiltin(builtin); | 169 context.installBuiltin(builtin, true); |
171 } | 170 } |
172 } | 171 } |
173 | 172 |
174 // TODO (mlvdv) command line options | 173 // TODO (mlvdv) command line options |
175 /* Enables demonstration of per-type tabulation of node execution counts */ | 174 /* Enables demonstration of per-type tabulation of node execution counts */ |
379 } | 378 } |
380 } | 379 } |
381 return result.toString(); | 380 return result.toString(); |
382 } | 381 } |
383 | 382 |
383 public static SLLanguage find() { | |
384 return SLLanguage.findContext(SLLanguage.class); | |
385 } | |
386 | |
384 @Override | 387 @Override |
385 protected Object eval(Source code) throws IOException { | 388 protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException { |
389 final SLContext c = new SLContext(this); | |
390 final Exception[] failed = {null}; | |
386 try { | 391 try { |
387 context.evalSource(code); | 392 c.evalSource(code); |
393 failed[0] = null; | |
388 } catch (Exception e) { | 394 } catch (Exception e) { |
389 throw new IOException(e); | 395 failed[0] = e; |
390 } | 396 } |
391 return null; | 397 return new CallTarget() { |
398 @Override | |
399 public Object call(Object... arguments) { | |
400 if (failed[0] instanceof RuntimeException) { | |
401 throw (RuntimeException) failed[0]; | |
402 } | |
403 if (failed[0] != null) { | |
404 throw new IllegalStateException(failed[0]); | |
405 } | |
406 SLLanguage current = SLLanguage.find(); | |
407 SLContext fillIn = current.context; | |
408 final SLFunctionRegistry functionRegistry = fillIn.getFunctionRegistry(); | |
409 for (SLFunction f : c.getFunctionRegistry().getFunctions()) { | |
410 RootCallTarget callTarget = f.getCallTarget(); | |
411 if (callTarget == null) { | |
412 continue; | |
413 } | |
414 functionRegistry.lookup(f.getName()); | |
415 functionRegistry.register(f.getName(), (SLRootNode) f.getCallTarget().getRootNode()); | |
416 } | |
417 return null; | |
418 } | |
419 }; | |
392 } | 420 } |
393 | 421 |
394 @Override | 422 @Override |
395 protected Object findExportedSymbol(String globalName, boolean onlyExplicit) { | 423 protected Object findExportedSymbol(String globalName, boolean onlyExplicit) { |
396 for (SLFunction f : context.getFunctionRegistry().getFunctions()) { | 424 for (SLFunction f : context.getFunctionRegistry().getFunctions()) { |
463 coverageTracker.print(System.out); | 491 coverageTracker.print(System.out); |
464 coverageTracker.dispose(); | 492 coverageTracker.dispose(); |
465 } | 493 } |
466 } | 494 } |
467 | 495 |
496 public SLContext getContext() { | |
497 return context; | |
498 } | |
499 | |
468 private final class SLDebugProvider implements DebugSupportProvider { | 500 private final class SLDebugProvider implements DebugSupportProvider { |
469 | 501 |
470 public SLDebugProvider() { | 502 public SLDebugProvider() { |
471 if (registeredASTProber == null) { | 503 if (registeredASTProber == null) { |
472 registeredASTProber = new SLStandardASTProber(); | 504 registeredASTProber = new SLStandardASTProber(); |