wardite 0.8.2 → 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 (163) 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/examples/load_perf.rb +24 -0
  101. data/lib/wardite/alu_f32.generated.rb +13 -10
  102. data/lib/wardite/alu_f64.generated.rb +13 -10
  103. data/lib/wardite/alu_i32.generated.rb +38 -31
  104. data/lib/wardite/alu_i64.generated.rb +48 -48
  105. data/lib/wardite/cli/cli.rb +73 -22
  106. data/lib/wardite/const.rb +1 -0
  107. data/lib/wardite/convert.generated.rb +7 -5
  108. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  109. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  110. data/lib/wardite/efflorescence/inspection.rb +165 -0
  111. data/lib/wardite/efflorescence/ir.rb +1151 -0
  112. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  113. data/lib/wardite/efflorescence/register_validation.rb +290 -0
  114. data/lib/wardite/efflorescence/store_access.rb +271 -0
  115. data/lib/wardite/efflorescence/translator.rb +859 -0
  116. data/lib/wardite/efflorescence/vm.rb +1006 -0
  117. data/lib/wardite/efflorescence/wasi.rb +108 -0
  118. data/lib/wardite/efflorescence.rb +11 -0
  119. data/lib/wardite/errors.rb +20 -0
  120. data/lib/wardite/float_bits.rb +46 -0
  121. data/lib/wardite/instruction.rb +460 -69
  122. data/lib/wardite/leb128.rb +35 -17
  123. data/lib/wardite/load.rb +260 -159
  124. data/lib/wardite/revisitor.rb +12 -4
  125. data/lib/wardite/value.rb +169 -175
  126. data/lib/wardite/version.rb +1 -1
  127. data/lib/wardite/wasi/consts.rb +1 -0
  128. data/lib/wardite/wasm_module.rb +6 -3
  129. data/lib/wardite.rb +220 -105
  130. data/rbs_collection.lock.yaml +36 -96
  131. data/scripts/gen_alu.rb +90 -34
  132. data/scripts/templates/alu_module.rb.tmpl +7 -5
  133. data/scripts/templates/conv_module.rb.tmpl +7 -5
  134. data/sig/generated/wardite/alu_f32.generated.rbs +4 -3
  135. data/sig/generated/wardite/alu_f64.generated.rbs +4 -3
  136. data/sig/generated/wardite/alu_i32.generated.rbs +4 -3
  137. data/sig/generated/wardite/alu_i64.generated.rbs +4 -3
  138. data/sig/generated/wardite/cli/cli.rbs +13 -6
  139. data/sig/generated/wardite/const.rbs +2 -0
  140. data/sig/generated/wardite/convert.generated.rbs +4 -3
  141. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  142. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  143. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  144. data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
  145. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  146. data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
  147. data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
  148. data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
  149. data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
  150. data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
  151. data/sig/generated/wardite/errors.rbs +12 -0
  152. data/sig/generated/wardite/float_bits.rbs +30 -0
  153. data/sig/generated/wardite/instruction.rbs +35 -10
  154. data/sig/generated/wardite/leb128.rbs +3 -1
  155. data/sig/generated/wardite/load.rbs +38 -13
  156. data/sig/generated/wardite/value.rbs +70 -11
  157. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  158. data/sig/generated/wardite.rbs +55 -18
  159. data/sig/vernier.rbs +3 -0
  160. data/spinel/README.md +30 -0
  161. data/spinel/bin/wardite.rb +6 -0
  162. data/spinel/spin.toml +7 -0
  163. metadata +128 -3
data/lib/wardite.rb CHANGED
@@ -4,8 +4,10 @@
4
4
  require_relative "wardite/version"
5
5
  require_relative "wardite/leb128"
6
6
  require_relative "wardite/const"
7
+ require_relative "wardite/float_bits"
7
8
  require_relative "wardite/instruction"
8
9
  require_relative "wardite/value"
10
+ require_relative "wardite/wasm_module"
9
11
  require_relative "wardite/load"
10
12
  require_relative "wardite/errors"
11
13
 
@@ -28,7 +30,7 @@ module Wardite
28
30
 
