|
| 1 | +#!/usr/bin/env jruby |
| 2 | + |
| 3 | +require 'optparse' |
| 4 | +require 'jruby' |
| 5 | + |
| 6 | +opts = {} |
| 7 | +options = { |
| 8 | + :print_source => false, |
| 9 | + :print_sexp => false, |
| 10 | + :print_ast => true, |
| 11 | + :print_ir => false, |
| 12 | + :print_pass => nil, |
| 13 | + :pretty_ir => false, |
| 14 | + :dot_format => false |
| 15 | +} |
| 16 | + |
| 17 | +OptionParser.new do |opts| |
| 18 | + opts.banner = "Usage: #{$0} [options]" |
| 19 | + |
| 20 | + opts.on('-d', '--dot', 'Display as dot data') do |h| |
| 21 | + options[:dot_format] = true |
| 22 | + end |
| 23 | + |
| 24 | + opts.on('-h', '--help', 'Display this help') do |h| |
| 25 | + puts opts |
| 26 | + exit true |
| 27 | + end |
| 28 | + |
| 29 | + opts.on('-i', '--ir', 'Dump all IR passes without executing') do |h| |
| 30 | + options[:print_ir] = true |
| 31 | + end |
| 32 | + |
| 33 | + opts.on('-p', '--pass passes_list', 'Dump IR after running a pass') do |pass| |
| 34 | + options[:print_pass] = pass |
| 35 | + end |
| 36 | + |
| 37 | + opts.on('-f', '--formatted-ir', 'Pretty printer for IR (without CFG)') do |f| |
| 38 | + options[:pretty_ir] = f |
| 39 | + end |
| 40 | + |
| 41 | + opts.on('-s', '--sexp', 'Display the S-Expression for the AST') do |t| |
| 42 | + options[:print_sexp] = true |
| 43 | + end |
| 44 | + |
| 45 | + opts.on('--source', 'Display the source') do |s| |
| 46 | + options[:print_source] = true |
| 47 | + end |
| 48 | + |
| 49 | + opts.on('--no-ast', 'Do not print out the AST for this (only useful with -s)') do |a| |
| 50 | + options[:print_ast] = false |
| 51 | + end |
| 52 | + |
| 53 | + opts.on('-e exp', '--expression') do |e| |
| 54 | + options[:expression] = e |
| 55 | + end |
| 56 | + |
| 57 | +end.parse! |
| 58 | + |
| 59 | +if ARGV.length > 1 |
| 60 | + abort "You may only specify one script (see --help)" |
| 61 | +elsif ARGV.length == 1 |
| 62 | + if options[:expression] |
| 63 | + abort "-e and a script is not a valid combination (see --help)" |
| 64 | + end |
| 65 | + options[:expression] = File.read(ARGV.shift) |
| 66 | +elsif ! options.has_key?(:expression) |
| 67 | + abort "No script specified (see --help)" |
| 68 | +end |
| 69 | + |
| 70 | +if options[:print_ir] && options[:print_pass] |
| 71 | + abort "-p and -i is not valid. Use only one of them (see --help)" |
| 72 | +end |
| 73 | + |
| 74 | +$indent_string = " " |
| 75 | + |
| 76 | +def indexes(string, lindex, rindex) |
| 77 | + lindex = string.index("(", lindex) if lindex != nil |
| 78 | + rindex = string.index(")", rindex) if rindex != nil |
| 79 | + return lindex, rindex |
| 80 | +end |
| 81 | + |
| 82 | +def indent(string) |
| 83 | + depth = -1 |
| 84 | + |
| 85 | + lindex, rindex = indexes(string, 0, 0) |
| 86 | + |
| 87 | + while (lindex != nil || rindex != nil) |
| 88 | + if (lindex != nil && lindex < rindex) |
| 89 | + depth += 1 |
| 90 | + string[lindex, 1] = "\n#{$indent_string * depth}" |
| 91 | + else |
| 92 | + depth -= 1 |
| 93 | + string[rindex, 1] = "\n" |
| 94 | + end |
| 95 | + |
| 96 | + lindex, rindex = indexes(string, lindex, rindex) |
| 97 | + end |
| 98 | + string.gsub(/,\s*$/, '').squeeze("\n") |
| 99 | +end |
| 100 | + |
| 101 | +if options[:print_source] |
| 102 | + puts "Source:" |
| 103 | + puts options[:expression] |
| 104 | + puts |
| 105 | +end |
| 106 | + |
| 107 | +module DotGraph |
| 108 | + def self.dot_label(node) |
| 109 | + extra = case node |
| 110 | + when org.jruby.ast.StrNode then |
| 111 | + ": '#{node.value}'" |
| 112 | + when org.jruby.ast.FixnumNode then |
| 113 | + ": #{node.value}" |
| 114 | + when org.jruby.ast.FloatNode then |
| 115 | + ": #{node.value}" |
| 116 | + when org.jruby.ast.types.INameNode then |
| 117 | + ": #{node.name}" |
| 118 | + else |
| 119 | + "" |
| 120 | + end |
| 121 | + |
| 122 | + "#{short_name(node)}#{extra}" |
| 123 | + end |
| 124 | + |
| 125 | + def self.short_name(node) |
| 126 | + node.class.name.split("::")[-1].gsub("Node", "") |
| 127 | + end |
| 128 | + |
| 129 | + def self.dot_node_def(node) |
| 130 | + %Q{#{node.hash} [label="#{dot_label(node)}"];\n} |
| 131 | + end |
| 132 | + |
| 133 | + def self.dot(defs, parent) |
| 134 | + defs[parent.hash] = dot_node_def(parent) |
| 135 | + |
| 136 | + "".tap do |str| |
| 137 | + parent.child_nodes.each do |child| |
| 138 | + str << "#{parent.hash} -> #{child.hash};\n" |
| 139 | + str << dot(defs, child) |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + def self.print_graph(root_node) |
| 145 | + defs = {} |
| 146 | + graph_section = DotGraph.dot(defs, root_node) |
| 147 | + puts "digraph AST {" |
| 148 | + puts defs.values.join('') |
| 149 | + puts graph_section |
| 150 | + puts "}" |
| 151 | + end |
| 152 | +end |
| 153 | + |
| 154 | +root = JRuby.parse(options[:expression]) |
| 155 | + |
| 156 | +if options[:print_ast] |
| 157 | + if options[:dot_format] |
| 158 | + DotGraph.print_graph(root) |
| 159 | + else |
| 160 | + print "AST:" |
| 161 | + puts indent(root.to_string) |
| 162 | + puts |
| 163 | + end |
| 164 | +end |
| 165 | + |
| 166 | +def print_passes_on(scope, passes) |
| 167 | + if !scope.kind_of? org.jruby.ir.IRClosure |
| 168 | + passes.each { |pass| pass.run(scope) } |
| 169 | + end |
| 170 | + |
| 171 | + scope.lexical_scopes.each do |child_scope| |
| 172 | + child_scope.prepare_for_compilation |
| 173 | + print_passes_on(child_scope, passes) |
| 174 | + end |
| 175 | +end |
| 176 | + |
| 177 | +def print_pass_on(scope, pass) |
| 178 | + if !scope.kind_of? org.jruby.ir.IRClosure |
| 179 | + pass.run(scope) |
| 180 | + end |
| 181 | + |
| 182 | + scope.lexical_scopes.each do |child_scope| |
| 183 | + print_pass_on(child_scope, pass) |
| 184 | + end |
| 185 | +end |
| 186 | + |
| 187 | +def ir_setup(root) |
| 188 | + runtime = JRuby::runtime |
| 189 | + manager = runtime.ir_manager |
| 190 | + |
| 191 | + JRuby::IR.compiler_debug = true |
| 192 | + |
| 193 | + |
| 194 | + builder = org.jruby.ir.builder.IRBuilderAST |
| 195 | + |
| 196 | + scope = builder.build_root(manager, root).scope |
| 197 | + scope.prepare_for_compilation |
| 198 | + passes = manager.get_compiler_passes(scope) |
| 199 | + [scope, passes] |
| 200 | +end |
| 201 | + |
| 202 | +module IRPrettyPrinter |
| 203 | + def self.pretty_ir(scope, indent="") |
| 204 | + i = 0 |
| 205 | + pretty_str = scope.instrs.map do |instr| |
| 206 | + f_str = "%s%3i\s\s%s" % [indent, i, instr] |
| 207 | + i += 1 |
| 208 | + f_str |
| 209 | + end |
| 210 | + pretty_str = [indent + scope.to_s] + pretty_str |
| 211 | + scope.lexical_scopes.each do |lex_scope| |
| 212 | + pretty_str += pretty_ir(lex_scope, indent + "\s" * 4) |
| 213 | + end |
| 214 | + pretty_str |
| 215 | + end |
| 216 | + |
| 217 | + def self.print_ir(scope) |
| 218 | + instrs = pretty_ir(scope) |
| 219 | + instrs.each do |instr| |
| 220 | + puts instr |
| 221 | + end |
| 222 | + end |
| 223 | +end |
| 224 | + |
| 225 | +if options[:pretty_ir] |
| 226 | + scope, passes = ir_setup(root) |
| 227 | + puts "IR:" |
| 228 | + IRPrettyPrinter.print_ir(scope) |
| 229 | +end |
| 230 | + |
| 231 | +if options[:print_pass] |
| 232 | + scope, passes = ir_setup(root) |
| 233 | + pass_name = options[:print_pass] |
| 234 | + pass = passes.find do |p| |
| 235 | + p.java_class.to_s.include?(pass_name.to_s) |
| 236 | + end |
| 237 | + print_pass_on(scope, pass) |
| 238 | +end |
| 239 | + |
| 240 | +if options[:print_ir] |
| 241 | + scope, passes = ir_setup(root) |
| 242 | + |
| 243 | + print_passes_on(scope, passes) |
| 244 | +end |
| 245 | + |
| 246 | +if options[:print_sexp] |
| 247 | + puts "SEXP:" |
| 248 | + puts org.jruby.ast.util.SexpMaker.create(root) |
| 249 | +end |
0 commit comments