changeset 70:16a5a8eb1831

Add format_inst() in dalvik_opcodes.py
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 01 Jul 2011 17:56:43 +0800
parents d07fd67e2b08
children 17b9d02a5f3c
files paraspace/dalvik_opcodes.py paraspace/tests/dalvik_opcodes_test.py
diffstat 2 files changed, 59 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/paraspace/dalvik_opcodes.py	Fri Jul 01 16:19:42 2011 +0800
+++ b/paraspace/dalvik_opcodes.py	Fri Jul 01 17:56:43 2011 +0800
@@ -1028,35 +1028,35 @@
 fmt_fmtstr = {
 'kFmtUnknown': '',
 'kFmt10x': '',
-'kFmt12x': 'vA, vB',
-'kFmt11n': 'vA, #+B',
-'kFmt11x': 'vAA',
-'kFmt10t': '+AA',
-'kFmt20t': '+AAAA',
-'kFmt22x': 'vAA, vBBBB',
-'kFmt21t': 'vAA, +BBBB',
-'kFmt21s': 'vAA, #+BBBB',
-'kFmt21h': 'vAA, #+BBBB00000[00000000]',
-'kFmt21c': 'vAA, thing@BBBB',
-'kFmt23x': 'vAA, vBB, vCC',
-'kFmt22b': 'vAA, vBB, #+CC',
-'kFmt22t': 'vA, vB, +CCCC',
-'kFmt22s': 'vA, vB, #+CCCC',
-'kFmt22c': 'vA, vB, thing@CCCC',
-'kFmt22cs': 'vA, vB, field offset CCCC',
-'kFmt32x': 'vAAAA, vBBBB',
-'kFmt30t': '+AAAAAAAA',
-'kFmt31t': 'vAA, +BBBBBBBB',
-'kFmt31i': 'vAA, #+BBBBBBBB',
-'kFmt31c': 'vAA, thing@BBBBBBBB',
-'kFmt35c': '{vC, vD, vE, vF, vG}, thing@BBBB (B: count, A: vG)',
-'kFmt35ms': 'invoke-virtual+super',
+'kFmt12x': 'v{0}, v{1}',
+'kFmt11n': 'v{0}, #+{1}',
+'kFmt11x': 'v{0}',
+'kFmt10t': '+{0}',
+'kFmt20t': '+{0}',
+'kFmt22x': 'v{0}, {1:04x}',
+'kFmt21t': 'v{0}, +{1:04x}',
+'kFmt21s': 'v{0}, #+{1:04x}',
+'kFmt21h': 'v{0}, #+{1:04x}00000[00000000]',
+'kFmt21c': 'v{0}, thing@{:04x}',
+'kFmt23x': 'v{0}, v{1}, v{2}',
+'kFmt22b': 'v{0}, v{1}, #+{2:02x}',
+'kFmt22t': 'v{0}, v{1}, +{2:04x}',
+'kFmt22s': 'v{0}, v{1}, #+{2:04x}',
+'kFmt22c': 'v{0}, v{1}, thing@{2:04x}',
+'kFmt22cs': 'v0{}, v{1}, field offset {2:04x}',
+'kFmt32x': 'v{0:04x}, v{1:04x}',
+'kFmt30t': '+{0:08x}',
+'kFmt31t': 'v{0}, +{1:08x}',
+'kFmt31i': 'v{0}, #+{1:08x}',
+'kFmt31c': 'v{0}, thing@{1:08x}',
+'kFmt35c': '{{v{3}, v{4}, v{5}, v{6}, v{0}}}, thing@{2:04x} ({1}: count, A: vG)',
+'kFmt35ms': '{{v{3}, v{4}, v{5}, v{6}, v{0}}}, thing@{2:04x} ({1}: count, A: vG)',
 'kFmt35fs': 'invoke-interface',
-'kFmt3rc': '{vCCCC .. v(CCCC+AA-1)}, meth@BBBB',
-'kFmt3rms': 'invoke-virtual+super/range',
+'kFmt3rc': '{{v{2:04x} .. v({2:04x}+{0:02x}-1)}}, meth@{1:04x}',
+'kFmt3rms': '{{v{2:04x} .. v({2:04x}+{0:02x}-1)}}, meth@{1:04x}',
 'kFmt3rfs': 'invoke-interface/range',
-'kFmt3inline': 'inline invoke',
-'kFmt51l': 'vAA, #+BBBBBBBBBBBBBBBB'
+'kFmt3inline': '{{v{3}, v{4}, v{5}, v{6}}} thing@{2:04x} ({1}: count)',
+'kFmt51l': 'v{0}, #+{1:016x}'
 }
 
 fmt_parse_cfgs = {
@@ -1199,6 +1199,20 @@
     inst = chr(opcode) + arg_part
     return inst
 
+
+def format_inst(op_vector):
+    opcode, args = op_vector
+
+    opname = opcode_names[opcode]
+
+    fmt = opcode_fmts[opcode]
+    fmt_name = fmt_names[fmt]
+    fmt_str = fmt_fmtstr[fmt_name]
+
+    line = opname + ' ' + fmt_str.format(*args)
+    return line
+
+
 del name
 del names
 del opcode
--- a/paraspace/tests/dalvik_opcodes_test.py	Fri Jul 01 16:19:42 2011 +0800
+++ b/paraspace/tests/dalvik_opcodes_test.py	Fri Jul 01 17:56:43 2011 +0800
@@ -1,4 +1,5 @@
-from paraspace.dalvik_opcodes import decode_inst, encode_inst
+from paraspace.dalvik_opcodes import decode_inst, encode_inst, format_inst
+
 
 def encode_inst_test():
     opv = (0x90, (00, 02, 03))
@@ -15,6 +16,7 @@
     assert insn == '\x6f\x10\x01\xa6\x01\x00'
     pass
 
+
 def decode_inst_test():
     insn = '\x90\00\02\03'
     opv = decode_inst(insn)
@@ -37,3 +39,18 @@
     assert len(args) == 7
     assert args == (0, 1, 0xa601, 1, 0, 0, 0)
     pass
+
+
+def format_inst_test():
+    opv = (0x90, (0, 2, 3))
+    line = format_inst(opv)
+    assert line == 'OP_ADD_INT v0, v2, v3'
+
+    opv = (0x59, (0, 2, 0x0002))
+    line = format_inst(opv)
+    assert line == 'OP_IPUT v0, v2, thing@0002'
+
+    opv = (0x6f, (0, 1, 0xa601, 1, 0, 0, 0))
+    line = format_inst(opv)
+    assert line == 'OP_INVOKE_SUPER {v1, v0, v0, v0, v0}, thing@a601 (1: count, A: vG)'
+    pass