wardite 1.2.2 → 1.3.1

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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -15
  3. data/Rakefile +0 -177
  4. data/benchmark/README.md +10 -36
  5. data/benchmark/call_cost_worker.rb +3 -2
  6. data/benchmark/compare.rb +1 -2
  7. data/benchmark/compare_linear_memory_loads.py +144 -0
  8. data/benchmark/i32_memory.wat +5 -1
  9. data/benchmark/investigate_calls.rb +4 -4
  10. data/benchmark/linear_memory_loads.wat +52 -0
  11. data/benchmark/optimization_worker.rb +3 -2
  12. data/benchmark/patches/adr0017-spinel-signed64.patch +190 -0
  13. data/benchmark/patches/adr0017-wardite-split-load64.patch +70 -0
  14. data/benchmark/results/adr0019-startup.json +1687 -0
  15. data/benchmark/results/adr0020-i64.json +2034 -0
  16. data/benchmark/results/adr0021-optimizer.json +5798 -0
  17. data/benchmark/results/adr0022-lazy-mir.json +3057 -0
  18. data/benchmark/results/crc8-stream-repeat.csv +89 -0
  19. data/benchmark/results/crc8-stream.csv +89 -0
  20. data/benchmark/results/linear-memory-loads-a2aa05227.csv +793 -0
  21. data/benchmark/results/linear-memory-loads-stage-b.csv +353 -0
  22. data/benchmark/results/linear-memory-loads.csv +1057 -0
  23. data/benchmark/results/real-workloads-baseline-2026-11-18.json +1741 -0
  24. data/benchmark/results/spinel-072516af1.csv +149 -0
  25. data/benchmark/results/stack-vm-removal.json +334 -0
  26. data/benchmark/validate_m4.rb +0 -2
  27. data/docs/adr/0017-efflorescence-linear-memory-loads.md +90 -0
  28. data/docs/adr/0018-efflorescence-real-workload-measurement.md +126 -0
  29. data/docs/adr/0019-efflorescence-startup-quick-wins.md +85 -0
  30. data/docs/adr/0020-efflorescence-inline-i64-bit-operations.md +89 -0
  31. data/docs/adr/0021-efflorescence-optimize-real-functions.md +133 -0
  32. data/docs/adr/0022-efflorescence-lazy-function-build.md +111 -0
  33. data/docs/adr/0023-efflorescence-byte-sequences-and-copy-coalescing.md +56 -0
  34. data/docs/efflorescence-ja.md +3 -5
  35. data/docs/efflorescence-linear-memory-loads.md +134 -0
  36. data/docs/efflorescence-m6.md +1 -1
  37. data/docs/efflorescence-mir-generation-performance.md +58 -0
  38. data/docs/efflorescence-mir2-cache.md +7 -0
  39. data/docs/efflorescence-real-workloads.md +312 -0
  40. data/docs/efflorescence-spinel-linear-memory.md +2 -2
  41. data/docs/efflorescence.md +3 -5
  42. data/docs/stack-vm-removal.md +49 -0
  43. data/lib/wardite/cli/cli.rb +24 -24
  44. data/lib/wardite/efflorescence/byte_sequences.rb +80 -0
  45. data/lib/wardite/efflorescence/function_preparer.rb +64 -0
  46. data/lib/wardite/efflorescence/i64_arithmetic.rb +31 -0
  47. data/lib/wardite/efflorescence/inspection.rb +44 -12
  48. data/lib/wardite/efflorescence/ir.rb +33 -10
  49. data/lib/wardite/efflorescence/mir_cache.rb +10 -1
  50. data/lib/wardite/efflorescence/optimizer.rb +151 -42
  51. data/lib/wardite/efflorescence/store_access.rb +17 -0
  52. data/lib/wardite/efflorescence/translation_source.rb +50 -0
  53. data/lib/wardite/efflorescence/translator.rb +135 -19
  54. data/lib/wardite/efflorescence/vm.rb +116 -48
  55. data/lib/wardite/efflorescence.rb +3 -0
  56. data/lib/wardite/load.rb +46 -62
  57. data/lib/wardite/version.rb +1 -1
  58. data/lib/wardite.rb +21 -757
  59. data/scripts/build_spinel_zig.rb +29 -17
  60. data/sig/generated/wardite/cli/cli.rbs +4 -2
  61. data/sig/generated/wardite/efflorescence/byte_sequences.rbs +33 -0
  62. data/sig/generated/wardite/efflorescence/function_preparer.rbs +25 -0
  63. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +19 -0
  64. data/sig/generated/wardite/efflorescence/inspection.rbs +8 -0
  65. data/sig/generated/wardite/efflorescence/ir.rbs +18 -2
  66. data/sig/generated/wardite/efflorescence/optimizer.rbs +41 -3
  67. data/sig/generated/wardite/efflorescence/store_access.rbs +5 -0
  68. data/sig/generated/wardite/efflorescence/translation_source.rbs +34 -0
  69. data/sig/generated/wardite/efflorescence/translator.rbs +28 -1
  70. data/sig/generated/wardite/efflorescence/vm.rbs +19 -9
  71. data/sig/generated/wardite/load.rbs +18 -18
  72. data/sig/generated/wardite.rbs +4 -155
  73. metadata +33 -17
  74. data/benchmark/compare_engines.rb +0 -119
  75. data/benchmark/engine_phases.rb +0 -30
  76. data/lib/wardite/alu_f32.generated.rb +0 -261
  77. data/lib/wardite/alu_f64.generated.rb +0 -261
  78. data/lib/wardite/alu_i32.generated.rb +0 -510
  79. data/lib/wardite/alu_i64.generated.rb +0 -572
  80. data/lib/wardite/convert.generated.rb +0 -340
  81. data/scripts/gen_alu.rb +0 -864
  82. data/scripts/gen_conv.rb +0 -86
  83. data/scripts/templates/alu_module.rb.tmpl +0 -20
  84. data/scripts/templates/conv_module.rb.tmpl +0 -20
  85. data/sig/generated/wardite/alu_f32.generated.rbs +0 -12
  86. data/sig/generated/wardite/alu_f64.generated.rbs +0 -12
  87. data/sig/generated/wardite/alu_i32.generated.rbs +0 -12
  88. data/sig/generated/wardite/alu_i64.generated.rbs +0 -12
  89. data/sig/generated/wardite/convert.generated.rbs +0 -12
