summaryrefslogtreecommitdiff
path: root/bindings/python
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python')
-rw-r--r--bindings/python/llvm/common.py50
-rw-r--r--bindings/python/llvm/disassembler.py11
-rw-r--r--bindings/python/llvm/tests/test_disassembler.py13
3 files changed, 58 insertions, 16 deletions
diff --git a/bindings/python/llvm/common.py b/bindings/python/llvm/common.py
index 0c5fcd03d844..17c22b8ef448 100644
--- a/bindings/python/llvm/common.py
+++ b/bindings/python/llvm/common.py
@@ -12,10 +12,14 @@ from ctypes import c_void_p
from ctypes import cdll
import ctypes.util
+import platform
+
+# LLVM_VERSION: sync with PACKAGE_VERSION in autoconf/configure.ac and CMakeLists.txt
+# but leave out the 'svn' suffix.
+LLVM_VERSION = '3.3'
__all__ = [
'c_object_p',
- 'find_library',
'get_library',
]
@@ -87,20 +91,36 @@ class CachedProperty(object):
return value
-def find_library():
- # FIXME should probably have build system define absolute path of shared
- # library at install time.
- for lib in ['LLVM-3.1svn', 'libLLVM-3.1svn', 'LLVM', 'libLLVM']:
- result = ctypes.util.find_library(lib)
- if result:
- return result
-
- return None
-
def get_library():
"""Obtain a reference to the llvm library."""
- lib = find_library()
- if not lib:
- raise Exception('LLVM shared library not found!')
- return cdll.LoadLibrary(lib)
+ # On Linux, ctypes.cdll.LoadLibrary() respects LD_LIBRARY_PATH
+ # while ctypes.util.find_library() doesn't.
+ # See http://docs.python.org/2/library/ctypes.html#finding-shared-libraries
+ #
+ # To make it possible to run the unit tests without installing the LLVM shared
+ # library into a default linker search path. Always Try ctypes.cdll.LoadLibrary()
+ # with all possible library names first, then try ctypes.util.find_library().
+
+ names = ['LLVM-' + LLVM_VERSION, 'LLVM-' + LLVM_VERSION + 'svn']
+ t = platform.system()
+ if t == 'Darwin':
+ pfx, ext = 'lib', '.dylib'
+ elif t == 'Windows':
+ pfx, ext = '', '.dll'
+ else:
+ pfx, ext = 'lib', '.so'
+
+ for i in names:
+ try:
+ lib = cdll.LoadLibrary(pfx + i + ext)
+ except OSError:
+ pass
+ else:
+ return lib
+
+ for i in names:
+ t = ctypes.util.find_library(i)
+ if t:
+ return cdll.LoadLibrary(t)
+ raise Exception('LLVM shared library not found!')
diff --git a/bindings/python/llvm/disassembler.py b/bindings/python/llvm/disassembler.py
index 5030b989a944..dcef9ac26905 100644
--- a/bindings/python/llvm/disassembler.py
+++ b/bindings/python/llvm/disassembler.py
@@ -31,6 +31,9 @@ __all__ = [
lib = get_library()
callbacks = {}
+# Constants for set_options
+Option_UseMarkup = 1
+
class Disassembler(LLVMObject):
"""Represents a disassembler instance.
@@ -113,6 +116,10 @@ class Disassembler(LLVMObject):
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,
@@ -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,
diff --git a/bindings/python/llvm/tests/test_disassembler.py b/bindings/python/llvm/tests/test_disassembler.py
index 545e8668b6c9..46d12f705626 100644
--- a/bindings/python/llvm/tests/test_disassembler.py
+++ b/bindings/python/llvm/tests/test_disassembler.py
@@ -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):
@@ -26,3 +26,14 @@ class TestDisassembler(TestBase):
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>}')