Class: RLTK::CG::ConstantInteger Abstract

Inherits:
ConstantNumber show all
Includes:
Filigree::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 inherited from ConstantNumber

type, #type

Methods inherited from Constant

#addr_space_cast, #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:

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: nil)

    Optional length of string to use.



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/rltk/cg/value.rb', line 574

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:

  • If the integer is signed or not.



554
555
556
# File 'lib/rltk/cg/value.rb', line 554

def signed
  @signed
end

Instance Method Details

#%(rhs) ⇒ ConstantInteger

Modulo this value by another value. Uses signed modulo.

Parameters:

Returns:

  • Instance of the same class.



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

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

#*(rhs) ⇒ ConstantInteger

Multiply this value with another value.

Parameters:

Returns:

  • Instance of the same class.



673
674
675
# File 'lib/rltk/cg/value.rb', line 673

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

#+(rhs) ⇒ ConstantInteger

Add this value with another value.

Parameters:

Returns:

  • Instance of the same class.



611
612
613
# File 'lib/rltk/cg/value.rb', line 611

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

#-(rhs) ⇒ ConstantInteger

Subtract a value from this value.

Parameters:

Returns:

  • Instance of the same class.



642
643
644
# File 'lib/rltk/cg/value.rb', line 642

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

#-@ConstantInteger

Negate this value.

Returns:

  • Instance of the same class



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

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

#/(rhs) ⇒ ConstantInteger

Divide this value by another value. Uses signed division.

Parameters:

Returns:

  • Instance of the same class.



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

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

#and(rhs) ⇒ ConstantInteger

Bitwise AND this value with another.

Parameters:

Returns:

  • Instance of the same class.



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

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

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

Arithmetic right shift.

Parameters:

  • Number of bits to shift.

Returns:

  • Instance of the same class.



817
818
819
# File 'lib/rltk/cg/value.rb', line 817

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:

  • Desired type to cast to.

  • (defaults to: true)

    Is the value signed or not.

Returns:

  • This value as as the given type.



875
876
877
# File 'lib/rltk/cg/value.rb', line 875

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.

See Also:

Parameters:

  • An integer predicate.

  • Value to compare to.

Returns:

  • Value used to represent a Boolean value.



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

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:

  • Instance of the same class.



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

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

#lshr(bits) ⇒ ConstantInteger

Logical right shift.

Parameters:

  • Number of bits to shift.

Returns:

  • Instance of the same class.



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

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

#notConstantInteger

Bitwise NOT this value.

Returns:

  • Instance of the same class.



861
862
863
# File 'lib/rltk/cg/value.rb', line 861

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:

  • Instance of the same class.



621
622
623
# File 'lib/rltk/cg/value.rb', line 621

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:

  • Instance of the same class.



683
684
685
# File 'lib/rltk/cg/value.rb', line 683

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:

  • Instance of the same class



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

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:

  • Instance of the same class.



652
653
654
# File 'lib/rltk/cg/value.rb', line 652

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:

  • Instance of the same class.



631
632
633
# File 'lib/rltk/cg/value.rb', line 631

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:

  • Instance of the same class.



693
694
695
# File 'lib/rltk/cg/value.rb', line 693

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:

  • Instance of the same class



765
766
767
# File 'lib/rltk/cg/value.rb', line 765

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:

  • Instance of the same class.



662
663
664
# File 'lib/rltk/cg/value.rb', line 662

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:

  • Instance of the same class.



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

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:

  • The direction to shift.

  • Number of bits to shift.

  • (defaults to: :arithmetic)

    Shift mode for right shifts.

Returns:

  • Instance of the same class.



781
782
783
784
785
786
# File 'lib/rltk/cg/value.rb', line 781

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:

  • Number of bits to shift.

Returns:

  • Instance of the same class.



793
794
795
# File 'lib/rltk/cg/value.rb', line 793

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:

  • Number of bits to shift.

  • (defaults to: :arithmetic)

    Shift mode.

Returns:

  • Instance of the same class.



805
806
807
808
809
810
# File 'lib/rltk/cg/value.rb', line 805

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 of float to convert to.

Returns:

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



896
897
898
# File 'lib/rltk/cg/value.rb', line 896

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:

  • Instance of the same class.



722
723
724
# File 'lib/rltk/cg/value.rb', line 722

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:

  • Instance of the same class.



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

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:

  • (defaults to: :sign)

    Extension method.

Returns:



905
906
907
908
909
910
# File 'lib/rltk/cg/value.rb', line 905

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:

  • Instance of the same class.



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

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