@@ -193,6 +193,7 @@ module Wardite
193
193
  f64_load: Opcode::F64_LOAD, f64_store: Opcode::F64_STORE }.freeze
194
194
  STORE_CODES = (MEMORY_WIDTHS.keys + %i[global_get global_set memory_size memory_grow memory_init memory_copy memory_fill]).freeze
195
195
 
196
+ # @rbs @source: Wardite::Efflorescence::Translator::ModuleSource
196
197
  # @rbs @instance: Wardite::Instance
197
198
  # @rbs @entries: Array[Wardite::Efflorescence::FunctionEntry]
198
199
  # @rbs @stack: Array[Integer]
@@ -207,10 +208,14 @@ module Wardite
207
208
  # @rbs @handler_depth: Integer
208
209
 
209
210
  # @rbs instance: Wardite::Instance
210
- def initialize(instance)
211
+ # @rbs source: ModuleSource
212
+ # @rbs entries: Array[FunctionEntry]
213
+ def initialize(instance, source = ModuleSource.new(instance), entries = [])
214
+ @source = source
211
215
  @instance = instance
212
- @entries = []
213
- @stack, @controls, @constants = [], [], {}
216
+ @entries = entries
217
+ @stack, @controls = [], []
218
+ @constants = {}
214
219
  @i64_constants = nil
215
220
  @f32_constants = nil
216
221
  @f64_constants = nil
@@ -251,15 +256,120 @@ module Wardite
251
256
  # @rbs return: Wardite::Efflorescence::IR
252
257
  def translate(linked = link)
253
258
  @entries = linked.entries
254
- functions = [] #: Array[Wardite::Efflorescence::Function]
255
- @instance.store.funcs.each_with_index do |function, index|
256
- if function.is_a?(Wardite::WasmFunction)
257
- functions << translate_function(function, index, functions.size)
258
- end
259
+ trace = ENV["WARDITE_TRACE"]
260
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC) if trace
261
+ configured = ENV["WARDITE_MIR_WORKERS"]
262
+ # CRuby serializes Ruby translation under the GVL. On Spinel, avoid
263
+ # starting helpers for tiny modules unless explicitly requested.
264
+ workers = if configured
265
+ configured.to_i.clamp(1, 8)
266
+ elsif RUBY_ENGINE == "spinel" && @entries.size - linked.hosts.size >= 32
267
+ (ENV["SPINEL_WORKERS"] || "8").to_i.clamp(1, 8)
268
+ else
269
+ 1
270
+ end
271
+ functions = if workers > 1
272
+ translate_parallel(workers)
273
+ else
274
+ translate_range(0, @entries.size)
275
+ end
276
+ if started
277
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
278
+ $stderr.puts "semantic MIR translation: #{elapsed}"
259
279
  end
260
280
  IR.new(functions, entries: @entries, hosts: linked.hosts)
261
281
  end
262
282
 
