wardite 1.2.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -15
  3. data/Rakefile +0 -177
  4. data/benchmark/README.md +10 -36
  5. data/benchmark/call_cost_worker.rb +3 -2
  6. data/benchmark/compare.rb +1 -2
  7. data/benchmark/compare_linear_memory_loads.py +144 -0
  8. data/benchmark/i32_memory.wat +5 -1
  9. data/benchmark/investigate_calls.rb +4 -4
  10. data/benchmark/linear_memory_loads.wat +52 -0
  11. data/benchmark/optimization_worker.rb +3 -2
  12. data/benchmark/patches/adr0017-spinel-signed64.patch +190 -0
  13. data/benchmark/patches/adr0017-wardite-split-load64.patch +70 -0
  14. data/benchmark/results/adr0019-startup.json +1687 -0
  15. data/benchmark/results/adr0020-i64.json +2034 -0
  16. data/benchmark/results/adr0021-optimizer.json +5798 -0
  17. data/benchmark/results/adr0022-lazy-mir.json +3057 -0
  18. data/benchmark/results/crc8-stream-repeat.csv +89 -0
  19. data/benchmark/results/crc8-stream.csv +89 -0
  20. data/benchmark/results/linear-memory-loads-a2aa05227.csv +793 -0
  21. data/benchmark/results/linear-memory-loads-stage-b.csv +353 -0
  22. data/benchmark/results/linear-memory-loads.csv +1057 -0
  23. data/benchmark/results/real-workloads-baseline-2026-11-18.json +1741 -0
  24. data/benchmark/results/spinel-072516af1.csv +149 -0
  25. data/benchmark/results/stack-vm-removal.json +334 -0
  26. data/benchmark/validate_m4.rb +0 -2
  27. data/docs/adr/0017-efflorescence-linear-memory-loads.md +90 -0
  28. data/docs/adr/0018-efflorescence-real-workload-measurement.md +126 -0
  29. data/docs/adr/0019-efflorescence-startup-quick-wins.md +85 -0
  30. data/docs/adr/0020-efflorescence-inline-i64-bit-operations.md +89 -0
  31. data/docs/adr/0021-efflorescence-optimize-real-functions.md +133 -0
  32. data/docs/adr/0022-efflorescence-lazy-function-build.md +111 -0
  33. data/docs/adr/0023-efflorescence-byte-sequences-and-copy-coalescing.md +56 -0
  34. data/docs/efflorescence-ja.md +3 -5
  35. data/docs/efflorescence-linear-memory-loads.md +134 -0
  36. data/docs/efflorescence-m6.md +1 -1
  37. data/docs/efflorescence-mir-generation-performance.md +58 -0
  38. data/docs/efflorescence-mir2-cache.md +7 -0
  39. data/docs/efflorescence-real-workloads.md +312 -0
  40. data/docs/efflorescence-spinel-linear-memory.md +2 -2
  41. data/docs/efflorescence.md +3 -5
  42. data/docs/stack-vm-removal.md +49 -0
  43. data/lib/wardite/cli/cli.rb +24 -24
  44. data/lib/wardite/efflorescence/byte_sequences.rb +80 -0
  45. data/lib/wardite/efflorescence/function_preparer.rb +64 -0
  46. data/lib/wardite/efflorescence/i64_arithmetic.rb +31 -0
  47. data/lib/wardite/efflorescence/inspection.rb +44 -12
  48. data/lib/wardite/efflorescence/ir.rb +33 -10
  49. data/lib/wardite/efflorescence/mir_cache.rb +10 -1
  50. data/lib/wardite/efflorescence/optimizer.rb +151 -42
  51. data/lib/wardite/efflorescence/store_access.rb +17 -0
  52. data/lib/wardite/efflorescence/translation_source.rb +50 -0
  53. data/lib/wardite/efflorescence/translator.rb +135 -19
  54. data/lib/wardite/efflorescence/vm.rb +116 -48
  55. data/lib/wardite/efflorescence.rb +3 -0
  56. data/lib/wardite/load.rb +46 -62
  57. data/lib/wardite/version.rb +1 -1
  58. data/lib/wardite.rb +21 -757
  59. data/scripts/build_spinel_zig.rb +29 -17
  60. data/sig/generated/wardite/cli/cli.rbs +4 -2
  61. data/sig/generated/wardite/efflorescence/byte_sequences.rbs +33 -0
  62. data/sig/generated/wardite/efflorescence/function_preparer.rbs +25 -0
  63. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +19 -0
  64. data/sig/generated/wardite/efflorescence/inspection.rbs +8 -0
  65. data/sig/generated/wardite/efflorescence/ir.rbs +18 -2
  66. data/sig/generated/wardite/efflorescence/optimizer.rbs +41 -3
  67. data/sig/generated/wardite/efflorescence/store_access.rbs +5 -0
  68. data/sig/generated/wardite/efflorescence/translation_source.rbs +34 -0
  69. data/sig/generated/wardite/efflorescence/translator.rbs +28 -1
  70. data/sig/generated/wardite/efflorescence/vm.rbs +19 -9
  71. data/sig/generated/wardite/load.rbs +18 -18
  72. data/sig/generated/wardite.rbs +4 -155
  73. metadata +33 -17
  74. data/benchmark/compare_engines.rb +0 -119
  75. data/benchmark/engine_phases.rb +0 -30
  76. data/lib/wardite/alu_f32.generated.rb +0 -261
  77. data/lib/wardite/alu_f64.generated.rb +0 -261
  78. data/lib/wardite/alu_i32.generated.rb +0 -510
  79. data/lib/wardite/alu_i64.generated.rb +0 -572
  80. data/lib/wardite/convert.generated.rb +0 -340
  81. data/scripts/gen_alu.rb +0 -864
  82. data/scripts/gen_conv.rb +0 -86
  83. data/scripts/templates/alu_module.rb.tmpl +0 -20
  84. data/scripts/templates/conv_module.rb.tmpl +0 -20
  85. data/sig/generated/wardite/alu_f32.generated.rbs +0 -12
  86. data/sig/generated/wardite/alu_f64.generated.rbs +0 -12
  87. data/sig/generated/wardite/alu_i32.generated.rbs +0 -12
  88. data/sig/generated/wardite/alu_i64.generated.rbs +0 -12
  89. data/sig/generated/wardite/convert.generated.rbs +0 -12
