wardite 1.1.0 → 1.2.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.
@@ -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
@@ -590,7 +590,9 @@ module Wardite
590
590
 
591
591
  attr_reader result_types: Array[Symbol]
592
592
 
593
- attr_reader local_count: Integer
593
+ attr_accessor local_count: Integer
594
+
595
+ attr_accessor cache_pending: bool
594
596
 
595
597
  attr_reader instructions: Array[Wardite::Efflorescence::Instruction]
596
598
 
@@ -874,7 +876,8 @@ module Wardite
874
876
 
875
877
  # @rbs function: Wardite::Efflorescence::Function
876
878
  # @rbs reuse: bool
877
- def initialize: (Wardite::Efflorescence::Function function, bool reuse) -> untyped
879
+ # @rbs cache: Wardite::Efflorescence::MIRCache?
880
+ def initialize: (Wardite::Efflorescence::Function function, bool reuse, ?Wardite::Efflorescence::MIRCache? cache) -> untyped
878
881
 
879
882
  # @rbs destination: Integer
880
883
  # @rbs return: Wardite::Efflorescence::Frame
@@ -51,8 +51,12 @@ module Wardite
51
51
  # All records are little-endian. Vectors carry a nonnegative 32-bit element count.
52
52
  # A record count is bounded by the bytes still available to decode it.
53
53
  class Reader
54
+ attr_reader offset: Integer
55
+
54
56
  # @rbs bytes: String
55
- def initialize: (String bytes) -> untyped
57
+ # @rbs offset: Integer
58
+ # @rbs length: Integer?
59
+ def initialize: (String bytes, ?offset: Integer, ?length: Integer?) -> untyped
56
60
 
57
61
  # @rbs length: Integer
58
62
  # @rbs return: String
@@ -70,6 +74,12 @@ module Wardite
70
74
  # @rbs return: Array[Integer]
71
75
  def vector: (String format, Integer width) -> Array[Integer]
72
76
 
77
+ # @rbs target: Array[Integer]
78
+ # @rbs format: String
79
+ # @rbs width: Integer
80
+ # @rbs return: void
81
+ def read_into: (Array[Integer] target, String format, Integer width) -> void
82
+
73
83
  # @rbs return: Array[Symbol]
74
84
  def types: () -> Array[Symbol]
75
85
 
@@ -109,6 +119,8 @@ module Wardite
109
119
  class Decoder
110
120
  include Opcode
111
121
 
122
+ attr_reader functions: Array[Function]
123
+
112
124
  # Reuse the semantic register roles for fixed operands. Lowering
113
125
  # moves direct call arguments into a side table and leaves optional
114
126
  # destinations and return operands to be checked against signatures.
@@ -116,25 +128,32 @@ module Wardite
116
128
 
117
129
  EXTRA_OPERANDS: Array[Symbol?]
118
130
 
119
- # @rbs reader: Reader
131
+ # @rbs payload: String
120
132
  # @rbs instance: Wardite::Instance
121
133
  # @rbs entries: Array[FunctionEntry]
122
- def initialize: (Reader reader, Wardite::Instance instance, Array[FunctionEntry] entries) -> untyped
134
+ def initialize: (String payload, Wardite::Instance instance, Array[FunctionEntry] entries) -> untyped
123
135
 
124
- # @rbs return: Array[Function]
125
- def decode: () -> Array[Function]
136
+ # @rbs layout: String
137
+ # @rbs return: void
138
+ def declare: (String layout) -> void
139
+
140
+ # A failed decode must never expose a partially filled function. Keep
141
+ # the slot unavailable even if a Ruby caller catches the error.
142
+ # @rbs function: Function
143
+ # @rbs return: void
144
+ def restore: (Function function) -> void
126
145
 
127
146
  private
128
147
 
129
148
  # @rbs body: Wardite::CodeSection::CodeBody
130
- # @rbs entry: FunctionEntry
131
- # @rbs index: Integer
132
- # @rbs return: Function
133
- def read_function: (Wardite::CodeSection::CodeBody body, FunctionEntry entry, Integer index) -> Function
149
+ # @rbs function: Function
150
+ # @rbs return: void
151
+ def read_function: (Wardite::CodeSection::CodeBody body, Function function) -> void
134
152
 
135
153
  # @rbs function: Function
136
- # @rbs return: Array[[Integer, Array[Integer]]]
137
- def read_sites: (Function function) -> Array[[ Integer, Array[Integer] ]]
154
+ # @rbs functions: Array[Function]
155
+ # @rbs return: void
156
+ def read_sites: (Function function, Array[Function] functions) -> void
138
157
 
139
158
  # @rbs function: Function
140
159
  # @rbs return: DynamicCallSite
@@ -182,18 +201,26 @@ module Wardite
182
201
  # @rbs wasm: String
183
202
  # @rbs configuration: Configuration
184
203
  # @rbs verify: Symbol
185
- def initialize: (String directory, String wasm, Configuration configuration, ?verify: Symbol) -> untyped
204
+ # @rbs eager: bool
205
+ def initialize: (String directory, String wasm, Configuration configuration, ?verify: Symbol, ?eager: bool) -> untyped
186
206
 
187
207
  # Restore against module declarations so a miss can decode and check the
188
208
  # Wasm bodies before Store initialization or any host lookup.
189
209
  # @rbs instance: Wardite::Instance
190
210
  # @rbs return: Array[Function]?
