Class: Udb::CsrField

Inherits:
DatabaseObject show all
Extended by:
T::Sig
Includes:
Idl::CsrField, CertifiableObject
Defined in:
lib/udb/obj/csr_field.rb

Overview

A CSR field object

Defined Under Namespace

Classes: Alias

Constant Summary collapse

TYPE_DESC_MAP =
{
  "RO" =>
    <<~DESC,
  "RO-H" =>
    <<~DESC,
  "RW" =>
    <<~DESC,
  "RW-R" =>
    <<~DESC,
  "RW-H" =>
    <<~DESC,
  "RW-RH" =>
    <<~DESC
      *Read-Write Restricted with Hardware update*

      Field is writable by software.
      Only certain values are legal.
      Writing an illegal value into the field is ignored, such that the field retains its prior state.
      Hardware also updates the field without an explicit software write.)
    DESC
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_csr, field_name, field_data)

Parameters:

  • parent_csr (Csr)

    The Csr that defined this field

  • field_name (String)
  • field_data (Hash{String => T.untyped})

    Field data from the arch spec



42
43
44
45
# File 'lib/udb/obj/csr_field.rb', line 42

def initialize(parent_csr, field_name, field_data)
  super(field_data, parent_csr.data_path, parent_csr.arch, DatabaseObject::Kind::CsrField, name: field_name)
  @parent = parent_csr
end

Instance Attribute Details

#parentCsr (readonly) Also known as: csr

Returns The Csr that defines this field.

Returns:

  • (Csr)

    The Csr that defines this field



26
27
28
# File 'lib/udb/obj/csr_field.rb', line 26

def parent
  @parent
end

Instance Method Details

#__sourceObject

CSR fields are defined in their parent CSR YAML file



48
# File 'lib/udb/obj/csr_field.rb', line 48

def __source = @parent.__source

#affected_by?(ext_ver) ⇒ Boolean

Returns Whether or not the presence of ext_ver affects this CSR Field definition This does not take the parent CSR into account, i.e., a field can be unaffected by ext_ver even if the parent CSR is affected.

Parameters:

Returns:

  • (Boolean)

    Whether or not the presence of ext_ver affects this CSR Field definition This does not take the parent CSR into account, i.e., a field can be unaffected by ext_ver even if the parent CSR is affected



99
100
101
# File 'lib/udb/obj/csr_field.rb', line 99

def affected_by?(ext_ver)
  @data["definedBy"].nil? ? false : defined_by_condition.possibly_satisfied_by?(ext_ver)
end

#aliasAlias?

Returns The aliased field, or nil if there is no alias.

Returns:

  • (Alias, nil)

    The aliased field, or nil if there is no alias



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
# File 'lib/udb/obj/csr_field.rb', line 270

def alias
  return @alias unless @alias.nil?

  if @data.key?("alias")
    raise "Can't parse alias" unless data["alias"] =~ /^[a-z][a-z0-9]+\.[A-Z0-9]+(\[([0-9]+)(:[0-9]+)?\])?$/

    csr_name = T.must(Regexp.last_match(1))
    csr_field = Regexp.last_match(2)
    range = Regexp.last_match(3)
    range_start = Regexp.last_match(4)
    range_end = Regexp.last_match(5)

    csr_field = T.must(cfg_arch.csr(csr_name)).field(csr_field)
    range =
      if range.nil?
        csr_field.location
      elsif range_end.nil?
        (range_start.to_i..range_start.to_i)
      else
        (range_start.to_i..range_end[1..].to_i)
      end
    @alias = Alias.new(csr_field, range)
  end
  @alias
end

#basenil, Integer

Returns:

  • (nil)

    if the CsrField exists in any XLEN

  • (Integer, nil)

    The base XLEN required for this CsrField to exist. One of [32, 64]



37
# File 'lib/udb/obj/csr_field.rb', line 37

def base = @data["base"]

#base32_only?Boolean

Returns Whether or not this field only exists when XLEN == 32.

Returns:

  • (Boolean)

    Whether or not this field only exists when XLEN == 32



