Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_UTILS_DEX_INSTRUCTION_UTILS_H_ |
| 18 | #define ART_COMPILER_UTILS_DEX_INSTRUCTION_UTILS_H_ |
| 19 | |
| 20 | #include "dex_instruction.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | // Dex invoke type corresponds to the ordering of INVOKE instructions; |
| 25 | // this order is the same for range and non-range invokes. |
| 26 | enum DexInvokeType : uint8_t { |
| 27 | kDexInvokeVirtual = 0, // invoke-virtual, invoke-virtual-range |
| 28 | kDexInvokeSuper, // invoke-super, invoke-super-range |
| 29 | kDexInvokeDirect, // invoke-direct, invoke-direct-range |
| 30 | kDexInvokeStatic, // invoke-static, invoke-static-range |
| 31 | kDexInvokeInterface, // invoke-interface, invoke-interface-range |
| 32 | kDexInvokeTypeCount |
| 33 | }; |
| 34 | |
| 35 | // Dex instruction memory access types correspond to the ordering of GET/PUT instructions; |
| 36 | // this order is the same for IGET, IPUT, SGET, SPUT, AGET and APUT. |
| 37 | enum DexMemAccessType : uint8_t { |
| 38 | kDexMemAccessWord = 0, // op 0; int or float, the actual type is not encoded. |
| 39 | kDexMemAccessWide, // op_WIDE 1; long or double, the actual type is not encoded. |
| 40 | kDexMemAccessObject, // op_OBJECT 2; the actual reference type is not encoded. |
| 41 | kDexMemAccessBoolean, // op_BOOLEAN 3 |
| 42 | kDexMemAccessByte, // op_BYTE 4 |
| 43 | kDexMemAccessChar, // op_CHAR 5 |
| 44 | kDexMemAccessShort, // op_SHORT 6 |
| 45 | kDexMemAccessTypeCount |
| 46 | }; |
| 47 | |
| 48 | std::ostream& operator<<(std::ostream& os, const DexMemAccessType& type); |
| 49 | |
| 50 | // NOTE: The following functions disregard quickened instructions. |
| 51 | |
| 52 | constexpr bool IsInstructionInvoke(Instruction::Code opcode) { |
| 53 | return Instruction::INVOKE_VIRTUAL <= opcode && opcode <= Instruction::INVOKE_INTERFACE_RANGE && |
| 54 | opcode != Instruction::RETURN_VOID_BARRIER; |
| 55 | } |
| 56 | |
| 57 | constexpr bool IsInstructionInvokeStatic(Instruction::Code opcode) { |
| 58 | return opcode == Instruction::INVOKE_STATIC || opcode == Instruction::INVOKE_STATIC_RANGE; |
| 59 | } |
| 60 | |
| 61 | constexpr bool IsInstructionIfCc(Instruction::Code opcode) { |
| 62 | return Instruction::IF_EQ <= opcode && opcode <= Instruction::IF_LE; |
| 63 | } |
| 64 | |
| 65 | constexpr bool IsInstructionIfCcZ(Instruction::Code opcode) { |
| 66 | return Instruction::IF_EQZ <= opcode && opcode <= Instruction::IF_LEZ; |
| 67 | } |
| 68 | |
| 69 | constexpr bool IsInstructionIGet(Instruction::Code code) { |
| 70 | return Instruction::IGET <= code && code <= Instruction::IGET_SHORT; |
| 71 | } |
| 72 | |
| 73 | constexpr bool IsInstructionIPut(Instruction::Code code) { |
| 74 | return Instruction::IPUT <= code && code <= Instruction::IPUT_SHORT; |
| 75 | } |
| 76 | |
| 77 | constexpr bool IsInstructionSGet(Instruction::Code code) { |
| 78 | return Instruction::SGET <= code && code <= Instruction::SGET_SHORT; |
| 79 | } |
| 80 | |
| 81 | constexpr bool IsInstructionSPut(Instruction::Code code) { |
| 82 | return Instruction::SPUT <= code && code <= Instruction::SPUT_SHORT; |
| 83 | } |
| 84 | |
| 85 | constexpr bool IsInstructionAGet(Instruction::Code code) { |
| 86 | return Instruction::AGET <= code && code <= Instruction::AGET_SHORT; |
| 87 | } |
| 88 | |
| 89 | constexpr bool IsInstructionAPut(Instruction::Code code) { |
| 90 | return Instruction::APUT <= code && code <= Instruction::APUT_SHORT; |
| 91 | } |
| 92 | |
| 93 | constexpr bool IsInstructionIGetOrIPut(Instruction::Code code) { |
| 94 | return Instruction::IGET <= code && code <= Instruction::IPUT_SHORT; |
| 95 | } |
| 96 | |
| 97 | constexpr bool IsInstructionSGetOrSPut(Instruction::Code code) { |
| 98 | return Instruction::SGET <= code && code <= Instruction::SPUT_SHORT; |
| 99 | } |
| 100 | |
| 101 | constexpr bool IsInstructionAGetOrAPut(Instruction::Code code) { |
| 102 | return Instruction::AGET <= code && code <= Instruction::APUT_SHORT; |
| 103 | } |
| 104 | |
| 105 | // TODO: Remove the #if guards below when we fully migrate to C++14. |
| 106 | |
| 107 | constexpr bool IsInvokeInstructionRange(Instruction::Code opcode) { |
| 108 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 109 | DCHECK(IsInstructionInvoke(opcode)); |
| 110 | #endif |
| 111 | return opcode >= Instruction::INVOKE_VIRTUAL_RANGE; |
| 112 | } |
| 113 | |
| 114 | constexpr DexInvokeType InvokeInstructionType(Instruction::Code opcode) { |
| 115 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 116 | DCHECK(IsInstructionInvoke(opcode)); |
| 117 | #endif |
| 118 | return static_cast<DexInvokeType>(IsInvokeInstructionRange(opcode) |
| 119 | ? (opcode - Instruction::INVOKE_VIRTUAL_RANGE) |
| 120 | : (opcode - Instruction::INVOKE_VIRTUAL)); |
| 121 | } |
| 122 | |
| 123 | constexpr DexMemAccessType IGetMemAccessType(Instruction::Code code) { |
| 124 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 125 | DCHECK(IsInstructionIGet(opcode)); |
| 126 | #endif |
| 127 | return static_cast<DexMemAccessType>(code - Instruction::IGET); |
| 128 | } |
| 129 | |
| 130 | constexpr DexMemAccessType IPutMemAccessType(Instruction::Code code) { |
| 131 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 132 | DCHECK(IsInstructionIPut(opcode)); |
| 133 | #endif |
| 134 | return static_cast<DexMemAccessType>(code - Instruction::IPUT); |
| 135 | } |
| 136 | |
| 137 | constexpr DexMemAccessType SGetMemAccessType(Instruction::Code code) { |
| 138 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 139 | DCHECK(IsInstructionSGet(opcode)); |
| 140 | #endif |
| 141 | return static_cast<DexMemAccessType>(code - Instruction::SGET); |
| 142 | } |
| 143 | |
| 144 | constexpr DexMemAccessType SPutMemAccessType(Instruction::Code code) { |
| 145 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 146 | DCHECK(IsInstructionSPut(opcode)); |
| 147 | #endif |
| 148 | return static_cast<DexMemAccessType>(code - Instruction::SPUT); |
| 149 | } |
| 150 | |
| 151 | constexpr DexMemAccessType AGetMemAccessType(Instruction::Code code) { |
| 152 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 153 | DCHECK(IsInstructionAGet(opcode)); |
| 154 | #endif |
| 155 | return static_cast<DexMemAccessType>(code - Instruction::AGET); |
| 156 | } |
| 157 | |
| 158 | constexpr DexMemAccessType APutMemAccessType(Instruction::Code code) { |
| 159 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 160 | DCHECK(IsInstructionAPut(opcode)); |
| 161 | #endif |
| 162 | return static_cast<DexMemAccessType>(code - Instruction::APUT); |
| 163 | } |
| 164 | |
| 165 | constexpr DexMemAccessType IGetOrIPutMemAccessType(Instruction::Code code) { |
| 166 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 167 | DCHECK(IsInstructionIGetOrIPut(opcode)); |
| 168 | #endif |
| 169 | return (code >= Instruction::IPUT) ? IPutMemAccessType(code) : IGetMemAccessType(code); |
| 170 | } |
| 171 | |
| 172 | constexpr DexMemAccessType SGetOrSPutMemAccessType(Instruction::Code code) { |
| 173 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 174 | DCHECK(IsInstructionSGetOrSPut(opcode)); |
| 175 | #endif |
| 176 | return (code >= Instruction::SPUT) ? SPutMemAccessType(code) : SGetMemAccessType(code); |
| 177 | } |
| 178 | |
| 179 | constexpr DexMemAccessType AGetOrAPutMemAccessType(Instruction::Code code) { |
| 180 | #if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions. |
| 181 | DCHECK(IsInstructionAGetOrAPut(opcode)); |
| 182 | #endif |
| 183 | return (code >= Instruction::APUT) ? APutMemAccessType(code) : AGetMemAccessType(code); |
| 184 | } |
| 185 | |
| 186 | } // namespace art |
| 187 | |
| 188 | #endif // ART_COMPILER_UTILS_DEX_INSTRUCTION_UTILS_H_ |