data/lib/wardite.rb CHANGED
@@ -11,17 +11,6 @@ require_relative "wardite/wasm_module"
11
11
  require_relative "wardite/load"
12
12
  require_relative "wardite/errors"
13
13
 
14
- module Wardite
15
- module Evaluator
16
- extend Wardite::ValueHelper
17
- end
18
- end
19
- require_relative "wardite/alu_i32.generated"
20
- require_relative "wardite/alu_i64.generated"
21
- require_relative "wardite/alu_f32.generated"
22
- require_relative "wardite/alu_f64.generated"
23
- require_relative "wardite/convert.generated"
24
-
25
14
  require "stringio"
26
15
 
27
16
  module Wardite
@@ -30,7 +19,7 @@ module Wardite
30
19
 
31
20
  attr_accessor :sections #: Array[Section]
32
21
 
33
- attr_accessor :runtime #: Runtime | Efflorescence::VM
22
+ attr_accessor :runtime #: Efflorescence::VM
34
23
 
35
24
  attr_accessor :types #: Array[Type]
36
25
 
@@ -48,9 +37,11 @@ module Wardite
48
37
  # @rbs engine: Symbol
49
38
  # @rbs disabled_optimizations: Array[Symbol]
50
39
  # @rbs mir_profile: bool
40
+ # @rbs eager_mir: bool
51
41
  # @rbs mir_cache: Efflorescence::MIRCache?
52
42
  # @rbs &blk: (Instance) -> void
53
- def initialize(import_object, engine: :efflorescence, disabled_optimizations: [], mir_profile: false, mir_cache: nil, &blk)
43
+ def initialize(import_object, engine: :efflorescence, disabled_optimizations: [], mir_profile: false, mir_cache: nil, eager_mir: false, &blk)
44
+ raise LoadError, "unknown engine: #{engine}" unless engine == :efflorescence
54
45
  @wasi = nil
55
46
 
56
47
  blk.call(self)
@@ -63,9 +54,9 @@ module Wardite
63
54
  @import_object = import_object #: Hash[Symbol, wasmModule]
64
55
 
65
56
  @tags = []
66
- check_module_features(engine)
57
+ check_module_features
67
58
  cached_functions = nil #: Array[Efflorescence::Function]?
68
- if engine == :efflorescence && mir_cache && !mir_profile
59
+ if mir_cache && !mir_profile
69
60
  cached_functions = mir_cache.read_functions(self)
70
61
  end
71
62
  types = type_section
@@ -73,7 +64,7 @@ module Wardite
73
64
  if cached_functions
74
65
  body.type_section = types
75
66
  else
76
- body.check_features(types, engine == :stack)
67
+ body.check_features(types)
77
68
  end