29
31
  attr_accessor :sections #: Array[Section]
30
32
 
31
- attr_accessor :runtime #: Runtime
33
+ attr_accessor :runtime #: Runtime | Efflorescence::VM
32
34
 
33
35
  attr_accessor :types #: Array[Type]
34
36
 
@@ -36,13 +38,18 @@ module Wardite
36
38
 
37
39
  attr_accessor :exports #: Exports
38
40
 
41
+ attr_reader :tags #: Array[Wardite::Efflorescence::Tag]
42
+
39
43
  attr_reader :import_object #: Hash[Symbol, wasmModule]
40
44
 
41
45
  attr_accessor :wasi #: ::Wardite::WasiSnapshotPreview1?
42
46
 
43
47
  # @rbs import_object: Hash[Symbol, wasmModuleSrc]
48
+ # @rbs engine: Symbol
49
+ # @rbs disabled_optimizations: Array[Symbol]
50
+ # @rbs mir_profile: bool
44
51
  # @rbs &blk: (Instance) -> void
45
- def initialize(import_object, &blk)
52
+ def initialize(import_object, engine: :stack, disabled_optimizations: [], mir_profile: false, &blk)
46
53
  @wasi = nil
47
54
 
48
55
  blk.call(self)
@@ -54,9 +61,11 @@ module Wardite
54
61
 
55
62
  @import_object = import_object #: Hash[Symbol, wasmModule]
56
63
 
64
+ @tags = []
65
+ check_engine_features(engine)
66
+ check_data_count
57
67
  @store = Store.new(self)
58
68
  @exports = Exports.new(self.export_section, store)
59
- @runtime = Runtime.new(self)
60
69
 
61
70
  @types = []
62
71
  type_section = self.type_section
@@ -67,17 +76,95 @@ module Wardite
67
76
  end
68
77
  end
69
78
 
70
- check_data_count
79
+ @runtime = case engine
80
+ when :stack then Runtime.new(self)
81
+ when :efflorescence then Efflorescence::VM.new(self, disabled_optimizations: disabled_optimizations, profile: mir_profile)
82
+ else raise LoadError, "unknown engine: #{engine}"
83
+ end
84
+ end
85
+
86
+ # Run before allocating Store/Exports or executing a start function.
87
+ # @rbs engine: Symbol
88
+ # @rbs return: void
89
+ def check_engine_features(engine)
90
+ check_memory_initialization if engine == :efflorescence
91
+ types = type_section
92
+ if engine == :efflorescence
93
+ import_section.imports.each do |import|
94
+ params = types&.defined_types&.[](import.sig_index)
95
+ results = types&.defined_results&.[](import.sig_index)
96
+ raise LoadError, "invalid import type index #{import.sig_index}" unless params && results
97
+ Efflorescence::FunctionEntry.check_signature(params, results, "#{import.module_name}.#{import.name}")
98
+ end
99
+ end
100
+ tag_section = @sections.find { |section| section.is_a?(TagSection) }
101
+ if tag_section.is_a?(TagSection)
102
+ raise LoadError, "stack engine does not support tags/EH" unless engine == :efflorescence
103
+ tag_section.type_indices.each do |index|
104
+ params = types&.defined_types&.[](index)
105
+ results = types&.defined_results&.[](index)
106
+ raise LoadError, "invalid tag type index #{index}" unless params && results
107
+ unless params.all? { |type| Efflorescence::FunctionEntry.numeric_type?(type) } && results.empty?
108
+ raise LoadError, "only numeric tag payloads with no results are supported"
109
+ end
110
+ @tags << Efflorescence::Tag.new(params)
111
+ end
112
+ end
113
+ if engine == :stack && types && (types.defined_types + types.defined_results).any? { |list| list.include?(:exnref) }
114
+ raise LoadError, "stack engine does not support exnref"
115
+ end
116
+ codes = code_section
117
+ return unless codes
118
+ codes.func_codes.each do |body|
119
+ if engine == :stack && body.locals_type.include?(:exnref)
120
+ raise LoadError, "stack engine does not support exnref"
121
+ end
122
+ body.body.each do |instruction|
123
+ code = instruction[1]
124
+ if engine == :stack && [:try_table, :throw, :throw_ref].include?(code)
125
+ raise LoadError, "stack engine does not support EH"
126
+ end
127
+ instruction[2].each do |operand|
128
+ next unless operand.is_a?(Block)
129
+ index = operand.type_index
130
+ if index >= 0
131
+ params = types&.defined_types&.[](index)
132
+ results = types&.defined_results&.[](index)
133
+ raise LoadError, "invalid block type index #{index}" unless params && results
134
+ operand.resolve(params, results)
135
+ end
136
+ if engine == :stack && (index >= 0 || operand.result_types.include?(:exnref))
137
+ raise LoadError, "stack engine does not support indexed/reference block signatures"
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ # Validate active segments before shared initialization can mutate memory.
145
+ # @rbs return: void
146
+ def check_memory_initialization
147
+ segments = data_section&.segments
148
+ return unless segments
149
+ limits = memory_section&.limits
150
+ segments.each do |segment|
151
+ next unless segment.mode == :active
152
+ unless limits && limits.size == 1 && segment.mem_index == 0
153
+ raise LoadError, "active data segment requires memory 0"
154
+ end
155
+ size = limits.fetch(0).fetch(0) * 65536
156
+ start = segment.offset
157
+ unless start >= 0 && start <= size && segment.data.bytesize <= size - start
158
+ raise LoadError, "active data segment out of bounds"
159
+ end
160
+ end
71
161
  end
