comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java @ 22374:299c279c87b3

Backed out changeset dea950d41ef3
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 18 Nov 2015 09:02:41 +0100
parents dea950d41ef3
children b4e89154a774
comparison
equal deleted inserted replaced
22373:dea950d41ef3 22374:299c279c87b3
29 import java.io.OutputStream; 29 import java.io.OutputStream;
30 import java.lang.annotation.ElementType; 30 import java.lang.annotation.ElementType;
31 import java.lang.annotation.Retention; 31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy; 32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.annotation.Target; 33 import java.lang.annotation.Target;
34 import java.util.Objects; 34 import java.util.Collections;
35 import java.util.Map;
36 import java.util.WeakHashMap;
35 37
36 import com.oracle.truffle.api.debug.Debugger; 38 import com.oracle.truffle.api.debug.Debugger;
37 import com.oracle.truffle.api.debug.SuspendedEvent; 39 import com.oracle.truffle.api.debug.SuspendedEvent;
38 import com.oracle.truffle.api.frame.FrameInstance; 40 import com.oracle.truffle.api.frame.FrameInstance;
39 import com.oracle.truffle.api.frame.MaterializedFrame; 41 import com.oracle.truffle.api.frame.MaterializedFrame;
45 import com.oracle.truffle.api.instrument.Visualizer; 47 import com.oracle.truffle.api.instrument.Visualizer;
46 import com.oracle.truffle.api.instrument.WrapperNode; 48 import com.oracle.truffle.api.instrument.WrapperNode;
47 import com.oracle.truffle.api.nodes.Node; 49 import com.oracle.truffle.api.nodes.Node;
48 import com.oracle.truffle.api.nodes.RootNode; 50 import com.oracle.truffle.api.nodes.RootNode;
49 import com.oracle.truffle.api.source.Source; 51 import com.oracle.truffle.api.source.Source;
52 import java.util.Objects;
50 53
51 /** 54 /**
52 * An entry point for everyone who wants to implement a Truffle based language. By providing an 55 * An entry point for everyone who wants to implement a Truffle based language. By providing an
53 * implementation of this type and registering it using {@link Registration} annotation, your 56 * implementation of this type and registering it using {@link Registration} annotation, your
54 * language becomes accessible to users of the {@link com.oracle.truffle.api.vm.PolyglotEngine 57 * language becomes accessible to users of the {@link com.oracle.truffle.api.vm.PolyglotEngine
67 * {@link #parse(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, java.lang.String...) 70 * {@link #parse(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, java.lang.String...)
68 * parsed} by the language 71 * parsed} by the language
69 */ 72 */
70 @SuppressWarnings("javadoc") 73 @SuppressWarnings("javadoc")
71 public abstract class TruffleLanguage<C> { 74 public abstract class TruffleLanguage<C> {
75 private final Map<Source, CallTarget> compiled = Collections.synchronizedMap(new WeakHashMap<Source, CallTarget>());
72 76
73 /** 77 /**
74 * Constructor to be called by subclasses. 78 * Constructor to be called by subclasses.
75 */ 79 */
76 protected TruffleLanguage() { 80 protected TruffleLanguage() {
434 protected CallTarget parse(TruffleLanguage<?> truffleLanguage, Source code, Node context, String... argumentNames) throws IOException { 438 protected CallTarget parse(TruffleLanguage<?> truffleLanguage, Source code, Node context, String... argumentNames) throws IOException {
435 return truffleLanguage.parse(code, context, argumentNames); 439 return truffleLanguage.parse(code, context, argumentNames);
436 } 440 }
437 441
438 @Override 442 @Override
439 protected CallTarget parseForEval(TruffleLanguage<?> language, Source source) throws IOException { 443 protected Object eval(TruffleLanguage<?> language, Source source) throws IOException {
440 CallTarget target = language.parse(source, null); 444 CallTarget target = language.compiled.get(source);
441 if (target == null) { 445 if (target == null) {
442 throw new IOException("Parsing has not produced a CallTarget for " + source); 446 target = language.parse(source, null);
447 if (target == null) {
448 throw new IOException("Parsing has not produced a CallTarget for " + source);
449 }
450 language.compiled.put(source, target);
443 } 451 }
444 return target; 452 try {
453 return target.call();
454 } catch (Throwable ex) {
455 throw new IOException(ex);
456 }
445 } 457 }
446 458
447 @Override 459 @Override
448 protected Object evalInContext(Object vm, SuspendedEvent ev, String code, FrameInstance frame) throws IOException { 460 protected Object evalInContext(Object vm, SuspendedEvent ev, String code, FrameInstance frame) throws IOException {
449 Node n = frame == null ? ev.getNode() : frame.getCallNode(); 461 Node n = frame == null ? ev.getNode() : frame.getCallNode();