| 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 | 10 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } | 69 } |
| 70 | 70 |
| 71 visitLabeledStatement(LabeledStatement node) { | 71 visitLabeledStatement(LabeledStatement node) { |
| 72 Block target = new Block(node.label); | 72 Block target = new Block(node.label); |
| 73 breakTargets[node.label] = target; | 73 breakTargets[node.label] = target; |
| 74 visitStatement(node.body); | 74 visitStatement(node.body); |
| 75 _addBlock(target); | 75 _addBlock(target); |
| 76 visitStatement(node.next); | 76 visitStatement(node.next); |
| 77 } | 77 } |
| 78 | 78 |
| 79 visitAssign(Assign node) { | |
| 80 _addStatement(node); | |
| 81 visitStatement(node.next); | |
| 82 } | |
| 83 | |
| 84 visitReturn(Return node) { | 79 visitReturn(Return node) { |
| 85 _addStatement(node); | 80 _addStatement(node); |
| 86 } | 81 } |
| 87 | 82 |
| 88 visitBreak(Break node) { | 83 visitBreak(Break node) { |
| 89 _addStatement(node); | 84 _addStatement(node); |
| 90 blocks.last.addEdgeTo(breakTargets[node.target]); | 85 blocks.last.addEdgeTo(breakTargets[node.target]); |
| 91 } | 86 } |
| 92 | 87 |
| 93 visitContinue(Continue node) { | 88 visitContinue(Continue node) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 visitExpressionStatement(ExpressionStatement node) { | 161 visitExpressionStatement(ExpressionStatement node) { |
| 167 _addStatement(node); | 162 _addStatement(node); |
| 168 visitStatement(node.next); | 163 visitStatement(node.next); |
| 169 } | 164 } |
| 170 | 165 |
| 171 visitFunctionDeclaration(FunctionDeclaration node) { | 166 visitFunctionDeclaration(FunctionDeclaration node) { |
| 172 _addStatement(node); | 167 _addStatement(node); |
| 173 visitStatement(node.next); | 168 visitStatement(node.next); |
| 174 } | 169 } |
| 175 | 170 |
| 176 visitSetField(SetField node) { | 171 visitVariableDeclaration(VariableDeclaration node) { |
| 177 _addStatement(node); | 172 _addStatement(node); |
| 178 visitStatement(node.next); | 173 visitStatement(node.next); |
| 179 } | 174 } |
| 180 | |
| 181 } | 175 } |
| 182 | 176 |
| 183 class TreeTracer extends TracerUtil with StatementVisitor { | 177 class TreeTracer extends TracerUtil with StatementVisitor { |
| 184 String get passName => null; | 178 String get passName => null; |
| 185 | 179 |
| 186 final EventSink<String> output; | 180 final EventSink<String> output; |
| 187 | 181 |
| 188 TreeTracer(this.output); | 182 TreeTracer(this.output); |
| 189 | 183 |
| 190 List<Variable> parameters; | 184 List<Variable> parameters; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 name = 'x${statementCounter++}'; | 253 name = 'x${statementCounter++}'; |
| 260 } | 254 } |
| 261 addIndent(); | 255 addIndent(); |
| 262 add("$bci $uses $name $contents <|@\n"); | 256 add("$bci $uses $name $contents <|@\n"); |
| 263 } | 257 } |
| 264 | 258 |
| 265 visitLabeledStatement(LabeledStatement node) { | 259 visitLabeledStatement(LabeledStatement node) { |
| 266 // These do not get added to a block's list of statements. | 260 // These do not get added to a block's list of statements. |
| 267 } | 261 } |
| 268 | 262 |
| 269 visitAssign(Assign node) { | |
| 270 String name = names.varName(node.variable); | |
| 271 String rhs = expr(node.value); | |
| 272 Variable v = node.variable; | |
| 273 String extra = "(r=${v.readCount}, w=${v.writeCount})"; | |
| 274 printStatement(null, "assign $name = $rhs $extra"); | |
| 275 } | |
| 276 | |
| 277 visitReturn(Return node) { | 263 visitReturn(Return node) { |
| 278 printStatement(null, "return ${expr(node.value)}"); | 264 printStatement(null, "return ${expr(node.value)}"); |
| 279 } | 265 } |
| 280 | 266 |
| 281 visitBreak(Break node) { | 267 visitBreak(Break node) { |
| 282 printStatement(null, "break ${collector.breakTargets[node.target].name}"); | 268 printStatement(null, "break ${collector.breakTargets[node.target].name}"); |
| 283 } | 269 } |
| 284 | 270 |
| 285 visitContinue(Continue node) { | 271 visitContinue(Continue node) { |
| 286 printStatement(null, | 272 printStatement(null, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 314 } | 300 } |
| 315 | 301 |
| 316 visitExpressionStatement(ExpressionStatement node) { | 302 visitExpressionStatement(ExpressionStatement node) { |
| 317 printStatement(null, expr(node.expression)); | 303 printStatement(null, expr(node.expression)); |
| 318 } | 304 } |
| 319 | 305 |
| 320 visitFunctionDeclaration(FunctionDeclaration node) { | 306 visitFunctionDeclaration(FunctionDeclaration node) { |
| 321 printStatement(null, 'function ${node.definition.element.name}'); | 307 printStatement(null, 'function ${node.definition.element.name}'); |
| 322 } | 308 } |
| 323 | 309 |
| 310 visitVariableDeclaration(VariableDeclaration node) { |
| 311 String variable = names.varName(node.variable); |
| 312 String value = expr(node.value); |
| 313 printStatement(null, 'declare $variable = $value'); |
| 314 } |
| 315 |
| 324 visitSetField(SetField node) { | 316 visitSetField(SetField node) { |
| 325 String object = expr(node.object); | 317 String object = expr(node.object); |
| 326 String field = node.field.name; | 318 String field = node.field.name; |
| 327 String value = expr(node.value); | 319 String value = expr(node.value); |
| 328 if (SubexpressionVisitor.usesInfixNotation(node.object)) { | 320 if (SubexpressionVisitor.usesInfixNotation(node.object)) { |
| 329 object = '($object)'; | 321 object = '($object)'; |
| 330 } | 322 } |
| 331 printStatement(null, '$object.$field = $value'); | 323 printStatement(null, '$object.$field = $value'); |
| 332 } | 324 } |
| 333 | 325 |
| 334 String expr(Expression e) { | 326 String expr(Expression e) { |
| 335 return e.accept(new SubexpressionVisitor(names)); | 327 return e.accept(new SubexpressionVisitor(names)); |
| 336 } | 328 } |
| 337 } | 329 } |
| 338 | 330 |
| 339 class SubexpressionVisitor extends ExpressionVisitor<String> { | 331 class SubexpressionVisitor extends ExpressionVisitor<String> { |
| 340 Names names; | 332 Names names; |
| 341 | 333 |
| 342 SubexpressionVisitor(this.names); | 334 SubexpressionVisitor(this.names); |
| 343 | 335 |
| 344 String visitVariableUse(VariableUse node) { | 336 String visitVariableUse(VariableUse node) { |
| 345 return names.varName(node.variable); | 337 return names.varName(node.variable); |
| 346 } | 338 } |
| 347 | 339 |
| 340 String visitAssign(Assign node) { |
| 341 String variable = names.varName(node.variable); |
| 342 String value = visitExpression(node.value); |
| 343 return '$variable = $value'; |
| 344 } |
| 345 |
| 348 String formatArguments(Invoke node) { | 346 String formatArguments(Invoke node) { |
| 349 List<String> args = new List<String>(); | 347 List<String> args = new List<String>(); |
| 350 int positionalArgumentCount = node.selector.positionalArgumentCount; | 348 int positionalArgumentCount = node.selector.positionalArgumentCount; |
| 351 for (int i = 0; i < positionalArgumentCount; ++i) { | 349 for (int i = 0; i < positionalArgumentCount; ++i) { |
| 352 args.add(node.arguments[i].accept(this)); | 350 args.add(node.arguments[i].accept(this)); |
| 353 } | 351 } |
| 354 for (int i = 0; i < node.selector.namedArgumentCount; ++i) { | 352 for (int i = 0; i < node.selector.namedArgumentCount; ++i) { |
| 355 String name = node.selector.namedArguments[i]; | 353 String name = node.selector.namedArguments[i]; |
| 356 String arg = node.arguments[positionalArgumentCount + i].accept(this); | 354 String arg = node.arguments[positionalArgumentCount + i].accept(this); |
| 357 args.add("$name: $arg"); | 355 args.add("$name: $arg"); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 | 417 |
| 420 String visitThis(This node) { | 418 String visitThis(This node) { |
| 421 return "this"; | 419 return "this"; |
| 422 } | 420 } |
| 423 | 421 |
| 424 String visitReifyTypeVar(ReifyTypeVar node) { | 422 String visitReifyTypeVar(ReifyTypeVar node) { |
| 425 return "typevar [${node.typeVariable.name}]"; | 423 return "typevar [${node.typeVariable.name}]"; |
| 426 } | 424 } |
| 427 | 425 |
| 428 static bool usesInfixNotation(Expression node) { | 426 static bool usesInfixNotation(Expression node) { |
| 429 return node is Conditional || node is LogicalOperator; | 427 return node is Conditional || |
| 428 node is LogicalOperator || |
| 429 node is Assign || |
| 430 node is SetField; |
| 430 } | 431 } |
| 431 | 432 |
| 432 String visitConditional(Conditional node) { | 433 String visitConditional(Conditional node) { |
| 433 String condition = visitExpression(node.condition); | 434 String condition = visitExpression(node.condition); |
| 434 String thenExpr = visitExpression(node.thenExpression); | 435 String thenExpr = visitExpression(node.thenExpression); |
| 435 String elseExpr = visitExpression(node.elseExpression); | 436 String elseExpr = visitExpression(node.elseExpression); |
| 436 return "$condition ? $thenExpr : $elseExpr"; | 437 return "$condition ? $thenExpr : $elseExpr"; |
| 437 } | 438 } |
| 438 | 439 |
| 439 String visitLogicalOperator(LogicalOperator node) { | 440 String visitLogicalOperator(LogicalOperator node) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 | 477 |
| 477 String visitGetField(GetField node) { | 478 String visitGetField(GetField node) { |
| 478 String object = visitExpression(node.object); | 479 String object = visitExpression(node.object); |
| 479 String field = node.field.name; | 480 String field = node.field.name; |
| 480 if (usesInfixNotation(node.object)) { | 481 if (usesInfixNotation(node.object)) { |
| 481 object = '($object)'; | 482 object = '($object)'; |
| 482 } | 483 } |
| 483 return '$object.$field'; | 484 return '$object.$field'; |
| 484 } | 485 } |
| 485 | 486 |
| 487 String visitSetField(SetField node) { |
| 488 String object = visitExpression(node.object); |
| 489 String field = node.field.name; |
| 490 if (usesInfixNotation(node.object)) { |
| 491 object = '($object)'; |
| 492 } |
| 493 String value = visitExpression(node.value); |
| 494 return '$object.$field = $value'; |
| 495 } |
| 496 |
| 486 String visitCreateBox(CreateBox node) { | 497 String visitCreateBox(CreateBox node) { |
| 487 return 'CreateBox'; | 498 return 'CreateBox'; |
| 488 } | 499 } |
| 489 | 500 |
| 490 String visitCreateInstance(CreateInstance node) { | 501 String visitCreateInstance(CreateInstance node) { |
| 491 String className = node.classElement.name; | 502 String className = node.classElement.name; |
| 492 String arguments = node.arguments.map(visitExpression).join(', '); | 503 String arguments = node.arguments.map(visitExpression).join(', '); |
| 493 return 'CreateInstance $className($arguments)'; | 504 return 'CreateInstance $className($arguments)'; |
| 494 } | 505 } |
| 495 | 506 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 String prefix = v.element == null ? 'v' : '${v.element.name}_'; | 539 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
| 529 while (name == null || _usedNames.contains(name)) { | 540 while (name == null || _usedNames.contains(name)) { |
| 530 name = "$prefix${_counter++}"; | 541 name = "$prefix${_counter++}"; |
| 531 } | 542 } |
| 532 _names[v] = name; | 543 _names[v] = name; |
| 533 _usedNames.add(name); | 544 _usedNames.add(name); |
| 534 } | 545 } |
| 535 return name; | 546 return name; |
| 536 } | 547 } |
| 537 } | 548 } |
| OLD | NEW |