72
162
 
73
163
  # @rbs return: void
74
164
  def check_data_count
75
165
  data_count = self.data_count_section&.count
76
166
  if data_count
77
- actual_count = self.data_section&.segments&.size
78
- if !actual_count
79
- raise LoadError, "invalid data segment count"
80
- end
167
+ actual_count = self.data_section&.segments&.size || 0
81
168
  if (data_count != actual_count)
82
169
  raise LoadError, "invalid data segment count"
83
170
  end
@@ -425,7 +512,7 @@ module Wardite
425
512
  break
426
513
  end
427
514
  cur_frame.pc += 1
428
- insn = cur_frame.body[cur_frame.pc] #: Op
515
+ insn = cur_frame.body[cur_frame.pc]
429
516
  if !insn
430
517
  break
431
518
  end
@@ -434,37 +521,38 @@ module Wardite
434
521
  end
435
522
 
436
523
  # @rbs frame: Frame
437
- # @rbs insn: Op
524
+ # @rbs insn: [Symbol, Symbol, Array[operandItem], Integer?, Integer?]
438
525
  # @rbs return: void
439
526
  def eval_insn(frame, insn)
440
- case insn.namespace
527
+ namespace, code, operand, else_pos, end_pos = *insn
528
+ case namespace
441
529
  when :convert
442
- return Evaluator.convert_eval_insn(self, frame, insn)
530
+ return Evaluator.convert_eval_insn(self, frame, code, operand)
443
531
  when :i32
444
- return Evaluator.i32_eval_insn(self, frame, insn)
532
+ return Evaluator.i32_eval_insn(self, frame, code, operand)
445
533
  when :i64
446
- return Evaluator.i64_eval_insn(self, frame, insn)
534
+ return Evaluator.i64_eval_insn(self, frame, code, operand)
447
535
  when :f32
448
- return Evaluator.f32_eval_insn(self, frame, insn)
536
+ return Evaluator.f32_eval_insn(self, frame, code, operand)
449
537
  when :f64
450
- return Evaluator.f64_eval_insn(self, frame, insn)
538
+ return Evaluator.f64_eval_insn(self, frame, code, operand)
451
539
  end
452
540
 
453
541
  # unmached namespace...
454
- case insn.code
542
+ case code
455
543
  when :unreachable
456
544
  raise Unreachable, "unreachable op"
457
545
  when :nop
458
546
  return
459
547
 
460
548
  when :br
461
- level = insn.operand[0]
549
+ level = operand[0]
462
550
  raise EvalError, "br op without level" if !level.is_a?(Integer)
