Class: Idl::Type

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

Overview

Data types

Constant Summary collapse

KINDS =
[
  :void,     # empty
  :boolean,  # true or false, not compatible with bits/int/xreg
  :bits,     # integer with compile-time-known bit width
  :enum,     # enumeration class
  :enum_ref, # reference to an enumeration element, convertable to int and/or Bits<bit_width(MAX_ENUM_VALUE)>
  :bitfield, # bitfield, convertable to int and/or Bits<width>
  :struct,   # structure class
  :array,    # array of other types
  :tuple,    # tuple of other disimilar types
  :function, # function
  :template_function, # template function, where the template arguments are known but template values need to be applied to become a full function
  :csr,      # a CSR register type
  :dontcare, # matches everything
  :string    # fixed-length character string
].freeze
QUALIFIERS =
[
  :const,
  :signed,
  :global
].freeze
TYPE_FROM_KIND =
[:boolean, :void, :dontcare].map { |k| [k, Type.new(k)] }.to_h.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, qualifiers: [], width: nil, sub_type: nil, name: nil, tuple_types: nil, return_type: nil, arguments: nil, enum_class: nil, csr: nil) ⇒ Type

Returns a new instance of Type.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/idl/type.rb', line 77

def initialize(kind, qualifiers: [], width: nil, sub_type: nil, name: nil, tuple_types: nil, return_type: nil, arguments: nil, enum_class: nil, csr: nil)
  raise "Invalid kind '#{kind}'" unless KINDS.include?(kind)

  @kind = kind
  raise "Invalid qualifier" unless qualifiers.intersection(QUALIFIERS) == qualifiers

  @qualifiers = qualifiers
  # raise "#{width.class.name}" if (kind == :bits && !width.is_a?(Integer))

  raise "Should be a FunctionType" if kind == :function && !self.is_a?(FunctionType)

  raise "Width must be an Integer, is a #{width.class}" unless width.nil? || width.is_a?(Integer) || width == :unknown
  @width = width
  @sub_type = sub_type
  raise "Tuples need a type list" if kind == :tuple && tuple_types.nil?
  @tuple_types = tuple_types
  @return_type = return_type
  @arguments = arguments
  @enum_class = enum_class
  @name = name
  if kind == :bits
    raise "Bits type must have width" unless @width
    raise "Bits type must have positive width" unless @width == :unknown || @width.positive?
  end
  if kind == :enum
    raise "Enum type must have width" unless @width
  end
  if kind == :array
    raise "Array must have a subtype" unless @sub_type
  end
  if kind == :csr
    raise 'CSR type must have a csr argument' if csr.nil?

    @csr = csr
    raise "CSR types must have a width" if width.nil?

    @width = width
  end
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



56
57
58
# File 'lib/idl/type.rb', line 56

def arguments
  @arguments
end

#enum_classObject (readonly)

Returns the value of attribute enum_class.



56
57
58
# File 'lib/idl/type.rb', line 56

def enum_class
  @enum_class
end

#kindObject (readonly)

Returns the value of attribute kind.



56
57
58
# File 'lib/idl/type.rb', line 56

def kind
  @kind
end

#qualifiersObject (readonly)

Returns the value of attribute qualifiers.



56
57
58
# File 'lib/idl/type.rb', line 56

def qualifiers
  @qualifiers
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



56
57
58
# File 'lib/idl/type.rb', line 56

def return_type
  @return_type
end

#sub_typeObject (readonly)

Returns the value of attribute sub_type.



56
57
58
# File 'lib/idl/type.rb', line 56

def sub_type
  @sub_type
end

#tuple_typesObject (readonly)

Returns the value of attribute tuple_types.



56
57
58
# File 'lib/idl/type.rb', line 56

def tuple_types
  @tuple_types
end

#widthObject (readonly)

Returns the value of attribute width.



56
57
58
# File 'lib/idl/type.rb', line 56

def width
  @width
end

Class Method Details

.from_json_schema(schema) ⇒ Object



476
477
478
479
480
481
482
483
484
# File 'lib/idl/type.rb', line 476

def self.from_json_schema(schema)
  hsh = schema.to_h
  case hsh["type"]
  when "boolean", "integer", "string"
    from_json_schema_scalar_type(hsh)
  when "array"
    from_json_schema_array_type(hsh)
  end
end

.from_typename(type_name, arch_def) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/idl/type.rb', line 64

def self.from_typename(type_name, arch_def)
  case type_name
  when 'XReg'
    return Type.new(:bits, width: arch_def.param_values['XLEN'])
  when 'FReg'
    return Type.new(:freg, width: 32)
  when 'DReg'
    return Type.new(:dreg, width: 64)
  when /Bits<((?:0x)?[0-9a-fA-F]+)>/
    Type.new(:bits, width: $1.to_i)
  end
end

Instance Method Details

