Class: Idl::CsrFieldReadExpressionAst

Inherits:
AstNode
  • Object
show all
Includes:
Rvalue
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/prune.rb,
lib/idlc/passes/gen_adoc.rb

Defined Under Namespace

Classes: MemoizedState

Constant Summary

Constants inherited from AstNode

AstNode::Bits1Type, AstNode::Bits32Type, AstNode::Bits64Type, AstNode::BoolType, AstNode::ConstBoolType, AstNode::PossiblyUnknownBits1Type, AstNode::PossiblyUnknownBits32Type, AstNode::PossiblyUnknownBits64Type, AstNode::ReachableFunctionCacheType, AstNode::StringType, AstNode::VoidType

Instance Attribute Summary

Attributes inherited from AstNode

#children, #input, #interval, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rvalue

#max_value, #min_value, #truncate, #values

Methods inherited from AstNode

#always_terminates?, #declaration?, #executable?, extract_base_var_name, #find_ancestor, #find_dst_registers, #find_referenced_csrs, #find_src_registers, #freeze_tree, #gen_option_adoc, #input_file, input_from_source_yaml, #inspect, #internal_error, interval_from_source_yaml, #lineno, #lines_around, #nullify_assignments, #pass_find_return_values, #path, #print_ast, #reachable_exceptions, #reachable_functions, #set_input_file, #set_input_file_unless_already_set, #source_line_file_offsets, #source_starting_offset, #source_yaml, #starting_line, #text_value, #truncation_warn, #type_error, #unindent, value_else, #value_else, value_error, #value_error, value_try, #value_try, write_back_nested

Constructor Details

#initialize(input, interval, csr, field_name) ⇒ CsrFieldReadExpressionAst

Returns a new instance of CsrFieldReadExpressionAst.



9350
9351
9352
9353
9354
9355
9356
# File 'lib/idlc/ast.rb', line 9350

def initialize(input, interval, csr, field_name)
  super(input, interval, [csr])

  @csr = csr
  @field_name = field_name
  @memo = MemoizedState.new(value_calculated: false)
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
# File 'lib/idlc/ast.rb', line 9414

def self.from_h(yaml, source_mapper)
  raise "Bad YAML" unless yaml.key?("kind") && yaml.fetch("kind") == "csr_field_read_expr"

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  CsrFieldReadExpressionAst.new(
    input, interval,
    T.cast(AstNode.from_h(yaml.fetch("csr"), source_mapper), CsrReadExpressionAst),
    yaml.fetch("field_name")
  )
end

Instance Method Details

#calc_type(symtab) ⇒ Object



9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
# File 'lib/idlc/ast.rb', line 9434

def calc_type(symtab)
  fd = field_def(symtab)

  if fd.defined_in_all_bases?
    Type.new(:bits, width: symtab.possible_xlens.map { |xlen| fd.width(xlen) }.max)
  elsif fd.base64_only?
    Type.new(:bits, width: fd.width(64))
  elsif fd.base32_only?
    Type.new(:bits, width: fd.width(32))
  else
    internal_error "unexpected field base"
  end
end

#calc_value(symtab) ⇒ Object



9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
# File 'lib/idlc/ast.rb', line 9463

def calc_value(symtab)
  # field isn't implemented, so it must be zero
  @memo.value_calculated = true

  if !field_def(symtab).exists?
    @memo.value = 0
    return
  end

  symtab.possible_xlens.each do |effective_xlen|
    unless field_def(symtab).type(effective_xlen) == "RO"
      value_error "'#{csr_name}.#{field_name(symtab)}' is not RO"
    end
  end

  v = field_def(symtab).reset_value
  v = nil if v == "UNDEFINED_LEGAL"
  @memo.value = v
end

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


9347
# File 'lib/idlc/ast.rb', line 9347

def const_eval?(symtab) = !@value.nil?

#csr_def(symtab) ⇒ Object