463
551
  pc = do_branch(frame.labels, stack, level)
464
552
  frame.pc = pc
465
553
 
466
554
  when :br_if
467
- level = insn.operand[0]
555
+ level = operand[0]
468
556
  raise EvalError, "br op without level" if !level.is_a?(Integer)
469
557
  cond = stack.pop
470
558
  raise EvalError, "cond not found" if !cond.is_a?(I32)
@@ -475,9 +563,10 @@ module Wardite
475
563
  frame.pc = pc
476
564
 
477
565
  when :br_table
478
- level_vec = insn.operand[0]
479
- raise EvalError, "no level vector" if !level_vec.is_a?(Array)
480
- default = insn.operand[1]
566
+ levels = operand[0]
567
+ raise EvalError, "no level vector" unless levels.is_a?(OperandVector)
568
+ level_vec = levels.entries
569
+ default = operand[1]
481
570
  raise EvalError, "no default specified" if !default.is_a?(Integer)
482
571
  idx = stack.pop
483
572
  raise EvalError, "idx not found" if !idx.is_a?(I32)
@@ -490,29 +579,29 @@ module Wardite
490
579
  frame.pc = pc
491
580
 
492
581
  when :block
493
- block = insn.operand[0]
582
+ block = operand[0]
494
583
  raise EvalError, "block op without block" if !block.is_a?(Block)
495
- next_pc = insn.meta[:end_pos]
584
+ next_pc = end_pos || -1
496
585
  label = Label.new(:block, next_pc, stack.size, block.result_size)
497
586
  frame.labels.push(label)
498
587
 
499
588
  when :loop
500
- block = insn.operand[0]
589
+ block = operand[0]
501
590
  raise EvalError, "loop op without block" if !block.is_a?(Block)
502
591
  start = frame.pc
503
- next_pc = insn.meta[:end_pos]
592
+ next_pc = end_pos || -1
504
593
  label = Label.new(:loop, next_pc, stack.size, block.result_size, start)
505
594
  frame.labels.push(label)
506
595
 
507
596
  when :if
508
- block = insn.operand[0]
597
+ block = operand[0]
509
598
  raise EvalError, "if op without block" if !block.is_a?(Block)
510
599
  cond = stack.pop
511
600
  raise EvalError, "cond not found" if !cond.is_a?(I32)
512
- next_pc = insn.meta[:end_pos]
601
+ next_pc = end_pos || -1
513
602
 
514
603
  if cond.value.zero?
515
- frame.pc = insn.meta[:else_pos]
604
+ frame.pc = else_pos || -1
516
605
  end
517
606
 
518
607
  if frame.pc == next_pc
@@ -532,7 +621,7 @@ module Wardite
532
621
  end
533
622
 
534
623
  when :call
535
- idx = insn.operand[0]
624
+ idx = operand[0]
536
625
  raise EvalError, "[BUG] local operand not found" if !idx.is_a?(Integer)
537
626
  fn = self.instance.store.funcs[idx]
538
627
  case fn
@@ -548,9 +637,9 @@ module Wardite
548
637
  when :call_indirect
549
638
  table = self.instance.store.tables[0]
550
639
  raise EvalError, "table required but not found" if !table
551
- type_idx = insn.operand[0]
640
+ type_idx = operand[0]
552
641
  raise EvalError, "[BUG] index operand invalid" if !type_idx.is_a?(Integer)
553
- nullbyte = insn.operand[1]
642
+ nullbyte = operand[1]
554
643
  raise EvalError, "[BUG] invalid bytearray of call_indirect" if nullbyte != 0x0
555
644
  table_idx = stack.pop
556
645
  raise EvalError, "[BUG] index stack invalid" if !table_idx.is_a?(I32)
@@ -618,7 +707,7 @@ module Wardite
618
707
  stack.push(cond.value != 0 ? left : right)
619
708
 
620
709
  when :local_get
621
- idx = insn.operand[0]
710
+ idx = operand[0]
622
711
  if !idx.is_a?(Integer)
