Class: Udb::CsrField

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

Overview

A CSR field object

Defined Under Namespace

Classes: Alias, MemoizedState

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

Attributes inherited from DatabaseObject

#arch, #data, #data_path, #long_name, #name

Instance Method Summary collapse

Methods inherited from DatabaseObject

#<=>, #cfg_arch, #cfg_arch?, #clone, #defer, #description, #inspect, #kind

Constructor Details

#initialize(parent_csr, field_name, field_data) ⇒ CsrField

Returns a new instance of CsrField.



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

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
  @memo = MemoizedState.new(reachable_functions: {})
end

Instance Attribute Details

#parentObject (readonly) Also known as: csr

Returns the value of attribute parent.



22
23
24
# File 'lib/udb/obj/csr_field.rb', line 22

def parent
  @parent
end

Instance Method Details

#__sourceObject

CSR fields are defined in their parent CSR YAML file



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

def __source = @parent.__source

#affected_by?(ext_ver) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'lib/udb/obj/csr_field.rb', line 123

def affected_by?(ext_ver)
  defined_by_condition.empty? \
    ? false
    : defined_by_condition.satisfiability_depends_on_ext_req?(ext_ver.to_ext_req)
end

#aliasObject



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

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

#baseObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/udb/obj/csr_field.rb', line 33

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

#base32_only?Boolean

Returns:

  • (Boolean)


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

def base32_only? = base == 32

#base64_only?Boolean

Returns:

  • (Boolean)


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

def base64_only? = base == 64

#defined_by_conditionObject



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

def defined_by_condition
  @defined_by_condition ||=
    if @data.key?("definedBy")
      Condition.new(@data["definedBy"], @cfg_arch)
    else
      @parent.defined_by_condition
    end
end

#defined_in_all_bases?Boolean

Returns:

  • (Boolean)


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

def defined_in_all_bases? = base.nil?

#defined_in_base32?Boolean

Returns:

  • (Boolean)


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

def defined_in_base32? = base != 64

#defined_in_base64?Boolean

Returns:

  • (Boolean)


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

def defined_in_base64? = base != 32

#defined_in_base?(xlen) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#dynamic_location?Boolean

Returns:

  • (Boolean)


371
372
373
374
375
376
377
378
379
380
381
# File 'lib/udb/obj/csr_field.rb', line 371

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

  # MMR fields never have dynamic locations (no privilege modes)
  parent_reg = parent
  return false unless parent_reg.is_a?(Csr)

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

#dynamic_reset_value?Boolean

Returns:

  • (Boolean)


465
466
467
468
469
470
471
472
# File 'lib/udb/obj/csr_field.rb', line 465

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)


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

def exists? = exists_in_cfg?(cfg_arch)

#exists_in_cfg?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/udb/obj/csr_field.rb', line 86

def exists_in_cfg?(cfg_arch)
  # When the field has no definedBy of its own, its existence is identical to the parent's.
  # base is also nil in this case (no xlen restriction from the condition), so we can
  # skip the condition evaluation entirely and just delegate to the parent.
  return parent.exists_in_cfg?(cfg_arch) unless @data.key?("definedBy")

  if cfg_arch.fully_configured?
    parent.exists_in_cfg?(cfg_arch) &&
      (base.nil? || cfg_arch.possible_xlens.include?(base)) &&
      (defined_by_condition.satisfied_by_cfg_arch?(cfg_arch) == SatisfiedResult::Yes)
  elsif cfg_arch.partially_configured?
    parent.exists_in_cfg?(cfg_arch) &&
      (base.nil? || cfg_arch.possible_xlens.include?(base)) &&
      defined_by_condition.could_be_satisfied_by_cfg_arch?(cfg_arch)
  else
    true
  end
end

