wardite 0.9.0 → 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/lib/wardite/alu_f32.generated.rb +1 -0
- data/lib/wardite/alu_f64.generated.rb +1 -0
- data/lib/wardite/alu_i32.generated.rb +14 -9
- data/lib/wardite/alu_i64.generated.rb +18 -20
- data/lib/wardite/cli/cli.rb +61 -28
- 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 +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 +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +203 -138
- 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 +184 -70
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +66 -10
- data/sig/generated/wardite/cli/cli.rbs +8 -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 +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 +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +29 -4
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +49 -12
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +126 -3
data/lib/wardite/load.rb
CHANGED
|
@@ -24,6 +24,16 @@ module Wardite
|
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
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
|
+
|
|
27
37
|
class FunctionSection < Section
|
|
28
38
|
attr_accessor :func_indices #: Array[Integer]
|
|
29
39
|
|
|
@@ -104,11 +114,16 @@ module Wardite
|
|
|
104
114
|
end
|
|
105
115
|
|
|
106
116
|
class ElemSection < Section
|
|
117
|
+
# @rbs!
|
|
118
|
+
# type indexVector = Array[Integer]
|
|
119
|
+
|
|
107
120
|
attr_accessor :table_indices #: Array[Integer]
|
|
108
121
|
|
|
109
122
|
attr_accessor :table_offsets #: Array[Integer]
|
|
110
123
|
|
|
111
|
-
|
|
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]
|
|
112
127
|
|
|
113
128
|
# @rbs return: void
|
|
114
129
|
def initialize
|
|
@@ -132,6 +147,8 @@ module Wardite
|
|
|
132
147
|
# @rbs &blk: (CodeBody) -> void
|
|
133
148
|
# @rbs return: void
|
|
134
149
|
def initialize(&blk)
|
|
150
|
+
@locals_count = []
|
|
151
|
+
@locals_type = []
|
|
135
152
|
blk.call(self)
|
|
136
153
|
end
|
|
137
154
|
end
|
|
@@ -251,8 +268,11 @@ module Wardite
|
|
|
251
268
|
# @rbs buf: File|StringIO
|
|
252
269
|
# @rbs import_object: Hash[Symbol, wasmModuleSrc]
|
|
253
270
|
# @rbs enable_wasi: boolish
|
|
271
|
+
# @rbs engine: Symbol
|
|
272
|
+
# @rbs disabled_optimizations: Array[Symbol]
|
|
273
|
+
# @rbs mir_profile: bool
|
|
254
274
|
# @rbs return: Instance
|
|
255
|
-
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)
|
|
256
276
|
start = Time.now.to_f #: Float
|
|
257
277
|
@buf = buf
|
|
258
278
|
|
|
@@ -261,17 +281,25 @@ module Wardite
|
|
|
261
281
|
|
|
262
282
|
wasi_env = nil
|
|
263
283
|
if enable_wasi
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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
|
|
267
297
|
end
|
|
268
298
|
|
|
269
|
-
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|
|
|
270
300
|
i.version = version
|
|
271
301
|
i.sections = sections_
|
|
272
|
-
|
|
273
|
-
i.wasi = wasi_env
|
|
274
|
-
end
|
|
302
|
+
i.wasi = wasi_env
|
|
275
303
|
end
|
|
276
304
|
ensure
|
|
277
305
|
$stderr.puts "load_from_buffer: #{Time.now.to_f - start}" if ENV['WARITE_TRACE']
|
|
@@ -291,10 +319,8 @@ module Wardite
|
|
|
291
319
|
if !vstr
|
|
292
320
|
raise LoadError, "buffer too short"
|
|
293
321
|
end
|
|
294
|
-
version = vstr.
|
|
295
|
-
|
|
296
|
-
.inject(0) {|dest, (c, i)| dest | (c.ord << i*8) }
|
|
297
|
-
if version != 1
|
|
322
|
+
version = vstr.unpack1("V")
|
|
323
|
+
if !version.is_a?(Integer) || version != 1
|
|
298
324
|
raise LoadError, "unsupported version: #{version}"
|
|
299
325
|
end
|
|
300
326
|
version
|
|
@@ -316,6 +342,8 @@ module Wardite
|
|
|
316
342
|
type_section
|
|
317
343
|
when Wardite::SectionImport
|
|
318
344
|
import_section
|
|
345
|
+
when Wardite::SectionTag
|
|
346
|
+
tag_section
|
|
319
347
|
when Wardite::SectionFunction
|
|
320
348
|
function_section
|
|
321
349
|
when Wardite::SectionTable
|
|
@@ -368,6 +396,8 @@ module Wardite
|
|
|
368
396
|
arg = [] #: Array[Symbol]
|
|
369
397
|
arglen.times do
|
|
370
398
|
case ty = assert_read(sbuf, 1)&.ord
|
|
399
|
+
when 0x69
|
|
400
|
+
arg << :exnref
|
|
371
401
|
when 0x7f
|
|
372
402
|
arg << :i32
|
|
373
403
|
when 0x7e
|
|
@@ -377,7 +407,7 @@ module Wardite
|
|
|
377
407
|
when 0x7c
|
|
378
408
|
arg << :f64
|
|
379
409
|
else
|
|
380
|
-
raise
|
|
410
|
+
raise LoadError, "unsupported value type: #{ty.inspect}"
|
|
381
411
|
end
|
|
382
412
|
end
|
|
383
413
|
dest.defined_types << arg
|
|
@@ -386,6 +416,8 @@ module Wardite
|
|
|
386
416
|
ret = [] #: Array[Symbol]
|
|
387
417
|
retlen.times do
|
|
388
418
|
case ty = assert_read(sbuf, 1)&.ord
|
|
419
|
+
when 0x69
|
|
420
|
+
ret << :exnref
|
|
389
421
|
when 0x7f
|
|
390
422
|
ret << :i32
|
|
391
423
|
when 0x7e
|
|
@@ -395,7 +427,7 @@ module Wardite
|
|
|
395
427
|
when 0x7c
|
|
396
428
|
ret << :f64
|
|
397
429
|
else
|
|
398
|
-
raise
|
|
430
|
+
raise LoadError, "unsupported value type: #{ty.inspect}"
|
|
399
431
|
end
|
|
400
432
|
end
|
|
401
433
|
dest.defined_results << ret
|
|
@@ -404,6 +436,19 @@ module Wardite
|
|
|
404
436
|
dest
|
|
405
437
|
end
|
|
406
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
|
+
|
|
407
452
|
# @rbs return: ImportSection
|
|
408
453
|
def self.import_section
|
|
409
454
|
dest = ImportSection.new
|
|
@@ -423,6 +468,8 @@ module Wardite
|
|
|
423
468
|
raise "[BUG] empty unpacked string" # guard rbs
|
|
424
469
|
end
|
|
425
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
|
|
426
473
|
index = fetch_uleb128(sbuf)
|
|
427
474
|
dest.add_desc do |desc|
|
|
428
475
|
desc.module_name = module_name
|
|
@@ -440,22 +487,25 @@ module Wardite
|
|
|
440
487
|
dest = MemorySection.new
|
|
441
488
|
size = fetch_uleb128(@buf)
|
|
442
489
|
dest.size = size
|
|
443
|
-
sbuf = StringIO.new(@buf
|
|
490
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
444
491
|
|
|
445
492
|
len = fetch_uleb128(sbuf)
|
|
446
|
-
if len
|
|
447
|
-
raise LoadError, "
|
|
493
|
+
if len > 1
|
|
494
|
+
raise LoadError, "only one memory is supported: #{len}"
|
|
448
495
|
end
|
|
449
496
|
len.times do |i|
|
|
450
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
|
|
451
501
|
min = fetch_uleb128(sbuf)
|
|
452
|
-
|
|
453
|
-
max
|
|
454
|
-
|
|
455
|
-
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"
|
|
456
505
|
end
|
|
457
506
|
dest.limits << [min, max]
|
|
458
507
|
end
|
|
508
|
+
raise LoadError, "trailing memory section bytes" unless sbuf.eof?
|
|
459
509
|
dest
|
|
460
510
|
end
|
|
461
511
|
|
|
@@ -480,26 +530,23 @@ module Wardite
|
|
|
480
530
|
len = fetch_uleb128(sbuf)
|
|
481
531
|
len.times do |i|
|
|
482
532
|
etype = fetch_uleb128(sbuf)
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
code = fetch_insn_while_end(sbuf)
|
|
488
|
-
ops = code_body(StringIO.new(code))
|
|
489
|
-
offset = decode_expr(ops)
|
|
490
|
-
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))
|
|
491
537
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
end
|
|
498
|
-
dest.element_indices << elms
|
|
499
|
-
else
|
|
500
|
-
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"
|
|
501
543
|
end
|
|
544
|
+
|
|
545
|
+
elms = [] #: Array[Integer]
|
|
546
|
+
fetch_uleb128(sbuf).times { elms << fetch_uleb128(sbuf) }
|
|
547
|
+
dest.element_indices << elms
|
|
502
548
|
end
|
|
549
|
+
raise LoadError, "trailing element section data" unless sbuf.eof?
|
|
503
550
|
dest
|
|
504
551
|
end
|
|
505
552
|
|
|
@@ -512,15 +559,25 @@ module Wardite
|
|
|
512
559
|
|
|
513
560
|
len = fetch_uleb128(sbuf)
|
|
514
561
|
len.times do |i|
|
|
515
|
-
|
|
516
|
-
gtype = Op.i2type(typeb)
|
|
562
|
+
gtype = Op.i2type(assert_read(sbuf, 1).ord)
|
|
517
563
|
mut = sbuf.read 1
|
|
518
564
|
if !mut
|
|
519
565
|
raise LoadError, "global section too short"
|
|
520
566
|
end
|
|
567
|
+
unless mut.ord == 0 || mut.ord == 1
|
|
568
|
+
raise LoadError, "unsupported global mutability flags"
|
|
569
|
+
end
|
|
521
570
|
|
|
522
571
|
code = fetch_insn_while_end(sbuf)
|
|
523
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
|
|
524
581
|
value = decode_global_expr(ops)
|
|
525
582
|
|
|
526
583
|
global = GlobalSection::Global.new do |g|
|
|
@@ -578,38 +635,35 @@ module Wardite
|
|
|
578
635
|
dest = CodeSection.new
|
|
579
636
|
size = fetch_uleb128(@buf)
|
|
580
637
|
dest.size = size
|
|
581
|
-
sbuf = StringIO.new(@buf
|
|
638
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
582
639
|
|
|
583
640
|
len = fetch_uleb128(sbuf)
|
|
584
|
-
len.times
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
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|
|
|
597
659
|
locals_len = fetch_uleb128(cbuf)
|
|
598
|
-
locals_len
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
locals_type << Op.i2type(value_type || -1)
|
|
603
|
-
end
|
|
604
|
-
body = code_body(cbuf)
|
|
605
|
-
|
|
606
|
-
dest.func_codes << CodeSection::CodeBody.new do |b|
|
|
607
|
-
b.locals_count = locals_count
|
|
608
|
-
b.locals_type = locals_type
|
|
609
|
-
b.body = body
|
|
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
|
|
610
664
|
end
|
|
665
|
+
body.body = code_body(cbuf)
|
|
611
666
|
end
|
|
612
|
-
dest
|
|
613
667
|
end
|
|
614
668
|
|
|
615
669
|
# @rbs buf: StringIO
|
|
@@ -631,44 +685,38 @@ module Wardite
|
|
|
631
685
|
end
|
|
632
686
|
operand << ope.ord
|
|
633
687
|
when :u32
|
|
634
|
-
|
|
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
|
|
635
693
|
when :u32_vec
|
|
636
694
|
len = fetch_uleb128(buf)
|
|
637
695
|
vec = [] #: Array[Integer]
|
|
638
696
|
len.times do
|
|
639
697
|
vec << fetch_uleb128(buf)
|
|
640
698
|
end
|
|
641
|
-
operand << vec
|
|
699
|
+
operand << OperandVector.new(vec)
|
|
642
700
|
when :i32
|
|
643
|
-
operand << fetch_sleb128(buf)
|
|
701
|
+
operand << fetch_sleb128(buf, width: 32)
|
|
644
702
|
when :i64
|
|
645
|
-
operand << fetch_sleb128(buf,
|
|
646
|
-
when :f32
|
|
647
|
-
data = buf
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
raise "String#unpack is broken" if !v.is_a?(Float)
|
|
661
|
-
operand << v
|
|
662
|
-
when :u8_block
|
|
663
|
-
block_ope = buf.read 1
|
|
664
|
-
if ! block_ope
|
|
665
|
-
raise LoadError, "buffer too short for if"
|
|
666
|
-
end
|
|
667
|
-
if block_ope.ord == 0x40
|
|
668
|
-
operand << Block.void
|
|
669
|
-
else
|
|
670
|
-
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
|
|
671
718
|
end
|
|
719
|
+
operand << OperandVector.new(clauses)
|
|
672
720
|
else
|
|
673
721
|
$stderr.puts "warning: unknown type #{typ.inspect}. defaulting to u32"
|
|
674
722
|
operand << fetch_uleb128(buf)
|
|
@@ -677,7 +725,7 @@ module Wardite
|
|
|
677
725
|
|
|
678
726
|
# HINT: [namespace, code, operand, else_pos, end_pos]
|
|
679
727
|
dest << [namespace, code, operand, nil, nil]
|
|
680
|
-
if code == :block || code == :loop || code == :if
|
|
728
|
+
if code == :block || code == :loop || code == :if || code == :try_table
|
|
681
729
|
branching_stack << [code, dest.size - 1, -1, -1]
|
|
682
730
|
elsif code == :else
|
|
683
731
|
if branching_stack.empty?
|
|
@@ -697,12 +745,13 @@ module Wardite
|
|
|
697
745
|
end
|
|
698
746
|
end
|
|
699
747
|
|
|
748
|
+
raise LoadError, "unterminated structured control" unless branching_stack.empty?
|
|
700
749
|
fixed_stack.each do |(sym, begin_idx, else_idx, end_idx)|
|
|
701
750
|
if end_idx == -1
|
|
702
751
|
raise "broken sequence: branching without end"
|
|
703
752
|
end
|
|
704
753
|
case sym
|
|
705
|
-
when :block, :loop
|
|
754
|
+
when :block, :loop, :try_table
|
|
706
755
|
dest[begin_idx][-1] = end_idx
|
|
707
756
|
when :if
|
|
708
757
|
dest[begin_idx][-2] = else_idx == -1 ? end_idx : else_idx
|
|
@@ -720,6 +769,24 @@ module Wardite
|
|
|
720
769
|
raise e
|
|
721
770
|
end
|
|
722
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
|
+
|
|
723
790
|
# @rbs c: String
|
|
724
791
|
# @rbs buf: StringIO
|
|
725
792
|
# @rbs return: [Symbol, Symbol, Array[Symbol]]
|
|
@@ -732,8 +799,8 @@ module Wardite
|
|
|
732
799
|
namespace = Op::FC_OPERANDS[lower] || :default
|
|
733
800
|
return [namespace, sym, Op.operand_of(sym)]
|
|
734
801
|
end
|
|
735
|
-
namespace = Op::NAMESPACES[ord] || raise("unsupported code: #{ord}")
|
|
736
|
-
return [namespace, code, Op
|
|
802
|
+
namespace = Op::NAMESPACES[ord] || raise(LoadError, "unsupported code: #{ord}")
|
|
803
|
+
return [namespace, code, Op.operand_of(code)]
|
|
737
804
|
end
|
|
738
805
|
|
|
739
806
|
# @rbs return: DataSection
|
|
@@ -741,36 +808,32 @@ module Wardite
|
|
|
741
808
|
dest = DataSection.new
|
|
742
809
|
size = fetch_uleb128(@buf)
|
|
743
810
|
dest.size = size
|
|
744
|
-
sbuf = StringIO.new(@buf
|
|
811
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
745
812
|
|
|
746
813
|
len = fetch_uleb128(sbuf)
|
|
747
814
|
len.times do |i|
|
|
748
815
|
data_type = fetch_uleb128(sbuf)
|
|
749
816
|
case data_type
|
|
750
|
-
when 0x0
|
|
751
|
-
#
|
|
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
|
|
752
821
|
code = fetch_insn_while_end(sbuf)
|
|
753
822
|
ops = code_body(StringIO.new(code))
|
|
754
823
|
offset = decode_expr(ops)
|
|
755
824
|
len = fetch_uleb128(sbuf)
|
|
756
|
-
data = sbuf
|
|
757
|
-
if !data
|
|
758
|
-
raise LoadError, "buffer too short"
|
|
759
|
-
end
|
|
825
|
+
data = assert_read(sbuf, len).b
|
|
760
826
|
segment = DataSection::Segment.new do |seg|
|
|
761
827
|
seg.mode = :active
|
|
762
|
-
seg.mem_index =
|
|
763
|
-
seg.offset = offset
|
|
828
|
+
seg.mem_index = memory_index
|
|
829
|
+
seg.offset = offset & 0xffffffff
|
|
764
830
|
seg.data = data
|
|
765
831
|
end
|
|
766
832
|
dest.segments << segment
|
|
767
833
|
when 0x1
|
|
768
834
|
# passive
|
|
769
835
|
dsize = fetch_uleb128(sbuf)
|
|
770
|
-
data = sbuf
|
|
771
|
-
if !data
|
|
772
|
-
raise LoadError, "data too short"
|
|
773
|
-
end
|
|
836
|
+
data = assert_read(sbuf, dsize).b
|
|
774
837
|
segment = DataSection::Segment.new do |seg|
|
|
775
838
|
seg.mode = :passive
|
|
776
839
|
seg.mem_index = 0 # unused
|
|
@@ -778,17 +841,21 @@ module Wardite
|
|
|
778
841
|
seg.data = data
|
|
779
842
|
end
|
|
780
843
|
dest.segments << segment
|
|
844
|
+
else
|
|
845
|
+
raise LoadError, "unsupported data segment flags #{data_type}"
|
|
781
846
|
end
|
|
782
847
|
end
|
|
848
|
+
raise LoadError, "trailing data section bytes" unless sbuf.eof?
|
|
783
849
|
dest
|
|
784
850
|
end
|
|
785
851
|
|
|
786
852
|
# @rbs return: DataCountSection
|
|
787
853
|
def self.data_count_section
|
|
788
854
|
size = fetch_uleb128(@buf)
|
|
789
|
-
sbuf = StringIO.new(@buf
|
|
855
|
+
sbuf = StringIO.new(assert_read(@buf, size))
|
|
790
856
|
count = fetch_uleb128(sbuf)
|
|
791
857
|
dest = DataCountSection.new(count)
|
|
858
|
+
raise LoadError, "trailing data count bytes" unless sbuf.eof?
|
|
792
859
|
dest.size = size
|
|
793
860
|
dest
|
|
794
861
|
end
|
|
@@ -796,18 +863,19 @@ module Wardite
|
|
|
796
863
|
# @rbs sbuf: StringIO
|
|
797
864
|
# @rbs return: String
|
|
798
865
|
def self.fetch_insn_while_end(sbuf)
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
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)
|
|
811
879
|
end
|
|
812
880
|
|
|
813
881
|
# @rbs ops: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
|
|
@@ -853,16 +921,12 @@ module Wardite
|
|
|
853
921
|
return I64(arg)
|
|
854
922
|
when :f32_const
|
|
855
923
|
arg = op[2][0]
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
end
|
|
859
|
-
return F32(arg)
|
|
924
|
+
raise "Invalid definition of operand" unless arg.is_a?(FloatImmediate)
|
|
925
|
+
return F32.from_bits(arg.bits)
|
|
860
926
|
when :f64_const
|
|
861
927
|
arg = op[2][0]
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
end
|
|
865
|
-
return F64(arg)
|
|
928
|
+
raise "Invalid definition of operand" unless arg.is_a?(FloatImmediate)
|
|
929
|
+
return F64.from_bits(arg.bits)
|
|
866
930
|
else
|
|
867
931
|
raise "Unimplemented offset op: #{op.inspect}"
|
|
868
932
|
end
|
|
@@ -885,6 +949,7 @@ module Wardite
|
|
|
885
949
|
raise "[BUG] empty unpacked string" # guard rbs
|
|
886
950
|
end
|
|
887
951
|
|
|
952
|
+
raise LoadError, "tag exports are not supported" if kind == 4
|
|
888
953
|
index = fetch_uleb128(sbuf)
|
|
889
954
|
dest.add_desc do |desc|
|
|
890
955
|
desc.name = name
|
|
@@ -905,7 +970,7 @@ module Wardite
|
|
|
905
970
|
nil
|
|
906
971
|
end
|
|
907
972
|
|
|
908
|
-
# @rbs sbuf: StringIO
|
|
973
|
+
# @rbs sbuf: File|StringIO
|
|
909
974
|
# @rbs n: Integer
|
|
910
975
|
# @rbs return: String
|
|
911
976
|
def self.assert_read(sbuf, n)
|
|
@@ -913,10 +978,10 @@ module Wardite
|
|
|
913
978
|
if !ret
|
|
914
979
|
raise LoadError, "too short section size"
|
|
915
980
|
end
|
|
916
|
-
if ret.
|
|
981
|
+
if ret.bytesize != n
|
|
917
982
|
raise LoadError, "too short section size"
|
|
918
983
|
end
|
|
919
984
|
ret
|
|
920
985
|
end
|
|
921
986
|
end
|
|
922
|
-
end
|
|
987
|
+
end
|