Class: Rlang::Parser::DAta
- Includes:
- Log
- Defined in:
- lib/rlang/parser/data.rb
Constant Summary collapse
- TMPL =
'(data 0 (i32.const %{addr}) "%{value}") ;; %{comment}'
- @@label_table =
{}
- @@current_address =
0
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
-
#wtype ⇒ Object
readonly
Returns the value of attribute wtype.
Class Method Summary collapse
- .[](label) ⇒ Object
- .address=(address) ⇒ Object
-
.align(n) ⇒ Object
Align current address to closest multiple of n by higher value.
-
.append(label, value, wtype = WType::DEFAULT) ⇒ Object
Append a value to the DAta object identified by the label and return this DAta object object.
- .exist?(label) ⇒ Boolean
- .reset! ⇒ Object
-
.transpile(depth) ⇒ Object
Transpile data to WAT code in order of increasing address.
Instance Method Summary collapse
- #aligned? ⇒ Boolean
-
#append_value(value, wtype) ⇒ Object
Append a value to the DAta object and return this DAta object.
-
#initialize(label, value, wtype = WType::DEFAULT) ⇒ DAta
constructor
NOTE: new and append only takes individual DAta values not an array of values TODO: fix DAta.new and DAta.append not accepting an array of values.
Methods included from Log
included, logger, #logger, logger=
Constructor Details
#initialize(label, value, wtype = WType::DEFAULT) ⇒ DAta
NOTE: new and append only takes individual DAta values not an array of values TODO: fix DAta.new and DAta.append not accepting an array of values
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rlang/parser/data.rb', line 23 def initialize(label, value, wtype=WType::DEFAULT) raise "Data label '#{label}' already initialized" \ if self.class.exist? label logger.debug "@@current_address: #{@@current_address}" @label = label @wtype = wtype @address = @@current_address @@label_table[@label] = self @value = [] self.append_value(value, wtype) logger.debug "New Data[#{@label}] initialized with #{@value} at address #{@address} / new current address: #{@@current_address}" end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
18 19 20 |
# File 'lib/rlang/parser/data.rb', line 18 def address @address end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
18 19 20 |
# File 'lib/rlang/parser/data.rb', line 18 def label @label end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
18 19 20 |
# File 'lib/rlang/parser/data.rb', line 18 def value @value end |
#wtype ⇒ Object (readonly)
Returns the value of attribute wtype.
18 19 20 |
# File 'lib/rlang/parser/data.rb', line 18 def wtype @wtype end |
Class Method Details
.[](label) ⇒ Object
65 66 67 68 |
# File 'lib/rlang/parser/data.rb', line 65 def self.[](label) raise "Unknown data label '#{label}'" unless self.exist? label @@label_table[label].address end |
.address=(address) ⇒ Object
81 82 83 84 85 |
# File 'lib/rlang/parser/data.rb', line 81 def self.address=(address) logger.fatal "ERROR!! Cannot decrease current DAta address (was #{@@current_address}, got #{address}) " \ if address < @@current_address @@current_address = address end |
.align(n) ⇒ Object
Align current address to closest multiple of n by higher value
89 90 91 92 93 94 95 |
# File 'lib/rlang/parser/data.rb', line 89 def self.align(n) if (m = @@current_address % n) != 0 @@current_address = (@@current_address - m) + n end logger.debug "Aligning current address to #{@@current_address}" @@current_address end |
.append(label, value, wtype = WType::DEFAULT) ⇒ Object
Append a value to the DAta object identified by the label and return this DAta object object
72 73 74 75 76 77 78 79 |
# File 'lib/rlang/parser/data.rb', line 72 def self.append(label, value, wtype=WType::DEFAULT) logger.debug "appending #{value} to DAta[#{label}]" if self.exist? label @@label_table[label].append_value(value, wtype) else self.new(label, value, wtype) end end |
.exist?(label) ⇒ Boolean
61 62 63 |
# File 'lib/rlang/parser/data.rb', line 61 def self.exist?(label) @@label_table.has_key? label end |
.reset! ⇒ Object
36 37 38 39 |
# File 'lib/rlang/parser/data.rb', line 36 def self.reset! @@label_table = {} @@current_address = 0 end |
.transpile(depth) ⇒ Object
Transpile data to WAT code in order of increasing address
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rlang/parser/data.rb', line 99 def self.transpile(depth) indent = ' ' * depth * 2 output = [] @@label_table.sort_by {|s,d| d.address}.each do |s,d| logger.debug "Generating data #{d.inspect}" address = d.address d.value.each do |elt| if elt.is_a? String output << TMPL % {addr: address, value: elt.to_wasm, comment: s} address += d.value.size elsif elt.is_a? Integer output << TMPL % {addr: address, value: elt.to_little_endian(d.wtype.size), comment: "(#{elt} #{s})"} address += d.wtype.size elsif elt.is_a? DAta output << TMPL % {addr: address, value: elt.address.to_little_endian(d.wtype.size), comment: "(#{elt} #{s})"} address += d.wtype.size else raise "Unknown Data type: #{value.class}" end end end indent + output.join("\n #{indent}") end |
Instance Method Details
#aligned? ⇒ Boolean
57 58 59 |
# File 'lib/rlang/parser/data.rb', line 57 def aligned? (@@current_address % @wtype.size) == 0 end |
#append_value(value, wtype) ⇒ Object
Append a value to the DAta object and return this DAta object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rlang/parser/data.rb', line 43 def append_value(value, wtype) logger.warn "Data type #{@wtype} misaligned!!! (Data[:#{@label}] value #{value} at address #{@address}" \ unless self.aligned? @value << value if value.is_a? String @@current_address += value.length else @@current_address += @wtype.size end # Always make sure current address is aligned self.class.align(WType::DEFAULT.size) self end |