wardite 1.0.0 → 1.1.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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -7
  3. data/Steepfile +1 -0
  4. data/assets/wardite.svg +12 -0
  5. data/benchmark/measure_mir_cache_verify.rb +58 -0
  6. data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
  7. data/benchmark/measure_ruby_wasm_startup.rb +43 -0
  8. data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
  9. data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
  10. data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
  11. data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
  12. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
  13. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
  14. data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
  15. data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
  16. data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
  17. data/docs/efflorescence-generated-c-guards.md +5 -0
  18. data/docs/efflorescence-mir2-cache.md +123 -0
  19. data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
  20. data/docs/efflorescence-spinel-linear-memory.md +72 -0
  21. data/docs/efflorescence-startup-snapshot.md +60 -0
  22. data/docs/efflorescence-unboxed-globals.md +61 -0
  23. data/lib/wardite/alu_f32.generated.rb +10 -2
  24. data/lib/wardite/alu_f64.generated.rb +10 -2
  25. data/lib/wardite/alu_i32.generated.rb +40 -8
  26. data/lib/wardite/alu_i64.generated.rb +55 -11
  27. data/lib/wardite/cli/cli.rb +56 -6
  28. data/lib/wardite/efflorescence/ir.rb +14 -1
  29. data/lib/wardite/efflorescence/mir_cache.rb +657 -0
  30. data/lib/wardite/efflorescence/register_validation.rb +128 -56
  31. data/lib/wardite/efflorescence/snapshot.rb +229 -0
  32. data/lib/wardite/efflorescence/store_access.rb +103 -8
  33. data/lib/wardite/efflorescence/translator.rb +10 -3
  34. data/lib/wardite/efflorescence/vm.rb +202 -38
  35. data/lib/wardite/efflorescence/wasi.rb +0 -56
  36. data/lib/wardite/efflorescence.rb +6 -0
  37. data/lib/wardite/load.rb +108 -24
  38. data/lib/wardite/version.rb +1 -1
  39. data/lib/wardite/wasi/spinel.rb +41 -0
  40. data/lib/wardite/wasi.rb +483 -268
  41. data/lib/wardite.rb +218 -70
  42. data/scripts/gen_alu.rb +55 -11
  43. data/scripts/verify_ruby_wasm.rb +70 -0
  44. data/sig/generated/wardite/cli/cli.rbs +15 -0
  45. data/sig/generated/wardite/efflorescence/ir.rbs +7 -0
  46. data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
  47. data/sig/generated/wardite/efflorescence/register_validation.rbs +31 -3
  48. data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
  49. data/sig/generated/wardite/efflorescence/store_access.rbs +13 -2
  50. data/sig/generated/wardite/efflorescence/translator.rbs +5 -1
  51. data/sig/generated/wardite/efflorescence/vm.rbs +68 -7
  52. data/sig/generated/wardite/efflorescence/wasi.rbs +0 -13
  53. data/sig/generated/wardite/load.rbs +32 -5
  54. data/sig/generated/wardite/wasi/spinel.rbs +15 -0
  55. data/sig/generated/wardite/wasi.rbs +111 -4
  56. data/sig/generated/wardite.rbs +67 -3
  57. data/sig/mir_cache_stdlib.rbs +5 -0
  58. data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
  59. data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
  60. metadata +29 -1
@@ -7,41 +7,55 @@ module Wardite
7
7
  # This makes stale temporary slots harmless when a frame is reused. Keep
8
8
  # states per basic block, rather than per instruction, to bound their size.
9
9
  class RegisterValidation
10
+ # Keep each word within Spinel's fixed-width Integer.
11
+ WORD_SHIFT = 5
12
+ WORD_BITS = 1 << WORD_SHIFT
13
+ WORD_MASK = WORD_BITS - 1
14
+
10
15
  class Block
11
16
  attr_reader :first #: Integer
12
17
  attr_reader :limit #: Integer
13
18
  attr_reader :defined #: Array[Integer]
19
+ attr_reader :successors #: Array[Integer]
20
+ attr_accessor :pending #: bool
14
21
 
15
22
  # @rbs first: Integer
16
23
  # @rbs limit: Integer
17
- # @rbs register_count: Integer
18
- def initialize(first, limit, register_count)
24
+ def initialize(first, limit)
19
25
  @first = first
