<commit_before>// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#library('tracer');

#import('dart:io');
#import('ssa.dart');
#import('../leg.dart');

final bool GENERATE_SSA_TRACE = false;

class HTracer extends HGraphVisitor implements Tracer {
  int indent = 0;
  final RandomAccessFile output;
  final bool enabled = GENERATE_SSA_TRACE;

  HTracer([String path = "dart.cfg"])
      : output = GENERATE_SSA_TRACE ? new File(path).openSync(FileMode.WRITE)
                                    : null;

  void close() {
    if (enabled) output.closeSync();
  }

  void traceCompilation(String methodName) {
    tag("compilation", () {
      printProperty("name", methodName);
      printProperty("method", methodName);
      printProperty("date", new Date.now().value);
    });
  }

  void traceGraph(String name, HGraph graph) {
    if (!enabled) return;
    tag("cfg", () {
      printProperty("name", name);
      visitDominatorTree(graph);
    });
  }

  void addPredecessors(HBasicBlock block) {
    if (block.predecessors.isEmpty()) {
      printEmptyProperty("predecessors");
    } else {
      addIndent();
      add("predecessors");
      for (HBasicBlock predecessor in block.predecessors) {
        add(' "B${predecessor.id}"');
      }
      add("\n");
    }
  }

  void addSuccessors(HBasicBlock block) {
    if (block.successors.isEmpty()) {
      printEmptyProperty("successors");
    } else {
      addIndent();
      add("successors");
      for (HBasicBlock successor in block.successors) {
        add(' "B${successor.id}"');
      }
      add("\n");
    }
  }

  void addInstructions(HInstructionStringifier stringifier,
                       HInstructionList list) {
    for (HInstruction instruction = list.first;
         instruction !== null;
         instruction = instruction.next) {
      int bci = 0;
      int uses = instruction.usedBy.length;
      addIndent();
      String temporaryId = stringifier.temporaryId(instruction);
      String instructionString = stringifier.visit(instruction);
      add("$bci $uses $temporaryId $instructionString <|@\n");
    }
  }

  void visitBasicBlock(HBasicBlock block) {
    HInstructionStringifier stringifier = new HInstructionStringifier(block);
    assert(block.id !== null);
    tag("block", () {
      printProperty("name", "B${block.id}");
      printProperty("from_bci", -1);
      printProperty("to_bci", -1);
      addPredecessors(block);
      addSuccessors(block);
      printEmptyProperty("xhandlers");
      printEmptyProperty("flags");
      if (block.dominator !== null) {
        printProperty("dominator", "B${block.dominator.id}");
      }
      tag("states", () {
        tag("locals", () {
          printProperty("size", 0);
          printProperty("method", "None");
          block.forEachPhi((phi) {
            String phiId = stringifier.temporaryId(phi);
            StringBuffer inputIds = new StringBuffer();
            for (int i = 0; i < phi.inputs.length; i++) {
              inputIds.add(stringifier.temporaryId(phi.inputs[i]));
              inputIds.add(" ");
            }
            println("${phi.id} $phiId [ $inputIds]");
          });
        });
      });
      tag("HIR", () {
        addInstructions(stringifier, block.phis);
        addInstructions(stringifier, block);
      });
    });
  }

  void tag(String tagName, Function f) {
    println("begin_$tagName");
    indent++;
    f();
    indent--;
    println("end_$tagName");
  }

  void println(String string) {
    addIndent();
    add(string);
    add("\n");
  }

  void printEmptyProperty(String propertyName) {
    println(propertyName);
  }

  void printProperty(String propertyName, var value) {
    if (value is num) {
      println("$propertyName $value");
    } else {
      println('$propertyName "$value"');
    }
  }

  void add(String string) {
    output.writeStringSync(string);
  }

  void addIndent() {
    for (int i = 0; i < indent; i++) {
      add("  ");
    }
  }
}

class HInstructionStringifier implements HVisitor<String> {
  HBasicBlock currentBlock;

  HInstructionStringifier(this.currentBlock);

  visit(HInstruction node) => node.accept(this);

  visitBasicBlock(HBasicBlock node) {
    unreachable();
  }

  String temporaryId(HInstruction instruction) {
    String prefix;
    HType type = instruction.propagatedType;
    switch (type) {
      case HType.MUTABLE_ARRAY: prefix = 'm'; break;
      case HType.READABLE_ARRAY: prefix = 'a'; break;
      case HType.BOOLEAN: prefix = 'b'; break;
      case HType.INTEGER: prefix = 'i'; break;
      case HType.DOUBLE: prefix = 'd'; break;
      case HType.NUMBER: prefix = 'n'; break;
      case HType.STRING: prefix = 's'; break;
      case HType.UNKNOWN: prefix = 'v'; break;
      case HType.CONFLICTING: prefix = 'c'; break;
      case HType.STRING_OR_ARRAY: prefix = 'r'; break;
      default: unreachable();
    }
    return "$prefix${instruction.id}";
  }<commit_msg>Fix the tracer. Review URL: https://chromiumcodereview.appspot.com//10166030<commit_after>// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#library('tracer');

#import('dart:io');
#import('ssa.dart');
#import('../leg.dart');

final bool GENERATE_SSA_TRACE = false;

class HTracer extends HGraphVisitor implements Tracer {
  int indent = 0;
  final RandomAccessFile output;
  final bool enabled = GENERATE_SSA_TRACE;

