dump CFG to a graph by dehydra:
– function ‘write_dot_graph’ in analysis.js : prints out a graph in ‘dot’ format, as used by GraphViz
 to produce an SVG file, run ‘dot -Tsvg foo.dot > foo.svg’
– more of a demo that prints live range data. 
C’est quoi les CFG ?
Context-free grammar en fait.
La documentation là dessus dans GCC : Et un peu de réflexion sur le sujet. 
Resulting Treehydra script that prints all the CFGs alone :
// Run after CFG-building pass so that we have CFGs.
require({ after_gcc_pass: 'cfg' });
include('gcc_util.js');
include('gcc_print.js');
// Treehyra calls this callback for each function defined.
function process_tree(fndecl) {
  // Get the CFG for the function
  var cfg = function_decl_cfg(fndecl);
  // Write a DOT graph representation of the CFG
  my_write_dot_graph(cfg, 'My CFG', '/tmp/cfg.dot');
}
// I copied this from analysis.js and removed a line that prints out
// live ranges based on a previous analysis.
/** Write a DOT representation of the cfg to the given file. */
function my_write_dot_graph(cfg, name, filename) {
  let content = 'digraph ' + name + ' {\n';
  content += '\tnode [shape="rect", fontname="Helvetica"];\n'
  for (let bb in cfg_bb_iterator(cfg)) {
    //content += '\t' + bb.index + '[label="' + bb_label(cfg, bb) + '"];\n';
    let items = [
      bb_label(bb),
      [ isn_display(isn) for (isn in bb_isn_iterator(bb)) ].join('\\n')
    ];
    let label = '{' + items.join('|') + '}';
    // Dot requires escaping with record shapes.
    label = label.replace('>', '\\>')
    label = label.replace('< ', '\\<')
    content += '\t' + bb.index + '[shape=record,label="' + label + '"];\n'
    for (let bb_succ in bb_succs(bb)) {
      content += '\t' + bb.index + ' -> ' + bb_succ.index + ';\n'
    }
  }
  content += '}\n';
  write_file(filename, content);
} 


Commentaires