623
712
  raise EvalError, "[BUG] invalid type of operand"
624
713
  end
@@ -630,7 +719,7 @@ module Wardite
630
719
  stack.push(local)
631
720
 
632
721
  when :local_set
633
- idx = insn.operand[0]
722
+ idx = operand[0]
634
723
  if !idx.is_a?(Integer)
635
724
  raise EvalError, "[BUG] invalid type of operand"
636
725
  end
@@ -641,7 +730,7 @@ module Wardite
641
730
  frame.locals[idx] = value
642
731
 
643
732
  when :local_tee
644
- idx = insn.operand[0]
733
+ idx = operand[0]
645
734
  if !idx.is_a?(Integer)
646
735
  raise EvalError, "[BUG] invalid type of operand"
647
736
  end
@@ -653,7 +742,7 @@ module Wardite
653
742
  stack.push value
654
743
 
655
744
  when :global_get
656
- idx = insn.operand[0]
745
+ idx = operand[0]
657
746
  if !idx.is_a?(Integer)
658
747
  raise EvalError, "[BUG] invalid type of operand"
659
748
  end
@@ -664,7 +753,7 @@ module Wardite
664
753
  stack.push(global.value)
665
754
 
666
755
  when :global_set
667
- idx = insn.operand[0]
756
+ idx = operand[0]
668
757
  if !idx.is_a?(Integer)
669
758
  raise EvalError, "[BUG] invalid type of operand"
670
759
  end
@@ -695,11 +784,11 @@ module Wardite
695
784
  stack.push(I32(memory.grow(delta.value)))
696
785
 
697
786
  when :memory_init
698
- idx = insn.operand[0]
787
+ idx = operand[0]
699
788
  if !idx.is_a?(Integer)
700
789
  raise EvalError, "[BUG] invalid type of operand"
701
790
  end
702
- if insn.operand[1] != 0x0
791
+ if operand[1] != 0x0
703
792
  $stderr.puts "warning: :memory_init is not ending with 0x00"
704
793
  end
705
794
  data_sec = instance.data_section
@@ -721,7 +810,7 @@ module Wardite
721
810
  memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
722
811
 
723
812
  when :memory_copy
724
- if insn.operand[0] != 0x0 || insn.operand[1] != 0x0
813
+ if operand[0] != 0x0 || operand[1] != 0x0
725
814
  $stderr.puts "warning: :memory_copy is not ending with 0x00"
726
815
  end
727
816
  length, src_offset, dest_offset = stack.pop, stack.pop, stack.pop
@@ -734,7 +823,7 @@ module Wardite
734
823
  memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
735
824
 
736
825
  when :memory_fill
737
- if insn.operand[0] != 0x0
826
+ if operand[0] != 0x0
738
827
  $stderr.puts "warning: :memory_fill is not ending with 0x00"
739
828
  end
740
829
  length, byte, dest_offset = stack.pop, stack.pop, stack.pop
@@ -827,8 +916,9 @@ module Wardite
827
916
  end
828
917
 
829
918
  # @rbs name: String|Symbol
919
+ # @rbs include_private: bool
830
920
  # @rbs return: bool
831
- def respond_to? name
921
+ def respond_to?(name, include_private = false)
832
922
  callable?(name) || super
833
923
  end
834
924
 
@@ -846,7 +936,7 @@ module Wardite
846
936
 
847
937
  # @rbs callsig: Array[Symbol]
848
938
  # @rbs retsig: Array[Symbol]
849
- # @rbs returb: void
939
+ # @rbs return: void
850
940
  def initialize(callsig, retsig)
851
941
  @callsig = callsig
852
942
  @retsig = retsig
@@ -857,7 +947,7 @@ module Wardite
857
947
  attr_accessor :pc #: Integer
858
948
  attr_accessor :sp #: Integer
859
949
 
860
- attr_accessor :body #: Array[Op]
950
+ attr_accessor :body #: Array[[Symbol, Symbol, Array[Wardite::operandItem], Integer?, Integer?]]
861
951
 
862
952
  attr_accessor :arity #: Integer
863
953
 