20
26
  @limit = limit
21
- @defined = Array.new(register_count, 0)
27
+ @defined = [] #: Array[Integer]
28
+ @successors = [] #: Array[Integer]
22
29
  @reached = false
30
+ @pending = false
23
31
  end
24
32
 
25
33
  # Incoming facts only lose definitions after the first reachable edge.
26
- # A backedge cannot establish a definition missing on initial entry.
34
+ # Return true when a changed block needs to enter the worklist.
27
35
  # @rbs incoming: Array[Integer]
28
36
  # @rbs return: bool
29
37
  def merge(incoming)
38
+ changed = false
30
39
  unless @reached
31
40
  @defined = incoming.dup
32
41
  @reached = true
33
- return true
34
- end
35
- changed = false
36
- index = 0
37
- while index < @defined.size
38
- if @defined[index] == 1 && incoming[index] == 0
39
- @defined[index] = 0
40
- changed = true
42
+ changed = true
43
+ else
44
+ index = 0
45
+ while index < @defined.size
46
+ previous = @defined.fetch(index)
47
+ intersection = previous & incoming.fetch(index)
48
+ if intersection != previous
49
+ @defined[index] = intersection
50
+ changed = true
51
+ end
52
+ index += 1
41
53
  end
42
- index += 1
43
54
  end
44
- changed
55
+ return false unless changed && !@pending
56
+
57
+ @pending = true
58
+ true
45
59
  end
46
60
  end
47
61
 
@@ -54,69 +68,127 @@ module Wardite
54
68
  raise TranslationError, "Efflorescence: empty MIR function" if instructions.empty?
55
69
  validate_types(function, ir, tags)
56
70
  analysis = Analysis.new(function)
71
+ flow = analysis.flow
72
+ register_count = function.register_count
57
73
  blocks = [] #: Array[Wardite::Efflorescence::RegisterValidation::Block]
58
74
  block_for_pc = [] #: Array[Integer]
59
75
  first = 0
60
76
  instructions.size.times do |pc|
61
77
  if pc > 0 && analysis.leaders[pc] == 1
62
- blocks << Block.new(first, pc, function.register_count)
78
+ blocks << Block.new(first, pc)
63
79
  first = pc
64
80
  end
65
81
  block_for_pc << blocks.size
66
82
  end
67
- blocks << Block.new(first, instructions.size, function.register_count)
83
+ blocks << Block.new(first, instructions.size)
84
+ order = reverse_postorder(blocks, block_for_pc, flow)
68
85
 
69
- constants = Array.new(function.register_count, 0)
70
- defined = Array.new(function.register_count, 0)
71
- index = 0
72
- while index < function.parameter_count + function.local_count
73
- defined[index] = 1
74
- index += 1
75
- end
86
+ word_count = (register_count + WORD_MASK) >> WORD_SHIFT
87
+ constants = Array.new(word_count, 0)
88
+ defined = Array.new(word_count, 0)
89
+ (function.parameter_count + function.local_count).times { |register| mark(defined, register) }
76
90
  function.constant_registers.each do |register|
77
- constants[register] = 1
78
- defined[register] = 1
91
+ mark(constants, register)
92
+ mark(defined, register)
79
93
  end
80
94
  blocks.fetch(0).merge(defined)
81
- pending = [] #: Array[Integer]
82
- pending << 0
83
- until pending.empty?
84
- block_index = pending.pop || raise(TranslationError, "missing MIR block")
85
- block = blocks.fetch(block_index)
86
- defined = block.defined.dup
87
- pc = block.first
88
- while pc < block.limit
89
- instruction = instructions.fetch(pc)
90
- function.reads(instruction).each do |register|
91
- unless register >= 0 && register < defined.size && defined[register] == 1
92
- raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: r#{register} read before definition"
95
+ pending = 1
96
+ while pending > 0
97
+ order.each do |block_index|
98
+ break if pending == 0
99
+ block = blocks.fetch(block_index)
100
+ next unless block.pending
101
+
102
+ block.pending = false
103
+ pending -= 1
104
+ defined = block.defined.dup
105
+ pc = block.first
106
+ while pc < block.limit
107
+ instruction = instructions.fetch(pc)
108
+ function.reads(instruction).each do |register|
109
+ unless register >= 0 && register < register_count && marked?(defined, register)
110
+ raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: r#{register} read before definition"
111
+ end
93
112
  end
