Class: RLTK::CG::ConstantInteger Abstract

Inherits:
ConstantNumber show all
Includes:
AbstractClass
Defined in:
lib/rltk/cg/value.rb

Overview

This class is abstract.

All integer constants inherit from this class.

Direct Known Subclasses

Int1, Int16, Int32, Int64, Int8

Instance Attribute Summary collapse

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods included from AbstractClass

included

Methods inherited from ConstantNumber

type, #type

Methods inherited from Constant

#bitcast_to, #get_element_ptr, #get_element_ptr_in_bounds

Methods inherited from User

#operands

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(overloaded0 = nil, overloaded1 = nil, size = nil) ⇒ ConstantInteger

The constructor for ConstantInteger’s various sub-classes. This constructor is a bit complicated due to having two overloaded parameters, but once you see the valid combinations it is a bit simpler.

Examples:

Constant (signed) integer from Ruby Integer:

Int32.new(128)

Constant (signed) base 8 integer from Ruby String:

Int32.new('72', 8)

Constant integer of all 1s:

Int32.new

Parameters:

  • overloaded0 (FFI::Pointer, Integer, String, nil) (defaults to: nil)

    Pointer to a ConstantInteger, value, or string representing value.

  • overloaded1 (Boolean, Integer) (defaults to: nil)

    Signed or unsigned (when overloaded0 is Integer) or base used to decode string value.

  • size (Integer) (defaults to: nil)

    Optional length of string to use.



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/rltk/cg/value.rb', line 566

def initialize(overloaded0 = nil, overloaded1 = nil, size = nil)
	@ptr =
	case overloaded0
	when FFI::Pointer
		overloaded0
		
	when Integer
		@signed = overloaded1 or true
		
		Bindings.const_int(self.type, overloaded0, @signed.to_i)
		
	when String
		base = overloaded1 or 10
		
		if size
			Bindings.const_int_of_string_and_size(self.type, overloaded0, size, base)
		else
			Bindings.const_int_of_string(self.type, overloaded0, base)
		end
	else
		@signed = true
		
		Bindings.const_all_ones(self.type)
	end
end

Instance Attribute Details

#signedBoolean (readonly)

Returns If the integer is signed or not.

Returns:

  • (Boolean)

    If the integer is signed or not.



547
548
549
# File 'lib/rltk/cg/value.rb', line 547

def signed
  @signed
end

Instance Method Details

#%(rhs) ⇒ ConstantInteger

Modulo this value by another value. Uses signed modulo.

Parameters:

Returns:



725
726
727
# File 'lib/rltk/cg/value.rb', line 725

def %(rhs)
	self.class.new(Bindings.const_s_rem(@ptr, rhs))
end

#*(rhs) ⇒ ConstantInteger

Multiply this value with another value.

Parameters:

Returns:



665
666
667
# File 'lib/rltk/cg/value.rb', line 665

def *(rhs)
	self.class.new(Bindings.const_mul(@ptr, rhs))
end

#+(rhs) ⇒ ConstantInteger

Add this value with another value.

Parameters:

Returns:



603
604
605
# File 'lib/rltk/cg/value.rb', line 603

def +(rhs)
	self.class.new(Bindings.const_add(@ptr, rhs))
end

#-(rhs) ⇒ ConstantInteger

Subtract a value from this value.

Parameters:

Returns:



634
635
636
# File 'lib/rltk/cg/value.rb', line 634

def -(rhs)
	self.class.new(Bindings.const_sub(@ptr, rhs))
end

#-@ConstantInteger

Negate this value.

Returns:



743
744
745
# File 'lib/rltk/cg/value.rb', line 743

def -@
	self.class.new(Bindings.const_neg(@ptr))
end

#/(rhs) ⇒ ConstantInteger

Divide this value by another value. Uses signed division.

Parameters:

Returns:



696
697
698
# File 'lib/rltk/cg/value.rb', line 696

def /(rhs)
	self.class.new(Bindings.const_s_div(@ptr, rhs))
end

#and(rhs) ⇒ ConstantInteger

Bitwise AND this value with another.

Parameters:

Returns:



828
829
830
# File 'lib/rltk/cg/value.rb', line 828

def and(rhs)
	self.class.new(Bindings.const_and(@ptr, rhs))
