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_om2.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_om2.om` 705 lines ยท 16025 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note OM-2 unit for AArch64 legacy encoder, decoder, and validator (om/units.sh). 3 om2 4 note aarch64.om: seeded Omega port of src/aarch64_encoder.c (the legacy AArch64 instruction writer) 5 note and src/aarch64_decoder.c (its decoder and code-buffer validator), with the enum numbering of 6 note src/aarch64_target.h and src/aarch64_decoder.h (aien-dev/omega-mv#726). Same words, same 7 note decode fields, same refusals, same validator messages as the C built with the Makefile CFLAGS. 8 note 9 note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length 10 note ulong/uchar arrays, long result). Each a64
returns the 32-bit instruction word the 11 note matching aarch64_emit_ writes, or a refusal code where the C prints 12 note A64_LEGACY_FIELD_OUT_OF_RANGE and aborts: -(k+1) when field k (1-based, in the C parameter order 13 note after max_len) is dst of its architectural range, the first such field in the C's check order. 14 note a64put b st w is the C's emit_u32_le: a refusal code passes through unwritten, else -1 when 15 note st 0 + 4 > st 1 (pos, max_len), else the word goes little-endian to b at st 0, st 0 += 4, 0. 16 note So aarch64_emit_(buf, &pos, max_len, fields) = a64put b st a64 fields. 17 note a64addreg sf rd rn rm a64subreg a64mulreg a64andreg a64orrreg a64eorreg (same fields) 18 note a64movreg sf rd rm a64movz sf rd imm16 shift a64movk sf rd imm16 shift 19 note a64ret a64b imm26 a64bcond cond imm19 20 note a64cbz sf rt imm19 a64cbnz sf rt imm19 a64adr rd imm21 21 note a64ldruoff sf rt rn uoff a64struoff sf rt rn uoff a64ldrbuoff rt rn uoff a64strbuoff rt rn uoff 22 note a64ldrpost rt rn simm9 a64strpost a64ldrxpost a64strxpost (same fields) 23 note a64subsimm sf rd rn imm12 a64subsreg sf rd rn rm 24 note a64put b st w b edit uchar * (AARCH64_MAX_CODE_BYTES), st edit ulong 2 25 note a64decode insn dst aarch64_decode_instruction; dst edit ulong 9 = DecodedInsn cells 26 note op sf rd rn rm imm16 hw branch_imm cond; returns 0 or -1 27 note a64validate code n msg ml aarch64_validate_code_buffer; code view uchar *, msg edit uchar * 28 note A value outside the C parameter's type (bool sf, uint8_t register or shift, uint16_t imm16/uoff/ 29 note imm12, int16_t simm9, int32_t immediates and cond) traps 5: the C cannot receive it. 30 31 note ---- conversions the C does by parameter type: trap 5 where the value does not fit ---- 32 33 fn nbool v ulong to void 34 if gt v 1 35 trap 5 36 end 37 end 38 39 fn nu8 v ulong to void 40 if gt v 255 41 trap 5 42 end 43 end 44 45 fn nu16 v ulong to void 46 if gt v 65535 47 trap 5 48 end 49 end 50 51 fn ni16 v long to void 52 if or lt v -32768 gt v 32767 53 trap 5 54 end 55 end 56 57 fn ni32 v long to void 58 if or lt v -`2147483648` gt v `2147483647` 59 trap 5 60 end 61 end 62 63 note ---- encoder: src/aarch64_encoder.c ---- 64 65 note base | sf<<31 | rm<<16 | rn<<5 | rd, after CHECK3(rd, rn, rm); fields sf rd rn rm = 1 2 3 4 66 fn a64rrr base ulong sf ulong rd ulong rn ulong rm ulong to long 67 nbool sf 68 nu8 rd 69 nu8 rn 70 nu8 rm 71 if gt rd 31 72 ret -3 73 end 74 if gt rn 31 75 ret -4 76 end 77 if gt rm 31 78 ret -5 79 end 80 w = or base shl sf 31 81 w = or w shl rm 16 82 w = or w shl rn 5 83 ret long or w rd 84 end 85 86 note 0x0B000000 87 export fn a64addreg sf ulong rd ulong rn ulong rm ulong to long 88 ret a64rrr `184549376` sf rd rn rm 89 end 90 91 note 1<<30 | 0x0B000000 92 export fn a64subreg sf ulong rd ulong rn ulong rm ulong to long 93 ret a64rrr `1258291200` sf rd rn rm 94 end 95 96 note MADD Xd, Xn, Xm, XZR: 0x1B000000 | 0x1f<<10 97 export fn a64mulreg sf ulong rd ulong rn ulong rm ulong to long 98 ret a64rrr `453016576` sf rd rn rm 99 end 100 101 note 0x0A000000 102 export fn a64andreg sf ulong rd ulong rn ulong rm ulong to long 103 ret a64rrr `167772160` sf rd rn rm 104 end 105 106 note 0x2A000000 107 export fn a64orrreg sf ulong rd ulong rn ulong rm ulong to long 108 ret a64rrr `704643072` sf rd rn rm 109 end 110 111 note 0x4A000000 112 export fn a64eorreg sf ulong rd ulong rn ulong rm ulong to long 113 ret a64rrr `1241513984` sf rd rn rm 114 end 115 116 note ORR rd, XZR, rm after its own CHECK_REG(rd), CHECK_REG(rm); fields sf rd rm = 1 2 3 117 export fn a64movreg sf ulong rd ulong rm ulong to long 118 nbool sf 119 nu8 rd 120 nu8 rm 121 if gt rd 31 122 ret -3 123 end 124 if gt rm 31 125 ret -4 126 end 127 ret a64orrreg sf rd 31 rm 128 end 129 130 note MOVZ/MOVK: base | sf<<31 | (shift/16 & 3)<<21 | imm16<<5 | rd; fields sf rd imm16 shift = 1 2 3 4 131 fn a64hw base ulong sf ulong rd ulong imm ulong sh ulong to long 132 nbool sf 133 nu8 rd 134 nu16 imm 135 nu8 sh 136 if gt rd 31 137 ret -3 138 end 139 mx = ulong 16 140 if eq sf 1 141 mx = 48 142 end 143 if or ne rem sh 16 0 gt sh mx 144 ret -5 145 end 146 w = or base shl sf 31 147 w = or w shl and div sh 16 3 21 148 w = or w shl imm 5 149 ret long or w rd 150 end 151 152 note 0x52800000 153 export fn a64movz sf ulong rd ulong imm ulong sh ulong to long 154 ret a64hw `1384120320` sf rd imm sh 155 end 156 157 note 0x72800000 158 export fn a64movk sf ulong rd ulong imm ulong sh ulong to long 159 ret a64hw `1920991232` sf rd imm sh 160 end 161 162 note 0xD65F03C0 163 export fn a64ret to long 164 ret `3596551104` 165 end 166 167 note 0x14000000 | imm26 & 0x03FFFFFF; imm26 in [-2^25, 2^25) is field 1 168 export fn a64b imm long to long 169 ni32 imm 170 if or lt imm -`33554432` ge imm `33554432` 171 ret -2 172 end 173 ret long or `335544320` and bitcast ulong imm `67108863` 174 end 175 176 note 0x54000000 | (imm19 & 0x7FFFF)<<5 | cond; cond 0..15 field 1, imm19 field 2 177 export fn a64bcond cond long imm long to long 178 ni32 cond 179 ni32 imm 180 if or lt cond 0 gt cond 15 181 ret -2 182 end 183 if or lt imm -262144 ge imm 262144 184 ret -3 185 end 186 w = shl and bitcast ulong imm 524287 5 187 ret long or or `1409286144` w bitcast ulong cond 188 end 189 190 note CBZ/CBNZ: base | sf<<31 | (imm19 & 0x7FFFF)<<5 | rt; fields sf rt imm19 = 1 2 3 191 fn a64cbx base ulong sf ulong rt ulong imm long to long 192 nbool sf 193 nu8 rt 194 ni32 imm 195 if gt rt 31 196 ret -3 197 end 198 if or lt imm -262144 ge imm 262144 199 ret -4 200 end 201 w = or base shl sf 31 202 w = or w shl and bitcast ulong imm 524287 5 203 ret long or w rt 204 end 205 206 note 0x34000000 207 export fn a64cbz sf ulong rt ulong imm long to long 208 ret a64cbx `872415232` sf rt imm 209 end 210 211 note 0x35000000 212 export fn a64cbnz sf ulong rt ulong imm long to long 213 ret a64cbx `889192448` sf rt imm 214 end 215 216 note 0x10000000 | (imm21 & 3)<<29 | ((imm21 >> 2) & 0x7FFFF)<<5 | rd; fields rd imm21 = 1 2 217 export fn a64adr rd ulong imm long to long 218 nu8 rd 219 ni32 imm 220 if gt rd 31 221 ret -2 222 end 223 if or lt imm -`1048576` ge imm `1048576` 224 ret -3 225 end 226 x = and bitcast ulong imm `2097151` 227 w = or `268435456` shl and x 3 29 228 w = or w shl shr x 2 5 229 ret long or w rd 230 end 231 232 note LDR/STR (unsigned offset): (sf ? bx : bw) | (uoff >> (sf ? 3 : 2))<<10 | rn<<5 | rt; 233 note CHECK_SCALED: uoff a multiple of 8 (sf) or 4 with uoff/size <= 4095; fields sf rt rn uoff = 1 2 3 4 234 fn a64ls bx ulong bw ulong sf ulong rt ulong rn ulong uo ulong to long 235 nbool sf 236 nu8 rt 237 nu8 rn 238 nu16 uo 239 if gt rt 31 240 ret -3 241 end 242 if gt rn 31 243 ret -4 244 end 245 sc = ulong 2 246 sz = ulong 4 247 base = bw 248 if eq sf 1 249 sc = 3 250 sz = 8 251 base = bx 252 end 253 if or ne rem uo sz 0 gt div uo sz 4095 254 ret -5 255 end 256 w = or base shl and shr uo sc 4095 10 257 w = or w shl rn 5 258 ret long or w rt 259 end 260 261 note 0xF9400000 / 0xB9400000 262 export fn a64ldruoff sf ulong rt ulong rn ulong uo ulong to long 263 ret a64ls `4181721088` `3107979264` sf rt rn uo 264 end 265 266 note 0xF9000000 / 0xB9000000 267 export fn a64struoff sf ulong rt ulong rn ulong uo ulong to long 268 ret a64ls `4177526784` `3103784960` sf rt rn uo 269 end 270 271 note LDRB/STRB (unsigned offset): base | uoff<<10 | rn<<5 | rt; uoff 0..4095; fields rt rn uoff = 1 2 3 272 fn a64lsb base ulong rt ulong rn ulong uo ulong to long 273 nu8 rt 274 nu8 rn 275 nu16 uo 276 if gt rt 31 277 ret -2 278 end 279 if gt rn 31 280 ret -3 281 end 282 if gt uo 4095 283 ret -4 284 end 285 w = or base shl uo 10 286 w = or w shl rn 5 287 ret long or w rt 288 end 289 290 note 0x39400000 291 export fn a64ldrbuoff rt ulong rn ulong uo ulong to long 292 ret a64lsb `960495616` rt rn uo 293 end 294 295 note 0x39000000 296 export fn a64strbuoff rt ulong rn ulong uo ulong to long 297 ret a64lsb `956301312` rt rn uo 298 end 299 300 note post-index: base | (simm9 & 0x1FF)<<12 | rn<<5 | rt; simm9 in [-256, 255]; fields rt rn simm9 = 1 2 3 301 fn a64post base ulong rt ulong rn ulong imm long to long 302 nu8 rt 303 nu8 rn 304 ni16 imm 305 if gt rt 31 306 ret -2 307 end 308 if gt rn 31 309 ret -3 310 end 311 if or lt imm -256 ge imm 256 312 ret -4 313 end 314 w = or base shl and bitcast ulong imm 511 12 315 w = or w shl rn 5 316 ret long or w rt 317 end 318 319 note 0xB8400400 320 export fn a64ldrpost rt ulong rn ulong imm long to long 321 ret a64post `3091203072` rt rn imm 322 end 323 324 note 0xB8000400 325 export fn a64strpost rt ulong rn ulong imm long to long 326 ret a64post `3087008768` rt rn imm 327 end 328 329 note 0xF8400400 330 export fn a64ldrxpost rt ulong rn ulong imm long to long 331 ret a64post `4164944896` rt rn imm 332 end 333 334 note 0xF8000400 335 export fn a64strxpost rt ulong rn ulong imm long to long 336 ret a64post `4160750592` rt rn imm 337 end 338 339 note SUBS (immediate): (sf ? 0xF1000000 : 0x71000000) | imm12<<10 | rn<<5 | rd; fields sf rd rn imm12 = 1 2 3 4 340 export fn a64subsimm sf ulong rd ulong rn ulong imm ulong to long 341 nbool sf 342 nu8 rd 343 nu8 rn 344 nu16 imm 345 if gt rd 31 346 ret -3 347 end 348 if gt rn 31 349 ret -4 350 end 351 if gt imm 4095 352 ret -5 353 end 354 base = ulong `1895825408` 355 if eq sf 1 356 base = `4043309056` 357 end 358 w = or base shl imm 10 359 w = or w shl rn 5 360 ret long or w rd 361 end 362 363 note SUBS (shifted register): (sf ? 0xEB000000 : 0x6B000000) | rm<<16 | rn<<5 | rd, which is 364 note 0x6B000000 | sf<<31 | ... 365 export fn a64subsreg sf ulong rd ulong rn ulong rm ulong to long 366 ret a64rrr `1795162112` sf rd rn rm 367 end 368 369 note emit_u32_le over a 4096-byte buffer; st 0 = *pos, st 1 = max_len. Where the C would write past 370 note a 4096-byte buffer (max_len above 4096, or pos + 4 wrapping), the first byte at index 4096 or 371 note beyond traps 3; the bytes below 4096 are already written. 372 export fn a64put b edit uchar * st edit ulong 2 w long to long 373 if lt w 0 374 ret w 375 end 376 x = ulong uint w 377 p = get st 0 378 if gt add p 4 get st 1 379 ret -1 380 end 381 put b p uchar and x 255 382 put b add p 1 uchar and shr x 8 255 383 put b add p 2 uchar and shr x 16 255 384 put b add p 3 uchar shr x 24 385 put st 0 add p 4 386 ret 0 387 end 388 389 note ---- decoder: src/aarch64_decoder.c ---- 390 391 note op; sf = bit sb of insn unless sb is 32; rd; rn when n >= 2; rm when n >= 3 392 fn dregs dst edit ulong 9 op ulong insn ulong sb ulong n ulong to void 393 put dst 0 op 394 if lt sb 32 395 put dst 1 and shr insn sb 1 396 end 397 put dst 2 and insn 31 398 if ge n 2 399 put dst 3 and shr insn 5 31 400 end 401 if ge n 3 402 put dst 4 and shr insn 16 31 403 end 404 end 405 406 note MOVZ/MOVK fields 407 fn dwide dst edit ulong 9 op ulong insn ulong to void 408 dregs dst op insn 31 1 409 put dst 5 and shr insn 5 65535 410 put dst 6 and shr insn 21 3 411 end 412 413 note the checks of aarch64_decode_instruction 1-9 (RET .. SUB); 0 matched, 1 not 414 fn dec1 insn ulong dst edit ulong 9 to ulong 415 if eq insn `3596551104` 416 put dst 0 8 417 put dst 3 30 418 ret 0 419 end 420 if eq and insn `4227858432` `335544320` 421 put dst 0 9 422 put dst 7 and insn `67108863` 423 ret 0 424 end 425 if eq and insn `4278190096` `1409286144` 426 put dst 0 10 427 put dst 8 and insn 15 428 put dst 7 and shr insn 5 524287 429 ret 0 430 end 431 m = and insn `2130706432` 432 if eq m `872415232` 433 dregs dst 11 insn 31 1 434 put dst 7 and shr insn 5 524287 435 ret 0 436 end 437 if eq m `889192448` 438 dregs dst 12 insn 31 1 439 put dst 7 and shr insn 5 524287 440 ret 0 441 end 442 if eq and insn `2139095040` `1384120320` 443 dwide dst 7 insn 444 ret 0 445 end 446 if eq and insn `2145418240` `453016576` 447 dregs dst 3 insn 31 3 448 ret 0 449 end 450 m = and insn `2132803584` 451 if eq m `167772160` 452 dregs dst 4 insn 31 3 453 ret 0 454 end 455 if eq m `704643072` 456 dregs dst 5 insn 31 3 457 ret 0 458 end 459 if eq m `1241513984` 460 dregs dst 6 insn 31 3 461 ret 0 462 end 463 if eq m `184549376` 464 dregs dst 1 insn 31 3 465 ret 0 466 end 467 if eq m `1258291200` 468 dregs dst 2 insn 31 3 469 ret 0 470 end 471 ret 1 472 end 473 474 note the checks 10-15 (MOVK .. SUBS); 0 matched, 1 not 475 fn dec2 insn ulong dst edit ulong 9 to ulong 476 if eq and insn `2139095040` `1920991232` 477 dwide dst 13 insn 478 ret 0 479 end 480 if eq and insn `2667577344` `268435456` 481 dregs dst 14 insn 32 1 482 ret 0 483 end 484 m = and insn `3208642560` 485 if eq m `3107979264` 486 dregs dst 15 insn 30 2 487 ret 0 488 end 489 if eq m `3103784960` 490 dregs dst 16 insn 30 2 491 ret 0 492 end 493 m = and insn `3219129344` 494 if eq m `3091203072` 495 dregs dst 15 insn 30 2 496 ret 0 497 end 498 if eq m `3087008768` 499 dregs dst 16 insn 30 2 500 ret 0 501 end 502 m = and insn `4290772992` 503 if eq m `960495616` 504 dregs dst 17 insn 32 2 505 ret 0 506 end 507 if eq m `956301312` 508 dregs dst 18 insn 32 2 509 ret 0 510 end 511 if eq and insn `2139095040` `1895825408` 512 dregs dst 19 insn 31 2 513 ret 0 514 end 515 if eq and insn `2132803584` `1795162112` 516 dregs dst 19 insn 31 3 517 ret 0 518 end 519 ret 1 520 end 521 522 note aarch64_decode_instruction: dst = DecodedInsn (op sf rd rn rm imm16 hw branch_imm cond), all 523 note zero first; 0, or -1 with op 0 (DECODED_INVALID). branch_imm is the raw field, not sign-extended. 524 export fn a64decode insn ulong dst edit ulong 9 to long 525 nu32 insn 526 for i ulong 9 527 put dst i 0 528 end 529 if eq dec1 insn dst 0 530 ret 0 531 end 532 if eq dec2 insn dst 0 533 ret 0 534 end 535 put dst 0 0 536 ret -1 537 end 538 539 fn nu32 v ulong to void 540 if gt v `4294967295` 541 trap 5 542 end 543 end 544 545 note ---- validator: aarch64_validate_code_buffer and its snprintf messages ---- 546 547 note append the c low bytes of v (little-endian) to t at n; the new length 548 fn app t edit uchar 128 n ulong v ulong c ulong to ulong 549 for i ulong c 550 put t add n i uchar and shr v mul i 8 255 551 end 552 ret add n c 553 end 554 555 fn hexch v ulong to ulong 556 if lt v 10 557 ret add v 48 558 end 559 ret add v 87 560 end 561 562 note %x of v with at least w digits (w >= 1): %08x is w 8, %zx is w 1 563 fn apphex t edit uchar 128 n ulong v ulong w ulong to ulong 564 k = ulong 1 565 while lt k 16 bound 16 566 if eq shr v mul k 4 0 567 break 568 end 569 k = add k 1 570 end 571 if lt k w 572 k = w 573 end 574 for i ulong k 575 put t add n i uchar hexch and shr v mul sub sub k 1 i 4 15 576 end 577 ret add n k 578 end 579 580 note %zu / %d (nonnegative) of v 581 fn appdec t edit uchar 128 n ulong v ulong to ulong 582 k = ulong 1 583 q = div v 10 584 while gt q 0 bound 20 585 k = add k 1 586 q = div q 10 587 end 588 q = v 589 for i ulong k 590 put t add n sub sub k 1 i uchar add 48 rem q 10 591 q = div q 10 592 end 593 ret add n k 594 end 595 596 note snprintf(msg, ml, ...) of the n bytes in t: at most ml - 1 bytes and a NUL, nothing when ml is 0 597 fn snp msg edit uchar * ml ulong t view uchar 128 n ulong to void 598 if eq ml 0 599 ret 600 end 601 m = n 602 if gt m sub ml 1 603 m = sub ml 1 604 end 605 for i ulong m 606 put msg i get t i 607 end 608 put msg m 0 609 end 610 611 note "Empty or null code buffer" 612 fn msgnull t edit uchar 128 to ulong 613 n = app t 0 `8245845148755324229` 8 614 n = app t n `8026294610952547872` 8 615 n = app t n `7306640099798705508` 8 616 ret app t n 114 1 617 end 618 619 note "Code buffer length %zu is not a multiple of 4 bytes" 620 fn msglen t edit uchar 128 v ulong to ulong 621 n = app t 0 `7382915055423352643` 8 622 n = app t n `7453005948419990886` 8 623 n = app t n `2123892` 3 624 n = appdec t n v 625 n = app t n `2338616625293650208` 8 626 n = app t n `8100133413977923681` 8 627 n = app t n `2320515332325795180` 8 628 ret app t n `495623371106` 5 629 end 630 631 note "Undefined/unrecognized AArch64 opcode 0x%08x at offset 0x%zx" 632 fn msgop t edit uchar 128 w ulong o ulong to ulong 633 n = app t 0 `7308895133777555029` 8 634 n = app t n `8026370503058206564` 8 635 n = app t n `4692861198728261223` 8 636 n = app t n `8007457545748968001` 8 637 n = app t n `8660457703282271088` 8 638 n = apphex t n w 8 639 n = app t n `8315446389458100512` 8 640 n = app t n `516203508837` 5 641 ret apphex t n o 1 642 end 643 644 note "Function does not terminate with RET (terminates with op %d)" 645 fn msgret t edit uchar 128 op ulong to ulong 646 n = app t 0 `7957695015192261958` 8 647 n = app t n `8029390865643299872` 8 648 n = app t n `7956010554822500468` 8 649 n = app t n `7526756837608485985` 8 650 n = app t n `7310512214447051296` 8 651 n = app t n `8315180248822541682` 8 652 n = app t n `8101729887664568096` 8 653 n = app t n 32 1 654 n = appdec t n op 655 ret app t n 41 1 656 end 657 658 fn ld32 a view uchar * o ulong to ulong 659 x = ulong get a add o 3 660 x = or shl x 8 ulong get a add o 2 661 x = or shl x 8 ulong get a add o 1 662 ret or shl x 8 ulong get a o 663 end 664 665 note the instruction loop of the validator over c words: 0 when valid, else the message length in t 666 fn valscan code view uchar * c ulong t edit uchar 128 d edit ulong 9 to ulong 667 for i ulong c 668 o = mul i 4 669 w = ld32 code o 670 if or ne a64decode w d 0 eq get d 0 0 671 ret msgop t w o 672 end 673 if and eq i sub c 1 ne get d 0 8 674 ret msgret t get d 0 675 end 676 end 677 ret 0 678 end 679 680 note aarch64_validate_code_buffer(code, n, msg, ml): 0, or -1 with the C's message in msg. 681 note ml above 256 (more capacity than msg has) traps 3 before anything is read or written. 682 export fn a64validate code view uchar * n ulong msg edit uchar * ml ulong to long 683 if gt ml len msg 684 trap 3 685 end 686 t = new uchar 128 0 687 d = new ulong 9 0 688 if eq n 0 689 k = msgnull t 690 snp msg ml t k 691 ret -1 692 end 693 if ne rem n 4 0 694 k = msglen t n 695 snp msg ml t k 696 ret -1 697 end 698 k = valscan code div n 4 t d 699 if gt k 0 700 snp msg ml t k 701 ret -1 702 end 703 ret 0 704 end 705