78
69
  end
79
70
  check_data_count
@@ -89,32 +80,28 @@ module Wardite
89
80
  end
90
81
  end
91
82
 
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
83
+ # A cache hit can decode its own pending bodies on inspection. A cold
84
+ # build replays the original bytes even if the host edits Store later.
85
+ code = code_section
86
+ source_code = code.source_bytes if code && !cached_functions
87
+ code.source_bytes = nil if code
88
+ @runtime = Efflorescence::VM.new(self, source_code: source_code, disabled_optimizations: disabled_optimizations, profile: mir_profile,
89
+ mir_cache: mir_cache, cached_functions: cached_functions, cache_checked: true, eager_mir: eager_mir)
99
90
  end
100
91
 
101
92
  # Run before allocating Store/Exports or executing a start function.
102
- # @rbs engine: Symbol
103
93
  # @rbs return: void
104
- def check_module_features(engine)
105
- check_memory_initialization if engine == :efflorescence
94
+ def check_module_features
95
+ check_memory_initialization
106
96
  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
97
+ import_section.imports.each do |import|
98
+ params = types&.defined_types&.[](import.sig_index)
99
+ results = types&.defined_results&.[](import.sig_index)
100
+ raise LoadError, "invalid import type index #{import.sig_index}" unless params && results
101
+ Efflorescence::FunctionEntry.check_signature(params, results, "#{import.module_name}.#{import.name}")
114
102
  end
115
103
  tag_section = @sections.find { |section| section.is_a?(TagSection) }
116
104
  if tag_section.is_a?(TagSection)
117
- raise LoadError, "stack engine does not support tags/EH" unless engine == :efflorescence
118
105
  tag_section.type_indices.each do |index|
119
106
  params = types&.defined_types&.[](index)
120
107
  results = types&.defined_results&.[](index)
@@ -125,9 +112,6 @@ module Wardite
125
112
  @tags << Efflorescence::Tag.new(params)
126
113
  end
127
114
  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
115
  end
132
116
 
133
117
  # @rbs &block: (Array[Symbol], Array[Symbol], CodeSection::CodeBody) -> void
@@ -333,622 +317,6 @@ module Wardite
333
317
  end
334
318
  end
335
319
 
