wardite 0.9.0 → 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/lib/wardite/alu_f32.generated.rb +1 -0
- data/lib/wardite/alu_f64.generated.rb +1 -0
- data/lib/wardite/alu_i32.generated.rb +14 -9
- data/lib/wardite/alu_i64.generated.rb +18 -20
- data/lib/wardite/cli/cli.rb +61 -28
- data/lib/wardite/const.rb +1 -0
- 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 +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +203 -138
- 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 +184 -70
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +66 -10
- data/sig/generated/wardite/cli/cli.rbs +8 -6
- data/sig/generated/wardite/const.rbs +2 -0
- 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 +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +29 -4
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +49 -12
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +126 -3
data/lib/wardite/cli/cli.rb
CHANGED
|
@@ -12,6 +12,10 @@ 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
|
|
15
19
|
|
|
16
20
|
attr_reader :profile_file_path #: String?
|
|
17
21
|
|
|
@@ -19,8 +23,12 @@ module Wardite
|
|
|
19
23
|
# @rbs return: void
|
|
20
24
|
def initialize(args)
|
|
21
25
|
@invoke = @mapdir = nil
|
|
22
|
-
@wasi =
|
|
26
|
+
@wasi = RUBY_ENGINE != "spinel"
|
|
23
27
|
@yjit = false
|
|
28
|
+
@new_engine = false
|
|
29
|
+
@disabled_optimizations = []
|
|
30
|
+
@mir_dump = false
|
|
31
|
+
@mir_profile = false
|
|
24
32
|
@memsize = 1
|
|
25
33
|
options = OptionParser.new do |opts|
|
|
26
34
|
opts.version = Wardite::VERSION
|
|
@@ -36,6 +44,14 @@ module Wardite
|
|
|
36
44
|
opts.on("--no-wasi", "Disable WASI feature") {|_v|
|
|
37
45
|
@wasi = false
|
|
38
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 }
|
|
39
55
|
opts.on("--yjit", "Enable yjit if available; setting WARDITE_YJIT_ON=1 has the same effect") {|_v|
|
|
40
56
|
@yjit = true
|
|
41
57
|
}
|
|
@@ -45,21 +61,27 @@ module Wardite
|
|
|
45
61
|
opts.on("FILE.wasm") { }
|
|
46
62
|
end
|
|
47
63
|
options.parse!(args)
|
|
64
|
+
if !new_engine && (!disabled_optimizations.empty? || mir_dump || mir_profile)
|
|
65
|
+
raise "MIR options require --new-engine"
|
|
66
|
+
end
|
|
48
67
|
@file = args[0] || raise("require file argument")
|
|
49
68
|
@args = (args[1..-1] || [])
|
|
50
69
|
@args.unshift if @args[0] == '--'
|
|
51
70
|
|
|
52
|
-
if
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
55
77
|
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
58
81
|
end
|
|
59
82
|
end
|
|
60
83
|
|
|
61
84
|
# @rbs return: Array[Integer | Float]
|
|
62
|
-
# @rbs %a{pure}
|
|
63
85
|
def args
|
|
64
86
|
@args.map do |a|
|
|
65
87
|
if a.include? "."
|
|
@@ -72,8 +94,10 @@ module Wardite
|
|
|
72
94
|
|
|
73
95
|
# @rbs return: ::Wardite::Instance
|
|
74
96
|
def new_instance
|
|
75
|
-
|
|
76
|
-
|
|
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
|
|
77
101
|
if memsize > 1
|
|
78
102
|
ins.store.memories[0].grow(memsize)
|
|
79
103
|
end
|
|
@@ -82,14 +106,18 @@ module Wardite
|
|
|
82
106
|
|
|
83
107
|
# @rbs return: void
|
|
84
108
|
def run
|
|
85
|
-
if
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
|
89
119
|
__run
|
|
90
120
|
end
|
|
91
|
-
else
|
|
92
|
-
__run
|
|
93
121
|
end
|
|
94
122
|
end
|
|
95
123
|
|
|
@@ -113,39 +141,44 @@ module Wardite
|
|
|
113
141
|
end
|
|
114
142
|
instance = new_instance
|
|
115
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
|
|
116
149
|
$stderr.puts "return value: #{ret.inspect}"
|
|
117
150
|
end
|
|
118
151
|
|
|
119
152
|
# @rbs return: void
|
|
120
153
|
def invoke_wasi
|
|
121
154
|
instance = new_instance #: ::Wardite::Instance
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
|
130
165
|
end
|
|
131
166
|
|
|
132
167
|
if defined? Bundler
|
|
133
168
|
Bundler.with_original_env do
|
|
134
|
-
instance.runtime._start
|
|
169
|
+
instance.runtime.call(:_start, [])
|
|
135
170
|
end
|
|
136
171
|
else
|
|
137
|
-
instance.runtime._start
|
|
172
|
+
instance.runtime.call(:_start, [])
|
|
138
173
|
end
|
|
139
174
|
end
|
|
140
175
|
|
|
141
176
|
# @rbs return: String?
|
|
142
|
-
# @rbs %a{pure}
|
|
143
177
|
def mount_src
|
|
144
178
|
mapdir&.split(":")&.first
|
|
145
179
|
end
|
|
146
180
|
|
|
147
181
|
# @rbs return: String?
|
|
148
|
-
# @rbs %a{pure}
|
|
149
182
|
def mount_dst
|
|
150
183
|
m = mapdir&.split(":")
|
|
151
184
|
if m
|
|
@@ -154,4 +187,4 @@ module Wardite
|
|
|
154
187
|
end
|
|
155
188
|
end
|
|
156
189
|
end
|
|
157
|
-
end
|
|
190
|
+
end
|
data/lib/wardite/const.rb
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Wardite
|
|
5
|
+
module Efflorescence
|
|
6
|
+
module I64Arithmetic
|
|
7
|
+
MASK = 0xffffffff
|
|
8
|
+
MIN = -9223372036854775807 - 1 #: Integer
|
|
9
|
+
|
|
10
|
+
# @rbs value: Integer
|
|
11
|
+
# @rbs return: Integer
|
|
12
|
+
def self.low(value)
|
|
13
|
+
value & MASK
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @rbs value: Integer
|
|
17
|
+
# @rbs return: Integer
|
|
18
|
+
def self.high(value)
|
|
19
|
+
(value >> 32) & MASK
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @rbs low: Integer
|
|
23
|
+
# @rbs high: Integer
|
|
24
|
+
# @rbs return: Integer
|
|
25
|
+
def self.join(low, high)
|
|
26
|
+
low | ((high & 0x7fffffff) << 32) | ((high >> 31) * MIN)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @rbs value: Integer
|
|
30
|
+
# @rbs return: Integer
|
|
31
|
+
def self.normalize(value)
|
|
32
|
+
join(low(value), high(value))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sign extension for an integer field of at most 32 bits.
|
|
36
|
+
# @rbs value: Integer
|
|
37
|
+
# @rbs width: Integer
|
|
38
|
+
# @rbs return: Integer
|
|
39
|
+
def self.extend(value, width)
|
|
40
|
+
limit = 1 << width
|
|
41
|
+
field = value & (limit - 1)
|
|
42
|
+
field >= (limit >> 1) ? field - limit : field
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @rbs left: Integer
|
|
46
|
+
# @rbs right: Integer
|
|
47
|
+
# @rbs return: bool
|
|
48
|
+
def self.unsigned_less?(left, right)
|
|
49
|
+
a, b = high(left), high(right)
|
|
50
|
+
a == b ? low(left) < low(right) : a < b
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @rbs value: Integer
|
|
54
|
+
# @rbs count: Integer
|
|
55
|
+
# @rbs return: Integer
|
|
56
|
+
def self.shift_left(value, count)
|
|
57
|
+
return value if count == 0
|
|
58
|
+
|
|
59
|
+
lo = low(value)
|
|
60
|
+
if count < 32
|
|
61
|
+
hi = ((high(value) << count) | (lo >> (32 - count))) & MASK
|
|
62
|
+
join((lo << count) & MASK, hi)
|
|
63
|
+
elsif count == 32
|
|
64
|
+
join(0, lo)
|
|
65
|
+
else
|
|
66
|
+
join(0, (lo << (count - 32)) & MASK)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @rbs value: Integer
|
|
71
|
+
# @rbs count: Integer
|
|
72
|
+
# @rbs signed: bool
|
|
73
|
+
# @rbs return: Integer
|
|
74
|
+
def self.shift_right(value, count, signed)
|
|
75
|
+
return value if count == 0
|
|
76
|
+
|
|
77
|
+
hi = signed ? value >> 32 : high(value)
|
|
78
|
+
if count < 32
|
|
79
|
+
# Mask hi before shifting left so the intermediate fits in 63 bits.
|
|
80
|
+
lo = (low(value) >> count) | ((high(value) << (32 - count)) & MASK)
|
|
81
|
+
join(lo, (hi >> count) & MASK)
|
|
82
|
+
else
|
|
83
|
+
lo = count == 32 ? hi : hi >> (count - 32)
|
|
84
|
+
join(lo & MASK, hi < 0 ? MASK : 0)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @rbs value: Integer
|
|
89
|
+
# @rbs return: Integer
|
|
90
|
+
def self.clz32(value)
|
|
91
|
+
count = 32
|
|
92
|
+
while value != 0
|
|
93
|
+
count -= 1
|
|
94
|
+
value >>= 1
|
|
95
|
+
end
|
|
96
|
+
count
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @rbs value: Integer
|
|
100
|
+
# @rbs return: Integer
|
|
101
|
+
def self.ctz32(value)
|
|
102
|
+
return 32 if value == 0
|
|
103
|
+
|
|
104
|
+
count = 0
|
|
105
|
+
while (value & 1) == 0
|
|
106
|
+
count += 1
|
|
107
|
+
value >>= 1
|
|
108
|
+
end
|
|
109
|
+
count
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# @rbs value: Integer
|
|
113
|
+
# @rbs return: Integer
|
|
114
|
+
def self.popcnt32(value)
|
|
115
|
+
count = 0
|
|
116
|
+
while value != 0
|
|
117
|
+
value &= value - 1
|
|
118
|
+
count += 1
|
|
119
|
+
end
|
|
120
|
+
count
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @rbs left: Integer
|
|
124
|
+
# @rbs right: Integer
|
|
125
|
+
# @rbs return: Integer
|
|
126
|
+
def self.add_i64(left, right)
|
|
127
|
+
sum = low(left) + low(right)
|
|
128
|
+
join(sum & MASK, (high(left) + high(right) + (sum >> 32)) & MASK)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# @rbs left: Integer
|
|
132
|
+
# @rbs right: Integer
|
|
133
|
+
# @rbs return: Integer
|
|
134
|
+
def self.subtract_i64(left, right)
|
|
135
|
+
difference = low(left) - low(right)
|
|
136
|
+
join(difference & MASK, (high(left) - high(right) + (difference >> 32)) & MASK)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Only the four least significant base-2**16 digits contribute.
|
|
140
|
+
# @rbs left: Integer
|
|
141
|
+
# @rbs right: Integer
|
|
142
|
+
# @rbs return: Integer
|
|
143
|
+
def self.multiply_i64(left, right)
|
|
144
|
+
a0, a1, a2, a3 = left & 0xffff, (left >> 16) & 0xffff, (left >> 32) & 0xffff, (left >> 48) & 0xffff
|
|
145
|
+
b0, b1, b2, b3 = right & 0xffff, (right >> 16) & 0xffff, (right >> 32) & 0xffff, (right >> 48) & 0xffff
|
|
146
|
+
w0 = a0 * b0
|
|
147
|
+
w1 = (w0 >> 16) + a0 * b1 + a1 * b0
|
|
148
|
+
w2 = (w1 >> 16) + a0 * b2 + a1 * b1 + a2 * b0
|
|
149
|
+
w3 = (w2 >> 16) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0
|
|
150
|
+
join((w0 & 0xffff) | ((w1 & 0xffff) << 16), (w2 & 0xffff) | ((w3 & 0xffff) << 16))
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The sign bit remains part of the unsigned magnitude, including MIN.
|
|
154
|
+
# @rbs opcode: Integer
|
|
155
|
+
# @rbs left: Integer
|
|
156
|
+
# @rbs right: Integer
|
|
157
|
+
# @rbs return: Integer
|
|
158
|
+
def self.divide(opcode, left, right)
|
|
159
|
+
raise Wardite::EvalError, "integer divide by zero" if right == 0
|
|
160
|
+
if opcode == Opcode::I64_DIV_S && left == MIN && right == -1
|
|
161
|
+
raise Wardite::IntegerOverflow, "integer division overflow"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
signed = opcode == Opcode::I64_DIV_S || opcode == Opcode::I64_REM_S
|
|
165
|
+
remainder = opcode == Opcode::I64_REM_S || opcode == Opcode::I64_REM_U
|
|
166
|
+
left_negative = signed && left < 0
|
|
167
|
+
right_negative = signed && right < 0
|
|
168
|
+
dividend = left_negative ? subtract_i64(0, left) : left
|
|
169
|
+
divisor = right_negative ? subtract_i64(0, right) : right
|
|
170
|
+
result = unsigned_divide(dividend, divisor, remainder)
|
|
171
|
+
negative = remainder ? left_negative : left_negative != right_negative
|
|
172
|
+
negative ? subtract_i64(0, result) : result
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Shift in one dividend bit per step. Preserve the extra remainder bit
|
|
176
|
+
# until comparison and subtraction have consumed it.
|
|
177
|
+
# @rbs dividend: Integer
|
|
178
|
+
# @rbs divisor: Integer
|
|
179
|
+
# @rbs remainder: bool
|
|
180
|
+
# @rbs return: Integer
|
|
181
|
+
def self.unsigned_divide(dividend, divisor, remainder)
|
|
182
|
+
nlo, nhi = low(dividend), high(dividend)
|
|
183
|
+
dlo, dhi = low(divisor), high(divisor)
|
|
184
|
+
rlo, rhi = 0, 0
|
|
185
|
+
qlo, qhi = 0, 0
|
|
186
|
+
bit = 63
|
|
187
|
+
while bit >= 0
|
|
188
|
+
incoming = bit >= 32 ? (nhi >> (bit - 32)) & 1 : (nlo >> bit) & 1
|
|
189
|
+
carry = rhi >> 31
|
|
190
|
+
rhi = ((rhi << 1) | (rlo >> 31)) & MASK
|
|
191
|
+
rlo = ((rlo << 1) | incoming) & MASK
|
|
192
|
+
if carry != 0 || rhi > dhi || (rhi == dhi && rlo >= dlo)
|
|
193
|
+
difference = rlo - dlo
|
|
194
|
+
rlo = difference & MASK
|
|
195
|
+
rhi = (rhi - dhi + (difference >> 32)) & MASK
|
|
196
|
+
if bit >= 32
|
|
197
|
+
qhi |= 1 << (bit - 32)
|
|
198
|
+
else
|
|
199
|
+
qlo |= 1 << bit
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
bit -= 1
|
|
203
|
+
end
|
|
204
|
+
remainder ? join(rlo, rhi) : join(qlo, qhi)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# @rbs opcode: Integer
|
|
208
|
+
# @rbs left: Integer
|
|
209
|
+
# @rbs right: Integer
|
|
210
|
+
# @rbs return: Integer
|
|
211
|
+
def self.calculate(opcode, left, right)
|
|
212
|
+
case opcode
|
|
213
|
+
when Opcode::I64_ADD then add_i64(left, right)
|
|
214
|
+
when Opcode::I64_SUB then subtract_i64(left, right)
|
|
215
|
+
when Opcode::I64_MUL then multiply_i64(left, right)
|
|
216
|
+
when Opcode::I64_DIV_S, Opcode::I64_DIV_U, Opcode::I64_REM_S, Opcode::I64_REM_U
|
|
217
|
+
divide(opcode, left, right)
|
|
218
|
+
when Opcode::I64_EQZ then left == 0 ? 1 : 0
|
|
219
|
+
when Opcode::I64_EQ then left == right ? 1 : 0
|
|
220
|
+
when Opcode::I64_NE then left != right ? 1 : 0
|
|
221
|
+
when Opcode::I64_LTS then left < right ? 1 : 0
|
|
222
|
+
when Opcode::I64_LTU then unsigned_less?(left, right) ? 1 : 0
|
|
223
|
+
when Opcode::I64_GTS then left > right ? 1 : 0
|
|
224
|
+
when Opcode::I64_GTU then unsigned_less?(right, left) ? 1 : 0
|
|
225
|
+
when Opcode::I64_LES then left <= right ? 1 : 0
|
|
226
|
+
when Opcode::I64_LEU then unsigned_less?(right, left) ? 0 : 1
|
|
227
|
+
when Opcode::I64_GES then left >= right ? 1 : 0
|
|
228
|
+
when Opcode::I64_GEU then unsigned_less?(left, right) ? 0 : 1
|
|
229
|
+
when Opcode::I64_AND then left & right
|
|
230
|
+
when Opcode::I64_OR then left | right
|
|
231
|
+
when Opcode::I64_XOR then left ^ right
|
|
232
|
+
when Opcode::I64_SHL then shift_left(left, right & 63)
|
|
233
|
+
when Opcode::I64_SHR_S then shift_right(left, right & 63, true)
|
|
234
|
+
when Opcode::I64_SHR_U then shift_right(left, right & 63, false)
|
|
235
|
+
when Opcode::I64_ROTL, Opcode::I64_ROTR
|
|
236
|
+
count = right & 63
|
|
237
|
+
count = (64 - count) & 63 if opcode == Opcode::I64_ROTR
|
|
238
|
+
shift_left(left, count) | shift_right(left, (64 - count) & 63, false)
|
|
239
|
+
when Opcode::I64_CLZ
|
|
240
|
+
hi = high(left)
|
|
241
|
+
hi == 0 ? 32 + clz32(low(left)) : clz32(hi)
|
|
242
|
+
when Opcode::I64_CTZ
|
|
243
|
+
lo = low(left)
|
|
244
|
+
lo == 0 ? 32 + ctz32(high(left)) : ctz32(lo)
|
|
245
|
+
when Opcode::I64_POPCNT then popcnt32(low(left)) + popcnt32(high(left))
|
|
246
|
+
when Opcode::I32_WRAP_I64 then low(left)
|
|
247
|
+
when Opcode::I64_EXTEND_I32_U then left
|
|
248
|
+
when Opcode::I64_EXTEND_I32_S, Opcode::I64_EXTEND32_S then extend(left, 32)
|
|
249
|
+
when Opcode::I64_EXTEND16_S then extend(left, 16)
|
|
250
|
+
when Opcode::I64_EXTEND8_S then extend(left, 8)
|
|
251
|
+
else
|
|
252
|
+
raise Wardite::EvalError, "invalid i64 opcode"
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|