283
+ # Partition in module order by decoded instruction count. Each translator
284
+ # owns its stacks and output. Only linked declarations are shared.
285
+ # @rbs workers: Integer
286
+ # @rbs return: Array[Function]
287
+ def translate_parallel(workers)
288
+ store = @instance.store.funcs
289
+ total = 0
290
+ store.each { |wasm| total += wasm.body.size if wasm.is_a?(Wardite::WasmFunction) }
291
+ target = (total + workers - 1) / workers
292
+ starts = [0]
293
+ accumulated = 0
294
+ store.each_with_index do |wasm, index|
295
+ next unless wasm.is_a?(Wardite::WasmFunction)
296
+ if accumulated >= target && accumulated > 0 && starts.size < workers
297
+ starts << index
298
+ accumulated = 0
299
+ end
300
+ accumulated += wasm.body.size
301
+ end
302
+ threads = [] #: Array[Thread]
303
+ begin
304
+ (starts.size - 1).times do |i|
305
+ threads << start_translation(starts.fetch(i), starts.fetch(i + 1))
306
+ end
307
+ tail = translate_range(starts.last || 0, store.size)
308
+ ensure
309
+ # Wait for every worker even when several ranges fail. The earliest
310
+ # range in module order has the same error as serial translation.
311
+ error = nil #: StandardError?
312
+ threads.each do |thread|
313
+ begin
314
+ thread.join
315
+ rescue StandardError => failure
316
+ error ||= failure
317
+ end
318
+ end
319
+ raise error if error
320
+ end
321
+ functions = [] #: Array[Function]
322
+ threads.each { |thread| functions.concat(thread.value) }
323
+ functions.concat(tail)
324
+ end
325
+
326
+ # @rbs first: Integer
327
+ # @rbs limit: Integer
328
+ # @rbs return: Thread
329
+ def start_translation(first, limit)
330
+ worker = Translator.new(@instance, @source, @entries)
331
+ Thread.new do
332
+ Thread.current.report_on_exception = false
333
+ worker.translate_range(first, limit)
334
+ end
335
+ end
336
+
337
+ # @rbs first: Integer
338
+ # @rbs limit: Integer
339
+ # @rbs return: Array[Function]
340
+ def translate_range(first, limit)
341
+ functions = [] #: Array[Function]
342
+ index = first
343
+ while index < limit
344
+ wasm = @instance.store.funcs.fetch(index)
345
+ if wasm.is_a?(Wardite::WasmFunction)
346
+ functions << translate_function(wasm, index, @entries.fetch(index).defined_index)
347
+ end
348
+ index += 1
349
+ end
350
+ functions
351
+ end
352
+
353
+ # Replay the original declarations without re-linking host functions,
354
+ # executing start, or consulting live global/memory/table slots.
355
+ # @rbs linked: IR
356
+ # @rbs bytes: String
357
+ # @rbs return: IR
358
+ def translate_source(linked, bytes)
359
+ @entries = linked.entries
360
+ bodies = BinaryLoader.decode_code_section(bytes).func_codes
361
+ types = @source.block_types
362
+ functions = [] #: Array[Wardite::Efflorescence::Function]
363
+ linked.entries.each_with_index do |entry, index|
364
+ next unless entry.kind == :defined
365
+ body = bodies.fetch(entry.defined_index)
366
+ body.check_features(types)
367
+ wasm = Wardite::WasmFunction.new(entry.parameter_types, entry.result_types, body)
368
+ functions << translate_function(wasm, index, functions.size)
369
+ end
370
+ IR.new(functions, entries: linked.entries, hosts: linked.hosts)
371
+ end
372
+
263
373
  # @rbs wasm: Wardite::WasmFunction
264
374
  # @rbs index: Integer
265
375
  # @rbs pool_index: Integer
@@ -272,7 +382,8 @@ module Wardite
272
382
  locals = [] #: Array[Symbol]
273
383
  wasm.locals_type.each_with_index { |type, i| wasm.locals_count.fetch(i).times { locals << type } }
274
384
  @function = Function.new(index, wasm.callsig, wasm.retsig, locals.size, local_types: locals, pool_index: pool_index)
275
- @stack, @controls, @constants = [], [], {}
385
+ @stack, @controls = [], []
386
+ @constants = {}
276
387
  @i64_constants = nil
277
388
  @f32_constants = nil
278
389
  @f64_constants = nil
@@ -365,7 +476,7 @@ module Wardite
365
476
  @stack << dst
366
477
  when :global_get, :global_set
367
478
  index = integer_operand(operands[0])
368
- type = @instance.store.globals.fetch(index).type
479
+ type = @source.globals.fetch(index).type
369
480
  @function.uses_i64 = true if type == :i64
370
481
  @function.uses_float = true if FloatBits.float_type?(type)
371
482
  if code == :global_get
@@ -495,9 +606,8 @@ module Wardite
495
606
  type_index = integer_operand(operands[0])
496
607
  table_index = integer_operand(operands[1])
497
608
  raise UnsupportedFeature, context("only call_indirect through table 0 is supported") unless table_index == 0
498
- table = @instance.store.tables[0]
499
- fail_translation("call_indirect requires funcref table 0") unless table && table.type == :funcref
500
- type = @instance.types[type_index] if type_index >= 0
609
+ fail_translation("call_indirect requires funcref table 0") unless @source.funcref_table
610
+ type = @source.types[type_index] if type_index >= 0
501
611
  fail_translation("invalid indirect call type index #{type_index}") unless type
502
612
  FunctionEntry.check_signature(type.callsig, type.retsig, "type #{type_index}")
503
613
  @function.uses_dynamic_calls = true
@@ -564,7 +674,7 @@ module Wardite
564
674
  @function.uses_store = true
565
675
  if code == :global_get || code == :global_set
566
676
  index = integer_operand(operands[0])
567
- global = index >= 0 ? @instance.store.globals[index] : nil
677
+ global = index >= 0 ? @source.globals[index] : nil
568
678
  fail_translation("invalid global index #{index}") unless global
569
679
  case global.type
570
680
  when :i32