94
- end
95
- # Exceptional writes are edge-local. In particular a CALL result
96
- # is absent here, even though its normal successor defines it.
97
- analysis.flow.exceptional.fetch(pc).each do |clause|
98
- edge_defined = defined.dup
99
- clause.destinations.each { |register| edge_defined[register] = 1 }
100
- target = block_for_pc.fetch(clause.target_pc)
101
- pending << target if blocks.fetch(target).merge(edge_defined)
102
- end
103
- destination = Opcode.destination(instruction)
104
- if destination >= 0
105
- if destination >= defined.size || constants[destination] == 1
106
- raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: invalid register destination r#{destination}"
113
+ # A CALL result exists only on its normal successor.
114
+ flow.exceptional.fetch(pc).each do |clause|
115
+ edge_defined = defined.dup
116
+ clause.destinations.each { |register| mark(edge_defined, register) }
117
+ target = block_for_pc.fetch(clause.target_pc)
118
+ pending += 1 if blocks.fetch(target).merge(edge_defined)
119
+ end
120
+ destination = Opcode.destination(instruction)
121
+ if destination >= 0
122
+ if destination >= register_count || marked?(constants, destination)
123
+ raise TranslationError, "Efflorescence: function #{function.index}, MIR PC #{pc}: invalid register destination r#{destination}"
124
+ end
125
+ mark(defined, destination)
107
126
  end
108
- defined[destination] = 1
127
+ pc += 1
128
+ end
129
+ flow.normal.fetch(block.limit - 1).each do |target_pc|
130
+ target = block_for_pc.fetch(target_pc)
131
+ pending += 1 if blocks.fetch(target).merge(defined)
109
132
  end
110
- pc += 1
111
- end
112
- analysis.flow.normal.fetch(block.limit - 1).each do |pc|
113
- target = block_for_pc.fetch(pc)
114
- pending << target if blocks.fetch(target).merge(defined)
115
133
  end
116
134
  end
117
135
  end
118
136
  private
119
137
 
138
+ # Visit predecessors before joins where possible, then revisit changed
139
+ # states across backedges until the worklist is empty.
140
+ # @rbs blocks: Array[Wardite::Efflorescence::RegisterValidation::Block]
141
+ # @rbs block_for_pc: Array[Integer]
142
+ # @rbs flow: Wardite::Efflorescence::FlowGraph
143
+ # @rbs return: Array[Integer]
144
+ def reverse_postorder(blocks, block_for_pc, flow)
145
+ blocks.each do |block|
146
+ flow.normal.fetch(block.limit - 1).each { |pc| block.successors << block_for_pc.fetch(pc) }
147
+ (block.first...block.limit).each do |pc|
148
+ flow.exceptional.fetch(pc).each { |clause| block.successors << block_for_pc.fetch(clause.target_pc) }
149
+ end
150
+ end
151
+
152
+ seen = Array.new(blocks.size, false)
153
+ next_edge = Array.new(blocks.size, 0)
154
+ seen[0] = true
155
+ stack = [0]
156
+ postorder = [] #: Array[Integer]
157
+ until stack.empty?
158
+ index = stack.fetch(stack.size - 1)
159
+ successors = blocks.fetch(index).successors
160
+ edge = next_edge.fetch(index)
161
+ if edge == successors.size
162
+ postorder << index
163
+ stack.pop
164
+ else
165
+ target = successors.fetch(edge)
166
+ next_edge[index] = edge + 1
167
+ unless seen.fetch(target)
168
+ seen[target] = true
169
+ stack << target
170
+ end
171
+ end
172
+ end
173
+ postorder.reverse
174
+ end
175
+
176
+ # @rbs words: Array[Integer]
177
+ # @rbs register: Integer
178
+ # @rbs return: void
179
+ def mark(words, register)
180
+ index = register >> WORD_SHIFT
181
+ words[index] = words.fetch(index) | (1 << (register & WORD_MASK))
182
+ end
183
+
184
+ # @rbs words: Array[Integer]
185
+ # @rbs register: Integer
186
+ # @rbs return: bool
187
+ def marked?(words, register)
188
+ word = words.fetch(register >> WORD_SHIFT)
189
+ (word & (1 << (register & WORD_MASK))) != 0
190
+ end
191
+
120
192
  # Validate all registers, including unreachable instructions and table-only