#fill_symtab_for_reset(ast) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/udb/obj/csr_field.rb', line 605

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

  @reset_return_type ||= Idl::Type.new(:bits, width: max_width).freeze
  symtab.add("__expected_return_type", @reset_return_type)

  # XLEN at reset is always mxlen
  unless cfg_arch.mxlen.nil?
    mxlen = cfg_arch.mxlen
    @reset_xlen_var ||= Idl::Var.new("__effective_xlen", Csr::BITS6_TYPE, mxlen).freeze
    symtab.add("__effective_xlen", @reset_xlen_var)
  end

  symtab
end

#fill_symtab_for_sw_write(effective_xlen, ast) ⇒ Object



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 554

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", Csr::ENCODING_SIZE_VAR)
  symtab.add("__expected_return_type", Csr::BITS128_TYPE)
  symtab.add(
    "csr_value",
    Idl::Var.new("csr_value", csr.bitfield_type(@cfg_arch, effective_xlen))
  )
  if symtab.get("MXLEN").value.nil?
    mxlen_key = effective_xlen.nil? ? :nil_xlen : effective_xlen
    @mxlen_var_cache ||= {}
    @mxlen_var_cache[mxlen_key] ||= Idl::Var.new(
      "MXLEN",
      Csr::BITS6_CONST_TYPE,
      effective_xlen,
      param: true
    ).freeze
    symtab.add("MXLEN", @mxlen_var_cache[mxlen_key])
  end
  symtab
end

#fill_symtab_for_type(effective_xlen, ast) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/udb/obj/csr_field.rb', line 580

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", Csr::ENCODING_SIZE_VAR)
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:enum_ref, enum_class: symtab.get("CsrFieldType"))
  )
  if symtab.get("MXLEN").value.nil?
    mxlen_key = effective_xlen.nil? ? :nil_xlen : effective_xlen
    @mxlen_var_cache ||= {}
    @mxlen_var_cache[mxlen_key] ||= Idl::Var.new(
      "MXLEN",
      Csr::BITS6_CONST_TYPE,
      effective_xlen,
      param: true
    ).freeze
    symtab.add("MXLEN", @mxlen_var_cache[mxlen_key])
  end

  symtab
end

#has_custom_sw_write?Boolean

Returns:

  • (Boolean)


489
490
491
# File 'lib/udb/obj/csr_field.rb', line 489

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

#location(effective_xlen = nil) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/udb/obj/csr_field.rb', line 659

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 || 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 || 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
        Udb.logger.warn "Location (#{key} = #{@data[key]}) is past the max csr length (#{csr.max_length}) in #{csr.name}.#{name}"
      end
    elsif e > csr_length
      Udb.logger.warn "Location (#{key} = #{@data[key]}) is past the csr length (#{csr_length}) in #{csr.name}.#{name}"

    end

    s..e
  end
end

#location_cond32Object



767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/udb/obj/csr_field.rb', line 767

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

#location_cond64Object



783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/udb/obj/csr_field.rb', line 783

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

#location_pretty(effective_xlen = nil) ⇒ Object



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/udb/obj/csr_field.rb', line 801

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", "D"
        "CSR[misa].MXL == %%"
      when "S"
        "CSR[mstatus].SXL == %%"
      when "VS"
        "CSR[hstatus].VSXL == %%"
      when "U"
        "CSR[mstatus].UXL == %%"
      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_widthObject



753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/udb/obj/csr_field.rb', line 753

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:

  • (Boolean)


110
111
112
113
114
115
116
117
# File 'lib/udb/obj/csr_field.rb', line 110

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) &&
    defined_by_condition.empty? \
      ? parent.optional_in_cfg?(cfg_arch)
      : (defined_by_condition.satisfied_by_cfg_arch?(cfg_arch) == SatisfiedResult::Maybe)
end

#pruned_reset_value_astObject



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/udb/obj/csr_field.rb', line 425

def pruned_reset_value_ast
  return @pruned_reset_value_ast if instance_variable_defined?(:@pruned_reset_value_ast)

  return (@pruned_reset_value_ast = 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) ⇒ Object



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/udb/obj/csr_field.rb', line 626

