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/compiler/osc_a64_fields.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 / compiler / osc_a64_fields.om` 524 lines ยท 13428 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Encode/decode the variable fields of OSC's supported 64-bit AArch64 forms. 3 note The opcode table supplies base bits and the class. Register kind, canonical 4 note operands and range checks precede packing. Negative offsets use low field bits. 5 note bridge c-type a64field OscA64Fields 6 note bridge c-define OSC_A64_ERR_UNUSED_SHIFT 1 7 note bridge c-define OSC_A64_ERR_UNUSED_COND 2 8 note bridge c-define OSC_A64_ERR_UNUSED_IMM2 3 9 note bridge c-define OSC_A64_ERR_UNUSED_IMM 4 10 note bridge c-define OSC_A64_ERR_SHIFT_KIND 5 11 note bridge c-define OSC_A64_ERR_SHIFT_AMOUNT 6 12 note bridge c-define OSC_A64_ERR_IMM12 7 13 note bridge c-define OSC_A64_ERR_LSL12 8 14 note bridge c-define OSC_A64_ERR_IMMR 9 15 note bridge c-define OSC_A64_ERR_IMMS 10 16 note bridge c-define OSC_A64_ERR_COND 11 17 note bridge c-define OSC_A64_ERR_IMM16 12 18 note bridge c-define OSC_A64_ERR_MOV_SHIFT 13 19 note bridge c-define OSC_A64_ERR_MEM_OFFSET 14 20 note bridge c-define OSC_A64_ERR_PAIR_OFFSET 15 21 note bridge c-define OSC_A64_ERR_WRITEBACK_ALIAS 16 22 note bridge c-define OSC_A64_ERR_LOAD_ALIAS 17 23 note bridge c-define OSC_A64_ERR_BRANCH_OFFSET 18 24 note bridge c-define OSC_A64_ERR_BRANCH_COND 19 25 note bridge c-define OSC_A64_ERR_UNUSED_REG 32 26 note bridge c-define OSC_A64_ERR_REG_SP 36 27 note bridge c-define OSC_A64_ERR_REG_ZR 40 28 note bridge c-define OSC_A64_ERR_BAD_REG 44 29 om2 30 layout a64spec 24 31 field base uint at 8 little 32 field form uchar at 12 little 33 field role uchar 4 at 13 little 34 field flags uchar at 17 little 35 end 36 layout a64insn 24 37 field opcode uchar at 0 little 38 field reg uchar 4 at 1 little 39 field shift uchar at 5 little 40 field cond uchar at 6 little 41 field imm long at 8 little 42 field imm2 long at 16 little 43 end 44 layout a64field 40 45 field rd uint at 0 little 46 field rn uint at 4 little 47 field rm uint at 8 little 48 field ra uint at 12 little 49 field shift uint at 16 little 50 field cond uint at 20 little 51 field imm long at 24 little 52 field imm2 long at 32 little 53 end 54 55 const CLS_NONE ulong 0 56 const CLS_RRR_SH ulong 1 57 const CLS_RI ulong 2 58 const CLS_R4 ulong 3 59 const CLS_MULH ulong 4 60 const CLS_R3 ulong 5 61 const CLS_BFM ulong 6 62 const CLS_CSINC ulong 7 63 const CLS_MOV16 ulong 8 64 const CLS_MEM ulong 9 65 const CLS_PAIR ulong 10 66 const CLS_B26 ulong 11 67 const CLS_BCOND ulong 12 68 const CLS_CB ulong 13 69 const CLS_BREG ulong 14 70 const CLS_BRK ulong 15 71 72 export fn osc_a64_pack_fields form ulong base ulong f view a64field to ulong 73 rd = ulong get f rd 74 rn = shl ulong get f rn 5 75 rm = shl ulong get f rm 16 76 ra = shl ulong get f ra 10 77 imm = bitcast ulong get f imm 78 second = bitcast ulong get f imm2 79 if eq form CLS_RRR_SH 80 regs = or rd or rn rm 81 returnbits = or shl ulong get f shift 22 shl imm 10 82 ret or base or regs returnbits 83 end 84 if eq form CLS_RI 85 ret or base or or rd rn or shl second 22 shl imm 10 86 end 87 if eq form CLS_R4 88 ret or base or or rd rn or rm ra 89 end 90 if or eq form CLS_MULH eq form CLS_R3 91 ret or base or rd or rn rm 92 end 93 if eq form CLS_BFM 94 ret or base or or rd rn or shl imm 16 shl second 10 95 end 96 if eq form CLS_CSINC 97 regs = or rd or rn rm 98 ret or base or regs shl ulong get f cond 12 99 end 100 if eq form CLS_MOV16 101 ret or base or rd or shl shr second 4 21 shl imm 5 102 end 103 if eq form CLS_MEM 104 ret or base or or rd rn shl shr imm 3 10 105 end 106 if eq form CLS_PAIR 107 offset = shl and shr imm 3 0x7f 15 108 ret or base or or rd rn or ra offset 109 end 110 if eq form CLS_B26 111 ret or base and shr imm 2 0x03ffffff 112 end 113 if or eq form CLS_BCOND eq form CLS_CB 114 offset = shl and shr imm 2 0x7ffff 5 115 if eq form CLS_BCOND 116 ret or base or offset ulong get f cond 117 end 118 ret or base or offset rd 119 end 120 if eq form CLS_BREG 121 ret or base rn 122 end 123 if eq form CLS_BRK 124 ret or base shl imm 5 125 end 126 trap 5 127 end 128 129 fn a64_scaled_signed fieldbits ulong signbit ulong scale ulong to long 130 ret bitcast long shl sub xor fieldbits signbit signbit scale 131 end 132 133 export fn osc_a64_unpack_fields form ulong word ulong f edit a64field to void 134 put f rd uint and word 31 135 put f rn uint and shr word 5 31 136 put f rm uint and shr word 16 31 137 put f ra uint and shr word 10 31 138 put f shift 0 139 put f cond 0 140 put f imm 0 141 put f imm2 0 142 if eq form CLS_RRR_SH 143 put f shift uint and shr word 22 3 144 put f imm long and shr word 10 63 145 end 146 if eq form CLS_RI 147 put f imm long and shr word 10 0xfff 148 put f imm2 long and shr word 22 1 149 end 150 if eq form CLS_BFM 151 put f imm long and shr word 16 63 152 put f imm2 long and shr word 10 63 153 end 154 if eq form CLS_CSINC 155 put f cond uint and shr word 12 15 156 end 157 if eq form CLS_MOV16 158 put f imm long and shr word 5 0xffff 159 put f imm2 long shl and shr word 21 3 4 160 end 161 if eq form CLS_MEM 162 put f imm long shl and shr word 10 0xfff 3 163 end 164 if eq form CLS_PAIR 165 put f imm a64_scaled_signed and shr word 15 0x7f 0x40 3 166 end 167 if eq form CLS_B26 168 put f imm a64_scaled_signed and word 0x03ffffff 0x02000000 2 169 end 170 if or eq form CLS_BCOND eq form CLS_CB 171 put f imm a64_scaled_signed and shr word 5 0x7ffff 0x40000 2 172 if eq form CLS_BCOND 173 put f cond uint and word 15 174 end 175 end 176 if eq form CLS_BRK 177 put f imm long and shr word 5 0xffff 178 end 179 end 180 const ERR_UNUSED_SHIFT ulong 1 181 const ERR_UNUSED_COND ulong 2 182 const ERR_UNUSED_IMM2 ulong 3 183 const ERR_UNUSED_IMM ulong 4 184 const ERR_SHIFT_KIND ulong 5 185 const ERR_SHIFT_AMOUNT ulong 6 186 const ERR_IMM12 ulong 7 187 const ERR_LSL12 ulong 8 188 const ERR_IMMR ulong 9 189 const ERR_IMMS ulong 10 190 const ERR_COND ulong 11 191 const ERR_IMM16 ulong 12 192 const ERR_MOV_SHIFT ulong 13 193 const ERR_MEM_OFFSET ulong 14 194 const ERR_PAIR_OFFSET ulong 15 195 const ERR_WRITEBACK_ALIAS ulong 16 196 const ERR_LOAD_ALIAS ulong 17 197 const ERR_BRANCH_OFFSET ulong 18 198 const ERR_BRANCH_COND ulong 19 199 200 note Pair flags come from the opcode table. Encoded base register 31 is SP 201 note for the pair class, so it cannot alias a transfer even when that is XZR. 202 export fn osc_a64_check_fields cls ulong f view a64field writeback ulong loadpair ulong to ulong 203 imm = get f imm 204 second = get f imm2 205 if and ne cls CLS_RRR_SH ne get f shift 0 206 ret ERR_UNUSED_SHIFT 207 end 208 if and ne get f cond 0 and ne cls CLS_CSINC ne cls CLS_BCOND 209 ret ERR_UNUSED_COND 210 end 211 if ne second 0 212 if and ne cls CLS_RI and ne cls CLS_BFM ne cls CLS_MOV16 213 ret ERR_UNUSED_IMM2 214 end 215 end 216 if ne imm 0 217 if or eq cls CLS_R4 or eq cls CLS_MULH eq cls CLS_R3 218 ret ERR_UNUSED_IMM 219 end 220 if or eq cls CLS_CSINC eq cls CLS_BREG 221 ret ERR_UNUSED_IMM 222 end 223 end 224 if eq cls CLS_RRR_SH 225 if gt get f shift 2 226 ret ERR_SHIFT_KIND 227 end 228 if or lt imm 0 gt imm 63 229 ret ERR_SHIFT_AMOUNT 230 end 231 end 232 if eq cls CLS_RI 233 if or lt imm 0 gt imm 4095 234 ret ERR_IMM12 235 end 236 if and ne second 0 ne second 1 237 ret ERR_LSL12 238 end 239 end 240 if eq cls CLS_BFM 241 if or lt imm 0 gt imm 63 242 ret ERR_IMMR 243 end 244 if or lt second 0 gt second 63 245 ret ERR_IMMS 246 end 247 end 248 if and eq cls CLS_CSINC gt get f cond 14 249 ret ERR_COND 250 end 251 if or eq cls CLS_MOV16 eq cls CLS_BRK 252 if or lt imm 0 gt imm 65535 253 ret ERR_IMM16 254 end 255 if eq cls CLS_MOV16 256 if or lt second 0 or gt second 48 ne and second 15 0 257 ret ERR_MOV_SHIFT 258 end 259 end 260 end 261 if eq cls CLS_MEM 262 if or lt imm 0 or gt imm 32760 ne and imm 7 0 263 ret ERR_MEM_OFFSET 264 end 265 end 266 if eq cls CLS_PAIR 267 if or lt imm -512 or gt imm 504 ne and imm 7 0 268 ret ERR_PAIR_OFFSET 269 end 270 if and ne writeback 0 ne get f rn 31 271 base = get f rn 272 if or eq base get f rd eq base get f ra 273 ret ERR_WRITEBACK_ALIAS 274 end 275 end 276 if and ne loadpair 0 eq get f rd get f ra 277 ret ERR_LOAD_ALIAS 278 end 279 end 280 if eq cls CLS_B26 281 if or lt imm -`134217728` or gt imm `134217724` ne and imm 3 0 282 ret ERR_BRANCH_OFFSET 283 end 284 end 285 if or eq cls CLS_BCOND eq cls CLS_CB 286 if and eq cls CLS_BCOND gt get f cond 13 287 ret ERR_BRANCH_COND 288 end 289 if or lt imm -`1048576` or gt imm `1048572` ne and imm 3 0 290 ret ERR_BRANCH_OFFSET 291 end 292 end 293 ret 0 294 end 295 296 note Checked encoder result: the low word is an instruction on success. 297 note The high word carries the existing diagnostic code on refusal. 298 export fn osc_a64_checked_word form ulong base ulong f view a64field flags ulong to ulong 299 status = osc_a64_check_fields form f and flags 1 shr flags 1 300 if ne status 0 301 ret shl status 32 302 end 303 ret osc_a64_pack_fields form base f 304 end 305 306 note The opcode matcher has already checked the fixed bits. Unpack, check 307 note and re-encode each candidate inside the same Omega invocation, so a 308 note successful decode always proves the original word is canonical. 309 export fn osc_a64_canonical_word form ulong base ulong word ulong flags ulong fields edit a64field to ulong 310 osc_a64_unpack_fields form word fields 311 if and eq form CLS_BREG eq get fields rn 31 312 ret 0 313 end 314 if ne osc_a64_checked_word form base fields flags word 315 ret 0 316 end 317 ret 1 318 end 319 320 note Plan the move-wide instructions for a 64-bit constant in lane order. 321 note Each record holds the opcode in the high word, shift and immediate in 322 note the low word. A tied MOVZ/MOVN cost selects MOVZ for stable code bytes. 323 export fn osc_a64_materialize value ulong plan edit ulong 4 to ulong 324 zeros = ulong 0 325 ones = ulong 0 326 for lane ulong 4 327 chunk = and shr value mul lane 16 65535 328 if eq chunk 0 329 zeros = add zeros 1 330 end 331 if eq chunk 65535 332 ones = add ones 1 333 end 334 end 335 fill = ulong 0 336 initial = ulong 25 337 if gt ones zeros 338 fill = 65535 339 initial = 26 340 end 341 count = ulong 0 342 for lane ulong 4 343 shift = mul lane 16 344 chunk = and shr value shift 65535 345 if ne chunk fill 346 opcode = ulong 27 347 if eq count 0 348 opcode = initial 349 chunk = xor chunk fill 350 end 351 payload = or chunk shl shift 16 352 put plan count or shl opcode 32 payload 353 count = add count 1 354 end 355 end 356 if eq count 0 357 put plan 0 shl initial 32 358 ret 1 359 end 360 ret count 361 end 362 363 note The descriptor table is indexed in public opcode order. Class masks 364 note select the variable bits excluded from the fixed opcode comparison. 365 fn a64_mask form ulong to ulong 366 if eq form CLS_RRR_SH 367 ret 0x00dfffff 368 end 369 if eq form CLS_RI 370 ret 0x007fffff 371 end 372 if eq form CLS_R4 373 ret 0x001f7fff 374 end 375 if eq form CLS_MULH 376 ret 0x001f03ff 377 end 378 if eq form CLS_R3 379 ret 0x001f03ff 380 end 381 if eq form CLS_BFM 382 ret 0x003fffff 383 end 384 if eq form CLS_CSINC 385 ret 0x001ff3ff 386 end 387 if eq form CLS_MOV16 388 ret 0x007fffff 389 end 390 if eq form CLS_MEM 391 ret 0x003fffff 392 end 393 if eq form CLS_PAIR 394 ret 0x003fffff 395 end 396 if eq form CLS_B26 397 ret 0x03ffffff 398 end 399 if eq form CLS_BCOND 400 ret 0x00ffffef 401 end 402 if eq form CLS_CB 403 ret 0x00ffffff 404 end 405 if eq form CLS_BREG 406 ret 0x000003e0 407 end 408 if eq form CLS_BRK 409 ret 0x001fffe0 410 end 411 ret 0 412 end 413 414 fn a64_encode_registers spec view a64spec insn view a64insn fields edit a64field to ulong 415 regs = new uint 4 0 416 for i ulong 4 417 if and eq get spec role i 0 ne get insn reg i 0 418 ret shl add 32 i 32 419 end 420 end 421 for i ulong 4 422 role = get spec role i 423 regno = get insn reg i 424 if le regno 30 425 put regs i uint regno 426 else 427 if eq regno 33 428 if ne role 2 429 ret shl add 36 i 32 430 end 431 else 432 if eq regno 32 433 if ne role 1 434 ret shl add 40 i 32 435 end 436 else 437 ret shl add 44 i 32 438 end 439 end 440 put regs i 31 441 end 442 end 443 put fields rd get regs 0 444 put fields rn get regs 1 445 put fields rm get regs 2 446 put fields ra get regs 3 447 put fields shift uint get insn shift 448 put fields cond uint get insn cond 449 put fields imm get insn imm 450 put fields imm2 get insn imm2 451 ret osc_a64_checked_word ulong get spec form ulong get spec base fields ulong get spec flags 452 end 453 454 export fn osc_a64_write spec view uchar 24 insn view uchar 24 to ulong 455 fields = new uchar 40 0 456 ret a64_encode_registers spec insn fields 457 end 458 459 fn a64_decode_register role uchar bits uint to uchar 460 if eq role 0 461 ret 0 462 end 463 if lt bits 31 464 ret uchar bits 465 end 466 if eq role 1 467 ret 32 468 end 469 if eq role 2 470 ret 33 471 end 472 ret 255 473 end 474 475 fn a64_try_opcode word ulong op ulong spec view a64spec insn edit a64insn fields edit a64field to ulong 476 form = ulong get spec form 477 base = ulong get spec base 478 if eq osc_a64_canonical_word form base word ulong get spec flags fields 0 479 ret 0 480 end 481 put insn opcode uchar op 482 put insn reg 0 a64_decode_register get spec role 0 get fields rd 483 put insn reg 1 a64_decode_register get spec role 1 get fields rn 484 put insn reg 2 a64_decode_register get spec role 2 get fields rm 485 put insn reg 3 a64_decode_register get spec role 3 get fields ra 486 put insn shift uchar get fields shift 487 put insn cond uchar get fields cond 488 put insn imm get fields imm 489 put insn imm2 get fields imm2 490 ret 1 491 end 492 493 fn a64_matches specs view uchar * pos ulong word ulong to bool 494 form = ulong get specs add pos 12 495 base = ulong get specs add pos 8 496 base = or base shl ulong get specs add pos 9 8 497 base = or base shl ulong get specs add pos 10 16 498 base = or base shl ulong get specs add pos 11 24 499 ret and ne form 0 eq and word xor a64_mask form 0xffffffff base 500 end 501 502 fn a64_copy_spec specs view uchar * pos ulong spec edit uchar 24 to void 503 for i ulong 10 504 put spec add 8 i get specs add add pos 8 i 505 end 506 end 507 508 export fn osc_a64_read word ulong specs view uchar * insn edit uchar 24 to ulong 509 fields = new uchar 40 0 510 spec = new uchar 24 0 511 n = div len specs 24 512 op = ulong 1 513 while lt op n bound 255 514 pos = mul op 24 515 if a64_matches specs pos word 516 a64_copy_spec specs pos spec 517 if ne a64_try_opcode word op spec insn fields 0 518 ret 1 519 end 520 end 521 op = add op 1 522 end 523 ret 0 524 end