336
- class Runtime
337
- include ValueHelper
338
-
339
- # TODO: add types of class that the stack accomodates
340
- attr_accessor :stack #: Array[wasmValue]
341
-
342
- attr_accessor :call_stack #: Array[Frame]
343
-
344
- attr_reader :instance #: Instance
345
-
346
- # @rbs inst: Instance
347
- def initialize(inst)
348
- @stack = []
349
- @call_stack = []
350
- @instance = inst
351
-
352
- invoke_start_section
353
- end
354
-
355
- # @rbs return: void
356
- def invoke_start_section
357
- start_section = instance.start_section
358
- if start_section
359
- call_by_index(start_section.func_index)
360
- end
361
- end
362
-
363
- # @rbs name: String|Symbol
364
- # @rbs return: bool
365
- def callable?(name)
366
- !! @instance.exports[name.to_s]
367
- end
368
-
369
- # @rbs name: String|Symbol
370
- # @rbs args: Array[Object]
371
- # @rbs return: Object|nil
372
- def call(name, args)
373
- if !callable?(name)
374
- raise ::NoMethodError, "function #{name} not found"
375
- end
376
- kind, fn = @instance.exports.mappings[name.to_s]
377
- if kind != 0
378
- raise ::NoMethodError, "#{name} is not a function"
379
- end
380
- if !fn.is_a?(WasmFunction) && !fn.is_a?(ExternalFunction)
381
- raise ::NoMethodError, "#{name} is not a function"
382
- end
383
- if fn.callsig.size != args.size
384
- raise ArgumentError, "unmatch arg size"
385
- end
386
- args.each_with_index do |arg, idx|
387
- case fn.callsig[idx]
388
- when :i32
389
- raise "type mismatch: i32(#{arg})" unless arg.is_a?(Integer)
390
- stack.push I32(arg)
391
- else
392
- raise "TODO: add me"
393
- end
394
- end
395
-
396
- case fn
397
- when WasmFunction
398
- r = invoke_internal(fn)
399
- r
400
- when ExternalFunction
401
- invoke_external(fn)
402
- else
403
- raise GenericError, "registered pointer is not to a function"
404
- end
405
- end
406
-
407
- # @rbs idx: Integer
408
- # @rbs return: void
409
- def call_by_index(idx)
410
- fn = @instance.store.funcs[idx]
411
-
412
- case fn
413
- when WasmFunction
414
- invoke_internal(fn)
415
- when ExternalFunction
416
- invoke_external(fn)
417
- else
418
- raise GenericError, "registered pointer is not to a function"
419
- end
420
- end
421
-
422
- # @rbs wasm_function: WasmFunction
423
- # @rbs return: void
424
- def push_frame(wasm_function)
425
- local_start = stack.size - wasm_function.callsig.size
426
- locals = stack[local_start..]
427
- if !locals
428
- raise LoadError, "stack too short"
429
- end
430
- self.stack = drained_stack(local_start)
431
- locals.concat(wasm_function.default_locals)
432
-
433
- arity = wasm_function.retsig.size
434
- frame = Frame.new(-1, stack.size, wasm_function.body, arity, locals)
435
- frame.findex = wasm_function.findex
436
- self.call_stack.push(frame)
437
- end
438
-
439
- # @rbs wasm_function: WasmFunction
440
- # @rbs return: Object|nil
441
- def invoke_internal(wasm_function)
442
- arity = wasm_function.retsig.size
443
- push_frame(wasm_function)
444
- execute!
445
-
446
- if arity > 0
447
- if arity > 1
448
- raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;"
449
- end
450
- if self.stack.empty?
451
- raise "[BUG] stack empry"
452
- end
453
- v = self.stack.pop
454
- return v
455
- end
456
-
457
- return nil
458
- end
459
-
460
- # @rbs!
461
- # $GLOBAL_EXTERNAL_ELAP: Float
462
- # $GLOBAL_EXTERNAL_TIMES: Integer
463
- $GLOBAL_EXTERNAL_ELAP = 0.0 #: Float
464
- $GLOBAL_EXTERNAL_TIMES = 0 #: Integer
465
- END {
466
- if ENV["WARDITE_TRACE"] == "1"
467
- $stderr.puts "external call count: #{$GLOBAL_EXTERNAL_TIMES}"
468
- $stderr.puts "external call elapsed: #{$GLOBAL_EXTERNAL_ELAP}(s)"
469
- end
470
- }
471
-
472
- # @rbs external_function: ExternalFunction
473
- # @rbs return: wasmValue|nil
474
- def invoke_external(external_function)
475
- start = Time.now.to_f
476
- $stderr.puts "[trace] call external function: #{external_function.name}" if ENV["WARDITE_TRACE"]
477
- local_start = stack.size - external_function.callsig.size
478
- args = stack[local_start..]
479
- if !args
480
- raise LoadError, "stack too short"
481
- end
482
- self.stack = drained_stack(local_start)
483
-
484
- proc = external_function.callable
485
- val = proc[self.instance.store, args]
486
- if !val
487
- return
488
- end
489
-
490
- case val
491
- when I32, I64, F32, F64
492
- return val
493
- when Integer
494
- case external_function.retsig[0]
495
- when :i32
496
- return I32(val)
497
- when :i64
498
- return I64(val)
499
- end
500
- when Float
501
- case external_function.retsig[0]
502
- when :f32
503
- return F32(val)
504
- when :f64
505
- return F64(val)
506
- end
507
- end
508
-
509
- raise "invalid type of value returned in proc. val: #{val.inspect}"
510
- ensure
511
- $GLOBAL_EXTERNAL_TIMES += 1
512
- $GLOBAL_EXTERNAL_ELAP += (Time.now.to_f - start)
513
- end
514
-
515
- # @rbs return: void
516
- def execute!
517
- loop do
518
- cur_frame = self.call_stack.last #: Frame
519
- if !cur_frame
520
- break
521
- end
522
- cur_frame.pc += 1
523
- insn = cur_frame.body[cur_frame.pc]
524
- if !insn
525
- break
526
- end
527
- eval_insn(cur_frame, insn)
528
- end
529
- end
530
-
531
- # @rbs frame: Frame
532
- # @rbs insn: [Symbol, Symbol, Array[operandItem], Integer?, Integer?]
533
- # @rbs return: void
534
- def eval_insn(frame, insn)
535
- namespace, code, operand, else_pos, end_pos = *insn
536
- case namespace
537
- when :convert
538
- return Evaluator.convert_eval_insn(self, frame, code, operand)
539
- when :i32
540
- return Evaluator.i32_eval_insn(self, frame, code, operand)
541
- when :i64
542
- return Evaluator.i64_eval_insn(self, frame, code, operand)
543
- when :f32
544
- return Evaluator.f32_eval_insn(self, frame, code, operand)
545
- when :f64
546
- return Evaluator.f64_eval_insn(self, frame, code, operand)
547
- end
548
-
549
- # unmached namespace...
550
- case code
551
- when :unreachable
552
- raise Unreachable, "unreachable op"
553
- when :nop
554
- return
555
-
556
- when :br
557
- level = operand[0]
558
- raise EvalError, "br op without level" if !level.is_a?(Integer)
559
- pc = do_branch(frame.labels, stack, level)
560
- frame.pc = pc
561
-
562
- when :br_if
563
- level = operand[0]
564
- raise EvalError, "br op without level" if !level.is_a?(Integer)
565
- cond = stack.pop
566
- raise EvalError, "cond not found" if !cond.is_a?(I32)
567
- if cond.value.zero?
568
- return
569
- end
570
- pc = do_branch(frame.labels, stack, level)
571
- frame.pc = pc
572
-
573
- when :br_table
574
- levels = operand[0]
575
- raise EvalError, "no level vector" unless levels.is_a?(OperandVector)
576
- level_vec = levels.entries
577
- default = operand[1]
578
- raise EvalError, "no default specified" if !default.is_a?(Integer)
579
- idx = stack.pop
580
- raise EvalError, "idx not found" if !idx.is_a?(I32)
581
- level = if idx.value_s < 0 || idx.value_s >= level_vec.size
582
- default
583
- else
584
- level_vec[idx.value_s]
585
- end
586
- pc = do_branch(frame.labels, stack, level)
587
- frame.pc = pc
588
-
589
- when :block
590
- block = operand[0]
591
- raise EvalError, "block op without block" if !block.is_a?(Block)
592
- next_pc = end_pos || -1
593
- label = Label.new(:block, next_pc, stack.size, block.result_size)
594
- frame.labels.push(label)
595
-
596
- when :loop
597
- block = operand[0]
598
- raise EvalError, "loop op without block" if !block.is_a?(Block)
599
- start = frame.pc
600
- next_pc = end_pos || -1
601
- label = Label.new(:loop, next_pc, stack.size, block.result_size, start)
602
- frame.labels.push(label)
603
-
604
- when :if
605
- block = operand[0]
606
- raise EvalError, "if op without block" if !block.is_a?(Block)
607
- cond = stack.pop
608
- raise EvalError, "cond not found" if !cond.is_a?(I32)
609
- next_pc = end_pos || -1
610
-
611
- if cond.value.zero?
612
- frame.pc = else_pos || -1
613
- end
614
-
615
- if frame.pc == next_pc
616
- # This means if block has no else instr.
617
- return
618
- end
619
-
620
- label = Label.new(:if, next_pc, stack.size, block.result_size)
621
- frame.labels.push(label)
622
-
623
- when :else
624
- if old_label = frame.labels.pop
625
- frame.pc = old_label.pc
626
- stack_unwind(old_label.sp, old_label.arity)
627
- else
628
- raise EvalError, "else should be in if block"
629
- end
630
-
631
- when :call
632
- idx = operand[0]
633
- raise EvalError, "[BUG] local operand not found" if !idx.is_a?(Integer)
634
- fn = self.instance.store.funcs[idx]
635
- case fn
636
- when WasmFunction
637
- push_frame(fn)
638
- when ExternalFunction
639
- ret = invoke_external(fn)
640
- self.stack.push ret if ret
641
- else
642
- raise GenericError, "got a non-function pointer"
643
- end
644
-
645
- when :call_indirect
646
- table = self.instance.store.tables[0]
647
- raise EvalError, "table required but not found" if !table
648
- type_idx = operand[0]
649
- raise EvalError, "[BUG] index operand invalid" if !type_idx.is_a?(Integer)
650
- nullbyte = operand[1]
651
- raise EvalError, "[BUG] invalid bytearray of call_indirect" if nullbyte != 0x0
652
- table_idx = stack.pop
653
- raise EvalError, "[BUG] index stack invalid" if !table_idx.is_a?(I32)
654
- fntype = self.instance.types[type_idx]
655
- if !fntype
656
- raise EvalError, "undefined type index: idx=#{type_idx}"
657
- end
658
- refs = self.instance.store.tables[0]&.refs
659
- if !refs
660
- raise EvalError, "uninitialized element idx:#{table_idx}"
661
- end
662
-
663
- fn = refs[table_idx.value]
664
- case fn
665
- when WasmFunction
666
- if table.type != :funcref
667
- raise EvalError, "invalid type of elem; expected: #{table.type}"
668
- end
669
- fn = fn.clone(override_type: fntype)
670
- push_frame(fn)
671
- when ExternalFunction
672
- if table.type != :externref
673
- raise EvalError, "invalid type of elem; expected: #{table.type}"
674
- end
675
- fn = fn.clone(override_type: fntype)
676
- ret = invoke_external(fn)
677
- self.stack.push ret if ret
678
- when nil
679
- raise EvalError, "uninitialized element idx:#{table_idx.value}"
680
- else
681
- raise EvalError, "[BUG] unknwon function type #{fn}"
682
- end
683
-
684
- when :return
685
- old_frame = call_stack.pop
686
- if !old_frame
687
- raise EvalError, "maybe empty call stack"
688
- end
689
-
690
- stack_unwind(old_frame.sp, old_frame.arity)
691
-
692
- when :end
693
- if old_label = frame.labels.pop
694
- frame.pc = old_label.pc
695
- stack_unwind(old_label.sp, old_label.arity)
696
- else
697
- old_frame = call_stack.pop
698
- if !old_frame
699
- raise EvalError, "maybe empty call stack"
700
- end
701
- stack_unwind(old_frame.sp, old_frame.arity)
702
- end
703
-
704
- when :drop
705
- stack.pop
706
-
707
- when :select
708
- cond, right, left = stack.pop, stack.pop, stack.pop
709
- if !cond.is_a?(I32)
710
- raise EvalError, "invalid stack for select"
711
- end
712
- if !right || !left
713
- raise EvalError, "stack too short"
714
- end
715
- stack.push(cond.value != 0 ? left : right)
716
-
717
- when :local_get
718
- idx = operand[0]
719
- if !idx.is_a?(Integer)
720
- raise EvalError, "[BUG] invalid type of operand"
721
- end
722
- local = frame.locals[idx]
723
- if !local
724
- # require "irb"; binding.irb
725
- raise EvalError, "local not found, idx = #{idx}"
726
- end
727
- stack.push(local)
728
-
729
- when :local_set
730
- idx = operand[0]
731
- if !idx.is_a?(Integer)
732
- raise EvalError, "[BUG] invalid type of operand"
733
- end
734
- value = stack.pop
735
- if !value
736
- raise EvalError, "value should be pushed"
737
- end
738
- frame.locals[idx] = value
739
-
740
- when :local_tee
741
- idx = operand[0]
742
- if !idx.is_a?(Integer)
743
- raise EvalError, "[BUG] invalid type of operand"
744
- end
745
- value = stack.pop
746
- if !value
747
- raise EvalError, "value should be pushed"
748
- end
749
- frame.locals[idx] = value
750
- stack.push value
751
-
752
- when :global_get
753
- idx = operand[0]
754
- if !idx.is_a?(Integer)
755
- raise EvalError, "[BUG] invalid type of operand"
756
- end
757
- global = instance.store.globals[idx]
758
- if !global
759
- raise EvalError, "global not found"
760
- end
761
- stack.push(global.value)
762
-
763
- when :global_set
764
- idx = operand[0]
765
- if !idx.is_a?(Integer)
766
- raise EvalError, "[BUG] invalid type of operand"
767
- end
768
- current_global = instance.store.globals[idx]
769
- if !current_global
770
- raise EvalError, "global index not valid"
771
- end
772
- if !current_global.mutable?
773
- raise EvalError, "global not mutable"
774
- end
775
- value = stack.pop
776
- if !value
777
- raise EvalError, "value should be pushed"
778
- end
779
- current_global.value = value
780
- instance.store.globals[idx] = current_global
781
-
782
- when :memory_size
783
- memory = instance.store.memories[0] || raise("[BUG] no memory")
784
- stack.push(I32(memory.current))
785
-
786
- when :memory_grow
787
- delta = stack.pop
788
- if !delta.is_a?(I32)
789
- raise EvalError, "maybe stack too short"
790
- end
791
- memory = instance.store.memories[0] || raise("[BUG] no memory")
792
- stack.push(I32(memory.grow(delta.value)))
793
-
794
- when :memory_init
795
- idx = operand[0]
796
- if !idx.is_a?(Integer)
797
- raise EvalError, "[BUG] invalid type of operand"
798
- end
799
- if operand[1] != 0x0
800
- $stderr.puts "warning: :memory_init is not ending with 0x00"
801
- end
802
- data_sec = instance.data_section
803
- if !data_sec
804
- raise EvalError, "data segment out of range"
805
- end
806
- data_seg = data_sec.segments[idx]
807
- if !data_seg
808
- raise EvalError, "data segment out of range"
809
- end
810
-
811
- memory = instance.store.memories[0] || raise("[BUG] no memory")
812
- length, src_offset, dest_offset = stack.pop, stack.pop, stack.pop
813
- if !length.is_a?(I32) || !src_offset.is_a?(I32) || !dest_offset.is_a?(I32)
814
- raise EvalError, "invalid stack values"
815
- end
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
823
-
824
- when :memory_copy
825
- if operand[0] != 0x0 || operand[1] != 0x0
826
- $stderr.puts "warning: :memory_copy is not ending with 0x00"
827
- end
828
- length, src_offset, dest_offset = stack.pop, stack.pop, stack.pop
829
- if !length.is_a?(I32) || !src_offset.is_a?(I32) || !dest_offset.is_a?(I32)
830
- raise EvalError, "invalid stack values"
831
- end
832
- memory = instance.store.memories[0] || raise("[BUG] no memory")
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
840
-
841
- when :memory_fill
842
- if operand[0] != 0x0
843
- $stderr.puts "warning: :memory_fill is not ending with 0x00"
844
- end
845
- length, byte, dest_offset = stack.pop, stack.pop, stack.pop
846
- if !length.is_a?(I32) || !byte.is_a?(I32) || !dest_offset.is_a?(I32)
847
- raise EvalError, "invalid stack values"
848
- end
849
- memory = instance.store.memories[0] || raise("[BUG] no memory")
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
856
-
857
- else
858
- raise "TODO! unsupported #{insn.inspect}"
859
- end
860
-
861
- rescue => e
862
- if ENV["DEBUG"]
863
- require "pp"
864
- $stderr.puts "instance:::\n#{self.instance.pretty_inspect}"
865
- $stderr.puts "frame:::\n#{frame.pretty_inspect}"
866
- $stderr.puts "stack:::\n#{stack.pretty_inspect}"
867
- end
868
- raise e
869
- end
870
-
871
- # @rbs labels: Array[Label]
872
- # @rbs stack: Array[wasmValue]
873
- # @rbs level: Integer
874
- # @rbs return: Integer
875
- def do_branch(labels, stack, level)
876
- idx = labels.size - 1 - level
877
- label = labels[idx]
878
- pc = if label.kind == :loop
879
- # keep the top of labels for loop again...
880
- while labels.size > idx + 1
881
- labels.pop
882
- end
883
- stack_unwind(label.sp, 0)
884
- label.start || raise(EvalError, "loop withour start")
885
- else
886
- while labels.size > idx
887
- labels.pop
888
- end
889
- stack_unwind(label.sp, label.arity)
890
- label.pc
891
- end
892
-
893
- pc
894
- end
895
-
896
- # unwind the stack and put return value if exists
897
- # @rbs sp: Integer
898
- # @rbs arity: Integer
899
- # @rbs return: void
900
- def stack_unwind(sp, arity)
901
- if arity > 0
902
- if arity > 1
903
- raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;"
904
- end
905
- value = stack.pop
906
- if !value
907
- raise EvalError, "cannot obtain return value"
908
- end
909
- self.stack = drained_stack(sp)
910
- stack.push value
911
- else
912
- self.stack = drained_stack(sp)
913
- end
914
- end
915
-
916
- # @rbs finish: Integer
917
- # @rbs return: Array[wasmValue]
918
- def drained_stack(finish)
919
- drained = stack[0...finish]
920
- if ! drained
921
- $stderr.puts "warning: state of stack: #{stack.inspect}"
922
- raise EvalError, "stack too short"
923
- end
924
- return drained
925
- end
926
-
927
- # @rbs name: Symbol
928
- # @rbs args: Array[Object]
929
- # @rbs return: untyped
930
- def method_missing(name, *args)
931
- if callable? name
932
- call(name, args)
933
- else
934
- super
935
- end
936
- end
937
-
938
- # @rbs name: String|Symbol
939
- # @rbs include_private: bool
940
- # @rbs return: bool
941
- def respond_to?(name, include_private = false)
942
- callable?(name) || super
943
- end
944
-
945
- # @rbs args: Array[Object]
946
- # @rbs return: Object?
947
- def _start(*args)
948
- call(:_start, args)
949
- end
950
- end
951
-
952
320
  class Type