@@ -869,10 +959,10 @@ module Wardite
869
959
 
870
960
  # @rbs pc: Integer
871
961
  # @rbs sp: Integer
872
- # @rbs body: Array[Op]
962
+ # @rbs body: Array[[Symbol, Symbol, Array[Wardite::operandItem], Integer?, Integer?]]
873
963
  # @rbs arity: Integer
874
964
  # @rbs locals: Array[wasmValue]
875
- # @rbs returb: void
965
+ # @rbs return: void
876
966
  def initialize(pc, sp, body, arity, locals)
877
967
  @pc = pc
878
968
  @sp = sp
@@ -917,6 +1007,8 @@ module Wardite
917
1007
 
918
1008
  attr_accessor :memories #: Array[Memory]
919
1009
 
1010
+ attr_reader :data_segments #: Array[String]
1011
+
920
1012
  attr_accessor :globals #: Array[Global]
921
1013
 
922
1014
  attr_accessor :tables #: Array[Table]
@@ -934,27 +1026,30 @@ module Wardite
934
1026
  @funcs = []
935
1027
  @modules = inst.import_object
936
1028
 
937
- if type_section && func_section && code_section
938
- import_section.imports.each do |desc|
939
- callsig = type_section.defined_types[desc.sig_index]
940
- retsig = type_section.defined_results[desc.sig_index]
941
- target_module = self.modules[desc.module_name.to_sym]
942
- if target_module.nil?
943
- raise ::NameError, "module #{desc.module_name} not found"
944
- end
945
- target_name = desc.name.to_sym
946
-
947
- ext_function = ExternalFunction.new(target_module, target_name, callsig, retsig)
948
- self.funcs << ext_function
949
- end
1029
+ import_section.imports.each do |desc|
1030
+ callsig = type_section&.defined_types&.[](desc.sig_index)
1031
+ retsig = type_section&.defined_results&.[](desc.sig_index)
1032
+ raise LoadError, "invalid import type index #{desc.sig_index}" unless callsig && retsig
1033
+
1034
+ module_name = desc.module_name.to_sym
1035
+ target_module = self.modules.fetch(module_name) { self.modules.default(module_name) }
1036
+ raise ::NameError, "module #{desc.module_name} not found" unless target_module
950
1037
 
1038
+ self.funcs << ExternalFunction.new(target_module, desc.name.to_sym, callsig, retsig)
1039
+ end
1040
+
1041
+ if (func_section&.func_indices&.size || 0) != (code_section&.func_codes&.size || 0)
1042
+ raise LoadError, "function and code section lengths differ"
1043
+ end
1044
+ if func_section && code_section
951
1045
  func_section.func_indices.each_with_index do |sigindex, findex|
952
- callsig = type_section.defined_types[sigindex]
953
- retsig = type_section.defined_results[sigindex]
954
- codes = code_section.func_codes[findex]
1046
+ callsig = type_section&.defined_types&.[](sigindex)
1047
+ retsig = type_section&.defined_results&.[](sigindex)
1048
+ raise LoadError, "invalid function type index #{sigindex}" unless callsig && retsig
1049
+
1050
+ codes = code_section.func_codes.fetch(findex)
955
1051
  wasm_function = WasmFunction.new(callsig, retsig, codes)
956
- idx = self.funcs.size
957
- wasm_function.findex = idx
1052
+ wasm_function.findex = self.funcs.size
958
1053
  self.funcs << wasm_function
959
1054
  end
960
1055
  end
@@ -978,8 +1073,8 @@ module Wardite
978
1073
  end
979
1074
 
980
1075
  data_start = segment.offset
981
- data_end = segment.offset + segment.data.size
982
- if data_end > memory.data.size
1076
+ data_end = segment.offset + segment.data.bytesize
1077
+ if data_start < 0 || data_end > memory.data.bytesize
983
1078
  raise GenericError, "data too large for memory"
984
1079
  end
985
1080
 
@@ -988,6 +1083,16 @@ module Wardite
988
1083
  end
989
1084
  end
990
1085
 