#ary?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/idl/type.rb', line 358

def ary?
  @kind == :array
end

#ary_type(ary) ⇒ Object

given an N-dimensional array type, return the primitive type



195
196
197
198
199
200
201
# File 'lib/idl/type.rb', line 195

def ary_type(ary)
  if ary.sub_type == :array
    ary_type(ary.sub_type)
  else
    ary.sub_type
  end
end

#cloneObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/idl/type.rb', line 118

def clone
  Type.new(
    @kind,
    qualifiers: @qualifiers&.map(&:clone),
    width: @width,
    sub_type: @sub_type&.clone,
    name: @name.dup,
    tuple_types: @tuple_types&.map(&:clone),
    return_type: @return_type&.clone,
    arguments: @arguments&.map(&:clone),
    enum_class: @enum_class&.clone,
    csr: @csr
  )
end

#comparable_to?(type) ⇒ Boolean

returns true if ‘type’ can be compared (e.g., >=, <, etc) to self ‘type’ can be a Type object or a kind (as a Symbol)

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/idl/type.rb', line 135

def comparable_to?(type)
  if type.is_a?(Symbol)
    raise "#{type} is not a kind" unless KINDS.include?(type)

    type = Type.new(type)
  end

  case @kind
  when :boolean
    return type.kind == :boolean
  when :enum_ref
    return \
      (type.kind == :enum_ref && type.enum_class.name == @enum_class.name) \
      || (type.kind == :enum && type.name == @enum_class.name)
  when :bits
    return type.convertable_to?(self)
  when :enum
    return type.convertable_to?(:bits)
  when :function
    # functions are not comparable to anything
    return false
  when :csr
    return ((type.kind == :csr) && (type.csr.name == @csr.name)) ||
          type.convertable_to?(Type.new(:bits, width: type.csr.width))
  when :string
    return type.kind == :string
  else
    raise "unimplemented #{@kind}"
  end
end

#const?Boolean

Returns:

  • (Boolean)


362
363
364
# File 'lib/idl/type.rb', line 362

def const?
  @qualifiers.include?(:const)
end

#convertable_to?(type) ⇒ Boolean

returns true if self can be converted to ‘type’ ‘type’ can be a Type object or a kind (as a Symbol)

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/idl/type.rb', line 205

def convertable_to?(type)
  if type.is_a?(Symbol)
    raise "#{type} is not a kind" unless KINDS.include?(type)

    type = TYPE_FROM_KIND[type]
  end

  case @kind
  when :boolean
    return type.kind == :boolean
  when :enum_ref
    return \
      (type.kind == :enum && type.name == @enum_class.name) || \
      (type.kind == :enum_ref && type.enum_class.name == @enum_class.name)
  when :dontcare
    return true
  when :bits
    return type.kind != :boolean
  when :function
    return @return_type.convertable_to?(type)
  when :enum
    if type.kind == :bits
      return false
      # return (type.width == :unknown) || (width <= type.width)
    elsif type.kind == :enum
      return type.enum_class == enum_class
    else
      return false
    end
  when :tuple
    is_tuple_of_same_size = (type.kind == :tuple) && (@tuple_types.size == type.tuple_types.size)
    if is_tuple_of_same_size
      @tuple_types.each_index do |i|
        unless @tuple_types[i].convertable_to?(type.tuple_types[i])
          return false
        end
      end
      return true
    else
      return false
    end
  when :csr
    return (type.kind == :csr && type.csr.name == @csr.name) || type.convertable_to?(Type.new(:bits, width:))
  when :bitfield
    if (type.kind == :bitfield && name == type.name)
      return true
    elsif (type.kind == :bits && type.width == @width)
      return true
    else
      # be strict with bitfields -- only accept integrals that are exact width Bit types
      return false
    end
  when :array
    return type.kind == :array && type.sub_type.convertable_to?(sub_type) && type.width == @width
  when :string
    return type.kind == :string
  when :void
    return false
  when :struct
    return type.kind == :struct && (type.type_name == type_name)
  else
    raise "unimplemented type '#{@kind}'"
  end
end

#defaultObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/idl/type.rb', line 33

def default
  case @kind
  when :bits, :bitfield
    0
  when :boolean
    false
  when :array
    if @width == :unknown
      Array.new
    else
      Array.new(@width, sub_type.default)
    end
  when :string
    ""
  when :enum_ref
    @enum_class.element_values.min
  when :enum
    raise "?"
  else
    raise "No default for #{@kind}"
  end
end

#equal_to?(type) ⇒ Boolean

returns true if identical to type, excluding qualifiers

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/idl/type.rb', line 167

