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 / seed / ng / pack / sem_pack.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/pack/sem_pack.om: Compile-on-Arrive Semantic Package (OMPK v1) wire codec, |
| 3 | note fail-closed SHA-256 verifier, content-addressed package store with DFS cycle detection, |
| 4 | note and TargetDesc-gated Realization Cache integration. |
| 5 | note |
| 6 | note Library unit for program mode (no om2 line, no fn main; prefix pk_ / PK_). |
| 7 | note Expects seed/ng/sha256.om and seed/ng/target/target_desc.om linked before it. |
| 8 | note |
| 9 | note OMPK v1 Binary Wire Layout: |
| 10 | note [0..3] magic "OMPK" (79, 77, 80, 75) |
| 11 | note [4..7] version u32 LE (1) |
| 12 | note [8..39] sem_id (32-byte SHA-256 of canonical OIR payload) |
| 13 | note [40..71] func_id (32-byte canonical semantic function digest; equals sem_id when unset) |
| 14 | note [72..119] 6x u64 LE physical silicon contract: |
| 15 | note [72..79] req_exec |
| 16 | note [80..87] req_smem |
| 17 | note [88..95] req_regs |
| 18 | note [96..103] req_subgroup |
| 19 | note [104..111] req_mma |
| 20 | note [112..119] req_feat |
| 21 | note [120..123] import_count u32 LE (max 16) |
| 22 | note [124..127] ir_len u32 LE (max 4096) |
| 23 | note [128 .. 128 + import_count*32) digest-addressed import table (import_sem_ids) |
| 24 | note [128 + import_count*32 .. end) raw canonical OIR payload bytes (ir_len bytes) |
| 25 | |
| 26 | const PK_MAGIC_0 ulong 79 |
| 27 | const PK_MAGIC_1 ulong 77 |
| 28 | const PK_MAGIC_2 ulong 80 |
| 29 | const PK_MAGIC_3 ulong 75 |
| 30 | const PK_VERSION ulong 1 |
| 31 | const PK_VERSION_2 ulong 2 |
| 32 | const PK_HDR_LEN ulong 128 |
| 33 | const PK_V2_HDR_LEN ulong 160 |
| 34 | const PK_V2_EXT_TAG ulong 844516176 |
| 35 | const PK_DIGEST_LEN ulong 32 |
| 36 | const PK_MAX_IMPORTS ulong 16 |
| 37 | const PK_MAX_IR_LEN ulong 4096 |
| 38 | const PK_MAX_PKG_LEN ulong 4768 |
| 39 | const PK_BUF_CAP ulong 8192 |
| 40 | |
| 41 | const PK_STORE_CAP ulong 32 |
| 42 | const PK_STORE_STRIDE ulong 8192 |
| 43 | const PK_NONE ulong 999999 |
| 44 | |
| 45 | note Status and refusal codes (1..6 mirror TDK_ERR_EXEC..TDK_ERR_FEAT from target_desc.om) |
| 46 | const PK_OK ulong 0 |
| 47 | const PK_ERR_MAGIC ulong 10 |
| 48 | const PK_ERR_VERSION ulong 11 |
| 49 | const PK_ERR_LENGTH ulong 12 |
| 50 | const PK_ERR_DIGEST ulong 13 |
| 51 | const PK_ERR_IMPORTS ulong 14 |
| 52 | const PK_ERR_STORE_FULL ulong 15 |
| 53 | const PK_ERR_MISSING_IMPORT ulong 16 |
| 54 | const PK_ERR_CYCLE ulong 17 |
| 55 | const PK_ERR_PROOF ulong 18 |
| 56 | const PK_ERR_CACHE_FULL ulong 19 |
| 57 | const PK_ERR_BAD_TARGET ulong 20 |
| 58 | const PK_ERR_NOT_FOUND ulong 21 |
| 59 | const PK_ERR_CONTRACT ulong 22 |
| 60 | |
| 61 | note Active OMPK wire buffer and exact byte length |
| 62 | global pk_buf uchar 8192 |
| 63 | global pk_len ulong 1 |
| 64 | |
| 65 | note Builder staging state for pk_pack |
| 66 | global pk_ir_in uchar 4096 |
| 67 | global pk_ir_in_len ulong 1 |
| 68 | global pk_fid_in uchar 32 |
| 69 | global pk_req_in ulong 6 |
| 70 | global pk_imp_in uchar 512 |
| 71 | global pk_imp_cnt ulong 1 |
| 72 | note OMPK v2 contract staging: 0=active, 1=purity, 2=precision, 3=hard_dl, 4=err_bound, 5=deadline_us, 6=power_mw |
| 73 | global pk_v2_in ulong 8 |
| 74 | |
| 75 | note Digest query and last-packed / last-stored metadata |
| 76 | global pk_query_id uchar 32 |
| 77 | global pk_last_sem uchar 32 |
| 78 | global pk_last_fid uchar 32 |
| 79 | global pk_last_slot ulong 1 |
| 80 | |
| 81 | note Content-addressed in-memory package store (32 slots) |
| 82 | global pk_store_data uchar 262144 |
| 83 | global pk_store_used ulong 32 |
| 84 | global pk_store_len ulong 32 |
| 85 | global pk_store_sem uchar 1024 |
| 86 | global pk_store_fid uchar 1024 |
| 87 | global pk_store_icnt ulong 32 |
| 88 | global pk_store_imps uchar 16384 |
| 89 | global pk_store_count ulong 1 |
| 90 | |
| 91 | note 3-color DFS state (0=white, 1=gray, 2=black) and topological order output |
| 92 | global pk_dfs_state ulong 32 |
| 93 | global pk_topo_order ulong 32 |
| 94 | global pk_topo_len ulong 1 |
| 95 | |
| 96 | note Compile-on-Arrive realization telemetry: |
| 97 | note [0]=status, [1]=cache_slot, [2]=cache_hit, [3]=cache_key, |
| 98 | note [4]=code_ptr, [5]=codegen_count, [6]=dedup_hits, [7]=used_sem_fallback |
| 99 | global pk_real_out ulong 8 |
| 100 | |
| 101 | text pk_tag_fn "omega.function.v1:" |
| 102 | |
| 103 | note ---- Little-endian wire encoding helpers on pk_buf ---- |
| 104 | |
| 105 | fn pk_put_u8 off ulong v ulong to void |
| 106 | put pk_buf off uchar and v 255 |
| 107 | end |
| 108 | |
| 109 | fn pk_put_u32 off ulong v ulong to void |
| 110 | pk_put_u8 off v |
| 111 | pk_put_u8 add off 1 shr v 8 |
| 112 | pk_put_u8 add off 2 shr v 16 |
| 113 | pk_put_u8 add off 3 shr v 24 |
| 114 | end |
| 115 | |
| 116 | fn pk_put_u64 off ulong v ulong to void |
| 117 | pk_put_u32 off and v 4294967295 |
| 118 | pk_put_u32 add off 4 shr v 32 |
| 119 | end |
| 120 | |
| 121 | fn pk_get_u32 off ulong to ulong |
| 122 | b0 = ulong get pk_buf off |
| 123 | b1 = shl ulong get pk_buf add off 1 8 |
| 124 | b2 = shl ulong get pk_buf add off 2 16 |
| 125 | b3 = shl ulong get pk_buf add off 3 24 |
| 126 | ret or or b0 b1 or b2 b3 |
| 127 | end |
| 128 | |
| 129 | fn pk_get_u64 off ulong to ulong |
| 130 | lo = pk_get_u32 off |
| 131 | hi = pk_get_u32 add off 4 |
| 132 | ret or lo shl hi 32 |
| 133 | end |
| 134 | |
| 135 | note ---- Reset and builder staging helpers ---- |
| 136 | |
| 137 | fn pk_clear_func_id to void |
| 138 | for i ulong 32 |
| 139 | put pk_fid_in i 0 |
| 140 | end |
| 141 | end |
| 142 | |
| 143 | fn pk_ir_reset to void |
| 144 | put pk_ir_in_len 0 0 |
| 145 | end |
| 146 | |
| 147 | fn pk_ir_push ch uchar to ulong |
| 148 | cur = get pk_ir_in_len 0 |
| 149 | if ge cur PK_MAX_IR_LEN |
| 150 | ret PK_ERR_LENGTH |
| 151 | end |
| 152 | put pk_ir_in cur ch |
| 153 | put pk_ir_in_len 0 add cur 1 |
| 154 | ret PK_OK |
| 155 | end |
| 156 | |
| 157 | fn pk_set_req reqExec ulong reqSmem ulong reqRegs ulong reqSubg ulong reqMma ulong reqFeat ulong to void |
| 158 | put pk_req_in 0 reqExec |
| 159 | put pk_req_in 1 reqSmem |
| 160 | put pk_req_in 2 reqRegs |
| 161 | put pk_req_in 3 reqSubg |
| 162 | put pk_req_in 4 reqMma |
| 163 | put pk_req_in 5 reqFeat |
| 164 | end |
| 165 | |
| 166 | fn pk_clear_contract to void |
| 167 | for i ulong 8 |
| 168 | put pk_v2_in i 0 |
| 169 | end |
| 170 | end |
| 171 | |
| 172 | fn pk_v2_contract_ok pur ulong prc ulong hd ulong rsv ulong err ulong dl ulong to bool |
| 173 | if or gt pur 1 gt prc 2 |
| 174 | ret false |
| 175 | end |
| 176 | if or gt hd 1 ne rsv 0 |
| 177 | ret false |
| 178 | end |
| 179 | if and ne prc 2 ne err 0 |
| 180 | ret false |
| 181 | end |
| 182 | if and eq hd 1 eq dl 0 |
| 183 | ret false |
| 184 | end |
| 185 | ret true |
| 186 | end |
| 187 | |
| 188 | fn pk_set_contract pur ulong prc ulong hd ulong err ulong dl ulong pwr ulong to void |
| 189 | put pk_v2_in 0 1 |
| 190 | put pk_v2_in 1 pur |
| 191 | put pk_v2_in 2 prc |
| 192 | put pk_v2_in 3 hd |
| 193 | put pk_v2_in 4 err |
| 194 | put pk_v2_in 5 dl |
| 195 | put pk_v2_in 6 pwr |
| 196 | end |
| 197 | |
| 198 | fn pk_set_func_id_from_sh to void |
| 199 | for i ulong 32 |
| 200 | put pk_fid_in i get sh_dig i |
| 201 | end |
| 202 | end |
| 203 | |
| 204 | fn pk_hdr_len_cur to ulong |
| 205 | if eq pk_get_u32 4 PK_VERSION_2 |
| 206 | ret PK_V2_HDR_LEN |
| 207 | end |
| 208 | ret PK_HDR_LEN |
| 209 | end |
| 210 | |
| 211 | fn pk_get_ver to ulong |
| 212 | ret pk_get_u32 4 |
| 213 | end |
| 214 | |
| 215 | fn pk_get_purity to ulong |
| 216 | if ne pk_get_u32 4 PK_VERSION_2 |
| 217 | ret 0 |
| 218 | end |
| 219 | ret ulong get pk_buf 128 |
| 220 | end |
| 221 | |
| 222 | fn pk_get_precision to ulong |
| 223 | if ne pk_get_u32 4 PK_VERSION_2 |
| 224 | ret 0 |
| 225 | end |
| 226 | ret ulong get pk_buf 129 |
| 227 | end |
| 228 | |
| 229 | fn pk_get_hard_dl to ulong |
| 230 | if ne pk_get_u32 4 PK_VERSION_2 |
| 231 | ret 0 |
| 232 | end |
| 233 | ret ulong get pk_buf 130 |
| 234 | end |
| 235 | |
| 236 | fn pk_get_err_bound to ulong |
| 237 | if ne pk_get_u32 4 PK_VERSION_2 |
| 238 | ret 0 |
| 239 | end |
| 240 | ret pk_get_u64 136 |
| 241 | end |
| 242 | |
| 243 | fn pk_get_deadline_us to ulong |
| 244 | if ne pk_get_u32 4 PK_VERSION_2 |
| 245 | ret 0 |
| 246 | end |
| 247 | ret pk_get_u64 144 |
| 248 | end |
| 249 | |
| 250 | fn pk_get_power_mw to ulong |
| 251 | if ne pk_get_u32 4 PK_VERSION_2 |
| 252 | ret 0 |
| 253 | end |
| 254 | ret pk_get_u64 152 |
| 255 | end |
| 256 | |
| 257 | fn pk_reset_builder to void |
| 258 | put pk_ir_in_len 0 0 |
| 259 | put pk_imp_cnt 0 0 |
| 260 | pk_clear_func_id |
| 261 | pk_clear_contract |
| 262 | for i ulong 6 |
| 263 | put pk_req_in i 0 |
| 264 | end |
| 265 | end |
| 266 | |
| 267 | fn pk_store_reset to void |
| 268 | put pk_store_count 0 0 |
| 269 | put pk_topo_len 0 0 |
| 270 | put pk_last_slot 0 PK_NONE |
| 271 | for i ulong 32 |
| 272 | put pk_store_used i 0 |
| 273 | put pk_store_len i 0 |
| 274 | put pk_store_icnt i 0 |
| 275 | put pk_dfs_state i 0 |
| 276 | put pk_topo_order i 0 |
| 277 | end |
| 278 | end |
| 279 | |
| 280 | fn pk_reset_all to void |
| 281 | pk_reset_builder |
| 282 | pk_store_reset |
| 283 | put pk_len 0 0 |
| 284 | for i ulong 8 |
| 285 | put pk_real_out i 0 |
| 286 | end |
| 287 | put pk_real_out 1 PK_NONE |
| 288 | end |
| 289 | |
| 290 | note Compute canonical polynomial FUNCTION_ID via SHA-256("omega.function.v1:" || w || c0 || c1 || c2) |
| 291 | fn pk_sh_put_u64 off ulong v ulong to void |
| 292 | for j ulong 8 |
| 293 | s = mul j 8 |
| 294 | put sh_dat add off j uchar and shr v s 255 |
| 295 | end |
| 296 | end |
| 297 | |
| 298 | fn pk_compute_poly_func_id width ulong c0 ulong c1 ulong c2 ulong to void |
| 299 | tlen = len pk_tag_fn |
| 300 | for i ulong tlen bound 64 |
| 301 | put sh_dat i get pk_tag_fn i |
| 302 | end |
| 303 | pk_sh_put_u64 tlen width |
| 304 | pk_sh_put_u64 add tlen 8 c0 |
| 305 | pk_sh_put_u64 add tlen 16 c1 |
| 306 | pk_sh_put_u64 add tlen 24 c2 |
| 307 | sh_all add tlen 32 |
| 308 | for i ulong 32 |
| 309 | put pk_fid_in i get sh_dig i |
| 310 | end |
| 311 | end |
| 312 | |
| 313 | fn pk_add_import_from_query to ulong |
| 314 | ic = get pk_imp_cnt 0 |
| 315 | if ge ic PK_MAX_IMPORTS |
| 316 | ret PK_ERR_IMPORTS |
| 317 | end |
| 318 | base = mul ic PK_DIGEST_LEN |
| 319 | for i ulong 32 |
| 320 | put pk_imp_in add base i get pk_query_id i |
| 321 | end |
| 322 | put pk_imp_cnt 0 add ic 1 |
| 323 | ret PK_OK |
| 324 | end |
| 325 | |
| 326 | fn pk_add_import_from_store slot ulong to ulong |
| 327 | if ge slot PK_STORE_CAP |
| 328 | ret PK_ERR_NOT_FOUND |
| 329 | end |
| 330 | if eq get pk_store_used slot 0 |
| 331 | ret PK_ERR_NOT_FOUND |
| 332 | end |
| 333 | ic = get pk_imp_cnt 0 |
| 334 | if ge ic PK_MAX_IMPORTS |
| 335 | ret PK_ERR_IMPORTS |
| 336 | end |
| 337 | dstBase = mul ic PK_DIGEST_LEN |
| 338 | srcBase = mul slot PK_DIGEST_LEN |
| 339 | for i ulong 32 |
| 340 | put pk_imp_in add dstBase i get pk_store_sem add srcBase i |
| 341 | end |
| 342 | put pk_imp_cnt 0 add ic 1 |
| 343 | ret PK_OK |
| 344 | end |
| 345 | |
| 346 | note ---- SHA-256 helpers reusing seed/ng/sha256.om ---- |
| 347 | |
| 348 | fn pk_sha256_ir_in nbytes ulong to void |
| 349 | for i ulong nbytes bound 4096 |
| 350 | put sh_dat i get pk_ir_in i |
| 351 | end |
| 352 | sh_all nbytes |
| 353 | end |
| 354 | |
| 355 | fn pk_sha256_buf_slice start ulong nbytes ulong to void |
| 356 | for i ulong nbytes bound 4096 |
| 357 | put sh_dat i get pk_buf add start i |
| 358 | end |
| 359 | sh_all nbytes |
| 360 | end |
| 361 | |
| 362 | fn pk_fid_in_is_zero to bool |
| 363 | acc = ulong 0 |
| 364 | for i ulong 32 |
| 365 | acc = or acc ulong get pk_fid_in i |
| 366 | end |
| 367 | ret eq acc 0 |
| 368 | end |
| 369 | |
| 370 | note ---- OMPK v1/v2 Packing (pk_pack) ---- |
| 371 | |
| 372 | fn pk_write_hdr ic ulong irl ulong to void |
| 373 | pk_put_u8 0 PK_MAGIC_0 |
| 374 | pk_put_u8 1 PK_MAGIC_1 |
| 375 | pk_put_u8 2 PK_MAGIC_2 |
| 376 | pk_put_u8 3 PK_MAGIC_3 |
| 377 | isv2 = ne get pk_v2_in 0 0 |
| 378 | if isv2 |
| 379 | pk_put_u32 4 PK_VERSION_2 |
| 380 | else |
| 381 | pk_put_u32 4 PK_VERSION |
| 382 | end |
| 383 | useSem = pk_fid_in_is_zero |
| 384 | for i ulong 32 |
| 385 | b = get sh_dig i |
| 386 | put pk_buf add 8 i b |
| 387 | put pk_last_sem i b |
| 388 | if useSem |
| 389 | put pk_buf add 40 i b |
| 390 | put pk_last_fid i b |
| 391 | else |
| 392 | fb = get pk_fid_in i |
| 393 | put pk_buf add 40 i fb |
| 394 | put pk_last_fid i fb |
| 395 | end |
| 396 | end |
| 397 | for k ulong 6 |
| 398 | pk_put_u64 add 72 mul k 8 get pk_req_in k |
| 399 | end |
| 400 | pk_put_u32 120 ic |
| 401 | pk_put_u32 124 irl |
| 402 | if isv2 |
| 403 | pk_put_u8 128 get pk_v2_in 1 |
| 404 | pk_put_u8 129 get pk_v2_in 2 |
| 405 | pk_put_u8 130 get pk_v2_in 3 |
| 406 | pk_put_u8 131 0 |
| 407 | pk_put_u32 132 PK_V2_EXT_TAG |
| 408 | pk_put_u64 136 get pk_v2_in 4 |
| 409 | pk_put_u64 144 get pk_v2_in 5 |
| 410 | pk_put_u64 152 get pk_v2_in 6 |
| 411 | end |
| 412 | end |
| 413 | |
| 414 | fn pk_write_body ic ulong irl ulong to void |
| 415 | hdrLen = PK_HDR_LEN |
| 416 | if ne get pk_v2_in 0 0 |
| 417 | hdrLen = PK_V2_HDR_LEN |
| 418 | end |
| 419 | impBytes = mul ic PK_DIGEST_LEN |
| 420 | for i ulong impBytes bound 512 |
| 421 | put pk_buf add hdrLen i get pk_imp_in i |
| 422 | end |
| 423 | irOff = add hdrLen impBytes |
| 424 | for i ulong irl bound 4096 |
| 425 | put pk_buf add irOff i get pk_ir_in i |
| 426 | end |
| 427 | put pk_len 0 add irOff irl |
| 428 | end |
| 429 | |
| 430 | fn pk_in_contract_ok to bool |
| 431 | pur = get pk_v2_in 1 |
| 432 | prc = get pk_v2_in 2 |
| 433 | hd = get pk_v2_in 3 |
| 434 | ev = get pk_v2_in 4 |
| 435 | dl = get pk_v2_in 5 |
| 436 | ret pk_v2_contract_ok pur prc hd 0 ev dl |
| 437 | end |
| 438 | |
| 439 | fn pk_buf_contract_ok to bool |
| 440 | pur = ulong get pk_buf 128 |
| 441 | prc = ulong get pk_buf 129 |
| 442 | hd = ulong get pk_buf 130 |
| 443 | rsv = ulong get pk_buf 131 |
| 444 | ev = pk_get_u64 136 |
| 445 | dl = pk_get_u64 144 |
| 446 | ret pk_v2_contract_ok pur prc hd rsv ev dl |
| 447 | end |
| 448 | |
| 449 | fn pk_pack to ulong |
| 450 | ic = get pk_imp_cnt 0 |
| 451 | irl = get pk_ir_in_len 0 |
| 452 | if gt ic PK_MAX_IMPORTS |
| 453 | ret PK_ERR_IMPORTS |
| 454 | end |
| 455 | if gt irl PK_MAX_IR_LEN |
| 456 | ret PK_ERR_LENGTH |
| 457 | end |
| 458 | if ne get pk_v2_in 0 0 |
| 459 | if not pk_in_contract_ok |
| 460 | ret PK_ERR_CONTRACT |
| 461 | end |
| 462 | end |
| 463 | pk_sha256_ir_in irl |
| 464 | pk_write_hdr ic irl |
| 465 | pk_write_body ic irl |
| 466 | ret PK_OK |
| 467 | end |
| 468 | |
| 469 | note ---- Fail-Closed Verifier (pk_verify) ---- |
| 470 | |
| 471 | fn pk_check_magic to bool |
| 472 | if ne ulong get pk_buf 0 PK_MAGIC_0 |
| 473 | ret false |
| 474 | end |
| 475 | if ne ulong get pk_buf 1 PK_MAGIC_1 |
| 476 | ret false |
| 477 | end |
| 478 | if ne ulong get pk_buf 2 PK_MAGIC_2 |
| 479 | ret false |
| 480 | end |
| 481 | if ne ulong get pk_buf 3 PK_MAGIC_3 |
| 482 | ret false |
| 483 | end |
| 484 | ret true |
| 485 | end |
| 486 | |
| 487 | fn pk_verify_digest to bool |
| 488 | diff = ulong 0 |
| 489 | for i ulong 32 |
| 490 | if ne get sh_dig i get pk_buf add 8 i |
| 491 | diff = add diff 1 |
| 492 | end |
| 493 | end |
| 494 | ret eq diff 0 |
| 495 | end |
| 496 | |
| 497 | fn pk_verify to ulong |
| 498 | blen = get pk_len 0 |
| 499 | if lt blen PK_HDR_LEN |
| 500 | ret PK_ERR_LENGTH |
| 501 | end |
| 502 | if gt blen PK_BUF_CAP |
| 503 | ret PK_ERR_LENGTH |
| 504 | end |
| 505 | if not pk_check_magic |
| 506 | ret PK_ERR_MAGIC |
| 507 | end |
| 508 | ver = pk_get_u32 4 |
| 509 | hdrLen = PK_HDR_LEN |
| 510 | if eq ver PK_VERSION_2 |
| 511 | if lt blen PK_V2_HDR_LEN |
| 512 | ret PK_ERR_VERSION |
| 513 | end |
| 514 | if ne pk_get_u32 132 PK_V2_EXT_TAG |
| 515 | ret PK_ERR_VERSION |
| 516 | end |
| 517 | if not pk_buf_contract_ok |
| 518 | ret PK_ERR_CONTRACT |
| 519 | end |
| 520 | hdrLen = PK_V2_HDR_LEN |
| 521 | else |
| 522 | if ne ver PK_VERSION |
| 523 | ret PK_ERR_VERSION |
| 524 | end |
| 525 | end |
| 526 | ic = pk_get_u32 120 |
| 527 | if gt ic PK_MAX_IMPORTS |
| 528 | ret PK_ERR_IMPORTS |
| 529 | end |
| 530 | irl = pk_get_u32 124 |
| 531 | irOff = add hdrLen mul ic PK_DIGEST_LEN |
| 532 | if ne blen add irOff irl |
| 533 | ret PK_ERR_LENGTH |
| 534 | end |
| 535 | if gt irl PK_MAX_IR_LEN |
| 536 | ret PK_ERR_LENGTH |
| 537 | end |
| 538 | pk_sha256_buf_slice irOff irl |
| 539 | if not pk_verify_digest |
| 540 | ret PK_ERR_DIGEST |
| 541 | end |
| 542 | ret PK_OK |
| 543 | end |
| 544 | |
| 545 | note ---- Content-Addressed Package Store + DFS Dependency Resolver ---- |
| 546 | |
| 547 | fn pk_copy_buf_sem_to_query to void |
| 548 | for i ulong 32 |
| 549 | put pk_query_id i get pk_buf add 8 i |
| 550 | end |
| 551 | end |
| 552 | |
| 553 | fn pk_copy_store_sem_to_query slot ulong to void |
| 554 | base = mul slot PK_DIGEST_LEN |
| 555 | for i ulong 32 |
| 556 | put pk_query_id i get pk_store_sem add base i |
| 557 | end |
| 558 | end |
| 559 | |
| 560 | fn pk_copy_store_imp_to_query slot ulong impIdx ulong to void |
| 561 | base = mul add mul slot PK_MAX_IMPORTS impIdx PK_DIGEST_LEN |
| 562 | for i ulong 32 |
| 563 | put pk_query_id i get pk_store_imps add base i |
| 564 | end |
| 565 | end |
| 566 | |
| 567 | fn pk_store_match_slot slot ulong to bool |
| 568 | if eq get pk_store_used slot 0 |
| 569 | ret false |
| 570 | end |
| 571 | base = mul slot PK_DIGEST_LEN |
| 572 | for i ulong 32 |
| 573 | if ne get pk_store_sem add base i get pk_query_id i |
| 574 | ret false |
| 575 | end |
| 576 | end |
| 577 | ret true |
| 578 | end |
| 579 | |
| 580 | fn pk_store_find_query to ulong |
| 581 | for s ulong 32 |
| 582 | if pk_store_match_slot s |
| 583 | ret s |
| 584 | end |
| 585 | end |
| 586 | ret PK_NONE |
| 587 | end |
| 588 | |
| 589 | fn pk_store_has to bool |
| 590 | ret ne pk_store_find_query PK_NONE |
| 591 | end |
| 592 | |
| 593 | fn pk_store_copy_bytes slot ulong blen ulong to void |
| 594 | dataBase = mul slot PK_STORE_STRIDE |
| 595 | for i ulong blen bound 4768 |
| 596 | put pk_store_data add dataBase i get pk_buf i |
| 597 | end |
| 598 | put pk_store_len slot blen |
| 599 | end |
| 600 | |
| 601 | fn pk_store_copy_digests slot ulong to void |
| 602 | digBase = mul slot PK_DIGEST_LEN |
| 603 | for i ulong 32 |
| 604 | put pk_store_sem add digBase i get pk_buf add 8 i |
| 605 | put pk_store_fid add digBase i get pk_buf add 40 i |
| 606 | end |
| 607 | end |
| 608 | |
| 609 | fn pk_store_copy_imps slot ulong to void |
| 610 | hdrLen = pk_hdr_len_cur |
| 611 | ic = pk_get_u32 120 |
| 612 | put pk_store_icnt slot ic |
| 613 | impBytes = mul ic PK_DIGEST_LEN |
| 614 | impBase = mul slot 512 |
| 615 | for i ulong impBytes bound 512 |
| 616 | put pk_store_imps add impBase i get pk_buf add hdrLen i |
| 617 | end |
| 618 | end |
| 619 | |
| 620 | fn pk_store_write_slot slot ulong blen ulong to void |
| 621 | pk_store_copy_bytes slot blen |
| 622 | pk_store_copy_digests slot |
| 623 | pk_store_copy_imps slot |
| 624 | put pk_store_used slot 1 |
| 625 | end |
| 626 | |
| 627 | fn pk_store_put to ulong |
| 628 | vrc = pk_verify |
| 629 | if ne vrc PK_OK |
| 630 | ret vrc |
| 631 | end |
| 632 | pk_copy_buf_sem_to_query |
| 633 | ex = pk_store_find_query |
| 634 | if ne ex PK_NONE |
| 635 | put pk_last_slot 0 ex |
| 636 | ret PK_OK |
| 637 | end |
| 638 | freeSlot = PK_NONE |
| 639 | for s ulong 32 |
| 640 | if eq get pk_store_used s 0 |
| 641 | freeSlot = s |
| 642 | break |
| 643 | end |
| 644 | end |
| 645 | if eq freeSlot PK_NONE |
| 646 | ret PK_ERR_STORE_FULL |
| 647 | end |
| 648 | blen = get pk_len 0 |
| 649 | pk_store_write_slot freeSlot blen |
| 650 | put pk_store_count 0 add get pk_store_count 0 1 |
| 651 | put pk_last_slot 0 freeSlot |
| 652 | ret PK_OK |
| 653 | end |
| 654 | |
| 655 | fn pk_store_load slot ulong to ulong |
| 656 | if ge slot PK_STORE_CAP |
| 657 | ret PK_ERR_NOT_FOUND |
| 658 | end |
| 659 | if eq get pk_store_used slot 0 |
| 660 | ret PK_ERR_NOT_FOUND |
| 661 | end |
| 662 | slen = get pk_store_len slot |
| 663 | if gt slen PK_MAX_PKG_LEN |
| 664 | ret PK_ERR_LENGTH |
| 665 | end |
| 666 | dataBase = mul slot PK_STORE_STRIDE |
| 667 | for i ulong slen bound 4768 |
| 668 | put pk_buf i get pk_store_data add dataBase i |
| 669 | end |
| 670 | put pk_len 0 slen |
| 671 | ret PK_OK |
| 672 | end |
| 673 | |
| 674 | fn pk_dfs_visit slot ulong to ulong |
| 675 | st = get pk_dfs_state slot |
| 676 | if eq st 1 |
| 677 | ret PK_ERR_CYCLE |
| 678 | end |
| 679 | if eq st 2 |
| 680 | ret PK_OK |
| 681 | end |
| 682 | put pk_dfs_state slot 1 |
| 683 | ic = get pk_store_icnt slot |
| 684 | for i ulong ic bound 16 |
| 685 | pk_copy_store_imp_to_query slot i |
| 686 | dep = pk_store_find_query |
| 687 | if eq dep PK_NONE |
| 688 | ret PK_ERR_MISSING_IMPORT |
| 689 | end |
| 690 | rc = pk_dfs_visit dep |
| 691 | if ne rc PK_OK |
| 692 | ret rc |
| 693 | end |
| 694 | end |
| 695 | put pk_dfs_state slot 2 |
| 696 | tlen = get pk_topo_len 0 |
| 697 | if lt tlen PK_STORE_CAP |
| 698 | put pk_topo_order tlen slot |
| 699 | put pk_topo_len 0 add tlen 1 |
| 700 | end |
| 701 | ret PK_OK |
| 702 | end |
| 703 | |
| 704 | fn pk_resolve_deps to ulong |
| 705 | root = pk_store_find_query |
| 706 | if eq root PK_NONE |
| 707 | ret PK_ERR_NOT_FOUND |
| 708 | end |
| 709 | for i ulong 32 |
| 710 | put pk_dfs_state i 0 |
| 711 | put pk_topo_order i 0 |
| 712 | end |
| 713 | put pk_topo_len 0 0 |
| 714 | ret pk_dfs_visit root |
| 715 | end |
| 716 | |
| 717 | note ---- Compile-on-Arrive Realization (TargetDesc Contract + Proof-Gated Cache) ---- |
| 718 | |
| 719 | fn pk_rotl64 x ulong r ulong to ulong |
| 720 | ret or shl x r shr x sub 64 r |
| 721 | end |
| 722 | |
| 723 | fn pk_digest_key64 off ulong to ulong |
| 724 | w0 = pk_get_u64 off |
| 725 | w1 = pk_rotl64 pk_get_u64 add off 8 17 |
| 726 | w2 = pk_rotl64 pk_get_u64 add off 16 31 |
| 727 | w3 = pk_rotl64 pk_get_u64 add off 24 47 |
| 728 | k = xor xor w0 w1 xor w2 w3 |
| 729 | if eq k 0 |
| 730 | k = ulong 1 |
| 731 | end |
| 732 | ret k |
| 733 | end |
| 734 | |
| 735 | fn pk_buf_is_zero32 off ulong to bool |
| 736 | acc = ulong 0 |
| 737 | for i ulong 32 |
| 738 | acc = or acc ulong get pk_buf add off i |
| 739 | end |
| 740 | ret eq acc 0 |
| 741 | end |
| 742 | |
| 743 | fn pk_select_cache_key to ulong |
| 744 | if pk_buf_is_zero32 40 |
| 745 | put pk_real_out 7 1 |
| 746 | ret pk_digest_key64 8 |
| 747 | end |
| 748 | put pk_real_out 7 0 |
| 749 | ret pk_digest_key64 40 |
| 750 | end |
| 751 | |
| 752 | fn pk_load_contract_to_td to void |
| 753 | r0 = pk_get_u64 72 |
| 754 | r1 = pk_get_u64 80 |
| 755 | r2 = pk_get_u64 88 |
| 756 | r3 = pk_get_u64 96 |
| 757 | r4 = pk_get_u64 104 |
| 758 | r5 = pk_get_u64 112 |
| 759 | tdSetReq r0 r1 r2 r3 r4 r5 |
| 760 | end |
| 761 | |
| 762 | fn pk_check_contract tid ulong feat ulong to ulong |
| 763 | pk_load_contract_to_td |
| 764 | crc = tdCheckKernelContract tid |
| 765 | if ne crc TDK_OK |
| 766 | ret crc |
| 767 | end |
| 768 | rf = pk_get_u64 112 |
| 769 | if ne and rf feat rf |
| 770 | ret TDK_ERR_FEAT |
| 771 | end |
| 772 | ret TDK_OK |
| 773 | end |
| 774 | |
| 775 | fn pk_make_code_ptr tid ulong cg ulong to ulong |
| 776 | ret add 65536 add mul tid 4096 mul cg 64 |
| 777 | end |
| 778 | |
| 779 | fn pk_install_realization key ulong tid ulong feat ulong shapeSig ulong proofMask ulong to ulong |
| 780 | cg = add get pk_real_out 5 1 |
| 781 | codePtr = pk_make_code_ptr tid cg |
| 782 | ins = tdCacheInstall key tid feat shapeSig codePtr proofMask |
| 783 | if eq ins PK_NONE |
| 784 | if ne and proofMask 7 7 |
| 785 | put pk_real_out 0 PK_ERR_PROOF |
| 786 | ret PK_ERR_PROOF |
| 787 | end |
| 788 | put pk_real_out 0 PK_ERR_CACHE_FULL |
| 789 | ret PK_ERR_CACHE_FULL |
| 790 | end |
| 791 | put pk_real_out 5 cg |
| 792 | put pk_real_out 0 PK_OK |
| 793 | put pk_real_out 1 ins |
| 794 | put pk_real_out 2 0 |
| 795 | put pk_real_out 4 codePtr |
| 796 | ret PK_OK |
| 797 | end |
| 798 | |
| 799 | fn pk_realize_on_arrive tid ulong feat ulong shapeSig ulong proofMask ulong to ulong |
| 800 | put pk_real_out 0 PK_OK |
| 801 | put pk_real_out 1 PK_NONE |
| 802 | put pk_real_out 2 0 |
| 803 | put pk_real_out 3 0 |
| 804 | put pk_real_out 4 0 |
| 805 | put pk_real_out 7 0 |
| 806 | if ge tid get tdCount 0 |
| 807 | put pk_real_out 0 PK_ERR_BAD_TARGET |
| 808 | ret PK_ERR_BAD_TARGET |
| 809 | end |
| 810 | rc = pk_verify |
| 811 | if ne rc PK_OK |
| 812 | put pk_real_out 0 rc |
| 813 | ret rc |
| 814 | end |
| 815 | rc = pk_check_contract tid feat |
| 816 | if ne rc TDK_OK |
| 817 | put pk_real_out 0 rc |
| 818 | ret rc |
| 819 | end |
| 820 | key = pk_select_cache_key |
| 821 | put pk_real_out 3 key |
| 822 | hitSlot = tdCacheLookup key tid feat shapeSig |
| 823 | if ne hitSlot PK_NONE |
| 824 | put pk_real_out 0 PK_OK |
| 825 | put pk_real_out 1 hitSlot |
| 826 | put pk_real_out 2 1 |
| 827 | put pk_real_out 4 get rcPtr hitSlot |
| 828 | put pk_real_out 6 add get pk_real_out 6 1 |
| 829 | ret PK_OK |
| 830 | end |
| 831 | ret pk_install_realization key tid feat shapeSig proofMask |
| 832 | end |