Пример #1
0
def build_cfg (func):
    '''Given a Python function, create a bytecode flow, visit the flow
    object, and return a control flow graph.'''
    import byte_flow
    return ControlFlowBuilder().visit(
        byte_flow.build_flow(func),
        opcode_util.get_code_object(func).co_argcount)
Пример #2
0
def build_cfg(func):
    '''Given a Python function, create a bytecode flow, visit the flow
    object, and return a control flow graph.'''
    import byte_flow
    return ControlFlowBuilder().visit(
        byte_flow.build_flow(func),
        opcode_util.get_code_object(func).co_argcount)
Пример #3
0
 def translate (self, function, llvm_type = None, env = None):
     if llvm_type is None:
         llvm_type = lc.Type.function(lvoid, ())
     if env is None:
         env = {}
     else:
         env = env.copy()
     env.update((name, method)
                for name, method in lc.Builder.__dict__.items()
                if not name.startswith('_'))
     env.update((name, value)
                for name, value in bytetype.__dict__.items()
                if not name.startswith('_'))
     self.loop_stack = []
     self.llvm_type = llvm_type
     self.function = function
     self.code_obj = opcode_util.get_code_object(function)
     func_globals = getattr(function, 'func_globals',
                            getattr(function, '__globals__', {})).copy()
     func_globals.update(env)
     self.globals = func_globals
     nargs = self.code_obj.co_argcount
     self.cfg = self.control_flow_builder.visit(
         self.bytecode_flow_builder.visit(self.code_obj), nargs)
     flow = self.phi_injector.visit_cfg(self.cfg, nargs)
     ret_val = self.visit(flow)
     del self.cfg
     del self.globals
     del self.code_obj
     del self.function
     del self.llvm_type
     del self.loop_stack
     return ret_val
Пример #4
0
    def translate(self,
                  function,
                  llvm_type=None,
                  llvm_function=None,
                  env=None):
        '''Translate a function to the given LLVM function type.

        If no type is given, then assume the function is of LLVM type
        "void ()".

        The optional env parameter allows extension of the global
        environment.'''
        if llvm_type is None:
            if llvm_function is None:
                llvm_type = lc.Type.function(lvoid, ())
            else:
                llvm_type = llvm_function.type.pointee
        if env is None:
            env = {}
        else:
            env = env.copy()
        env.update((name, method)
                   for name, method in lc.Builder.__dict__.items()
                   if not name.startswith('_'))
        env.update((name, value) for name, value in bytetype.__dict__.items()
                   if not name.startswith('_'))
        self.loop_stack = []
        self.llvm_type = llvm_type
        self.target_function_name = env.get('target_function_name',
                                            function.__name__)
        self.function = function
        self.code_obj = opcode_util.get_code_object(function)
        func_globals = getattr(function, 'func_globals',
                               getattr(function, '__globals__', {})).copy()
        func_globals.update(env)
        self.globals = func_globals
        nargs = self.code_obj.co_argcount
        self.cfg = self.control_flow_builder.visit(
            self.bytecode_flow_builder.visit(self.code_obj), nargs)
        self.llvm_function = llvm_function
        flow = self.phi_injector.visit_cfg(self.cfg, nargs)
        ret_val = self.visit(flow)
        del self.cfg
        del self.globals
        del self.code_obj
        del self.target_function_name
        del self.function
        del self.llvm_type
        del self.loop_stack
        return ret_val
Пример #5
0
    def translate (self, function, llvm_type = None, llvm_function = None,
                   env = None):
        '''Translate a function to the given LLVM function type.

        If no type is given, then assume the function is of LLVM type
        "void ()".

        The optional env parameter allows extension of the global
        environment.'''
        if llvm_type is None:
            if llvm_function is None:
                llvm_type = lc.Type.function(lvoid, ())
            else:
                llvm_type = llvm_function.type.pointee
        if env is None:
            env = {}
        else:
            env = env.copy()
        env.update((name, method)
                   for name, method in lc.Builder.__dict__.items()
                   if not name.startswith('_'))
        env.update((name, value)
                   for name, value in bytetype.__dict__.items()
                   if not name.startswith('_'))
        self.loop_stack = []
        self.llvm_type = llvm_type
        self.target_function_name = env.get('target_function_name',
                                            function.__name__)
        self.function = function
        self.code_obj = opcode_util.get_code_object(function)
        func_globals = getattr(function, 'func_globals',
                               getattr(function, '__globals__', {})).copy()
        func_globals.update(env)
        self.globals = func_globals
        nargs = self.code_obj.co_argcount
        self.cfg = self.control_flow_builder.visit(
            self.bytecode_flow_builder.visit(self.code_obj), nargs)
        self.llvm_function = llvm_function
        flow = self.phi_injector.visit_cfg(self.cfg, nargs)
        ret_val = self.visit(flow)
        del self.cfg
        del self.globals
        del self.code_obj
        del self.target_function_name
        del self.function
        del self.llvm_type
        del self.loop_stack
        return ret_val
Пример #6
0
def build_flow (func):
    '''Given a Python function, return a bytecode flow tree for that
    function.'''
    return BytecodeFlowBuilder().visit(opcode_util.get_code_object(func))
Пример #7
0
def build_flow(func):
    '''Given a Python function, return a bytecode flow tree for that
    function.'''
    return BytecodeFlowBuilder().visit(opcode_util.get_code_object(func))
Пример #8
0
def build_cfg (func):
    import byte_flow
    return ControlFlowBuilder().visit(
        byte_flow.build_flow(func),
        opcode_util.get_code_object(func).co_argcount)
Пример #9
0
def build_flow (func):
    return BytecodeFlowBuilder().visit(opcode_util.get_code_object(func))
Пример #10
0
def build_cfg (func):
    '''Given a Python function, create a bytecode flow, visit the flow
    object, and return a control flow graph.'''
    co_obj = opcode_util.get_code_object(func)
    return ControlFlowBuilder().visit(opcode_util.build_basic_blocks(co_obj),
                                      co_obj.co_argcount)