This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
mirrored from https://android.googlesource.com/platform/dalvik.git
-
Notifications
You must be signed in to change notification settings - Fork 262
/
DecodedInstruction.java
488 lines (397 loc) · 13.1 KB
/
DecodedInstruction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.io.instructions;
import com.android.dex.DexException;
import com.android.dx.io.IndexType;
import com.android.dx.io.OpcodeInfo;
import com.android.dx.io.Opcodes;
import com.android.dx.util.Hex;
import java.io.EOFException;
/**
* A decoded Dalvik instruction. This consists of a format codec, a
* numeric opcode, an optional index type, and any additional
* arguments of the instruction. The additional arguments (if any) are
* represented as uninterpreted data.
*
* <p><b>Note:</b> The names of the arguments are <i>not</i> meant to
* match the names given in the Dalvik instruction format
* specification, specification which just names fields (somewhat)
* arbitrarily alphabetically from A. In this class, non-register
* fields are given descriptive names and register fields are
* consistently named alphabetically.</p>
*/
public abstract class DecodedInstruction {
/** non-null; instruction format / codec */
private final InstructionCodec format;
/** opcode number */
private final int opcode;
/** constant index argument */
private final int index;
/** null-ok; index type */
private final IndexType indexType;
/**
* target address argument. This is an absolute address, not just
* a signed offset. <b>Note:</b> The address is unsigned, even
* though it is stored in an {@code int}.
*/
private final int target;
/**
* literal value argument; also used for special verification error
* constants (format 20bc) as well as should-be-zero values
* (formats 10x, 20t, 30t, and 32x)
*/
private final long literal;
/**
* Decodes an instruction from the given input source.
*/
public static DecodedInstruction decode(CodeInput in) throws EOFException {
int opcodeUnit = in.read();
int opcode = Opcodes.extractOpcodeFromUnit(opcodeUnit);
InstructionCodec format = OpcodeInfo.getFormat(opcode);
return format.decode(opcodeUnit, in);
}
/**
* Decodes an array of instructions. The result has non-null
* elements at each offset that represents the start of an
* instruction.
*/
public static DecodedInstruction[] decodeAll(short[] encodedInstructions) {
int size = encodedInstructions.length;
DecodedInstruction[] decoded = new DecodedInstruction[size];
ShortArrayCodeInput in = new ShortArrayCodeInput(encodedInstructions);
try {
while (in.hasMore()) {
decoded[in.cursor()] = DecodedInstruction.decode(in);
}
} catch (EOFException ex) {
throw new DexException(ex);
}
return decoded;
}
/**
* Constructs an instance.
*/
public DecodedInstruction(InstructionCodec format, int opcode,
int index, IndexType indexType, int target, long literal) {
if (format == null) {
throw new NullPointerException("format == null");
}
if (!Opcodes.isValidShape(opcode)) {
throw new IllegalArgumentException("invalid opcode");
}
this.format = format;
this.opcode = opcode;
this.index = index;
this.indexType = indexType;
this.target = target;
this.literal = literal;
}
public final InstructionCodec getFormat() {
return format;
}
public final int getOpcode() {
return opcode;
}
/**
* Gets the opcode, as a code unit.
*/
public final short getOpcodeUnit() {
return (short) opcode;
}
public final int getIndex() {
return index;
}
/**
* Gets the index, as a code unit.
*/
public final short getIndexUnit() {
return (short) index;
}
public final IndexType getIndexType() {
return indexType;
}
/**
* Gets the raw target.
*/
public final int getTarget() {
return target;
}
/**
* Gets the target as a relative offset from the given address.
*/
public final int getTarget(int baseAddress) {
return target - baseAddress;
}
/**
* Gets the target as a relative offset from the given base
* address, as a code unit. This will throw if the value is out of
* the range of a signed code unit.
*/
public final short getTargetUnit(int baseAddress) {
int relativeTarget = getTarget(baseAddress);
if (relativeTarget != (short) relativeTarget) {
throw new DexException("Target out of range: "
+ Hex.s4(relativeTarget));
}
return (short) relativeTarget;
}
/**
* Gets the target as a relative offset from the given base
* address, masked to be a byte in size. This will throw if the
* value is out of the range of a signed byte.
*/
public final int getTargetByte(int baseAddress) {
int relativeTarget = getTarget(baseAddress);
if (relativeTarget != (byte) relativeTarget) {
throw new DexException("Target out of range: "
+ Hex.s4(relativeTarget));
}
return relativeTarget & 0xff;
}
public final long getLiteral() {
return literal;
}
/**
* Gets the literal value, masked to be an int in size. This will
* throw if the value is out of the range of a signed int.
*/
public final int getLiteralInt() {
if (literal != (int) literal) {
throw new DexException("Literal out of range: " + Hex.u8(literal));
}
return (int) literal;
}
/**
* Gets the literal value, as a code unit. This will throw if the
* value is out of the range of a signed code unit.
*/
public final short getLiteralUnit() {
if (literal != (short) literal) {
throw new DexException("Literal out of range: " + Hex.u8(literal));
}
return (short) literal;
}
/**
* Gets the literal value, masked to be a byte in size. This will
* throw if the value is out of the range of a signed byte.
*/
public final int getLiteralByte() {
if (literal != (byte) literal) {
throw new DexException("Literal out of range: " + Hex.u8(literal));
}
return (int) literal & 0xff;
}
/**
* Gets the literal value, masked to be a nibble in size. This
* will throw if the value is out of the range of a signed nibble.
*/
public final int getLiteralNibble() {
if ((literal < -8) || (literal > 7)) {
throw new DexException("Literal out of range: " + Hex.u8(literal));
}
return (int) literal & 0xf;
}
public abstract int getRegisterCount();
public int getA() {
return 0;
}
public int getB() {
return 0;
}
public int getC() {
return 0;
}
public int getD() {
return 0;
}
public int getE() {
return 0;
}
/**
* Gets the register count, as a code unit. This will throw if the
* value is out of the range of an unsigned code unit.
*/
public final short getRegisterCountUnit() {
int registerCount = getRegisterCount();
if ((registerCount & ~0xffff) != 0) {
throw new DexException("Register count out of range: "
+ Hex.u8(registerCount));
}
return (short) registerCount;
}
/**
* Gets the A register number, as a code unit. This will throw if the
* value is out of the range of an unsigned code unit.
*/
public final short getAUnit() {
int a = getA();
if ((a & ~0xffff) != 0) {
throw new DexException("Register A out of range: " + Hex.u8(a));
}
return (short) a;
}
/**
* Gets the A register number, as a byte. This will throw if the
* value is out of the range of an unsigned byte.
*/
public final short getAByte() {
int a = getA();
if ((a & ~0xff) != 0) {
throw new DexException("Register A out of range: " + Hex.u8(a));
}
return (short) a;
}
/**
* Gets the A register number, as a nibble. This will throw if the
* value is out of the range of an unsigned nibble.
*/
public final short getANibble() {
int a = getA();
if ((a & ~0xf) != 0) {
throw new DexException("Register A out of range: " + Hex.u8(a));
}
return (short) a;
}
/**
* Gets the B register number, as a code unit. This will throw if the
* value is out of the range of an unsigned code unit.
*/
public final short getBUnit() {
int b = getB();
if ((b & ~0xffff) != 0) {
throw new DexException("Register B out of range: " + Hex.u8(b));
}
return (short) b;
}
/**
* Gets the B register number, as a byte. This will throw if the
* value is out of the range of an unsigned byte.
*/
public final short getBByte() {
int b = getB();
if ((b & ~0xff) != 0) {
throw new DexException("Register B out of range: " + Hex.u8(b));
}
return (short) b;
}
/**
* Gets the B register number, as a nibble. This will throw if the
* value is out of the range of an unsigned nibble.
*/
public final short getBNibble() {
int b = getB();
if ((b & ~0xf) != 0) {
throw new DexException("Register B out of range: " + Hex.u8(b));
}
return (short) b;
}
/**
* Gets the C register number, as a code unit. This will throw if the
* value is out of the range of an unsigned code unit.
*/
public final short getCUnit() {
int c = getC();
if ((c & ~0xffff) != 0) {
throw new DexException("Register C out of range: " + Hex.u8(c));
}
return (short) c;
}
/**
* Gets the C register number, as a byte. This will throw if the
* value is out of the range of an unsigned byte.
*/
public final short getCByte() {
int c = getC();
if ((c & ~0xff) != 0) {
throw new DexException("Register C out of range: " + Hex.u8(c));
}
return (short) c;
}
/**
* Gets the C register number, as a nibble. This will throw if the
* value is out of the range of an unsigned nibble.
*/
public final short getCNibble() {
int c = getC();
if ((c & ~0xf) != 0) {
throw new DexException("Register C out of range: " + Hex.u8(c));
}
return (short) c;
}
/**
* Gets the D register number, as a code unit. This will throw if the
* value is out of the range of an unsigned code unit.
*/
public final short getDUnit() {
int d = getD();
if ((d & ~0xffff) != 0) {
throw new DexException("Register D out of range: " + Hex.u8(d));
}
return (short) d;
}
/**
* Gets the D register number, as a byte. This will throw if the
* value is out of the range of an unsigned byte.
*/
public final short getDByte() {
int d = getD();
if ((d & ~0xff) != 0) {
throw new DexException("Register D out of range: " + Hex.u8(d));
}
return (short) d;
}
/**
* Gets the D register number, as a nibble. This will throw if the
* value is out of the range of an unsigned nibble.
*/
public final short getDNibble() {
int d = getD();
if ((d & ~0xf) != 0) {
throw new DexException("Register D out of range: " + Hex.u8(d));
}
return (short) d;
}
/**
* Gets the E register number, as a nibble. This will throw if the
* value is out of the range of an unsigned nibble.
*/
public final short getENibble() {
int e = getE();
if ((e & ~0xf) != 0) {
throw new DexException("Register E out of range: " + Hex.u8(e));
}
return (short) e;
}
/**
* Encodes this instance to the given output.
*/
public final void encode(CodeOutput out) {
format.encode(this, out);
}
/**
* Returns an instance just like this one, except with the index replaced
* with the given one.
*/
public abstract DecodedInstruction withIndex(int newIndex);
/** Update the instruction with a new 45cc or 4rcc proto index. */
public DecodedInstruction withProtoIndex(int newIndex, int newProtoIndex) {
throw new IllegalStateException(getClass().toString());
}
/** Returns a 45cc or 4rcc proto index. */
public short getProtoIndex() {
throw new IllegalStateException(getClass().toString());
}
}