Class: Idl::BitfieldDefinitionAst

Inherits:
AstNode
  • Object
show all
Includes:
Declaration
Defined in:
lib/idlc/ast.rb

Overview

represents a bitfield definition

# this will result in a BitfieldDefinitionAst
bitfield (64) Sv39PageTableEntry {
  N 63
  PBMT 62-61
  Reserved 60-54
  PPN2 53-28
  PPN1 27-19
  PPN0 18-10
  PPN 53-10 # in addition to the components, we define the entire PPN
  RSW  9-8
  D 7
  A 6
  G 5
  U 4
  X 3
  W 2
  R 1
  V 0
}

Defined Under Namespace

Classes: Memo

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 Declaration

#declaration?

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_adoc, #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, #prune, #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, name, size, fields) ⇒ BitfieldDefinitionAst

Returns a new instance of BitfieldDefinitionAst.



2244
2245
2246
2247
2248
2249
2250
2251
# File 'lib/idlc/ast.rb', line 2244

def initialize(input, interval, name, size, fields)
  super(input, interval, [name, size] + fields)

  @name = name
  @size = size
  @fields = fields
  @memo = Memo.new
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
# File 'lib/idlc/ast.rb', line 2344

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

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  BitfieldDefinitionAst.new(
    input, interval,
    T.cast(AstNode.from_h(yaml.fetch("name"), source_mapper), T.any(BuiltinTypeNameAst, UserTypeNameAst)),
    T.cast(AstNode.from_h(yaml.fetch("size"), source_mapper), IntLiteralAst),
    yaml.fetch("fields").map { |f| AstNode.from_h(f, source_mapper) }
  )
end

Instance Method Details

#add_symbol(symtab) ⇒ Object



2294
2295
2296
2297
2298
2299
2300
# File 'lib/idlc/ast.rb', line 2294

def add_symbol(symtab)
  internal_error "All Bitfields should be declared at global scope" unless symtab.levels == 1

  t = type(symtab)

  symtab.add!(name, t)
end

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


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

def const_eval?(symtab) = true

#element_namesObject



2261
2262
2263
2264
2265
# File 'lib/idlc/ast.rb', line 2261

def element_names
  return @memo.element_names unless @memo.element_names.nil?

  @memo.element_names = @fields.map(&:name)
end

#element_ranges(symtab) ⇒ Object



2270
2271
2272
2273
2274
# File 'lib/idlc/ast.rb', line 2270

def element_ranges(symtab)
  return @memo.element_ranges[symtab] unless @memo.element_ranges[symtab].nil?

  @memo.element_ranges[symtab] = @fields.map { |f| f.range(symtab) }
end

#nameObject



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

def name = @name.text_value

#size(symtab) ⇒ Object



2255
2256
2257
# File 'lib/idlc/ast.rb', line 2255

def size(symtab)
  @size.value(symtab)
end

#to_hObject



2335
2336
2337
2338
2339
2340
2341
# File 'lib/idlc/ast.rb', line 2335

def to_h = {
  "kind" => "bitfield_decl",
  "name" => @name.to_h,
  "size" => @size.to_h,
  "fields" => @fields.map(&:to_h),
  "source" => source_yaml
}

#to_idlObject



2325
2326
2327
2328
2329
2330
2331
2332
# File 'lib/idlc/ast.rb', line 2325

def to_idl
  idl = ["bitfield (#{@size.to_idl}) #{@name.to_idl} { "]
  @fields.each do |f|
    idl << f.to_idl
  end
  idl << "}"
  idl.join("\n")
end

#type(symtab) ⇒ Object



2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
# File 'lib/idlc/ast.rb', line 2304

def type(symtab)
  return @memo.type unless @memo.type.nil?

  @memo.type = BitfieldType.new(
    name,
    @size.value(symtab),
    element_names,
    element_ranges(symtab)
  )
end

#type_check(symtab, strict:) ⇒ Object



2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
# File 'lib/idlc/ast.rb', line 2278

def type_check(symtab, strict:)
  type_error "Cannot use reserved word '#{name}' as user-defined type name" if ReservedWords::RESERVED.include?(name)

  @size.type_check(symtab, strict:)
  @fields.each do |f|
    f.type_check(symtab, strict:)
    r = f.range(symtab)
    type_error "Field position (#{r}) is larger than the bitfield width (#{@size.value(symtab)} #{@size.text_value})" if r.first >= @size.value(symtab)
  end

  add_symbol(symtab)
  @name.type_check(symtab, strict:)
end

#value(_symtab) ⇒ Object



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

def value(_symtab) = raise AstNode::InternalError, "Bitfield definitions have no value"