wardite 1.1.0 → 1.2.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +9 -1
  3. data/README.md +36 -4
  4. data/benchmark/README.md +8 -0
  5. data/benchmark/compare_mir_generation_builds.rb +66 -0
  6. data/benchmark/count_ruby_wasm_functions.rb +37 -0
  7. data/benchmark/generate_mir_generation_fixtures.rb +44 -0
  8. data/benchmark/measure_mir_cache_restore.rb +59 -0
  9. data/benchmark/measure_mir_generation.rb +52 -0
  10. data/benchmark/mir_copy_worker.rb +29 -0
  11. data/benchmark/mir_generation_worker.rb +50 -0
  12. data/benchmark/prepare_mir_generation_profile.rb +80 -0
  13. data/benchmark/profile_inline_snapshots.rb +63 -0
  14. data/benchmark/profile_mir_generation_allocations.rb +73 -0
  15. data/benchmark/results/efflorescence-mir2-function-restore.json +480 -0
  16. data/benchmark/results/mir-generation-adr0014.json +1779 -0
  17. data/benchmark/results/mir-generation-adr0015.json +3132 -0
  18. data/benchmark/results/mir-generation-allocations.json +236 -0
  19. data/benchmark/results/mir-generation-coarse.json +909 -0
  20. data/benchmark/results/mir-generation-control.json +463 -0
  21. data/benchmark/results/mir-generation-detail.json +387 -0
  22. data/benchmark/results/mir-generation-scaling.json +2945 -0
  23. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +1 -1
  24. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +1 -1
  25. data/docs/adr/0014-efflorescence-mir-build-validation-and-decoding.md +136 -0
  26. data/docs/adr/0015-efflorescence-selective-inline-snapshots.md +150 -0
  27. data/docs/adr/0016-efflorescence-generation-tagged-copy-propagation.md +163 -0
  28. data/docs/efflorescence-mir-build-validation-and-decoding-results.md +93 -0
  29. data/docs/efflorescence-mir-generation-implementation.md +77 -0
  30. data/docs/efflorescence-mir-generation-performance.md +184 -0
  31. data/docs/efflorescence-mir2-cache.md +31 -2
  32. data/docs/efflorescence-selective-inline-snapshots-results.md +92 -0
  33. data/docs/efflorescence-spinel-linear-memory.md +6 -4
  34. data/lib/wardite/cli/cli.rb +3 -1
  35. data/lib/wardite/efflorescence/ir.rb +131 -40
  36. data/lib/wardite/efflorescence/mir_cache.rb +165 -73
  37. data/lib/wardite/efflorescence/optimizer.rb +65 -15
  38. data/lib/wardite/efflorescence/register_validation.rb +162 -32
  39. data/lib/wardite/efflorescence/snapshot.rb +5 -2
  40. data/lib/wardite/efflorescence/vm.rb +4 -3
  41. data/lib/wardite/instruction.rb +42 -1
  42. data/lib/wardite/leb128.rb +2 -5
  43. data/lib/wardite/load.rb +154 -75
  44. data/lib/wardite/version.rb +1 -1
  45. data/lib/wardite.rb +1 -1
  46. data/scripts/build_spinel_zig.rb +121 -0
  47. data/sig/generated/wardite/efflorescence/ir.rbs +34 -3
  48. data/sig/generated/wardite/efflorescence/mir_cache.rbs +41 -14
  49. data/sig/generated/wardite/efflorescence/optimizer.rbs +27 -3
  50. data/sig/generated/wardite/efflorescence/register_validation.rbs +45 -4
  51. data/sig/generated/wardite/efflorescence/snapshot.rbs +2 -1
  52. data/sig/generated/wardite/instruction.rbs +23 -0
  53. data/sig/generated/wardite/load.rbs +63 -9
  54. data/sig/io_buffer.rbs +7 -0
  55. metadata +28 -3
  56. data/spinel/patches/0001-io-buffer-string-view.patch +0 -699
  57. data/spinel/patches/0002-io-buffer-allocation-error.patch +0 -47
data/lib/wardite/load.rb CHANGED
@@ -136,14 +136,28 @@ module Wardite
136
136
  end
137
137
  end
138
138
 
139
+ # The loader owns this result until a CodeBody exposes its mutable instructions.
140
+ class DecodedBody
141
+ attr_reader :instructions #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
142
+ attr_reader :blocks #: Array[Block]
143
+ attr_reader :has_eh_instructions #: bool
144
+
145
+ # @rbs instructions: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
146
+ # @rbs blocks: Array[Block]
147
+ # @rbs has_eh_instructions: bool
148
+ def initialize(instructions, blocks, has_eh_instructions)
149
+ @instructions = instructions
150
+ @blocks = blocks
151
+ @has_eh_instructions = has_eh_instructions
152
+ end
153
+ end
154
+
139
155
  class CodeSection < Section