@@ -581,7 +691,7 @@ module Wardite
581
691
  fail_translation("global.set on immutable global #{index}") if code == :global_set && !global.mutable
582
692
  return
583
693
  end
584
- fail_translation("memory instruction requires memory 0") if @instance.store.memories.empty?
694
+ fail_translation("memory instruction requires memory 0") unless @source.has_memory
585
695
  alignment = MEMORY_WIDTHS[code]
586
696
  if alignment
587
697
  hint = integer_operand(operands[0])
@@ -590,8 +700,8 @@ module Wardite
590
700
  fail_translation("invalid memory offset") unless offset >= 0 && offset <= 0xffffffff
591
701
  elsif code == :memory_init
592
702
  data_index = integer_operand(operands[0])
593
- fail_translation("memory.init requires data count") unless @instance.data_count_section
594
- count = @instance.data_section&.segments&.size || 0
703
+ fail_translation("memory.init requires data count") unless @source.has_data_count
704
+ count = @source.data_segments
595
705
  fail_translation("invalid data index #{data_index}") unless data_index >= 0 && data_index < count
596
706
  check_memory_index(integer_operand(operands[1]))
597
707
  elsif code == :memory_copy
@@ -631,7 +741,13 @@ module Wardite
631
741
  when :f64 then @f64_constants ||= {} #: Hash[Integer, Integer]
632
742
  else @constants
633
743
  end
634
- constants[value] ||= @function.allocate_constant(value, type: type)
744
+ # Keep the lookup on the original integer-keyed table in Spinel.
745
+ bits = Integer(value)
746
+ cached = constants[bits]
747
+ return cached if cached
748
+ slot = @function.allocate_constant(bits, type: type)
749
+ constants[bits] = slot
750
+ slot
635
751
  end
636
752
 
637
753
  # @rbs kind: Symbol
@@ -768,7 +884,7 @@ module Wardite
768
884
  # @rbs return: Wardite::Efflorescence::Tag
769
885
  def tag_at(index)
770
886
  fail_translation("invalid tag index #{index}") if index < 0
771
- @instance.tags[index] || raise(TranslationError, context("invalid tag index #{index}"))
887
+ @source.tags[index] || raise(TranslationError, context("invalid tag index #{index}"))
772
888
  end
773
889
 
774
890
  # Catch labels resolve before pushing the try_table's own label.
@@ -5,13 +5,14 @@ module Wardite
5
5
  module Efflorescence
6
6
  class VM
7
7
  attr_reader :instance #: Wardite::Instance
8
- attr_reader :ir #: Wardite::Efflorescence::IR
8
+ # @rbs @preparer: FunctionPreparer?
9
9
  attr_reader :metrics #: Wardite::Efflorescence::Metrics
10
10
  attr_reader :mir_cache_hit #: bool
11
11
  attr_reader :mir_cache #: Wardite::Efflorescence::MIRCache?
12
12
  # Records a startup snapshot before the first WASI call outside its allowlist.
13
13
  attr_accessor :snapshot #: Wardite::Efflorescence::Snapshot?
14
14
 
15
+ # @rbs @inspection_links: Wardite::Efflorescence::IR?
15
16
  # @rbs @semantic_ir: Wardite::Efflorescence::IR?
16
17
  # @rbs @optimized_ir: Wardite::Efflorescence::IR?
17
18
  # @rbs @global_bits: Array[Integer]
@@ -21,8 +22,10 @@ module Wardite
21
22
  # @rbs profile: bool
22
23
  # @rbs mir_cache: Wardite::Efflorescence::MIRCache?
23
24
  # @rbs cached_functions: Array[Function]?
25
+ # @rbs source_code: String?
26
+ # @rbs eager_mir: bool
24
27
  # @rbs cache_checked: bool
25
- def initialize(instance, disabled_optimizations: [], profile: false, mir_cache: nil, cached_functions: nil, cache_checked: false)
28
+ def initialize(instance, disabled_optimizations: [], profile: false, mir_cache: nil, cached_functions: nil, cache_checked: false, source_code: nil, eager_mir: false)
26
29
  @instance = instance
27
30
  @store_access = StoreAccess.new(instance.store)
28
31
  @global_bits = []
@@ -44,16 +47,24 @@ module Wardite
44
47
  @snapshot = nil
45
48
  @active_frames = [] #: Array[Wardite::Efflorescence::Frame]
46
49
  @depth = 0
47
- translator = Translator.new(instance)
50
+ @module_source = Translator::ModuleSource.new(instance, source_code)
51
+ translator = Translator.new(instance, @module_source)
48
52
  linked = translator.link
53
+ @inspection_links = source_code ? linked.copy : nil
49
54
  if mir_cache && !profile && !cache_checked
50
55
  cached_functions = mir_cache.read_functions(instance)
51
56
  end
52
57
  @mir_cache_hit = !cached_functions.nil?
58
+ @preparer = nil
53
59
  @ir = if cached_functions
54
60
  IR.new(cached_functions, entries: linked.entries, hosts: linked.hosts)
