Module: Tem::Builders::Assembler::CodeBuilderBase
- Defined in:
- lib/tem/builders/assembler.rb
Overview
Module injected into the assembler’s code builder class.
Instance Method Summary collapse
-
#done_assembling(proxy) ⇒ Object
Called by assemble after its associated block returns.
-
#emit_bytes(emit_atom_name, emit_data) ⇒ Object
Emits code or data bytes, with associated link directives.
-
#emit_label(label_name) ⇒ Object
Emits labels, which are symbolic names for addresses.
-
#start_assembling ⇒ Object
Called by assemble before its associated block receives control.
Instance Method Details
#done_assembling(proxy) ⇒ Object
Called by assemble after its associated block returns.
This method is responsible for the final assembly steps (i.e. linking) and returning a processed result. The method’s result is returned by assemble.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/tem/builders/assembler.rb', line 292 def done_assembling(proxy) # Process link directives. abi = proxy.class.const_get :Abi @link_directives.each do |directive| if label = directive[:label] raise "Label #{label} undefined" unless address = @labels[label] else address = directive[:address] end if directive[:relative] address -= directive[:offset] + directive[:relative] end address_bytes = abi.send :"signed_to_#{directive[:type]}", address @bytes[directive[:offset], address_bytes.length] = *address_bytes end # Wrap all the built data into a nice package and return it. { :bytes => @bytes, :link_directives => @link_direcives, :labels => @labels, :line_info => @line_info } end |
#emit_bytes(emit_atom_name, emit_data) ⇒ Object
Emits code or data bytes, with associated link directives.
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/tem/builders/assembler.rb', line 269 def emit_bytes(emit_atom_name, emit_data) (emit_data[:link_directives] || []).each do |directive| directive[:offset] += @bytes.length @link_directives << directive end emit_data[:emit] ||= [] if emit_data[:emit].length > 0 @line_info << [@bytes.length, emit_atom_name, Kernel.caller(2)] end @bytes += emit_data[:emit] || [] end |
#emit_label(label_name) ⇒ Object
Emits labels, which are symbolic names for addresses.
283 284 285 286 |
# File 'lib/tem/builders/assembler.rb', line 283 def emit_label(label_name) raise "label #{label_name} already defined" if @labels[label_name] @labels[label_name] = @bytes.length end |
#start_assembling ⇒ Object
Called by assemble before its associated block receives control.
This method is responsible for setting up the state needed by the emit_ methods.
261 262 263 264 265 266 |
# File 'lib/tem/builders/assembler.rb', line 261 def start_assembling @bytes = [] @link_directives = [] @line_info = [] @labels = {} end |