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.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +80 -1
  5. data/Rakefile +5 -0
  6. data/benchmark/README.md +500 -0
  7. data/benchmark/build_call_profile.rb +42 -0
  8. data/benchmark/call_cost_worker.rb +27 -0
  9. data/benchmark/compare.rb +93 -0
  10. data/benchmark/compare_call_types.rb +212 -0
  11. data/benchmark/compare_dispatch.rb +123 -0
  12. data/benchmark/compare_engines.rb +119 -0
  13. data/benchmark/compare_optimizations.rb +139 -0
  14. data/benchmark/engine_phases.rb +30 -0
  15. data/benchmark/figures/README.md +35 -0
  16. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  17. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  18. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  19. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  20. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  21. data/benchmark/float_m5.wat +42 -0
  22. data/benchmark/i32_call_arities.wat +89 -0
  23. data/benchmark/i32_calls.wat +174 -0
  24. data/benchmark/i32_memory.wat +58 -0
  25. data/benchmark/i32_mix.wat +80 -0
  26. data/benchmark/i64_m4.wat +55 -0
  27. data/benchmark/investigate_calls.rb +231 -0
  28. data/benchmark/investigate_generated_guards.rb +218 -0
  29. data/benchmark/m3_call_worker.rb +38 -0
  30. data/benchmark/measure_m1.rb +51 -0
  31. data/benchmark/measure_m2.rb +55 -0
  32. data/benchmark/measure_m3_calls.rb +89 -0
  33. data/benchmark/measure_m4.rb +134 -0
  34. data/benchmark/measure_m5_boundary.rb +41 -0
  35. data/benchmark/measure_wasi_write.rb +75 -0
  36. data/benchmark/optimization_worker.rb +23 -0
  37. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  38. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  39. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  40. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  41. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  42. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  43. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  44. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  45. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  46. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  47. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  48. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  49. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  50. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  51. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  52. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  53. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  54. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  55. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  56. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  57. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  58. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  59. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  60. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  61. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  62. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  63. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  64. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  65. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  66. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  67. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  68. data/benchmark/summarize_call_profile.py +54 -0
  69. data/benchmark/validate_m1_eh.rb +34 -0
  70. data/benchmark/validate_m2.rb +119 -0
  71. data/benchmark/validate_m3.rb +44 -0
  72. data/benchmark/validate_m4.rb +95 -0
  73. data/benchmark/validate_m5.rb +108 -0
  74. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  75. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  76. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  77. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  78. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  79. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  80. data/docs/efflorescence-call-arities.md +115 -0
  81. data/docs/efflorescence-call-investigation.md +155 -0
  82. data/docs/efflorescence-call-transfer.md +128 -0
  83. data/docs/efflorescence-call-types.md +102 -0
  84. data/docs/efflorescence-coverage-ja.md +351 -0
  85. data/docs/efflorescence-generated-c-guards.md +405 -0
  86. data/docs/efflorescence-inlining.md +165 -0
  87. data/docs/efflorescence-ja.md +155 -0
  88. data/docs/efflorescence-m1.md +160 -0
  89. data/docs/efflorescence-m2.md +233 -0
  90. data/docs/efflorescence-m3.md +131 -0
  91. data/docs/efflorescence-m4.md +88 -0
  92. data/docs/efflorescence-m5.md +82 -0
  93. data/docs/efflorescence-m6.md +95 -0
  94. data/docs/efflorescence-operand-decoding.md +105 -0
  95. data/docs/efflorescence-phase2-ja.md +225 -0
  96. data/docs/efflorescence-phase2.md +240 -0
  97. data/docs/efflorescence-register-initialization.md +75 -0
  98. data/docs/efflorescence.md +144 -0
  99. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  100. data/examples/load_perf.rb +24 -0
  101. data/lib/wardite/alu_f32.generated.rb +13 -10
  102. data/lib/wardite/alu_f64.generated.rb +13 -10
  103. data/lib/wardite/alu_i32.generated.rb +38 -31
  104. data/lib/wardite/alu_i64.generated.rb +48 -48
  105. data/lib/wardite/cli/cli.rb +73 -22
  106. data/lib/wardite/const.rb +1 -0
  107. data/lib/wardite/convert.generated.rb +7 -5
  108. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  109. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  110. data/lib/wardite/efflorescence/inspection.rb +165 -0
  111. data/lib/wardite/efflorescence/ir.rb +1151 -0
  112. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  113. data/lib/wardite/efflorescence/register_validation.rb +290 -0
  114. data/lib/wardite/efflorescence/store_access.rb +271 -0
  115. data/lib/wardite/efflorescence/translator.rb +859 -0
  116. data/lib/wardite/efflorescence/vm.rb +1006 -0
  117. data/lib/wardite/efflorescence/wasi.rb +108 -0
  118. data/lib/wardite/efflorescence.rb +11 -0
  119. data/lib/wardite/errors.rb +20 -0
  120. data/lib/wardite/float_bits.rb +46 -0
  121. data/lib/wardite/instruction.rb +460 -69
  122. data/lib/wardite/leb128.rb +35 -17
  123. data/lib/wardite/load.rb +260 -159
  124. data/lib/wardite/revisitor.rb +12 -4
  125. data/lib/wardite/value.rb +169 -175
  126. data/lib/wardite/version.rb +1 -1
  127. data/lib/wardite/wasi/consts.rb +1 -0
  128. data/lib/wardite/wasm_module.rb +6 -3
  129. data/lib/wardite.rb +220 -105
  130. data/rbs_collection.lock.yaml +36 -96
  131. data/scripts/gen_alu.rb +90 -34
  132. data/scripts/templates/alu_module.rb.tmpl +7 -5
  133. data/scripts/templates/conv_module.rb.tmpl +7 -5
  134. data/sig/generated/wardite/alu_f32.generated.rbs +4 -3
  135. data/sig/generated/wardite/alu_f64.generated.rbs +4 -3
  136. data/sig/generated/wardite/alu_i32.generated.rbs +4 -3
  137. data/sig/generated/wardite/alu_i64.generated.rbs +4 -3
  138. data/sig/generated/wardite/cli/cli.rbs +13 -6
  139. data/sig/generated/wardite/const.rbs +2 -0
  140. data/sig/generated/wardite/convert.generated.rbs +4 -3
  141. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  142. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  143. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  144. data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
  145. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  146. data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
  147. data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
  148. data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
  149. data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
  150. data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
  151. data/sig/generated/wardite/errors.rbs +12 -0
  152. data/sig/generated/wardite/float_bits.rbs +30 -0
  153. data/sig/generated/wardite/instruction.rbs +35 -10
  154. data/sig/generated/wardite/leb128.rbs +3 -1
  155. data/sig/generated/wardite/load.rbs +38 -13
  156. data/sig/generated/wardite/value.rbs +70 -11
  157. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  158. data/sig/generated/wardite.rbs +55 -18
  159. data/sig/vernier.rbs +3 -0
  160. data/spinel/README.md +30 -0
  161. data/spinel/bin/wardite.rb +6 -0
  162. data/spinel/spin.toml +7 -0
  163. metadata +128 -3
