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 / target / oir_x64.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/target/oir_x64.om: Turing-Complete ΩIR binary (seed/ng/src/ir.om + opt.om) -> Register-Allocated x86-64 ELF64 compiler! |
| 3 | note Supports straight-line ALU/bitwise/conv + full CFG blocks, parallel-copy block-param phi moves (br), |
| 4 | note conditional branches (cbr), signed/unsigned comparisons (eq..ge), loops with back-edge live-range extension, and 2-pass rel32 patching. |
| 5 | |
| 6 | global oxValIdx ulong 256 |
| 7 | global oxValCnt ulong 1 |
| 8 | |
| 9 | note Block offset table (indexed by block handle mod 256) + jump fixup list |
| 10 | global oxBlkId ulong 128 |
| 11 | global oxBlkOff ulong 128 |
| 12 | global oxBlkStartPos ulong 128 |
| 13 | global oxBlkCnt ulong 1 |
| 14 | global oxFixPos ulong 256 |
| 15 | global oxFixBlk ulong 256 |
| 16 | global oxFixCnt ulong 1 |
| 17 | |
| 18 | fn oxFindOrAddVal vid ulong to ulong |
| 19 | n = get oxValCnt 0 |
| 20 | for i ulong n bound 256 |
| 21 | if eq get oxValIdx i vid |
| 22 | ret i |
| 23 | end |
| 24 | end |
| 25 | put oxValIdx n vid |
| 26 | put oxValCnt 0 add n 1 |
| 27 | ret n |
| 28 | end |
| 29 | |
| 30 | fn oxFindBlkIdx bid ulong to ulong |
| 31 | n = get oxBlkCnt 0 |
| 32 | for i ulong n bound 128 |
| 33 | if eq get oxBlkId i bid |
| 34 | ret i |
| 35 | end |
| 36 | end |
| 37 | ret 999999 |
| 38 | end |
| 39 | |
| 40 | note Physical x86-64 register map for allocator slots 0..14 (avoids RSP=4): |
| 41 | note 0=RAX(0), 1=RDI(7), 2=RSI(6), 3=RDX(2), 4=RCX(1), 5=RBX(3), 6=RBP(5), 7..14=R8..R15(8..15) |
| 42 | fn oxX64Reg slot ulong to ulong |
| 43 | if eq slot 0 |
| 44 | ret 0 |
| 45 | end |
| 46 | if eq slot 1 |
| 47 | ret 7 |
| 48 | end |
| 49 | if eq slot 2 |
| 50 | ret 6 |
| 51 | end |
| 52 | if eq slot 3 |
| 53 | ret 2 |
| 54 | end |
| 55 | if eq slot 4 |
| 56 | ret 1 |
| 57 | end |
| 58 | if eq slot 5 |
| 59 | ret 3 |
| 60 | end |
| 61 | if eq slot 6 |
| 62 | ret 5 |
| 63 | end |
| 64 | ret add slot 1 |
| 65 | end |
| 66 | |
| 67 | fn oxPhysOfVal vid ulong to ulong |
| 68 | n = get oxValCnt 0 |
| 69 | for i ulong n bound 256 |
| 70 | if eq get oxValIdx i vid |
| 71 | ret oxX64Reg get raPhys i |
| 72 | end |
| 73 | end |
| 74 | ret 0 |
| 75 | end |
| 76 | |
| 77 | note Emit x86-64 movabs r64, imm64 |
| 78 | fn oxEmitImm64 pr ulong imm ulong to void |
| 79 | rex = ulong 72 |
| 80 | if ge pr 8 |
| 81 | rex = or rex 1 |
| 82 | end |
| 83 | isEmitU8 rex |
| 84 | isEmitU8 add 184 and pr 7 |
| 85 | isEmitU64 imm |
| 86 | end |
| 87 | |
| 88 | fn oxEmitMov prDst ulong prSrc ulong to void |
| 89 | if eq prDst prSrc |
| 90 | ret |
| 91 | end |
| 92 | isEmitU8 isX64Rex prSrc prDst |
| 93 | isEmitU8 137 |
| 94 | isEmitU8 isX64ModRm prSrc prDst |
| 95 | end |
| 96 | |
| 97 | fn oxEmitPush pr ulong to void |
| 98 | if ge pr 8 |
| 99 | isEmitU8 65 |
| 100 | end |
| 101 | isEmitU8 add 80 and pr 7 |
| 102 | end |
| 103 | |
| 104 | fn oxEmitPop pr ulong to void |
| 105 | if ge pr 8 |
| 106 | isEmitU8 65 |
| 107 | end |
| 108 | isEmitU8 add 88 and pr 7 |
| 109 | end |
| 110 | |
| 111 | fn oxEmitAlu opc ulong pd ulong pn ulong pm ulong to void |
| 112 | if eq pd pm |
| 113 | if ne pd pn |
| 114 | oxEmitPush pm |
| 115 | oxEmitMov pd pn |
| 116 | oxEmitPop 15 |
| 117 | isEmitU8 isX64Rex 15 pd |
| 118 | isEmitU8 opc |
| 119 | isEmitU8 isX64ModRm 15 pd |
| 120 | ret |
| 121 | end |
| 122 | end |
| 123 | oxEmitMov pd pn |
| 124 | isEmitU8 isX64Rex pm pd |
| 125 | isEmitU8 opc |
| 126 | isEmitU8 isX64ModRm pm pd |
| 127 | end |
| 128 | |
| 129 | fn oxEmitMul pd ulong pn ulong pm ulong to void |
| 130 | if eq pd pm |
| 131 | if ne pd pn |
| 132 | oxEmitPush pm |
| 133 | oxEmitMov pd pn |
| 134 | oxEmitPop 15 |
| 135 | isEmitU8 isX64Rex pd 15 |
| 136 | isEmitU8 15 |
| 137 | isEmitU8 175 |
| 138 | isEmitU8 isX64ModRm pd 15 |
| 139 | ret |
| 140 | end |
| 141 | end |
| 142 | oxEmitMov pd pn |
| 143 | isEmitU8 isX64Rex pd pm |
| 144 | isEmitU8 15 |
| 145 | isEmitU8 175 |
| 146 | isEmitU8 isX64ModRm pd pm |
| 147 | end |
| 148 | |
| 149 | fn oxEmitNeg pd ulong pn ulong to void |
| 150 | oxEmitMov pd pn |
| 151 | rex = ulong 72 |
| 152 | if ge pd 8 |
| 153 | rex = or rex 1 |
| 154 | end |
| 155 | isEmitU8 rex |
| 156 | isEmitU8 247 |
| 157 | isEmitU8 or 216 and pd 7 |
| 158 | end |
| 159 | |
| 160 | note Emit signed/unsigned comparison: cmp pn, pm; setcc pd_b; movzx pd, pd_b |
| 161 | fn oxCmpCc op ulong isSigned bool to ulong |
| 162 | if eq op 30 |
| 163 | ret 148 |
| 164 | end |
| 165 | if eq op 31 |
| 166 | ret 149 |
| 167 | end |
| 168 | if isSigned |
| 169 | if eq op 32 |
| 170 | ret 156 |
| 171 | end |
| 172 | if eq op 33 |
| 173 | ret 158 |
| 174 | end |
| 175 | if eq op 34 |
| 176 | ret 159 |
| 177 | end |
| 178 | ret 157 |
| 179 | end |
| 180 | if eq op 32 |
| 181 | ret 146 |
| 182 | end |
| 183 | if eq op 33 |
| 184 | ret 150 |
| 185 | end |
| 186 | if eq op 34 |
| 187 | ret 151 |
| 188 | end |
| 189 | ret 147 |
| 190 | end |
| 191 | |
| 192 | fn oxEmitCmp op ulong pd ulong pn ulong pm ulong isSigned bool to void |
| 193 | note cmp pn, pm (REX.W 0x39 /r: reg=pm, r/m=pn) |
| 194 | isEmitU8 isX64Rex pm pn |
| 195 | isEmitU8 57 |
| 196 | isEmitU8 isX64ModRm pm pn |
| 197 | note setcc pd_low8 (REX 0x40|B 0x0F cc 0xC0|(pd&7)) |
| 198 | rexB = ulong 64 |
| 199 | if ge pd 8 |
| 200 | rexB = ulong 65 |
| 201 | end |
| 202 | isEmitU8 rexB |
| 203 | isEmitU8 15 |
| 204 | isEmitU8 oxCmpCc op isSigned |
| 205 | isEmitU8 or 192 and pd 7 |
| 206 | note movzx pd, pd_low8 (REX.W 0x0F 0xB6 /r: reg=pd, r/m=pd) |
| 207 | isEmitU8 isX64Rex pd pd |
| 208 | isEmitU8 15 |
| 209 | isEmitU8 182 |
| 210 | isEmitU8 isX64ModRm pd pd |
| 211 | end |
| 212 | |
| 213 | fn oxEmitJmpRel32 targetBid ulong to void |
| 214 | isEmitU8 233 |
| 215 | f = get oxFixCnt 0 |
| 216 | put oxFixPos f get isLen 0 |
| 217 | put oxFixBlk f targetBid |
| 218 | put oxFixCnt 0 add f 1 |
| 219 | isEmitU32 0 |
| 220 | end |
| 221 | |
| 222 | fn oxEmitJnzRel32 condPr ulong targetBid ulong to void |
| 223 | note test condPr, condPr (REX.W 0x85 /r) |
| 224 | isEmitU8 isX64Rex condPr condPr |
| 225 | isEmitU8 133 |
| 226 | isEmitU8 isX64ModRm condPr condPr |
| 227 | note jnz rel32 (0x0F 0x85 rel32) |
| 228 | isEmitU8 15 |
| 229 | isEmitU8 133 |
| 230 | f = get oxFixCnt 0 |
| 231 | put oxFixPos f get isLen 0 |
| 232 | put oxFixBlk f targetBid |
| 233 | put oxFixCnt 0 add f 1 |
| 234 | isEmitU32 0 |
| 235 | end |
| 236 | |
| 237 | fn oxEmitExit prVal ulong to void |
| 238 | oxEmitMov 7 prVal |
| 239 | isEmitU8 184 |
| 240 | isEmitU32 60 |
| 241 | isEmitU8 15 |
| 242 | isEmitU8 5 |
| 243 | end |
| 244 | |
| 245 | fn oxTouchUse av ulong pos ulong to void |
| 246 | if eq av 0 |
| 247 | ret |
| 248 | end |
| 249 | vi = oxFindOrAddVal av |
| 250 | if lt vi get raNVal 0 |
| 251 | if lt pos get raStart vi |
| 252 | put raStart vi pos |
| 253 | end |
| 254 | if gt pos get raEnd vi |
| 255 | put raEnd vi pos |
| 256 | end |
| 257 | end |
| 258 | end |
| 259 | |
| 260 | fn oxExtendBackEdge targetBid ulong backPos ulong to void |
| 261 | bi = oxFindBlkIdx targetBid |
| 262 | if eq bi 999999 |
| 263 | ret |
| 264 | end |
| 265 | hdrPos = get oxBlkStartPos bi |
| 266 | if ge hdrPos backPos |
| 267 | ret |
| 268 | end |
| 269 | nv = get raNVal 0 |
| 270 | for i ulong nv bound 256 |
| 271 | s = get raStart i |
| 272 | e = get raEnd i |
| 273 | if and le s backPos ge e hdrPos |
| 274 | if lt hdrPos s |
| 275 | put raStart i hdrPos |
| 276 | end |
| 277 | if gt backPos e |
| 278 | put raEnd i backPos |
| 279 | end |
| 280 | end |
| 281 | end |
| 282 | end |
| 283 | |
| 284 | fn oxScanDefs f uint to void |
| 285 | b = get i_fb1 f |
| 286 | pos = ulong 0 |
| 287 | while ne b 0 bound 128 |
| 288 | bc = get oxBlkCnt 0 |
| 289 | put oxBlkId bc ulong b |
| 290 | put oxBlkStartPos bc pos |
| 291 | put oxBlkCnt 0 add bc 1 |
| 292 | p = get i_bp1 b |
| 293 | while ne p 0 bound 32 |
| 294 | if ne get i_vty p 20 |
| 295 | idx = oxFindOrAddVal ulong p |
| 296 | if ge idx get raNVal 0 |
| 297 | id = raAddInterval pos pos RA_SPILL |
| 298 | end |
| 299 | end |
| 300 | p = get i_vnx p |
| 301 | end |
| 302 | o = get i_bo1 b |
| 303 | while ne o 0 bound 256 |
| 304 | res = ulong get i_ors o |
| 305 | if ne res 0 |
| 306 | if ne get i_vty res 20 |
| 307 | idx = oxFindOrAddVal res |
| 308 | if ge idx get raNVal 0 |
| 309 | id = raAddInterval pos pos RA_SPILL |
| 310 | end |
| 311 | end |
| 312 | end |
| 313 | pos = add pos 1 |
| 314 | o = get i_onx o |
| 315 | end |
| 316 | b = get i_bnx b |
| 317 | end |
| 318 | end |
| 319 | |
| 320 | fn oxScanOpUses o uint pos ulong to void |
| 321 | op = ulong get i_oop o |
| 322 | na = ulong get i_ona o |
| 323 | a0 = ulong get i_oa0 o |
| 324 | if eq op 60 |
| 325 | if gt na 2 |
| 326 | for k ulong sub na 2 bound 16 |
| 327 | oxTouchUse ulong get i_arg add a0 add k 2 pos |
| 328 | end |
| 329 | end |
| 330 | oxExtendBackEdge ulong get i_arg a0 pos |
| 331 | ret |
| 332 | end |
| 333 | if eq op 61 |
| 334 | oxTouchUse ulong get i_arg add a0 1 pos |
| 335 | oxExtendBackEdge ulong get i_arg add a0 2 pos |
| 336 | oxExtendBackEdge ulong get i_arg add a0 3 pos |
| 337 | ret |
| 338 | end |
| 339 | for k ulong na bound 16 |
| 340 | oxTouchUse ulong get i_arg add a0 k pos |
| 341 | end |
| 342 | end |
| 343 | |
| 344 | fn oxSortIntervals to void |
| 345 | n = get raNVal 0 |
| 346 | for i ulong n bound 256 |
| 347 | if gt i 0 |
| 348 | j = i |
| 349 | while gt j 0 bound 256 |
| 350 | jm = sub j 1 |
| 351 | if le get raStart jm get raStart j |
| 352 | break |
| 353 | end |
| 354 | ts = get raStart jm |
| 355 | te = get raEnd jm |
| 356 | tv = get oxValIdx jm |
| 357 | put raStart jm get raStart j |
| 358 | put raEnd jm get raEnd j |
| 359 | put oxValIdx jm get oxValIdx j |
| 360 | put raStart j ts |
| 361 | put raEnd j te |
| 362 | put oxValIdx j tv |
| 363 | j = jm |
| 364 | end |
| 365 | end |
| 366 | end |
| 367 | end |
| 368 | |
| 369 | note 2-pass live-interval builder (handles rotated loops) + sort by start + shared regalloc.om (14 GPRs)! |
| 370 | fn oxAllocFunc f uint to bool |
| 371 | put oxValCnt 0 0 |
| 372 | put oxBlkCnt 0 0 |
| 373 | raReset |
| 374 | oxScanDefs f |
| 375 | b = get i_fb1 f |
| 376 | pos = ulong 0 |
| 377 | while ne b 0 bound 128 |
| 378 | o = get i_bo1 b |
| 379 | while ne o 0 bound 256 |
| 380 | oxScanOpUses o pos |
| 381 | pos = add pos 1 |
| 382 | o = get i_onx o |
| 383 | end |
| 384 | b = get i_bnx b |
| 385 | end |
| 386 | oxSortIntervals |
| 387 | tdSet TID_X64_AVX2 TDF_NGPR 14 |
| 388 | sp = raRunForTarget TID_X64_AVX2 |
| 389 | tdSet TID_X64_AVX2 TDF_NGPR 15 |
| 390 | if ne raVerifyColoring 0 |
| 391 | ret false |
| 392 | end |
| 393 | ret eq sp 0 |
| 394 | end |
| 395 | |
| 396 | note Emit parallel-copy block-param moves for `br target(tok, v1, v2, ...)` via push/pop (cycle-safe!) |
| 397 | fn oxEmitBrPhi o uint to void |
| 398 | dstP = new ulong 16 0 |
| 399 | a0 = ulong get i_oa0 o |
| 400 | na = ulong get i_ona o |
| 401 | tb = get i_arg a0 |
| 402 | nArg = ulong 0 |
| 403 | if gt na 2 |
| 404 | nArg = sub na 2 |
| 405 | end |
| 406 | for k ulong nArg bound 16 |
| 407 | srcV = ulong get i_arg add a0 add k 2 |
| 408 | oxEmitPush oxPhysOfVal srcV |
| 409 | end |
| 410 | p = get i_bp1 tb |
| 411 | di = ulong 0 |
| 412 | while ne p 0 bound 32 |
| 413 | if ne get i_vty p 20 |
| 414 | put dstP di ulong p |
| 415 | di = add di 1 |
| 416 | end |
| 417 | p = get i_vnx p |
| 418 | end |
| 419 | for k ulong di bound 16 |
| 420 | ri = sub sub di 1 k |
| 421 | oxEmitPop oxPhysOfVal get dstP ri |
| 422 | end |
| 423 | oxEmitJmpRel32 ulong tb |
| 424 | end |
| 425 | |
| 426 | fn oxLowerOneOp o uint to bool |
| 427 | op = ulong get i_oop o |
| 428 | res = ulong get i_ors o |
| 429 | a0 = ulong get i_oa0 o |
| 430 | if eq op 1 |
| 431 | oxEmitImm64 oxPhysOfVal res get i_oim o |
| 432 | ret true |
| 433 | end |
| 434 | if eq op 10 |
| 435 | oxEmitAlu 1 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 436 | ret true |
| 437 | end |
| 438 | if eq op 11 |
| 439 | oxEmitAlu 41 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 440 | ret true |
| 441 | end |
| 442 | if eq op 12 |
| 443 | oxEmitMul oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 444 | ret true |
| 445 | end |
| 446 | if eq op 15 |
| 447 | oxEmitNeg oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 |
| 448 | ret true |
| 449 | end |
| 450 | if eq op 16 |
| 451 | oxEmitAlu 33 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 452 | ret true |
| 453 | end |
| 454 | if eq op 17 |
| 455 | oxEmitAlu 9 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 456 | ret true |
| 457 | end |
| 458 | if eq op 18 |
| 459 | oxEmitAlu 49 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1 |
| 460 | ret true |
| 461 | end |
| 462 | if and ge op 30 le op 35 |
| 463 | av0 = ulong get i_arg a0 |
| 464 | vt = ulong get i_vty av0 |
| 465 | isS = and ge vt 6 le vt 9 |
| 466 | oxEmitCmp op oxPhysOfVal res oxPhysOfVal av0 oxPhysOfVal ulong get i_arg add a0 1 isS |
| 467 | ret true |
| 468 | end |
| 469 | if eq op 36 |
| 470 | oxEmitMov oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 |
| 471 | ret true |
| 472 | end |
| 473 | if eq op 60 |
| 474 | oxEmitBrPhi o |
| 475 | ret true |
| 476 | end |
| 477 | if eq op 61 |
| 478 | oxEmitJnzRel32 oxPhysOfVal ulong get i_arg add a0 1 ulong get i_arg add a0 2 |
| 479 | oxEmitJmpRel32 ulong get i_arg add a0 3 |
| 480 | ret true |
| 481 | end |
| 482 | if or eq op 63 eq op 66 |
| 483 | oxEmitExit oxPhysOfVal ulong get i_arg add a0 1 |
| 484 | ret true |
| 485 | end |
| 486 | if eq op 62 |
| 487 | oxEmitExit 0 |
| 488 | ret true |
| 489 | end |
| 490 | if and ge op 70 le op 93 |
| 491 | ret true |
| 492 | end |
| 493 | ret false |
| 494 | end |
| 495 | |
| 496 | fn oxPatchFixups to bool |
| 497 | fc = get oxFixCnt 0 |
| 498 | for i ulong fc bound 256 |
| 499 | fp = get oxFixPos i |
| 500 | tb = get oxFixBlk i |
| 501 | bi = oxFindBlkIdx tb |
| 502 | if eq bi 999999 |
| 503 | ret false |
| 504 | end |
| 505 | tgt = get oxBlkOff bi |
| 506 | rel = sub tgt add fp 4 |
| 507 | put isBuf fp uchar and rel 255 |
| 508 | put isBuf add fp 1 uchar and shr rel 8 255 |
| 509 | put isBuf add fp 2 uchar and shr rel 16 255 |
| 510 | put isBuf add fp 3 uchar and shr rel 24 255 |
| 511 | end |
| 512 | ret true |
| 513 | end |
| 514 | |
| 515 | fn oxCompileExport to ulong |
| 516 | nf = get i_n 3 |
| 517 | expF = uint 0 |
| 518 | for fi ulong nf bound 64 |
| 519 | f = uint add fi 1 |
| 520 | if eq get i_fk f 2 |
| 521 | expF = f |
| 522 | end |
| 523 | end |
| 524 | if eq expF 0 |
| 525 | ret 0 |
| 526 | end |
| 527 | if not oxAllocFunc expF |
| 528 | ret 0 |
| 529 | end |
| 530 | isBufReset |
| 531 | put oxFixCnt 0 0 |
| 532 | b = get i_fb1 expF |
| 533 | while ne b 0 bound 128 |
| 534 | bi = oxFindBlkIdx ulong b |
| 535 | if ne bi 999999 |
| 536 | put oxBlkOff bi get isLen 0 |
| 537 | end |
| 538 | o = get i_bo1 b |
| 539 | while ne o 0 bound 256 |
| 540 | if not oxLowerOneOp o |
| 541 | ret 0 |
| 542 | end |
| 543 | o = get i_onx o |
| 544 | end |
| 545 | b = get i_bnx b |
| 546 | end |
| 547 | if not oxPatchFixups |
| 548 | ret 0 |
| 549 | end |
| 550 | ret elfEmitStatic TID_X64_AVX2 |
| 551 | end |
| 552 | |
| 553 | fn main to int |
| 554 | tdInitAll |
| 555 | isInitRules |
| 556 | tdCacheInit |
| 557 | r = ar_rdin 0 |
| 558 | if ne r 0 |
| 559 | ret 2 |
| 560 | end |
| 561 | ar_init |
| 562 | n = get ar_inl 0 |
| 563 | rr = ir_read 0 n |
| 564 | if ne rr 0 |
| 565 | ret int rr |
| 566 | end |
| 567 | op_run |
| 568 | eln = oxCompileExport |
| 569 | if eq eln 0 |
| 570 | ret 3 |
| 571 | end |
| 572 | w = write 1 elfBuf 0 eln |
| 573 | if ne w long eln |
| 574 | ret 4 |
| 575 | end |
| 576 | ret 0 |
| 577 | end |