Code Execution

0 views
Skip to first unread message

Samaneh Berenjian

unread,
Jun 14, 2017, 4:36:39 PM6/14/17
to numba...@continuum.io
Dear all,

I have written the following code in the form of LLVM IR, but I have a problem for getting output from it.

import llvmlite.binding as llvm
import pyvex

CODE = b"\x55\x48\x8b\x05\xb8\x13\x00\x00"
mehran = -100

#llvm.initialize()
#llvm.initialize_native_target()
#llvm.initialize_native_asmprinter()

module = ll.Module()
func_ty = ll.FunctionType(ll.VoidType(), [])
func = ll.Function(module, func_ty, name='read_instructions')

bb_entry = func.append_basic_block('entry')
irbuilder = ll.IRBuilder(bb_entry)

irsb = pyvex.block.IRSB(CODE, 0x400400, archinfo.ArchAMD64())

for stmt in irsb.statements:

    if isinstance(stmt, pyvex.IRStmt.Store):
       result =  irbuilder.store_reg(ll.Constant(ll.IntType(64), stmt.data), ll.IntType(64), stmt.addr)
       irbuilde.ret(result)

print("****************LLVM***********************")
print(module)

#Execute generated code.
"""
# initialize the LLVM machine
# These are all required (apparently)
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()

# Create engine and attach the generated module
# Create a target machine representing the host
target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
# And an execution engine with an empty backing module
backing_mod = llvm.parse_assembly("")
engine = llvm.create_mcjit_compiler(backing_mod, target_machine)

# Parse our generated module
mod = llvm.parse_assembly( str( module ) )
mod.verify()
# Now add the module and make sure it is ready for execution
engine.add_module(mod)
engine.finalize_object()

func = engine.get_function_address('read_instructions')

Actually, I want this code to be compiled and executed and print out the output from:
       result =  irbuilder.store_reg(ll.Constant(ll.IntType(64), stmt.data), ll.IntType(64), stmt.addr)
Who knows how can I add lines for getting output for it?

--
Best Regards
Samaneh Berenjian
Reply all
Reply to author
Forward
0 new messages