Class: Rubycc::Type::IntegerType

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycc/type.rb

Overview

A standard integer type, identified by its C spelling (name), its width in bytes (size, one of 1/2/4/8) and its signedness (signed). A single shared instance stands in for each type (Type::Int, Type::ULong, ...), so identity comparison doubles as value comparison; two variables both int name the very same object. bool singles out _Bool, whose values are constrained to 0 and 1.

Value representation (see Backend::X86_64): an integer value narrower than 8 bytes lives in its virtual-register slot extended to (at least) 32 bits following this type's signedness — sign-extended when #signed?, zero-extended when #unsigned? — with the slot's bits 32..63 left indeterminate. An 8-byte value (long/unsigned long) uses the whole slot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, size, signed, bool: false) ⇒ IntegerType

Returns a new instance of IntegerType.



52
53
54
55
56
57
# File 'lib/rubycc/type.rb', line 52

def initialize(name, size, signed, bool: false)
  @name = name
  @size = size
  @signed = signed
  @bool = bool
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



50
51
52
# File 'lib/rubycc/type.rb', line 50

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

Each integer type is a shared singleton, so equality is normally identity. The one exception: a completed enum is an int object (6.7.2.2), so the plain int singleton is equal to any EnumType completed to int — this makes the equality symmetric with EnumType#==, so a function-type compatibility check matches whichever side holds the int. #eql?/#hash are left as identity, so this does not disturb the singletons' use as hash keys.



137
138
139
140
141
# File 'lib/rubycc/type.rb', line 137

def ==(other)
  return true if equal?(other)

  int? && other.is_a?(EnumType) && other.complete?
end

#alignmentObject

An integer type is aligned to its own width: char/_Bool 1, short 2, int 4, long 8.



126
127
128
# File 'lib/rubycc/type.rb', line 126

def alignment
  @size
end

#arithmetic?Boolean

Every integer type is an arithmetic type; #float? tells it apart from a floating one (see Type::FloatType), which is likewise #arithmetic? but not #integer?.

Returns:

  • (Boolean)


88
89
90
# File 'lib/rubycc/type.rb', line 88

def arithmetic?
  true
end

#array?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rubycc/type.rb', line 108

def array?
  false
end

#bool?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rubycc/type.rb', line 104

def bool?
  @bool
end

#char?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubycc/type.rb', line 73

def char?
  @name == "char"
end

#float?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rubycc/type.rb', line 92

def float?
  false
end

#function?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/rubycc/type.rb', line 116

def function?
  false
end

#int?Boolean

#int? and #char? name the plain int and char types alone, so an unsigned int is not #int? and neither signed char nor unsigned char is #char?; code that means "any integer" asks #integer? instead, and code that means "any character type" asks Type.character?. Plain char is #char? under either signedness, since both instances spell themselves the same way (see Type.plain_char).

Returns:

  • (Boolean)


69
70
71
# File 'lib/rubycc/type.rb', line 69

def int?
  @name == "int"
end

#integer?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rubycc/type.rb', line 81

def integer?
  true
end

#pointer?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rubycc/type.rb', line 59

def pointer?
  false
end

#signed?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/rubycc/type.rb', line 96

def signed?
  @signed
end

#sizeObject



120
121
122
# File 'lib/rubycc/type.rb', line 120

def size
  @size
end

#struct?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/rubycc/type.rb', line 112

def struct?
  false
end

#to_sObject



143
144
145
# File 'lib/rubycc/type.rb', line 143

def to_s
  @name
end

#unsigned?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rubycc/type.rb', line 100

def unsigned?
  !@signed
end

#void?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubycc/type.rb', line 77

def void?
  false
end