140
156
  class CodeBody
141
157
  attr_accessor :locals_count #: Array[Integer]
142
158
 
143
159
  attr_accessor :locals_type #: Array[Symbol]
144
160
 
145
- attr_writer :instruction_bytes #: String?
146
-
147
161
  attr_writer :type_section #: TypeSection?
148
162
 
149
163
  # @rbs &blk: (CodeBody) -> void
@@ -153,6 +167,7 @@ module Wardite
153
167
  @locals_type = []
154
168
  @body = [] #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
155
169
  @instruction_bytes = nil
170
+ @decoded = nil #: DecodedBody?
156
171
  @type_section = nil
157
172
  blk.call(self)
158
173
  end
@@ -160,6 +175,8 @@ module Wardite
160
175
  # @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
161
176
  def body
162
177
  check_features(@type_section, false) if @instruction_bytes
178
+ # The caller can mutate both the array and the Block objects in it.
179
+ @decoded = nil
163
180
  @body
164
181
  end
165
182
 
@@ -168,6 +185,20 @@ module Wardite
168
185
  def body=(instructions)
169
186
  @body = instructions
170
187
  @instruction_bytes = nil
188
+ @decoded = nil
189
+ end
190
+
191
+ # @rbs bytes: String?
192
+ # @rbs return: void
193
+ def instruction_bytes=(bytes)
194
+ @instruction_bytes = bytes
195
+ @decoded = nil
196
+ end
197
+
198
+ # @rbs buf: File|StringIO
199
+ # @rbs return: void
200
+ def decode_from(buf)
201
+ install_decoded(BinaryLoader.decode_body(buf))
171
202
  end
172
203
 
173
204
  # @rbs types: TypeSection?
@@ -177,38 +208,69 @@ module Wardite
177
208
  if stack && locals_type.include?(:exnref)
178
209
  raise LoadError, "stack engine does not support exnref"
179
210
  end
180
- bytes = @instruction_bytes
181
- instructions = bytes ? BinaryLoader.code_body(StringIO.new(bytes)) : @body
182
- check_instructions(instructions, types, stack)
183
- self.body = instructions if bytes
211
+ if (bytes = @instruction_bytes)
212
+ decoded = BinaryLoader.decode_body(StringIO.new(bytes))
213
+ check_decoded(decoded, types, stack)
214
+ install_decoded(decoded)
215
+ elsif (decoded = @decoded)
216
+ check_decoded(decoded, types, stack)
217
+ else
218
+ check_instructions(@body, types, stack)
219
+ end
184
220
  end
185
221
 
186
222
  private
187
223
 
224
+ # @rbs decoded: DecodedBody
225
+ # @rbs return: void
226
+ def install_decoded(decoded)
227
+ @body = decoded.instructions
228
+ @instruction_bytes = nil
229
+ @decoded = decoded
230
+ end
231
+
232
+ # @rbs decoded: DecodedBody
233
+ # @rbs types: TypeSection?
234
+ # @rbs stack: bool
235
+ # @rbs return: void
236
+ def check_decoded(decoded, types, stack)
237
+ if stack && decoded.has_eh_instructions
238
+ raise LoadError, "stack engine does not support EH"
239
+ end
240
+ decoded.blocks.each { |block| check_block(block, types, stack) }
241
+ end
242
+
188
243
  # @rbs instructions: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
189
244
  # @rbs types: TypeSection?
190
245
  # @rbs stack: bool
191
246
  # @rbs return: void
192
247
  def check_instructions(instructions, types, stack)
193
248
  instructions.each do |instruction|
194
- if stack && [:try_table, :throw, :throw_ref].include?(instruction[1])
249
+ if stack && BinaryLoader.eh_instruction?(instruction[1])
195
250
  raise LoadError, "stack engine does not support EH"
196
251
  end
197
252
  instruction[2].each do |operand|
198
- next unless operand.is_a?(Block)
199
- index = operand.type_index
200
- if index >= 0
201
- params = types&.defined_types&.[](index)
202
- results = types&.defined_results&.[](index)
203
- raise LoadError, "invalid block type index #{index}" unless params && results
204
- operand.resolve(params, results)
205
- end
206
- if stack && (index >= 0 || operand.result_types.include?(:exnref))
207
- raise LoadError, "stack engine does not support indexed/reference block signatures"
208
- end
253
+ check_block(operand, types, stack) if operand.is_a?(Block)
209
254
  end
