Class: CsrField

Inherits:
ArchDefObject show all
Defined in:
lib/arch_obj_models/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 writeable 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_data) ⇒ CsrField

Returns a new instance of CsrField.

Parameters:

  • parent_csr (Csr)

    The Csr that defined this field

  • field_data (Hash<String,Object>)

    Field data from the arch spec



26
27
28
29
# File 'lib/arch_obj_models/csr_field.rb', line 26

def initialize(parent_csr, field_data)
  super(field_data)
  @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



10
11
12
# File 'lib/arch_obj_models/csr_field.rb', line 10

def parent
  @parent
end

Instance Method Details

#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



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
# File 'lib/arch_obj_models/csr_field.rb', line 234

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 = 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 = arch_def.csr(csr_name).field(csr_field)
    range =
      if range.nil?
        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

#baseInteger?

Returns:

  • (Integer)

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

  • (nil)

    if the CsrField exists in any XLEN



20
21
22
# File 'lib/arch_obj_models/csr_field.rb', line 20

def base
  @data["base"]
end

#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



661
# File 'lib/arch_obj_models/csr_field.rb', line 661

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



658
# File 'lib/arch_obj_models/csr_field.rb', line 658

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

#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



667
# File 'lib/arch_obj_models/csr_field.rb', line 667

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

#defined_in_base32?Boolean

Returns:

  • (Boolean)


663
# File 'lib/arch_obj_models/csr_field.rb', line 663

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

#defined_in_base64?Boolean

Returns:

  • (Boolean)


664
# File 'lib/arch_obj_models/csr_field.rb', line 664

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

#dynamic_location?(arch_def) ⇒ Boolean

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

Parameters:

  • arch_def (ArchDef)

    A configuration

Returns:

  • (Boolean)

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



341
342
343
344
345
346
347
# File 'lib/arch_obj_models/csr_field.rb', line 341

def dynamic_location?(arch_def)
  # 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| arch_def.multi_xlen_in_mode?(mode) }
end

#dynamic_reset_value?(arch_def) ⇒ Boolean

Returns:

  • (Boolean)


457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/arch_obj_models/csr_field.rb', line 457

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

  value_result = value_try do
    if arch_def.mxlen.nil?
      # need to try with generic symtab_32/symtab_64
      reset_value_32 = reset_value(arch_def, 32)
      reset_value_64 = reset_value(arch_def, 64)
      reset_value_32 != reset_value_64
    else
      # just call the function, see if we get a value error
      reset_value(arch_def)
      false
    end
  end || true
end

#exists_in_cfg?(arch_def) ⇒ Boolean

Returns whether or not the instruction is implemented given the supplies config options.

Parameters:

  • possible_xlens (Array<Integer>)

    List of xlens that be used in any implemented mode

  • extensions (Array<ExtensionVersion>)

    List of extensions implemented

Returns:

  • (Boolean)

    whether or not the instruction is implemented given the supplies config options



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arch_obj_models/csr_field.rb', line 34

def exists_in_cfg?(arch_def)
  if arch_def.fully_configured?
    parent.exists_in_cfg?(arch_def) &&
      (@data["base"].nil? || arch_def.possible_xlens.include?(@data["base"])) &&
      (@data["definedBy"].nil? || arch_def.implemented_extensions.any? { |ext_ver| defined_by?(ext_ver) })
  else
    raise "unexpected type" unless arch_def.partially_configured?

    parent.exists_in_cfg?(arch_def) &&
      (@data["base"].nil? || arch_def.possible_xlens.include?(@data["base"])) &&
      (@data["definedBy"].nil? || arch_def.prohibited_extensions.none? { |ext_ver| defined_by?(ext_ver) })
  end
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



496
497
498
# File 'lib/arch_obj_models/csr_field.rb', line 496

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

#location(arch_def, effective_xlen = nil) ⇒ Range

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

Parameters:

  • arch_def (ArchDef)

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

  • effective_xlen (Integer) (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)

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