121
193
  # operands. Numeric registers are checked before flat execution.
122
194
  # @rbs function: Wardite::Efflorescence::Function
@@ -0,0 +1,229 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "digest"
5
+ require "fileutils"
6
+ require "wardite/efflorescence/mir_cache" unless RUBY_ENGINE == "spinel"
7
+
8
+ module Wardite
9
+ module Efflorescence
10
+ class SavedFrame
11
+ attr_reader :pool_index #: Integer
12
+ attr_reader :pc #: Integer
13
+ attr_reader :return_register #: Integer
14
+ attr_reader :registers #: Array[Integer]
15
+ attr_reader :handlers #: Array[Integer]
16
+
17
+ # @rbs pool_index: Integer
18
+ # @rbs pc: Integer
19
+ # @rbs return_register: Integer
20
+ # @rbs registers: Array[Integer]
21
+ # @rbs handlers: Array[Integer]
22
+ def initialize(pool_index, pc, return_register, registers, handlers)
23
+ @pool_index = pool_index
24
+ @pc = pc
25
+ @return_register = return_register
26
+ @registers = registers
27
+ @handlers = handlers
28
+ end
29
+ end
30
+
31
+ # Startup snapshot of a WASI command (ADR 0012). The state is saved just
32
+ # before the first WASI call whose result is not fixed by the key, and a
33
+ # later run with the same key resumes from there.
34
+ class Snapshot
35
+ MAGIC = "WARDSNP\0".b
36
+ FORMAT_VERSION = 1
37
+ PAGE_SIZE = 65536
38
+ ZERO_PAGE = ("\0" * PAGE_SIZE).b
39
+ # Their results depend only on values in the key, or are frozen into the
40
+ # snapshot (clocks and random bytes). fd_filestat_get is allowed for
41
+ # stdio only: CRuby probes fds 0-2 before anything else.
42
+ ALLOWED = %i[args_sizes_get args_get environ_sizes_get environ_get fd_prestat_get
43
+ fd_prestat_dir_name fd_fdstat_get clock_res_get clock_time_get random_get].freeze
44
+
45
+ attr_reader :path #: String
46
+
47
+ # @rbs directory: String
48
+ # @rbs instance: Wardite::Instance
49
+ # @rbs mir_cache: Wardite::Efflorescence::MIRCache
50
+ def initialize(directory, instance, mir_cache)
51
+ wasi = instance.wasi || raise(Wardite::ArgumentError, "snapshot requires WASI")
52
+ @wasi = wasi
53
+ @fds = wasi.fd_table.keys.sort
54
+ @fd_count = wasi.fd_count
55
+ pages = instance.store.memories.map { |memory| memory.current }
56
+ key_data = [FORMAT_VERSION, pages.size, *pages].pack("L<*") + mir_cache.key + wasi.snapshot_identity
57
+ key = Digest::SHA256.digest(key_data)
58
+ @header = MAGIC + [FORMAT_VERSION].pack("L<") + key
59
+ @directory = File.expand_path(directory)
60
+ @path = File.join(@directory, "#{key.unpack1("H*")}.snap")
61
+ end
62
+
63
+ # Does this host call end the deterministic prefix of the run?
64
+ # @rbs host: Wardite::Efflorescence::HostFunction
65
+ # @rbs registers: Array[Integer]
66
+ # @rbs sources: Array[Integer]
67
+ # @rbs return: bool
68
+ def checkpoint?(host, registers, sources)
69
+ external = host.external
70
+ return true unless @wasi.equal?(external.target_module)
71
+ name = external.name
72
+ return registers[sources.fetch(0)] > 2 if name == :fd_filestat_get
73
+ !ALLOWED.include?(name)
74
+ end
75
+
76
+ # Saving is best effort: any refusal or I/O error leaves the run as is.
77
+ # @rbs instance: Wardite::Instance
78
+ # @rbs frames: Array[Wardite::Efflorescence::Frame]
79
+ # @rbs return: void
80
+ def save(instance, frames)
81
+ started = Time.now.to_f
82
+ return trace("skipped (open descriptors changed)") unless @wasi.fd_table.keys.sort == @fds && @wasi.fd_count == @fd_count
83
+ store = instance.store
84
+ return trace("skipped (multiple memories)") if store.memories.size > 1
85
+ writer = MIRCache::Writer.new
86
+ writer.integers(@fd_count)
87
+ access = StoreAccess.new(store)
88
+ globals = [] #: Array[Integer]
89
+ store.globals.each_with_index do |global, index|
90
+ globals << case global.type
91
+ when :i64 then access.global_get_i64(index)
92
+ when :f32, :f64 then access.global_get_float(index, global.type)
93
+ else access.global_get(index)
94
+ end
95
+ end
96
+ writer.vector(globals, "q<")
97
+ writer.integers(frames.size)
98
+ frames.each do |frame|
99
+ return trace("skipped (pending exception reference)") unless frame.exception_registers.compact.empty?
100
+ writer.integers(frame.pool_index, frame.pc, frame.return_register)
101
+ writer.vector(frame.registers, "q<")
102
+ writer.vector(frame.handlers)
103
+ end
104
+ memory = store.memories[0]
105
+ pages = memory ? memory.current : 0
106
+ used = [] #: Array[Integer]
107
+ if memory
108
+ pages.times { |page| used << page unless memory.read_bytes(page * PAGE_SIZE, PAGE_SIZE) == ZERO_PAGE }
109
+ end
110
+ writer.integers(pages)
111
+ writer.vector(used)
112
+ payload = writer.bytes
113
+ if memory
114
+ return trace("skipped (too large)") if payload.bytesize + used.size * PAGE_SIZE > MIRCache::MAX_BYTES
115
+ used.each { |page| payload << memory.read_bytes(page * PAGE_SIZE, PAGE_SIZE) }
116
+ end
117
+ FileUtils.mkdir_p(@directory)
118
+ MIRCache.publish(@directory, @path, @header + Digest::SHA256.digest(payload), payload)
119
+ trace("saved", started)
120
+ rescue SystemCallError, IOError
121
+ trace("skipped (write failed)")
122
+ end
123
+
124
+ # Validate everything before touching the Store, so a rejected file
125
+ # leaves a fresh instance for the normal start.
126
+ # @rbs vm: Wardite::Efflorescence::VM
127
+ # @rbs return: Array[Wardite::Efflorescence::SavedFrame]?
128
+ def restore(vm)
129
+ started = Time.now.to_f
130
+ instance = vm.instance
131
+ store = instance.store
132
+ header_size = @header.bytesize
133
+ prefix_size = header_size + MIRCache::DIGEST_SIZE
134
+ contents = MIRCache.read_contents(@path, @header, prefix_size)
135
+ payload = contents.byteslice(prefix_size..) || raise(MIRCache::Invalid)
136
+ raise MIRCache::Invalid unless contents.byteslice(header_size, MIRCache::DIGEST_SIZE) == Digest::SHA256.digest(payload)
137
+ reader = MIRCache::Reader.new(payload)
138
+ raise MIRCache::Invalid unless reader.integer == @wasi.fd_count && store.memories.size <= 1
139
+ globals = reader.vector("q<", 8)
140
+ raise MIRCache::Invalid unless globals.size == store.globals.size
141
+ functions = vm.ir.functions
142
+ frames = [] #: Array[SavedFrame]
143
+ reader.count(12).times do
144
+ pool_index = reader.integer
145
+ pc = reader.integer
146
+ return_register = reader.integer
147
+ registers = reader.vector("q<", 8)
148
+ handlers = reader.vector("l<", 4)
149
+ frames << SavedFrame.new(pool_index, pc, return_register, registers, handlers)
150
+ end
151
+ raise MIRCache::Invalid if frames.empty?
152
+ frames.each_with_index { |frame, index| check_frame(functions, frames, index) }
153
+ memory = store.memories[0]
154
+ pages = reader.integer
155
+ used = reader.vector("l<", 4)
156
+ current = memory ? memory.current : 0
157
+ raise MIRCache::Invalid unless (memory || pages == 0) && pages >= current
158
+ raise MIRCache::Invalid unless used.all? { |page| page >= 0 && page < pages }
159
+ contents = reader.bytes(used.size * PAGE_SIZE)
160
+ reader.finish
161
+
162
+ if memory
163
+ raise MIRCache::Invalid if pages > current && memory.grow(pages - current) < 0
164
+ used_pages = Array.new(pages, false)
165
+ used.each_with_index do |page, i|
166
+ used_pages[page] = true
167
+ memory.write_bytes(page * PAGE_SIZE, contents.byteslice(i * PAGE_SIZE, PAGE_SIZE) || raise(MIRCache::Invalid))
168
+ end
169
+ used_pages.each_with_index { |nonzero, page| memory.write_bytes(page * PAGE_SIZE, ZERO_PAGE) unless nonzero }
170
+ end
171
+ access = StoreAccess.new(store)
172
+ store.globals.each_with_index do |global, index|
173
+ value = globals.fetch(index)
174
+ case global.type
175
+ when :i64 then access.global_set_i64(index, value)
176
+ when :f32, :f64 then access.global_set_float(index, global.type, value)
177
+ else access.global_set(index, value)
178
+ end
179
+ end
180
+ trace("restored", started)
181
+ frames
182
+ rescue SystemCallError, IOError, MIRCache::Invalid
183
+ trace("miss")
184
+ nil
185
+ end
186
+
187
+ private
188
+
189
+ # The top frame resumes at its host call. Callers resume after the call
190
+ # that created the frame above them, which writes its return register.
191
+ # @rbs functions: Array[Wardite::Efflorescence::Function]
192
+ # @rbs frames: Array[Wardite::Efflorescence::SavedFrame]
193
+ # @rbs index: Integer
194
+ # @rbs return: void
195
+ def check_frame(functions, frames, index)
196
+ frame = frames.fetch(index)
197
+ function = functions[frame.pool_index] || raise(MIRCache::Invalid)
198
+ code = function.bytecode
199
+ pc = frame.pc
200
+ raise MIRCache::Invalid unless pc >= 0 && pc < code.size && pc % Lowerer::WIDTH == 0
201
+ raise MIRCache::Invalid unless frame.registers.size == function.initial_registers.size
202
+ raise MIRCache::Invalid unless frame.handlers.all? { |id| id >= 0 && id < function.flat_handlers.size }
203
+ if index == frames.size - 1
204
+ opcode = code.fetch(pc)
205
+ raise MIRCache::Invalid unless opcode == Opcode::CALL_HOST || opcode == Opcode::CALL_INDIRECT
206
+ else
207
+ raise MIRCache::Invalid unless pc >= Lowerer::WIDTH
208
+ opcode = code.fetch(pc - Lowerer::WIDTH)
209
+ raise MIRCache::Invalid unless opcode == Opcode::CALL || opcode == Opcode::CALL_INDIRECT
210
+ end
211
+ destination = frame.return_register
212
+ if index == 0
213
+ raise MIRCache::Invalid unless destination == -1
214
+ else
215
+ raise MIRCache::Invalid unless destination >= -1 && destination < frames.fetch(index - 1).registers.size
216
+ end
217
+ end
218
+
219
+ # @rbs message: String
220
+ # @rbs started: Float
221
+ # @rbs return: void
222
+ def trace(message, started = 0.0)
223
+ return unless ENV["WARDITE_TRACE"]
224
+ elapsed = started > 0 ? " (#{(Time.now.to_f - started).round(3)}s)" : ""
225
+ $stderr.puts "snapshot #{message}: #{@path}#{elapsed}"
226
+ end
227
+ end
228
+ end
229
+ end
@@ -3,8 +3,8 @@
3
3
 