9382
9383
9384
# File 'lib/idlc/ast.rb', line 9382

def csr_def(symtab)
  csr_obj(symtab)
end

#csr_nameObject



9387
# File 'lib/idlc/ast.rb', line 9387

def csr_name = @csr.csr_name

#csr_obj(symtab) ⇒ Object



9359
9360
9361
9362
9363
9364
9365
9366
9367
# File 'lib/idlc/ast.rb', line 9359

def csr_obj(symtab)
  @memo.csr ||=
    begin
      obj = @csr.csr_def(symtab)
      type_error "No CSR '#{@csr.text_value}'" if obj.nil?

      obj
    end
end

#field_def(symtab) ⇒ Object



9390
9391
9392
# File 'lib/idlc/ast.rb', line 9390

def field_def(symtab)
  T.must(csr_obj(symtab).fields.find { |f| f.name == @field_name })
end

#field_name(symtab) ⇒ Object



9395
9396
9397
# File 'lib/idlc/ast.rb', line 9395

def field_name(symtab)
  field_def(symtab).name
end

#gen_adoc(indent = 0, indent_spaces: 2) ⇒ Object



319
320
321
322
323
# File 'lib/idlc/passes/gen_adoc.rb', line 319

def gen_adoc(indent = 0, indent_spaces: 2)
  csr_link = "%%UDB_DOC_LINK%csr;#{csr_name};#{csr_name}%%"
  field_link = "%%UDB_DOC_LINK%csr_field;#{csr_name}*#{@field_name};#{@field_name}%%"
  "#{' ' * indent}" + "CSR[#{csr_link}].#{field_link}"
end

#prune(symtab, forced_type: nil) ⇒ Object



732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/idlc/passes/prune.rb', line 732

def prune(symtab, forced_type: nil)
  value_result = value_try do
    v = value(symtab)
    if type(symtab).width == :unknown
      value_error "unknown width"
    end
    return PruneHelpers.create_int_literal(v, forced_type: forced_type || type(symtab))
  end
  value_else(value_result) do
    CsrFieldReadExpressionAst.new(input, interval, @csr.dup, @field_name)
  end
end

#to_hObject



9406
9407
9408
9409
9410
9411
# File 'lib/idlc/ast.rb', line 9406

def to_h = {
  "kind" => "csr_field_read_expr",
  "csr" => @csr.to_h,
  "field_name" => @field_name,
  "source" => source_yaml
}

#to_idlObject



9401
9402
9403
# File 'lib/idlc/ast.rb', line 9401

def to_idl
  "CSR[#{csr_name}].#{@field_name}"
end

#type(symtab) ⇒ Object



9428
9429
9430
# File 'lib/idlc/ast.rb', line 9428

def type(symtab)
  @memo.type ||= T.must(calc_type(symtab))
end

#type_check(symtab, strict:) ⇒ Object



9371
9372
9373
9374
9375
9376
9377
9378
9379
# File 'lib/idlc/ast.rb', line 9371

def type_check(symtab, strict:)
  @csr.type_check(symtab, strict:)

  type_error "#{@field_name} is not a field of CSR[#{csr_name}]" unless csr_def(symtab).fields.any? { |f| f.name == @field_name }
  if strict
    type_error "CSR[#{csr_name}].#{@field_name} is not defined in RV32" if symtab.mxlen == 32 && !field_def(symtab).defined_in_base32?
    type_error "CSR[#{csr_name}].#{@field_name} is not defined in RV64" if symtab.mxlen == 64 && !field_def(symtab).defined_in_base64?
  end
end

#value(symtab) ⇒ Object



9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
# File 'lib/idlc/ast.rb', line 9450

def value(symtab)
  calc_value(symtab) unless (@memo.value_calculated == true)


  if @memo.value.nil?
    value_error "'#{csr_name}.#{field_name(symtab)}' is not RO"
  else
    @memo.value
  end
end