210
255
  end
211
256
  end
257
+
258
+ # @rbs block: Block
259
+ # @rbs types: TypeSection?
260
+ # @rbs stack: bool
261
+ # @rbs return: void
262
+ def check_block(block, types, stack)
263
+ index = block.type_index
264
+ if index >= 0
265
+ params = types&.defined_types&.[](index)
266
+ results = types&.defined_results&.[](index)
267
+ raise LoadError, "invalid block type index #{index}" unless params && results
268
+ block.resolve(params, results)
269
+ end
270
+ if stack && (index >= 0 || block.result_types.include?(:exnref))
271
+ raise LoadError, "stack engine does not support indexed/reference block signatures"
272
+ end
273
+ end
212
274
  end
213
275
 
214
276
  attr_accessor :func_codes #:Array[CodeBody]
@@ -331,8 +393,9 @@ module Wardite
331
393
  # @rbs mir_profile: bool
332
394
  # @rbs mir_cache: String?
333
395
  # @rbs mir_cache_verify: Symbol
396
+ # @rbs mir_cache_eager: bool
334
397
  # @rbs return: Instance
335
- def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false, mir_cache: nil, mir_cache_verify: :checksum)
398
+ def self.load_from_buffer(buf, import_object: {}, enable_wasi: true, engine: :stack, disabled_optimizations: [], mir_profile: false, mir_cache: nil, mir_cache_verify: :checksum, mir_cache_eager: false)
336
399
  start = Time.now.to_f #: Float
337
400
  unless mir_cache_verify == :checksum || mir_cache_verify == :sha256
338
401
  raise ArgumentError, "MIR cache verification must be checksum or sha256"
@@ -347,7 +410,7 @@ module Wardite
347
410
  bytes = buf.read || "".b
348
411
  configuration = Efflorescence::Configuration.new(disabled_optimizations)
349
412
  disabled_optimizations = configuration.disabled
350
- cache = Efflorescence::MIRCache.new(mir_cache, bytes, configuration, verify: mir_cache_verify)
413
+ cache = Efflorescence::MIRCache.new(mir_cache, bytes, configuration, verify: mir_cache_verify, eager: mir_cache_eager)
351
414
  @buf = StringIO.new(bytes)
352
415
  end
353
416
  end
@@ -409,11 +472,8 @@ module Wardite
409
472
  sections = [] #: Array[Section]
410
473
 
411
474
  loop do
412
- byte = @buf.read(1)
413
- if !byte
414
- break
415
- end
416
- code = byte.ord
475
+ code = @buf.getbyte
476
+ break unless code
417
477
 
418
478
  section = case code
419
479
  when Wardite::SectionType
@@ -465,15 +525,15 @@ module Wardite
465
525
 
466
526
  len = fetch_uleb128(sbuf)
467
527
  len.times do |i|
468
- fncode = assert_read(sbuf, 1)
469
- if fncode != "\x60"
528
+ fncode = assert_byte(sbuf)
529
+ if fncode != 0x60
470
530
  raise LoadError, "not a function definition"
471
531
  end
472
532
 
473
533
  arglen = fetch_uleb128(sbuf)
474
534
  arg = [] #: Array[Symbol]
475
535
  arglen.times do
476
- case ty = assert_read(sbuf, 1)&.ord
536
+ case ty = assert_byte(sbuf)
477
537
  when 0x69
478
538
  arg << :exnref
479
539
  when 0x7f
@@ -493,7 +553,7 @@ module Wardite
493
553
  retlen = fetch_uleb128(sbuf)
494
554
  ret = [] #: Array[Symbol]
495
555
  retlen.times do
496
- case ty = assert_read(sbuf, 1)&.ord
556
+ case ty = assert_byte(sbuf)
497
557
  when 0x69
498
558
  ret << :exnref
499
559
  when 0x7f
@@ -520,7 +580,7 @@ module Wardite
520
580
  dest.size = fetch_uleb128(@buf)
521
581
  sbuf = StringIO.new(assert_read(@buf, dest.size))
522
582
  fetch_uleb128(sbuf).times do
523
- raise LoadError, "unsupported tag attribute" unless assert_read(sbuf, 1).ord == 0
583
+ raise LoadError, "unsupported tag attribute" unless assert_byte(sbuf) == 0
524
584
  dest.type_indices << fetch_uleb128(sbuf)
525
585
  end
526
586
  raise LoadError, "trailing tag section data" unless sbuf.eof?