61
+ elsif eager_mir || mir_cache || @module_source.code.nil?
62
+ build_mir(translator.translate(linked), profile, @module_source.code.nil?)
55
63
  else
56
- build_mir(translator, linked, profile)
64
+ translator.translate(linked)
65
+ end
66
+ if cached_functions || (!eager_mir && !mir_cache && @module_source.code)
67
+ @preparer = FunctionPreparer.new(@ir, @configuration, @module_source.tags, profile, mir_cache)
57
68
  end
58
69
  if mir_cache && !profile && !@mir_cache_hit
59
70
  mir_cache.write_ir(instance, @ir)
@@ -68,8 +79,21 @@ module Wardite
68
79
  end
69
80
  end
70
81
 
71
- # A caller explicitly inspecting a cache hit can still request the
72
- # intermediate forms. Execution continues to use the restored MIR2.
82
+ # Inspecting the executable IR requests every uncached function body.
83
+ # MIR2 consumers can still inspect pending cache slots individually.
84
+ # @rbs return: Wardite::Efflorescence::IR
85
+ def ir
86
+ prepare_all unless @mir_cache_hit
87
+ @ir
88
+ end
89
+
90
+ # @rbs return: void
91
+ def prepare_all
92
+ preparer = @preparer
93
+ @ir.functions.each { |function| preparer.prepare(function) } if preparer
94
+ end
95
+
96
+ # Build both inspection views on first access. Execution keeps its own IR.
73
97
  # @rbs return: Wardite::Efflorescence::IR
74
98
  def semantic_ir
75
99
  restore_inspection_ir unless @semantic_ir
@@ -89,7 +113,7 @@ module Wardite
89
113
  !!export && export.kind == 0
90
114
  end
91
115
 
92
- # Compatible with Runtime.call: boxing happens once, at the Ruby boundary.
116
+ # Box the result once, at the Ruby boundary.
93
117
  # @rbs name: String|Symbol
94
118
  # @rbs arguments: Array[Object]
95
119
  # @rbs return: Wardite::wasmValue?
@@ -153,26 +177,34 @@ module Wardite
153
177
 
154
178
  private
155
179
 
156
- # @rbs translator: Wardite::Efflorescence::Translator
157
- # @rbs linked: Wardite::Efflorescence::IR
180
+ # @rbs semantic: Wardite::Efflorescence::IR
158
181
  # @rbs profile: bool
182
+ # @rbs inspect: bool
159
183
  # @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) }
184
+ def build_mir(semantic, profile, inspect)
185
+ optimizer = Optimizer.new(@configuration)
186
+ optimized = inspect ? optimizer.optimize(semantic) : optimizer.optimize_in_place(semantic)
187
+ optimized.functions.each do |function|
188
+ RegisterValidation.new.validate(function, optimized, tags: @module_source.tags)
189
+ optimizer.fuse_byte_sequences(function)
190
+ end
164
191
  ir = profile ? Instrumentation.new.instrument(optimized) : optimized
165
192
  Lowerer.new.compile(ir)
166
- @semantic_ir = semantic
167
- @optimized_ir = optimized
193
+ if inspect
194
+ @semantic_ir = semantic
195
+ @optimized_ir = optimized
196
+ end
168
197
  ir
169
198
  end
170
199
 
171
200
  # @rbs return: void
172
201
  def restore_inspection_ir
173
202
  @mir_cache&.restore_all
174
- linked = IR.new([], entries: @ir.entries, hosts: @ir.hosts)
175
- build_mir(Translator.new(@instance), linked, false)
203
+ linked = @inspection_links || IR.new([], entries: @ir.entries, hosts: @ir.hosts).copy
204
+ translator = Translator.new(@instance, @module_source)
205
+ bytes = @module_source.code
206
+ semantic = bytes ? translator.translate_source(linked, bytes) : translator.translate(linked)
207
+ build_mir(semantic, @profiling, true)
176
208
  end
177
209
 
178
210
  # @rbs name: String|Symbol
@@ -486,7 +518,7 @@ module Wardite
486
518
  # @rbs return: Array[Wardite::Efflorescence::FramePool]
487
519
  def new_pools
488
520
  pools = [] #: Array[Wardite::Efflorescence::FramePool]
489
- @ir.functions.each { |function| pools << FramePool.new(function, @reuse_frames, @mir_cache) }
521
+ @ir.functions.each { |function| pools << FramePool.new(function, @reuse_frames, @preparer) }
490
522
  pools
491
523
  end
492
524
 
@@ -662,6 +694,41 @@ module Wardite
662
694
  # Integer literal labels let Spinel emit a C switch. Keep the
663
695
  # Opcode comments in sync; the dispatch contract test checks them.
664
696
  case opcode
