Class: Idl::VariableAssignmentAst

Inherits:
AstNode
  • Object
show all
Includes:
Executable
Defined in:
lib/idl/ast.rb

Overview

represents a scalar variable assignment statement

for example, these will result in a VariableAssignmentAst

# given: Bits<XLEN> zero;
zero = XLEN'b0

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, lhs_ast, rhs_ast) ⇒ VariableAssignmentAst

Returns a new instance of VariableAssignmentAst.



1670
1671
1672
1673
# File 'lib/idl/ast.rb', line 1670

def initialize(input, interval, lhs_ast, rhs_ast)
  super(input, interval, [lhs_ast, rhs_ast])
  @vars = {}
end

Instance Method Details

#execute(symtab) ⇒ void

This method returns an undefined value.

“execute” the statement by updating the variables in the symbol table

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Raises:

  • ValueError if some part of the statement cannot be executed at compile time



1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
# File 'lib/idl/ast.rb', line 1696

def execute(symtab)
  if lhs.is_a?(CsrWriteAst)
    value_error "CSR writes are never compile-time-known"
  else
    variable = var(symtab)

    internal_error "No variable #{lhs.text_value}" if variable.nil?

    value_result = value_try do
      variable.value = rhs.value(symtab)
    end
    value_else(value_result) do
      variable.value = nil
      value_error ""
    end
  end
end

#execute_unknown(symtab) ⇒ void

This method returns an undefined value.

“execute” the statement, forcing any variable assignments to an unknown state This is used down unknown conditional paths.

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Raises:

  • ValueError if some part of the statement cannot be executed at compile time



1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/idl/ast.rb', line 1715

def execute_unknown(symtab)
  if lhs.is_a?(CsrWriteAst)
    value_error "CSR writes are never compile-time-known"
  else
    variable = var(symtab)

    internal_error "No variable #{lhs.text_value}" if variable.nil?

    variable.value = nil
  end
end

#lhsObject



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

def lhs = @children[0]

#rhsObject



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

def rhs = @children[1]

#to_idlString

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

Returns:

  • (String)

    IDL code for the node



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

def to_idl = "#{lhs.to_idl} = #{rhs.to_idl}"

#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:



1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'lib/idl/ast.rb', line 1676

def type_check(symtab)
  lhs.type_check(symtab)
  type_error "Cannot assign to a const" if lhs.type(symtab).const?

  rhs.type_check(symtab)
  unless rhs.type(symtab).convertable_to?(lhs.type(symtab))
    type_error "Incompatible type in assignment (#{lhs.type(symtab)}, #{rhs.type(symtab)})"
  end
end

#var(symtab) ⇒ Object



1686
1687
1688
1689
1690
1691
1692
1693
# File 'lib/idl/ast.rb', line 1686

def var(symtab)
  variable = @vars[symtab.archdef]
  if variable.nil?
    variable = symtab.get(lhs.text_value)
    @vars[symtab.archdef] = variable
  end
  variable
end