614
615
616
617
618
619
620
621
622
623
624
625
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
654
655
# File 'lib/arch_obj_models/csr_field.rb', line 614

def location(arch_def, 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(arch_def, 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(arch_def)
        raise "Location (#{key} = #{@data[key]}) is past the max csr length (#{csr.max_length(arch_def)}) in #{csr.name}.#{name}"
      end
    elsif @data[key] > csr_length
      raise "Location (#{key} = #{@data[key]}) is past the csr length (#{csr.length(arch_def, effective_xlen)}) in #{csr.name}.#{name}"
    end

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

    csr_length = csr.length(arch_def, 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(arch_def)
        raise "Location (#{key} = #{@data[key]}) is past the max csr length (#{csr.max_length(arch_def)}) 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_cond32Object



676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/arch_obj_models/csr_field.rb', line 676

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_cond64Object



689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/arch_obj_models/csr_field.rb', line 689

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(arch_def, effective_xlen = nil) ⇒ String

Returns Pretty-printed location string.

Returns:

  • (String)

    Pretty-printed location string



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/arch_obj_models/csr_field.rb', line 703

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

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

  if dynamic_location?(arch_def)
    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(arch_def, 32))} when #{condition.sub('%%', '0')}
        * #{derangeify.call(location(arch_def, 64))} when #{condition.sub('%%', '1')}
      LOC
    else
      derangeify.call(location(arch_def, effective_xlen))
    end
  else
    derangeify.call(location(arch_def, arch_def.mxlen))
  end
end

#optional_in_cfg?(arch_def) ⇒ Boolean

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

Returns:

  • (Boolean)

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/arch_obj_models/csr_field.rb', line 49

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

  exists_in_cfg?(arch_def) &&
    (
      if data["definedBy"].nil?
        parent.optional_in_cfg?(arch_def)
      else
        arch_def.mandatory_extensions.all? do |ext_req|
          ext_req.satisfying_versions(arch_def).none? do |ext_ver|
            defined_by?(ext_ver)
          end
        end
      end
    )
end

#pruned_reset_value_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • symtab (Idl::SymbolTable)

    Global symbol table

Returns:

  • (Idl::FunctionBodyAst)

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

  • (nil)

    If the reset_value is not a function



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/arch_obj_models/csr_field.rb', line 398

def pruned_reset_value_ast(symtab)
  @pruned_reset_value_asts ||= {}
  ast = @pruned_reset_value_asts[symtab.hash]
  return ast unless ast.nil?

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

  ast = type_checked_reset_value_ast(symtab)

  symtab_hash = symtab.hash
  symtab = symtab.deep_clone
  symtab.push(ast)
  symtab.add("__expected_return_type", Idl::Type.new(:bits, width: 64))

  ast = ast.prune(symtab)

  symtab.pop

  ast.freeze_tree(symtab)

  symtab.push(ast)
  symtab.add("__expected_return_type", Idl::Type.new(:bits, width: 64))
  symtab.archdef.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{csr.name}].#{name}.reset_value()"
  )

  @type_checked_reset_value_asts[symtab_hash] = ast
end

#pruned_sw_write_ast(arch_def, effective_xlen) ⇒ Idl::FunctionBodyAst?

Parameters:

  • effective_xlen (Integer)

    effective xlen, needed because fields can change in different bases

  • arch_def (ArchDef)

    A configuration

Returns:

  • (Idl::FunctionBodyAst)

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

  • (nil)

    if there is no sw_write() function

Raises:

  • (ArgumentError)


567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/arch_obj_models/csr_field.rb', line 567

def pruned_sw_write_ast(arch_def, effective_xlen)
  @pruned_sw_write_asts ||= {}
  ast = @pruned_sw_write_asts[arch_def.name]
  return ast unless ast.nil?

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

  raise ArgumentError, "arch_def must be configured to prune" if arch_def.unconfigured?

  symtab = arch_def.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(arch_def, effective_xlen))
  )

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

  ast.freeze_tree(arch_def.symtab)


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

  symtab.pop
  symtab.release

  @pruned_sw_write_asts[arch_def.name] = ast