4
4
  module Wardite
5
5
  module Efflorescence
6
- # Checked byte operations live off the integer dispatch path. Store remains
7
- # the owner of all state; never retain a Memory#data buffer across a call.
6
+ # Checked byte operations and global synchronization live off the integer
7
+ # dispatch path. Never retain a Memory#data buffer across a call.
8
8
  class StoreAccess
9
9
  PAGE_SIZE = 65536
10
10
  MAX_PAGES = 65536
@@ -84,16 +84,51 @@ module Wardite
84
84
  global_at(index).value = Wardite::I64(value)
85
85
  end
86
86
 
87
+ # @rbs index: Integer
88
+ # @rbs type: Symbol
89
+ # @rbs return: Integer
90
+ def global_bits_at(index, type)
91
+ case type
92
+ when :i32 then global_get(index)
93
+ when :i64 then global_get_i64(index)
94
+ else global_get_float(index, type)
95
+ end
96
+ end
97
+
98
+ # @rbs index: Integer
99
+ # @rbs type: Symbol
100
+ # @rbs value: Integer
101
+ # @rbs return: void
102
+ def write_global_bits(index, type, value)
103
+ return if value == global_bits_at(index, type)
104
+
105
+ case type
106
+ when :i32 then global_set(index, value)
107
+ when :i64 then global_set_i64(index, value)
108
+ else global_set_float(index, type, value)
109
+ end
110
+ end
111
+
87
112
  # @rbs address: Integer
