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.



177
178
179
# File 'lib/rltk/cg/instruction.rb', line 177

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:



186
187
188
189
190
191
192
# File 'lib/rltk/cg/instruction.rb', line 186

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.

Examples:

Adding a single block/value pair:

phi.incoming.add(bb, val)

Adding several block/value pairs:

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

Parameters:



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rltk/cg/instruction.rb', line 206

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)

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

#block(index) ⇒ BasicBlock

Returns Incoming BasicBlock.

Parameters:

  • index (Integer)

    Index of desired incoming BasicBlock.

Returns:



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

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.



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

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:



253
254
255
# File 'lib/rltk/cg/instruction.rb', line 253

def size
	Bindings.count_incoming(@phi)
end

#value(index) ⇒ BasicBlock

Returns Incoming Value.

Parameters:

  • index (Integer)

    Index of desired incoming Value.

Returns:



260
261
262
263
264
265
266
# File 'lib/rltk/cg/instruction.rb', line 260

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