@@ -540,11 +600,7 @@ module Wardite
540
600
  module_name = assert_read(sbuf, mlen)
541
601
  nlen = fetch_uleb128(sbuf)
542
602
  name = assert_read(sbuf, nlen)
543
- kind_ = assert_read(sbuf, 1)
544
- kind = kind_[0]&.ord
545
- if !kind
546
- raise "[BUG] empty unpacked string" # guard rbs
547
- end
603
+ kind = assert_byte(sbuf)
548
604
 
549
605
  raise LoadError, "tag imports are not supported" if kind == 4
550
606
  raise LoadError, "unsupported import kind #{kind} for #{module_name}.#{name}" unless kind == 0
@@ -616,7 +672,7 @@ module Wardite
616
672
  code = fetch_insn_while_end(sbuf)
617
673
  ops = code_body(StringIO.new(code))
618
674
  dest.table_offsets << decode_expr(ops)
619
- if etype == 2 && assert_read(sbuf, 1).ord != 0
675
+ if etype == 2 && assert_byte(sbuf) != 0
620
676
  raise LoadError, "unsupported element type"
621
677
  end
622
678
 
@@ -637,12 +693,12 @@ module Wardite
637
693
 
638
694
  len = fetch_uleb128(sbuf)
639
695
  len.times do |i|
640
- gtype = Op.i2type(assert_read(sbuf, 1).ord)
641
- mut = sbuf.read 1
696
+ gtype = Op.i2type(assert_byte(sbuf))
697
+ mut = sbuf.getbyte
642
698
  if !mut
643
699
  raise LoadError, "global section too short"
644
700
  end
645
- unless mut.ord == 0 || mut.ord == 1
701
+ unless mut == 0 || mut == 1
646
702
  raise LoadError, "unsupported global mutability flags"
647
703
  end
648
704
 
@@ -660,7 +716,7 @@ module Wardite
660
716
 
661
717
  global = GlobalSection::Global.new do |g|
662
718
  g.type = gtype
663
- g.mutable = (mut.ord == 0x01)
719
+ g.mutable = (mut == 0x01)
664
720
  g.shared = false # always
665
721
  g.value = value
666
722
  end
@@ -727,50 +783,58 @@ module Wardite
727
783
  def self.read_function_body(sbuf, defer_code = false)
728
784
  ilen = fetch_uleb128(sbuf)
729
785
  code = assert_read(sbuf, ilen)
730
- last_code = code[-1]
786
+ last_code = code.getbyte(-1)
731
787
  if ! last_code
732
788
  raise "[BUG] empty code fetched" # guard for steep check
733
789
  end
734
- if last_code.ord != 0x0b
735
- $stderr.puts "warning: instruction not ended with inst end(0x0b): 0x0#{last_code.ord}"
790
+ if last_code != 0x0b
791
+ $stderr.puts "warning: instruction not ended with inst end(0x0b): 0x0#{last_code}"
736
792
  end
737
793
  cbuf = StringIO.new(code)
738
794
  CodeSection::CodeBody.new do |body|
739
795
  locals_len = fetch_uleb128(cbuf)
740
796
  while locals_len > 0
741
797
  body.locals_count << fetch_uleb128(cbuf)
742
- body.locals_type << Op.i2type(assert_read(cbuf, 1).ord)
798
+ body.locals_type << Op.i2type(assert_byte(cbuf))
743
799
  locals_len -= 1
744
800
  end
745
801
  if defer_code
746
802
  body.instruction_bytes = (cbuf.read || "".b).freeze
747
803
  else
748
- body.body = code_body(cbuf)
804
+ body.decode_from(cbuf)
749
805
  end
750
806
  end
751
807
  end
752
808
 
753
- # @rbs buf: StringIO
809
+ # @rbs buf: File|StringIO
754
810
  # @rbs return: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
755
811
  def self.code_body(buf)
812
+ decode_body(buf).instructions
813
+ end
814
+
815
+ # @rbs buf: File|StringIO
816
+ # @rbs return: DecodedBody
817
+ def self.decode_body(buf)
756
818
  dest = [] #: Array[[Symbol, Symbol, Array[operandItem], Integer?, Integer?]]
819
+ blocks = [] #: Array[Block]
820
+ has_eh_instructions = false
757
821
  # HINT: [symname, index of op, index of else, index of end]
758
822
  branching_stack = [] #: Array[[Symbol, Integer, Integer, Integer]]
759
823
  fixed_stack = [] #: Array[[Symbol, Integer, Integer, Integer]]