953
321
  attr_accessor :callsig #: Array[Symbol]
954
322
 
@@ -963,63 +331,6 @@ module Wardite
963
331
  end
964
332
  end
965
333
 
966
- class Frame
967
- attr_accessor :pc #: Integer
968
- attr_accessor :sp #: Integer
969
-
970
- attr_accessor :body #: Array[[Symbol, Symbol, Array[Wardite::operandItem], Integer?, Integer?]]
971
-
972
- attr_accessor :arity #: Integer
973
-
974
- attr_accessor :labels #: Array[Label]
975
-
976
- attr_accessor :locals #: Array[wasmValue]
977
-
978
- attr_accessor :findex #: Integer
979
-
980
- # @rbs pc: Integer
981
- # @rbs sp: Integer
982
- # @rbs body: Array[[Symbol, Symbol, Array[Wardite::operandItem], Integer?, Integer?]]
983
- # @rbs arity: Integer
984
- # @rbs locals: Array[wasmValue]
985
- # @rbs return: void
986
- def initialize(pc, sp, body, arity, locals)
987
- @pc = pc
988
- @sp = sp
989
- @body = body
990
- @arity = arity
991
- @locals = locals
992
- @labels = []
993
-
994
- @findex = 0
995
- end
996
- end
997
-
998
- class Label
999
- attr_accessor :kind #: (:if|:loop|:block)
1000
-
1001
- attr_accessor :pc #: Integer
1002
- attr_accessor :sp #: Integer
1003
-
1004
- attr_accessor :arity #: Integer
1005
-
1006
- attr_accessor :start #: Integer|nil
1007
-
1008
- # @rbs kind: (:if|:loop|:block)
1009
- # @rbs pc: Integer
1010
- # @rbs sp: Integer
1011
- # @rbs arity: Integer
1012
- # @rbs start: Integer|nil
1013
- # @rbs return: void
1014
- def initialize(kind, pc, sp, arity, start=nil)
1015
- @kind = kind
1016
- @pc = pc
1017
- @sp = sp
1018
- @arity = arity
1019
- @start = start
1020
- end
1021
- end
1022
-
1023
334
  class Store
