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