Class: Kumi::Core::LIR::Peephole::Window
- Inherits:
-
Object
- Object
- Kumi::Core::LIR::Peephole::Window
- 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
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
- #const?(offset = 0, value: nil) ⇒ Boolean
- #current ⇒ Object
- #delete(count = 1) ⇒ Object
-
#initialize(ops, index) ⇒ Window
constructor
A new instance of Window.
- #insert_after(*new_ops) ⇒ Object
- #insert_before(*new_ops) ⇒ Object
- #instruction(offset = 0) ⇒ Object
- #instructions(count) ⇒ Object
- #literal(offset = 0) ⇒ Object
- #literal_value(offset = 0) ⇒ Object
- #match?(*opcodes) ⇒ Boolean
- #next_index ⇒ Object
- #opcode(offset = 0) ⇒ Object
- #replace(count, with:) ⇒ Object
- #result_register(offset = 0) ⇒ Object
- #rewind(count = 1) ⇒ Object
- #size ⇒ Object
- #skip(count = 1) ⇒ Object
- #zero?(offset = 0) ⇒ Boolean
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
#index ⇒ Object (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
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 |
#current ⇒ Object
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
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_index ⇒ Object
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 |
#size ⇒ Object
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
86 87 88 |
# File 'lib/kumi/core/lir/peephole.rb', line 86 def zero?(offset = 0) const?(offset, value: 0) end |