end

#pruned_type_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • symtab (Idl::SymbolTable)

    Global symbols

Returns:

  • (Idl::FunctionBodyAst)

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

  • (nil)

    if the type property is not a function



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/arch_obj_models/csr_field.rb', line 121

def pruned_type_ast(symtab)
  @pruned_type_asts ||= {}
  ast = @pruned_type_asts[symtab.hash]
  return ast unless ast.nil?

  ast = type_checked_type_ast(symtab).prune(symtab.deep_clone)

  symtab_hash = symtab.hash
  symtab = symtab.global_clone
  symtab.push(ast)
  # all CSR instructions are 32-bit
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:enum_ref, enum_class: symtab.get("CsrFieldType"))
  )

  ast.freeze_tree(symtab)

  symtab.archdef.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{name}].type()"
  )
  symtab.pop
  symtab.release
  @pruned_type_asts[symtab_hash] = ast
end

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

Returns List of functions called thorugh this field.

Parameters:

  • archdef (ArchDef)

    a configuration

Returns:

  • (Array<Idl::FunctionDefAst>)

    List of functions called thorugh this field



263
264
265
266
267
268
269
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
300
301
302
303
# File 'lib/arch_obj_models/csr_field.rb', line 263

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

  symtab =
    if (archdef.configured?)
      archdef.symtab
    else
      raise ArgumentError, "Must supply effective_xlen for generic ArchDef" if effective_xlen.nil?

      if effective_xlen == 32
        archdef.symtab_32
      else
        archdef.symtab_64
      end
    end

  fns = []
  if has_custom_sw_write?
    ast = pruned_sw_write_ast(archdef, effective_xlen)
    unless ast.nil?
      sw_write_symtab = symtab.deep_clone
      sw_write_symtab.push(ast)
      sw_write_symtab.add("csr_value", Idl::Var.new("csr_value", csr.bitfield_type(symtab.archdef, effective_xlen)))
      fns.concat ast.reachable_functions(sw_write_symtab)
    end
  end
  if @data.key?("type()")
    ast = pruned_type_ast(symtab.deep_clone)
    unless ast.nil?
      fns.concat ast.reachable_functions(symtab.deep_clone.push(ast))
    end
  end
  if @data.key?("reset_value()")
    ast = pruned_reset_value_ast(symtab.deep_clone)
    unless ast.nil?
      fns.concat ast.reachable_functions(symtab.deep_clone.push(ast))
    end
  end

  @reachable_functions = fns.uniq
end

#reachable_functions_unevaluated(symtab) ⇒ Array<Idl::FunctionDefAst>

Returns List of functions called thorugh this field, irrespective of context.

Parameters:

  • symtab (SymbolTable)

Returns:

  • (Array<Idl::FunctionDefAst>)

    List of functions called thorugh this field, irrespective of context

Raises:

  • (ArgumentError)


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/arch_obj_models/csr_field.rb', line 307

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

  return @reachable_functions_unevaluated unless @reachable_functions_unevaluated.nil?

  fns = []
  if has_custom_sw_write?
    ast = sw_write_ast(symtab)
    unless ast.nil?
      fns.concat ast.reachable_functions_unevaluated(symtab)
    end
  end
  if @data.key?("type()")
    ast = type_ast(symtab)
    unless ast.nil?
      fns.concat ast.reachable_functions_unevaluated(symtab)
    end
  end
  if @data.key?("reset_value()")
    ast = reset_value_ast(symtab)
    unless ast.nil?
      fns.concat ast.reachable_functions_unevalutated(symtab)
    end
  end

  @reachable_functions_unevaluated = fns.uniq
end

#reset_value(arch_def, effective_xlen = nil) ⇒ Integer, String

Parameters:

Returns:

  • (Integer)

    The reset value of this field

  • (String)

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



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/arch_obj_models/csr_field.rb', line 432