681
# File 'lib/udb/obj/csr_field.rb', line 681

def base32_only? = @data.key?("base") && @data["base"] == 32

#base64_only?Boolean

Returns Whether or not this field only exists when XLEN == 64.

Returns:

  • (Boolean)

    Whether or not this field only exists when XLEN == 64



677
# File 'lib/udb/obj/csr_field.rb', line 677

def base64_only? = @data.key?("base") && @data["base"] == 64

#cert_coverage_point(id) ⇒ CertNormativeRule? Originally defined in module CertifiableObject

Parameters:

  • id (String)

    Unique ID for the normative rule

Returns:

  • (CertNormativeRule)
  • (nil)

    if there is no certification normative ruleed with ID of id

#cert_coverage_point_hashHash<String, CertNormativeRule> Originally defined in module CertifiableObject

Returns Hash with ID as key of all normative rules defined by database object.

Returns:

  • (Hash<String, CertNormativeRule>)

    Hash with ID as key of all normative rules defined by database object

#cert_normative_rulesArray<CertNormativeRule> Originally defined in module CertifiableObject

Returns:

#cert_test_procedure(id) ⇒ CertTestProcedure? Originally defined in module CertifiableObject

Parameters:

  • id (String)

    Unique ID for test procedure

Returns:

  • (CertTestProcedure)
  • (nil)

    if there is no certification test procedure with ID id

#cert_test_procedure_hashHash<String, CertTestProcedure> Originally defined in module CertifiableObject

Returns Hash of all normative rules defined by database object.

Returns:

  • (Hash<String, CertTestProcedure>)

    Hash of all normative rules defined by database object

#cert_test_proceduresArray<CertTestProcedure> Originally defined in module CertifiableObject

Returns:

#defined_in_all_bases?Boolean

Returns Whether or not this field exists for any XLEN.

Returns:

  • (Boolean)

    Whether or not this field exists for any XLEN



694
# File 'lib/udb/obj/csr_field.rb', line 694

def defined_in_all_bases? = @data["base"].nil?

#defined_in_base32?Boolean

Returns:

  • (Boolean)


684
# File 'lib/udb/obj/csr_field.rb', line 684

def defined_in_base32? = @data["base"].nil? || @data["base"] == 32

#defined_in_base64?Boolean

Returns:

  • (Boolean)


687
# File 'lib/udb/obj/csr_field.rb', line 687

def defined_in_base64? = @data["base"].nil? || @data["base"] == 64

#defined_in_base?(xlen) ⇒ Boolean

Parameters:

  • xlen (Integer)

Returns:

  • (Boolean)


690
# File 'lib/udb/obj/csr_field.rb', line 690

def defined_in_base?(xlen) = @data["base"].nil? || @data["base"] == xlen

#dynamic_location?Boolean

Returns Whether or not the location of the field changes dynamically (e.g., based on mstatus.SXL) in the configuration.

Returns:

  • (Boolean)

    Whether or not the location of the field changes dynamically (e.g., based on mstatus.SXL) in the configuration



338
339
340
341
342
343
344
# File 'lib/udb/obj/csr_field.rb', line 338

def dynamic_location?
  # if there is no location_rv32, the the field never changes
  return false unless @data["location"].nil?

  # the field changes *if* some mode with access can change XLEN
  csr.modes_with_access.any? { |mode| @cfg_arch.multi_xlen_in_mode?(mode) }
end

#dynamic_reset_value?Boolean

Returns:

  • (Boolean)


427
428
429
430
431
432
433
434
# File 'lib/udb/obj/csr_field.rb', line 427

def dynamic_reset_value?
  return false unless @data["reset_value"].nil?

  Idl::AstNode.value_try do
    reset_value
    false
  end || true
end

#exists?Boolean

Returns:

  • (Boolean)


76
# File 'lib/udb/obj/csr_field.rb', line 76

def exists? = exists_in_cfg?(cfg_arch)

#exists_in_cfg?(cfg_arch) ⇒ Boolean