191
- def read: (Wardite::Instance instance) -> Array[Function]?
211
+ def read_functions: (Wardite::Instance instance) -> Array[Function]?
212
+
213
+ # @rbs function: Function
214
+ # @rbs return: void
215
+ def restore_function: (Function function) -> void
216
+
217
+ # @rbs return: void
218
+ def restore_all: () -> void
192
219
 
193
220
  # @rbs instance: Wardite::Instance
194
221
  # @rbs ir: IR
195
222
  # @rbs return: void
196
- def write: (Wardite::Instance instance, IR ir) -> void
223
+ def write_ir: (Wardite::Instance instance, IR ir) -> void
197
224
 
198
225
  private
199
226
 
@@ -71,8 +71,9 @@ module Wardite
71
71
  # @rbs functions: Array[Wardite::Efflorescence::Function]
72
72
  # @rbs frames: Array[Wardite::Efflorescence::SavedFrame]
73
73
  # @rbs index: Integer
74
+ # @rbs cache: Wardite::Efflorescence::MIRCache
74
75
  # @rbs return: void
75
- def check_frame: (Array[Wardite::Efflorescence::Function] functions, Array[Wardite::Efflorescence::SavedFrame] frames, Integer index) -> void
76
+ def check_frame: (Array[Wardite::Efflorescence::Function] functions, Array[Wardite::Efflorescence::SavedFrame] frames, Integer index, Wardite::Efflorescence::MIRCache cache) -> void
76
77
 
77
78
  # @rbs message: String
78
79
  # @rbs started: Float
@@ -212,8 +212,9 @@ module Wardite
212
212
  # @rbs mir_profile: bool
213
213
  # @rbs mir_cache: String?
214
214
  # @rbs mir_cache_verify: Symbol
215
+ # @rbs mir_cache_eager: bool
215
216
  # @rbs return: Instance
216
- def self.load_from_buffer: (File | StringIO buf, ?import_object: Hash[Symbol, wasmModuleSrc], ?enable_wasi: boolish, ?engine: Symbol, ?disabled_optimizations: Array[Symbol], ?mir_profile: bool, ?mir_cache: String?, ?mir_cache_verify: Symbol) -> Instance
217
+ def self.load_from_buffer: (File | StringIO buf, ?import_object: Hash[Symbol, wasmModuleSrc], ?enable_wasi: boolish, ?engine: Symbol, ?disabled_optimizations: Array[Symbol], ?mir_profile: bool, ?mir_cache: String?, ?mir_cache_verify: Symbol, ?mir_cache_eager: bool) -> Instance
217
218
 
218
219
  # @rbs return: Integer
219
220
  def self.preamble: () -> Integer
data/sig/io_buffer.rbs ADDED
@@ -0,0 +1,7 @@
1
+ class IO
2
+ class Buffer
3
+ # The core signature currently omits the block form documented by Ruby.
4
+ def self.for: [Result] (String) { (Buffer) -> Result } -> Result
5
+ | ...
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wardite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio Kondo
@@ -31,6 +31,7 @@ files:
31
31
  - benchmark/compare_dispatch.rb
32
32
  - benchmark/compare_engines.rb
33
33
  - benchmark/compare_optimizations.rb
34
+ - benchmark/count_ruby_wasm_functions.rb
34
35
  - benchmark/engine_phases.rb
35
36
  - benchmark/figures/README.md
36
37
  - benchmark/figures/plot_spinel_comparison.py
@@ -52,6 +53,7 @@ files:
52
53
  - benchmark/measure_m3_calls.rb
53
54
  - benchmark/measure_m4.rb
54
55
  - benchmark/measure_m5_boundary.rb
56
+ - benchmark/measure_mir_cache_restore.rb
55
57
  - benchmark/measure_mir_cache_verify.rb
56
58
  - benchmark/measure_ruby_wasm_cache_hit.rb
57
59
  - benchmark/measure_ruby_wasm_startup.rb
@@ -73,6 +75,7 @@ files:
73
75
  - benchmark/results/efflorescence-m2-i32-spinel.json
74
76
  - benchmark/results/efflorescence-m2-memory.json
75
77
  - benchmark/results/efflorescence-mir2-cache-integrity.json
78
+ - benchmark/results/efflorescence-mir2-function-restore.json
76
79
  - benchmark/results/efflorescence-unboxed-globals.json
77
80
  - benchmark/results/generated_guards_exploratory_2026-11-04.json
78
81
  - benchmark/results/generated_guards_profile_2026-11-04.json
@@ -204,6 +207,7 @@ files:
204
207
  - misc/slides/tokyo12.md
205
208
  - rbs_collection.lock.yaml
206
209
  - rbs_collection.yaml
210
+ - scripts/build_spinel_zig.rb
207
211
  - scripts/gen_alu.rb
208
212
  - scripts/gen_conv.rb
209
213
  - scripts/templates/alu_module.rb.tmpl
@@ -245,13 +249,12 @@ files:
245
249
  - sig/generated/wardite/wasi/preopens.rbs
246
250
  - sig/generated/wardite/wasi/spinel.rbs
247
251
  - sig/generated/wardite/wasm_module.rbs
252
+ - sig/io_buffer.rbs
248
253
  - sig/mir_cache_stdlib.rbs
249
254
  - sig/vernier.rbs
250
255
  - sig/wardite.rbs
251
256
  - spinel/README.md
252
257
  - spinel/bin/wardite.rb
253
- - spinel/patches/0001-io-buffer-string-view.patch
254
- - spinel/patches/0002-io-buffer-allocation-error.patch
255
258
  - spinel/spin.toml
256
259
  homepage: https://github.com/udzura/wardite
257
260
  licenses: