wardite 0.9.0 → 1.1.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 (184) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +107 -2
  5. data/Rakefile +5 -0
  6. data/Steepfile +1 -0
  7. data/assets/wardite.svg +12 -0
  8. data/benchmark/README.md +500 -0
  9. data/benchmark/build_call_profile.rb +42 -0
  10. data/benchmark/call_cost_worker.rb +27 -0
  11. data/benchmark/compare.rb +93 -0
  12. data/benchmark/compare_call_types.rb +212 -0
  13. data/benchmark/compare_dispatch.rb +123 -0
  14. data/benchmark/compare_engines.rb +119 -0
  15. data/benchmark/compare_optimizations.rb +139 -0
  16. data/benchmark/engine_phases.rb +30 -0
  17. data/benchmark/figures/README.md +35 -0
  18. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  19. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  20. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  21. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  22. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  23. data/benchmark/float_m5.wat +42 -0
  24. data/benchmark/i32_call_arities.wat +89 -0
  25. data/benchmark/i32_calls.wat +174 -0
  26. data/benchmark/i32_memory.wat +58 -0
  27. data/benchmark/i32_mix.wat +80 -0
  28. data/benchmark/i64_m4.wat +55 -0
  29. data/benchmark/investigate_calls.rb +231 -0
  30. data/benchmark/investigate_generated_guards.rb +218 -0
  31. data/benchmark/m3_call_worker.rb +38 -0
  32. data/benchmark/measure_m1.rb +51 -0
  33. data/benchmark/measure_m2.rb +55 -0
  34. data/benchmark/measure_m3_calls.rb +89 -0
  35. data/benchmark/measure_m4.rb +134 -0
  36. data/benchmark/measure_m5_boundary.rb +41 -0
  37. data/benchmark/measure_mir_cache_verify.rb +58 -0
  38. data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
  39. data/benchmark/measure_ruby_wasm_startup.rb +43 -0
  40. data/benchmark/measure_wasi_write.rb +75 -0
  41. data/benchmark/optimization_worker.rb +23 -0
  42. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  43. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  44. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  45. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  46. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  47. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  48. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  49. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  50. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  51. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  52. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  53. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  54. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  55. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  56. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  57. data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
  58. data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
  59. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  60. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  61. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  62. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  63. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  64. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  65. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  66. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  67. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  68. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  69. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  70. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  71. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  72. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  73. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  74. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  75. data/benchmark/summarize_call_profile.py +54 -0
  76. data/benchmark/validate_m1_eh.rb +34 -0
  77. data/benchmark/validate_m2.rb +119 -0
  78. data/benchmark/validate_m3.rb +44 -0
  79. data/benchmark/validate_m4.rb +95 -0
  80. data/benchmark/validate_m5.rb +108 -0
  81. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  82. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  83. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  84. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  85. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  86. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  87. data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
  88. data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
  89. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
  90. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
  91. data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
  92. data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
  93. data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
  94. data/docs/efflorescence-call-arities.md +115 -0
  95. data/docs/efflorescence-call-investigation.md +155 -0
  96. data/docs/efflorescence-call-transfer.md +128 -0
  97. data/docs/efflorescence-call-types.md +102 -0
  98. data/docs/efflorescence-coverage-ja.md +351 -0
  99. data/docs/efflorescence-generated-c-guards.md +410 -0
  100. data/docs/efflorescence-inlining.md +165 -0
  101. data/docs/efflorescence-ja.md +155 -0
  102. data/docs/efflorescence-m1.md +160 -0
  103. data/docs/efflorescence-m2.md +233 -0
  104. data/docs/efflorescence-m3.md +131 -0
  105. data/docs/efflorescence-m4.md +88 -0
  106. data/docs/efflorescence-m5.md +82 -0
  107. data/docs/efflorescence-m6.md +95 -0
  108. data/docs/efflorescence-mir2-cache.md +123 -0
  109. data/docs/efflorescence-operand-decoding.md +105 -0
  110. data/docs/efflorescence-phase2-ja.md +225 -0
  111. data/docs/efflorescence-phase2.md +240 -0
  112. data/docs/efflorescence-register-initialization.md +75 -0
  113. data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
  114. data/docs/efflorescence-spinel-linear-memory.md +72 -0
  115. data/docs/efflorescence-startup-snapshot.md +60 -0
  116. data/docs/efflorescence-unboxed-globals.md +61 -0
  117. data/docs/efflorescence.md +144 -0
  118. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  119. data/lib/wardite/alu_f32.generated.rb +11 -2
  120. data/lib/wardite/alu_f64.generated.rb +11 -2
  121. data/lib/wardite/alu_i32.generated.rb +54 -17
  122. data/lib/wardite/alu_i64.generated.rb +73 -31
  123. data/lib/wardite/cli/cli.rb +112 -29
  124. data/lib/wardite/const.rb +1 -0
  125. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  126. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  127. data/lib/wardite/efflorescence/inspection.rb +165 -0
  128. data/lib/wardite/efflorescence/ir.rb +1164 -0
  129. data/lib/wardite/efflorescence/mir_cache.rb +657 -0
  130. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  131. data/lib/wardite/efflorescence/register_validation.rb +362 -0
  132. data/lib/wardite/efflorescence/snapshot.rb +229 -0
  133. data/lib/wardite/efflorescence/store_access.rb +366 -0
  134. data/lib/wardite/efflorescence/translator.rb +866 -0
  135. data/lib/wardite/efflorescence/vm.rb +1170 -0
  136. data/lib/wardite/efflorescence/wasi.rb +52 -0
  137. data/lib/wardite/efflorescence.rb +17 -0
  138. data/lib/wardite/errors.rb +20 -0
  139. data/lib/wardite/float_bits.rb +46 -0
  140. data/lib/wardite/instruction.rb +35 -4
  141. data/lib/wardite/leb128.rb +33 -12
  142. data/lib/wardite/load.rb +292 -143
  143. data/lib/wardite/value.rb +169 -175
  144. data/lib/wardite/version.rb +1 -1
  145. data/lib/wardite/wasi/consts.rb +1 -0
  146. data/lib/wardite/wasi/spinel.rb +41 -0
  147. data/lib/wardite/wasi.rb +483 -268
  148. data/lib/wardite/wasm_module.rb +6 -3
  149. data/lib/wardite.rb +360 -98
  150. data/rbs_collection.lock.yaml +36 -96
  151. data/scripts/gen_alu.rb +121 -21
  152. data/scripts/verify_ruby_wasm.rb +70 -0
  153. data/sig/generated/wardite/cli/cli.rbs +23 -6
  154. data/sig/generated/wardite/const.rbs +2 -0
  155. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  156. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  157. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  158. data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
  159. data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
  160. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  161. data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
  162. data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
  163. data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
  164. data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
  165. data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
  166. data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
  167. data/sig/generated/wardite/errors.rbs +12 -0
  168. data/sig/generated/wardite/float_bits.rbs +30 -0
  169. data/sig/generated/wardite/instruction.rbs +22 -1
  170. data/sig/generated/wardite/leb128.rbs +3 -1
  171. data/sig/generated/wardite/load.rbs +59 -7
  172. data/sig/generated/wardite/value.rbs +70 -11
  173. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  174. data/sig/generated/wardite/wasi/spinel.rbs +15 -0
  175. data/sig/generated/wardite/wasi.rbs +111 -4
  176. data/sig/generated/wardite.rbs +114 -13
  177. data/sig/mir_cache_stdlib.rbs +5 -0
  178. data/sig/vernier.rbs +2 -2
  179. data/spinel/README.md +30 -0
  180. data/spinel/bin/wardite.rb +6 -0
  181. data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
  182. data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
  183. data/spinel/spin.toml +7 -0
  184. metadata +154 -3