1086
+ # Runtime segment instances differ from their syntax declarations: active
1087
+ # segments have already been consumed by initialization.
1088
+ @data_segments = []
1089
+ data_section = inst.data_section
1090
+ if data_section
1091
+ data_section.segments.each do |segment|
1092
+ @data_segments << (segment.mode == :passive ? segment.data.dup.b : "".b)
1093
+ end
1094
+ end
1095
+
991
1096
  @globals = []
992
1097
  global_section = inst.global_section
993
1098
  if global_section
@@ -1018,36 +1123,19 @@ module Wardite
1018
1123
  elem_section = inst.elem_section
1019
1124
  if elem_section
1020
1125
  elem_section.table_indices.each_with_index do |tidx, idx|
1021
- table = @tables[tidx]
1022
- if !table
1023
- raise LoadError, "invalid table index #{tidx}"
1024
- end
1025
- typ = table.type
1026
- offset = elem_section.table_offsets[idx]
1027
- if !offset
1028
- raise LoadError, "invalid element index #{idx}"
1126
+ table = @tables[tidx] || raise(LoadError, "invalid table index #{tidx}")
1127
+ offset = elem_section.table_offsets.fetch(idx)
1128
+ indices = elem_section.element_indices.fetch(idx)
1129
+ raise LoadError, "element segment requires a funcref table" unless table.type == :funcref
1130
+ if offset < 0 || offset > table.current || indices.size > table.current - offset
1131
+ raise LoadError, "active element segment out of bounds"
1029
1132
  end
1030
- indices = elem_section.element_indices[idx]
1031
- if !indices
1032
- raise LoadError, "invalid element index #{idx}"
1033
- end
1034
- elms = [typ, offset, indices] #: [Symbol, Integer, Array[Integer]]
1035
- @elements << elms
1036
- end
1037
- end
1038
-
1039
- @elements.each_with_index do |(typ, offset, indices), idx|
1040
- table = @tables[idx]
1041
- if !table
1042
- raise LoadError, "invalid table index #{idx}"
1043
- end
1044
- indices.each_with_index do |eidx, tidx|
1045
- case typ
1046
- when :funcref
1047
- table.set(offset + tidx, @funcs[eidx])
1048
- when :externref
1049
- raise NotImplementedError, "no support :externref"
1133
+ indices.each_with_index do |function_index, position|
1134
+ function = @funcs[function_index] if function_index >= 0
1135
+ raise LoadError, "invalid element function index #{function_index}" unless function
1136
+ table.set(offset + position, function)
1050
1137
  end
1138
+ @elements << [table.type, offset, indices]
1051
1139
  end
1052
1140
  end
1053
1141
  end
@@ -1069,7 +1157,7 @@ module Wardite
1069
1157
  # @rbs max: Integer|nil
1070
1158
  # @rbs return: void
1071
1159
  def initialize(min, max)
1072
- @data = String.new("\0" * (min * 64 * 1024))
1160
+ @data = String.new("\0" * (min * 64 * 1024)).b
1073
1161
  @current = min
1074
1162
  @max = max
1075
1163
  end
@@ -1118,8 +1206,8 @@ module Wardite
1118
1206
  # @rbs elem: WasmFunction|ExternalFunction|nil
1119
1207
  # @rbs return: void
1120
1208
  def set(idx, elem)
1121
- if idx >= @current
1122
- raise GenericError, "idx too large for table"
1209
+ if idx < 0 || idx >= @current
1210
+ raise GenericError, "table index out of bounds"
1123
1211
  end
1124
1212
  @refs[idx] = elem
1125
1213
  end
@@ -1163,6 +1251,9 @@ module Wardite
1163
1251
  VOID = nil #: nil
1164
1252
 
1165
1253
  attr_accessor :block_types #: nil|Array[Integer]
1254
+ attr_reader :type_index #: Integer
1255
+ attr_reader :parameter_types #: Array[Symbol]
1256
+ attr_reader :result_types #: Array[Symbol]
1166
1257
 
1167
1258
  # @rbs return: Block
1168
1259
  def self.void
@@ -1170,9 +1261,21 @@ module Wardite
1170
1261
  end