For a full config, whether or not the field is implemented For a partial config, whether or the it is possible for the field to be implemented

Parameters:

Returns:

  • (Boolean)

    True if this field might exist in a config



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/udb/obj/csr_field.rb', line 61

def exists_in_cfg?(cfg_arch)
  if cfg_arch.fully_configured?
    parent.exists_in_cfg?(cfg_arch) &&
      (@data["base"].nil? || cfg_arch.possible_xlens.include?(@data["base"])) &&
      (@data["definedBy"].nil? || cfg_arch.transitive_implemented_extension_versions.any? { |ext_ver| defined_by_condition.possibly_satisfied_by?(ext_ver) })
  elsif cfg_arch.partially_configured?
    parent.exists_in_cfg?(cfg_arch) &&
      (@data["base"].nil? || cfg_arch.possible_xlens.include?(@data["base"])) &&
      (@data["definedBy"].nil? || cfg_arch.possible_extension_versions.any? { |ext_ver| defined_by_condition.possibly_satisfied_by?(ext_ver) } )
  else
    true
  end
end

#fill_symtab_for_reset(ast) ⇒ Object



579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/udb/obj/csr_field.rb', line 579

def fill_symtab_for_reset(ast)
  symtab = cfg_arch.symtab.global_clone
  symtab.push(ast)

  symtab.add("__expected_return_type", Idl::Type.new(:bits, width: max_width))

  # XLEN at reset is always mxlen
  symtab.add(
    "__effective_xlen",
    Idl::Var.new("__effective_xlen", Idl::Type.new(:bits, width: 6), cfg_arch.mxlen)
  )

  symtab
end

#fill_symtab_for_sw_write(effective_xlen, ast) ⇒ Idl::SymbolTable

Parameters:

  • effective_xlen (Integer, nil)
  • ast (Idl::AstNode)

Returns:

  • (Idl::SymbolTable)


519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/udb/obj/csr_field.rb', line 519

def fill_symtab_for_sw_write(effective_xlen, ast)
  symtab = cfg_arch.symtab.global_clone
  symtab.push(ast)

  # all CSR instructions are 32-bit
  symtab.add(
    "__instruction_encoding_size",
    Idl::Var.new("__instruction_encoding_size", Idl::Type.new(:bits, width: 6), 32)
  )
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:bits, width: 128)
  )
  symtab.add(
    "csr_value",
    Idl::Var.new("csr_value", csr.bitfield_type(@cfg_arch, effective_xlen))
  )
  if symtab.get("MXLEN").value.nil?
    symtab.add(
      "MXLEN",
      Idl::Var.new(
        "MXLEN",
        Idl::Type.new(:bits, width: 6, qualifiers: [:const]),
        effective_xlen,
        param: true
      )
    )
  end
  symtab
end

#fill_symtab_for_type(effective_xlen, ast) ⇒ Idl::SymbolTable

Parameters:

  • effective_xlen (Integer, nil)
  • ast (Idl::AstNode)

Returns:

  • (Idl::SymbolTable)


551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/udb/obj/csr_field.rb', line 551

def fill_symtab_for_type(effective_xlen, ast)
  symtab = cfg_arch.symtab.global_clone
  symtab.push(ast)

  # all CSR instructions are 32-bit
  symtab.add(
    "__instruction_encoding_size",
    Idl::Var.new("__instruction_encoding_size", Idl::Type.new(:bits, width: 6), 32)
  )
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:enum_ref, enum_class: symtab.get("CsrFieldType"))
  )
  if symtab.get("MXLEN").value.nil?
    symtab.add(
      "MXLEN",
      Idl::Var.new(
        "MXLEN",
        Idl::Type.new(:bits, width: 6, qualifiers: [:const]),
        effective_xlen,
        param: true
      )
    )
  end

  symtab
end

#has_custom_sw_write?Boolean

Returns true if the CSR field has a custom sw_write function.

Returns:

  • (Boolean)

    true if the CSR field has a custom sw_write function



