def cg_load_bitcode_from_file(file_name): try: with open(file_name, "rb") as fin: mod = Module.from_bitcode(fin) except: log.debug("Failed to load bitcode: " + file_name) return None log.debug("Loaded bitcode from file: " + file_name) return mod
def bitey_reconstruct(md, bitcode): name, = md mod = new_module(name) llvm_module = Module.from_bitcode(BytesIO(bitcode)) engine = ExecutionEngine.new(llvm_module) make_all_wrappers(llvm_module, engine, mod) return mod
def wrap_llvm_bitcode(bitcode, py_module): ''' Given a byte-string of LLVM bitcode and a Python module, populate the module with ctypes bindings for public methods in the bitcode. ''' llvm_module = Module.from_bitcode(io.BytesIO(bitcode)) engine = llvm.ee.ExecutionEngine.new(llvm_module) wrap_llvm_module(llvm_module, engine, py_module) setattr(py_module, '_llvm_module', llvm_module) setattr(py_module, '_llvm_engine', engine)
def test_bitcode_roundtrip(self): m = self.create_module() testasm_bc = os.path.join(self.tmpdir, 'testasm.bc') with open(testasm_bc, "wb") as fout: m.to_bitcode(fout) # read it back into a module with open(testasm_bc, "rb") as fin: m2 = Module.from_bitcode(fin) # The default `m.id` is '<string>'. m2.id = m.id # Copy the name from `m` with open(testasm_bc, "rb") as fin: m3 = Module.from_bitcode(fin.read()) # The default `m.id` is '<string>'. m3.id = m.id # Copy the name from `m` self.assertEqual(str(m2).strip(), str(m).strip()) self.assertEqual(str(m3).strip(), str(m).strip())
def test_bitcode(self): # create a module m = Module.new('module1') m.add_global_variable(Type.int(), 'i') # write it's assembly representation to a file asm = str(m) testasm_bc = os.path.join(self.tmpdir, 'testasm.bc') with open(testasm_bc, "wb") as fout: m.to_bitcode(fout) # read it back into a module with open(testasm_bc, "rb") as fin: m2 = Module.from_bitcode(fin) # The default `m.id` is '<string>'. m2.id = m.id # Copy the name from `m` self.assertEqual(str(m2).strip(), asm.strip())
def test_bitcode(self): # create a module m = Module.new("module1") m.add_global_variable(Type.int(), "i") # write it's assembly representation to a file asm = str(m) testasm_bc = os.path.join(self.tmpdir, "testasm.bc") with open(testasm_bc, "wb") as fout: m.to_bitcode(fout) # read it back into a module with open(testasm_bc, "rb") as fin: m2 = Module.from_bitcode(fin) # The default `m.id` is '<string>'. m2.id = m.id # Copy the name from `m` self.assertEqual(str(m2).strip(), asm.strip())