760
- while c = buf.read(1)
761
- namespace, code, operand_types = resolve_code(c, buf)
824
+ while opcode = buf.getbyte
825
+ spec = resolve_spec(opcode, buf)
826
+ code = spec.code
827
+ has_eh_instructions ||= eh_instruction?(code)
762
828
  operand = [] #: Array[operandItem]
763
- operand_types.each do |typ|
829
+ spec.operand_types.each do |typ|
764
830
  case typ
765
831
  when :u8
766
- ope = buf.read 1
767
- if ! ope
768
- raise LoadError, "buffer too short"
769
- end
770
- operand << ope.ord
832
+ byte = buf.getbyte
833
+ raise LoadError, "buffer too short" unless byte
834
+ operand << byte
771
835
  when :u32
772
836
  value = fetch_uleb128(buf)
773
- if operand.empty? && c.ord >= 0x28 && c.ord <= 0x3e && value >= 64
837
+ if operand.empty? && opcode >= 0x28 && opcode <= 0x3e && value >= 64
774
838
  raise LoadError, "multi-memory memargs are not supported"
775
839
  end
776
840
  operand << value
@@ -790,11 +854,12 @@ module Wardite
790
854
  operand << FloatImmediate.new(typ, FloatBits.read(typ, data))
791
855
  when :u8_block, :blocktype
792
856
  value = read_blocktype(buf)
857
+ blocks << value
793
858
  operand << value
794
859
  when :catches
795
860
  clauses = [] #: Array[Integer]
796
861
  fetch_uleb128(buf).times do
797
- kind = assert_read(buf, 1).ord
862
+ kind = assert_byte(buf)
798
863
  raise LoadError, "invalid catch kind #{kind}" unless kind <= 3
799
864
  tag = kind <= 1 ? fetch_uleb128(buf) : -1
800
865
  depth = fetch_uleb128(buf)
@@ -808,7 +873,7 @@ module Wardite
808
873
  end
809
874
 
810
875
  # HINT: [namespace, code, operand, else_pos, end_pos]
811
- dest << [namespace, code, operand, nil, nil]
876
+ dest << [spec.namespace, code, operand, nil, nil]
812
877
  if code == :block || code == :loop || code == :if || code == :try_table
813
878
  branching_stack << [code, dest.size - 1, -1, -1]
814
879
  elsif code == :else
@@ -844,7 +909,7 @@ module Wardite
844
909
  raise "[BUG] unknown sym #{sym.inspect}"
845
910
  end
846
911
  end
847
- dest
912
+ DecodedBody.new(dest, blocks, has_eh_instructions)
848
913
  rescue => e
849
914
  require "pp"
850
915
  $stderr.puts "parsed:::"
@@ -853,8 +918,14 @@ module Wardite
853
918
  raise e
854
919
  end
855
920
 
921
+ # @rbs code: Symbol
922
+ # @rbs return: bool
923
+ def self.eh_instruction?(code)
924
+ code == :try_table || code == :throw || code == :throw_ref
925
+ end
926
+
856
927
  # Decode s33, distinguishing the one-byte value/void forms from indices.
857
- # @rbs buf: StringIO
928
+ # @rbs buf: File|StringIO
858
929
  # @rbs return: Block
859
930
  def self.read_blocktype(buf)
860
931
  first = buf.pos
@@ -872,20 +943,26 @@ module Wardite
872
943
  end
873
944
 
874
945
  # @rbs c: String
875
- # @rbs buf: StringIO
946
+ # @rbs buf: File|StringIO
876
947
  # @rbs return: [Symbol, Symbol, Array[Symbol]]
877
948
  def self.resolve_code(c, buf)
878
- ord = c.ord
879
- code = Op::SYMS[ord]
880
- if code == :fc
949
+ spec = resolve_spec(c.ord, buf)
950
+ [spec.namespace, spec.code, spec.operand_types.dup]
951
+ end
952
+
953
+ # @rbs opcode: Integer
954
+ # @rbs buf: File|StringIO
955
+ # @rbs return: Op::DecodeSpec
956
+ def self.resolve_spec(opcode, buf)
957
+ if opcode == 0xfc
881
958
  lower = fetch_uleb128(buf)
882
- sym = Op::FC_SYMS[lower]
883
- namespace = Op::FC_OPERANDS[lower] || :default
884
- return [namespace, sym, Op.operand_of(sym)]
959
+ # Preserve the unresolved opcode for the engine's unsupported-op check.
960
+ Op::FC_SPECS[lower] || Op::DecodeSpec.new(:default, Op::FC_SYMS[lower], [])
961
+ else
962
+ Op::OPCODE_SPECS[opcode] || raise(LoadError, "unsupported code: #{opcode}")
885
963
  end
