Class: RLTK::CG::GenericValue

Inherits:
Object
  • Object
show all
Includes:
BindingClass
Defined in:
lib/rltk/cg/generic_value.rb

Overview

GenericValue objects are used to pass parameters into ExecutionEngines as well as retreive an evaluated function’s result. They may contain values of several different types:

* Integer
* Float
* Boolean

Constant Summary collapse

CLASS_FINALIZER =

The Proc object called by the garbage collector to free resources used by LLVM.

Proc.new { |id| Bindings.dispose_generic_value(ptr) if ptr = ObjectSpace._id2ref(id).ptr }

Instance Attribute Summary collapse

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods included from BindingClass

#==

Constructor Details

#initialize(ruby_val, type = nil, signed = true) ⇒ GenericValue

Creates a new GenericValue from a given Ruby value.

Parameters:

  • ruby_val (FFI::Pointer, Integer, ::Float, Boolean)
  • type (Type) (defaults to: nil)

    Type of Integer or Float to create.

  • signed (Boolean) (defaults to: true)

    Signed or unsigned Integer.



41
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
67
# File 'lib/rltk/cg/generic_value.rb', line 41

def initialize(ruby_val, type = nil, signed = true)
	@ptr, @type =
	case ruby_val
	when FFI::Pointer
		[ruby_val, nil]

	when ::Integer
		type = if type then check_cg_type(type, IntType) else NativeIntType.instance end

		[Bindings.create_generic_value_of_int(type, ruby_val, signed.to_i), type]

	when ::Float
		type = if type then check_cg_type(type, RealType) else FloatType.instance end

		[Bindings.create_generic_value_of_float(type, ruby_val), type]

	when TrueClass
		[Bindings.create_generic_value_of_int(Int1Type, 1, 0), Int1Type]

	when FalseClass
		[Bindings.create_generic_value_of_int(Int1Type, 0, 0), Int1Type]
	end

	# Define a finalizer to free the memory used by LLVM for this
	# generic value.
	ObjectSpace.define_finalizer(self, CLASS_FINALIZER)
end

Instance Attribute Details

#typeType (readonly)

Returns LLVM type of this GenericValue.

Returns:

  • (Type)

    LLVM type of this GenericValue.



34
35
36
# File 'lib/rltk/cg/generic_value.rb', line 34

def type
  @type
end

Instance Method Details

#to_boolBoolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rltk/cg/generic_value.rb', line 86

def to_bool
	self.to_i(false).to_bool
end

#to_f(type = RLTK::CG::FloatType) ⇒ Float

Parameters:

  • type (FloatType) (defaults to: RLTK::CG::FloatType)

    Type of the real value stored in this GenericValue.

Returns:



81
82
83
# File 'lib/rltk/cg/generic_value.rb', line 81

def to_f(type = RLTK::CG::FloatType)
	Bindings.generic_value_to_float(@type || check_cg_type(type, RLTK::CG::NumberType), @ptr)
end

#to_i(signed = true) ⇒ Integer

Parameters:

  • signed (Boolean) (defaults to: true)

    Treat the GenericValue as a signed integer.

Returns:

  • (Integer)


72
73
74
75
76
# File 'lib/rltk/cg/generic_value.rb', line 72

def to_i(signed = true)
	val = Bindings.generic_value_to_int(@ptr, signed.to_i)

	if signed and val >= 2**63 then val - 2**64 else val end
end

#to_ptr_valueFFI::Pointer

Returns GenericValue as a pointer.

Returns:

  • (FFI::Pointer)

    GenericValue as a pointer.



91
92
93
# File 'lib/rltk/cg/generic_value.rb', line 91

def to_ptr_value
	Bindings.generic_value_to_pointer(@ptr)
end