wardite 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +80 -1
  5. data/Rakefile +5 -0
  6. data/benchmark/README.md +500 -0
  7. data/benchmark/build_call_profile.rb +42 -0
  8. data/benchmark/call_cost_worker.rb +27 -0
  9. data/benchmark/compare.rb +93 -0
  10. data/benchmark/compare_call_types.rb +212 -0
  11. data/benchmark/compare_dispatch.rb +123 -0
  12. data/benchmark/compare_engines.rb +119 -0
  13. data/benchmark/compare_optimizations.rb +139 -0
  14. data/benchmark/engine_phases.rb +30 -0
  15. data/benchmark/figures/README.md +35 -0
  16. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  17. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  18. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  19. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  20. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  21. data/benchmark/float_m5.wat +42 -0
  22. data/benchmark/i32_call_arities.wat +89 -0
  23. data/benchmark/i32_calls.wat +174 -0
  24. data/benchmark/i32_memory.wat +58 -0
  25. data/benchmark/i32_mix.wat +80 -0
  26. data/benchmark/i64_m4.wat +55 -0
  27. data/benchmark/investigate_calls.rb +231 -0
  28. data/benchmark/investigate_generated_guards.rb +218 -0
  29. data/benchmark/m3_call_worker.rb +38 -0
  30. data/benchmark/measure_m1.rb +51 -0
  31. data/benchmark/measure_m2.rb +55 -0
  32. data/benchmark/measure_m3_calls.rb +89 -0
  33. data/benchmark/measure_m4.rb +134 -0
  34. data/benchmark/measure_m5_boundary.rb +41 -0
  35. data/benchmark/measure_wasi_write.rb +75 -0
  36. data/benchmark/optimization_worker.rb +23 -0
  37. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  38. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  39. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  40. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  41. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  42. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  43. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  44. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  45. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  46. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  47. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  48. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  49. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  50. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  51. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  52. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  53. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  54. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  55. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  56. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  57. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  58. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  59. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  60. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  61. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  62. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  63. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  64. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  65. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  66. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  67. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  68. data/benchmark/summarize_call_profile.py +54 -0
  69. data/benchmark/validate_m1_eh.rb +34 -0
  70. data/benchmark/validate_m2.rb +119 -0
  71. data/benchmark/validate_m3.rb +44 -0
  72. data/benchmark/validate_m4.rb +95 -0
  73. data/benchmark/validate_m5.rb +108 -0
  74. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  75. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  76. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  77. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  78. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  79. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  80. data/docs/efflorescence-call-arities.md +115 -0
  81. data/docs/efflorescence-call-investigation.md +155 -0
  82. data/docs/efflorescence-call-transfer.md +128 -0
  83. data/docs/efflorescence-call-types.md +102 -0
  84. data/docs/efflorescence-coverage-ja.md +351 -0
  85. data/docs/efflorescence-generated-c-guards.md +405 -0
  86. data/docs/efflorescence-inlining.md +165 -0
  87. data/docs/efflorescence-ja.md +155 -0
  88. data/docs/efflorescence-m1.md +160 -0
  89. data/docs/efflorescence-m2.md +233 -0
  90. data/docs/efflorescence-m3.md +131 -0
  91. data/docs/efflorescence-m4.md +88 -0
  92. data/docs/efflorescence-m5.md +82 -0
  93. data/docs/efflorescence-m6.md +95 -0
  94. data/docs/efflorescence-operand-decoding.md +105 -0
  95. data/docs/efflorescence-phase2-ja.md +225 -0
  96. data/docs/efflorescence-phase2.md +240 -0
  97. data/docs/efflorescence-register-initialization.md +75 -0
  98. data/docs/efflorescence.md +144 -0
  99. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  100. data/lib/wardite/alu_f32.generated.rb +1 -0
  101. data/lib/wardite/alu_f64.generated.rb +1 -0
  102. data/lib/wardite/alu_i32.generated.rb +14 -9
  103. data/lib/wardite/alu_i64.generated.rb +18 -20
  104. data/lib/wardite/cli/cli.rb +61 -28
  105. data/lib/wardite/const.rb +1 -0
  106. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  107. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  108. data/lib/wardite/efflorescence/inspection.rb +165 -0
  109. data/lib/wardite/efflorescence/ir.rb +1151 -0
  110. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  111. data/lib/wardite/efflorescence/register_validation.rb +290 -0
  112. data/lib/wardite/efflorescence/store_access.rb +271 -0
  113. data/lib/wardite/efflorescence/translator.rb +859 -0
  114. data/lib/wardite/efflorescence/vm.rb +1006 -0
  115. data/lib/wardite/efflorescence/wasi.rb +108 -0
  116. data/lib/wardite/efflorescence.rb +11 -0
  117. data/lib/wardite/errors.rb +20 -0
  118. data/lib/wardite/float_bits.rb +46 -0
  119. data/lib/wardite/instruction.rb +35 -4
  120. data/lib/wardite/leb128.rb +33 -12
  121. data/lib/wardite/load.rb +203 -138
  122. data/lib/wardite/value.rb +169 -175
  123. data/lib/wardite/version.rb +1 -1
  124. data/lib/wardite/wasi/consts.rb +1 -0
  125. data/lib/wardite/wasm_module.rb +6 -3
  126. data/lib/wardite.rb +184 -70
  127. data/rbs_collection.lock.yaml +36 -96
  128. data/scripts/gen_alu.rb +66 -10
  129. data/sig/generated/wardite/cli/cli.rbs +8 -6
  130. data/sig/generated/wardite/const.rbs +2 -0
  131. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  132. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  133. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  134. data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
  135. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  136. data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
  137. data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
  138. data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
  139. data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
  140. data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
  141. data/sig/generated/wardite/errors.rbs +12 -0
  142. data/sig/generated/wardite/float_bits.rbs +30 -0
  143. data/sig/generated/wardite/instruction.rbs +22 -1
  144. data/sig/generated/wardite/leb128.rbs +3 -1
  145. data/sig/generated/wardite/load.rbs +29 -4
  146. data/sig/generated/wardite/value.rbs +70 -11
  147. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  148. data/sig/generated/wardite.rbs +49 -12
  149. data/sig/vernier.rbs +2 -2
  150. data/spinel/README.md +30 -0
  151. data/spinel/bin/wardite.rb +6 -0
  152. data/spinel/spin.toml +7 -0
  153. metadata +126 -3
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ # Verify definite assignment once, after optimization and before execution.
7
+ # This makes stale temporary slots harmless when a frame is reused. Keep
8
+ # states per basic block, rather than per instruction, to bound their size.
9
+ class RegisterValidation
10
+ class Block
11
+ attr_reader :first #: Integer
12
+ attr_reader :limit #: Integer
13
+ attr_reader :defined #: Array[Integer]
14
+
15
+ # @rbs first: Integer
16
+ # @rbs limit: Integer
17
+ # @rbs register_count: Integer
18
+ def initialize(first, limit, register_count)
19
+ @first = first
20
+ @limit = limit
21
+ @defined = Array.new(register_count, 0)
22
+ @reached = false
23
+ end
24
+
25
+ # Incoming facts only lose definitions after the first reachable edge.
26
+ # A backedge cannot establish a definition missing on initial entry.
27
+ # @rbs incoming: Array[Integer]
28
+ # @rbs return: bool
29
+ def merge(incoming)
30
+ unless @reached
31
+ @defined = incoming.dup
32
+ @reached = true
33
+ return true
34
+ end
35
+ changed = false
36
+ index = 0
37
+ while index < @defined.size
38
+ if @defined[index] == 1 && incoming[index] == 0
39
+ @defined[index] = 0
40
+ changed = true
41
+ end
42
+ index += 1
43
+ end
44
+ changed
45
+ end
46
+ end
47
+
48
+ # @rbs function: Wardite::Efflorescence::Function
49
+ # @rbs return: void
50
+ # @rbs ir: Wardite::Efflorescence::IR?
51
+ # @rbs tags: Array[Wardite::Efflorescence::Tag]?
52
+ def validate(function, ir = nil, tags: nil)
53
+ instructions = function.instructions
54
+ raise TranslationError, "Efflorescence: empty MIR function" if instructions.empty?
55
+ validate_types(function, ir, tags)
56
+ analysis = Analysis.new(function)
57
+ blocks = [] #: Array[Wardite::Efflorescence::RegisterValidation::Block]
58
+ block_for_pc = [] #: Array[Integer]
59
+ first = 0
60
+ instructions.size.times do |pc|
61
+ if pc > 0 && analysis.leaders[pc] == 1
62
+ blocks << Block.new(first, pc, function.register_count)
63
+ first = pc
64
+ end
65
+ block_for_pc << blocks.size
66
+ end
67
+ blocks << Block.new(first, instructions.size, function.register_count)
68
+
69
+ constants = Array.new(function.register_count, 0)
70
+ defined = Array.new(function.register_count, 0)
71
+ index = 0
72
+ while index < function.parameter_count + function.local_count
73
+ defined[index] = 1
74
+ index += 1
75
+ end
76
+ function.constant_registers.each do |register|
77
+ constants[register] = 1
78
+ defined[register] = 1
79
+ end
80
+ blocks.fetch(0).merge(defined)
81
+ pending = [] #: Array[Integer]
82
+ pending << 0
83
+ until pending.empty?
84
+ block_index = pending.pop || raise(TranslationError, "missing MIR block")
85
+ block = blocks.fetch(block_index)
86
+ defined = block.defined.dup
87
+ pc = block.first
88
+ while pc < block.limit
89
+ instruction = instructions.fetch(pc)
90
+ function.reads(instruction).each do |register|
91
+ unless register >= 0 && register < defined.size && defined[register] == 1
92
+ raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: r#{register} read before definition"
93
+ end
94
+ end
95
+ # Exceptional writes are edge-local. In particular a CALL result
96
+ # is absent here, even though its normal successor defines it.
97
+ analysis.flow.exceptional.fetch(pc).each do |clause|
98
+ edge_defined = defined.dup
99
+ clause.destinations.each { |register| edge_defined[register] = 1 }
100
+ target = block_for_pc.fetch(clause.target_pc)
101
+ pending << target if blocks.fetch(target).merge(edge_defined)
102
+ end
103
+ destination = Opcode.destination(instruction)
104
+ if destination >= 0
105
+ if destination >= defined.size || constants[destination] == 1
106
+ raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: invalid register destination r#{destination}"
107
+ end
108
+ defined[destination] = 1
109
+ end
110
+ pc += 1
111
+ end
112
+ analysis.flow.normal.fetch(block.limit - 1).each do |pc|
113
+ target = block_for_pc.fetch(pc)
114
+ pending << target if blocks.fetch(target).merge(defined)
115
+ end
116
+ end
117
+ end
118
+ private
119
+
120
+ # Validate all registers, including unreachable instructions and table-only
121
+ # operands. Numeric registers are checked before flat execution.
122
+ # @rbs function: Wardite::Efflorescence::Function
123
+ # @rbs return: void
124
+ # @rbs ir: Wardite::Efflorescence::IR?
125
+ # @rbs tags: Array[Wardite::Efflorescence::Tag]?
126
+ def validate_types(function, ir, tags)
127
+ function.instructions.each do |instruction|
128
+ opcode = instruction[0]
129
+ if Opcode.memory_site?(opcode)
130
+ index = instruction[1]
131
+ unless index >= 0 && index < function.memory_sites.size
132
+ raise TranslationError, "invalid MIR memory site index"
133
+ end
134
+ site = function.memory_site(index)
135
+ unless site.sources.size == 3 && site.memory_index == 0 && site.source_memory_index == 0 &&
136
+ (opcode != Opcode::MEMORY_INIT || site.data_index >= 0)
137
+ raise TranslationError, "invalid MIR memory site operands"
138
+ end
139
+ end
140
+ if Opcode.dynamic_call?(opcode)
141
+ site = function.dynamic_site(instruction[2])
142
+ destination = instruction[1]
143
+ if opcode == Opcode::CALL_HOST
144
+ unless site.host_index >= 0 && site.table_index == -1 && instruction[3] == 0
145
+ raise TranslationError, "invalid MIR host call operands"
146
+ end
147
+ elsif site.host_index != -1 || site.table_index != 0
148
+ raise TranslationError, "invalid MIR indirect call operands"
149
+ end
150
+ unless site.arguments.size == site.parameter_types.size &&
151
+ site.parameter_types.all? { |type| FunctionEntry.numeric_type?(type) } &&
152
+ (site.result_types.empty? ? destination == -1 : site.result_types.size == 1 &&
153
+ FunctionEntry.numeric_type?(site.result_types.fetch(0)) && destination >= 0)
154
+ raise TranslationError, "invalid MIR dynamic call signature"
155
+ end
156
+ if ir && opcode == Opcode::CALL_HOST
157
+ host = ir.hosts[site.host_index]
158
+ unless host && host.entry.parameter_types == site.parameter_types && host.entry.result_types == site.result_types
159
+ raise TranslationError, "MIR host call signature mismatch"
160
+ end
161
+ end
162
+ check_register(function, instruction[3], :i32) if opcode == Opcode::CALL_INDIRECT
163
+ site.arguments.each_with_index { |register, i| check_register(function, register, site.parameter_types.fetch(i)) }
164
+ check_result(function, destination, site.result_types)
165
+ next
166
+ end
167
+ reads = function.reads(instruction)
168
+ destination = Opcode.destination(instruction)
169
+ if opcode > Opcode::CALL_INDIRECT
170
+ validate_extended_instruction(function, instruction, reads, destination)
171
+ next
172
+ end
173
+ case opcode
174
+ when Opcode::THROW
175
+ site = function.throw_sites.fetch(instruction[1])
176
+ types = payload_types(tags, site.tag_index, reads.size)
177
+ reads.each_with_index { |register, i| check_register(function, register, types.fetch(i)) }
178
+ when Opcode::COPY
179
+ type = function.register_types[destination] if destination >= 0
180
+ unless type && FunctionEntry.numeric_type?(type)
181
+ raise TranslationError, "invalid MIR numeric copy destination r#{destination}"
182
+ end
183
+ check_register(function, instruction[2], type)
184
+ when Opcode::RETURN
185
+ check_result(function, instruction[1], function.result_types)
186
+ when Opcode::CALL
187
+ if ir
188
+ entry = ir.entry_at(instruction[2])
189
+ unless entry.kind == :defined && reads.size == entry.parameter_types.size
190
+ raise TranslationError, "invalid MIR direct call signature"
191
+ end
192
+ reads.each_with_index { |register, i| check_register(function, register, entry.parameter_types.fetch(i)) }
193
+ check_result(function, destination, entry.result_types)
194
+ else
195
+ reads.each { |register| check_register(function, register, :i32) }
196
+ check_register(function, destination, :i32) if destination >= 0
197
+ end
198
+ else
199
+ expected = opcode == Opcode::COPY_REF || opcode == Opcode::THROW_REF ? :exnref : :i32
200
+ reads.each { |register| check_register(function, register, expected) }
201
+ check_register(function, destination, expected) if destination >= 0
202
+ end
203
+ end
204
+ function.handlers.each do |handler|
205
+ handler.clauses.each do |clause|
206
+ types = payload_types(tags, clause.tag_index, clause.payload_registers.size)
207
+ clause.payload_registers.each_with_index do |register, i|
208
+ check_destination(function, register, types.fetch(i))
209
+ end
210
+ check_destination(function, clause.reference_register, :exnref) if clause.reference_register >= 0
211
+ end
212
+ end
213
+ end
214
+
215
+ # @rbs function: Wardite::Efflorescence::Function
216
+ # @rbs instruction: Wardite::Efflorescence::Instruction
217
+ # @rbs reads: Array[Integer]
218
+ # @rbs destination: Integer
219
+ # @rbs return: void
220
+ def validate_extended_instruction(function, instruction, reads, destination)
221
+ opcode = instruction[0]
222
+ types = Opcode.extended_alu_types(opcode)
223
+ if types
224
+ check_register(function, destination, types.fetch(0))
225
+ reads.each_with_index { |register, i| check_register(function, register, types.fetch(i + 1)) }
226
+ elsif (type = Opcode::EXTENDED_LOAD_TYPES[opcode])
227
+ check_register(function, destination, type)
228
+ check_register(function, instruction[2], :i32)
229
+ elsif (type = Opcode::EXTENDED_STORE_TYPES[opcode])
230
+ check_register(function, instruction[1], type)
231
+ check_register(function, instruction[2], :i32)
232
+ elsif (type = Opcode::EXTENDED_GLOBAL_GET_TYPES[opcode])
233
+ check_register(function, destination, type)
234
+ elsif (type = Opcode::EXTENDED_GLOBAL_SET_TYPES[opcode])
235
+ check_register(function, instruction[2], type)
236
+ else
237
+ raise TranslationError, "unknown extended MIR opcode #{opcode}"
238
+ end
239
+ end
240
+
241
+ # @rbs tags: Array[Wardite::Efflorescence::Tag]?
242
+ # @rbs index: Integer
243
+ # @rbs size: Integer
244
+ # @rbs return: Array[Symbol]
245
+ def payload_types(tags, index, size)
246
+ return Array.new(size, :i32) unless tags
247
+
248
+ tag = tags[index] if index >= 0
249
+ types = tag ? tag.parameter_types : [] #: Array[Symbol]
250
+ if (index >= 0 && !tag) || size != types.size
251
+ raise TranslationError, "invalid MIR exception payload"
252
+ end
253
+ types
254
+ end
255
+
256
+ # @rbs function: Wardite::Efflorescence::Function
257
+ # @rbs register: Integer
258
+ # @rbs types: Array[Symbol]
259
+ # @rbs return: void
260
+ def check_result(function, register, types)
261
+ if types.empty?
262
+ raise TranslationError, "invalid MIR void result" unless register == -1
263
+ else
264
+ check_register(function, register, types.fetch(0))
265
+ end
266
+ end
267
+
268
+ # @rbs function: Wardite::Efflorescence::Function
269
+ # @rbs register: Integer
270
+ # @rbs type: Symbol
271
+ # @rbs return: void
272
+ def check_register(function, register, type)
273
+ unless register >= 0 && function.register_types[register] == type
274
+ raise TranslationError, "invalid MIR register type/index r#{register}"
275
+ end
276
+ end
277
+
278
+ # @rbs function: Wardite::Efflorescence::Function
279
+ # @rbs register: Integer
280
+ # @rbs type: Symbol
281
+ # @rbs return: void
282
+ def check_destination(function, register, type)
283
+ unless register >= 0 && function.register_types[register] == type && !function.constant_registers.include?(register)
284
+ raise TranslationError, "invalid catch destination r#{register}"
285
+ end
286
+ end
287
+
288
+ end
289
+ end
290
+ end
@@ -0,0 +1,271 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ # Checked byte operations live off the integer dispatch path. Store remains
7
+ # the owner of all state; never retain a Memory#data buffer across a call.
8
+ class StoreAccess
9
+ PAGE_SIZE = 65536
10
+ MAX_PAGES = 65536
11
+
12
+ # @rbs start: Integer
13
+ # @rbs length: Integer
14
+ # @rbs size: Integer
15
+ # @rbs return: void
16
+ def self.check_range(start, length, size)
17
+ unless start >= 0 && length >= 0 && start <= size && length <= size - start
18
+ raise Wardite::EvalError, "out of bounds memory access"
19
+ end
20
+ end
21
+
22
+ # @rbs store: Wardite::Store
23
+ def initialize(store)
24
+ @store = store
25
+ end
26
+
27
+ # @rbs index: Integer
28
+ # @rbs return: Wardite::Global
29
+ def global_at(index)
30
+ @store.globals[index] || raise(Wardite::EvalError, "invalid global index")
31
+ end
32
+
33
+ # @rbs index: Integer
34
+ # @rbs return: Integer
35
+ def global_get(index)
36
+ value = global_at(index).value
37
+ raise Wardite::EvalError, "invalid i32 global value" unless value.is_a?(Wardite::I32)
38
+ value.value & 0xffffffff
39
+ end
40
+
41
+ # @rbs index: Integer
42
+ # @rbs return: Integer
43
+ def global_get_i64(index)
44
+ value = global_at(index).value
45
+ raise Wardite::EvalError, "invalid i64 global value" unless value.is_a?(Wardite::I64)
46
+ value.value_s
47
+ end
48
+
49
+ # @rbs index: Integer
50
+ # @rbs type: Symbol
51
+ # @rbs return: Integer
52
+ def global_get_float(index, type)
53
+ value = global_at(index).value
54
+ if type == :f32 && value.is_a?(Wardite::F32)
55
+ value.raw_bits
56
+ elsif type == :f64 && value.is_a?(Wardite::F64)
57
+ value.raw_bits
58
+ else
59
+ raise Wardite::EvalError, "invalid #{type} global value"
60
+ end
61
+ end
62
+
63
+ # @rbs index: Integer
64
+ # @rbs type: Symbol
65
+ # @rbs value: Integer
66
+ # @rbs return: void
67
+ def global_set_float(index, type, value)
68
+ global_at(index).value = type == :f32 ? Wardite::F32.from_bits(value) : Wardite::F64.from_bits(value)
69
+ end
70
+
71
+ # Boxing is confined to the shared Store boundary. Existing register
72
+ # snapshots contain independent integers, never a mutable Global object.
73
+ # @rbs index: Integer
74
+ # @rbs value: Integer
75
+ # @rbs return: void
76
+ def global_set(index, value)
77
+ global_at(index).value = Wardite::I32.new(value)
78
+ end
79
+
80
+ # @rbs index: Integer
81
+ # @rbs value: Integer
82
+ # @rbs return: void
83
+ def global_set_i64(index, value)
84
+ global_at(index).value = Wardite::I64(value)
85
+ end
86
+
87
+ # @rbs address: Integer
88
+ # @rbs offset: Integer
89
+ # @rbs width: Integer
90
+ # @rbs signed: bool
91
+ # @rbs return: Integer
92
+ def load(address, offset, width, signed)
93
+ data = memory.data
94
+ start = address + offset
95
+ StoreAccess.check_range(start, width, data.bytesize)
96
+ value = read_word(data, start, width)
97
+ if signed && value >= (1 << (width * 8 - 1))
98
+ value -= 1 << (width * 8)
99
+ end
100
+ value & 0xffffffff
101
+ end
102
+
103
+ # @rbs address: Integer
104
+ # @rbs offset: Integer
105
+ # @rbs width: Integer
106
+ # @rbs signed: bool
107
+ # @rbs return: Integer
108
+ def load_i64(address, offset, width, signed)
109
+ data = memory.data
110
+ start = address + offset
111
+ StoreAccess.check_range(start, width, data.bytesize)
112
+ if width == 8
113
+ I64Arithmetic.join(read_word(data, start, 4), read_word(data, start + 4, 4))
114
+ else
115
+ value = read_word(data, start, width)
116
+ signed ? I64Arithmetic.extend(value, width * 8) : value
117
+ end
118
+ end
119
+
120
+ # Check the whole access before the first write. Effective addresses are
121
+ # wide integers; the static offset never wraps back into memory32.
122
+ # @rbs address: Integer
123
+ # @rbs offset: Integer
124
+ # @rbs width: Integer
125
+ # @rbs value: Integer
126
+ # @rbs return: void
127
+ def store(address, offset, width, value)
128
+ data = memory.data
129
+ start = address + offset
130
+ StoreAccess.check_range(start, width, data.bytesize)
131
+ byte = 0
132
+ while byte < width
133
+ data.setbyte(start + byte, (value >> (byte * 8)) & 255)
134
+ byte += 1
135
+ end
136
+ end
137
+
138
+ # @rbs return: Integer
139
+ def size
140
+ memory.current
141
+ end
142
+
143
+ # Only the buffer preparation boundary handles allocation failure. Ruby
144
+ # implementation errors and traps must propagate rather than look like a
145
+ # successful Wasm instruction returning -1.
146
+ # @rbs delta: Integer
147
+ # @rbs return: Integer
148
+ def grow(delta)
149
+ target = memory
150
+ previous = target.current
151
+ declared = target.max
152
+ limit = declared && declared < MAX_PAGES ? declared : MAX_PAGES
153
+ return 0xffffffff if delta < 0 || previous > limit || delta > limit - previous
154
+ return previous if delta == 0
155
+ begin
156
+ data = grown_buffer(target, delta)
157
+ rescue NoMemoryError
158
+ return 0xffffffff
159
+ end
160
+ target.data = data
161
+ target.current = previous + delta
162
+ previous
163
+ end
164
+
165
+ # Kept separate so tests can inject allocation failure without requesting
166
+ # gigabytes or depending on the process's available resources.
167
+ # @rbs target: Wardite::Memory
168
+ # @rbs delta: Integer
169
+ # @rbs return: String
170
+ def grown_buffer(target, delta)
171
+ target.data + ("\0".b * (delta * PAGE_SIZE))
172
+ end
173
+
174
+ # Resolve table operands off the hot dispatch path. In particular do not
175
+ # widen its b/c integer-word locals with nullable array reads.
176
+ # @rbs opcode: Integer
177
+ # @rbs site: Wardite::Efflorescence::MemorySite
178
+ # @rbs registers: Array[Integer]
179
+ # @rbs return: void
180
+ def bulk(opcode, site, registers)
181
+ destination = registers.fetch(site.sources.fetch(0))
182
+ source = registers.fetch(site.sources.fetch(1))
183
+ length = registers.fetch(site.sources.fetch(2))
184
+ case opcode
185
+ when Opcode::MEMORY_INIT then init(site.data_index, destination, source, length)
186
+ when Opcode::MEMORY_COPY then copy(destination, source, length)
187
+ when Opcode::MEMORY_FILL then fill(destination, source, length)
188
+ else raise Wardite::EvalError, "invalid bulk memory opcode"
189
+ end
190
+ end
191
+
192
+ # @rbs destination: Integer
193
+ # @rbs source: Integer
194
+ # @rbs length: Integer
195
+ # @rbs return: void
196
+ def copy(destination, source, length)
197
+ data = memory.data
198
+ StoreAccess.check_range(destination, length, data.bytesize)
199
+ StoreAccess.check_range(source, length, data.bytesize)
200
+ # Walk backward when the destination starts inside the source, so an
201
+ # overlapping copy reads each byte before overwriting it.
202
+ if destination > source
203
+ index = length - 1
204
+ while index >= 0
205
+ data.setbyte(destination + index, data.getbyte(source + index) || raise(Wardite::EvalError, "missing checked memory byte"))
206
+ index -= 1
207
+ end
208
+ else
209
+ index = 0
210
+ while index < length
211
+ data.setbyte(destination + index, data.getbyte(source + index) || raise(Wardite::EvalError, "missing checked memory byte"))
212
+ index += 1
213
+ end
214
+ end
215
+ end
216
+
217
+ # @rbs destination: Integer
218
+ # @rbs value: Integer
219
+ # @rbs length: Integer
220
+ # @rbs return: void
221
+ def fill(destination, value, length)
222
+ data = memory.data
223
+ StoreAccess.check_range(destination, length, data.bytesize)
224
+ byte = value & 255
225
+ index = 0
226
+ while index < length
227
+ data.setbyte(destination + index, byte)
228
+ index += 1
229
+ end
230
+ end
231
+
232
+ # @rbs index: Integer
233
+ # @rbs destination: Integer
234
+ # @rbs source: Integer
235
+ # @rbs length: Integer
236
+ # @rbs return: void
237
+ def init(index, destination, source, length)
238
+ data = memory.data
239
+ segment = @store.data_segments[index] || raise(Wardite::EvalError, "invalid data segment")
240
+ StoreAccess.check_range(destination, length, data.bytesize)
241
+ StoreAccess.check_range(source, length, segment.bytesize)
242
+ index = 0
243
+ while index < length
244
+ data.setbyte(destination + index, segment.getbyte(source + index) || raise(Wardite::EvalError, "missing checked segment byte"))
245
+ index += 1
246
+ end
247
+ end
248
+
249
+ # Read up to four checked little-endian bytes as an unsigned limb.
250
+ # @rbs data: String
251
+ # @rbs start: Integer
252
+ # @rbs width: Integer
253
+ # @rbs return: Integer
254
+ def read_word(data, start, width)
255
+ value = 0
256
+ byte = 0
257
+ while byte < width
258
+ part = data.getbyte(start + byte) || raise(Wardite::EvalError, "missing checked memory byte")
259
+ value |= part << (byte * 8)
260
+ byte += 1
261
+ end
262
+ value
263
+ end
264
+
265
+ # @rbs return: Wardite::Memory
266
+ def memory
267
+ @store.memories[0] || raise(Wardite::EvalError, "missing memory 0")
268
+ end
269
+ end
270
+ end
271
+ end