diff options
author | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 21:05:05 +0000 |
---|---|---|
committer | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 21:05:05 +0000 |
commit | 0b3aae906fa302bcad0863b92f16aec020db26ad (patch) | |
tree | 57100d6755aac9b6ada58b98bd78fd3e5fb3ef15 /bindings/python/llvm/tests | |
parent | d3ac784fbb1e3cc31f9d8bd702fcda5a63d3ca8e (diff) | |
download | external_llvm-0b3aae906fa302bcad0863b92f16aec020db26ad.zip external_llvm-0b3aae906fa302bcad0863b92f16aec020db26ad.tar.gz external_llvm-0b3aae906fa302bcad0863b92f16aec020db26ad.tar.bz2 |
[llvm.py] Implement interface to enhanced disassembler
This requires a C++ change to EDDisassembler's ctor to function properly
(the llvm::InitializeAll* functions aren't being called currently and
there is no way to call them from Python).
Code is partially tested and works well enough for initial commit. There
are probably many small bugs.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152506 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings/python/llvm/tests')
-rw-r--r-- | bindings/python/llvm/tests/test_disassembler.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/bindings/python/llvm/tests/test_disassembler.py b/bindings/python/llvm/tests/test_disassembler.py new file mode 100644 index 0000000..6eb11a2 --- /dev/null +++ b/bindings/python/llvm/tests/test_disassembler.py @@ -0,0 +1,62 @@ +from unittest import expectedFailure +from unittest import skip + +from .base import TestBase +from ..disassembler import DisassemblerByteArraySource +from ..disassembler import DisassemblerFileSource +from ..disassembler import Disassembler +from ..object import ObjectFile + +class TestDisassembler(TestBase): + def test_simple(self): + sequence = '\x67\xe3\x81' # jcxz -127 + triple = 'i686-apple-darwin9' + + source = DisassemblerByteArraySource(sequence) + + disassembler = Disassembler(triple, source) + instructions = list(disassembler.get_instructions()) + + self.assertEqual(len(instructions), 1) + + i = instructions[0] + self.assertEqual(str(i), '\tjcxz\t-127\n') + self.assertEqual(i.byte_size, 3) + self.assertEqual(i.id, 1032) + self.assertTrue(i.is_branch) + self.assertFalse(i.is_move) + self.assertEqual(i.branch_target_id, 0) + + tokens = list(i.get_tokens()) + self.assertEqual(len(tokens), 4) + token = tokens[0] + self.assertEqual(str(token), 'jcxz') + self.assertFalse(token.is_whitespace) + self.assertFalse(token.is_punctuation) + self.assertTrue(token.is_opcode) + self.assertFalse(token.is_literal) + self.assertFalse(token.is_register) + + self.assertTrue(tokens[1].is_whitespace) + + operands = list(i.get_operands()) + self.assertEqual(len(operands), 1) + + # TODO implement operand tests + + @skip('This test is horribly broken and probably not even correct.') + def test_read_instructions(self): + filename = self.get_test_binary() + o = ObjectFile(filename=filename) + + for symbol in o.get_symbols(): + address = symbol.address + offset = symbol.file_offset + size = symbol.size + + source = DisassemblerFileSource(filename, offset, length=size, + start_address=address) + + disassembler = Disassembler('x86-generic-gnu-linux', source) + for instruction in disassembler.get_instructions(): + print instruction |