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 / tests / program / test_program_id.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 implementation of Program Identity v2 (spec/program-identity.md) |
| 3 | note covering all 5 gates: BODY_BOUND, REPRESENTATION_INVARIANT (incl. 400-chain |
| 4 | note seeded associativity), MUTATION_SEPARATION (3000-program differential + 2000x8 |
| 5 | note single mutations), LIBRARY_REGRESSION (library/DAG/corpus/discovery/refactor/ |
| 6 | note VC-bridge/triple-id/64-step), and VISOR_REGRESSION. |
| 7 | om2 |
| 8 | |
| 9 | note ---- top-level texts ---- |
| 10 | text t_gate "GATE " |
| 11 | text t_sp_pass " PASS " |
| 12 | text t_sp_fail " FAIL " |
| 13 | text t_pass "PASS " |
| 14 | text t_fail "FAIL " |
| 15 | text t_slash "/" |
| 16 | text t_nl "\n" |
| 17 | text t_g0 "OMEGA_PROGRAM_ID_BODY_BOUND_PASS" |
| 18 | text t_g1 "OMEGA_PROGRAM_ID_REPRESENTATION_INVARIANT_PASS" |
| 19 | text t_g2 "OMEGA_PROGRAM_ID_MUTATION_SEPARATION_PASS" |
| 20 | text t_g3 "OMEGA_PROGRAM_ID_LIBRARY_REGRESSION_PASS" |
| 21 | text t_g4 "OMEGA_PROGRAM_ID_VISOR_REGRESSION_PASS" |
| 22 | text t_assoc1 "associativity: " |
| 23 | text t_assoc2 " chains, " |
| 24 | text t_assoc3 " random bracketings, " |
| 25 | text t_assoc4 " a;b vs b;a pairs\n" |
| 26 | text t_diff1 "differential: " |
| 27 | text t_diff2 " programs, " |
| 28 | text t_diff3 " pairs, " |
| 29 | text t_diff4 " structurally equal\n" |
| 30 | text t_mut1 "mutations: " |
| 31 | text t_mut2 " single mutations over " |
| 32 | text t_mut3 " programs\n" |
| 33 | text t_fail_chk "FAIL check gate=" |
| 34 | text t_omega_param "omega.param" |
| 35 | text t_omega_compose "omega.compose" |
| 36 | text t_seq "seq" |
| 37 | text t_prog_dom "omega.program.v2" |
| 38 | text t_triple_dom "omega.realize.triple.v1" |
| 39 | text t_pre_xge0 "x >= 0" |
| 40 | text t_pre_true "true" |
| 41 | text t_pre_xlt10 "x < 10" |
| 42 | text t_post_true "true" |
| 43 | text t_post_rge1 "result >= 1" |
| 44 | text t_post_reqx "result == x" |
| 45 | text t_post_same "same" |
| 46 | text t_post_repl "replaced" |
| 47 | text t_post_rge0 "result >= 0" |
| 48 | text t_post_rge2 "result >= 2" |
| 49 | text t_post_rge3 "result >= 3" |
| 50 | text t_post_rge4 "result >= 4" |
| 51 | text t_post_rge5 "result >= 5" |
| 52 | text t_post_rge6 "result >= 6" |
| 53 | |
| 54 | note Visor fn strings for lowering tests |
| 55 | text s_fn_f1 "fn f(x: u64) -> u64 { x + 1 }" |
| 56 | text s_fn_f2 "fn f(x: u64) -> u64 { x + 2 }" |
| 57 | text s_fn_t21 "fn t(x: u64) -> u64 requires true ensures result >= 1 { x * 2 + 1 }" |
| 58 | text s_fn_t31 "fn t(x: u64) -> u64 requires true ensures result >= 1 { x * 3 + 1 }" |
| 59 | text s_same_0 "fn f(x: u64) -> u64 requires true ensures result >= 1 { x * 2 + 1 }" |
| 60 | text s_same_1 "fn f(x:u64)->u64 requires true ensures result>=1 {x*2+1}" |
| 61 | text s_same_2 "fn f(n: u64) -> u64 requires true ensures result >= 1 { n * 2 + 1 }" |
| 62 | text s_same_3 "fn f(x: u64) -> u64 requires true ensures result >= 1 { 1 + 2 * x }" |
| 63 | text s_same_4 "fn f(x: u64) -> u64 requires /* c */ true ensures result >= 0x1 { (x * 0b10) + 1 }" |
| 64 | text s_same_5 "fn other_name(y: u64) -> u64 requires true ensures result >= 1 { (2 * y) + (1) }" |
| 65 | text s_vis_t1 "fn t(x: u64) -> u64 { x + 1 }" |
| 66 | text s_vis_t2 "fn t(x: u64) -> u64 { x + 2 }" |
| 67 | text s_vis_t3 "fn t(x: u64) -> u64 { x + 3 }" |
| 68 | text s_vis_t3b "fn t(y: u64) -> u64 { y+3 }" |
| 69 | text s_vis_u3 "fn u(x: u64) -> u64 { x + 3 }" |
| 70 | text s_vis_te "fn t(x: u64) -> u64 ensures result >= 3 { x + 3 }" |
| 71 | text t_kw_fn "fn" |
| 72 | text t_kw_req "requires" |
| 73 | text t_kw_ens "ensures" |
| 74 | text t_kw_true "true" |
| 75 | text t_kw_false "false" |
| 76 | text t_ty_u64 "u64" |
| 77 | text t_res_result "result" |
| 78 | text t_x "x" |
| 79 | text t_t "t" |
| 80 | text t_u "u" |
| 81 | text t_tok_add "+" |
| 82 | text t_tok_sub "-" |
| 83 | text t_tok_mul "*" |
| 84 | text t_tok_amp "&" |
| 85 | text t_tok_pip "|" |
| 86 | text t_tok_eq "==" |
| 87 | text t_tok_ne "!=" |
| 88 | text t_tok_lt "<" |
| 89 | text t_tok_le "<=" |
| 90 | text t_tok_gt ">" |
| 91 | text t_tok_ge ">=" |
| 92 | |
| 93 | note ---- globals ---- |
| 94 | global g_pass ulong 5 |
| 95 | global g_total ulong 5 |
| 96 | global g_numbuf uchar 32 |
| 97 | |
| 98 | note SHA-256 buffers |
| 99 | global g_sha_in uchar 1024 |
| 100 | global g_sha_out uchar 32 |
| 101 | global g_sha_k uint 64 |
| 102 | global g_sha_s uint 8 |
| 103 | global g_sha_w uint 64 |
| 104 | global g_sha_pad uchar 128 |
| 105 | |
| 106 | note Canonical encoding scratch |
| 107 | global g_enc uchar 512 |
| 108 | |
| 109 | note Scratch IDs |
| 110 | global g_tid_in uchar 32 |
| 111 | global g_tid_out uchar 32 |
| 112 | global g_cid uchar 32 |
| 113 | global g_oid uchar 32 |
| 114 | global g_cur uchar 32 |
| 115 | global g_tmp_id uchar 32 |
| 116 | global g_tmp_id2 uchar 32 |
| 117 | global g_pre_cache uchar 96 |
| 118 | global g_post_cache uchar 96 |
| 119 | global g_post7_cache uchar 224 |
| 120 | global g_tid32 uchar 32 |
| 121 | global g_tid64 uchar 32 |
| 122 | global g_param32 uchar 32 |
| 123 | global g_param64 uchar 32 |
| 124 | |
| 125 | note Program pool: 32 slots (0..31) |
| 126 | note Each slot p has: has_body, scnt, sop[64], simm[64], in_w, out_w, |
| 127 | note pre_id[32], post_id[32], leaf_cnt, leaves[64*32], prog_id[32], |
| 128 | note clen, code[512], insn_cnt, latency, verified |
| 129 | global g_p_has ulong 32 |
| 130 | global g_p_scnt ulong 32 |
| 131 | global g_p_sop ulong 2048 |
| 132 | global g_p_simm ulong 2048 |
| 133 | global g_p_inw ulong 32 |
| 134 | global g_p_outw ulong 32 |
| 135 | global g_p_preid uchar 1024 |
| 136 | global g_p_postid uchar 1024 |
| 137 | global g_p_lcnt ulong 32 |
| 138 | global g_p_leaves uchar 65536 |
| 139 | global g_p_id uchar 1024 |
| 140 | global g_p_clen ulong 32 |
| 141 | global g_p_code uchar 32768 |
| 142 | global g_p_insn ulong 32 |
| 143 | global g_p_lat ulong 32 |
| 144 | global g_p_ver ulong 32 |
| 145 | |
| 146 | note Scratch for compose post_leaves |
| 147 | global g_la_id uchar 2048 |
| 148 | global g_lb_id uchar 2048 |
| 149 | global g_code_tmp uchar 1024 |
| 150 | global g_code_tlen ulong 1 |
| 151 | global g_lift_op ulong 64 |
| 152 | global g_lift_imm ulong 64 |
| 153 | global g_lift_cnt ulong 1 |
| 154 | |
| 155 | note RNG states |
| 156 | global g_brng ulong 1 |
| 157 | global g_rng ulong 1 |
| 158 | |
| 159 | note Differential test storage (DIFF_N = 3000) |
| 160 | global g_d_scnt ulong 3000 |
| 161 | global g_d_op uchar 24000 |
| 162 | global g_d_imm ulong 24000 |
| 163 | global g_d_w ulong 3000 |
| 164 | global g_d_pre uchar 3000 |
| 165 | global g_d_post uchar 3000 |
| 166 | global g_d_id uchar 96000 |
| 167 | |
| 168 | note Library storage (16 entries + deps adjacency) |
| 169 | global g_lib_cnt ulong 1 |
| 170 | global g_lib_id uchar 512 |
| 171 | global g_lib_dep_cnt ulong 16 |
| 172 | global g_lib_deps uchar 2048 |
| 173 | |
| 174 | note Visor mini-session (16 programs, 16 bindings) |
| 175 | global g_vs_pcnt ulong 1 |
| 176 | global g_vs_pslot ulong 16 |
| 177 | global g_vs_bcnt ulong 1 |
| 178 | global g_vs_bchar ulong 16 |
| 179 | global g_vs_bpidx ulong 16 |
| 180 | global g_vs_bid uchar 512 |
| 181 | |
| 182 | note Mini lexer/parser for Visor fn lines |
| 183 | global g_line uchar 256 |
| 184 | global g_llen ulong 1 |
| 185 | global g_tcnt ulong 1 |
| 186 | global g_tkind ulong 64 |
| 187 | global g_tval ulong 64 |
| 188 | global g_tstr uchar 4096 |
| 189 | global g_tslen ulong 64 |
| 190 | global g_ppos ulong 1 |
| 191 | global g_ancnt ulong 1 |
| 192 | global g_akind ulong 64 |
| 193 | global g_aop ulong 64 |
| 194 | global g_alhs long 64 |
| 195 | global g_arhs long 64 |
| 196 | global g_aval ulong 64 |
| 197 | global g_astr uchar 4096 |
| 198 | global g_aslen ulong 64 |
| 199 | global g_aparam uchar 64 |
| 200 | global g_aplen ulong 1 |
| 201 | global g_aname0 ulong 1 |
| 202 | global g_req_buf uchar 64 |
| 203 | global g_req_len ulong 1 |
| 204 | global g_ens_buf uchar 64 |
| 205 | global g_ens_len ulong 1 |
| 206 | global g_tmp_post uchar 64 |
| 207 | global g_tmp_plen ulong 1 |
| 208 | |
| 209 | note ---- I/O helpers ---- |
| 210 | fn write_all s view uchar * n ulong to void |
| 211 | if eq n 0 |
| 212 | ret |
| 213 | end |
| 214 | k = write 1 s 0 n |
| 215 | if lt k 0 |
| 216 | ret |
| 217 | end |
| 218 | end |
| 219 | |
| 220 | fn print_u64 v ulong to void |
| 221 | if eq v 0 |
| 222 | put g_numbuf 0 uchar 48 |
| 223 | k = write 1 g_numbuf 0 1 |
| 224 | ret |
| 225 | end |
| 226 | x = v |
| 227 | cnt = ulong 0 |
| 228 | while gt x 0 bound 24 |
| 229 | put g_numbuf cnt uchar add rem x 10 48 |
| 230 | cnt = add cnt 1 |
| 231 | x = div x 10 |
| 232 | end |
| 233 | i = ulong 0 |
| 234 | j = sub cnt 1 |
| 235 | while lt i j bound 24 |
| 236 | a = get g_numbuf i |
| 237 | b = get g_numbuf j |
| 238 | put g_numbuf i b |
| 239 | put g_numbuf j a |
| 240 | i = add i 1 |
| 241 | j = sub j 1 |
| 242 | end |
| 243 | k = write 1 g_numbuf 0 cnt |
| 244 | end |
| 245 | |
| 246 | fn chk gate ulong ok bool to void |
| 247 | put g_total gate add get g_total gate 1 |
| 248 | if ok |
| 249 | put g_pass gate add get g_pass gate 1 |
| 250 | else |
| 251 | write_all t_fail_chk len t_fail_chk |
| 252 | print_u64 gate |
| 253 | write_all t_nl len t_nl |
| 254 | end |
| 255 | end |
| 256 | |
| 257 | note ---- SHA-256 ---- |
| 258 | fn sha_kinit to void |
| 259 | put g_sha_k 0 0x428a2f98 |
| 260 | put g_sha_k 1 0x71374491 |
| 261 | put g_sha_k 2 0xb5c0fbcf |
| 262 | put g_sha_k 3 0xe9b5dba5 |
| 263 | put g_sha_k 4 0x3956c25b |
| 264 | put g_sha_k 5 0x59f111f1 |
| 265 | put g_sha_k 6 0x923f82a4 |
| 266 | put g_sha_k 7 0xab1c5ed5 |
| 267 | put g_sha_k 8 0xd807aa98 |
| 268 | put g_sha_k 9 0x12835b01 |
| 269 | put g_sha_k 10 0x243185be |
| 270 | put g_sha_k 11 0x550c7dc3 |
| 271 | put g_sha_k 12 0x72be5d74 |
| 272 | put g_sha_k 13 0x80deb1fe |
| 273 | put g_sha_k 14 0x9bdc06a7 |
| 274 | put g_sha_k 15 0xc19bf174 |
| 275 | put g_sha_k 16 0xe49b69c1 |
| 276 | put g_sha_k 17 0xefbe4786 |
| 277 | put g_sha_k 18 0x0fc19dc6 |
| 278 | put g_sha_k 19 0x240ca1cc |
| 279 | put g_sha_k 20 0x2de92c6f |
| 280 | put g_sha_k 21 0x4a7484aa |
| 281 | put g_sha_k 22 0x5cb0a9dc |
| 282 | put g_sha_k 23 0x76f988da |
| 283 | put g_sha_k 24 0x983e5152 |
| 284 | put g_sha_k 25 0xa831c66d |
| 285 | put g_sha_k 26 0xb00327c8 |
| 286 | put g_sha_k 27 0xbf597fc7 |
| 287 | put g_sha_k 28 0xc6e00bf3 |
| 288 | put g_sha_k 29 0xd5a79147 |
| 289 | put g_sha_k 30 0x06ca6351 |
| 290 | put g_sha_k 31 0x14292967 |
| 291 | put g_sha_k 32 0x27b70a85 |
| 292 | put g_sha_k 33 0x2e1b2138 |
| 293 | put g_sha_k 34 0x4d2c6dfc |
| 294 | put g_sha_k 35 0x53380d13 |
| 295 | put g_sha_k 36 0x650a7354 |
| 296 | put g_sha_k 37 0x766a0abb |
| 297 | put g_sha_k 38 0x81c2c92e |
| 298 | put g_sha_k 39 0x92722c85 |
| 299 | put g_sha_k 40 0xa2bfe8a1 |
| 300 | put g_sha_k 41 0xa81a664b |
| 301 | put g_sha_k 42 0xc24b8b70 |
| 302 | put g_sha_k 43 0xc76c51a3 |
| 303 | put g_sha_k 44 0xd192e819 |
| 304 | put g_sha_k 45 0xd6990624 |
| 305 | put g_sha_k 46 0xf40e3585 |
| 306 | put g_sha_k 47 0x106aa070 |
| 307 | put g_sha_k 48 0x19a4c116 |
| 308 | put g_sha_k 49 0x1e376c08 |
| 309 | put g_sha_k 50 0x2748774c |
| 310 | put g_sha_k 51 0x34b0bcb5 |
| 311 | put g_sha_k 52 0x391c0cb3 |
| 312 | put g_sha_k 53 0x4ed8aa4a |
| 313 | put g_sha_k 54 0x5b9cca4f |
| 314 | put g_sha_k 55 0x682e6ff3 |
| 315 | put g_sha_k 56 0x748f82ee |
| 316 | put g_sha_k 57 0x78a5636f |
| 317 | put g_sha_k 58 0x84c87814 |
| 318 | put g_sha_k 59 0x8cc70208 |
| 319 | put g_sha_k 60 0x90befffa |
| 320 | put g_sha_k 61 0xa4506ceb |
| 321 | put g_sha_k 62 0xbef9a3f7 |
| 322 | put g_sha_k 63 0xc67178f2 |
| 323 | end |
| 324 | |
| 325 | fn sha_sched_in off ulong to void |
| 326 | for i ulong 16 |
| 327 | q = add off mul i 4 |
| 328 | x = uint get g_sha_in q |
| 329 | x = or shl x 8 uint get g_sha_in add q 1 |
| 330 | x = or shl x 8 uint get g_sha_in add q 2 |
| 331 | put g_sha_w i or shl x 8 uint get g_sha_in add q 3 |
| 332 | end |
| 333 | for i ulong 48 |
| 334 | x = get g_sha_w add i 1 |
| 335 | y = get g_sha_w add i 14 |
| 336 | x = add add get g_sha_w i get g_sha_w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 337 | put g_sha_w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 338 | end |
| 339 | end |
| 340 | |
| 341 | fn sha_sched_pad off ulong to void |
| 342 | for i ulong 16 |
| 343 | q = add off mul i 4 |
| 344 | x = uint get g_sha_pad q |
| 345 | x = or shl x 8 uint get g_sha_pad add q 1 |
| 346 | x = or shl x 8 uint get g_sha_pad add q 2 |
| 347 | put g_sha_w i or shl x 8 uint get g_sha_pad add q 3 |
| 348 | end |
| 349 | for i ulong 48 |
| 350 | x = get g_sha_w add i 1 |
| 351 | y = get g_sha_w add i 14 |
| 352 | x = add add get g_sha_w i get g_sha_w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 353 | put g_sha_w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 354 | end |
| 355 | end |
| 356 | |
| 357 | fn sha_rounds to void |
| 358 | a = get g_sha_s 0 |
| 359 | b = get g_sha_s 1 |
| 360 | c = get g_sha_s 2 |
| 361 | d = get g_sha_s 3 |
| 362 | e = get g_sha_s 4 |
| 363 | f = get g_sha_s 5 |
| 364 | g = get g_sha_s 6 |
| 365 | h = get g_sha_s 7 |
| 366 | t = uint 0 |
| 367 | for i ulong 64 |
| 368 | h = add h get g_sha_k i |
| 369 | h = add h get g_sha_w i |
| 370 | t = xor rotr e 6 rotr e 11 |
| 371 | t = xor t rotr e 25 |
| 372 | h = add h t |
| 373 | h = add h xor g and e xor f g |
| 374 | d = add d h |
| 375 | t = xor rotr a 2 rotr a 13 |
| 376 | t = xor t rotr a 22 |
| 377 | h = add h t |
| 378 | t = and c or a b |
| 379 | h = add h or and a b t |
| 380 | t = h |
| 381 | h = g |
| 382 | g = f |
| 383 | f = e |
| 384 | e = d |
| 385 | d = c |
| 386 | c = b |
| 387 | b = a |
| 388 | a = t |
| 389 | end |
| 390 | put g_sha_s 0 add get g_sha_s 0 a |
| 391 | put g_sha_s 1 add get g_sha_s 1 b |
| 392 | put g_sha_s 2 add get g_sha_s 2 c |
| 393 | put g_sha_s 3 add get g_sha_s 3 d |
| 394 | put g_sha_s 4 add get g_sha_s 4 e |
| 395 | put g_sha_s 5 add get g_sha_s 5 f |
| 396 | put g_sha_s 6 add get g_sha_s 6 g |
| 397 | put g_sha_s 7 add get g_sha_s 7 h |
| 398 | end |
| 399 | |
| 400 | fn sha256_hash n ulong to void |
| 401 | put g_sha_s 0 0x6a09e667 |
| 402 | put g_sha_s 1 0xbb67ae85 |
| 403 | put g_sha_s 2 0x3c6ef372 |
| 404 | put g_sha_s 3 0xa54ff53a |
| 405 | put g_sha_s 4 0x510e527f |
| 406 | put g_sha_s 5 0x9b05688c |
| 407 | put g_sha_s 6 0x1f83d9ab |
| 408 | put g_sha_s 7 0x5be0cd19 |
| 409 | nb = shr n 6 |
| 410 | for b ulong nb bound 32 |
| 411 | sha_sched_in shl b 6 |
| 412 | sha_rounds |
| 413 | end |
| 414 | remn = and n 63 |
| 415 | base = shl nb 6 |
| 416 | for i ulong 128 |
| 417 | put g_sha_pad i uchar 0 |
| 418 | end |
| 419 | for i ulong remn bound 64 |
| 420 | put g_sha_pad i get g_sha_in add base i |
| 421 | end |
| 422 | put g_sha_pad remn uchar 0x80 |
| 423 | tot = ulong 64 |
| 424 | if ge remn 56 |
| 425 | tot = 128 |
| 426 | end |
| 427 | bits = shl n 3 |
| 428 | for i ulong 8 |
| 429 | put g_sha_pad sub tot add i 1 uchar and shr bits mul i 8 0xff |
| 430 | end |
| 431 | sha_sched_pad 0 |
| 432 | sha_rounds |
| 433 | if eq tot 128 |
| 434 | sha_sched_pad 64 |
| 435 | sha_rounds |
| 436 | end |
| 437 | for i ulong 8 |
| 438 | w = get g_sha_s i |
| 439 | put g_sha_out mul i 4 uchar and shr w 24 0xff |
| 440 | put g_sha_out add mul i 4 1 uchar and shr w 16 0xff |
| 441 | put g_sha_out add mul i 4 2 uchar and shr w 8 0xff |
| 442 | put g_sha_out add mul i 4 3 uchar and w 0xff |
| 443 | end |
| 444 | end |
| 445 | |
| 446 | note ---- Canonical OMG0 encoders ---- |
| 447 | fn enc_u16be pos ulong v ulong to ulong |
| 448 | put g_enc pos uchar and shr v 8 0xff |
| 449 | put g_enc add pos 1 uchar and v 0xff |
| 450 | ret add pos 2 |
| 451 | end |
| 452 | |
| 453 | fn enc_u32be pos ulong v ulong to ulong |
| 454 | put g_enc pos uchar and shr v 24 0xff |
| 455 | put g_enc add pos 1 uchar and shr v 16 0xff |
| 456 | put g_enc add pos 2 uchar and shr v 8 0xff |
| 457 | put g_enc add pos 3 uchar and v 0xff |
| 458 | ret add pos 4 |
| 459 | end |
| 460 | |
| 461 | fn enc_u16le pos ulong v ulong to ulong |
| 462 | put g_enc pos uchar and v 0xff |
| 463 | put g_enc add pos 1 uchar and shr v 8 0xff |
| 464 | ret add pos 2 |
| 465 | end |
| 466 | |
| 467 | fn enc_u32le pos ulong v ulong to ulong |
| 468 | put g_enc pos uchar and v 0xff |
| 469 | put g_enc add pos 1 uchar and shr v 8 0xff |
| 470 | put g_enc add pos 2 uchar and shr v 16 0xff |
| 471 | put g_enc add pos 3 uchar and shr v 24 0xff |
| 472 | ret add pos 4 |
| 473 | end |
| 474 | |
| 475 | fn enc_hdr kind ulong to ulong |
| 476 | put g_enc 0 uchar 0x4f |
| 477 | put g_enc 1 uchar 0x4d |
| 478 | put g_enc 2 uchar 0x47 |
| 479 | put g_enc 3 uchar 0x30 |
| 480 | put g_enc 4 uchar 1 |
| 481 | put g_enc 5 uchar and kind 0xff |
| 482 | ret 6 |
| 483 | end |
| 484 | |
| 485 | fn hash_enc n ulong to void |
| 486 | for i ulong n bound 512 |
| 487 | put g_sha_in i get g_enc i |
| 488 | end |
| 489 | sha256_hash n |
| 490 | end |
| 491 | |
| 492 | fn hash_type_uint w ulong to void |
| 493 | p = enc_hdr 2 |
| 494 | p = enc_u16be p 0 |
| 495 | p = enc_u16be p 0 |
| 496 | p = enc_u16be p 0 |
| 497 | p = enc_u32be p 44 |
| 498 | for i ulong 44 |
| 499 | put g_enc add p i uchar 0 |
| 500 | end |
| 501 | u = enc_u32le p 3 |
| 502 | u = enc_u16le add p 4 w |
| 503 | hash_enc 60 |
| 504 | end |
| 505 | |
| 506 | fn hash_param0_for_tid to void |
| 507 | p = enc_hdr 1 |
| 508 | p = enc_u16be p 1 |
| 509 | put g_enc p uchar 11 |
| 510 | p = add p 1 |
| 511 | for i ulong 11 |
| 512 | put g_enc add p i get t_omega_param i |
| 513 | end |
| 514 | p = add p 11 |
| 515 | p = enc_u16be p 2 |
| 516 | p = enc_u16be p 0 |
| 517 | p = enc_u16be p 0 |
| 518 | p = enc_u16be p 0 |
| 519 | p = enc_u32be p 98 |
| 520 | for i ulong 98 |
| 521 | put g_enc add p i uchar 0 |
| 522 | end |
| 523 | for i ulong 32 |
| 524 | put g_enc add p i get g_tid_in i |
| 525 | end |
| 526 | hash_enc add p 98 |
| 527 | end |
| 528 | |
| 529 | fn hash_val_uint_cur w ulong val ulong to void |
| 530 | p = enc_hdr 1 |
| 531 | p = enc_u16be p 0 |
| 532 | p = enc_u16be p 0 |
| 533 | p = enc_u16be p 0 |
| 534 | p = enc_u32be p 98 |
| 535 | for i ulong 98 |
| 536 | put g_enc add p i uchar 0 |
| 537 | end |
| 538 | for i ulong 32 |
| 539 | put g_enc add p i get g_tid_in i |
| 540 | end |
| 541 | nb = div add w 7 8 |
| 542 | p = enc_u16le add p 32 nb |
| 543 | for i ulong nb bound 8 |
| 544 | sh = mul sub sub nb 1 i 8 |
| 545 | b = uchar and shr val sh 0xff |
| 546 | put g_enc add p i b |
| 547 | end |
| 548 | hash_enc 114 |
| 549 | for i ulong 32 |
| 550 | put g_cid i get g_sha_out i |
| 551 | end |
| 552 | end |
| 553 | |
| 554 | fn hash_op_cur opc ulong to void |
| 555 | p = enc_hdr 3 |
| 556 | p = enc_u16be p 0 |
| 557 | p = enc_u16be p 0 |
| 558 | p = enc_u16be p 0 |
| 559 | p = enc_u32be p 204 |
| 560 | for i ulong 204 |
| 561 | put g_enc add p i uchar 0 |
| 562 | end |
| 563 | u = enc_u32le p opc |
| 564 | u = enc_u32le add p 4 1 |
| 565 | for i ulong 32 |
| 566 | b = get g_tid_in i |
| 567 | put g_enc add add p 8 i b |
| 568 | put g_enc add add p 41 i b |
| 569 | put g_enc add add p 73 i b |
| 570 | put g_enc add add p 169 i b |
| 571 | end |
| 572 | put g_enc add p 40 uchar 2 |
| 573 | hash_enc 220 |
| 574 | for i ulong 32 |
| 575 | put g_oid i get g_sha_out i |
| 576 | end |
| 577 | end |
| 578 | |
| 579 | fn hash_apply_cur to void |
| 580 | p = enc_hdr 3 |
| 581 | p = enc_u16be p 0 |
| 582 | p = enc_u16be p 0 |
| 583 | p = enc_u16be p 0 |
| 584 | p = enc_u32be p 161 |
| 585 | for i ulong 161 |
| 586 | put g_enc add p i uchar 0 |
| 587 | end |
| 588 | for i ulong 32 |
| 589 | put g_enc add p i get g_oid i |
| 590 | put g_enc add add p 33 i get g_cur i |
| 591 | put g_enc add add p 65 i get g_cid i |
| 592 | end |
| 593 | put g_enc add p 32 uchar 2 |
| 594 | hash_enc 177 |
| 595 | for i ulong 32 |
| 596 | put g_cur i get g_sha_out i |
| 597 | end |
| 598 | end |
| 599 | |
| 600 | fn hash_constraint_txt kind ulong s view uchar * to void |
| 601 | n = len s |
| 602 | p = enc_hdr 5 |
| 603 | p = enc_u16be p 0 |
| 604 | p = enc_u16be p 0 |
| 605 | p = enc_u16be p 1 |
| 606 | p = enc_u16be p kind |
| 607 | p = enc_u16be p n |
| 608 | for i ulong n bound 64 |
| 609 | put g_enc add p i get s i |
| 610 | end |
| 611 | p = add p n |
| 612 | p = enc_u32be p 0 |
| 613 | hash_enc p |
| 614 | end |
| 615 | |
| 616 | fn hash_constraint_buf kind ulong is_ens bool to void |
| 617 | n = get g_req_len 0 |
| 618 | if is_ens |
| 619 | n = get g_ens_len 0 |
| 620 | end |
| 621 | p = enc_hdr 5 |
| 622 | p = enc_u16be p 0 |
| 623 | p = enc_u16be p 0 |
| 624 | p = enc_u16be p 1 |
| 625 | p = enc_u16be p kind |
| 626 | p = enc_u16be p n |
| 627 | for i ulong n bound 64 |
| 628 | if is_ens |
| 629 | put g_enc add p i get g_ens_buf i |
| 630 | else |
| 631 | put g_enc add p i get g_req_buf i |
| 632 | end |
| 633 | end |
| 634 | p = add p n |
| 635 | p = enc_u32be p 0 |
| 636 | hash_enc p |
| 637 | end |
| 638 | |
| 639 | fn hash_constraint_tmp_post to void |
| 640 | n = get g_tmp_plen 0 |
| 641 | p = enc_hdr 5 |
| 642 | p = enc_u16be p 0 |
| 643 | p = enc_u16be p 0 |
| 644 | p = enc_u16be p 1 |
| 645 | p = enc_u16be p 8 |
| 646 | p = enc_u16be p n |
| 647 | for i ulong n bound 64 |
| 648 | put g_enc add p i get g_tmp_post i |
| 649 | end |
| 650 | p = add p n |
| 651 | p = enc_u32be p 0 |
| 652 | hash_enc p |
| 653 | end |
| 654 | |
| 655 | fn init_caches to void |
| 656 | sha_kinit |
| 657 | hash_type_uint 32 |
| 658 | for i ulong 32 |
| 659 | put g_tid32 i get g_sha_out i |
| 660 | put g_tid_in i get g_sha_out i |
| 661 | end |
| 662 | hash_param0_for_tid |
| 663 | for i ulong 32 |
| 664 | put g_param32 i get g_sha_out i |
| 665 | end |
| 666 | hash_type_uint 64 |
| 667 | for i ulong 32 |
| 668 | put g_tid64 i get g_sha_out i |
| 669 | put g_tid_in i get g_sha_out i |
| 670 | end |
| 671 | hash_param0_for_tid |
| 672 | for i ulong 32 |
| 673 | put g_param64 i get g_sha_out i |
| 674 | end |
| 675 | hash_constraint_txt 7 t_pre_xge0 |
| 676 | for i ulong 32 |
| 677 | put g_pre_cache i get g_sha_out i |
| 678 | end |
| 679 | hash_constraint_txt 7 t_pre_true |
| 680 | for i ulong 32 |
| 681 | put g_pre_cache add 32 i get g_sha_out i |
| 682 | end |
| 683 | hash_constraint_txt 7 t_pre_xlt10 |
| 684 | for i ulong 32 |
| 685 | put g_pre_cache add 64 i get g_sha_out i |
| 686 | end |
| 687 | hash_constraint_txt 8 t_post_true |
| 688 | for i ulong 32 |
| 689 | put g_post_cache i get g_sha_out i |
| 690 | end |
| 691 | hash_constraint_txt 8 t_post_rge1 |
| 692 | for i ulong 32 |
| 693 | put g_post_cache add 32 i get g_sha_out i |
| 694 | end |
| 695 | hash_constraint_txt 8 t_post_reqx |
| 696 | for i ulong 32 |
| 697 | put g_post_cache add 64 i get g_sha_out i |
| 698 | end |
| 699 | hash_constraint_txt 8 t_post_rge0 |
| 700 | for i ulong 32 |
| 701 | put g_post7_cache i get g_sha_out i |
| 702 | end |
| 703 | hash_constraint_txt 8 t_post_rge1 |
| 704 | for i ulong 32 |
| 705 | put g_post7_cache add 32 i get g_sha_out i |
| 706 | end |
| 707 | hash_constraint_txt 8 t_post_rge2 |
| 708 | for i ulong 32 |
| 709 | put g_post7_cache add 64 i get g_sha_out i |
| 710 | end |
| 711 | hash_constraint_txt 8 t_post_rge3 |
| 712 | for i ulong 32 |
| 713 | put g_post7_cache add 96 i get g_sha_out i |
| 714 | end |
| 715 | hash_constraint_txt 8 t_post_rge4 |
| 716 | for i ulong 32 |
| 717 | put g_post7_cache add 128 i get g_sha_out i |
| 718 | end |
| 719 | hash_constraint_txt 8 t_post_rge5 |
| 720 | for i ulong 32 |
| 721 | put g_post7_cache add 160 i get g_sha_out i |
| 722 | end |
| 723 | hash_constraint_txt 8 t_post_rge6 |
| 724 | for i ulong 32 |
| 725 | put g_post7_cache add 192 i get g_sha_out i |
| 726 | end |
| 727 | end |
| 728 | |
| 729 | note ---- Program Pool Operations ---- |
| 730 | fn load_tid_in w ulong to void |
| 731 | if eq w 32 |
| 732 | for i ulong 32 |
| 733 | put g_tid_in i get g_tid32 i |
| 734 | put g_cur i get g_param32 i |
| 735 | end |
| 736 | else |
| 737 | for i ulong 32 |
| 738 | put g_tid_in i get g_tid64 i |
| 739 | put g_cur i get g_param64 i |
| 740 | end |
| 741 | end |
| 742 | end |
| 743 | |
| 744 | fn load_tid_out w ulong to void |
| 745 | if eq w 32 |
| 746 | for i ulong 32 |
| 747 | put g_tid_out i get g_tid32 i |
| 748 | end |
| 749 | else |
| 750 | for i ulong 32 |
| 751 | put g_tid_out i get g_tid64 i |
| 752 | end |
| 753 | end |
| 754 | end |
| 755 | |
| 756 | fn prog_init s ulong to void |
| 757 | put g_p_has s 0 |
| 758 | put g_p_scnt s 0 |
| 759 | put g_p_inw s 64 |
| 760 | put g_p_outw s 64 |
| 761 | put g_p_lcnt s 0 |
| 762 | put g_p_clen s 0 |
| 763 | put g_p_insn s 0 |
| 764 | put g_p_lat s 0 |
| 765 | put g_p_ver s 0 |
| 766 | ib = mul s 32 |
| 767 | for i ulong 32 |
| 768 | put g_p_id add ib i uchar 0 |
| 769 | put g_p_preid add ib i uchar 0 |
| 770 | put g_p_postid add ib i uchar 0 |
| 771 | end |
| 772 | end |
| 773 | |
| 774 | fn prog_copy_ids dst ulong src ulong to void |
| 775 | ib = mul src 32 |
| 776 | idb = mul dst 32 |
| 777 | for i ulong 32 |
| 778 | put g_p_id add idb i get g_p_id add ib i |
| 779 | put g_p_preid add idb i get g_p_preid add ib i |
| 780 | put g_p_postid add idb i get g_p_postid add ib i |
| 781 | end |
| 782 | end |
| 783 | |
| 784 | fn prog_copy_leaves dst ulong src ulong to void |
| 785 | n = mul get g_p_lcnt src 32 |
| 786 | lb = mul src 2048 |
| 787 | ldb = mul dst 2048 |
| 788 | for i ulong n bound 2048 |
| 789 | put g_p_leaves add ldb i get g_p_leaves add lb i |
| 790 | end |
| 791 | end |
| 792 | |
| 793 | fn prog_copy_code dst ulong src ulong to void |
| 794 | cl = get g_p_clen src |
| 795 | cb = mul src 1024 |
| 796 | cdb = mul dst 1024 |
| 797 | for i ulong cl bound 1024 |
| 798 | put g_p_code add cdb i get g_p_code add cb i |
| 799 | end |
| 800 | end |
| 801 | |
| 802 | fn prog_copy dst ulong src ulong to void |
| 803 | put g_p_has dst get g_p_has src |
| 804 | sc = get g_p_scnt src |
| 805 | put g_p_scnt dst sc |
| 806 | put g_p_inw dst get g_p_inw src |
| 807 | put g_p_outw dst get g_p_outw src |
| 808 | put g_p_lcnt dst get g_p_lcnt src |
| 809 | put g_p_clen dst get g_p_clen src |
| 810 | put g_p_insn dst get g_p_insn src |
| 811 | put g_p_lat dst get g_p_lat src |
| 812 | put g_p_ver dst get g_p_ver src |
| 813 | sb = mul src 64 |
| 814 | db = mul dst 64 |
| 815 | for i ulong sc bound 64 |
| 816 | put g_p_sop add db i get g_p_sop add sb i |
| 817 | put g_p_simm add db i get g_p_simm add sb i |
| 818 | end |
| 819 | prog_copy_ids dst src |
| 820 | prog_copy_leaves dst src |
| 821 | prog_copy_code dst src |
| 822 | end |
| 823 | |
| 824 | fn prog_id_eq a ulong b ulong to bool |
| 825 | ab = mul a 32 |
| 826 | bb = mul b 32 |
| 827 | for i ulong 32 |
| 828 | if ne get g_p_id add ab i get g_p_id add bb i |
| 829 | ret false |
| 830 | end |
| 831 | end |
| 832 | ret true |
| 833 | end |
| 834 | |
| 835 | fn prog_id_zero s ulong to bool |
| 836 | sb = mul s 32 |
| 837 | for i ulong 32 |
| 838 | if ne ulong get g_p_id add sb i 0 |
| 839 | ret false |
| 840 | end |
| 841 | end |
| 842 | ret true |
| 843 | end |
| 844 | |
| 845 | fn prog_body_root s ulong to long |
| 846 | if eq get g_p_has s 0 |
| 847 | ret -1 |
| 848 | end |
| 849 | sc = get g_p_scnt s |
| 850 | if gt sc 64 |
| 851 | ret -1 |
| 852 | end |
| 853 | w = get g_p_inw s |
| 854 | load_tid_in w |
| 855 | sb = mul s 64 |
| 856 | for i ulong sc bound 64 |
| 857 | hash_val_uint_cur w get g_p_simm add sb i |
| 858 | hash_op_cur get g_p_sop add sb i |
| 859 | hash_apply_cur |
| 860 | end |
| 861 | ret 0 |
| 862 | end |
| 863 | |
| 864 | fn prog_compute_id s ulong to long |
| 865 | ib = mul s 32 |
| 866 | for i ulong 32 |
| 867 | put g_p_id add ib i uchar 0 |
| 868 | end |
| 869 | if lt prog_body_root s 0 |
| 870 | ret -1 |
| 871 | end |
| 872 | load_tid_out get g_p_outw s |
| 873 | for i ulong 16 |
| 874 | put g_sha_in i get t_prog_dom i |
| 875 | end |
| 876 | put g_sha_in 16 uchar 0 |
| 877 | for i ulong 32 |
| 878 | put g_sha_in add 17 i get g_cur i |
| 879 | put g_sha_in add 49 i get g_tid_in i |
| 880 | put g_sha_in add 81 i get g_tid_out i |
| 881 | put g_sha_in add 113 i get g_p_preid add ib i |
| 882 | put g_sha_in add 145 i get g_p_postid add ib i |
| 883 | end |
| 884 | sha256_hash 177 |
| 885 | for i ulong 32 |
| 886 | put g_p_id add ib i get g_sha_out i |
| 887 | end |
| 888 | ret 0 |
| 889 | end |
| 890 | |
| 891 | fn prog_put_insn s ulong w ulong to void |
| 892 | cl = get g_p_clen s |
| 893 | cb = add mul s 1024 cl |
| 894 | put g_p_code cb uchar and w 0xff |
| 895 | put g_p_code add cb 1 uchar and shr w 8 0xff |
| 896 | put g_p_code add cb 2 uchar and shr w 16 0xff |
| 897 | put g_p_code add cb 3 uchar and shr w 24 0xff |
| 898 | put g_p_clen s add cl 4 |
| 899 | end |
| 900 | |
| 901 | fn prog_emit_body s ulong to long |
| 902 | if eq get g_p_has s 0 |
| 903 | ret -1 |
| 904 | end |
| 905 | put g_p_clen s 0 |
| 906 | sc = get g_p_scnt s |
| 907 | sb = mul s 64 |
| 908 | for i ulong sc bound 64 |
| 909 | imm = get g_p_simm add sb i |
| 910 | op = get g_p_sop add sb i |
| 911 | lo = and imm 0xffff |
| 912 | hi = and shr imm 16 0xffff |
| 913 | prog_put_insn s or 0xd2800001 shl lo 5 |
| 914 | if ne hi 0 |
| 915 | prog_put_insn s or 0xf2a00001 shl hi 5 |
| 916 | end |
| 917 | if eq op 3 |
| 918 | prog_put_insn s 0x8b010000 |
| 919 | else |
| 920 | if eq op 4 |
| 921 | prog_put_insn s 0xcb010000 |
| 922 | else |
| 923 | if eq op 5 |
| 924 | prog_put_insn s 0x9b017c00 |
| 925 | else |
| 926 | if eq op 9 |
| 927 | prog_put_insn s 0x8a010000 |
| 928 | else |
| 929 | prog_put_insn s 0xaa010000 |
| 930 | end |
| 931 | end |
| 932 | end |
| 933 | end |
| 934 | end |
| 935 | prog_put_insn s 0xd65f03c0 |
| 936 | ret 0 |
| 937 | end |
| 938 | |
| 939 | fn tmp_put_insn w ulong to void |
| 940 | cl = get g_code_tlen 0 |
| 941 | put g_code_tmp cl uchar and w 0xff |
| 942 | put g_code_tmp add cl 1 uchar and shr w 8 0xff |
| 943 | put g_code_tmp add cl 2 uchar and shr w 16 0xff |
| 944 | put g_code_tmp add cl 3 uchar and shr w 24 0xff |
| 945 | put g_code_tlen 0 add cl 4 |
| 946 | end |
| 947 | |
| 948 | fn emit_lift_to_tmp to void |
| 949 | put g_code_tlen 0 0 |
| 950 | sc = get g_lift_cnt 0 |
| 951 | for i ulong sc bound 64 |
| 952 | imm = get g_lift_imm i |
| 953 | op = get g_lift_op i |
| 954 | lo = and imm 0xffff |
| 955 | hi = and shr imm 16 0xffff |
| 956 | tmp_put_insn or 0xd2800001 shl lo 5 |
| 957 | if ne hi 0 |
| 958 | tmp_put_insn or 0xf2a00001 shl hi 5 |
| 959 | end |
| 960 | if eq op 3 |
| 961 | tmp_put_insn 0x8b010000 |
| 962 | else |
| 963 | if eq op 4 |
| 964 | tmp_put_insn 0xcb010000 |
| 965 | else |
| 966 | if eq op 5 |
| 967 | tmp_put_insn 0x9b017c00 |
| 968 | else |
| 969 | if eq op 9 |
| 970 | tmp_put_insn 0x8a010000 |
| 971 | else |
| 972 | tmp_put_insn 0xaa010000 |
| 973 | end |
| 974 | end |
| 975 | end |
| 976 | end |
| 977 | end |
| 978 | tmp_put_insn 0xd65f03c0 |
| 979 | end |
| 980 | |
| 981 | fn read_u32le_code s ulong off ulong to ulong |
| 982 | cb = add mul s 1024 off |
| 983 | b0 = ulong get g_p_code cb |
| 984 | b1 = ulong get g_p_code add cb 1 |
| 985 | b2 = ulong get g_p_code add cb 2 |
| 986 | b3 = ulong get g_p_code add cb 3 |
| 987 | ret or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 988 | end |
| 989 | |
| 990 | fn decode_alu_op w ulong to ulong |
| 991 | if eq w 0x8b010000 |
| 992 | ret 3 |
| 993 | end |
| 994 | if eq w 0xcb010000 |
| 995 | ret 4 |
| 996 | end |
| 997 | if eq w 0x9b017c00 |
| 998 | ret 5 |
| 999 | end |
| 1000 | if eq w 0x8a010000 |
| 1001 | ret 9 |
| 1002 | end |
| 1003 | if eq w 0xaa010000 |
| 1004 | ret 10 |
| 1005 | end |
| 1006 | ret 0 |
| 1007 | end |
| 1008 | |
| 1009 | fn lift_body_slice s ulong off ulong clen ulong to long |
| 1010 | if or lt clen 4 ne rem clen 4 0 |
| 1011 | ret -1 |
| 1012 | end |
| 1013 | n = div clen 4 |
| 1014 | i = ulong 0 |
| 1015 | sc = ulong 0 |
| 1016 | while lt add i 1 n bound 256 |
| 1017 | w = read_u32le_code s add off mul i 4 |
| 1018 | if ne and w 0xffe0001f 0xd2800001 |
| 1019 | ret -1 |
| 1020 | end |
| 1021 | imm = and shr w 5 0xffff |
| 1022 | i = add i 1 |
| 1023 | if lt i n |
| 1024 | w2 = read_u32le_code s add off mul i 4 |
| 1025 | if eq and w2 0xffe0001f 0xf2a00001 |
| 1026 | imm = or imm shl and shr w2 5 0xffff 16 |
| 1027 | i = add i 1 |
| 1028 | end |
| 1029 | end |
| 1030 | if ge i n |
| 1031 | ret -1 |
| 1032 | end |
| 1033 | op = decode_alu_op read_u32le_code s add off mul i 4 |
| 1034 | if eq op 0 |
| 1035 | ret -1 |
| 1036 | end |
| 1037 | i = add i 1 |
| 1038 | if ge sc 64 |
| 1039 | ret -1 |
| 1040 | end |
| 1041 | put g_lift_op sc op |
| 1042 | put g_lift_imm sc imm |
| 1043 | sc = add sc 1 |
| 1044 | end |
| 1045 | if ne read_u32le_code s add off mul sub n 1 4 0xd65f03c0 |
| 1046 | ret -1 |
| 1047 | end |
| 1048 | put g_lift_cnt 0 sc |
| 1049 | emit_lift_to_tmp |
| 1050 | if ne get g_code_tlen 0 clen |
| 1051 | ret -1 |
| 1052 | end |
| 1053 | cb = add mul s 1024 off |
| 1054 | for k ulong clen bound 1024 |
| 1055 | if ne get g_code_tmp k get g_p_code add cb k |
| 1056 | ret -1 |
| 1057 | end |
| 1058 | end |
| 1059 | ret 0 |
| 1060 | end |
| 1061 | |
| 1062 | fn format_unary_post op ulong imm ulong to void |
| 1063 | put g_tmp_post 0 uchar 120 |
| 1064 | put g_tmp_post 1 uchar 32 |
| 1065 | ch = uchar 43 |
| 1066 | if eq op 4 |
| 1067 | ch = uchar 45 |
| 1068 | end |
| 1069 | if eq op 5 |
| 1070 | ch = uchar 42 |
| 1071 | end |
| 1072 | if eq op 9 |
| 1073 | ch = uchar 38 |
| 1074 | end |
| 1075 | if eq op 10 |
| 1076 | ch = uchar 124 |
| 1077 | end |
| 1078 | put g_tmp_post 2 ch |
| 1079 | put g_tmp_post 3 uchar 32 |
| 1080 | pos = ulong 4 |
| 1081 | if eq imm 0 |
| 1082 | put g_tmp_post pos uchar 48 |
| 1083 | pos = add pos 1 |
| 1084 | else |
| 1085 | x = imm |
| 1086 | cnt = ulong 0 |
| 1087 | while gt x 0 bound 24 |
| 1088 | put g_numbuf cnt uchar add rem x 10 48 |
| 1089 | cnt = add cnt 1 |
| 1090 | x = div x 10 |
| 1091 | end |
| 1092 | k = cnt |
| 1093 | while gt k 0 bound 24 |
| 1094 | k = sub k 1 |
| 1095 | put g_tmp_post pos get g_numbuf k |
| 1096 | pos = add pos 1 |
| 1097 | end |
| 1098 | end |
| 1099 | put g_tmp_plen 0 pos |
| 1100 | end |
| 1101 | |
| 1102 | fn prog_build_unary s ulong op ulong imm ulong to long |
| 1103 | if gt imm 0xffffffff |
| 1104 | ret -1 |
| 1105 | end |
| 1106 | prog_init s |
| 1107 | put g_p_has s 1 |
| 1108 | put g_p_scnt s 1 |
| 1109 | sb = mul s 64 |
| 1110 | put g_p_sop sb op |
| 1111 | put g_p_simm sb imm |
| 1112 | ib = mul s 32 |
| 1113 | for i ulong 32 |
| 1114 | put g_p_preid add ib i get g_pre_cache i |
| 1115 | end |
| 1116 | format_unary_post op imm |
| 1117 | hash_constraint_tmp_post |
| 1118 | for i ulong 32 |
| 1119 | put g_p_postid add ib i get g_sha_out i |
| 1120 | end |
| 1121 | if lt prog_compute_id s 0 |
| 1122 | ret -1 |
| 1123 | end |
| 1124 | if lt prog_emit_body s 0 |
| 1125 | ret -1 |
| 1126 | end |
| 1127 | put g_p_insn s div get g_p_clen s 4 |
| 1128 | if eq op 5 |
| 1129 | put g_p_lat s 3 |
| 1130 | else |
| 1131 | put g_p_lat s 2 |
| 1132 | end |
| 1133 | ret 0 |
| 1134 | end |
| 1135 | |
| 1136 | fn prog_set_contract s ulong pre view uchar * post view uchar * to void |
| 1137 | put g_p_lcnt s 0 |
| 1138 | ib = mul s 32 |
| 1139 | hash_constraint_txt 7 pre |
| 1140 | for i ulong 32 |
| 1141 | put g_p_preid add ib i get g_sha_out i |
| 1142 | end |
| 1143 | hash_constraint_txt 8 post |
| 1144 | for i ulong 32 |
| 1145 | put g_p_postid add ib i get g_sha_out i |
| 1146 | end |
| 1147 | rc = prog_compute_id s |
| 1148 | end |
| 1149 | |
| 1150 | note Fold n leaves in g_p_leaves[s] into g_tmp_id |
| 1151 | fn fold_post_seq_slot s ulong n ulong to void |
| 1152 | lb = mul s 2048 |
| 1153 | last = add lb mul sub n 1 32 |
| 1154 | for i ulong 32 |
| 1155 | put g_tmp_id i get g_p_leaves add last i |
| 1156 | end |
| 1157 | idx = sub n 1 |
| 1158 | while gt idx 0 bound 64 |
| 1159 | idx = sub idx 1 |
| 1160 | p = enc_hdr 5 |
| 1161 | p = enc_u16be p 1 |
| 1162 | put g_enc p uchar 13 |
| 1163 | p = add p 1 |
| 1164 | for k ulong 13 |
| 1165 | put g_enc add p k get t_omega_compose k |
| 1166 | end |
| 1167 | p = add p 13 |
| 1168 | p = enc_u16be p 3 |
| 1169 | for k ulong 3 |
| 1170 | put g_enc add p k get t_seq k |
| 1171 | end |
| 1172 | p = add p 3 |
| 1173 | p = enc_u16be p 0 |
| 1174 | p = enc_u16be p 1 |
| 1175 | p = enc_u16be p 8 |
| 1176 | p = enc_u16be p 64 |
| 1177 | cur = add lb mul idx 32 |
| 1178 | for k ulong 32 |
| 1179 | put g_enc add p k get g_p_leaves add cur k |
| 1180 | put g_enc add add p 32 k get g_tmp_id k |
| 1181 | end |
| 1182 | p = add p 64 |
| 1183 | p = enc_u32be p 0 |
| 1184 | hash_enc p |
| 1185 | for k ulong 32 |
| 1186 | put g_tmp_id k get g_sha_out k |
| 1187 | end |
| 1188 | end |
| 1189 | end |
| 1190 | |
| 1191 | fn post_leaves_into_la s ulong to ulong |
| 1192 | lc = get g_p_lcnt s |
| 1193 | if and ge lc 2 le lc 64 |
| 1194 | fold_post_seq_slot s lc |
| 1195 | ib = mul s 32 |
| 1196 | same = true |
| 1197 | for i ulong 32 |
| 1198 | if ne get g_tmp_id i get g_p_postid add ib i |
| 1199 | same = false |
| 1200 | break |
| 1201 | end |
| 1202 | end |
| 1203 | if same |
| 1204 | lb = mul s 2048 |
| 1205 | for i ulong mul lc 32 bound 2048 |
| 1206 | put g_la_id i get g_p_leaves add lb i |
| 1207 | end |
| 1208 | ret lc |
| 1209 | end |
| 1210 | end |
| 1211 | ib = mul s 32 |
| 1212 | for i ulong 32 |
| 1213 | put g_la_id i get g_p_postid add ib i |
| 1214 | end |
| 1215 | ret 1 |
| 1216 | end |
| 1217 | |
| 1218 | fn post_leaves_into_lb s ulong to ulong |
| 1219 | lc = get g_p_lcnt s |
| 1220 | if and ge lc 2 le lc 64 |
| 1221 | fold_post_seq_slot s lc |
| 1222 | ib = mul s 32 |
| 1223 | same = true |
| 1224 | for i ulong 32 |
| 1225 | if ne get g_tmp_id i get g_p_postid add ib i |
| 1226 | same = false |
| 1227 | break |
| 1228 | end |
| 1229 | end |
| 1230 | if same |
| 1231 | lb = mul s 2048 |
| 1232 | for i ulong mul lc 32 bound 2048 |
| 1233 | put g_lb_id i get g_p_leaves add lb i |
| 1234 | end |
| 1235 | ret lc |
| 1236 | end |
| 1237 | end |
| 1238 | ib = mul s 32 |
| 1239 | for i ulong 32 |
| 1240 | put g_lb_id i get g_p_postid add ib i |
| 1241 | end |
| 1242 | ret 1 |
| 1243 | end |
| 1244 | |
| 1245 | fn prog_compose_leaves_copy dst ulong na ulong nb ulong to void |
| 1246 | dlb = mul dst 2048 |
| 1247 | n1 = mul na 32 |
| 1248 | for i ulong n1 bound 2048 |
| 1249 | put g_p_leaves add dlb i get g_la_id i |
| 1250 | end |
| 1251 | dlb2 = add dlb n1 |
| 1252 | n2 = mul nb 32 |
| 1253 | for i ulong n2 bound 2048 |
| 1254 | put g_p_leaves add dlb2 i get g_lb_id i |
| 1255 | end |
| 1256 | end |
| 1257 | |
| 1258 | fn prog_compose_leaves a ulong dst ulong na ulong nb ulong to void |
| 1259 | aib = mul a 32 |
| 1260 | dib = mul dst 32 |
| 1261 | for i ulong 32 |
| 1262 | put g_p_preid add dib i get g_p_preid add aib i |
| 1263 | end |
| 1264 | prog_compose_leaves_copy dst na nb |
| 1265 | put g_p_lcnt dst add na nb |
| 1266 | fold_post_seq_slot dst add na nb |
| 1267 | for i ulong 32 |
| 1268 | put g_p_postid add dib i get g_tmp_id i |
| 1269 | end |
| 1270 | end |
| 1271 | |
| 1272 | fn prog_compose_steps a ulong b ulong dst ulong to void |
| 1273 | sa = get g_p_scnt a |
| 1274 | sb = get g_p_scnt b |
| 1275 | put g_p_has dst 1 |
| 1276 | put g_p_scnt dst add sa sb |
| 1277 | asb = mul a 64 |
| 1278 | bsb = mul b 64 |
| 1279 | dsb = mul dst 64 |
| 1280 | for i ulong sa bound 64 |
| 1281 | put g_p_sop add dsb i get g_p_sop add asb i |
| 1282 | put g_p_simm add dsb i get g_p_simm add asb i |
| 1283 | end |
| 1284 | for i ulong sb bound 64 |
| 1285 | put g_p_sop add add dsb sa i get g_p_sop add bsb i |
| 1286 | put g_p_simm add add dsb sa i get g_p_simm add bsb i |
| 1287 | end |
| 1288 | end |
| 1289 | |
| 1290 | fn prog_compose a ulong b ulong dst ulong to long |
| 1291 | if ne get g_p_outw a get g_p_inw b |
| 1292 | ret -1 |
| 1293 | end |
| 1294 | na = post_leaves_into_la a |
| 1295 | nb = post_leaves_into_lb b |
| 1296 | if gt add na nb 64 |
| 1297 | ret -1 |
| 1298 | end |
| 1299 | if gt add get g_p_scnt a get g_p_scnt b 64 |
| 1300 | ret -1 |
| 1301 | end |
| 1302 | prog_init dst |
| 1303 | put g_p_inw dst get g_p_inw a |
| 1304 | put g_p_outw dst get g_p_outw b |
| 1305 | prog_compose_leaves a dst na nb |
| 1306 | prog_compose_steps a b dst |
| 1307 | ic = add get g_p_insn a get g_p_insn b |
| 1308 | if gt ic 0 |
| 1309 | ic = sub ic 1 |
| 1310 | end |
| 1311 | put g_p_insn dst ic |
| 1312 | put g_p_lat dst add get g_p_lat a get g_p_lat b |
| 1313 | u = prog_compute_id dst |
| 1314 | u = prog_emit_body dst |
| 1315 | ret 0 |
| 1316 | end |
| 1317 | |
| 1318 | note ---- Mini Visor fn Lexer/Parser/Lowerer (for fn lines in test_program_id) ---- |
| 1319 | fn is_id_ch c ulong to bool |
| 1320 | if and ge c 97 le c 122 |
| 1321 | ret true |
| 1322 | end |
| 1323 | if and ge c 65 le c 90 |
| 1324 | ret true |
| 1325 | end |
| 1326 | if and ge c 48 le c 57 |
| 1327 | ret true |
| 1328 | end |
| 1329 | ret eq c 95 |
| 1330 | end |
| 1331 | |
| 1332 | fn dval c ulong to ulong |
| 1333 | if and ge c 48 le c 57 |
| 1334 | ret sub c 48 |
| 1335 | end |
| 1336 | if and ge c 97 le c 102 |
| 1337 | ret add sub c 97 10 |
| 1338 | end |
| 1339 | if and ge c 65 le c 70 |
| 1340 | ret add sub c 65 10 |
| 1341 | end |
| 1342 | ret 99 |
| 1343 | end |
| 1344 | |
| 1345 | fn mtok_eq ti ulong s view uchar * to bool |
| 1346 | if ne get g_tslen ti len s |
| 1347 | ret false |
| 1348 | end |
| 1349 | b = mul ti 64 |
| 1350 | for i ulong len s bound 64 |
| 1351 | if ne get g_tstr add b i get s i |
| 1352 | ret false |
| 1353 | end |
| 1354 | end |
| 1355 | ret true |
| 1356 | end |
| 1357 | |
| 1358 | fn mlex_int start ulong slen ulong k ulong to ulong |
| 1359 | i = start |
| 1360 | base = ulong 10 |
| 1361 | if lt add i 1 slen |
| 1362 | c0 = ulong get g_line i |
| 1363 | c1 = ulong get g_line add i 1 |
| 1364 | if and eq c0 48 eq c1 120 |
| 1365 | base = 16 |
| 1366 | i = add i 2 |
| 1367 | else |
| 1368 | if and eq c0 48 eq c1 98 |
| 1369 | base = 2 |
| 1370 | i = add i 2 |
| 1371 | end |
| 1372 | end |
| 1373 | end |
| 1374 | v = ulong 0 |
| 1375 | while lt i slen bound 64 |
| 1376 | d = dval ulong get g_line i |
| 1377 | if ge d base |
| 1378 | break |
| 1379 | end |
| 1380 | v = add mul v base d |
| 1381 | i = add i 1 |
| 1382 | end |
| 1383 | put g_tkind k 2 |
| 1384 | put g_tval k v |
| 1385 | ret i |
| 1386 | end |
| 1387 | |
| 1388 | fn mlex_ident start ulong slen ulong k ulong to ulong |
| 1389 | j = start |
| 1390 | while lt j slen bound 64 |
| 1391 | if not is_id_ch ulong get g_line j |
| 1392 | break |
| 1393 | end |
| 1394 | j = add j 1 |
| 1395 | end |
| 1396 | nl = sub j start |
| 1397 | tb = mul k 64 |
| 1398 | for m ulong nl bound 64 |
| 1399 | put g_tstr add tb m get g_line add start m |
| 1400 | end |
| 1401 | put g_tslen k nl |
| 1402 | tk = ulong 1 |
| 1403 | if mtok_eq k t_kw_fn |
| 1404 | tk = 4 |
| 1405 | end |
| 1406 | if mtok_eq k t_kw_req |
| 1407 | tk = 5 |
| 1408 | end |
| 1409 | if mtok_eq k t_kw_ens |
| 1410 | tk = 6 |
| 1411 | end |
| 1412 | if mtok_eq k t_kw_true |
| 1413 | tk = 7 |
| 1414 | end |
| 1415 | if mtok_eq k t_kw_false |
| 1416 | tk = 8 |
| 1417 | end |
| 1418 | put g_tkind k tk |
| 1419 | ret j |
| 1420 | end |
| 1421 | |
| 1422 | fn mlex_line s view uchar * to void |
| 1423 | slen = len s |
| 1424 | for i ulong slen bound 256 |
| 1425 | put g_line i get s i |
| 1426 | end |
| 1427 | put g_llen 0 slen |
| 1428 | i = ulong 0 |
| 1429 | k = ulong 0 |
| 1430 | while lt i slen bound 256 |
| 1431 | c = ulong get g_line i |
| 1432 | if or eq c 32 eq c 9 |
| 1433 | i = add i 1 |
| 1434 | continue |
| 1435 | end |
| 1436 | c2 = ulong 0 |
| 1437 | if lt add i 1 slen |
| 1438 | c2 = ulong get g_line add i 1 |
| 1439 | end |
| 1440 | if and eq c 47 eq c2 42 |
| 1441 | i = add i 2 |
| 1442 | while lt add i 1 slen bound 256 |
| 1443 | if eq ulong get g_line i 42 |
| 1444 | if eq ulong get g_line add i 1 47 |
| 1445 | i = add i 2 |
| 1446 | break |
| 1447 | end |
| 1448 | end |
| 1449 | i = add i 1 |
| 1450 | end |
| 1451 | continue |
| 1452 | end |
| 1453 | if and ge c 48 le c 57 |
| 1454 | i = mlex_int i slen k |
| 1455 | k = add k 1 |
| 1456 | continue |
| 1457 | end |
| 1458 | if is_id_ch c |
| 1459 | i = mlex_ident i slen k |
| 1460 | k = add k 1 |
| 1461 | continue |
| 1462 | end |
| 1463 | if and eq c 45 eq c2 62 |
| 1464 | put g_tkind k 15 |
| 1465 | i = add i 2 |
| 1466 | k = add k 1 |
| 1467 | continue |
| 1468 | end |
| 1469 | if and eq c 62 eq c2 61 |
| 1470 | put g_tkind k 28 |
| 1471 | i = add i 2 |
| 1472 | k = add k 1 |
| 1473 | continue |
| 1474 | end |
| 1475 | if and eq c 60 eq c2 61 |
| 1476 | put g_tkind k 26 |
| 1477 | i = add i 2 |
| 1478 | k = add k 1 |
| 1479 | continue |
| 1480 | end |
| 1481 | if and eq c 61 eq c2 61 |
| 1482 | put g_tkind k 23 |
| 1483 | i = add i 2 |
| 1484 | k = add k 1 |
| 1485 | continue |
| 1486 | end |
| 1487 | tk = ulong 0 |
| 1488 | if eq c 40 |
| 1489 | tk = 9 |
| 1490 | end |
| 1491 | if eq c 41 |
| 1492 | tk = 10 |
| 1493 | end |
| 1494 | if eq c 123 |
| 1495 | tk = 11 |
| 1496 | end |
| 1497 | if eq c 125 |
| 1498 | tk = 12 |
| 1499 | end |
| 1500 | if eq c 58 |
| 1501 | tk = 13 |
| 1502 | end |
| 1503 | if eq c 43 |
| 1504 | tk = 17 |
| 1505 | end |
| 1506 | if eq c 45 |
| 1507 | tk = 18 |
| 1508 | end |
| 1509 | if eq c 42 |
| 1510 | tk = 19 |
| 1511 | end |
| 1512 | if eq c 38 |
| 1513 | tk = 21 |
| 1514 | end |
| 1515 | if eq c 124 |
| 1516 | tk = 22 |
| 1517 | end |
| 1518 | if eq c 60 |
| 1519 | tk = 25 |
| 1520 | end |
| 1521 | if eq c 62 |
| 1522 | tk = 27 |
| 1523 | end |
| 1524 | put g_tkind k tk |
| 1525 | i = add i 1 |
| 1526 | k = add k 1 |
| 1527 | end |
| 1528 | put g_tkind k 0 |
| 1529 | put g_tcnt 0 add k 1 |
| 1530 | end |
| 1531 | |
| 1532 | fn mp_peek to ulong |
| 1533 | ret get g_ppos 0 |
| 1534 | end |
| 1535 | |
| 1536 | fn mp_next to ulong |
| 1537 | p = get g_ppos 0 |
| 1538 | put g_ppos 0 add p 1 |
| 1539 | ret p |
| 1540 | end |
| 1541 | |
| 1542 | fn mp_alloc kind ulong to long |
| 1543 | n = get g_ancnt 0 |
| 1544 | put g_ancnt 0 add n 1 |
| 1545 | put g_akind n kind |
| 1546 | put g_aop n 0 |
| 1547 | put g_alhs n -1 |
| 1548 | put g_arhs n -1 |
| 1549 | put g_aval n 0 |
| 1550 | put g_aslen n 0 |
| 1551 | ret long n |
| 1552 | end |
| 1553 | |
| 1554 | fn mp_primary to long |
| 1555 | ti = mp_peek |
| 1556 | tk = get g_tkind ti |
| 1557 | if eq tk 2 |
| 1558 | u = mp_next |
| 1559 | ni = mp_alloc 1 |
| 1560 | put g_aval ulong ni get g_tval ti |
| 1561 | ret ni |
| 1562 | end |
| 1563 | if eq tk 1 |
| 1564 | u = mp_next |
| 1565 | ni = mp_alloc 3 |
| 1566 | ret ni |
| 1567 | end |
| 1568 | if eq tk 9 |
| 1569 | u = mp_next |
| 1570 | r = mp_or |
| 1571 | u = mp_next |
| 1572 | ret r |
| 1573 | end |
| 1574 | ret -1 |
| 1575 | end |
| 1576 | |
| 1577 | fn mp_mul to long |
| 1578 | lhs = mp_primary |
| 1579 | while eq get g_tkind mp_peek 19 bound 32 |
| 1580 | u = mp_next |
| 1581 | rhs = mp_primary |
| 1582 | ni = mp_alloc 4 |
| 1583 | put g_aop ulong ni 5 |
| 1584 | put g_alhs ulong ni lhs |
| 1585 | put g_arhs ulong ni rhs |
| 1586 | lhs = ni |
| 1587 | end |
| 1588 | ret lhs |
| 1589 | end |
| 1590 | |
| 1591 | fn mp_add to long |
| 1592 | lhs = mp_mul |
| 1593 | while or eq get g_tkind mp_peek 17 eq get g_tkind mp_peek 18 bound 32 |
| 1594 | ti = mp_next |
| 1595 | op = ulong 3 |
| 1596 | if eq get g_tkind ti 18 |
| 1597 | op = 4 |
| 1598 | end |
| 1599 | rhs = mp_mul |
| 1600 | ni = mp_alloc 4 |
| 1601 | put g_aop ulong ni op |
| 1602 | put g_alhs ulong ni lhs |
| 1603 | put g_arhs ulong ni rhs |
| 1604 | lhs = ni |
| 1605 | end |
| 1606 | ret lhs |
| 1607 | end |
| 1608 | |
| 1609 | fn mp_and to long |
| 1610 | lhs = mp_add |
| 1611 | while eq get g_tkind mp_peek 21 bound 32 |
| 1612 | u = mp_next |
| 1613 | rhs = mp_add |
| 1614 | ni = mp_alloc 4 |
| 1615 | put g_aop ulong ni 9 |
| 1616 | put g_alhs ulong ni lhs |
| 1617 | put g_arhs ulong ni rhs |
| 1618 | lhs = ni |
| 1619 | end |
| 1620 | ret lhs |
| 1621 | end |
| 1622 | |
| 1623 | fn mp_or to long |
| 1624 | lhs = mp_and |
| 1625 | while eq get g_tkind mp_peek 22 bound 32 |
| 1626 | u = mp_next |
| 1627 | rhs = mp_and |
| 1628 | ni = mp_alloc 4 |
| 1629 | put g_aop ulong ni 10 |
| 1630 | put g_alhs ulong ni lhs |
| 1631 | put g_arhs ulong ni rhs |
| 1632 | lhs = ni |
| 1633 | end |
| 1634 | ret lhs |
| 1635 | end |
| 1636 | |
| 1637 | fn mcl_append is_ens bool s view uchar * to void |
| 1638 | if is_ens |
| 1639 | cur = get g_ens_len 0 |
| 1640 | if gt cur 0 |
| 1641 | put g_ens_buf cur uchar 32 |
| 1642 | cur = add cur 1 |
| 1643 | end |
| 1644 | for i ulong len s bound 64 |
| 1645 | put g_ens_buf add cur i get s i |
| 1646 | end |
| 1647 | put g_ens_len 0 add cur len s |
| 1648 | ret |
| 1649 | end |
| 1650 | cur = get g_req_len 0 |
| 1651 | if gt cur 0 |
| 1652 | put g_req_buf cur uchar 32 |
| 1653 | cur = add cur 1 |
| 1654 | end |
| 1655 | for i ulong len s bound 64 |
| 1656 | put g_req_buf add cur i get s i |
| 1657 | end |
| 1658 | put g_req_len 0 add cur len s |
| 1659 | end |
| 1660 | |
| 1661 | fn mcl_append_u64 is_ens bool v ulong to void |
| 1662 | cnt = ulong 0 |
| 1663 | if eq v 0 |
| 1664 | put g_numbuf 0 uchar 48 |
| 1665 | cnt = 1 |
| 1666 | else |
| 1667 | x = v |
| 1668 | while gt x 0 bound 24 |
| 1669 | put g_numbuf cnt uchar add rem x 10 48 |
| 1670 | cnt = add cnt 1 |
| 1671 | x = div x 10 |
| 1672 | end |
| 1673 | i = ulong 0 |
| 1674 | j = sub cnt 1 |
| 1675 | while lt i j bound 24 |
| 1676 | a = get g_numbuf i |
| 1677 | b = get g_numbuf j |
| 1678 | put g_numbuf i b |
| 1679 | put g_numbuf j a |
| 1680 | i = add i 1 |
| 1681 | j = sub j 1 |
| 1682 | end |
| 1683 | end |
| 1684 | if is_ens |
| 1685 | cur = get g_ens_len 0 |
| 1686 | if gt cur 0 |
| 1687 | put g_ens_buf cur uchar 32 |
| 1688 | cur = add cur 1 |
| 1689 | end |
| 1690 | for i ulong cnt bound 24 |
| 1691 | put g_ens_buf add cur i get g_numbuf i |
| 1692 | end |
| 1693 | put g_ens_len 0 add cur cnt |
| 1694 | ret |
| 1695 | end |
| 1696 | cur = get g_req_len 0 |
| 1697 | if gt cur 0 |
| 1698 | put g_req_buf cur uchar 32 |
| 1699 | cur = add cur 1 |
| 1700 | end |
| 1701 | for i ulong cnt bound 24 |
| 1702 | put g_req_buf add cur i get g_numbuf i |
| 1703 | end |
| 1704 | put g_req_len 0 add cur cnt |
| 1705 | end |
| 1706 | |
| 1707 | fn mparse_clause is_ens bool to void |
| 1708 | if is_ens |
| 1709 | put g_ens_len 0 0 |
| 1710 | else |
| 1711 | put g_req_len 0 0 |
| 1712 | end |
| 1713 | while true bound 32 |
| 1714 | ti = mp_peek |
| 1715 | tk = get g_tkind ti |
| 1716 | if or or eq tk 0 eq tk 11 eq tk 6 |
| 1717 | break |
| 1718 | end |
| 1719 | if eq tk 7 |
| 1720 | mcl_append is_ens t_kw_true |
| 1721 | else |
| 1722 | if eq tk 2 |
| 1723 | mcl_append_u64 is_ens get g_tval ti |
| 1724 | else |
| 1725 | if eq tk 1 |
| 1726 | if mtok_eq ti t_res_result |
| 1727 | mcl_append is_ens t_res_result |
| 1728 | else |
| 1729 | mcl_append is_ens t_x |
| 1730 | end |
| 1731 | else |
| 1732 | if eq tk 28 |
| 1733 | mcl_append is_ens t_tok_ge |
| 1734 | end |
| 1735 | if eq tk 23 |
| 1736 | mcl_append is_ens t_tok_eq |
| 1737 | end |
| 1738 | if eq tk 25 |
| 1739 | mcl_append is_ens t_tok_lt |
| 1740 | end |
| 1741 | if eq tk 18 |
| 1742 | mcl_append is_ens t_tok_sub |
| 1743 | end |
| 1744 | end |
| 1745 | end |
| 1746 | end |
| 1747 | u = mp_next |
| 1748 | end |
| 1749 | end |
| 1750 | |
| 1751 | fn lower_fn_body root ulong slot ulong to void |
| 1752 | r_ops = new ulong 32 0 |
| 1753 | r_imms = new ulong 32 0 |
| 1754 | sc = ulong 0 |
| 1755 | idx = root |
| 1756 | while eq get g_akind idx 4 bound 32 |
| 1757 | op = get g_aop idx |
| 1758 | li = ulong get g_alhs idx |
| 1759 | ri = ulong get g_arhs idx |
| 1760 | if eq get g_akind ri 1 |
| 1761 | put r_ops sc op |
| 1762 | put r_imms sc get g_aval ri |
| 1763 | idx = li |
| 1764 | else |
| 1765 | put r_ops sc op |
| 1766 | put r_imms sc get g_aval li |
| 1767 | idx = ri |
| 1768 | end |
| 1769 | sc = add sc 1 |
| 1770 | end |
| 1771 | prog_init slot |
| 1772 | put g_p_has slot 1 |
| 1773 | put g_p_scnt slot sc |
| 1774 | sb = mul slot 64 |
| 1775 | for i ulong sc bound 32 |
| 1776 | put g_p_sop add sb i get r_ops sub sub sc 1 i |
| 1777 | put g_p_simm add sb i get r_imms sub sub sc 1 i |
| 1778 | end |
| 1779 | end |
| 1780 | |
| 1781 | fn lower_fn_to_slot s view uchar * slot ulong to long |
| 1782 | mlex_line s |
| 1783 | put g_ppos 0 0 |
| 1784 | put g_ancnt 0 0 |
| 1785 | u = mp_next |
| 1786 | nm = mp_next |
| 1787 | put g_aname0 0 ulong get g_tstr mul nm 64 |
| 1788 | u = mp_next |
| 1789 | u = mp_next |
| 1790 | u = mp_next |
| 1791 | u = mp_next |
| 1792 | u = mp_next |
| 1793 | u = mp_next |
| 1794 | u = mp_next |
| 1795 | for i ulong 4 |
| 1796 | put g_req_buf i get t_kw_true i |
| 1797 | put g_ens_buf i get t_kw_true i |
| 1798 | end |
| 1799 | put g_req_len 0 4 |
| 1800 | put g_ens_len 0 4 |
| 1801 | if eq get g_tkind mp_peek 5 |
| 1802 | u = mp_next |
| 1803 | mparse_clause false |
| 1804 | end |
| 1805 | if eq get g_tkind mp_peek 6 |
| 1806 | u = mp_next |
| 1807 | mparse_clause true |
| 1808 | end |
| 1809 | u = mp_next |
| 1810 | root = mp_or |
| 1811 | lower_fn_body ulong root slot |
| 1812 | ib = mul slot 32 |
| 1813 | hash_constraint_buf 7 false |
| 1814 | for i ulong 32 |
| 1815 | put g_p_preid add ib i get g_sha_out i |
| 1816 | end |
| 1817 | hash_constraint_buf 8 true |
| 1818 | for i ulong 32 |
| 1819 | put g_p_postid add ib i get g_sha_out i |
| 1820 | end |
| 1821 | rc = prog_compute_id slot |
| 1822 | rc = prog_emit_body slot |
| 1823 | put g_p_insn slot div get g_p_clen slot 4 |
| 1824 | ret 0 |
| 1825 | end |
| 1826 | |
| 1827 | note ---- Gate 0: BODY_BOUND ---- |
| 1828 | fn test_body_bound to void |
| 1829 | chk 0 and eq prog_build_unary 0 3 1 0 eq prog_build_unary 1 3 2 0 |
| 1830 | chk 0 not prog_id_eq 0 1 |
| 1831 | prog_set_contract 0 t_pre_true t_post_true |
| 1832 | prog_set_contract 1 t_pre_true t_post_true |
| 1833 | chk 0 not prog_id_eq 0 1 |
| 1834 | chk 0 eq prog_build_unary 2 4 1 0 |
| 1835 | prog_set_contract 2 t_pre_true t_post_true |
| 1836 | chk 0 not prog_id_eq 0 2 |
| 1837 | chk 0 and eq get g_p_insn 0 get g_p_insn 2 eq get g_p_lat 0 get g_p_lat 2 |
| 1838 | chk 0 and eq lower_fn_to_slot s_fn_f1 0 0 eq lower_fn_to_slot s_fn_f2 1 0 |
| 1839 | chk 0 not prog_id_eq 0 1 |
| 1840 | chk 0 and eq lower_fn_to_slot s_fn_t21 0 0 eq lower_fn_to_slot s_fn_t31 1 0 |
| 1841 | chk 0 not prog_id_eq 0 1 |
| 1842 | u = prog_build_unary 4 5 2 |
| 1843 | u = prog_build_unary 5 3 1 |
| 1844 | chk 0 eq prog_compose 4 5 0 0 |
| 1845 | u = prog_build_unary 4 3 1 |
| 1846 | u = prog_build_unary 5 5 2 |
| 1847 | chk 0 eq prog_compose 4 5 1 0 |
| 1848 | prog_set_contract 0 t_pre_xge0 t_post_same |
| 1849 | prog_set_contract 1 t_pre_xge0 t_post_same |
| 1850 | chk 0 not prog_id_eq 0 1 |
| 1851 | u = prog_body_root 0 |
| 1852 | for i ulong 32 |
| 1853 | put g_tmp_id i get g_cur i |
| 1854 | end |
| 1855 | u = prog_body_root 1 |
| 1856 | diff = false |
| 1857 | for i ulong 32 |
| 1858 | if ne get g_tmp_id i get g_cur i |
| 1859 | diff = true |
| 1860 | end |
| 1861 | end |
| 1862 | chk 0 diff |
| 1863 | prog_init 3 |
| 1864 | ib = ulong 96 |
| 1865 | for i ulong 32 |
| 1866 | put g_p_id add ib i uchar 0xab |
| 1867 | end |
| 1868 | chk 0 and eq prog_compute_id 3 -1 prog_id_zero 3 |
| 1869 | chk 0 ne prog_build_unary 3 3 0x100000000 0 |
| 1870 | chk 0 eq prog_build_unary 3 3 0xffffffff 0 |
| 1871 | note v1 recipe vs v2 id |
| 1872 | u = prog_build_unary 0 3 1 |
| 1873 | put g_sha_in 0 uchar 80 |
| 1874 | put g_sha_in 1 uchar 82 |
| 1875 | put g_sha_in 2 uchar 79 |
| 1876 | put g_sha_in 3 uchar 71 |
| 1877 | put g_sha_in 4 uchar 1 |
| 1878 | put g_sha_in 5 uchar 102 |
| 1879 | put g_sha_in 6 uchar 3 |
| 1880 | put g_sha_in 7 uchar 64 |
| 1881 | put g_sha_in 8 uchar 3 |
| 1882 | put g_sha_in 9 uchar 64 |
| 1883 | for i ulong 32 |
| 1884 | put g_sha_in add 10 i get g_p_preid i |
| 1885 | put g_sha_in add 42 i get g_p_postid i |
| 1886 | end |
| 1887 | put g_sha_in 74 uchar and get g_p_insn 0 0xff |
| 1888 | put g_sha_in 75 uchar and get g_p_lat 0 0xff |
| 1889 | sha256_hash 76 |
| 1890 | v1diff = false |
| 1891 | for i ulong 32 |
| 1892 | if ne get g_sha_out i get g_p_id i |
| 1893 | v1diff = true |
| 1894 | end |
| 1895 | end |
| 1896 | chk 0 v1diff |
| 1897 | chk 0 eq len t_prog_dom 16 |
| 1898 | end |
| 1899 | |
| 1900 | note ---- Gate 1: REPRESENTATION_INVARIANT + Associativity ---- |
| 1901 | fn brng_next to ulong |
| 1902 | x = get g_brng 0 |
| 1903 | x = xor x shr x 12 |
| 1904 | x = xor x shl x 25 |
| 1905 | x = xor x shr x 27 |
| 1906 | put g_brng 0 x |
| 1907 | ret mul x 0x2545f4914f6cdd1d |
| 1908 | end |
| 1909 | |
| 1910 | fn rng_next to ulong |
| 1911 | x = get g_rng 0 |
| 1912 | x = xor x shr x 12 |
| 1913 | x = xor x shl x 25 |
| 1914 | x = xor x shr x 27 |
| 1915 | put g_rng 0 x |
| 1916 | ret mul x 0x2545f4914f6cdd1d |
| 1917 | end |
| 1918 | |
| 1919 | fn op_from_idx k ulong to ulong |
| 1920 | if eq k 0 |
| 1921 | ret 3 |
| 1922 | end |
| 1923 | if eq k 1 |
| 1924 | ret 4 |
| 1925 | end |
| 1926 | if eq k 2 |
| 1927 | ret 5 |
| 1928 | end |
| 1929 | if eq k 3 |
| 1930 | ret 9 |
| 1931 | end |
| 1932 | ret 10 |
| 1933 | end |
| 1934 | |
| 1935 | note Compose parts in slots [lo, hi) (lo..hi-1 in 0..7) into dst (using temp slots 16+depth*2) |
| 1936 | fn compose_bracketed lo ulong hi ulong dst ulong depth ulong to long |
| 1937 | if eq sub hi lo 1 |
| 1938 | prog_copy dst lo |
| 1939 | ret 0 |
| 1940 | end |
| 1941 | span = sub sub hi lo 1 |
| 1942 | sp = add add lo 1 rem brng_next span |
| 1943 | ls = add 16 mul depth 2 |
| 1944 | rs = add ls 1 |
| 1945 | if lt compose_bracketed lo sp ls add depth 1 0 |
| 1946 | ret -1 |
| 1947 | end |
| 1948 | if lt compose_bracketed sp hi rs add depth 1 0 |
| 1949 | ret -1 |
| 1950 | end |
| 1951 | ret prog_compose ls rs dst |
| 1952 | end |
| 1953 | |
| 1954 | fn assoc_set_post7 slot ulong pi ulong to void |
| 1955 | put g_p_lcnt slot 0 |
| 1956 | ib = mul slot 32 |
| 1957 | pb = mul pi 32 |
| 1958 | for k ulong 32 |
| 1959 | put g_p_preid add ib k get g_pre_cache k |
| 1960 | put g_p_postid add ib k get g_post7_cache add pb k |
| 1961 | end |
| 1962 | rc = prog_compute_id slot |
| 1963 | end |
| 1964 | |
| 1965 | fn assoc_one_chain to ulong |
| 1966 | n = add 3 rem rng_next 6 |
| 1967 | for i ulong n bound 8 |
| 1968 | v = rng_next |
| 1969 | u = prog_build_unary i op_from_idx rem v 5 rem shr v 8 100 |
| 1970 | if eq rem shr v 40 4 0 |
| 1971 | assoc_set_post7 i rem shr v 48 7 |
| 1972 | end |
| 1973 | end |
| 1974 | prog_copy 8 0 |
| 1975 | for i ulong sub n 1 bound 8 |
| 1976 | u = prog_compose 8 add i 1 9 |
| 1977 | prog_copy 8 9 |
| 1978 | end |
| 1979 | chk 1 eq get g_p_lcnt 8 n |
| 1980 | for b ulong 4 |
| 1981 | rc = compose_bracketed 0 n 10 0 |
| 1982 | ok = eq rc 0 |
| 1983 | if not prog_id_eq 10 8 |
| 1984 | ok = false |
| 1985 | end |
| 1986 | if ne get g_p_lcnt 10 n |
| 1987 | ok = false |
| 1988 | end |
| 1989 | chk 1 ok |
| 1990 | end |
| 1991 | if or ne get g_p_sop 0 get g_p_sop 64 ne get g_p_simm 0 get g_p_simm 64 |
| 1992 | r1 = prog_compose 0 1 10 |
| 1993 | r2 = prog_compose 1 0 11 |
| 1994 | ok = and eq r1 0 eq r2 0 |
| 1995 | if prog_id_eq 10 11 |
| 1996 | ok = false |
| 1997 | end |
| 1998 | chk 1 ok |
| 1999 | ret 1 |
| 2000 | end |
| 2001 | ret 0 |
| 2002 | end |
| 2003 | |
| 2004 | fn test_assoc_stale to void |
| 2005 | prog_copy 10 8 |
| 2006 | hash_constraint_txt 8 t_post_repl |
| 2007 | for i ulong 32 |
| 2008 | put g_p_postid add 320 i get g_sha_out i |
| 2009 | end |
| 2010 | prog_copy 11 8 |
| 2011 | prog_set_contract 11 t_pre_xge0 t_post_repl |
| 2012 | r1 = prog_compose 10 0 12 |
| 2013 | r2 = prog_compose 11 0 13 |
| 2014 | ok = and eq r1 0 eq r2 0 |
| 2015 | if ne get g_p_lcnt 12 2 |
| 2016 | ok = false |
| 2017 | end |
| 2018 | for i ulong 32 |
| 2019 | if ne get g_p_postid add 384 i get g_p_postid add 416 i |
| 2020 | ok = false |
| 2021 | end |
| 2022 | end |
| 2023 | chk 1 ok |
| 2024 | end |
| 2025 | |
| 2026 | fn test_associativity to void |
| 2027 | put g_brng 0 0x243f6a8885a308d3 |
| 2028 | put g_rng 0 0x5851f42d4c957f2d |
| 2029 | swaps = ulong 0 |
| 2030 | for ch ulong 400 |
| 2031 | swaps = add swaps assoc_one_chain |
| 2032 | end |
| 2033 | test_assoc_stale |
| 2034 | write_all t_assoc1 len t_assoc1 |
| 2035 | print_u64 400 |
| 2036 | write_all t_assoc2 len t_assoc2 |
| 2037 | print_u64 1600 |
| 2038 | write_all t_assoc3 len t_assoc3 |
| 2039 | print_u64 swaps |
| 2040 | write_all t_assoc4 len t_assoc4 |
| 2041 | end |
| 2042 | |
| 2043 | fn test_representation to void |
| 2044 | u = prog_build_unary 0 3 1 |
| 2045 | u = prog_build_unary 1 3 1 |
| 2046 | chk 1 prog_id_eq 0 1 |
| 2047 | prog_copy 2 0 |
| 2048 | u = prog_compute_id 2 |
| 2049 | chk 1 prog_id_eq 0 2 |
| 2050 | put g_p_insn 2 add get g_p_insn 2 7 |
| 2051 | put g_p_lat 2 99 |
| 2052 | put g_p_code 2048 xor get g_p_code 2048 uchar 0xff |
| 2053 | u = prog_compute_id 2 |
| 2054 | chk 1 prog_id_eq 0 2 |
| 2055 | chk 1 eq lower_fn_to_slot s_same_0 0 0 |
| 2056 | chk 1 and eq lower_fn_to_slot s_same_1 1 0 prog_id_eq 0 1 |
| 2057 | chk 1 and eq lower_fn_to_slot s_same_2 1 0 prog_id_eq 0 1 |
| 2058 | chk 1 and eq lower_fn_to_slot s_same_3 1 0 prog_id_eq 0 1 |
| 2059 | chk 1 and eq lower_fn_to_slot s_same_4 1 0 prog_id_eq 0 1 |
| 2060 | chk 1 and eq lower_fn_to_slot s_same_5 1 0 prog_id_eq 0 1 |
| 2061 | u = prog_build_unary 3 5 2 |
| 2062 | u = prog_build_unary 4 3 1 |
| 2063 | chk 1 eq prog_compose 3 4 2 0 |
| 2064 | prog_set_contract 2 t_pre_true t_post_rge1 |
| 2065 | chk 1 prog_id_eq 2 0 |
| 2066 | test_associativity |
| 2067 | note Lift + re-emit roundtrip on ref (slot 0) |
| 2068 | u = lower_fn_to_slot s_same_0 0 |
| 2069 | cl = get g_p_clen 0 |
| 2070 | chk 1 eq lift_body_slice 0 0 cl 0 |
| 2071 | chk 1 eq get g_lift_cnt 0 get g_p_scnt 0 |
| 2072 | chk 1 and eq get g_lift_op 0 5 eq get g_lift_imm 0 2 |
| 2073 | chk 1 and eq get g_lift_op 1 3 eq get g_lift_imm 1 1 |
| 2074 | chk 1 ne lift_body_slice 0 4 sub cl 4 0 |
| 2075 | end |
| 2076 | |
| 2077 | note ---- Gate 2: MUTATION_SEPARATION ---- |
| 2078 | fn diff_compute_id idx ulong to void |
| 2079 | w = get g_d_w idx |
| 2080 | sc = get g_d_scnt idx |
| 2081 | load_tid_in w |
| 2082 | sb = mul idx 8 |
| 2083 | for i ulong sc bound 8 |
| 2084 | hash_val_uint_cur w get g_d_imm add sb i |
| 2085 | hash_op_cur ulong get g_d_op add sb i |
| 2086 | hash_apply_cur |
| 2087 | end |
| 2088 | load_tid_out w |
| 2089 | for i ulong 16 |
| 2090 | put g_sha_in i get t_prog_dom i |
| 2091 | end |
| 2092 | put g_sha_in 16 uchar 0 |
| 2093 | pb = mul ulong get g_d_pre idx 32 |
| 2094 | qb = mul ulong get g_d_post idx 32 |
| 2095 | for i ulong 32 |
| 2096 | put g_sha_in add 17 i get g_cur i |
| 2097 | put g_sha_in add 49 i get g_tid_in i |
| 2098 | put g_sha_in add 81 i get g_tid_out i |
| 2099 | put g_sha_in add 113 i get g_pre_cache add pb i |
| 2100 | put g_sha_in add 145 i get g_post_cache add qb i |
| 2101 | end |
| 2102 | sha256_hash 177 |
| 2103 | ib = mul idx 32 |
| 2104 | for i ulong 32 |
| 2105 | put g_d_id add ib i get g_sha_out i |
| 2106 | end |
| 2107 | end |
| 2108 | |
| 2109 | fn diff_gen idx ulong to void |
| 2110 | n = add 1 rem rng_next 3 |
| 2111 | put g_d_scnt idx n |
| 2112 | sb = mul idx 8 |
| 2113 | for i ulong n bound 8 |
| 2114 | put g_d_op add sb i uchar op_from_idx rem rng_next 5 |
| 2115 | put g_d_imm add sb i rem rng_next 3 |
| 2116 | end |
| 2117 | w = ulong 64 |
| 2118 | if eq rem rng_next 4 0 |
| 2119 | w = 32 |
| 2120 | end |
| 2121 | put g_d_w idx w |
| 2122 | put g_d_pre idx uchar rem rng_next 3 |
| 2123 | put g_d_post idx uchar rem rng_next 3 |
| 2124 | u = rng_next |
| 2125 | diff_compute_id idx |
| 2126 | end |
| 2127 | |
| 2128 | fn diff_oracle_eq a ulong b ulong to bool |
| 2129 | sa = get g_d_scnt a |
| 2130 | if ne sa get g_d_scnt b |
| 2131 | ret false |
| 2132 | end |
| 2133 | if ne get g_d_w a get g_d_w b |
| 2134 | ret false |
| 2135 | end |
| 2136 | if ne get g_d_pre a get g_d_pre b |
| 2137 | ret false |
| 2138 | end |
| 2139 | if ne get g_d_post a get g_d_post b |
| 2140 | ret false |
| 2141 | end |
| 2142 | ab = mul a 8 |
| 2143 | bb = mul b 8 |
| 2144 | for i ulong sa bound 8 |
| 2145 | if ne get g_d_op add ab i get g_d_op add bb i |
| 2146 | ret false |
| 2147 | end |
| 2148 | if ne get g_d_imm add ab i get g_d_imm add bb i |
| 2149 | ret false |
| 2150 | end |
| 2151 | end |
| 2152 | ret true |
| 2153 | end |
| 2154 | |
| 2155 | fn diff_id_eq a ulong b ulong to bool |
| 2156 | ab = mul a 32 |
| 2157 | bb = mul b 32 |
| 2158 | for i ulong 32 |
| 2159 | if ne get g_d_id add ab i get g_d_id add bb i |
| 2160 | ret false |
| 2161 | end |
| 2162 | end |
| 2163 | ret true |
| 2164 | end |
| 2165 | |
| 2166 | fn gen_wide_slot s ulong to ulong |
| 2167 | prog_init s |
| 2168 | put g_p_has s 1 |
| 2169 | n = add 1 rem rng_next 8 |
| 2170 | put g_p_scnt s n |
| 2171 | sb = mul s 64 |
| 2172 | for i ulong n bound 8 |
| 2173 | put g_p_sop add sb i op_from_idx rem rng_next 5 |
| 2174 | put g_p_simm add sb i and rng_next 0xffffffff |
| 2175 | end |
| 2176 | w = ulong 64 |
| 2177 | if eq rem rng_next 4 0 |
| 2178 | w = 32 |
| 2179 | end |
| 2180 | put g_p_inw s w |
| 2181 | put g_p_outw s w |
| 2182 | pr = rem rng_next 3 |
| 2183 | po = rem rng_next 3 |
| 2184 | u = rng_next |
| 2185 | ib = mul s 32 |
| 2186 | pb = mul pr 32 |
| 2187 | qb = mul po 32 |
| 2188 | for i ulong 32 |
| 2189 | put g_p_preid add ib i get g_pre_cache add pb i |
| 2190 | put g_p_postid add ib i get g_post_cache add qb i |
| 2191 | end |
| 2192 | rc = prog_compute_id s |
| 2193 | ret or shl pr 8 po |
| 2194 | end |
| 2195 | |
| 2196 | fn mutate_op k ulong to void |
| 2197 | cur = get g_p_sop add 64 k |
| 2198 | ci = ulong 0 |
| 2199 | for idx ulong 5 |
| 2200 | if eq op_from_idx idx cur |
| 2201 | ci = idx |
| 2202 | end |
| 2203 | end |
| 2204 | step = add 1 rem rng_next 4 |
| 2205 | put g_p_sop add 64 k op_from_idx rem add ci step 5 |
| 2206 | end |
| 2207 | |
| 2208 | fn mutate_finish pr ulong po ulong to void |
| 2209 | pb = mul pr 32 |
| 2210 | qb = mul po 32 |
| 2211 | for i ulong 32 |
| 2212 | put g_p_preid add 32 i get g_pre_cache add pb i |
| 2213 | put g_p_postid add 32 i get g_post_cache add qb i |
| 2214 | end |
| 2215 | u = prog_compute_id 1 |
| 2216 | end |
| 2217 | |
| 2218 | fn mutate_slot kind ulong prpo ulong to void |
| 2219 | prog_copy 1 0 |
| 2220 | sc = get g_p_scnt 1 |
| 2221 | k = rem rng_next sc |
| 2222 | pr = shr prpo 8 |
| 2223 | po = and prpo 255 |
| 2224 | if eq kind 0 |
| 2225 | mutate_op k |
| 2226 | end |
| 2227 | if eq kind 1 |
| 2228 | sh = rem rng_next 32 |
| 2229 | cur = get g_p_simm add 64 k |
| 2230 | put g_p_simm add 64 k xor cur shl ulong 1 sh |
| 2231 | end |
| 2232 | if eq kind 2 |
| 2233 | nw = ulong 64 |
| 2234 | if eq get g_p_inw 1 64 |
| 2235 | nw = 32 |
| 2236 | end |
| 2237 | put g_p_inw 1 nw |
| 2238 | put g_p_outw 1 nw |
| 2239 | end |
| 2240 | if eq kind 3 |
| 2241 | pr = rem add add pr 1 rem rng_next 2 3 |
| 2242 | end |
| 2243 | if eq kind 4 |
| 2244 | po = rem add add po 1 rem rng_next 2 3 |
| 2245 | end |
| 2246 | if eq kind 5 |
| 2247 | if lt sc 64 |
| 2248 | put g_p_sop add 64 sc op_from_idx rem rng_next 5 |
| 2249 | put g_p_simm add 64 sc and rng_next 0xffff |
| 2250 | put g_p_scnt 1 add sc 1 |
| 2251 | end |
| 2252 | end |
| 2253 | if eq kind 6 |
| 2254 | if gt sc 1 |
| 2255 | put g_p_scnt 1 sub sc 1 |
| 2256 | else |
| 2257 | put g_p_simm 64 add get g_p_simm 64 1 |
| 2258 | end |
| 2259 | end |
| 2260 | if eq kind 7 |
| 2261 | nw = ulong 64 |
| 2262 | if eq get g_p_outw 1 64 |
| 2263 | nw = 32 |
| 2264 | end |
| 2265 | put g_p_outw 1 nw |
| 2266 | end |
| 2267 | mutate_finish pr po |
| 2268 | end |
| 2269 | |
| 2270 | fn test_mutation to void |
| 2271 | put g_rng 0 0x9e3779b97f4a7c15 |
| 2272 | for i ulong 3000 |
| 2273 | diff_gen i |
| 2274 | end |
| 2275 | eq_pairs = ulong 0 |
| 2276 | pairs = ulong 0 |
| 2277 | for i ulong 3000 |
| 2278 | j = add i 1 |
| 2279 | while lt j 3000 bound 3000 |
| 2280 | o = diff_oracle_eq i j |
| 2281 | h = diff_id_eq i j |
| 2282 | pairs = add pairs 1 |
| 2283 | if o |
| 2284 | eq_pairs = add eq_pairs 1 |
| 2285 | end |
| 2286 | chk 2 eq o h |
| 2287 | j = add j 1 |
| 2288 | end |
| 2289 | end |
| 2290 | chk 2 gt eq_pairs 100 |
| 2291 | write_all t_diff1 len t_diff1 |
| 2292 | print_u64 3000 |
| 2293 | write_all t_diff2 len t_diff2 |
| 2294 | print_u64 pairs |
| 2295 | write_all t_diff3 len t_diff3 |
| 2296 | print_u64 eq_pairs |
| 2297 | write_all t_diff4 len t_diff4 |
| 2298 | |
| 2299 | put g_rng 0 0xd1b54a32d192ed03 |
| 2300 | mut = ulong 0 |
| 2301 | for i ulong 2000 |
| 2302 | prpo = gen_wide_slot 0 |
| 2303 | for kind ulong 8 |
| 2304 | mutate_slot kind prpo |
| 2305 | mut = add mut 1 |
| 2306 | chk 2 not prog_id_eq 1 0 |
| 2307 | end |
| 2308 | end |
| 2309 | write_all t_mut1 len t_mut1 |
| 2310 | print_u64 mut |
| 2311 | write_all t_mut2 len t_mut2 |
| 2312 | print_u64 2000 |
| 2313 | write_all t_mut3 len t_mut3 |
| 2314 | end |
| 2315 | |
| 2316 | note ---- Gate 3: LIBRARY_REGRESSION ---- |
| 2317 | fn lib_reset to void |
| 2318 | put g_lib_cnt 0 0 |
| 2319 | end |
| 2320 | |
| 2321 | fn lib_find_slot s ulong to long |
| 2322 | cnt = get g_lib_cnt 0 |
| 2323 | sb = mul s 32 |
| 2324 | for i ulong cnt bound 16 |
| 2325 | lb = mul i 32 |
| 2326 | same = true |
| 2327 | for k ulong 32 |
| 2328 | if ne get g_lib_id add lb k get g_p_id add sb k |
| 2329 | same = false |
| 2330 | break |
| 2331 | end |
| 2332 | end |
| 2333 | if same |
| 2334 | ret long i |
| 2335 | end |
| 2336 | end |
| 2337 | ret -1 |
| 2338 | end |
| 2339 | |
| 2340 | fn lib_insert s ulong to long |
| 2341 | if or eq get g_p_has s 0 prog_id_zero s |
| 2342 | ret -1 |
| 2343 | end |
| 2344 | if ge lib_find_slot s 0 |
| 2345 | ret -1 |
| 2346 | end |
| 2347 | cnt = get g_lib_cnt 0 |
| 2348 | if ge cnt 16 |
| 2349 | ret -1 |
| 2350 | end |
| 2351 | put g_lib_cnt 0 add cnt 1 |
| 2352 | put g_lib_dep_cnt cnt 0 |
| 2353 | sb = mul s 32 |
| 2354 | lb = mul cnt 32 |
| 2355 | for k ulong 32 |
| 2356 | put g_lib_id add lb k get g_p_id add sb k |
| 2357 | end |
| 2358 | ret long cnt |
| 2359 | end |
| 2360 | |
| 2361 | fn lib_add_dep li ulong dep_slot ulong to void |
| 2362 | dc = get g_lib_dep_cnt li |
| 2363 | db = add mul li 128 mul dc 32 |
| 2364 | sb = mul dep_slot 32 |
| 2365 | for k ulong 32 |
| 2366 | put g_lib_deps add db k get g_p_id add sb k |
| 2367 | end |
| 2368 | put g_lib_dep_cnt li add dc 1 |
| 2369 | end |
| 2370 | |
| 2371 | fn lib_has_dep_on li ulong target_slot ulong to bool |
| 2372 | dc = get g_lib_dep_cnt li |
| 2373 | tb = mul target_slot 32 |
| 2374 | for d ulong dc bound 4 |
| 2375 | db = add mul li 128 mul d 32 |
| 2376 | same = true |
| 2377 | for k ulong 32 |
| 2378 | if ne get g_lib_deps add db k get g_p_id add tb k |
| 2379 | same = false |
| 2380 | break |
| 2381 | end |
| 2382 | end |
| 2383 | if same |
| 2384 | ret true |
| 2385 | end |
| 2386 | end |
| 2387 | ret false |
| 2388 | end |
| 2389 | |
| 2390 | fn compute_triple_id pslot ulong codeslot ulong to void |
| 2391 | for i ulong 23 |
| 2392 | put g_sha_in i get t_triple_dom i |
| 2393 | end |
| 2394 | put g_sha_in 23 uchar 0 |
| 2395 | pb = mul pslot 32 |
| 2396 | for i ulong 32 |
| 2397 | put g_sha_in add 24 i get g_p_id add pb i |
| 2398 | put g_sha_in add 56 i uchar add i 1 |
| 2399 | end |
| 2400 | cl = get g_p_clen codeslot |
| 2401 | cb = mul codeslot 1024 |
| 2402 | for i ulong cl bound 1024 |
| 2403 | put g_sha_in add 88 i get g_p_code add cb i |
| 2404 | end |
| 2405 | sha256_hash add 88 cl |
| 2406 | end |
| 2407 | |
| 2408 | fn test_library to void |
| 2409 | lib_reset |
| 2410 | u = prog_build_unary 0 3 7 |
| 2411 | u = prog_build_unary 1 3 7 |
| 2412 | u = prog_build_unary 2 3 8 |
| 2413 | chk 3 eq lib_insert 0 0 |
| 2414 | chk 3 and lt lib_insert 1 0 eq get g_lib_cnt 0 1 |
| 2415 | chk 3 and eq lib_insert 2 1 eq get g_lib_cnt 0 2 |
| 2416 | fa = lib_find_slot 0 |
| 2417 | fc = lib_find_slot 2 |
| 2418 | chk 3 and ge fa 0 and ge fc 0 ne fa fc |
| 2419 | prog_init 3 |
| 2420 | u = prog_compute_id 3 |
| 2421 | chk 3 and lt lib_insert 3 0 eq get g_lib_cnt 0 2 |
| 2422 | chk 3 eq prog_compose 0 2 4 0 |
| 2423 | ci = lib_insert 4 |
| 2424 | chk 3 ge ci 0 |
| 2425 | lib_add_dep ulong ci 0 |
| 2426 | lib_add_dep ulong ci 2 |
| 2427 | chk 3 lib_has_dep_on ulong ci 0 |
| 2428 | note Corpus benchmark (4 programs sharing mul2;add1) |
| 2429 | u = prog_build_unary 10 5 2 |
| 2430 | u = prog_build_unary 11 3 1 |
| 2431 | u = prog_compose 10 11 12 |
| 2432 | u = prog_build_unary 13 3 5 |
| 2433 | u = prog_compose 12 13 0 |
| 2434 | u = prog_build_unary 13 5 3 |
| 2435 | u = prog_compose 12 13 1 |
| 2436 | u = prog_build_unary 13 4 4 |
| 2437 | u = prog_compose 12 13 2 |
| 2438 | u = prog_build_unary 13 3 10 |
| 2439 | u = prog_compose 12 13 3 |
| 2440 | chk 3 not prog_id_eq 0 1 |
| 2441 | chk 3 not prog_id_eq 0 2 |
| 2442 | chk 3 not prog_id_eq 0 3 |
| 2443 | chk 3 not prog_id_eq 1 2 |
| 2444 | chk 3 not prog_id_eq 1 3 |
| 2445 | chk 3 not prog_id_eq 2 3 |
| 2446 | note Shared prefix lift (mul2; add1 = 16 bytes + RET) |
| 2447 | chk 3 eq get g_p_scnt 12 2 |
| 2448 | chk 3 and eq get g_p_sop 768 5 eq get g_p_simm 768 2 |
| 2449 | chk 3 and eq get g_p_sop 769 3 eq get g_p_simm 769 1 |
| 2450 | chk 3 not prog_id_zero 12 |
| 2451 | note Refactoring preserves id, changes cost |
| 2452 | prog_copy 4 0 |
| 2453 | put g_p_insn 4 sub get g_p_insn 4 2 |
| 2454 | u = prog_compute_id 4 |
| 2455 | chk 3 and prog_id_eq 4 0 ne get g_p_insn 4 get g_p_insn 0 |
| 2456 | note VC bridge admission into fresh library |
| 2457 | lib_reset |
| 2458 | chk 3 and eq lib_insert 12 0 eq get g_lib_cnt 0 1 |
| 2459 | note Realization triple id separates bodies |
| 2460 | u = prog_build_unary 0 5 3 |
| 2461 | u = prog_build_unary 1 5 4 |
| 2462 | compute_triple_id 0 0 |
| 2463 | for i ulong 32 |
| 2464 | put g_tmp_id i get g_sha_out i |
| 2465 | end |
| 2466 | compute_triple_id 1 0 |
| 2467 | tdiff = false |
| 2468 | for i ulong 32 |
| 2469 | if ne get g_tmp_id i get g_sha_out i |
| 2470 | tdiff = true |
| 2471 | end |
| 2472 | end |
| 2473 | chk 3 tdiff |
| 2474 | note Verify builder program |
| 2475 | chk 3 eq prog_build_unary 0 10 0x10 0 |
| 2476 | chk 3 gt get g_p_clen 0 0 |
| 2477 | note 64-step body accepted, 65-step refused |
| 2478 | u = prog_build_unary 0 3 1 |
| 2479 | u = prog_build_unary 1 3 1 |
| 2480 | for i ulong 63 |
| 2481 | u = prog_compose 0 1 2 |
| 2482 | prog_copy 0 2 |
| 2483 | end |
| 2484 | chk 3 and eq get g_p_scnt 0 64 not prog_id_zero 0 |
| 2485 | chk 3 ne prog_compose 0 1 2 0 |
| 2486 | end |
| 2487 | |
| 2488 | note ---- Gate 4: VISOR_REGRESSION ---- |
| 2489 | fn vs_reset to void |
| 2490 | put g_vs_pcnt 0 0 |
| 2491 | put g_vs_bcnt 0 0 |
| 2492 | end |
| 2493 | |
| 2494 | fn vs_eval_fn s view uchar * to long |
| 2495 | u = lower_fn_to_slot s 29 |
| 2496 | cnt = get g_vs_pcnt 0 |
| 2497 | pidx = long -1 |
| 2498 | for i ulong cnt bound 16 |
| 2499 | if prog_id_eq get g_vs_pslot i 29 |
| 2500 | pidx = long i |
| 2501 | break |
| 2502 | end |
| 2503 | end |
| 2504 | if lt pidx 0 |
| 2505 | slot = add 12 cnt |
| 2506 | prog_copy slot 29 |
| 2507 | put g_vs_pslot cnt slot |
| 2508 | pidx = long cnt |
| 2509 | put g_vs_pcnt 0 add cnt 1 |
| 2510 | end |
| 2511 | nm = get g_aname0 0 |
| 2512 | bc = get g_vs_bcnt 0 |
| 2513 | bi = long -1 |
| 2514 | for i ulong bc bound 16 |
| 2515 | if eq get g_vs_bchar i nm |
| 2516 | bi = long i |
| 2517 | break |
| 2518 | end |
| 2519 | end |
| 2520 | if lt bi 0 |
| 2521 | bi = long bc |
| 2522 | put g_vs_bchar bc nm |
| 2523 | put g_vs_bcnt 0 add bc 1 |
| 2524 | end |
| 2525 | put g_vs_bpidx ulong bi ulong pidx |
| 2526 | ib = mul ulong bi 32 |
| 2527 | pb = mul get g_vs_pslot ulong pidx 32 |
| 2528 | for i ulong 32 |
| 2529 | put g_vs_bid add ib i get g_p_id add pb i |
| 2530 | end |
| 2531 | ret pidx |
| 2532 | end |
| 2533 | |
| 2534 | fn prog_code_eq a ulong b ulong to bool |
| 2535 | cl = get g_p_clen a |
| 2536 | if ne cl get g_p_clen b |
| 2537 | ret false |
| 2538 | end |
| 2539 | ca = mul a 1024 |
| 2540 | cb = mul b 1024 |
| 2541 | for i ulong cl bound 1024 |
| 2542 | if ne get g_p_code add ca i get g_p_code add cb i |
| 2543 | ret false |
| 2544 | end |
| 2545 | end |
| 2546 | ret true |
| 2547 | end |
| 2548 | |
| 2549 | fn test_visor to void |
| 2550 | vs_reset |
| 2551 | p1 = vs_eval_fn s_vis_t1 |
| 2552 | chk 4 ge p1 0 |
| 2553 | prog_copy 0 get g_vs_pslot ulong p1 |
| 2554 | vs_reset |
| 2555 | p2 = vs_eval_fn s_vis_t2 |
| 2556 | chk 4 ge p2 0 |
| 2557 | prog_copy 1 get g_vs_pslot ulong p2 |
| 2558 | chk 4 not prog_id_eq 0 1 |
| 2559 | np = get g_vs_pcnt 0 |
| 2560 | p3 = vs_eval_fn s_vis_t3 |
| 2561 | chk 4 and ge p3 0 eq get g_vs_pcnt 0 add np 1 |
| 2562 | prog_copy 2 get g_vs_pslot ulong p3 |
| 2563 | chk 4 not prog_id_eq 1 2 |
| 2564 | np = get g_vs_pcnt 0 |
| 2565 | p4 = vs_eval_fn s_vis_t3b |
| 2566 | chk 4 and eq p4 p3 eq get g_vs_pcnt 0 np |
| 2567 | p5 = vs_eval_fn s_vis_u3 |
| 2568 | chk 4 and eq p5 p3 eq get g_vs_pcnt 0 np |
| 2569 | s3 = get g_vs_pslot ulong p3 |
| 2570 | prog_copy 3 s3 |
| 2571 | u = prog_compute_id 3 |
| 2572 | chk 4 prog_id_eq 3 s3 |
| 2573 | u = prog_emit_body 3 |
| 2574 | chk 4 prog_code_eq 3 s3 |
| 2575 | p6 = vs_eval_fn s_vis_te |
| 2576 | chk 4 and ge p6 0 not prog_id_eq get g_vs_pslot ulong p6 s3 |
| 2577 | end |
| 2578 | |
| 2579 | fn print_gate idx ulong name view uchar * to bool |
| 2580 | p = get g_pass idx |
| 2581 | t = get g_total idx |
| 2582 | ok = and eq p t gt t 0 |
| 2583 | write_all t_gate len t_gate |
| 2584 | write_all name len name |
| 2585 | if ok |
| 2586 | write_all t_sp_pass len t_sp_pass |
| 2587 | else |
| 2588 | write_all t_sp_fail len t_sp_fail |
| 2589 | end |
| 2590 | print_u64 p |
| 2591 | write_all t_slash len t_slash |
| 2592 | print_u64 t |
| 2593 | write_all t_nl len t_nl |
| 2594 | ret ok |
| 2595 | end |
| 2596 | |
| 2597 | fn main to int |
| 2598 | init_caches |
| 2599 | test_body_bound |
| 2600 | test_representation |
| 2601 | test_mutation |
| 2602 | test_library |
| 2603 | test_visor |
| 2604 | ok = true |
| 2605 | if not print_gate 0 t_g0 |
| 2606 | ok = false |
| 2607 | end |
| 2608 | if not print_gate 1 t_g1 |
| 2609 | ok = false |
| 2610 | end |
| 2611 | if not print_gate 2 t_g2 |
| 2612 | ok = false |
| 2613 | end |
| 2614 | if not print_gate 3 t_g3 |
| 2615 | ok = false |
| 2616 | end |
| 2617 | if not print_gate 4 t_g4 |
| 2618 | ok = false |
| 2619 | end |
| 2620 | tp = ulong 0 |
| 2621 | tt = ulong 0 |
| 2622 | for i ulong 5 |
| 2623 | tp = add tp get g_pass i |
| 2624 | tt = add tt get g_total i |
| 2625 | end |
| 2626 | if ok |
| 2627 | write_all t_pass len t_pass |
| 2628 | else |
| 2629 | write_all t_fail len t_fail |
| 2630 | end |
| 2631 | print_u64 tp |
| 2632 | write_all t_slash len t_slash |
| 2633 | print_u64 tt |
| 2634 | write_all t_nl len t_nl |
| 2635 | if ok |
| 2636 | ret 0 |
| 2637 | end |
| 2638 | ret 1 |
| 2639 | end |