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
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Wardite
|
|
5
|
+
module Efflorescence
|
|
6
|
+
# The operand/control stacks exist only during translation. Execution uses
|
|
7
|
+
# typed register banks, absolute PCs and explicit per-frame handler stacks.
|
|
8
|
+
class Translator
|
|
9
|
+
class Control
|
|
10
|
+
attr_reader :kind #: Symbol
|
|
11
|
+
attr_reader :entry_stack #: Array[Integer]
|
|
12
|
+
attr_reader :start_pc #: Integer
|
|
13
|
+
attr_reader :result_types #: Array[Symbol]
|
|
14
|
+
attr_reader :result_registers #: Array[Integer]
|
|
15
|
+
attr_reader :patches #: Array[Integer]
|
|
16
|
+
attr_reader :catch_patches #: Array[Wardite::Efflorescence::CatchTarget]
|
|
17
|
+
attr_reader :handler_depth #: Integer
|
|
18
|
+
attr_reader :entry_reachable #: bool
|
|
19
|
+
attr_accessor :polymorphic #: bool
|
|
20
|
+
attr_accessor :has_branch #: bool
|
|
21
|
+
attr_accessor :false_patch #: Integer
|
|
22
|
+
attr_accessor :else_seen #: bool
|
|
23
|
+
attr_accessor :then_reachable #: bool
|
|
24
|
+
|
|
25
|
+
# @rbs kind: Symbol
|
|
26
|
+
# @rbs entry_stack: Array[Integer]
|
|
27
|
+
# @rbs start_pc: Integer
|
|
28
|
+
# @rbs result_types: Array[Symbol]
|
|
29
|
+
# @rbs result_registers: Array[Integer]
|
|
30
|
+
# @rbs handler_depth: Integer
|
|
31
|
+
# @rbs reachable: bool
|
|
32
|
+
def initialize(kind, entry_stack, start_pc, result_types, result_registers, handler_depth, reachable)
|
|
33
|
+
@kind, @entry_stack, @start_pc = kind, entry_stack.dup, start_pc
|
|
34
|
+
@result_types, @result_registers = result_types, result_registers
|
|
35
|
+
@handler_depth, @entry_reachable = handler_depth, reachable
|
|
36
|
+
@patches, @catch_patches = [], []
|
|
37
|
+
@has_branch, @else_seen, @then_reachable = false, false, false
|
|
38
|
+
@false_patch = -1
|
|
39
|
+
@polymorphic = false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
BINARY = {
|
|
44
|
+
i32_add: Opcode::I32_ADD, i32_sub: Opcode::I32_SUB, i32_mul: Opcode::I32_MUL,
|
|
45
|
+
i32_xor: Opcode::I32_XOR, i32_shl: Opcode::I32_SHL, i32_shr_u: Opcode::I32_SHR_U,
|
|
46
|
+
i32_eq: Opcode::I32_EQ, i32_ne: Opcode::I32_NE, i32_lts: Opcode::I32_LTS,
|
|
47
|
+
i32_ltu: Opcode::I32_LTU, i32_gts: Opcode::I32_GTS, i32_gtu: Opcode::I32_GTU,
|
|
48
|
+
i32_les: Opcode::I32_LES, i32_leu: Opcode::I32_LEU, i32_ges: Opcode::I32_GES,
|
|
49
|
+
i32_geu: Opcode::I32_GEU, i32_div_s: Opcode::I32_DIV_S, i32_div_u: Opcode::I32_DIV_U,
|
|
50
|
+
i32_rem_s: Opcode::I32_REM_S, i32_rem_u: Opcode::I32_REM_U,
|
|
51
|
+
i32_and: Opcode::I32_AND, i32_or: Opcode::I32_OR, i32_shr_s: Opcode::I32_SHR_S,
|
|
52
|
+
i32_rotl: Opcode::I32_ROTL, i32_rotr: Opcode::I32_ROTR
|
|
53
|
+
}.freeze
|
|
54
|
+
UNARY = { i32_eqz: Opcode::I32_EQZ, i32_clz: Opcode::I32_CLZ,
|
|
55
|
+
i32_ctz: Opcode::I32_CTZ, i32_popcnt: Opcode::I32_POPCNT,
|
|
56
|
+
i32_extend8_s: Opcode::I32_EXTEND8_S, i32_extend16_s: Opcode::I32_EXTEND16_S }.freeze
|
|
57
|
+
|
|
58
|
+
I64_ALU = {
|
|
59
|
+
i64_eqz: Opcode::I64_EQZ,
|
|
60
|
+
i32_wrap_i64: Opcode::I32_WRAP_I64,
|
|
61
|
+
i64_eq: Opcode::I64_EQ,
|
|
62
|
+
i64_ne: Opcode::I64_NE,
|
|
63
|
+
i64_lts: Opcode::I64_LTS,
|
|
64
|
+
i64_ltu: Opcode::I64_LTU,
|
|
65
|
+
i64_gts: Opcode::I64_GTS,
|
|
66
|
+
i64_gtu: Opcode::I64_GTU,
|
|
67
|
+
i64_les: Opcode::I64_LES,
|
|
68
|
+
i64_leu: Opcode::I64_LEU,
|
|
69
|
+
i64_ges: Opcode::I64_GES,
|
|
70
|
+
i64_geu: Opcode::I64_GEU,
|
|
71
|
+
i64_clz: Opcode::I64_CLZ,
|
|
72
|
+
i64_ctz: Opcode::I64_CTZ,
|
|
73
|
+
i64_popcnt: Opcode::I64_POPCNT,
|
|
74
|
+
i64_extend8_s: Opcode::I64_EXTEND8_S,
|
|
75
|
+
i64_extend16_s: Opcode::I64_EXTEND16_S,
|
|
76
|
+
i64_extend32_s: Opcode::I64_EXTEND32_S,
|
|
77
|
+
i64_extend_i32_s: Opcode::I64_EXTEND_I32_S,
|
|
78
|
+
i64_extend_i32_u: Opcode::I64_EXTEND_I32_U,
|
|
79
|
+
i64_and: Opcode::I64_AND,
|
|
80
|
+
i64_or: Opcode::I64_OR,
|
|
81
|
+
i64_xor: Opcode::I64_XOR,
|
|
82
|
+
i64_shl: Opcode::I64_SHL,
|
|
83
|
+
i64_shr_s: Opcode::I64_SHR_S,
|
|
84
|
+
i64_shr_u: Opcode::I64_SHR_U,
|
|
85
|
+
i64_rotl: Opcode::I64_ROTL,
|
|
86
|
+
i64_rotr: Opcode::I64_ROTR,
|
|
87
|
+
i64_add: Opcode::I64_ADD,
|
|
88
|
+
i64_sub: Opcode::I64_SUB,
|
|
89
|
+
i64_mul: Opcode::I64_MUL,
|
|
90
|
+
i64_div_s: Opcode::I64_DIV_S,
|
|
91
|
+
i64_div_u: Opcode::I64_DIV_U,
|
|
92
|
+
i64_rem_s: Opcode::I64_REM_S,
|
|
93
|
+
i64_rem_u: Opcode::I64_REM_U,
|
|
94
|
+
}.freeze
|
|
95
|
+
|
|
96
|
+
# Natural alignment exponents; alignment is a hint, not an address rule.
|
|
97
|
+
FLOAT_ALU = {
|
|
98
|
+
f32_eq: Opcode::F32_EQ,
|
|
99
|
+
f32_ne: Opcode::F32_NE,
|
|
100
|
+
f32_lt: Opcode::F32_LT,
|
|
101
|
+
f32_gt: Opcode::F32_GT,
|
|
102
|
+
f32_le: Opcode::F32_LE,
|
|
103
|
+
f32_ge: Opcode::F32_GE,
|
|
104
|
+
f32_abs: Opcode::F32_ABS,
|
|
105
|
+
f32_neg: Opcode::F32_NEG,
|
|
106
|
+
f32_ceil: Opcode::F32_CEIL,
|
|
107
|
+
f32_floor: Opcode::F32_FLOOR,
|
|
108
|
+
f32_trunc: Opcode::F32_TRUNC,
|
|
109
|
+
f32_nearest: Opcode::F32_NEAREST,
|
|
110
|
+
f32_sqrt: Opcode::F32_SQRT,
|
|
111
|
+
f32_add: Opcode::F32_ADD,
|
|
112
|
+
f32_sub: Opcode::F32_SUB,
|
|
113
|
+
f32_mul: Opcode::F32_MUL,
|
|
114
|
+
f32_div: Opcode::F32_DIV,
|
|
115
|
+
f32_min: Opcode::F32_MIN,
|
|
116
|
+
f32_max: Opcode::F32_MAX,
|
|
117
|
+
f32_copysign: Opcode::F32_COPYSIGN,
|
|
118
|
+
f64_eq: Opcode::F64_EQ,
|
|
119
|
+
f64_ne: Opcode::F64_NE,
|
|
120
|
+
f64_lt: Opcode::F64_LT,
|
|
121
|
+
f64_gt: Opcode::F64_GT,
|
|
122
|
+
f64_le: Opcode::F64_LE,
|
|
123
|
+
f64_ge: Opcode::F64_GE,
|
|
124
|
+
f64_abs: Opcode::F64_ABS,
|
|
125
|
+
f64_neg: Opcode::F64_NEG,
|
|
126
|
+
f64_ceil: Opcode::F64_CEIL,
|
|
127
|
+
f64_floor: Opcode::F64_FLOOR,
|
|
128
|
+
f64_trunc: Opcode::F64_TRUNC,
|
|
129
|
+
f64_nearest: Opcode::F64_NEAREST,
|
|
130
|
+
f64_sqrt: Opcode::F64_SQRT,
|
|
131
|
+
f64_add: Opcode::F64_ADD,
|
|
132
|
+
f64_sub: Opcode::F64_SUB,
|
|
133
|
+
f64_mul: Opcode::F64_MUL,
|
|
134
|
+
f64_div: Opcode::F64_DIV,
|
|
135
|
+
f64_min: Opcode::F64_MIN,
|
|
136
|
+
f64_max: Opcode::F64_MAX,
|
|
137
|
+
f64_copysign: Opcode::F64_COPYSIGN,
|
|
138
|
+
i32_trunc_f32_s: Opcode::I32_TRUNC_F32_S,
|
|
139
|
+
i32_trunc_f32_u: Opcode::I32_TRUNC_F32_U,
|
|
140
|
+
i32_trunc_f64_s: Opcode::I32_TRUNC_F64_S,
|
|
141
|
+
i32_trunc_f64_u: Opcode::I32_TRUNC_F64_U,
|
|
142
|
+
i64_trunc_f32_s: Opcode::I64_TRUNC_F32_S,
|
|
143
|
+
i64_trunc_f32_u: Opcode::I64_TRUNC_F32_U,
|
|
144
|
+
i64_trunc_f64_s: Opcode::I64_TRUNC_F64_S,
|
|
145
|
+
i64_trunc_f64_u: Opcode::I64_TRUNC_F64_U,
|
|
146
|
+
i32_trunc_sat_f32_s: Opcode::I32_TRUNC_SAT_F32_S,
|
|
147
|
+
i32_trunc_sat_f32_u: Opcode::I32_TRUNC_SAT_F32_U,
|
|
148
|
+
i32_trunc_sat_f64_s: Opcode::I32_TRUNC_SAT_F64_S,
|
|
149
|
+
i32_trunc_sat_f64_u: Opcode::I32_TRUNC_SAT_F64_U,
|
|
150
|
+
i64_trunc_sat_f32_s: Opcode::I64_TRUNC_SAT_F32_S,
|
|
151
|
+
i64_trunc_sat_f32_u: Opcode::I64_TRUNC_SAT_F32_U,
|
|
152
|
+
i64_trunc_sat_f64_s: Opcode::I64_TRUNC_SAT_F64_S,
|
|
153
|
+
i64_trunc_sat_f64_u: Opcode::I64_TRUNC_SAT_F64_U,
|
|
154
|
+
f32_convert_i32_s: Opcode::F32_CONVERT_I32_S,
|
|
155
|
+
f32_convert_i32_u: Opcode::F32_CONVERT_I32_U,
|
|
156
|
+
f32_convert_i64_s: Opcode::F32_CONVERT_I64_S,
|
|
157
|
+
f32_convert_i64_u: Opcode::F32_CONVERT_I64_U,
|
|
158
|
+
f64_convert_i32_s: Opcode::F64_CONVERT_I32_S,
|
|
159
|
+
f64_convert_i32_u: Opcode::F64_CONVERT_I32_U,
|
|
160
|
+
f64_convert_i64_s: Opcode::F64_CONVERT_I64_S,
|
|
161
|
+
f64_convert_i64_u: Opcode::F64_CONVERT_I64_U,
|
|
162
|
+
f32_demote_f64: Opcode::F32_DEMOTE_F64,
|
|
163
|
+
f64_promote_f32: Opcode::F64_PROMOTE_F32,
|
|
164
|
+
i32_reinterpret_f32: Opcode::I32_REINTERPRET_F32,
|
|
165
|
+
i64_reinterpret_f64: Opcode::I64_REINTERPRET_F64,
|
|
166
|
+
f32_reinterpret_i32: Opcode::F32_REINTERPRET_I32,
|
|
167
|
+
f64_reinterpret_i64: Opcode::F64_REINTERPRET_I64,
|
|
168
|
+
}.freeze #: Hash[Symbol, Integer]
|
|
169
|
+
|
|
170
|
+
MEMORY_WIDTHS = { i32_load: 2, i32_load8_s: 0, i32_load8_u: 0,
|
|
171
|
+
i32_load16_s: 1, i32_load16_u: 1,
|
|
172
|
+
i32_store: 2, i32_store8: 0, i32_store16: 1,
|
|
173
|
+
i64_load: 3, i64_load8_s: 0, i64_load8_u: 0,
|
|
174
|
+
i64_load16_s: 1, i64_load16_u: 1, i64_load32_s: 2, i64_load32_u: 2,
|
|
175
|
+
i64_store: 3, i64_store8: 0, i64_store16: 1, i64_store32: 2,
|
|
176
|
+
f32_load: 2, f32_store: 2, f64_load: 3, f64_store: 3 }.freeze
|
|
177
|
+
MEMORY_OPCODES = { i32_load: Opcode::I32_LOAD, i32_load8_s: Opcode::I32_LOAD8_S,
|
|
178
|
+
i32_load8_u: Opcode::I32_LOAD8_U, i32_load16_s: Opcode::I32_LOAD16_S,
|
|
179
|
+
i32_load16_u: Opcode::I32_LOAD16_U, i32_store: Opcode::I32_STORE,
|
|
180
|
+
i32_store8: Opcode::I32_STORE8, i32_store16: Opcode::I32_STORE16,
|
|
181
|
+
i64_load: Opcode::I64_LOAD,
|
|
182
|
+
i64_load8_s: Opcode::I64_LOAD8_S,
|
|
183
|
+
i64_load8_u: Opcode::I64_LOAD8_U,
|
|
184
|
+
i64_load16_s: Opcode::I64_LOAD16_S,
|
|
185
|
+
i64_load16_u: Opcode::I64_LOAD16_U,
|
|
186
|
+
i64_load32_s: Opcode::I64_LOAD32_S,
|
|
187
|
+
i64_load32_u: Opcode::I64_LOAD32_U,
|
|
188
|
+
i64_store: Opcode::I64_STORE,
|
|
189
|
+
i64_store8: Opcode::I64_STORE8,
|
|
190
|
+
i64_store16: Opcode::I64_STORE16,
|
|
191
|
+
i64_store32: Opcode::I64_STORE32,
|
|
192
|
+
f32_load: Opcode::F32_LOAD, f32_store: Opcode::F32_STORE,
|
|
193
|
+
f64_load: Opcode::F64_LOAD, f64_store: Opcode::F64_STORE }.freeze
|
|
194
|
+
STORE_CODES = (MEMORY_WIDTHS.keys + %i[global_get global_set memory_size memory_grow memory_init memory_copy memory_fill]).freeze
|
|
195
|
+
|
|
196
|
+
# @rbs @instance: Wardite::Instance
|
|
197
|
+
# @rbs @entries: Array[Wardite::Efflorescence::FunctionEntry]
|
|
198
|
+
# @rbs @stack: Array[Integer]
|
|
199
|
+
# @rbs @controls: Array[Wardite::Efflorescence::Translator::Control]
|
|
200
|
+
# @rbs @constants: Hash[Integer, Integer]
|
|
201
|
+
# @rbs @i64_constants: Hash[Integer, Integer]?
|
|
202
|
+
# @rbs @f32_constants: Hash[Integer, Integer]?
|
|
203
|
+
# @rbs @f64_constants: Hash[Integer, Integer]?
|
|
204
|
+
# @rbs @function: Wardite::Efflorescence::Function
|
|
205
|
+
# @rbs @reachable: bool
|
|
206
|
+
# @rbs @wasm_pc: Integer
|
|
207
|
+
# @rbs @handler_depth: Integer
|
|
208
|
+
|
|
209
|
+
# @rbs instance: Wardite::Instance
|
|
210
|
+
def initialize(instance)
|
|
211
|
+
@instance = instance
|
|
212
|
+
@entries = []
|
|
213
|
+
@stack, @controls, @constants = [], [], {}
|
|
214
|
+
@i64_constants = nil
|
|
215
|
+
@f32_constants = nil
|
|
216
|
+
@f64_constants = nil
|
|
217
|
+
@reachable, @wasm_pc, @handler_depth = true, 0, 0
|
|
218
|
+
@function = Function.new(0, [], [], 0)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# @rbs return: Wardite::Efflorescence::IR
|
|
222
|
+
def translate
|
|
223
|
+
hosts = [] #: Array[Wardite::Efflorescence::HostFunction]
|
|
224
|
+
defined_count = 0
|
|
225
|
+
@entries = @instance.store.funcs.map do |function|
|
|
226
|
+
if function.is_a?(Wardite::ExternalFunction)
|
|
227
|
+
entry = FunctionEntry.new(:host, function.callsig, function.retsig, -1, hosts.size)
|
|
228
|
+
callable = function.callable #: Wardite::_WasmCallable & Object
|
|
229
|
+
unless callable.respond_to?(:call)
|
|
230
|
+
raise Wardite::LoadError, "import #{function.name} is not callable"
|
|
231
|
+
end
|
|
232
|
+
unboxed_wasi = nil
|
|
233
|
+
if RUBY_ENGINE != "spinel"
|
|
234
|
+
wasi = @instance.wasi
|
|
235
|
+
if wasi && wasi.equal?(function.target_module) && wasi.is_a?(Wardite::Efflorescence::WasiSnapshotPreview1)
|
|
236
|
+
unboxed_wasi = Wardite::Efflorescence::WasiSnapshotPreview1.unboxed_callable(wasi, function)
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
hosts << HostFunction.new(function, entry, callable, unboxed_wasi)
|
|
240
|
+
entry
|
|
241
|
+
else
|
|
242
|
+
entry = FunctionEntry.new(:defined, function.callsig, function.retsig, defined_count, -1)
|
|
243
|
+
defined_count += 1
|
|
244
|
+
entry
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
functions = [] #: Array[Wardite::Efflorescence::Function]
|
|
248
|
+
@instance.store.funcs.each_with_index do |function, index|
|
|
249
|
+
if function.is_a?(Wardite::WasmFunction)
|
|
250
|
+
functions << translate_function(function, index, functions.size)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
IR.new(functions, entries: @entries, hosts: hosts)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# @rbs wasm: Wardite::WasmFunction
|
|
257
|
+
# @rbs index: Integer
|
|
258
|
+
# @rbs pool_index: Integer
|
|
259
|
+
# @rbs return: Wardite::Efflorescence::Function
|
|
260
|
+
def translate_function(wasm, index, pool_index)
|
|
261
|
+
FunctionEntry.check_signature(wasm.callsig, wasm.retsig, "function #{index}")
|
|
262
|
+
unless wasm.locals_type.all? { |type| FunctionEntry.numeric_type?(type) || type == :exnref }
|
|
263
|
+
raise UnsupportedFeature, "Efflorescence: function #{index} has unsupported locals"
|
|
264
|
+
end
|
|
265
|
+
locals = [] #: Array[Symbol]
|
|
266
|
+
wasm.locals_type.each_with_index { |type, i| wasm.locals_count.fetch(i).times { locals << type } }
|
|
267
|
+
@function = Function.new(index, wasm.callsig, wasm.retsig, locals.size, local_types: locals, pool_index: pool_index)
|
|
268
|
+
@stack, @controls, @constants = [], [], {}
|
|
269
|
+
@i64_constants = nil
|
|
270
|
+
@f32_constants = nil
|
|
271
|
+
@f64_constants = nil
|
|
272
|
+
@reachable, @handler_depth = true, 0
|
|
273
|
+
@controls << new_control(:function, wasm.retsig)
|
|
274
|
+
wasm.body.each_with_index do |instruction, pc|
|
|
275
|
+
@wasm_pc = pc
|
|
276
|
+
fail_translation("instruction after function end") if @controls.empty?
|
|
277
|
+
code, operands = instruction[1], instruction[2]
|
|
278
|
+
validate_store_instruction(code, operands) if STORE_CODES.include?(code)
|
|
279
|
+
if BINARY.key?(code)
|
|
280
|
+
right, left = pop_typed(:i32), pop_typed(:i32)
|
|
281
|
+
dst = register
|
|
282
|
+
emit([BINARY.fetch(code), dst, left, right]) if @reachable
|
|
283
|
+
@stack << dst
|
|
284
|
+
next
|
|
285
|
+
elsif UNARY.key?(code)
|
|
286
|
+
source = pop_typed(:i32)
|
|
287
|
+
dst = register
|
|
288
|
+
emit([UNARY.fetch(code), dst, source]) if @reachable
|
|
289
|
+
@stack << dst
|
|
290
|
+
next
|
|
291
|
+
end
|
|
292
|
+
case code
|
|
293
|
+
when :block, :loop, :if, :try_table
|
|
294
|
+
block = operands[0]
|
|
295
|
+
fail_translation("expected block signature") unless block.is_a?(Wardite::Block)
|
|
296
|
+
unless block.parameter_types.empty? && block.result_types.all? { |type| FunctionEntry.numeric_type?(type) || type == :exnref }
|
|
297
|
+
raise UnsupportedFeature, context("block parameters and unsupported block result types are not supported")
|
|
298
|
+
end
|
|
299
|
+
condition = code == :if ? pop_typed(:i32) : -1
|
|
300
|
+
snapshot_locals if @reachable
|
|
301
|
+
control = new_control(code, block.result_types)
|
|
302
|
+
if code == :if && @reachable
|
|
303
|
+
control.false_patch = emit([Opcode::BR_IF_ZERO, condition, -1])
|
|
304
|
+
elsif code == :try_table
|
|
305
|
+
@function.uses_eh = true
|
|
306
|
+
clauses = operands[1]
|
|
307
|
+
fail_translation("expected catch clauses") unless clauses.is_a?(Wardite::OperandVector)
|
|
308
|
+
descriptor = make_handler(clauses.entries)
|
|
309
|
+
id = @function.handlers.size
|
|
310
|
+
@function.handlers << descriptor
|
|
311
|
+
emit([Opcode::EH_ENTER, id]) if @reachable
|
|
312
|
+
@handler_depth += 1
|
|
313
|
+
end
|
|
314
|
+
@function.uses_eh = true if block.result_types.include?(:exnref)
|
|
315
|
+
@controls << control
|
|
316
|
+
when :else
|
|
317
|
+
else_control
|
|
318
|
+
when :end
|
|
319
|
+
end_control
|
|
320
|
+
when :i32_load, :i32_load8_s, :i32_load8_u, :i32_load16_s, :i32_load16_u,
|
|
321
|
+
:i32_store, :i32_store8, :i32_store16,
|
|
322
|
+
:i64_load, :i64_load8_s, :i64_load8_u, :i64_load16_s, :i64_load16_u, :i64_load32_s, :i64_load32_u,
|
|
323
|
+
:i64_store, :i64_store8, :i64_store16, :i64_store32,
|
|
324
|
+
:f32_load, :f64_load, :f32_store, :f64_store
|
|
325
|
+
opcode = MEMORY_OPCODES.fetch(code)
|
|
326
|
+
offset = integer_operand(operands[1])
|
|
327
|
+
store_type = Opcode::EXTENDED_STORE_TYPES[opcode]
|
|
328
|
+
type = store_type || Opcode::EXTENDED_LOAD_TYPES[opcode] || :i32
|
|
329
|
+
@function.uses_i64 = true if type == :i64
|
|
330
|
+
@function.uses_float = true if FloatBits.float_type?(type)
|
|
331
|
+
if store_type || code == :i32_store || code == :i32_store8 || code == :i32_store16
|
|
332
|
+
value = pop_typed(type)
|
|
333
|
+
address = pop_typed(:i32)
|
|
334
|
+
emit([opcode, value, address, offset]) if @reachable
|
|
335
|
+
else
|
|
336
|
+
address = pop_typed(:i32)
|
|
337
|
+
dst = register(type)
|
|
338
|
+
emit([opcode, dst, address, offset]) if @reachable
|
|
339
|
+
@stack << dst
|
|
340
|
+
end
|
|
341
|
+
when :memory_init, :memory_copy, :memory_fill
|
|
342
|
+
length, source, destination = pop_typed(:i32), pop_typed(:i32), pop_typed(:i32)
|
|
343
|
+
next unless @reachable
|
|
344
|
+
data_index = code == :memory_init ? integer_operand(operands[0]) : -1
|
|
345
|
+
site = MemorySite.new([destination, source, length], data_index: data_index)
|
|
346
|
+
id = @function.memory_sites.size
|
|
347
|
+
@function.memory_sites << site
|
|
348
|
+
opcode = case code
|
|
349
|
+
when :memory_init then Opcode::MEMORY_INIT
|
|
350
|
+
when :memory_copy then Opcode::MEMORY_COPY
|
|
351
|
+
else Opcode::MEMORY_FILL
|
|
352
|
+
end
|
|
353
|
+
emit([opcode, id])
|
|
354
|
+
when :memory_size, :memory_grow
|
|
355
|
+
source = code == :memory_grow ? pop_typed(:i32) : -1
|
|
356
|
+
dst = register
|
|
357
|
+
emit(code == :memory_grow ? [Opcode::MEMORY_GROW, dst, source] : [Opcode::MEMORY_SIZE, dst]) if @reachable
|
|
358
|
+
@stack << dst
|
|
359
|
+
when :global_get, :global_set
|
|
360
|
+
index = integer_operand(operands[0])
|
|
361
|
+
type = @instance.store.globals.fetch(index).type
|
|
362
|
+
@function.uses_i64 = true if type == :i64
|
|
363
|
+
@function.uses_float = true if FloatBits.float_type?(type)
|
|
364
|
+
if code == :global_get
|
|
365
|
+
dst = register(type)
|
|
366
|
+
opcode = case type
|
|
367
|
+
when :i64 then Opcode::I64_GLOBAL_GET
|
|
368
|
+
when :f32 then Opcode::F32_GLOBAL_GET
|
|
369
|
+
when :f64 then Opcode::F64_GLOBAL_GET
|
|
370
|
+
else Opcode::GLOBAL_GET
|
|
371
|
+
end
|
|
372
|
+
emit([opcode, dst, index]) if @reachable
|
|
373
|
+
@stack << dst
|
|
374
|
+
else
|
|
375
|
+
source = pop_typed(type)
|
|
376
|
+
opcode = case type
|
|
377
|
+
when :i64 then Opcode::I64_GLOBAL_SET
|
|
378
|
+
when :f32 then Opcode::F32_GLOBAL_SET
|
|
379
|
+
when :f64 then Opcode::F64_GLOBAL_SET
|
|
380
|
+
else Opcode::GLOBAL_SET
|
|
381
|
+
end
|
|
382
|
+
emit([opcode, index, source]) if @reachable
|
|
383
|
+
end
|
|
384
|
+
when :local_get, :local_set, :local_tee
|
|
385
|
+
local = integer_operand(operands[0])
|
|
386
|
+
fail_translation("invalid local index #{local}") unless local >= 0 && local < @function.parameter_count + @function.local_count
|
|
387
|
+
if code == :local_get
|
|
388
|
+
@stack << local
|
|
389
|
+
else
|
|
390
|
+
source = pop_typed(type_of(local))
|
|
391
|
+
if @reachable
|
|
392
|
+
preserve_local(local)
|
|
393
|
+
copy_value(local, source) unless local == source
|
|
394
|
+
end
|
|
395
|
+
@stack << local if code == :local_tee
|
|
396
|
+
end
|
|
397
|
+
when :i32_const, :i64_const
|
|
398
|
+
immediate = integer_operand(operands[0])
|
|
399
|
+
if code == :i64_const
|
|
400
|
+
@function.uses_i64 = true
|
|
401
|
+
@stack << constant(I64Arithmetic.normalize(immediate), :i64)
|
|
402
|
+
else
|
|
403
|
+
@stack << constant(immediate & 0xffffffff)
|
|
404
|
+
end
|
|
405
|
+
when :f32_const, :f64_const
|
|
406
|
+
operand = operands[0]
|
|
407
|
+
type = code == :f32_const ? :f32 : :f64
|
|
408
|
+
unless operand.is_a?(FloatImmediate) && operand.type == type
|
|
409
|
+
fail_translation("invalid float immediate")
|
|
410
|
+
end
|
|
411
|
+
@stack << constant(operand.bits, type)
|
|
412
|
+
when :br, :br_if
|
|
413
|
+
target = label(integer_operand(operands[0]))
|
|
414
|
+
condition = code == :br_if ? pop_typed(:i32) : -1
|
|
415
|
+
types = label_types(target)
|
|
416
|
+
sources = top_values(types)
|
|
417
|
+
unless @reachable
|
|
418
|
+
if code == :br
|
|
419
|
+
mark_unreachable
|
|
420
|
+
else
|
|
421
|
+
types.size.times { pop_value }
|
|
422
|
+
@stack.concat(target.result_registers) unless target.kind == :loop
|
|
423
|
+
end
|
|
424
|
+
next
|
|
425
|
+
end
|
|
426
|
+
if code == :br_if && (types.size > 0 || @handler_depth != target.handler_depth)
|
|
427
|
+
skip = emit([Opcode::BR_IF_ZERO, condition, -1])
|
|
428
|
+
branch(target, sources)
|
|
429
|
+
patch(skip, @function.instructions.size)
|
|
430
|
+
else
|
|
431
|
+
branch(target, sources, condition)
|
|
432
|
+
end
|
|
433
|
+
mark_unreachable if code == :br
|
|
434
|
+
when :br_table
|
|
435
|
+
depths = operands[0]
|
|
436
|
+
fail_translation("expected branch table") unless depths.is_a?(Wardite::OperandVector)
|
|
437
|
+
targets = depths.entries.map { |depth| label(depth) }
|
|
438
|
+
default = label(integer_operand(operands[1]))
|
|
439
|
+
types = label_types(default)
|
|
440
|
+
fail_translation("inconsistent br_table label types") unless targets.all? { |target| label_types(target) == types }
|
|
441
|
+
selector = pop_typed(:i32)
|
|
442
|
+
sources = top_values(types)
|
|
443
|
+
unless @reachable
|
|
444
|
+
mark_unreachable
|
|
445
|
+
next
|
|
446
|
+
end
|
|
447
|
+
targets.each_with_index do |target, i|
|
|
448
|
+
comparison = register
|
|
449
|
+
emit([Opcode::I32_EQ, comparison, selector, constant(i)])
|
|
450
|
+
skip = emit([Opcode::BR_IF_ZERO, comparison, -1])
|
|
451
|
+
branch(target, sources)
|
|
452
|
+
patch(skip, @function.instructions.size)
|
|
453
|
+
end
|
|
454
|
+
branch(default, sources)
|
|
455
|
+
mark_unreachable
|
|
456
|
+
when :select
|
|
457
|
+
condition, right, left = pop_typed(:i32), pop_value, pop_value
|
|
458
|
+
type = right < 0 ? type_of(left) : type_of(right)
|
|
459
|
+
unless (FunctionEntry.numeric_type?(type) || type == :unknown) && (left < 0 || type_of(left) == type)
|
|
460
|
+
fail_translation("select requires matching integer operands")
|
|
461
|
+
end
|
|
462
|
+
dst = type == :unknown ? -1 : register(type)
|
|
463
|
+
@stack << dst
|
|
464
|
+
next unless @reachable
|
|
465
|
+
copy_value(dst, right)
|
|
466
|
+
skip = emit([Opcode::BR_IF_ZERO, condition, -1])
|
|
467
|
+
copy_value(dst, left)
|
|
468
|
+
patch(skip, @function.instructions.size)
|
|
469
|
+
when :call
|
|
470
|
+
index = integer_operand(operands[0])
|
|
471
|
+
fail_translation("invalid call target #{index}") if index < 0
|
|
472
|
+
callee = @entries[index]
|
|
473
|
+
fail_translation("invalid call target #{index}") unless callee
|
|
474
|
+
FunctionEntry.check_signature(callee.parameter_types, callee.result_types, "function #{index}")
|
|
475
|
+
@function.uses_dynamic_calls = true if callee.kind == :host
|
|
476
|
+
arguments = callee.parameter_types.reverse.map { |type| pop_typed(type) }.reverse
|
|
477
|
+
dst = callee.result_types.empty? ? -1 : register(callee.result_types.fetch(0))
|
|
478
|
+
@stack << dst if dst >= 0
|
|
479
|
+
next unless @reachable
|
|
480
|
+
if callee.kind == :host
|
|
481
|
+
id = @function.dynamic_sites.size
|
|
482
|
+
@function.dynamic_sites << DynamicCallSite.new(callee.host_index, arguments, callee.parameter_types, callee.result_types)
|
|
483
|
+
emit([Opcode::CALL_HOST, dst, id, 0])
|
|
484
|
+
else
|
|
485
|
+
emit([Opcode::CALL, dst, index] + arguments)
|
|
486
|
+
end
|
|
487
|
+
when :call_indirect
|
|
488
|
+
type_index = integer_operand(operands[0])
|
|
489
|
+
table_index = integer_operand(operands[1])
|
|
490
|
+
raise UnsupportedFeature, context("only call_indirect through table 0 is supported") unless table_index == 0
|
|
491
|
+
table = @instance.store.tables[0]
|
|
492
|
+
fail_translation("call_indirect requires funcref table 0") unless table && table.type == :funcref
|
|
493
|
+
type = @instance.types[type_index] if type_index >= 0
|
|
494
|
+
fail_translation("invalid indirect call type index #{type_index}") unless type
|
|
495
|
+
FunctionEntry.check_signature(type.callsig, type.retsig, "type #{type_index}")
|
|
496
|
+
@function.uses_dynamic_calls = true
|
|
497
|
+
selector = pop_typed(:i32)
|
|
498
|
+
arguments = type.callsig.reverse.map { |param| pop_typed(param) }.reverse
|
|
499
|
+
dst = type.retsig.empty? ? -1 : register(type.retsig.fetch(0))
|
|
500
|
+
@stack << dst if dst >= 0
|
|
501
|
+
next unless @reachable
|
|
502
|
+
id = @function.dynamic_sites.size
|
|
503
|
+
@function.dynamic_sites << DynamicCallSite.new(-1, arguments, type.callsig, type.retsig, table_index: table_index)
|
|
504
|
+
emit([Opcode::CALL_INDIRECT, dst, id, selector])
|
|
505
|
+
when :throw
|
|
506
|
+
tag_index = integer_operand(operands[0])
|
|
507
|
+
tag = tag_at(tag_index)
|
|
508
|
+
@function.uses_eh = true
|
|
509
|
+
payload = tag.parameter_types.reverse.map { |type| pop_typed(type) }.reverse
|
|
510
|
+
if @reachable
|
|
511
|
+
id = @function.throw_sites.size
|
|
512
|
+
@function.throw_sites << ThrowSite.new(tag_index, payload)
|
|
513
|
+
emit([Opcode::THROW, id])
|
|
514
|
+
end
|
|
515
|
+
mark_unreachable
|
|
516
|
+
when :throw_ref
|
|
517
|
+
@function.uses_eh = true
|
|
518
|
+
reference = pop_typed(:exnref)
|
|
519
|
+
emit([Opcode::THROW_REF, reference]) if @reachable
|
|
520
|
+
mark_unreachable
|
|
521
|
+
when :return
|
|
522
|
+
source = @function.result_types.empty? ? -1 : pop_typed(@function.result_types.fetch(0))
|
|
523
|
+
emit([Opcode::RETURN, source]) if @reachable
|
|
524
|
+
mark_unreachable
|
|
525
|
+
when :unreachable
|
|
526
|
+
emit([Opcode::TRAP]) if @reachable
|
|
527
|
+
mark_unreachable
|
|
528
|
+
when :drop
|
|
529
|
+
pop_value
|
|
530
|
+
when :nop
|
|
531
|
+
else
|
|
532
|
+
opcode = I64_ALU[code] || FLOAT_ALU[code]
|
|
533
|
+
raise UnsupportedFeature, context("unsupported instruction #{code}") unless opcode
|
|
534
|
+
types = Opcode.extended_alu_types(opcode) || raise(TranslationError, context("missing ALU signature"))
|
|
535
|
+
if opcode >= Opcode::F32_EQ
|
|
536
|
+
@function.uses_float = true
|
|
537
|
+
else
|
|
538
|
+
@function.uses_i64 = true
|
|
539
|
+
end
|
|
540
|
+
sources = types.drop(1).reverse.map { |type| pop_typed(type) }.reverse
|
|
541
|
+
dst = register(types.fetch(0))
|
|
542
|
+
emit([opcode, dst] + sources) if @reachable
|
|
543
|
+
@stack << dst
|
|
544
|
+
end
|
|
545
|
+
end
|
|
546
|
+
fail_translation("missing function end") unless @controls.empty?
|
|
547
|
+
@function
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
private
|
|
551
|
+
|
|
552
|
+
# Validate even dead instructions before deciding whether to emit MIR.
|
|
553
|
+
# @rbs code: Symbol
|
|
554
|
+
# @rbs operands: Array[Wardite::operandItem]
|
|
555
|
+
# @rbs return: void
|
|
556
|
+
def validate_store_instruction(code, operands)
|
|
557
|
+
@function.uses_store = true
|
|
558
|
+
if code == :global_get || code == :global_set
|
|
559
|
+
index = integer_operand(operands[0])
|
|
560
|
+
global = index >= 0 ? @instance.store.globals[index] : nil
|
|
561
|
+
fail_translation("invalid global index #{index}") unless global
|
|
562
|
+
case global.type
|
|
563
|
+
when :i32
|
|
564
|
+
fail_translation("global initializer type mismatch") unless global.value.is_a?(Wardite::I32)
|
|
565
|
+
when :i64
|
|
566
|
+
fail_translation("global initializer type mismatch") unless global.value.is_a?(Wardite::I64)
|
|
567
|
+
when :f32
|
|
568
|
+
fail_translation("global initializer type mismatch") unless global.value.is_a?(Wardite::F32)
|
|
569
|
+
when :f64
|
|
570
|
+
fail_translation("global initializer type mismatch") unless global.value.is_a?(Wardite::F64)
|
|
571
|
+
else
|
|
572
|
+
raise UnsupportedFeature, context("only numeric global access is supported")
|
|
573
|
+
end
|
|
574
|
+
fail_translation("global.set on immutable global #{index}") if code == :global_set && !global.mutable
|
|
575
|
+
return
|
|
576
|
+
end
|
|
577
|
+
fail_translation("memory instruction requires memory 0") if @instance.store.memories.empty?
|
|
578
|
+
alignment = MEMORY_WIDTHS[code]
|
|
579
|
+
if alignment
|
|
580
|
+
hint = integer_operand(operands[0])
|
|
581
|
+
offset = integer_operand(operands[1])
|
|
582
|
+
fail_translation("invalid memory alignment") unless hint >= 0 && hint <= alignment
|
|
583
|
+
fail_translation("invalid memory offset") unless offset >= 0 && offset <= 0xffffffff
|
|
584
|
+
elsif code == :memory_init
|
|
585
|
+
data_index = integer_operand(operands[0])
|
|
586
|
+
fail_translation("memory.init requires data count") unless @instance.data_count_section
|
|
587
|
+
count = @instance.data_section&.segments&.size || 0
|
|
588
|
+
fail_translation("invalid data index #{data_index}") unless data_index >= 0 && data_index < count
|
|
589
|
+
check_memory_index(integer_operand(operands[1]))
|
|
590
|
+
elsif code == :memory_copy
|
|
591
|
+
check_memory_index(integer_operand(operands[0]))
|
|
592
|
+
check_memory_index(integer_operand(operands[1]))
|
|
593
|
+
else
|
|
594
|
+
check_memory_index(integer_operand(operands[0]))
|
|
595
|
+
end
|
|
596
|
+
end
|
|
597
|
+
|
|
598
|
+
# @rbs index: Integer
|
|
599
|
+
# @rbs return: void
|
|
600
|
+
def check_memory_index(index)
|
|
601
|
+
raise UnsupportedFeature, context("only memory 0 is supported") unless index == 0
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
# @rbs value: Wardite::operandItem?
|
|
605
|
+
# @rbs return: Integer
|
|
606
|
+
def integer_operand(value)
|
|
607
|
+
fail_translation("expected integer operand") unless value.is_a?(Integer)
|
|
608
|
+
value
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
# @rbs type: Symbol
|
|
612
|
+
# @rbs return: Integer
|
|
613
|
+
def register(type = :i32)
|
|
614
|
+
@function.allocate_register(0, type: type)
|
|
615
|
+
end
|
|
616
|
+
|
|
617
|
+
# @rbs value: Integer
|
|
618
|
+
# @rbs return: Integer
|
|
619
|
+
# @rbs type: Symbol
|
|
620
|
+
def constant(value, type = :i32)
|
|
621
|
+
constants = case type
|
|
622
|
+
when :i64 then @i64_constants ||= {} #: Hash[Integer, Integer]
|
|
623
|
+
when :f32 then @f32_constants ||= {} #: Hash[Integer, Integer]
|
|
624
|
+
when :f64 then @f64_constants ||= {} #: Hash[Integer, Integer]
|
|
625
|
+
else @constants
|
|
626
|
+
end
|
|
627
|
+
constants[value] ||= @function.allocate_constant(value, type: type)
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
# @rbs kind: Symbol
|
|
631
|
+
# @rbs types: Array[Symbol]
|
|
632
|
+
# @rbs return: Wardite::Efflorescence::Translator::Control
|
|
633
|
+
def new_control(kind, types)
|
|
634
|
+
Control.new(kind, @stack, @function.instructions.size, types, types.map { |type| register(type) }, @handler_depth, @reachable)
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
# @rbs instruction: Array[Integer]
|
|
638
|
+
# @rbs return: Integer
|
|
639
|
+
def emit(instruction)
|
|
640
|
+
position = @function.instructions.size
|
|
641
|
+
@function.instructions << Instruction.new(instruction)
|
|
642
|
+
position
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
# @rbs pc: Integer
|
|
646
|
+
# @rbs target: Integer
|
|
647
|
+
# @rbs return: void
|
|
648
|
+
def patch(pc, target)
|
|
649
|
+
instruction = @function.instructions.fetch(pc)
|
|
650
|
+
instruction[instruction.size - 1] = target
|
|
651
|
+
end
|
|
652
|
+
|
|
653
|
+
# @rbs return: Integer
|
|
654
|
+
def pop_value
|
|
655
|
+
control = @controls.last || raise(TranslationError, "missing control")
|
|
656
|
+
if @stack.size == control.entry_stack.size
|
|
657
|
+
return -1 if control.polymorphic
|
|
658
|
+
fail_translation("operand stack underflow")
|
|
659
|
+
end
|
|
660
|
+
@stack.pop || raise(TranslationError, "missing operand")
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
# @rbs register: Integer
|
|
664
|
+
# @rbs return: Symbol
|
|
665
|
+
def type_of(register)
|
|
666
|
+
register < 0 ? :unknown : @function.register_types.fetch(register)
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
# @rbs type: Symbol
|
|
670
|
+
# @rbs return: Integer
|
|
671
|
+
def pop_typed(type)
|
|
672
|
+
value = pop_value
|
|
673
|
+
fail_translation("expected #{type}, got #{type_of(value)}") unless value < 0 || type_of(value) == type
|
|
674
|
+
value
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
# @rbs destination: Integer
|
|
678
|
+
# @rbs source: Integer
|
|
679
|
+
# @rbs return: void
|
|
680
|
+
def copy_value(destination, source)
|
|
681
|
+
fail_translation("copy type mismatch") unless type_of(destination) == type_of(source)
|
|
682
|
+
emit([type_of(source) == :exnref ? Opcode::COPY_REF : Opcode::COPY, destination, source])
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
# @rbs destinations: Array[Integer]
|
|
686
|
+
# @rbs sources: Array[Integer]
|
|
687
|
+
# @rbs return: void
|
|
688
|
+
def parallel_copy(destinations, sources)
|
|
689
|
+
if sources.size > 1
|
|
690
|
+
sources = sources.map do |source|
|
|
691
|
+
temp = register(type_of(source))
|
|
692
|
+
copy_value(temp, source)
|
|
693
|
+
temp
|
|
694
|
+
end
|
|
695
|
+
end
|
|
696
|
+
destinations.each_with_index { |dst, i| copy_value(dst, sources.fetch(i)) }
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
# @rbs local: Integer
|
|
700
|
+
# @rbs return: void
|
|
701
|
+
def preserve_local(local)
|
|
702
|
+
return unless @stack.include?(local)
|
|
703
|
+
snapshot = register(type_of(local))
|
|
704
|
+
copy_value(snapshot, local)
|
|
705
|
+
@stack.map! { |source| source == local ? snapshot : source }
|
|
706
|
+
end
|
|
707
|
+
|
|
708
|
+
# @rbs return: void
|
|
709
|
+
def snapshot_locals
|
|
710
|
+
@stack.dup.each { |source| preserve_local(source) if source < @function.parameter_count + @function.local_count }
|
|
711
|
+
end
|
|
712
|
+
|
|
713
|
+
# @rbs depth: Integer
|
|
714
|
+
# @rbs return: Wardite::Efflorescence::Translator::Control
|
|
715
|
+
def label(depth)
|
|
716
|
+
fail_translation("invalid branch depth #{depth}") unless depth >= 0 && depth < @controls.size
|
|
717
|
+
@controls.fetch(@controls.size - 1 - depth)
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
# @rbs target: Wardite::Efflorescence::Translator::Control
|
|
721
|
+
# @rbs return: Array[Symbol]
|
|
722
|
+
def label_types(target)
|
|
723
|
+
target.kind == :loop ? [] : target.result_types
|
|
724
|
+
end
|
|
725
|
+
|
|
726
|
+
# @rbs types: Array[Symbol]
|
|
727
|
+
# @rbs return: Array[Integer]
|
|
728
|
+
def top_values(types)
|
|
729
|
+
return [] if types.empty?
|
|
730
|
+
|
|
731
|
+
control = @controls.last || raise(TranslationError, "missing control")
|
|
732
|
+
available = @stack.size - control.entry_stack.size
|
|
733
|
+
if available < types.size
|
|
734
|
+
fail_translation("operand stack underflow") unless control.polymorphic
|
|
735
|
+
values = Array.new(types.size - available, -1) + @stack.last(available)
|
|
736
|
+
else
|
|
737
|
+
values = @stack.last(types.size)
|
|
738
|
+
end
|
|
739
|
+
values.each_with_index do |value, i|
|
|
740
|
+
fail_translation("branch result type mismatch") if value >= 0 && type_of(value) != types.fetch(i)
|
|
741
|
+
end
|
|
742
|
+
values
|
|
743
|
+
end
|
|
744
|
+
|
|
745
|
+
# @rbs target: Wardite::Efflorescence::Translator::Control
|
|
746
|
+
# @rbs sources: Array[Integer]
|
|
747
|
+
# @rbs condition: Integer
|
|
748
|
+
# @rbs return: void
|
|
749
|
+
def branch(target, sources, condition = -1)
|
|
750
|
+
parallel_copy(target.result_registers, sources) unless target.kind == :loop
|
|
751
|
+
emit([Opcode::EH_TRIM, target.handler_depth]) if @handler_depth != target.handler_depth
|
|
752
|
+
pc = target.kind == :loop ? target.start_pc : -1
|
|
753
|
+
position = emit(condition < 0 ? [Opcode::BR, pc] : [Opcode::BR_IF, condition, pc])
|
|
754
|
+
unless target.kind == :loop
|
|
755
|
+
target.patches << position
|
|
756
|
+
target.has_branch = true
|
|
757
|
+
end
|
|
758
|
+
end
|
|
759
|
+
|
|
760
|
+
# @rbs index: Integer
|
|
761
|
+
# @rbs return: Wardite::Efflorescence::Tag
|
|
762
|
+
def tag_at(index)
|
|
763
|
+
fail_translation("invalid tag index #{index}") if index < 0
|
|
764
|
+
@instance.tags[index] || raise(TranslationError, context("invalid tag index #{index}"))
|
|
765
|
+
end
|
|
766
|
+
|
|
767
|
+
# Catch labels resolve before pushing the try_table's own label.
|
|
768
|
+
# @rbs clauses: Array[Integer]
|
|
769
|
+
# @rbs return: Wardite::Efflorescence::HandlerDescriptor
|
|
770
|
+
def make_handler(clauses)
|
|
771
|
+
targets = [] #: Array[Wardite::Efflorescence::CatchTarget]
|
|
772
|
+
clauses.each_slice(3) do |clause|
|
|
773
|
+
kind, tag_index, depth = clause.fetch(0), clause.fetch(1), clause.fetch(2)
|
|
774
|
+
target = label(depth)
|
|
775
|
+
types = kind <= 1 ? tag_at(tag_index).parameter_types.dup : [] #: Array[Symbol]
|
|
776
|
+
reference = kind == 1 || kind == 3
|
|
777
|
+
types << :exnref if reference
|
|
778
|
+
fail_translation("catch payload/label type mismatch") unless types == label_types(target)
|
|
779
|
+
destinations = target.kind == :loop ? [] : target.result_registers.dup #: Array[Integer]
|
|
780
|
+
ref = reference ? (destinations.pop || raise(TranslationError, "missing reference result")) : -1
|
|
781
|
+
entry = CatchTarget.new(tag_index, destinations, ref, target.kind == :loop ? target.start_pc : -1, target.handler_depth)
|
|
782
|
+
if target.kind != :loop
|
|
783
|
+
target.catch_patches << entry
|
|
784
|
+
target.has_branch = true if @reachable
|
|
785
|
+
end
|
|
786
|
+
targets << entry
|
|
787
|
+
end
|
|
788
|
+
HandlerDescriptor.new(targets)
|
|
789
|
+
end
|
|
790
|
+
|
|
791
|
+
# @rbs control: Wardite::Efflorescence::Translator::Control
|
|
792
|
+
# @rbs return: void
|
|
793
|
+
def finish_arm(control)
|
|
794
|
+
height = @stack.size - control.entry_stack.size
|
|
795
|
+
fail_translation("incorrect stack height at end") if height > control.result_types.size
|
|
796
|
+
sources = top_values(control.result_types)
|
|
797
|
+
parallel_copy(control.result_registers, sources) if @reachable && !sources.empty?
|
|
798
|
+
end
|
|
799
|
+
|
|
800
|
+
# @rbs return: void
|
|
801
|
+
def else_control
|
|
802
|
+
control = @controls.last || raise(TranslationError, "unexpected else")
|
|
803
|
+
fail_translation("unexpected else") unless control.kind == :if && !control.else_seen
|
|
804
|
+
finish_arm(control)
|
|
805
|
+
control.then_reachable = @reachable
|
|
806
|
+
control.patches << emit([Opcode::BR, -1]) if @reachable
|
|
807
|
+
patch(control.false_patch, @function.instructions.size) if control.false_patch >= 0
|
|
808
|
+
control.else_seen = true
|
|
809
|
+
control.polymorphic = false
|
|
810
|
+
@stack = control.entry_stack.dup
|
|
811
|
+
@reachable = control.entry_reachable
|
|
812
|
+
end
|
|
813
|
+
|
|
814
|
+
# @rbs return: void
|
|
815
|
+
def end_control
|
|
816
|
+
control = @controls.last || raise(TranslationError, "unexpected end")
|
|
817
|
+
finish_arm(control)
|
|
818
|
+
if control.kind == :try_table
|
|
819
|
+
@handler_depth = control.handler_depth
|
|
820
|
+
emit([Opcode::EH_TRIM, @handler_depth]) if @reachable
|
|
821
|
+
end
|
|
822
|
+
if control.kind == :if && !control.else_seen
|
|
823
|
+
fail_translation("result-bearing if requires else") unless control.result_types.empty?
|
|
824
|
+
patch(control.false_patch, @function.instructions.size) if control.false_patch >= 0
|
|
825
|
+
@reachable ||= control.entry_reachable
|
|
826
|
+
end
|
|
827
|
+
@controls.pop
|
|
828
|
+
pc = @function.instructions.size
|
|
829
|
+
control.patches.each { |position| patch(position, pc) }
|
|
830
|
+
control.catch_patches.each { |entry| entry.target_pc = pc }
|
|
831
|
+
@stack = control.entry_stack.dup + control.result_registers
|
|
832
|
+
@reachable = @reachable || control.has_branch || control.then_reachable
|
|
833
|
+
if control.kind == :function
|
|
834
|
+
emit([Opcode::RETURN, control.result_registers.empty? ? -1 : control.result_registers.fetch(0)])
|
|
835
|
+
end
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
# @rbs return: void
|
|
839
|
+
def mark_unreachable
|
|
840
|
+
control = @controls.last || raise(TranslationError, "missing control")
|
|
841
|
+
control.polymorphic = true
|
|
842
|
+
@stack = control.entry_stack.dup
|
|
843
|
+
@reachable = false
|
|
844
|
+
end
|
|
845
|
+
|
|
846
|
+
# @rbs message: String
|
|
847
|
+
# @rbs return: String
|
|
848
|
+
def context(message)
|
|
849
|
+
"Efflorescence: function #{@function.index}, Wasm PC #{@wasm_pc}: #{message}"
|
|
850
|
+
end
|
|
851
|
+
|
|
852
|
+
# @rbs message: String
|
|
853
|
+
# @rbs return: bot
|
|
854
|
+
def fail_translation(message)
|
|
855
|
+
raise TranslationError, context(message)
|
|
856
|
+
end
|
|
857
|
+
end
|
|
858
|
+
end
|
|
859
|
+
end
|