wardite 1.2.1 → 1.3.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -16
  3. data/Rakefile +0 -177
  4. data/benchmark/README.md +11 -37
  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 +3 -3
  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/stack-vm-removal.json +334 -0
  25. data/benchmark/validate_m4.rb +0 -2
  26. data/docs/adr/0017-efflorescence-linear-memory-loads.md +90 -0
  27. data/docs/adr/0018-efflorescence-real-workload-measurement.md +126 -0
  28. data/docs/adr/0019-efflorescence-startup-quick-wins.md +85 -0
  29. data/docs/adr/0020-efflorescence-inline-i64-bit-operations.md +89 -0
  30. data/docs/adr/0021-efflorescence-optimize-real-functions.md +133 -0
  31. data/docs/adr/0022-efflorescence-lazy-function-build.md +111 -0
  32. data/docs/adr/0023-efflorescence-byte-sequences-and-copy-coalescing.md +56 -0
  33. data/docs/efflorescence-ja.md +6 -9
  34. data/docs/efflorescence-linear-memory-loads.md +134 -0
  35. data/docs/efflorescence-m6.md +1 -1
  36. data/docs/efflorescence-mir2-cache.md +7 -0
  37. data/docs/efflorescence-real-workloads.md +312 -0
  38. data/docs/efflorescence-spinel-linear-memory.md +2 -2
  39. data/docs/efflorescence.md +7 -8
  40. data/docs/stack-vm-removal.md +49 -0
  41. data/lib/wardite/cli/cli.rb +24 -21
  42. data/lib/wardite/efflorescence/byte_sequences.rb +80 -0
  43. data/lib/wardite/efflorescence/function_preparer.rb +64 -0
  44. data/lib/wardite/efflorescence/i64_arithmetic.rb +31 -0
  45. data/lib/wardite/efflorescence/inspection.rb +44 -12
  46. data/lib/wardite/efflorescence/ir.rb +33 -10
  47. data/lib/wardite/efflorescence/mir_cache.rb +10 -1
  48. data/lib/wardite/efflorescence/optimizer.rb +151 -42
  49. data/lib/wardite/efflorescence/store_access.rb +17 -0
  50. data/lib/wardite/efflorescence/translation_source.rb +50 -0
  51. data/lib/wardite/efflorescence/translator.rb +43 -13
  52. data/lib/wardite/efflorescence/vm.rb +116 -48
  53. data/lib/wardite/efflorescence.rb +3 -0
  54. data/lib/wardite/load.rb +47 -63
  55. data/lib/wardite/version.rb +1 -1
  56. data/lib/wardite.rb +21 -757
  57. data/scripts/build_spinel_zig.rb +1 -1
  58. data/sig/generated/wardite/cli/cli.rbs +4 -2
  59. data/sig/generated/wardite/efflorescence/byte_sequences.rbs +33 -0
  60. data/sig/generated/wardite/efflorescence/function_preparer.rbs +25 -0
  61. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +19 -0
  62. data/sig/generated/wardite/efflorescence/inspection.rbs +8 -0
  63. data/sig/generated/wardite/efflorescence/ir.rbs +18 -2
  64. data/sig/generated/wardite/efflorescence/optimizer.rbs +41 -3
  65. data/sig/generated/wardite/efflorescence/store_access.rbs +5 -0
  66. data/sig/generated/wardite/efflorescence/translation_source.rbs +34 -0
  67. data/sig/generated/wardite/efflorescence/translator.rbs +11 -1
  68. data/sig/generated/wardite/efflorescence/vm.rbs +19 -9
  69. data/sig/generated/wardite/load.rbs +18 -18
  70. data/sig/generated/wardite.rbs +4 -155
  71. metadata +32 -17
  72. data/benchmark/compare_engines.rb +0 -119
  73. data/benchmark/engine_phases.rb +0 -30
  74. data/lib/wardite/alu_f32.generated.rb +0 -261
  75. data/lib/wardite/alu_f64.generated.rb +0 -261
  76. data/lib/wardite/alu_i32.generated.rb +0 -510
  77. data/lib/wardite/alu_i64.generated.rb +0 -572
  78. data/lib/wardite/convert.generated.rb +0 -340
  79. data/scripts/gen_alu.rb +0 -864
  80. data/scripts/gen_conv.rb +0 -86
  81. data/scripts/templates/alu_module.rb.tmpl +0 -20
  82. data/scripts/templates/conv_module.rb.tmpl +0 -20
  83. data/sig/generated/wardite/alu_f32.generated.rbs +0 -12
  84. data/sig/generated/wardite/alu_f64.generated.rbs +0 -12
  85. data/sig/generated/wardite/alu_i32.generated.rbs +0 -12
  86. data/sig/generated/wardite/alu_i64.generated.rbs +0 -12
  87. data/sig/generated/wardite/convert.generated.rbs +0 -12
