Class: RLTK::CG::Instruction

Inherits:
User show all
Defined in:
lib/rltk/cg/instruction.rb

Overview

This class represents LLVM IR instructions.

Constant Summary collapse

TESTABLE =

Many of the C functions for interacting with instructions treat all instructions as Instruction objects. However, some Instruction sub-types can be tested for. This is a list of those sub-types and the names of their tests.

[
	:Alloca,
	:BitCast,
	:Call,
	:ExtractElement,
	:ExtractValue,
	:FCmp,
	[:FPExtend, :FPExt],
	:FPToSI,
	:FPToUI,
	:FPTrunc,
	:GetElementPtr,
	[:IntCmp, :ICmp],
	:InsertElement,
	:InsertValue,
	:IntToPtr,
	:Invoke,
	:Load,
	:PtrToInt,
	:Return,
	[:SignExtend, :SExt],
	:SIToFP,
	:Select,
	:ShuffleVector,
	:Store,
	:Switch,
	[:Truncate, :Trunc],
	:UIToFP,
	:Unreachable
]

Instance Attribute Summary

Attributes included from BindingClass

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from User

#operands

Methods included from AbstractClass

included

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(ptr) ⇒ Instruction

You should never instantiate Instruction object directly. Use the builder class to add new instructions.

Parameters:

  • ptr (FFI::Pointer)

    Pointer to a C instruction object.



87
88
89
# File 'lib/rltk/cg/instruction.rb', line 87

def initialize(ptr)
	@ptr = check_type(ptr, FFI::Pointer, 'ptr')
end

Class Method Details

.from_ptr(ptr) ⇒ Instruction

Instantiate an Instruction object from a given pointer. The type of the instruction is tested and the appropriate sub-type is picked if possible. If not, a generic Instruction object is returned.

Parameters:

  • ptr (FFI::Pointer)

    Pointer to a C instruction object.

Returns:

  • (Instruction)

    An Instruction or one of its sub-types.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rltk/cg/instruction.rb', line 65

def self.from_ptr(ptr)
	match = nil
	
	TESTABLE.each do |el|
		klass, test =
		if el.is_a?(Symbol)
			[RLTK::CG.const_get("#{el}Inst".to_sym), Bindings.get_bname("IsA#{el}Inst")]
			
		else
			[RLTK::CG.const_get("#{el.first}Inst".to_sym), Bindings.get_bname("IsA#{el.last}Inst")]
		end
		
		match = klass if Bindings.send(test, ptr)
	end
	
	if match then match else Intruction end.new(ptr)
end

Instance Method Details

#nextInstruction?

Returns Instruction that follows the current one in a BasicBlock.

Returns:



92
93
94
# File 'lib/rltk/cg/instruction.rb', line 92

def next
	if (ptr = Bindings.get_next_instruction(@ptr)).null? then nil else Instruction.from_ptr(ptr) end
end

#parentBasicBlock

Returns BasicBlock that contains this Instruction.

Returns:

  • (BasicBlock)

    BasicBlock that contains this Instruction.



97
98
99
# File 'lib/rltk/cg/instruction.rb', line 97

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

#previousInstruction?

Returns Instruction that precedes the current on in a BasicBlock.

Returns:



102
103
104
# File 'lib/rltk/cg/instruction.rb', line 102

def previous
	if (ptr = Bindings.get_previous_instruction(@ptr)).null? then nil else Instruction.from_ptr(ptr) end
end