451
452
453
# File 'lib/udb/obj/csr_field.rb', line 451

def has_custom_sw_write?
  @data.key?("sw_write(csr_value)") && !@data["sw_write(csr_value)"].empty?
end

#location(effective_xlen = nil) ⇒ Range<Integer>

Returns the location within the CSR as a range (single bit fields will be a range of size 1).

Parameters:

  • cfg_arch (ConfiguredArchitecture)

    A config. May be nil if the location is not configturation-dependent

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

    The effective xlen, needed since some fields change location with XLEN. If the field location is not determined by XLEN, then this parameter can be nil

Returns:

  • (Range<Integer>)

    the location within the CSR as a range (single bit fields will be a range of size 1)



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/udb/obj/csr_field.rb', line 630

def location(effective_xlen = nil)
  key =
    if @data.key?("location")
      "location"
    else
      raise ArgumentError, "The location of #{csr.name}.#{name} changes with XLEN, so effective_xlen must be provided" unless [32, 64].include?(effective_xlen)

      "location_rv#{effective_xlen}"
    end

  raise "Missing location for #{csr.name}.#{name} (#{key})?" unless @data.key?(key)

  if @data[key].is_a?(Integer)
    csr_length = csr.length(effective_xlen || @data["base"])
    if csr_length.nil?
      # we don't know the csr length for sure, so we can only check again max_length
      if @data[key] > csr.max_length
        raise "Location (#{key} = #{@data[key]}) is past the max csr length (#{csr.max_length}) in #{csr.name}.#{name}"
      end
    elsif @data[key] > csr_length
      raise "Location (#{key} = #{@data[key]}) is past the csr length (#{csr.length(effective_xlen)}) in #{csr.name}.#{name}"
    end

    @data[key]..@data[key]
  else
    raise "Unexpected location field" unless @data[key].is_a?(String)

    e, s = @data[key].split("-").map(&:to_i)
    raise "Invalid location" if s > e

    csr_length = csr.length(effective_xlen || @data["base"])
    if csr_length.nil?
      # we don't know the csr length for sure, so we can only check again max_length
      if e > csr.max_length
        raise "Location (#{key} = #{@data[key]}) is past the max csr length (#{csr.max_length}) in #{csr.name}.#{name}"
      end
    elsif e > csr_length
      raise "Location (#{key} = #{@data[key]}) is past the csr length (#{csr_length}) in #{csr.name}.#{name}"

    end

    s..e
  end
end

#location_cond32String

Returns:

  • (String)


718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/udb/obj/csr_field.rb', line 718

def location_cond32
  case csr.priv_mode
  when "M"
    "CSR[misa].MXL == 0"
  when "S"
    "CSR[mstatus].SXL == 0"
  when "VS"
    "CSR[hstatus].VSXL == 0"
  else
    raise "Unexpected priv mode #{csr.priv_mode} for #{csr.name}"
  end
end

#location_cond64String

Returns:

  • (String)


732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/udb/obj/csr_field.rb', line 732

def location_cond64
  case csr.priv_mode
  when "M"
    "CSR[misa].MXL == 1"
  when "S"
    "CSR[mstatus].SXL == 1"
  when "VS"
    "CSR[hstatus].VSXL == 1"
  else
    raise "Unexpected priv mode #{csr.priv_mode} for #{csr.name}"
  end
end

#location_pretty(effective_xlen = nil) ⇒ String

Returns Pretty-printed location string.

Parameters:

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

    32 or 64 for fixed xlen, nil for dynamic

Returns:

  • (String)

    Pretty-printed location string



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/udb/obj/csr_field.rb', line 748

