| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <cstddef> |
| 7 |
#include <string> |
| 8 |
#include <string_view> |
| 9 |
#include <variant> |
| 10 |
|
| 11 |
namespace Common::GekkoAssembler |
| 12 |
{ |
| 13 |
struct Interval |
| 14 |
{ |
| 15 |
size_t begin; |
| 16 |
size_t len; |
| 17 |
constexpr size_t End() const { return begin + len; } |
| 18 |
}; |
| 19 |
|
| 20 |
struct AssemblerError |
| 21 |
{ |
| 22 |
std::string message; |
| 23 |
std::string_view error_line; |
| 24 |
size_t line; |
| 25 |
size_t col; |
| 26 |
size_t len; |
| 27 |
|
| 28 |
std::string FormatError() const; |
| 29 |
}; |
| 30 |
|
| 31 |
template <typename Tag, typename T> |
| 32 |
using Tagged = std::pair<Tag, T>; |
| 33 |
template <typename Tag, typename T> |
| 34 |
constexpr const Tag& TagOf(const Tagged<Tag, T>& val) |
| 35 |
{ |
| 36 |
return std::get<0>(val); |
| 37 |
} |
| 38 |
template <typename Tag, typename T> |
| 39 |
constexpr Tag& TagOf(Tagged<Tag, T>& val) |
| 40 |
{ |
| 41 |
return std::get<0>(val); |
| 42 |
} |
| 43 |
template <typename Tag, typename T> |
| 44 |
constexpr const T& ValueOf(const Tagged<Tag, T>& val) |
| 45 |
{ |
| 46 |
return std::get<1>(val); |
| 47 |
} |
| 48 |
template <typename Tag, typename T> |
| 49 |
constexpr T& ValueOf(Tagged<Tag, T>& val) |
| 50 |
{ |
| 51 |
return std::get<1>(val); |
| 52 |
} |
| 53 |
|
| 54 |
template <typename T> |
| 55 |
using FailureOr = std::variant<AssemblerError, T>; |
| 56 |
template <typename T> |
| 57 |
constexpr bool IsFailure(const FailureOr<T>& var) |
| 58 |
{ |
| 59 |
return std::holds_alternative<AssemblerError>(var); |
| 60 |
} |
| 61 |
template <typename T> |
| 62 |
constexpr AssemblerError& GetFailure(FailureOr<T>& var) |
| 63 |
{ |
| 64 |
return std::get<AssemblerError>(var); |
| 65 |
} |
| 66 |
template <typename T> |
| 67 |
constexpr const AssemblerError& GetFailure(const FailureOr<T>& var) |
| 68 |
{ |
| 69 |
return std::get<AssemblerError>(var); |
| 70 |
} |
| 71 |
template <typename T> |
| 72 |
constexpr const T& GetT(const FailureOr<T>& var) |
| 73 |
{ |
| 74 |
return std::get<T>(var); |
| 75 |
} |
| 76 |
template <typename T> |
| 77 |
constexpr T& GetT(FailureOr<T>& var) |
| 78 |
{ |
| 79 |
return std::get<T>(var); |
| 80 |
} |
| 81 |
|
| 82 |
enum class GekkoDirective |
| 83 |
{ |
| 84 |
Byte, |
| 85 |
_2byte, |
| 86 |
_4byte, |
| 87 |
_8byte, |
| 88 |
Float, |
| 89 |
Double, |
| 90 |
Locate, |
| 91 |
PadAlign, |
| 92 |
Align, |
| 93 |
Zeros, |
| 94 |
Skip, |
| 95 |
DefVar, |
| 96 |
Ascii, |
| 97 |
Asciz |
| 98 |
}; |
| 99 |
|
| 100 |
enum class GekkoMnemonic : size_t |
| 101 |
{ |
| 102 |
Add, |
| 103 |
Addc, |
| 104 |
Adde, |
| 105 |
Addi, |
| 106 |
Addic, |
| 107 |
AddicDot, |
| 108 |
Addis, |
| 109 |
Addme, |
| 110 |
Addze, |
| 111 |
Divw, |
| 112 |
Divwu, |
| 113 |
Mulhw, |
| 114 |
Mulhwu, |
| 115 |
Mulli, |
| 116 |
Mullw, |
| 117 |
Neg, |
| 118 |
Subf, |
| 119 |
Subfc, |
| 120 |
Subfe, |
| 121 |
Subfic, |
| 122 |
Subfme, |
| 123 |
Subfze, |
| 124 |
Cmp, |
| 125 |
Cmpi, |
| 126 |
Cmpl, |
| 127 |
Cmpli, |
| 128 |
And, |
| 129 |
Andc, |
| 130 |
AndiDot, |
| 131 |
AndisDot, |
| 132 |
Cntlzw, |
| 133 |
Eqv, |
| 134 |
Extsb, |
| 135 |
Extsh, |
| 136 |
Nand, |
| 137 |
Nor, |
| 138 |
Or, |
| 139 |
Orc, |
| 140 |
Ori, |
| 141 |
Oris, |
| 142 |
Xor, |
| 143 |
Xori, |
| 144 |
Xoris, |
| 145 |
Rlwimi, |
| 146 |
Rlwinm, |
| 147 |
Rlwnm, |
| 148 |
Slw, |
| 149 |
Sraw, |
| 150 |
Srawi, |
| 151 |
Srw, |
| 152 |
Fadd, |
| 153 |
Fadds, |
| 154 |
Fdiv, |
| 155 |
Fdivs, |
| 156 |
Fmul, |
| 157 |
Fmuls, |
| 158 |
Fres, |
| 159 |
Frsqrte, |
| 160 |
Fsub, |
| 161 |
Fsubs, |
| 162 |
Fsel, |
| 163 |
Fmadd, |
| 164 |
Fmadds, |
| 165 |
Fmsub, |
| 166 |
Fmsubs, |
| 167 |
Fnmadd, |
| 168 |
Fnmadds, |
| 169 |
Fnmsub, |
| 170 |
Fnmsubs, |
| 171 |
Fctiw, |
| 172 |
Fctiwz, |
| 173 |
Frsp, |
| 174 |
Fcmpo, |
| 175 |
Fcmpu, |
| 176 |
Mcrfs, |
| 177 |
Mffs, |
| 178 |
Mtfsb0, |
| 179 |
Mtfsb1, |
| 180 |
Mtfsf, |
| 181 |
Mtfsfi, |
| 182 |
Lbz, |
| 183 |
Lbzu, |
| 184 |
Lbzux, |
| 185 |
Lbzx, |
| 186 |
Lha, |
| 187 |
Lhau, |
| 188 |
Lhaux, |
| 189 |
Lhax, |
| 190 |
Lhz, |
| 191 |
Lhzu, |
| 192 |
Lhzux, |
| 193 |
Lhzx, |
| 194 |
Lwz, |
| 195 |
Lwzu, |
| 196 |
Lwzux, |
| 197 |
Lwzx, |
| 198 |
Stb, |
| 199 |
Stbu, |
| 200 |
Stbux, |
| 201 |
Stbx, |
| 202 |
Sth, |
| 203 |
Sthu, |
| 204 |
Sthux, |
| 205 |
Sthx, |
| 206 |
Stw, |
| 207 |
Stwu, |
| 208 |
Stwux, |
| 209 |
Stwx, |
| 210 |
Lhbrx, |
| 211 |
Lwbrx, |
| 212 |
Sthbrx, |
| 213 |
Stwbrx, |
| 214 |
Lmw, |
| 215 |
Stmw, |
| 216 |
Lswi, |
| 217 |
Lswx, |
| 218 |
Stswi, |
| 219 |
Stswx, |
| 220 |
Eieio, |
| 221 |
Isync, |
| 222 |
Lwarx, |
| 223 |
StwcxDot, |
| 224 |
Sync, |
| 225 |
Lfd, |
| 226 |
Lfdu, |
| 227 |
Lfdux, |
| 228 |
Lfdx, |
| 229 |
Lfs, |
| 230 |
Lfsu, |
| 231 |
Lfsux, |
| 232 |
Lfsx, |
| 233 |
Stfd, |
| 234 |
Stfdu, |
| 235 |
Stfdux, |
| 236 |
Stfdx, |
| 237 |
Stfiwx, |
| 238 |
Stfs, |
| 239 |
Stfsu, |
| 240 |
Stfsux, |
| 241 |
Stfsx, |
| 242 |
Fabs, |
| 243 |
Fmr, |
| 244 |
Fnabs, |
| 245 |
Fneg, |
| 246 |
B, |
| 247 |
Bc, |
| 248 |
Bcctr, |
| 249 |
Bclr, |
| 250 |
Crand, |
| 251 |
Crandc, |
| 252 |
Creqv, |
| 253 |
Crnand, |
| 254 |
Crnor, |
| 255 |
Cror, |
| 256 |
Crorc, |
| 257 |
Crxor, |
| 258 |
Mcrf, |
| 259 |
Rfi, |
| 260 |
Sc, |
| 261 |
Tw, |
| 262 |
Twi, |
| 263 |
Mcrxr, |
| 264 |
Mfcr, |
| 265 |
Mfmsr, |
| 266 |
Mfspr_nobitswap, |
| 267 |
Mftb_nobitswap, |
| 268 |
Mtcrf, |
| 269 |
Mtmsr, |
| 270 |
Mtspr_nobitswap, |
| 271 |
Dcbf, |
| 272 |
Dcbi, |
| 273 |
Dcbst, |
| 274 |
Dcbt, |
| 275 |
Dcbtst, |
| 276 |
Dcbz, |
| 277 |
Icbi, |
| 278 |
Mfsr, |
| 279 |
Mfsrin, |
| 280 |
Mtsr, |
| 281 |
Mtsrin, |
| 282 |
Tlbie, |
| 283 |
Tlbsync, |
| 284 |
Eciwx, |
| 285 |
Ecowx, |
| 286 |
Psq_lx, |
| 287 |
Psq_stx, |
| 288 |
Psq_lux, |
| 289 |
Psq_stux, |
| 290 |
Psq_l, |
| 291 |
Psq_lu, |
| 292 |
Psq_st, |
| 293 |
Psq_stu, |
| 294 |
Ps_div, |
| 295 |
Ps_sub, |
| 296 |
Ps_add, |
| 297 |
Ps_sel, |
| 298 |
Ps_res, |
| 299 |
Ps_mul, |
| 300 |
Ps_rsqrte, |
| 301 |
Ps_msub, |
| 302 |
Ps_madd, |
| 303 |
Ps_nmsub, |
| 304 |
Ps_nmadd, |
| 305 |
Ps_neg, |
| 306 |
Ps_mr, |
| 307 |
Ps_nabs, |
| 308 |
Ps_abs, |
| 309 |
Ps_sum0, |
| 310 |
Ps_sum1, |
| 311 |
Ps_muls0, |
| 312 |
Ps_muls1, |
| 313 |
Ps_madds0, |
| 314 |
Ps_madds1, |
| 315 |
Ps_cmpu0, |
| 316 |
Ps_cmpo0, |
| 317 |
Ps_cmpu1, |
| 318 |
Ps_cmpo1, |
| 319 |
Ps_merge00, |
| 320 |
Ps_merge01, |
| 321 |
Ps_merge10, |
| 322 |
Ps_merge11, |
| 323 |
Dcbz_l, |
| 324 |
LastMnemonic = Dcbz_l, |
| 325 |
InvalidMnemonic, |
| 326 |
}; |
| 327 |
|
| 328 |
enum class ExtendedGekkoMnemonic : size_t |
| 329 |
{ |
| 330 |
Subi, |
| 331 |
Subis, |
| 332 |
Subic, |
| 333 |
SubicDot, |
| 334 |
Sub, |
| 335 |
Subc, |
| 336 |
Cmpwi, |
| 337 |
Cmpw, |
| 338 |
Cmplwi, |
| 339 |
Cmplw, |
| 340 |
Extlwi, |
| 341 |
Extrwi, |
| 342 |
Inslwi, |
| 343 |
Insrwi, |
| 344 |
Rotlwi, |
| 345 |
Rotrwi, |
| 346 |
Rotlw, |
| 347 |
Slwi, |
| 348 |
Srwi, |
| 349 |
Clrlwi, |
| 350 |
Clrrwi, |
| 351 |
Clrlslwi, |
| 352 |
Bt, |
| 353 |
Bf, |
| 354 |
Bdnz, |
| 355 |
Bdnzt, |
| 356 |
Bdnzf, |
| 357 |
Bdz, |
| 358 |
Bdzt, |
| 359 |
Bdzf, |
| 360 |
BtPredict, |
| 361 |
BfPredict, |
| 362 |
BdnzPredict, |
| 363 |
BdnztPredict, |
| 364 |
BdnzfPredict, |
| 365 |
BdzPredict, |
| 366 |
BdztPredict, |
| 367 |
BdzfPredict, |
| 368 |
Blr, |
| 369 |
Btlr, |
| 370 |
Bflr, |
| 371 |
Bdnzlr, |
| 372 |
Bdnztlr, |
| 373 |
Bdnzflr, |
| 374 |
Bdzlr, |
| 375 |
Bdztlr, |
| 376 |
Bdzflr, |
| 377 |
BtlrPredict, |
| 378 |
BflrPredict, |
| 379 |
BdnzlrPredict, |
| 380 |
BdnztlrPredict, |
| 381 |
BdnzflrPredict, |
| 382 |
BdzlrPredict, |
| 383 |
BdztlrPredict, |
| 384 |
BdzflrPredict, |
| 385 |
Bctr, |
| 386 |
Btctr, |
| 387 |
Bfctr, |
| 388 |
BtctrPredict, |
| 389 |
BfctrPredict, |
| 390 |
Blt, |
| 391 |
Ble, |
| 392 |
Beq, |
| 393 |
Bge, |
| 394 |
Bgt, |
| 395 |
Bnl, |
| 396 |
Bne, |
| 397 |
Bng, |
| 398 |
Bso, |
| 399 |
Bns, |
| 400 |
Bun, |
| 401 |
Bnu, |
| 402 |
BltPredict, |
| 403 |
BlePredict, |
| 404 |
BeqPredict, |
| 405 |
BgePredict, |
| 406 |
BgtPredict, |
| 407 |
BnlPredict, |
| 408 |
BnePredict, |
| 409 |
BngPredict, |
| 410 |
BsoPredict, |
| 411 |
BnsPredict, |
| 412 |
BunPredict, |
| 413 |
BnuPredict, |
| 414 |
Bltlr, |
| 415 |
Blelr, |
| 416 |
Beqlr, |
| 417 |
Bgelr, |
| 418 |
Bgtlr, |
| 419 |
Bnllr, |
| 420 |
Bnelr, |
| 421 |
Bnglr, |
| 422 |
Bsolr, |
| 423 |
Bnslr, |
| 424 |
Bunlr, |
| 425 |
Bnulr, |
| 426 |
BltlrPredict, |
| 427 |
BlelrPredict, |
| 428 |
BeqlrPredict, |
| 429 |
BgelrPredict, |
| 430 |
BgtlrPredict, |
| 431 |
BnllrPredict, |
| 432 |
BnelrPredict, |
| 433 |
BnglrPredict, |
| 434 |
BsolrPredict, |
| 435 |
BnslrPredict, |
| 436 |
BunlrPredict, |
| 437 |
BnulrPredict, |
| 438 |
Bltctr, |
| 439 |
Blectr, |
| 440 |
Beqctr, |
| 441 |
Bgectr, |
| 442 |
Bgtctr, |
| 443 |
Bnlctr, |
| 444 |
Bnectr, |
| 445 |
Bngctr, |
| 446 |
Bsoctr, |
| 447 |
Bnsctr, |
| 448 |
Bunctr, |
| 449 |
Bnuctr, |
| 450 |
BltctrPredict, |
| 451 |
BlectrPredict, |
| 452 |
BeqctrPredict, |
| 453 |
BgectrPredict, |
| 454 |
BgtctrPredict, |
| 455 |
BnlctrPredict, |
| 456 |
BnectrPredict, |
| 457 |
BngctrPredict, |
| 458 |
BsoctrPredict, |
| 459 |
BnsctrPredict, |
| 460 |
BunctrPredict, |
| 461 |
BnuctrPredict, |
| 462 |
Crset, |
| 463 |
Crclr, |
| 464 |
Crmove, |
| 465 |
Crnot, |
| 466 |
Twlt, |
| 467 |
Twlti, |
| 468 |
Twle, |
| 469 |
Twlei, |
| 470 |
Tweq, |
| 471 |
Tweqi, |
| 472 |
Twge, |
| 473 |
Twgei, |
| 474 |
Twgt, |
| 475 |
Twgti, |
| 476 |
Twnl, |
| 477 |
Twnli, |
| 478 |
Twne, |
| 479 |
Twnei, |
| 480 |
Twng, |
| 481 |
Twngi, |
| 482 |
Twllt, |
| 483 |
Twllti, |
| 484 |
Twlle, |
| 485 |
Twllei, |
| 486 |
Twlge, |
| 487 |
Twlgei, |
| 488 |
Twlgt, |
| 489 |
Twlgti, |
| 490 |
Twlnl, |
| 491 |
Twlnli, |
| 492 |
Twlng, |
| 493 |
Twlngi, |
| 494 |
Trap, |
| 495 |
Mtxer, |
| 496 |
Mfxer, |
| 497 |
Mtlr, |
| 498 |
Mflr, |
| 499 |
Mtctr, |
| 500 |
Mfctr, |
| 501 |
Mtdsisr, |
| 502 |
Mfdsisr, |
| 503 |
Mtdar, |
| 504 |
Mfdar, |
| 505 |
Mtdec, |
| 506 |
Mfdec, |
| 507 |
Mtsdr1, |
| 508 |
Mfsdr1, |
| 509 |
Mtsrr0, |
| 510 |
Mfsrr0, |
| 511 |
Mtsrr1, |
| 512 |
Mfsrr1, |
| 513 |
Mtasr, |
| 514 |
Mfasr, |
| 515 |
Mtear, |
| 516 |
Mfear, |
| 517 |
Mttbl, |
| 518 |
Mftbl, |
| 519 |
Mttbu, |
| 520 |
Mftbu, |
| 521 |
Mtsprg, |
| 522 |
Mfsprg, |
| 523 |
Mtibatu, |
| 524 |
Mfibatu, |
| 525 |
Mtibatl, |
| 526 |
Mfibatl, |
| 527 |
Mtdbatu, |
| 528 |
Mfdbatu, |
| 529 |
Mtdbatl, |
| 530 |
Mfdbatl, |
| 531 |
Nop, |
| 532 |
Li, |
| 533 |
Lis, |
| 534 |
La, |
| 535 |
Mr, |
| 536 |
Not, |
| 537 |
Mtcr, |
| 538 |
Mfspr, |
| 539 |
Mftb, |
| 540 |
Mtspr, |
| 541 |
LastMnemonic = Mtspr, |
| 542 |
InvalidMnemonic |
| 543 |
}; |
| 544 |
} |