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
@@ -1,510 +0,0 @@
1
- # rbs_inline: enabled
2
- require_relative "value"
3
-
4
- module Wardite
5
- module Evaluator
6
- # @rbs runtime: Runtime
7
- # @rbs frame: Frame
8
- # @rbs code: Symbol
9
- # @rbs operand: Array[operandItem]
10
- # @rbs return: bool?
11
- def self.i32_eval_insn(runtime, frame, code, operand)
12
- case code
13
-
14
- when :i32_load
15
- _align = operand[0] # TODO: alignment support?
16
- offset = operand[1]
17
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
18
-
19
- addr = runtime.stack.pop
20
- if !addr.is_a?(I32)
21
- raise EvalError, "maybe stack too short"
22
- end
23
-
24
- at = addr.value + offset
25
- data_end = at + I32.new.memsize / 8
26
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
27
- buf = if RUBY_ENGINE == "spinel"
28
- memory.read_bytes(at, data_end - at)
29
- else
30
- memory.data[at...data_end]
31
- end
32
- if !buf
33
- raise EvalError, "invalid memory range"
34
- end
35
- runtime.stack.push(I32.from_bytes(buf))
36
-
37
-
38
- when :i32_load8_s
39
- _align = operand[0] # TODO: alignment support?
40
- offset = operand[1]
41
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
42
-
43
- addr = runtime.stack.pop
44
- if !addr.is_a?(I32)
45
- raise EvalError, "maybe stack too short"
46
- end
47
-
48
- at = addr.value + offset
49
- data_end = at + 1
50
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
51
- buf = if RUBY_ENGINE == "spinel"
52
- memory.read_bytes(at, data_end - at)
53
- else
54
- memory.data[at...data_end]
55
- end
56
- if !buf
57
- raise EvalError, "invalid memory range"
58
- end
59
- runtime.stack.push(I32.from_bytes(buf, size: 8, signed: true))
60
-
61
-
62
- when :i32_load8_u
63
- _align = operand[0] # TODO: alignment support?
64
- offset = operand[1]
65
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
66
-
67
- addr = runtime.stack.pop
68
- if !addr.is_a?(I32)
69
- raise EvalError, "maybe stack too short"
70
- end
71
-
72
- at = addr.value + offset
73
- data_end = at + 1
74
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
75
- buf = if RUBY_ENGINE == "spinel"
76
- memory.read_bytes(at, data_end - at)
77
- else
78
- memory.data[at...data_end]
79
- end
80
- if !buf
81
- raise EvalError, "invalid memory range"
82
- end
83
- runtime.stack.push(I32.from_bytes(buf, size: 8, signed: false))
84
-
85
-
86
- when :i32_load16_s
87
- _align = operand[0] # TODO: alignment support?
88
- offset = operand[1]
89
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
90
-
91
- addr = runtime.stack.pop
92
- if !addr.is_a?(I32)
93
- raise EvalError, "maybe stack too short"
94
- end
95
-
96
- at = addr.value + offset
97
- data_end = at + 2
98
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
99
- buf = if RUBY_ENGINE == "spinel"
100
- memory.read_bytes(at, data_end - at)
101
- else
102
- memory.data[at...data_end]
103
- end
104
- if !buf
105
- raise EvalError, "invalid memory range"
106
- end
107
- runtime.stack.push(I32.from_bytes(buf, size: 16, signed: true))
108
-
109
-
110
- when :i32_load16_u
111
- _align = operand[0] # TODO: alignment support?
112
- offset = operand[1]
113
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
114
-
115
- addr = runtime.stack.pop
116
- if !addr.is_a?(I32)
117
- raise EvalError, "maybe stack too short"
118
- end
119
-
120
- at = addr.value + offset
121
- data_end = at + 2
122
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
123
- buf = if RUBY_ENGINE == "spinel"
124
- memory.read_bytes(at, data_end - at)
125
- else
126
- memory.data[at...data_end]
127
- end
128
- if !buf
129
- raise EvalError, "invalid memory range"
130
- end
131
- runtime.stack.push(I32.from_bytes(buf, size: 16, signed: false))
132
-
133
-
134
- when :i32_store
135
- _align = operand[0] # TODO: alignment support?
136
- offset = operand[1]
137
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
138
-
139
- value = runtime.stack.pop
140
- addr = runtime.stack.pop
141
- if !value.is_a?(I32) || !addr.is_a?(I32)
142
- raise EvalError, "maybe stack too short"
143
- end
144
-
145
- at = addr.value + offset
146
- data_end = at + value.packed.size
147
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
148
- if RUBY_ENGINE == "spinel"
149
- memory.write_bytes(at, value.packed)
150
- else
151
- memory.data[at...data_end] = value.packed
152
- end
153
-
154
-
155
- when :i32_store8
156
- _align = operand[0] # TODO: alignment support?
157
- offset = operand[1]
158
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
159
-
160
- value = runtime.stack.pop
161
- addr = runtime.stack.pop
162
- if !value.is_a?(I32) || !addr.is_a?(I32)
163
- raise EvalError, "maybe stack too short"
164
- end
165
-
166
- at = addr.value + offset
167
- data_end = at + 1
168
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
169
- if RUBY_ENGINE == "spinel"
170
- memory.write_bytes(at, value.packed(size: 8))
171
- else
172
- memory.data[at...data_end] = value.packed(size: 8)
173
- end
174
-
175
-
176
- when :i32_store16
177
- _align = operand[0] # TODO: alignment support?
178
- offset = operand[1]
179
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
180
-
181
- value = runtime.stack.pop
182
- addr = runtime.stack.pop
183
- if !value.is_a?(I32) || !addr.is_a?(I32)
184
- raise EvalError, "maybe stack too short"
185
- end
186
-
187
- at = addr.value + offset
188
- data_end = at + 2
189
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
190
- if RUBY_ENGINE == "spinel"
191
- memory.write_bytes(at, value.packed(size: 16))
192
- else
193
- memory.data[at...data_end] = value.packed(size: 16)
194
- end
195
-
196
-
197
- when :i32_const
198
- const = operand[0]
199
- if !const.is_a?(Integer)
200
- raise EvalError, "invalid type of operand"
201
- end
202
- runtime.stack.push(I32(const))
203
-
204
-
205
- when :i32_eqz
206
- target = runtime.stack.pop
207
- if !target.is_a?(I32)
208
- raise EvalError, "maybe empty or invalid stack"
209
- end
210
- value = target.value.zero? ? 1 : 0
211
- runtime.stack.push(I32(value))
212
-
213
-
214
- when :i32_eq
215
- right, left = runtime.stack.pop, runtime.stack.pop
216
- if !right.is_a?(I32) || !left.is_a?(I32)
217
- raise EvalError, "maybe empty or invalid stack"
218
- end
219
- value = (left.value == right.value) ? 1 : 0
220
- runtime.stack.push(I32(value))
221
-
222
-
223
- when :i32_ne
224
- right, left = runtime.stack.pop, runtime.stack.pop
225
- if !right.is_a?(I32) || !left.is_a?(I32)
226
- raise EvalError, "maybe empty or invalid stack"
227
- end
228
- value = (left.value != right.value) ? 1 : 0
229
- runtime.stack.push(I32(value))
230
-
231
-
232
- when :i32_lts
233
- right, left = runtime.stack.pop, runtime.stack.pop
234
- if !right.is_a?(I32) || !left.is_a?(I32)
235
- raise EvalError, "maybe empty or invalid stack"
236
- end
237
- value = (left.value_s < right.value_s) ? 1 : 0
238
- runtime.stack.push(I32(value))
239
-
240
-
241
- when :i32_ltu
242
- right, left = runtime.stack.pop, runtime.stack.pop
243
- if !right.is_a?(I32) || !left.is_a?(I32)
244
- raise EvalError, "maybe empty or invalid stack"
245
- end
246
- value = (left.value < right.value) ? 1 : 0
247
- runtime.stack.push(I32(value))
248
-
249
-
250
- when :i32_gts
251
- right, left = runtime.stack.pop, runtime.stack.pop
252
- if !right.is_a?(I32) || !left.is_a?(I32)
253
- raise EvalError, "maybe empty or invalid stack"
254
- end
255
- value = (left.value_s > right.value_s) ? 1 : 0
256
- runtime.stack.push(I32(value))
257
-
258
-
259
- when :i32_gtu
260
- right, left = runtime.stack.pop, runtime.stack.pop
261
- if !right.is_a?(I32) || !left.is_a?(I32)
262
- raise EvalError, "maybe empty or invalid stack"
263
- end
264
- value = (left.value > right.value) ? 1 : 0
265
- runtime.stack.push(I32(value))
266
-
267
-
268
- when :i32_les
269
- right, left = runtime.stack.pop, runtime.stack.pop
270
- if !right.is_a?(I32) || !left.is_a?(I32)
271
- raise EvalError, "maybe empty or invalid stack"
272
- end
273
- value = (left.value_s <= right.value_s) ? 1 : 0
274
- runtime.stack.push(I32(value))
275
-
276
-
277
- when :i32_leu
278
- right, left = runtime.stack.pop, runtime.stack.pop
279
- if !right.is_a?(I32) || !left.is_a?(I32)
280
- raise EvalError, "maybe empty or invalid stack"
281
- end
282
- value = (left.value <= right.value) ? 1 : 0
283
- runtime.stack.push(I32(value))
284
-
285
-
286
- when :i32_ges
287
- right, left = runtime.stack.pop, runtime.stack.pop
288
- if !right.is_a?(I32) || !left.is_a?(I32)
289
- raise EvalError, "maybe empty or invalid stack"
290
- end
291
- value = (left.value_s >= right.value_s) ? 1 : 0
292
- runtime.stack.push(I32(value))
293
-
294
-
295
- when :i32_geu
296
- right, left = runtime.stack.pop, runtime.stack.pop
297
- if !right.is_a?(I32) || !left.is_a?(I32)
298
- raise EvalError, "maybe empty or invalid stack"
299
- end
300
- value = (left.value >= right.value) ? 1 : 0
301
- runtime.stack.push(I32(value))
302
-
303
-
304
- when :i32_clz
305
- target = runtime.stack.pop
306
- if !target.is_a?(I32)
307
- raise EvalError, "maybe empty or invalid stack"
308
- end
309
- start = target.memsize - 1
310
- count = 0
311
- while start > -1
312
- if (target.value >> start).zero?
313
- count += 1
314
- start -= 1
315
- else
316
- break
317
- end
318
- end
319
- runtime.stack.push(I32(count))
320
-
321
-
322
- when :i32_ctz
323
- target = runtime.stack.pop
324
- if !target.is_a?(I32)
325
- raise EvalError, "maybe empty or invalid stack"
326
- end
327
- finish = target.memsize
328
- count = 0
329
- while count < finish
330
- if target.value[count].zero?
331
- count += 1
332
- else
333
- break
334
- end
335
- end
336
- runtime.stack.push(I32(count))
337
-
338
-
339
- when :i32_popcnt
340
- target = runtime.stack.pop
341
- if !target.is_a?(I32)
342
- raise EvalError, "maybe empty or invalid stack"
343
- end
344
- digits = target.memsize
345
- count = 0
346
- digits.times do |i|
347
- if target.value[i].nonzero?
348
- count += 1
349
- end
350
- end
351
- runtime.stack.push(I32(count))
352
-
353
-
354
- when :i32_add
355
- right, left = runtime.stack.pop, runtime.stack.pop
356
- if !right.is_a?(I32) || !left.is_a?(I32)
357
- raise EvalError, "maybe empty or invalid stack"
358
- end
359
- runtime.stack.push(I32(left.value + right.value))
360
-
361
-
362
- when :i32_sub
363
- right, left = runtime.stack.pop, runtime.stack.pop
364
- if !right.is_a?(I32) || !left.is_a?(I32)
365
- raise EvalError, "maybe empty or invalid stack"
366
- end
367
- runtime.stack.push(I32(left.value - right.value))
368
-
369
-
370
- when :i32_mul
371
- right, left = runtime.stack.pop, runtime.stack.pop
372
- if !right.is_a?(I32) || !left.is_a?(I32)
373
- raise EvalError, "maybe empty or invalid stack"
374
- end
375
- runtime.stack.push(left.multiply(right))
376
-
377
-
378
- when :i32_div_s
379
- right, left = runtime.stack.pop, runtime.stack.pop
380
- if !right.is_a?(I32) || !left.is_a?(I32)
381
- raise EvalError, "maybe empty or invalid stack"
382
- end
383
- dividend = left.value_s
384
- divisor = right.value_s
385
- signed_min = -(1 << (left.memsize - 2)) * 2
386
- if dividend == signed_min && divisor == -1
387
- raise IntegerOverflow, "integer overflow"
388
- end
389
- iresult = dividend / divisor
390
- # Ruby rounds integer division down; Wasm truncates toward zero.
391
- if (dividend < 0) != (divisor < 0) && dividend % divisor != 0
392
- iresult += 1
393
- end
394
- runtime.stack.push(I32(iresult))
395
-
396
-
397
- when :i32_div_u
398
- right, left = runtime.stack.pop, runtime.stack.pop
399
- if !right.is_a?(I32) || !left.is_a?(I32)
400
- raise EvalError, "maybe empty or invalid stack"
401
- end
402
- runtime.stack.push(I32(left.value / right.value))
403
-
404
-
405
- when :i32_rem_s
406
- right, left = runtime.stack.pop, runtime.stack.pop
407
- if !right.is_a?(I32) || !left.is_a?(I32)
408
- raise EvalError, "maybe empty or invalid stack"
409
- end
410
- result = left.value_s % right.value_s
411
- if result > 0 && left.value_s < 0
412
- result = result - right.value_s
413
- elsif result < 0 && left.value_s > 0
414
- result = result - right.value_s
415
- end
416
- runtime.stack.push(I32(result))
417
-
418
-
419
- when :i32_rem_u
420
- right, left = runtime.stack.pop, runtime.stack.pop
421
- if !right.is_a?(I32) || !left.is_a?(I32)
422
- raise EvalError, "maybe empty or invalid stack"
423
- end
424
- runtime.stack.push(I32(left.value % right.value))
425
-
426
-
427
- when :i32_and
428
- right, left = runtime.stack.pop, runtime.stack.pop
429
- if !right.is_a?(I32) || !left.is_a?(I32)
430
- raise EvalError, "maybe empty or invalid stack"
431
- end
432
- runtime.stack.push(I32(left.value & right.value))
433
-
434
-
435
- when :i32_or
436
- right, left = runtime.stack.pop, runtime.stack.pop
437
- if !right.is_a?(I32) || !left.is_a?(I32)
438
- raise EvalError, "maybe empty or invalid stack"
439
- end
440
- runtime.stack.push(I32(left.value | right.value))
441
-
442
-
443
- when :i32_xor
444
- right, left = runtime.stack.pop, runtime.stack.pop
445
- if !right.is_a?(I32) || !left.is_a?(I32)
446
- raise EvalError, "maybe empty or invalid stack"
447
- end
448
- runtime.stack.push(I32(left.value ^ right.value))
449
-
450
-
451
- when :i32_shl
452
- right, left = runtime.stack.pop, runtime.stack.pop
453
- if !right.is_a?(I32) || !left.is_a?(I32)
454
- raise EvalError, "maybe empty or invalid stack"
455
- end
456
- value = left.value << (right.value % right.memsize)
457
- value %= 1 << right.memsize
458
-
459
- runtime.stack.push(I32(value))
460
-
461
-
462
- when :i32_shr_s
463
- right, left = runtime.stack.pop, runtime.stack.pop
464
- if !right.is_a?(I32) || !left.is_a?(I32)
465
- raise EvalError, "maybe empty or invalid stack"
466
- end
467
- value = left.value_s >> (right.value % right.memsize)
468
- runtime.stack.push(I32(value))
469
-
470
-
471
- when :i32_shr_u
472
- right, left = runtime.stack.pop, runtime.stack.pop
473
- if !right.is_a?(I32) || !left.is_a?(I32)
474
- raise EvalError, "maybe empty or invalid stack"
475
- end
476
- value = left.value >> (right.value % right.memsize)
477
- runtime.stack.push(I32(value))
478
-
479
-
480
- when :i32_rotl
481
- right, left = runtime.stack.pop, runtime.stack.pop
482
- if !right.is_a?(I32) || !left.is_a?(I32)
483
- raise EvalError, "maybe empty or invalid stack"
484
- end
485
- k = (right.value % right.memsize)
486
- rotated = left.value << k
487
- rest = left.value >> (right.memsize - k)
488
- value = rotated | rest
489
- runtime.stack.push(I32(value))
490
-
491
-
492
- when :i32_rotr
493
- right, left = runtime.stack.pop, runtime.stack.pop
494
- if !right.is_a?(I32) || !left.is_a?(I32)
495
- raise EvalError, "maybe empty or invalid stack"
496
- end
497
- k = (right.value % right.memsize)
498
- rotated = left.value >> k
499
- rest = left.value & (I32::I32_MAX >> (right.memsize - k))
500
- value = rotated | (rest << (right.memsize - k))
501
- runtime.stack.push(I32(value))
502
-
503
-
504
- else
505
- return
506
- end
507
- return true
508
- end
509
- end
510
- end