Class: Idl::AryElementAccessAst

Inherits:
AstNode
  • Object
show all
Includes:
Rvalue
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/prune.rb,
lib/idlc/passes/gen_adoc.rb,
lib/idlc/passes/find_src_registers.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, #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, index) ⇒ AryElementAccessAst

Returns a new instance of AryElementAccessAst.



2550
2551
2552
# File 'lib/idlc/ast.rb', line 2550

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

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
# File 'lib/idlc/ast.rb', line 2636

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  AryElementAccessAst.new(
    input, interval,
    AstNode.from_h(yaml.fetch("array"), source_mapper),
    AstNode.from_h(yaml.fetch("index"), source_mapper)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
# File 'lib/idlc/ast.rb', line 2534

def const_eval?(symtab)
  var_type = begin
    var.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
    var.const_eval?(symtab) && index.const_eval?(symtab)
  end
end

#find_src_registers(symtab) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/idlc/passes/find_src_registers.rb', line 81

def find_src_registers(symtab)
  value_result = value_try do
    var_type = var.type(symtab) rescue nil
    if var_type&.kind == :array && var_type.sub_type.is_a?(RegFileElementType) && var_type.qualifiers.include?(:global)
      rf_name = var_type.sub_type.name
      return [[rf_name, index.value(symtab)]]
    else
      return []
    end
  end
  value_else(value_result) do
    var_type = var.type(symtab) rescue nil
    if var_type&.kind == :array && var_type.sub_type.is_a?(RegFileElementType) && var_type.qualifiers.include?(:global)
      rf_name = var_type.sub_type.name
      if index.type(symtab).const?
        return [[rf_name, index.gen_cpp(symtab, 0)]]
      else
        raise ComplexRegDetermination
      end
    else
      return []
    end
  end
end

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



234
235
236
# File 'lib/idlc/passes/gen_adoc.rb', line 234

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

#indexObject



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

def index = @children[1]

#prune(symtab, forced_type: nil) ⇒ Object



808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/idlc/passes/prune.rb', line 808

def prune(symtab, forced_type: nil)
  value_result = value_try do
    v = value(symtab)
    if type(symtab).kind == :bits
      if type(symtab).width == :unknown
        value_error "Unknown width"
      end
    end
    return PruneHelpers.create_literal(symtab, v, type(symtab), forced_type: forced_type || type(symtab))
  end
  value_else(value_result) do
    AryElementAccessAst.new(input, interval, var.prune(symtab), index.prune(symtab))
  end
end

#to_hObject



2628
2629
2630
2631
2632
2633
# File 'lib/idlc/ast.rb', line 2628

def to_h = {
  "kind" => "array_access",
  "array" => var.to_h,
  "index" => index.to_h,
  "source" => source_yaml
}

#to_idlObject



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

def to_idl = "#{var.to_idl}[#{index.to_idl}]"

#type(symtab) ⇒ Object



2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
# File 'lib/idlc/ast.rb', line 2585

def type(symtab)
  var_type = var.type(symtab)
  if var_type.kind == :array
    var_type.sub_type
  elsif var_type.integral?
    if var_type.known?
      Bits1Type
    else
      PossiblyUnknownBits1Type
    end
  else
    internal_error "Bad ary element access"
  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:



2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
# File 'lib/idlc/ast.rb', line 2555

def type_check(symtab, strict:)
  var.type_check(symtab, strict:)
  index.type_check(symtab, strict:)

  type_error "Array index must be integral" unless index.type(symtab).integral?

  var_type = var.type(symtab)
  if var_type.kind == :array
    value_result = value_try do
      index_value = index.value(symtab)
      if var_type.width != :unknown
        type_error "Array index out of range" if index_value >= var_type.width
      end
    end # Ok, doesn't need to be known

  elsif var_type.integral?
    if var_type.kind == :bits
      value_result = value_try do
        index_value = index.value(symtab)
        if (var_type.width != :unknown) && (index_value >= var_type.width)
          type_error "Bits element index (#{index_value}) out of range (max #{var_type.width - 1}) in access '#{text_value}'"
        end
      end # OK, doesn need to be known
    end

  else
    type_error "Array element access can only be used with integral types and arrays"
  end
end

#value(symtab) ⇒ Object



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

def value(symtab)
  var_val = var.value(symtab)
  if var.type(symtab).integral?
    (var_val >> index.value(symtab)) & 1
  else
    var_type = begin
      var.type(symtab)
    rescue AstNode::TypeError, AstNode::InternalError
      nil
    end
    value_error "Register file registers are not compile-time-known" if var_type.is_a?(Type) && var_type.kind == :array && var_type.sub_type.is_a?(RegFileElementType) && var_type.qualifiers.include?(:global)

    # internal_error "Not an array" unless ary.type.kind == :array

    internal_error "Not an array (is a #{var_val.class.name})" unless var_val.is_a?(Array)

    idx = index.value(symtab)
    internal_error "Index out of range; make sure type_check is called" if idx >= var_val.size

    var_val[idx]
  end
end

#varObject



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

def var = @children[0]