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