Class: Idl::SymbolTable
- Inherits:
-
Object
- Object
- Idl::SymbolTable
- Defined in:
- lib/idl/symbol_table.rb
Overview
scoped symbol table holding known symbols at a current point in parsing
Defined Under Namespace
Classes: DuplicateSymError
Instance Attribute Summary collapse
-
#archdef ⇒ Object
readonly
Returns the value of attribute archdef.
-
#mxlen ⇒ Integer
readonly
32 or 64, the XLEN in M-mode.
Instance Method Summary collapse
-
#add(name, var) ⇒ Object
add a new symbol at the outermost scope.
-
#add!(name, var) ⇒ Object
add a new symbol at the outermost scope, unless that symbol is already defined.
-
#add_above!(name, var) ⇒ Object
add to the scope above the tail, and make sure name is unique at that scope.
-
#add_at!(level, name, var) ⇒ Object
add to the scope at level, and make sure name is unique at that scope.
-
#at_global_scope? ⇒ Boolean
True if the symbol table is at the global scope.
- #callstack ⇒ Object
-
#deep_clone(clone_values: false, freeze_global: true) ⇒ SymbolTable
A deep clone of this SymbolTable.
-
#deep_freeze ⇒ Object
do a deep freeze to protect the sym table and all its entries from modification.
-
#find_all(single_scope: false) {|obj| ... } ⇒ Array<Object>
searches the symbol table scope-by-scope to find all entries for which the block returns true.
-
#get(name) ⇒ Object
searches the symbol table scope-by-scope to find ‘name’.
- #get_from(name, level) ⇒ Object
-
#get_global(name) ⇒ Object
The symbol named ‘name’ from global scope, or nil if not found.
-
#global_clone ⇒ SymbolTable
A mutable clone of the global scope of this SymbolTable.
- #hash ⇒ Object
- #in_use? ⇒ Boolean
-
#initialize(arch_def, effective_xlen = nil) ⇒ SymbolTable
constructor
A new instance of SymbolTable.
-
#inspect ⇒ String
Inspection string.
-
#key?(name) ⇒ Boolean
Whether or not any symbol ‘name’ is defined at any level in the symbol table.
- #keys_pretty ⇒ Object
-
#levels ⇒ Integer
Number of scopes on the symbol table (global at 1).
-
#pop ⇒ Object
pops the top of the scope stack.
-
#print ⇒ Object
pretty-print the symbol table contents.
-
#push(ast) ⇒ SymbolTable
pushes a new scope.
- #release ⇒ Object
Constructor Details
#initialize(arch_def, effective_xlen = nil) ⇒ SymbolTable
Returns a new instance of SymbolTable.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/idl/symbol_table.rb', line 92 def initialize(arch_def, effective_xlen = nil) @archdef = arch_def if arch_def.fully_configured? raise "effective_xlen should not be set when symbol table is given a fully-configured ArchDef" unless effective_xlen.nil? else raise "effective_xlen should be set when symbol table is given an ArchDef" if effective_xlen.nil? && arch_def.mxlen.nil? end @mxlen = effective_xlen.nil? ? arch_def.mxlen : effective_xlen @callstack = [nil] @scopes = [{ "X" => Var.new( "X", Type.new(:array, sub_type: XregType.new(@mxlen), width: 32, qualifiers: [:global]) ), "XReg" => XregType.new(@mxlen), "Boolean" => Type.new(:boolean), "true" => Var.new( "true", Type.new(:boolean), true ), "false" => Var.new( "false", Type.new(:boolean), false ) }] arch_def.params_with_value.each do |param_with_value| type = Type.from_json_schema(param_with_value.schema).make_const if type.kind == :array && type.width == :unknown type = Type.new(:array, width: param_with_value.value.length, sub_type: type.sub_type, qualifiers: [:const]) end # could already be present... existing_sym = get(param_with_value.name) if existing_sym.nil? add!(param_with_value.name, Var.new(param_with_value.name, type, param_with_value.value)) else unless existing_sym.type.equal_to?(type) && existing_sym.value == param_with_value.value raise DuplicateSymError, "Definition error: Param #{param.name} is defined by multiple extensions and is not the same definition in each" end end end # now add all parameters, even those not implemented arch_def.params_without_value.each do |param| if param.exts.size == 1 if param.name == "XLEN" # special case: we actually do know XLEN add!(param.name, Var.new(param.name, param.idl_type.clone.make_const, @mxlen)) else add!(param.name, Var.new(param.name, param.idl_type.clone.make_const)) end else # could already be present... existing_sym = get(param.name) if existing_sym.nil? add!(param.name, Var.new(param.name, param.idl_type.clone.make_const)) else unless existing_sym.type.equal_to?(param.idl_type) raise "Definition error: Param #{param.name} is defined by multiple extensions and is not the same definition in each" end end end end # add the builtin extensions # add!( # "ExtensionName", # EnumerationType.new( # "ExtensionName", # arch_def.extensions.map(&:name), # Array.new(arch_def.extensions.size) { |i| i + 1 } # ) # ) # add!( # "ExceptionCode", # EnumerationType.new( # "ExceptionCode", # arch_def.exception_codes.map(&:var), # arch_def.exception_codes.map(&:num) # ) # ) # add!( # "InterruptCode", # EnumerationType.new( # "InterruptCode", # arch_def.interrupt_codes.map(&:var), # arch_def.interrupt_codes.map(&:num) # ) # ) end |
Instance Attribute Details
#archdef ⇒ Object (readonly)
Returns the value of attribute archdef.
78 79 80 |
# File 'lib/idl/symbol_table.rb', line 78 def archdef @archdef end |
#mxlen ⇒ Integer (readonly)
Returns 32 or 64, the XLEN in M-mode.
81 82 83 |
# File 'lib/idl/symbol_table.rb', line 81 def mxlen @mxlen end |
Instance Method Details
#add(name, var) ⇒ Object
add a new symbol at the outermost scope
309 310 311 |
# File 'lib/idl/symbol_table.rb', line 309 def add(name, var) @scopes.last[name] = var end |
#add!(name, var) ⇒ Object
add a new symbol at the outermost scope, unless that symbol is already defined
318 319 320 321 322 |
# File 'lib/idl/symbol_table.rb', line 318 def add!(name, var) raise DuplicateSymError, "Symbol #{name} already defined as #{get(name)}" unless @scopes.select { |h| h.key? name }.empty? @scopes.last[name] = var end |
#add_above!(name, var) ⇒ Object
add to the scope above the tail, and make sure name is unique at that scope
325 326 327 328 329 330 331 |
# File 'lib/idl/symbol_table.rb', line 325 def add_above!(name, var) raise "There is only one scope" if @scopes.size <= 1 raise "Symbol #{name} already defined" unless @scopes[0..-2].select { |h| h.key? name }.empty? @scopes[-2][name] = var end |
#add_at!(level, name, var) ⇒ Object
add to the scope at level, and make sure name is unique at that scope
334 335 336 337 338 339 340 |
# File 'lib/idl/symbol_table.rb', line 334 def add_at!(level, name, var) raise "Level #{level} is too large #{@scopes.size}" if level >= @scopes.size raise "Symbol #{name} already defined" unless @scopes[0...level].select { |h| h.key? name }.empty? @scopes[level][name] = var end |
#at_global_scope? ⇒ Boolean
Returns true if the symbol table is at the global scope.
357 358 359 |
# File 'lib/idl/symbol_table.rb', line 357 def at_global_scope? @scopes.size == 1 end |
#callstack ⇒ Object
243 244 245 |
# File 'lib/idl/symbol_table.rb', line 243 def callstack @callstack.reverse.map { |ast| ast.nil? ? "" : "#{ast.input_file}:#{ast.lineno}" }.join("\n") end |
#deep_clone(clone_values: false, freeze_global: true) ⇒ SymbolTable
Returns a deep clone of this SymbolTable.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/idl/symbol_table.rb', line 402 def deep_clone(clone_values: false, freeze_global: true) raise "don't do this" unless freeze_global # globals are frozen, so we can just return a shallow clone # if we are in global scope if levels == 1 copy = dup copy.instance_variable_set(:@scopes, copy.instance_variable_get(:@scopes).dup) copy.instance_variable_set(:@callstack, copy.instance_variable_get(:@callstack).dup) return copy end copy = dup # back up the table to global scope copy.instance_variable_set(:@callstack, @callstack.dup) copy.instance_variable_set(:@scopes, []) c_scopes = copy.instance_variable_get(:@scopes) c_scopes.push(@scopes[0]) @scopes[1..].each do |scope| c_scopes << {} scope.each do |k, v| if clone_values c_scopes.last[k] = v.dup else c_scopes.last[k] = v end end end copy end |
#deep_freeze ⇒ Object
do a deep freeze to protect the sym table and all its entries from modification
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/idl/symbol_table.rb', line 186 def deep_freeze @scopes.each do |k, v| k.freeze v.freeze end @scopes.freeze # set frozen_hash so that we can quickly compare symtabs @frozen_hash = [@scopes.hash, @archdef.hash].hash # set up the global clone that be used as a mutable table @global_clone_pool = [] 5.times do copy = SymbolTable.allocate copy.instance_variable_set(:@scopes, [@scopes[0]]) copy.instance_variable_set(:@callstack, [@callstack[0]]) copy.instance_variable_set(:@archdef, @archdef) copy.instance_variable_set(:@mxlen, @mxlen) copy.instance_variable_set(:@global_clone_pool, @global_clone_pool) copy.instance_variable_set(:@in_use, false) @global_clone_pool << copy end freeze self end |
#find_all(single_scope: false) {|obj| ... } ⇒ Array<Object>
searches the symbol table scope-by-scope to find all entries for which the block returns true
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/idl/symbol_table.rb', line 289 def find_all(single_scope: false, &block) raise ArgumentError, "Block needed" unless block_given? raise ArgumentError, "Find block takes one argument" unless block.arity == 1 matches = [] @scopes.reverse_each do |s| s.each_value do |v| matches << v if yield v end break if single_scope && !matches.empty? end matches end |
#get(name) ⇒ Object
searches the symbol table scope-by-scope to find ‘name’
259 260 261 262 263 264 265 |
# File 'lib/idl/symbol_table.rb', line 259 def get(name) @scopes.reverse_each do |s| result = s.fetch(name, nil) return result unless result.nil? end nil end |
#get_from(name, level) ⇒ Object
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/idl/symbol_table.rb', line 267 def get_from(name, level) raise ArgumentError, "level must be positive" unless level.positive? raise "There is no level #{level}" unless level < levels @scopes[0..level - 1].reverse_each do |s| return s[name] if s.key?(name) end nil end |
#get_global(name) ⇒ Object
Returns the symbol named ‘name’ from global scope, or nil if not found.
279 280 281 |
# File 'lib/idl/symbol_table.rb', line 279 def get_global(name) get_from(name, 1) end |
#global_clone ⇒ SymbolTable
Returns a mutable clone of the global scope of this SymbolTable.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/idl/symbol_table.rb', line 362 def global_clone # raise "symtab isn't frozen" if @global_clone.nil? # raise "global clone isn't at global scope" unless @global_clone.at_global_scope? @global_clone_pool.each do |symtab| unless symtab.in_use? symtab.instance_variable_set(:@in_use, true) return symtab end end # need more! warn "Allocating more SymbolTables" 5.times do copy = SymbolTable.allocate copy.instance_variable_set(:@scopes, [@scopes[0]]) copy.instance_variable_set(:@callstack, [@callstack[0]]) copy.instance_variable_set(:@archdef, @archdef) copy.instance_variable_set(:@mxlen, @mxlen) copy.instance_variable_set(:@global_clone_pool, @global_clone_pool) copy.instance_variable_set(:@in_use, false) @global_clone_pool << copy end global_clone end |
#hash ⇒ Object
86 87 88 89 90 |
# File 'lib/idl/symbol_table.rb', line 86 def hash return @frozen_hash unless @frozen_hash.nil? [@scopes.hash, @archdef.hash].hash end |
#in_use? ⇒ Boolean
399 |
# File 'lib/idl/symbol_table.rb', line 399 def in_use? = @in_use |
#inspect ⇒ String
Returns inspection string.
215 216 217 |
# File 'lib/idl/symbol_table.rb', line 215 def inspect "SymbolTable[#{@archdef.name}]#{frozen? ? ' (frozen)' : ''}" end |
#key?(name) ⇒ Boolean
Returns whether or not any symbol ‘name’ is defined at any level in the symbol table.
248 249 250 |
# File 'lib/idl/symbol_table.rb', line 248 def key?(name) @scopes.each { |s| return true if s.key?(name) } end |
#keys_pretty ⇒ Object
252 253 254 |
# File 'lib/idl/symbol_table.rb', line 252 def keys_pretty @scopes.map { |s| s.map { |k, v| v.is_a?(Var) && v.template_val? ? "#{k} (template)" : k }} end |
#levels ⇒ Integer
Returns Number of scopes on the symbol table (global at 1).
343 344 345 |
# File 'lib/idl/symbol_table.rb', line 343 def levels @scopes.size end |
#pop ⇒ Object
pops the top of the scope stack
233 234 235 236 237 238 239 240 241 |
# File 'lib/idl/symbol_table.rb', line 233 def pop # puts "pop #{caller[0]}" # puts " from #{@scope_caller.pop}" raise "Error: popping the symbol table would remove global scope" if @scopes.size == 1 raise "?" unless @scopes.size == @callstack.size @scopes.pop @callstack.pop end |
#print ⇒ Object
pretty-print the symbol table contents
348 349 350 351 352 353 354 |
# File 'lib/idl/symbol_table.rb', line 348 def print @scopes.each do |s| s.each do |name, obj| puts "#{name} #{obj}" end end end |
#push(ast) ⇒ SymbolTable
pushes a new scope
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/idl/symbol_table.rb', line 221 def push(ast) # puts "push #{caller[0]}" # @scope_caller ||= [] # @scope_caller.push caller[0] raise "#{@scopes.size} #{@callstack.size}" unless @scopes.size == @callstack.size @scopes << {} @callstack << ast @frozen_hash = nil self end |
#release ⇒ Object
389 390 391 392 393 394 395 396 397 |
# File 'lib/idl/symbol_table.rb', line 389 def release pop while levels > 1 raise "Clone isn't back in global scope" unless at_global_scope? raise "You are calling release on the frozen SymbolTable" if frozen? raise "??" if @in_use.nil? raise "Double release detected" unless @in_use @in_use = false end |