Class: Kumi::Core::LIR::Peephole::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/lir/peephole.rb

Overview

A mutable view over the instruction stream anchored at a given index.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops, index) ⇒ Window

Returns a new instance of Window.



48
49
50
51
52
# File 'lib/kumi/core/lir/peephole.rb', line 48

def initialize(ops, index)
  @ops = ops
  @index = index
  @next_index = index + 1
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



46
47
48
# File 'lib/kumi/core/lir/peephole.rb', line 46

def index
  @index
end

Instance Method Details

#const?(offset = 0, value: nil) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/kumi/core/lir/peephole.rb', line 77

def const?(offset = 0, value: nil)
  ins = instruction(offset)
  return false unless ins&.opcode == :Constant

  return true if value.nil?

  literal_value(offset) == value
end

#currentObject



54
# File 'lib/kumi/core/lir/peephole.rb', line 54

def current = instruction(0)

#delete(count = 1) ⇒ Object



106
107
108
# File 'lib/kumi/core/lir/peephole.rb', line 106

def delete(count = 1)
  replace(count, with: [])
end

#insert_after(*new_ops) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/kumi/core/lir/peephole.rb', line 120

def insert_after(*new_ops)
  new_ops = normalize(new_ops)
  return @next_index = @index + 1 if new_ops.empty?

  @ops.insert(@index + 1, *new_ops)
  @next_index = @index + 1
  new_ops
end

#insert_before(*new_ops) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/kumi/core/lir/peephole.rb', line 110

def insert_before(*new_ops)
  new_ops = normalize(new_ops)
  return @next_index = @index if new_ops.empty?

  @ops.insert(@index, *new_ops)
  @index += new_ops.length
  @next_index = @index
  new_ops
end

#instruction(offset = 0) ⇒ Object



56
# File 'lib/kumi/core/lir/peephole.rb', line 56

def instruction(offset = 0) = @ops[@index + offset]

#instructions(count) ⇒ Object



62
63
64
# File 'lib/kumi/core/lir/peephole.rb', line 62

def instructions(count)
  @ops[@index, count].compact
end

#literal(offset = 0) ⇒ Object



90
91
92
93
# File 'lib/kumi/core/lir/peephole.rb', line 90

def literal(offset = 0)
  ins = instruction(offset)
  Array(ins&.immediates).first
end

#literal_value(offset = 0) ⇒ Object



95
96
97
# File 'lib/kumi/core/lir/peephole.rb', line 95

def literal_value(offset = 0)
  literal(offset)&.value
end

#match?(*opcodes) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/kumi/core/lir/peephole.rb', line 66

def match?(*opcodes)
  opcodes.each_with_index.all? do |opcode, off|
    ins = instruction(off)
    ins&.respond_to?(:opcode) && ins.opcode == opcode
  end
end

#next_indexObject



141
142
143
144
145
# File 'lib/kumi/core/lir/peephole.rb', line 141

def next_index
  @next_index = 0 if @next_index.negative?
  @next_index = @ops.length if @next_index > @ops.length
  @next_index
end

#opcode(offset = 0) ⇒ Object



58
59
60
# File 'lib/kumi/core/lir/peephole.rb', line 58

def opcode(offset = 0)
  instruction(offset)&.opcode
end

#replace(count, with:) ⇒ Object



99
100
101
102
103
104
# File 'lib/kumi/core/lir/peephole.rb', line 99

def replace(count, with:)
  replacements = normalize(with)
  @ops[@index, count] = replacements
  @next_index = @index
  replacements
end

#result_register(offset = 0) ⇒ Object



73
74
75
# File 'lib/kumi/core/lir/peephole.rb', line 73

def result_register(offset = 0)
  instruction(offset)&.result_register
end

#rewind(count = 1) ⇒ Object



135
136
137
138
139
# File 'lib/kumi/core/lir/peephole.rb', line 135

def rewind(count = 1)
  count = 1 if count.nil? || count < 1
  @next_index = [@index - count, 0].max
  nil
end

#sizeObject



147
148
149
# File 'lib/kumi/core/lir/peephole.rb', line 147

def size
  @ops.length
end

#skip(count = 1) ⇒ Object



129
130
131
132
133
# File 'lib/kumi/core/lir/peephole.rb', line 129

def skip(count = 1)
  count = 1 if count.nil? || count < 1
  @next_index = @index + count
  nil
end

#zero?(offset = 0) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/kumi/core/lir/peephole.rb', line 86

def zero?(offset = 0)
  const?(offset, value: 0)
end