blob: bb2c592f137b5f992b75a3d0cd5295e22ee2d498 [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
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +000017#ifndef ART_COMPILER_UTILS_DEX_INSTRUCTION_UTILS_H_
18#define ART_COMPILER_UTILS_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 &&
58 opcode != Instruction::RETURN_VOID_BARRIER;
59}
60
61constexpr bool IsInstructionInvokeStatic(Instruction::Code opcode) {
62 return opcode == Instruction::INVOKE_STATIC || opcode == Instruction::INVOKE_STATIC_RANGE;
63}
64
Vladimir Marko26e7d452014-11-24 14:09:46 +000065constexpr bool IsInstructionGoto(Instruction::Code opcode) {
66 return Instruction::GOTO <= opcode && opcode <= Instruction::GOTO_32;
67}
68
Vladimir Markoaf6925b2014-10-31 16:37:32 +000069constexpr bool IsInstructionIfCc(Instruction::Code opcode) {
70 return Instruction::IF_EQ <= opcode && opcode <= Instruction::IF_LE;
71}
72
73constexpr bool IsInstructionIfCcZ(Instruction::Code opcode) {
74 return Instruction::IF_EQZ <= opcode && opcode <= Instruction::IF_LEZ;
75}
76
77constexpr bool IsInstructionIGet(Instruction::Code code) {
78 return Instruction::IGET <= code && code <= Instruction::IGET_SHORT;
79}
80
81constexpr bool IsInstructionIPut(Instruction::Code code) {
82 return Instruction::IPUT <= code && code <= Instruction::IPUT_SHORT;
83}
84
85constexpr bool IsInstructionSGet(Instruction::Code code) {
86 return Instruction::SGET <= code && code <= Instruction::SGET_SHORT;
87}
88
89constexpr bool IsInstructionSPut(Instruction::Code code) {
90 return Instruction::SPUT <= code && code <= Instruction::SPUT_SHORT;
91}
92
93constexpr bool IsInstructionAGet(Instruction::Code code) {
94 return Instruction::AGET <= code && code <= Instruction::AGET_SHORT;
95}
96
97constexpr bool IsInstructionAPut(Instruction::Code code) {
98 return Instruction::APUT <= code && code <= Instruction::APUT_SHORT;
99}
100
101constexpr bool IsInstructionIGetOrIPut(Instruction::Code code) {
102 return Instruction::IGET <= code && code <= Instruction::IPUT_SHORT;
103}
104
105constexpr bool IsInstructionSGetOrSPut(Instruction::Code code) {
106 return Instruction::SGET <= code && code <= Instruction::SPUT_SHORT;
107}
108
109constexpr bool IsInstructionAGetOrAPut(Instruction::Code code) {
110 return Instruction::AGET <= code && code <= Instruction::APUT_SHORT;
111}
112
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000113constexpr bool IsInstructionBinOp2Addr(Instruction::Code code) {
114 return Instruction::ADD_INT_2ADDR <= code && code <= Instruction::REM_DOUBLE_2ADDR;
115}
116
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000117// TODO: Remove the #if guards below when we fully migrate to C++14.
118
119constexpr bool IsInvokeInstructionRange(Instruction::Code opcode) {
120#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
121 DCHECK(IsInstructionInvoke(opcode));
122#endif
123 return opcode >= Instruction::INVOKE_VIRTUAL_RANGE;
124}
125
126constexpr DexInvokeType InvokeInstructionType(Instruction::Code opcode) {
127#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
128 DCHECK(IsInstructionInvoke(opcode));
129#endif
130 return static_cast<DexInvokeType>(IsInvokeInstructionRange(opcode)
131 ? (opcode - Instruction::INVOKE_VIRTUAL_RANGE)
132 : (opcode - Instruction::INVOKE_VIRTUAL));
133}
134
135constexpr DexMemAccessType IGetMemAccessType(Instruction::Code code) {
136#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
137 DCHECK(IsInstructionIGet(opcode));
138#endif
139 return static_cast<DexMemAccessType>(code - Instruction::IGET);
140}
141
142constexpr DexMemAccessType IPutMemAccessType(Instruction::Code code) {
143#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
144 DCHECK(IsInstructionIPut(opcode));
145#endif
146 return static_cast<DexMemAccessType>(code - Instruction::IPUT);
147}
148
149constexpr DexMemAccessType SGetMemAccessType(Instruction::Code code) {
150#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
151 DCHECK(IsInstructionSGet(opcode));
152#endif
153 return static_cast<DexMemAccessType>(code - Instruction::SGET);
154}
155
156constexpr DexMemAccessType SPutMemAccessType(Instruction::Code code) {
157#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
158 DCHECK(IsInstructionSPut(opcode));
159#endif
160 return static_cast<DexMemAccessType>(code - Instruction::SPUT);
161}
162
163constexpr DexMemAccessType AGetMemAccessType(Instruction::Code code) {
164#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
165 DCHECK(IsInstructionAGet(opcode));
166#endif
167 return static_cast<DexMemAccessType>(code - Instruction::AGET);
168}
169
170constexpr DexMemAccessType APutMemAccessType(Instruction::Code code) {
171#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
172 DCHECK(IsInstructionAPut(opcode));
173#endif
174 return static_cast<DexMemAccessType>(code - Instruction::APUT);
175}
176
177constexpr DexMemAccessType IGetOrIPutMemAccessType(Instruction::Code code) {
178#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
179 DCHECK(IsInstructionIGetOrIPut(opcode));
180#endif
181 return (code >= Instruction::IPUT) ? IPutMemAccessType(code) : IGetMemAccessType(code);
182}
183
184constexpr DexMemAccessType SGetOrSPutMemAccessType(Instruction::Code code) {
185#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
186 DCHECK(IsInstructionSGetOrSPut(opcode));
187#endif
188 return (code >= Instruction::SPUT) ? SPutMemAccessType(code) : SGetMemAccessType(code);
189}
190
191constexpr DexMemAccessType AGetOrAPutMemAccessType(Instruction::Code code) {
192#if __cplusplus >= 201402 // C++14 allows the DCHECK() in constexpr functions.
193 DCHECK(IsInstructionAGetOrAPut(opcode));
194#endif
195 return (code >= Instruction::APUT) ? APutMemAccessType(code) : AGetMemAccessType(code);
196}
197
198} // namespace art
199
Nicolas Geoffraya5ca8882015-02-24 08:10:57 +0000200#endif // ART_COMPILER_UTILS_DEX_INSTRUCTION_UTILS_H_