Class: Idl::ReplicationExpressionAst

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

Overview

represents a replication expression

for example:

{5{5'd3}}

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, n, v) ⇒ ReplicationExpressionAst

Returns a new instance of ReplicationExpressionAst.



5590
5591
5592
# File 'lib/idlc/ast.rb', line 5590

def initialize(input, interval, n, v)
  super(input, interval, [n, v])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
# File 'lib/idlc/ast.rb', line 5643

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  ReplicationExpressionAst.new(
    input, interval,
    AstNode.from_h(yaml.fetch("count"), source_mapper),
    AstNode.from_h(yaml.fetch("expr"), source_mapper)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


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

def const_eval?(symtab) = n.const_eval?(symtab) && v.const_eval?(symtab)

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



282
283
284
# File 'lib/idlc/passes/gen_adoc.rb', line 282

def gen_adoc(indent = 0, indent_spaces: 2)
  "#{' ' * indent}{#{n.gen_adoc(0, indent_spaces:)}{#{v.gen_adoc(indent, indent_spaces:)}}}"
end

#nObject



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

def n = @children[0]

#prune(symtab, forced_type: nil) ⇒ Object



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/idlc/passes/prune.rb', line 668

def prune(symtab, forced_type: nil)
  value_result = value_try do
    v = value(symtab)
    return PruneHelpers.create_int_literal(v, forced_type: forced_type || type(symtab))
  end
  value_else(value_result) do
    c = ReplicationExpressionAst.new(input, interval, n.prune(symtab), v.prune(symtab))
    if forced_type
      if forced_type.width < type(symtab).width
        c = AryRangeAccessAst.new(
          input, interval, c, PruneHelpers.create_int_literal(forced_type.width - 1), create_int_literal(0)
        )
      elsif forced_type.width > type(symtab).width
        extra = forced_type.width - type(symtab).width
        mock_type = Struct.new(:width)
        c = ConcatenationExpressionAst.new(
          input, interval, [PruneHelpers.create_int_literal(0, forced_type: mock_type.new(extra))] + @children.map { |c| c.prune(symtab) }
        )
      end
    end
    c
  end
end

#to_hObject



5635
5636
5637
5638
5639
5640
# File 'lib/idlc/ast.rb', line 5635

def to_h = {
  "kind" => "repl_expr",
  "count" => n.to_h,
  "expr" => v.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "{#{n.to_idl}{#{v.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:



5620
5621
5622
5623
5624
5625
5626
5627
5628
# File 'lib/idlc/ast.rb', line 5620

def type(symtab)
  value_result = value_try do
    width = (n.value(symtab) * v.type(symtab).width)
    return Type.new(:bits, width:, qualifiers: [:known])
  end
  value_else(value_result) do
    Type.new(:bits, width: :unknown)
  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:



5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
# File 'lib/idlc/ast.rb', line 5595

def type_check(symtab, strict:)
  n.type_check(symtab, strict:)
  v.type_check(symtab, strict:)

  type_error "value of replication must be a Bits type" unless v.type(symtab).kind == :bits
  value_try do
    type_error "replication amount must be positive (#{n.value(symtab)})" unless n.value(symtab).positive?
  end
  # type_error "replication amount must be known at compile time"
end

#vObject



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

def v = @children[1]

#value(symtab) ⇒ Object

Return the compile-time-known value of the node



5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
# File 'lib/idlc/ast.rb', line 5607

def value(symtab)
  result = T.let(UnknownLiteral.new(0, 0), T.any(UnknownLiteral, Integer))
  n.value(symtab).times do |i|
    result |= v.value(symtab) << (i * v.type(symtab).width)
  end
  if result.is_a?(UnknownLiteral)
    result.unknown_mask.zero? ? result.known_value : result
  else
    result
  end
end