def location_pretty(effective_xlen = nil)
  derangeify = proc { |loc|
    next loc.min.to_s if loc.size == 1

    "#{loc.max}:#{loc.min}"
  }

  if dynamic_location?
    condition =
      case csr.priv_mode
      when "M"
        "CSR[misa].MXL == %%"
      when "S"
        "CSR[mstatus].SXL == %%"
      when "VS"
        "CSR[hstatus].VSXL == %%"
      else
        raise "Unexpected priv mode #{csr.priv_mode} for #{csr.name}"
      end

    if effective_xlen.nil?
      <<~LOC
        * #{derangeify.call(location(32))} when #{condition.sub('%%', '0')}
        * #{derangeify.call(location(64))} when #{condition.sub('%%', '1')}
      LOC
    else
      derangeify.call(location(effective_xlen))
    end
  else
    derangeify.call(location(cfg_arch.mxlen))
  end
end

#max_widthInteger

Returns:

  • (Integer)


704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/udb/obj/csr_field.rb', line 704

def max_width
  @max_width ||=
    if base64_only?
      cfg_arch.possible_xlens.include?(64) ? width(64) : 0
    elsif base32_only?
      cfg_arch.possible_xlens.include?(32) ? width(32) : 0
    else
      @cfg_arch.possible_xlens.map do |xlen|
        width(xlen)
      end.max
    end
end

#optional_in_cfg?(cfg_arch) ⇒ Boolean

Returns For a partially configured cfg_arch, whether or not the field is optional (not mandatory or prohibited).

Parameters:

Returns:

  • (Boolean)

    For a partially configured cfg_arch, whether or not the field is optional (not mandatory or prohibited)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/udb/obj/csr_field.rb', line 80

def optional_in_cfg?(cfg_arch)
  raise "optional_in_cfg? should only be called on a partially configured cfg_arch" unless cfg_arch.partially_configured?

  exists_in_cfg?(cfg_arch) &&
    (
      if data["definedBy"].nil?
        parent.optional_in_cfg?(cfg_arch)
      else
        cfg_arch.prohibited_extension_versions.none? do |ext_ver|
          defined_by_condition.possibly_satisfied_by?(ext_ver)
        end
      end
    )
end

#pruned_reset_value_astnil, Idl::FunctionBodyAst

Returns:

  • (nil)

    If the reset_value is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the reset_value function, type checked and pruned



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/udb/obj/csr_field.rb', line 388

def pruned_reset_value_ast
  return @pruned_reset_value_ast unless @pruned_reset_value_ast.nil?

  return nil unless @data.key?("reset_value()")

  ast = T.must(type_checked_reset_value_ast)

  symtab = fill_symtab_for_reset(ast)
  ast = ast.prune(symtab)
  symtab.pop
  ast.freeze_tree(symtab)
  symtab.release

  @pruned_reset_value_ast = ast
end

#pruned_sw_write_ast(effective_xlen) ⇒ nil, Idl::AstNode

Parameters:

  • effective_xlen (Integer, nil)

    effective xlen, needed because fields can change in different bases

Returns:

  • (nil)

    if there is no sw_write() function

  • (Idl::AstNode, nil)

    The abstract syntax tree of the sw_write() function, type checked and pruned



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/udb/obj/csr_field.rb', line 598

def pruned_sw_write_ast(effective_xlen)
  return @pruned_sw_write_ast unless @pruned_sw_write_ast.nil?

  return nil unless @data.key?("sw_write(csr_value)")

  ast = T.must(type_checked_sw_write_ast(cfg_arch.symtab, effective_xlen))

  return ast if cfg_arch.unconfigured?

  symtab = fill_symtab_for_sw_write(effective_xlen, ast)

  ast = ast.prune(symtab)
  raise "Symbol table didn't come back at global + 1" unless symtab.levels == 2

  ast.freeze_tree(cfg_arch.symtab)

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

  symtab.pop
  symtab.release

  @pruned_sw_write_ast = ast
end

#pruned_type_ast(effective_xlen) ⇒ nil, Idl::FunctionBodyAst

Parameters:

  • effective_xlen (Integer, nil)

    The effective xlen to evaluate for

Returns:

  • (nil)

    if the type property is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the type() function, after it has been type checked and pruned



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
186
187
188
189
# File 'lib/udb/obj/csr_field.rb', line 161