def pruned_sw_write_ast(effective_xlen)
  @pruned_sw_write_ast ||= {}
  return @pruned_sw_write_ast[effective_xlen] if @pruned_sw_write_ast.key?(effective_xlen)

  return (@pruned_sw_write_ast[effective_xlen] = nil) unless @data.key?("sw_write(csr_value)")

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

  return (@pruned_sw_write_ast[effective_xlen] = 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[effective_xlen] = ast
end

#pruned_type_ast(effective_xlen) ⇒ Object



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

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, cache = T.let({}, Idl::AstNode::ReachableFunctionCacheType)) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/udb/obj/csr_field.rb', line 332

def reachable_functions(effective_xlen, cache = T.let({}, Idl::AstNode::ReachableFunctionCacheType))
  cache_key = effective_xlen.nil? ? :nil : effective_xlen
  return @memo.reachable_functions[cache_key] unless @memo.reachable_functions[cache_key].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, cache)
      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, cache)
      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, cache)
      symtab.release
    end
  end

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

#reset_valueObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/udb/obj/csr_field.rb', line 444

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_astObject



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

def reset_value_ast
  return @reset_value_ast if instance_variable_defined?(:@reset_value_ast)
  return (@reset_value_ast = 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_prettyObject



475
476
477
478
479
480
481
482
483
484
485
# File 'lib/udb/obj/csr_field.rb', line 475

def reset_value_pretty
  str = T.let(nil, T.nilable(Idl::ValueRbType))
  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) ⇒ Object



77
78
79
# File 'lib/udb/obj/csr_field.rb', line 77

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

#sw_write_ast(symtab) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/udb/obj/csr_field.rb', line 533

def sw_write_ast(symtab)
  return @sw_write_ast if instance_variable_defined?(:@sw_write_ast)
  return (@sw_write_ast = 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) ⇒ Object



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
275
276
277
278
279
280
# File 'lib/udb/obj/csr_field.rb', line 230

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_astObject



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

def type_ast
  return @type_ast if instance_variable_defined?(:@type_ast)
  return (@type_ast = 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_astObject



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

def type_checked_reset_value_ast
  return @type_checked_reset_value_ast if instance_variable_defined?(:@type_checked_reset_value_ast)

  return (@type_checked_reset_value_ast = 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) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/udb/obj/csr_field.rb', line 497

def type_checked_sw_write_ast(symtab, effective_xlen)
  @type_checked_sw_write_asts ||= {}
  symtab_key = "#{symtab.name}/#{symtab.mxlen}"
  ast = @type_checked_sw_write_asts[symtab_key]
  return ast unless ast.nil?

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

  symtab = symtab.global_clone
  symtab.push(ast)
  # all CSR instructions are 32-bit
  symtab.add("__instruction_encoding_size", Csr::ENCODING_SIZE_VAR)
  symtab.add(
    "__expected_return_type",
    Csr::BITS128_TYPE # 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_key] = ast
end

#type_checked_type_ast(effective_xlen) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/udb/obj/csr_field.rb', line 158

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) ⇒ Object



887
888
889
# File 'lib/udb/obj/csr_field.rb', line 887

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

#type_pretty(effective_xlen = nil) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/udb/obj/csr_field.rb', line 285

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) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/udb/obj/csr_field.rb', line 728

def width(effective_xlen)
  loc =
    if @data.key?("location")
      @data.fetch("location")
    else
      if effective_xlen.nil?
        # just pick one. they better be the same
        @data.fetch("location_rv32")
      else
        @data.fetch("location_rv#{effective_xlen}")
      end
    end
  if loc.is_a?(Integer)
    return 1
  else
    raise "Unexpected location field" unless loc.is_a?(String)

    e, s = loc.split("-").map(&:to_i)
    raise "Invalid location" if T.must(s) > T.must(e)

    T.must((s..e).size)
  end
end