Class: Idl::SymbolTable

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cfg_arch) ⇒ 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
# File 'lib/idl/symbol_table.rb', line 92

def initialize(cfg_arch)
  raise "Must provide cfg_arch" if cfg_arch.nil?
  # TODO: XXX: Put this check back in when replaced by Design class.
  #            See https://github.com/riscv-software-src/riscv-unified-db/pull/371
  #raise "The cfg_arch must be a ConfiguredArchitecture but is a #{cfg_arch.class}" unless (cfg_arch.is_a?(ConfiguredArchitecture) || cfg_arch.is_a?(MockConfiguredArchitecture))

  @cfg_arch = cfg_arch
  @mxlen = cfg_arch.unconfigured? ? nil : cfg_arch.mxlen
  @callstack = [nil]
  @scopes = [{
    "X" => Var.new(
      "X",
      Type.new(:array, sub_type: XregType.new(@mxlen.nil? ? :unknown : @mxlen), width: 32, qualifiers: [:global])
    ),
    "XReg" => XregType.new(@mxlen.nil? ? :unknown : @mxlen),
    "Boolean" => Type.new(:boolean),
    "true" => Var.new(
      "true",
      Type.new(:boolean),
      true
    ),
    "false" => Var.new(
      "false",
      Type.new(:boolean),
      false
    )

  }]
  cfg_arch.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
  cfg_arch.params_without_value.each do |param|
    if param.exts.size == 1
      add!(param.name, Var.new(param.name, param.idl_type.clone.make_const))
    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
end

Instance Attribute Details

#mxlenInteger (readonly)

Returns 32 or 64, the XLEN in M-mode.

Returns:

  • (Integer)

    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

Parameters:

  • name (#to_s)

    Symbol name

  • var (Object)

    Symbol object (usually a Var or a Type)



279
280
281
# File 'lib/idl/symbol_table.rb', line 279

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

Parameters:

  • name (#to_s)

    Symbol name

  • var (Object)

    Symbol object (usually a Var or a Type)

Raises:

  • (DuplicationSymError)

    if ‘name’ is already in the symbol table



288
289
290
291
292
# File 'lib/idl/symbol_table.rb', line 288

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



295
296
297
298
299
300
301
# File 'lib/idl/symbol_table.rb', line 295

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



304
305
306
307
308
309
310
# File 'lib/idl/symbol_table.rb', line 304

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.

Returns:

  • (Boolean)

    true if the symbol table is at the global scope



327
328
329
# File 'lib/idl/symbol_table.rb', line 327

def at_global_scope?
  @scopes.size == 1
end

#callstackObject



213
214
215
# File 'lib/idl/symbol_table.rb', line 213

def callstack
  @callstack.reverse.map { |ast| ast.nil? ? "" : "#{ast.input_file}:#{ast.lineno}" }.join("\n")
end

#cfg_archObject



78
# File 'lib/idl/symbol_table.rb', line 78

def cfg_arch = @cfg_arch

#deep_clone(clone_values: false, freeze_global: true) ⇒ SymbolTable

Returns a deep clone of this SymbolTable.

Returns:



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/idl/symbol_table.rb', line 372

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_freezeObject

do a deep freeze to protect the sym table and all its entries from modification



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
# File 'lib/idl/symbol_table.rb', line 156

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, @cfg_arch.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(:@cfg_arch, @cfg_arch)
    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

Parameters:

  • single_scope (Boolean) (defaults to: false)

    If true, stop searching more scope as soon as there are matches

Yield Parameters:

  • obj (Object)

    A object stored in the symbol table

Yield Returns:

  • (Boolean)

    Whether or not the object is the one you are looking for

Returns:

  • (Array<Object>)

    All matches

Raises:

  • (ArgumentError)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/idl/symbol_table.rb', line 259

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’

Returns:

  • (Object)

    A symbol named ‘name’, or nil if not found



229
230
231
232
233
234
235
# File 'lib/idl/symbol_table.rb', line 229

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

Raises:

  • (ArgumentError)


237
238
239
240
241
242
243
244
245
246
# File 'lib/idl/symbol_table.rb', line 237

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.

Returns:

  • (Object)

    the symbol named ‘name’ from global scope, or nil if not found



249
250
251
# File 'lib/idl/symbol_table.rb', line 249

def get_global(name)
  get_from(name, 1)
end

#global_cloneSymbolTable

Returns a mutable clone of the global scope of this SymbolTable.

Returns:

  • (SymbolTable)

    a mutable clone of the global scope of this SymbolTable



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/idl/symbol_table.rb', line 332

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(:@cfg_arch, @cfg_arch)
    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

#hashObject



86
87
88
89
90
# File 'lib/idl/symbol_table.rb', line 86

def hash
  return @frozen_hash unless @frozen_hash.nil?

  [@scopes.hash, @cfg_arch.hash].hash
end

#in_use?Boolean

Returns:

  • (Boolean)


369
# File 'lib/idl/symbol_table.rb', line 369

def in_use? = @in_use

#inspectString

Returns inspection string.

Returns:

  • (String)

    inspection string



185
186
187
# File 'lib/idl/symbol_table.rb', line 185

def inspect
  "SymbolTable[#{@cfg_arch.name}]#{frozen? ? ' (frozen)' : ''}"
end

#key?(name) ⇒ Boolean

Returns whether or not any symbol ‘name’ is defined at any level in the symbol table.

Returns:

  • (Boolean)

    whether or not any symbol ‘name’ is defined at any level in the symbol table



218
219
220
# File 'lib/idl/symbol_table.rb', line 218

def key?(name)
  @scopes.each { |s| return true if s.key?(name) }
end

#keys_prettyObject



222
223
224
# File 'lib/idl/symbol_table.rb', line 222

def keys_pretty
  @scopes.map { |s| s.map { |k, v| v.is_a?(Var) && v.template_val? ? "#{k} (template)" : k }}
end

#levelsInteger

Returns Number of scopes on the symbol table (global at 1).

Returns:

  • (Integer)

    Number of scopes on the symbol table (global at 1)



313
314
315
# File 'lib/idl/symbol_table.rb', line 313

def levels
  @scopes.size
end

#popObject

pops the top of the scope stack



203
204
205
206
207
208
209
210
211
# File 'lib/idl/symbol_table.rb', line 203

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

pretty-print the symbol table contents



318
319
320
321
322
323
324
# File 'lib/idl/symbol_table.rb', line 318

def print
  @scopes.each do |s|
    s.each do |name, obj|
      puts "#{name} #{obj}"
    end
  end
end

#push(ast) ⇒ SymbolTable

pushes a new scope

Returns:



191
192
193
194
195
196
197
198
199
200
# File 'lib/idl/symbol_table.rb', line 191

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

#releaseObject



359
360
361
362
363
364
365
366
367
# File 'lib/idl/symbol_table.rb', line 359

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