def pruned_type_ast(effective_xlen)
  @pruned_type_ast ||= { 32 => nil, 64 => nil }
  return @pruned_type_ast[effective_xlen] unless @pruned_type_ast[effective_xlen].nil?

  ast = type_checked_type_ast(effective_xlen)

  if ast.nil?
    # there is no type() (it must be constant)
    return nil
  end

  symtab = fill_symtab_for_type(effective_xlen, ast)
  ast = ast.prune(symtab)
  symtab.release

  symtab = fill_symtab_for_type(effective_xlen, ast)
  ast.freeze_tree(symtab)

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

  symtab.pop
  symtab.release

  @pruned_type_ast[effective_xlen] = ast
end

#reachable_functions(effective_xlen) ⇒ Array<Idl::FunctionDefAst>

Returns List of functions called through this field.

Parameters:

Returns:

  • (Array<Idl::FunctionDefAst>)

    List of functions called through this field



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
# File 'lib/udb/obj/csr_field.rb', line 300

def reachable_functions(effective_xlen)
  return @reachable_functions unless @reachable_functions.nil?

  fns = []
  if has_custom_sw_write?
    ast = pruned_sw_write_ast(effective_xlen)
    unless ast.nil?
      sw_write_symtab = fill_symtab_for_sw_write(effective_xlen, ast)
      fns.concat ast.reachable_functions(sw_write_symtab)
      sw_write_symtab.release
    end
  end
  if @data.key?("type()")
    ast = pruned_type_ast(effective_xlen)
    unless ast.nil?
      type_symtab = fill_symtab_for_type(effective_xlen, ast)
      fns.concat ast.reachable_functions(type_symtab)
      type_symtab.release
    end
  end
  if @data.key?("reset_value()")
    ast = pruned_reset_value_ast
    unless ast.nil?
      symtab = fill_symtab_for_reset(ast)
      fns.concat ast.reachable_functions(symtab)
      symtab.release
    end
  end

  @reachable_functions = fns.uniq
end

#reset_valueInteger, String

Returns:

  • (Integer)

    The reset value of this field

  • (String)

    The string ‘UNDEFINED_LEGAL’ if, for this config, there is no defined reset value



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/udb/obj/csr_field.rb', line 406

def reset_value
  @reset_value ||=
    if @data.key?("reset_value")
      @data["reset_value"]
    else
      ast = T.must(pruned_reset_value_ast)
      symtab = fill_symtab_for_reset(ast)
      val = T.let(nil, T.untyped)
      value_result = Idl::AstNode.value_try do
        val = ast.return_value(symtab)
      end
      Idl::AstNode.value_else(value_result) do
        val = "UNDEFINED_LEGAL"
      end
      val = "UNDEFINED_LEGAL" if val == 0x1_0000_0000_0000_0000
      symtab.release
      val
    end
end

#reset_value_astnil, Idl::FunctionBodyAst

Returns:

  • (nil)

    If the reset_value is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the reset_value function



349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/udb/obj/csr_field.rb', line 349

def reset_value_ast
  return @reset_value_ast unless @reset_value_ast.nil?
  return nil unless @data.key?("reset_value()")

  @reset_value_ast = cfg_arch.idl_compiler.compile_func_body(
    @data["reset_value()"],
    return_type: Idl::Type.new(:bits, width: max_width),
    name: "CSR[#{parent.name}].#{name}.reset_value()",
    input_file: csr.__source,
    input_line: csr.source_line(["fields", name, "reset_value()"]),
    symtab: cfg_arch.symtab,
    type_check: false
  )
end

#reset_value_prettyString

Returns:

  • (String)


437
438
439
440
441
442
443
444
445
446
447
# File 'lib/udb/obj/csr_field.rb', line 437

def reset_value_pretty
  str = T.let(nil, T.nilable(String))
  value_result = Idl::AstNode.value_try do
    str = reset_value
  end
  Idl::AstNode.value_else(value_result) do
    ast = T.must(reset_value_ast)
    str = ast.gen_option_adoc
  end
  T.must(str).to_s
end

#source_line(path) ⇒ Integer

