wardite 0.9.0 → 1.1.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 (184) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +107 -2
  5. data/Rakefile +5 -0
  6. data/Steepfile +1 -0
  7. data/assets/wardite.svg +12 -0
  8. data/benchmark/README.md +500 -0
  9. data/benchmark/build_call_profile.rb +42 -0
  10. data/benchmark/call_cost_worker.rb +27 -0
  11. data/benchmark/compare.rb +93 -0
  12. data/benchmark/compare_call_types.rb +212 -0
  13. data/benchmark/compare_dispatch.rb +123 -0
  14. data/benchmark/compare_engines.rb +119 -0
  15. data/benchmark/compare_optimizations.rb +139 -0
  16. data/benchmark/engine_phases.rb +30 -0
  17. data/benchmark/figures/README.md +35 -0
  18. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  19. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  20. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  21. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  22. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  23. data/benchmark/float_m5.wat +42 -0
  24. data/benchmark/i32_call_arities.wat +89 -0
  25. data/benchmark/i32_calls.wat +174 -0
  26. data/benchmark/i32_memory.wat +58 -0
  27. data/benchmark/i32_mix.wat +80 -0
  28. data/benchmark/i64_m4.wat +55 -0
  29. data/benchmark/investigate_calls.rb +231 -0
  30. data/benchmark/investigate_generated_guards.rb +218 -0
  31. data/benchmark/m3_call_worker.rb +38 -0
  32. data/benchmark/measure_m1.rb +51 -0
  33. data/benchmark/measure_m2.rb +55 -0
  34. data/benchmark/measure_m3_calls.rb +89 -0
  35. data/benchmark/measure_m4.rb +134 -0
  36. data/benchmark/measure_m5_boundary.rb +41 -0
  37. data/benchmark/measure_mir_cache_verify.rb +58 -0
  38. data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
  39. data/benchmark/measure_ruby_wasm_startup.rb +43 -0
  40. data/benchmark/measure_wasi_write.rb +75 -0
  41. data/benchmark/optimization_worker.rb +23 -0
  42. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  43. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  44. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  45. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  46. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  47. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  48. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  49. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  50. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  51. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  52. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  53. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  54. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  55. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  56. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  57. data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
  58. data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
  59. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  60. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  61. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  62. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  63. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  64. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  65. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  66. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  67. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  68. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  69. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  70. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  71. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  72. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  73. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  74. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  75. data/benchmark/summarize_call_profile.py +54 -0
  76. data/benchmark/validate_m1_eh.rb +34 -0
  77. data/benchmark/validate_m2.rb +119 -0
  78. data/benchmark/validate_m3.rb +44 -0
  79. data/benchmark/validate_m4.rb +95 -0
  80. data/benchmark/validate_m5.rb +108 -0
  81. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  82. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  83. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  84. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  85. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  86. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  87. data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
  88. data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
  89. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
  90. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
  91. data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
  92. data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
  93. data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
  94. data/docs/efflorescence-call-arities.md +115 -0
  95. data/docs/efflorescence-call-investigation.md +155 -0
  96. data/docs/efflorescence-call-transfer.md +128 -0
  97. data/docs/efflorescence-call-types.md +102 -0
  98. data/docs/efflorescence-coverage-ja.md +351 -0
  99. data/docs/efflorescence-generated-c-guards.md +410 -0
  100. data/docs/efflorescence-inlining.md +165 -0
  101. data/docs/efflorescence-ja.md +155 -0
  102. data/docs/efflorescence-m1.md +160 -0
  103. data/docs/efflorescence-m2.md +233 -0
  104. data/docs/efflorescence-m3.md +131 -0
  105. data/docs/efflorescence-m4.md +88 -0
  106. data/docs/efflorescence-m5.md +82 -0
  107. data/docs/efflorescence-m6.md +95 -0
  108. data/docs/efflorescence-mir2-cache.md +123 -0
  109. data/docs/efflorescence-operand-decoding.md +105 -0
  110. data/docs/efflorescence-phase2-ja.md +225 -0
  111. data/docs/efflorescence-phase2.md +240 -0
  112. data/docs/efflorescence-register-initialization.md +75 -0
  113. data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
  114. data/docs/efflorescence-spinel-linear-memory.md +72 -0
  115. data/docs/efflorescence-startup-snapshot.md +60 -0
  116. data/docs/efflorescence-unboxed-globals.md +61 -0
  117. data/docs/efflorescence.md +144 -0
  118. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  119. data/lib/wardite/alu_f32.generated.rb +11 -2
  120. data/lib/wardite/alu_f64.generated.rb +11 -2
  121. data/lib/wardite/alu_i32.generated.rb +54 -17
  122. data/lib/wardite/alu_i64.generated.rb +73 -31
  123. data/lib/wardite/cli/cli.rb +112 -29
  124. data/lib/wardite/const.rb +1 -0
  125. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  126. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  127. data/lib/wardite/efflorescence/inspection.rb +165 -0
  128. data/lib/wardite/efflorescence/ir.rb +1164 -0
  129. data/lib/wardite/efflorescence/mir_cache.rb +657 -0
  130. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  131. data/lib/wardite/efflorescence/register_validation.rb +362 -0
  132. data/lib/wardite/efflorescence/snapshot.rb +229 -0
  133. data/lib/wardite/efflorescence/store_access.rb +366 -0
  134. data/lib/wardite/efflorescence/translator.rb +866 -0
  135. data/lib/wardite/efflorescence/vm.rb +1170 -0
  136. data/lib/wardite/efflorescence/wasi.rb +52 -0
  137. data/lib/wardite/efflorescence.rb +17 -0
  138. data/lib/wardite/errors.rb +20 -0
  139. data/lib/wardite/float_bits.rb +46 -0
  140. data/lib/wardite/instruction.rb +35 -4
  141. data/lib/wardite/leb128.rb +33 -12
  142. data/lib/wardite/load.rb +292 -143
  143. data/lib/wardite/value.rb +169 -175
  144. data/lib/wardite/version.rb +1 -1
  145. data/lib/wardite/wasi/consts.rb +1 -0
  146. data/lib/wardite/wasi/spinel.rb +41 -0
  147. data/lib/wardite/wasi.rb +483 -268
  148. data/lib/wardite/wasm_module.rb +6 -3
  149. data/lib/wardite.rb +360 -98
  150. data/rbs_collection.lock.yaml +36 -96
  151. data/scripts/gen_alu.rb +121 -21
  152. data/scripts/verify_ruby_wasm.rb +70 -0
  153. data/sig/generated/wardite/cli/cli.rbs +23 -6
  154. data/sig/generated/wardite/const.rbs +2 -0
  155. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  156. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  157. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  158. data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
  159. data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
  160. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  161. data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
  162. data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
  163. data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
  164. data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
  165. data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
  166. data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
  167. data/sig/generated/wardite/errors.rbs +12 -0
  168. data/sig/generated/wardite/float_bits.rbs +30 -0
  169. data/sig/generated/wardite/instruction.rbs +22 -1
  170. data/sig/generated/wardite/leb128.rbs +3 -1
  171. data/sig/generated/wardite/load.rbs +59 -7
  172. data/sig/generated/wardite/value.rbs +70 -11
  173. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  174. data/sig/generated/wardite/wasi/spinel.rbs +15 -0
  175. data/sig/generated/wardite/wasi.rbs +111 -4
  176. data/sig/generated/wardite.rbs +114 -13
  177. data/sig/mir_cache_stdlib.rbs +5 -0
  178. data/sig/vernier.rbs +2 -2
  179. data/spinel/README.md +30 -0
  180. data/spinel/bin/wardite.rb +6 -0
  181. data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
  182. data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
  183. data/spinel/spin.toml +7 -0
  184. metadata +154 -3
