comparison truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.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 503529c65456
comparison
equal deleted inserted replaced
22045:ffbc7f472438 22046:e7c2d36daf72
72 private final PrintWriter output; 72 private final PrintWriter output;
73 private final SLFunctionRegistry functionRegistry; 73 private final SLFunctionRegistry functionRegistry;
74 private final Shape emptyShape; 74 private final Shape emptyShape;
75 75
76 public SLContext(SLLanguage language, BufferedReader input, PrintWriter output) { 76 public SLContext(SLLanguage language, BufferedReader input, PrintWriter output) {
77 this(language, input, output, true);
78 }
79
80 public SLContext(SLLanguage language) {
81 this(language, null, null, false);
82 }
83
84 private SLContext(SLLanguage language, BufferedReader input, PrintWriter output, boolean installBuiltins) {
77 this.language = language; 85 this.language = language;
78 this.input = input; 86 this.input = input;
79 this.output = output; 87 this.output = output;
80 this.functionRegistry = new SLFunctionRegistry(); 88 this.functionRegistry = new SLFunctionRegistry();
81 installBuiltins(); 89 installBuiltins(installBuiltins);
82 90
83 this.emptyShape = LAYOUT.createShape(new ObjectType()); 91 this.emptyShape = LAYOUT.createShape(new ObjectType());
84 } 92 }
85 93
86 /** 94 /**
112 120
113 /** 121 /**
114 * Adds all builtin functions to the {@link SLFunctionRegistry}. This method lists all 122 * Adds all builtin functions to the {@link SLFunctionRegistry}. This method lists all
115 * {@link SLBuiltinNode builtin implementation classes}. 123 * {@link SLBuiltinNode builtin implementation classes}.
116 */ 124 */
117 private void installBuiltins() { 125 private void installBuiltins(boolean registerRootNodes) {
118 installBuiltin(SLReadlnBuiltinFactory.getInstance()); 126 installBuiltin(SLReadlnBuiltinFactory.getInstance(), registerRootNodes);
119 installBuiltin(SLPrintlnBuiltinFactory.getInstance()); 127 installBuiltin(SLPrintlnBuiltinFactory.getInstance(), registerRootNodes);
120 installBuiltin(SLNanoTimeBuiltinFactory.getInstance()); 128 installBuiltin(SLNanoTimeBuiltinFactory.getInstance(), registerRootNodes);
121 installBuiltin(SLDefineFunctionBuiltinFactory.getInstance()); 129 installBuiltin(SLDefineFunctionBuiltinFactory.getInstance(), registerRootNodes);
122 installBuiltin(SLStackTraceBuiltinFactory.getInstance()); 130 installBuiltin(SLStackTraceBuiltinFactory.getInstance(), registerRootNodes);
123 installBuiltin(SLHelloEqualsWorldBuiltinFactory.getInstance()); 131 installBuiltin(SLHelloEqualsWorldBuiltinFactory.getInstance(), registerRootNodes);
124 installBuiltin(SLAssertTrueBuiltinFactory.getInstance()); 132 installBuiltin(SLAssertTrueBuiltinFactory.getInstance(), registerRootNodes);
125 installBuiltin(SLAssertFalseBuiltinFactory.getInstance()); 133 installBuiltin(SLAssertFalseBuiltinFactory.getInstance(), registerRootNodes);
126 installBuiltin(SLNewObjectBuiltinFactory.getInstance()); 134 installBuiltin(SLNewObjectBuiltinFactory.getInstance(), registerRootNodes);
127 } 135 }
128 136
129 public void installBuiltin(NodeFactory<? extends SLBuiltinNode> factory) { 137 public void installBuiltin(NodeFactory<? extends SLBuiltinNode> factory, boolean registerRootNodes) {
130 /* 138 /*
131 * The builtin node factory is a class that is automatically generated by the Truffle DSL. 139 * The builtin node factory is a class that is automatically generated by the Truffle DSL.
132 * The signature returned by the factory reflects the signature of the @Specialization 140 * The signature returned by the factory reflects the signature of the @Specialization
133 * methods in the builtin classes. 141 * methods in the builtin classes.
134 */ 142 */
147 /* The name of the builtin function is specified via an annotation on the node class. */ 155 /* The name of the builtin function is specified via an annotation on the node class. */
148 String name = lookupNodeInfo(builtinBodyNode.getClass()).shortName(); 156 String name = lookupNodeInfo(builtinBodyNode.getClass()).shortName();
149 /* Wrap the builtin in a RootNode. Truffle requires all AST to start with a RootNode. */ 157 /* Wrap the builtin in a RootNode. Truffle requires all AST to start with a RootNode. */
150 SLRootNode rootNode = new SLRootNode(this, new FrameDescriptor(), builtinBodyNode, name); 158 SLRootNode rootNode = new SLRootNode(this, new FrameDescriptor(), builtinBodyNode, name);
151 159
152 /* Register the builtin function in our function registry. */ 160 if (registerRootNodes) {
153 getFunctionRegistry().register(name, rootNode); 161 /* Register the builtin function in our function registry. */
162 getFunctionRegistry().register(name, rootNode);
163 } else {
164 // make sure the function is known
165 getFunctionRegistry().lookup(name);
166 }
154 } 167 }
155 168
156 public static NodeInfo lookupNodeInfo(Class<?> clazz) { 169 public static NodeInfo lookupNodeInfo(Class<?> clazz) {
157 if (clazz == null) { 170 if (clazz == null) {
158 return null; 171 return null;