Class: RLTK::CG::BasicBlock

Inherits:
Value show all
Defined in:
lib/rltk/cg/basic_block.rb

Overview

A BasicBlock is what instructions are inserted into and what functions are made of. BasicBlock objects may be created either using BasicBlock.new or using the Function::BasicBlockCollection#append method.

Defined Under Namespace

Classes: InstructionCollection

Instance Attribute Summary

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods inherited from Value

#==, #attributes, #bitcast, #constant?, #dump, #hash, #name, #name=, #null?, #print, #trunc, #trunc_or_bitcast, #type, #undefined?, #zextend, #zextend_or_bitcast

Methods included from BindingClass

#==

Constructor Details

#initialize(overloaded, name = '', builder = nil, context = nil, *block_args, &block) ⇒ BasicBlock

Create a new BasicBlock object. The way the block is created is determined by the overloaded parameter. If it is a Function object then the new block is appended to the end of the function. If overloaded is another BasicBlock object the new block will be inserted before that block.

A block may be given to this function to be invoked by #build.

Parameters:

  • overloaded (FFI::Pointer, Function, BasicBlock)

    Overloaded paramater that determines creation behaviour.

  • name (String) (defaults to: '')

    Name of this BasicBlock.

  • builder (Builder, nil) (defaults to: nil)

    Builder to be used by #build.

  • context (Context, nil) (defaults to: nil)

    Context in which to create the block.

  • block_args (Array<Object>)

    Arguments to be passed when block is invoked.

  • block (Proc)

    Block to be invoked by #build.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rltk/cg/basic_block.rb', line 42

def initialize(overloaded, name = '', builder = nil, context = nil, *block_args, &block)
	check_type(context, Context, 'context') if context
	
	@ptr =
	case overloaded
	when FFI::Pointer
		overloaded
		
	when Function
		if context
			Bindings.append_basic_block_in_context(context, overloaded, name)
		else
			Bindings.append_basic_block(overloaded, name)
		end
		
	when BasicBlock
		if context
			Bindings.insert_basic_block_in_context(context, overloaded, name)
		else
			Bindings.insert_basic_block(overloaded, name)
		end
	end
	
	self.build(builder, *block_args, &block) if block
end

Instance Method Details

#build(builder = nil, *block_args, &block) ⇒ Object

Used to add instructions to a BasicBlock. The block given to this method is executed inside the context of a RLTK::CG::Builder object, either the one passed in the builder parameter or one created for this call. Arguments may be passed into this block via the block_args parameter.

Examples:

fun = Function.new(...)
block.build do
     ret add(fun.params[0], fun.params[1])
end

Parameters:

  • builder (Builder, nil) (defaults to: nil)

    Builder in which to execute this block.

  • block_args (Array<Object>)

    Arguments to pass into block.

  • block (Proc)

    Block to execute inside builder.

Returns:



85
86
87
# File 'lib/rltk/cg/basic_block.rb', line 85

def build(builder = nil, *block_args, &block)
	if builder then builder else Builder.new end.build(self, *block_args, &block)
end

#insert_before(name = '', context = nil) ⇒ BasicBlock

Creates a new BasicBlock inserted immediately before this block.

Parameters:

  • name (String) (defaults to: '')

    Name of this BasicBlock.

  • context (Context) (defaults to: nil)

    Context in which to create this BasicBlock.

Returns:



95
96
97
# File 'lib/rltk/cg/basic_block.rb', line 95

def insert_before(name = '', context = nil)
	BasicBlock.new(self, name, context)
end

#instructionsInstructionCollect

Returns Collection of all instructions inside this BasicBlock.

Returns:

  • (InstructionCollect)

    Collection of all instructions inside this BasicBlock.



100
101
102
# File 'lib/rltk/cg/basic_block.rb', line 100

def instructions
	@instructions ||= InstructionCollection.new(self)
end

#nextBasicBlock?

Returns BasicBlock that occures immediately after this block or nil.

Returns:

  • (BasicBlock, nil)

    BasicBlock that occures immediately after this block or nil.



105
106
107
# File 'lib/rltk/cg/basic_block.rb', line 105

def next
	if (ptr = Bindings.get_next_basic_block(@ptr)).null? then nil else BasicBlock.new(ptr) end
end

#parentFunction

Returns Function object that this BasicBlock belongs to.

Returns:

  • (Function)

    Function object that this BasicBlock belongs to.



110
111
112
# File 'lib/rltk/cg/basic_block.rb', line 110

def parent
	if (ptr = Bindings.get_basic_block_parent(@ptr)).null? then nil else Function.new(ptr) end
end

#previousBasicBlock?

Returns BasicBlock that occures immediately before this block or nil.

Returns:

  • (BasicBlock, nil)

    BasicBlock that occures immediately before this block or nil.



115
116
117
# File 'lib/rltk/cg/basic_block.rb', line 115

def previous
	if (ptr = Bindings.get_previous_basic_block(@ptr)).null? then nil else BasicBlock.new(ptr) end
end