886
- namespace = Op::NAMESPACES[ord] || raise(LoadError, "unsupported code: #{ord}")
887
- return [namespace, code, Op.operand_of(code)]
888
964
  end
965
+ private_class_method :resolve_spec
889
966
 
890
967
  # @rbs return: DataSection
891
968
  def self.data_section
@@ -948,7 +1025,7 @@ module Wardite
948
1025
  # @rbs return: String
949
1026
  def self.fetch_insn_while_end(sbuf)
950
1027
  first = sbuf.pos
951
- opcode = assert_read(sbuf, 1).ord
1028
+ opcode = assert_byte(sbuf)
952
1029
  case opcode
953
1030
  when 0x41 then fetch_sleb128(sbuf, width: 32)
954
1031
  when 0x42 then fetch_sleb128(sbuf, width: 64)
@@ -956,7 +1033,7 @@ module Wardite
956
1033
  when 0x44 then assert_read(sbuf, 8)
957
1034
  else raise LoadError, "unsupported constant expression"
958
1035
  end
959
- raise LoadError, "constant expression requires end" unless assert_read(sbuf, 1).ord == 0x0b
1036
+ raise LoadError, "constant expression requires end" unless assert_byte(sbuf) == 0x0b
960
1037
  length = sbuf.pos - first
961
1038
  sbuf.pos = first
962
1039
  assert_read(sbuf, length)
@@ -1027,11 +1104,7 @@ module Wardite
1027
1104
  len.times do |i|
1028
1105
  nlen = fetch_uleb128(sbuf)
1029
1106
  name = assert_read(sbuf, nlen)
1030
- kind_ = assert_read(sbuf, 1)
1031
- kind = kind_[0]&.ord
1032
- if !kind
1033
- raise "[BUG] empty unpacked string" # guard rbs
1034
- end
1107
+ kind = assert_byte(sbuf)
1035
1108
 
1036
1109
  raise LoadError, "tag exports are not supported" if kind == 4
1037
1110
  index = fetch_uleb128(sbuf)
@@ -1054,6 +1127,12 @@ module Wardite
1054
1127
  nil
1055
1128
  end
1056
1129
 
1130
+ # @rbs sbuf: File|StringIO
1131
+ # @rbs return: Integer
1132
+ def self.assert_byte(sbuf)
1133
+ sbuf.getbyte || raise(LoadError, "too short section size")
1134
+ end
1135
+
1057
1136
  # @rbs sbuf: File|StringIO
1058
1137
  # @rbs n: Integer
1059
1138
  # @rbs return: String
@@ -2,5 +2,5 @@
2
2
  # rbs_inline: enabled
3
3
 
4
4
  module Wardite
5
- VERSION = "1.1.0" #: String
5
+ VERSION = "1.2.1" #: String
6
6
  end
data/lib/wardite.rb CHANGED
@@ -66,7 +66,7 @@ module Wardite
66
66
  check_module_features(engine)
67
67
  cached_functions = nil #: Array[Efflorescence::Function]?
68
68
  if engine == :efflorescence && mir_cache && !mir_profile
69
- cached_functions = mir_cache.read(self)
69
+ cached_functions = mir_cache.read_functions(self)
70
70
  end
71
71
  types = type_section