1024
335
  attr_accessor :funcs #: Array[WasmFunction|ExternalFunction]
1025
336
 
@@ -1426,19 +737,6 @@ module Wardite
1426
737
  @result_types = results.dup
1427
738
  end
1428
739
 
1429
- # @rbs return: bool
1430
- def void?
1431
- !!block_types
1432
- end
1433
-
1434
- # @rbs return: Integer
1435
- def result_size
1436
- if block_types # !void?
1437
- block_types.size
1438
- else
1439
- 0
1440
- end
1441
- end
1442
740
  end
1443
741
 
1444
742
  # @rbs!
@@ -1492,7 +790,6 @@ module Wardite
1492
790
  # TODO: common interface btw. WasmFunction and ExternalFunction?
1493
791
  # may be _WasmCallable?
1494
792
  class WasmFunction
1495
- include ValueHelper
1496
793
 
1497
794
  attr_accessor :callsig #: Array[Symbol]
1498
795
 
@@ -1502,8 +799,6 @@ module Wardite
1502
799
 
1503
800
  attr_accessor :findex #: Integer
1504
801
 
1505
- # @rbs @default_locals: Array[wasmValue]?
1506
-
1507
802
  # @rbs callsig: Array[Symbol]
1508
803
  # @rbs retsig: Array[Symbol]