def reset_value(arch_def, effective_xlen = nil)
  cached_value = @reset_value_cache.nil? ? nil : @reset_value_cache[arch_def]
  return cached_value if cached_value

  @reset_value_cache ||= {}

  @reset_value_cache[arch_def] =
    if @data.key?("reset_value")
      @data["reset_value"]
    else
      symtab =
        if !arch_def.mxlen.nil?
          arch_def.symtab
        else
          raise ArgumentError, "effective_xlen is required when using generic arch_def" if effective_xlen.nil?

          effective_xlen == 32 ? arch_def.symtab_32 : arch_def.symtab_64
        end
      ast = pruned_reset_value_ast(symtab.deep_clone)
      val = ast.return_value(symtab.deep_clone.push(ast))
      val = "UNDEFINED_LEGAL" if val == 0x1_0000_0000_0000_0000
      val
    end
end

#reset_value_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • arch_def (IdL::Compiler)

    A compiler

Returns:

  • (Idl::FunctionBodyAst)

    Abstract syntax tree of the reset_value function

  • (nil)

    If the reset_value is not a function

Raises:

  • (ArgumentError)


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/arch_obj_models/csr_field.rb', line 352

def reset_value_ast(symtab)
  raise ArgumentError, "Argument should be a symtab (is a #{symtab.class.name})" unless symtab.is_a?(Idl::SymbolTable)

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

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

#reset_value_pretty(arch_def) ⇒ Object



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/arch_obj_models/csr_field.rb', line 474

def reset_value_pretty(arch_def)
  str = nil
  value_result = Idl::AstNode.value_try do
    str =
      if arch_def.mxlen.nil?
        if dynamic_reset_value?(arch_def)
          Idl::AstNode.value_error ""
        else
          reset_value(arch_def, 32) # 32 or 64, doesn't matter
        end
      else
        reset_value(arch_def)
      end
  end
  Idl::AstNode.value_else(value_result) do
    ast = reset_value_ast(arch_def.symtab)
    str = ast.gen_option_adoc
  end
  str
end

#sw_write_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • archdef (ArchDef)

    An architecture definition

Returns:

  • (Idl::FunctionBodyAst)

    The abstract syntax tree of the sw_write() function

  • (nil)

    If there is no sw_write() function

Raises:

  • (ArgumentError)


541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/arch_obj_models/csr_field.rb', line 541

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

  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 = symtab.archdef.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(symtab) ⇒ String

returns the definitive type for a configuration

Parameters:

  • symtab (SymbolTable)

    Symbol table

Returns:

  • (String)

    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
    

Raises:

  • (ArgumentError)


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
186
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
216
# File 'lib/arch_obj_models/csr_field.rb', line 160

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

  unless @type_cache.nil?
    raise "Different archdef for type #{@type_cache.keys},  #{symtab.archdef}" unless @type_cache.key?(symtab.archdef)

    return @type_cache[symtab.archdef]
  end

  type =
    if @data.key?("type")
      @data["type"]
    else
      # the type is config-specific...
      idl = @data["type()"]
      raise "type() is nil for #{csr.name}.#{name} #{@data}?" if idl.nil?



      # value_result = Idl::AstNode.value_try do
      ast = type_checked_type_ast(symtab)
      begin
        symtab = symtab.global_clone

        symtab.push(ast)
        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
      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_cache ||= {}
  @type_cache[symtab.archdef] = type
end

#type_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • symtab (SymbolTable)

    Symbol table with execution context

Returns:

  • (Idl::FunctionBodyAst)

    Abstract syntax tree of the type() function

  • (nil)

    if the type property is not a function



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arch_obj_models/csr_field.rb', line 69

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

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

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

  @type_ast
end

#type_checked_reset_value_ast(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • symtab (Idl::SymbolTable)

    A symbol table with globals

Returns:

  • (Idl::FunctionBodyAst)

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

  • (nil)

    If the reset_value is not a function