88
113
  # @rbs offset: Integer
89
114
  # @rbs width: Integer
90
115
  # @rbs signed: bool
91
116
  # @rbs return: Integer
92
117
  def load(address, offset, width, signed)
93
- data = memory.data
94
118
  start = address + offset
95
- StoreAccess.check_range(start, width, data.bytesize)
96
- value = read_word(data, start, width)
119
+ if RUBY_ENGINE == "spinel"
120
+ source = memory.buffer
121
+ StoreAccess.check_range(start, width, source.size)
122
+ value = case width
123
+ when 1 then Integer(source.get_value(:U8, start))
124
+ when 2 then Integer(source.get_value(:u16, start))
125
+ else Integer(source.get_value(:u32, start))
126
+ end
127
+ else
128
+ data = memory.data
129
+ StoreAccess.check_range(start, width, data.bytesize)
130
+ value = read_word(data, start, width)
131
+ end
97
132
  if signed && value >= (1 << (width * 8 - 1))
98
133
  value -= 1 << (width * 8)
99
134
  end
@@ -106,8 +141,20 @@ module Wardite
106
141
  # @rbs signed: bool
107
142
  # @rbs return: Integer
108
143
  def load_i64(address, offset, width, signed)
109
- data = memory.data
110
144
  start = address + offset
