Commit ac4948e
Phase L+M: resonance caching + typed HIR with specialized dispatch
## Phase L — Resonance / portal caching
New unary_cache_pass in bytecode_opt.rs. Precomputes pure-unary
harmonic ops on constants at compile time, before the constant folder
so chained arithmetic sees a single constant:
LoadConst(N); Resonance → LoadConst(precomputed_float)
LoadConst(N); Fold1 → LoadConst(snapped_int)
LoadConst(N); IsFibonacci → LoadConst(1 or 0)
LoadConst(N); Fibonacci → LoadConst(fib(N))
LoadConst(N); HimScore → LoadConst(precomputed_float)
LoadConst(N); Neg → LoadConst(-N)
LoadConst(N); BitNot → LoadConst(!N)
LoadConst(B); Not → LoadConst(!B)
Mixed example: res(89) + 0.5 folds in two passes — cache res(89) -> 1.0,
then fold 1.0 + 0.5 -> 1.5, collapsing two ops to a single LoadConst.
New stats counter unary_calls_cached. Aggregated into total via a new
accumulate() helper (previously each stat was added by hand, easy to
miss).
## Phase M — Typed HIR with specialized dispatch
Compiler now tracks variable types as it lowers AST to bytecode.
TypeTag = Option<&'static str> over "int" / "float" / "string" / "bool"
/ "array" (None = couldn't prove statically; runtime polymorphism
applies as before).
Sources of type info:
- Typed function parameters: fn add(x: int, y: int)
- Return-type annotations: fn foo() -> int
- Variable decls inferred from value: h x = 89 ⇒ int
- Arithmetic on known-typed operands: int + int ⇒ int
- Comparisons / bitwise: always bool / int
- Built-in call sites: large catalog of fixed return types
(fibonacci -> int, sqrt -> float, str_uppercase -> string, etc.)
New typed-fast-path opcodes that skip the runtime is_float() check:
Op::AddInt, Op::SubInt, Op::MulInt
Op::AddFloat, Op::SubFloat, Op::MulFloat
Compiler emits these in place of polymorphic Op::Add etc. when BOTH
operands' inferred types match. The constant folder learned to fold
the typed variants too — `1 + 2 + 3` with both operands int still
folds end-to-end.
CompiledFunction gained param_types: Vec<Option<String>> and
return_type: Option<String> so cross-function type info survives
into the compiled module (useful for future passes and debugging).
## Consistency fix
Tree-walk is_fibonacci() now returns HInt(0)/HInt(1) instead of Bool,
matching the VM's Op::IsFibonacci and the canonical Python OMC idiom
`if is_fibonacci(x) == 1`. Both paths now agree on the wire format.
## Tests
125 passing across the workspace (was 118). +7 new unit tests in
bytecode_opt::tests for the caching pass:
- caches_resonance_of_constant
- caches_phi_fold_of_constant
- caches_fibonacci_of_constant
- caches_is_fibonacci_of_constant
- caches_unary_minus_of_constant
- caches_bitnot_of_constant
- chains_unary_cache_then_constant_fold
## Compatibility
Canonical sweep still 22/30 in both tree-walk and VM. No regressions.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 888779a commit ac4948e
7 files changed
Lines changed: 474 additions & 27 deletions
File tree
- omnimcode-core/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
7 | 41 | | |
8 | 42 | | |
9 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
55 | 65 | | |
56 | 66 | | |
57 | 67 | | |
| |||
112 | 122 | | |
113 | 123 | | |
114 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
115 | 131 | | |
116 | 132 | | |
117 | 133 | | |
| |||
129 | 145 | | |
130 | 146 | | |
131 | 147 | | |
| 148 | + | |
| 149 | + | |
132 | 150 | | |
133 | 151 | | |
134 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| |||
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
37 | 45 | | |
38 | 46 | | |
39 | 47 | | |
| |||
98 | 106 | | |
99 | 107 | | |
100 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
101 | 186 | | |
102 | 187 | | |
103 | 188 | | |
| |||
131 | 216 | | |
132 | 217 | | |
133 | 218 | | |
134 | | - | |
135 | | - | |
136 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
137 | 222 | | |
138 | 223 | | |
139 | 224 | | |
| |||
153 | 238 | | |
154 | 239 | | |
155 | 240 | | |
156 | | - | |
157 | | - | |
158 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
159 | 244 | | |
160 | 245 | | |
161 | 246 | | |
| |||
204 | 289 | | |
205 | 290 | | |
206 | 291 | | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 292 | + | |
212 | 293 | | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
| 294 | + | |
218 | 295 | | |
219 | 296 | | |
220 | 297 | | |
221 | 298 | | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
222 | 307 | | |
223 | 308 | | |
224 | 309 | | |
| |||
307 | 392 | | |
308 | 393 | | |
309 | 394 | | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
310 | 488 | | |
0 commit comments