Class: CompilerData

Inherits:
Object
  • Object
show all
Includes:
JRubyFX
Defined in:
lib/jruby_visualizer/compiler_data.rb

Overview

Data container for

* Abstract Syntax Tree (AST),
* Intermediate Code (IR)
* Ruby Code

It models and handles the dependencies between those compiler artifacts with JRubyFX properties

Constant Summary collapse

@@ir_builder =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_code = '') ⇒ CompilerData

Returns a new instance of CompilerData.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jruby_visualizer/compiler_data.rb', line 93

def initialize(ruby_code='')
  @ruby_code = SimpleStringProperty.new(ruby_code)
  @ast_root = SimpleObjectProperty.new(self, 'ast_root', self.class.parse(ruby_code))
  @ir_scope = FireChangeObjectProperty.new(self, 'ir_scope', self.class.build_ir(@ast_root.get))
  # bind change of Ruby code to reparsing an AST and set the property
  @ruby_code.add_invalidation_listener do |new_code_property|
    @ast_root.set(self.class.parse(new_code_property.get))
  end
  # bind change of AST to rebuilding IR and set the property
  @ast_root.add_invalidation_listener do |new_ast_property|
    @ir_scope.set(self.class.build_ir(new_ast_property.get))
  end

  # initialize scheduler
  reset_scheduler
end

Instance Attribute Details

#current_passObject

Returns the value of attribute current_pass.



49
50
51
# File 'lib/jruby_visualizer/compiler_data.rb', line 49

def current_pass
  @current_pass
end

#next_passObject

Returns the value of attribute next_pass.



49
50
51
# File 'lib/jruby_visualizer/compiler_data.rb', line 49

def next_pass
  @next_pass
end

Class Method Details

.build_ir(root_node) ⇒ Object



62
63
64
65
66
67
# File 'lib/jruby_visualizer/compiler_data.rb', line 62

def self.build_ir(root_node)
  unless @@ir_builder
    @@ir_builder = create_ir_builder
  end
  @@ir_builder.build_root(root_node)
end

.compiler_passes_namesObject



73
74
75
76
77
78
# File 'lib/jruby_visualizer/compiler_data.rb', line 73

def self.compiler_passes_names
  scheduler = JRuby::runtime.ir_manager.schedule_passes
  scheduler.map do |pass|
    pass_to_s(pass)
  end
end

.create_ir_builderObject



55
56
57
58
59
60
# File 'lib/jruby_visualizer/compiler_data.rb', line 55

def self.create_ir_builder
  ir_manager = JRuby::runtime.ir_manager
  ir_manager.dry_run = true

  org.jruby.ir.IRBuilder::createIRBuilder(JRuby::runtime, ir_manager)
end

.parse(code) ⇒ Object



51
52
53
# File 'lib/jruby_visualizer/compiler_data.rb', line 51

def self.parse(code)
  JRuby.parse(code)
end

.pass_to_s(pass) ⇒ Object



69
70
71
# File 'lib/jruby_visualizer/compiler_data.rb', line 69

def self.pass_to_s(pass)
  pass.class.to_s.split("::").last
end

.run_pass_on_all_scopes(pass, scope) ⇒ Object



110
111
112
113
114
115
# File 'lib/jruby_visualizer/compiler_data.rb', line 110

def self.run_pass_on_all_scopes(pass, scope)
  pass.run(scope)
  scope.lexical_scopes.each do |lex_scope|
    run_pass_on_all_scopes(pass, lex_scope)
  end
end

Instance Method Details

#reset_schedulerObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jruby_visualizer/compiler_data.rb', line 80

def reset_scheduler
  ir_manager = JRuby::runtime.ir_manager
  @scheduler = ir_manager.schedule_passes.iterator
  if @scheduler.has_next
    @current_pass = nil
    @next_pass = @scheduler.next
  else
    @current_pass = @next_pass = nil
  end
  # trigger to re build the old ir scope
  @ir_scope.set(self.class.build_ir(@ast_root.get))
end

#step_ir_passesObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jruby_visualizer/compiler_data.rb', line 117

def step_ir_passes
  if @next_pass
    @current_pass = @next_pass
    @next_pass =
      if @scheduler.has_next
        @scheduler.next
      else
        nil
      end
    scope = @ir_scope.get
    self.class.run_pass_on_all_scopes(@current_pass, scope)
    @ir_scope.set(scope)
  end
end