145
+ if RUBY_ENGINE == "spinel"
146
+ source = memory.buffer
147
+ StoreAccess.check_range(start, width, source.size)
148
+ return Integer(source.get_value(:s64, start)) if width == 8
149
+
150
+ value = case width
151
+ when 1 then Integer(source.get_value(:U8, start))
152
+ when 2 then Integer(source.get_value(:u16, start))
153
+ else Integer(source.get_value(:u32, start))
154
+ end
155
+ return signed ? I64Arithmetic.extend(value, width * 8) : value
156
+ end
157
+ data = memory.data
111
158
  StoreAccess.check_range(start, width, data.bytesize)
112
159
  if width == 8
113
160
  I64Arithmetic.join(read_word(data, start, 4), read_word(data, start + 4, 4))
@@ -125,8 +172,19 @@ module Wardite
125
172
  # @rbs value: Integer
126
173
  # @rbs return: void
127
174
  def store(address, offset, width, value)
128
- data = memory.data
129
175
  start = address + offset
176
+ if RUBY_ENGINE == "spinel"
177
+ target = memory.buffer
178
+ StoreAccess.check_range(start, width, target.size)
179
+ case width
180
+ when 1 then target.set_value(:U8, start, value & 255)
181
+ when 2 then target.set_value(:u16, start, value & 0xffff)
182
+ when 4 then target.set_value(:u32, start, value & 0xffffffff)
183
+ when 8 then target.set_value(:s64, start, value)
184
+ end
185
+ return
186
+ end
187
+ data = memory.data
130
188
  StoreAccess.check_range(start, width, data.bytesize)
131
189
  byte = 0
132
190
  while byte < width
@@ -147,6 +205,8 @@ module Wardite
147
205
  # @rbs return: Integer
148
206
  def grow(delta)
149
207
  target = memory
208
+ return target.grow(delta) & 0xffffffff if RUBY_ENGINE == "spinel"
209
+
150
210
  previous = target.current
151
211
  declared = target.max
152
212
  limit = declared && declared < MAX_PAGES ? declared : MAX_PAGES
@@ -194,6 +254,13 @@ module Wardite
194
254
  # @rbs length: Integer