1171
1262
 
1172
1263
  # @rbs block_types: nil|Array[Integer]
1264
+ # @rbs type_index: Integer
1173
1265
  # @rbs return: void
1174
- def initialize(block_types=VOID)
1266
+ def initialize(block_types=VOID, type_index: -1)
1175
1267
  @block_types = block_types
1268
+ @type_index = type_index
1269
+ @parameter_types = []
1270
+ @result_types = (block_types || []).map { |code| Op.i2type(code) }
1271
+ end
1272
+
1273
+ # @rbs params: Array[Symbol]
1274
+ # @rbs results: Array[Symbol]
1275
+ # @rbs return: void
1276
+ def resolve(params, results)
1277
+ @parameter_types = params.dup
1278
+ @result_types = results.dup
1176
1279
  end
1177
1280
 
1178
1281
  # @rbs return: bool
@@ -1222,7 +1325,10 @@ module Wardite
1222
1325
  @mappings[name]&.[](1)
1223
1326
  end
1224
1327
 
1225
- def respond_to?(name)
1328
+ # @rbs name: String|Symbol
1329
+ # @rbs include_private: bool
1330
+ # @rbs return: bool
1331
+ def respond_to?(name, include_private = false)
1226
1332
  !!self[name.to_s] || super
1227
1333
  end
1228
1334
 
@@ -1248,7 +1354,7 @@ module Wardite
1248
1354
 
1249
1355
  attr_accessor :findex #: Integer
1250
1356
 
1251
- attr_accessor :default_locals #: Array[wasmValue]
1357
+ # @rbs @default_locals: Array[wasmValue]?
1252
1358
 
1253
1359
  # @rbs callsig: Array[Symbol]
1254
1360
  # @rbs retsig: Array[Symbol]
@@ -1260,10 +1366,10 @@ module Wardite
1260
1366
 
1261
1367
  @code_body = code_body
1262
1368
  @findex = 0 # for debug
1263
- @default_locals = construct_default_locals
1369
+ @default_locals = nil
1264
1370
  end
1265
1371
 
1266
- # @rbs return: Array[Op]
1372
+ # @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
1267
1373
  def body
1268
1374
  code_body.body
1269
1375
  end
@@ -1271,13 +1377,19 @@ module Wardite
1271
1377
  # @rbs return: Array[Symbol]
1272
1378
  def locals_type
1273
1379
  code_body.locals_type
1274
- end
1380
+ end
1275
1381
 
1276
1382
  # @rbs return: Array[Integer]
1277
1383
  def locals_count
1278
1384
  code_body.locals_count
1279
1385
  end
1280
1386
 
1387
+ # Stack-engine values are boxed lazily; Efflorescence uses type metadata.
1388
+ # @rbs return: Array[wasmValue]
1389
+ def default_locals
1390
+ @default_locals ||= construct_default_locals
1391
+ end
1392
+
1281
1393
  # @rbs return: Array[wasmValue]
1282
1394
  def construct_default_locals
1283
1395
  locals = [] #: Array[wasmValue]
@@ -1330,7 +1442,8 @@ module Wardite
1330
1442
 
1331
1443
  # @rbs callsig: Array[Symbol]
1332
1444
  # @rbs retsig: Array[Symbol]
1333
- # @rbs callable: _WasmCallable
1445
+ # @rbs target_module: wasmModule
1446
+ # @rbs name: Symbol
1334
1447
  # @rbs return: void
1335
1448
  def initialize(target_module, name, callsig, retsig)
1336
1449
  @target_module = target_module
@@ -1370,9 +1483,11 @@ module Wardite
1370
1483
  if path
1371
1484
  buffer = File.open(path)
1372
1485
  end
1373
- if !buffer
1486
+ if buffer.nil?
1374
1487
  raise ::ArgumentError, "nil buffer passed"
1375
1488
  end
1376
1489
  Wardite::BinaryLoader::load_from_buffer(buffer, **options);
1377
1490
  end
1378
1491
  end
1492
+
1493
+ require_relative "wardite/efflorescence"