| 29 | if (hi > 31) |
| 30 | hi = 31; |
| 31 | |
| 32 | // Guarded against shifting by 32 or more, which is undefined behaviour. |
| 33 | const int width = hi - lo + 1; |
| 34 | if (width >= 32) |
| 35 | return v >> lo; |
| 36 | |
| 37 | const uint32_t mask = (1u << width) - 1u; |
| 38 | return (v >> lo) & mask; |
| 39 | } |
| 40 | } // namespace KittyAsm |
| 41 | |
| 42 | using namespace KittyAsm; |
| 43 | |
| 44 | namespace KittyArm32 |
| 45 | { |
| 46 | // ──── Register naming ───────────────────────────────────────────────────── |
| 47 | |
| 48 | /// Reverses regName(). A linear match on the spelled name rather than a table: |
| 49 | /// the alias registers (sp/lr/pc, and fp/ip/sb on ARM32) have to be recognised |
| 50 | /// alongside the plain rN/xN forms, and a table would have to encode both. |
| 51 | int regIndex(const std::string &name) |
| 52 | { |
| 53 | if (name.empty()) |
| 54 | return -1; |
| 55 | if (name == "sp") |
| 56 | return 13; |
| 57 | if (name == "lr") |
| 58 | return 14; |
| 59 | if (name == "pc") |
| 60 | return 15; |
| 61 | if (name.size() < 2 || name[0] != 'r') |
| 62 | return -1; |
| 63 | |
| 64 | // Bounded as it goes: an unbounded accumulate would overflow - undefined |
| 65 | // behaviour - on a long digit string, and no valid name has three digits. |
| 66 | int v = 0; |
| 67 | for (size_t i = 1; i < name.size(); ++i) |
| 68 | { |
| 69 | if (name[i] < '0' || name[i] > '9' || v > 15) |
| 70 | return -1; |
| 71 | v = v * 10 + (name[i] - '0'); |
| 72 | } |
| 73 | return v < 16 ? v : -1; |
| 74 | } |
| 75 | |
| 76 | // ──── Instruction classification ────────────────────────────────────────── |
| 77 | |
| 78 | /// One case per enumerator, with no default: adding a type and forgetting to |
| 79 | /// name it is then a compiler warning rather than a silent "UNKNOWN" in reports. |
| 80 | std::string typeToString(EKittyInsnTypeArm32 t) |
| 81 | { |
| 82 | #define CASE(x) \ |