195
255
  # @rbs return: void
196
256
  def copy(destination, source, length)
257
+ if RUBY_ENGINE == "spinel"
258
+ target = memory.buffer
259
+ StoreAccess.check_range(destination, length, target.size)
260
+ StoreAccess.check_range(source, length, target.size)
261
+ target.copy(target, destination, length, source) if length > 0
262
+ return
263
+ end
197
264
  data = memory.data
198
265
  StoreAccess.check_range(destination, length, data.bytesize)
199
266
  StoreAccess.check_range(source, length, data.bytesize)
@@ -219,9 +286,30 @@ module Wardite
219
286
  # @rbs length: Integer
220
287
  # @rbs return: void
221
288
  def fill(destination, value, length)
289
+ if RUBY_ENGINE == "spinel"
290
+ target = memory.buffer
291
+ StoreAccess.check_range(destination, length, target.size)
292
+ target.clear(value & 255, destination, length) if length > 0
293
+ return
294
+ end
222
295
  data = memory.data
223
296
  StoreAccess.check_range(destination, length, data.bytesize)
297
+ return if length == 0
298
+
224
299
  byte = value & 255
300
+ if data.encoding == Encoding::BINARY
301
+ chunk_size = length < PAGE_SIZE ? length : PAGE_SIZE
302
+ byte_string = byte.chr(Encoding::BINARY)
303
+ chunk = byte_string * chunk_size
304
+ while length >= chunk_size
305
+ data[destination, chunk_size] = chunk
306
+ destination += chunk_size
307
+ length -= chunk_size
308
+ end
309
+ data[destination, length] = byte_string * length if length > 0
310
+ return
311
+ end
312
+
225
313
  index = 0
226
314
  while index < length
227
315
  data.setbyte(destination + index, byte)
@@ -235,8 +323,15 @@ module Wardite
235
323
  # @rbs length: Integer
236
324
  # @rbs return: void
237
325
  def init(index, destination, source, length)
238
- data = memory.data
239
326
  segment = @store.data_segments[index] || raise(Wardite::EvalError, "invalid data segment")
327
+ if RUBY_ENGINE == "spinel"
328
+ target = memory.buffer
329
+ StoreAccess.check_range(destination, length, target.size)
330
+ StoreAccess.check_range(source, length, segment.bytesize)
331
+ target.set_string(segment, destination, length, source) if length > 0
332
+ return
333
+ end
334
+ data = memory.data
240
335
  StoreAccess.check_range(destination, length, data.bytesize)
241
336
  StoreAccess.check_range(source, length, segment.bytesize)
242
337
  index = 0
@@ -219,10 +219,10 @@ module Wardite
219
219
  end
220
220
 
221
221
  # @rbs return: Wardite::Efflorescence::IR
222
- def translate
222
+ def link
223
223
  hosts = [] #: Array[Wardite::Efflorescence::HostFunction]
224
224
  defined_count = 0
225
- @entries = @instance.store.funcs.map do |function|
225
+ entries = @instance.store.funcs.map do |function|
226
226
  if function.is_a?(Wardite::ExternalFunction)
227
227
  entry = FunctionEntry.new(:host, function.callsig, function.retsig, -1, hosts.size)
228
228
  callable = function.callable #: Wardite::_WasmCallable & Object
@@ -244,13 +244,20 @@ module Wardite
244
244
  entry
245
245
  end
246
246
  end
247
+ IR.new([], entries: entries, hosts: hosts)
248
+ end
249
+
250
+ # @rbs linked: Wardite::Efflorescence::IR
251
+ # @rbs return: Wardite::Efflorescence::IR
252
+ def translate(linked = link)
253
+ @entries = linked.entries
247
254
  functions = [] #: Array[Wardite::Efflorescence::Function]
248
255
  @instance.store.funcs.each_with_index do |function, index|
249
256
  if function.is_a?(Wardite::WasmFunction)
250
257
  functions << translate_function(function, index, functions.size)
251
258
  end
252
259
  end
253
- IR.new(functions, entries: @entries, hosts: hosts)
260
+ IR.new(functions, entries: @entries, hosts: linked.hosts)
254
261
  end
255
262
 
256
263
  # @rbs wasm: Wardite::WasmFunction