@@ -0,0 +1,1170 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ class VM
7
+ attr_reader :instance #: Wardite::Instance
8
+ attr_reader :ir #: Wardite::Efflorescence::IR
9
+ attr_reader :metrics #: Wardite::Efflorescence::Metrics
10
+ attr_reader :mir_cache_hit #: bool
11
+ attr_reader :mir_cache #: Wardite::Efflorescence::MIRCache?
12
+ # Records a startup snapshot before the first WASI call outside its allowlist.
13
+ attr_accessor :snapshot #: Wardite::Efflorescence::Snapshot?
14
+
15
+ # @rbs @semantic_ir: Wardite::Efflorescence::IR?
16
+ # @rbs @optimized_ir: Wardite::Efflorescence::IR?
17
+ # @rbs @global_bits: Array[Integer]
18
+
19
+ # @rbs instance: Wardite::Instance
20
+ # @rbs disabled_optimizations: Array[Symbol]
21
+ # @rbs profile: bool
22
+ # @rbs mir_cache: Wardite::Efflorescence::MIRCache?
23
+ # @rbs cached_functions: Array[Function]?
24
+ # @rbs cache_checked: bool
25
+ def initialize(instance, disabled_optimizations: [], profile: false, mir_cache: nil, cached_functions: nil, cache_checked: false)
26
+ @instance = instance
27
+ @store_access = StoreAccess.new(instance.store)
28
+ @global_bits = []
29
+ @has_globals = !instance.store.globals.empty?
30
+ @function_indices = {} #: Hash[Integer, Integer]
31
+ # Retain the original objects so IDs stay valid even if a host edits
32
+ # Store.funcs. Spinel cannot use an identity Hash for these objects.
33
+ @indirect_functions = [] #: Array[Wardite::WasmFunction | Wardite::ExternalFunction]
34
+ unless instance.store.tables.empty?
35
+ @indirect_functions = instance.store.funcs.dup
36
+ @indirect_functions.each_with_index { |function, index| @function_indices[function.object_id] = index }
37
+ end
38
+ @configuration = Configuration.new(disabled_optimizations)
39
+ @reuse_frames = @configuration.enabled?(:frames)
40
+ @profiling = profile
41
+ @metrics = Metrics.new
42
+ @semantic_ir = @optimized_ir = nil
43
+ @mir_cache = mir_cache
44
+ @snapshot = nil
45
+ @active_frames = [] #: Array[Wardite::Efflorescence::Frame]
46
+ @depth = 0
47
+ translator = Translator.new(instance)
48
+ linked = translator.link
49
+ if mir_cache && !profile && !cache_checked
50
+ cached_functions = mir_cache.read(instance)
51
+ end
52
+ @mir_cache_hit = !cached_functions.nil?
53
+ @ir = if cached_functions
54
+ IR.new(cached_functions, entries: linked.entries, hosts: linked.hosts)
55
+ else
56
+ build_mir(translator, linked, profile)
57
+ end
58
+ if mir_cache && !profile && !@mir_cache_hit
59
+ mir_cache.write(instance, @ir)
60
+ end
61
+ start = instance.start_section
62
+ if start
63
+ entry = @ir.entry_at(start.func_index)
64
+ unless entry.parameter_types.empty? && entry.result_types.empty?
65
+ raise Wardite::LoadError, "start function must have no parameters or results"
66
+ end
67
+ execute(entry, [])
68
+ end
69
+ end
70
+
71
+ # A caller explicitly inspecting a cache hit can still request the
72
+ # intermediate forms. Execution continues to use the restored MIR2.
73
+ # @rbs return: Wardite::Efflorescence::IR
74
+ def semantic_ir
75
+ restore_inspection_ir unless @semantic_ir
76
+ @semantic_ir || raise(Wardite::EvalError, "missing semantic MIR")
77
+ end
78
+
79
+ # @rbs return: Wardite::Efflorescence::IR
80
+ def optimized_ir
81
+ restore_inspection_ir unless @optimized_ir
82
+ @optimized_ir || raise(Wardite::EvalError, "missing optimized MIR")
83
+ end
84
+
85
+ # @rbs name: String|Symbol
86
+ # @rbs return: bool
87
+ def callable?(name)
88
+ export = @instance.export_section.exports[name.to_s]
89
+ !!export && export.kind == 0
90
+ end
91
+
92
+ # Compatible with Runtime.call: boxing happens once, at the Ruby boundary.
93
+ # @rbs name: String|Symbol
94
+ # @rbs arguments: Array[Object]
95
+ # @rbs return: Wardite::wasmValue?
96
+ def call(name, arguments)
97
+ entry = export_entry(name)
98
+ result = execute_public(entry, arguments)
99
+ return nil if result.nil?
100
+ box_result(entry.result_types.fetch(0), result)
101
+ end
102
+
103
+ # Integers retain their register representation. Floats cross as Ruby Float.
104
+ # @rbs name: String|Symbol
105
+ # @rbs arguments: Array[Object]
106
+ # @rbs return: (Integer | Float)?
107
+ def call_unboxed(name, arguments)
108
+ entry = export_entry(name)
109
+ result = execute_public(entry, arguments)
110
+ return nil if result.nil?
111
+ unbox_result(entry.result_types.fetch(0), result)
112
+ end
113
+
114
+ # @rbs name: Symbol
115
+ # @rbs *arguments: Object
116
+ # @rbs return: Wardite::wasmValue?
117
+ def method_missing(name, *arguments)
118
+ callable?(name) ? call(name, arguments) : super
119
+ end
120
+
121
+ # @rbs name: String|Symbol
122
+ # @rbs include_private: bool
123
+ # @rbs return: bool
124
+ def respond_to_missing?(name, include_private = false)
125
+ callable?(name) || super
126
+ end
127
+
128
+ # @rbs return: String
129
+ def dump_mir
130
+ inspection = Inspection.new
131
+ "Semantic MIR (#{inspection.statistics(semantic_ir)})\n#{inspection.disassemble(semantic_ir)}\n" \
132
+ "Optimized MIR (#{inspection.statistics(optimized_ir)})\n#{inspection.disassemble(optimized_ir)}"
133
+ end
134
+
135
+ # Continue a public call restored from a startup snapshot. Each frame
136
+ # resumes at its saved pc: the host call for the top, a return for others.
137
+ # @rbs saved: Array[Wardite::Efflorescence::SavedFrame]
138
+ # @rbs return: void
139
+ def resume(saved)
140
+ pools = new_pools
141
+ frames = [] #: Array[Wardite::Efflorescence::Frame]
142
+ saved.each do |state|
143
+ frame = pool_at(pools, state.pool_index).acquire(state.return_register)
144
+ registers = frame.registers
145
+ state.registers.each_with_index { |value, i| registers[i] = value }
146
+ state.handlers.each { |handler| frame.handlers << handler }
147
+ frame.pc = state.pc
148
+ frames << frame
149
+ end
150
+ run_frames(pools, frames, 0)
151
+ nil
152
+ end
153
+
154
+ private
155
+
156
+ # @rbs translator: Wardite::Efflorescence::Translator
157
+ # @rbs linked: Wardite::Efflorescence::IR
158
+ # @rbs profile: bool
159
+ # @rbs return: Wardite::Efflorescence::IR
160
+ def build_mir(translator, linked, profile)
161
+ semantic = translator.translate(linked)
162
+ optimized = Optimizer.new(@configuration).optimize(semantic)
163
+ optimized.functions.each { |function| RegisterValidation.new.validate(function, optimized, tags: @instance.tags) }
164
+ ir = profile ? Instrumentation.new.instrument(optimized) : optimized
165
+ Lowerer.new.compile(ir)
166
+ @semantic_ir = semantic
167
+ @optimized_ir = optimized
168
+ ir
169
+ end
170
+
171
+ # @rbs return: void
172
+ def restore_inspection_ir
173
+ linked = IR.new([], entries: @ir.entries, hosts: @ir.hosts)
174
+ build_mir(Translator.new(@instance), linked, false)
175
+ end
176
+
177
+ # @rbs name: String|Symbol
178
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
179
+ def export_entry(name)
180
+ export = @instance.export_section.exports[name.to_s]
181
+ raise ::NoMethodError, "function #{name} not found" unless export
182
+ raise ::NoMethodError, "#{name} is not a function" unless export.kind == 0
183
+ @ir.entry_at(export.index)
184
+ end
185
+
186
+ # @rbs entry: Wardite::Efflorescence::FunctionEntry
187
+ # @rbs arguments: Array[Object]
188
+ # @rbs return: Integer?
189
+ def execute_public(entry, arguments)
190
+ unless arguments.size == entry.parameter_types.size
191
+ raise Wardite::ArgumentError, "unmatch arg size"
192
+ end
193
+ values = [] #: Array[Integer]
194
+ arguments.each_with_index do |value, i|
195
+ type = entry.parameter_types.fetch(i)
196
+ if FloatBits.float_type?(type)
197
+ bits = float_argument(type, value)
198
+ raise Wardite::ArgumentError, "type mismatch: #{type}(#{value})" if bits.nil?
199
+ values << bits
200
+ elsif type == :i64 && value.is_a?(Wardite::I64)
201
+ values << value.value_s
202
+ elsif value.is_a?(Integer)
203
+ values << (type == :i64 ? I64Arithmetic.normalize(value) : value & 0xffffffff)
204
+ else
205
+ raise Wardite::ArgumentError, "type mismatch: #{type}(#{value})"
206
+ end
207
+ end
208
+ execute(entry, values)
209
+ end
210
+
211
+ # @rbs type: Symbol
212
+ # @rbs value: Object?
213
+ # @rbs return: Integer?
214
+ def float_argument(type, value)
215
+ return FloatBits.from_float(type, value) if value.is_a?(Float)
216
+ if type == :f32 && value.is_a?(Wardite::F32)
217
+ value.raw_bits
218
+ elsif type == :f64 && value.is_a?(Wardite::F64)
219
+ value.raw_bits
220
+ end
221
+ end
222
+
223
+ # @rbs type: Symbol
224
+ # @rbs bits: Integer
225
+ # @rbs return: Integer | Float
226
+ def unbox_result(type, bits)
227
+ FloatBits.float_type?(type) ? FloatBits.to_float(type, bits) : bits
228
+ end
229
+
230
+ # @rbs type: Symbol
231
+ # @rbs bits: Integer
232
+ # @rbs return: Wardite::wasmValue
233
+ def box_result(type, bits)
234
+ case type
235
+ when :i64 then Wardite::I64(bits)
236
+ when :f32 then Wardite::F32.from_bits(bits)
237
+ when :f64 then Wardite::F64.from_bits(bits)
238
+ else Wardite::I32.new(bits)
239
+ end
240
+ end
241
+
242
+ # A concrete return type breaks the inference cycle between the current
243
+ # frame and the array it is restored from after every internal RETURN.
244
+ # @rbs return: Wardite::Efflorescence::Frame
245
+ def current_frame(frames)
246
+ frames.last || raise(Wardite::EvalError, "missing MIR caller")
247
+ end
248
+
249
+ # @rbs value: Integer
250
+ # @rbs return: Integer
251
+ def signed(value)
252
+ value >= 0x80000000 ? value - 0x100000000 : value
253
+ end
254
+
255
+ # Cold/new ALU paths stay outside the established hot-loop operations.
256
+ # @rbs opcode: Integer
257
+ # @rbs left: Integer
258
+ # @rbs right: Integer
259
+ # @rbs return: Integer
260
+ def calculate(opcode, left, right)
261
+ case opcode
262
+ when Opcode::I32_EQ then left == right ? 1 : 0
263
+ when Opcode::I32_NE then left != right ? 1 : 0
264
+ when Opcode::I32_LTS then signed(left) < signed(right) ? 1 : 0
265
+ when Opcode::I32_LTU then left < right ? 1 : 0
266
+ when Opcode::I32_GTS then signed(left) > signed(right) ? 1 : 0
267
+ when Opcode::I32_GTU then left > right ? 1 : 0
268
+ when Opcode::I32_LES then signed(left) <= signed(right) ? 1 : 0
269
+ when Opcode::I32_LEU then left <= right ? 1 : 0
270
+ when Opcode::I32_GES then signed(left) >= signed(right) ? 1 : 0
271
+ when Opcode::I32_GEU then left >= right ? 1 : 0
272
+ when Opcode::I32_CLZ
273
+ count = 32
274
+ while left != 0
275
+ left >>= 1
276
+ count -= 1
277
+ end
278
+ count
279
+ when Opcode::I32_CTZ
280
+ return 32 if left == 0
281
+ count = 0
282
+ while (left & 1) == 0
283
+ left >>= 1
284
+ count += 1
285
+ end
286
+ count
287
+ when Opcode::I32_POPCNT
288
+ count = 0
289
+ while left != 0
290
+ left &= left - 1
291
+ count += 1
292
+ end
293
+ count
294
+ when Opcode::I32_DIV_S, Opcode::I32_REM_S
295
+ raise Wardite::EvalError, "integer divide by zero" if right == 0
296
+ a, b = signed(left), signed(right)
297
+ if opcode == Opcode::I32_DIV_S && a == -2147483648 && b == -1
298
+ raise Wardite::IntegerOverflow, "integer division overflow"
299
+ end
300
+ quotient = a.abs / b.abs
301
+ quotient = -quotient if (a < 0) != (b < 0)
302
+ (opcode == Opcode::I32_DIV_S ? quotient : a - quotient * b) & 0xffffffff
303
+ when Opcode::I32_DIV_U, Opcode::I32_REM_U
304
+ raise Wardite::EvalError, "integer divide by zero" if right == 0
305
+ opcode == Opcode::I32_DIV_U ? left / right : left % right
306
+ when Opcode::I32_AND then left & right
307
+ when Opcode::I32_OR then left | right
308
+ when Opcode::I32_SHR_S then (signed(left) >> (right & 31)) & 0xffffffff
309
+ when Opcode::I32_ROTL, Opcode::I32_ROTR
310
+ shift = right & 31
311
+ shift = (32 - shift) & 31 if opcode == Opcode::I32_ROTR
312
+ ((left << shift) | (left >> ((32 - shift) & 31))) & 0xffffffff
313
+ when Opcode::I32_EXTEND8_S
314
+ value = left & 255
315
+ (value >= 128 ? value - 256 : value) & 0xffffffff
316
+ when Opcode::I32_EXTEND16_S
317
+ value = left & 65535
318
+ (value >= 32768 ? value - 65536 : value) & 0xffffffff
319
+ else
320
+ raise Wardite::EvalError, "invalid i32 opcode"
321
+ end
322
+ end
323
+
324
+ # The pending value owns its payload before any frame is released. No
325
+ # Ruby raise/rescue is used to unwind the explicit Wasm call stack.
326
+ # @rbs value: Wardite::Efflorescence::ExceptionValue
327
+ # @rbs frames: Array[Wardite::Efflorescence::Frame]
328
+ # @rbs pools: Array[Wardite::Efflorescence::FramePool]
329
+ # @rbs return: Wardite::Efflorescence::Frame
330
+ def propagate(value, frames, pools)
331
+ until frames.empty?
332
+ frame = current_frame(frames)
333
+ until frame.handlers.empty?
334
+ id = frame.handlers.pop || raise(Wardite::EvalError, "missing handler")
335
+ descriptors = frame.function.flat_handlers
336
+ descriptors.fetch(id).clauses.each do |clause|
337
+ if clause.tag_index < 0 || @instance.tags.fetch(clause.tag_index).equal?(value.tag)
338
+ frame.trim_handlers(clause.handler_depth)
339
+ clause.payload_registers.each_with_index do |register, i|
340
+ frame.registers[register] = value.payload_bits.fetch(i)
341
+ end
342
+ frame.exception_registers[clause.reference_register] = value if clause.reference_register >= 0
343
+ frame.pc = Integer(clause.target_pc)
344
+ return frame
345
+ end
346
+ end
347
+ end
348
+ frames.pop
349
+ pool_at(pools, frame.pool_index).release(frame)
350
+ end
351
+ raise Wardite::UncaughtWasmException, value
352
+ end
353
+
354
+ # @rbs frame: Wardite::Efflorescence::Frame
355
+ # @rbs id: Integer
356
+ # @rbs return: Wardite::Efflorescence::ExceptionValue
357
+ def exception_at(frame, id)
358
+ site = frame.function.throw_sites.fetch(id)
359
+ payload = site.payload_registers.map { |register| frame.registers[register] }
360
+ ExceptionValue.new(@instance.tags.fetch(site.tag_index), payload)
361
+ end
362
+
363
+ # @rbs pools: Array[Wardite::Efflorescence::FramePool]
364
+ # @rbs index: Integer
365
+ # @rbs return: Wardite::Efflorescence::FramePool
366
+ def pool_at(pools, index)
367
+ pools[index] || raise(Wardite::EvalError, "missing MIR frame pool")
368
+ end
369
+
370
+ # Update in place so a suspended execute_flat retains the same array
371
+ # across a host call, including a reentrant call on this VM.
372
+ # @rbs return: void
373
+ def read_globals
374
+ @instance.store.globals.each_with_index do |global, index|
375
+ @global_bits[index] = @store_access.global_bits_at(index, global.type)
376
+ end
377
+ end
378
+
379
+ # @rbs return: void
380
+ def write_globals
381
+ @instance.store.globals.each_with_index do |global, index|
382
+ @store_access.write_global_bits(index, global.type, @global_bits.fetch(index))
383
+ end
384
+ end
385
+
386
+ # @rbs host_index: Integer
387
+ # @rbs arguments: Array[Integer]
388
+ # @rbs metrics: Wardite::Efflorescence::Metrics
389
+ # @rbs sources: Array[Integer]?
390
+ # @rbs return: Integer?
391
+ def invoke_host_with_globals(host_index, arguments, metrics, sources)
392
+ write_globals
393
+ begin
394
+ invoke_host(host_index, arguments, metrics, sources)
395
+ ensure
396
+ read_globals
397
+ end
398
+ end
399
+
400
+ # @rbs host_index: Integer
401
+ # @rbs arguments: Array[Integer]
402
+ # @rbs metrics: Wardite::Efflorescence::Metrics
403
+ # @rbs sources: Array[Integer]?
404
+ # @rbs return: Integer?
405
+ def invoke_host(host_index, arguments, metrics, sources)
406
+ host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
407
+ metrics.host_calls += 1 if @profiling
408
+ if RUBY_ENGINE != "spinel"
409
+ wasi = host.unboxed_wasi
410
+ return wasi.call(@instance.store, arguments, sources) if wasi
411
+ end
412
+ return invoke_typed_host(host, arguments, sources) if host.typed_abi
413
+
414
+ boxed = [] #: Array[Wardite::wasmValue]
415
+ if sources
416
+ sources.each { |register| boxed << Wardite::I32.new(arguments[register]) }
417
+ else
418
+ arguments.each { |value| boxed << Wardite::I32.new(value) }
419
+ end
420
+ result = host.callable.call(@instance.store, boxed)
421
+ if host.entry.result_types.empty?
422
+ return nil if result.nil?
423
+ elsif result.is_a?(Integer)
424
+ return result & 0xffffffff
425
+ elsif result.is_a?(Wardite::I32)
426
+ return result.value & 0xffffffff
427
+ end
428
+ raise Wardite::EvalError, "host ABI result mismatch for #{host.external.name}"
429
+ end
430
+
431
+ # @rbs host: Wardite::Efflorescence::HostFunction
432
+ # @rbs arguments: Array[Integer]
433
+ # @rbs sources: Array[Integer]?
434
+ # @rbs return: Integer?
435
+ def invoke_typed_host(host, arguments, sources)
436
+ boxed = [] #: Array[Wardite::wasmValue]
437
+ host.entry.parameter_types.each_with_index do |type, i|
438
+ index = sources ? sources.fetch(i) : i
439
+ value = arguments.fetch(index)
440
+ boxed << box_result(type, value)
441
+ end
442
+ result = host.callable.call(@instance.store, boxed)
443
+ type = host.entry.result_types[0]
444
+ case type
445
+ when nil
446
+ return nil if result.nil?
447
+ when :i64
448
+ return result.value_s if result.is_a?(Wardite::I64)
449
+ return I64Arithmetic.normalize(result) if result.is_a?(Integer)
450
+ when :f32, :f64
451
+ bits = float_argument(type, result)
452
+ return bits unless bits.nil?
453
+ when :i32
454
+ return result.value & 0xffffffff if result.is_a?(Wardite::I32)
455
+ return result & 0xffffffff if result.is_a?(Integer)
456
+ end
457
+ raise Wardite::EvalError, "host ABI result mismatch for #{host.external.name}"
458
+ end
459
+
460
+ # Each public or start entry owns its frames, pools and counters. Host
461
+ # calls synchronize globals before a callback can re-enter the VM.
462
+ # @rbs entry: Wardite::Efflorescence::FunctionEntry
463
+ # @rbs arguments: Array[Integer]
464
+ # @rbs return: Integer?
465
+ def execute(entry, arguments)
466
+ if entry.kind == :host
467
+ read_globals if @has_globals
468
+ metrics = @profiling ? Metrics.new : @metrics
469
+ begin
470
+ return @has_globals ? invoke_host_with_globals(entry.host_index, arguments, metrics, nil) :
471
+ invoke_host(entry.host_index, arguments, metrics, nil)
472
+ ensure
473
+ @metrics = metrics
474
+ write_globals if @has_globals
475
+ end
476
+ end
477
+
478
+ pools = new_pools
479
+ frame = pool_at(pools, entry.defined_index).acquire(-1)
480
+ arguments.each_with_index { |value, i| frame.registers[i] = value }
481
+ result = run_frames(pools, [frame], 1)
482
+ entry.result_types.empty? ? nil : result
483
+ end
484
+
485
+ # @rbs return: Array[Wardite::Efflorescence::FramePool]
486
+ def new_pools
487
+ pools = [] #: Array[Wardite::Efflorescence::FramePool]
488
+ @ir.functions.each { |function| pools << FramePool.new(function, @reuse_frames) }
489
+ pools
490
+ end
491
+
492
+ # Globals live in @global_bits while frames run. A restored snapshot
493
+ # writes the Store first, so both public calls and resume read it here.
494
+ # @rbs pools: Array[Wardite::Efflorescence::FramePool]
495
+ # @rbs active_frames: Array[Wardite::Efflorescence::Frame]
496
+ # @rbs root_entries: Integer
497
+ # @rbs return: Integer
498
+ def run_frames(pools, active_frames, root_entries)
499
+ read_globals if @has_globals
500
+ metrics = @profiling ? Metrics.new : @metrics
501
+ metrics.root_defined_entries = root_entries if @profiling
502
+ outer = @active_frames
503
+ @active_frames = active_frames
504
+ @depth += 1
505
+ execute_flat(current_frame(active_frames), pools, active_frames, metrics)
506
+ ensure
507
+ @depth -= 1
508
+ @active_frames = outer
509
+ active_frames.each { |active| pool_at(pools, active.pool_index).release(active) }
510
+ if @profiling
511
+ created = 0
512
+ pools.each { |pool| created += pool.created }
513
+ metrics.frames_created = created
514
+ @metrics = metrics
515
+ end
516
+ write_globals if @has_globals
517
+ end
518
+
519
+ # @rbs frame: Wardite::Efflorescence::Frame
520
+ # @rbs site: Wardite::Efflorescence::DynamicCallSite
521
+ # @rbs host_index: Integer
522
+ # @rbs destination: Integer
523
+ # @rbs metrics: Wardite::Efflorescence::Metrics
524
+ # @rbs return: void
525
+ def execute_host_site(frame, site, host_index, destination, metrics)
526
+ registers = frame.registers
527
+ recorder = @snapshot
528
+ if recorder
529
+ host = @ir.hosts[host_index] || raise(Wardite::EvalError, "missing MIR host function")
530
+ if recorder.checkpoint?(host, registers, site.arguments)
531
+ @snapshot = nil
532
+ if @depth == 1
533
+ # The snapshot reads globals from the Store.
534
+ write_globals if @has_globals
535
+ recorder.save(@instance, @active_frames)
536
+ end
537
+ end
538
+ end
539
+ result = unless @has_globals
540
+ invoke_host(host_index, registers, metrics, site.arguments)
541
+ else
542
+ invoke_host_with_globals(host_index, registers, metrics, site.arguments)
543
+ end
544
+ registers[destination] = result if destination >= 0 && result
545
+ end
546
+
547
+ # @rbs site: Wardite::Efflorescence::DynamicCallSite
548
+ # @rbs selector: Integer
549
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
550
+ def indirect_entry(site, selector)
551
+ table = @instance.store.tables[site.table_index]
552
+ raise Wardite::EvalError, "call_indirect requires funcref table 0" unless table && table.type == :funcref
553
+ index = selector & 0xffffffff
554
+ raise Wardite::EvalError, "call_indirect table index out of bounds: #{index}" if index >= table.current
555
+ function = table.refs[index]
556
+ raise Wardite::EvalError, "call_indirect null reference" if function.nil?
557
+ function_index = @function_indices[function.object_id]
558
+ unless function_index && @indirect_functions[function_index].equal?(function)
559
+ raise Wardite::EvalError, "call_indirect unknown function reference"
560
+ end
561
+ entry = @ir.entry_at(function_index)
562
+ unless entry.parameter_types == site.parameter_types && entry.result_types == site.result_types
563
+ raise Wardite::EvalError, "call_indirect function type mismatch"
564
+ end
565
+ entry
566
+ end
567
+
568
+ # @rbs frame: Wardite::Efflorescence::Frame
569
+ # @rbs site_id: Integer
570
+ # @rbs destination: Integer
571
+ # @rbs metrics: Wardite::Efflorescence::Metrics
572
+ # @rbs return: void
573
+ def execute_direct_host(frame, site_id, destination, metrics)
574
+ site = frame.function.dynamic_site(site_id)
575
+ execute_host_site(frame, site, site.host_index, destination, metrics)
576
+ end
577
+
578
+ # Keep table resolution and the additional call locals off the hot path.
579
+ # @rbs frame: Wardite::Efflorescence::Frame
580
+ # @rbs pools: Array[Wardite::Efflorescence::FramePool]
581
+ # @rbs frames: Array[Wardite::Efflorescence::Frame]
582
+ # @rbs destination: Integer
583
+ # @rbs site_id: Integer
584
+ # @rbs selector: Integer
585
+ # @rbs metrics: Wardite::Efflorescence::Metrics
586
+ # @rbs return: Wardite::Efflorescence::Frame
587
+ def execute_indirect(frame, pools, frames, destination, site_id, selector, metrics)
588
+ site = frame.function.dynamic_site(site_id)
589
+ registers = frame.registers
590
+ entry = indirect_entry(site, registers[selector])
591
+ if entry.kind == :host
592
+ execute_host_site(frame, site, entry.host_index, destination, metrics)
593
+ frame.pc += Lowerer::WIDTH
594
+ frame
595
+ else
596
+ frame.pc += Lowerer::WIDTH
597
+ next_frame = pool_at(pools, entry.defined_index).acquire(destination)
598
+ frames << next_frame
599
+ target = next_frame.registers
600
+ site.arguments.each_with_index { |register, i| target[i] = registers[register] }
601
+ metrics.enter_indirect_defined(site.arguments.size) if @profiling
602
+ next_frame
603
+ end
604
+ end
605
+
606
+ # Float instructions use the same flat words and integer registers.
607
+ # Their dispatch stays outside the main integer switch.
608
+ # @rbs opcode: Integer
609
+ # @rbs instructions: Array[Integer]
610
+ # @rbs pc: Integer
611
+ # @rbs a: Integer
612
+ # @rbs registers: Array[Integer]
613
+ # @rbs return: void
614
+ def execute_float(opcode, instructions, pc, a, registers)
615
+ b = instructions.fetch(pc + 2)
616
+ if opcode >= Opcode::F32_EQ && opcode <= Opcode::F64_COPYSIGN
617
+ types = Opcode::FLOAT_ALU_TYPES.fetch(opcode)
618
+ right = types.size == 3 ? registers[instructions.fetch(pc + 3)] : 0
619
+ registers[a] = FloatArithmetic.calculate(opcode, registers[b], right)
620
+ elsif opcode >= Opcode::I32_TRUNC_F32_S && opcode <= Opcode::F64_PROMOTE_F32
621
+ registers[a] = FloatArithmetic.convert(opcode, registers[b])
622
+ elsif opcode >= Opcode::I32_REINTERPRET_F32 && opcode <= Opcode::F64_REINTERPRET_I64
623
+ registers[a] = registers[b]
624
+ else
625
+ case opcode
626
+ when Opcode::F32_LOAD
627
+ offset = instructions.fetch(pc + 3)
628
+ registers[a] = @store_access.load(registers[b], offset, 4, false)
629
+ when Opcode::F64_LOAD
630
+ offset = instructions.fetch(pc + 3)
631
+ registers[a] = @store_access.load_i64(registers[b], offset, 8, false)
632
+ when Opcode::F32_STORE
633
+ offset = instructions.fetch(pc + 3)
634
+ @store_access.store(registers[b], offset, 4, registers[a])
635
+ when Opcode::F64_STORE
636
+ offset = instructions.fetch(pc + 3)
637
+ @store_access.store(registers[b], offset, 8, registers[a])
638
+ else
639
+ raise Wardite::EvalError, "Efflorescence: invalid MIR opcode #{opcode}"
640
+ end
641
+ end
642
+ end
643
+
644
+ # @rbs frame: Wardite::Efflorescence::Frame
645
+ # @rbs pools: Array[Wardite::Efflorescence::FramePool]
646
+ # @rbs active_frames: Array[Wardite::Efflorescence::Frame]
647
+ # @rbs return: Integer
648
+ # @rbs metrics: Wardite::Efflorescence::Metrics
649
+ def execute_flat(frame, pools, active_frames, metrics)
650
+ frames = active_frames
651
+ registers = frame.registers
652
+ global_bits = @global_bits
653
+ instructions = frame.function.bytecode
654
+ pc = Integer(frame.pc)
655
+ # Avoid Kernel#loop's StopIteration handler in the native hot loop.
656
+ while true
657
+ opcode = instructions.fetch(pc)
658
+ a = instructions.fetch(pc + 1)
659
+ # PCs still advance by four words, but short instructions do not
660
+ # read their unused b/c padding. Control transfers skip the increment.
661
+ # Integer literal labels let Spinel emit a C switch. Keep the
662
+ # Opcode comments in sync; the dispatch contract test checks them.
663
+ case opcode
664
+ when 0 # Opcode::COPY
665
+ b = instructions.fetch(pc + 2)
666
+ registers[a] = registers[b]
667
+ when 1 # Opcode::I32_ADD
668
+ b = instructions.fetch(pc + 2)
669
+ c = instructions.fetch(pc + 3)
670
+ registers[a] = (registers[b] + registers[c]) & 0xffffffff
671
+ when 2 # Opcode::I32_SUB
672
+ b = instructions.fetch(pc + 2)
673
+ c = instructions.fetch(pc + 3)
674
+ registers[a] = (registers[b] - registers[c]) & 0xffffffff
675
+ when 3 # Opcode::I32_MUL
676
+ b = instructions.fetch(pc + 2)
677
+ c = instructions.fetch(pc + 3)
678
+ # A full unsigned i32 product can overflow Spinel's native signed
679
+ # word. Split into 16-bit limbs; all intermediates fit in 49 bits.
680
+ left = registers[b]
681
+ right = registers[c]
682
+ low = (left & 0xffff) * right
683
+ high = (((left >> 16) * right) & 0xffff) << 16
684
+ registers[a] = (low + high) & 0xffffffff
685
+ when 4 # Opcode::I32_XOR
686
+ b = instructions.fetch(pc + 2)
687
+ c = instructions.fetch(pc + 3)
688
+ registers[a] = registers[b] ^ registers[c]
689
+ when 5 # Opcode::I32_SHL
690
+ b = instructions.fetch(pc + 2)
691
+ c = instructions.fetch(pc + 3)
692
+ registers[a] = (registers[b] << (registers[c] & 31)) & 0xffffffff
693
+ when 6 # Opcode::I32_SHR_U
694
+ b = instructions.fetch(pc + 2)
695
+ c = instructions.fetch(pc + 3)
696
+ registers[a] = registers[b] >> (registers[c] & 31)
697
+ when 7 # Opcode::I32_EQZ
698
+ b = instructions.fetch(pc + 2)
699
+ registers[a] = registers[b] == 0 ? 1 : 0
700
+ when 13 # Opcode::I32_ADD_IMM
701
+ b = instructions.fetch(pc + 2)
702
+ c = instructions.fetch(pc + 3)
703
+ registers[a] = (registers[b] + c) & 0xffffffff
704
+ when 14 # Opcode::I32_SUB_IMM
705
+ b = instructions.fetch(pc + 2)
706
+ c = instructions.fetch(pc + 3)
707
+ registers[a] = (registers[b] - c) & 0xffffffff
708
+ when 15 # Opcode::I32_MUL_IMM
709
+ b = instructions.fetch(pc + 2)
710
+ c = instructions.fetch(pc + 3)
711
+ left = registers[b]
712
+ right = c
713
+ low = (left & 0xffff) * right
714
+ high = (((left >> 16) * right) & 0xffff) << 16
715
+ registers[a] = (low + high) & 0xffffffff
716
+ when 16 # Opcode::I32_XOR_IMM
717
+ b = instructions.fetch(pc + 2)
718
+ c = instructions.fetch(pc + 3)
719
+ registers[a] = registers[b] ^ c
720
+ when 17 # Opcode::I32_SHL_IMM
721
+ b = instructions.fetch(pc + 2)
722
+ c = instructions.fetch(pc + 3)
723
+ registers[a] = (registers[b] << (c & 31)) & 0xffffffff
724
+ when 18 # Opcode::I32_SHR_U_IMM
725
+ b = instructions.fetch(pc + 2)
726
+ c = instructions.fetch(pc + 3)
727
+ registers[a] = registers[b] >> (c & 31)
728
+ when 20 # Opcode::I32_XOR_SHR_U_IMM
729
+ b = instructions.fetch(pc + 2)
730
+ c = instructions.fetch(pc + 3)
731
+ value = registers[b]
732
+ registers[a] = value ^ (value >> c)
733
+ when 21 # Opcode::I32_XOR_SHL_IMM
734
+ b = instructions.fetch(pc + 2)
735
+ c = instructions.fetch(pc + 3)
736
+ value = registers[b]
737
+ registers[a] = (value ^ (value << c)) & 0xffffffff
738
+ when 8 # Opcode::BR
739
+ pc = a
740
+ next
741
+ when 9 # Opcode::BR_IF
742
+ b = instructions.fetch(pc + 2)
743
+ if registers[a] != 0
744
+ pc = b
745
+ next
746
+ end
747
+ when 19 # Opcode::BR_IF_ZERO
748
+ b = instructions.fetch(pc + 2)
749
+ if registers[a] == 0
750
+ pc = b
751
+ next
752
+ end
753
+ when 10 # Opcode::CALL
754
+ b = instructions.fetch(pc + 2)
755
+ c = instructions.fetch(pc + 3)
756
+ frame.pc = pc + Lowerer::WIDTH
757
+ site = frame.function.call_site(b)
758
+ pool = pools[site.pool_index]
759
+ raise Wardite::EvalError, "missing MIR frame pool" unless pool
760
+ next_frame = pool.acquire(a)
761
+ if c >= 0
762
+ next_frame.registers[0] = registers[c]
763
+ else
764
+ site.copy_arguments(next_frame.registers, registers)
765
+ end
766
+ frames << next_frame
767
+ frame = next_frame
768
+ registers = frame.registers
769
+ instructions = frame.function.bytecode
770
+ pc = 0
771
+ next
772
+ when 67 # Opcode::CALL_HOST
773
+ frame.pc = pc
774
+ execute_direct_host(frame, instructions.fetch(pc + 2), a, metrics)
775
+ when 68 # Opcode::CALL_INDIRECT
776
+ b = instructions.fetch(pc + 2)
777
+ c = instructions.fetch(pc + 3)
778
+ frame.pc = pc
779
+ frame = execute_indirect(frame, pools, frames, a, b, c, metrics)
780
+ registers = frame.registers
781
+ instructions = frame.function.bytecode
782
+ pc = Integer(frame.pc)
783
+ next
784
+ when 11 # Opcode::RETURN
785
+ # Verified RETURN reads an initialized integer. Keep its native type
786
+ # separate from the nullable array reads used by the ALU cases.
787
+ return_value = a < 0 ? 0 : Integer(registers[a])
788
+ destination = frame.return_register
789
+ frames.pop
790
+ pool = pools[frame.pool_index]
791
+ raise Wardite::EvalError, "missing MIR frame pool" unless pool
792
+ pool.release(frame)
793
+ return return_value if frames.empty?
794
+ frame = current_frame(frames)
795
+ registers = frame.registers
796
+ registers[destination] = return_value if destination >= 0
797
+ instructions = frame.function.bytecode
798
+ pc = Integer(frame.pc)
799
+ next
800
+ when 12 # Opcode::TRAP
801
+ raise Wardite::Unreachable, "unreachable op"
802
+ when 23 # Opcode::I32_EQ
803
+ b = instructions.fetch(pc + 2)
804
+ c = instructions.fetch(pc + 3)
805
+ registers[a] = calculate(23, registers[b], registers[c])
806
+ when 24 # Opcode::I32_NE
807
+ b = instructions.fetch(pc + 2)
808
+ c = instructions.fetch(pc + 3)
809
+ registers[a] = calculate(24, registers[b], registers[c])
810
+ when 25 # Opcode::I32_LTS
811
+ b = instructions.fetch(pc + 2)
812
+ c = instructions.fetch(pc + 3)
813
+ registers[a] = calculate(25, registers[b], registers[c])
814
+ when 26 # Opcode::I32_LTU
815
+ b = instructions.fetch(pc + 2)
816
+ c = instructions.fetch(pc + 3)
817
+ registers[a] = calculate(26, registers[b], registers[c])
818
+ when 27 # Opcode::I32_GTS
819
+ b = instructions.fetch(pc + 2)
820
+ c = instructions.fetch(pc + 3)
821
+ registers[a] = calculate(27, registers[b], registers[c])
822
+ when 28 # Opcode::I32_GTU
823
+ b = instructions.fetch(pc + 2)
824
+ c = instructions.fetch(pc + 3)
825
+ registers[a] = calculate(28, registers[b], registers[c])
826
+ when 29 # Opcode::I32_LES
827
+ b = instructions.fetch(pc + 2)
828
+ c = instructions.fetch(pc + 3)
829
+ registers[a] = calculate(29, registers[b], registers[c])
830
+ when 30 # Opcode::I32_LEU
831
+ b = instructions.fetch(pc + 2)
832
+ c = instructions.fetch(pc + 3)
833
+ registers[a] = calculate(30, registers[b], registers[c])
834
+ when 31 # Opcode::I32_GES
835
+ b = instructions.fetch(pc + 2)
836
+ c = instructions.fetch(pc + 3)
837
+ registers[a] = calculate(31, registers[b], registers[c])
838
+ when 32 # Opcode::I32_GEU
839
+ b = instructions.fetch(pc + 2)
840
+ c = instructions.fetch(pc + 3)
841
+ registers[a] = calculate(32, registers[b], registers[c])
842
+ when 33 # Opcode::I32_CLZ
843
+ b = instructions.fetch(pc + 2)
844
+ registers[a] = calculate(33, registers[b], 0)
845
+ when 34 # Opcode::I32_CTZ
846
+ b = instructions.fetch(pc + 2)
847
+ registers[a] = calculate(34, registers[b], 0)
848
+ when 35 # Opcode::I32_POPCNT
849
+ b = instructions.fetch(pc + 2)
850
+ registers[a] = calculate(35, registers[b], 0)
851
+ when 36 # Opcode::I32_DIV_S
852
+ b = instructions.fetch(pc + 2)
853
+ c = instructions.fetch(pc + 3)
854
+ registers[a] = calculate(36, registers[b], registers[c])
855
+ when 37 # Opcode::I32_DIV_U
856
+ b = instructions.fetch(pc + 2)
857
+ c = instructions.fetch(pc + 3)
858
+ registers[a] = calculate(37, registers[b], registers[c])
859
+ when 38 # Opcode::I32_REM_S
860
+ b = instructions.fetch(pc + 2)
861
+ c = instructions.fetch(pc + 3)
862
+ registers[a] = calculate(38, registers[b], registers[c])
863
+ when 39 # Opcode::I32_REM_U
864
+ b = instructions.fetch(pc + 2)
865
+ c = instructions.fetch(pc + 3)
866
+ registers[a] = calculate(39, registers[b], registers[c])
867
+ when 40 # Opcode::I32_AND
868
+ b = instructions.fetch(pc + 2)
869
+ c = instructions.fetch(pc + 3)
870
+ registers[a] = calculate(40, registers[b], registers[c])
871
+ when 41 # Opcode::I32_OR
872
+ b = instructions.fetch(pc + 2)
873
+ c = instructions.fetch(pc + 3)
874
+ registers[a] = calculate(41, registers[b], registers[c])
875
+ when 42 # Opcode::I32_SHR_S
876
+ b = instructions.fetch(pc + 2)
877
+ c = instructions.fetch(pc + 3)
878
+ registers[a] = calculate(42, registers[b], registers[c])
879
+ when 43 # Opcode::I32_ROTL
880
+ b = instructions.fetch(pc + 2)
881
+ c = instructions.fetch(pc + 3)
882
+ registers[a] = calculate(43, registers[b], registers[c])
883
+ when 44 # Opcode::I32_ROTR
884
+ b = instructions.fetch(pc + 2)
885
+ c = instructions.fetch(pc + 3)
886
+ registers[a] = calculate(44, registers[b], registers[c])
887
+ when 45 # Opcode::I32_EXTEND8_S
888
+ b = instructions.fetch(pc + 2)
889
+ registers[a] = calculate(45, registers[b], 0)
890
+ when 46 # Opcode::I32_EXTEND16_S
891
+ b = instructions.fetch(pc + 2)
892
+ registers[a] = calculate(46, registers[b], 0)
893
+ when 47 # Opcode::COPY_REF
894
+ b = instructions.fetch(pc + 2)
895
+ frame.exception_registers[a] = frame.exception_registers[b]
896
+ when 48 # Opcode::EH_ENTER
897
+ frame.handlers << a
898
+ when 49 # Opcode::EH_TRIM
899
+ frame.trim_handlers(a)
900
+ when 50 # Opcode::THROW
901
+ value_ref = exception_at(frame, a)
902
+ frame = propagate(value_ref, frames, pools)
903
+ registers = frame.registers
904
+ instructions = frame.function.bytecode
905
+ pc = Integer(frame.pc)
906
+ next
907
+ when 51 # Opcode::THROW_REF
908
+ value_ref = frame.exception_registers[a]
909
+ raise Wardite::EvalError, "throw_ref null" unless value_ref
910
+ frame = propagate(value_ref, frames, pools)
911
+ registers = frame.registers
912
+ instructions = frame.function.bytecode
913
+ pc = Integer(frame.pc)
914
+ next
915
+ when 52 # Opcode::GLOBAL_GET
916
+ b = instructions.fetch(pc + 2)
917
+ registers[a] = global_bits[b]
918
+ when 53 # Opcode::GLOBAL_SET
919
+ b = instructions.fetch(pc + 2)
920
+ global_bits[a] = registers[b]
921
+ when 54 # Opcode::I32_LOAD
922
+ b = instructions.fetch(pc + 2)
923
+ c = instructions.fetch(pc + 3)
924
+ registers[a] = @store_access.load(registers[b], c, 4, false)
925
+ when 55 # Opcode::I32_LOAD8_S
926
+ b = instructions.fetch(pc + 2)
927
+ c = instructions.fetch(pc + 3)
928
+ registers[a] = @store_access.load(registers[b], c, 1, true)
929
+ when 56 # Opcode::I32_LOAD8_U
930
+ b = instructions.fetch(pc + 2)
931
+ c = instructions.fetch(pc + 3)
932
+ registers[a] = @store_access.load(registers[b], c, 1, false)
933
+ when 57 # Opcode::I32_LOAD16_S
934
+ b = instructions.fetch(pc + 2)
935
+ c = instructions.fetch(pc + 3)
936
+ registers[a] = @store_access.load(registers[b], c, 2, true)
937
+ when 58 # Opcode::I32_LOAD16_U
938
+ b = instructions.fetch(pc + 2)
939
+ c = instructions.fetch(pc + 3)
940
+ registers[a] = @store_access.load(registers[b], c, 2, false)
941
+ when 59 # Opcode::I32_STORE
942
+ b = instructions.fetch(pc + 2)
943
+ c = instructions.fetch(pc + 3)
944
+ @store_access.store(registers[b], c, 4, registers[a])
945
+ when 60 # Opcode::I32_STORE8
946
+ b = instructions.fetch(pc + 2)
947
+ c = instructions.fetch(pc + 3)
948
+ @store_access.store(registers[b], c, 1, registers[a])
949
+ when 61 # Opcode::I32_STORE16
950
+ b = instructions.fetch(pc + 2)
951
+ c = instructions.fetch(pc + 3)
952
+ @store_access.store(registers[b], c, 2, registers[a])
953
+ when 62 # Opcode::MEMORY_SIZE
954
+ registers[a] = @store_access.size
955
+ when 63 # Opcode::MEMORY_GROW
956
+ b = instructions.fetch(pc + 2)
957
+ registers[a] = @store_access.grow(registers[b])
958
+ when 64 # Opcode::MEMORY_INIT
959
+ @store_access.bulk(64, frame.function.memory_site(a), registers)
960
+ when 65 # Opcode::MEMORY_COPY
961
+ @store_access.bulk(65, frame.function.memory_site(a), registers)
962
+ when 66 # Opcode::MEMORY_FILL
963
+ @store_access.bulk(66, frame.function.memory_site(a), registers)
964
+ when 69 # Opcode::I64_EQZ
965
+ b = instructions.fetch(pc + 2)
966
+ registers[a] = I64Arithmetic.calculate(69, registers[b], 0)
967
+ when 70 # Opcode::I32_WRAP_I64
968
+ b = instructions.fetch(pc + 2)
969
+ registers[a] = I64Arithmetic.calculate(70, registers[b], 0)
970
+ when 71 # Opcode::I64_EQ
971
+ b = instructions.fetch(pc + 2)
972
+ c = instructions.fetch(pc + 3)
973
+ registers[a] = I64Arithmetic.calculate(71, registers[b], registers[c])
974
+ when 72 # Opcode::I64_NE
975
+ b = instructions.fetch(pc + 2)
976
+ c = instructions.fetch(pc + 3)
977
+ registers[a] = I64Arithmetic.calculate(72, registers[b], registers[c])
978
+ when 73 # Opcode::I64_LTS
979
+ b = instructions.fetch(pc + 2)
980
+ c = instructions.fetch(pc + 3)
981
+ registers[a] = I64Arithmetic.calculate(73, registers[b], registers[c])
982
+ when 74 # Opcode::I64_LTU
983
+ b = instructions.fetch(pc + 2)
984
+ c = instructions.fetch(pc + 3)
985
+ registers[a] = I64Arithmetic.calculate(74, registers[b], registers[c])
986
+ when 75 # Opcode::I64_GTS
987
+ b = instructions.fetch(pc + 2)
988
+ c = instructions.fetch(pc + 3)
989
+ registers[a] = I64Arithmetic.calculate(75, registers[b], registers[c])
990
+ when 76 # Opcode::I64_GTU
991
+ b = instructions.fetch(pc + 2)
992
+ c = instructions.fetch(pc + 3)
993
+ registers[a] = I64Arithmetic.calculate(76, registers[b], registers[c])
994
+ when 77 # Opcode::I64_LES
995
+ b = instructions.fetch(pc + 2)
996
+ c = instructions.fetch(pc + 3)
997
+ registers[a] = I64Arithmetic.calculate(77, registers[b], registers[c])
998
+ when 78 # Opcode::I64_LEU
999
+ b = instructions.fetch(pc + 2)
1000
+ c = instructions.fetch(pc + 3)
1001
+ registers[a] = I64Arithmetic.calculate(78, registers[b], registers[c])
1002
+ when 79 # Opcode::I64_GES
1003
+ b = instructions.fetch(pc + 2)
1004
+ c = instructions.fetch(pc + 3)
1005
+ registers[a] = I64Arithmetic.calculate(79, registers[b], registers[c])
1006
+ when 80 # Opcode::I64_GEU
1007
+ b = instructions.fetch(pc + 2)
1008
+ c = instructions.fetch(pc + 3)
1009
+ registers[a] = I64Arithmetic.calculate(80, registers[b], registers[c])
1010
+ when 81 # Opcode::I64_CLZ
1011
+ b = instructions.fetch(pc + 2)
1012
+ registers[a] = I64Arithmetic.calculate(81, registers[b], 0)
1013
+ when 82 # Opcode::I64_CTZ
1014
+ b = instructions.fetch(pc + 2)
1015
+ registers[a] = I64Arithmetic.calculate(82, registers[b], 0)
1016
+ when 83 # Opcode::I64_POPCNT
1017
+ b = instructions.fetch(pc + 2)
1018
+ registers[a] = I64Arithmetic.calculate(83, registers[b], 0)
1019
+ when 84 # Opcode::I64_EXTEND8_S
1020
+ b = instructions.fetch(pc + 2)
1021
+ registers[a] = I64Arithmetic.calculate(84, registers[b], 0)
1022
+ when 85 # Opcode::I64_EXTEND16_S
1023
+ b = instructions.fetch(pc + 2)
1024
+ registers[a] = I64Arithmetic.calculate(85, registers[b], 0)
1025
+ when 86 # Opcode::I64_EXTEND32_S
1026
+ b = instructions.fetch(pc + 2)
1027
+ registers[a] = I64Arithmetic.calculate(86, registers[b], 0)
1028
+ when 87 # Opcode::I64_EXTEND_I32_S
1029
+ b = instructions.fetch(pc + 2)
1030
+ registers[a] = I64Arithmetic.calculate(87, registers[b], 0)
1031
+ when 88 # Opcode::I64_EXTEND_I32_U
1032
+ b = instructions.fetch(pc + 2)
1033
+ registers[a] = I64Arithmetic.calculate(88, registers[b], 0)
1034
+ when 89 # Opcode::I64_AND
1035
+ b = instructions.fetch(pc + 2)
1036
+ c = instructions.fetch(pc + 3)
1037
+ registers[a] = I64Arithmetic.calculate(89, registers[b], registers[c])
1038
+ when 90 # Opcode::I64_OR
1039
+ b = instructions.fetch(pc + 2)
1040
+ c = instructions.fetch(pc + 3)
1041
+ registers[a] = I64Arithmetic.calculate(90, registers[b], registers[c])
1042
+ when 91 # Opcode::I64_XOR
1043
+ b = instructions.fetch(pc + 2)
1044
+ c = instructions.fetch(pc + 3)
1045
+ registers[a] = I64Arithmetic.calculate(91, registers[b], registers[c])
1046
+ when 92 # Opcode::I64_SHL
1047
+ b = instructions.fetch(pc + 2)
1048
+ c = instructions.fetch(pc + 3)
1049
+ registers[a] = I64Arithmetic.calculate(92, registers[b], registers[c])
1050
+ when 93 # Opcode::I64_SHR_S
1051
+ b = instructions.fetch(pc + 2)
1052
+ c = instructions.fetch(pc + 3)
1053
+ registers[a] = I64Arithmetic.calculate(93, registers[b], registers[c])
1054
+ when 94 # Opcode::I64_SHR_U
1055
+ b = instructions.fetch(pc + 2)
1056
+ c = instructions.fetch(pc + 3)
1057
+ registers[a] = I64Arithmetic.calculate(94, registers[b], registers[c])
1058
+ when 95 # Opcode::I64_ROTL
1059
+ b = instructions.fetch(pc + 2)
1060
+ c = instructions.fetch(pc + 3)
1061
+ registers[a] = I64Arithmetic.calculate(95, registers[b], registers[c])
1062
+ when 96 # Opcode::I64_ROTR
1063
+ b = instructions.fetch(pc + 2)
1064
+ c = instructions.fetch(pc + 3)
1065
+ registers[a] = I64Arithmetic.calculate(96, registers[b], registers[c])
1066
+ when 97 # Opcode::I64_ADD
1067
+ b = instructions.fetch(pc + 2)
1068
+ c = instructions.fetch(pc + 3)
1069
+ registers[a] = I64Arithmetic.calculate(97, registers[b], registers[c])
1070
+ when 98 # Opcode::I64_SUB
1071
+ b = instructions.fetch(pc + 2)
1072
+ c = instructions.fetch(pc + 3)
1073
+ registers[a] = I64Arithmetic.calculate(98, registers[b], registers[c])
1074
+ when 99 # Opcode::I64_MUL
1075
+ b = instructions.fetch(pc + 2)
1076
+ c = instructions.fetch(pc + 3)
1077
+ registers[a] = I64Arithmetic.calculate(99, registers[b], registers[c])
1078
+ when 100 # Opcode::I64_DIV_S
1079
+ b = instructions.fetch(pc + 2)
1080
+ c = instructions.fetch(pc + 3)
1081
+ registers[a] = I64Arithmetic.calculate(100, registers[b], registers[c])
1082
+ when 101 # Opcode::I64_DIV_U
1083
+ b = instructions.fetch(pc + 2)
1084
+ c = instructions.fetch(pc + 3)
1085
+ registers[a] = I64Arithmetic.calculate(101, registers[b], registers[c])
1086
+ when 102 # Opcode::I64_REM_S
1087
+ b = instructions.fetch(pc + 2)
1088
+ c = instructions.fetch(pc + 3)
1089
+ registers[a] = I64Arithmetic.calculate(102, registers[b], registers[c])
1090
+ when 103 # Opcode::I64_REM_U
1091
+ b = instructions.fetch(pc + 2)
1092
+ c = instructions.fetch(pc + 3)
1093
+ registers[a] = I64Arithmetic.calculate(103, registers[b], registers[c])
1094
+ when 104 # Opcode::I64_LOAD
1095
+ b = instructions.fetch(pc + 2)
1096
+ c = instructions.fetch(pc + 3)
1097
+ registers[a] = @store_access.load_i64(registers[b], c, 8, false)
1098
+ when 105 # Opcode::I64_LOAD8_S
1099
+ b = instructions.fetch(pc + 2)
1100
+ c = instructions.fetch(pc + 3)
1101
+ registers[a] = @store_access.load_i64(registers[b], c, 1, true)
1102
+ when 106 # Opcode::I64_LOAD8_U
1103
+ b = instructions.fetch(pc + 2)
1104
+ c = instructions.fetch(pc + 3)
1105
+ registers[a] = @store_access.load_i64(registers[b], c, 1, false)
1106
+ when 107 # Opcode::I64_LOAD16_S
1107
+ b = instructions.fetch(pc + 2)
1108
+ c = instructions.fetch(pc + 3)
1109
+ registers[a] = @store_access.load_i64(registers[b], c, 2, true)
1110
+ when 108 # Opcode::I64_LOAD16_U
1111
+ b = instructions.fetch(pc + 2)
1112
+ c = instructions.fetch(pc + 3)
1113
+ registers[a] = @store_access.load_i64(registers[b], c, 2, false)
1114
+ when 109 # Opcode::I64_LOAD32_S
1115
+ b = instructions.fetch(pc + 2)
1116
+ c = instructions.fetch(pc + 3)
1117
+ registers[a] = @store_access.load_i64(registers[b], c, 4, true)
1118
+ when 110 # Opcode::I64_LOAD32_U
1119
+ b = instructions.fetch(pc + 2)
1120
+ c = instructions.fetch(pc + 3)
1121
+ registers[a] = @store_access.load_i64(registers[b], c, 4, false)
1122
+ when 111 # Opcode::I64_STORE
1123
+ b = instructions.fetch(pc + 2)
1124
+ c = instructions.fetch(pc + 3)
1125
+ @store_access.store(registers[b], c, 8, registers[a])
1126
+ when 112 # Opcode::I64_STORE8
1127
+ b = instructions.fetch(pc + 2)
1128
+ c = instructions.fetch(pc + 3)
1129
+ @store_access.store(registers[b], c, 1, registers[a])
1130
+ when 113 # Opcode::I64_STORE16
1131
+ b = instructions.fetch(pc + 2)
1132
+ c = instructions.fetch(pc + 3)
1133
+ @store_access.store(registers[b], c, 2, registers[a])
1134
+ when 114 # Opcode::I64_STORE32
1135
+ b = instructions.fetch(pc + 2)
1136
+ c = instructions.fetch(pc + 3)
1137
+ @store_access.store(registers[b], c, 4, registers[a])
1138
+ when 115 # Opcode::I64_GLOBAL_GET
1139
+ b = instructions.fetch(pc + 2)
1140
+ registers[a] = global_bits[b]
1141
+ when 116 # Opcode::I64_GLOBAL_SET
1142
+ b = instructions.fetch(pc + 2)
1143
+ global_bits[a] = registers[b]
1144
+ when 191 # Opcode::F32_GLOBAL_GET
1145
+ b = instructions.fetch(pc + 2)
1146
+ registers[a] = global_bits[b]
1147
+ when 192 # Opcode::F64_GLOBAL_GET
1148
+ b = instructions.fetch(pc + 2)
1149
+ registers[a] = global_bits[b]
1150
+ when 193 # Opcode::F32_GLOBAL_SET
1151
+ b = instructions.fetch(pc + 2)
1152
+ global_bits[a] = registers[b]
1153
+ when 194 # Opcode::F64_GLOBAL_SET
1154
+ b = instructions.fetch(pc + 2)
1155
+ global_bits[a] = registers[b]
1156
+ when 22 # Opcode::PROFILE
1157
+ b = instructions.fetch(pc + 2)
1158
+ c = instructions.fetch(pc + 3)
1159
+ metrics.record(a, b, c)
1160
+ else
1161
+ execute_float(opcode, instructions, pc, a, registers)
1162
+ end
1163
+ pc += Lowerer::WIDTH
1164
+ end
1165
+ # Steep treats while as possibly nil; execution always returns or raises.
1166
+ raise Wardite::EvalError, "Efflorescence: MIR execution ended without RETURN"
1167
+ end
1168
+ end
1169
+ end
1170
+ end