72
72
  code_section&.func_codes&.each do |body|
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "rbconfig"
5
+ require "shellwords"
6
+ require "tmpdir"
7
+
8
+ # Spinel's driver selects inputs and linker flags for the host. Keep its C
9
+ # compiler flags and splitter, and substitute the target's link inputs.
10
+ if ARGV.first == "cc"
11
+ ARGV.shift
12
+ target = ENV.fetch("WARDITE_ZIG_TARGET")
13
+ host = ENV.fetch("WARDITE_SPINEL_HOST")
14
+ runtime = ENV.fetch("WARDITE_SPINEL_RUNTIME")
15
+ args = ARGV.map do |arg|
16
+ if arg == "-Wl,-dead_strip" && target.include?("-linux-")
17
+ "-Wl,--gc-sections"
18
+ elsif arg.start_with?("#{host}/") && arg.end_with?(".a", ".o")
19
+ relative = File.expand_path(arg).delete_prefix("#{host}/")
20
+ input = File.join(runtime, relative)
21
+ abort "Spinel target link input is missing: #{input}" unless File.file?(input)
22
+ input
23
+ else
24
+ arg
25
+ end
26
+ end
27
+ # Zig 0.17.1 leaves an empty a.o when Spinel probes the compiler version.
28
+ args += ["-o", File::NULL] if args == ["--version"]
29
+ exec ENV.fetch("ZIG"), "cc", "-target", target, *args
30
+ elsif ARGV.first == "ar"
31
+ # Spinel's Makefile runs `$(AR_DET) ar rcs …`.
32
+ exec ENV.fetch("ZIG"), "ar", *ARGV.drop(2)
33
+ end
34
+
35
+ # Keep this revision in sync with the Spinel job in runtime-tests.yml.
36
+ SPINEL_REF = "58ee6361642befd3770516137c41a6553f0262d1"
37
+
38
+ def run(*command, **options)
39
+ system(*command, **options, exception: true)
40
+ end
41
+
42
+ def unpack(archive, directory)
43
+ return if File.directory?(directory)
44
+
45
+ Dir.mktmpdir("spinel-source-", File.dirname(directory)) do |staging|
46
+ source = File.join(staging, "source")
47
+ Dir.mkdir(source)
48
+ run("tar", "-xzf", archive, "--strip-components", "1", "-C", source)
49
+ File.rename(source, directory)
50
+ end
51
+ end
52
+
53
+ make, build_dir, glibc, macos, jobs, spinel_flags, *targets = ARGV
54
+ abort "spinel-zig requires a macOS build host" unless RUBY_PLATFORM.include?("darwin")
55
+ abort "ZIG_GLIBC and ZIG_MACOS must be version numbers (e.g. 2.28 and 13.0)" unless
56
+ [glibc, macos].all? { |version| version.match?(/\A\d+\.\d+(?:\.\d+)?\z/) }
57
+ abort "ZIG_JOBS must be an integer from 1 to 64" unless jobs.match?(/\A\d+\z/) && (1..64).cover?(jobs.to_i)
58
+
59
+ supported_targets = {
60
+ "aarch64-macos" => "aarch64-macos.#{macos}",
61
+ "x86_64-linux" => "x86_64-linux-gnu.#{glibc}",
62
+ "aarch64-linux" => "aarch64-linux-gnu.#{glibc}"
63
+ }
64
+ if targets.empty? || (targets - supported_targets.keys).any?
65
+ abort "ZIG_TARGETS must select from: #{supported_targets.keys.join(" ")}"
66
+ end
67
+
68
+ root = File.join(File.expand_path(build_dir), "spinel-zig")
69
+ FileUtils.mkdir_p(root)
70
+ root = File.realpath(root)
71
+ ENV["ZIG_GLOBAL_CACHE_DIR"] ||= File.join(root, "zig-global")
72
+ ENV["ZIG_LOCAL_CACHE_DIR"] ||= File.join(root, "zig-local")
73
+ run(ENV.fetch("ZIG"), "version")
74
+
75
+ File.open(File.join(root, "build.lock"), "a") do |lock|
76
+ lock.flock(File::LOCK_EX)
77
+ archive = File.join(root, "spinel-#{SPINEL_REF}.tar.gz")
78
+ unless File.file?(archive)
79
+ Dir.mktmpdir("spinel-download-", root) do |staging|
80
+ download = File.join(staging, "spinel.tar.gz")
81
+ url = "https://codeload.github.com/matz/spinel/tar.gz/#{SPINEL_REF}"
82
+ run("curl", "-fLsS", "--retry", "2", "--max-time", "120", url, "-o", download)
83
+ File.rename(download, archive)
84
+ end
85
+ end
86
+
87
+ host = File.join(root, "host-#{SPINEL_REF}")
88
+ unpack(archive, host)
89
+ spinel = File.join(host, "bin", "spinel")
90
+ inputs = ["lib/libspinel_rt.a", "packages/stringio/sp_stringio.o"]
91
+ submake = [make, "-j#{jobs}", "NO_CCACHE=1"]
92
+ make_env = {"MAKEFLAGS" => nil, "MFLAGS" => nil}
93
+ run(make_env, *submake, "-C", host, "deps")
94
+ run(make_env, *submake, "-C", host, "bin/spinel", *inputs)
95
+
96
+ script = [RbConfig.ruby, File.expand_path(__FILE__)]
97
+ cc = Shellwords.join([*script, "cc"])
98
+ ar = Shellwords.join([*script, "ar"])
99
+ targets.uniq.each do |name|
100
+ target = supported_targets.fetch(name)
101
+ output = File.join(root, target)
102
+ FileUtils.mkdir_p(output)
103
+ runtime = File.join(output, "spinel-#{SPINEL_REF}")
104
+ unpack(archive, runtime)
105
+ env = {
106
+ "WARDITE_ZIG_TARGET" => target,
107
+ "WARDITE_SPINEL_HOST" => host,
108
+ "WARDITE_SPINEL_RUNTIME" => runtime
109
+ }
110
+ puts "Building Wardite for #{target}"
111
+ run(make_env.merge(env), *submake, "-C", runtime, "CC=#{cc}", "AR_DET=#{ar}", *inputs)
112
+ Dir.mktmpdir("wardite-", output) do |staging|
113
+ binary = File.join(staging, "wardite")
114
+ run(env, spinel, "-I", "lib", "--strict-require", *Shellwords.split(spinel_flags),
115
+ "--cc=#{cc}", "--jobs=#{jobs}", "--split-parts=#{[jobs.to_i * 3, 64].min}",
116
+ "exe/wardite", "-o", binary)
117
+ File.rename(binary, File.join(output, "wardite"))
118
+ end
119
+ puts File.join(output, "wardite")
120
+ end
121
+ end
@@ -418,7 +418,22 @@ module Wardite
418
418
 
