[llvm] r190388 - [python bindings] Added code to get the length of a memory buffer. Tests are included.

Michael Gottesman mgottesman at apple.com
Mon Sep 9 23:57:57 PDT 2013


Author: mgottesman
Date: Tue Sep 10 01:57:57 2013
New Revision: 190388

URL: http://llvm.org/viewvc/llvm-project?rev=190388&view=rev
Log:
[python bindings] Added code to get the length of a memory buffer. Tests are included.

This is a part of a series of patches that have been sitting fallow on a
personal branch that I have been messing with for a bit.

The patches start to flesh out the python llvm-c wrapper to the point where you can:

1. Load Modules from Bitcode/Dump/Print them.
2. Iterate over Functions from those modules/get their names/dump them.
3. Iterate over the BasicBlocks from said function/get the BB's name/dump it.
4. Iterate over the Instructions in said BasicBlocks/get the instructions
   name/dump the instruction.

My main interest in developing this was to be able to gather statistics about
LLVM IR using python scripts to speed up statistical profiling of different IR
level transformations (hence the focus on printing/dumping/getting names).

This is a gift from me to the LLVM community = ).

I am going to be committing the patches slowly over the next bit as I have time
to prepare the patches.

The overall organization follows the c-api like the bindings that are already
implemented.

Added:
    llvm/trunk/bindings/python/llvm/tests/test_file
Modified:
    llvm/trunk/bindings/python/llvm/core.py
    llvm/trunk/bindings/python/llvm/tests/base.py
    llvm/trunk/bindings/python/llvm/tests/test_core.py

Modified: llvm/trunk/bindings/python/llvm/core.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/python/llvm/core.py?rev=190388&r1=190387&r2=190388&view=diff
==============================================================================
--- llvm/trunk/bindings/python/llvm/core.py (original)
+++ llvm/trunk/bindings/python/llvm/core.py Tue Sep 10 01:57:57 2013
@@ -83,11 +83,17 @@ class MemoryBuffer(LLVMObject):
 
         LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
 
+    def __len__(self):
+        return lib.LLVMGetBufferSize(self)
+
 def register_library(library):
+    # Memory buffer declarations
     library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
             POINTER(c_object_p), POINTER(c_char_p)]
     library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
 
+    library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
+
     library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
 
 def register_enumerations():

Modified: llvm/trunk/bindings/python/llvm/tests/base.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/python/llvm/tests/base.py?rev=190388&r1=190387&r2=190388&view=diff
==============================================================================
--- llvm/trunk/bindings/python/llvm/tests/base.py (original)
+++ llvm/trunk/bindings/python/llvm/tests/base.py Tue Sep 10 01:57:57 2013
@@ -30,3 +30,6 @@ class TestBase(unittest.TestCase):
 
         raise Exception('No suitable test binaries available!')
     get_test_binary.__test__ = False
+
+    def get_test_file(self):
+        return os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_file")

Modified: llvm/trunk/bindings/python/llvm/tests/test_core.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/python/llvm/tests/test_core.py?rev=190388&r1=190387&r2=190388&view=diff
==============================================================================
--- llvm/trunk/bindings/python/llvm/tests/test_core.py (original)
+++ llvm/trunk/bindings/python/llvm/tests/test_core.py Tue Sep 10 01:57:57 2013
@@ -21,3 +21,7 @@ class TestCore(TestBase):
         with self.assertRaises(Exception):
             MemoryBuffer(filename="/hopefully/this/path/doesnt/exist")
 
+    def test_memory_buffer_len(self):
+        source = self.get_test_file()
+        m = MemoryBuffer(filename=source)
+        self.assertEqual(len(m), 50)

Added: llvm/trunk/bindings/python/llvm/tests/test_file
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/python/llvm/tests/test_file?rev=190388&view=auto
==============================================================================
--- llvm/trunk/bindings/python/llvm/tests/test_file (added)
+++ llvm/trunk/bindings/python/llvm/tests/test_file Tue Sep 10 01:57:57 2013
@@ -0,0 +1 @@
+I,"ìcAGðxq‘ÑԐ¹d«±ùà§vl¥À\»L>šg>`ö©ÿ©`‡wÉ©
\ No newline at end of file





More information about the llvm-commits mailing list