Class: RLTK::CG::PhiInst::IncomingCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rltk/cg/instruction.rb

Overview

This class is used to access a Phi node’s incoming BasicBlock/Value pairs.

Instance Method Summary collapse

Constructor Details

#initialize(phi) ⇒ IncomingCollection

Returns a new instance of IncomingCollection.

Parameters:

  • phi (PhiInst)

    Phi instruction for which this is a proxy.



162
163
164
# File 'lib/rltk/cg/instruction.rb', line 162

def initialize(phi)
	@phi = phi
end

Instance Method Details

#[](index) ⇒ Array(BasicBlock, Value)

Access the BasicBlock/Value pair at the given index.

Parameters:

  • index (Integer)

    Index of the desired pair. May be negative.

Returns:



171
172
173
174
175
176
177
# File 'lib/rltk/cg/instruction.rb', line 171

def [](index)
	index += self.size if index < 0
	
	if 0 <= index and index < self.size
		[self.block(index), self.value(index)]
	end
end

#add(overloaded, value = nil) ⇒ void Also known as: <<

This method returns an undefined value.

Add incoming BasicBlock/Value pairs to a Phi node.

phi.incoming.add(bb, val)

phi.incoming.add(=> val0, bb1 => val1)

Examples:

Adding a single block/value pair:

Adding several block/value pairs:

Parameters:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rltk/cg/instruction.rb', line 191

def add(overloaded, value = nil)
	blks, vals =
	if overloaded.is_a?(BasicBlock) and check_type(value, Value, 'value')
		[overloaded, value]
	else
		if RUBY_VERSION[0..2] == '1.9'
			[overloaded.keys, overloaded.values]
		else
			[(keys = overloaded.keys), overloaded.values_at(*keys)]
		end
	end
	
	vals_ptr = FFI::MemoryPointer.new(:pointer, vals.size)
	vals_ptr.write_array_of_pointer(vals)
	
	blks_ptr = FFI::MemoryPointer.new(:pointer, blks.size)
	blks_ptr.write_array_of_pointer(blks)

	returning(nil) { Bindings.add_incoming(@phi, vals_ptr, blks_ptr, vals.length) }
end

#block(index) ⇒ BasicBlock

Returns Incoming BasicBlock.

Parameters:

Returns:



216
217
218
219
220
221
222
# File 'lib/rltk/cg/instruction.rb', line 216

def block(index)
	index += self.size if index < 0
	
	if 0 <= index and index < self.size
		BasicBlock.new(Bindings.get_incoming_block(@phi, index))
	end
end

#each {|pair| ... } ⇒ Enumerator

An iterator for each incoming BasicBlock/Value pair.

Yield Parameters:

Returns:

  • (Enumerator)

    Returns an Enumerator if no block is given.



229
230
231
232
233
234
235
# File 'lib/rltk/cg/instruction.rb', line 229

def each
	return to_enum(:each) unless block_given?
	
	self.size.times { |index| yield self[index] }
	
	self
end

#sizeInteger

Returns Number of incoming BasicBlock/Value pairs.

Returns:



238
239
240
# File 'lib/rltk/cg/instruction.rb', line 238

def size
	Bindings.count_incoming(@phi)
end

#value(index) ⇒ BasicBlock

Returns Incoming Value.

Parameters:

Returns:



245
246
247
248
249
250
251
# File 'lib/rltk/cg/instruction.rb', line 245

def value(index)
	index += self.size if index < 0
	
	if 0 <= index and index < self.size
		Value.new(Bindings.get_incoming_value(@phi, index))
	end
end