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 / check / a64check.om
| 1 | om2 |
| 2 | note a64check.om -- REFERENCE CHECK ONLY for the seed chain (seed/README.md). It is never used to make a |
| 3 | note byte of seed/. Seeded-Omega port of seed/check/a64check.c together with the two C files that program |
| 4 | note links, src/aarch64_decoder.c (aarch64_decode_instruction) and src/aarch64_encoder.c (the legacy |
| 5 | note emitters), so the hand-written words can be compared without a C compiler. |
| 6 | note om/aarch64/aarch64.om is an existing API port of the same routines. This CLI currently duplicates |
| 7 | note those routines; consolidation needs separate parity of its CLI conversions and refusal output. |
| 8 | note Signal identity parity is scoped to default signal dispositions. With SIGABRT inherited ignored, |
| 9 | note killpid does not reset that disposition: this port falls back to exit 134, unlike C abort(). |
| 10 | note a64check decode stdin: one hex word per line; stdout: the decoder's fields per word |
| 11 | note a64check encode <form> <args...> one word from the legacy encoder for that form, as 8 hex digits |
| 12 | note Parity contract with the C program (stdout, stderr and exit status byte for byte): |
| 13 | note - decode reads stdin the way fgets with a 128-byte buffer does (chunks of at most 127 bytes, each |
| 14 | note ending at a newline or at the end of input) and reads each chunk the way strtoul base 16 does |
| 15 | note (leading C-locale space, a sign, an optional 0x, hex digits; overflow gives all ones), then keeps |
| 16 | note the low 32 bits. An unknown word prints op=INVALID with every field 0. |
| 17 | note - encode reads each argument the way strtol base 0 does (sign, 0x hex, leading-0 octal, decimal, |
| 18 | note saturating at the long limits) and then converts it the way the C call does to the emitter's |
| 19 | note parameter type: bool, uint8_t, uint16_t, int16_t or int32_t (Aarch64Cond is read as int32_t). |
| 20 | note - an out-of-range field prints "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_FORM FIELD=VALUE" on |
| 21 | note stderr, like the C refuse(), and the program stops itself with SIGABRT (status 134 in sh), like abort(). |
| 22 | note - wrong form or argument count: "a64check: unknown form or argument count: FORM" on stderr, exit 2; |
| 23 | note anything else: the usage line on stderr, exit 2. |
| 24 | note Not ported: aarch64_validate_code_buffer (a64check.c never calls it) and the "REFUSED" exit 1 path, |
| 25 | note which the C program cannot reach (every emitter gets a 4-byte buffer at position 0). |
| 26 | text tminl "9223372036854775808" |
| 27 | text opn "INVALID ADD SUB MUL AND ORR EOR MOVZ RET B B_COND CBZ CBNZ MOVK ADR LDR STR LDRB STRB SUBS " |
| 28 | text forms "add_reg sub_reg mul_reg and_reg orr_reg eor_reg subs_reg subs_imm mov_reg movz movk ret b b_cond cbz cbnz adr ldr_uoff str_uoff ldrb_uoff strb_uoff ldr_post str_post ldr_x_post str_x_post " |
| 29 | text usage "usage: a64check decode | a64check encode <form> <args...>\n" |
| 30 | text unk "a64check: unknown form or argument count: " |
| 31 | text oor "A64_LEGACY_FIELD_OUT_OF_RANGE aarch64_emit_" |
| 32 | text tdecode "decode" |
| 33 | text tencode "encode" |
| 34 | text hexd "0123456789abcdef" |
| 35 | text tsf " sf=" |
| 36 | text trdq " rd=" |
| 37 | text trnq " rn=" |
| 38 | text trmq " rm=" |
| 39 | text timmq " imm16=" |
| 40 | text thwq " hw=" |
| 41 | text tbimq " bimm=" |
| 42 | text tcndq " cond=" |
| 43 | text topq "op=" |
| 44 | text trd "rd" |
| 45 | text trn "rn" |
| 46 | text trm "rm" |
| 47 | text trt "rt" |
| 48 | text tshift "shift" |
| 49 | text timm26 "imm26" |
| 50 | text tcond "cond" |
| 51 | text timm19 "imm19" |
| 52 | text timm21 "imm21" |
| 53 | text tuoff "uoff" |
| 54 | text tsimm9 "simm9" |
| 55 | text timm12 "imm12" |
| 56 | global ib uchar 65536 |
| 57 | global ist ulong 3 |
| 58 | global ob uchar 65536 |
| 59 | global oc ulong 1 |
| 60 | global lb uchar 128 |
| 61 | global ab uchar 131072 |
| 62 | global fm uchar 131072 |
| 63 | global fmn ulong 1 |
| 64 | global df ulong 9 |
| 65 | global av ulong 8 |
| 66 | |
| 67 | note ---- output: one buffer, flushed to a descriptor when full and at the end ---- |
| 68 | note oflush: every pass writes at least one byte of at most len ob, so len ob passes suffice. |
| 69 | fn oflush fd int to void |
| 70 | off = ulong 0 |
| 71 | n = get oc 0 |
| 72 | while lt off n bound 65536 |
| 73 | w = write fd ob off sub n off |
| 74 | if le w 0 |
| 75 | put oc 0 0 |
| 76 | ret |
| 77 | end |
| 78 | off = add off ulong w |
| 79 | end |
| 80 | put oc 0 0 |
| 81 | end |
| 82 | |
| 83 | fn oputb b uchar to void |
| 84 | m = get oc 0 |
| 85 | if ge m len ob |
| 86 | oflush 1 |
| 87 | m = get oc 0 |
| 88 | end |
| 89 | put ob m b |
| 90 | put oc 0 add m 1 |
| 91 | end |
| 92 | |
| 93 | fn oputt t view uchar * to void |
| 94 | for i ulong len t |
| 95 | oputb get t i |
| 96 | end |
| 97 | end |
| 98 | |
| 99 | fn oputn v ulong to void |
| 100 | d = new uchar 20 0 |
| 101 | i = ulong 0 |
| 102 | x = v |
| 103 | put d 0 uchar add rem x 10 48 |
| 104 | x = div x 10 |
| 105 | i = add i 1 |
| 106 | while gt x 0 bound 20 |
| 107 | put d i uchar add rem x 10 48 |
| 108 | x = div x 10 |
| 109 | i = add i 1 |
| 110 | end |
| 111 | j = i |
| 112 | while gt j 0 bound 20 |
| 113 | j = sub j 1 |
| 114 | oputb get d j |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | note oputs: a signed value in decimal, as printf %lld does. |
| 119 | fn oputs v long to void |
| 120 | if lt v 0 |
| 121 | oputb 45 |
| 122 | if eq v -9223372036854775808 |
| 123 | oputt tminl |
| 124 | ret |
| 125 | end |
| 126 | oputn ulong neg v |
| 127 | ret |
| 128 | end |
| 129 | oputn ulong v |
| 130 | end |
| 131 | |
| 132 | |
| 133 | fn ohex8 x ulong to void |
| 134 | for i ulong 8 |
| 135 | s = sub 28 mul i 4 |
| 136 | oputb get hexd and shr x s 15 |
| 137 | end |
| 138 | end |
| 139 | |
| 140 | note oname: word k of opn, the C opname() table. |
| 141 | fn oname k ulong to void |
| 142 | w = ulong 0 |
| 143 | for i ulong len opn |
| 144 | c = get opn i |
| 145 | if eq c 32 |
| 146 | w = add w 1 |
| 147 | else |
| 148 | if eq w k |
| 149 | oputb c |
| 150 | end |
| 151 | end |
| 152 | end |
| 153 | end |
| 154 | |
| 155 | fn oputfm to void |
| 156 | for i ulong get fmn 0 |
| 157 | oputb get fm i |
| 158 | end |
| 159 | end |
| 160 | |
| 161 | note ---- input: stdin in 64 KiB blocks ---- |
| 162 | fn getc to long |
| 163 | p = get ist 0 |
| 164 | n = get ist 1 |
| 165 | if ge p n |
| 166 | if ne get ist 2 0 |
| 167 | ret -1 |
| 168 | end |
| 169 | r = read 0 ib 0 len ib |
| 170 | if le r 0 |
| 171 | put ist 2 1 |
| 172 | ret -1 |
| 173 | end |
| 174 | put ist 0 0 |
| 175 | put ist 1 ulong r |
| 176 | p = get ist 0 |
| 177 | end |
| 178 | c = get ib p |
| 179 | put ist 0 add p 1 |
| 180 | ret long c |
| 181 | end |
| 182 | |
| 183 | note rdline: one fgets chunk into lb: at most 127 bytes, ending after a newline; 0 only at end of input. |
| 184 | fn rdline to ulong |
| 185 | k = ulong 0 |
| 186 | while lt k 127 bound 128 |
| 187 | c = getc |
| 188 | if lt c 0 |
| 189 | ret k |
| 190 | end |
| 191 | put lb k uchar c |
| 192 | k = add k 1 |
| 193 | if eq c 10 |
| 194 | ret k |
| 195 | end |
| 196 | end |
| 197 | ret k |
| 198 | end |
| 199 | |
| 200 | note lc: byte i of the current chunk, or 0 (the C string end) past it. |
| 201 | fn lc i ulong k ulong to ulong |
| 202 | if lt i k |
| 203 | ret ulong get lb i |
| 204 | end |
| 205 | ret 0 |
| 206 | end |
| 207 | |
| 208 | note ac: byte i of the current argument (length k in ab), or 0 past it. |
| 209 | fn ac i ulong k ulong to ulong |
| 210 | if lt i k |
| 211 | ret ulong get ab i |
| 212 | end |
| 213 | ret 0 |
| 214 | end |
| 215 | |
| 216 | fn isspc c ulong to bool |
| 217 | if eq c 32 |
| 218 | ret true |
| 219 | end |
| 220 | ret and ge c 9 le c 13 |
| 221 | end |
| 222 | |
| 223 | note dval: the value of a digit or letter in bases up to 36, 99 for anything else. |
| 224 | fn dval c ulong to ulong |
| 225 | if and ge c 48 le c 57 |
| 226 | ret sub c 48 |
| 227 | end |
| 228 | if and ge c 97 le c 122 |
| 229 | ret sub c 87 |
| 230 | end |
| 231 | if and ge c 65 le c 90 |
| 232 | ret sub c 55 |
| 233 | end |
| 234 | ret 99 |
| 235 | end |
| 236 | |
| 237 | note pul: strtoul(chunk, NULL, 16) over lb[0..k). |
| 238 | fn pul k ulong to ulong |
| 239 | i = ulong 0 |
| 240 | while isspc lc i k bound 128 |
| 241 | i = add i 1 |
| 242 | end |
| 243 | ng = false |
| 244 | c = lc i k |
| 245 | if eq c 45 |
| 246 | ng = true |
| 247 | i = add i 1 |
| 248 | else |
| 249 | if eq c 43 |
| 250 | i = add i 1 |
| 251 | end |
| 252 | end |
| 253 | if eq lc i k 48 |
| 254 | if eq or lc add i 1 k 32 120 |
| 255 | if lt dval lc add i 2 k 16 |
| 256 | i = add i 2 |
| 257 | end |
| 258 | end |
| 259 | end |
| 260 | v = ulong 0 |
| 261 | ov = false |
| 262 | d = dval lc i k |
| 263 | while lt d 16 bound 130 |
| 264 | if gt v div sub 0xffffffffffffffff d 16 |
| 265 | ov = true |
| 266 | end |
| 267 | v = add mul v 16 d |
| 268 | i = add i 1 |
| 269 | d = dval lc i k |
| 270 | end |
| 271 | if ov |
| 272 | ret 0xffffffffffffffff |
| 273 | end |
| 274 | if ng |
| 275 | ret neg v |
| 276 | end |
| 277 | ret v |
| 278 | end |
| 279 | |
| 280 | note sl: strtol(argv[i], NULL, 0) as the bits of a long. |
| 281 | fn sl i ulong to ulong |
| 282 | n = argread i ab 0 len ab |
| 283 | k = ulong 0 |
| 284 | if gt n 0 |
| 285 | k = ulong n |
| 286 | end |
| 287 | j = ulong 0 |
| 288 | while isspc ac j k bound 131080 |
| 289 | j = add j 1 |
| 290 | end |
| 291 | ng = false |
| 292 | c = ac j k |
| 293 | if eq c 45 |
| 294 | ng = true |
| 295 | j = add j 1 |
| 296 | else |
| 297 | if eq c 43 |
| 298 | j = add j 1 |
| 299 | end |
| 300 | end |
| 301 | b = ulong 10 |
| 302 | if eq ac j k 48 |
| 303 | b = 8 |
| 304 | if eq or ac add j 1 k 32 120 |
| 305 | b = 16 |
| 306 | j = add j 2 |
| 307 | end |
| 308 | end |
| 309 | v = ulong 0 |
| 310 | ov = false |
| 311 | d = dval ac j k |
| 312 | while lt d b bound 131080 |
| 313 | if gt v div sub 0xffffffffffffffff d b |
| 314 | ov = true |
| 315 | end |
| 316 | v = add mul v b d |
| 317 | j = add j 1 |
| 318 | d = dval ac j k |
| 319 | end |
| 320 | if ov |
| 321 | if ng |
| 322 | ret 0x8000000000000000 |
| 323 | end |
| 324 | ret 0x7fffffffffffffff |
| 325 | end |
| 326 | if ng |
| 327 | if gt v 0x8000000000000000 |
| 328 | ret 0x8000000000000000 |
| 329 | end |
| 330 | ret neg v |
| 331 | end |
| 332 | if gt v 0x7fffffffffffffff |
| 333 | ret 0x7fffffffffffffff |
| 334 | end |
| 335 | ret v |
| 336 | end |
| 337 | |
| 338 | note ---- the decoder (src/aarch64_decoder.c, same order of tests) ---- |
| 339 | fn f3 op ulong w ulong to void |
| 340 | put df 0 op |
| 341 | put df 1 and shr w 31 1 |
| 342 | put df 2 and w 31 |
| 343 | put df 3 and shr w 5 31 |
| 344 | put df 4 and shr w 16 31 |
| 345 | end |
| 346 | |
| 347 | fn fcb op ulong w ulong to void |
| 348 | put df 0 op |
| 349 | put df 1 and shr w 31 1 |
| 350 | put df 2 and w 31 |
| 351 | put df 7 and shr w 5 0x7ffff |
| 352 | end |
| 353 | |
| 354 | fn fmv op ulong w ulong to void |
| 355 | put df 0 op |
| 356 | put df 1 and shr w 31 1 |
| 357 | put df 2 and w 31 |
| 358 | put df 5 and shr w 5 0xffff |
| 359 | put df 6 and shr w 21 3 |
| 360 | end |
| 361 | |
| 362 | fn fls op ulong w ulong to void |
| 363 | put df 0 op |
| 364 | put df 1 and shr w 30 1 |
| 365 | put df 2 and w 31 |
| 366 | put df 3 and shr w 5 31 |
| 367 | end |
| 368 | |
| 369 | fn dec w ulong to void |
| 370 | for i ulong 9 |
| 371 | put df i 0 |
| 372 | end |
| 373 | if eq w 0xd65f03c0 |
| 374 | put df 0 8 |
| 375 | put df 3 30 |
| 376 | ret |
| 377 | end |
| 378 | if eq and w 0xfc000000 0x14000000 |
| 379 | put df 0 9 |
| 380 | put df 7 and w 0x3ffffff |
| 381 | ret |
| 382 | end |
| 383 | if eq and w 0xff000010 0x54000000 |
| 384 | put df 0 10 |
| 385 | put df 8 and w 15 |
| 386 | put df 7 and shr w 5 0x7ffff |
| 387 | ret |
| 388 | end |
| 389 | if eq and w 0x7f000000 0x34000000 |
| 390 | fcb 11 w |
| 391 | ret |
| 392 | end |
| 393 | if eq and w 0x7f000000 0x35000000 |
| 394 | fcb 12 w |
| 395 | ret |
| 396 | end |
| 397 | if eq and w 0x7f800000 0x52800000 |
| 398 | fmv 7 w |
| 399 | ret |
| 400 | end |
| 401 | if eq and w 0x7fe07c00 0x1b007c00 |
| 402 | f3 3 w |
| 403 | ret |
| 404 | end |
| 405 | dec2 w |
| 406 | end |
| 407 | |
| 408 | fn dec2 w ulong to void |
| 409 | m = and w 0x7f200000 |
| 410 | if eq m 0xa000000 |
| 411 | f3 4 w |
| 412 | ret |
| 413 | end |
| 414 | if eq m 0x2a000000 |
| 415 | f3 5 w |
| 416 | ret |
| 417 | end |
| 418 | if eq m 0x4a000000 |
| 419 | f3 6 w |
| 420 | ret |
| 421 | end |
| 422 | if eq m 0xb000000 |
| 423 | f3 1 w |
| 424 | ret |
| 425 | end |
| 426 | if eq m 0x4b000000 |
| 427 | f3 2 w |
| 428 | ret |
| 429 | end |
| 430 | if eq and w 0x7f800000 0x72800000 |
| 431 | fmv 13 w |
| 432 | ret |
| 433 | end |
| 434 | if eq and w 0x9f000000 0x10000000 |
| 435 | put df 0 14 |
| 436 | put df 2 and w 31 |
| 437 | ret |
| 438 | end |
| 439 | dec3 w |
| 440 | end |
| 441 | |
| 442 | fn dec3 w ulong to void |
| 443 | if eq and w 0xbf400000 0xb9400000 |
| 444 | fls 15 w |
| 445 | ret |
| 446 | end |
| 447 | if eq and w 0xbf400000 0xb9000000 |
| 448 | fls 16 w |
| 449 | ret |
| 450 | end |
| 451 | if eq and w 0xbfe00400 0xb8400400 |
| 452 | fls 15 w |
| 453 | ret |
| 454 | end |
| 455 | if eq and w 0xbfe00400 0xb8000400 |
| 456 | fls 16 w |
| 457 | ret |
| 458 | end |
| 459 | if eq and w 0xffc00000 0x39400000 |
| 460 | fls 17 w |
| 461 | put df 1 0 |
| 462 | ret |
| 463 | end |
| 464 | if eq and w 0xffc00000 0x39000000 |
| 465 | fls 18 w |
| 466 | put df 1 0 |
| 467 | ret |
| 468 | end |
| 469 | if eq and w 0x7f800000 0x71000000 |
| 470 | f3 19 w |
| 471 | put df 4 0 |
| 472 | ret |
| 473 | end |
| 474 | if eq and w 0x7f200000 0x6b000000 |
| 475 | f3 19 w |
| 476 | ret |
| 477 | end |
| 478 | end |
| 479 | |
| 480 | fn decline to void |
| 481 | oputt topq |
| 482 | oname get df 0 |
| 483 | oputt tsf |
| 484 | oputn get df 1 |
| 485 | oputt trdq |
| 486 | oputn get df 2 |
| 487 | oputt trnq |
| 488 | oputn get df 3 |
| 489 | oputt trmq |
| 490 | oputn get df 4 |
| 491 | oputt timmq |
| 492 | oputn get df 5 |
| 493 | oputt thwq |
| 494 | oputn get df 6 |
| 495 | oputt tbimq |
| 496 | oputn get df 7 |
| 497 | oputt tcndq |
| 498 | oputn get df 8 |
| 499 | oputb 10 |
| 500 | end |
| 501 | |
| 502 | fn decode to int |
| 503 | while true bound 9223372036854775807 |
| 504 | k = rdline |
| 505 | if eq k 0 |
| 506 | oflush 1 |
| 507 | ret 0 |
| 508 | end |
| 509 | dec and pul k 0xffffffff |
| 510 | decline |
| 511 | end |
| 512 | oflush 1 |
| 513 | ret 0 |
| 514 | end |
| 515 | |
| 516 | note ---- the legacy encoder (src/aarch64_encoder.c) ---- |
| 517 | note refuse: the C refuse(): one stderr line, then SIGABRT like abort(). |
| 518 | fn refuse f view uchar * v long to void |
| 519 | put oc 0 0 |
| 520 | oputt oor |
| 521 | oputfm |
| 522 | oputb 32 |
| 523 | oputt f |
| 524 | oputb 61 |
| 525 | oputs v |
| 526 | oputb 10 |
| 527 | oflush 2 |
| 528 | r = killpid int getpid 6 |
| 529 | exit 134 |
| 530 | end |
| 531 | |
| 532 | fn creg r ulong f view uchar * to void |
| 533 | if gt r 31 |
| 534 | refuse f long r |
| 535 | end |
| 536 | end |
| 537 | |
| 538 | note csig: CHECK_SIGNED(v, bits) on the value the C parameter holds. |
| 539 | fn csig v long bits ulong f view uchar * to void |
| 540 | h = long shl ulong 1 sub bits 1 |
| 541 | if or lt v neg h ge v h |
| 542 | refuse f v |
| 543 | end |
| 544 | end |
| 545 | |
| 546 | fn cumax v ulong f view uchar * to void |
| 547 | if gt v 0xfff |
| 548 | refuse f long v |
| 549 | end |
| 550 | end |
| 551 | |
| 552 | fn cv8 x ulong to ulong |
| 553 | ret and x 255 |
| 554 | end |
| 555 | |
| 556 | fn cv16 x ulong to ulong |
| 557 | ret and x 0xffff |
| 558 | end |
| 559 | |
| 560 | fn s16 x ulong to long |
| 561 | v = and x 0xffff |
| 562 | if ge v 0x8000 |
| 563 | ret sub long v 65536 |
| 564 | end |
| 565 | ret long v |
| 566 | end |
| 567 | |
| 568 | fn s32 x ulong to long |
| 569 | v = and x 0xffffffff |
| 570 | if ge v 0x80000000 |
| 571 | ret sub long v 4294967296 |
| 572 | end |
| 573 | ret long v |
| 574 | end |
| 575 | |
| 576 | fn bsf x ulong to ulong |
| 577 | if ne x 0 |
| 578 | ret 1 |
| 579 | end |
| 580 | ret 0 |
| 581 | end |
| 582 | |
| 583 | note formid: 1-based index of the form name in forms, 0 when it is not one. |
| 584 | fn formid to ulong |
| 585 | id = ulong 1 |
| 586 | st = ulong 0 |
| 587 | n = get fmn 0 |
| 588 | for i ulong len forms |
| 589 | if eq get forms i 32 |
| 590 | if eq sub i st n |
| 591 | if fmeq st |
| 592 | ret id |
| 593 | end |
| 594 | end |
| 595 | id = add id 1 |
| 596 | st = add i 1 |
| 597 | end |
| 598 | end |
| 599 | ret 0 |
| 600 | end |
| 601 | |
| 602 | fn fmeq st ulong to bool |
| 603 | for i ulong get fmn 0 |
| 604 | if ne get fm i get forms add st i |
| 605 | ret false |
| 606 | end |
| 607 | end |
| 608 | ret true |
| 609 | end |
| 610 | |
| 611 | note needc: the argc the C chain requires for form id. |
| 612 | fn needc id ulong to long |
| 613 | if le id 8 |
| 614 | ret 7 |
| 615 | end |
| 616 | if le id 9 |
| 617 | ret 6 |
| 618 | end |
| 619 | if le id 11 |
| 620 | ret 7 |
| 621 | end |
| 622 | if eq id 12 |
| 623 | ret 3 |
| 624 | end |
| 625 | if eq id 13 |
| 626 | ret 4 |
| 627 | end |
| 628 | if eq id 14 |
| 629 | ret 5 |
| 630 | end |
| 631 | if le id 16 |
| 632 | ret 6 |
| 633 | end |
| 634 | if eq id 17 |
| 635 | ret 5 |
| 636 | end |
| 637 | if le id 19 |
| 638 | ret 7 |
| 639 | end |
| 640 | ret 6 |
| 641 | end |
| 642 | |
| 643 | fn a3base id ulong to ulong |
| 644 | if eq id 1 |
| 645 | ret 0xb000000 |
| 646 | end |
| 647 | if eq id 2 |
| 648 | ret 0x4b000000 |
| 649 | end |
| 650 | if eq id 3 |
| 651 | ret 0x1b007c00 |
| 652 | end |
| 653 | if eq id 4 |
| 654 | ret 0xa000000 |
| 655 | end |
| 656 | if eq id 5 |
| 657 | ret 0x2a000000 |
| 658 | end |
| 659 | if eq id 6 |
| 660 | ret 0x4a000000 |
| 661 | end |
| 662 | ret 0x6b000000 |
| 663 | end |
| 664 | |
| 665 | note enc3: add_reg .. subs_reg (ids 1..7): sf rd rn rm, CHECK3. |
| 666 | fn enc3 id ulong to ulong |
| 667 | sf = bsf get av 3 |
| 668 | rd = cv8 get av 4 |
| 669 | rn = cv8 get av 5 |
| 670 | rm = cv8 get av 6 |
| 671 | creg rd trd |
| 672 | creg rn trn |
| 673 | creg rm trm |
| 674 | ret or or or or shl sf 31 a3base id shl rm 16 shl rn 5 rd |
| 675 | end |
| 676 | |
| 677 | fn encsi to ulong |
| 678 | sf = bsf get av 3 |
| 679 | rd = cv8 get av 4 |
| 680 | rn = cv8 get av 5 |
| 681 | im = cv16 get av 6 |
| 682 | creg rd trd |
| 683 | creg rn trn |
| 684 | cumax im timm12 |
| 685 | ret or or or or shl sf 31 0x71000000 shl im 10 shl rn 5 rd |
| 686 | end |
| 687 | |
| 688 | fn encmov to ulong |
| 689 | sf = bsf get av 3 |
| 690 | rd = cv8 get av 4 |
| 691 | rm = cv8 get av 5 |
| 692 | creg rd trd |
| 693 | creg rm trm |
| 694 | ret or or or or shl sf 31 0x2a000000 shl rm 16 0x3e0 rd |
| 695 | end |
| 696 | |
| 697 | fn encmz base ulong to ulong |
| 698 | sf = bsf get av 3 |
| 699 | rd = cv8 get av 4 |
| 700 | im = cv16 get av 5 |
| 701 | s = cv8 get av 6 |
| 702 | creg rd trd |
| 703 | mx = ulong 16 |
| 704 | if ne sf 0 |
| 705 | mx = 48 |
| 706 | end |
| 707 | if or ne rem s 16 0 gt s mx |
| 708 | refuse tshift long s |
| 709 | end |
| 710 | hw = and div s 16 3 |
| 711 | ret or or or or shl sf 31 base shl hw 21 shl im 5 rd |
| 712 | end |
| 713 | |
| 714 | fn encb to ulong |
| 715 | x = get av 3 |
| 716 | csig s32 x 26 timm26 |
| 717 | ret or 0x14000000 and x 0x3ffffff |
| 718 | end |
| 719 | |
| 720 | fn encbc to ulong |
| 721 | c = s32 get av 3 |
| 722 | if or lt c 0 gt c 15 |
| 723 | refuse tcond c |
| 724 | end |
| 725 | x = get av 4 |
| 726 | csig s32 x 19 timm19 |
| 727 | ret or or 0x54000000 shl and x 0x7ffff 5 and get av 3 15 |
| 728 | end |
| 729 | |
| 730 | fn enccb base ulong to ulong |
| 731 | sf = bsf get av 3 |
| 732 | rt = cv8 get av 4 |
| 733 | x = get av 5 |
| 734 | creg rt trt |
| 735 | csig s32 x 19 timm19 |
| 736 | ret or or or shl sf 31 base shl and x 0x7ffff 5 rt |
| 737 | end |
| 738 | |
| 739 | fn encadr to ulong |
| 740 | rd = cv8 get av 3 |
| 741 | x = get av 4 |
| 742 | creg rd trd |
| 743 | csig s32 x 21 timm21 |
| 744 | lo = shl and x 3 29 |
| 745 | hi = shl and shr and x 0xffffffff 2 0x7ffff 5 |
| 746 | ret or or or 0x10000000 lo hi rd |
| 747 | end |
| 748 | |
| 749 | fn encuo ld bool to ulong |
| 750 | sf = bsf get av 3 |
| 751 | rt = cv8 get av 4 |
| 752 | rn = cv8 get av 5 |
| 753 | uo = cv16 get av 6 |
| 754 | creg rt trt |
| 755 | creg rn trn |
| 756 | sz = ulong 4 |
| 757 | sc = ulong 2 |
| 758 | if ne sf 0 |
| 759 | sz = 8 |
| 760 | sc = 3 |
| 761 | end |
| 762 | if or ne rem uo sz 0 gt div uo sz 0xfff |
| 763 | refuse tuoff long uo |
| 764 | end |
| 765 | base = ulong 0xb9000000 |
| 766 | if ne sf 0 |
| 767 | base = 0xf9000000 |
| 768 | end |
| 769 | if ld |
| 770 | base = or base 0x400000 |
| 771 | end |
| 772 | ret or or or base shl and shr uo sc 0xfff 10 shl rn 5 rt |
| 773 | end |
| 774 | |
| 775 | fn encbo base ulong to ulong |
| 776 | rt = cv8 get av 3 |
| 777 | rn = cv8 get av 4 |
| 778 | uo = cv16 get av 5 |
| 779 | creg rt trt |
| 780 | creg rn trn |
| 781 | cumax uo tuoff |
| 782 | ret or or or base shl and uo 0xfff 10 shl rn 5 rt |
| 783 | end |
| 784 | |
| 785 | fn encpo base ulong to ulong |
| 786 | rt = cv8 get av 3 |
| 787 | rn = cv8 get av 4 |
| 788 | x = get av 5 |
| 789 | creg rt trt |
| 790 | creg rn trn |
| 791 | csig s16 x 9 tsimm9 |
| 792 | ret or or or base shl and x 0x1ff 12 shl rn 5 rt |
| 793 | end |
| 794 | |
| 795 | fn encid id ulong to ulong |
| 796 | if le id 7 |
| 797 | ret enc3 id |
| 798 | end |
| 799 | if eq id 8 |
| 800 | ret encsi |
| 801 | end |
| 802 | if eq id 9 |
| 803 | ret encmov |
| 804 | end |
| 805 | if eq id 10 |
| 806 | ret encmz 0x52800000 |
| 807 | end |
| 808 | if eq id 11 |
| 809 | ret encmz 0x72800000 |
| 810 | end |
| 811 | if eq id 12 |
| 812 | ret 0xd65f03c0 |
| 813 | end |
| 814 | if eq id 13 |
| 815 | ret encb |
| 816 | end |
| 817 | if eq id 14 |
| 818 | ret encbc |
| 819 | end |
| 820 | if eq id 15 |
| 821 | ret enccb 0x34000000 |
| 822 | end |
| 823 | if eq id 16 |
| 824 | ret enccb 0x35000000 |
| 825 | end |
| 826 | if eq id 17 |
| 827 | ret encadr |
| 828 | end |
| 829 | ret encid2 id |
| 830 | end |
| 831 | |
| 832 | fn encid2 id ulong to ulong |
| 833 | if eq id 18 |
| 834 | ret encuo true |
| 835 | end |
| 836 | if eq id 19 |
| 837 | ret encuo false |
| 838 | end |
| 839 | if eq id 20 |
| 840 | ret encbo 0x39400000 |
| 841 | end |
| 842 | if eq id 21 |
| 843 | ret encbo 0x39000000 |
| 844 | end |
| 845 | if eq id 22 |
| 846 | ret encpo 0xb8400400 |
| 847 | end |
| 848 | if eq id 23 |
| 849 | ret encpo 0xb8000400 |
| 850 | end |
| 851 | if eq id 24 |
| 852 | ret encpo 0xf8400400 |
| 853 | end |
| 854 | ret encpo 0xf8000400 |
| 855 | end |
| 856 | |
| 857 | fn encode to int |
| 858 | n = argread 2 fm 0 len fm |
| 859 | if lt n 0 |
| 860 | n = 0 |
| 861 | end |
| 862 | put fmn 0 ulong n |
| 863 | id = formid |
| 864 | c = argcnt |
| 865 | if eq id 0 |
| 866 | oputt unk |
| 867 | oputfm |
| 868 | oputb 10 |
| 869 | oflush 2 |
| 870 | ret 2 |
| 871 | end |
| 872 | if ne c needc id |
| 873 | oputt unk |
| 874 | oputfm |
| 875 | oputb 10 |
| 876 | oflush 2 |
| 877 | ret 2 |
| 878 | end |
| 879 | for i ulong 7 |
| 880 | if ge i 3 |
| 881 | if lt long i c |
| 882 | put av i sl i |
| 883 | end |
| 884 | end |
| 885 | end |
| 886 | w = encid id |
| 887 | ohex8 w |
| 888 | oputb 10 |
| 889 | oflush 1 |
| 890 | ret 0 |
| 891 | end |
| 892 | |
| 893 | note argis: argument 1 equals text t. |
| 894 | fn argis t view uchar * to bool |
| 895 | n = argread 1 ab 0 len ab |
| 896 | if ne n long len t |
| 897 | ret false |
| 898 | end |
| 899 | for i ulong len t |
| 900 | if ne get ab i get t i |
| 901 | ret false |
| 902 | end |
| 903 | end |
| 904 | ret true |
| 905 | end |
| 906 | |
| 907 | fn main to int |
| 908 | c = argcnt |
| 909 | if eq c 2 |
| 910 | if argis tdecode |
| 911 | ret decode |
| 912 | end |
| 913 | end |
| 914 | if ge c 3 |
| 915 | if argis tencode |
| 916 | ret encode |
| 917 | end |
| 918 | end |
| 919 | oputt usage |
| 920 | oflush 2 |
| 921 | ret 2 |
| 922 | end |