@@ -0,0 +1,1151 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ class UnsupportedFeature < Wardite::LoadError; end
7
+ class TranslationError < Wardite::LoadError; end
8
+
9
+ module Opcode
10
+ COPY = 0
11
+ I32_ADD = 1
12
+ I32_SUB = 2
13
+ I32_MUL = 3
14
+ I32_XOR = 4
15
+ I32_SHL = 5
16
+ I32_SHR_U = 6
17
+ I32_EQZ = 7
18
+ BR = 8
19
+ BR_IF = 9
20
+ CALL = 10
21
+ RETURN = 11
22
+ TRAP = 12
23
+ I32_ADD_IMM = 13
24
+ I32_SUB_IMM = 14
25
+ I32_MUL_IMM = 15
26
+ I32_XOR_IMM = 16
27
+ I32_SHL_IMM = 17
28
+ I32_SHR_U_IMM = 18
29
+ BR_IF_ZERO = 19
30
+ I32_XOR_SHR_U_IMM = 20
31
+ I32_XOR_SHL_IMM = 21
32
+ PROFILE = 22
33
+
34
+ I32_EQ = 23
35
+ I32_NE = 24
36
+ I32_LTS = 25
37
+ I32_LTU = 26
38
+ I32_GTS = 27
39
+ I32_GTU = 28
40
+ I32_LES = 29
41
+ I32_LEU = 30
42
+ I32_GES = 31
43
+ I32_GEU = 32
44
+ I32_CLZ = 33
45
+ I32_CTZ = 34
46
+ I32_POPCNT = 35
47
+ I32_DIV_S = 36
48
+ I32_DIV_U = 37
49
+ I32_REM_S = 38
50
+ I32_REM_U = 39
51
+ I32_AND = 40
52
+ I32_OR = 41
53
+ I32_SHR_S = 42
54
+ I32_ROTL = 43
55
+ I32_ROTR = 44
56
+ I32_EXTEND8_S = 45
57
+ I32_EXTEND16_S = 46
58
+ COPY_REF = 47
59
+ EH_ENTER = 48
60
+ EH_TRIM = 49
61
+ THROW = 50
62
+ THROW_REF = 51
63
+
64
+ GLOBAL_GET = 52
65
+ GLOBAL_SET = 53
66
+ I32_LOAD = 54
67
+ I32_LOAD8_S = 55
68
+ I32_LOAD8_U = 56
69
+ I32_LOAD16_S = 57
70
+ I32_LOAD16_U = 58
71
+ I32_STORE = 59
72
+ I32_STORE8 = 60
73
+ I32_STORE16 = 61
74
+ MEMORY_SIZE = 62
75
+ MEMORY_GROW = 63
76
+ MEMORY_INIT = 64
77
+ MEMORY_COPY = 65
78
+ MEMORY_FILL = 66
79
+ CALL_HOST = 67
80
+ CALL_INDIRECT = 68
81
+
82
+ I64_EQZ = 69
83
+ I32_WRAP_I64 = 70
84
+ I64_EQ = 71
85
+ I64_NE = 72
86
+ I64_LTS = 73
87
+ I64_LTU = 74
88
+ I64_GTS = 75
89
+ I64_GTU = 76
90
+ I64_LES = 77
91
+ I64_LEU = 78
92
+ I64_GES = 79
93
+ I64_GEU = 80
94
+ I64_CLZ = 81
95
+ I64_CTZ = 82
96
+ I64_POPCNT = 83
97
+ I64_EXTEND8_S = 84
98
+ I64_EXTEND16_S = 85
99
+ I64_EXTEND32_S = 86
100
+ I64_EXTEND_I32_S = 87
101
+ I64_EXTEND_I32_U = 88
102
+ I64_AND = 89
103
+ I64_OR = 90
104
+ I64_XOR = 91
105
+ I64_SHL = 92
106
+ I64_SHR_S = 93
107
+ I64_SHR_U = 94
108
+ I64_ROTL = 95
109
+ I64_ROTR = 96
110
+
111
+ I64_ADD = 97
112
+ I64_SUB = 98
113
+ I64_MUL = 99
114
+ I64_DIV_S = 100
115
+ I64_DIV_U = 101
116
+ I64_REM_S = 102
117
+ I64_REM_U = 103
118
+
119
+ I64_LOAD = 104
120
+ I64_LOAD8_S = 105
121
+ I64_LOAD8_U = 106
122
+ I64_LOAD16_S = 107
123
+ I64_LOAD16_U = 108
124
+ I64_LOAD32_S = 109
125
+ I64_LOAD32_U = 110
126
+ I64_STORE = 111
127
+ I64_STORE8 = 112
128
+ I64_STORE16 = 113
129
+ I64_STORE32 = 114
130
+ I64_GLOBAL_GET = 115
131
+ I64_GLOBAL_SET = 116
132
+
133
+ F32_EQ = 117
134
+ F32_NE = 118
135
+ F32_LT = 119
136
+ F32_GT = 120
137
+ F32_LE = 121
138
+ F32_GE = 122
139
+ F32_ABS = 123
140
+ F32_NEG = 124
141
+ F32_CEIL = 125
142
+ F32_FLOOR = 126
143
+ F32_TRUNC = 127
144
+ F32_NEAREST = 128
145
+ F32_SQRT = 129
146
+ F32_ADD = 130
147
+ F32_SUB = 131
148
+ F32_MUL = 132
149
+ F32_DIV = 133
150
+ F32_MIN = 134
151
+ F32_MAX = 135
152
+ F32_COPYSIGN = 136
153
+ F64_EQ = 137
154
+ F64_NE = 138
155
+ F64_LT = 139
156
+ F64_GT = 140
157
+ F64_LE = 141
158
+ F64_GE = 142
159
+ F64_ABS = 143
160
+ F64_NEG = 144
161
+ F64_CEIL = 145
162
+ F64_FLOOR = 146
163
+ F64_TRUNC = 147
164
+ F64_NEAREST = 148
165
+ F64_SQRT = 149
166
+ F64_ADD = 150
167
+ F64_SUB = 151
168
+ F64_MUL = 152
169
+ F64_DIV = 153
170
+ F64_MIN = 154
171
+ F64_MAX = 155
172
+ F64_COPYSIGN = 156
173
+
174
+ I32_TRUNC_F32_S = 157
175
+ I32_TRUNC_F32_U = 158
176
+ I32_TRUNC_F64_S = 159
177
+ I32_TRUNC_F64_U = 160
178
+ I64_TRUNC_F32_S = 161
179
+ I64_TRUNC_F32_U = 162
180
+ I64_TRUNC_F64_S = 163
181
+ I64_TRUNC_F64_U = 164
182
+ I32_TRUNC_SAT_F32_S = 165
183
+ I32_TRUNC_SAT_F32_U = 166
184
+ I32_TRUNC_SAT_F64_S = 167
185
+ I32_TRUNC_SAT_F64_U = 168
186
+ I64_TRUNC_SAT_F32_S = 169
187
+ I64_TRUNC_SAT_F32_U = 170
188
+ I64_TRUNC_SAT_F64_S = 171
189
+ I64_TRUNC_SAT_F64_U = 172
190
+ F32_CONVERT_I32_S = 173
191
+ F32_CONVERT_I32_U = 174
192
+ F32_CONVERT_I64_S = 175
193
+ F32_CONVERT_I64_U = 176
194
+ F64_CONVERT_I32_S = 177
195
+ F64_CONVERT_I32_U = 178
196
+ F64_CONVERT_I64_S = 179
197
+ F64_CONVERT_I64_U = 180
198
+ F32_DEMOTE_F64 = 181
199
+ F64_PROMOTE_F32 = 182
200
+ I32_REINTERPRET_F32 = 183
201
+ I64_REINTERPRET_F64 = 184
202
+ F32_REINTERPRET_I32 = 185
203
+ F64_REINTERPRET_I64 = 186
204
+
205
+ F32_LOAD = 187
206
+ F64_LOAD = 188
207
+ F32_STORE = 189
208
+ F64_STORE = 190
209
+ F32_GLOBAL_GET = 191
210
+ F64_GLOBAL_GET = 192
211
+ F32_GLOBAL_SET = 193
212
+ F64_GLOBAL_SET = 194
213
+
214
+ NAMES = %w[COPY I32_ADD I32_SUB I32_MUL I32_XOR I32_SHL I32_SHR_U I32_EQZ
215
+ BR BR_IF CALL RETURN TRAP I32_ADD_IMM I32_SUB_IMM I32_MUL_IMM
216
+ I32_XOR_IMM I32_SHL_IMM I32_SHR_U_IMM BR_IF_ZERO
217
+ I32_XOR_SHR_U_IMM I32_XOR_SHL_IMM PROFILE
218
+ I32_EQ I32_NE I32_LTS I32_LTU I32_GTS I32_GTU I32_LES I32_LEU I32_GES I32_GEU I32_CLZ I32_CTZ I32_POPCNT I32_DIV_S I32_DIV_U I32_REM_S I32_REM_U I32_AND I32_OR I32_SHR_S I32_ROTL I32_ROTR I32_EXTEND8_S I32_EXTEND16_S COPY_REF EH_ENTER EH_TRIM THROW THROW_REF
219
+ GLOBAL_GET GLOBAL_SET I32_LOAD I32_LOAD8_S I32_LOAD8_U I32_LOAD16_S I32_LOAD16_U I32_STORE I32_STORE8 I32_STORE16 MEMORY_SIZE MEMORY_GROW MEMORY_INIT MEMORY_COPY MEMORY_FILL CALL_HOST CALL_INDIRECT
220
+ I64_EQZ I32_WRAP_I64
221
+ I64_EQ I64_NE I64_LTS I64_LTU I64_GTS I64_GTU I64_LES I64_LEU I64_GES I64_GEU
222
+ I64_CLZ I64_CTZ I64_POPCNT I64_EXTEND8_S I64_EXTEND16_S I64_EXTEND32_S
223
+ I64_EXTEND_I32_S I64_EXTEND_I32_U
224
+ I64_AND I64_OR I64_XOR I64_SHL I64_SHR_S I64_SHR_U I64_ROTL I64_ROTR
225
+ I64_ADD I64_SUB I64_MUL I64_DIV_S I64_DIV_U I64_REM_S I64_REM_U
226
+ I64_LOAD I64_LOAD8_S I64_LOAD8_U I64_LOAD16_S I64_LOAD16_U I64_LOAD32_S I64_LOAD32_U
227
+ I64_STORE I64_STORE8 I64_STORE16 I64_STORE32 I64_GLOBAL_GET I64_GLOBAL_SET
228
+ F32_EQ F32_NE F32_LT F32_GT F32_LE F32_GE F32_ABS F32_NEG
229
+ F32_CEIL F32_FLOOR F32_TRUNC F32_NEAREST F32_SQRT F32_ADD F32_SUB F32_MUL
230
+ F32_DIV F32_MIN F32_MAX F32_COPYSIGN F64_EQ F64_NE F64_LT F64_GT
231
+ F64_LE F64_GE F64_ABS F64_NEG F64_CEIL F64_FLOOR F64_TRUNC F64_NEAREST
232
+ F64_SQRT F64_ADD F64_SUB F64_MUL F64_DIV F64_MIN F64_MAX F64_COPYSIGN
233
+ I32_TRUNC_F32_S I32_TRUNC_F32_U I32_TRUNC_F64_S I32_TRUNC_F64_U
234
+ I64_TRUNC_F32_S I64_TRUNC_F32_U I64_TRUNC_F64_S I64_TRUNC_F64_U
235
+ I32_TRUNC_SAT_F32_S I32_TRUNC_SAT_F32_U I32_TRUNC_SAT_F64_S I32_TRUNC_SAT_F64_U
236
+ I64_TRUNC_SAT_F32_S I64_TRUNC_SAT_F32_U I64_TRUNC_SAT_F64_S I64_TRUNC_SAT_F64_U
237
+ F32_CONVERT_I32_S F32_CONVERT_I32_U F32_CONVERT_I64_S F32_CONVERT_I64_U
238
+ F64_CONVERT_I32_S F64_CONVERT_I32_U F64_CONVERT_I64_S F64_CONVERT_I64_U
239
+ F32_DEMOTE_F64 F64_PROMOTE_F32 I32_REINTERPRET_F32 I64_REINTERPRET_F64
240
+ F32_REINTERPRET_I32 F64_REINTERPRET_I64
241
+ F32_LOAD F64_LOAD F32_STORE F64_STORE
242
+ F32_GLOBAL_GET F64_GLOBAL_GET F32_GLOBAL_SET F64_GLOBAL_SET].freeze
243
+
244
+ # Each signature lists the destination type followed by the source types.
245
+ I64_ALU_TYPES = {
246
+ I64_EQZ => [:i32, :i64],
247
+ I32_WRAP_I64 => [:i32, :i64],
248
+ I64_EQ => [:i32, :i64, :i64],
249
+ I64_NE => [:i32, :i64, :i64],
250
+ I64_LTS => [:i32, :i64, :i64],
251
+ I64_LTU => [:i32, :i64, :i64],
252
+ I64_GTS => [:i32, :i64, :i64],
253
+ I64_GTU => [:i32, :i64, :i64],
254
+ I64_LES => [:i32, :i64, :i64],
255
+ I64_LEU => [:i32, :i64, :i64],
256
+ I64_GES => [:i32, :i64, :i64],
257
+ I64_GEU => [:i32, :i64, :i64],
258
+ I64_CLZ => [:i64, :i64],
259
+ I64_CTZ => [:i64, :i64],
260
+ I64_POPCNT => [:i64, :i64],
261
+ I64_EXTEND8_S => [:i64, :i64],
262
+ I64_EXTEND16_S => [:i64, :i64],
263
+ I64_EXTEND32_S => [:i64, :i64],
264
+ I64_EXTEND_I32_S => [:i64, :i32],
265
+ I64_EXTEND_I32_U => [:i64, :i32],
266
+ I64_AND => [:i64, :i64, :i64],
267
+ I64_OR => [:i64, :i64, :i64],
268
+ I64_XOR => [:i64, :i64, :i64],
269
+ I64_SHL => [:i64, :i64, :i64],
270
+ I64_SHR_S => [:i64, :i64, :i64],
271
+ I64_SHR_U => [:i64, :i64, :i64],
272
+ I64_ROTL => [:i64, :i64, :i64],
273
+ I64_ROTR => [:i64, :i64, :i64],
274
+ I64_ADD => [:i64, :i64, :i64],
275
+ I64_SUB => [:i64, :i64, :i64],
276
+ I64_MUL => [:i64, :i64, :i64],
277
+ I64_DIV_S => [:i64, :i64, :i64],
278
+ I64_DIV_U => [:i64, :i64, :i64],
279
+ I64_REM_S => [:i64, :i64, :i64],
280
+ I64_REM_U => [:i64, :i64, :i64],
281
+ }.freeze #: Hash[Integer, Array[Symbol]]
282
+
283
+ FLOAT_ALU_TYPES = {
284
+ F32_EQ => [:i32, :f32, :f32],
285
+ F32_NE => [:i32, :f32, :f32],
286
+ F32_LT => [:i32, :f32, :f32],
287
+ F32_GT => [:i32, :f32, :f32],
288
+ F32_LE => [:i32, :f32, :f32],
289
+ F32_GE => [:i32, :f32, :f32],
290
+ F32_ABS => [:f32, :f32],
291
+ F32_NEG => [:f32, :f32],
292
+ F32_CEIL => [:f32, :f32],
293
+ F32_FLOOR => [:f32, :f32],
294
+ F32_TRUNC => [:f32, :f32],
295
+ F32_NEAREST => [:f32, :f32],
296
+ F32_SQRT => [:f32, :f32],
297
+ F32_ADD => [:f32, :f32, :f32],
298
+ F32_SUB => [:f32, :f32, :f32],
299
+ F32_MUL => [:f32, :f32, :f32],
300
+ F32_DIV => [:f32, :f32, :f32],
301
+ F32_MIN => [:f32, :f32, :f32],
302
+ F32_MAX => [:f32, :f32, :f32],
303
+ F32_COPYSIGN => [:f32, :f32, :f32],
304
+ F64_EQ => [:i32, :f64, :f64],
305
+ F64_NE => [:i32, :f64, :f64],
306
+ F64_LT => [:i32, :f64, :f64],
307
+ F64_GT => [:i32, :f64, :f64],
308
+ F64_LE => [:i32, :f64, :f64],
309
+ F64_GE => [:i32, :f64, :f64],
310
+ F64_ABS => [:f64, :f64],
311
+ F64_NEG => [:f64, :f64],
312
+ F64_CEIL => [:f64, :f64],
313
+ F64_FLOOR => [:f64, :f64],
314
+ F64_TRUNC => [:f64, :f64],
315
+ F64_NEAREST => [:f64, :f64],
316
+ F64_SQRT => [:f64, :f64],
317
+ F64_ADD => [:f64, :f64, :f64],
318
+ F64_SUB => [:f64, :f64, :f64],
319
+ F64_MUL => [:f64, :f64, :f64],
320
+ F64_DIV => [:f64, :f64, :f64],
321
+ F64_MIN => [:f64, :f64, :f64],
322
+ F64_MAX => [:f64, :f64, :f64],
323
+ F64_COPYSIGN => [:f64, :f64, :f64],
324
+ I32_TRUNC_F32_S => [:i32, :f32],
325
+ I32_TRUNC_F32_U => [:i32, :f32],
326
+ I32_TRUNC_F64_S => [:i32, :f64],
327
+ I32_TRUNC_F64_U => [:i32, :f64],
328
+ I64_TRUNC_F32_S => [:i64, :f32],
329
+ I64_TRUNC_F32_U => [:i64, :f32],
330
+ I64_TRUNC_F64_S => [:i64, :f64],
331
+ I64_TRUNC_F64_U => [:i64, :f64],
332
+ I32_TRUNC_SAT_F32_S => [:i32, :f32],
333
+ I32_TRUNC_SAT_F32_U => [:i32, :f32],
334
+ I32_TRUNC_SAT_F64_S => [:i32, :f64],
335
+ I32_TRUNC_SAT_F64_U => [:i32, :f64],
336
+ I64_TRUNC_SAT_F32_S => [:i64, :f32],
337
+ I64_TRUNC_SAT_F32_U => [:i64, :f32],
338
+ I64_TRUNC_SAT_F64_S => [:i64, :f64],
339
+ I64_TRUNC_SAT_F64_U => [:i64, :f64],
340
+ F32_CONVERT_I32_S => [:f32, :i32],
341
+ F32_CONVERT_I32_U => [:f32, :i32],
342
+ F32_CONVERT_I64_S => [:f32, :i64],
343
+ F32_CONVERT_I64_U => [:f32, :i64],
344
+ F64_CONVERT_I32_S => [:f64, :i32],
345
+ F64_CONVERT_I32_U => [:f64, :i32],
346
+ F64_CONVERT_I64_S => [:f64, :i64],
347
+ F64_CONVERT_I64_U => [:f64, :i64],
348
+ F32_DEMOTE_F64 => [:f32, :f64],
349
+ F64_PROMOTE_F32 => [:f64, :f32],
350
+ I32_REINTERPRET_F32 => [:i32, :f32],
351
+ I64_REINTERPRET_F64 => [:i64, :f64],
352
+ F32_REINTERPRET_I32 => [:f32, :i32],
353
+ F64_REINTERPRET_I64 => [:f64, :i64],
354
+ }.freeze #: Hash[Integer, Array[Symbol]]
355
+
356
+ # @rbs opcode: Integer
357
+ # @rbs return: Array[Symbol]?
358
+ def self.extended_alu_types(opcode)
359
+ opcode >= F32_EQ ? FLOAT_ALU_TYPES[opcode] : I64_ALU_TYPES[opcode]
360
+ end
361
+
362
+ EXTENDED_LOAD_TYPES = {
363
+ I64_LOAD => :i64,
364
+ I64_LOAD8_S => :i64,
365
+ I64_LOAD8_U => :i64,
366
+ I64_LOAD16_S => :i64,
367
+ I64_LOAD16_U => :i64,
368
+ I64_LOAD32_S => :i64,
369
+ I64_LOAD32_U => :i64,
370
+ F32_LOAD => :f32,
371
+ F64_LOAD => :f64,
372
+ }.freeze #: Hash[Integer, Symbol]
373
+
374
+ EXTENDED_STORE_TYPES = {
375
+ I64_STORE => :i64,
376
+ I64_STORE8 => :i64,
377
+ I64_STORE16 => :i64,
378
+ I64_STORE32 => :i64,
379
+ F32_STORE => :f32,
380
+ F64_STORE => :f64,
381
+ }.freeze #: Hash[Integer, Symbol]
382
+
383
+ EXTENDED_GLOBAL_GET_TYPES = {
384
+ I64_GLOBAL_GET => :i64,
385
+ F32_GLOBAL_GET => :f32,
386
+ F64_GLOBAL_GET => :f64,
387
+ }.freeze #: Hash[Integer, Symbol]
388
+
389
+ EXTENDED_GLOBAL_SET_TYPES = {
390
+ I64_GLOBAL_SET => :i64,
391
+ F32_GLOBAL_SET => :f32,
392
+ F64_GLOBAL_SET => :f64,
393
+ }.freeze #: Hash[Integer, Symbol]
394
+
395
+ # These are semantic register accesses, excluding frame initialization.
396
+ # @rbs instruction: Wardite::Efflorescence::Instruction
397
+ # @rbs return: Array[Integer]
398
+ def self.read_positions(instruction)
399
+ opcode = instruction[0]
400
+ case opcode
401
+ when GLOBAL_SET, I32_LOAD, I32_LOAD8_S, I32_LOAD8_U, I32_LOAD16_S, I32_LOAD16_U, MEMORY_GROW,
402
+ COPY, COPY_REF, I32_EQZ, I32_CLZ, I32_CTZ, I32_POPCNT, I32_EXTEND8_S, I32_EXTEND16_S, I32_ADD_IMM, I32_SUB_IMM, I32_MUL_IMM,
403
+ I32_XOR_IMM, I32_SHL_IMM, I32_SHR_U_IMM, I32_XOR_SHR_U_IMM, I32_XOR_SHL_IMM
404
+ [2]
405
+ when I32_ADD, I32_SUB, I32_MUL, I32_XOR, I32_SHL, I32_SHR_U,
406
+ I32_EQ, I32_NE, I32_LTS, I32_LTU, I32_GTS, I32_GTU, I32_LES, I32_LEU, I32_GES, I32_GEU,
407
+ I32_DIV_S, I32_DIV_U, I32_REM_S, I32_REM_U, I32_AND, I32_OR, I32_SHR_S, I32_ROTL, I32_ROTR
408
+ [2, 3]
409
+ when BR_IF, BR_IF_ZERO
410
+ [1]
411
+ when CALL
412
+ (3...instruction.size).to_a
413
+ when CALL_INDIRECT
414
+ [3]
415
+ when RETURN
416
+ instruction[1] < 0 ? [] : [1]
417
+ when I32_STORE, I32_STORE8, I32_STORE16
418
+ [1, 2]
419
+ when THROW_REF
420
+ [1]
421
+ when BR, TRAP, PROFILE, EH_ENTER, EH_TRIM, THROW, GLOBAL_GET, MEMORY_SIZE, MEMORY_INIT, MEMORY_COPY, MEMORY_FILL, CALL_HOST
422
+ []
423
+ else
424
+ types = extended_alu_types(opcode)
425
+ return types.size == 2 ? [2] : [2, 3] if types
426
+ return [2] if EXTENDED_LOAD_TYPES.key?(opcode) || EXTENDED_GLOBAL_SET_TYPES.key?(opcode)
427
+ return [1, 2] if EXTENDED_STORE_TYPES.key?(opcode)
428
+ return [] if EXTENDED_GLOBAL_GET_TYPES.key?(opcode)
429
+
430
+ raise UnsupportedFeature, "Efflorescence: unknown MIR opcode #{opcode}"
431
+ end
432
+ end
433
+
434
+ # @rbs instruction: Wardite::Efflorescence::Instruction
435
+ # @rbs return: Integer
436
+ def self.destination(instruction)
437
+ opcode = instruction[0]
438
+ if opcode <= I32_EQZ || opcode == CALL || dynamic_call?(opcode) ||
439
+ (opcode >= I32_ADD_IMM && opcode <= I32_SHR_U_IMM) ||
440
+ opcode == I32_XOR_SHR_U_IMM || opcode == I32_XOR_SHL_IMM ||
441
+ (opcode >= I32_EQ && opcode <= COPY_REF) || opcode == GLOBAL_GET ||
442
+ (opcode >= I32_LOAD && opcode <= I32_LOAD16_U) || opcode == MEMORY_SIZE || opcode == MEMORY_GROW ||
443
+ (opcode > CALL_INDIRECT && (extended_alu_types(opcode) || EXTENDED_LOAD_TYPES.key?(opcode) || EXTENDED_GLOBAL_GET_TYPES.key?(opcode)))
444
+ instruction[1]
445
+ else
446
+ -1
447
+ end
448
+ end
449
+
450
+ # @rbs opcode: Integer
451
+ # @rbs return: bool
452
+ def self.may_throw?(opcode)
453
+ opcode == CALL || opcode == CALL_INDIRECT || opcode == THROW || opcode == THROW_REF
454
+ end
455
+
456
+ # @rbs opcode: Integer
457
+ # @rbs return: bool
458
+ def self.may_trap?(opcode)
459
+ opcode == TRAP || opcode == CALL_INDIRECT || opcode == THROW_REF || (opcode >= I32_DIV_S && opcode <= I32_REM_U) ||
460
+ (opcode >= I32_LOAD && opcode <= I32_STORE16) || memory_site?(opcode) ||
461
+ (opcode >= I32_TRUNC_F32_S && opcode <= I64_TRUNC_F64_U) ||
462
+ (opcode > CALL_INDIRECT && (EXTENDED_LOAD_TYPES.key?(opcode) || EXTENDED_STORE_TYPES.key?(opcode) ||
463
+ opcode == I64_DIV_S || opcode == I64_DIV_U || opcode == I64_REM_S || opcode == I64_REM_U))
464
+ end
465
+
466
+ # @rbs opcode: Integer
467
+ # @rbs return: bool
468
+ def self.side_effect?(opcode)
469
+ dynamic_call?(opcode) || may_throw?(opcode) || may_trap?(opcode) || reads_store?(opcode) || writes_store?(opcode) ||
470
+ [BR, BR_IF, BR_IF_ZERO, RETURN, EH_ENTER, EH_TRIM, PROFILE].include?(opcode)
471
+ end
472
+
473
+ # @rbs opcode: Integer
474
+ # @rbs return: bool
475
+ def self.dynamic_call?(opcode)
476
+ opcode == CALL_HOST || opcode == CALL_INDIRECT
477
+ end
478
+
479
+ # @rbs opcode: Integer
480
+ # @rbs return: bool
481
+ def self.memory_site?(opcode)
482
+ opcode == MEMORY_INIT || opcode == MEMORY_COPY || opcode == MEMORY_FILL
483
+ end
484
+
485
+ # @rbs opcode: Integer
486
+ # @rbs return: bool
487
+ def self.reads_store?(opcode)
488
+ opcode == GLOBAL_GET || (opcode >= I32_LOAD && opcode <= I32_LOAD16_U) ||
489
+ opcode == MEMORY_SIZE || opcode == MEMORY_GROW || opcode == MEMORY_INIT || opcode == MEMORY_COPY ||
490
+ (opcode > CALL_INDIRECT && (EXTENDED_GLOBAL_GET_TYPES.key?(opcode) || EXTENDED_LOAD_TYPES.key?(opcode)))
491
+ end
492
+
493
+ # @rbs opcode: Integer
494
+ # @rbs return: bool
495
+ def self.writes_store?(opcode)
496
+ opcode == GLOBAL_SET || (opcode >= I32_STORE && opcode <= I32_STORE16) ||
497
+ opcode == MEMORY_GROW || memory_site?(opcode) ||
498
+ (opcode > CALL_INDIRECT && (EXTENDED_GLOBAL_SET_TYPES.key?(opcode) || EXTENDED_STORE_TYPES.key?(opcode)))
499
+ end
500
+
501
+ # @rbs opcode: Integer
502
+ # @rbs return: bool
503
+ def self.terminal?(opcode)
504
+ [BR, RETURN, TRAP, THROW, THROW_REF].include?(opcode)
505
+ end
506
+
507
+ # @rbs instruction: Wardite::Efflorescence::Instruction
508
+ # @rbs return: Integer
509
+ def self.target_position(instruction)
510
+ case instruction[0]
511
+ when BR then 1
512
+ when BR_IF, BR_IF_ZERO then 2
513
+ else -1
514
+ end
515
+ end
516
+ end
517
+
518
+ class Tag
519
+ attr_reader :parameter_types #: Array[Symbol]
520
+
521
+ # @rbs parameter_types: Array[Symbol]
522
+ def initialize(parameter_types)
523
+ @parameter_types = parameter_types.dup.freeze
524
+ end
525
+ end
526
+
527
+ class ExceptionValue
528
+ attr_reader :tag #: Wardite::Efflorescence::Tag
529
+ attr_reader :payload_bits #: Array[Integer]
530
+
531
+ # @rbs tag: Wardite::Efflorescence::Tag
532
+ # @rbs payload_bits: Array[Integer]
533
+ def initialize(tag, payload_bits)
534
+ @tag = tag
535
+ @payload_bits = payload_bits.dup.freeze
536
+ freeze
537
+ end
538
+ end
539
+
540
+ class CatchTarget
541
+ attr_reader :tag_index #: Integer
542
+ attr_reader :payload_registers #: Array[Integer]
543
+ attr_reader :reference_register #: Integer
544
+ attr_accessor :target_pc #: Integer
545
+ attr_reader :handler_depth #: Integer
546
+
547
+ # @rbs tag_index: Integer
548
+ # @rbs payload_registers: Array[Integer]
549
+ # @rbs reference_register: Integer
550
+ # @rbs pc: Integer
551
+ # @rbs handler_depth: Integer
552
+ def initialize(tag_index, payload_registers, reference_register, pc, handler_depth)
553
+ @tag_index = tag_index
554
+ @payload_registers = payload_registers.dup
555
+ @reference_register = reference_register
556
+ @target_pc = pc
557
+ @handler_depth = handler_depth
558
+ end
559
+
560
+ # @rbs register: Integer
561
+ # @rbs return: void
562
+ def replace_reference_register(register)
563
+ @reference_register = register
564
+ end
565
+
566
+ # @rbs return: Wardite::Efflorescence::CatchTarget
567
+ def copy
568
+ CatchTarget.new(@tag_index, @payload_registers, @reference_register, @target_pc, @handler_depth)
569
+ end
570
+
571
+ # @rbs return: Array[Integer]
572
+ def destinations
573
+ @reference_register < 0 ? @payload_registers.dup : @payload_registers + [@reference_register]
574
+ end
575
+ end
576
+
577
+ class HandlerDescriptor
578
+ attr_reader :clauses #: Array[Wardite::Efflorescence::CatchTarget]
579
+
580
+ # @rbs clauses: Array[Wardite::Efflorescence::CatchTarget]
581
+ def initialize(clauses)
582
+ @clauses = clauses
583
+ end
584
+
585
+ # @rbs return: Wardite::Efflorescence::HandlerDescriptor
586
+ def copy
587
+ HandlerDescriptor.new(@clauses.map { |clause| clause.copy })
588
+ end
589
+ end
590
+
591
+ class ThrowSite
592
+ attr_reader :tag_index #: Integer
593
+ attr_reader :payload_registers #: Array[Integer]
594
+
595
+ # @rbs tag_index: Integer
596
+ # @rbs payload_registers: Array[Integer]
597
+ def initialize(tag_index, payload_registers)
598
+ @tag_index = tag_index
599
+ @payload_registers = payload_registers.dup
600
+ end
601
+
602
+ # @rbs return: Wardite::Efflorescence::ThrowSite
603
+ def copy
604
+ ThrowSite.new(@tag_index, @payload_registers)
605
+ end
606
+ end
607
+
608
+ # Bulk operands stay static in the Function. Values remain in Frame registers.
609
+ class MemorySite
610
+ attr_reader :sources #: Array[Integer]
611
+ attr_reader :memory_index #: Integer
612
+ attr_reader :source_memory_index #: Integer
613
+ attr_reader :data_index #: Integer
614
+
615
+ # @rbs sources: Array[Integer]
616
+ # @rbs memory_index: Integer
617
+ # @rbs source_memory_index: Integer
618
+ # @rbs data_index: Integer
619
+ def initialize(sources, memory_index: 0, source_memory_index: 0, data_index: -1)
620
+ @sources = sources.dup
621
+ @memory_index = memory_index
622
+ @source_memory_index = source_memory_index
623
+ @data_index = data_index
624
+ end
625
+
626
+ # @rbs return: Wardite::Efflorescence::MemorySite
627
+ def copy
628
+ MemorySite.new(@sources, memory_index: @memory_index, source_memory_index: @source_memory_index, data_index: @data_index)
629
+ end
630
+ end
631
+
632
+ # A small wrapper keeps operand reads monomorphic even when a compiler
633
+ # represents the outer instruction table as a generic object array.
634
+ class Instruction
635
+ attr_reader :operands #: Array[Integer]
636
+
637
+ # @rbs operands: Array[Integer]
638
+ def initialize(operands)
639
+ @operands = operands
640
+ end
641
+
642
+ # @rbs index: Integer
643
+ # @rbs return: Integer
644
+ def [](index)
645
+ # Keep generic CFG callbacks from widening native operand/PC reads.
646
+ Integer(@operands.fetch(index))
647
+ end
648
+
649
+ # @rbs index: Integer
650
+ # @rbs value: Integer
651
+ # @rbs return: Integer
652
+ def []=(index, value)
653
+ @operands[index] = value
654
+ end
655
+
656
+ # @rbs return: Integer
657
+ def size
658
+ @operands.size
659
+ end
660
+
661
+ # @rbs return: Wardite::Efflorescence::Instruction
662
+ def copy
663
+ Instruction.new(@operands.dup)
664
+ end
665
+ end
666
+
667
+ # Each instruction holds an integer array. Sources and destinations are slots
668
+ # in initial_registers; constants occupy immutable, preinitialized slots.
669
+ # This is deliberately separate from translation and execution so later
670
+ # function-level optimization passes can consume and produce MIR.
671
+ class Function
672
+ attr_reader :index #: Integer
673
+ attr_reader :pool_index #: Integer
674
+ attr_reader :parameter_types #: Array[Symbol]
675
+ attr_reader :result_types #: Array[Symbol]
676
+ attr_reader :local_count #: Integer
677
+ attr_reader :instructions #: Array[Wardite::Efflorescence::Instruction]
678
+ attr_reader :initial_registers #: Array[Integer]
679
+ attr_reader :constant_registers #: Array[Integer]
680
+ attr_reader :bytecode #: Array[Integer]
681
+ attr_reader :register_types #: Array[Symbol]
682
+ attr_reader :handlers #: Array[Wardite::Efflorescence::HandlerDescriptor]
683
+ attr_reader :flat_handlers #: Array[Wardite::Efflorescence::HandlerDescriptor]
684
+ attr_reader :throw_sites #: Array[Wardite::Efflorescence::ThrowSite]
685
+ attr_accessor :uses_i64 #: bool
686
+ attr_accessor :uses_float #: bool
687
+ attr_accessor :uses_eh #: bool
688
+ attr_accessor :uses_store #: bool
689
+ attr_accessor :uses_dynamic_calls #: bool
690
+ attr_reader :dynamic_sites #: Array[Wardite::Efflorescence::DynamicCallSite]
691
+ attr_reader :memory_sites #: Array[Wardite::Efflorescence::MemorySite]
692
+ attr_reader :calls #: Array[Wardite::Efflorescence::CallSite]
693
+
694
+ # @rbs index: Integer
695
+ # @rbs parameter_types: Array[Symbol]
696
+ # @rbs result_types: Array[Symbol]
697
+ # @rbs local_count: Integer
698
+ # @rbs local_types: Array[Symbol]?
699
+ # @rbs pool_index: Integer?
700
+ def initialize(index, parameter_types, result_types, local_count, local_types: nil, pool_index: nil)
701
+ @index = index
702
+ @pool_index = pool_index || index
703
+ @parameter_types = parameter_types.dup
704
+ @result_types = result_types.dup
705
+ @local_count = local_count
706
+ @register_types = parameter_types + (local_types || Array.new(local_count, :i32))
707
+ @handlers = []
708
+ @flat_handlers = []
709
+ @throw_sites = []
710
+ @memory_sites = []
711
+ @uses_store = false
712
+ @uses_dynamic_calls = false
713
+ @dynamic_sites = []
714
+ @uses_i64 = @register_types.include?(:i64) || result_types.include?(:i64)
715
+ @uses_float = @register_types.any? { |type| FloatBits.float_type?(type) } || result_types.any? { |type| FloatBits.float_type?(type) }
716
+ @uses_eh = @register_types.include?(:exnref)
717
+ @instructions = []
718
+ @initial_registers = Array.new(parameter_types.size + local_count, 0)
719
+ @constant_registers = []
720
+ @bytecode = []
721
+ @calls = []
722
+ end
723
+
724
+ # @rbs return: Integer
725
+ def register_count
726
+ @initial_registers.size
727
+ end
728
+
729
+ # @rbs return: Integer
730
+ def parameter_count
731
+ @parameter_types.size
732
+ end
733
+
734
+ # @rbs index: Integer
735
+ # @rbs return: Integer
736
+ def initial_value(index)
737
+ @initial_registers.fetch(index)
738
+ end
739
+
740
+ # @rbs value: Integer
741
+ # @rbs return: Integer
742
+ # @rbs type: Symbol
743
+ def allocate_register(value, type: :i32)
744
+ slot = @initial_registers.size
745
+ @initial_registers << value
746
+ @uses_i64 = true if type == :i64
747
+ @uses_float = true if FloatBits.float_type?(type)
748
+ @register_types << type
749
+ slot
750
+ end
751
+
752
+ # @rbs value: Integer
753
+ # @rbs return: Integer
754
+ # @rbs type: Symbol
755
+ def allocate_constant(value, type: :i32)
756
+ slot = allocate_register(value, type: type)
757
+ @constant_registers << slot
758
+ slot
759
+ end
760
+
761
+ # @rbs instructions: Array[Wardite::Efflorescence::Instruction]
762
+ # @rbs return: void
763
+ def replace_instructions(instructions)
764
+ @instructions = instructions
765
+ end
766
+
767
+ # @rbs code: Array[Integer]
768
+ # @rbs calls: Array[Wardite::Efflorescence::CallSite]
769
+ # @rbs return: void
770
+ def install_code(code, calls)
771
+ @bytecode = code
772
+ @calls = calls
773
+ end
774
+
775
+ # Keep the element type at the array boundary. In particular, fetch on
776
+ # an object array makes Spinel fall back to a generic boxed container.
777
+ # @rbs index: Integer
778
+ # @rbs return: Wardite::Efflorescence::CallSite
779
+ def call_site(index)
780
+ @calls[index] || raise(Wardite::EvalError, "invalid MIR call site")
781
+ end
782
+
783
+ # @rbs index: Integer
784
+ # @rbs return: Wardite::Efflorescence::MemorySite
785
+ def memory_site(index)
786
+ raise Wardite::EvalError, "invalid MIR memory site" if index < 0
787
+ @memory_sites[index] || raise(Wardite::EvalError, "invalid MIR memory site")
788
+ end
789
+
790
+ # @rbs index: Integer
791
+ # @rbs return: Wardite::Efflorescence::DynamicCallSite
792
+ def dynamic_site(index)
793
+ raise TranslationError, "invalid MIR dynamic call site" if index < 0
794
+ @dynamic_sites[index] || raise(TranslationError, "invalid MIR dynamic call site")
795
+ end
796
+
797
+ # All PC consumers (rewrites, profiling, lowering) share this enumeration.
798
+ # @rbs mapping: Array[Integer]
799
+ # @rbs return: void
800
+ def remap_targets!(mapping)
801
+ @instructions.each do |instruction|
802
+ position = Opcode.target_position(instruction)
803
+ instruction[position] = mapping.fetch(instruction[position]) if position >= 0
804
+ end
805
+ remap_handler_targets!(@handlers, mapping)
806
+ end
807
+
808
+ # @rbs handlers: Array[Wardite::Efflorescence::HandlerDescriptor]
809
+ # @rbs mapping: Array[Integer]
810
+ # @rbs return: void
811
+ def remap_handler_targets!(handlers, mapping)
812
+ handlers.each do |handler|
813
+ handler.clauses.each { |clause| clause.target_pc = mapping.fetch(clause.target_pc) }
814
+ end
815
+ end
816
+
817
+ # @rbs return: void
818
+ def lower_handlers
819
+ @flat_handlers = @handlers.map { |handler| handler.copy }
820
+ return if @flat_handlers.empty?
821
+ mapping = (0..@instructions.size).map { |pc| pc * Lowerer::WIDTH }
822
+ remap_handler_targets!(@flat_handlers, mapping)
823
+ end
824
+
825
+ # Rename MIR references, including operands stored outside instructions.
826
+ # The caller supplies existing same-typed slots and validates the result.
827
+ # @rbs mapping: Array[Integer]
828
+ # @rbs return: void
829
+ def replace_register_references!(mapping)
830
+ @instructions.each do |instruction|
831
+ Opcode.read_positions(instruction).each do |position|
832
+ instruction[position] = mapping.fetch(instruction[position])
833
+ end
834
+ destination = Opcode.destination(instruction)
835
+ instruction[1] = mapping.fetch(destination) if destination >= 0
836
+ end
837
+ @throw_sites.each { |site| site.payload_registers.map! { |register| mapping.fetch(register) } }
838
+ @memory_sites.each { |site| site.sources.map! { |register| mapping.fetch(register) } }
839
+ @dynamic_sites.each { |site| site.arguments.map! { |register| mapping.fetch(register) } }
840
+ @handlers.each do |handler|
841
+ handler.clauses.each do |clause|
842
+ clause.payload_registers.map! { |register| mapping.fetch(register) }
843
+ if clause.reference_register >= 0
844
+ clause.replace_reference_register(mapping.fetch(clause.reference_register))
845
+ end
846
+ end
847
+ end
848
+ end
849
+
850
+ # @rbs instruction: Wardite::Efflorescence::Instruction
851
+ # @rbs return: Array[Integer]
852
+ def reads(instruction)
853
+ result = Opcode.read_positions(instruction).map { |position| instruction[position] }
854
+ result += @throw_sites.fetch(instruction[1]).payload_registers if instruction[0] == Opcode::THROW
855
+ result += memory_site(instruction[1]).sources if Opcode.memory_site?(instruction[0])
856
+ result += dynamic_site(instruction[2]).arguments if Opcode.dynamic_call?(instruction[0])
857
+ result
858
+ end
859
+
860
+ # @rbs return: Wardite::Efflorescence::Function
861
+ def copy
862
+ result = Function.new(@index, @parameter_types, @result_types, @local_count, local_types: @register_types[@parameter_types.size, @local_count], pool_index: @pool_index)
863
+ result.uses_i64 = @uses_i64
864
+ result.uses_float = @uses_float
865
+ result.uses_eh = @uses_eh
866
+ result.uses_store = @uses_store
867
+ result.uses_dynamic_calls = @uses_dynamic_calls
868
+ @dynamic_sites.each { |site| result.dynamic_sites << site.copy }
869
+ @memory_sites.each { |site| result.memory_sites << site.copy }
870
+ @handlers.each { |handler| result.handlers << handler.copy }
871
+ @throw_sites.each { |site| result.throw_sites << site.copy }
872
+ index = @parameter_types.size + @local_count
873
+ while index < register_count
874
+ if @constant_registers.include?(index)
875
+ result.allocate_constant(initial_value(index), type: @register_types.fetch(index))
876
+ else
877
+ result.allocate_register(initial_value(index), type: @register_types.fetch(index))
878
+ end
879
+ index += 1
880
+ end
881
+ result.replace_instructions(@instructions.map { |instruction| instruction.copy })
882
+ result
883
+ end
884
+ end
885
+
886
+ class CallSite
887
+ attr_reader :callee #: Wardite::Efflorescence::Function
888
+ attr_reader :arguments #: Array[Integer]
889
+ attr_reader :pool_index #: Integer
890
+
891
+ # @rbs callee: Wardite::Efflorescence::Function
892
+ # @rbs arguments: Array[Integer]
893
+ def initialize(callee, arguments)
894
+ @callee = callee
895
+ @arguments = arguments
896
+ @pool_index = callee.pool_index
897
+ end
898
+
899
+ # Keep the generic argument loop out of the interpreter's live locals.
900
+ # The common single-argument path copies directly from the CALL operand.
901
+ # @rbs target: Array[Integer]
902
+ # @rbs source: Array[Integer]
903
+ # @rbs return: void
904
+ def copy_arguments(target, source)
905
+ argument = 0
906
+ count = @arguments.size
907
+ while argument < count
908
+ target[argument] = source[@arguments.fetch(argument)]
909
+ argument += 1
910
+ end
911
+ end
912
+ end
913
+
914
+ class DynamicCallSite
915
+ attr_reader :host_index #: Integer
916
+ attr_reader :table_index #: Integer
917
+ attr_reader :arguments #: Array[Integer]
918
+ attr_reader :parameter_types #: Array[Symbol]
919
+ attr_reader :result_types #: Array[Symbol]
920
+
921
+ # @rbs host_index: Integer
922
+ # @rbs arguments: Array[Integer]
923
+ # @rbs parameter_types: Array[Symbol]
924
+ # @rbs result_types: Array[Symbol]
925
+ # @rbs table_index: Integer
926
+ def initialize(host_index, arguments, parameter_types, result_types, table_index: -1)
927
+ @host_index = host_index
928
+ @table_index = table_index
929
+ @arguments = arguments.dup
930
+ @parameter_types = parameter_types.dup
931
+ @result_types = result_types.dup
932
+ end
933
+
934
+ # @rbs return: Wardite::Efflorescence::DynamicCallSite
935
+ def copy
936
+ DynamicCallSite.new(@host_index, @arguments, @parameter_types, @result_types, table_index: @table_index)
937
+ end
938
+ end
939
+
940
+ # @rbs!
941
+ # interface _WasiCallable
942
+ # def call: (Wardite::Store, Array[Integer], Array[Integer]?) -> Integer?
943
+ # end
944
+
945
+ class HostFunction
946
+ attr_reader :external #: Wardite::ExternalFunction
947
+ attr_reader :entry #: Wardite::Efflorescence::FunctionEntry
948
+ attr_reader :callable #: Wardite::_WasmCallable
949
+ attr_reader :typed_abi #: bool
950
+ attr_reader :unboxed_wasi #: Wardite::Efflorescence::_WasiCallable?
951
+
952
+ # @rbs external: Wardite::ExternalFunction
953
+ # @rbs entry: Wardite::Efflorescence::FunctionEntry
954
+ # @rbs callable: Wardite::_WasmCallable
955
+ # @rbs unboxed_wasi: Wardite::Efflorescence::_WasiCallable?
956
+ def initialize(external, entry, callable, unboxed_wasi)
957
+ @external = external
958
+ @entry = entry
959
+ @callable = callable
960
+ @typed_abi = entry.parameter_types.any? { |type| type != :i32 } || entry.result_types.any? { |type| type != :i32 }
961
+ @unboxed_wasi = unboxed_wasi
962
+ end
963
+
964
+ # @rbs return: Wardite::Efflorescence::HostFunction
965
+ def copy
966
+ HostFunction.new(@external, @entry.copy, @callable, @unboxed_wasi)
967
+ end
968
+ end
969
+
970
+ class FunctionEntry
971
+ attr_reader :kind #: Symbol
972
+ attr_reader :parameter_types #: Array[Symbol]
973
+ attr_reader :result_types #: Array[Symbol]
974
+ attr_reader :defined_index #: Integer
975
+ attr_reader :host_index #: Integer
976
+
977
+ # @rbs kind: Symbol
978
+ # @rbs parameter_types: Array[Symbol]
979
+ # @rbs result_types: Array[Symbol]
980
+ # @rbs defined_index: Integer
981
+ # @rbs host_index: Integer
982
+ def initialize(kind, parameter_types, result_types, defined_index, host_index)
983
+ @kind = kind
984
+ @parameter_types = parameter_types.dup
985
+ @result_types = result_types.dup
986
+ @defined_index = defined_index
987
+ @host_index = host_index
988
+ end
989
+
990
+ # @rbs params: Array[Symbol]
991
+ # @rbs results: Array[Symbol]
992
+ # @rbs label: String
993
+ # @rbs return: void
994
+ def self.check_signature(params, results, label)
995
+ unless results.size <= 1 && params.all? { |type| numeric_type?(type) } &&
996
+ results.all? { |type| numeric_type?(type) }
997
+ raise UnsupportedFeature, "Efflorescence: #{label} requires a numeric signature with at most one result (i32/i64/f32/f64)"
998
+ end
999
+ end
1000
+
1001
+ # @rbs type: Symbol
1002
+ # @rbs return: bool
1003
+ def self.numeric_type?(type)
1004
+ type == :i32 || type == :i64 || FloatBits.float_type?(type)
1005
+ end
1006
+
1007
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
1008
+ def copy
1009
+ FunctionEntry.new(@kind, @parameter_types, @result_types, @defined_index, @host_index)
1010
+ end
1011
+ end
1012
+
1013
+ class IR
1014
+ attr_reader :functions #: Array[Wardite::Efflorescence::Function]
1015
+ attr_reader :entries #: Array[Wardite::Efflorescence::FunctionEntry]
1016
+ attr_reader :hosts #: Array[Wardite::Efflorescence::HostFunction]
1017
+
1018
+ # @rbs functions: Array[Wardite::Efflorescence::Function]
1019
+ # @rbs entries: Array[Wardite::Efflorescence::FunctionEntry]?
1020
+ # @rbs hosts: Array[Wardite::Efflorescence::HostFunction]
1021
+ def initialize(functions, entries: nil, hosts: [])
1022
+ @functions = functions
1023
+ @hosts = hosts
1024
+ @entries = entries || functions.map do |function|
1025
+ FunctionEntry.new(:defined, function.parameter_types, function.result_types, function.pool_index, -1)
1026
+ end
1027
+ end
1028
+
1029
+ # @rbs index: Integer
1030
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
1031
+ def entry_at(index)
1032
+ raise Wardite::EvalError, "invalid MIR function index #{index}" if index < 0
1033
+ @entries[index] || raise(Wardite::EvalError, "invalid MIR function index #{index}")
1034
+ end
1035
+
1036
+ # @rbs index: Integer
1037
+ # @rbs return: Wardite::Efflorescence::Function
1038
+ def function_at(index)
1039
+ entry = entry_at(index)
1040
+ raise Wardite::EvalError, "MIR function #{index} is not defined" unless entry.kind == :defined
1041
+ defined_at(entry.defined_index)
1042
+ end
1043
+
1044
+ # @rbs index: Integer
1045
+ # @rbs return: Wardite::Efflorescence::Function
1046
+ def defined_at(index)
1047
+ @functions[index] || raise(Wardite::EvalError, "invalid MIR defined index #{index}")
1048
+ end
1049
+
1050
+ # @rbs return: Wardite::Efflorescence::IR
1051
+ def copy
1052
+ IR.new(@functions.map { |function| function.copy }, entries: @entries.map { |entry| entry.copy },
1053
+ hosts: @hosts.map { |host| host.copy })
1054
+ end
1055
+ end
1056
+
1057
+ class Frame
1058
+ attr_reader :function #: Wardite::Efflorescence::Function
1059
+ attr_reader :pool_index #: Integer
1060
+ # Numeric registers hold unsigned 32-bit or signed 64-bit carriers.
1061
+ attr_reader :registers #: Array[Integer]
1062
+ attr_reader :exception_registers #: Array[Wardite::Efflorescence::ExceptionValue?]
1063
+ attr_reader :handlers #: Array[Integer]
1064
+ attr_reader :return_register #: Integer
1065
+ attr_accessor :pc #: Integer
1066
+
1067
+ # @rbs function: Wardite::Efflorescence::Function
1068
+ # @rbs return_register: Integer
1069
+ def initialize(function, return_register)
1070
+ @function = function
1071
+ @pool_index = function.pool_index
1072
+ # Constants are initialized once and never written by verified MIR.
1073
+ # Copying the template also avoids zero-filling and then overwriting
1074
+ # every slot in a second loop on a newly allocated frame.
1075
+ @uses_eh = function.uses_eh
1076
+ @registers = function.initial_registers.dup
1077
+ @exception_registers = function.register_types.include?(:exnref) ? Array.new(function.register_count, nil) : []
1078
+ @handlers = []
1079
+ @local_start = function.parameter_count
1080
+ @local_end = @local_start + function.local_count
1081
+ @return_register = return_register
1082
+ @pc = 0
1083
+ end
1084
+
1085
+ # @rbs return: void
1086
+ def clear_references
1087
+ @exception_registers.fill(nil)
1088
+ @handlers.clear
1089
+ end
1090
+
1091
+ # @rbs depth: Integer
1092
+ # @rbs return: void
1093
+ def trim_handlers(depth)
1094
+ @handlers.pop while @handlers.size > depth
1095
+ end
1096
+
1097
+ # @rbs return_register: Integer
1098
+ # @rbs return: void
1099
+ def reset(return_register)
1100
+ clear_references if @uses_eh
1101
+ # CALL overwrites all parameters; Wasm initializes only non-parameter
1102
+ # locals to zero. RegisterValidation proves that every temporary is
1103
+ # written before it is read, including across joins and backedges.
1104
+ index = @local_start
1105
+ while index < @local_end
1106
+ @registers[index] = 0
1107
+ index += 1
1108
+ end
1109
+ @return_register = return_register
1110
+ @pc = 0
1111
+ end
1112
+ end
1113
+
1114
+ # An invocation owns its pools. Active recursive frames never enter the
1115
+ # free list, and overlapping/reentrant public calls cannot share a frame.
1116
+ class FramePool
1117
+ attr_reader :created #: Integer
1118
+
1119
+ # @rbs function: Wardite::Efflorescence::Function
1120
+ # @rbs reuse: bool
1121
+ def initialize(function, reuse)
1122
+ @function = function
1123
+ @reuse = reuse
1124
+ @clear_references = function.uses_eh
1125
+ @free = [] #: Array[Wardite::Efflorescence::Frame]
1126
+ @created = 0
1127
+ end
1128
+
1129
+ # @rbs destination: Integer
1130
+ # @rbs return: Wardite::Efflorescence::Frame
1131
+ def acquire(destination)
1132
+ if @reuse
1133
+ frame = @free.pop
1134
+ if frame
1135
+ frame.reset(destination)
1136
+ return frame
1137
+ end
1138
+ end
1139
+ @created += 1
1140
+ Frame.new(@function, destination)
1141
+ end
1142
+
1143
+ # @rbs frame: Wardite::Efflorescence::Frame
1144
+ # @rbs return: void
1145
+ def release(frame)
1146
+ frame.clear_references if @clear_references
1147
+ @free << frame if @reuse
1148
+ end
1149
+ end
1150
+ end
1151
+ end