Exemple #1
0
def execute(ir_mod):
    llvm.initialize()
    llvm.initialize_native_target()
    llvm.initialize_native_asmprinter()

    llmod = llvm.parse_assembly(str(ir_mod))

    print('optimized'.center(80, '-'))
    pmb = llvm.create_pass_manager_builder()
    pmb.opt_level = 1
    pm = llvm.create_module_pass_manager()
    pmb.populate(pm)
    pm.run(llmod)
    print(llmod)

    target_machine = llvm.Target.from_default_triple().create_target_machine()

    with llvm.create_mcjit_compiler(llmod, target_machine) as ee:
        ee.finalize_object()
        cfptr = ee.get_function_address("entry_fib")

        from ctypes import CFUNCTYPE, c_int

        cfunc = CFUNCTYPE(c_int, c_int)(cfptr)

        # TEST
        for i in range(12):
            res = cfunc(i)
            print('fib({}) = {}'.format(i, res))

        # Get CFG
        ll_fib_more = llmod.get_function('fib_more')
        cfg = llvm.get_function_cfg(ll_fib_more)
        llvm.view_dot_graph(cfg, view=True)
    def exitProgram(self, ctx):
        print "* Target cpu: " + llvm.get_host_cpu_name()
        programAst = ProgramAST()
        for child in ctx.getChildren():
            child_ast = self.prop[child]
            programAst.asts.append(child_ast) 
        mod, cfg_list  = programAst.codeGenerate(self.var_ptr_symbolTBL)
        strmod = str(mod)
        print "=== Generated IR code ===\n"
        print strmod
        with open("output.ll", 'w') as f:
            f.write(strmod)
 
        llmod = llvm.parse_assembly(strmod)
        answer = raw_input('* Optimizing this code? (y/n): ')
        if answer.lower() == "y":
            opt = True
        else:
            opt = False

        if opt:
            pm = llvm.create_module_pass_manager()
            pmb = llvm.create_pass_manager_builder()
            pmb.opt_level = 3  # -O3
            pmb.populate(pm)
            # optimize
            pm.run(llmod)
            print "=== Generated optimized IR code ===\n"
            print llmod
            with open("output_opt.ll", 'w') as f:
                f.write(str(llmod))


        llmod.verify()
        with llvm.create_mcjit_compiler(llmod, self.tm) as ee:
            ee.finalize_object()
            print "=== Generated assembly code ===\n"
            print(self.tm.emit_assembly(llmod))
            with open("output.asm", 'w') as f:
                f.write(self.tm.emit_assembly(llmod))
        answer = raw_input('Do you want to create CFG Graph? (y/n) : ')
        if answer.lower() == 'y': 
            for cfg in cfg_list:
                dot = llvm.get_function_cfg(cfg)
                llvm.view_dot_graph(dot ,filename=cfg.name,view = True)
Exemple #3
0
    def exitProgram(self, ctx):
        print "* Target cpu: " + llvm.get_host_cpu_name()
        programAst = ProgramAST()
        for child in ctx.getChildren():
            child_ast = self.prop[child]
            programAst.asts.append(child_ast)
        mod, cfg_list = programAst.codeGenerate(self.var_ptr_symbolTBL)
        strmod = str(mod)
        print "=== Generated IR code ===\n"
        print strmod
        with open("output.ll", 'w') as f:
            f.write(strmod)

        llmod = llvm.parse_assembly(strmod)
        answer = raw_input('* Optimizing this code? (y/n): ')
        if answer.lower() == "y":
            opt = True
        else:
            opt = False

        if opt:
            pm = llvm.create_module_pass_manager()
            pmb = llvm.create_pass_manager_builder()
            pmb.opt_level = 3  # -O3
            pmb.populate(pm)
            # optimize
            pm.run(llmod)
            print "=== Generated optimized IR code ===\n"
            print llmod
            with open("output_opt.ll", 'w') as f:
                f.write(str(llmod))

        llmod.verify()
        with llvm.create_mcjit_compiler(llmod, self.tm) as ee:
            ee.finalize_object()
            print "=== Generated assembly code ===\n"
            print(self.tm.emit_assembly(llmod))
            with open("output.asm", 'w') as f:
                f.write(self.tm.emit_assembly(llmod))
        answer = raw_input('Do you want to create CFG Graph? (y/n) : ')
        if answer.lower() == 'y':
            for cfg in cfg_list:
                dot = llvm.get_function_cfg(cfg)
                llvm.view_dot_graph(dot, filename=cfg.name, view=True)
Exemple #4
0
    def display(self, filename=None, view=False):
        """
        Plot the CFG.  In IPython notebook, the return image object can be
        inlined.

        The *filename* option can be set to a specific path for the rendered
        output to write to.  If *view* option is True, the plot is opened by
        the system default application for the image format (PDF).
        """
        return ll.view_dot_graph(self.dot, filename=filename, view=view)
def graph(module):
    module_ref = llvm.parse_assembly(str(module))
    functions = module_ref.functions
    images = []
    for func in functions:
        cfg = llvm.get_function_cfg(func)
        graph = llvm.view_dot_graph(cfg, view=False)
        image = graph.render(format='png', directory="graphs")
        images.append(image)
    return images
Exemple #6
0
    def display(self, filename=None, view=False):
        """
        Plot the CFG.  In IPython notebook, the return image object can be
        inlined.

        The *filename* option can be set to a specific path for the rendered
        output to write to.  If *view* option is True, the plot is opened by
        the system default application for the image format (PDF).
        """
        return ll.view_dot_graph(self.dot, filename=filename, view=view)