wardite 0.8.2 → 1.0.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.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +80 -1
- data/Rakefile +5 -0
- data/benchmark/README.md +500 -0
- data/benchmark/build_call_profile.rb +42 -0
- data/benchmark/call_cost_worker.rb +27 -0
- data/benchmark/compare.rb +93 -0
- data/benchmark/compare_call_types.rb +212 -0
- data/benchmark/compare_dispatch.rb +123 -0
- data/benchmark/compare_engines.rb +119 -0
- data/benchmark/compare_optimizations.rb +139 -0
- data/benchmark/engine_phases.rb +30 -0
- data/benchmark/figures/README.md +35 -0
- data/benchmark/figures/plot_spinel_comparison.py +137 -0
- data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
- data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
- data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
- data/benchmark/figures/spinel_new_vm_summary.json +82 -0
- data/benchmark/float_m5.wat +42 -0
- data/benchmark/i32_call_arities.wat +89 -0
- data/benchmark/i32_calls.wat +174 -0
- data/benchmark/i32_memory.wat +58 -0
- data/benchmark/i32_mix.wat +80 -0
- data/benchmark/i64_m4.wat +55 -0
- data/benchmark/investigate_calls.rb +231 -0
- data/benchmark/investigate_generated_guards.rb +218 -0
- data/benchmark/m3_call_worker.rb +38 -0
- data/benchmark/measure_m1.rb +51 -0
- data/benchmark/measure_m2.rb +55 -0
- data/benchmark/measure_m3_calls.rb +89 -0
- data/benchmark/measure_m4.rb +134 -0
- data/benchmark/measure_m5_boundary.rb +41 -0
- data/benchmark/measure_wasi_write.rb +75 -0
- data/benchmark/optimization_worker.rb +23 -0
- data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
- data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
- data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
- data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
- data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
- data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
- data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
- data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
- data/benchmark/results/efflorescence-m1-cruby.json +799 -0
- data/benchmark/results/efflorescence-m1-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
- data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
- data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-memory.json +408 -0
- data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
- data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
- data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
- data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
- data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
- data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
- data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
- data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
- data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
- data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
- data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
- data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
- data/benchmark/summarize_call_profile.py +54 -0
- data/benchmark/validate_m1_eh.rb +34 -0
- data/benchmark/validate_m2.rb +119 -0
- data/benchmark/validate_m3.rb +44 -0
- data/benchmark/validate_m4.rb +95 -0
- data/benchmark/validate_m5.rb +108 -0
- data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
- data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
- data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
- data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
- data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
- data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
- data/docs/efflorescence-call-arities.md +115 -0
- data/docs/efflorescence-call-investigation.md +155 -0
- data/docs/efflorescence-call-transfer.md +128 -0
- data/docs/efflorescence-call-types.md +102 -0
- data/docs/efflorescence-coverage-ja.md +351 -0
- data/docs/efflorescence-generated-c-guards.md +405 -0
- data/docs/efflorescence-inlining.md +165 -0
- data/docs/efflorescence-ja.md +155 -0
- data/docs/efflorescence-m1.md +160 -0
- data/docs/efflorescence-m2.md +233 -0
- data/docs/efflorescence-m3.md +131 -0
- data/docs/efflorescence-m4.md +88 -0
- data/docs/efflorescence-m5.md +82 -0
- data/docs/efflorescence-m6.md +95 -0
- data/docs/efflorescence-operand-decoding.md +105 -0
- data/docs/efflorescence-phase2-ja.md +225 -0
- data/docs/efflorescence-phase2.md +240 -0
- data/docs/efflorescence-register-initialization.md +75 -0
- data/docs/efflorescence.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/examples/load_perf.rb +24 -0
- data/lib/wardite/alu_f32.generated.rb +13 -10
- data/lib/wardite/alu_f64.generated.rb +13 -10
- data/lib/wardite/alu_i32.generated.rb +38 -31
- data/lib/wardite/alu_i64.generated.rb +48 -48
- data/lib/wardite/cli/cli.rb +73 -22
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/convert.generated.rb +7 -5
- data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
- data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
- data/lib/wardite/efflorescence/inspection.rb +165 -0
- data/lib/wardite/efflorescence/ir.rb +1151 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +290 -0
- data/lib/wardite/efflorescence/store_access.rb +271 -0
- data/lib/wardite/efflorescence/translator.rb +859 -0
- data/lib/wardite/efflorescence/vm.rb +1006 -0
- data/lib/wardite/efflorescence/wasi.rb +108 -0
- data/lib/wardite/efflorescence.rb +11 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +460 -69
- data/lib/wardite/leb128.rb +35 -17
- data/lib/wardite/load.rb +260 -159
- data/lib/wardite/revisitor.rb +12 -4
- data/lib/wardite/value.rb +169 -175
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/consts.rb +1 -0
- data/lib/wardite/wasm_module.rb +6 -3
- data/lib/wardite.rb +220 -105
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +90 -34
- data/scripts/templates/alu_module.rb.tmpl +7 -5
- data/scripts/templates/conv_module.rb.tmpl +7 -5
- data/sig/generated/wardite/alu_f32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_f64.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i64.generated.rbs +4 -3
- data/sig/generated/wardite/cli/cli.rbs +13 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/convert.generated.rbs +4 -3
- data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
- data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
- data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +35 -10
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +38 -13
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +55 -18
- data/sig/vernier.rbs +3 -0
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +128 -3
|
@@ -5,14 +5,15 @@ module Wardite
|
|
|
5
5
|
module Evaluator
|
|
6
6
|
# @rbs runtime: Runtime
|
|
7
7
|
# @rbs frame: Frame
|
|
8
|
-
# @rbs
|
|
9
|
-
# @rbs
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
# @rbs code: Symbol
|
|
9
|
+
# @rbs operand: Array[operandItem]
|
|
10
|
+
# @rbs return: bool?
|
|
11
|
+
def self.i64_eval_insn(runtime, frame, code, operand)
|
|
12
|
+
case code
|
|
12
13
|
|
|
13
14
|
when :i64_load
|
|
14
|
-
_align =
|
|
15
|
-
offset =
|
|
15
|
+
_align = operand[0] # TODO: alignment support?
|
|
16
|
+
offset = operand[1]
|
|
16
17
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
17
18
|
|
|
18
19
|
addr = runtime.stack.pop
|
|
@@ -31,8 +32,8 @@ module Wardite
|
|
|
31
32
|
|
|
32
33
|
|
|
33
34
|
when :i64_load8_s
|
|
34
|
-
_align =
|
|
35
|
-
offset =
|
|
35
|
+
_align = operand[0] # TODO: alignment support?
|
|
36
|
+
offset = operand[1]
|
|
36
37
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
37
38
|
|
|
38
39
|
addr = runtime.stack.pop
|
|
@@ -51,8 +52,8 @@ module Wardite
|
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
when :i64_load8_u
|
|
54
|
-
_align =
|
|
55
|
-
offset =
|
|
55
|
+
_align = operand[0] # TODO: alignment support?
|
|
56
|
+
offset = operand[1]
|
|
56
57
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
57
58
|
|
|
58
59
|
addr = runtime.stack.pop
|
|
@@ -71,8 +72,8 @@ module Wardite
|
|
|
71
72
|
|
|
72
73
|
|
|
73
74
|
when :i64_load16_s
|
|
74
|
-
_align =
|
|
75
|
-
offset =
|
|
75
|
+
_align = operand[0] # TODO: alignment support?
|
|
76
|
+
offset = operand[1]
|
|
76
77
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
77
78
|
|
|
78
79
|
addr = runtime.stack.pop
|
|
@@ -91,8 +92,8 @@ module Wardite
|
|
|
91
92
|
|
|
92
93
|
|
|
93
94
|
when :i64_load16_u
|
|
94
|
-
_align =
|
|
95
|
-
offset =
|
|
95
|
+
_align = operand[0] # TODO: alignment support?
|
|
96
|
+
offset = operand[1]
|
|
96
97
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
97
98
|
|
|
98
99
|
addr = runtime.stack.pop
|
|
@@ -111,8 +112,8 @@ module Wardite
|
|
|
111
112
|
|
|
112
113
|
|
|
113
114
|
when :i64_load32_s
|
|
114
|
-
_align =
|
|
115
|
-
offset =
|
|
115
|
+
_align = operand[0] # TODO: alignment support?
|
|
116
|
+
offset = operand[1]
|
|
116
117
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
117
118
|
|
|
118
119
|
addr = runtime.stack.pop
|
|
@@ -131,8 +132,8 @@ module Wardite
|
|
|
131
132
|
|
|
132
133
|
|
|
133
134
|
when :i64_load32_u
|
|
134
|
-
_align =
|
|
135
|
-
offset =
|
|
135
|
+
_align = operand[0] # TODO: alignment support?
|
|
136
|
+
offset = operand[1]
|
|
136
137
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
137
138
|
|
|
138
139
|
addr = runtime.stack.pop
|
|
@@ -151,8 +152,8 @@ module Wardite
|
|
|
151
152
|
|
|
152
153
|
|
|
153
154
|
when :i64_store
|
|
154
|
-
_align =
|
|
155
|
-
offset =
|
|
155
|
+
_align = operand[0] # TODO: alignment support?
|
|
156
|
+
offset = operand[1]
|
|
156
157
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
157
158
|
|
|
158
159
|
value = runtime.stack.pop
|
|
@@ -168,8 +169,8 @@ module Wardite
|
|
|
168
169
|
|
|
169
170
|
|
|
170
171
|
when :i64_store8
|
|
171
|
-
_align =
|
|
172
|
-
offset =
|
|
172
|
+
_align = operand[0] # TODO: alignment support?
|
|
173
|
+
offset = operand[1]
|
|
173
174
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
174
175
|
|
|
175
176
|
value = runtime.stack.pop
|
|
@@ -185,8 +186,8 @@ module Wardite
|
|
|
185
186
|
|
|
186
187
|
|
|
187
188
|
when :i64_store16
|
|
188
|
-
_align =
|
|
189
|
-
offset =
|
|
189
|
+
_align = operand[0] # TODO: alignment support?
|
|
190
|
+
offset = operand[1]
|
|
190
191
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
191
192
|
|
|
192
193
|
value = runtime.stack.pop
|
|
@@ -202,8 +203,8 @@ module Wardite
|
|
|
202
203
|
|
|
203
204
|
|
|
204
205
|
when :i64_store32
|
|
205
|
-
_align =
|
|
206
|
-
offset =
|
|
206
|
+
_align = operand[0] # TODO: alignment support?
|
|
207
|
+
offset = operand[1]
|
|
207
208
|
raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
|
|
208
209
|
|
|
209
210
|
value = runtime.stack.pop
|
|
@@ -219,7 +220,7 @@ module Wardite
|
|
|
219
220
|
|
|
220
221
|
|
|
221
222
|
when :i64_const
|
|
222
|
-
const =
|
|
223
|
+
const = operand[0]
|
|
223
224
|
if !const.is_a?(Integer)
|
|
224
225
|
raise EvalError, "invalid type of operand"
|
|
225
226
|
end
|
|
@@ -351,7 +352,7 @@ module Wardite
|
|
|
351
352
|
finish = target.memsize
|
|
352
353
|
count = 0
|
|
353
354
|
while count < finish
|
|
354
|
-
if
|
|
355
|
+
if target.value[count].zero?
|
|
355
356
|
count += 1
|
|
356
357
|
else
|
|
357
358
|
break
|
|
@@ -368,7 +369,7 @@ module Wardite
|
|
|
368
369
|
digits = target.memsize
|
|
369
370
|
count = 0
|
|
370
371
|
digits.times do |i|
|
|
371
|
-
if
|
|
372
|
+
if target.value[i].nonzero?
|
|
372
373
|
count += 1
|
|
373
374
|
end
|
|
374
375
|
end
|
|
@@ -380,7 +381,7 @@ module Wardite
|
|
|
380
381
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
381
382
|
raise EvalError, "maybe empty or invalid stack"
|
|
382
383
|
end
|
|
383
|
-
runtime.stack.push(
|
|
384
|
+
runtime.stack.push(left.add(right))
|
|
384
385
|
|
|
385
386
|
|
|
386
387
|
when :i64_sub
|
|
@@ -396,7 +397,7 @@ module Wardite
|
|
|
396
397
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
397
398
|
raise EvalError, "maybe empty or invalid stack"
|
|
398
399
|
end
|
|
399
|
-
runtime.stack.push(
|
|
400
|
+
runtime.stack.push(left.multiply(right))
|
|
400
401
|
|
|
401
402
|
|
|
402
403
|
when :i64_div_s
|
|
@@ -404,12 +405,17 @@ module Wardite
|
|
|
404
405
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
405
406
|
raise EvalError, "maybe empty or invalid stack"
|
|
406
407
|
end
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
408
|
+
dividend = left.value_s
|
|
409
|
+
divisor = right.value_s
|
|
410
|
+
signed_min = -(1 << (left.memsize - 2)) * 2
|
|
411
|
+
if dividend == signed_min && divisor == -1
|
|
410
412
|
raise IntegerOverflow, "integer overflow"
|
|
411
413
|
end
|
|
412
|
-
|
|
414
|
+
iresult = dividend / divisor
|
|
415
|
+
# Ruby rounds integer division down; Wasm truncates toward zero.
|
|
416
|
+
if (dividend < 0) != (divisor < 0) && dividend % divisor != 0
|
|
417
|
+
iresult += 1
|
|
418
|
+
end
|
|
413
419
|
runtime.stack.push(I64(iresult))
|
|
414
420
|
|
|
415
421
|
|
|
@@ -472,10 +478,7 @@ module Wardite
|
|
|
472
478
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
473
479
|
raise EvalError, "maybe empty or invalid stack"
|
|
474
480
|
end
|
|
475
|
-
|
|
476
|
-
value %= 1 << right.memsize
|
|
477
|
-
|
|
478
|
-
runtime.stack.push(I64(value))
|
|
481
|
+
runtime.stack.push(left.shift_left(right.value % 64))
|
|
479
482
|
|
|
480
483
|
|
|
481
484
|
when :i64_shr_s
|
|
@@ -501,10 +504,8 @@ module Wardite
|
|
|
501
504
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
502
505
|
raise EvalError, "maybe empty or invalid stack"
|
|
503
506
|
end
|
|
504
|
-
k =
|
|
505
|
-
|
|
506
|
-
rest = left.value & (I64::I64_MAX << (right.memsize - k))
|
|
507
|
-
value = rotated | (rest >> (right.memsize - k))
|
|
507
|
+
k = right.value % 64
|
|
508
|
+
value = left.shift_left(k).value | (left.value >> (64 - k))
|
|
508
509
|
runtime.stack.push(I64(value))
|
|
509
510
|
|
|
510
511
|
|
|
@@ -513,16 +514,15 @@ module Wardite
|
|
|
513
514
|
if !right.is_a?(I64) || !left.is_a?(I64)
|
|
514
515
|
raise EvalError, "maybe empty or invalid stack"
|
|
515
516
|
end
|
|
516
|
-
k =
|
|
517
|
-
|
|
518
|
-
rest = left.value & (I64::I64_MAX >> (right.memsize - k))
|
|
519
|
-
value = rotated | (rest << (right.memsize - k))
|
|
517
|
+
k = right.value % 64
|
|
518
|
+
value = (left.value >> k) | left.shift_left((64 - k) % 64).value
|
|
520
519
|
runtime.stack.push(I64(value))
|
|
521
520
|
|
|
522
521
|
|
|
523
522
|
else
|
|
524
|
-
|
|
523
|
+
return
|
|
525
524
|
end
|
|
525
|
+
return true
|
|
526
526
|
end
|
|
527
527
|
end
|
|
528
528
|
end
|
data/lib/wardite/cli/cli.rb
CHANGED
|
@@ -12,13 +12,23 @@ module Wardite
|
|
|
12
12
|
attr_reader :memsize #: Integer
|
|
13
13
|
attr_reader :wasi #: bool
|
|
14
14
|
attr_reader :yjit #: bool
|
|
15
|
+
attr_reader :new_engine #: bool
|
|
16
|
+
attr_reader :disabled_optimizations #: Array[Symbol]
|
|
17
|
+
attr_reader :mir_dump #: bool
|
|
18
|
+
attr_reader :mir_profile #: bool
|
|
19
|
+
|
|
20
|
+
attr_reader :profile_file_path #: String?
|
|
15
21
|
|
|
16
22
|
# @rbs args: Array[String]
|
|
17
23
|
# @rbs return: void
|
|
18
24
|
def initialize(args)
|
|
19
25
|
@invoke = @mapdir = nil
|
|
20
|
-
@wasi =
|
|
26
|
+
@wasi = RUBY_ENGINE != "spinel"
|
|
21
27
|
@yjit = false
|
|
28
|
+
@new_engine = false
|
|
29
|
+
@disabled_optimizations = []
|
|
30
|
+
@mir_dump = false
|
|
31
|
+
@mir_profile = false
|
|
22
32
|
@memsize = 1
|
|
23
33
|
options = OptionParser.new do |opts|
|
|
24
34
|
opts.version = Wardite::VERSION
|
|
@@ -34,27 +44,44 @@ module Wardite
|
|
|
34
44
|
opts.on("--no-wasi", "Disable WASI feature") {|_v|
|
|
35
45
|
@wasi = false
|
|
36
46
|
}
|
|
47
|
+
opts.on("--new-engine", "Use the Efflorescence register VM") {|_v|
|
|
48
|
+
@new_engine = true
|
|
49
|
+
}
|
|
50
|
+
opts.on("--mir-disable NAME", "Disable copy, immediate, branch, superinstruction, inline, frames, or all (repeatable)") {|v|
|
|
51
|
+
@disabled_optimizations << v.to_sym
|
|
52
|
+
}
|
|
53
|
+
opts.on("--mir-dump", "Print semantic and optimized MIR") { @mir_dump = true }
|
|
54
|
+
opts.on("--mir-profile", "Instrument MIR execution (do not use for timing)") { @mir_profile = true }
|
|
37
55
|
opts.on("--yjit", "Enable yjit if available; setting WARDITE_YJIT_ON=1 has the same effect") {|_v|
|
|
38
56
|
@yjit = true
|
|
39
57
|
}
|
|
58
|
+
opts.on("--out-profile [file]", "Enable Vernier profiling and save to the file") {|v|
|
|
59
|
+
@profile_file_path = v
|
|
60
|
+
}
|
|
40
61
|
opts.on("FILE.wasm") { }
|
|
41
62
|
end
|
|
42
63
|
options.parse!(args)
|
|
64
|
+
if !new_engine && (!disabled_optimizations.empty? || mir_dump || mir_profile)
|
|
65
|
+
raise "MIR options require --new-engine"
|
|
66
|
+
end
|
|
43
67
|
@file = args[0] || raise("require file argument")
|
|
44
68
|
@args = (args[1..-1] || [])
|
|
45
69
|
@args.unshift if @args[0] == '--'
|
|
46
70
|
|
|
47
|
-
if
|
|
48
|
-
|
|
49
|
-
|
|
71
|
+
if RUBY_ENGINE == "spinel"
|
|
72
|
+
warn "Warning: YJIT is not available in Spinel builds" if yjit || ENV["WARDITE_YJIT_ON"] == "1"
|
|
73
|
+
else
|
|
74
|
+
if (yjit || ENV["WARDITE_YJIT_ON"] == "1") && (defined? RubyVM::YJIT)
|
|
75
|
+
RubyVM::YJIT.enable
|
|
76
|
+
end
|
|
50
77
|
|
|
51
|
-
|
|
52
|
-
|
|
78
|
+
if (yjit || ENV["WARDITE_YJIT_ON"] == "1") && !defined?(RubyVM::YJIT)
|
|
79
|
+
warn "Warning: --yjit option is specified, but not available in this Ruby build"
|
|
80
|
+
end
|
|
53
81
|
end
|
|
54
82
|
end
|
|
55
83
|
|
|
56
84
|
# @rbs return: Array[Integer | Float]
|
|
57
|
-
# @rbs %a{pure}
|
|
58
85
|
def args
|
|
59
86
|
@args.map do |a|
|
|
60
87
|
if a.include? "."
|
|
@@ -67,8 +94,10 @@ module Wardite
|
|
|
67
94
|
|
|
68
95
|
# @rbs return: ::Wardite::Instance
|
|
69
96
|
def new_instance
|
|
70
|
-
|
|
71
|
-
|
|
97
|
+
ins = File.open(file, "rb") do |f|
|
|
98
|
+
Wardite::BinaryLoader.load_from_buffer(f, enable_wasi: wasi, engine: new_engine ? :efflorescence : :stack,
|
|
99
|
+
disabled_optimizations: disabled_optimizations, mir_profile: mir_profile)
|
|
100
|
+
end
|
|
72
101
|
if memsize > 1
|
|
73
102
|
ins.store.memories[0].grow(memsize)
|
|
74
103
|
end
|
|
@@ -77,6 +106,23 @@ module Wardite
|
|
|
77
106
|
|
|
78
107
|
# @rbs return: void
|
|
79
108
|
def run
|
|
109
|
+
if RUBY_ENGINE == "spinel"
|
|
110
|
+
__run
|
|
111
|
+
else
|
|
112
|
+
if profile_file_path
|
|
113
|
+
require "vernier"
|
|
114
|
+
$stderr.puts "Activated Vernier profiling"
|
|
115
|
+
Vernier.profile(out: profile_file_path) do
|
|
116
|
+
__run
|
|
117
|
+
end
|
|
118
|
+
else
|
|
119
|
+
__run
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @rbs return: void
|
|
125
|
+
def __run
|
|
80
126
|
if invoke
|
|
81
127
|
invoke_function
|
|
82
128
|
else
|
|
@@ -95,39 +141,44 @@ module Wardite
|
|
|
95
141
|
end
|
|
96
142
|
instance = new_instance
|
|
97
143
|
ret = instance.runtime.call(invoke, args)
|
|
144
|
+
runtime = instance.runtime
|
|
145
|
+
if runtime.is_a?(Wardite::Efflorescence::VM)
|
|
146
|
+
$stderr.puts runtime.dump_mir if mir_dump
|
|
147
|
+
$stderr.puts runtime.metrics.report if mir_profile
|
|
148
|
+
end
|
|
98
149
|
$stderr.puts "return value: #{ret.inspect}"
|
|
99
150
|
end
|
|
100
151
|
|
|
101
152
|
# @rbs return: void
|
|
102
153
|
def invoke_wasi
|
|
103
154
|
instance = new_instance #: ::Wardite::Instance
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
155
|
+
wasi_env = instance.wasi
|
|
156
|
+
if wasi_env
|
|
157
|
+
wasi_env.argv = ["wardite"] + @args
|
|
158
|
+
destination = mount_dst
|
|
159
|
+
source = mount_src
|
|
160
|
+
if mapdir && destination && source
|
|
161
|
+
# TODO: support multiple mapdir
|
|
162
|
+
wasi_env.mapdir[destination] = source
|
|
163
|
+
wasi_env.set_preopened_dir(destination, source)
|
|
164
|
+
end
|
|
112
165
|
end
|
|
113
166
|
|
|
114
167
|
if defined? Bundler
|
|
115
168
|
Bundler.with_original_env do
|
|
116
|
-
instance.runtime._start
|
|
169
|
+
instance.runtime.call(:_start, [])
|
|
117
170
|
end
|
|
118
171
|
else
|
|
119
|
-
instance.runtime._start
|
|
172
|
+
instance.runtime.call(:_start, [])
|
|
120
173
|
end
|
|
121
174
|
end
|
|
122
175
|
|
|
123
176
|
# @rbs return: String?
|
|
124
|
-
# @rbs %a{pure}
|
|
125
177
|
def mount_src
|
|
126
178
|
mapdir&.split(":")&.first
|
|
127
179
|
end
|
|
128
180
|
|
|
129
181
|
# @rbs return: String?
|
|
130
|
-
# @rbs %a{pure}
|
|
131
182
|
def mount_dst
|
|
132
183
|
m = mapdir&.split(":")
|
|
133
184
|
if m
|
|
@@ -136,4 +187,4 @@ module Wardite
|
|
|
136
187
|
end
|
|
137
188
|
end
|
|
138
189
|
end
|
|
139
|
-
end
|
|
190
|
+
end
|
data/lib/wardite/const.rb
CHANGED
|
@@ -5,10 +5,11 @@ module Wardite
|
|
|
5
5
|
module Evaluator
|
|
6
6
|
# @rbs runtime: Runtime
|
|
7
7
|
# @rbs frame: Frame
|
|
8
|
-
# @rbs
|
|
9
|
-
# @rbs
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
# @rbs code: Symbol
|
|
9
|
+
# @rbs operand: Array[operandItem]
|
|
10
|
+
# @rbs return: bool?
|
|
11
|
+
def self.convert_eval_insn(runtime, frame, code, operand)
|
|
12
|
+
case code
|
|
12
13
|
|
|
13
14
|
when :i32_wrap_i64
|
|
14
15
|
from = runtime.stack.pop
|
|
@@ -331,8 +332,9 @@ module Wardite
|
|
|
331
332
|
|
|
332
333
|
|
|
333
334
|
else
|
|
334
|
-
|
|
335
|
+
return
|
|
335
336
|
end
|
|
337
|
+
return true
|
|
336
338
|
end
|
|
337
339
|
end
|
|
338
340
|
end
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Wardite
|
|
5
|
+
module Efflorescence
|
|
6
|
+
module FloatArithmetic
|
|
7
|
+
F32_SIGN = 0x80000000
|
|
8
|
+
F32_INFINITY = 0x7f800000
|
|
9
|
+
F64_INFINITY = 0x7ff0000000000000
|
|
10
|
+
F32_NAN = 0x7fc00000
|
|
11
|
+
F64_NAN = 0x7ff8000000000000
|
|
12
|
+
|
|
13
|
+
# @rbs opcode: Integer
|
|
14
|
+
# @rbs left: Integer
|
|
15
|
+
# @rbs right: Integer
|
|
16
|
+
# @rbs return: Integer
|
|
17
|
+
def self.calculate(opcode, left, right)
|
|
18
|
+
type = opcode < Opcode::F64_EQ ? :f32 : :f64
|
|
19
|
+
op = type == :f32 ? opcode : opcode - (Opcode::F64_EQ - Opcode::F32_EQ)
|
|
20
|
+
sign = type == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
21
|
+
magnitude = left & ~sign
|
|
22
|
+
infinity = type == :f32 ? F32_INFINITY : F64_INFINITY
|
|
23
|
+
nan = type == :f32 ? F32_NAN : F64_NAN
|
|
24
|
+
case op
|
|
25
|
+
when Opcode::F32_ABS then return magnitude
|
|
26
|
+
when Opcode::F32_NEG then return left ^ sign
|
|
27
|
+
when Opcode::F32_COPYSIGN then return magnitude | (right & sign)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
other_magnitude = right & ~sign
|
|
31
|
+
if magnitude > infinity || other_magnitude > infinity
|
|
32
|
+
return op == Opcode::F32_NE ? 1 : 0 if op <= Opcode::F32_GE
|
|
33
|
+
return nan
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
x = FloatBits.to_float(type, left)
|
|
37
|
+
if op >= Opcode::F32_CEIL && op <= Opcode::F32_NEAREST
|
|
38
|
+
# Values from 2**fraction_width onward are already integral.
|
|
39
|
+
limit = type == :f32 ? 0x4b000000 : 0x4330000000000000
|
|
40
|
+
return left if magnitude == 0 || magnitude >= limit
|
|
41
|
+
rounded = integral(op, x)
|
|
42
|
+
return left & sign if rounded == 0
|
|
43
|
+
return FloatBits.from_float(type, rounded.to_f)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if op == Opcode::F32_SQRT
|
|
47
|
+
return nan if (left & sign) != 0 && magnitude != 0
|
|
48
|
+
return FloatBits.from_float(type, sqrt(x))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
y = FloatBits.to_float(type, right)
|
|
52
|
+
case op
|
|
53
|
+
when Opcode::F32_EQ then return x == y ? 1 : 0
|
|
54
|
+
when Opcode::F32_NE then return x != y ? 1 : 0
|
|
55
|
+
when Opcode::F32_LT then return x < y ? 1 : 0
|
|
56
|
+
when Opcode::F32_GT then return x > y ? 1 : 0
|
|
57
|
+
when Opcode::F32_LE then return x <= y ? 1 : 0
|
|
58
|
+
when Opcode::F32_GE then return x >= y ? 1 : 0
|
|
59
|
+
when Opcode::F32_MIN
|
|
60
|
+
return (left | right) if magnitude == 0 && other_magnitude == 0
|
|
61
|
+
return x < y ? left : right
|
|
62
|
+
when Opcode::F32_MAX
|
|
63
|
+
return (left & right) if magnitude == 0 && other_magnitude == 0
|
|
64
|
+
return x > y ? left : right
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
result = case op
|
|
68
|
+
when Opcode::F32_ADD then x + y
|
|
69
|
+
when Opcode::F32_SUB then x - y
|
|
70
|
+
when Opcode::F32_MUL then x * y
|
|
71
|
+
when Opcode::F32_DIV then divide(x, y)
|
|
72
|
+
else raise EvalError, "invalid MIR float opcode"
|
|
73
|
+
end
|
|
74
|
+
result.nan? ? nan : FloatBits.from_float(type, result)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @rbs op: Integer
|
|
78
|
+
# @rbs value: Float
|
|
79
|
+
# @rbs return: Integer
|
|
80
|
+
def self.integral(op, value)
|
|
81
|
+
case op
|
|
82
|
+
when Opcode::F32_CEIL then value.ceil
|
|
83
|
+
when Opcode::F32_FLOOR then value.floor
|
|
84
|
+
when Opcode::F32_TRUNC then value.to_i
|
|
85
|
+
else
|
|
86
|
+
# Use the magnitude so subtraction near 0.5 is exact.
|
|
87
|
+
magnitude = value.abs
|
|
88
|
+
lower = magnitude.floor
|
|
89
|
+
fraction = magnitude - lower.to_f
|
|
90
|
+
rounded = fraction > 0.5 || (fraction == 0.5 && lower.odd?) ? lower + 1 : lower
|
|
91
|
+
value < 0.0 ? -rounded : rounded
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @rbs opcode: Integer
|
|
96
|
+
# @rbs bits: Integer
|
|
97
|
+
# @rbs return: Integer
|
|
98
|
+
def self.convert(opcode, bits)
|
|
99
|
+
signature = Opcode::FLOAT_ALU_TYPES.fetch(opcode)
|
|
100
|
+
target, source = signature.fetch(0), signature.fetch(1)
|
|
101
|
+
case opcode
|
|
102
|
+
when Opcode::F32_DEMOTE_F64, Opcode::F64_PROMOTE_F32
|
|
103
|
+
mask = source == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
104
|
+
infinity = source == :f32 ? F32_INFINITY : F64_INFINITY
|
|
105
|
+
return target == :f32 ? F32_NAN : F64_NAN if (bits & ~mask) > infinity
|
|
106
|
+
|
|
107
|
+
FloatBits.from_float(target, FloatBits.to_float(source, bits))
|
|
108
|
+
else
|
|
109
|
+
if opcode < Opcode::F32_CONVERT_I32_S
|
|
110
|
+
signed = (opcode - Opcode::I32_TRUNC_F32_S).even?
|
|
111
|
+
saturating = opcode >= Opcode::I32_TRUNC_SAT_F32_S
|
|
112
|
+
truncate(target, source, bits, signed, saturating)
|
|
113
|
+
else
|
|
114
|
+
signed = (opcode - Opcode::F32_CONVERT_I32_S).even?
|
|
115
|
+
integer = source == :i32 && signed ? I64Arithmetic.extend(bits, 32) : bits
|
|
116
|
+
integer_to_float(target, integer, signed)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Round the unsigned magnitude directly to the target significand.
|
|
122
|
+
# @rbs target: Symbol
|
|
123
|
+
# @rbs value: Integer
|
|
124
|
+
# @rbs signed: bool
|
|
125
|
+
# @rbs return: Integer
|
|
126
|
+
def self.integer_to_float(target, value, signed)
|
|
127
|
+
return 0 if value == 0
|
|
128
|
+
|
|
129
|
+
negative = signed && value < 0
|
|
130
|
+
magnitude = negative ? I64Arithmetic.subtract_i64(0, value) : value
|
|
131
|
+
high = I64Arithmetic.high(magnitude)
|
|
132
|
+
exponent = high == 0 ? 31 - I64Arithmetic.clz32(magnitude) : 63 - I64Arithmetic.clz32(high)
|
|
133
|
+
fraction_width = target == :f32 ? 23 : 52
|
|
134
|
+
bias = target == :f32 ? 127 : 1023
|
|
135
|
+
shift = exponent - fraction_width
|
|
136
|
+
if shift > 0
|
|
137
|
+
significand = I64Arithmetic.shift_right(magnitude, shift, false)
|
|
138
|
+
halfway = 1 << (shift - 1)
|
|
139
|
+
remainder = magnitude & ((halfway << 1) - 1)
|
|
140
|
+
significand += 1 if remainder > halfway || (remainder == halfway && significand.odd?)
|
|
141
|
+
if significand == (2 << fraction_width)
|
|
142
|
+
significand >>= 1
|
|
143
|
+
exponent += 1
|
|
144
|
+
end
|
|
145
|
+
else
|
|
146
|
+
significand = magnitude << -shift
|
|
147
|
+
end
|
|
148
|
+
bits = ((exponent + bias) << fraction_width) | (significand & ((1 << fraction_width) - 1))
|
|
149
|
+
sign_bit = target == :f32 ? F32_SIGN : I64Arithmetic::MIN
|
|
150
|
+
negative ? bits | sign_bit : bits
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Compare the truncated magnitude in integer space, including MIN and
|
|
154
|
+
# the upper half of unsigned i64, which both have the carrier sign set.
|
|
155
|
+
# @rbs target: Symbol
|
|
156
|
+
# @rbs source: Symbol
|
|
157
|
+
# @rbs bits: Integer
|
|
158
|
+
# @rbs signed: bool
|
|
159
|
+
# @rbs saturating: bool
|
|
160
|
+
# @rbs return: Integer
|
|
161
|
+
def self.truncate(target, source, bits, signed, saturating)
|
|
162
|
+
fraction_width = source == :f32 ? 23 : 52
|
|
163
|
+
bias = source == :f32 ? 127 : 1023
|
|
164
|
+
negative = source == :f32 ? (bits & F32_SIGN) != 0 : bits < 0
|
|
165
|
+
exponent = ((bits >> fraction_width) & (2 * bias + 1)) - bias
|
|
166
|
+
fraction = bits & ((1 << fraction_width) - 1)
|
|
167
|
+
if exponent == bias + 1 && fraction != 0
|
|
168
|
+
raise EvalError, "invalid conversion to integer" unless saturating
|
|
169
|
+
return 0
|
|
170
|
+
end
|
|
171
|
+
return 0 if exponent < 0
|
|
172
|
+
|
|
173
|
+
width = target == :i32 ? 32 : 64
|
|
174
|
+
if exponent < width && (signed || !negative)
|
|
175
|
+
significand = fraction | (1 << fraction_width)
|
|
176
|
+
shift = exponent - fraction_width
|
|
177
|
+
magnitude = shift < 0 ? significand >> -shift : I64Arithmetic.shift_left(significand, shift)
|
|
178
|
+
sign_bit = width == 32 ? F32_SIGN : I64Arithmetic::MIN
|
|
179
|
+
if !signed || exponent < width - 1 || (negative && magnitude == sign_bit)
|
|
180
|
+
result = negative ? I64Arithmetic.subtract_i64(0, magnitude) : magnitude
|
|
181
|
+
return width == 32 ? I64Arithmetic.low(result) : result
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
raise Wardite::IntegerOverflow, "integer overflow" unless saturating
|
|
185
|
+
|
|
186
|
+
if negative
|
|
187
|
+
signed ? (width == 32 ? F32_SIGN : I64Arithmetic::MIN) : 0
|
|
188
|
+
elsif signed
|
|
189
|
+
width == 32 ? 0x7fffffff : 0x7fffffffffffffff
|
|
190
|
+
else
|
|
191
|
+
width == 32 ? 0xffffffff : -1
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# A normalized quotient of binary32 inputs is A/B scaled by a power of
|
|
196
|
+
# two. B < 2**24. Its distance from a binary32 midpoint, unless equal,
|
|
197
|
+
# exceeds 2**-48 in the quotient's binade scaled to [1, 2).
|
|
198
|
+
# @rbs left: Float
|
|
199
|
+
# @rbs right: Float
|
|
200
|
+
# @rbs return: Float
|
|
201
|
+
def self.divide(left, right)
|
|
202
|
+
left / right
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Squaring a binary32 midpoint gives a dyadic with at most 48 fractional
|
|
206
|
+
# bits in that binade's scale. A distinct binary32 radicand differs by at
|
|
207
|
+
# least 2**-48, so its square root differs by more than 2**-50.
|
|
208
|
+
# @rbs value: Float
|
|
209
|
+
# @rbs return: Float
|
|
210
|
+
def self.sqrt(value)
|
|
211
|
+
return value if value == 0.0
|
|
212
|
+
value < 0.0 ? Float::NAN : Math.sqrt(value)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|