697
+ when 195 # Opcode::I32_TABLE_STORE8
698
+ address = (registers[instructions.fetch(pc + 2)] + registers[instructions.fetch(pc + 3)]) & 0xffffffff
699
+ registers[a] = address
700
+ byte = @store_access.load8_u(address, instructions.fetch(pc + 7))
701
+ registers[instructions.fetch(pc + 5)] = byte
702
+ @store_access.store(registers[instructions.fetch(pc + 10)], instructions.fetch(pc + 11), 1, byte)
703
+ registers[instructions.fetch(pc + 13)] = (registers[instructions.fetch(pc + 14)] + instructions.fetch(pc + 15)) & 0xffffffff
704
+ pc += 3 * Lowerer::WIDTH
705
+ when 196 # Opcode::I64_BYTE_LOOKUP
706
+ address = (registers[instructions.fetch(pc + 2)] + instructions.fetch(pc + 3)) & 0xffffffff
707
+ registers[a] = address
708
+ byte = @store_access.load8_u(address, 0)
709
+ registers[instructions.fetch(pc + 5)] = byte
710
+ table_address = (registers[instructions.fetch(pc + 10)] + byte) & 0xffffffff
711
+ registers[instructions.fetch(pc + 9)] = table_address
712
+ registers[instructions.fetch(pc + 13)] = @store_access.load8_u(table_address, 0)
713
+ pc += 3 * Lowerer::WIDTH
714
+ when 197 # Opcode::I32_BYTE_MUL_ADD
715
+ address = (registers[instructions.fetch(pc + 2)] + instructions.fetch(pc + 3)) & 0xffffffff
716
+ registers[a] = address
717
+ byte = @store_access.load8_u(address, 0)
718
+ registers[instructions.fetch(pc + 5)] = byte
719
+ # An unsigned byte times an unsigned i32 fits in Spinel's signed word.
720
+ product = (byte * instructions.fetch(pc + 11)) & 0xffffffff
721
+ registers[instructions.fetch(pc + 9)] = product
722
+ registers[instructions.fetch(pc + 13)] = (registers[instructions.fetch(pc + 14)] + product) & 0xffffffff
723
+ pc += 3 * Lowerer::WIDTH
724
+ when 198 # Opcode::I32_BYTE_ADD_STORE
725
+ byte = @store_access.load8_u(registers[instructions.fetch(pc + 2)], 0)
726
+ registers[a] = byte
727
+ sum = (byte + registers[instructions.fetch(pc + 7)]) & 0xffffffff
728
+ registers[instructions.fetch(pc + 5)] = sum
729
+ @store_access.store(registers[instructions.fetch(pc + 10)], 0, 1, sum)
730
+ registers[instructions.fetch(pc + 13)] = (registers[instructions.fetch(pc + 14)] + instructions.fetch(pc + 15)) & 0xffffffff
731
+ pc += 3 * Lowerer::WIDTH
665
732
  when 0 # Opcode::COPY
666
733
  b = instructions.fetch(pc + 2)
667
734
  registers[a] = registers[b]
@@ -736,19 +803,20 @@ module Wardite
736
803
  c = instructions.fetch(pc + 3)
737
804
  value = registers[b]
738
805
  registers[a] = (value ^ (value << c)) & 0xffffffff
806
+ # Spinel infers nullable operands. Keep branch targets and the loop PC integers.
739
807
  when 8 # Opcode::BR
740
- pc = a
808
+ pc = Integer(a)
741
809
  next
742
810
  when 9 # Opcode::BR_IF
743
811
  b = instructions.fetch(pc + 2)
744
812
  if registers[a] != 0
745
- pc = b
813
+ pc = Integer(b)
746
814
  next
747
815
  end
748
816
  when 19 # Opcode::BR_IF_ZERO
749
817
  b = instructions.fetch(pc + 2)
750
818
  if registers[a] == 0
751
- pc = b
819
+ pc = Integer(b)
752
820
  next
753
821
  end
754
822
  when 10 # Opcode::CALL
@@ -930,7 +998,7 @@ module Wardite
930
998
  when 56 # Opcode::I32_LOAD8_U
931
999
  b = instructions.fetch(pc + 2)
932
1000
  c = instructions.fetch(pc + 3)
933
- registers[a] = @store_access.load(registers[b], c, 1, false)
1001
+ registers[a] = @store_access.load8_u(registers[b], c)
934
1002
  when 57 # Opcode::I32_LOAD16_S
935
1003
  b = instructions.fetch(pc + 2)
936
1004
  c = instructions.fetch(pc + 3)
@@ -964,50 +1032,50 @@ module Wardite
964
1032
  @store_access.bulk(66, frame.function.memory_site(a), registers)
965
1033
  when 69 # Opcode::I64_EQZ
966
1034
  b = instructions.fetch(pc + 2)
967
- registers[a] = I64Arithmetic.calculate(69, registers[b], 0)
1035
+ registers[a] = registers.fetch(b) == 0 ? 1 : 0
968
1036
  when 70 # Opcode::I32_WRAP_I64
969
1037
  b = instructions.fetch(pc + 2)
970
- registers[a] = I64Arithmetic.calculate(70, registers[b], 0)
1038
+ registers[a] = registers.fetch(b) & 0xffffffff
971
1039
  when 71 # Opcode::I64_EQ
972
1040
  b = instructions.fetch(pc + 2)
973
1041
  c = instructions.fetch(pc + 3)
974
- registers[a] = I64Arithmetic.calculate(71, registers[b], registers[c])
1042
+ registers[a] = registers.fetch(b) == registers.fetch(c) ? 1 : 0
975
1043
  when 72 # Opcode::I64_NE
