Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_MANAGED_REGISTER_X86_H_ |
| 4 | #define ART_SRC_MANAGED_REGISTER_X86_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "constants_x86.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 7 | #include "managed_register.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 8 | |
| 9 | namespace art { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 10 | namespace x86 { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 11 | |
| 12 | // Values for register pairs. |
| 13 | // The registers in kReservedCpuRegistersArray in x86.cc are not used in pairs. |
| 14 | // The table kRegisterPairs in x86.cc must be kept in sync with this enum. |
| 15 | enum RegisterPair { |
| 16 | EAX_EDX = 0, |
| 17 | EAX_ECX = 1, |
| 18 | EAX_EBX = 2, |
| 19 | EAX_EDI = 3, |
| 20 | EDX_ECX = 4, |
| 21 | EDX_EBX = 5, |
| 22 | EDX_EDI = 6, |
| 23 | ECX_EBX = 7, |
| 24 | ECX_EDI = 8, |
| 25 | EBX_EDI = 9, |
| 26 | kNumberOfRegisterPairs = 10, |
| 27 | kNoRegisterPair = -1, |
| 28 | }; |
| 29 | |
| 30 | std::ostream& operator<<(std::ostream& os, const RegisterPair& reg); |
| 31 | |
| 32 | const int kNumberOfCpuRegIds = kNumberOfCpuRegisters; |
| 33 | const int kNumberOfCpuAllocIds = kNumberOfCpuRegisters; |
| 34 | |
| 35 | const int kNumberOfXmmRegIds = kNumberOfXmmRegisters; |
| 36 | const int kNumberOfXmmAllocIds = kNumberOfXmmRegisters; |
| 37 | |
| 38 | const int kNumberOfX87RegIds = kNumberOfX87Registers; |
| 39 | const int kNumberOfX87AllocIds = kNumberOfX87Registers; |
| 40 | |
| 41 | const int kNumberOfPairRegIds = kNumberOfRegisterPairs; |
| 42 | |
| 43 | const int kNumberOfRegIds = kNumberOfCpuRegIds + kNumberOfXmmRegIds + |
| 44 | kNumberOfX87RegIds + kNumberOfPairRegIds; |
| 45 | const int kNumberOfAllocIds = kNumberOfCpuAllocIds + kNumberOfXmmAllocIds + |
| 46 | kNumberOfX87RegIds; |
| 47 | |
| 48 | // Register ids map: |
| 49 | // [0..R[ cpu registers (enum Register) |
| 50 | // [R..X[ xmm registers (enum XmmRegister) |
| 51 | // [X..S[ x87 registers (enum X87Register) |
| 52 | // [S..P[ register pairs (enum RegisterPair) |
| 53 | // where |
| 54 | // R = kNumberOfCpuRegIds |
| 55 | // X = R + kNumberOfXmmRegIds |
| 56 | // S = X + kNumberOfX87RegIds |
| 57 | // P = X + kNumberOfRegisterPairs |
| 58 | |
| 59 | // Allocation ids map: |
| 60 | // [0..R[ cpu registers (enum Register) |
| 61 | // [R..X[ xmm registers (enum XmmRegister) |
| 62 | // [X..S[ x87 registers (enum X87Register) |
| 63 | // where |
| 64 | // R = kNumberOfCpuRegIds |
| 65 | // X = R + kNumberOfXmmRegIds |
| 66 | // S = X + kNumberOfX87RegIds |
| 67 | |
| 68 | |
| 69 | // An instance of class 'ManagedRegister' represents a single cpu register (enum |
| 70 | // Register), an xmm register (enum XmmRegister), or a pair of cpu registers |
| 71 | // (enum RegisterPair). |
| 72 | // 'ManagedRegister::NoRegister()' provides an invalid register. |
| 73 | // There is a one-to-one mapping between ManagedRegister and register id. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 74 | class X86ManagedRegister : public ManagedRegister { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 75 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 76 | Register AsCpuRegister() const { |
| 77 | CHECK(IsCpuRegister()); |
| 78 | return static_cast<Register>(id_); |
| 79 | } |
| 80 | |
| 81 | XmmRegister AsXmmRegister() const { |
| 82 | CHECK(IsXmmRegister()); |
| 83 | return static_cast<XmmRegister>(id_ - kNumberOfCpuRegIds); |
| 84 | } |
| 85 | |
| 86 | X87Register AsX87Register() const { |
| 87 | CHECK(IsX87Register()); |
| 88 | return static_cast<X87Register>(id_ - |
| 89 | (kNumberOfCpuRegIds + kNumberOfXmmRegIds)); |
| 90 | } |
| 91 | |
| 92 | Register AsRegisterPairLow() const { |
| 93 | CHECK(IsRegisterPair()); |
| 94 | // Appropriate mapping of register ids allows to use AllocIdLow(). |
| 95 | return FromRegId(AllocIdLow()).AsCpuRegister(); |
| 96 | } |
| 97 | |
| 98 | Register AsRegisterPairHigh() const { |
| 99 | CHECK(IsRegisterPair()); |
| 100 | // Appropriate mapping of register ids allows to use AllocIdHigh(). |
| 101 | return FromRegId(AllocIdHigh()).AsCpuRegister(); |
| 102 | } |
| 103 | |
| 104 | bool IsCpuRegister() const { |
| 105 | CHECK(IsValidManagedRegister()); |
| 106 | return (0 <= id_) && (id_ < kNumberOfCpuRegIds); |
| 107 | } |
| 108 | |
| 109 | bool IsXmmRegister() const { |
| 110 | CHECK(IsValidManagedRegister()); |
| 111 | const int test = id_ - kNumberOfCpuRegIds; |
| 112 | return (0 <= test) && (test < kNumberOfXmmRegIds); |
| 113 | } |
| 114 | |
| 115 | bool IsX87Register() const { |
| 116 | CHECK(IsValidManagedRegister()); |
| 117 | const int test = id_ - (kNumberOfCpuRegIds + kNumberOfXmmRegIds); |
| 118 | return (0 <= test) && (test < kNumberOfXmmRegIds); |
| 119 | } |
| 120 | |
| 121 | bool IsRegisterPair() const { |
| 122 | CHECK(IsValidManagedRegister()); |
| 123 | const int test = id_ - |
| 124 | (kNumberOfCpuRegIds + kNumberOfXmmRegIds + kNumberOfX87RegIds); |
| 125 | return (0 <= test) && (test < kNumberOfPairRegIds); |
| 126 | } |
| 127 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 128 | void Print(std::ostream& os) const; |
| 129 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 130 | // Returns true if the two managed-registers ('this' and 'other') overlap. |
| 131 | // Either managed-register may be the NoRegister. If both are the NoRegister |
| 132 | // then false is returned. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 133 | bool Overlaps(const X86ManagedRegister& other) const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 134 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 135 | static X86ManagedRegister FromCpuRegister(Register r) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 136 | CHECK_NE(r, kNoRegister); |
| 137 | return FromRegId(r); |
| 138 | } |
| 139 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 140 | static X86ManagedRegister FromXmmRegister(XmmRegister r) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 141 | CHECK_NE(r, kNoXmmRegister); |
| 142 | return FromRegId(r + kNumberOfCpuRegIds); |
| 143 | } |
| 144 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 145 | static X86ManagedRegister FromX87Register(X87Register r) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 146 | CHECK_NE(r, kNoX87Register); |
| 147 | return FromRegId(r + kNumberOfCpuRegIds + kNumberOfXmmRegIds); |
| 148 | } |
| 149 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 150 | static X86ManagedRegister FromRegisterPair(RegisterPair r) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 151 | CHECK_NE(r, kNoRegisterPair); |
| 152 | return FromRegId(r + (kNumberOfCpuRegIds + kNumberOfXmmRegIds + |
| 153 | kNumberOfX87RegIds)); |
| 154 | } |
| 155 | |
| 156 | private: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 157 | bool IsValidManagedRegister() const { |
| 158 | return (0 <= id_) && (id_ < kNumberOfRegIds); |
| 159 | } |
| 160 | |
| 161 | int RegId() const { |
| 162 | CHECK(!IsNoRegister()); |
| 163 | return id_; |
| 164 | } |
| 165 | |
| 166 | int AllocId() const { |
| 167 | CHECK(IsValidManagedRegister() && !IsRegisterPair()); |
| 168 | CHECK_LT(id_, kNumberOfAllocIds); |
| 169 | return id_; |
| 170 | } |
| 171 | |
| 172 | int AllocIdLow() const; |
| 173 | int AllocIdHigh() const; |
| 174 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 175 | friend class ManagedRegister; |
| 176 | |
| 177 | X86ManagedRegister(int reg_id) : ManagedRegister(reg_id) {} |
| 178 | |
| 179 | static X86ManagedRegister FromRegId(int reg_id) { |
| 180 | X86ManagedRegister reg(reg_id); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 181 | CHECK(reg.IsValidManagedRegister()); |
| 182 | return reg; |
| 183 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 186 | std::ostream& operator<<(std::ostream& os, const X86ManagedRegister& reg); |
| 187 | |
| 188 | } // namespace x86 |
| 189 | |
| 190 | inline x86::X86ManagedRegister ManagedRegister::AsX86() const { |
| 191 | x86::X86ManagedRegister reg(id_); |
| 192 | CHECK(reg.IsNoRegister() || reg.IsValidManagedRegister()); |
| 193 | return reg; |
| 194 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 195 | |
| 196 | } // namespace art |
| 197 | |
| 198 | #endif // ART_SRC_MANAGED_REGISTER_X86_H_ |