Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/aarch64/aarch64.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / aarch64 / aarch64.om` 1215 lines ยท 23902 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aarch64.om: seeded Omega port of src/aarch64_encoder.c (the legacy AArch64 instruction writer) 3 note and src/aarch64_decoder.c (its decoder and code-buffer validator), with the enum numbering of 4 note src/aarch64_target.h and src/aarch64_decoder.h (aien-dev/omega-mv#726). Same words, same 5 note decode fields, same refusals, same validator messages as the C built with the Makefile CFLAGS. 6 note 7 note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length 8 note ulong/uchar arrays, long result). Each a64
returns the 32-bit instruction word the 9 note matching aarch64_emit_ writes, or a refusal code where the C prints 10 note A64_LEGACY_FIELD_OUT_OF_RANGE and aborts: -(k+1) when field k (1-based, in the C parameter order 11 note after max_len) is out of its architectural range, the first such field in the C's check order. 12 note a64put b st w is the C's emit_u32_le: a refusal code passes through unwritten, else -1 when 13 note st 0 + 4 > st 1 (pos, max_len), else the word goes little-endian to b at st 0, st 0 += 4, 0. 14 note So aarch64_emit_(buf, &pos, max_len, fields) = a64put b st a64 fields. 15 note a64addreg sf rd rn rm a64subreg a64mulreg a64andreg a64orrreg a64eorreg (same fields) 16 note a64movreg sf rd rm a64movz sf rd imm16 shift a64movk sf rd imm16 shift 17 note a64ret a64b imm26 a64bcond cond imm19 18 note a64cbz sf rt imm19 a64cbnz sf rt imm19 a64adr rd imm21 19 note a64ldruoff sf rt rn uoff a64struoff sf rt rn uoff a64ldrbuoff rt rn uoff a64strbuoff rt rn uoff 20 note a64ldrpost rt rn simm9 a64strpost a64ldrxpost a64strxpost (same fields) 21 note a64subsimm sf rd rn imm12 a64subsreg sf rd rn rm 22 note a64put b st w b edit uchar 4096 (AARCH64_MAX_CODE_BYTES), st edit ulong 2 23 note a64decode insn out aarch64_decode_instruction; out edit ulong 9 = DecodedInsn cells 24 note op sf rd rn rm imm16 hw branch_imm cond; returns 0 or -1 25 note a64validate code n msg ml aarch64_validate_code_buffer; code view uchar 4096, msg edit uchar 256 26 note A value outside the C parameter's type (bool sf, uint8_t register or shift, uint16_t imm16/uoff/ 27 note imm12, int16_t simm9, int32_t immediates and cond) traps 5: the C cannot receive it. 28 29 note ---- conversions the C does by parameter type: trap 5 where the value does not fit ---- 30 31 fn nbool v ulong to void 32 if gt v 1 33 trap 5 34 end 35 end 36 37 fn nu8 v ulong to void 38 if gt v 255 39 trap 5 40 end 41 end 42 43 fn nu16 v ulong to void 44 if gt v 65535 45 trap 5 46 end 47 end 48 49 fn ni16 v long to void 50 if or lt v -32768 gt v 32767 51 trap 5 52 end 53 end 54 55 fn ni32 v long to void 56 if or lt v -`2147483648` gt v `2147483647` 57 trap 5 58 end 59 end 60 61 note ---- encoder: src/aarch64_encoder.c ---- 62 63 note base | sf<<31 | rm<<16 | rn<<5 | rd, after CHECK3(rd, rn, rm); fields sf rd rn rm = 1 2 3 4 64 fn a64rrr base ulong sf ulong rd ulong rn ulong rm ulong to long 65 nbool sf 66 nu8 rd 67 nu8 rn 68 nu8 rm 69 if gt rd 31 70 ret -3 71 end 72 if gt rn 31 73 ret -4 74 end 75 if gt rm 31 76 ret -5 77 end 78 w = or base shl sf 31 79 w = or w shl rm 16 80 w = or w shl rn 5 81 ret long or w rd 82 end 83 84 note 0x0B000000 85 fn a64addreg sf ulong rd ulong rn ulong rm ulong to long 86 ret a64rrr `184549376` sf rd rn rm 87 end 88 89 note 1<<30 | 0x0B000000 90 fn a64subreg sf ulong rd ulong rn ulong rm ulong to long 91 ret a64rrr `1258291200` sf rd rn rm 92 end 93 94 note MADD Xd, Xn, Xm, XZR: 0x1B000000 | 0x1f<<10 95 fn a64mulreg sf ulong rd ulong rn ulong rm ulong to long 96 ret a64rrr `453016576` sf rd rn rm 97 end 98 99 note 0x0A000000 100 fn a64andreg sf ulong rd ulong rn ulong rm ulong to long 101 ret a64rrr `167772160` sf rd rn rm 102 end 103 104 note 0x2A000000 105 fn a64orrreg sf ulong rd ulong rn ulong rm ulong to long 106 ret a64rrr `704643072` sf rd rn rm 107 end 108 109 note 0x4A000000 110 fn a64eorreg sf ulong rd ulong rn ulong rm ulong to long 111 ret a64rrr `1241513984` sf rd rn rm 112 end 113 114 note ORR rd, XZR, rm after its own CHECK_REG(rd), CHECK_REG(rm); fields sf rd rm = 1 2 3 115 fn a64movreg sf ulong rd ulong rm ulong to long 116 nbool sf 117 nu8 rd 118 nu8 rm 119 if gt rd 31 120 ret -3 121 end 122 if gt rm 31 123 ret -4 124 end 125 ret a64orrreg sf rd 31 rm 126 end 127 128 note MOVZ/MOVK: base | sf<<31 | (shift/16 & 3)<<21 | imm16<<5 | rd; fields sf rd imm16 shift = 1 2 3 4 129 fn a64hw base ulong sf ulong rd ulong imm ulong sh ulong to long 130 nbool sf 131 nu8 rd 132 nu16 imm 133 nu8 sh 134 if gt rd 31 135 ret -3 136 end 137 mx = ulong 16 138 if eq sf 1 139 mx = 48 140 end 141 if or ne rem sh 16 0 gt sh mx 142 ret -5 143 end 144 w = or base shl sf 31 145 w = or w shl and div sh 16 3 21 146 w = or w shl imm 5 147 ret long or w rd 148 end 149 150 note 0x52800000 151 fn a64movz sf ulong rd ulong imm ulong sh ulong to long 152 ret a64hw `1384120320` sf rd imm sh 153 end 154 155 note 0x72800000 156 fn a64movk sf ulong rd ulong imm ulong sh ulong to long 157 ret a64hw `1920991232` sf rd imm sh 158 end 159 160 note 0xD65F03C0 161 fn a64ret to long 162 ret `3596551104` 163 end 164 165 note 0x14000000 | imm26 & 0x03FFFFFF; imm26 in [-2^25, 2^25) is field 1 166 fn a64b imm long to long 167 ni32 imm 168 if or lt imm -`33554432` ge imm `33554432` 169 ret -2 170 end 171 ret long or `335544320` and bitcast ulong imm `67108863` 172 end 173 174 note 0x54000000 | (imm19 & 0x7FFFF)<<5 | cond; cond 0..15 field 1, imm19 field 2 175 fn a64bcond cond long imm long to long 176 ni32 cond 177 ni32 imm 178 if or lt cond 0 gt cond 15 179 ret -2 180 end 181 if or lt imm -262144 ge imm 262144 182 ret -3 183 end 184 w = shl and bitcast ulong imm 524287 5 185 ret long or or `1409286144` w bitcast ulong cond 186 end 187 188 note CBZ/CBNZ: base | sf<<31 | (imm19 & 0x7FFFF)<<5 | rt; fields sf rt imm19 = 1 2 3 189 fn a64cbx base ulong sf ulong rt ulong imm long to long 190 nbool sf 191 nu8 rt 192 ni32 imm 193 if gt rt 31 194 ret -3 195 end 196 if or lt imm -262144 ge imm 262144 197 ret -4 198 end 199 w = or base shl sf 31 200 w = or w shl and bitcast ulong imm 524287 5 201 ret long or w rt 202 end 203 204 note 0x34000000 205 fn a64cbz sf ulong rt ulong imm long to long 206 ret a64cbx `872415232` sf rt imm 207 end 208 209 note 0x35000000 210 fn a64cbnz sf ulong rt ulong imm long to long 211 ret a64cbx `889192448` sf rt imm 212 end 213 214 note 0x10000000 | (imm21 & 3)<<29 | ((imm21 >> 2) & 0x7FFFF)<<5 | rd; fields rd imm21 = 1 2 215 fn a64adr rd ulong imm long to long 216 nu8 rd 217 ni32 imm 218 if gt rd 31 219 ret -2 220 end 221 if or lt imm -`1048576` ge imm `1048576` 222 ret -3 223 end 224 x = and bitcast ulong imm `2097151` 225 w = or `268435456` shl and x 3 29 226 w = or w shl shr x 2 5 227 ret long or w rd 228 end 229 230 note LDR/STR (unsigned offset): (sf ? bx : bw) | (uoff >> (sf ? 3 : 2))<<10 | rn<<5 | rt; 231 note CHECK_SCALED: uoff a multiple of 8 (sf) or 4 with uoff/size <= 4095; fields sf rt rn uoff = 1 2 3 4 232 fn a64ls bx ulong bw ulong sf ulong rt ulong rn ulong uo ulong to long 233 nbool sf 234 nu8 rt 235 nu8 rn 236 nu16 uo 237 if gt rt 31 238 ret -3 239 end 240 if gt rn 31 241 ret -4 242 end 243 sc = ulong 2 244 sz = ulong 4 245 base = bw 246 if eq sf 1 247 sc = 3 248 sz = 8 249 base = bx 250 end 251 if or ne rem uo sz 0 gt div uo sz 4095 252 ret -5 253 end 254 w = or base shl and shr uo sc 4095 10 255 w = or w shl rn 5 256 ret long or w rt 257 end 258 259 note 0xF9400000 / 0xB9400000 260 fn a64ldruoff sf ulong rt ulong rn ulong uo ulong to long 261 ret a64ls `4181721088` `3107979264` sf rt rn uo 262 end 263 264 note 0xF9000000 / 0xB9000000 265 fn a64struoff sf ulong rt ulong rn ulong uo ulong to long 266 ret a64ls `4177526784` `3103784960` sf rt rn uo 267 end 268 269 note LDRB/STRB (unsigned offset): base | uoff<<10 | rn<<5 | rt; uoff 0..4095; fields rt rn uoff = 1 2 3 270 fn a64lsb base ulong rt ulong rn ulong uo ulong to long 271 nu8 rt 272 nu8 rn 273 nu16 uo 274 if gt rt 31 275 ret -2 276 end 277 if gt rn 31 278 ret -3 279 end 280 if gt uo 4095 281 ret -4 282 end 283 w = or base shl uo 10 284 w = or w shl rn 5 285 ret long or w rt 286 end 287 288 note 0x39400000 289 fn a64ldrbuoff rt ulong rn ulong uo ulong to long 290 ret a64lsb `960495616` rt rn uo 291 end 292 293 note 0x39000000 294 fn a64strbuoff rt ulong rn ulong uo ulong to long 295 ret a64lsb `956301312` rt rn uo 296 end 297 298 note post-index: base | (simm9 & 0x1FF)<<12 | rn<<5 | rt; simm9 in [-256, 255]; fields rt rn simm9 = 1 2 3 299 fn a64post base ulong rt ulong rn ulong imm long to long 300 nu8 rt 301 nu8 rn 302 ni16 imm 303 if gt rt 31 304 ret -2 305 end 306 if gt rn 31 307 ret -3 308 end 309 if or lt imm -256 ge imm 256 310 ret -4 311 end 312 w = or base shl and bitcast ulong imm 511 12 313 w = or w shl rn 5 314 ret long or w rt 315 end 316 317 note 0xB8400400 318 fn a64ldrpost rt ulong rn ulong imm long to long 319 ret a64post `3091203072` rt rn imm 320 end 321 322 note 0xB8000400 323 fn a64strpost rt ulong rn ulong imm long to long 324 ret a64post `3087008768` rt rn imm 325 end 326 327 note 0xF8400400 328 fn a64ldrxpost rt ulong rn ulong imm long to long 329 ret a64post `4164944896` rt rn imm 330 end 331 332 note 0xF8000400 333 fn a64strxpost rt ulong rn ulong imm long to long 334 ret a64post `4160750592` rt rn imm 335 end 336 337 note SUBS (immediate): (sf ? 0xF1000000 : 0x71000000) | imm12<<10 | rn<<5 | rd; fields sf rd rn imm12 = 1 2 3 4 338 fn a64subsimm sf ulong rd ulong rn ulong imm ulong to long 339 nbool sf 340 nu8 rd 341 nu8 rn 342 nu16 imm 343 if gt rd 31 344 ret -3 345 end 346 if gt rn 31 347 ret -4 348 end 349 if gt imm 4095 350 ret -5 351 end 352 base = ulong `1895825408` 353 if eq sf 1 354 base = `4043309056` 355 end 356 w = or base shl imm 10 357 w = or w shl rn 5 358 ret long or w rd 359 end 360 361 note SUBS (shifted register): (sf ? 0xEB000000 : 0x6B000000) | rm<<16 | rn<<5 | rd, which is 362 note 0x6B000000 | sf<<31 | ... 363 fn a64subsreg sf ulong rd ulong rn ulong rm ulong to long 364 ret a64rrr `1795162112` sf rd rn rm 365 end 366 367 note emit_u32_le over a 4096-byte buffer; st 0 = *pos, st 1 = max_len. Where the C would write past 368 note a 4096-byte buffer (max_len above 4096, or pos + 4 wrapping), the first byte at index 4096 or 369 note beyond traps 3; the bytes below 4096 are already written. 370 fn a64put b edit uchar 4096 st edit ulong 2 w long to long 371 if lt w 0 372 ret w 373 end 374 x = ulong uint w 375 p = get st 0 376 if gt add p 4 get st 1 377 ret -1 378 end 379 put b p uchar and x 255 380 put b add p 1 uchar and shr x 8 255 381 put b add p 2 uchar and shr x 16 255 382 put b add p 3 uchar shr x 24 383 put st 0 add p 4 384 ret 0 385 end 386 387 note ---- decoder: src/aarch64_decoder.c ---- 388 389 note op; sf = bit sb of insn unless sb is 32; rd; rn when n >= 2; rm when n >= 3 390 fn dregs out edit ulong 9 op ulong insn ulong sb ulong n ulong to void 391 put out 0 op 392 if lt sb 32 393 put out 1 and shr insn sb 1 394 end 395 put out 2 and insn 31 396 if ge n 2 397 put out 3 and shr insn 5 31 398 end 399 if ge n 3 400 put out 4 and shr insn 16 31 401 end 402 end 403 404 note MOVZ/MOVK fields 405 fn dwide out edit ulong 9 op ulong insn ulong to void 406 dregs out op insn 31 1 407 put out 5 and shr insn 5 65535 408 put out 6 and shr insn 21 3 409 end 410 411 note the checks of aarch64_decode_instruction 1-9 (RET .. SUB); 0 matched, 1 not 412 fn dec1 insn ulong out edit ulong 9 to ulong 413 if eq insn `3596551104` 414 put out 0 8 415 put out 3 30 416 ret 0 417 end 418 if eq and insn `4227858432` `335544320` 419 put out 0 9 420 put out 7 and insn `67108863` 421 ret 0 422 end 423 if eq and insn `4278190096` `1409286144` 424 put out 0 10 425 put out 8 and insn 15 426 put out 7 and shr insn 5 524287 427 ret 0 428 end 429 m = and insn `2130706432` 430 if eq m `872415232` 431 dregs out 11 insn 31 1 432 put out 7 and shr insn 5 524287 433 ret 0 434 end 435 if eq m `889192448` 436 dregs out 12 insn 31 1 437 put out 7 and shr insn 5 524287 438 ret 0 439 end 440 if eq and insn `2139095040` `1384120320` 441 dwide out 7 insn 442 ret 0 443 end 444 if eq and insn `2145418240` `453016576` 445 dregs out 3 insn 31 3 446 ret 0 447 end 448 m = and insn `2132803584` 449 if eq m `167772160` 450 dregs out 4 insn 31 3 451 ret 0 452 end 453 if eq m `704643072` 454 dregs out 5 insn 31 3 455 ret 0 456 end 457 if eq m `1241513984` 458 dregs out 6 insn 31 3 459 ret 0 460 end 461 if eq m `184549376` 462 dregs out 1 insn 31 3 463 ret 0 464 end 465 if eq m `1258291200` 466 dregs out 2 insn 31 3 467 ret 0 468 end 469 ret 1 470 end 471 472 note the checks 10-15 (MOVK .. SUBS); 0 matched, 1 not 473 fn dec2 insn ulong out edit ulong 9 to ulong 474 if eq and insn `2139095040` `1920991232` 475 dwide out 13 insn 476 ret 0 477 end 478 if eq and insn `2667577344` `268435456` 479 dregs out 14 insn 32 1 480 ret 0 481 end 482 m = and insn `3208642560` 483 if eq m `3107979264` 484 dregs out 15 insn 30 2 485 ret 0 486 end 487 if eq m `3103784960` 488 dregs out 16 insn 30 2 489 ret 0 490 end 491 m = and insn `3219129344` 492 if eq m `3091203072` 493 dregs out 15 insn 30 2 494 ret 0 495 end 496 if eq m `3087008768` 497 dregs out 16 insn 30 2 498 ret 0 499 end 500 m = and insn `4290772992` 501 if eq m `960495616` 502 dregs out 17 insn 32 2 503 ret 0 504 end 505 if eq m `956301312` 506 dregs out 18 insn 32 2 507 ret 0 508 end 509 if eq and insn `2139095040` `1895825408` 510 dregs out 19 insn 31 2 511 ret 0 512 end 513 if eq and insn `2132803584` `1795162112` 514 dregs out 19 insn 31 3 515 ret 0 516 end 517 ret 1 518 end 519 520 note aarch64_decode_instruction: out = DecodedInsn (op sf rd rn rm imm16 hw branch_imm cond), all 521 note zero first; 0, or -1 with op 0 (DECODED_INVALID). branch_imm is the raw field, not sign-extended. 522 fn a64decode insn ulong out edit ulong 9 to long 523 nu32 insn 524 for i ulong 9 525 put out i 0 526 end 527 if eq dec1 insn out 0 528 ret 0 529 end 530 if eq dec2 insn out 0 531 ret 0 532 end 533 put out 0 0 534 ret -1 535 end 536 537 fn nu32 v ulong to void 538 if gt v `4294967295` 539 trap 5 540 end 541 end 542 543 note ---- validator: aarch64_validate_code_buffer and its snprintf messages ---- 544 545 note append the c low bytes of v (little-endian) to t at n; the new length 546 fn app t edit uchar 128 n ulong v ulong c ulong to ulong 547 for i ulong c 548 put t add n i uchar and shr v mul i 8 255 549 end 550 ret add n c 551 end 552 553 fn hexch v ulong to ulong 554 if lt v 10 555 ret add v 48 556 end 557 ret add v 87 558 end 559 560 note %x of v with at least w digits (w >= 1): %08x is w 8, %zx is w 1 561 fn apphex t edit uchar 128 n ulong v ulong w ulong to ulong 562 k = ulong 1 563 while lt k 16 bound 16 564 if eq shr v mul k 4 0 565 break 566 end 567 k = add k 1 568 end 569 if lt k w 570 k = w 571 end 572 for i ulong k 573 put t add n i uchar hexch and shr v mul sub sub k 1 i 4 15 574 end 575 ret add n k 576 end 577 578 note %zu / %d (nonnegative) of v 579 fn appdec t edit uchar 128 n ulong v ulong to ulong 580 k = ulong 1 581 q = div v 10 582 while gt q 0 bound 20 583 k = add k 1 584 q = div q 10 585 end 586 q = v 587 for i ulong k 588 put t add n sub sub k 1 i uchar add 48 rem q 10 589 q = div q 10 590 end 591 ret add n k 592 end 593 594 note snprintf(msg, ml, ...) of the n bytes in t: at most ml - 1 bytes and a NUL, nothing when ml is 0 595 fn snp msg edit uchar 256 ml ulong t view uchar 128 n ulong to void 596 if eq ml 0 597 ret 598 end 599 m = n 600 if gt m sub ml 1 601 m = sub ml 1 602 end 603 for i ulong m 604 put msg i get t i 605 end 606 put msg m 0 607 end 608 609 note "Empty or null code buffer" 610 fn msgnull t edit uchar 128 to ulong 611 n = app t 0 `8245845148755324229` 8 612 n = app t n `8026294610952547872` 8 613 n = app t n `7306640099798705508` 8 614 ret app t n 114 1 615 end 616 617 note "Code buffer length %zu is not a multiple of 4 bytes" 618 fn msglen t edit uchar 128 v ulong to ulong 619 n = app t 0 `7382915055423352643` 8 620 n = app t n `7453005948419990886` 8 621 n = app t n `2123892` 3 622 n = appdec t n v 623 n = app t n `2338616625293650208` 8 624 n = app t n `8100133413977923681` 8 625 n = app t n `2320515332325795180` 8 626 ret app t n `495623371106` 5 627 end 628 629 note "Undefined/unrecognized AArch64 opcode 0x%08x at offset 0x%zx" 630 fn msgop t edit uchar 128 w ulong o ulong to ulong 631 n = app t 0 `7308895133777555029` 8 632 n = app t n `8026370503058206564` 8 633 n = app t n `4692861198728261223` 8 634 n = app t n `8007457545748968001` 8 635 n = app t n `8660457703282271088` 8 636 n = apphex t n w 8 637 n = app t n `8315446389458100512` 8 638 n = app t n `516203508837` 5 639 ret apphex t n o 1 640 end 641 642 note "Function does not terminate with RET (terminates with op %d)" 643 fn msgret t edit uchar 128 op ulong to ulong 644 n = app t 0 `7957695015192261958` 8 645 n = app t n `8029390865643299872` 8 646 n = app t n `7956010554822500468` 8 647 n = app t n `7526756837608485985` 8 648 n = app t n `7310512214447051296` 8 649 n = app t n `8315180248822541682` 8 650 n = app t n `8101729887664568096` 8 651 n = app t n 32 1 652 n = appdec t n op 653 ret app t n 41 1 654 end 655 656 fn ld32 a view uchar 4096 o ulong to ulong 657 x = ulong get a add o 3 658 x = or shl x 8 ulong get a add o 2 659 x = or shl x 8 ulong get a add o 1 660 ret or shl x 8 ulong get a o 661 end 662 663 note the instruction loop of the validator over c words: 0 when valid, else the message length in t 664 fn valscan code view uchar 4096 c ulong t edit uchar 128 d edit ulong 9 to ulong 665 for i ulong c 666 o = mul i 4 667 w = ld32 code o 668 if or ne a64decode w d 0 eq get d 0 0 669 ret msgop t w o 670 end 671 if and eq i sub c 1 ne get d 0 8 672 ret msgret t get d 0 673 end 674 end 675 ret 0 676 end 677 678 note aarch64_validate_code_buffer(code, n, msg, ml): 0, or -1 with the C's message in msg. 679 note ml above 256 (more capacity than msg has) traps 3 before anything is read or written. 680 fn a64validate code view uchar 4096 n ulong msg edit uchar 256 ml ulong to long 681 if gt ml 256 682 trap 3 683 end 684 t = new uchar 128 0 685 d = new ulong 9 0 686 if eq n 0 687 k = msgnull t 688 snp msg ml t k 689 ret -1 690 end 691 if ne rem n 4 0 692 k = msglen t n 693 snp msg ml t k 694 ret -1 695 end 696 k = valscan code div n 4 t d 697 if gt k 0 698 snp msg ml t k 699 ret -1 700 end 701 ret 0 702 end 703 704 note ==== parity harness: program mode only; everything above this line is the library ==== 705 note Reads corpus records on stdin (om/aarch64/parity/corpus.py), writes one result line per record: 706 note E form f1 f2 f3 f4 one emitter call (aarch64_emit_ into a fresh 4096-byte buffer), then 707 note the decode of the written word and the round-trip mark (see README) 708 note P maxlen pos n calls a sequence of emitter calls into one buffer (buffer-full path) 709 note D word aarch64_decode_instruction 710 note V len ml bytes aarch64_validate_code_buffer 711 note Numbers are lowercase hex without leading zeros (words: 8 digits), refusal values signed decimal. 712 713 text enm "add_reg sub_reg mul_reg and_reg orr_reg eor_reg mov_reg movz ret b b_cond cbz cbnz movk adr ldr_uoff str_uoff ldrb_uoff strb_uoff ldr_post str_post ldr_x_post str_x_post subs_imm subs_reg " 714 text fnm "sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rn rm |sf rd rm |sf rd imm16 shift |- |imm26 |cond imm19 |sf rt imm19 |sf rt imm19 |sf rd imm16 shift |rd imm21 |sf rt rn uoff |sf rt rn uoff |rt rn uoff |rt rn uoff |rt rn simm9 |rt rn simm9 |rt rn simm9 |rt rn simm9 |sf rd rn imm12 |sf rd rn rm |" 715 text tref "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_" 716 717 global ib uchar `1048576` 718 global ist ulong 2 719 global ob uchar `1048576` 720 global obn ulong 1 721 global fv ulong 4 722 723 fn inb to ulong 724 p = get ist 0 725 if ge p get ist 1 726 r = read 0 ib 0 `1048576` 727 if le r 0 728 ret 256 729 end 730 put ist 1 ulong r 731 p = 0 732 end 733 put ist 0 add p 1 734 ret ulong get ib p 735 end 736 737 fn obflush to void 738 n = get obn 0 739 if gt n 0 740 r = write 1 ob 0 n 741 end 742 put obn 0 0 743 end 744 745 fn ob1 c ulong to void 746 n = get obn 0 747 if ge n `1048576` 748 obflush 749 n = 0 750 end 751 put ob n uchar c 752 put obn 0 add n 1 753 end 754 755 fn ohx v ulong to void 756 ob1 32 757 k = ulong 1 758 while lt k 16 bound 16 759 if eq shr v mul k 4 0 760 break 761 end 762 k = add k 1 763 end 764 for i ulong k 765 ob1 hexch and shr v mul sub sub k 1 i 4 15 766 end 767 end 768 769 fn ohx8 v ulong to void 770 ob1 32 771 for i ulong 8 772 ob1 hexch and shr v mul sub 7 i 4 15 773 end 774 end 775 776 fn ohxb b ulong to void 777 ob1 hexch shr b 4 778 ob1 hexch and b 15 779 end 780 781 fn odec v long to void 782 if lt v 0 783 ob1 45 784 end 785 u = ulong 0 786 if lt v 0 787 u = add not bitcast ulong v 1 788 else 789 u = ulong v 790 end 791 t = new uchar 24 0 792 k = ulong 0 793 while true bound 21 794 put t k uchar add 48 rem u 10 795 k = add k 1 796 u = div u 10 797 if eq u 0 798 break 799 end 800 end 801 for i ulong k 802 ob1 ulong get t sub sub k 1 i 803 end 804 end 805 806 fn hexval c ulong to ulong 807 if and ge c 48 le c 57 808 ret sub c 48 809 end 810 if and ge c 97 le c 102 811 ret sub c 87 812 end 813 ret 16 814 end 815 816 fn rdcode to ulong 817 c = inb 818 while or eq c 10 eq c 32 bound 999999 819 c = inb 820 end 821 ret c 822 end 823 824 fn rdhex to ulong 825 c = inb 826 while eq c 32 bound 999999 827 c = inb 828 end 829 v = ulong 0 830 while lt hexval c 16 bound 17 831 v = or shl v 4 hexval c 832 c = inb 833 end 834 ret v 835 end 836 837 fn sgnl v ulong to long 838 if ge v `9223372036854775808` 839 ret neg long add not v 1 840 end 841 ret long v 842 end 843 844 note the word of emitter form f (1..25, the order of aarch64_encoder.h) on the fields in fv 845 fn enca f ulong to long 846 a = get fv 0 847 b = get fv 1 848 c = get fv 2 849 d = get fv 3 850 if eq f 1 851 ret a64addreg a b c d 852 end 853 if eq f 2 854 ret a64subreg a b c d 855 end 856 if eq f 3 857 ret a64mulreg a b c d 858 end 859 if eq f 4 860 ret a64andreg a b c d 861 end 862 if eq f 5 863 ret a64orrreg a b c d 864 end 865 if eq f 6 866 ret a64eorreg a b c d 867 end 868 if eq f 7 869 ret a64movreg a b c 870 end 871 if eq f 8 872 ret a64movz a b c d 873 end 874 if eq f 9 875 ret a64ret 876 end 877 if eq f 10 878 ret a64b sgnl a 879 end 880 if eq f 11 881 ret a64bcond sgnl a sgnl b 882 end 883 if eq f 12 884 ret a64cbz a b sgnl c 885 end 886 ret encb f 887 end 888 889 fn encb f ulong to long 890 a = get fv 0 891 b = get fv 1 892 c = get fv 2 893 d = get fv 3 894 if eq f 13 895 ret a64cbnz a b sgnl c 896 end 897 if eq f 14 898 ret a64movk a b c d 899 end 900 if eq f 15 901 ret a64adr a sgnl b 902 end 903 if eq f 16 904 ret a64ldruoff a b c d 905 end 906 if eq f 17 907 ret a64struoff a b c d 908 end 909 if eq f 18 910 ret a64ldrbuoff a b c 911 end 912 if eq f 19 913 ret a64strbuoff a b c 914 end 915 if eq f 20 916 ret a64ldrpost a b sgnl c 917 end 918 if eq f 21 919 ret a64strpost a b sgnl c 920 end 921 if eq f 22 922 ret a64ldrxpost a b sgnl c 923 end 924 if eq f 23 925 ret a64strxpost a b sgnl c 926 end 927 if eq f 24 928 ret a64subsimm a b c d 929 end 930 if eq f 25 931 ret a64subsreg a b c d 932 end 933 trap 9 934 end 935 936 note print the k-th (from 1) space-ended word of the f-th |-ended entry of fnm 937 fn ofield f ulong k ulong to void 938 e = ulong 1 939 w = ulong 1 940 for i ulong len fnm 941 c = ulong get fnm i 942 if eq c 124 943 e = add e 1 944 w = 1 945 continue 946 end 947 if eq c 32 948 w = add w 1 949 continue 950 end 951 if and eq e f eq w k 952 ob1 c 953 end 954 end 955 end 956 957 fn oname f ulong to void 958 e = ulong 1 959 for i ulong len enm 960 c = ulong get enm i 961 if eq c 32 962 e = add e 1 963 continue 964 end 965 if eq e f 966 ob1 c 967 end 968 end 969 end 970 971 note the C's stderr line for a refusal code r of form f 972 fn orefuse f ulong r long to void 973 for i ulong len tref 974 ob1 ulong get tref i 975 end 976 oname f 977 ob1 32 978 k = ulong sub neg r 1 979 ofield f k 980 ob1 61 981 odec sgnl get fv sub k 1 982 end 983 984 fn sx v ulong bits ulong to long 985 h = shl ulong 1 sub bits 1 986 if ge v h 987 ret sub long v long shl h 1 988 end 989 ret long v 990 end 991 992 note re-encode a decoded word whose op carries every field (-100 for the others) 993 fn reenc d edit ulong 9 to long 994 op = get d 0 995 if and ge op 1 le op 6 996 ret enc4 op get d 1 get d 2 get d 3 get d 4 997 end 998 if eq op 7 999 ret a64movz get d 1 get d 2 get d 5 mul get d 6 16 1000 end 1001 if eq op 13 1002 ret a64movk get d 1 get d 2 get d 5 mul get d 6 16 1003 end 1004 if eq op 8 1005 ret a64ret 1006 end 1007 if eq op 9 1008 ret a64b sx get d 7 26 1009 end 1010 if eq op 10 1011 ret a64bcond long get d 8 sx get d 7 19 1012 end 1013 if eq op 11 1014 ret a64cbz get d 1 get d 2 sx get d 7 19 1015 end 1016 if eq op 12 1017 ret a64cbnz get d 1 get d 2 sx get d 7 19 1018 end 1019 ret -100 1020 end 1021 1022 fn enc4 op ulong a ulong b ulong c ulong d ulong to long 1023 if eq op 1 1024 ret a64addreg a b c d 1025 end 1026 if eq op 2 1027 ret a64subreg a b c d 1028 end 1029 if eq op 3 1030 ret a64mulreg a b c d 1031 end 1032 if eq op 4 1033 ret a64andreg a b c d 1034 end 1035 if eq op 5 1036 ret a64orrreg a b c d 1037 end 1038 ret a64eorreg a b c d 1039 end 1040 1041 note for a form whose decode drops fields: does the decode show the expected op and the input fields? 1042 fn lossok f ulong d edit ulong 9 to bool 1043 op = get d 0 1044 if eq f 15 1045 ret and eq op 14 eq get d 2 get fv 0 1046 end 1047 if and ge f 20 le f 23 1048 ret lospost f d 1049 end 1050 if and ge f 18 le f 19 1051 ok = and eq op sub f 1 eq get d 2 get fv 0 1052 ret and ok eq get d 3 get fv 1 1053 end 1054 if or lt f 16 gt f 25 1055 ret false 1056 end 1057 note forms 16 17 24 25: op, sf, rd, rn; and rm (0 for subs_imm) for 24 25 1058 eop = sub f 1 1059 if ge f 24 1060 eop = 19 1061 end 1062 ok = and eq op eop eq get d 1 get fv 0 1063 ok = and ok eq get d 2 get fv 1 1064 ok = and ok eq get d 3 get fv 2 1065 if eq f 24 1066 ok = and ok eq get d 4 0 1067 end 1068 if eq f 25 1069 ok = and ok eq get d 4 get fv 3 1070 end 1071 ret ok 1072 end 1073 1074 fn lospost f ulong d edit ulong 9 to bool 1075 eop = ulong 15 1076 if or eq f 21 eq f 23 1077 eop = 16 1078 end 1079 esf = ulong 0 1080 if ge f 22 1081 esf = 1 1082 end 1083 ok = and eq get d 0 eop eq get d 1 esf 1084 ok = and ok eq get d 2 get fv 0 1085 ret and ok eq get d 3 get fv 1 1086 end 1087 1088 note E form f1 f2 f3 f4 1089 fn doenc to void 1090 b = new uchar 4096 170 1091 st = new ulong 2 0 1092 d = new ulong 9 0 1093 f = rdhex 1094 for i ulong 4 1095 put fv i rdhex 1096 end 1097 put st 1 4096 1098 w = enca f 1099 if lt w 0 1100 orefuse f w 1101 ret 1102 end 1103 r = a64put b st w 1104 x = ld32 b 0 1105 ob1 69 1106 ohx8 x 1107 ohx bitcast ulong a64decode x d 1108 for i ulong 9 1109 ohx get d i 1110 end 1111 ob1 32 1112 e = reenc d 1113 if ge e 0 1114 if eq ulong e x 1115 ob1 61 1116 else 1117 ob1 33 1118 end 1119 ret 1120 end 1121 if lossok f d 1122 ob1 126 1123 else 1124 ob1 33 1125 end 1126 end 1127 1128 note P maxlen pos n, then n times form f1 f2 f3 f4 1129 fn dobuf to void 1130 b = new uchar 4096 170 1131 st = new ulong 2 0 1132 put st 1 rdhex 1133 p0 = rdhex 1134 put st 0 p0 1135 n = rdhex 1136 ob1 80 1137 for j ulong n 1138 f = rdhex 1139 for i ulong 4 1140 put fv i rdhex 1141 end 1142 ob1 32 1143 odec a64put b st enca f 1144 end 1145 ohx get st 0 1146 ob1 32 1147 e = add p0 mul n 4 1148 if gt e 4096 1149 e = 4096 1150 end 1151 for i ulong sub e p0 1152 ohxb ulong get b add p0 i 1153 end 1154 end 1155 1156 fn dodec to void 1157 d = new ulong 9 0 1158 w = rdhex 1159 ob1 68 1160 ohx bitcast ulong a64decode w d 1161 for i ulong 9 1162 ohx get d i 1163 end 1164 end 1165 1166 note V len ml bytes: bytes is one hex token of len bytes ("-" when empty) 1167 fn doval to void 1168 code = new uchar 4096 0 1169 msg = new uchar 256 170 1170 n = rdhex 1171 ml = rdhex 1172 c = inb 1173 while eq c 32 bound 999999 1174 c = inb 1175 end 1176 k = ulong 0 1177 while lt hexval c 16 bound 999999 1178 e = inb 1179 put code k uchar or shl hexval c 4 hexval e 1180 k = add k 1 1181 c = inb 1182 end 1183 r = a64validate code n msg ml 1184 ob1 86 1185 ob1 32 1186 odec r 1187 ob1 32 1188 for i ulong 100 1189 ohxb ulong get msg i 1190 end 1191 end 1192 1193 fn main to int 1194 for q ulong `4294967296` 1195 c = rdcode 1196 if eq c 256 1197 break 1198 end 1199 if eq c 69 1200 doenc 1201 end 1202 if eq c 80 1203 dobuf 1204 end 1205 if eq c 68 1206 dodec 1207 end 1208 if eq c 86 1209 doval 1210 end 1211 ob1 10 1212 end 1213 obflush 1214 ret 0 1215 end