OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library tree_ir_tracer; | 5 library tree_ir_tracer; |
6 | 6 |
7 import 'dart:async' show EventSink; | 7 import 'dart:async' show EventSink; |
8 import '../tracer.dart'; | 8 import '../tracer.dart'; |
9 import 'tree_ir_nodes.dart'; | 9 import 'tree_ir_nodes.dart'; |
10 import 'optimization/optimization.dart'; | 10 import 'optimization/optimization.dart'; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 visitExpressionStatement(ExpressionStatement node) { | 144 visitExpressionStatement(ExpressionStatement node) { |
145 _addStatement(node); | 145 _addStatement(node); |
146 visitStatement(node.next); | 146 visitStatement(node.next); |
147 } | 147 } |
148 | 148 |
149 visitFunctionDeclaration(FunctionDeclaration node) { | 149 visitFunctionDeclaration(FunctionDeclaration node) { |
150 _addStatement(node); | 150 _addStatement(node); |
151 visitStatement(node.next); | 151 visitStatement(node.next); |
152 } | 152 } |
153 | 153 |
| 154 visitSetField(SetField node) { |
| 155 _addStatement(node); |
| 156 visitStatement(node.next); |
| 157 } |
| 158 |
154 } | 159 } |
155 | 160 |
156 class TreeTracer extends TracerUtil with StatementVisitor, PassMixin { | 161 class TreeTracer extends TracerUtil with StatementVisitor, PassMixin { |
157 final EventSink<String> output; | 162 final EventSink<String> output; |
158 | 163 |
159 TreeTracer(this.output); | 164 TreeTracer(this.output); |
160 | 165 |
161 Names names; | 166 Names names; |
162 BlockCollector collector; | 167 BlockCollector collector; |
163 int statementCounter; | 168 int statementCounter; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } | 275 } |
271 | 276 |
272 visitExpressionStatement(ExpressionStatement node) { | 277 visitExpressionStatement(ExpressionStatement node) { |
273 printStatement(null, expr(node.expression)); | 278 printStatement(null, expr(node.expression)); |
274 } | 279 } |
275 | 280 |
276 visitFunctionDeclaration(FunctionDeclaration node) { | 281 visitFunctionDeclaration(FunctionDeclaration node) { |
277 printStatement(null, 'function ${node.definition.element.name}'); | 282 printStatement(null, 'function ${node.definition.element.name}'); |
278 } | 283 } |
279 | 284 |
| 285 visitSetField(SetField node) { |
| 286 String object = expr(node.object); |
| 287 String field = node.field.name; |
| 288 String value = expr(node.value); |
| 289 if (SubexpressionVisitor.usesInfixNotation(node.object)) { |
| 290 object = '($object)'; |
| 291 } |
| 292 printStatement(null, '$object.$field = $value'); |
| 293 } |
| 294 |
280 String expr(Expression e) { | 295 String expr(Expression e) { |
281 return e.accept(new SubexpressionVisitor(names)); | 296 return e.accept(new SubexpressionVisitor(names)); |
282 } | 297 } |
283 } | 298 } |
284 | 299 |
285 class SubexpressionVisitor extends ExpressionVisitor<String> { | 300 class SubexpressionVisitor extends ExpressionVisitor<String> { |
286 Names names; | 301 Names names; |
287 | 302 |
288 SubexpressionVisitor(this.names); | 303 SubexpressionVisitor(this.names); |
289 | 304 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 376 } |
362 | 377 |
363 String visitThis(This node) { | 378 String visitThis(This node) { |
364 return "this"; | 379 return "this"; |
365 } | 380 } |
366 | 381 |
367 String visitReifyTypeVar(ReifyTypeVar node) { | 382 String visitReifyTypeVar(ReifyTypeVar node) { |
368 return "typevar [${node.typeVariable.name}]"; | 383 return "typevar [${node.typeVariable.name}]"; |
369 } | 384 } |
370 | 385 |
371 bool usesInfixNotation(Expression node) { | 386 static bool usesInfixNotation(Expression node) { |
372 return node is Conditional || node is LogicalOperator; | 387 return node is Conditional || node is LogicalOperator; |
373 } | 388 } |
374 | 389 |
375 String visitConditional(Conditional node) { | 390 String visitConditional(Conditional node) { |
376 String condition = visitExpression(node.condition); | 391 String condition = visitExpression(node.condition); |
377 String thenExpr = visitExpression(node.thenExpression); | 392 String thenExpr = visitExpression(node.thenExpression); |
378 String elseExpr = visitExpression(node.elseExpression); | 393 String elseExpr = visitExpression(node.elseExpression); |
379 return "$condition ? $thenExpr : $elseExpr"; | 394 return "$condition ? $thenExpr : $elseExpr"; |
380 } | 395 } |
381 | 396 |
(...skipping 28 matching lines...) Expand all Loading... |
410 } | 425 } |
411 | 426 |
412 String visitFieldInitializer(FieldInitializer node) { | 427 String visitFieldInitializer(FieldInitializer node) { |
413 throw "$node should not be visited by $this"; | 428 throw "$node should not be visited by $this"; |
414 } | 429 } |
415 | 430 |
416 String visitSuperInitializer(SuperInitializer node) { | 431 String visitSuperInitializer(SuperInitializer node) { |
417 throw "$node should not be visited by $this"; | 432 throw "$node should not be visited by $this"; |
418 } | 433 } |
419 | 434 |
| 435 String visitGetField(GetField node) { |
| 436 String object = visitExpression(node.object); |
| 437 String field = node.field.name; |
| 438 if (usesInfixNotation(node.object)) { |
| 439 object = '($object)'; |
| 440 } |
| 441 return '$object.$field'; |
| 442 } |
| 443 |
| 444 String visitCreateBox(CreateBox node) { |
| 445 return 'CreateBox'; |
| 446 } |
| 447 |
| 448 String visitCreateClosureClass(CreateClosureClass node) { |
| 449 String className = node.classElement.name; |
| 450 String arguments = node.arguments.map(visitExpression).join(', '); |
| 451 return 'CreateClosure $className($arguments)'; |
| 452 } |
| 453 |
420 } | 454 } |
421 | 455 |
422 /** | 456 /** |
423 * Invents (and remembers) names for Variables that do not have an associated | 457 * Invents (and remembers) names for Variables that do not have an associated |
424 * identifier. | 458 * identifier. |
425 * | 459 * |
426 * In case a variable is named v0, v1, etc, it may be assigned a different | 460 * In case a variable is named v0, v1, etc, it may be assigned a different |
427 * name to avoid clashing with a previously synthesized variable name. | 461 * name to avoid clashing with a previously synthesized variable name. |
428 */ | 462 */ |
429 class Names { | 463 class Names { |
430 final Map<Variable, String> _names = {}; | 464 final Map<Variable, String> _names = {}; |
431 final Set<String> _usedNames = new Set(); | 465 final Set<String> _usedNames = new Set(); |
432 int _counter = 0; | 466 int _counter = 0; |
433 | 467 |
434 String varName(Variable v) { | 468 String varName(Variable v) { |
435 String name = _names[v]; | 469 String name = _names[v]; |
436 if (name == null) { | 470 if (name == null) { |
437 String prefix = v.element == null ? 'v' : '${v.element.name}_'; | 471 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
438 while (name == null || _usedNames.contains(name)) { | 472 while (name == null || _usedNames.contains(name)) { |
439 name = "$prefix${_counter++}"; | 473 name = "$prefix${_counter++}"; |
440 } | 474 } |
441 _names[v] = name; | 475 _names[v] = name; |
442 _usedNames.add(name); | 476 _usedNames.add(name); |
443 } | 477 } |
444 return name; | 478 return name; |
445 } | 479 } |
446 } | 480 } |
OLD | NEW |