omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / crates / aienos-c1-tree / c1_tree.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-c1-tree/c1_tree.om: Pure-Omega C1 Copy-On-Write Prefix Tree engine. |
| 3 | note Converted from aienux crates/aienos-c1-tree/src/{page,pool,metrics,tree,lib}.rs. |
| 4 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-c1-tree/c1_tree.om ... |
| 5 | |
| 6 | const C1_PAGE_CAP ulong 64 |
| 7 | const C1_KV_ELEMS_PER_TOK ulong 128 |
| 8 | const C1_PAGE_KV_BYTES ulong 32768 |
| 9 | const C1_MAX_PAGES ulong 64 |
| 10 | const C1_MAX_BRANCHES ulong 16 |
| 11 | const C1_MAX_BPAGES ulong 16 |
| 12 | |
| 13 | const C1_OK int 0 |
| 14 | const C1_ERR_NOT_FOUND int -1 |
| 15 | const C1_ERR_EVICTED int -2 |
| 16 | const C1_ERR_EXISTS int -3 |
| 17 | const C1_ERR_CORRUPT int -4 |
| 18 | const C1_ERR_QUOTA int -5 |
| 19 | const C1_ERR_CAPACITY int -6 |
| 20 | |
| 21 | note Physical page pool state |
| 22 | global c1_next_pid ulong 1 |
| 23 | global c1_p_used uchar 64 |
| 24 | global c1_p_id ulong 64 |
| 25 | global c1_p_len ulong 64 |
| 26 | global c1_p_ref ulong 64 |
| 27 | global c1_p_toks uint 4096 |
| 28 | global c1_p_cksum uchar 2048 |
| 29 | |
| 30 | note Branch table state |
| 31 | global c1_b_used uchar 16 |
| 32 | global c1_b_id ulong 16 |
| 33 | global c1_b_evicted uchar 16 |
| 34 | global c1_b_npages ulong 16 |
| 35 | global c1_b_ntoks ulong 16 |
| 36 | global c1_b_pids ulong 256 |
| 37 | |
| 38 | note Reservation receipt output: 0=branch_id, 1=tokens_reserved, 2=pages_reserved, 3=tick |
| 39 | global c1_res_out ulong 4 |
| 40 | |
| 41 | note Metrics output: 0=phys_pages, 1=phys_bytes, 2=virt_tokens, 3=phys_token_cap |
| 42 | global c1_met_out ulong 4 |
| 43 | |
| 44 | fn c1_init to void |
| 45 | put c1_next_pid 0 1 |
| 46 | for i ulong C1_MAX_PAGES |
| 47 | put c1_p_used i 0 |
| 48 | put c1_p_id i 0 |
| 49 | put c1_p_len i 0 |
| 50 | put c1_p_ref i 0 |
| 51 | end |
| 52 | for i ulong C1_MAX_BRANCHES |
| 53 | put c1_b_used i 0 |
| 54 | put c1_b_id i 0 |
| 55 | put c1_b_evicted i 0 |
| 56 | put c1_b_npages i 0 |
| 57 | put c1_b_ntoks i 0 |
| 58 | end |
| 59 | end |
| 60 | |
| 61 | fn c1_find_page pid ulong to long |
| 62 | for i ulong C1_MAX_PAGES |
| 63 | if and eq get c1_p_used i 1 eq get c1_p_id i pid |
| 64 | ret long i |
| 65 | end |
| 66 | end |
| 67 | ret -1 |
| 68 | end |
| 69 | |
| 70 | fn c1_find_branch bid ulong to long |
| 71 | for i ulong C1_MAX_BRANCHES |
| 72 | if and eq get c1_b_used i 1 eq get c1_b_id i bid |
| 73 | ret long i |
| 74 | end |
| 75 | end |
| 76 | ret -1 |
| 77 | end |
| 78 | |
| 79 | fn c1_page_hash slot ulong out_ck edit uchar * to void |
| 80 | ctx = new ulong 40 0 |
| 81 | sha256_init ctx |
| 82 | pid = get c1_p_id slot |
| 83 | hdr = new uchar 8 0 |
| 84 | for i ulong 8 |
| 85 | sh = mul sub 7 i 8 |
| 86 | put hdr i uchar and shr pid sh 0xff |
| 87 | end |
| 88 | sha256_update ctx hdr |
| 89 | plen = get c1_p_len slot |
| 90 | tbase = mul slot C1_PAGE_CAP |
| 91 | tbuf = new uchar 4 0 |
| 92 | for i ulong plen bound 64 |
| 93 | tok = get c1_p_toks add tbase i |
| 94 | put tbuf 0 uchar and shr tok 24 0xff |
| 95 | put tbuf 1 uchar and shr tok 16 0xff |
| 96 | put tbuf 2 uchar and shr tok 8 0xff |
| 97 | put tbuf 3 uchar and tok 0xff |
| 98 | sha256_update ctx tbuf |
| 99 | end |
| 100 | sha256_final ctx out_ck |
| 101 | end |
| 102 | |
| 103 | fn c1_page_recompute slot ulong to void |
| 104 | ck = new uchar 32 0 |
| 105 | c1_page_hash slot ck |
| 106 | cbase = mul slot 32 |
| 107 | for i ulong 32 |
| 108 | put c1_p_cksum add cbase i get ck i |
| 109 | end |
| 110 | end |
| 111 | |
| 112 | fn c1_page_verify slot ulong to bool |
| 113 | ck = new uchar 32 0 |
| 114 | c1_page_hash slot ck |
| 115 | cbase = mul slot 32 |
| 116 | for i ulong 32 |
| 117 | if ne get c1_p_cksum add cbase i get ck i |
| 118 | ret false |
| 119 | end |
| 120 | end |
| 121 | ret true |
| 122 | end |
| 123 | |
| 124 | fn c1_pool_alloc to ulong |
| 125 | for i ulong C1_MAX_PAGES |
| 126 | if eq get c1_p_used i 0 |
| 127 | pid = get c1_next_pid 0 |
| 128 | put c1_next_pid 0 add pid 1 |
| 129 | put c1_p_used i 1 |
| 130 | put c1_p_id i pid |
| 131 | put c1_p_len i 0 |
| 132 | put c1_p_ref i 1 |
| 133 | c1_page_recompute i |
| 134 | ret pid |
| 135 | end |
| 136 | end |
| 137 | ret 0 |
| 138 | end |
| 139 | |
| 140 | fn c1_pool_retain pid ulong to void |
| 141 | sl = c1_find_page pid |
| 142 | if ge sl 0 |
| 143 | u = ulong sl |
| 144 | put c1_p_ref u add get c1_p_ref u 1 |
| 145 | end |
| 146 | end |
| 147 | |
| 148 | fn c1_pool_release pid ulong to void |
| 149 | sl = c1_find_page pid |
| 150 | if ge sl 0 |
| 151 | u = ulong sl |
| 152 | r = get c1_p_ref u |
| 153 | if gt r 1 |
| 154 | put c1_p_ref u sub r 1 |
| 155 | else |
| 156 | put c1_p_ref u 0 |
| 157 | put c1_p_used u 0 |
| 158 | put c1_p_id u 0 |
| 159 | put c1_p_len u 0 |
| 160 | end |
| 161 | end |
| 162 | end |
| 163 | |
| 164 | fn c1_copy_page_tokens su ulong du ulong to void |
| 165 | plen = get c1_p_len su |
| 166 | put c1_p_len du plen |
| 167 | sbase = mul su C1_PAGE_CAP |
| 168 | dbase = mul du C1_PAGE_CAP |
| 169 | for i ulong plen bound 64 |
| 170 | put c1_p_toks add dbase i get c1_p_toks add sbase i |
| 171 | end |
| 172 | c1_page_recompute du |
| 173 | end |
| 174 | |
| 175 | fn c1_pool_cow src_pid ulong to ulong |
| 176 | src_sl = c1_find_page src_pid |
| 177 | if lt src_sl 0 |
| 178 | ret 0 |
| 179 | end |
| 180 | new_pid = c1_pool_alloc |
| 181 | if eq new_pid 0 |
| 182 | ret 0 |
| 183 | end |
| 184 | dst_sl = c1_find_page new_pid |
| 185 | if lt dst_sl 0 |
| 186 | ret 0 |
| 187 | end |
| 188 | c1_copy_page_tokens ulong src_sl ulong dst_sl |
| 189 | c1_pool_release src_pid |
| 190 | ret new_pid |
| 191 | end |
| 192 | |
| 193 | fn c1_pool_active_count to ulong |
| 194 | cnt = ulong 0 |
| 195 | for i ulong C1_MAX_PAGES |
| 196 | if eq get c1_p_used i 1 |
| 197 | cnt = add cnt 1 |
| 198 | end |
| 199 | end |
| 200 | ret cnt |
| 201 | end |
| 202 | |
| 203 | fn c1_pool_memory_bytes to ulong |
| 204 | ret mul c1_pool_active_count C1_PAGE_KV_BYTES |
| 205 | end |
| 206 | |
| 207 | fn c1_page_append_slot slot ulong tok uint to bool |
| 208 | plen = get c1_p_len slot |
| 209 | if ge plen C1_PAGE_CAP |
| 210 | ret false |
| 211 | end |
| 212 | tbase = mul slot C1_PAGE_CAP |
| 213 | put c1_p_toks add tbase plen tok |
| 214 | put c1_p_len slot add plen 1 |
| 215 | c1_page_recompute slot |
| 216 | ret true |
| 217 | end |
| 218 | |
| 219 | fn c1_append_single bslot ulong tok uint to int |
| 220 | np = get c1_b_npages bslot |
| 221 | bbase = mul bslot C1_MAX_BPAGES |
| 222 | if eq np 0 |
| 223 | pid = c1_pool_alloc |
| 224 | if eq pid 0 |
| 225 | ret C1_ERR_CAPACITY |
| 226 | end |
| 227 | psl = c1_find_page pid |
| 228 | if lt psl 0 |
| 229 | ret C1_ERR_CORRUPT |
| 230 | end |
| 231 | ok = c1_page_append_slot ulong psl tok |
| 232 | if not ok |
| 233 | ret C1_ERR_CORRUPT |
| 234 | end |
| 235 | put c1_b_pids bbase pid |
| 236 | put c1_b_npages bslot 1 |
| 237 | put c1_b_ntoks bslot add get c1_b_ntoks bslot 1 |
| 238 | ret C1_OK |
| 239 | end |
| 240 | |
| 241 | last_idx = sub np 1 |
| 242 | last_pid = get c1_b_pids add bbase last_idx |
| 243 | psl = c1_find_page last_pid |
| 244 | if lt psl 0 |
| 245 | ret C1_ERR_CORRUPT |
| 246 | end |
| 247 | pu = ulong psl |
| 248 | if not c1_page_verify pu |
| 249 | ret C1_ERR_CORRUPT |
| 250 | end |
| 251 | if lt get c1_p_len pu C1_PAGE_CAP |
| 252 | if eq get c1_p_ref pu 1 |
| 253 | ok = c1_page_append_slot pu tok |
| 254 | if not ok |
| 255 | ret C1_ERR_CORRUPT |
| 256 | end |
| 257 | else |
| 258 | new_pid = c1_pool_cow last_pid |
| 259 | if eq new_pid 0 |
| 260 | ret C1_ERR_CAPACITY |
| 261 | end |
| 262 | nsl = c1_find_page new_pid |
| 263 | if lt nsl 0 |
| 264 | ret C1_ERR_CORRUPT |
| 265 | end |
| 266 | ok = c1_page_append_slot ulong nsl tok |
| 267 | if not ok |
| 268 | ret C1_ERR_CORRUPT |
| 269 | end |
| 270 | put c1_b_pids add bbase last_idx new_pid |
| 271 | end |
| 272 | else |
| 273 | if ge np C1_MAX_BPAGES |
| 274 | ret C1_ERR_CAPACITY |
| 275 | end |
| 276 | new_pid = c1_pool_alloc |
| 277 | if eq new_pid 0 |
| 278 | ret C1_ERR_CAPACITY |
| 279 | end |
| 280 | nsl = c1_find_page new_pid |
| 281 | if lt nsl 0 |
| 282 | ret C1_ERR_CORRUPT |
| 283 | end |
| 284 | ok = c1_page_append_slot ulong nsl tok |
| 285 | if not ok |
| 286 | ret C1_ERR_CORRUPT |
| 287 | end |
| 288 | put c1_b_pids add bbase np new_pid |
| 289 | put c1_b_npages bslot add np 1 |
| 290 | end |
| 291 | put c1_b_ntoks bslot add get c1_b_ntoks bslot 1 |
| 292 | ret C1_OK |
| 293 | end |
| 294 | |
| 295 | fn c1_create_root bid ulong toks view uint * to int |
| 296 | if ge c1_find_branch bid 0 |
| 297 | ret C1_ERR_EXISTS |
| 298 | end |
| 299 | bslot = ulong C1_MAX_BRANCHES |
| 300 | for i ulong C1_MAX_BRANCHES |
| 301 | if eq get c1_b_used i 0 |
| 302 | bslot = i |
| 303 | break |
| 304 | end |
| 305 | end |
| 306 | if eq bslot C1_MAX_BRANCHES |
| 307 | ret C1_ERR_CAPACITY |
| 308 | end |
| 309 | put c1_b_used bslot 1 |
| 310 | put c1_b_id bslot bid |
| 311 | put c1_b_evicted bslot 0 |
| 312 | put c1_b_npages bslot 0 |
| 313 | put c1_b_ntoks bslot 0 |
| 314 | for i ulong len toks bound 1024 |
| 315 | rc = c1_append_single bslot get toks i |
| 316 | if ne rc C1_OK |
| 317 | ret rc |
| 318 | end |
| 319 | end |
| 320 | ret C1_OK |
| 321 | end |
| 322 | |
| 323 | fn c1_fork_branch parent_id ulong child_id ulong to int |
| 324 | psl = c1_find_branch parent_id |
| 325 | if lt psl 0 |
| 326 | ret C1_ERR_NOT_FOUND |
| 327 | end |
| 328 | pu = ulong psl |
| 329 | if eq get c1_b_evicted pu 1 |
| 330 | ret C1_ERR_EVICTED |
| 331 | end |
| 332 | if ge c1_find_branch child_id 0 |
| 333 | ret C1_ERR_EXISTS |
| 334 | end |
| 335 | cslot = ulong C1_MAX_BRANCHES |
| 336 | for i ulong C1_MAX_BRANCHES |
| 337 | if eq get c1_b_used i 0 |
| 338 | cslot = i |
| 339 | break |
| 340 | end |
| 341 | end |
| 342 | if eq cslot C1_MAX_BRANCHES |
| 343 | ret C1_ERR_CAPACITY |
| 344 | end |
| 345 | np = get c1_b_npages pu |
| 346 | pbase = mul pu C1_MAX_BPAGES |
| 347 | cbase = mul cslot C1_MAX_BPAGES |
| 348 | for i ulong np bound 16 |
| 349 | pid = get c1_b_pids add pbase i |
| 350 | c1_pool_retain pid |
| 351 | put c1_b_pids add cbase i pid |
| 352 | end |
| 353 | put c1_b_used cslot 1 |
| 354 | put c1_b_id cslot child_id |
| 355 | put c1_b_evicted cslot 0 |
| 356 | put c1_b_npages cslot np |
| 357 | put c1_b_ntoks cslot get c1_b_ntoks pu |
| 358 | ret C1_OK |
| 359 | end |
| 360 | |
| 361 | fn c1_reserve_step bid ulong num_toks ulong tick ulong max_pages ulong to int |
| 362 | bsl = c1_find_branch bid |
| 363 | if lt bsl 0 |
| 364 | ret C1_ERR_NOT_FOUND |
| 365 | end |
| 366 | bu = ulong bsl |
| 367 | if eq get c1_b_evicted bu 1 |
| 368 | ret C1_ERR_EVICTED |
| 369 | end |
| 370 | needed = ulong 0 |
| 371 | if gt num_toks 0 |
| 372 | needed = div add num_toks sub C1_PAGE_CAP 1 C1_PAGE_CAP |
| 373 | end |
| 374 | alloc = c1_pool_active_count |
| 375 | if gt add alloc needed max_pages |
| 376 | ret C1_ERR_QUOTA |
| 377 | end |
| 378 | put c1_res_out 0 bid |
| 379 | put c1_res_out 1 num_toks |
| 380 | put c1_res_out 2 needed |
| 381 | put c1_res_out 3 tick |
| 382 | ret C1_OK |
| 383 | end |
| 384 | |
| 385 | fn c1_append_tokens bid ulong toks view uint * to int |
| 386 | bsl = c1_find_branch bid |
| 387 | if lt bsl 0 |
| 388 | ret C1_ERR_NOT_FOUND |
| 389 | end |
| 390 | bu = ulong bsl |
| 391 | if eq get c1_b_evicted bu 1 |
| 392 | ret C1_ERR_EVICTED |
| 393 | end |
| 394 | for i ulong len toks bound 1024 |
| 395 | rc = c1_append_single bu get toks i |
| 396 | if ne rc C1_OK |
| 397 | ret rc |
| 398 | end |
| 399 | end |
| 400 | ret C1_OK |
| 401 | end |
| 402 | |
| 403 | fn c1_copy_page_out pu ulong dst edit uint * pos ulong to ulong |
| 404 | plen = get c1_p_len pu |
| 405 | tbase = mul pu C1_PAGE_CAP |
| 406 | for j ulong plen bound 64 |
| 407 | put dst add pos j get c1_p_toks add tbase j |
| 408 | end |
| 409 | ret add pos plen |
| 410 | end |
| 411 | |
| 412 | fn c1_get_tokens bid ulong dst edit uint * to long |
| 413 | bsl = c1_find_branch bid |
| 414 | if lt bsl 0 |
| 415 | ret long C1_ERR_NOT_FOUND |
| 416 | end |
| 417 | bu = ulong bsl |
| 418 | if eq get c1_b_evicted bu 1 |
| 419 | ret long C1_ERR_EVICTED |
| 420 | end |
| 421 | if gt get c1_b_ntoks bu len dst |
| 422 | ret long C1_ERR_CAPACITY |
| 423 | end |
| 424 | np = get c1_b_npages bu |
| 425 | bbase = mul bu C1_MAX_BPAGES |
| 426 | pos = ulong 0 |
| 427 | for i ulong np bound 16 |
| 428 | psl = c1_find_page get c1_b_pids add bbase i |
| 429 | if lt psl 0 |
| 430 | ret long C1_ERR_CORRUPT |
| 431 | end |
| 432 | if not c1_page_verify ulong psl |
| 433 | ret long C1_ERR_CORRUPT |
| 434 | end |
| 435 | pos = c1_copy_page_out ulong psl dst pos |
| 436 | end |
| 437 | ret long pos |
| 438 | end |
| 439 | |
| 440 | fn c1_evict_branch bid ulong to int |
| 441 | bsl = c1_find_branch bid |
| 442 | if lt bsl 0 |
| 443 | ret C1_ERR_NOT_FOUND |
| 444 | end |
| 445 | bu = ulong bsl |
| 446 | if eq get c1_b_evicted bu 1 |
| 447 | ret C1_OK |
| 448 | end |
| 449 | np = get c1_b_npages bu |
| 450 | bbase = mul bu C1_MAX_BPAGES |
| 451 | for i ulong np bound 16 |
| 452 | c1_pool_release get c1_b_pids add bbase i |
| 453 | end |
| 454 | put c1_b_npages bu 0 |
| 455 | put c1_b_evicted bu 1 |
| 456 | ret C1_OK |
| 457 | end |
| 458 | |
| 459 | fn c1_remove_branch bid ulong to int |
| 460 | bsl = c1_find_branch bid |
| 461 | if lt bsl 0 |
| 462 | ret C1_ERR_NOT_FOUND |
| 463 | end |
| 464 | bu = ulong bsl |
| 465 | if eq get c1_b_evicted bu 0 |
| 466 | np = get c1_b_npages bu |
| 467 | bbase = mul bu C1_MAX_BPAGES |
| 468 | for i ulong np bound 16 |
| 469 | c1_pool_release get c1_b_pids add bbase i |
| 470 | end |
| 471 | end |
| 472 | put c1_b_used bu 0 |
| 473 | put c1_b_id bu 0 |
| 474 | put c1_b_npages bu 0 |
| 475 | put c1_b_ntoks bu 0 |
| 476 | put c1_b_evicted bu 0 |
| 477 | ret C1_OK |
| 478 | end |
| 479 | |
| 480 | fn c1_reconstruct_branch bid ulong toks view uint * to int |
| 481 | bsl = c1_find_branch bid |
| 482 | if lt bsl 0 |
| 483 | ret C1_ERR_NOT_FOUND |
| 484 | end |
| 485 | bu = ulong bsl |
| 486 | if eq get c1_b_evicted bu 0 |
| 487 | np = get c1_b_npages bu |
| 488 | bbase = mul bu C1_MAX_BPAGES |
| 489 | for i ulong np bound 16 |
| 490 | c1_pool_release get c1_b_pids add bbase i |
| 491 | end |
| 492 | end |
| 493 | put c1_b_npages bu 0 |
| 494 | put c1_b_ntoks bu 0 |
| 495 | put c1_b_evicted bu 0 |
| 496 | for i ulong len toks bound 1024 |
| 497 | rc = c1_append_single bu get toks i |
| 498 | if ne rc C1_OK |
| 499 | ret rc |
| 500 | end |
| 501 | end |
| 502 | ret C1_OK |
| 503 | end |
| 504 | |
| 505 | fn c1_compute_metrics to void |
| 506 | pp = c1_pool_active_count |
| 507 | pb = mul pp C1_PAGE_KV_BYTES |
| 508 | vt = ulong 0 |
| 509 | for i ulong C1_MAX_BRANCHES |
| 510 | if eq get c1_b_used i 1 |
| 511 | vt = add vt get c1_b_ntoks i |
| 512 | end |
| 513 | end |
| 514 | put c1_met_out 0 pp |
| 515 | put c1_met_out 1 pb |
| 516 | put c1_met_out 2 vt |
| 517 | put c1_met_out 3 mul pp C1_PAGE_CAP |
| 518 | end |