Class: Idl::AryRangeAccessAst

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

Constant Summary

Constants inherited from AstNode

Idl::AstNode::Bits1Type, Idl::AstNode::Bits32Type, Idl::AstNode::Bits64Type, Idl::AstNode::BoolType, Idl::AstNode::ConstBoolType, Idl::AstNode::PossiblyUnknownBits1Type, Idl::AstNode::PossiblyUnknownBits32Type, Idl::AstNode::PossiblyUnknownBits64Type, Idl::AstNode::ReachableFunctionCacheType, Idl::AstNode::StringType, Idl::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, var, msb, lsb) ⇒ AryRangeAccessAst

Returns a new instance of AryRangeAccessAst.



2672
2673
2674
# File 'lib/idlc/ast.rb', line 2672

def initialize(input, interval, var, msb, lsb)
  super(input, interval, [var, msb, lsb])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
# File 'lib/idlc/ast.rb', line 2742

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  AryRangeAccessAst.new(
    input, interval,
    AstNode.from_h(yaml.fetch("array"), source_mapper),
    AstNode.from_h(yaml.fetch("range").fetch("msb"), source_mapper),
    AstNode.from_h(yaml.fetch("range").fetch("lsb"), source_mapper)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
# File 'lib/idlc/ast.rb', line 2653

def const_eval?(symtab)
  v = var
  var_type = begin
    v.type(symtab)
  rescue AstNode::TypeError, AstNode::InternalError
    nil
  end
  if var_type.is_a?(Type) && var_type.kind == :array && var_type.sub_type.is_a?(RegFileElementType) && var_type.qualifiers.include?(:global)
    false
  else
    v.const_eval?(symtab) && msb.const_eval?(symtab).const_eval? && lsb.const_eval?(symtab)
  end
end

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



173
174
175
# File 'lib/idlc/passes/gen_adoc.rb', line 173

def gen_adoc(indent = 0, indent_spaces: 2)
  "#{' ' * indent}#{var.gen_adoc(indent, indent_spaces:)}[#{msb.gen_adoc(0, indent_spaces:)}:#{lsb.gen_adoc(0, indent_spaces:)}]"
end

#lsbObject



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

def lsb = @children[2]

#msbObject



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

def msb = @children[1]

#prune(symtab, forced_type: nil) ⇒ Object



825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/idlc/passes/prune.rb', line 825

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
    AryRangeAccessAst.new(input, interval, var.prune(symtab), msb.prune(symtab), lsb.prune(symtab))
  end
end

#to_hObject



2731
2732
2733
2734
2735
2736
2737
2738
2739
# File 'lib/idlc/ast.rb', line 2731

def to_h = {
  "kind" => "array_range_access",
  "array" => var.to_h,
  "range" => {
    "lsb" => lsb.to_h,
    "msb" => msb.to_h
  },
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "#{var.to_idl}[#{msb.to_idl}:#{lsb.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:



2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
# File 'lib/idlc/ast.rb', line 2702

def type(symtab)
  value_result = value_try do
    msb_value = msb.value(symtab)
    lsb_value = lsb.value(symtab)
    range_size = msb_value - lsb_value + 1
    if var.type(symtab).known?
      return Type.new(:bits, width: range_size, qualifiers: [:known])
    else
      return Type.new(:bits, width: range_size)
    end
  end
  # don't know the width at compile time....assume the worst
  value_else(value_result) { var.type(symtab) }
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:



2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
# File 'lib/idlc/ast.rb', line 2677

def type_check(symtab, strict:)
  var.type_check(symtab, strict:)
  msb.type_check(symtab, strict:)
  lsb.type_check(symtab, strict:)

  type_error "Range operator only defined for integral types (found #{var.type(symtab)})" unless var.type(symtab).integral?

  type_error "Range MSB must be an integral type" unless msb.type(symtab).integral?
  type_error "Range LSB must be an integral type" unless lsb.type(symtab).integral?

  value_result = value_try do
    msb_value = msb.value(symtab)
    lsb_value = lsb.value(symtab)

    var_type = var.type(symtab)
    if strict && var_type.kind == :bits && var_type.width != :unknown && msb_value >= var_type.width
      type_error "Range too large for bits (msb = #{msb_value}, range size = #{var_type.width})"
    end

    range_size = msb_value - lsb_value + 1
    type_error "zero/negative range (#{msb_value}:#{lsb_value})" if range_size <= 0
  end  # OK, don't have to know
end

#value(symtab) ⇒ Object

Return the compile-time-known value of the node



2718
2719
2720
2721
2722
2723
2724
# File 'lib/idlc/ast.rb', line 2718

def value(symtab)
  msb_val = msb.value(symtab)
  lsb_val = lsb.value(symtab)
  var_val = T.cast(var.value(symtab), Integer)
  mask = (1 << (msb_val - lsb_val + 1)) - 1
  (var_val >> lsb_val) & mask
end

#varObject



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

def var = T.cast(@children[0], RvalueAst)