  HTracer([String path = "dart.cfg"])
      : output = GENERATE_SSA_TRACE ? new File(path).openSync(FileMode.WRITE)
                                    : null;

  void close() {
    if (enabled) output.closeSync();
  }

  void traceCompilation(String methodName) {
    tag("compilation", () {
      printProperty("name", methodName);
      printProperty("method", methodName);
      printProperty("date", new Date.now().value);
    });
  }

  void traceGraph(String name, HGraph graph) {
    if (!enabled) return;
    tag("cfg", () {
      printProperty("name", name);
      visitDominatorTree(graph);
    });
  }

  void addPredecessors(HBasicBlock block) {
    if (block.predecessors.isEmpty()) {
      printEmptyProperty("predecessors");
    } else {
      addIndent();
      add("predecessors");
      for (HBasicBlock predecessor in block.predecessors) {
        add(' "B${predecessor.id}"');
      }
      add("\n");
    }
  }

  void addSuccessors(HBasicBlock block) {
    if (block.successors.isEmpty()) {
      printEmptyProperty("successors");
    } else {
      addIndent();
      add("successors");
      for (HBasicBlock successor in block.successors) {
        add(' "B${successor.id}"');
      }
      add("\n");
    }
  }

  void addInstructions(HInstructionStringifier stringifier,
                       HInstructionList list) {
    for (HInstruction instruction = list.first;
         instruction !== null;
         instruction = instruction.next) {
      int bci = 0;
      int uses = instruction.usedBy.length;
      addIndent();
      String temporaryId = stringifier.temporaryId(instruction);
      String instructionString = stringifier.visit(instruction);
      add("$bci $uses $temporaryId $instructionString <|@\n");
    }
  }

  void visitBasicBlock(HBasicBlock block) {
    HInstructionStringifier stringifier = new HInstructionStringifier(block);
    assert(block.id !== null);
    tag("block", () {
      printProperty("name", "B${block.id}");
      printProperty("from_bci", -1);
      printProperty("to_bci", -1);
      addPredecessors(block);
      addSuccessors(block);
      printEmptyProperty("xhandlers");
      printEmptyProperty("flags");
      if (block.dominator !== null) {
        printProperty("dominator", "B${block.dominator.id}");
      }
      tag("states", () {
        tag("locals", () {
          printProperty("size", 0);
          printProperty("method", "None");
          block.forEachPhi((phi) {
            String phiId = stringifier.temporaryId(phi);
            StringBuffer inputIds = new StringBuffer();
            for (int i = 0; i < phi.inputs.length; i++) {
              inputIds.add(stringifier.temporaryId(phi.inputs[i]));
              inputIds.add(" ");
            }
            println("${phi.id} $phiId [ $inputIds]");
          });
        });
      });
      tag("HIR", () {
        addInstructions(stringifier, block.phis);
        addInstructions(stringifier, block);
      });
    });
  }

  void tag(String tagName, Function f) {
    println("begin_$tagName");
    indent++;
    f();
    indent--;
    println("end_$tagName");
  }

  void println(String string) {
    addIndent();
    add(string);
    add("\n");
  }

  void printEmptyProperty(String propertyName) {
    println(propertyName);
  }

  void printProperty(String propertyName, var value) {
    if (value is num) {
      println("$propertyName $value");
    } else {
      println('$propertyName "$value"');
    }
  }

  void add(String string) {
    output.writeStringSync(string);
  }

  void addIndent() {
    for (int i = 0; i < indent; i++) {
      add("  ");
    }
  }
}

class HInstructionStringifier implements HVisitor<String> {
  HBasicBlock currentBlock;

  HInstructionStringifier(this.currentBlock);

  visit(HInstruction node) => node.accept(this);

  visitBasicBlock(HBasicBlock node) {
    unreachable();
  }

  String temporaryId(HInstruction instruction) {
    String prefix;
    HType type = instruction.propagatedType;
    if (type.isNonPrimitive()) {
      prefix = 'U';
    } else {
      switch (type) {
        case HType.MUTABLE_ARRAY: prefix = 'm'; break;
        case HType.READABLE_ARRAY: prefix = 'a'; break;
        case HType.BOOLEAN: prefix = 'b'; break;
        case HType.INTEGER: prefix = 'i'; break;
        case HType.DOUBLE: prefix = 'd'; break;
        case HType.NUMBER: prefix = 'n'; break;
        case HType.STRING: prefix = 's'; break;
        case HType.UNKNOWN: prefix = 'v'; break;
        case HType.CONFLICTING: prefix = 'c'; break;
        case HType.STRING_OR_ARRAY: prefix = 'r'; break;
        default: unreachable();
      }
    }
    return "$prefix${instruction.id}";
  }
{'fraction_non_alphanumeric': 0.14049666634457436, 'fraction_numerical': 0.002898830804908687, 'mean_word_length': 2.7176724137931036, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4616028', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3131, 'n_words': 696}