CSR field data starts at fields: NAME: with the YAML

Parameters:

  • path (Array<String>)

Returns:

  • (Integer)


52
53
54
# File 'lib/udb/obj/csr_field.rb', line 52

def source_line(path)
  super(["fields", name].concat(path))
end

#sw_write_ast(symtab) ⇒ nil, Idl::FunctionBodyAst

Parameters:

  • symtab (Idl::SymbolTable)

    Symbols

Returns:

  • (nil)

    If there is no sw_write() function

  • (Idl::FunctionBodyAst, nil)

    The abstract syntax tree of the sw_write() function



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/udb/obj/csr_field.rb', line 498

def sw_write_ast(symtab)
  return @sw_write_ast unless @sw_write_ast.nil?
  return nil if @data["sw_write(csr_value)"].nil?

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

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

  @sw_write_ast
end

#type(effective_xlen = nil) ⇒ nil, String

returns the definitive type for a configuration

Parameters:

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

    The effective xlen to evaluate for

Returns:

  • (nil)

    when the type isn’t knowable

  • (String, nil)

    The type of the field. One of: ‘RO’ => Read-only ‘RO-H’ => Read-only, with hardware update ‘RW’ => Read-write ‘RW-R’ => Read-write, with a restricted set of legal values ‘RW-H’ => Read-write, with a hardware update ‘RW-RH’ => Read-write, with a hardware update and a restricted set of legal values



204
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
# File 'lib/udb/obj/csr_field.rb', line 204

def type(effective_xlen = nil)
  @type ||= { 32 => nil, 64 => nil }
  return @type[effective_xlen] unless @type[effective_xlen].nil?

  type = T.let(nil, T.untyped)
  type =
    if @data.key?("type")
      @data["type"]
    else
      # the type is config-specific...

      ast = T.must(type_checked_type_ast(effective_xlen))
      begin
        symtab = fill_symtab_for_type(effective_xlen, ast)

        value_result = ast.value_try do
          type =  case ast.return_value(symtab)
                  when 0
                    "RO"
                  when 1
                    "RO-H"
                  when 2
                    "RW"
                  when 3
                    "RW-R"
                  when 4
                    "RW-H"
                  when 5
                    "RW-RH"
                  else
                    raise "Unhandled CsrFieldType value"
                  end
        end
        ast.value_else(value_result) do
          type = nil
        end
      ensure
        symtab&.pop
        symtab&.release
      end
      type
      # end
      # Idl::AstNode.value_else(value_result) do
      #   warn "In parsing #{csr.name}.#{name}::type()"
      #   raise "  Return of type() function cannot be evaluated at compile time"
      #   Idl::AstNode.value_error ""
      # end
    end

  @type[effective_xlen] = type
end

#type_astnil, Idl::FunctionBodyAst

Returns:

  • (nil)

    if the type property is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the type() function



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/udb/obj/csr_field.rb', line 106

def type_ast
  return @type_ast unless @type_ast.nil?
  return nil if @data["type()"].nil?

  idl_code = T.must(@data["type()"])

  @type_ast = @cfg_arch.idl_compiler.compile_func_body(
    idl_code,
    name: "CSR[#{csr.name}].#{name}.type()",
    input_file: csr.__source,
    input_line: csr.source_line(["fields", name, "type()"]),
    symtab: @cfg_arch.symtab,
    type_check: false
  )

  raise "ast is nil?" if @type_ast.nil?

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

  @type_ast
end

#type_checked_reset_value_astnil, Idl::FunctionBodyAst

Returns:

  • (nil)

    If the reset_value is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the reset_value function, after being type checked



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/udb/obj/csr_field.rb', line 367

def type_checked_reset_value_ast
  return @type_checked_reset_value_ast unless @type_checked_reset_value_ast.nil?

  return nil unless @data.key?("reset_value()")

  ast = reset_value_ast

  symtab = fill_symtab_for_reset(ast)
  cfg_arch.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{csr.name}].reset_value()"
  )
  symtab.release

  @type_checked_reset_value_ast = ast
