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,257 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ module I64Arithmetic
7
+ MASK = 0xffffffff
8
+ MIN = -9223372036854775807 - 1 #: Integer
9
+
10
+ # @rbs value: Integer
11
+ # @rbs return: Integer
12
+ def self.low(value)
13
+ value & MASK
14
+ end
15
+
16
+ # @rbs value: Integer
17
+ # @rbs return: Integer
18
+ def self.high(value)
19
+ (value >> 32) & MASK
20
+ end
21
+
22
+ # @rbs low: Integer
23
+ # @rbs high: Integer
24
+ # @rbs return: Integer
25
+ def self.join(low, high)
26
+ low | ((high & 0x7fffffff) << 32) | ((high >> 31) * MIN)
27
+ end
28
+
29
+ # @rbs value: Integer
30
+ # @rbs return: Integer
31
+ def self.normalize(value)
32
+ join(low(value), high(value))
33
+ end
34
+
35
+ # Sign extension for an integer field of at most 32 bits.
36
+ # @rbs value: Integer
37
+ # @rbs width: Integer
38
+ # @rbs return: Integer
39
+ def self.extend(value, width)
40
+ limit = 1 << width
41
+ field = value & (limit - 1)
42
+ field >= (limit >> 1) ? field - limit : field
43
+ end
44
+
45
+ # @rbs left: Integer
46
+ # @rbs right: Integer
47
+ # @rbs return: bool
48
+ def self.unsigned_less?(left, right)
49
+ a, b = high(left), high(right)
50
+ a == b ? low(left) < low(right) : a < b
51
+ end
52
+
53
+ # @rbs value: Integer
54
+ # @rbs count: Integer
55
+ # @rbs return: Integer
56
+ def self.shift_left(value, count)
57
+ return value if count == 0
58
+
59
+ lo = low(value)
60
+ if count < 32
61
+ hi = ((high(value) << count) | (lo >> (32 - count))) & MASK
62
+ join((lo << count) & MASK, hi)
63
+ elsif count == 32
64
+ join(0, lo)
65
+ else
66
+ join(0, (lo << (count - 32)) & MASK)
67
+ end
68
+ end
69
+
70
+ # @rbs value: Integer
71
+ # @rbs count: Integer
72
+ # @rbs signed: bool
73
+ # @rbs return: Integer
74
+ def self.shift_right(value, count, signed)
75
+ return value if count == 0
76
+
77
+ hi = signed ? value >> 32 : high(value)
78
+ if count < 32
79
+ # Mask hi before shifting left so the intermediate fits in 63 bits.
80
+ lo = (low(value) >> count) | ((high(value) << (32 - count)) & MASK)
81
+ join(lo, (hi >> count) & MASK)
82
+ else
83
+ lo = count == 32 ? hi : hi >> (count - 32)
84
+ join(lo & MASK, hi < 0 ? MASK : 0)
85
+ end
86
+ end
87
+
88
+ # @rbs value: Integer
89
+ # @rbs return: Integer
90
+ def self.clz32(value)
91
+ count = 32
92
+ while value != 0
93
+ count -= 1
94
+ value >>= 1
95
+ end
96
+ count
97
+ end
98
+
99
+ # @rbs value: Integer
100
+ # @rbs return: Integer
101
+ def self.ctz32(value)
102
+ return 32 if value == 0
103
+
104
+ count = 0
105
+ while (value & 1) == 0
106
+ count += 1
107
+ value >>= 1
108
+ end
109
+ count
110
+ end
111
+
112
+ # @rbs value: Integer
113
+ # @rbs return: Integer
114
+ def self.popcnt32(value)
115
+ count = 0
116
+ while value != 0
117
+ value &= value - 1
118
+ count += 1
119
+ end
120
+ count
121
+ end
122
+
123
+ # @rbs left: Integer
124
+ # @rbs right: Integer
125
+ # @rbs return: Integer
126
+ def self.add_i64(left, right)
127
+ sum = low(left) + low(right)
128
+ join(sum & MASK, (high(left) + high(right) + (sum >> 32)) & MASK)
129
+ end
130
+
131
+ # @rbs left: Integer
132
+ # @rbs right: Integer
133
+ # @rbs return: Integer
134
+ def self.subtract_i64(left, right)
135
+ difference = low(left) - low(right)
136
+ join(difference & MASK, (high(left) - high(right) + (difference >> 32)) & MASK)
137
+ end
138
+
139
+ # Only the four least significant base-2**16 digits contribute.
140
+ # @rbs left: Integer
141
+ # @rbs right: Integer
142
+ # @rbs return: Integer
143
+ def self.multiply_i64(left, right)
144
+ a0, a1, a2, a3 = left & 0xffff, (left >> 16) & 0xffff, (left >> 32) & 0xffff, (left >> 48) & 0xffff
145
+ b0, b1, b2, b3 = right & 0xffff, (right >> 16) & 0xffff, (right >> 32) & 0xffff, (right >> 48) & 0xffff
146
+ w0 = a0 * b0
147
+ w1 = (w0 >> 16) + a0 * b1 + a1 * b0
148
+ w2 = (w1 >> 16) + a0 * b2 + a1 * b1 + a2 * b0
149
+ w3 = (w2 >> 16) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0
150
+ join((w0 & 0xffff) | ((w1 & 0xffff) << 16), (w2 & 0xffff) | ((w3 & 0xffff) << 16))
151
+ end
152
+
153
+ # The sign bit remains part of the unsigned magnitude, including MIN.
154
+ # @rbs opcode: Integer
155
+ # @rbs left: Integer
156
+ # @rbs right: Integer
157
+ # @rbs return: Integer
158
+ def self.divide(opcode, left, right)
159
+ raise Wardite::EvalError, "integer divide by zero" if right == 0
160
+ if opcode == Opcode::I64_DIV_S && left == MIN && right == -1
161
+ raise Wardite::IntegerOverflow, "integer division overflow"
162
+ end
163
+
164
+ signed = opcode == Opcode::I64_DIV_S || opcode == Opcode::I64_REM_S
165
+ remainder = opcode == Opcode::I64_REM_S || opcode == Opcode::I64_REM_U
166
+ left_negative = signed && left < 0
167
+ right_negative = signed && right < 0
168
+ dividend = left_negative ? subtract_i64(0, left) : left
169
+ divisor = right_negative ? subtract_i64(0, right) : right
170
+ result = unsigned_divide(dividend, divisor, remainder)
171
+ negative = remainder ? left_negative : left_negative != right_negative
172
+ negative ? subtract_i64(0, result) : result
173
+ end
174
+
175
+ # Shift in one dividend bit per step. Preserve the extra remainder bit
176
+ # until comparison and subtraction have consumed it.
177
+ # @rbs dividend: Integer
178
+ # @rbs divisor: Integer
179
+ # @rbs remainder: bool
180
+ # @rbs return: Integer
181
+ def self.unsigned_divide(dividend, divisor, remainder)
182
+ nlo, nhi = low(dividend), high(dividend)
183
+ dlo, dhi = low(divisor), high(divisor)
184
+ rlo, rhi = 0, 0
185
+ qlo, qhi = 0, 0
186
+ bit = 63
187
+ while bit >= 0
188
+ incoming = bit >= 32 ? (nhi >> (bit - 32)) & 1 : (nlo >> bit) & 1
189
+ carry = rhi >> 31
190
+ rhi = ((rhi << 1) | (rlo >> 31)) & MASK
191
+ rlo = ((rlo << 1) | incoming) & MASK
192
+ if carry != 0 || rhi > dhi || (rhi == dhi && rlo >= dlo)
193
+ difference = rlo - dlo
194
+ rlo = difference & MASK
195
+ rhi = (rhi - dhi + (difference >> 32)) & MASK
196
+ if bit >= 32
197
+ qhi |= 1 << (bit - 32)
198
+ else
199
+ qlo |= 1 << bit
200
+ end
201
+ end
202
+ bit -= 1
203
+ end
204
+ remainder ? join(rlo, rhi) : join(qlo, qhi)
205
+ end
206
+
207
+ # @rbs opcode: Integer
208
+ # @rbs left: Integer
209
+ # @rbs right: Integer
210
+ # @rbs return: Integer
211
+ def self.calculate(opcode, left, right)
212
+ case opcode
213
+ when Opcode::I64_ADD then add_i64(left, right)
214
+ when Opcode::I64_SUB then subtract_i64(left, right)
215
+ when Opcode::I64_MUL then multiply_i64(left, right)
216
+ when Opcode::I64_DIV_S, Opcode::I64_DIV_U, Opcode::I64_REM_S, Opcode::I64_REM_U
217
+ divide(opcode, left, right)
218
+ when Opcode::I64_EQZ then left == 0 ? 1 : 0
219
+ when Opcode::I64_EQ then left == right ? 1 : 0
220
+ when Opcode::I64_NE then left != right ? 1 : 0
221
+ when Opcode::I64_LTS then left < right ? 1 : 0
222
+ when Opcode::I64_LTU then unsigned_less?(left, right) ? 1 : 0
223
+ when Opcode::I64_GTS then left > right ? 1 : 0
224
+ when Opcode::I64_GTU then unsigned_less?(right, left) ? 1 : 0
225
+ when Opcode::I64_LES then left <= right ? 1 : 0
226
+ when Opcode::I64_LEU then unsigned_less?(right, left) ? 0 : 1
227
+ when Opcode::I64_GES then left >= right ? 1 : 0
228
+ when Opcode::I64_GEU then unsigned_less?(left, right) ? 0 : 1
229
+ when Opcode::I64_AND then left & right
230
+ when Opcode::I64_OR then left | right
231
+ when Opcode::I64_XOR then left ^ right
232
+ when Opcode::I64_SHL then shift_left(left, right & 63)
233
+ when Opcode::I64_SHR_S then shift_right(left, right & 63, true)
234
+ when Opcode::I64_SHR_U then shift_right(left, right & 63, false)
235
+ when Opcode::I64_ROTL, Opcode::I64_ROTR
236
+ count = right & 63
237
+ count = (64 - count) & 63 if opcode == Opcode::I64_ROTR
238
+ shift_left(left, count) | shift_right(left, (64 - count) & 63, false)
239
+ when Opcode::I64_CLZ
240
+ hi = high(left)
241
+ hi == 0 ? 32 + clz32(low(left)) : clz32(hi)
242
+ when Opcode::I64_CTZ
243
+ lo = low(left)
244
+ lo == 0 ? 32 + ctz32(high(left)) : ctz32(lo)
245
+ when Opcode::I64_POPCNT then popcnt32(low(left)) + popcnt32(high(left))
246
+ when Opcode::I32_WRAP_I64 then low(left)
247
+ when Opcode::I64_EXTEND_I32_U then left
248
+ when Opcode::I64_EXTEND_I32_S, Opcode::I64_EXTEND32_S then extend(left, 32)
249
+ when Opcode::I64_EXTEND16_S then extend(left, 16)
250
+ when Opcode::I64_EXTEND8_S then extend(left, 8)
251
+ else
252
+ raise Wardite::EvalError, "invalid i64 opcode"
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ class Metrics
7
+ attr_reader :opcode_counts #: Array[Integer]
8
+ attr_reader :register_reads #: Integer
9
+ attr_reader :register_writes #: Integer
10
+ attr_accessor :frames_created #: Integer
11
+ attr_accessor :host_calls #: Integer
12
+ attr_accessor :root_defined_entries #: Integer
13
+
14
+ def initialize
15
+ @opcode_counts = Array.new(Opcode::NAMES.size, 0)
16
+ @register_reads = 0
17
+ @register_writes = 0
18
+ @frames_created = 0
19
+ @host_calls = 0
20
+ @root_defined_entries = 0
21
+ @indirect_defined_entries = 0
22
+ end
23
+
24
+ # @rbs opcode: Integer
25
+ # @rbs reads: Integer
26
+ # @rbs writes: Integer
27
+ # @rbs return: void
28
+ def record(opcode, reads, writes)
29
+ @opcode_counts[opcode] += 1
30
+ @register_reads += reads
31
+ @register_writes += writes
32
+ end
33
+
34
+ # @rbs return: Integer
35
+ def dispatches
36
+ @opcode_counts.sum
37
+ end
38
+
39
+ # @rbs return: Integer
40
+ def copies
41
+ @opcode_counts[Opcode::COPY]
42
+ end
43
+
44
+ # @rbs parameter_count: Integer
45
+ # @rbs return: void
46
+ def enter_indirect_defined(parameter_count)
47
+ @indirect_defined_entries += 1
48
+ @register_writes += parameter_count
49
+ end
50
+
51
+ # @rbs return: Integer
52
+ def indirect_calls
53
+ @opcode_counts[Opcode::CALL_INDIRECT]
54
+ end
55
+
56
+ # @rbs return: Integer
57
+ def function_entries
58
+ @root_defined_entries + @opcode_counts[Opcode::CALL] + @indirect_defined_entries + @host_calls
59
+ end
60
+
61
+ # @rbs return: String
62
+ def report
63
+ counters = "dispatches=#{dispatches} copies=#{copies} register_reads=#{@register_reads} " \
64
+ "register_writes=#{@register_writes} function_entries=#{function_entries} frames_created=#{@frames_created}"
65
+ if @host_calls > 0 || indirect_calls > 0
66
+ counters += " host_calls=#{@host_calls} indirect_calls=#{indirect_calls}"
67
+ end
68
+ counters
69
+ end
70
+ end
71
+
72
+ # Profiling is compiled into a separate instrumented MIR. The ordinary
73
+ # dispatch loop never tests an enabled flag or updates per-instruction stats.
74
+ class Instrumentation
75
+ # @rbs ir: Wardite::Efflorescence::IR
76
+ # @rbs return: Wardite::Efflorescence::IR
77
+ def instrument(ir)
78
+ output = ir.copy
79
+ output.functions.each do |function|
80
+ rewrite = Rewrite.new(function)
81
+ function.instructions.each_with_index do |instruction, pc|
82
+ rewrite.mark(pc)
83
+ reads = function.reads(instruction).size
84
+ writes = Opcode.destination(instruction) >= 0 ? 1 : 0
85
+ # Parameter copies and the eventual return destination write count
86
+ # at CALL; callee RETURN accounts for reading the returned value.
87
+ writes += instruction.size - 3 if instruction[0] == Opcode::CALL
88
+ rewrite.emit(Instruction.new([Opcode::PROFILE, instruction[0], reads, writes]))
89
+ rewrite.emit(instruction.copy)
90
+ end
91
+ rewrite.finish
92
+ end
93
+ output
94
+ end
95
+ end
96
+
97
+ class Inspection
98
+ # @rbs ir: Wardite::Efflorescence::IR
99
+ # @rbs return: String
100
+ def disassemble(ir)
101
+ lines = [] #: Array[String]
102
+ ir.hosts.each_with_index do |host, i|
103
+ lines << "host #{i}: #{host.external.name} #{host.entry.parameter_types.inspect} -> #{host.entry.result_types.inspect}"
104
+ end
105
+ ir.functions.each do |function|
106
+ lines << "function #{function.index}: params=#{function.parameter_count} locals=#{function.local_count} registers=#{function.register_count}"
107
+ if function.uses_i64 || function.uses_float
108
+ lines << " types: #{function.register_types.inspect} -> #{function.result_types.inspect}"
109
+ function.constant_registers.each do |register|
110
+ type = function.register_types.fetch(register)
111
+ value = function.initial_value(register)
112
+ if FloatBits.float_type?(type)
113
+ high = type == :f64 ? I64Arithmetic.high(value).to_s(16).rjust(8, "0") : ""
114
+ low = I64Arithmetic.low(value).to_s(16).rjust(8, "0")
115
+ lines << " const r#{register}: #{type} 0x#{high}#{low}"
116
+ else
117
+ lines << " const r#{register}: #{value}"
118
+ end
119
+ end
120
+ end
121
+ function.handlers.each_with_index do |handler, i|
122
+ handler.clauses.each do |clause|
123
+ lines << " handler #{i}: tag=#{clause.tag_index} payload=#{clause.payload_registers.inspect} ref=#{clause.reference_register} target=#{clause.target_pc} depth=#{clause.handler_depth}"
124
+ end
125
+ end
126
+ function.throw_sites.each_with_index do |site, i|
127
+ lines << " throw #{i}: tag=#{site.tag_index} payload=#{site.payload_registers.inspect}"
128
+ end
129
+ function.memory_sites.each_with_index do |site, i|
130
+ lines << " memory #{i}: sources=#{site.sources.inspect} memory=#{site.memory_index} source_memory=#{site.source_memory_index} data=#{site.data_index}"
131
+ end
132
+ function.dynamic_sites.each_with_index do |site, i|
133
+ target = site.host_index >= 0 ? "host=#{site.host_index}" : "table=#{site.table_index}"
134
+ lines << " call #{i}: #{target} #{site.parameter_types.inspect} -> #{site.result_types.inspect} arguments=#{site.arguments.inspect}"
135
+ end
136
+ function.instructions.each_with_index do |instruction, pc|
137
+ operands = [] #: Array[String]
138
+ index = 1
139
+ while index < instruction.size
140
+ operands << instruction[index].to_s
141
+ index += 1
142
+ end
143
+ name = Opcode::NAMES.fetch(instruction[0])
144
+ lines << " #{pc}: #{name} #{operands.join(', ')}"
145
+ end
146
+ end
147
+ lines.join("\n")
148
+ end
149
+
150
+ # @rbs ir: Wardite::Efflorescence::IR
151
+ # @rbs return: String
152
+ def statistics(ir)
153
+ instructions = 0
154
+ copies = 0
155
+ registers = 0
156
+ ir.functions.each do |function|
157
+ instructions += function.instructions.size
158
+ copies += function.instructions.count { |instruction| instruction[0] == Opcode::COPY }
159
+ registers += function.register_count
160
+ end
161
+ "instructions=#{instructions} copies=#{copies} registers=#{registers}"
162
+ end
163
+ end
164
+ end
165
+ end