Class: Rlang::Parser::Global

Inherits:
Object
  • Object
show all
Defined in:
lib/rlang/parser/global.rb

Constant Summary collapse

@@globals =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, wtype = WType::DEFAULT, value = 0, mutable = true) ⇒ Global

Returns a new instance of Global.



15
16
17
18
19
20
21
# File 'lib/rlang/parser/global.rb', line 15

def initialize(name, wtype=WType::DEFAULT, value=0, mutable=true)
  @name = name
  @wtype = wtype
  @mutable = mutable
  @value = value
  @@globals << self
end

Instance Attribute Details

#mutableObject

Returns the value of attribute mutable.



13
14
15
# File 'lib/rlang/parser/global.rb', line 13

def mutable
  @mutable
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/rlang/parser/global.rb', line 13

def name
  @name
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/rlang/parser/global.rb', line 13

def value
  @value
end

#wtypeObject

Returns the value of attribute wtype.



13
14
15
# File 'lib/rlang/parser/global.rb', line 13

def wtype
  @wtype
end

Class Method Details

.find(name) ⇒ Object



23
24
25
# File 'lib/rlang/parser/global.rb', line 23

def self.find(name)
  @@globals.find {|g| g.name == name}
end

.reset!Object



27
28
29
# File 'lib/rlang/parser/global.rb', line 27

def self.reset!
  @@globals = []
end

.transpile(depth) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rlang/parser/global.rb', line 55

def self.transpile(depth)
  # TODO : we should probably do that in a less "hardcoded" way
  # Adjust the Heap base address to start after the static DAta
  # section
  g_heap = Global.find(:$HEAP)
  g_heap.value = [DAta.align(8), g_heap.value].max if g_heap
  
  # Go generate code now
  indent = ' ' * depth * 2
  output = []
  @@globals.each do |g|
    if g.mutable?
      output << '(global %{name} (mut %{type}) (%{type}.const %{value}))' \
                % {name: g.wasm_name, type: g.wtype.wasm_type, value: g.value}
    else
      output << '(global %{name} %{type} (%{type}.const %{value}))' \
                % {name: g.wasm_name, type: g.wtype.wasm_type, value: g.value}
    end
  end
  indent + output.join("\n#{indent}")
end

Instance Method Details

#export!Object



47
48
49
# File 'lib/rlang/parser/global.rb', line 47

def export!
  Export.new(self)
end

#export_nameObject



43
44
45
# File 'lib/rlang/parser/global.rb', line 43

def export_name
  @name
end

#export_wasm_codeObject



51
52
53
# File 'lib/rlang/parser/global.rb', line 51

def export_wasm_code
  '(export  "%s" (global %s (mut %s)))' % [self.export_name, self.wasm_name, self.wtype.wasm_type]
end

#mutable?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rlang/parser/global.rb', line 31

def mutable?
  @mutable
end

#wasm_nameObject



35
36
37
# File 'lib/rlang/parser/global.rb', line 35

def wasm_name
  @name
end

#wasm_typeObject



39
40
41
# File 'lib/rlang/parser/global.rb', line 39

def wasm_type
  @wtype.wasm_type
end