end

#ashr(bits) ⇒ ConstantInteger Also known as: >>

Arithmetic right shift.

Parameters:

  • bits (Integer)

    Number of bits to shift.

Returns:



809
810
811
# File 'lib/rltk/cg/value.rb', line 809

def ashr(bits)
	self.class.new(Bindings.const_a_shr(@ptr, bits))
end

#cast(type, signed = true) ⇒ ConstantNumber

Cast this constant integer to another number type.

Parameters:

  • type (NumberType)

    Desired type to cast to.

  • signed (Boolean) (defaults to: true)

    Is the value signed or not.

Returns:



867
868
869
# File 'lib/rltk/cg/value.rb', line 867

def cast(type, signed = true)
	type.value_class.new(Bindings.const_int_cast(@ptr, check_cg_type(type, NumberType), signed.to_i))
end

#cmp(pred, rhs) ⇒ Int1

Compare this value to another value.

Parameters:

  • pred (Symbol)

    An integer predicate.

  • rhs (ConstantInteger)

    Value to compare to.

Returns:

  • (Int1)

    Value used to represent a Boolean value.

See Also:



879
880
881
# File 'lib/rltk/cg/value.rb', line 879

def cmp(pred, rhs)
	Int1.new(Bindings.const_i_cmp(pred, @ptr, rhs))
end

#extact_sdiv(rhs) ⇒ ConstantInteger

Divide this value by another value. Uses exact signed division.

Parameters:

Returns:



705
706
707
# File 'lib/rltk/cg/value.rb', line 705

def extact_sdiv(rhs)
	self.class.new(Bindings.const_extact_s_div(@ptr, rhs))
end

#lshr(bits) ⇒ ConstantInteger

Logical right shift.

Parameters:

  • bits (Integer)

    Number of bits to shift.

Returns:



819
820
821
# File 'lib/rltk/cg/value.rb', line 819

def lshr(bits)
	self.class.new(Bindings.const_l_shr(@ptr, bits))
end

#notConstantInteger

Bitwise NOT this value.

Returns:



853
854
855
# File 'lib/rltk/cg/value.rb', line 853

def not
	self.class.new(Bindings.const_not(@ptr))
end

#nsw_add(rhs) ⇒ ConstantInteger

Add this value with another value. Performs no signed wrap addition.

Parameters:

Returns:



613
614
615
# File 'lib/rltk/cg/value.rb', line 613

def nsw_add(rhs)
	self.class.new(Bindings.const_nsw_add(@ptr, rhs))
end

#nsw_mul(rhs) ⇒ ConstantInteger

Multiply this value with another value. Perform no signed wrap multiplication.

Parameters:

Returns:



675
676
677
# File 'lib/rltk/cg/value.rb', line 675

def nsw_mul(rhs)
	self.class.new(Bindings.const_nsw_mul(@ptr, rhs))
end

#nsw_negConstantInteger

Negate this value. Uses no signed wrap negation.

Returns:



750
751
752
# File 'lib/rltk/cg/value.rb', line 750

def nsw_neg
	self.class.new(Bindings.const_nsw_neg(@ptr))
end

#nsw_sub(rhs) ⇒ ConstantInteger

Subtract a value from this value. Performs no signed wrap subtraction.

Parameters:

Returns:



644
645
646
# File 'lib/rltk/cg/value.rb', line 644

def nsw_sub(rhs)
	self.class.new(Bindings.const_nsw_sub(@ptr, rhs))
end

#nuw_add(rhs) ⇒ ConstantInteger

Add this value with another value. Performs no unsigned wrap addition.

Parameters:

Returns:



623
624
625
# File 'lib/rltk/cg/value.rb', line 623

def nuw_add(rhs)
	self.class.new(Bindings.const_nuw_add(@ptr, rhs))
end

#nuw_mul(rhs) ⇒ ConstantInteger

Multiply this value with another value. Perform no unsigned wrap multiplication.

Parameters:

Returns:



685
686
687
# File 'lib/rltk/cg/value.rb', line 685

def nuw_mul(rhs)
	self.class.new(Bindings.const_nuw_mul(@ptr, rhs))
end

#nuw_negConstantInteger

Negate this value. Uses no unsigned wrap negation.

