Class: Kumi::Support::LIRPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/support/lir_printer.rb

Constant Summary collapse

LIR =
Kumi::Core::LIR

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(show_stamps:, show_locations:) ⇒ LIRPrinter

Returns a new instance of LIRPrinter.



25
26
27
28
# File 'lib/kumi/support/lir_printer.rb', line 25

def initialize(show_stamps:, show_locations:)
  @show_stamps = show_stamps
  @show_locations = show_locations
end

Class Method Details

.indent(str, n) ⇒ Object



137
138
139
140
# File 'lib/kumi/support/lir_printer.rb', line 137

def self.indent(str, n)
  pad = " " * n
  str.lines.map { |ln| pad + ln }.join
end

Public: print all declarations ops_by_decl: { “name” => { operations: [Instruction…] } }



10
11
12
13
14
15
16
17
18
# File 'lib/kumi/support/lir_printer.rb', line 10

def self.print(ops_by_decl, show_stamps: true, show_locations: false)
  out = +"(LIR\n"
  ops_by_decl.each do |name, h|
    out << "  (Declaration #{name}\n"
    out << indent(print_instructions(h[:operations], show_stamps:, show_locations:), 4)
    out << "  )\n"
  end
  out << ")\n"
end

Public: print a single instruction list



21
22
23
# File 'lib/kumi/support/lir_printer.rb', line 21

def self.print_instructions(instructions, show_stamps: true, show_locations: false)
  new(show_stamps:, show_locations:).print_instructions(instructions)
end

Instance Method Details



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kumi/support/lir_printer.rb', line 30

def print_instructions(instructions)
  out = +""
  indent = 0
  instructions.each do |ins|
    case ins.opcode
    when :LoopStart
      out << ("  " * indent)
      out << loop_start_line(ins) << nl(ins)
      indent += 1
    when :LoopEnd
      indent -= 1
      out << ("  " * indent)
      out << "end_loop" << nl(ins)
    else
      out << ("  " * indent)
      out << instr_line(ins) << nl(ins)
    end
  end
  out
end