Class: Idl::BitsCastAst

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

Overview

Node for a cast to a Bits<N> type

This will result in a BitsCaseAst:

$bits(ExceptionCode::LoadAccessFault)

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, exp) ⇒ BitsCastAst

Returns a new instance of BitsCastAst.



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

def initialize(input, interval, exp) = super(input, interval, [exp])

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
# File 'lib/idlc/ast.rb', line 4499

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  BitsCastAst.new(
    input, interval,
    T.cast(AstNode.from_h(yaml.fetch("expr"), source_mapper), RvalueAst)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


4422
4423
4424
# File 'lib/idlc/ast.rb', line 4422

def const_eval?(symtab)
  expr.const_eval?(symtab)
end

#exprAstNode

Returns The casted expression.

Returns:

  • (AstNode)

    The casted expression



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

def expr = @children[0]

#gen_adoc(indent, indent_spaces: 2) ⇒ Object



107
108
109
# File 'lib/idlc/passes/gen_adoc.rb', line 107

def gen_adoc(indent, indent_spaces: 2)
  "#{' ' * indent}$bits(#{expr.gen_adoc(0, indent_spaces:)})"
end

#prune(symtab, forced_type: nil) ⇒ Object



762
763
764
765
766
767
768
769
# File 'lib/idlc/passes/prune.rb', line 762

def prune(symtab, forced_type: nil)
  p = expr.prune(symtab, forced_type:)
  if p.type(symtab).kind == :bits
    return p
  else
    return BitsCastAst.new(input, interval, p)
  end
end

#to_hObject



4492
4493
4494
4495
4496
# File 'lib/idlc/ast.rb', line 4492

def to_h = {
  "kind" => "bits_cast",
  "expr" => expr.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "$bits(#{expr.to_idl})"

#type(symtab) ⇒ Type

Given a specific symbol table, return the type of this node.

Should not be called until #type_check is called with the same arguments

Parameters:

Returns:

  • (Type)

    The type of the node

Raises:



4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
# File 'lib/idlc/ast.rb', line 4441

def type(symtab)
  etype = expr.type(symtab)

  case etype.kind
  when :bits
    etype
  when :bitfield
    Type.new(:bits, width: etype.width, qualifiers: [:known])
  when :enum_ref
    Type.new(:bits, width: etype.enum_class.width, qualifiers: [:known])
  when :csr
    if (etype.csr.is_a?(Symbol) && etype.csr == :unknown) || etype.csr.dynamic_length?
      Type.new(:bits, width: :unknown, max_width: 64)
    else
      effective_xlen = symtab.get("__effective_xlen")
      Type.new(:bits, width: etype.csr.length(effective_xlen.nil? ? nil : effective_xlen.value))
    end
  else
    type_error "$bits cast is only defined for CSRs and Enum references"
  end
end

#type_check(symtab, strict:) ⇒ void

This method returns an undefined value.

type check this node and all children

Calls to #type and/or #value may depend on type_check being called first with the same symtab. If not, those functions may raise an AstNode::InternalError

Parameters:

Raises:



4432
4433
4434
4435
4436
4437
4438
# File 'lib/idlc/ast.rb', line 4432

def type_check(symtab, strict:)
  expr.type_check(symtab, strict:)

  unless [:bits, :enum_ref, :bitfield, :csr].include?(expr.type(symtab).kind)
    type_error "#{expr.type(symtab)} Cannot be cast to bits"
  end
end

#value(symtab) ⇒ Object

Return the compile-time-known value of the node



4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
# File 'lib/idlc/ast.rb', line 4464

def value(symtab)
  etype = expr.type(symtab)

  case etype.kind
  when :bits
    expr.value(symtab)
  when :bitfield
    expr.value(symtab)
  when :enum_ref
    if expr.is_a?(EnumRefAst)
      element_name = expr.text_value.split(":")[2]
      etype.enum_class.value(element_name)
    else
      # this is an expression with an EnumRef type
      expr.value(symtab)
    end
  when :csr
    expr.value(symtab)
  else
    type_error "TODO: Bits cast for #{etype.kind}"
  end
end