def equal_to?(type)
  if type.is_a?(Symbol)
    raise "#{type} is not a kind" unless KINDS.include?(type)

    type = TYPE_FROM_KIND[type]
  end

  case @kind
  when :boolean
    type.kind == :boolean
  when :enum_ref
    type.kind == :enum_ref && type.name == @enum_class.name
  when :dontcare
    true
  when :bits
    type.kind == :bits && type.width == @width
  when :string
    type.kind == :string && type.width == @width
  when :array
    type.kind == :array && type.sub_type.equal_to?(@sub_type)
  when :struct
    type.kind == :struct && (type.type_name == type_name)
  else
    raise "unimplemented type '#{@kind}'"
  end
end

#global?Boolean

Returns:

  • (Boolean)


374
375
376
# File 'lib/idl/type.rb', line 374

def global?
  @qualifiers.include?(:global)
end

#integral?Boolean

true for any type that can generally be treated as a scalar integer

Returns:

  • (Boolean)


29
30
31
# File 'lib/idl/type.rb', line 29

def integral?
  @kind == :bits
end

#make_constObject



383
384
385
386
# File 'lib/idl/type.rb', line 383

def make_const
  @qualifiers.append(:const).uniq!
  self
end

#make_globalObject



388
389
390
391
# File 'lib/idl/type.rb', line 388

def make_global
  @qualifiers.append(:global).uniq!
  self
end

#make_signedObject



378
379
380
381
# File 'lib/idl/type.rb', line 378

def make_signed
  @qualifiers.append(:signed).uniq!
  self
end

#mutable?Boolean

Returns:

  • (Boolean)


366
367
368
# File 'lib/idl/type.rb', line 366

def mutable?
  !const?
end

#nameObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/idl/type.rb', line 340

def name
  if @kind == :bits
    "Bits<#{@width}>"
  elsif @kind == :enum
    @name
  elsif @kind == :bitfield
    @name
  elsif @kind == :function || @kind == :template_function
    @name
  elsif @kind == :csr
    @csr.name
  elsif @kind == :enum_ref
    @enum_class.name
  else
    raise @kind.to_s
  end
end

#qualify(qualifier) ⇒ Object



58
59
60
61
62
# File 'lib/idl/type.rb', line 58

def qualify(qualifier)
  @qualifiers << qualifier
  @qualifiers.uniq!
  self
end

#signed?Boolean

Returns:

  • (Boolean)


370
371
372
# File 'lib/idl/type.rb', line 370

def signed?
  @qualifiers.include?(:signed)
end

#to_cxxObject



335
336
337
338
# File 'lib/idl/type.rb', line 335

def to_cxx
  ((@qualifiers.nil? || @qualifiers.empty?) ? '' : "#{@qualifiers.include?(:const) ? 'const' : ''} ") + \
  to_cxx_no_qualifiers
end

#to_cxx_no_qualifiersObject



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/idl/type.rb', line 302

def to_cxx_no_qualifiers
    if @kind == :bits
      raise "@width is unknown" if @width == :unknown
      raise "@width is a #{@width.class}" unless @width.is_a?(Integer)

      if signed?
        "SignedBits<#{@width.is_a?(Integer) ? @width : @width.to_cxx}>"
      else
        "Bits<#{@width.is_a?(Integer) ? @width : @width.to_cxx}>"
      end
    elsif @kind == :enum
      "#{@name}"
    elsif @kind == :boolean
      "bool"
    elsif @kind == :function
      "std::function<#{@return_type.to_cxx}(...)>"
    elsif @kind == :enum_ref
      "#{@enum_class.name}"
    elsif @kind == :tuple
      "std::tuple<#{@tuple_types.map{ |t| t.to_cxx }.join(',')}>"
    elsif @kind == :bitfield
      "#{@name}"
    elsif @kind == :array
      "#{@sub_type}[]"
    elsif @kind == :csr
      "#{@csr.downcase.capitalize}Csr"
    elsif @kind == :string
      "std::string"
    else
      raise @kind.to_s
    end
end

#to_sObject Also known as: fully_qualified_name



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/idl/type.rb', line 270

def to_s
  ((@qualifiers.nil? || @qualifiers.empty?) ? '' : "#{@qualifiers.map(&:to_s).join(' ')} ") + \
    if @kind == :bits
      "Bits<#{@width}>"
    elsif @kind == :enum
      "enum definition #{@name}"
    elsif @kind == :boolean
      "Boolean"
    elsif @kind == :function
      @return_type.to_s
    elsif @kind == :enum_ref
      "enum #{@enum_class.name}"
    elsif @kind == :tuple
      "(#{@tuple_types.map{ |t| t.to_s }.join(',')})"
    elsif @kind == :bitfield
      "bitfield #{@name}"
    elsif @kind == :array
      "array of #{@sub_type}"
    elsif @kind == :csr
      "CSR[#{@csr.name}]"
    elsif @kind == :void
      "void"
    elsif @kind == :string
      "string"
    elsif @kind == :struct
      "struct #{type_name}"
    else
      raise @kind.to_s
    end
end