Class: Udb::Csr

Inherits:
TopLevelDatabaseObject show all
Includes:
Idl::Csr, HasFields
Defined in:
lib/udb/obj/csr.rb

Overview

CSR definition

Defined Under Namespace

Classes: MemoizedState

Constant Summary collapse

BITS6_TYPE =

Frozen constant Type/Var objects reused across all fill_symtab calls

Idl::Type.new(:bits, width: 6).freeze
BITS6_CONST_TYPE =
Idl::Type.new(:bits, width: 6, qualifiers: [:const]).freeze
BITS128_TYPE =
Idl::Type.new(:bits, width: 128).freeze
ENCODING_SIZE_VAR =
Idl::Var.new("__instruction_encoding_size", BITS6_TYPE, 32).freeze
MODE_TO_XLEN_FIELD =

Privilege mode => IDL expression naming the field that selects the effective XLEN in that mode. Debug mode executes at MXLEN, so it shares M-mode's field.

{
  "M" => "CSR[misa].MXL",
  "D" => "CSR[misa].MXL",
  "S" => "CSR[mstatus].SXL",
  "U" => "CSR[mstatus].UXL",
  "VS" => "CSR[hstatus].VSXL",
  "VU" => "CSR[vsstatus].UXL"
}.freeze
XLEN_TO_ENCODING =
{ 32 => 0, 64 => 1 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, data_path, arch)

Parameters:



44
45
46
47
48
# File 'lib/udb/obj/csr.rb', line 44

def initialize(data, data_path, arch)
  super(data, data_path, arch)

  @memo = MemoizedState.new(reachable_functions: {})
end

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


51
52
53
# File 'lib/udb/obj/csr.rb', line 51

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/udb/obj/csr.rb', line 53

def ==(other)
  if other.is_a?(Csr)
    name == other.name
  else
    raise ArgumentError, "Csr is not comparable to #{other.class.name}"
  end
end

#addressInteger?

Returns:

  • (Integer)

    CSR address (the value passed as an immediate to csrrw, etc.)

  • (nil)

    if the CSR is indirect-accesss-only



63
64
65
# File 'lib/udb/obj/csr.rb', line 63

def address
  @data["address"]
end

#affected_by?(ext_ver) ⇒ Boolean Originally defined in module HasFields

Returns Whether or not the presence of ext_ver affects this register definition.

Returns:

  • (Boolean)

    Whether or not the presence of ext_ver affects this register definition

#basenil, Integer

Returns:

  • (nil)

    if this CSR is defined in all bases

  • (Integer, nil)

    32 or 64, the XLEN this CSR is exclusively defined in



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/udb/obj/csr.rb', line 108

def base
  return @base if defined?(@base)

  @base =
    if defined_by_condition.rv32_only?
      32
    elsif defined_by_condition.rv64_only?
      64
    else
      nil
    end
end

#bitfield_type(cfg_arch, effective_xlen = nil) ⇒ Idl::BitfieldType

Returns A bitfield type that can represent all fields of the CSR.