419
419
  EXTENDED_GLOBAL_SET_TYPES: Hash[Integer, Symbol]
420
420
 
421
- # These are semantic register accesses, excluding frame initialization.
421
+ # Semantic register operand positions, excluding frame initialization.
422
+ READ_POSITIONS: Array[Array[Integer]?]
423
+
424
+ # @rbs instruction: Wardite::Efflorescence::Instruction
425
+ # @rbs return: Integer
426
+ def self.read_count: (Wardite::Efflorescence::Instruction instruction) -> Integer
427
+
428
+ # @rbs instruction: Wardite::Efflorescence::Instruction
429
+ # @rbs ordinal: Integer
430
+ # @rbs return: Integer
431
+ def self.read_position: (Wardite::Efflorescence::Instruction instruction, Integer ordinal) -> Integer
432
+
433
+ # @rbs opcode: Integer
434
+ # @rbs return: Array[Integer]
435
+ def self.fixed_read_positions: (Integer opcode) -> Array[Integer]
436
+
422
437
  # @rbs instruction: Wardite::Efflorescence::Instruction
423
438
  # @rbs return: Array[Integer]
424
439
  def self.read_positions: (Wardite::Efflorescence::Instruction instruction) -> Array[Integer]
@@ -590,7 +605,9 @@ module Wardite
590
605
 
591
606
  attr_reader result_types: Array[Symbol]
592
607
 
593
- attr_reader local_count: Integer
608
+ attr_accessor local_count: Integer
609
+
610
+ attr_accessor cache_pending: bool
594
611
 
595
612
  attr_reader instructions: Array[Wardite::Efflorescence::Instruction]
596
613
 
@@ -701,6 +718,19 @@ module Wardite
701
718
  # @rbs return: void
702
719
  def replace_register_references!: (Array[Integer] mapping) -> void
703
720
 
721
+ # @rbs instruction: Wardite::Efflorescence::Instruction
722
+ # @rbs return: Integer
723
+ def read_count: (Wardite::Efflorescence::Instruction instruction) -> Integer
724
+
725
+ # @rbs instruction: Wardite::Efflorescence::Instruction
726
+ # @rbs ordinal: Integer
727
+ # @rbs return: Integer
728
+ def read_at: (Wardite::Efflorescence::Instruction instruction, Integer ordinal) -> Integer
729
+
730
+ # @rbs instruction: Wardite::Efflorescence::Instruction
731
+ # @rbs return: Array[Integer]?
732
+ def side_reads: (Wardite::Efflorescence::Instruction instruction) -> Array[Integer]?
733
+
704
734
  # @rbs instruction: Wardite::Efflorescence::Instruction
705
735
  # @rbs return: Array[Integer]
706
736
  def reads: (Wardite::Efflorescence::Instruction instruction) -> Array[Integer]
@@ -874,7 +904,8 @@ module Wardite
874
904
 
875
905
  # @rbs function: Wardite::Efflorescence::Function
876
906
  # @rbs reuse: bool
877
- def initialize: (Wardite::Efflorescence::Function function, bool reuse) -> untyped
907
+ # @rbs cache: Wardite::Efflorescence::MIRCache?
908
+ def initialize: (Wardite::Efflorescence::Function function, bool reuse, ?Wardite::Efflorescence::MIRCache? cache) -> untyped
878
909
 
879
910
  # @rbs destination: Integer
880
911
  # @rbs return: Wardite::Efflorescence::Frame