Returns:



757
758
759
# File 'lib/rltk/cg/value.rb', line 757

def nuw_neg
	self.class.new(Bindings.const_nuw_neg(@ptr))
end

#nuw_sub(rhs) ⇒ ConstantInteger

Subtract a value from this value. Performs no unsigned wrap subtraction.

Parameters:

Returns:



654
655
656
# File 'lib/rltk/cg/value.rb', line 654

def nuw_sub(rhs)
	self.class.new(Bindings.const_nuw_sub(@ptr, rhs))
end

#or(rhs) ⇒ ConstantInteger

Bitwise OR this value with another.

Parameters:

Returns:



837
838
839
# File 'lib/rltk/cg/value.rb', line 837

def or(rhs)
	self.class.new(Bindings.const_or(@ptr, rhs))
end

#shift(dir, bits, mode = :arithmetic) ⇒ ConstantInteger

A wrapper method around the #shift_left and #shift_right methods.

Parameters:

  • dir (:left, :right)

    The direction to shift.

  • bits (Integer)

    Number of bits to shift.

  • mode (:arithmetic, :logical) (defaults to: :arithmetic)

    Shift mode for right shifts.

Returns:



773
774
775
776
777
778
# File 'lib/rltk/cg/value.rb', line 773

def shift(dir, bits, mode = :arithmetic)
	case dir
	when :left	then shift_left(bits)
	when :right	then shift_right(bits, mode)
	end
end

#shift_left(bits) ⇒ ConstantInteger Also known as: shl, <<

Shift the value left a specific number of bits.

Parameters:

  • bits (Integer)

    Number of bits to shift.

Returns:



785
786
787
# File 'lib/rltk/cg/value.rb', line 785

def shift_left(bits)
	self.class.new(Bindings.const_shl(@ptr, bits))
end

#shift_right(bits, mode = :arithmetic) ⇒ ConstantInteger

Shift the value right a specific number of bits.

Parameters:

  • bits (Integer)

    Number of bits to shift.

  • mode (:arithmetic, :logical) (defaults to: :arithmetic)

    Shift mode.

Returns:



797
798
799
800
801
802
# File 'lib/rltk/cg/value.rb', line 797

def shift_right(bits, mode = :arithmetic)
	case mode
	when :arithmetic	then ashr(bits)
	when :logical		then lshr(bits)
	end
end

#to_f(type) ⇒ ConstantReal

Convert this integer to a float.

Parameters:

  • type (RealType)

    Type of float to convert to.

Returns:

  • (ConstantReal)

    This value as a floating point value of the given type.



888
889
890
# File 'lib/rltk/cg/value.rb', line 888

def to_f(type)
	type.value_class.new(Bindings.send(@signed ? :const_si_to_fp : :const_ui_to_fp, @ptr, check_cg_type(type, FloatingPointType)))
end

#udiv(rhs) ⇒ ConstantInteger

Divide this value by another value. Uses unsigned division.

Parameters:

Returns:



714
715
716
# File 'lib/rltk/cg/value.rb', line 714

def udiv(rhs)
	self.class.new(Bindings.const_u_div(@ptr, rhs))
end

#urem(rhs) ⇒ ConstantInteger

Modulo this value by another value. Uses unsigned modulo.

Parameters:

Returns:



734
735
736
# File 'lib/rltk/cg/value.rb', line 734

def urem(rhs)
	self.class.new(Bindings.const_u_rem(@ptr, rhs))
end

#value(extension = :sign) ⇒ Integer

Get the value of this constant as a signed or unsigned long long.

Parameters:

  • extension (:sign, :zero) (defaults to: :sign)

    Extension method.

Returns:



897
898
899
900
901
902
# File 'lib/rltk/cg/value.rb', line 897

def value(extension = :sign)
	case extension
	when :sign then Bindings.const_int_get_s_ext_value(@ptr)
	when :zero then Bindings.const_int_get_z_ext_value(@ptr)
	end
end

#xor(rhs) ⇒ ConstantInteger

Bitwise XOR this value with another.

Parameters:

Returns:



846
847
848
# File 'lib/rltk/cg/value.rb', line 846

def xor(rhs)
	self.class.new(Bindings.const_xor(@ptr, rhs))
end