Skip to content

Commit 3eda504

Browse files
committed
[DEX] Opcode convenient methods
1 parent c31e7cc commit 3eda504

File tree

4 files changed

+162
-107
lines changed

4 files changed

+162
-107
lines changed

src/main/java/com/reandroid/dex/ins/InsConstCommentHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static TypeKey findDataTypeFromNextInsLazy(SizeXIns constNumberIns) {
9595
}
9696
private static TypeKey findDataTypeFromReturnInsLazy(SizeXIns sizeXIns) {
9797
Opcode<?> opcode = sizeXIns.getOpcode();
98-
if (!opcode.isReturning()) {
98+
if (!opcode.isReturn()) {
9999
return null;
100100
}
101101
MethodDef methodDef = sizeXIns.getMethodDef();

src/main/java/com/reandroid/dex/ins/Opcode.java

Lines changed: 108 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.reandroid.dex.common.OperandType;
2121
import com.reandroid.dex.common.RegisterFormat;
2222
import com.reandroid.dex.id.IdItem;
23+
import com.reandroid.dex.key.TypeKey;
2324
import com.reandroid.dex.sections.SectionType;
2425
import com.reandroid.dex.smali.SmaliFormat;
2526
import com.reandroid.dex.smali.SmaliReader;
@@ -319,7 +320,6 @@ public class Opcode<T extends Ins> implements BlockCreator<T>, SmaliFormat {
319320
OpcodeFormat.Format12x format12xRw4WRead4W = new OpcodeFormat.Format12x(RegisterFormat.RW4W_READ4W);
320321
OpcodeFormat.Format20bc format20bc = new OpcodeFormat.Format20bc();
321322

322-
OpcodeFormat.FormatConst16 formatConst16 = new OpcodeFormat.FormatConst16();
323323
OpcodeFormat.Format21c format21cType = new OpcodeFormat.Format21c(OperandType.TYPE);
324324
OpcodeFormat.Format21c format21cField = new OpcodeFormat.Format21c(OperandType.FIELD);
325325
OpcodeFormat.Format21c format21cFieldRead = new OpcodeFormat.Format21c(RegisterFormat.READ8, OperandType.FIELD);
@@ -964,7 +964,6 @@ private Opcode(int value, int size, String name, OpcodeFormat<T> opcodeFormat) {
964964
this.opcodeFormat = opcodeFormat;
965965
}
966966

967-
968967
public int getValue() {
969968
return value;
970969
}
@@ -975,100 +974,94 @@ public String getName() {
975974
return name;
976975
}
977976
public boolean hasOutRegisters() {
978-
SectionType<?> sectionType = getSectionType();
979-
if (sectionType == SectionType.METHOD_ID || sectionType == SectionType.CALL_SITE_ID) {
980-
return true;
981-
}
982-
return this == FILLED_NEW_ARRAY || this == FILLED_NEW_ARRAY_RANGE;
977+
return getRegisterFormat().isOut();
983978
}
984-
public boolean isFieldAccess() {
985-
return getSectionType() == SectionType.FIELD_ID;
979+
public boolean isConstString() {
980+
int value = this.value;
981+
return value == 0x1a || value == 0x1b;
986982
}
987-
public boolean isFieldGet() {
988-
if (getSectionType() != SectionType.FIELD_ID) {
989-
return false;
990-
}
991-
return getName().charAt(1) == 'g';
983+
public boolean isArrayOp() {
984+
int value = this.value;
985+
return value >= 0x44 && value <= 0x51;
992986
}
993-
public boolean isFieldPut() {
994-
if (getSectionType() != SectionType.FIELD_ID) {
995-
return false;
996-
}
997-
return getName().charAt(1) == 'p';
987+
public boolean isFieldOp() {
988+
int value = this.value;
989+
return value >= 0x52 && value <= 0x6d;
998990
}
999-
public boolean isFieldAccessStatic() {
1000-
if (getSectionType() != SectionType.FIELD_ID) {
1001-
return false;
1002-
}
1003-
return getName().charAt(0) == 's';
991+
public boolean isFieldInstanceOp() {
992+
int value = this.value;
993+
return value >= 0x52 && value <= 0x5f;
1004994
}
1005-
public boolean isFieldAccessInstance() {
1006-
if (getSectionType() != SectionType.FIELD_ID) {
1007-
return false;
1008-
}
1009-
return getName().charAt(0) == 'i';
995+
public boolean isFieldInstanceGet() {
996+
int value = this.value;
997+
return value >= 0x52 && value <= 0x58;
998+
}
999+
public boolean isFieldInstancePut() {
1000+
int value = this.value;
1001+
return value >= 0x59 && value <= 0x5f;
1002+
}
1003+
public boolean isFieldStaticOp() {
1004+
int value = this.value;
1005+
return value >= 0x60 && value <= 0x6d;
10101006
}
10111007
public boolean isFieldStaticGet() {
1012-
if (getSectionType() != SectionType.FIELD_ID) {
1013-
return false;
1014-
}
1015-
String name = getName();
1016-
return name.charAt(0) == 's' && name.charAt(1) == 'g';
1008+
int value = this.value;
1009+
return value >= 0x60 && value <= 0x66;
10171010
}
10181011
public boolean isFieldStaticPut() {
1019-
if (getSectionType() != SectionType.FIELD_ID) {
1020-
return false;
1021-
}
1022-
String name = getName();
1023-
return name.charAt(0) == 's' && name.charAt(1) == 'p';
1012+
int value = this.value;
1013+
return value >= 0x67 && value <= 0x6d;
10241014
}
1025-
public boolean isFieldInstanceGet() {
1026-
if (getSectionType() != SectionType.FIELD_ID) {
1027-
return false;
1028-
}
1029-
String name = getName();
1030-
return name.charAt(0) == 'i' && name.charAt(1) == 'g';
1015+
public boolean isFieldGet() {
1016+
return isFieldInstanceGet() || isFieldStaticGet();
10311017
}
1032-
public boolean isFieldInstancePut() {
1033-
if (getSectionType() != SectionType.FIELD_ID) {
1034-
return false;
1035-
}
1036-
String name = getName();
1037-
return name.charAt(0) == 'i' && name.charAt(1) == 'p';
1018+
public boolean isFieldPut() {
1019+
return isFieldInstancePut() || isFieldStaticPut();
10381020
}
10391021
public boolean isMethodInvoke() {
1040-
return getSectionType() == SectionType.METHOD_ID;
1022+
return getOperandType() == OperandType.METHOD;
10411023
}
10421024
public boolean isMethodInvokeStatic() {
1043-
if (getSectionType() != SectionType.METHOD_ID) {
1044-
return false;
1045-
}
1046-
return getName().charAt(8) == 't';
1025+
int value = this.value;
1026+
return value == 0x71 || value == 0x77;
1027+
}
1028+
public boolean isConstNumber() {
1029+
int value = this.value;
1030+
return value >= 0x12 && value <= 0x19;
1031+
}
1032+
public boolean isConstInteger() {
1033+
int value = this.value;
1034+
return value >= 0x12 && value <= 0x15;
10471035
}
1048-
public boolean isReturning() {
1049-
String name = getName();
1050-
return name.charAt(0) == 'r' &&
1051-
name.charAt(2) == 't';
1036+
public boolean isConstWide() {
1037+
int value = this.value;
1038+
return value >= 0x16 && value <= 0x19;
10521039
}
1053-
public boolean isMoveResultValue() {
1054-
String name = getName();
1055-
if (name.length() < 6) {
1056-
return false;
1040+
public boolean isReturn() {
1041+
int value = this.value;
1042+
if (value >= 0x0e && value <= 0x11) {
1043+
return true;
10571044
}
1058-
return name.charAt(0) == 'm' &&
1059-
name.charAt(5) == 'r';
1045+
return value == 0xf1 || value == 0x73;
1046+
}
1047+
public boolean isMoveResult() {
1048+
int value = this.value;
1049+
return value == 0x0a || value == 0x0b || value == 0x0c;
1050+
}
1051+
public boolean isMove() {
1052+
int value = this.value;
1053+
return value >= 0x1 && value <= 0x9;
10601054
}
1061-
public boolean isMover() {
1062-
Opcode<?> opcode = this;
1063-
return opcode == MOVE ||
1064-
opcode == MOVE_16 ||
1065-
opcode == MOVE_FROM16 ||
1066-
opcode == MOVE_OBJECT ||
1067-
opcode == MOVE_OBJECT_16 ||
1068-
opcode == MOVE_OBJECT_FROM16 ||
1069-
opcode == MOVE_WIDE ||
1070-
opcode == MOVE_WIDE_16 ||
1071-
opcode == MOVE_WIDE_FROM16;
1055+
public boolean isIfTest() {
1056+
int value = this.value;
1057+
return value >= 0x32 && value <= 0x3d;
1058+
}
1059+
public boolean isGoto() {
1060+
int value = this.value;
1061+
return value == 0x28 || value == 0x29 || value == 0x2a;
1062+
}
1063+
public boolean isRange() {
1064+
return getRegisterFormat().isRange();
10721065
}
10731066
public SectionType<? extends IdItem> getSectionType() {
10741067
return opcodeFormat.getSectionType();
@@ -1157,7 +1150,7 @@ public static Opcode<?> parse(int start, String smali) {
11571150
}
11581151
return valueOf(smali.substring(i1, i2));
11591152
}
1160-
public static boolean isPrefix(byte b) {
1153+
private static boolean isPrefix(byte b) {
11611154
switch (b) {
11621155
case 'a':
11631156
case 'c':
@@ -1181,4 +1174,41 @@ public static boolean isPrefix(byte b) {
11811174
return false;
11821175
}
11831176
}
1177+
1178+
public static Opcode<?> getConstIntegerFor(int i) {
1179+
if (i >= -0x8 && i <= 0x7) {
1180+
return CONST_4;
1181+
}
1182+
if (i >= -0x8000 && i <= 0x7fff) {
1183+
return CONST_16;
1184+
}
1185+
if ((i & 0x0000ffff) == 0) {
1186+
return CONST_HIGH16;
1187+
}
1188+
return CONST;
1189+
}
1190+
public static Opcode<?> getConstWideFor(long l) {
1191+
if (l >= -0x8000 && l <= 0x7fff) {
1192+
return CONST_WIDE_16;
1193+
}
1194+
if ((l & 0x0000ffffffffffffL) == 0) {
1195+
return CONST_WIDE_HIGH16;
1196+
}
1197+
if (l >= -0x80000000L && l <= 0x7fffffffL) {
1198+
return CONST_WIDE_32;
1199+
}
1200+
return CONST_WIDE;
1201+
}
1202+
public static Opcode<?> getReturnForType(TypeKey typeKey) {
1203+
if (!typeKey.isPrimitive()) {
1204+
return RETURN_OBJECT;
1205+
}
1206+
if (TypeKey.TYPE_V.equals(typeKey)) {
1207+
return RETURN_VOID;
1208+
}
1209+
if (typeKey.isWide()) {
1210+
return RETURN_WIDE;
1211+
}
1212+
return RETURN;
1213+
}
11841214
}

src/main/java/com/reandroid/dex/ins/RegistersSet.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,29 @@ default boolean removeRegisterAt(int index) {
6262
setRegistersCount(last);
6363
return true;
6464
}
65+
66+
RegistersSet NO_REGISTERS = new RegistersSet() {
67+
@Override
68+
public int getRegistersCount() {
69+
return 0;
70+
}
71+
@Override
72+
public void setRegistersCount(int count) {
73+
}
74+
@Override
75+
public int getRegister(int index) {
76+
return -1;
77+
}
78+
@Override
79+
public void setRegister(int index, int value) {
80+
}
81+
@Override
82+
public int getRegister() {
83+
return -1;
84+
}
85+
@Override
86+
public RegisterFormat getRegisterFormat() {
87+
return RegisterFormat.NONE;
88+
}
89+
};
6590
}

0 commit comments

Comments
 (0)