end

#type_checked_sw_write_ast(symtab, effective_xlen) ⇒ Idl::FunctionBodyAst?

Returns The abstract syntax tree of the sw_write() function, after being type checked.

Parameters:

  • symtab (Idl::SymbolTable)

    Symbol table with globals

  • effective_xlen (Integer, nil)

    32 or 64; the effective XLEN to evaluate this field in (relevant when fields move in different XLENs)

Returns:

  • (Idl::FunctionBodyAst, nil)

    The abstract syntax tree of the sw_write() function, after being type checked



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/udb/obj/csr_field.rb', line 459

def type_checked_sw_write_ast(symtab, effective_xlen)
  @type_checked_sw_write_asts ||= {}
  ast = @type_checked_sw_write_asts[symtab.hash]
  return ast unless ast.nil?

  return nil unless @data.key?("sw_write(csr_value)")

  symtab_hash = symtab.hash
  symtab = symtab.global_clone
  symtab.push(ast)
  # all CSR instructions are 32-bit
  symtab.add(
    "__instruction_encoding_size",
    Idl::Var.new("__instruction_encoding_size", Idl::Type.new(:bits, width: 6), 32)
  )
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:bits, width: 128) # to accommodate special return values (e.g., UNDEFIEND_LEGAL_DETERMINISITIC)
  )
  symtab.add(
    "csr_value",
    Idl::Var.new("csr_value", csr.bitfield_type(@cfg_arch, effective_xlen))
  )

  ast = T.must(sw_write_ast(symtab))
  @cfg_arch.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{csr.name}].#{name}.sw_write()"
  )
  symtab.pop
  symtab.release
  @type_checked_sw_write_asts[symtab_hash] = ast
end

#type_checked_type_ast(effective_xlen) ⇒ nil, Idl::FunctionBodyAst

Parameters:

  • effective_xlen (Integer, nil)

    The effective xlen to evaluate for

Returns:

  • (nil)

    if the type property is not a function

  • (Idl::FunctionBodyAst, nil)

    Abstract syntax tree of the type() function, after it has been type checked



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/udb/obj/csr_field.rb', line 132

def type_checked_type_ast(effective_xlen)
  @type_checked_type_ast ||= { 32 => nil, 64 => nil }
  return @type_checked_type_ast[effective_xlen] unless @type_checked_type_ast[effective_xlen].nil?

  ast = type_ast

  if ast.nil?
    # there is no type() (it must be constant)
    return nil
  end

  symtab = fill_symtab_for_type(effective_xlen, ast)

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

  symtab.pop
  symtab.release

  @type_checked_type_ast[effective_xlen] = ast
end

#type_desc(effective_xlen = nil) ⇒ String

Returns Long description of the field type.

Parameters:

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

Returns:

  • (String)

    Long description of the field type



832
833
834
# File 'lib/udb/obj/csr_field.rb', line 832

def type_desc(effective_xlen=nil)
  TYPE_DESC_MAP.fetch(type(effective_xlen), "")
end

#type_pretty(effective_xlen = nil) ⇒ String

Returns A pretty-printed type string.

Parameters:

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

    The effective xlen to evaluate for

Returns:

  • (String)

    A pretty-printed type string



259
260
261
262
263
264
265
266
# File 'lib/udb/obj/csr_field.rb', line 259

def type_pretty(effective_xlen = nil)
  str = type(effective_xlen)
  if str.nil?
    ast = T.must(type_ast)
    str = ast.gen_option_adoc
  end
  T.must(str)
end

#width(effective_xlen) ⇒ Integer

Returns Number of bits in the field.

Parameters:

  • effective_xlen (Integer, nil)

    The effective xlen, needed since some fields change location with XLEN. If the field location is not determined by XLEN, then this parameter can be nil

Returns:

  • (Integer)

    Number of bits in the field



699
700
701
# File 'lib/udb/obj/csr_field.rb', line 699

def width(effective_xlen)
  T.must(location(effective_xlen).size)
end