• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 package com.android.dx.io;
18 
19 /**
20  * All the Dalvik opcode value constants.
21  *
22  * @see <a href="https://source.android.com/devices/tech/dalvik/dalvik-bytecode">Dalvik bytecode</a>
23  */
24 public final class Opcodes {
25     /**
26      * pseudo-opcode used for nonstandard format payload "instructions". TODO:
27      * Retire this concept, and start treating the payload instructions
28      * more like the rest.
29      */
30     public static final int SPECIAL_FORMAT = -1;
31 
32     /**
33      * pseudo-opcode used to indicate there is no next opcode; used
34      * in opcode chaining lists
35      */
36     public static final int NO_NEXT = -1;
37 
38     /** minimum valid opcode value */
39     public static final int MIN_VALUE = -1;
40 
41     /** maximum valid opcode value */
42     public static final int MAX_VALUE = 0xffff;
43 
44     // BEGIN(opcodes); GENERATED AUTOMATICALLY BY opcode-gen
45     public static final int NOP = 0x00;
46     public static final int MOVE = 0x01;
47     public static final int MOVE_FROM16 = 0x02;
48     public static final int MOVE_16 = 0x03;
49     public static final int MOVE_WIDE = 0x04;
50     public static final int MOVE_WIDE_FROM16 = 0x05;
51     public static final int MOVE_WIDE_16 = 0x06;
52     public static final int MOVE_OBJECT = 0x07;
53     public static final int MOVE_OBJECT_FROM16 = 0x08;
54     public static final int MOVE_OBJECT_16 = 0x09;
55     public static final int MOVE_RESULT = 0x0a;
56     public static final int MOVE_RESULT_WIDE = 0x0b;
57     public static final int MOVE_RESULT_OBJECT = 0x0c;
58     public static final int MOVE_EXCEPTION = 0x0d;
59     public static final int RETURN_VOID = 0x0e;
60     public static final int RETURN = 0x0f;
61     public static final int RETURN_WIDE = 0x10;
62     public static final int RETURN_OBJECT = 0x11;
63     public static final int CONST_4 = 0x12;
64     public static final int CONST_16 = 0x13;
65     public static final int CONST = 0x14;
66     public static final int CONST_HIGH16 = 0x15;
67     public static final int CONST_WIDE_16 = 0x16;
68     public static final int CONST_WIDE_32 = 0x17;
69     public static final int CONST_WIDE = 0x18;
70     public static final int CONST_WIDE_HIGH16 = 0x19;
71     public static final int CONST_STRING = 0x1a;
72     public static final int CONST_STRING_JUMBO = 0x1b;
73     public static final int CONST_CLASS = 0x1c;
74     public static final int MONITOR_ENTER = 0x1d;
75     public static final int MONITOR_EXIT = 0x1e;
76     public static final int CHECK_CAST = 0x1f;
77     public static final int INSTANCE_OF = 0x20;
78     public static final int ARRAY_LENGTH = 0x21;
79     public static final int NEW_INSTANCE = 0x22;
80     public static final int NEW_ARRAY = 0x23;
81     public static final int FILLED_NEW_ARRAY = 0x24;
82     public static final int FILLED_NEW_ARRAY_RANGE = 0x25;
83     public static final int FILL_ARRAY_DATA = 0x26;
84     public static final int THROW = 0x27;
85     public static final int GOTO = 0x28;
86     public static final int GOTO_16 = 0x29;
87     public static final int GOTO_32 = 0x2a;
88     public static final int PACKED_SWITCH = 0x2b;
89     public static final int SPARSE_SWITCH = 0x2c;
90     public static final int CMPL_FLOAT = 0x2d;
91     public static final int CMPG_FLOAT = 0x2e;
92     public static final int CMPL_DOUBLE = 0x2f;
93     public static final int CMPG_DOUBLE = 0x30;
94     public static final int CMP_LONG = 0x31;
95     public static final int IF_EQ = 0x32;
96     public static final int IF_NE = 0x33;
97     public static final int IF_LT = 0x34;
98     public static final int IF_GE = 0x35;
99     public static final int IF_GT = 0x36;
100     public static final int IF_LE = 0x37;
101     public static final int IF_EQZ = 0x38;
102     public static final int IF_NEZ = 0x39;
103     public static final int IF_LTZ = 0x3a;
104     public static final int IF_GEZ = 0x3b;
105     public static final int IF_GTZ = 0x3c;
106     public static final int IF_LEZ = 0x3d;
107     public static final int AGET = 0x44;
108     public static final int AGET_WIDE = 0x45;
109     public static final int AGET_OBJECT = 0x46;
110     public static final int AGET_BOOLEAN = 0x47;
111     public static final int AGET_BYTE = 0x48;
112     public static final int AGET_CHAR = 0x49;
113     public static final int AGET_SHORT = 0x4a;
114     public static final int APUT = 0x4b;
115     public static final int APUT_WIDE = 0x4c;
116     public static final int APUT_OBJECT = 0x4d;
117     public static final int APUT_BOOLEAN = 0x4e;
118     public static final int APUT_BYTE = 0x4f;
119     public static final int APUT_CHAR = 0x50;
120     public static final int APUT_SHORT = 0x51;
121     public static final int IGET = 0x52;
122     public static final int IGET_WIDE = 0x53;
123     public static final int IGET_OBJECT = 0x54;
124     public static final int IGET_BOOLEAN = 0x55;
125     public static final int IGET_BYTE = 0x56;
126     public static final int IGET_CHAR = 0x57;
127     public static final int IGET_SHORT = 0x58;
128     public static final int IPUT = 0x59;
129     public static final int IPUT_WIDE = 0x5a;
130     public static final int IPUT_OBJECT = 0x5b;
131     public static final int IPUT_BOOLEAN = 0x5c;
132     public static final int IPUT_BYTE = 0x5d;
133     public static final int IPUT_CHAR = 0x5e;
134     public static final int IPUT_SHORT = 0x5f;
135     public static final int SGET = 0x60;
136     public static final int SGET_WIDE = 0x61;
137     public static final int SGET_OBJECT = 0x62;
138     public static final int SGET_BOOLEAN = 0x63;
139     public static final int SGET_BYTE = 0x64;
140     public static final int SGET_CHAR = 0x65;
141     public static final int SGET_SHORT = 0x66;
142     public static final int SPUT = 0x67;
143     public static final int SPUT_WIDE = 0x68;
144     public static final int SPUT_OBJECT = 0x69;
145     public static final int SPUT_BOOLEAN = 0x6a;
146     public static final int SPUT_BYTE = 0x6b;
147     public static final int SPUT_CHAR = 0x6c;
148     public static final int SPUT_SHORT = 0x6d;
149     public static final int INVOKE_VIRTUAL = 0x6e;
150     public static final int INVOKE_SUPER = 0x6f;
151     public static final int INVOKE_DIRECT = 0x70;
152     public static final int INVOKE_STATIC = 0x71;
153     public static final int INVOKE_INTERFACE = 0x72;
154     public static final int INVOKE_VIRTUAL_RANGE = 0x74;
155     public static final int INVOKE_SUPER_RANGE = 0x75;
156     public static final int INVOKE_DIRECT_RANGE = 0x76;
157     public static final int INVOKE_STATIC_RANGE = 0x77;
158     public static final int INVOKE_INTERFACE_RANGE = 0x78;
159     public static final int NEG_INT = 0x7b;
160     public static final int NOT_INT = 0x7c;
161     public static final int NEG_LONG = 0x7d;
162     public static final int NOT_LONG = 0x7e;
163     public static final int NEG_FLOAT = 0x7f;
164     public static final int NEG_DOUBLE = 0x80;
165     public static final int INT_TO_LONG = 0x81;
166     public static final int INT_TO_FLOAT = 0x82;
167     public static final int INT_TO_DOUBLE = 0x83;
168     public static final int LONG_TO_INT = 0x84;
169     public static final int LONG_TO_FLOAT = 0x85;
170     public static final int LONG_TO_DOUBLE = 0x86;
171     public static final int FLOAT_TO_INT = 0x87;
172     public static final int FLOAT_TO_LONG = 0x88;
173     public static final int FLOAT_TO_DOUBLE = 0x89;
174     public static final int DOUBLE_TO_INT = 0x8a;
175     public static final int DOUBLE_TO_LONG = 0x8b;
176     public static final int DOUBLE_TO_FLOAT = 0x8c;
177     public static final int INT_TO_BYTE = 0x8d;
178     public static final int INT_TO_CHAR = 0x8e;
179     public static final int INT_TO_SHORT = 0x8f;
180     public static final int ADD_INT = 0x90;
181     public static final int SUB_INT = 0x91;
182     public static final int MUL_INT = 0x92;
183     public static final int DIV_INT = 0x93;
184     public static final int REM_INT = 0x94;
185     public static final int AND_INT = 0x95;
186     public static final int OR_INT = 0x96;
187     public static final int XOR_INT = 0x97;
188     public static final int SHL_INT = 0x98;
189     public static final int SHR_INT = 0x99;
190     public static final int USHR_INT = 0x9a;
191     public static final int ADD_LONG = 0x9b;
192     public static final int SUB_LONG = 0x9c;
193     public static final int MUL_LONG = 0x9d;
194     public static final int DIV_LONG = 0x9e;
195     public static final int REM_LONG = 0x9f;
196     public static final int AND_LONG = 0xa0;
197     public static final int OR_LONG = 0xa1;
198     public static final int XOR_LONG = 0xa2;
199     public static final int SHL_LONG = 0xa3;
200     public static final int SHR_LONG = 0xa4;
201     public static final int USHR_LONG = 0xa5;
202     public static final int ADD_FLOAT = 0xa6;
203     public static final int SUB_FLOAT = 0xa7;
204     public static final int MUL_FLOAT = 0xa8;
205     public static final int DIV_FLOAT = 0xa9;
206     public static final int REM_FLOAT = 0xaa;
207     public static final int ADD_DOUBLE = 0xab;
208     public static final int SUB_DOUBLE = 0xac;
209     public static final int MUL_DOUBLE = 0xad;
210     public static final int DIV_DOUBLE = 0xae;
211     public static final int REM_DOUBLE = 0xaf;
212     public static final int ADD_INT_2ADDR = 0xb0;
213     public static final int SUB_INT_2ADDR = 0xb1;
214     public static final int MUL_INT_2ADDR = 0xb2;
215     public static final int DIV_INT_2ADDR = 0xb3;
216     public static final int REM_INT_2ADDR = 0xb4;
217     public static final int AND_INT_2ADDR = 0xb5;
218     public static final int OR_INT_2ADDR = 0xb6;
219     public static final int XOR_INT_2ADDR = 0xb7;
220     public static final int SHL_INT_2ADDR = 0xb8;
221     public static final int SHR_INT_2ADDR = 0xb9;
222     public static final int USHR_INT_2ADDR = 0xba;
223     public static final int ADD_LONG_2ADDR = 0xbb;
224     public static final int SUB_LONG_2ADDR = 0xbc;
225     public static final int MUL_LONG_2ADDR = 0xbd;
226     public static final int DIV_LONG_2ADDR = 0xbe;
227     public static final int REM_LONG_2ADDR = 0xbf;
228     public static final int AND_LONG_2ADDR = 0xc0;
229     public static final int OR_LONG_2ADDR = 0xc1;
230     public static final int XOR_LONG_2ADDR = 0xc2;
231     public static final int SHL_LONG_2ADDR = 0xc3;
232     public static final int SHR_LONG_2ADDR = 0xc4;
233     public static final int USHR_LONG_2ADDR = 0xc5;
234     public static final int ADD_FLOAT_2ADDR = 0xc6;
235     public static final int SUB_FLOAT_2ADDR = 0xc7;
236     public static final int MUL_FLOAT_2ADDR = 0xc8;
237     public static final int DIV_FLOAT_2ADDR = 0xc9;
238     public static final int REM_FLOAT_2ADDR = 0xca;
239     public static final int ADD_DOUBLE_2ADDR = 0xcb;
240     public static final int SUB_DOUBLE_2ADDR = 0xcc;
241     public static final int MUL_DOUBLE_2ADDR = 0xcd;
242     public static final int DIV_DOUBLE_2ADDR = 0xce;
243     public static final int REM_DOUBLE_2ADDR = 0xcf;
244     public static final int ADD_INT_LIT16 = 0xd0;
245     public static final int RSUB_INT = 0xd1;
246     public static final int MUL_INT_LIT16 = 0xd2;
247     public static final int DIV_INT_LIT16 = 0xd3;
248     public static final int REM_INT_LIT16 = 0xd4;
249     public static final int AND_INT_LIT16 = 0xd5;
250     public static final int OR_INT_LIT16 = 0xd6;
251     public static final int XOR_INT_LIT16 = 0xd7;
252     public static final int ADD_INT_LIT8 = 0xd8;
253     public static final int RSUB_INT_LIT8 = 0xd9;
254     public static final int MUL_INT_LIT8 = 0xda;
255     public static final int DIV_INT_LIT8 = 0xdb;
256     public static final int REM_INT_LIT8 = 0xdc;
257     public static final int AND_INT_LIT8 = 0xdd;
258     public static final int OR_INT_LIT8 = 0xde;
259     public static final int XOR_INT_LIT8 = 0xdf;
260     public static final int SHL_INT_LIT8 = 0xe0;
261     public static final int SHR_INT_LIT8 = 0xe1;
262     public static final int USHR_INT_LIT8 = 0xe2;
263     public static final int INVOKE_POLYMORPHIC = 0xfa;
264     public static final int INVOKE_POLYMORPHIC_RANGE = 0xfb;
265     public static final int INVOKE_CUSTOM = 0xfc;
266     public static final int INVOKE_CUSTOM_RANGE = 0xfd;
267     public static final int CONST_METHOD_HANDLE = 0xfe;
268     public static final int CONST_METHOD_TYPE = 0xff;
269     // END(opcodes)
270 
271     // TODO: Generate these payload opcodes with opcode-gen.
272 
273     /**
274      * special pseudo-opcode value for packed-switch data payload
275      * instructions
276      */
277     public static final int PACKED_SWITCH_PAYLOAD = 0x100;
278 
279     /** special pseudo-opcode value for packed-switch data payload
280      * instructions
281      */
282     public static final int SPARSE_SWITCH_PAYLOAD = 0x200;
283 
284     /** special pseudo-opcode value for fill-array-data data payload
285      * instructions
286      */
287     public static final int FILL_ARRAY_DATA_PAYLOAD = 0x300;
288 
289     /**
290      * This class is uninstantiable.
291      */
Opcodes()292     private Opcodes() {
293         // This space intentionally left blank.
294     }
295 
296     /**
297      * Determines if the given opcode has the right "shape" to be
298      * valid. This includes the range {@code 0x01..0xfe}, the range
299      * {@code 0x00ff..0xffff} where the low-order byte is either
300      * {@code 0} or {@code 0xff}, and the special opcode values {@code
301      * SPECIAL_FORMAT} and {@code NO_NEXT}. Note that not all of the
302      * opcode values that pass this test are in fact used. This method
303      * is meant to perform a quick check to reject blatantly wrong
304      * values (e.g. when validating arguments).
305      *
306      * @param opcode the opcode value
307      * @return {@code true} iff the value has the right "shape" to be
308      * possibly valid
309      */
isValidShape(int opcode)310     public static boolean isValidShape(int opcode) {
311         /*
312          * Note: This method bakes in knowledge that all opcodes are
313          * one of the forms:
314          *
315          *   * single byte in range 0x01..0xfe -- normal opcodes
316          *   * (byteValue << 8) -- nop and data payload opcodes
317          *   * ((byteValue << 8) | 0xff) -- 16-bit extended opcodes
318          *   * SPECIAL_FORMAT or NO_NEXT -- pseudo-opcodes
319          */
320 
321         // Note: SPECIAL_FORMAT == NO_NEXT.
322         if (opcode < SPECIAL_FORMAT) {
323             return false;
324         } else if (opcode == SPECIAL_FORMAT) {
325             return true;
326         }
327 
328         int lowByte = opcode & 0xff;
329         if ((lowByte == 0) || (lowByte == 0xff)) {
330             return true;
331         }
332 
333         return (opcode & 0xff00) == 0;
334     }
335 
336     /**
337      * Gets the opcode out of an opcode unit, the latter of which may also
338      * include one or more argument values.
339      *
340      * @param opcodeUnit the opcode-containing code unit
341      * @return the extracted opcode
342      */
extractOpcodeFromUnit(int opcodeUnit)343     public static int extractOpcodeFromUnit(int opcodeUnit) {
344         /*
345          * Note: This method bakes in knowledge that all opcodes are
346          * either single-byte or of the forms (byteValue << 8) or
347          * ((byteValue << 8) | 0xff).
348          */
349 
350         int lowByte = opcodeUnit & 0xff;
351         return ((lowByte == 0) || (lowByte == 0xff)) ? opcodeUnit : lowByte;
352     }
353 }
354