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.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +107 -2
- data/Rakefile +5 -0
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/README.md +500 -0
- data/benchmark/build_call_profile.rb +42 -0
- data/benchmark/call_cost_worker.rb +27 -0
- data/benchmark/compare.rb +93 -0
- data/benchmark/compare_call_types.rb +212 -0
- data/benchmark/compare_dispatch.rb +123 -0
- data/benchmark/compare_engines.rb +119 -0
- data/benchmark/compare_optimizations.rb +139 -0
- data/benchmark/engine_phases.rb +30 -0
- data/benchmark/figures/README.md +35 -0
- data/benchmark/figures/plot_spinel_comparison.py +137 -0
- data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
- data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
- data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
- data/benchmark/figures/spinel_new_vm_summary.json +82 -0
- data/benchmark/float_m5.wat +42 -0
- data/benchmark/i32_call_arities.wat +89 -0
- data/benchmark/i32_calls.wat +174 -0
- data/benchmark/i32_memory.wat +58 -0
- data/benchmark/i32_mix.wat +80 -0
- data/benchmark/i64_m4.wat +55 -0
- data/benchmark/investigate_calls.rb +231 -0
- data/benchmark/investigate_generated_guards.rb +218 -0
- data/benchmark/m3_call_worker.rb +38 -0
- data/benchmark/measure_m1.rb +51 -0
- data/benchmark/measure_m2.rb +55 -0
- data/benchmark/measure_m3_calls.rb +89 -0
- data/benchmark/measure_m4.rb +134 -0
- data/benchmark/measure_m5_boundary.rb +41 -0
- data/benchmark/measure_mir_cache_verify.rb +58 -0
- data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
- data/benchmark/measure_ruby_wasm_startup.rb +43 -0
- data/benchmark/measure_wasi_write.rb +75 -0
- data/benchmark/optimization_worker.rb +23 -0
- data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
- data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
- data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
- data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
- data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
- data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
- data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
- data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
- data/benchmark/results/efflorescence-m1-cruby.json +799 -0
- data/benchmark/results/efflorescence-m1-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
- data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
- data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-memory.json +408 -0
- data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
- data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
- data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
- data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
- data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
- data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
- data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
- data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
- data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
- data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
- data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
- data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
- data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
- data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
- data/benchmark/summarize_call_profile.py +54 -0
- data/benchmark/validate_m1_eh.rb +34 -0
- data/benchmark/validate_m2.rb +119 -0
- data/benchmark/validate_m3.rb +44 -0
- data/benchmark/validate_m4.rb +95 -0
- data/benchmark/validate_m5.rb +108 -0
- data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
- data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
- data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
- data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
- data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
- data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
- data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
- data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
- data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
- data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
- data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
- data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
- data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
- data/docs/efflorescence-call-arities.md +115 -0
- data/docs/efflorescence-call-investigation.md +155 -0
- data/docs/efflorescence-call-transfer.md +128 -0
- data/docs/efflorescence-call-types.md +102 -0
- data/docs/efflorescence-coverage-ja.md +351 -0
- data/docs/efflorescence-generated-c-guards.md +410 -0
- data/docs/efflorescence-inlining.md +165 -0
- data/docs/efflorescence-ja.md +155 -0
- data/docs/efflorescence-m1.md +160 -0
- data/docs/efflorescence-m2.md +233 -0
- data/docs/efflorescence-m3.md +131 -0
- data/docs/efflorescence-m4.md +88 -0
- data/docs/efflorescence-m5.md +82 -0
- data/docs/efflorescence-m6.md +95 -0
- data/docs/efflorescence-mir2-cache.md +123 -0
- data/docs/efflorescence-operand-decoding.md +105 -0
- data/docs/efflorescence-phase2-ja.md +225 -0
- data/docs/efflorescence-phase2.md +240 -0
- data/docs/efflorescence-register-initialization.md +75 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +72 -0
- data/docs/efflorescence-startup-snapshot.md +60 -0
- data/docs/efflorescence-unboxed-globals.md +61 -0
- data/docs/efflorescence.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/lib/wardite/alu_f32.generated.rb +11 -2
- data/lib/wardite/alu_f64.generated.rb +11 -2
- data/lib/wardite/alu_i32.generated.rb +54 -17
- data/lib/wardite/alu_i64.generated.rb +73 -31
- data/lib/wardite/cli/cli.rb +112 -29
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
- data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
- data/lib/wardite/efflorescence/inspection.rb +165 -0
- data/lib/wardite/efflorescence/ir.rb +1164 -0
- data/lib/wardite/efflorescence/mir_cache.rb +657 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +362 -0
- data/lib/wardite/efflorescence/snapshot.rb +229 -0
- data/lib/wardite/efflorescence/store_access.rb +366 -0
- data/lib/wardite/efflorescence/translator.rb +866 -0
- data/lib/wardite/efflorescence/vm.rb +1170 -0
- data/lib/wardite/efflorescence/wasi.rb +52 -0
- data/lib/wardite/efflorescence.rb +17 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +292 -143
- data/lib/wardite/value.rb +169 -175
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/consts.rb +1 -0
- data/lib/wardite/wasi/spinel.rb +41 -0
- data/lib/wardite/wasi.rb +483 -268
- data/lib/wardite/wasm_module.rb +6 -3
- data/lib/wardite.rb +360 -98
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +121 -21
- data/scripts/verify_ruby_wasm.rb +70 -0
- data/sig/generated/wardite/cli/cli.rbs +23 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
- data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
- data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
- data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +59 -7
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite/wasi/spinel.rbs +15 -0
- data/sig/generated/wardite/wasi.rbs +111 -4
- data/sig/generated/wardite.rbs +114 -13
- data/sig/mir_cache_stdlib.rbs +5 -0
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
- data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
- data/spinel/spin.toml +7 -0
- metadata +154 -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,19 @@ 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
|
|
51
|
+
# @rbs mir_cache: Efflorescence::MIRCache?
|
|
44
52
|
# @rbs &blk: (Instance) -> void
|
|
45
|
-
def initialize(import_object, &blk)
|
|
53
|
+
def initialize(import_object, engine: :stack, disabled_optimizations: [], mir_profile: false, mir_cache: nil, &blk)
|
|
46
54
|
@wasi = nil
|
|
47
55
|
|
|
48
56
|
blk.call(self)
|
|
@@ -54,9 +62,23 @@ module Wardite
|
|
|
54
62
|
|
|
55
63
|
@import_object = import_object #: Hash[Symbol, wasmModule]
|
|
56
64
|
|
|
65
|
+
@tags = []
|
|
66
|
+
check_module_features(engine)
|
|
67
|
+
cached_functions = nil #: Array[Efflorescence::Function]?
|
|
68
|
+
if engine == :efflorescence && mir_cache && !mir_profile
|
|
69
|
+
cached_functions = mir_cache.read(self)
|
|
70
|
+
end
|
|
71
|
+
types = type_section
|
|
72
|
+
code_section&.func_codes&.each do |body|
|
|
73
|
+
if cached_functions
|
|
74
|
+
body.type_section = types
|
|
75
|
+
else
|
|
76
|
+
body.check_features(types, engine == :stack)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
check_data_count
|
|
57
80
|
@store = Store.new(self)
|
|
58
81
|
@exports = Exports.new(self.export_section, store)
|
|
59
|
-
@runtime = Runtime.new(self)
|
|
60
82
|
|
|
61
83
|
@types = []
|
|
62
84
|
type_section = self.type_section
|
|
@@ -67,17 +89,90 @@ module Wardite
|
|
|
67
89
|
end
|
|
68
90
|
end
|
|
69
91
|
|
|
70
|
-
|
|
92
|
+
@runtime = case engine
|
|
93
|
+
when :stack then Runtime.new(self)
|
|
94
|
+
when :efflorescence
|
|
95
|
+
Efflorescence::VM.new(self, disabled_optimizations: disabled_optimizations, profile: mir_profile,
|
|
96
|
+
mir_cache: mir_cache, cached_functions: cached_functions, cache_checked: true)
|
|
97
|
+
else raise LoadError, "unknown engine: #{engine}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Run before allocating Store/Exports or executing a start function.
|
|
102
|
+
# @rbs engine: Symbol
|
|
103
|
+
# @rbs return: void
|
|
104
|
+
def check_module_features(engine)
|
|
105
|
+
check_memory_initialization if engine == :efflorescence
|
|
106
|
+
types = type_section
|
|
107
|
+
if engine == :efflorescence
|
|
108
|
+
import_section.imports.each do |import|
|
|
109
|
+
params = types&.defined_types&.[](import.sig_index)
|
|
110
|
+
results = types&.defined_results&.[](import.sig_index)
|
|
111
|
+
raise LoadError, "invalid import type index #{import.sig_index}" unless params && results
|
|
112
|
+
Efflorescence::FunctionEntry.check_signature(params, results, "#{import.module_name}.#{import.name}")
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
tag_section = @sections.find { |section| section.is_a?(TagSection) }
|
|
116
|
+
if tag_section.is_a?(TagSection)
|
|
117
|
+
raise LoadError, "stack engine does not support tags/EH" unless engine == :efflorescence
|
|
118
|
+
tag_section.type_indices.each do |index|
|
|
119
|
+
params = types&.defined_types&.[](index)
|
|
120
|
+
results = types&.defined_results&.[](index)
|
|
121
|
+
raise LoadError, "invalid tag type index #{index}" unless params && results
|
|
122
|
+
unless params.all? { |type| Efflorescence::FunctionEntry.numeric_type?(type) } && results.empty?
|
|
123
|
+
raise LoadError, "only numeric tag payloads with no results are supported"
|
|
124
|
+
end
|
|
125
|
+
@tags << Efflorescence::Tag.new(params)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
if engine == :stack && types && (types.defined_types + types.defined_results).any? { |list| list.include?(:exnref) }
|
|
129
|
+
raise LoadError, "stack engine does not support exnref"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# @rbs &block: (Array[Symbol], Array[Symbol], CodeSection::CodeBody) -> void
|
|
134
|
+
# @rbs return: void
|
|
135
|
+
def each_defined_function(&block)
|
|
136
|
+
functions = function_section
|
|
137
|
+
codes = code_section
|
|
138
|
+
if (functions&.func_indices&.size || 0) != (codes&.func_codes&.size || 0)
|
|
139
|
+
raise LoadError, "function and code section lengths differ"
|
|
140
|
+
end
|
|
141
|
+
return unless functions && codes
|
|
142
|
+
|
|
143
|
+
types = type_section
|
|
144
|
+
functions.func_indices.each_with_index do |index, position|
|
|
145
|
+
params = types&.defined_types&.[](index)
|
|
146
|
+
results = types&.defined_results&.[](index)
|
|
147
|
+
raise LoadError, "invalid function type index #{index}" unless params && results
|
|
148
|
+
block.call(params, results, codes.func_codes.fetch(position))
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Validate active segments before shared initialization can mutate memory.
|
|
153
|
+
# @rbs return: void
|
|
154
|
+
def check_memory_initialization
|
|
155
|
+
segments = data_section&.segments
|
|
156
|
+
return unless segments
|
|
157
|
+
limits = memory_section&.limits
|
|
158
|
+
segments.each do |segment|
|
|
159
|
+
next unless segment.mode == :active
|
|
160
|
+
unless limits && limits.size == 1 && segment.mem_index == 0
|
|
161
|
+
raise LoadError, "active data segment requires memory 0"
|
|
162
|
+
end
|
|
163
|
+
size = limits.fetch(0).fetch(0) * 65536
|
|
164
|
+
start = segment.offset
|
|
165
|
+
unless start >= 0 && start <= size && segment.data.bytesize <= size - start
|
|
166
|
+
raise LoadError, "active data segment out of bounds"
|
|
167
|
+
end
|
|
168
|
+
end
|
|
71
169
|
end
|
|
72
170
|
|
|
73
171
|
# @rbs return: void
|
|
74
172
|
def check_data_count
|
|
75
173
|
data_count = self.data_count_section&.count
|
|
76
174
|
if data_count
|
|
77
|
-
actual_count = self.data_section&.segments&.size
|
|
78
|
-
if !actual_count
|
|
79
|
-
raise LoadError, "invalid data segment count"
|
|
80
|
-
end
|
|
175
|
+
actual_count = self.data_section&.segments&.size || 0
|
|
81
176
|
if (data_count != actual_count)
|
|
82
177
|
raise LoadError, "invalid data segment count"
|
|
83
178
|
end
|
|
@@ -476,8 +571,9 @@ module Wardite
|
|
|
476
571
|
frame.pc = pc
|
|
477
572
|
|
|
478
573
|
when :br_table
|
|
479
|
-
|
|
480
|
-
raise EvalError, "no level vector"
|
|
574
|
+
levels = operand[0]
|
|
575
|
+
raise EvalError, "no level vector" unless levels.is_a?(OperandVector)
|
|
576
|
+
level_vec = levels.entries
|
|
481
577
|
default = operand[1]
|
|
482
578
|
raise EvalError, "no default specified" if !default.is_a?(Integer)
|
|
483
579
|
idx = stack.pop
|
|
@@ -717,9 +813,13 @@ module Wardite
|
|
|
717
813
|
if !length.is_a?(I32) || !src_offset.is_a?(I32) || !dest_offset.is_a?(I32)
|
|
718
814
|
raise EvalError, "invalid stack values"
|
|
719
815
|
end
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
816
|
+
if RUBY_ENGINE == "spinel"
|
|
817
|
+
Efflorescence::StoreAccess.new(instance.store).init(idx, dest_offset.value, src_offset.value, length.value)
|
|
818
|
+
else
|
|
819
|
+
source = data_seg.data[src_offset.value...(src_offset.value+length.value)]
|
|
820
|
+
raise EvalError, "invalid source range" if !source
|
|
821
|
+
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
|
|
822
|
+
end
|
|
723
823
|
|
|
724
824
|
when :memory_copy
|
|
725
825
|
if operand[0] != 0x0 || operand[1] != 0x0
|
|
@@ -730,9 +830,13 @@ module Wardite
|
|
|
730
830
|
raise EvalError, "invalid stack values"
|
|
731
831
|
end
|
|
732
832
|
memory = instance.store.memories[0] || raise("[BUG] no memory")
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
833
|
+
if RUBY_ENGINE == "spinel"
|
|
834
|
+
Efflorescence::StoreAccess.new(instance.store).copy(dest_offset.value, src_offset.value, length.value)
|
|
835
|
+
else
|
|
836
|
+
source = memory.data[src_offset.value...(src_offset.value+length.value)]
|
|
837
|
+
raise EvalError, "invalid source range" if !source
|
|
838
|
+
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
|
|
839
|
+
end
|
|
736
840
|
|
|
737
841
|
when :memory_fill
|
|
738
842
|
if operand[0] != 0x0
|
|
@@ -743,8 +847,12 @@ module Wardite
|
|
|
743
847
|
raise EvalError, "invalid stack values"
|
|
744
848
|
end
|
|
745
849
|
memory = instance.store.memories[0] || raise("[BUG] no memory")
|
|
746
|
-
|
|
747
|
-
|
|
850
|
+
if RUBY_ENGINE == "spinel"
|
|
851
|
+
Efflorescence::StoreAccess.new(instance.store).fill(dest_offset.value, byte.value, length.value)
|
|
852
|
+
else
|
|
853
|
+
source = byte.value.chr * length.value
|
|
854
|
+
memory.data[dest_offset.value...(dest_offset.value+length.value)] = source
|
|
855
|
+
end
|
|
748
856
|
|
|
749
857
|
else
|
|
750
858
|
raise "TODO! unsupported #{insn.inspect}"
|
|
@@ -828,8 +936,9 @@ module Wardite
|
|
|
828
936
|
end
|
|
829
937
|
|
|
830
938
|
# @rbs name: String|Symbol
|
|
939
|
+
# @rbs include_private: bool
|
|
831
940
|
# @rbs return: bool
|
|
832
|
-
def respond_to?
|
|
941
|
+
def respond_to?(name, include_private = false)
|
|
833
942
|
callable?(name) || super
|
|
834
943
|
end
|
|
835
944
|
|
|
@@ -847,7 +956,7 @@ module Wardite
|
|
|
847
956
|
|
|
848
957
|
# @rbs callsig: Array[Symbol]
|
|
849
958
|
# @rbs retsig: Array[Symbol]
|
|
850
|
-
# @rbs
|
|
959
|
+
# @rbs return: void
|
|
851
960
|
def initialize(callsig, retsig)
|
|
852
961
|
@callsig = callsig
|
|
853
962
|
@retsig = retsig
|
|
@@ -873,7 +982,7 @@ module Wardite
|
|
|
873
982
|
# @rbs body: Array[[Symbol, Symbol, Array[Wardite::operandItem], Integer?, Integer?]]
|
|
874
983
|
# @rbs arity: Integer
|
|
875
984
|
# @rbs locals: Array[wasmValue]
|
|
876
|
-
# @rbs
|
|
985
|
+
# @rbs return: void
|
|
877
986
|
def initialize(pc, sp, body, arity, locals)
|
|
878
987
|
@pc = pc
|
|
879
988
|
@sp = sp
|
|
@@ -918,6 +1027,8 @@ module Wardite
|
|
|
918
1027
|
|
|
919
1028
|
attr_accessor :memories #: Array[Memory]
|
|
920
1029
|
|
|
1030
|
+
attr_reader :data_segments #: Array[String]
|
|
1031
|
+
|
|
921
1032
|
attr_accessor :globals #: Array[Global]
|
|
922
1033
|
|
|
923
1034
|
attr_accessor :tables #: Array[Table]
|
|
@@ -928,36 +1039,30 @@ module Wardite
|
|
|
928
1039
|
# @rbs return: void
|
|
929
1040
|
def initialize(inst)
|
|
930
1041
|
type_section = inst.type_section
|
|
931
|
-
func_section = inst.function_section
|
|
932
|
-
code_section = inst.code_section
|
|
933
1042
|
|
|
934
1043
|
import_section = inst.import_section
|
|
935
1044
|
@funcs = []
|
|
936
1045
|
@modules = inst.import_object
|
|
937
1046
|
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
target_module = self.modules[desc.module_name.to_sym]
|
|
943
|
-
if target_module.nil?
|
|
944
|
-
raise ::NameError, "module #{desc.module_name} not found"
|
|
945
|
-
end
|
|
946
|
-
target_name = desc.name.to_sym
|
|
947
|
-
|
|
948
|
-
ext_function = ExternalFunction.new(target_module, target_name, callsig, retsig)
|
|
949
|
-
self.funcs << ext_function
|
|
950
|
-
end
|
|
1047
|
+
import_section.imports.each do |desc|
|
|
1048
|
+
callsig = type_section&.defined_types&.[](desc.sig_index)
|
|
1049
|
+
retsig = type_section&.defined_results&.[](desc.sig_index)
|
|
1050
|
+
raise LoadError, "invalid import type index #{desc.sig_index}" unless callsig && retsig
|
|
951
1051
|
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1052
|
+
module_name = desc.module_name.to_sym
|
|
1053
|
+
target_module = self.modules.fetch(module_name) { self.modules.default(module_name) }
|
|
1054
|
+
raise ::NameError, "module #{desc.module_name} not found" unless target_module
|
|
1055
|
+
|
|
1056
|
+
name = desc.name.to_sym
|
|
1057
|
+
wasi = inst.wasi
|
|
1058
|
+
wasi.validate_import(name, callsig, retsig) if wasi && wasi.equal?(target_module)
|
|
1059
|
+
self.funcs << ExternalFunction.new(target_module, name, callsig, retsig)
|
|
1060
|
+
end
|
|
1061
|
+
|
|
1062
|
+
inst.each_defined_function do |callsig, retsig, body|
|
|
1063
|
+
wasm_function = WasmFunction.new(callsig, retsig, body)
|
|
1064
|
+
wasm_function.findex = self.funcs.size
|
|
1065
|
+
self.funcs << wasm_function
|
|
961
1066
|
end
|
|
962
1067
|
|
|
963
1068
|
@memories = []
|
|
@@ -978,17 +1083,25 @@ module Wardite
|
|
|
978
1083
|
raise GenericError, "invalid memory index: #{segment.mem_index}"
|
|
979
1084
|
end
|
|
980
1085
|
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
if data_end > memory.data.size
|
|
1086
|
+
payload = segment.data
|
|
1087
|
+
unless memory.range?(segment.offset, payload.bytesize)
|
|
984
1088
|
raise GenericError, "data too large for memory"
|
|
985
1089
|
end
|
|
986
|
-
|
|
987
|
-
memory.data[data_start...data_end] = segment.data
|
|
1090
|
+
memory.write_bytes(segment.offset, payload)
|
|
988
1091
|
end
|
|
989
1092
|
end
|
|
990
1093
|
end
|
|
991
1094
|
|
|
1095
|
+
# Runtime segment instances differ from their syntax declarations: active
|
|
1096
|
+
# segments have already been consumed by initialization.
|
|
1097
|
+
@data_segments = []
|
|
1098
|
+
data_section = inst.data_section
|
|
1099
|
+
if data_section
|
|
1100
|
+
data_section.segments.each do |segment|
|
|
1101
|
+
@data_segments << (segment.mode == :passive ? segment.data.dup.b : "".b)
|
|
1102
|
+
end
|
|
1103
|
+
end
|
|
1104
|
+
|
|
992
1105
|
@globals = []
|
|
993
1106
|
global_section = inst.global_section
|
|
994
1107
|
if global_section
|
|
@@ -1019,36 +1132,19 @@ module Wardite
|
|
|
1019
1132
|
elem_section = inst.elem_section
|
|
1020
1133
|
if elem_section
|
|
1021
1134
|
elem_section.table_indices.each_with_index do |tidx, idx|
|
|
1022
|
-
table = @tables[tidx]
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
if !offset
|
|
1029
|
-
raise LoadError, "invalid element index #{idx}"
|
|
1030
|
-
end
|
|
1031
|
-
indices = elem_section.element_indices[idx]
|
|
1032
|
-
if !indices
|
|
1033
|
-
raise LoadError, "invalid element index #{idx}"
|
|
1135
|
+
table = @tables[tidx] || raise(LoadError, "invalid table index #{tidx}")
|
|
1136
|
+
offset = elem_section.table_offsets.fetch(idx)
|
|
1137
|
+
indices = elem_section.element_indices.fetch(idx)
|
|
1138
|
+
raise LoadError, "element segment requires a funcref table" unless table.type == :funcref
|
|
1139
|
+
if offset < 0 || offset > table.current || indices.size > table.current - offset
|
|
1140
|
+
raise LoadError, "active element segment out of bounds"
|
|
1034
1141
|
end
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
@elements.each_with_index do |(typ, offset, indices), idx|
|
|
1041
|
-
table = @tables[idx]
|
|
1042
|
-
if !table
|
|
1043
|
-
raise LoadError, "invalid table index #{idx}"
|
|
1044
|
-
end
|
|
1045
|
-
indices.each_with_index do |eidx, tidx|
|
|
1046
|
-
case typ
|
|
1047
|
-
when :funcref
|
|
1048
|
-
table.set(offset + tidx, @funcs[eidx])
|
|
1049
|
-
when :externref
|
|
1050
|
-
raise NotImplementedError, "no support :externref"
|
|
1142
|
+
indices.each_with_index do |function_index, position|
|
|
1143
|
+
function = @funcs[function_index] if function_index >= 0
|
|
1144
|
+
raise LoadError, "invalid element function index #{function_index}" unless function
|
|
1145
|
+
table.set(offset + position, function)
|
|
1051
1146
|
end
|
|
1147
|
+
@elements << [table.type, offset, indices]
|
|
1052
1148
|
end
|
|
1053
1149
|
end
|
|
1054
1150
|
end
|
|
@@ -1060,37 +1156,176 @@ module Wardite
|
|
|
1060
1156
|
end
|
|
1061
1157
|
|
|
1062
1158
|
class Memory
|
|
1063
|
-
|
|
1159
|
+
PAGE_SIZE = 65536 #: Integer
|
|
1160
|
+
MAX_PAGES = 65536 #: Integer
|
|
1064
1161
|
|
|
1065
|
-
|
|
1162
|
+
# @rbs @data: String?
|
|
1163
|
+
# @rbs @buffer: IO::Buffer?
|
|
1164
|
+
# @rbs @storage: IO::Buffer?
|
|
1066
1165
|
|
|
1166
|
+
attr_accessor :current #: Integer
|
|
1067
1167
|
attr_accessor :max #: Integer|nil
|
|
1068
1168
|
|
|
1069
1169
|
# @rbs min: Integer
|
|
1070
1170
|
# @rbs max: Integer|nil
|
|
1071
1171
|
# @rbs return: void
|
|
1072
1172
|
def initialize(min, max)
|
|
1073
|
-
|
|
1173
|
+
if RUBY_ENGINE == "spinel"
|
|
1174
|
+
buffer = IO::Buffer.new(min * PAGE_SIZE)
|
|
1175
|
+
@storage = @buffer = buffer
|
|
1176
|
+
else
|
|
1177
|
+
@data = String.new("\0" * (min * PAGE_SIZE)).b
|
|
1178
|
+
end
|
|
1074
1179
|
@current = min
|
|
1075
1180
|
@max = max
|
|
1076
1181
|
end
|
|
1077
1182
|
|
|
1183
|
+
# The CRuby host API retains the mutable backing String.
|
|
1184
|
+
# @rbs return: String
|
|
1185
|
+
def data
|
|
1186
|
+
@data || raise(NotImplementedError, "Spinel linear memory uses Memory#buffer")
|
|
1187
|
+
end
|
|
1188
|
+
|
|
1189
|
+
# @rbs value: String
|
|
1190
|
+
# @rbs return: String
|
|
1191
|
+
def data=(value)
|
|
1192
|
+
raise NotImplementedError, "Spinel linear memory uses Memory#buffer" if RUBY_ENGINE == "spinel"
|
|
1193
|
+
@data = value
|
|
1194
|
+
end
|
|
1195
|
+
|
|
1196
|
+
# The Spinel host API exposes only the current Wasm bytes. Reacquire it
|
|
1197
|
+
# after any guest call that can grow memory.
|
|
1198
|
+
# @rbs return: IO::Buffer
|
|
1199
|
+
def buffer
|
|
1200
|
+
@buffer || raise(NotImplementedError, "CRuby linear memory uses Memory#data")
|
|
1201
|
+
end
|
|
1202
|
+
|
|
1203
|
+
# @rbs return: Integer
|
|
1204
|
+
def bytesize
|
|
1205
|
+
RUBY_ENGINE == "spinel" ? buffer.size : data.bytesize
|
|
1206
|
+
end
|
|
1207
|
+
|
|
1208
|
+
# @rbs offset: Integer
|
|
1209
|
+
# @rbs length: Integer
|
|
1210
|
+
# @rbs return: bool
|
|
1211
|
+
def range?(offset, length)
|
|
1212
|
+
size = bytesize
|
|
1213
|
+
offset >= 0 && length >= 0 && offset <= size && length <= size - offset
|
|
1214
|
+
end
|
|
1215
|
+
|
|
1216
|
+
# @rbs offset: Integer
|
|
1217
|
+
# @rbs length: Integer
|
|
1218
|
+
# @rbs return: String
|
|
1219
|
+
def read_bytes(offset, length)
|
|
1220
|
+
raise EvalError, "invalid memory range" unless range?(offset, length)
|
|
1221
|
+
return "".b if length == 0
|
|
1222
|
+
if RUBY_ENGINE == "spinel"
|
|
1223
|
+
buffer.get_string(offset, length)
|
|
1224
|
+
else
|
|
1225
|
+
data.byteslice(offset, length) || raise(EvalError, "invalid memory range")
|
|
1226
|
+
end
|
|
1227
|
+
end
|
|
1228
|
+
|
|
1229
|
+
# @rbs offset: Integer
|
|
1230
|
+
# @rbs return: Integer
|
|
1231
|
+
def read_u32(offset)
|
|
1232
|
+
raise EvalError, "invalid memory range" unless range?(offset, 4)
|
|
1233
|
+
if RUBY_ENGINE == "spinel"
|
|
1234
|
+
Integer(buffer.get_value(:u32, offset))
|
|
1235
|
+
else
|
|
1236
|
+
Integer(data.unpack1("V", offset: offset))
|
|
1237
|
+
end
|
|
1238
|
+
end
|
|
1239
|
+
|
|
1240
|
+
# @rbs offset: Integer
|
|
1241
|
+
# @rbs bytes: String
|
|
1242
|
+
# @rbs return: void
|
|
1243
|
+
def write_bytes(offset, bytes)
|
|
1244
|
+
length = bytes.bytesize
|
|
1245
|
+
raise EvalError, "invalid memory range" unless range?(offset, length)
|
|
1246
|
+
return if length == 0
|
|
1247
|
+
if RUBY_ENGINE == "spinel"
|
|
1248
|
+
buffer.set_string(bytes, offset)
|
|
1249
|
+
else
|
|
1250
|
+
data[offset, length] = bytes
|
|
1251
|
+
end
|
|
1252
|
+
end
|
|
1253
|
+
|
|
1078
1254
|
# @rbs delta: Integer
|
|
1079
1255
|
# @rbs return: Integer
|
|
1080
1256
|
def grow(delta)
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1257
|
+
previous = current
|
|
1258
|
+
if RUBY_ENGINE == "spinel"
|
|
1259
|
+
declared = max
|
|
1260
|
+
limit = declared && declared < MAX_PAGES ? declared : MAX_PAGES
|
|
1261
|
+
return -1 if delta < 0 || previous > limit || delta > limit - previous
|
|
1262
|
+
return previous if delta == 0
|
|
1263
|
+
|
|
1264
|
+
return -1 unless grow_buffer(previous + delta, limit)
|
|
1265
|
+
else
|
|
1266
|
+
declared = max
|
|
1267
|
+
return -1 if declared && previous + delta > declared
|
|
1268
|
+
self.data = data + String.new("\0" * (delta * PAGE_SIZE))
|
|
1085
1269
|
end
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
self.current = newsize
|
|
1089
|
-
prev
|
|
1270
|
+
self.current = previous + delta
|
|
1271
|
+
previous
|
|
1090
1272
|
end
|
|
1091
1273
|
|
|
1092
1274
|
def inspect
|
|
1093
|
-
|
|
1275
|
+
preview = read_bytes(0, [bytesize, 64].min)
|
|
1276
|
+
"#<Wardite::Memory initial=#{bytesize.inspect} max=#{@max.inspect} preview=#{preview.inspect}...>"
|
|
1277
|
+
end
|
|
1278
|
+
|
|
1279
|
+
private
|
|
1280
|
+
|
|
1281
|
+
# @rbs pages: Integer
|
|
1282
|
+
# @rbs limit: Integer
|
|
1283
|
+
# @rbs return: bool
|
|
1284
|
+
def grow_buffer(pages, limit)
|
|
1285
|
+
old = buffer
|
|
1286
|
+
return false if old.locked?
|
|
1287
|
+
|
|
1288
|
+
storage = @storage
|
|
1289
|
+
raise "missing linear memory storage" unless storage
|
|
1290
|
+
size = pages * PAGE_SIZE
|
|
1291
|
+
if size <= storage.size
|
|
1292
|
+
# Move ownership without copying. Spinel slices point to their
|
|
1293
|
+
# original root, so the transfer also invalidates outstanding views.
|
|
1294
|
+
replace_storage(storage.transfer, size)
|
|
1295
|
+
return true
|
|
1296
|
+
end
|
|
1297
|
+
|
|
1298
|
+
spare = [pages / 8, 1].max
|
|
1299
|
+
capacity = [pages + spare, limit].min
|
|
1300
|
+
replacement = allocate_storage(capacity)
|
|
1301
|
+
replacement = allocate_storage(pages) if !replacement && capacity > pages
|
|
1302
|
+
return false unless replacement
|
|
1303
|
+
replacement.copy(old, 0, old.size) if old.size > 0
|
|
1304
|
+
replace_storage(replacement, size)
|
|
1305
|
+
true
|
|
1306
|
+
end
|
|
1307
|
+
|
|
1308
|
+
# @rbs replacement: IO::Buffer
|
|
1309
|
+
# @rbs size: Integer
|
|
1310
|
+
# @rbs return: void
|
|
1311
|
+
def replace_storage(replacement, size)
|
|
1312
|
+
# Allocation zeroes the entire storage. The hidden suffix stays zero
|
|
1313
|
+
# because all guest and host accesses go through the size-bounded view.
|
|
1314
|
+
view = replacement.size == size ? replacement : replacement.slice(0, size)
|
|
1315
|
+
storage = @storage
|
|
1316
|
+
old = buffer
|
|
1317
|
+
old.free unless old.equal?(storage)
|
|
1318
|
+
storage&.free
|
|
1319
|
+
@storage = replacement
|
|
1320
|
+
@buffer = view
|
|
1321
|
+
end
|
|
1322
|
+
|
|
1323
|
+
# @rbs pages: Integer
|
|
1324
|
+
# @rbs return: IO::Buffer?
|
|
1325
|
+
def allocate_storage(pages)
|
|
1326
|
+
IO::Buffer.new(pages * PAGE_SIZE)
|
|
1327
|
+
rescue NoMemoryError, Errno::ENOMEM, IO::Buffer::AllocationError
|
|
1328
|
+
nil
|
|
1094
1329
|
end
|
|
1095
1330
|
end
|
|
1096
1331
|
|
|
@@ -1119,8 +1354,8 @@ module Wardite
|
|
|
1119
1354
|
# @rbs elem: WasmFunction|ExternalFunction|nil
|
|
1120
1355
|
# @rbs return: void
|
|
1121
1356
|
def set(idx, elem)
|
|
1122
|
-
if idx >= @current
|
|
1123
|
-
raise GenericError, "
|
|
1357
|
+
if idx < 0 || idx >= @current
|
|
1358
|
+
raise GenericError, "table index out of bounds"
|
|
1124
1359
|
end
|
|
1125
1360
|
@refs[idx] = elem
|
|
1126
1361
|
end
|
|
@@ -1164,6 +1399,9 @@ module Wardite
|
|
|
1164
1399
|
VOID = nil #: nil
|
|
1165
1400
|
|
|
1166
1401
|
attr_accessor :block_types #: nil|Array[Integer]
|
|
1402
|
+
attr_reader :type_index #: Integer
|
|
1403
|
+
attr_reader :parameter_types #: Array[Symbol]
|
|
1404
|
+
attr_reader :result_types #: Array[Symbol]
|
|
1167
1405
|
|
|
1168
1406
|
# @rbs return: Block
|
|
1169
1407
|
def self.void
|
|
@@ -1171,9 +1409,21 @@ module Wardite
|
|
|
1171
1409
|
end
|
|
1172
1410
|
|
|
1173
1411
|
# @rbs block_types: nil|Array[Integer]
|
|
1412
|
+
# @rbs type_index: Integer
|
|
1174
1413
|
# @rbs return: void
|
|
1175
|
-
def initialize(block_types=VOID)
|
|
1414
|
+
def initialize(block_types=VOID, type_index: -1)
|
|
1176
1415
|
@block_types = block_types
|
|
1416
|
+
@type_index = type_index
|
|
1417
|
+
@parameter_types = []
|
|
1418
|
+
@result_types = (block_types || []).map { |code| Op.i2type(code) }
|
|
1419
|
+
end
|
|
1420
|
+
|
|
1421
|
+
# @rbs params: Array[Symbol]
|
|
1422
|
+
# @rbs results: Array[Symbol]
|
|
1423
|
+
# @rbs return: void
|
|
1424
|
+
def resolve(params, results)
|
|
1425
|
+
@parameter_types = params.dup
|
|
1426
|
+
@result_types = results.dup
|
|
1177
1427
|
end
|
|
1178
1428
|
|
|
1179
1429
|
# @rbs return: bool
|
|
@@ -1223,7 +1473,10 @@ module Wardite
|
|
|
1223
1473
|
@mappings[name]&.[](1)
|
|
1224
1474
|
end
|
|
1225
1475
|
|
|
1226
|
-
|
|
1476
|
+
# @rbs name: String|Symbol
|
|
1477
|
+
# @rbs include_private: bool
|
|
1478
|
+
# @rbs return: bool
|
|
1479
|
+
def respond_to?(name, include_private = false)
|
|
1227
1480
|
!!self[name.to_s] || super
|
|
1228
1481
|
end
|
|
1229
1482
|
|
|
@@ -1249,7 +1502,7 @@ module Wardite
|
|
|
1249
1502
|
|
|
1250
1503
|
attr_accessor :findex #: Integer
|
|
1251
1504
|
|
|
1252
|
-
|
|
1505
|
+
# @rbs @default_locals: Array[wasmValue]?
|
|
1253
1506
|
|
|
1254
1507
|
# @rbs callsig: Array[Symbol]
|
|
1255
1508
|
# @rbs retsig: Array[Symbol]
|
|
@@ -1261,7 +1514,7 @@ module Wardite
|
|
|
1261
1514
|
|
|
1262
1515
|
@code_body = code_body
|
|
1263
1516
|
@findex = 0 # for debug
|
|
1264
|
-
@default_locals =
|
|
1517
|
+
@default_locals = nil
|
|
1265
1518
|
end
|
|
1266
1519
|
|
|
1267
1520
|
# @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
@@ -1279,6 +1532,12 @@ module Wardite
|
|
|
1279
1532
|
code_body.locals_count
|
|
1280
1533
|
end
|
|
1281
1534
|
|
|
1535
|
+
# Stack-engine values are boxed lazily; Efflorescence uses type metadata.
|
|
1536
|
+
# @rbs return: Array[wasmValue]
|
|
1537
|
+
def default_locals
|
|
1538
|
+
@default_locals ||= construct_default_locals
|
|
1539
|
+
end
|
|
1540
|
+
|
|
1282
1541
|
# @rbs return: Array[wasmValue]
|
|
1283
1542
|
def construct_default_locals
|
|
1284
1543
|
locals = [] #: Array[wasmValue]
|
|
@@ -1331,7 +1590,8 @@ module Wardite
|
|
|
1331
1590
|
|
|
1332
1591
|
# @rbs callsig: Array[Symbol]
|
|
1333
1592
|
# @rbs retsig: Array[Symbol]
|
|
1334
|
-
# @rbs
|
|
1593
|
+
# @rbs target_module: wasmModule
|
|
1594
|
+
# @rbs name: Symbol
|
|
1335
1595
|
# @rbs return: void
|
|
1336
1596
|
def initialize(target_module, name, callsig, retsig)
|
|
1337
1597
|
@target_module = target_module
|
|
@@ -1371,9 +1631,11 @@ module Wardite
|
|
|
1371
1631
|
if path
|
|
1372
1632
|
buffer = File.open(path)
|
|
1373
1633
|
end
|
|
1374
|
-
if
|
|
1634
|
+
if buffer.nil?
|
|
1375
1635
|
raise ::ArgumentError, "nil buffer passed"
|
|
1376
1636
|
end
|
|
1377
1637
|
Wardite::BinaryLoader::load_from_buffer(buffer, **options);
|
|
1378
1638
|
end
|
|
1379
1639
|
end
|
|
1640
|
+
|
|
1641
|
+
require_relative "wardite/efflorescence"
|