Parameters:

  • effective_xlen (Integer or nil) (defaults to: nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (Idl::BitfieldType)

    A bitfield type that can represent all fields of the CSR



462
463
464
465
466
467
468
469
470
# File 'lib/udb/obj/csr.rb', line 462

def bitfield_type(cfg_arch, effective_xlen = nil)
  @bitfield_type_cache ||= {}
  @bitfield_type_cache[effective_xlen] ||= Idl::BitfieldType.new(
    "Csr#{name.capitalize}Bitfield",
    length(effective_xlen),
    fields_for(effective_xlen).map(&:name),
    fields_for(effective_xlen).map { |f| f.location(effective_xlen) }
  )
end

#defined_in_all_bases?Boolean

Returns true if this CSR is defined regardless of the effective XLEN.

Returns:

  • (Boolean)

    true if this CSR is defined regardless of the effective XLEN



128
# File 'lib/udb/obj/csr.rb', line 128

def defined_in_all_bases? = base.nil?

#defined_in_base32?Boolean

Returns true if this CSR is defined when XLEN is 32.

Returns:

  • (Boolean)

    true if this CSR is defined when XLEN is 32



122
# File 'lib/udb/obj/csr.rb', line 122

def defined_in_base32? = base != 64

#defined_in_base64?Boolean

Returns true if this CSR is defined when XLEN is 64.

Returns:

  • (Boolean)

    true if this CSR is defined when XLEN is 64



125
# File 'lib/udb/obj/csr.rb', line 125

def defined_in_base64? = base != 32

#defined_in_base?(xlen) ⇒ Boolean

Returns true if this CSR is defined when XLEN is xlen.

Parameters:

  • xlen (32, 64)

    base

Returns:

  • (Boolean)

    true if this CSR is defined when XLEN is xlen



132
# File 'lib/udb/obj/csr.rb', line 132

def defined_in_base?(xlen) = xlen == 32 ? defined_in_base32? : defined_in_base64?

#defining_extension_requirementsArray<ExtensionRequirement>

return list of extension requirements that must be implemented for this Csr to be defined

will not include any extension requirements that are conditionally required e.g., definedBy = Zblah if XLEN == 32; defining_extension_requirements will not include Zblah

Returns:



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/udb/obj/csr.rb', line 431

def defining_extension_requirements
  @defining_extension_requirements ||=
    begin
      pb =
        Udb.create_progressbar(
          "Determining defining extensions for CSR #{name} [:bar] :current/:total",
          total: @cfg_arch.csrs.size,
          clear: true
        )
      @cfg_arch.extensions.map do |ext|
        pb.advance
        vers = ext.versions.select do |ext_ver|
          if defined_by_condition.mentions?(ext_ver)
            (-defined_by_condition & ext_ver.to_condition).unsatisfiable?
          end
        end
        unless vers.empty?
          ExtensionRequirement.create_from_ext_vers(vers)
        end
      end.compact
    end
end

#description_htmlString

parse description field with asciidoctor, and return the HTML result

Returns:

  • (String)

    Parsed description in HTML



422
423
424
# File 'lib/udb/obj/csr.rb', line 422

def description_html
  Asciidoctor.convert description
end

#dynamic_length?Boolean

Returns Whether or not the length of the CSR depends on a runtime value (e.g., mstatus.SXL).

Returns:

  • (Boolean)

    Whether or not the length of the CSR depends on a runtime value (e.g., mstatus.SXL)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/udb/obj/csr.rb', line 189

def dynamic_length?
  return false if @data["length"].is_a?(Integer)

  # when a CSR is only defined in one base, its length can't change
  return false unless base.nil?

  case @data["length"]
  when "MXLEN"
    # mxlen can never change at runtime, so if we have it in the config, the length is not dynamic
    # if we don't have it in the config, we don't know what the length is
    cfg_arch.mxlen.nil?
  when "SXLEN"
    # dynamic if either we don't know SXLEN or SXLEN is explicitly mutable
    cfg_arch.param_values["SXLEN"].nil? || cfg_arch.param_values["SXLEN"].size > 1
  when "VSXLEN"
    # dynamic if either we don't know VSXLEN or VSXLEN is explicitly mutable
    !cfg_arch.param_values.key?("VSXLEN") || cfg_arch.param_values["VSXLEN"].size > 1
  when "XLEN"
    modes_with_dynamic_xlen.any?
  else
    raise "Unexpected length"
  end
end

#exists_in_cfg?(cfg_arch) ⇒ Boolean

Returns whether or not the CSR is possibly implemented given the supplied config options.

Parameters:

Returns:

  • (Boolean)

    whether or not the CSR is possibly implemented given the supplied config options



609
610
611
# File 'lib/udb/obj/csr.rb', line 609

def exists_in_cfg?(cfg_arch)
  @exists_in_cfg ||= defined_by_condition.could_be_satisfied_by_cfg_arch?(cfg_arch)
end

#field(field_name) ⇒ CsrField? Originally defined in module HasFields

Returns field named 'field_name' if it exists, and nil otherwise.

Returns:

  • (CsrField, nil)

    field named 'field_name' if it exists, and nil otherwise

#field?(field_name) ⇒ Boolean Originally defined in module HasFields

Returns true if a field named 'field_name' is defined.

Returns:

  • (Boolean)

    true if a field named 'field_name' is defined

#field_hashHash<String,CsrField> Originally defined in module HasFields

Returns Hash of fields, indexed by field name.

Returns:

  • (Hash<String,CsrField>)

    Hash of fields, indexed by field name

#fieldsArray<CsrField>

Sorbet override to satisfy Idl::Csr abstract method. HasFields#fields returns CsrField instances but Sorbet can't see through the untyped module, so we cast explicitly.

Returns:



458
# File 'lib/udb/obj/csr.rb', line 458

def fields = T.unsafe(super)

#fields_for(effective_xlen) ⇒ Array<CsrField> Originally defined in module HasFields

Returns All known fields when XLEN == effective_xlen.

Parameters:

  • effective_xlen (Integer or nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (Array<CsrField>)

    All known fields when XLEN == effective_xlen

#fill_symtab(ast, effective_xlen) ⇒ IdL::SymbolTable

Returns A symbol table populated with globals and syms specific to this CSR.

Parameters:

  • ast (Idl::AstNode)

    An abstract syntax tree that will be evaluated with the returned symbol table

Returns:

  • (IdL::SymbolTable)

    A symbol table populated with globals and syms specific to this CSR



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/udb/obj/csr.rb', line 535

def fill_symtab(ast, effective_xlen)
  symtab = @cfg_arch.symtab.global_clone
  symtab.push(ast)
  # all CSR instructions are 32-bit
  if effective_xlen
    @xlen_var_cache ||= {}
    @xlen_var_cache[effective_xlen] ||= Idl::Var.new("__effective_xlen", BITS6_TYPE, effective_xlen).freeze
    symtab.add("__effective_xlen", @xlen_var_cache[effective_xlen])
  end
  symtab.add("__instruction_encoding_size", ENCODING_SIZE_VAR)
  symtab.add("__expected_return_type", BITS128_TYPE)
  if symtab.get("MXLEN").value.nil?
    # Cache per-xlen MXLEN var; use :nil_xlen sentinel so nil key doesn't collide with unset
    mxlen_key = effective_xlen.nil? ? :nil_xlen : effective_xlen
    @mxlen_var_cache ||= {}
    @mxlen_var_cache[mxlen_key] ||= Idl::Var.new(
      "MXLEN",
      BITS6_CONST_TYPE,
      effective_xlen,
      param: true
    ).freeze
    symtab.add("MXLEN", @mxlen_var_cache[mxlen_key])
  end
  symtab
end

#format_changes_with_xlen?Boolean

Returns Whether or not the format of this CSR changes when the effective XLEN changes in some mode.

Returns:

  • (Boolean)

    Whether or not the format of this CSR changes when the effective XLEN changes in some mode



135
136
137
138
139
# File 'lib/udb/obj/csr.rb', line 135

def format_changes_with_xlen?
  dynamic_length? || \
    (defined_in_all_bases? && (possible_fields_for(32) != possible_fields_for(64))) || \
    possible_fields.any?(&:dynamic_location?)
end

#has_custom_sw_read?Boolean

Returns true if the CSR has a custom sw_read function.

Returns:

  • (Boolean)

    true if the CSR has a custom sw_read function



473
474
475
# File 'lib/udb/obj/csr.rb', line 473

def has_custom_sw_read?
  @data.key?("sw_read()") && !@data["sw_read()"].empty?
end

#indirect?Boolean

Returns Whether or not the CSR can be accessed by indirect address.

Returns:

  • (Boolean)

    Whether or not the CSR can be accessed by indirect address



68
69
70
# File 'lib/udb/obj/csr.rb', line 68

def indirect?
  @data.key?("indirect_address")
end

#indirect_addressInteger

Returns The indirect address.

Returns:

  • (Integer)

    The indirect address



73
74
75
# File 'lib/udb/obj/csr.rb', line 73

def indirect_address
  @data["indirect_address"]
end

#indirect_slotInteger

Returns The indirect window slot.

Returns:

  • (Integer)

    The indirect window slot



78
79
80
# File 'lib/udb/obj/csr.rb', line 78

def indirect_slot
  @data["indirect_slot"]
end

#length(effective_xlen = nil) ⇒ Integer?

Parameters:

  • effective_xlen (Integer, nil) (defaults to: nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (Integer, nil)


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
269
270
271
272
273
274
# File 'lib/udb/obj/csr.rb', line 228

def length(effective_xlen = nil)
  case @data["length"]
  when "MXLEN"
    return T.must(cfg_arch.mxlen) unless cfg_arch.mxlen.nil?

    if !base.nil?
      base
    else
      # don't know MXLEN
      effective_xlen
    end
  when "SXLEN"
    if cfg_arch.param_values.key?("SXLEN")
      if cfg_arch.param_values["SXLEN"].size > 1
        effective_xlen
      else
        cfg_arch.param_values["SXLEN"][0]
      end
    elsif !base.nil?
      # if this CSR is only available in one base, then we know its length
      base
    else
      # don't know SXLEN
      effective_xlen
    end
  when "VSXLEN"
    if cfg_arch.param_values.key?("VSXLEN")
      if cfg_arch.param_values["VSXLEN"].size > 1
        effective_xlen
      else
        cfg_arch.param_values["VSXLEN"][0]
      end
    elsif !base.nil?
      # if this CSR is only available in one base, then we know its length
      base
    else
      # don't know VSXLEN
      effective_xlen
    end
  when "XLEN"
    effective_xlen
  when Integer
    @data["length"]
  else
    raise "Unexpected length field for #{name}"
  end
end

#length_cond(xlen) ⇒ String

Returns IDL condition of when the effective xlen is xlen.

Parameters:

  • xlen (Integer)

    32 or 64

Returns:

  • (String)

    IDL condition of when the effective xlen is xlen



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/udb/obj/csr.rb', line 356

def length_cond(xlen)
  encoding = XLEN_TO_ENCODING.fetch(xlen)
  case @data["length"]
  when "MXLEN"
    "#{MODE_TO_XLEN_FIELD['M']} == #{encoding}"
  when "SXLEN"
    "#{MODE_TO_XLEN_FIELD['S']} == #{encoding}"
  when "VSXLEN"
    "#{MODE_TO_XLEN_FIELD['VS']} == #{encoding}"
  when "XLEN"
    modes = modes_with_dynamic_xlen
    raise "No mode with access to #{name} has a dynamic XLEN" if modes.empty?

    modes.map { |mode|
      "(priv_mode() == PrivilegeMode::#{mode} && #{MODE_TO_XLEN_FIELD.fetch(mode)} == #{encoding})"
    }.join(" || ")
  else
    raise "Unexpected length #{@data['length']} for #{name}"
  end
end

#length_cond32String

Returns IDL condition of when the effective xlen is 32.

Returns:

  • (String)

    IDL condition of when the effective xlen is 32



378
# File 'lib/udb/obj/csr.rb', line 378

def length_cond32 = length_cond(32)

#length_cond64String

Returns IDL condition of when the effective xlen is 64.

Returns:

  • (String)

    IDL condition of when the effective xlen is 64



381
# File 'lib/udb/obj/csr.rb', line 381

def length_cond64 = length_cond(64)

#length_pretty(effective_xlen = nil) ⇒ String

Returns Pretty-printed length string.

Parameters:

  • effective_xlen (Integer or nil) (defaults to: nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (String)

    Pretty-printed length string

Raises:

  • (ArgumentError)


385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/udb/obj/csr.rb', line 385

def length_pretty(effective_xlen = nil)
  raise ArgumentError, "effective_xlen is non-nil and is a #{effective_xlen.class} but must be an Integer" unless effective_xlen.nil? || effective_xlen.is_a?(Integer)
  if dynamic_length?
    if effective_xlen.nil?
      [
        "* #{length(32)} when #{length_cond(32)}",
        "* #{length(64)} when #{length_cond(64)}"
      ].join("\n")
    else
      "#{length(effective_xlen)}-bit"
    end
  else
    "#{length}-bit"
  end
end

#long_nameObject



87
88
89
# File 'lib/udb/obj/csr.rb', line 87

def long_name
  @data["long_name"]
end

#max_lengthInteger

sig { override.returns(Integer) } dhower: sorbet doesn't think this is an override??

Returns:

  • (Integer)

    The largest length of this CSR in any valid mode/xlen for the config



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/udb/obj/csr.rb', line 279

def max_length
  return T.must(base) unless base.nil?

  case @data["length"]
  when "MXLEN"
    cfg_arch.mxlen || 64
  when "SXLEN"
    if cfg_arch.param_values.key?("SXLEN")
      if cfg_arch.param_values["SXLEN"].size > 1
        cfg_arch.param_values["SXLEN"].max
      else
        cfg_arch.param_values.fetch("SXLEN").fetch(0)
      end
    else
      64
    end
  when "VSXLEN"
    if cfg_arch.param_values.key?("VSXLEN")
      if cfg_arch.param_values["VSXLEN"].size > 1
        cfg_arch.param_values["VSXLEN"].max
      else
        cfg_arch.param_values["VSXLEN"][0]
      end
    else
      64
    end
  when "XLEN"
    if cfg_arch.possible_extensions.map(&:name).include?("Sm")
      cfg_arch.mxlen || 64
    elsif cfg_arch.possible_extensions.map(&:name).include?("S")
      if cfg_arch.param_values.key?("SXLEN")
        if cfg_arch.param_values.fetch("SXLEN").size > 1
          cfg_arch.param_values.fetch("SXLEN").max
        else
          cfg_arch.param_values.fetch("SXLEN").fetch(0)
        end
      else
        # SXLEN can never be greater than MXLEN
        cfg_arch.mxlen || 64
      end
    elsif cfg_arch.possible_extensions.map(&:name).include?("H")
      if cfg_arch.param_values.key?("VSXLEN")
        if cfg_arch.param_values["VSXLEN"].size > 1
          cfg_arch.param_values["VSXLEN"].max
        else
          cfg_arch.param_values["VSXLEN"][0]
        end
      else
        # VSXLEN can never be greater than MXLEN or SXLEN
        if cfg_arch.param_values.key?("SXLEN")
          if cfg_arch.param_values.fetch("SXLEN").size > 1
            cfg_arch.param_values.fetch("SXLEN").max
          else
            cfg_arch.param_values.fetch("SXLEN").fetch(0)
          end
        else
          cfg_arch.mxlen || 64
        end
      end
    else
      raise "Unexpected"
    end
  when Integer
    @data["length"]
  else
    raise "Unexpected length field for #{name}"
  end
end

#min_lengthInteger

Returns Smallest length of the CSR in any mode.

Parameters:

Returns:

  • (Integer)

    Smallest length of the CSR in any mode



215
216
217
218
219
220
221
222
223
224
# File 'lib/udb/obj/csr.rb', line 215

def min_length
  case @data["length"]
  when "MXLEN", "SXLEN", "VSXLEN", "XLEN"
    @cfg_arch.possible_xlens.min
  when Integer
    @data["length"]
  else
    raise "Unexpected length"
  end
end

#modes_with_accessObject

list of modes that can potentially access the field



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/udb/obj/csr.rb', line 402

def modes_with_access
  case @data["priv_mode"]
  when "M"
    ["M"]
  when "S"
    ["M", "S", "VS"]
  when "U"
    ["M", "S", "U", "VS", "VU"]
  when "VS"
    ["M", "S", "VS"]
  when "D"
    ["M", "D"]
  else
    raise "unexpected priv mode"
  end
end

#modes_with_dynamic_xlenArray<String>

Returns Modes with access to this CSR whose effective XLEN can vary in this configuration.

Returns:

  • (Array<String>)

    Modes with access to this CSR whose effective XLEN can vary in this configuration



350
351
352
# File 'lib/udb/obj/csr.rb', line 350

def modes_with_dynamic_xlen
  modes_with_access.select { |mode| cfg_arch.multi_xlen_in_mode?(mode) }
end

#optional_in_cfg?(cfg_arch) ⇒ Boolean Originally defined in module HasFields

Returns whether or not the register is optional in the config.

Parameters:

Returns:

  • (Boolean)

    whether or not the register is optional in the config

#possible_fieldsArray<CsrField> Originally defined in module HasFields

Returns All implemented fields, excluding fields defined by unimplemented extensions.

Returns:

  • (Array<CsrField>)

    All implemented fields, excluding fields defined by unimplemented extensions

#possible_fields_for(effective_xlen) ⇒ Array<CsrField> Originally defined in module HasFields

Returns All implemented fields at the given effective XLEN.

Parameters:

  • effective_xlen (Integer or nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (Array<CsrField>)

    All implemented fields at the given effective XLEN

Raises:

  • (ArgumentError)

#priv_modeString

Returns Least-privileged mode that can access this CSR. One of ['m', 's', 'u', 'vs', 'vu'].

Returns:

  • (String)

    Least-privileged mode that can access this CSR. One of ['m', 's', 'u', 'vs', 'vu']



83
84
85
# File 'lib/udb/obj/csr.rb', line 83

def priv_mode
  @data["priv_mode"]
end

#pruned_sw_read_ast(effective_xlen) ⇒ FunctionBodyAst

Returns Pruned (but not re-type-checked) AST for sw_read().

Parameters:

  • effective_xlen (Integer or nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (FunctionBodyAst)

    Pruned (but not re-type-checked) AST for sw_read()

Raises:

  • (ArgumentError)


563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/udb/obj/csr.rb', line 563

def pruned_sw_read_ast(effective_xlen)
  raise ArgumentError, "effective_xlen is non-nil and is a #{effective_xlen.class} but must be an Integer" unless effective_xlen.nil? || effective_xlen.is_a?(Integer)
  @pruned_sw_read_ast ||= {}
  return @pruned_sw_read_ast[effective_xlen] unless @pruned_sw_read_ast[effective_xlen].nil?

  ast = type_checked_sw_read_ast(effective_xlen)

  symtab = fill_symtab(ast, effective_xlen)

  ast = ast.prune(symtab)
  ast.freeze_tree(@cfg_arch.symtab)

  symtab.pop
  symtab.release

  @pruned_sw_read_ast[effective_xlen] = ast
end

#reachable_functions(effective_xlen = nil, cache = T.let({}, T::Hash[Integer, Idl::AstNode::ReachableFunctionCacheType])) ⇒ Array<Idl::FunctionDefAst>

Returns List of functions reachable from this CSR's sw_read or a field's sw_write function.

Parameters:

  • effective_xlen (Integer, nil) (defaults to: nil)

    32 or 64 for fixed xlen, nil for dynamic

  • cache (Hash{Integer => Idl::AstNode::ReachableFunctionCacheType}) (defaults to: T.let({}, T::Hash[Integer, Idl::AstNode::ReachableFunctionCacheType]))

Returns:

  • (Array<Idl::FunctionDefAst>)

    List of functions reachable from this CSR's sw_read or a field's sw_write function



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/udb/obj/csr.rb', line 150

def reachable_functions(effective_xlen = nil, cache = T.let({}, T::Hash[Integer, Idl::AstNode::ReachableFunctionCacheType]))
  cache_key = effective_xlen
  return @memo.reachable_functions[cache_key] unless @memo.reachable_functions[cache_key].nil?

  fns = []

  if has_custom_sw_read?
    xlens =
      if cfg_arch.multi_xlen?
        defined_in_all_bases? ? [32, 64] : [base]
      else
        [cfg_arch.possible_xlens[0]]
      end
    xlens.each do |xlen|
      ast = pruned_sw_read_ast(xlen)
      symtab = cfg_arch.symtab.deep_clone
      symtab.push(ast)
      fns.concat(ast.reachable_functions(symtab, cache.fetch(xlen)))
    end
  end

  if cfg_arch.multi_xlen?
    possible_fields_for(32).each do |field|
      fns.concat(field.reachable_functions(32, cache.fetch(32)))
    end
    possible_fields_for(64).each do |field|
      fns.concat(field.reachable_functions(64, cache.fetch(64)))
    end
  else
    possible_fields_for(cfg_arch.mxlen).each do |field|
      fns.concat(field.reachable_functions(cfg_arch.mxlen, cache.fetch(T.must(cfg_arch.mxlen))))
    end
  end

  @memo.reachable_functions[cache_key] = fns.uniq
end

#sw_read_ast(symtab) ⇒ FunctionBodyAst

Returns The abstract syntax tree of the sw_read() function.

Parameters:

Returns:

  • (FunctionBodyAst)

    The abstract syntax tree of the sw_read() function

Raises:

  • (ArgumentError)


511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/udb/obj/csr.rb', line 511

def sw_read_ast(symtab)
  raise ArgumentError, "Argument should be a symtab" unless symtab.is_a?(Idl::SymbolTable)

  return @sw_read_ast if instance_variable_defined?(:@sw_read_ast)
  return (@sw_read_ast = nil) if @data["sw_read()"].nil?

  # now, parse the function
  @sw_read_ast = @cfg_arch.idl_compiler.compile_func_body(
    @data["sw_read()"],
    return_type: Idl::Type.new(:bits, width: 128), # big int to hold special return values
    name: "CSR[#{name}].sw_read()",
    input_file: __source,
    input_line: source_line(["sw_read()"]),
    symtab:,
    type_check: false
  )

  raise "unexpected #{@sw_read_ast.class}" unless @sw_read_ast.is_a?(Idl::FunctionBodyAst)

  @sw_read_ast
end

#type_checked_pruned_sw_read_ast(effective_xlen) ⇒ FunctionBodyAst

Returns Pruned and re-type-checked AST for sw_read() Use this when you need the AST to be fully type-checked (e.g., for the type_check pass). For reachable_functions, use pruned_sw_read_ast directly.

Parameters:

  • effective_xlen (Integer or nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (FunctionBodyAst)

    Pruned and re-type-checked AST for sw_read() Use this when you need the AST to be fully type-checked (e.g., for the type_check pass). For reachable_functions, use pruned_sw_read_ast directly.

Raises:

  • (ArgumentError)


585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/udb/obj/csr.rb', line 585

def type_checked_pruned_sw_read_ast(effective_xlen)
  raise ArgumentError, "effective_xlen is non-nil and is a #{effective_xlen.class} but must be an Integer" unless effective_xlen.nil? || effective_xlen.is_a?(Integer)
  @type_checked_pruned_sw_read_ast ||= {}
  return @type_checked_pruned_sw_read_ast[effective_xlen] if @type_checked_pruned_sw_read_ast.key?(effective_xlen)

  ast = pruned_sw_read_ast(effective_xlen)

  symtab = fill_symtab(type_checked_sw_read_ast(effective_xlen), effective_xlen)

  @cfg_arch.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{name}].sw_read()"
  )

  symtab.pop
  symtab.release

  @type_checked_pruned_sw_read_ast[effective_xlen] = ast
end

#type_checked_sw_read_ast(effective_xlen) ⇒ Idl::FunctionBodyAst

Parameters:

  • effective_xlen (Integer, nil)

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (Idl::FunctionBodyAst)


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/udb/obj/csr.rb', line 479

def type_checked_sw_read_ast(effective_xlen)
  @type_checked_sw_read_asts ||= {}
  ast = @type_checked_sw_read_asts[effective_xlen.nil? ? :none : effective_xlen]
  return ast unless ast.nil?

  symtab = cfg_arch.symtab.global_clone
  symtab.push(ast)
  # all CSR instructions are 32-bit
  unless effective_xlen.nil?
    @xlen_var_cache ||= {}
    @xlen_var_cache[effective_xlen] ||= Idl::Var.new("__effective_xlen", BITS6_TYPE, effective_xlen).freeze
    symtab.add("__effective_xlen", @xlen_var_cache[effective_xlen])
  end
  symtab.add("__instruction_encoding_size", ENCODING_SIZE_VAR)
  symtab.add(
    "__expected_return_type",
    BITS128_TYPE
   )

  ast = sw_read_ast(symtab)
  @cfg_arch.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{name}].sw_read()"
  )
  symtab.pop
  symtab.release
  @type_checked_sw_read_asts[effective_xlen.nil? ? :none : effective_xlen] = ast
end

#valueInteger? Also known as: reset_value

Returns:

  • (Integer, nil)


98
99
100
101
102
# File 'lib/udb/obj/csr.rb', line 98

def value
  return nil unless fields.all? { |f| f.type == "RO" }

  fields.reduce(0) { |val, f| val | (T.cast(f.reset_value, Integer) << f.location.begin) }
end

#virtual_addressInteger?

Returns:

  • (Integer)

    CSR address in VS/VU mode, if different from other modes

  • (nil)

    If the CSR is not accessible in VS/VU mode, or if it's address does not change in those modes



93
94
95
# File 'lib/udb/obj/csr.rb', line 93

def virtual_address
  @data["virtual_address"]
end

#wavedrom_desc(cfg_arch, effective_xlen, exclude_unimplemented: false, optional_type: 2) ⇒ Hash Originally defined in module HasFields

Returns A representation of the WaveDrom drawing for the register.

Parameters:

  • cfg_arch (ConfiguredArchitecture)

    Architecture definition

  • effective_xlen (Integer, nil)

    Effective XLEN to use

  • exclude_unimplemented (Boolean) (defaults to: false)

    If true, do not include unimplemented fields

  • optional_type (Integer) (defaults to: 2)

    Wavedrom type (fill color) for optional fields

Returns:

  • (Hash)

    A representation of the WaveDrom drawing for the register

Raises:

  • (ArgumentError)

#writableBoolean Originally defined in module HasFields

Returns Whether or not the register can be written by software.

Returns:

  • (Boolean)

    Whether or not the register can be written by software