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.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +80 -1
- data/Rakefile +5 -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_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/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/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 +405 -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-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.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/examples/load_perf.rb +24 -0
- data/lib/wardite/alu_f32.generated.rb +13 -10
- data/lib/wardite/alu_f64.generated.rb +13 -10
- data/lib/wardite/alu_i32.generated.rb +38 -31
- data/lib/wardite/alu_i64.generated.rb +48 -48
- data/lib/wardite/cli/cli.rb +73 -22
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/convert.generated.rb +7 -5
- 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 +1151 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +290 -0
- data/lib/wardite/efflorescence/store_access.rb +271 -0
- data/lib/wardite/efflorescence/translator.rb +859 -0
- data/lib/wardite/efflorescence/vm.rb +1006 -0
- data/lib/wardite/efflorescence/wasi.rb +108 -0
- data/lib/wardite/efflorescence.rb +11 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +460 -69
- data/lib/wardite/leb128.rb +35 -17
- data/lib/wardite/load.rb +260 -159
- data/lib/wardite/revisitor.rb +12 -4
- 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/wasm_module.rb +6 -3
- data/lib/wardite.rb +220 -105
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +90 -34
- data/scripts/templates/alu_module.rb.tmpl +7 -5
- data/scripts/templates/conv_module.rb.tmpl +7 -5
- data/sig/generated/wardite/alu_f32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_f64.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i64.generated.rbs +4 -3
- data/sig/generated/wardite/cli/cli.rbs +13 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/convert.generated.rbs +4 -3
- 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 +881 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +35 -10
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +38 -13
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +55 -18
- data/sig/vernier.rbs +3 -0
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +128 -3
data/lib/wardite/load.rb
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# rbs_inline: enabled
|
|
2
|
-
require "wardite/revisitor"
|
|
3
2
|
|
|
4
3
|
module Wardite
|
|
5
4
|
class Section
|
|
@@ -25,6 +24,16 @@ module Wardite
|
|
|
25
24
|
end
|
|
26
25
|
end
|
|
27
26
|
|
|
27
|
+
class TagSection < Section
|
|
28
|
+
attr_reader :type_indices #: Array[Integer]
|
|
29
|
+
|
|
30
|
+
def initialize
|
|
31
|
+
self.name = "Tag"
|
|
32
|
+
self.code = Const::SectionTag
|
|
33
|
+
@type_indices = []
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
28
37
|
class FunctionSection < Section
|
|
29
38
|
attr_accessor :func_indices #: Array[Integer]
|
|
30
39
|
|
|
@@ -105,11 +114,16 @@ module Wardite
|
|
|
105
114
|
end
|
|
106
115
|
|
|
107
116
|
class ElemSection < Section
|
|
117
|
+
# @rbs!
|
|
118
|
+
# type indexVector = Array[Integer]
|
|
119
|
+
|
|
108
120
|
attr_accessor :table_indices #: Array[Integer]
|
|
109
121
|
|
|
110
122
|
attr_accessor :table_offsets #: Array[Integer]
|
|
111
123
|
|
|
112
|
-
|
|
124
|
+
# Rows are also shared with Store#elements. Keep the precise RBS type via
|
|
125
|
+
# an alias rather than pinning Spinel's specialized nested-array layout.
|
|
126
|
+
attr_accessor :element_indices #: Array[indexVector]
|
|
113
127
|
|
|
114
128
|
# @rbs return: void
|
|
115
129
|
def initialize
|
|
@@ -128,11 +142,13 @@ module Wardite
|
|
|
128
142
|
|
|
129
143
|
attr_accessor :locals_type #: Array[Symbol]
|
|
130
144
|
|
|
131
|
-
attr_accessor :body #: Array[
|
|
145
|
+
attr_accessor :body #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
132
146
|
|
|
133
147
|
# @rbs &blk: (CodeBody) -> void
|
|
134
148
|
# @rbs return: void
|
|
135
149
|
def initialize(&blk)
|
|
150
|
+
@locals_count = []
|
|
151
|
+
@locals_type = []
|
|
136
152
|
blk.call(self)
|
|
137
153
|
end
|
|
138
154
|
end
|
|
@@ -252,8 +268,11 @@ module Wardite
|
|
|
252
268
|
# @rbs buf: File|StringIO
|
|
253
269
|
# @rbs import_object: Hash[Symbol, wasmModuleSrc]
|
|
254
270
|
# @rbs enable_wasi: boolish
|
|
271
|
+
# @rbs engine: Symbol
|
|
272
|
+
# @rbs disabled_optimizations: Array[Symbol]
|
|
273
|
+
# @rbs mir_profile: bool
|
|
255
274
|
# @rbs return: Instance
|
|
256
|
-
def self.load_from_buffer(buf, import_object: {}, enable_wasi: true)
|
|
275
|
+
def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false)
|
|
257
276
|
start = Time.now.to_f #: Float
|
|
258
277
|
@buf = buf
|
|
259
278
|
|
|
@@ -262,17 +281,25 @@ module Wardite
|
|
|
262
281
|
|
|
263
282
|
wasi_env = nil
|
|
264
283
|
if enable_wasi
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
284
|
+
if RUBY_ENGINE == "spinel"
|
|
285
|
+
raise LoadError, "WASI is not supported in Spinel builds; pass enable_wasi: false"
|
|
286
|
+
elsif sections_.any? { |section| section.is_a?(ImportSection) &&
|
|
287
|
+
section.imports.any? { |import| import.module_name == "wasi_snapshot_preview1" } }
|
|
288
|
+
wasi_env = if engine == :efflorescence
|
|
289
|
+
require "wardite/efflorescence/wasi"
|
|
290
|
+
Wardite::Efflorescence::WasiSnapshotPreview1.new
|
|
291
|
+
else
|
|
292
|
+
require "wardite/wasi"
|
|
293
|
+
Wardite::WasiSnapshotPreview1.new
|
|
294
|
+
end
|
|
295
|
+
import_object[:wasi_snapshot_preview1] = wasi_env
|
|
296
|
+
end
|
|
268
297
|
end
|
|
269
298
|
|
|
270
|
-
return Instance.new(import_object) do |i|
|
|
299
|
+
return Instance.new(import_object, engine: engine, disabled_optimizations: disabled_optimizations, mir_profile: mir_profile) do |i|
|
|
271
300
|
i.version = version
|
|
272
301
|
i.sections = sections_
|
|
273
|
-
|
|
274
|
-
i.wasi = wasi_env
|
|
275
|
-
end
|
|
302
|
+
i.wasi = wasi_env
|
|
276
303
|
end
|
|
277
304
|
ensure
|
|
278
305
|
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['WARITE_TRACE']
|
|
@@ -292,10 +319,8 @@ module Wardite
|
|
|
292
319
|
if !vstr
|
|
293
320
|
raise LoadError, "buffer too short"
|
|
294
321
|
end
|
|
295
|
-
version = vstr.
|
|
296
|
-
|
|
297
|
-
.inject(0) {|dest, (c, i)| dest | (c.ord << i*8) }
|
|
298
|
-
if version != 1
|
|
322
|
+
version = vstr.unpack1("V")
|
|
323
|
+
if !version.is_a?(Integer) || version != 1
|
|
299
324
|
raise LoadError, "unsupported version: #{version}"
|
|
300
325
|
end
|
|
301
326
|
version
|
|
@@ -317,6 +342,8 @@ module Wardite
|
|
|
317
342
|
type_section
|
|
318
343
|
when Wardite::SectionImport
|
|
319
344
|
import_section
|
|
345
|
+
when Wardite::SectionTag
|
|
346
|
+
tag_section
|
|
320
347
|
when Wardite::SectionFunction
|
|
321
348
|
function_section
|
|
322
349
|
when Wardite::SectionTable
|
|
@@ -369,6 +396,8 @@ module Wardite
|
|
|
369
396
|
arg = [] #: Array[Symbol]
|
|
370
397
|
arglen.times do
|
|
371
398
|
case ty = assert_read(sbuf, 1)&.ord
|
|
399
|
+
when 0x69
|
|
400
|
+
arg << :exnref
|
|
372
401
|
when 0x7f
|
|
373
402
|
arg << :i32
|
|
374
403
|
when 0x7e
|
|
@@ -378,7 +407,7 @@ module Wardite
|
|
|
378
407
|
when 0x7c
|
|
379
408
|
arg << :f64
|
|
380
409
|
else
|
|
381
|
-
raise
|
|
410
|
+
raise LoadError, "unsupported value type: #{ty.inspect}"
|
|
382
411
|
end
|
|
383
412
|
end
|
|
384
413
|
dest.defined_types << arg
|
|
@@ -387,6 +416,8 @@ module Wardite
|
|
|
387
416
|
ret = [] #: Array[Symbol]
|
|
388
417
|
retlen.times do
|
|
389
418
|
case ty = assert_read(sbuf, 1)&.ord
|
|
419
|
+
when 0x69
|
|
420
|
+
ret << :exnref
|
|
390
421
|
when 0x7f
|
|
391
422
|
ret << :i32
|
|
392
423
|
when 0x7e
|
|
@@ -396,7 +427,7 @@ module Wardite
|
|
|
396
427
|
when 0x7c
|
|
397
428
|
ret << :f64
|
|
398
429
|
else
|
|
399
|
-
raise
|
|
430
|
+
raise LoadError, "unsupported value type: #{ty.inspect}"
|
|
400
431
|
end
|
|
401
432
|
end
|
|
402
433
|
dest.defined_results << ret
|
|
@@ -405,6 +436,19 @@ module Wardite
|
|
|
405
436
|
dest
|
|
406
437
|
end
|
|
407
438
|
|
|
439
|
+
# @rbs return: TagSection
|
|
440
|
+
def self.tag_section
|
|
441
|
+
dest = TagSection.new
|
|
442
|
+
dest.size = fetch_uleb128(@buf)
|
|
443
|
+
sbuf = StringIO.new(assert_read(@buf, dest.size))
|
|
444
|
+
fetch_uleb128(sbuf).times do
|
|
445
|
+
raise LoadError, "unsupported tag attribute" unless assert_read(sbuf, 1).ord == 0
|
|
446
|
+
dest.type_indices << fetch_uleb128(sbuf)
|
|
447
|
+
end
|
|
448
|
+
raise LoadError, "trailing tag section data" unless sbuf.eof?
|
|
449
|
+
dest
|
|
450
|
+
end
|
|
451
|
+
|
|
408
452
|
# @rbs return: ImportSection
|
|
409
453
|
def self.import_section
|
|
410
454
|
dest = ImportSection.new
|
|
@@ -424,6 +468,8 @@ module Wardite
|
|
|
424
468
|
raise "[BUG] empty unpacked string" # guard rbs
|
|
425
469
|
end
|
|
426
470
|
|
|
471
|
+
raise LoadError, "tag imports are not supported" if kind == 4
|
|
472
|
+
raise LoadError, "unsupported import kind #{kind} for #{module_name}.#{name}" unless kind == 0
|
|
427
473
|
index = fetch_uleb128(sbuf)
|
|
428
474
|
dest.add_desc do |desc|
|
|
429
475
|
desc.module_name = module_name
|
|
@@ -441,22 +487,25 @@ module Wardite
|
|
|
441
487
|
dest = MemorySection.new
|
|
442
488
|
size = fetch_uleb128(@buf)
|
|
443
489
|
dest.size = size
|
|
444
|
-
sbuf = StringIO.new(@buf
|
|
490
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
445
491
|
|
|
446
492
|
len = fetch_uleb128(sbuf)
|
|
447
|
-
if len
|
|
448
|
-
raise LoadError, "
|
|
493
|
+
if len > 1
|
|
494
|
+
raise LoadError, "only one memory is supported: #{len}"
|
|
449
495
|
end
|
|
450
496
|
len.times do |i|
|
|
451
497
|
flags = fetch_uleb128(sbuf)
|
|
498
|
+
unless flags == 0 || flags == 1
|
|
499
|
+
raise LoadError, "shared/memory64/custom-page memory limits are not supported"
|
|
500
|
+
end
|
|
452
501
|
min = fetch_uleb128(sbuf)
|
|
453
|
-
|
|
454
|
-
max
|
|
455
|
-
|
|
456
|
-
max = fetch_uleb128(sbuf)
|
|
502
|
+
max = flags == 1 ? fetch_uleb128(sbuf) : nil
|
|
503
|
+
if min > 65536 || (max && (max > 65536 || max < min))
|
|
504
|
+
raise LoadError, "invalid memory32 limits"
|
|
457
505
|
end
|
|
458
506
|
dest.limits << [min, max]
|
|
459
507
|
end
|
|
508
|
+
raise LoadError, "trailing memory section bytes" unless sbuf.eof?
|
|
460
509
|
dest
|
|
461
510
|
end
|
|
462
511
|
|
|
@@ -481,26 +530,23 @@ module Wardite
|
|
|
481
530
|
len = fetch_uleb128(sbuf)
|
|
482
531
|
len.times do |i|
|
|
483
532
|
etype = fetch_uleb128(sbuf)
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
code = fetch_insn_while_end(sbuf)
|
|
489
|
-
ops = code_body(StringIO.new(code))
|
|
490
|
-
offset = decode_expr(ops)
|
|
491
|
-
dest.table_offsets << offset
|
|
533
|
+
unless etype == 0 || etype == 2
|
|
534
|
+
raise LoadError, "unsupported element segment kind #{etype}"
|
|
535
|
+
end
|
|
536
|
+
dest.table_indices << (etype == 0 ? 0 : fetch_uleb128(sbuf))
|
|
492
537
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
end
|
|
499
|
-
dest.element_indices << elms
|
|
500
|
-
else
|
|
501
|
-
raise NotImplementedError, "element section type #{etype} is a TODO!"
|
|
538
|
+
code = fetch_insn_while_end(sbuf)
|
|
539
|
+
ops = code_body(StringIO.new(code))
|
|
540
|
+
dest.table_offsets << decode_expr(ops)
|
|
541
|
+
if etype == 2 && assert_read(sbuf, 1).ord != 0
|
|
542
|
+
raise LoadError, "unsupported element type"
|
|
502
543
|
end
|
|
544
|
+
|
|
545
|
+
elms = [] #: Array[Integer]
|
|
546
|
+
fetch_uleb128(sbuf).times { elms << fetch_uleb128(sbuf) }
|
|
547
|
+
dest.element_indices << elms
|
|
503
548
|
end
|
|
549
|
+
raise LoadError, "trailing element section data" unless sbuf.eof?
|
|
504
550
|
dest
|
|
505
551
|
end
|
|
506
552
|
|
|
@@ -513,15 +559,25 @@ module Wardite
|
|
|
513
559
|
|
|
514
560
|
len = fetch_uleb128(sbuf)
|
|
515
561
|
len.times do |i|
|
|
516
|
-
|
|
517
|
-
gtype = Op.i2type(typeb)
|
|
562
|
+
gtype = Op.i2type(assert_read(sbuf, 1).ord)
|
|
518
563
|
mut = sbuf.read 1
|
|
519
564
|
if !mut
|
|
520
565
|
raise LoadError, "global section too short"
|
|
521
566
|
end
|
|
567
|
+
unless mut.ord == 0 || mut.ord == 1
|
|
568
|
+
raise LoadError, "unsupported global mutability flags"
|
|
569
|
+
end
|
|
522
570
|
|
|
523
571
|
code = fetch_insn_while_end(sbuf)
|
|
524
572
|
ops = code_body(StringIO.new(code))
|
|
573
|
+
expected = case gtype
|
|
574
|
+
when :i32 then :i32_const
|
|
575
|
+
when :i64 then :i64_const
|
|
576
|
+
when :f32 then :f32_const
|
|
577
|
+
when :f64 then :f64_const
|
|
578
|
+
else raise LoadError, "unsupported global type #{gtype}"
|
|
579
|
+
end
|
|
580
|
+
raise LoadError, "global initializer type mismatch" unless ops.first&.[](1) == expected
|
|
525
581
|
value = decode_global_expr(ops)
|
|
526
582
|
|
|
527
583
|
global = GlobalSection::Global.new do |g|
|
|
@@ -579,49 +635,46 @@ module Wardite
|
|
|
579
635
|
dest = CodeSection.new
|
|
580
636
|
size = fetch_uleb128(@buf)
|
|
581
637
|
dest.size = size
|
|
582
|
-
sbuf = StringIO.new(@buf
|
|
638
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
583
639
|
|
|
584
640
|
len = fetch_uleb128(sbuf)
|
|
585
|
-
len.times
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
641
|
+
len.times { dest.func_codes << read_function_body(sbuf) }
|
|
642
|
+
dest
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
# @rbs sbuf: StringIO
|
|
646
|
+
# @rbs return: CodeSection::CodeBody
|
|
647
|
+
def self.read_function_body(sbuf)
|
|
648
|
+
ilen = fetch_uleb128(sbuf)
|
|
649
|
+
code = assert_read(sbuf, ilen)
|
|
650
|
+
last_code = code[-1]
|
|
651
|
+
if ! last_code
|
|
652
|
+
raise "[BUG] empty code fetched" # guard for steep check
|
|
653
|
+
end
|
|
654
|
+
if last_code.ord != 0x0b
|
|
655
|
+
$stderr.puts "warning: instruction not ended with inst end(0x0b): 0x0#{last_code.ord}"
|
|
656
|
+
end
|
|
657
|
+
cbuf = StringIO.new(code)
|
|
658
|
+
CodeSection::CodeBody.new do |body|
|
|
598
659
|
locals_len = fetch_uleb128(cbuf)
|
|
599
|
-
locals_len
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
locals_type << Op.i2type(value_type || -1)
|
|
604
|
-
end
|
|
605
|
-
body = code_body(cbuf)
|
|
606
|
-
revisitor = Revisitor.new(body)
|
|
607
|
-
revisitor.revisit!
|
|
608
|
-
|
|
609
|
-
dest.func_codes << CodeSection::CodeBody.new do |b|
|
|
610
|
-
b.locals_count = locals_count
|
|
611
|
-
b.locals_type = locals_type
|
|
612
|
-
b.body = revisitor.ops
|
|
660
|
+
while locals_len > 0
|
|
661
|
+
body.locals_count << fetch_uleb128(cbuf)
|
|
662
|
+
body.locals_type << Op.i2type(assert_read(cbuf, 1).ord)
|
|
663
|
+
locals_len -= 1
|
|
613
664
|
end
|
|
665
|
+
body.body = code_body(cbuf)
|
|
614
666
|
end
|
|
615
|
-
dest
|
|
616
667
|
end
|
|
617
668
|
|
|
618
669
|
# @rbs buf: StringIO
|
|
619
|
-
# @rbs return: Array[
|
|
670
|
+
# @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
620
671
|
def self.code_body(buf)
|
|
621
|
-
dest = [] #: Array[
|
|
672
|
+
dest = [] #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
673
|
+
# HINT: [symname, index of op, index of else, index of end]
|
|
674
|
+
branching_stack = [] #: Array[[Symbol, Integer, Integer, Integer]]
|
|
675
|
+
fixed_stack = [] #: Array[[Symbol, Integer, Integer, Integer]]
|
|
622
676
|
while c = buf.read(1)
|
|
623
|
-
namespace, code = resolve_code(c, buf)
|
|
624
|
-
operand_types = Op.operand_of(code)
|
|
677
|
+
namespace, code, operand_types = resolve_code(c, buf)
|
|
625
678
|
operand = [] #: Array[operandItem]
|
|
626
679
|
operand_types.each do |typ|
|
|
627
680
|
case typ
|
|
@@ -632,53 +685,81 @@ module Wardite
|
|
|
632
685
|
end
|
|
633
686
|
operand << ope.ord
|
|
634
687
|
when :u32
|
|
635
|
-
|
|
688
|
+
value = fetch_uleb128(buf)
|
|
689
|
+
if operand.empty? && c.ord >= 0x28 && c.ord <= 0x3e && value >= 64
|
|
690
|
+
raise LoadError, "multi-memory memargs are not supported"
|
|
691
|
+
end
|
|
692
|
+
operand << value
|
|
636
693
|
when :u32_vec
|
|
637
694
|
len = fetch_uleb128(buf)
|
|
638
695
|
vec = [] #: Array[Integer]
|
|
639
696
|
len.times do
|
|
640
697
|
vec << fetch_uleb128(buf)
|
|
641
698
|
end
|
|
642
|
-
operand << vec
|
|
699
|
+
operand << OperandVector.new(vec)
|
|
643
700
|
when :i32
|
|
644
|
-
operand << fetch_sleb128(buf)
|
|
701
|
+
operand << fetch_sleb128(buf, width: 32)
|
|
645
702
|
when :i64
|
|
646
|
-
operand << fetch_sleb128(buf,
|
|
647
|
-
when :f32
|
|
648
|
-
data = buf
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
raise "String#unpack is broken" if !v.is_a?(Float)
|
|
662
|
-
operand << v
|
|
663
|
-
when :u8_block
|
|
664
|
-
block_ope = buf.read 1
|
|
665
|
-
if ! block_ope
|
|
666
|
-
raise LoadError, "buffer too short for if"
|
|
667
|
-
end
|
|
668
|
-
if block_ope.ord == 0x40
|
|
669
|
-
operand << Block.void
|
|
670
|
-
else
|
|
671
|
-
operand << Block.new([block_ope.ord])
|
|
703
|
+
operand << fetch_sleb128(buf, width: 64)
|
|
704
|
+
when :f32, :f64
|
|
705
|
+
data = assert_read(buf, typ == :f32 ? 4 : 8)
|
|
706
|
+
operand << FloatImmediate.new(typ, FloatBits.read(typ, data))
|
|
707
|
+
when :u8_block, :blocktype
|
|
708
|
+
value = read_blocktype(buf)
|
|
709
|
+
operand << value
|
|
710
|
+
when :catches
|
|
711
|
+
clauses = [] #: Array[Integer]
|
|
712
|
+
fetch_uleb128(buf).times do
|
|
713
|
+
kind = assert_read(buf, 1).ord
|
|
714
|
+
raise LoadError, "invalid catch kind #{kind}" unless kind <= 3
|
|
715
|
+
tag = kind <= 1 ? fetch_uleb128(buf) : -1
|
|
716
|
+
depth = fetch_uleb128(buf)
|
|
717
|
+
clauses << kind << tag << depth
|
|
672
718
|
end
|
|
719
|
+
operand << OperandVector.new(clauses)
|
|
673
720
|
else
|
|
674
721
|
$stderr.puts "warning: unknown type #{typ.inspect}. defaulting to u32"
|
|
675
722
|
operand << fetch_uleb128(buf)
|
|
676
723
|
end
|
|
677
724
|
end
|
|
678
725
|
|
|
679
|
-
|
|
726
|
+
# HINT: [namespace, code, operand, else_pos, end_pos]
|
|
727
|
+
dest << [namespace, code, operand, nil, nil]
|
|
728
|
+
if code == :block || code == :loop || code == :if || code == :try_table
|
|
729
|
+
branching_stack << [code, dest.size - 1, -1, -1]
|
|
730
|
+
elsif code == :else
|
|
731
|
+
if branching_stack.empty?
|
|
732
|
+
raise "broken sequence: unmatched else"
|
|
733
|
+
else
|
|
734
|
+
last = branching_stack.pop || raise("[BUG] empty pop")
|
|
735
|
+
if last[0] != :if
|
|
736
|
+
raise "broken sequence: else without if"
|
|
737
|
+
end
|
|
738
|
+
branching_stack << [last[0], last[1], dest.size - 1, -1]
|
|
739
|
+
end
|
|
740
|
+
elsif code == :end
|
|
741
|
+
unless branching_stack.empty?
|
|
742
|
+
last = branching_stack.pop || raise("[BUG] empty pop")
|
|
743
|
+
fixed_stack << [last[0], last[1], last[2], dest.size - 1]
|
|
744
|
+
end
|
|
745
|
+
end
|
|
680
746
|
end
|
|
681
747
|
|
|
748
|
+
raise LoadError, "unterminated structured control" unless branching_stack.empty?
|
|
749
|
+
fixed_stack.each do |(sym, begin_idx, else_idx, end_idx)|
|
|
750
|
+
if end_idx == -1
|
|
751
|
+
raise "broken sequence: branching without end"
|
|
752
|
+
end
|
|
753
|
+
case sym
|
|
754
|
+
when :block, :loop, :try_table
|
|
755
|
+
dest[begin_idx][-1] = end_idx
|
|
756
|
+
when :if
|
|
757
|
+
dest[begin_idx][-2] = else_idx == -1 ? end_idx : else_idx
|
|
758
|
+
dest[begin_idx][-1] = end_idx
|
|
759
|
+
else
|
|
760
|
+
raise "[BUG] unknown sym #{sym.inspect}"
|
|
761
|
+
end
|
|
762
|
+
end
|
|
682
763
|
dest
|
|
683
764
|
rescue => e
|
|
684
765
|
require "pp"
|
|
@@ -688,16 +769,38 @@ module Wardite
|
|
|
688
769
|
raise e
|
|
689
770
|
end
|
|
690
771
|
|
|
772
|
+
# Decode s33, distinguishing the one-byte value/void forms from indices.
|
|
773
|
+
# @rbs buf: StringIO
|
|
774
|
+
# @rbs return: Block
|
|
775
|
+
def self.read_blocktype(buf)
|
|
776
|
+
first = buf.pos
|
|
777
|
+
value = fetch_sleb128(buf, max_level: 4)
|
|
778
|
+
raise LoadError, "blocktype exceeds s33" unless value >= -4294967296 && value <= 4294967295
|
|
779
|
+
if value < 0 && buf.pos - first != 1
|
|
780
|
+
raise LoadError, "value/void blocktypes must use their single-byte encoding"
|
|
781
|
+
end
|
|
782
|
+
return Block.void if value == -64
|
|
783
|
+
return Block.new(nil, type_index: value) if value >= 0
|
|
784
|
+
raise LoadError, "invalid block value type" if value < -64
|
|
785
|
+
code = value & 0x7f
|
|
786
|
+
Op.i2type(code) # Reject unsupported/nonnullable reference declarations.
|
|
787
|
+
Block.new([code])
|
|
788
|
+
end
|
|
789
|
+
|
|
691
790
|
# @rbs c: String
|
|
692
791
|
# @rbs buf: StringIO
|
|
693
|
-
# @rbs return: [Symbol, Symbol]
|
|
792
|
+
# @rbs return: [Symbol, Symbol, Array[Symbol]]
|
|
694
793
|
def self.resolve_code(c, buf)
|
|
695
|
-
|
|
696
|
-
|
|
794
|
+
ord = c.ord
|
|
795
|
+
code = Op::SYMS[ord]
|
|
796
|
+
if code == :fc
|
|
697
797
|
lower = fetch_uleb128(buf)
|
|
698
|
-
|
|
798
|
+
sym = Op::FC_SYMS[lower]
|
|
799
|
+
namespace = Op::FC_OPERANDS[lower] || :default
|
|
800
|
+
return [namespace, sym, Op.operand_of(sym)]
|
|
699
801
|
end
|
|
700
|
-
|
|
802
|
+
namespace = Op::NAMESPACES[ord] || raise(LoadError, "unsupported code: #{ord}")
|
|
803
|
+
return [namespace, code, Op.operand_of(code)]
|
|
701
804
|
end
|
|
702
805
|
|
|
703
806
|
# @rbs return: DataSection
|
|
@@ -705,36 +808,32 @@ module Wardite
|
|
|
705
808
|
dest = DataSection.new
|
|
706
809
|
size = fetch_uleb128(@buf)
|
|
707
810
|
dest.size = size
|
|
708
|
-
sbuf = StringIO.new(@buf
|
|
811
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
709
812
|
|
|
710
813
|
len = fetch_uleb128(sbuf)
|
|
711
814
|
len.times do |i|
|
|
712
815
|
data_type = fetch_uleb128(sbuf)
|
|
713
816
|
case data_type
|
|
714
|
-
when 0x0
|
|
715
|
-
#
|
|
817
|
+
when 0x0, 0x2
|
|
818
|
+
# Active explicit-memory segments still target memory 0 in M2.
|
|
819
|
+
memory_index = data_type == 2 ? fetch_uleb128(sbuf) : 0
|
|
820
|
+
raise LoadError, "only memory 0 is supported" unless memory_index == 0
|
|
716
821
|
code = fetch_insn_while_end(sbuf)
|
|
717
822
|
ops = code_body(StringIO.new(code))
|
|
718
823
|
offset = decode_expr(ops)
|
|
719
824
|
len = fetch_uleb128(sbuf)
|
|
720
|
-
data = sbuf
|
|
721
|
-
if !data
|
|
722
|
-
raise LoadError, "buffer too short"
|
|
723
|
-
end
|
|
825
|
+
data = assert_read(sbuf, len).b
|
|
724
826
|
segment = DataSection::Segment.new do |seg|
|
|
725
827
|
seg.mode = :active
|
|
726
|
-
seg.mem_index =
|
|
727
|
-
seg.offset = offset
|
|
828
|
+
seg.mem_index = memory_index
|
|
829
|
+
seg.offset = offset & 0xffffffff
|
|
728
830
|
seg.data = data
|
|
729
831
|
end
|
|
730
832
|
dest.segments << segment
|
|
731
833
|
when 0x1
|
|
732
834
|
# passive
|
|
733
835
|
dsize = fetch_uleb128(sbuf)
|
|
734
|
-
data = sbuf
|
|
735
|
-
if !data
|
|
736
|
-
raise LoadError, "data too short"
|
|
737
|
-
end
|
|
836
|
+
data = assert_read(sbuf, dsize).b
|
|
738
837
|
segment = DataSection::Segment.new do |seg|
|
|
739
838
|
seg.mode = :passive
|
|
740
839
|
seg.mem_index = 0 # unused
|
|
@@ -742,17 +841,21 @@ module Wardite
|
|
|
742
841
|
seg.data = data
|
|
743
842
|
end
|
|
744
843
|
dest.segments << segment
|
|
844
|
+
else
|
|
845
|
+
raise LoadError, "unsupported data segment flags #{data_type}"
|
|
745
846
|
end
|
|
746
847
|
end
|
|
848
|
+
raise LoadError, "trailing data section bytes" unless sbuf.eof?
|
|
747
849
|
dest
|
|
748
850
|
end
|
|
749
851
|
|
|
750
852
|
# @rbs return: DataCountSection
|
|
751
853
|
def self.data_count_section
|
|
752
854
|
size = fetch_uleb128(@buf)
|
|
753
|
-
sbuf = StringIO.new(@buf
|
|
855
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
754
856
|
count = fetch_uleb128(sbuf)
|
|
755
857
|
dest = DataCountSection.new(count)
|
|
858
|
+
raise LoadError, "trailing data count bytes" unless sbuf.eof?
|
|
756
859
|
dest.size = size
|
|
757
860
|
dest
|
|
758
861
|
end
|
|
@@ -760,21 +863,22 @@ module Wardite
|
|
|
760
863
|
# @rbs sbuf: StringIO
|
|
761
864
|
# @rbs return: String
|
|
762
865
|
def self.fetch_insn_while_end(sbuf)
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
866
|
+
first = sbuf.pos
|
|
867
|
+
opcode = assert_read(sbuf, 1).ord
|
|
868
|
+
case opcode
|
|
869
|
+
when 0x41 then fetch_sleb128(sbuf, width: 32)
|
|
870
|
+
when 0x42 then fetch_sleb128(sbuf, width: 64)
|
|
871
|
+
when 0x43 then assert_read(sbuf, 4)
|
|
872
|
+
when 0x44 then assert_read(sbuf, 8)
|
|
873
|
+
else raise LoadError, "unsupported constant expression"
|
|
874
|
+
end
|
|
875
|
+
raise LoadError, "constant expression requires end" unless assert_read(sbuf, 1).ord == 0x0b
|
|
876
|
+
length = sbuf.pos - first
|
|
877
|
+
sbuf.pos = first
|
|
878
|
+
assert_read(sbuf, length)
|
|
775
879
|
end
|
|
776
880
|
|
|
777
|
-
# @rbs ops: Array[
|
|
881
|
+
# @rbs ops: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
778
882
|
# @rbs return: Integer
|
|
779
883
|
def self.decode_expr(ops)
|
|
780
884
|
# sees first opcode
|
|
@@ -782,19 +886,19 @@ module Wardite
|
|
|
782
886
|
if !op
|
|
783
887
|
raise LoadError, "empty opcodes"
|
|
784
888
|
end
|
|
785
|
-
case op
|
|
889
|
+
case op[1]
|
|
786
890
|
when :i32_const
|
|
787
|
-
arg = op
|
|
891
|
+
arg = op[2][0]
|
|
788
892
|
if !arg.is_a?(Integer)
|
|
789
893
|
raise "Invalid definition of operand"
|
|
790
894
|
end
|
|
791
895
|
return arg
|
|
792
896
|
else
|
|
793
|
-
raise "Unimplemented offset op: #{op.
|
|
897
|
+
raise "Unimplemented offset op: #{op.inspect}"
|
|
794
898
|
end
|
|
795
899
|
end
|
|
796
900
|
|
|
797
|
-
# @rbs ops: Array[
|
|
901
|
+
# @rbs ops: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
798
902
|
# @rbs return: wasmValue
|
|
799
903
|
def self.decode_global_expr(ops)
|
|
800
904
|
# sees first opcode
|
|
@@ -802,33 +906,29 @@ module Wardite
|
|
|
802
906
|
if !op
|
|
803
907
|
raise LoadError, "empty opcodes"
|
|
804
908
|
end
|
|
805
|
-
case op
|
|
909
|
+
case op[1]
|
|
806
910
|
when :i32_const
|
|
807
|
-
arg = op
|
|
911
|
+
arg = op[2][0]
|
|
808
912
|
if !arg.is_a?(Integer)
|
|
809
913
|
raise "Invalid definition of operand"
|
|
810
914
|
end
|
|
811
915
|
return I32(arg)
|
|
812
916
|
when :i64_const
|
|
813
|
-
arg = op
|
|
917
|
+
arg = op[2][0]
|
|
814
918
|
if !arg.is_a?(Integer)
|
|
815
919
|
raise "Invalid definition of operand"
|
|
816
920
|
end
|
|
817
921
|
return I64(arg)
|
|
818
922
|
when :f32_const
|
|
819
|
-
arg = op
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
end
|
|
823
|
-
return F32(arg)
|
|
923
|
+
arg = op[2][0]
|
|
924
|
+
raise "Invalid definition of operand" unless arg.is_a?(FloatImmediate)
|
|
925
|
+
return F32.from_bits(arg.bits)
|
|
824
926
|
when :f64_const
|
|
825
|
-
arg = op
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
end
|
|
829
|
-
return F64(arg)
|
|
927
|
+
arg = op[2][0]
|
|
928
|
+
raise "Invalid definition of operand" unless arg.is_a?(FloatImmediate)
|
|
929
|
+
return F64.from_bits(arg.bits)
|
|
830
930
|
else
|
|
831
|
-
raise "Unimplemented offset op: #{op.
|
|
931
|
+
raise "Unimplemented offset op: #{op.inspect}"
|
|
832
932
|
end
|
|
833
933
|
end
|
|
834
934
|
|
|
@@ -849,6 +949,7 @@ module Wardite
|
|
|
849
949
|
raise "[BUG] empty unpacked string" # guard rbs
|
|
850
950
|
end
|
|
851
951
|
|
|
952
|
+
raise LoadError, "tag exports are not supported" if kind == 4
|
|
852
953
|
index = fetch_uleb128(sbuf)
|
|
853
954
|
dest.add_desc do |desc|
|
|
854
955
|
desc.name = name
|
|
@@ -869,7 +970,7 @@ module Wardite
|
|
|
869
970
|
nil
|
|
870
971
|
end
|
|
871
972
|
|
|
872
|
-
# @rbs sbuf: StringIO
|
|
973
|
+
# @rbs sbuf: File|StringIO
|
|
873
974
|
# @rbs n: Integer
|
|
874
975
|
# @rbs return: String
|
|
875
976
|
def self.assert_read(sbuf, n)
|
|
@@ -877,10 +978,10 @@ module Wardite
|
|
|
877
978
|
if !ret
|
|
878
979
|
raise LoadError, "too short section size"
|
|
879
980
|
end
|
|
880
|
-
if ret.
|
|
981
|
+
if ret.bytesize != n
|
|
881
982
|
raise LoadError, "too short section size"
|
|
882
983
|
end
|
|
883
984
|
ret
|
|
884
985
|
end
|
|
885
986
|
end
|
|
886
|
-
end
|
|
987
|
+
end
|