blob: f892f980a18a2989ce0d9a3766e76bc8593bfa7c [file] [log] [blame]
Vladimir Markoaf6925b2014-10-31 16:37:32 +00001/*
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 Chartiere5f13e52015-02-24 09:37:21 -080017#ifndef ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_
18#define ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_
Vladimir Markoaf6925b2014-10-31 16:37:32 +000019
20#include "dex_instruction.h"
21
22namespace 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.
26enum 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.
37enum 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
48std::ostream& operator<<(std::ostream& os, const DexMemAccessType& type);
49
50// NOTE: The following functions disregard quickened instructions.
51
Vladimir Marko321b9872014-11-24 16:33:51 +000052constexpr bool IsInstructionReturn(Instruction::Code opcode) {
53 return Instruction::RETURN_VOID <= opcode && opcode <= Instruction::RETURN_OBJECT;
54}
55
Vladimir Markoaf6925b2014-10-31 16:37:32 +000056constexpr bool IsInstructionInvoke(Instruction::Code opcode) {
57 return Instruction::INVOKE_VIRTUAL <= opcode && opcode <= Instruction::INVOKE_INTERFACE_RANGE &&
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -070058 opcode != Instruction::RETURN_VOID_NO_BARRIER;
Vladimir Markoaf6925b2014-10-31 16:37:32 +000059}
60
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080061constexpr bool IsInstructionQuickInvoke(Instruction::Code opcode) {
62 return opcode == Instruction::INVOKE_VIRTUAL_QUICK ||
63 opcode == Instruction::INVOKE_VIRTUAL_RANGE_QUICK;
64}
65
Vladimir Markoaf6925b2014-10-31 16:37:32 +000066constexpr bool IsInstructionInvokeStatic(Instruction::Code opcode) {
67 return opcode == Instruction::INVOKE_STATIC || opcode == Instruction::INVOKE_STATIC_RANGE;
68}
69
Vladimir Marko26e7d452014-11-24 14:09:46 +000070constexpr bool IsInstructionGoto(Instruction::Code opcode) {
71 return Instruction::GOTO <= opcode && opcode <= Instruction::GOTO_32;
72}
73
Vladimir Markoaf6925b2014-10-31 16:37:32 +000074constexpr bool IsInstructionIfCc(Instruction::Code opcode) {
75 return Instruction::IF_EQ <= opcode && opcode <= Instruction::IF_LE;
76}
77
78constexpr bool IsInstructionIfCcZ(Instruction::Code opcode) {
79 return Instruction::IF_EQZ <= opcode && opcode <= Instruction::IF_LEZ;
80}
81
82constexpr bool IsInstructionIGet(Instruction::Code code) {
83 return Instruction::IGET <= code && code <= Instruction::IGET_SHORT;
84}
85
86constexpr bool IsInstructionIPut(Instruction::Code code) {
87 return Instruction::IPUT <= code && code <= Instruction::IPUT_SHORT;
88}
89
90constexpr bool IsInstructionSGet(Instruction::Code code) {
91 return Instruction::SGET <= code && code <= Instruction::SGET_SHORT;
92}
93
94constexpr bool IsInstructionSPut(Instruction::Code code) {
95 return Instruction::SPUT <= code && code <= Instruction::SPUT_SHORT;
96}
97
98constexpr bool IsInstructionAGet(Instruction::Code code) {
99 return Instruction::AGET <= code && code <= Instruction::AGET_SHORT;
100}
101
102constexpr bool IsInstructionAPut(Instruction::Code code) {
103 return Instruction::APUT <= code && code <= Instruction::APUT_SHORT;
104}
105
106constexpr bool IsInstructionIGetOrIPut(Instruction::Code code) {
107 return Instruction::IGET <= code && code <= Instruction::IPUT_SHORT;
108}
109
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800110constexpr bool IsInstructionIGetQuickOrIPutQuick(Instruction::Code code) {
111 return (code >= Instruction::IGET_QUICK && code <= Instruction::IPUT_OBJECT_QUICK) ||
112 (code >= Instruction::IPUT_BOOLEAN_QUICK && code <= Instruction::IGET_SHORT_QUICK);
113}
114
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000115constexpr bool IsInstructionSGetOrSPut(Instruction::Code code) {
116 return Instruction::SGET <= code && code <= Instruction::SPUT_SHORT;
117}
118
119constexpr bool IsInstructionAGetOrAPut(Instruction::Code code) {
120 return Instruction::AGET <= code && code <= Instruction::APUT_SHORT;
121}
122
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000123constexpr bool IsInstructionBinOp2Addr(Instruction::Code code) {
124 return Instruction::ADD_INT_2ADDR <= code && code <= Instruction::REM_DOUBLE_2ADDR;
125}
126
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000127// TODO: Remove the #if guards below when we fully migrate to C++14.
128
129constexpr bool IsInvokeInstructionRange(Instruction::Code opcode) {
130#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
131 DCHECK(IsInstructionInvoke(opcode));
132#endif
133 return opcode >= Instruction::INVOKE_VIRTUAL_RANGE;
134}
135
136constexpr DexInvokeType InvokeInstructionType(Instruction::Code opcode) {
137#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
138 DCHECK(IsInstructionInvoke(opcode));
139#endif
140 return static_cast<DexInvokeType>(IsInvokeInstructionRange(opcode)
141 ? (opcode - Instruction::INVOKE_VIRTUAL_RANGE)
142 : (opcode - Instruction::INVOKE_VIRTUAL));
143}
144
145constexpr DexMemAccessType IGetMemAccessType(Instruction::Code code) {
146#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
147 DCHECK(IsInstructionIGet(opcode));
148#endif
149 return static_cast<DexMemAccessType>(code - Instruction::IGET);
150}
151
152constexpr DexMemAccessType IPutMemAccessType(Instruction::Code code) {
153#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
154 DCHECK(IsInstructionIPut(opcode));
155#endif
156 return static_cast<DexMemAccessType>(code - Instruction::IPUT);
157}
158
159constexpr DexMemAccessType SGetMemAccessType(Instruction::Code code) {
160#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
161 DCHECK(IsInstructionSGet(opcode));
162#endif
163 return static_cast<DexMemAccessType>(code - Instruction::SGET);
164}
165
166constexpr DexMemAccessType SPutMemAccessType(Instruction::Code code) {
167#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
168 DCHECK(IsInstructionSPut(opcode));
169#endif
170 return static_cast<DexMemAccessType>(code - Instruction::SPUT);
171}
172
173constexpr DexMemAccessType AGetMemAccessType(Instruction::Code code) {
174#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
175 DCHECK(IsInstructionAGet(opcode));
176#endif
177 return static_cast<DexMemAccessType>(code - Instruction::AGET);
178}
179
180constexpr DexMemAccessType APutMemAccessType(Instruction::Code code) {
181#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
182 DCHECK(IsInstructionAPut(opcode));
183#endif
184 return static_cast<DexMemAccessType>(code - Instruction::APUT);
185}
186
187constexpr DexMemAccessType IGetOrIPutMemAccessType(Instruction::Code code) {
188#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
189 DCHECK(IsInstructionIGetOrIPut(opcode));
190#endif
191 return (code >= Instruction::IPUT) ? IPutMemAccessType(code) : IGetMemAccessType(code);
192}
193
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800194static inline DexMemAccessType IGetQuickOrIPutQuickMemAccessType(Instruction::Code code) {
195 DCHECK(IsInstructionIGetQuickOrIPutQuick(code));
196 switch (code) {
197 case Instruction::IGET_QUICK: case Instruction::IPUT_QUICK:
198 return kDexMemAccessWord;
199 case Instruction::IGET_WIDE_QUICK: case Instruction::IPUT_WIDE_QUICK:
200 return kDexMemAccessWide;
201 case Instruction::IGET_OBJECT_QUICK: case Instruction::IPUT_OBJECT_QUICK:
202 return kDexMemAccessObject;
203 case Instruction::IGET_BOOLEAN_QUICK: case Instruction::IPUT_BOOLEAN_QUICK:
204 return kDexMemAccessBoolean;
205 case Instruction::IGET_BYTE_QUICK: case Instruction::IPUT_BYTE_QUICK:
206 return kDexMemAccessByte;
207 case Instruction::IGET_CHAR_QUICK: case Instruction::IPUT_CHAR_QUICK:
208 return kDexMemAccessChar;
209 case Instruction::IGET_SHORT_QUICK: case Instruction::IPUT_SHORT_QUICK:
210 return kDexMemAccessShort;
211 default:
212 LOG(FATAL) << code;
213 UNREACHABLE();
214 }
215}
216
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000217constexpr DexMemAccessType SGetOrSPutMemAccessType(Instruction::Code code) {
218#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
219 DCHECK(IsInstructionSGetOrSPut(opcode));
220#endif
221 return (code >= Instruction::SPUT) ? SPutMemAccessType(code) : SGetMemAccessType(code);
222}
223
224constexpr DexMemAccessType AGetOrAPutMemAccessType(Instruction::Code code) {
225#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
226 DCHECK(IsInstructionAGetOrAPut(opcode));
227#endif
228 return (code >= Instruction::APUT) ? APutMemAccessType(code) : AGetMemAccessType(code);
229}
230
231} // namespace art
232
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800233#endif // ART_RUNTIME_DEX_INSTRUCTION_UTILS_H_