976
1044
  b = instructions.fetch(pc + 2)
977
1045
  c = instructions.fetch(pc + 3)
978
- registers[a] = I64Arithmetic.calculate(72, registers[b], registers[c])
1046
+ registers[a] = registers.fetch(b) != registers.fetch(c) ? 1 : 0
979
1047
  when 73 # Opcode::I64_LTS
980
1048
  b = instructions.fetch(pc + 2)
981
1049
  c = instructions.fetch(pc + 3)
982
- registers[a] = I64Arithmetic.calculate(73, registers[b], registers[c])
1050
+ registers[a] = registers.fetch(b) < registers.fetch(c) ? 1 : 0
983
1051
  when 74 # Opcode::I64_LTU
984
1052
  b = instructions.fetch(pc + 2)
985
1053
  c = instructions.fetch(pc + 3)
986
- registers[a] = I64Arithmetic.calculate(74, registers[b], registers[c])
1054
+ registers[a] = (registers.fetch(b) ^ I64Arithmetic::MIN) < (registers.fetch(c) ^ I64Arithmetic::MIN) ? 1 : 0
987
1055
  when 75 # Opcode::I64_GTS
988
1056
  b = instructions.fetch(pc + 2)
989
1057
  c = instructions.fetch(pc + 3)
990
- registers[a] = I64Arithmetic.calculate(75, registers[b], registers[c])
1058
+ registers[a] = registers.fetch(b) > registers.fetch(c) ? 1 : 0
991
1059
  when 76 # Opcode::I64_GTU
992
1060
  b = instructions.fetch(pc + 2)
993
1061
  c = instructions.fetch(pc + 3)
994
- registers[a] = I64Arithmetic.calculate(76, registers[b], registers[c])
1062
+ registers[a] = (registers.fetch(b) ^ I64Arithmetic::MIN) > (registers.fetch(c) ^ I64Arithmetic::MIN) ? 1 : 0
995
1063
  when 77 # Opcode::I64_LES
996
1064
  b = instructions.fetch(pc + 2)
997
1065
  c = instructions.fetch(pc + 3)
998
- registers[a] = I64Arithmetic.calculate(77, registers[b], registers[c])
1066
+ registers[a] = registers.fetch(b) <= registers.fetch(c) ? 1 : 0
999
1067
  when 78 # Opcode::I64_LEU
1000
1068
  b = instructions.fetch(pc + 2)
1001
1069
  c = instructions.fetch(pc + 3)
1002
- registers[a] = I64Arithmetic.calculate(78, registers[b], registers[c])
1070
+ registers[a] = (registers.fetch(b) ^ I64Arithmetic::MIN) <= (registers.fetch(c) ^ I64Arithmetic::MIN) ? 1 : 0
1003
1071
  when 79 # Opcode::I64_GES
1004
1072
  b = instructions.fetch(pc + 2)
1005
1073
  c = instructions.fetch(pc + 3)
1006
- registers[a] = I64Arithmetic.calculate(79, registers[b], registers[c])
1074
+ registers[a] = registers.fetch(b) >= registers.fetch(c) ? 1 : 0
1007
1075
  when 80 # Opcode::I64_GEU
1008
1076
  b = instructions.fetch(pc + 2)
1009
1077
  c = instructions.fetch(pc + 3)
1010
- registers[a] = I64Arithmetic.calculate(80, registers[b], registers[c])
1078
+ registers[a] = (registers.fetch(b) ^ I64Arithmetic::MIN) >= (registers.fetch(c) ^ I64Arithmetic::MIN) ? 1 : 0
1011
1079
  when 81 # Opcode::I64_CLZ
1012
1080
  b = instructions.fetch(pc + 2)
1013
1081
  registers[a] = I64Arithmetic.calculate(81, registers[b], 0)
@@ -1019,51 +1087,51 @@ module Wardite
1019
1087
  registers[a] = I64Arithmetic.calculate(83, registers[b], 0)
1020
1088
  when 84 # Opcode::I64_EXTEND8_S
1021
1089
  b = instructions.fetch(pc + 2)
1022
- registers[a] = I64Arithmetic.calculate(84, registers[b], 0)
1090
+ registers[a] = ((registers.fetch(b) & 0xff) ^ 0x80) - 0x80
1023
1091
  when 85 # Opcode::I64_EXTEND16_S
1024
1092
  b = instructions.fetch(pc + 2)
1025
- registers[a] = I64Arithmetic.calculate(85, registers[b], 0)
1093
+ registers[a] = ((registers.fetch(b) & 0xffff) ^ 0x8000) - 0x8000
1026
1094
  when 86 # Opcode::I64_EXTEND32_S
1027
1095
  b = instructions.fetch(pc + 2)
1028
- registers[a] = I64Arithmetic.calculate(86, registers[b], 0)
1096
+ registers[a] = ((registers.fetch(b) & 0xffffffff) ^ 0x80000000) - 0x80000000
1029
1097
  when 87 # Opcode::I64_EXTEND_I32_S
1030
1098
  b = instructions.fetch(pc + 2)
