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 / egraph / gate_egraph.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/egraph/gate_egraph.om: Pure seeded OM-2 verification gate for seed/ng/egraph/ir_egraph.om. |
| 3 | note Program-mode gate (no om2 line; concatenated with printf 'om2\n' + ir.om + ir_egraph.om + gate_egraph.om). |
| 4 | note Exercises all 18 EGP3 rewrite rules across 32-bit and 64-bit widths + real ΩIR blocks, |
| 5 | note verifies strict cost reduction under both SM121 and X925 cost parameters, checks negative controls, |
| 6 | note self-validates the emitted EGP3 binary buffer, and optionally writes it to argv[1]. |
| 7 | |
| 8 | global g_dig uchar 24 |
| 9 | global g_path uchar 1024 |
| 10 | global g_neg ulong 1 |
| 11 | global g_vrm ulong 1 |
| 12 | global g_verr ulong 1 |
| 13 | |
| 14 | text g_msg_pass "NG-EGRAPH-GATE PASS certs=" |
| 15 | text g_msg_rules " rules_exercised=" |
| 16 | text g_msg_neg " neg_controls=" |
| 17 | text g_msg_bytes " egp3_bytes=" |
| 18 | text g_msg_nl "\n" |
| 19 | text g_msg_fail "NG-EGRAPH-GATE FAIL check=" |
| 20 | |
| 21 | fn g_wrt s view uchar * to void |
| 22 | r = write 1 s 0 len s |
| 23 | end |
| 24 | |
| 25 | fn g_num v ulong to void |
| 26 | x = v |
| 27 | p = ulong 24 |
| 28 | for i ulong 21 |
| 29 | p = sub p 1 |
| 30 | put g_dig p uchar add 48 rem x 10 |
| 31 | x = div x 10 |
| 32 | if eq x 0 |
| 33 | r = write 1 g_dig p sub 24 p |
| 34 | ret |
| 35 | end |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | fn g_fail code ulong to int |
| 40 | g_wrt g_msg_fail |
| 41 | g_num code |
| 42 | g_wrt g_msg_nl |
| 43 | ret int code |
| 44 | end |
| 45 | |
| 46 | fn g_popcount18 msk ulong to ulong |
| 47 | c = ulong 0 |
| 48 | for r ulong 18 |
| 49 | if ne and msk shl ulong 1 add r 1 0 |
| 50 | c = add c 1 |
| 51 | end |
| 52 | end |
| 53 | ret c |
| 54 | end |
| 55 | |
| 56 | note Direct algebraic cases 0..3 (compact locals to stay well within omc's register pool) |
| 57 | fn g_build_case_lo cid ulong to ulong |
| 58 | if eq cid 0 |
| 59 | note 32-bit: add(mul(r0, r1), add(r2, 0)) -> fma(r0, r1, r2) [Rules 1, 3] |
| 60 | eg3_reset 32 |
| 61 | r0 = eg3_add_ssa 0 |
| 62 | r1 = eg3_add_ssa 1 |
| 63 | r2 = eg3_add_ssa 2 |
| 64 | t0 = eg3_add_bin EG3_OP_MUL r0 r1 |
| 65 | t1 = eg3_add_bin EG3_OP_ADD r2 eg3_add_const 0 |
| 66 | ret eg3_add_bin EG3_OP_ADD t0 t1 |
| 67 | end |
| 68 | if eq cid 1 |
| 69 | note 32-bit: add(add(0, r2), mul(and(r0, r0), mul(r1, 1))) -> fma(r0, r1, r2) [Rules 2, 4, 5, 11] |
| 70 | eg3_reset 32 |
| 71 | r0 = eg3_add_ssa 0 |
| 72 | r1 = eg3_add_ssa 1 |
| 73 | r2 = eg3_add_ssa 2 |
| 74 | t0 = eg3_add_bin EG3_OP_ADD eg3_add_const 0 r2 |
| 75 | t1 = eg3_add_bin EG3_OP_AND r0 r0 |
| 76 | t2 = eg3_add_bin EG3_OP_MUL r1 eg3_add_const 1 |
| 77 | t3 = eg3_add_bin EG3_OP_MUL t1 t2 |
| 78 | ret eg3_add_bin EG3_OP_ADD t0 t3 |
| 79 | end |
| 80 | if eq cid 2 |
| 81 | note 32-bit: add(mul(8, r0), mul(0, r1)) -> shl(r0, 3) [Rules 6, 8, 18, 3] |
| 82 | eg3_reset 32 |
| 83 | r0 = eg3_add_ssa 0 |
| 84 | r1 = eg3_add_ssa 1 |
| 85 | t0 = eg3_add_bin EG3_OP_MUL eg3_add_const 8 r0 |
| 86 | t1 = eg3_add_bin EG3_OP_MUL eg3_add_const 0 r1 |
| 87 | ret eg3_add_bin EG3_OP_ADD t0 t1 |
| 88 | end |
| 89 | note 64-bit: fma(r0, 1<<33, 0) -> mul(r0, 1<<33) -> shl(r0, 33) [Rules 12, 18, val_hi!=0] |
| 90 | eg3_reset 64 |
| 91 | r0 = eg3_add_ssa 0 |
| 92 | t0 = eg3_add_const shl ulong 1 33 |
| 93 | t1 = eg3_add_const 0 |
| 94 | ret eg3_add_fma r0 t0 t1 |
| 95 | end |
| 96 | |
| 97 | note Direct algebraic cases 4..7 |
| 98 | fn g_build_case_hi cid ulong to ulong |
| 99 | if eq cid 4 |
| 100 | note 32-bit: add(add(r0, mul(r1, r2)), r3) -> add(r3, fma(r1, r2, r0)) [Rules 7, 9, 1, 2] |
| 101 | eg3_reset 32 |
| 102 | r0 = eg3_add_ssa 0 |
| 103 | r1 = eg3_add_ssa 1 |
| 104 | r2 = eg3_add_ssa 2 |
| 105 | r3 = eg3_add_ssa 3 |
| 106 | t0 = eg3_add_bin EG3_OP_MUL r1 r2 |
| 107 | t1 = eg3_add_bin EG3_OP_ADD r0 t0 |
| 108 | ret eg3_add_bin EG3_OP_ADD t1 r3 |
| 109 | end |
| 110 | if eq cid 5 |
| 111 | note 64-bit: mul(mul(r0, 8), 1) -> shl(r0, 3) [Rules 10, 5, 18] |
| 112 | eg3_reset 64 |
| 113 | r0 = eg3_add_ssa 0 |
| 114 | t0 = eg3_add_bin EG3_OP_MUL r0 eg3_add_const 8 |
| 115 | ret eg3_add_bin EG3_OP_MUL t0 eg3_add_const 1 |
| 116 | end |
| 117 | if eq cid 6 |
| 118 | note 32-bit: sub(add(sub(r0, 0), xor(r1, r1)), sub(r2, r2)) -> r0 [Rules 13, 14, 15, 3] |
| 119 | eg3_reset 32 |
| 120 | r0 = eg3_add_ssa 0 |
| 121 | r1 = eg3_add_ssa 1 |
| 122 | r2 = eg3_add_ssa 2 |
| 123 | t0 = eg3_add_bin EG3_OP_SUB r0 eg3_add_const 0 |
| 124 | t1 = eg3_add_bin EG3_OP_XOR r1 r1 |
| 125 | t2 = eg3_add_bin EG3_OP_SUB r2 r2 |
| 126 | t3 = eg3_add_bin EG3_OP_ADD t0 t1 |
| 127 | ret eg3_add_bin EG3_OP_SUB t3 t2 |
| 128 | end |
| 129 | note 64-bit: shl(or(and(r0, r0), xor(r1, r1)), sub(r2, r2)) -> r0 [Rules 11, 14, 15, 16, 17] |
| 130 | eg3_reset 64 |
| 131 | r0 = eg3_add_ssa 0 |
| 132 | r1 = eg3_add_ssa 1 |
| 133 | r2 = eg3_add_ssa 2 |
| 134 | t0 = eg3_add_bin EG3_OP_AND r0 r0 |
| 135 | t1 = eg3_add_bin EG3_OP_XOR r1 r1 |
| 136 | t2 = eg3_add_bin EG3_OP_SUB r2 r2 |
| 137 | t3 = eg3_add_bin EG3_OP_OR t0 t1 |
| 138 | ret eg3_add_bin EG3_OP_SHL t3 t2 |
| 139 | end |
| 140 | |
| 141 | fn g_build_case cid ulong to ulong |
| 142 | if lt cid 4 |
| 143 | ret g_build_case_lo cid |
| 144 | end |
| 145 | ret g_build_case_hi cid |
| 146 | end |
| 147 | |
| 148 | note Verify strict cost reduction for case cid under BOTH SM121 and X925, and emit cert |
| 149 | fn g_run_case cid ulong fnIdx ulong blkIdx ulong to int |
| 150 | eg3_set_costs_x925 |
| 151 | rx = g_build_case cid |
| 152 | eg3_compute_costs |
| 153 | cb = eg3_get_cost rx |
| 154 | eg3_saturate 2 |
| 155 | if ge eg3_get_cost rx cb |
| 156 | ret g_fail add 10 cid |
| 157 | end |
| 158 | eg3_set_costs_sm121 |
| 159 | rs = g_build_case cid |
| 160 | eg3_compute_costs |
| 161 | cb = eg3_get_cost rs |
| 162 | eg3_saturate 2 |
| 163 | if ge eg3_get_cost rs cb |
| 164 | ret g_fail add 20 cid |
| 165 | end |
| 166 | if eq get eg3_trn 0 0 |
| 167 | ret g_fail add 30 cid |
| 168 | end |
| 169 | if not eg3_cert_append fnIdx blkIdx rs eg3_get_best rs |
| 170 | ret g_fail add 40 cid |
| 171 | end |
| 172 | ret 0 |
| 173 | end |
| 174 | |
| 175 | note Emit token-bearing and convergent ops into 32-bit ΩIR block (returns latest token) |
| 176 | fn g_emit_refused32 b uint p0 uint p1 uint p2 uint tok0 uint to uint |
| 177 | o = ir_op b 70 0 0 3 |
| 178 | ir_set o 0 tok0 |
| 179 | ir_set o 1 p0 |
| 180 | ir_set o 2 p1 |
| 181 | tk = ir_otok o |
| 182 | o = ir_op b 42 4 0 3 |
| 183 | ir_set o 0 tk |
| 184 | ir_set o 1 p0 |
| 185 | ir_set o 2 p1 |
| 186 | o = ir_op b 43 0 0 4 |
| 187 | ir_set o 0 tk |
| 188 | ir_set o 1 p0 |
| 189 | ir_set o 2 p1 |
| 190 | ir_set o 3 p2 |
| 191 | tk = ir_otok o |
| 192 | o = ir_op b 110 4 0 1 |
| 193 | ir_set o 0 tk |
| 194 | tk = ir_otok o |
| 195 | o = ir_op b 44 0 0 1 |
| 196 | ir_set o 0 tk |
| 197 | tk = ir_otok o |
| 198 | o = ir_op b 45 4 0 2 |
| 199 | ir_set o 0 p0 |
| 200 | ir_set o 1 p1 |
| 201 | o = ir_op b 106 4 0 3 |
| 202 | ir_set o 0 p0 |
| 203 | ir_set o 1 p1 |
| 204 | ir_set o 2 p2 |
| 205 | ret tk |
| 206 | end |
| 207 | |
| 208 | note Build real ΩIR block 0 (32-bit u32): mixes token-bearing + convergent ops with pure arithmetic |
| 209 | fn g_build_ir_blk32 to uint |
| 210 | ir_init |
| 211 | f = ir_fnew 0 0 4 |
| 212 | p0 = ir_fpar f 4 |
| 213 | p1 = ir_fpar f 4 |
| 214 | p2 = ir_fpar f 4 |
| 215 | b = ir_blk f |
| 216 | tk = g_emit_refused32 b p0 p1 p2 ir_ftok f |
| 217 | c0 = ir_res ir_op b 1 4 0 0 |
| 218 | c8 = ir_res ir_op b 1 4 8 0 |
| 219 | o = ir_op b 12 4 0 2 |
| 220 | ir_set o 0 p0 |
| 221 | ir_set o 1 c8 |
| 222 | v1 = ir_res o |
| 223 | o = ir_op b 12 4 0 2 |
| 224 | ir_set o 0 v1 |
| 225 | ir_set o 1 p1 |
| 226 | v1 = ir_res o |
| 227 | o = ir_op b 10 4 0 2 |
| 228 | ir_set o 0 p2 |
| 229 | ir_set o 1 c0 |
| 230 | v2 = ir_res o |
| 231 | o = ir_op b 10 4 0 2 |
| 232 | ir_set o 0 v1 |
| 233 | ir_set o 1 v2 |
| 234 | v1 = ir_res o |
| 235 | o = ir_op b 63 0 0 2 |
| 236 | ir_set o 0 tk |
| 237 | ir_set o 1 v1 |
| 238 | ret b |
| 239 | end |
| 240 | |
| 241 | note Emit mmald(104), mmast(105), call(64) into 64-bit ΩIR block |
| 242 | fn g_emit_refused64 b uint f uint p0 uint p1 uint p2 uint tok0 uint to uint |
| 243 | o = ir_op b 104 5 0 5 |
| 244 | ir_set o 0 tok0 |
| 245 | ir_set o 1 p0 |
| 246 | ir_set o 2 p1 |
| 247 | ir_set o 3 p2 |
| 248 | ir_set o 4 p0 |
| 249 | o = ir_op b 105 0 0 6 |
| 250 | ir_set o 0 tok0 |
| 251 | ir_set o 1 p0 |
| 252 | ir_set o 2 p1 |
| 253 | ir_set o 3 p2 |
| 254 | ir_set o 4 p0 |
| 255 | ir_set o 5 p1 |
| 256 | tk = ir_otok o |
| 257 | o = ir_op b 64 5 0 2 |
| 258 | ir_set o 0 tk |
| 259 | ir_set o 1 f |
| 260 | ret ir_otok o |
| 261 | end |
| 262 | |
| 263 | note Build real ΩIR block 1 (64-bit u64): mixes mmald, mmast, call with pure u64 ops |
| 264 | fn g_build_ir_blk64 to uint |
| 265 | ir_init |
| 266 | f = ir_fnew 0 0 5 |
| 267 | p0 = ir_fpar f 5 |
| 268 | p1 = ir_fpar f 5 |
| 269 | p2 = ir_fpar f 5 |
| 270 | b = ir_blk f |
| 271 | tk = g_emit_refused64 b f p0 p1 p2 ir_ftok f |
| 272 | c1 = ir_res ir_op b 1 5 1 0 |
| 273 | c16 = ir_res ir_op b 1 5 16 0 |
| 274 | o = ir_op b 12 5 0 2 |
| 275 | ir_set o 0 p0 |
| 276 | ir_set o 1 c1 |
| 277 | v1 = ir_res o |
| 278 | o = ir_op b 12 5 0 2 |
| 279 | ir_set o 0 v1 |
| 280 | ir_set o 1 c16 |
| 281 | v1 = ir_res o |
| 282 | o = ir_op b 11 5 0 2 |
| 283 | ir_set o 0 p1 |
| 284 | ir_set o 1 p1 |
| 285 | v2 = ir_res o |
| 286 | o = ir_op b 17 5 0 2 |
| 287 | ir_set o 0 p2 |
| 288 | ir_set o 1 v2 |
| 289 | v2 = ir_res o |
| 290 | o = ir_op b 10 5 0 2 |
| 291 | ir_set o 0 v1 |
| 292 | ir_set o 1 v2 |
| 293 | v1 = ir_res o |
| 294 | o = ir_op b 63 0 0 2 |
| 295 | ir_set o 0 tk |
| 296 | ir_set o 1 v1 |
| 297 | ret b |
| 298 | end |
| 299 | |
| 300 | fn g_test_ir_blocks to int |
| 301 | b32 = g_build_ir_blk32 |
| 302 | eg3_set_costs_x925 |
| 303 | if eq eg3_opt_ir_block 4 0 b32 32 false 0 |
| 304 | ret g_fail 50 |
| 305 | end |
| 306 | eg3_set_costs_sm121 |
| 307 | if eq eg3_opt_ir_block 4 0 b32 32 true 0 |
| 308 | ret g_fail 51 |
| 309 | end |
| 310 | if ne get eg3_ir_stat 0 5 |
| 311 | ret g_fail 52 |
| 312 | end |
| 313 | if ne get eg3_ir_stat 1 3 |
| 314 | ret g_fail 53 |
| 315 | end |
| 316 | if ne get eg3_ir_stat 11 1 |
| 317 | ret g_fail 54 |
| 318 | end |
| 319 | put g_neg 0 add get g_neg 0 2 |
| 320 | |
| 321 | b64 = g_build_ir_blk64 |
| 322 | eg3_set_costs_x925 |
| 323 | if eq eg3_opt_ir_block 5 0 b64 64 false 0 |
| 324 | ret g_fail 55 |
| 325 | end |
| 326 | eg3_set_costs_sm121 |
| 327 | if eq eg3_opt_ir_block 5 0 b64 64 true 0 |
| 328 | ret g_fail 56 |
| 329 | end |
| 330 | if ne get eg3_ir_stat 0 2 |
| 331 | ret g_fail 57 |
| 332 | end |
| 333 | if ne get eg3_ir_stat 1 2 |
| 334 | ret g_fail 58 |
| 335 | end |
| 336 | if ne get eg3_ir_stat 11 1 |
| 337 | ret g_fail 59 |
| 338 | end |
| 339 | put g_neg 0 add get g_neg 0 2 |
| 340 | ret 0 |
| 341 | end |
| 342 | |
| 343 | note Negative controls part 1: non-power-of-two multiplies MUST NOT fire Rule 18 |
| 344 | fn g_neg_pow2 to int |
| 345 | eg3_set_costs_sm121 |
| 346 | eg3_reset 32 |
| 347 | r0 = eg3_add_ssa 0 |
| 348 | m6 = eg3_add_bin EG3_OP_MUL r0 eg3_add_const 6 |
| 349 | eg3_compute_costs |
| 350 | cb = eg3_get_cost m6 |
| 351 | eg3_saturate 2 |
| 352 | if ne and get eg3_rmask 0 shl ulong 1 18 0 |
| 353 | ret g_fail 60 |
| 354 | end |
| 355 | if ne eg3_get_cost m6 cb |
| 356 | ret g_fail 61 |
| 357 | end |
| 358 | put g_neg 0 add get g_neg 0 1 |
| 359 | |
| 360 | eg3_reset 64 |
| 361 | r0 = eg3_add_ssa 0 |
| 362 | mb = eg3_add_bin EG3_OP_MUL r0 eg3_add_const add shl ulong 1 33 1 |
| 363 | eg3_saturate 2 |
| 364 | if ne and get eg3_rmask 0 shl ulong 1 18 0 |
| 365 | ret g_fail 62 |
| 366 | end |
| 367 | put g_neg 0 add get g_neg 0 1 |
| 368 | ret 0 |
| 369 | end |
| 370 | |
| 371 | note Negative controls part 2: token-bearing, convergent, and float opcodes strictly refused |
| 372 | fn g_neg_opcodes to int |
| 373 | if ne eg3_ir_opcode_check 42 4 EG3_REFUSE_TOKEN |
| 374 | ret g_fail 63 |
| 375 | end |
| 376 | if ne eg3_ir_opcode_check 43 4 EG3_REFUSE_TOKEN |
| 377 | ret g_fail 64 |
| 378 | end |
| 379 | if ne eg3_ir_opcode_check 64 4 EG3_REFUSE_TOKEN |
| 380 | ret g_fail 65 |
| 381 | end |
| 382 | if ne eg3_ir_opcode_check 70 4 EG3_REFUSE_TOKEN |
| 383 | ret g_fail 66 |
| 384 | end |
| 385 | if ne eg3_ir_opcode_check 110 4 EG3_REFUSE_TOKEN |
| 386 | ret g_fail 67 |
| 387 | end |
| 388 | put g_neg 0 add get g_neg 0 1 |
| 389 | |
| 390 | if ne eg3_ir_opcode_check 44 4 EG3_REFUSE_CONVERGENT |
| 391 | ret g_fail 68 |
| 392 | end |
| 393 | if ne eg3_ir_opcode_check 45 4 EG3_REFUSE_CONVERGENT |
| 394 | ret g_fail 69 |
| 395 | end |
| 396 | if ne eg3_ir_opcode_check 104 4 EG3_REFUSE_CONVERGENT |
| 397 | ret g_fail 70 |
| 398 | end |
| 399 | if ne eg3_ir_opcode_check 105 4 EG3_REFUSE_CONVERGENT |
| 400 | ret g_fail 71 |
| 401 | end |
| 402 | if ne eg3_ir_opcode_check 106 4 EG3_REFUSE_CONVERGENT |
| 403 | ret g_fail 72 |
| 404 | end |
| 405 | put g_neg 0 add get g_neg 0 1 |
| 406 | |
| 407 | if ne eg3_ir_opcode_check 10 13 EG3_REFUSE_TYPE |
| 408 | ret g_fail 73 |
| 409 | end |
| 410 | if ne eg3_ir_opcode_check 12 14 EG3_REFUSE_TYPE |
| 411 | ret g_fail 74 |
| 412 | end |
| 413 | put g_neg 0 add get g_neg 0 1 |
| 414 | ret 0 |
| 415 | end |
| 416 | |
| 417 | note Negative controls part 3: pure-token/convergent block yields 0 ops, plus FMA target-cost sensitivity |
| 418 | fn g_neg_blk_cost to int |
| 419 | ir_init |
| 420 | f = ir_fnew 0 0 0 |
| 421 | p0 = ir_fpar f 4 |
| 422 | b = ir_blk f |
| 423 | o = ir_op b 44 0 0 1 |
| 424 | ir_set o 0 ir_ftok f |
| 425 | tk = ir_otok o |
| 426 | o = ir_op b 45 4 0 2 |
| 427 | ir_set o 0 p0 |
| 428 | ir_set o 1 p0 |
| 429 | o = ir_op b 62 0 0 1 |
| 430 | ir_set o 0 tk |
| 431 | d0 = eg3_opt_ir_block 9 0 b 32 true |
| 432 | if or ne d0 0 ne get eg3_ir_stat 4 0 |
| 433 | ret g_fail 75 |
| 434 | end |
| 435 | if ne get eg3_ir_stat 11 0 |
| 436 | ret g_fail 76 |
| 437 | end |
| 438 | put g_neg 0 add get g_neg 0 1 |
| 439 | |
| 440 | eg3_set_target_costs 0 2 8 0 |
| 441 | eg3_reset 32 |
| 442 | r0 = eg3_add_ssa 0 |
| 443 | r1 = eg3_add_ssa 1 |
| 444 | r2 = eg3_add_ssa 2 |
| 445 | m = eg3_add_bin EG3_OP_MUL r0 r1 |
| 446 | rt = eg3_add_bin EG3_OP_ADD m r2 |
| 447 | eg3_saturate 2 |
| 448 | if eq get eg3_op eg3_get_best rt EG3_OP_FMA |
| 449 | ret g_fail 77 |
| 450 | end |
| 451 | eg3_set_target_costs 0 2 8 1 |
| 452 | eg3_compute_costs |
| 453 | if ne get eg3_op eg3_get_best rt EG3_OP_FMA |
| 454 | ret g_fail 78 |
| 455 | end |
| 456 | put g_neg 0 add get g_neg 0 1 |
| 457 | ret 0 |
| 458 | end |
| 459 | |
| 460 | fn g_verify_nodes p0 ulong nc ulong w ulong to ulong |
| 461 | p = p0 |
| 462 | for i ulong nc bound 512 |
| 463 | if ne eg3_get_u32 add p 24 w |
| 464 | put g_verr 0 88 |
| 465 | ret 0 |
| 466 | end |
| 467 | p = add p 32 |
| 468 | end |
| 469 | ret p |
| 470 | end |
| 471 | |
| 472 | fn g_verify_trace p0 ulong nt ulong nc ulong to ulong |
| 473 | p = p0 |
| 474 | for i ulong nt bound 512 |
| 475 | rid = eg3_get_u32 p |
| 476 | if or lt rid 1 gt rid 18 |
| 477 | put g_verr 0 90 |
| 478 | ret 0 |
| 479 | end |
| 480 | if or ge eg3_get_u32 add p 4 nc ge eg3_get_u32 add p 8 nc |
| 481 | put g_verr 0 90 |
| 482 | ret 0 |
| 483 | end |
| 484 | bit = shl ulong 1 rid |
| 485 | put g_vrm 0 or get g_vrm 0 bit |
| 486 | p = add p 12 |
| 487 | end |
| 488 | ret p |
| 489 | end |
| 490 | |
| 491 | note Verify one serialized EGP3 cert entry starting at offset p0; returns next offset or 0 on error |
| 492 | fn g_verify_one_cert p0 ulong nb ulong to ulong |
| 493 | if gt add p0 28 nb |
| 494 | put g_verr 0 84 |
| 495 | ret 0 |
| 496 | end |
| 497 | w = eg3_get_u32 add p0 8 |
| 498 | nc = eg3_get_u32 add p0 12 |
| 499 | rb = eg3_get_u32 add p0 16 |
| 500 | ra = eg3_get_u32 add p0 20 |
| 501 | nt = eg3_get_u32 add p0 24 |
| 502 | if and ne w 32 ne w 64 |
| 503 | put g_verr 0 85 |
| 504 | ret 0 |
| 505 | end |
| 506 | if or eq nc 0 or ge rb nc or ge ra nc eq nt 0 |
| 507 | put g_verr 0 86 |
| 508 | ret 0 |
| 509 | end |
| 510 | p = add p0 28 |
| 511 | if gt add p mul nc 32 nb |
| 512 | put g_verr 0 87 |
| 513 | ret 0 |
| 514 | end |
| 515 | p = g_verify_nodes p nc w |
| 516 | if eq p 0 |
| 517 | ret 0 |
| 518 | end |
| 519 | if gt add p mul nt 12 nb |
| 520 | put g_verr 0 89 |
| 521 | ret 0 |
| 522 | end |
| 523 | ret g_verify_trace p nt nc |
| 524 | end |
| 525 | |
| 526 | fn g_verify_egp3_buf to int |
| 527 | nb = get eg3_bpos 0 |
| 528 | if lt nb 8 |
| 529 | ret g_fail 80 |
| 530 | end |
| 531 | if or ne get eg3_buf 0 69 ne get eg3_buf 1 71 |
| 532 | ret g_fail 81 |
| 533 | end |
| 534 | if or ne get eg3_buf 2 80 ne get eg3_buf 3 51 |
| 535 | ret g_fail 82 |
| 536 | end |
| 537 | ncerts = eg3_get_u32 4 |
| 538 | if ne ncerts get eg3_certs 0 |
| 539 | ret g_fail 83 |
| 540 | end |
| 541 | p = ulong 8 |
| 542 | put g_vrm 0 0 |
| 543 | put g_verr 0 0 |
| 544 | for k ulong ncerts bound 64 |
| 545 | p = g_verify_one_cert p nb |
| 546 | if eq p 0 |
| 547 | ret g_fail get g_verr 0 |
| 548 | end |
| 549 | end |
| 550 | if ne p nb |
| 551 | ret g_fail 91 |
| 552 | end |
| 553 | if ne g_popcount18 get g_vrm 0 18 |
| 554 | ret g_fail 92 |
| 555 | end |
| 556 | ret 0 |
| 557 | end |
| 558 | |
| 559 | fn main to int |
| 560 | put g_neg 0 0 |
| 561 | eg3_cert_init |
| 562 | |
| 563 | for cid ulong 8 |
| 564 | rc = g_run_case cid div cid 2 rem cid 2 |
| 565 | if ne rc 0 |
| 566 | ret rc |
| 567 | end |
| 568 | end |
| 569 | |
| 570 | rc = g_test_ir_blocks |
| 571 | if ne rc 0 |
| 572 | ret rc |
| 573 | end |
| 574 | |
| 575 | rc = g_neg_pow2 |
| 576 | if ne rc 0 |
| 577 | ret rc |
| 578 | end |
| 579 | |
| 580 | rc = g_neg_opcodes |
| 581 | if ne rc 0 |
| 582 | ret rc |
| 583 | end |
| 584 | |
| 585 | rc = g_neg_blk_cost |
| 586 | if ne rc 0 |
| 587 | ret rc |
| 588 | end |
| 589 | |
| 590 | rc = g_verify_egp3_buf |
| 591 | if ne rc 0 |
| 592 | ret rc |
| 593 | end |
| 594 | |
| 595 | if ge argcnt 2 |
| 596 | plen = argread 1 g_path 0 1023 |
| 597 | if lt plen 1 |
| 598 | ret g_fail 95 |
| 599 | end |
| 600 | fd = fopen g_path 0 ulong plen 577 420 |
| 601 | if lt fd 0 |
| 602 | ret g_fail 96 |
| 603 | end |
| 604 | ok = eg3_write_fd int fd |
| 605 | z = fclose int fd |
| 606 | if not ok |
| 607 | ret g_fail 97 |
| 608 | end |
| 609 | end |
| 610 | |
| 611 | g_wrt g_msg_pass |
| 612 | g_num get eg3_certs 0 |
| 613 | g_wrt g_msg_rules |
| 614 | g_num g_popcount18 get eg3_all_rmask 0 |
| 615 | g_wrt g_msg_neg |
| 616 | g_num get g_neg 0 |
| 617 | g_wrt g_msg_bytes |
| 618 | g_num get eg3_bpos 0 |
| 619 | g_wrt g_msg_nl |
| 620 | ret 0 |
| 621 | end |