Class: Idl::BitsCastAst

Inherits:
AstNode show all
Includes:
Rvalue
Defined in:
lib/idl/ast.rb

Overview

Node for a cast to a Bits<N> type

This will result in a BitsCaseAst:

$bits(ExceptionCode::LoadAccessFault)

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, exp) ⇒ BitsCastAst

Returns a new instance of BitsCastAst.



2569
# File 'lib/idl/ast.rb', line 2569

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

Instance Method Details

#expressionAstNode

Returns The casted expression.

Returns:

  • (AstNode)

    The casted expression



2567
# File 'lib/idl/ast.rb', line 2567

def expression = @children[0]

#to_idlString

Return valid IDL representation of the node (and its subtree)

Returns:

  • (String)

    IDL code for the node



2621
# File 'lib/idl/ast.rb', line 2621

def to_idl = "$signed(#{expression.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:



2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
# File 'lib/idl/ast.rb', line 2581

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

  case etype.kind
  when :bits
    etype
  when :enum_ref
    Type.new(:bits, width: etype.enum_class.width)
  when :csr
    if etype.csr.dynamic_length?(symtab.archdef)
      Type.new(:bits, width: :unknown)
    else
      Type.new(:bits, width: etype.csr.length(symtab.archdef))
    end
  end
end

#type_check(symtab) ⇒ 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:



2572
2573
2574
2575
2576
2577
2578
# File 'lib/idl/ast.rb', line 2572

def type_check(symtab)
  expression.type_check(symtab)

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

#value(symtab) ⇒ Object

Return the compile-time-known value of the node



2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
# File 'lib/idl/ast.rb', line 2599

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

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

#values(symtab) ⇒ Array<Integer>, ... Originally defined in module Rvalue

Return a complete list of possible compile-time-known values of the node, or raise a ValueError if the full list cannot be determined

For most AstNodes, this will just be a single-entry array

Parameters:

  • symtab (SymbolTable)

    The context for the evaulation

Returns:

  • (Array<Integer>)

    The complete list of compile-time-known values, when they are integral

  • (Array<Boolean>)

    The complete list of compile-time-known values, when they are booleans

  • (AstNode::ValueError)

    if the list of values is not knowable at compile time