# Buggy code
def printf(self, format, *args):
    """
    Inspired from numba/cgutils.py

    Calls printf().
    Argument `format` is expected to be a Python string.
    Values to be printed are listed in `args`.

    Note: There is no checking to ensure there is correct number of values
    in `args` and there type matches the declaration in the format string.
    """
    assert isinstance(format, str)
    mod = self.mod
    # Make global constant for format string
    cstring = llvm_ir.IntType(8).as_pointer()
    fmt_bytes = self.make_bytearray((format + "\00").encode("ascii"))

    base_name = "printf_format"
    count = 0
    while self.mod.get_global("%s_%d" % (base_name, count)):
        count += 1
    global_fmt = self.global_constant("%s_%d" % (base_name, count), fmt_bytes)
    fnty = llvm_ir.FunctionType(llvm_ir.IntType(32), [cstring], var_arg=True)
    # Insert printf()
    fn = mod.get_global("printf")
    if fn is None:
        fn = llvm_ir.Function(mod, fnty, name="printf")
    # Call
    ptr_fmt = self.builder.bitcast(global_fmt, cstring)
    return self.builder.call(fn, [ptr_fmt] + list(args))


# Traceback (KeyError)
TEST/ANALYSIS dse.py llvm
ERROR dse.py llvm
Traceback (most recent call last):
File "dse.py", line 157, in <module>
test(jit_engine)()
File "dse.py", line 61, in __call__
self.run()
File "dse.py", line 67, in run
self.myjit.continue_run()
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitload.py", line 369, in continue_run
return self.run_iterator.next()
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitload.py", line 340, in runiter_once
self.pc = self.runbloc(self.pc)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitload.py", line 297, in runbloc
return self.jit.runbloc(self.cpu, pc, self.breakpoints_handler.callbacks)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitcore.py", line 180, in runbloc
self.disbloc(lbl, cpu.vmmngr)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitcore.py", line 164, in disbloc
self.add_bloc(cur_block)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/jitcore_llvm.py", line 92, in add_bloc
func.from_asmblock(block)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/llvmconvert.py", line 1344, in from_asmblock
self.gen_pre_code(instr_attrib)
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/llvmconvert.py", line 1026, in gen_pre_code
instr_attrib.instr))
File "/usr/local/lib/python2.7/dist-packages/miasm2/jitter/llvmconvert.py", line 574, in printf
while self.mod.get_global("%s_%d" % (base_name, count)):
File "/usr/local/lib/python2.7/dist-packages/llvmlite/ir/module.py", line 138, in get_global
return self.globals[name]
KeyError: 'printf_format_0'

# Fixed code
def printf(self, format, *args):
    """
    Inspired from numba/cgutils.py

    Calls printf().
    Argument `format` is expected to be a Python string.
    Values to be printed are listed in `args`.

    Note: There is no checking to ensure there is correct number of values
    in `args` and there type matches the declaration in the format string.
    """
    assert isinstance(format, str)
    mod = self.mod
    # Make global constant for format string
    cstring = llvm_ir.IntType(8).as_pointer()
    fmt_bytes = self.make_bytearray((format + "\00").encode("ascii"))

    base_name = "printf_format"
    count = 0
    while "%s_%d" % (base_name, count) in self.mod.globals:
        count += 1
    global_fmt = self.global_constant("%s_%d" % (base_name, count), fmt_bytes)
    fnty = llvm_ir.FunctionType(llvm_ir.IntType(32), [cstring], var_arg=True)
    # Insert printf()
    fn = mod.globals.get("printf", None)
    if fn is None:
        fn = llvm_ir.Function(mod, fnty, name="printf")
    # Call
    ptr_fmt = self.builder.bitcast(global_fmt, cstring)
    return self.builder.call(fn, [ptr_fmt] + list(args))