1509
804
  # @rbs code_body: CodeSection::CodeBody
@@ -1514,7 +809,6 @@ module Wardite
1514
809
 
1515
810
  @code_body = code_body
1516
811
  @findex = 0 # for debug
1517
- @default_locals = nil
1518
812
  end
1519
813
 
1520
814
  # @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
@@ -1532,36 +826,6 @@ module Wardite
1532
826
  code_body.locals_count
1533
827
  end
1534
828
 
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
-
1541
- # @rbs return: Array[wasmValue]
1542
- def construct_default_locals
1543
- locals = [] #: Array[wasmValue]
1544
- locals_count.each_with_index do |count, i|
1545
- typ = locals_type[i]
1546
- count.times do
1547
- case typ
1548
- when :i32, :u32
1549
- locals.push I32(0)
1550
- when :i64, :u64
1551
- locals.push I64(0)
1552
- when :f32
1553
- locals.push F32(0.0)
1554
- when :f64
1555
- locals.push F64(0.0)
1556
- else
1557
- $stderr.puts "warning: unknown type #{typ.inspect}. default to I32"
1558
- locals.push I32(0)
1559
- end
1560
- end
1561
- end
1562
- locals
1563
- end
1564
-
1565
829
  # @rbs override_type: Type?
1566
830
  # @rbs return: WasmFunction
1567
831
  def clone(override_type: nil)