1031
- registers[a] = I64Arithmetic.calculate(87, registers[b], 0)
1099
+ registers[a] = ((registers.fetch(b) & 0xffffffff) ^ 0x80000000) - 0x80000000
1032
1100
  when 88 # Opcode::I64_EXTEND_I32_U
1033
1101
  b = instructions.fetch(pc + 2)
1034
- registers[a] = I64Arithmetic.calculate(88, registers[b], 0)
1102
+ registers[a] = registers.fetch(b)
1035
1103
  when 89 # Opcode::I64_AND
1036
1104
  b = instructions.fetch(pc + 2)
1037
1105
  c = instructions.fetch(pc + 3)
1038
- registers[a] = I64Arithmetic.calculate(89, registers[b], registers[c])
1106
+ registers[a] = registers.fetch(b) & registers.fetch(c)
1039
1107
  when 90 # Opcode::I64_OR
1040
1108
  b = instructions.fetch(pc + 2)
1041
1109
  c = instructions.fetch(pc + 3)
1042
- registers[a] = I64Arithmetic.calculate(90, registers[b], registers[c])
1110
+ registers[a] = registers.fetch(b) | registers.fetch(c)
1043
1111
  when 91 # Opcode::I64_XOR
1044
1112
  b = instructions.fetch(pc + 2)
1045
1113
  c = instructions.fetch(pc + 3)
1046
- registers[a] = I64Arithmetic.calculate(91, registers[b], registers[c])
1114
+ registers[a] = registers.fetch(b) ^ registers.fetch(c)
1047
1115
  when 92 # Opcode::I64_SHL
1048
1116
  b = instructions.fetch(pc + 2)
1049
1117
  c = instructions.fetch(pc + 3)
1050
- registers[a] = I64Arithmetic.calculate(92, registers[b], registers[c])
1118
+ registers[a] = I64Arithmetic.flat_shl(Integer(registers.fetch(b)), registers.fetch(c) & 63)
1051
1119
  when 93 # Opcode::I64_SHR_S
1052
1120
  b = instructions.fetch(pc + 2)
1053
1121
  c = instructions.fetch(pc + 3)
1054
- registers[a] = I64Arithmetic.calculate(93, registers[b], registers[c])
1122
+ registers[a] = registers.fetch(b) >> (registers.fetch(c) & 63)
1055
1123
  when 94 # Opcode::I64_SHR_U
1056
1124
  b = instructions.fetch(pc + 2)
1057
1125
  c = instructions.fetch(pc + 3)
1058
- registers[a] = I64Arithmetic.calculate(94, registers[b], registers[c])
1126
+ registers[a] = I64Arithmetic.flat_shr_u(Integer(registers.fetch(b)), registers.fetch(c) & 63)
1059
1127
  when 95 # Opcode::I64_ROTL
1060
1128
  b = instructions.fetch(pc + 2)
1061
1129
  c = instructions.fetch(pc + 3)
1062
- registers[a] = I64Arithmetic.calculate(95, registers[b], registers[c])
1130
+ registers[a] = I64Arithmetic.flat_rotl(Integer(registers.fetch(b)), registers.fetch(c) & 63)
1063
1131
  when 96 # Opcode::I64_ROTR
1064
1132
  b = instructions.fetch(pc + 2)
1065
1133
  c = instructions.fetch(pc + 3)
1066
- registers[a] = I64Arithmetic.calculate(96, registers[b], registers[c])
1134
+ registers[a] = I64Arithmetic.flat_rotl(Integer(registers.fetch(b)), (-(registers.fetch(c) & 63)) & 63)
1067
1135
  when 97 # Opcode::I64_ADD
1068
1136
  b = instructions.fetch(pc + 2)
1069
1137
  c = instructions.fetch(pc + 3)
@@ -1103,7 +1171,7 @@ module Wardite
1103
1171
  when 106 # Opcode::I64_LOAD8_U
1104
1172
  b = instructions.fetch(pc + 2)
1105
1173
  c = instructions.fetch(pc + 3)
1106
- registers[a] = @store_access.load_i64(registers[b], c, 1, false)
1174
+ registers[a] = @store_access.load8_u(registers[b], c)
1107
1175
  when 107 # Opcode::I64_LOAD16_S
1108
1176
  b = instructions.fetch(pc + 2)
1109
1177
  c = instructions.fetch(pc + 3)
@@ -5,10 +5,13 @@ require_relative "efflorescence/float_arithmetic"
5
5
  require_relative "efflorescence/ir"
6
6
  require_relative "efflorescence/store_access"
7
7
  require_relative "efflorescence/translator"
8
+ require_relative "efflorescence/byte_sequences"
8
9
  require_relative "efflorescence/optimizer"
9
10
  require_relative "efflorescence/register_validation"
10
11
  require_relative "efflorescence/inspection"
11
12
  require_relative "efflorescence/vm"
13
+ require_relative "efflorescence/translation_source"
14
+ require_relative "efflorescence/function_preparer"
12
15
 
13
16
  # Spinel initializes the cache tables at startup, after Opcode is defined.
14
17
  if RUBY_ENGINE == "spinel"