OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 import 'package:compiler/compiler.dart' as compiler; | 5 import 'dart:io' show |
kasperl
2015/02/10 19:56:57
Maybe give this a library a name?
ahe
2015/02/11 13:47:17
Done.
| |
6 | 6 File, |
7 import 'package:semantic_visitor/semantic_visitor.dart' as semantic_visitor; | 7 Platform; |
8 | |
9 import 'package:compiler/compiler.dart' as api; | |
8 | 10 |
9 import 'package:sharedfrontend/elements.dart' as elements; | 11 import 'package:sharedfrontend/elements.dart' as elements; |
10 | 12 |
11 main() { | 13 import 'package:compiler/src/apiimpl.dart' as apiimpl; |
12 print(compiler.compile); | 14 |
13 print(semantic_visitor.SemanticVisitor); | 15 import 'package:compiler/src/dart2jslib.dart' as dart2js; |
14 print(elements.Element); | 16 |
15 } | 17 import 'package:compiler/src/source_file_provider.dart' show |
18 FormattingDiagnosticHandler; | |
19 | |
20 import 'package:dart2js_incremental/compiler.dart' show | |
21 OutputProvider; | |
22 | |
23 import 'package:compiler/src/filenames.dart' show | |
24 appendSlash; | |
25 | |
26 import 'package:semantic_visitor/semantic_visitor.dart' show | |
27 SemanticVisitor; | |
28 | |
29 import 'package:compiler/src/elements/elements.dart'; | |
30 import 'package:compiler/src/resolution/resolution.dart'; | |
31 import 'package:compiler/src/tree/tree.dart'; | |
32 import 'package:compiler/src/universe/universe.dart'; | |
33 import 'package:compiler/src/util/util.dart' show Spannable; | |
34 import 'package:compiler/src/dart_types.dart'; | |
35 | |
36 import 'bytecodes.dart'; | |
37 | |
38 main(List<String> arguments) { | |
39 FormattingDiagnosticHandler handler = new FormattingDiagnosticHandler() | |
40 ..throwOnError = false; | |
41 | |
42 OutputProvider outputProvider = new OutputProvider(); | |
43 | |
44 Uri myLocation = Uri.base.resolveUri(Platform.script); | |
45 if (myLocation.scheme == 'package') { | |
46 Uri runtimePackageRoot = | |
47 Uri.base.resolve(appendSlash(Platform.packageRoot)); | |
48 myLocation = | |
kasperl
2015/02/10 19:56:57
My usual preference is to add a few more local var
ahe
2015/02/11 13:47:18
Done.
| |
49 new Uri.file( | |
50 new File.fromUri(runtimePackageRoot.resolve(myLocation.path)) | |
51 .resolveSymbolicLinksSync()); | |
52 } | |
53 | |
54 Uri script = Uri.base.resolve(arguments.single); | |
55 | |
56 Uri libraryRoot = myLocation.resolve('../../../../dart/sdk/'); | |
57 | |
58 Uri packageRoot = script.resolve('packages/'); | |
59 | |
60 List<String> options = <String>[]; | |
61 | |
62 Map<String, dynamic> environment = <String, dynamic>{}; | |
63 | |
64 FletchCompiler compiler = new FletchCompiler( | |
65 handler.provider, | |
66 outputProvider, | |
67 handler, | |
68 libraryRoot, | |
69 packageRoot, | |
70 options, | |
71 environment); | |
72 | |
73 compiler.run(script); | |
74 } | |
75 | |
76 abstract class FletchCompilerHack extends apiimpl.Compiler { | |
77 FletchCompilerHack( | |
78 api.CompilerInputProvider provider, | |
79 api.CompilerOutputProvider outputProvider, | |
80 api.DiagnosticHandler handler, | |
81 Uri libraryRoot, | |
82 Uri packageRoot, | |
83 List<String> options, | |
84 Map<String, dynamic> environment) | |
85 : super(provider, outputProvider, handler, libraryRoot, packageRoot, | |
86 options, environment) { | |
87 switchBackendHack(); | |
88 } | |
89 | |
90 void switchBackendHack() { | |
91 // TODO(ahe): Modify dart2js to support a custom backend directly, and | |
92 // remove this method. | |
93 int backendTaskCount = backend.tasks.length; | |
94 int apiimplTaskCount = 2; | |
95 int baseTaskCount = tasks.length - backendTaskCount - apiimplTaskCount; | |
96 | |
97 tasks.removeRange(baseTaskCount, baseTaskCount + backendTaskCount); | |
98 | |
99 backend = new FletchBackend(this); | |
100 tasks.addAll(backend.tasks); | |
101 } | |
102 } | |
103 | |
104 class FletchCompiler extends FletchCompilerHack { | |
105 FletchCompiler( | |
106 api.CompilerInputProvider provider, | |
107 api.CompilerOutputProvider outputProvider, | |
108 api.DiagnosticHandler handler, | |
109 Uri libraryRoot, | |
110 Uri packageRoot, | |
111 List<String> options, | |
112 Map<String, dynamic> environment) | |
113 : super(provider, outputProvider, handler, libraryRoot, packageRoot, | |
114 ['--output-type=dart']..addAll(options), environment); | |
115 | |
116 void computeMain() { | |
117 if (mainApp == null) return; | |
118 | |
119 mainFunction = mainApp.findLocal("_entry"); | |
120 } | |
121 | |
122 void onLibraryCreated(elements.LibraryElement library) { | |
123 // TODO(ahe): Remove this. | |
124 library.canUseNative = true; | |
125 super.onLibraryCreated(library); | |
126 } | |
127 } | |
128 | |
129 class FletchBackend extends dart2js.Backend { | |
130 final FletchResolutionCallbacks resolutionCallbacks = | |
131 new FletchResolutionCallbacks(); | |
132 | |
133 FletchBackend(dart2js.Compiler compiler) | |
134 : super(compiler); | |
135 | |
136 List<CompilerTask> get tasks => <CompilerTask>[]; | |
137 | |
138 dart2js.ConstantSystem get constantSystem { | |
139 throw new UnsupportedError("get constantSystem"); | |
140 } | |
141 | |
142 dart2js.BackendConstantEnvironment get constants { | |
143 throw new UnsupportedError("get constants"); | |
144 } | |
145 | |
146 dart2js.ConstantCompilerTask get constantCompilerTask { | |
147 throw new UnsupportedError("get constantCompilerTask"); | |
148 } | |
149 | |
150 void enqueueHelpers(dart2js.ResolutionEnqueuer world, dart2js.Registry registr y) { | |
kasperl
2015/02/10 19:56:57
Long line.
ahe
2015/02/11 13:47:18
Done.
| |
151 } | |
152 | |
153 void codegen(dart2js.CodegenWorkItem work) { | |
154 } | |
155 | |
156 bool get canHandleCompilationFailed => true; | |
157 | |
158 int assembleProgram() { | |
159 compiler.reportHint( | |
160 compiler.mainFunction, | |
161 dart2js.MessageKind.GENERIC, | |
162 {'text': 'Compiling ${compiler.mainFunction.name}'}); | |
163 | |
164 FletchVisitor visitor = new FletchVisitor(compiler.mainFunction); | |
165 compiler.mainFunction.node.accept(visitor); | |
166 print("Constants"); | |
167 visitor.constants.forEach((constant, int index) { | |
168 print(" #$index: $constant"); | |
169 }); | |
170 | |
171 print("Bytecodes:"); | |
172 int offset = 0; | |
173 for (Bytecode bytecode in visitor.bytecodes) { | |
174 print(" $offset: $bytecode"); | |
175 offset += bytecode.size; | |
176 } | |
177 } | |
178 } | |
179 | |
180 class FletchResolutionCallbacks extends dart2js.ResolutionCallbacks { | |
181 } | |
182 | |
183 class FletchVisitor extends SemanticVisitor { | |
kasperl
2015/02/10 19:56:56
FletchMethodVisitor perhaps?
ahe
2015/02/11 13:47:18
I think I like "BytecodeBuilder".
| |
184 final List<Bytecode> bytecodes = <Bytecode>[]; | |
185 | |
186 final Map<dynamic, int> constants = <dynamic, int>{}; | |
187 | |
188 FletchVisitor(element) | |
189 : super(element.resolvedAst.elements); | |
190 | |
191 void visitStaticMethodInvocation( | |
192 Send node, | |
193 /* MethodElement */ element, | |
194 NodeList arguments, | |
195 Selector selector) { | |
196 arguments.accept(this); | |
197 int id = constants.putIfAbsent(element, () => constants.length); | |
kasperl
2015/02/10 19:56:56
I'd consider adding a helper method for turning a
ahe
2015/02/11 13:47:18
Done.
| |
198 bytecodes.add(new InvokeStaticUnfold(id)); | |
199 bytecodes.add(const Pop()); | |
200 } | |
201 | |
202 void visitLiteralString(LiteralString node) { | |
203 int id = constants.putIfAbsent( | |
204 node.dartString.slowToString(), () => constants.length); | |
205 bytecodes.add(new LoadConstUnfold(id)); | |
206 } | |
207 | |
208 void visitLiteralInt(LiteralInt node) { | |
209 int id = constants.putIfAbsent(node.value, () => constants.length); | |
210 bytecodes.add(new LoadConstUnfold(id)); | |
211 } | |
212 | |
213 | |
214 void visitLiteral(Literal node) { | |
215 print("literal ${node}"); | |
kasperl
2015/02/10 19:56:57
Maybe just remove this printing again?
ahe
2015/02/11 13:47:18
Done.
| |
216 } | |
217 | |
218 void visitFunctionExpression(FunctionExpression node) { | |
219 node.body.accept(this); | |
220 } | |
221 | |
222 void visitBlock(Block node) { | |
223 node.visitChildren(this); | |
224 } | |
225 | |
226 void visitNodeList(NodeList node) { | |
227 node.visitChildren(this); | |
228 } | |
229 | |
230 void visitExpressionStatement(ExpressionStatement node) { | |
231 node.visitChildren(this); | |
kasperl
2015/02/10 19:56:56
How about moving the pop from visitStaticMethodInv
ahe
2015/02/10 21:38:07
Yes, of course!
| |
232 } | |
233 | |
234 void visitParameterAccess(Send node, ParameterElement element); | |
235 | |
236 void visitParameterAssignment(SendSet node, ParameterElement element, Node rhs ); | |
kasperl
2015/02/10 19:56:57
Long line.
ahe
2015/02/11 13:47:18
Done.
| |
237 void visitParameterInvocation(Send node, | |
238 ParameterElement element, | |
239 NodeList arguments, | |
240 Selector selector); | |
241 | |
242 void visitLocalVariableAccess(Send node, LocalVariableElement element); | |
243 void visitLocalVariableAssignment(SendSet node, | |
244 LocalVariableElement element, | |
245 Node rhs); | |
246 void visitLocalVariableInvocation(Send node, | |
247 LocalVariableElement element, | |
248 NodeList arguments, | |
249 Selector selector); | |
250 | |
251 void visitLocalFunctionAccess(Send node, LocalFunctionElement element); | |
252 void visitLocalFunctionAssignment(SendSet node, | |
253 LocalFunctionElement element, | |
254 Node rhs, | |
255 Selector selector); | |
256 void visitLocalFunctionInvocation(Send node, | |
257 LocalFunctionElement element, | |
258 NodeList arguments, | |
259 Selector selector); | |
260 | |
261 void visitDynamicAccess(Send node, Selector selector); | |
262 void visitDynamicAssignment(SendSet node, Selector selector, Node rhs); | |
263 void visitDynamicInvocation(Send node, | |
264 NodeList arguments, | |
265 Selector selector); | |
266 | |
267 void visitStaticFieldAccess(Send node, FieldElement element); | |
268 void visitStaticFieldAssignment(SendSet node, FieldElement element, Node rhs); | |
269 void visitStaticFieldInvocation(Send node, | |
270 FieldElement element, | |
271 NodeList arguments, | |
272 Selector selector); | |
273 | |
274 void visitStaticMethodAccess(Send node, MethodElement element); | |
275 | |
276 void visitStaticPropertyAccess(Send node, FunctionElement element); | |
277 void visitStaticPropertyAssignment(SendSet node, | |
278 FunctionElement element, | |
279 Node rhs); | |
280 void visitStaticPropertyInvocation(Send node, | |
281 FieldElement element, | |
282 NodeList arguments, | |
283 Selector selector); | |
284 | |
285 void visitTopLevelFieldAccess(Send node, FieldElement element); | |
286 void visitTopLevelFieldAssignment(SendSet node, FieldElement element, Node rhs ); | |
kasperl
2015/02/10 19:56:57
Long line.
ahe
2015/02/11 13:47:18
Done.
| |
287 void visitTopLevelFieldInvocation(Send node, | |
288 FieldElement element, | |
289 NodeList arguments, | |
290 Selector selector); | |
291 | |
292 void visitTopLevelMethodAccess(Send node, MethodElement element); | |
293 void visitTopLevelMethodInvocation(Send node, | |
294 MethodElement element, | |
295 NodeList arguments, | |
296 Selector selector); | |
297 | |
298 void visitTopLevelPropertyAccess(Send node, FunctionElement element); | |
299 void visitTopLevelPropertyAssignment(SendSet node, | |
300 FunctionElement element, | |
301 Node rhs); | |
302 void visitTopLevelPropertyInvocation(Send node, | |
303 FieldElement element, | |
304 NodeList arguments, | |
305 Selector selector); | |
306 | |
307 void visitClassTypeLiteralAccess(Send node, ClassElement element); | |
308 void visitClassTypeLiteralInvocation(Send node, | |
309 ClassElement element, | |
310 NodeList arguments, | |
311 Selector selector); | |
312 void visitClassTypeLiteralAssignment(SendSet node, | |
313 ClassElement element, | |
314 Node rhs); | |
315 | |
316 void visitTypedefTypeLiteralAccess(Send node, TypedefElement element); | |
317 | |
318 void visitTypedefTypeLiteralInvocation(Send node, | |
319 TypedefElement element, | |
320 NodeList arguments, | |
321 Selector selector); | |
322 | |
323 void visitTypedefTypeLiteralAssignment(SendSet node, | |
324 TypedefElement element, | |
325 Node rhs); | |
326 | |
327 void visitTypeVariableTypeLiteralAccess(Send node, TypeVariableElement element ); | |
kasperl
2015/02/10 19:56:57
Long line.
ahe
2015/02/11 13:47:17
Done.
| |
328 | |
329 void visitTypeVariableTypeLiteralInvocation(Send node, | |
330 TypeVariableElement element, | |
331 NodeList arguments, | |
332 Selector selector); | |
333 | |
334 void visitTypeVariableTypeLiteralAssignment(SendSet node, | |
335 TypeVariableElement element, | |
336 Node rhs); | |
337 | |
338 void visitDynamicTypeLiteralAccess(Send node); | |
339 | |
340 void visitAssert(Send node, Node expression); | |
341 | |
342 internalError(Spannable spannable, String reason); | |
343 | |
344 } | |
OLD | NEW |