data/scripts/gen_alu.rb DELETED
@@ -1,864 +0,0 @@
1
- require "stringio"
2
-
3
- module GenAlu
4
- def self.execute(path, prefix: "i32", defined_ops: [])
5
- parent_dir = File.dirname(path)
6
- system "mkdir -p #{parent_dir}"
7
-
8
- basic_module = File.read(
9
- File.expand_path("../templates/alu_module.rb.tmpl", __FILE__)
10
- )
11
- basic_module.gsub!(/\$\{PREFIX\}/, prefix)
12
- ope_defs = generate_ops(prefix: prefix, defined_ops: defined_ops)
13
- basic_module.gsub!(/\$\{DEFS\}/, ope_defs)
14
-
15
- dest = File.open(path, "w")
16
- dest.puts basic_module
17
-
18
- $stderr.puts "generated: #{path}"
19
- end
20
-
21
- def self.generate_ops(prefix:, defined_ops:)
22
- result = StringIO.new("")
23
- defined_ops.each do |op|
24
- key = ["i32", "i64"].include?(prefix) && op.to_sym == :mul ? :mul_integer : :"#{op}_#{prefix}"
25
- code = (DEFS[key] || DEFS[op.to_sym]).dup
26
- if ! code
27
- raise "unsupported code specified! #{op.inspect}"
28
- end
29
- code.gsub!(/\$\{PREFIX\}/, prefix)
30
- code.gsub!(/\$\{CLASS\}/, to_class(prefix.to_sym))
31
- result << "\n"
32
- code.each_line do |ln|
33
- result << " " * 6 << ln
34
- end
35
- result << "\n"
36
- end
37
- result.string
38
- end
39
-
40
- def self.to_class(prefix)
41
- {
42
- i32: "I32",
43
- i64: "I64",
44
- f32: "F32",
45
- f64: "F64",
46
- }[prefix]
47
- end
48
-
49
- # ope_templates
50
- DEFS = { #: Hash[Symbol, String]
51
- load: <<~RUBY,
52
- when :${PREFIX}_load
53
- _align = operand[0] # TODO: alignment support?
54
- offset = operand[1]
55
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
56
-
57
- addr = runtime.stack.pop
58
- if !addr.is_a?(I32)
59
- raise EvalError, "maybe stack too short"
60
- end
61
-
62
- at = addr.value + offset
63
- data_end = at + ${CLASS}.new.memsize / 8
64
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
65
- buf = if RUBY_ENGINE == "spinel"
66
- memory.read_bytes(at, data_end - at)
67
- else
68
- memory.data[at...data_end]
69
- end
70
- if !buf
71
- raise EvalError, "invalid memory range"
72
- end
73
- runtime.stack.push(${CLASS}.from_bytes(buf))
74
- RUBY
75
-
76
- load8_s: <<~RUBY,
77
- when :${PREFIX}_load8_s
78
- _align = operand[0] # TODO: alignment support?
79
- offset = operand[1]
80
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
81
-
82
- addr = runtime.stack.pop
83
- if !addr.is_a?(I32)
84
- raise EvalError, "maybe stack too short"
85
- end
86
-
87
- at = addr.value + offset
88
- data_end = at + 1
89
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
90
- buf = if RUBY_ENGINE == "spinel"
91
- memory.read_bytes(at, data_end - at)
92
- else
93
- memory.data[at...data_end]
94
- end
95
- if !buf
96
- raise EvalError, "invalid memory range"
97
- end
98
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 8, signed: true))
99
- RUBY
100
-
101
- load8_u: <<~RUBY,
102
- when :${PREFIX}_load8_u
103
- _align = operand[0] # TODO: alignment support?
104
- offset = operand[1]
105
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
106
-
107
- addr = runtime.stack.pop
108
- if !addr.is_a?(I32)
109
- raise EvalError, "maybe stack too short"
110
- end
111
-
112
- at = addr.value + offset
113
- data_end = at + 1
114
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
115
- buf = if RUBY_ENGINE == "spinel"
116
- memory.read_bytes(at, data_end - at)
117
- else
118
- memory.data[at...data_end]
119
- end
120
- if !buf
121
- raise EvalError, "invalid memory range"
122
- end
123
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 8, signed: false))
124
- RUBY
125
-
126
- load16_s: <<~RUBY,
127
- when :${PREFIX}_load16_s
128
- _align = operand[0] # TODO: alignment support?
129
- offset = operand[1]
130
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
131
-
132
- addr = runtime.stack.pop
133
- if !addr.is_a?(I32)
134
- raise EvalError, "maybe stack too short"
135
- end
136
-
137
- at = addr.value + offset
138
- data_end = at + 2
139
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
140
- buf = if RUBY_ENGINE == "spinel"
141
- memory.read_bytes(at, data_end - at)
142
- else
143
- memory.data[at...data_end]
144
- end
145
- if !buf
146
- raise EvalError, "invalid memory range"
147
- end
148
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 16, signed: true))
149
- RUBY
150
-
151
- load16_u: <<~RUBY,
152
- when :${PREFIX}_load16_u
153
- _align = operand[0] # TODO: alignment support?
154
- offset = operand[1]
155
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
156
-
157
- addr = runtime.stack.pop
158
- if !addr.is_a?(I32)
159
- raise EvalError, "maybe stack too short"
160
- end
161
-
162
- at = addr.value + offset
163
- data_end = at + 2
164
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
165
- buf = if RUBY_ENGINE == "spinel"
166
- memory.read_bytes(at, data_end - at)
167
- else
168
- memory.data[at...data_end]
169
- end
170
- if !buf
171
- raise EvalError, "invalid memory range"
172
- end
173
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 16, signed: false))
174
- RUBY
175
-
176
- load32_s: <<~RUBY,
177
- when :${PREFIX}_load32_s
178
- _align = operand[0] # TODO: alignment support?
179
- offset = operand[1]
180
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
181
-
182
- addr = runtime.stack.pop
183
- if !addr.is_a?(I32)
184
- raise EvalError, "maybe stack too short"
185
- end
186
-
187
- at = addr.value + offset
188
- data_end = at + 4
189
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
190
- buf = if RUBY_ENGINE == "spinel"
191
- memory.read_bytes(at, data_end - at)
192
- else
193
- memory.data[at...data_end]
194
- end
195
- if !buf
196
- raise EvalError, "invalid memory range"
197
- end
198
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 32, signed: true))
199
- RUBY
200
-
201
- load32_u: <<~RUBY,
202
- when :${PREFIX}_load32_u
203
- _align = operand[0] # TODO: alignment support?
204
- offset = operand[1]
205
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
206
-
207
- addr = runtime.stack.pop
208
- if !addr.is_a?(I32)
209
- raise EvalError, "maybe stack too short"
210
- end
211
-
212
- at = addr.value + offset
213
- data_end = at + 4
214
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
215
- buf = if RUBY_ENGINE == "spinel"
216
- memory.read_bytes(at, data_end - at)
217
- else
218
- memory.data[at...data_end]
219
- end
220
- if !buf
221
- raise EvalError, "invalid memory range"
222
- end
223
- runtime.stack.push(${CLASS}.from_bytes(buf, size: 32, signed: false))
224
- RUBY
225
-
226
- store: <<~RUBY,
227
- when :${PREFIX}_store
228
- _align = operand[0] # TODO: alignment support?
229
- offset = operand[1]
230
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
231
-
232
- value = runtime.stack.pop
233
- addr = runtime.stack.pop
234
- if !value.is_a?(${CLASS}) || !addr.is_a?(I32)
235
- raise EvalError, "maybe stack too short"
236
- end
237
-
238
- at = addr.value + offset
239
- data_end = at + value.packed.size
240
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
241
- if RUBY_ENGINE == "spinel"
242
- memory.write_bytes(at, value.packed)
243
- else
244
- memory.data[at...data_end] = value.packed
245
- end
246
- RUBY
247
-
248
- store8: <<~RUBY,
249
- when :${PREFIX}_store8
250
- _align = operand[0] # TODO: alignment support?
251
- offset = operand[1]
252
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
253
-
254
- value = runtime.stack.pop
255
- addr = runtime.stack.pop
256
- if !value.is_a?(${CLASS}) || !addr.is_a?(I32)
257
- raise EvalError, "maybe stack too short"
258
- end
259
-
260
- at = addr.value + offset
261
- data_end = at + 1
262
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
263
- if RUBY_ENGINE == "spinel"
264
- memory.write_bytes(at, value.packed(size: 8))
265
- else
266
- memory.data[at...data_end] = value.packed(size: 8)
267
- end
268
- RUBY
269
-
270
- store16: <<~RUBY,
271
- when :${PREFIX}_store16
272
- _align = operand[0] # TODO: alignment support?
273
- offset = operand[1]
274
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
275
-
276
- value = runtime.stack.pop
277
- addr = runtime.stack.pop
278
- if !value.is_a?(${CLASS}) || !addr.is_a?(I32)
279
- raise EvalError, "maybe stack too short"
280
- end
281
-
282
- at = addr.value + offset
283
- data_end = at + 2
284
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
285
- if RUBY_ENGINE == "spinel"
286
- memory.write_bytes(at, value.packed(size: 16))
287
- else
288
- memory.data[at...data_end] = value.packed(size: 16)
289
- end
290
- RUBY
291
-
292
- store32: <<~RUBY,
293
- when :${PREFIX}_store32
294
- _align = operand[0] # TODO: alignment support?
295
- offset = operand[1]
296
- raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
297
-
298
- value = runtime.stack.pop
299
- addr = runtime.stack.pop
300
- if !value.is_a?(${CLASS}) || !addr.is_a?(I32)
301
- raise EvalError, "maybe stack too short"
302
- end
303
-
304
- at = addr.value + offset
305
- data_end = at + 4
306
- memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
307
- if RUBY_ENGINE == "spinel"
308
- memory.write_bytes(at, value.packed(size: 32))
309
- else
310
- memory.data[at...data_end] = value.packed(size: 32)
311
- end
312
- RUBY
313
-
314
- const: <<~RUBY,
315
- when :${PREFIX}_const
316
- const = operand[0]
317
- if !const.is_a?(Integer)
318
- raise EvalError, "invalid type of operand"
319
- end
320
- runtime.stack.push(${CLASS}(const))
321
- RUBY
322
-
323
- const__f: <<~RUBY,
324
- when :${PREFIX}_const
325
- const = operand[0]
326
- const = const.value if const.is_a?(FloatImmediate)
327
- if !const.is_a?(Float)
328
- raise EvalError, "invalid type of operand"
329
- end
330
- runtime.stack.push(${CLASS}(const))
331
- RUBY
332
-
333
- eqz: <<~RUBY,
334
- when :${PREFIX}_eqz
335
- target = runtime.stack.pop
336
- if !target.is_a?(${CLASS})
337
- raise EvalError, "maybe empty or invalid stack"
338
- end
339
- value = target.value.zero? ? 1 : 0
340
- runtime.stack.push(I32(value))
341
- RUBY
342
-
343
- eq: <<~RUBY,
344
- when :${PREFIX}_eq
345
- right, left = runtime.stack.pop, runtime.stack.pop
346
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
347
- raise EvalError, "maybe empty or invalid stack"
348
- end
349
- value = (left.value == right.value) ? 1 : 0
350
- runtime.stack.push(I32(value))
351
- RUBY
352
-
353
- ne: <<~RUBY,
354
- when :${PREFIX}_ne
355
- right, left = runtime.stack.pop, runtime.stack.pop
356
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
357
- raise EvalError, "maybe empty or invalid stack"
358
- end
359
- value = (left.value != right.value) ? 1 : 0
360
- runtime.stack.push(I32(value))
361
- RUBY
362
-
363
- lts: <<~RUBY,
364
- when :${PREFIX}_lts
365
- right, left = runtime.stack.pop, runtime.stack.pop
366
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
367
- raise EvalError, "maybe empty or invalid stack"
368
- end
369
- value = (left.value_s < right.value_s) ? 1 : 0
370
- runtime.stack.push(I32(value))
371
- RUBY
372
-
373
- ltu: <<~RUBY,
374
- when :${PREFIX}_ltu
375
- right, left = runtime.stack.pop, runtime.stack.pop
376
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
377
- raise EvalError, "maybe empty or invalid stack"
378
- end
379
- value = (left.value < right.value) ? 1 : 0
380
- runtime.stack.push(I32(value))
381
- RUBY
382
-
383
- lt: <<~RUBY,
384
- when :${PREFIX}_lt
385
- right, left = runtime.stack.pop, runtime.stack.pop
386
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
387
- raise EvalError, "maybe empty or invalid stack"
388
- end
389
- value = (left.value < right.value) ? 1 : 0
390
- runtime.stack.push(I32(value))
391
- RUBY
392
-
393
- gts: <<~RUBY,
394
- when :${PREFIX}_gts
395
- right, left = runtime.stack.pop, runtime.stack.pop
396
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
397
- raise EvalError, "maybe empty or invalid stack"
398
- end
399
- value = (left.value_s > right.value_s) ? 1 : 0
400
- runtime.stack.push(I32(value))
401
- RUBY
402
-
403
- gtu: <<~RUBY,
404
- when :${PREFIX}_gtu
405
- right, left = runtime.stack.pop, runtime.stack.pop
406
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
407
- raise EvalError, "maybe empty or invalid stack"
408
- end
409
- value = (left.value > right.value) ? 1 : 0
410
- runtime.stack.push(I32(value))
411
- RUBY
412
-
413
- gt: <<~RUBY,
414
- when :${PREFIX}_gt
415
- right, left = runtime.stack.pop, runtime.stack.pop
416
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
417
- raise EvalError, "maybe empty or invalid stack"
418
- end
419
- value = (left.value > right.value) ? 1 : 0
420
- runtime.stack.push(I32(value))
421
- RUBY
422
-
423
- les: <<~RUBY,
424
- when :${PREFIX}_les
425
- right, left = runtime.stack.pop, runtime.stack.pop
426
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
427
- raise EvalError, "maybe empty or invalid stack"
428
- end
429
- value = (left.value_s <= right.value_s) ? 1 : 0
430
- runtime.stack.push(I32(value))
431
- RUBY
432
-
433
- leu: <<~RUBY,
434
- when :${PREFIX}_leu
435
- right, left = runtime.stack.pop, runtime.stack.pop
436
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
437
- raise EvalError, "maybe empty or invalid stack"
438
- end
439
- value = (left.value <= right.value) ? 1 : 0
440
- runtime.stack.push(I32(value))
441
- RUBY
442
-
443
- le: <<~RUBY,
444
- when :${PREFIX}_le
445
- right, left = runtime.stack.pop, runtime.stack.pop
446
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
447
- raise EvalError, "maybe empty or invalid stack"
448
- end
449
- value = (left.value <= right.value) ? 1 : 0
450
- runtime.stack.push(I32(value))
451
- RUBY
452
-
453
- ges: <<~RUBY,
454
- when :${PREFIX}_ges
455
- right, left = runtime.stack.pop, runtime.stack.pop
456
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
457
- raise EvalError, "maybe empty or invalid stack"
458
- end
459
- value = (left.value_s >= right.value_s) ? 1 : 0
460
- runtime.stack.push(I32(value))
461
- RUBY
462
-
463
- geu: <<~RUBY,
464
- when :${PREFIX}_geu
465
- right, left = runtime.stack.pop, runtime.stack.pop
466
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
467
- raise EvalError, "maybe empty or invalid stack"
468
- end
469
- value = (left.value >= right.value) ? 1 : 0
470
- runtime.stack.push(I32(value))
471
- RUBY
472
-
473
- ge: <<~RUBY,
474
- when :${PREFIX}_ge
475
- right, left = runtime.stack.pop, runtime.stack.pop
476
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
477
- raise EvalError, "maybe empty or invalid stack"
478
- end
479
- value = (left.value >= right.value) ? 1 : 0
480
- runtime.stack.push(I32(value))
481
- RUBY
482
-
483
- clz: <<~RUBY,
484
- when :${PREFIX}_clz
485
- target = runtime.stack.pop
486
- if !target.is_a?(${CLASS})
487
- raise EvalError, "maybe empty or invalid stack"
488
- end
489
- start = target.memsize - 1
490
- count = 0
491
- while start > -1
492
- if (target.value >> start).zero?
493
- count += 1
494
- start -= 1
495
- else
496
- break
497
- end
498
- end
499
- runtime.stack.push(${CLASS}(count))
500
- RUBY
501
-
502
- ctz: <<~RUBY,
503
- when :${PREFIX}_ctz
504
- target = runtime.stack.pop
505
- if !target.is_a?(${CLASS})
506
- raise EvalError, "maybe empty or invalid stack"
507
- end
508
- finish = target.memsize
509
- count = 0
510
- while count < finish
511
- if target.value[count].zero?
512
- count += 1
513
- else
514
- break
515
- end
516
- end
517
- runtime.stack.push(${CLASS}(count))
518
- RUBY
519
-
520
- popcnt: <<~RUBY,
521
- when :${PREFIX}_popcnt
522
- target = runtime.stack.pop
523
- if !target.is_a?(${CLASS})
524
- raise EvalError, "maybe empty or invalid stack"
525
- end
526
- digits = target.memsize
527
- count = 0
528
- digits.times do |i|
529
- if target.value[i].nonzero?
530
- count += 1
531
- end
532
- end
533
- runtime.stack.push(${CLASS}(count))
534
- RUBY
535
-
536
- add_i64: <<~RUBY,
537
- when :i64_add
538
- right, left = runtime.stack.pop, runtime.stack.pop
539
- if !right.is_a?(I64) || !left.is_a?(I64)
540
- raise EvalError, "maybe empty or invalid stack"
541
- end
542
- runtime.stack.push(left.add(right))
543
- RUBY
544
-
545
- add: <<~RUBY,
546
- when :${PREFIX}_add
547
- right, left = runtime.stack.pop, runtime.stack.pop
548
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
549
- raise EvalError, "maybe empty or invalid stack"
550
- end
551
- runtime.stack.push(${CLASS}(left.value + right.value))
552
- RUBY
553
-
554
- sub: <<~RUBY,
555
- when :${PREFIX}_sub
556
- right, left = runtime.stack.pop, runtime.stack.pop
557
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
558
- raise EvalError, "maybe empty or invalid stack"
559
- end
560
- runtime.stack.push(${CLASS}(left.value - right.value))
561
- RUBY
562
-
563
- mul_integer: <<~RUBY,
564
- when :${PREFIX}_mul
565
- right, left = runtime.stack.pop, runtime.stack.pop
566
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
567
- raise EvalError, "maybe empty or invalid stack"
568
- end
569
- runtime.stack.push(left.multiply(right))
570
- RUBY
571
-
572
- mul: <<~RUBY,
573
- when :${PREFIX}_mul
574
- right, left = runtime.stack.pop, runtime.stack.pop
575
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
576
- raise EvalError, "maybe empty or invalid stack"
577
- end
578
- runtime.stack.push(${CLASS}(left.value * right.value))
579
- RUBY
580
-
581
- div_s: <<~RUBY,
582
- when :${PREFIX}_div_s
583
- right, left = runtime.stack.pop, runtime.stack.pop
584
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
585
- raise EvalError, "maybe empty or invalid stack"
586
- end
587
- dividend = left.value_s
588
- divisor = right.value_s
589
- signed_min = -(1 << (left.memsize - 2)) * 2
590
- if dividend == signed_min && divisor == -1
591
- raise IntegerOverflow, "integer overflow"
592
- end
593
- iresult = dividend / divisor
594
- # Ruby rounds integer division down; Wasm truncates toward zero.
595
- if (dividend < 0) != (divisor < 0) && dividend % divisor != 0
596
- iresult += 1
597
- end
598
- runtime.stack.push(${CLASS}(iresult))
599
- RUBY
600
-
601
- div_u: <<~RUBY,
602
- when :${PREFIX}_div_u
603
- right, left = runtime.stack.pop, runtime.stack.pop
604
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
605
- raise EvalError, "maybe empty or invalid stack"
606
- end
607
- runtime.stack.push(${CLASS}(left.value / right.value))
608
- RUBY
609
-
610
- div: <<~RUBY,
611
- when :${PREFIX}_div
612
- right, left = runtime.stack.pop, runtime.stack.pop
613
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
614
- raise EvalError, "maybe empty or invalid stack"
615
- end
616
- runtime.stack.push(${CLASS}(left.value / right.value))
617
- RUBY
618
-
619
- rem_s: <<~RUBY,
620
- when :${PREFIX}_rem_s
621
- right, left = runtime.stack.pop, runtime.stack.pop
622
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
623
- raise EvalError, "maybe empty or invalid stack"
624
- end
625
- result = left.value_s % right.value_s
626
- if result > 0 && left.value_s < 0
627
- result = result - right.value_s
628
- elsif result < 0 && left.value_s > 0
629
- result = result - right.value_s
630
- end
631
- runtime.stack.push(${CLASS}(result))
632
- RUBY
633
-
634
- rem_u: <<~RUBY,
635
- when :${PREFIX}_rem_u
636
- right, left = runtime.stack.pop, runtime.stack.pop
637
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
638
- raise EvalError, "maybe empty or invalid stack"
639
- end
640
- runtime.stack.push(${CLASS}(left.value % right.value))
641
- RUBY
642
-
643
- and: <<~RUBY,
644
- when :${PREFIX}_and
645
- right, left = runtime.stack.pop, runtime.stack.pop
646
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
647
- raise EvalError, "maybe empty or invalid stack"
648
- end
649
- runtime.stack.push(${CLASS}(left.value & right.value))
650
- RUBY
651
-
652
- or: <<~RUBY,
653
- when :${PREFIX}_or
654
- right, left = runtime.stack.pop, runtime.stack.pop
655
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
656
- raise EvalError, "maybe empty or invalid stack"
657
- end
658
- runtime.stack.push(${CLASS}(left.value | right.value))
659
- RUBY
660
-
661
- xor: <<~RUBY,
662
- when :${PREFIX}_xor
663
- right, left = runtime.stack.pop, runtime.stack.pop
664
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
665
- raise EvalError, "maybe empty or invalid stack"
666
- end
667
- runtime.stack.push(${CLASS}(left.value ^ right.value))
668
- RUBY
669
-
670
- shl_i64: <<~RUBY,
671
- when :i64_shl
672
- right, left = runtime.stack.pop, runtime.stack.pop
673
- if !right.is_a?(I64) || !left.is_a?(I64)
674
- raise EvalError, "maybe empty or invalid stack"
675
- end
676
- runtime.stack.push(left.shift_left(right.value % 64))
677
- RUBY
678
-
679
- shl: <<~RUBY,
680
- when :${PREFIX}_shl
681
- right, left = runtime.stack.pop, runtime.stack.pop
682
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
683
- raise EvalError, "maybe empty or invalid stack"
684
- end
685
- value = left.value << (right.value % right.memsize)
686
- value %= 1 << right.memsize
687
-
688
- runtime.stack.push(${CLASS}(value))
689
- RUBY
690
-
691
- shr_s: <<~RUBY,
692
- when :${PREFIX}_shr_s
693
- right, left = runtime.stack.pop, runtime.stack.pop
694
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
695
- raise EvalError, "maybe empty or invalid stack"
696
- end
697
- value = left.value_s >> (right.value % right.memsize)
698
- runtime.stack.push(${CLASS}(value))
699
- RUBY
700
-
701
- shr_u: <<~RUBY,
702
- when :${PREFIX}_shr_u
703
- right, left = runtime.stack.pop, runtime.stack.pop
704
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
705
- raise EvalError, "maybe empty or invalid stack"
706
- end
707
- value = left.value >> (right.value % right.memsize)
708
- runtime.stack.push(${CLASS}(value))
709
- RUBY
710
-
711
- rotl_i64: <<~RUBY,
712
- when :i64_rotl
713
- right, left = runtime.stack.pop, runtime.stack.pop
714
- if !right.is_a?(I64) || !left.is_a?(I64)
715
- raise EvalError, "maybe empty or invalid stack"
716
- end
717
- k = right.value % 64
718
- value = left.shift_left(k).value | (left.value >> (64 - k))
719
- runtime.stack.push(I64(value))
720
- RUBY
721
-
722
- rotl: <<~RUBY,
723
- when :${PREFIX}_rotl
724
- right, left = runtime.stack.pop, runtime.stack.pop
725
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
726
- raise EvalError, "maybe empty or invalid stack"
727
- end
728
- k = (right.value % right.memsize)
729
- rotated = left.value << k
730
- rest = left.value >> (right.memsize - k)
731
- value = rotated | rest
732
- runtime.stack.push(${CLASS}(value))
733
- RUBY
734
-
735
- rotr_i64: <<~RUBY,
736
- when :i64_rotr
737
- right, left = runtime.stack.pop, runtime.stack.pop
738
- if !right.is_a?(I64) || !left.is_a?(I64)
739
- raise EvalError, "maybe empty or invalid stack"
740
- end
741
- k = right.value % 64
742
- value = (left.value >> k) | left.shift_left((64 - k) % 64).value
743
- runtime.stack.push(I64(value))
744
- RUBY
745
-
746
- rotr: <<~RUBY,
747
- when :${PREFIX}_rotr
748
- right, left = runtime.stack.pop, runtime.stack.pop
749
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
750
- raise EvalError, "maybe empty or invalid stack"
751
- end
752
- k = (right.value % right.memsize)
753
- rotated = left.value >> k
754
- rest = left.value & (${CLASS}::${CLASS}_MAX >> (right.memsize - k))
755
- value = rotated | (rest << (right.memsize - k))
756
- runtime.stack.push(${CLASS}(value))
757
- RUBY
758
-
759
- # instructions for float
760
- abs: <<~RUBY,
761
- when :${PREFIX}_abs
762
- x = runtime.stack.pop
763
- if !x.is_a?(${CLASS})
764
- raise EvalError, "maybe empty or invalid stack"
765
- end
766
- runtime.stack.push(${CLASS}(x.value.abs))
767
- RUBY
768
-
769
- neg: <<~RUBY,
770
- when :${PREFIX}_neg
771
- x = runtime.stack.pop
772
- if !x.is_a?(${CLASS})
773
- raise EvalError, "maybe empty or invalid stack"
774
- end
775
- runtime.stack.push(${CLASS}(-(x.value)))
776
- RUBY
777
-
778
- ceil: <<~RUBY,
779
- when :${PREFIX}_ceil
780
- x = runtime.stack.pop
781
- if !x.is_a?(${CLASS})
782
- raise EvalError, "maybe empty or invalid stack"
783
- end
784
- runtime.stack.push(${CLASS}(x.value.ceil.to_f))
785
- RUBY
786
-
787
- floor: <<~RUBY,
788
- when :${PREFIX}_floor
789
- x = runtime.stack.pop
790
- if !x.is_a?(${CLASS})
791
- raise EvalError, "maybe empty or invalid stack"
792
- end
793
- runtime.stack.push(${CLASS}(x.value.floor.to_f))
794
- RUBY
795
-
796
- trunc: <<~RUBY,
797
- when :${PREFIX}_trunc
798
- x = runtime.stack.pop
799
- if !x.is_a?(${CLASS})
800
- raise EvalError, "maybe empty or invalid stack"
801
- end
802
- runtime.stack.push(${CLASS}(x.value.to_i.to_f))
803
- RUBY
804
-
805
- nearest: <<~RUBY,
806
- when :${PREFIX}_nearest
807
- x = runtime.stack.pop
808
- if !x.is_a?(${CLASS})
809
- raise EvalError, "maybe empty or invalid stack"
810
- end
811
- runtime.stack.push(${CLASS}(x.value.round.to_f))
812
- RUBY
813
-
814
- sqrt: <<~RUBY,
815
- when :${PREFIX}_sqrt
816
- x = runtime.stack.pop
817
- if !x.is_a?(${CLASS})
818
- raise EvalError, "maybe empty or invalid stack"
819
- end
820
- runtime.stack.push(${CLASS}(x.value ** 0.5))
821
- RUBY
822
-
823
- min: <<~RUBY,
824
- when :${PREFIX}_min
825
- right, left = runtime.stack.pop, runtime.stack.pop
826
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
827
- raise EvalError, "maybe empty or invalid stack"
828
- end
829
- if right.value.nan? || left.value.nan?
830
- runtime.stack.push(${CLASS}(Float::NAN))
831
- return
832
- end
833
- runtime.stack.push(${CLASS}([left.value, right.value].min))
834
- RUBY
835
-
836
- max: <<~RUBY,
837
- when :${PREFIX}_max
838
- right, left = runtime.stack.pop, runtime.stack.pop
839
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
840
- raise EvalError, "maybe empty or invalid stack"
841
- end
842
- if right.value.nan? || left.value.nan?
843
- runtime.stack.push(${CLASS}(Float::NAN))
844
- return
845
- end
846
- runtime.stack.push(${CLASS}([left.value, right.value].max))
847
- RUBY
848
-
849
- copysign: <<~RUBY,
850
- when :${PREFIX}_copysign
851
- right, left = runtime.stack.pop, runtime.stack.pop
852
- if !right.is_a?(${CLASS}) || !left.is_a?(${CLASS})
853
- raise EvalError, "maybe empty or invalid stack"
854
- end
855
- if left.sign == right.sign
856
- runtime.stack.push(${CLASS}(left.value))
857
- else
858
- runtime.stack.push(${CLASS}(-left.value))
859
- end
860
- RUBY
861
-
862
- # ...end generative ops
863
- }
864
- end