Class: DAta

Inherits:
Object show all
Defined in:
lib/simul/classes/data.rb

Overview

Don’t use class name Data because it’s a deprecated Ruby class and it generates warning at runtime

Constant Summary collapse

@@symbol_table =
{}
@@current_address =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, value, wtype = Type::I32) ⇒ DAta

Returns a new instance of DAta.



17
18
19
20
21
22
23
24
25
26
# File 'lib/simul/classes/data.rb', line 17

def initialize(symbol, value, wtype=Type::I32)
  @symbol = symbol
  @value = (value.is_a?(Array) ? value : [value])
  @wtype = wtype
  @address = @@current_address
  @@symbol_table[@symbol] = self
  size = self.update(@symbol, @value, @wtype)
  Log.debug "size after update #{size}"
  @@current_address += size
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



15
16
17
# File 'lib/simul/classes/data.rb', line 15

def address
  @address
end

#symbolObject (readonly)

Returns the value of attribute symbol.



15
16
17
# File 'lib/simul/classes/data.rb', line 15

def symbol
  @symbol
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/simul/classes/data.rb', line 15

def value
  @value
end

#wtypeObject (readonly)

Returns the value of attribute wtype.



15
16
17
# File 'lib/simul/classes/data.rb', line 15

def wtype
  @wtype
end

Class Method Details

.[](symbol) ⇒ Object



53
54
55
56
# File 'lib/simul/classes/data.rb', line 53

def self.[](symbol)
  raise "Unknown data symbol '#{symbol}'" unless self.has_symbol? symbol
  @@symbol_table[symbol].address
end

.[]=(symbol, value) ⇒ Object

Store string or integer in memory (note: integer are treated as I32)



60
61
62
63
64
65
66
# File 'lib/simul/classes/data.rb', line 60

def self.[]=(symbol, value)
  if self.has_symbol? symbol
    self.update(symbol, value)
  else
    self.new(symbol, value)
  end
end

.address=(address) ⇒ Object



68
69
70
# File 'lib/simul/classes/data.rb', line 68

def self.address=(address)
  @@current_address = address
end

.align(n) ⇒ Object

Align current address to closest multiple of n by higher value



74
75
76
77
78
# File 'lib/simul/classes/data.rb', line 74

def self.align(n)
  if (m = @@current_address % n) != 0
    @@current_address = (@@current_address - m) + n
  end
end

.has_symbol?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/simul/classes/data.rb', line 49

def self.has_symbol?(symbol)
  @@symbol_table.has_key? symbol
end

Instance Method Details

#update(symbol, value, wtype) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simul/classes/data.rb', line 28

def update(symbol, value, wtype)
  Log.debug "updating data value #{value} at #{address} / #{wtype.size}"
  original_address = self.address
  new_address = address
  value.each do |elt|
    if elt.is_a? String
      elt.each_byte do |b| 
        Memory.store(new_address, b, 8)
        new_address += 1
      end
    elsif elt.is_a? Integer
      Memory.store(new_address, elt, wtype.size*8)
      new_address += wtype.size
      Log.debug "new _address : #{new_address}"
    else
      raise "Unknown Data type: #{value.class}"
    end
  end
  new_address - original_address
end