Skip to content

Commit

Permalink
[python] Add markup option to disassembler
Browse files Browse the repository at this point in the history
Patch contributed by Wladimir J. van der Laan <[email protected]>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169102 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
indygreg committed Dec 1, 2012
1 parent 9756ca7 commit fdddf77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions bindings/python/llvm/disassembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
lib = get_library()
callbacks = {}

# Constants for set_options
Option_UseMarkup = 1

class Disassembler(LLVMObject):
"""Represents a disassembler instance.
Expand Down Expand Up @@ -113,6 +116,10 @@ def get_instructions(self, source, pc=0):
address += result
offset += result

def set_options(self, options):
if not lib.LLVMSetDisasmOptions(self, options):
raise Exception('Unable to set all disassembler options in %i' % options)


def register_library(library):
library.LLVMCreateDisasm.argtypes = [c_char_p, c_void_p, c_int,
Expand All @@ -125,6 +132,10 @@ def register_library(library):
c_uint64, c_uint64, c_char_p, c_size_t]
library.LLVMDisasmInstruction.restype = c_size_t

library.LLVMSetDisasmOptions.argtypes = [Disassembler, c_uint64]
library.LLVMSetDisasmOptions.restype = c_int


callbacks['op_info'] = CFUNCTYPE(c_int, c_void_p, c_uint64, c_uint64, c_uint64,
c_int, c_void_p)
callbacks['symbol_lookup'] = CFUNCTYPE(c_char_p, c_void_p, c_uint64,
Expand Down
13 changes: 12 additions & 1 deletion bindings/python/llvm/tests/test_disassembler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .base import TestBase

from ..disassembler import Disassembler
from ..disassembler import Disassembler, Option_UseMarkup

class TestDisassembler(TestBase):
def test_instantiate(self):
Expand All @@ -26,3 +26,14 @@ def test_get_instructions(self):

self.assertEqual(instructions[0], (0, 3, '\tjcxz\t-127'))
self.assertEqual(instructions[1], (3, 2, '\taddl\t%eax, %edi'))

def test_set_options(self):
sequence = '\x10\x40\x2d\xe9'
triple = 'arm-linux-android'

disassembler = Disassembler(triple)
disassembler.set_options(Option_UseMarkup)
count, s = disassembler.get_instruction(sequence)
print s
self.assertEqual(count, 4)
self.assertEqual(s, '\tpush\t{<reg:r4>, <reg:lr>}')

0 comments on commit fdddf77

Please sign in to comment.