@@ -0,0 +1,1164 @@
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 return: Integer
784
+ def call_site_count
785
+ @calls.length
786
+ end
787
+
788
+ # @rbs site: Wardite::Efflorescence::CallSite
789
+ # @rbs return: void
790
+ def add_call_site(site)
791
+ @calls.push(site)
792
+ nil
793
+ end
794
+
795
+ # @rbs index: Integer
796
+ # @rbs return: Wardite::Efflorescence::MemorySite
797
+ def memory_site(index)
798
+ raise Wardite::EvalError, "invalid MIR memory site" if index < 0
799
+ @memory_sites[index] || raise(Wardite::EvalError, "invalid MIR memory site")
800
+ end
801
+
802
+ # @rbs index: Integer
803
+ # @rbs return: Wardite::Efflorescence::DynamicCallSite
804
+ def dynamic_site(index)
805
+ raise TranslationError, "invalid MIR dynamic call site" if index < 0
806
+ @dynamic_sites[index] || raise(TranslationError, "invalid MIR dynamic call site")
807
+ end
808
+
809
+ # All PC consumers (rewrites, profiling, lowering) share this enumeration.
810
+ # @rbs mapping: Array[Integer]
811
+ # @rbs return: void
812
+ def remap_targets!(mapping)
813
+ @instructions.each do |instruction|
814
+ position = Opcode.target_position(instruction)
815
+ instruction[position] = mapping.fetch(instruction[position]) if position >= 0
816
+ end
817
+ remap_handler_targets!(@handlers, mapping)
818
+ end
819
+
820
+ # @rbs handlers: Array[Wardite::Efflorescence::HandlerDescriptor]
821
+ # @rbs mapping: Array[Integer]
822
+ # @rbs return: void
823
+ def remap_handler_targets!(handlers, mapping)
824
+ handlers.each do |handler|
825
+ handler.clauses.each { |clause| clause.target_pc = mapping.fetch(clause.target_pc) }
826
+ end
827
+ end
828
+
829
+ # @rbs return: void
830
+ def lower_handlers
831
+ @flat_handlers = @handlers.map { |handler| handler.copy }
832
+ return if @flat_handlers.empty?
833
+ mapping = (0..@instructions.size).map { |pc| pc * Lowerer::WIDTH }
834
+ remap_handler_targets!(@flat_handlers, mapping)
835
+ end
836
+
837
+ # Rename MIR references, including operands stored outside instructions.
838
+ # The caller supplies existing same-typed slots and validates the result.
839
+ # @rbs mapping: Array[Integer]
840
+ # @rbs return: void
841
+ def replace_register_references!(mapping)
842
+ @instructions.each do |instruction|
843
+ Opcode.read_positions(instruction).each do |position|
844
+ instruction[position] = mapping.fetch(instruction[position])
845
+ end
846
+ destination = Opcode.destination(instruction)
847
+ instruction[1] = mapping.fetch(destination) if destination >= 0
848
+ end
849
+ @throw_sites.each { |site| site.payload_registers.map! { |register| mapping.fetch(register) } }
850
+ @memory_sites.each { |site| site.sources.map! { |register| mapping.fetch(register) } }
851
+ @dynamic_sites.each { |site| site.arguments.map! { |register| mapping.fetch(register) } }
852
+ @handlers.each do |handler|
853
+ handler.clauses.each do |clause|
854
+ clause.payload_registers.map! { |register| mapping.fetch(register) }
855
+ if clause.reference_register >= 0
856
+ clause.replace_reference_register(mapping.fetch(clause.reference_register))
857
+ end
858
+ end
859
+ end
860
+ end
861
+
862
+ # @rbs instruction: Wardite::Efflorescence::Instruction
863
+ # @rbs return: Array[Integer]
864
+ def reads(instruction)
865
+ result = Opcode.read_positions(instruction).map { |position| instruction[position] }
866
+ result += @throw_sites.fetch(instruction[1]).payload_registers if instruction[0] == Opcode::THROW
867
+ result += memory_site(instruction[1]).sources if Opcode.memory_site?(instruction[0])
868
+ result += dynamic_site(instruction[2]).arguments if Opcode.dynamic_call?(instruction[0])
869
+ result
870
+ end
871
+
872
+ # @rbs return: Wardite::Efflorescence::Function
873
+ def copy
874
+ result = Function.new(@index, @parameter_types, @result_types, @local_count, local_types: @register_types[@parameter_types.size, @local_count], pool_index: @pool_index)
875
+ result.uses_i64 = @uses_i64
876
+ result.uses_float = @uses_float
877
+ result.uses_eh = @uses_eh
878
+ result.uses_store = @uses_store
879
+ result.uses_dynamic_calls = @uses_dynamic_calls
880
+ @dynamic_sites.each { |site| result.dynamic_sites << site.copy }
881
+ @memory_sites.each { |site| result.memory_sites << site.copy }
882
+ @handlers.each { |handler| result.handlers << handler.copy }
883
+ @throw_sites.each { |site| result.throw_sites << site.copy }
884
+ index = @parameter_types.size + @local_count
885
+ while index < register_count
886
+ if @constant_registers.include?(index)
887
+ result.allocate_constant(initial_value(index), type: @register_types.fetch(index))
888
+ else
889
+ result.allocate_register(initial_value(index), type: @register_types.fetch(index))
890
+ end
891
+ index += 1
892
+ end
893
+ result.replace_instructions(@instructions.map { |instruction| instruction.copy })
894
+ result
895
+ end
896
+ end
897
+
898
+ class CallSite
899
+ attr_reader :callee #: Wardite::Efflorescence::Function
900
+ attr_reader :arguments #: Array[Integer]
901
+ attr_reader :pool_index #: Integer
902
+
903
+ # @rbs callee: Wardite::Efflorescence::Function
904
+ # @rbs arguments: Array[Integer]
905
+ def initialize(callee, arguments)
906
+ @callee = callee
907
+ @arguments = arguments
908
+ @pool_index = callee.pool_index
909
+ end
910
+
911
+ # Keep the generic argument loop out of the interpreter's live locals.
912
+ # The common single-argument path copies directly from the CALL operand.
913
+ # @rbs target: Array[Integer]
914
+ # @rbs source: Array[Integer]
915
+ # @rbs return: void
916
+ def copy_arguments(target, source)
917
+ argument = 0
918
+ count = @arguments.size
919
+ while argument < count
920
+ target[argument] = source[@arguments.fetch(argument)]
921
+ argument += 1
922
+ end
923
+ end
924
+ end
925
+
926
+ class DynamicCallSite
927
+ attr_reader :host_index #: Integer
928
+ attr_reader :table_index #: Integer
929
+ attr_reader :arguments #: Array[Integer]
930
+ attr_reader :parameter_types #: Array[Symbol]
931
+ attr_reader :result_types #: Array[Symbol]
932
+
933
+ # @rbs host_index: Integer
934
+ # @rbs arguments: Array[Integer]
935
+ # @rbs parameter_types: Array[Symbol]
936
+ # @rbs result_types: Array[Symbol]
937
+ # @rbs table_index: Integer
938
+ def initialize(host_index, arguments, parameter_types, result_types, table_index: -1)
939
+ @host_index = host_index
940
+ @table_index = table_index
941
+ @arguments = [] #: Array[Integer]
942
+ @arguments.replace(arguments)
943
+ @parameter_types = parameter_types.dup
944
+ @result_types = result_types.dup
945
+ end
946
+
947
+ # @rbs return: Wardite::Efflorescence::DynamicCallSite
948
+ def copy
949
+ DynamicCallSite.new(@host_index, @arguments, @parameter_types, @result_types, table_index: @table_index)
950
+ end
951
+ end
952
+
953
+ # @rbs!
954
+ # interface _WasiCallable
955
+ # def call: (Wardite::Store, Array[Integer], Array[Integer]?) -> Integer?
956
+ # end
957
+
958
+ class HostFunction
959
+ attr_reader :external #: Wardite::ExternalFunction
960
+ attr_reader :entry #: Wardite::Efflorescence::FunctionEntry
961
+ attr_reader :callable #: Wardite::_WasmCallable
962
+ attr_reader :typed_abi #: bool
963
+ attr_reader :unboxed_wasi #: Wardite::Efflorescence::_WasiCallable?
964
+
965
+ # @rbs external: Wardite::ExternalFunction
966
+ # @rbs entry: Wardite::Efflorescence::FunctionEntry
967
+ # @rbs callable: Wardite::_WasmCallable
968
+ # @rbs unboxed_wasi: Wardite::Efflorescence::_WasiCallable?
969
+ def initialize(external, entry, callable, unboxed_wasi)
970
+ @external = external
971
+ @entry = entry
972
+ @callable = callable
973
+ @typed_abi = entry.parameter_types.any? { |type| type != :i32 } || entry.result_types.any? { |type| type != :i32 }
974
+ @unboxed_wasi = unboxed_wasi
975
+ end
976
+
977
+ # @rbs return: Wardite::Efflorescence::HostFunction
978
+ def copy
979
+ HostFunction.new(@external, @entry.copy, @callable, @unboxed_wasi)
980
+ end
981
+ end
982
+
983
+ class FunctionEntry
984
+ attr_reader :kind #: Symbol
985
+ attr_reader :parameter_types #: Array[Symbol]
986
+ attr_reader :result_types #: Array[Symbol]
987
+ attr_reader :defined_index #: Integer
988
+ attr_reader :host_index #: Integer
989
+
990
+ # @rbs kind: Symbol
991
+ # @rbs parameter_types: Array[Symbol]
992
+ # @rbs result_types: Array[Symbol]
993
+ # @rbs defined_index: Integer
994
+ # @rbs host_index: Integer
995
+ def initialize(kind, parameter_types, result_types, defined_index, host_index)
996
+ @kind = kind
997
+ @parameter_types = parameter_types.dup
998
+ @result_types = result_types.dup
999
+ @defined_index = defined_index
1000
+ @host_index = host_index
1001
+ end
1002
+
1003
+ # @rbs params: Array[Symbol]
1004
+ # @rbs results: Array[Symbol]
1005
+ # @rbs label: String
1006
+ # @rbs return: void
1007
+ def self.check_signature(params, results, label)
1008
+ unless results.size <= 1 && params.all? { |type| numeric_type?(type) } &&
1009
+ results.all? { |type| numeric_type?(type) }
1010
+ raise UnsupportedFeature, "Efflorescence: #{label} requires a numeric signature with at most one result (i32/i64/f32/f64)"
1011
+ end
1012
+ end
1013
+
1014
+ # @rbs type: Symbol
1015
+ # @rbs return: bool
1016
+ def self.numeric_type?(type)
1017
+ type == :i32 || type == :i64 || FloatBits.float_type?(type)
1018
+ end
1019
+
1020
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
1021
+ def copy
1022
+ FunctionEntry.new(@kind, @parameter_types, @result_types, @defined_index, @host_index)
1023
+ end
1024
+ end
1025
+
1026
+ class IR
1027
+ attr_reader :functions #: Array[Wardite::Efflorescence::Function]
1028
+ attr_reader :entries #: Array[Wardite::Efflorescence::FunctionEntry]
1029
+ attr_reader :hosts #: Array[Wardite::Efflorescence::HostFunction]
1030
+
1031
+ # @rbs functions: Array[Wardite::Efflorescence::Function]
1032
+ # @rbs entries: Array[Wardite::Efflorescence::FunctionEntry]?
1033
+ # @rbs hosts: Array[Wardite::Efflorescence::HostFunction]
1034
+ def initialize(functions, entries: nil, hosts: [])
1035
+ @functions = functions
1036
+ @hosts = hosts
1037
+ @entries = entries || functions.map do |function|
1038
+ FunctionEntry.new(:defined, function.parameter_types, function.result_types, function.pool_index, -1)
1039
+ end
1040
+ end
1041
+
1042
+ # @rbs index: Integer
1043
+ # @rbs return: Wardite::Efflorescence::FunctionEntry
1044
+ def entry_at(index)
1045
+ raise Wardite::EvalError, "invalid MIR function index #{index}" if index < 0
1046
+ @entries[index] || raise(Wardite::EvalError, "invalid MIR function index #{index}")
1047
+ end
1048
+
1049
+ # @rbs index: Integer
1050
+ # @rbs return: Wardite::Efflorescence::Function
1051
+ def function_at(index)
1052
+ entry = entry_at(index)
1053
+ raise Wardite::EvalError, "MIR function #{index} is not defined" unless entry.kind == :defined
1054
+ defined_at(entry.defined_index)
1055
+ end
1056
+
1057
+ # @rbs index: Integer
1058
+ # @rbs return: Wardite::Efflorescence::Function
1059
+ def defined_at(index)
1060
+ @functions[index] || raise(Wardite::EvalError, "invalid MIR defined index #{index}")
1061
+ end
1062
+
1063
+ # @rbs return: Wardite::Efflorescence::IR
1064
+ def copy
1065
+ IR.new(@functions.map { |function| function.copy }, entries: @entries.map { |entry| entry.copy },
1066
+ hosts: @hosts.map { |host| host.copy })
1067
+ end
1068
+ end
1069
+
1070
+ class Frame
1071
+ attr_reader :function #: Wardite::Efflorescence::Function
1072
+ attr_reader :pool_index #: Integer
1073
+ # Numeric registers hold unsigned 32-bit or signed 64-bit carriers.
1074
+ attr_reader :registers #: Array[Integer]
1075
+ attr_reader :exception_registers #: Array[Wardite::Efflorescence::ExceptionValue?]
1076
+ attr_reader :handlers #: Array[Integer]
1077
+ attr_reader :return_register #: Integer
1078
+ attr_accessor :pc #: Integer
1079
+
1080
+ # @rbs function: Wardite::Efflorescence::Function
1081
+ # @rbs return_register: Integer
1082
+ def initialize(function, return_register)
1083
+ @function = function
1084
+ @pool_index = function.pool_index
1085
+ # Constants are initialized once and never written by verified MIR.
1086
+ # Copying the template also avoids zero-filling and then overwriting
1087
+ # every slot in a second loop on a newly allocated frame.
1088
+ @uses_eh = function.uses_eh
1089
+ @registers = function.initial_registers.dup
1090
+ @exception_registers = function.register_types.include?(:exnref) ? Array.new(function.register_count, nil) : []
1091
+ @handlers = []
1092
+ @local_start = function.parameter_count
1093
+ @local_end = @local_start + function.local_count
1094
+ @return_register = return_register
1095
+ @pc = 0
1096
+ end
1097
+
1098
+ # @rbs return: void
1099
+ def clear_references
1100
+ @exception_registers.fill(nil)
1101
+ @handlers.clear
1102
+ end
1103
+
1104
+ # @rbs depth: Integer
1105
+ # @rbs return: void
1106
+ def trim_handlers(depth)
1107
+ @handlers.pop while @handlers.size > depth
1108
+ end
1109
+
1110
+ # @rbs return_register: Integer
1111
+ # @rbs return: void
1112
+ def reset(return_register)
1113
+ clear_references if @uses_eh
1114
+ # CALL overwrites all parameters; Wasm initializes only non-parameter
1115
+ # locals to zero. RegisterValidation proves that every temporary is
1116
+ # written before it is read, including across joins and backedges.
1117
+ index = @local_start
1118
+ while index < @local_end
1119
+ @registers[index] = 0
1120
+ index += 1
1121
+ end
1122
+ @return_register = return_register
1123
+ @pc = 0
1124
+ end
1125
+ end
1126
+
1127
+ # An invocation owns its pools. Active recursive frames never enter the
1128
+ # free list, and overlapping/reentrant public calls cannot share a frame.
1129
+ class FramePool
1130
+ attr_reader :created #: Integer
1131
+
1132
+ # @rbs function: Wardite::Efflorescence::Function
1133
+ # @rbs reuse: bool
1134
+ def initialize(function, reuse)
1135
+ @function = function
1136
+ @reuse = reuse
1137
+ @clear_references = function.uses_eh
1138
+ @free = [] #: Array[Wardite::Efflorescence::Frame]
1139
+ @created = 0
1140
+ end
1141
+
1142
+ # @rbs destination: Integer
1143
+ # @rbs return: Wardite::Efflorescence::Frame
1144
+ def acquire(destination)
1145
+ if @reuse
1146
+ frame = @free.pop
1147
+ if frame
1148
+ frame.reset(destination)
1149
+ return frame
1150
+ end
1151
+ end
1152
+ @created += 1
1153
+ Frame.new(@function, destination)
1154
+ end
1155
+
1156
+ # @rbs frame: Wardite::Efflorescence::Frame
1157
+ # @rbs return: void
1158
+ def release(frame)
1159
+ frame.clear_references if @clear_references
1160
+ @free << frame if @reuse
1161
+ end
1162
+ end
1163
+ end
1164
+ end