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 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_ |
| 18 | #define ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_ |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 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 | |
Vladimir Marko | f8b3b8b | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 52 | // By "direct" const we mean to exclude const-string and const-class |
| 53 | // which load data from somewhere else, i.e. indirectly. |
| 54 | constexpr bool IsInstructionDirectConst(Instruction::Code opcode) { |
| 55 | return Instruction::CONST_4 <= opcode && opcode <= Instruction::CONST_WIDE_HIGH16; |
| 56 | } |
| 57 | |
| 58 | constexpr bool IsInstructionConstWide(Instruction::Code opcode) { |
| 59 | return Instruction::CONST_WIDE_16 <= opcode && opcode <= Instruction::CONST_WIDE_HIGH16; |
| 60 | } |
| 61 | |
Vladimir Marko | 321b987 | 2014-11-24 16:33:51 +0000 | [diff] [blame] | 62 | constexpr bool IsInstructionReturn(Instruction::Code opcode) { |
| 63 | return Instruction::RETURN_VOID <= opcode && opcode <= Instruction::RETURN_OBJECT; |
| 64 | } |
| 65 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 66 | constexpr bool IsInstructionInvoke(Instruction::Code opcode) { |
| 67 | return Instruction::INVOKE_VIRTUAL <= opcode && opcode <= Instruction::INVOKE_INTERFACE_RANGE && |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 68 | opcode != Instruction::RETURN_VOID_NO_BARRIER; |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 71 | constexpr bool IsInstructionQuickInvoke(Instruction::Code opcode) { |
| 72 | return opcode == Instruction::INVOKE_VIRTUAL_QUICK || |
| 73 | opcode == Instruction::INVOKE_VIRTUAL_RANGE_QUICK; |
| 74 | } |
| 75 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 76 | constexpr bool IsInstructionInvokeStatic(Instruction::Code opcode) { |
| 77 | return opcode == Instruction::INVOKE_STATIC || opcode == Instruction::INVOKE_STATIC_RANGE; |
| 78 | } |
| 79 | |
Vladimir Marko | 26e7d45 | 2014-11-24 14:09:46 +0000 | [diff] [blame] | 80 | constexpr bool IsInstructionGoto(Instruction::Code opcode) { |
| 81 | return Instruction::GOTO <= opcode && opcode <= Instruction::GOTO_32; |
| 82 | } |
| 83 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 84 | constexpr bool IsInstructionIfCc(Instruction::Code opcode) { |
| 85 | return Instruction::IF_EQ <= opcode && opcode <= Instruction::IF_LE; |
| 86 | } |
| 87 | |
| 88 | constexpr bool IsInstructionIfCcZ(Instruction::Code opcode) { |
| 89 | return Instruction::IF_EQZ <= opcode && opcode <= Instruction::IF_LEZ; |
| 90 | } |
| 91 | |
| 92 | constexpr bool IsInstructionIGet(Instruction::Code code) { |
| 93 | return Instruction::IGET <= code && code <= Instruction::IGET_SHORT; |
| 94 | } |
| 95 | |
| 96 | constexpr bool IsInstructionIPut(Instruction::Code code) { |
| 97 | return Instruction::IPUT <= code && code <= Instruction::IPUT_SHORT; |
| 98 | } |
| 99 | |
| 100 | constexpr bool IsInstructionSGet(Instruction::Code code) { |
| 101 | return Instruction::SGET <= code && code <= Instruction::SGET_SHORT; |
| 102 | } |
| 103 | |
| 104 | constexpr bool IsInstructionSPut(Instruction::Code code) { |
| 105 | return Instruction::SPUT <= code && code <= Instruction::SPUT_SHORT; |
| 106 | } |
| 107 | |
| 108 | constexpr bool IsInstructionAGet(Instruction::Code code) { |
| 109 | return Instruction::AGET <= code && code <= Instruction::AGET_SHORT; |
| 110 | } |
| 111 | |
| 112 | constexpr bool IsInstructionAPut(Instruction::Code code) { |
| 113 | return Instruction::APUT <= code && code <= Instruction::APUT_SHORT; |
| 114 | } |
| 115 | |
| 116 | constexpr bool IsInstructionIGetOrIPut(Instruction::Code code) { |
| 117 | return Instruction::IGET <= code && code <= Instruction::IPUT_SHORT; |
| 118 | } |
| 119 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | constexpr bool IsInstructionIGetQuickOrIPutQuick(Instruction::Code code) { |
| 121 | return (code >= Instruction::IGET_QUICK && code <= Instruction::IPUT_OBJECT_QUICK) || |
| 122 | (code >= Instruction::IPUT_BOOLEAN_QUICK && code <= Instruction::IGET_SHORT_QUICK); |
| 123 | } |
| 124 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 125 | constexpr bool IsInstructionSGetOrSPut(Instruction::Code code) { |
| 126 | return Instruction::SGET <= code && code <= Instruction::SPUT_SHORT; |
| 127 | } |
| 128 | |
| 129 | constexpr bool IsInstructionAGetOrAPut(Instruction::Code code) { |
| 130 | return Instruction::AGET <= code && code <= Instruction::APUT_SHORT; |
| 131 | } |
| 132 | |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 133 | constexpr bool IsInstructionBinOp2Addr(Instruction::Code code) { |
| 134 | return Instruction::ADD_INT_2ADDR <= code && code <= Instruction::REM_DOUBLE_2ADDR; |
| 135 | } |
| 136 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 137 | constexpr bool IsInvokeInstructionRange(Instruction::Code opcode) { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 138 | DCHECK(IsInstructionInvoke(opcode)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 139 | return opcode >= Instruction::INVOKE_VIRTUAL_RANGE; |
| 140 | } |
| 141 | |
| 142 | constexpr DexInvokeType InvokeInstructionType(Instruction::Code opcode) { |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 143 | DCHECK(IsInstructionInvoke(opcode)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 144 | return static_cast<DexInvokeType>(IsInvokeInstructionRange(opcode) |
| 145 | ? (opcode - Instruction::INVOKE_VIRTUAL_RANGE) |
| 146 | : (opcode - Instruction::INVOKE_VIRTUAL)); |
| 147 | } |
| 148 | |
| 149 | constexpr DexMemAccessType IGetMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 150 | DCHECK(IsInstructionIGet(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 151 | return static_cast<DexMemAccessType>(code - Instruction::IGET); |
| 152 | } |
| 153 | |
| 154 | constexpr DexMemAccessType IPutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 155 | DCHECK(IsInstructionIPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 156 | return static_cast<DexMemAccessType>(code - Instruction::IPUT); |
| 157 | } |
| 158 | |
| 159 | constexpr DexMemAccessType SGetMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 160 | DCHECK(IsInstructionSGet(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 161 | return static_cast<DexMemAccessType>(code - Instruction::SGET); |
| 162 | } |
| 163 | |
| 164 | constexpr DexMemAccessType SPutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 165 | DCHECK(IsInstructionSPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 166 | return static_cast<DexMemAccessType>(code - Instruction::SPUT); |
| 167 | } |
| 168 | |
| 169 | constexpr DexMemAccessType AGetMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 170 | DCHECK(IsInstructionAGet(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 171 | return static_cast<DexMemAccessType>(code - Instruction::AGET); |
| 172 | } |
| 173 | |
| 174 | constexpr DexMemAccessType APutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 175 | DCHECK(IsInstructionAPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 176 | return static_cast<DexMemAccessType>(code - Instruction::APUT); |
| 177 | } |
| 178 | |
| 179 | constexpr DexMemAccessType IGetOrIPutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 180 | DCHECK(IsInstructionIGetOrIPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 181 | return (code >= Instruction::IPUT) ? IPutMemAccessType(code) : IGetMemAccessType(code); |
| 182 | } |
| 183 | |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 184 | inline DexMemAccessType IGetQuickOrIPutQuickMemAccessType(Instruction::Code code) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 185 | DCHECK(IsInstructionIGetQuickOrIPutQuick(code)); |
| 186 | switch (code) { |
| 187 | case Instruction::IGET_QUICK: case Instruction::IPUT_QUICK: |
| 188 | return kDexMemAccessWord; |
| 189 | case Instruction::IGET_WIDE_QUICK: case Instruction::IPUT_WIDE_QUICK: |
| 190 | return kDexMemAccessWide; |
| 191 | case Instruction::IGET_OBJECT_QUICK: case Instruction::IPUT_OBJECT_QUICK: |
| 192 | return kDexMemAccessObject; |
| 193 | case Instruction::IGET_BOOLEAN_QUICK: case Instruction::IPUT_BOOLEAN_QUICK: |
| 194 | return kDexMemAccessBoolean; |
| 195 | case Instruction::IGET_BYTE_QUICK: case Instruction::IPUT_BYTE_QUICK: |
| 196 | return kDexMemAccessByte; |
| 197 | case Instruction::IGET_CHAR_QUICK: case Instruction::IPUT_CHAR_QUICK: |
| 198 | return kDexMemAccessChar; |
| 199 | case Instruction::IGET_SHORT_QUICK: case Instruction::IPUT_SHORT_QUICK: |
| 200 | return kDexMemAccessShort; |
| 201 | default: |
| 202 | LOG(FATAL) << code; |
| 203 | UNREACHABLE(); |
| 204 | } |
| 205 | } |
| 206 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 207 | constexpr DexMemAccessType SGetOrSPutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 208 | DCHECK(IsInstructionSGetOrSPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 209 | return (code >= Instruction::SPUT) ? SPutMemAccessType(code) : SGetMemAccessType(code); |
| 210 | } |
| 211 | |
| 212 | constexpr DexMemAccessType AGetOrAPutMemAccessType(Instruction::Code code) { |
Dan Albert | eb3bd88 | 2015-08-07 16:19:01 -0700 | [diff] [blame] | 213 | DCHECK(IsInstructionAGetOrAPut(code)); |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 214 | return (code >= Instruction::APUT) ? APutMemAccessType(code) : AGetMemAccessType(code); |
| 215 | } |
| 216 | |
| 217 | } // namespace art |
| 218 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 219 | #endif // ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_ |