Raises:

  • (ArgumentError)


372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/arch_obj_models/csr_field.rb', line 372

def type_checked_reset_value_ast(symtab)
  raise ArgumentError, "Expecting Idl::SymbolTable" unless symtab.is_a?(Idl::SymbolTable)

  @type_checked_reset_value_asts ||= {}
  ast = @type_checked_reset_value_asts[symtab.hash]
  return ast unless ast.nil?

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

  ast = reset_value_ast(symtab)

  symtab_hash = symtab.hash
  symtab = symtab.deep_clone
  symtab.push(ast)
  symtab.add("__expected_return_type", Idl::Type.new(:bits, width: 64))
  symtab.archdef.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{csr.name}].reset_value()"
  )
  @type_checked_reset_value_asts[symtab_hash] = ast
end

#type_checked_sw_write_ast(symtab, effective_xlen) ⇒ FunctionBodyAst

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

Parameters:

  • effective_xlen (Integer)

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

  • symtab (Idl::SymbolTable)

    Symbol table with globals

Returns:

  • (FunctionBodyAst)

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



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
528
529
530
531
532
533
534
535
536
# File 'lib/arch_obj_models/csr_field.rb', line 503

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 accomodate special return values (e.g., UNDEFIEND_LEGAL_DETERMINISITIC)
  )
  symtab.add(
    "csr_value",
    Idl::Var.new("csr_value", csr.bitfield_type(symtab.archdef, effective_xlen))
  )

  ast = sw_write_ast(symtab)
  symtab.archdef.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(symtab) ⇒ Idl::FunctionBodyAst?

Parameters:

  • symtab (Idl::SymbolTable)

    Symbol table

Returns:

  • (Idl::FunctionBodyAst)

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

  • (nil)

    if the type property is not a function



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
116
# File 'lib/arch_obj_models/csr_field.rb', line 90

def type_checked_type_ast(symtab)
  @type_checked_type_asts ||= {}
  ast = @type_checked_type_asts[symtab.hash]
  return ast unless ast.nil?

  symtab_hash = symtab.hash

  symtab = symtab.global_clone

  symtab.push(ast)
  # all CSR instructions are 32-bit
  symtab.add(
    "__expected_return_type",
    Idl::Type.new(:enum_ref, enum_class: symtab.get("CsrFieldType"))
  )

  ast = type_ast(symtab)
  symtab.archdef.idl_compiler.type_check(
    ast,
    symtab,
    "CSR[#{name}].type()"
  )
  symtab.pop
  symtab.release

  @type_checked_type_asts[symtab_hash] = ast
end

#type_desc(arch_def) ⇒ String

Returns Long description of the field type.

Returns:

  • (String)

    Long description of the field type



786
787
788
# File 'lib/arch_obj_models/csr_field.rb', line 786

def type_desc(arch_def)
  TYPE_DESC_MAP[type(arch_def.symtab)]
end

#type_pretty(symtab) ⇒ String

Returns A pretty-printed type string.

Returns:

  • (String)

    A pretty-printed type string

Raises:

  • (ArgumentError)


219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/arch_obj_models/csr_field.rb', line 219

def type_pretty(symtab)
  raise ArgumentError, "Expecting SymbolTable" unless symtab.is_a?(Idl::SymbolTable)

  str = nil
  value_result = Idl::AstNode.value_try do
    str = type(symtab)
  end
  Idl::AstNode.value_else(value_result) do
    ast = type_ast(symtab)
    str = ast.gen_option_adoc
  end
  str
end

#width(arch_def, effective_xlen) ⇒ Integer

Returns Number of bits in the field.

Parameters:

  • arch_def (ArchDef)

    A config. May be nil if the width of the field is not configuration-dependent

  • effective_xlen (Integer)

    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



672
673
674
# File 'lib/arch_obj_models/csr_field.rb', line 672

def width(arch_def, effective_xlen)
  location(arch_def, effective_xlen).size
end