def explore_function_opcodes(self): next_instr = 0 co_code = self.func_code.co_code print(repr(co_code)) while True: if next_instr < 0: if self.branch_indexes: next_instr = self.branch_indexes.pop() else: # Done, nothing to interpret break self.visited_indexes.add(next_instr) last_instr = next_instr self.current_opcode_index = last_instr # Used to get current opcode source location next_instr, opcode, oparg = opcode_decoder(co_code, next_instr) try: opcode_handler = TYPE_ANNOTATOR_OPCODE_FUNCTIONS[opcode] except KeyError: self.warning("Skipped opcode %s @ %d" % (opname[opcode], last_instr)) new_next_instr = -1 else: print("Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg)) new_next_instr = opcode_handler(self, oparg) assert new_next_instr is not None if new_next_instr >= 0: next_instr = new_next_instr elif next_instr >= len(co_code): next_instr = -1
def explore_function_opcodes( self ): next_instr = 0 co_code = self.func_code.co_code print( repr(co_code) ) while True: if next_instr < 0: if self.branch_indexes: next_instr = self.branch_indexes.pop() else: # Done, nothing to interpret break self.visited_indexes.add( next_instr ) last_instr = next_instr self.current_opcode_index = last_instr # Used to get current opcode source location next_instr, opcode, oparg = opcode_decoder( co_code, next_instr ) try: opcode_handler = TYPE_ANNOTATOR_OPCODE_FUNCTIONS[ opcode ] except KeyError: self.warning( "Skipped opcode %s @ %d" % (opname[opcode], last_instr) ) new_next_instr = -1 else: print( "Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg) ) new_next_instr = opcode_handler( self, oparg ) assert new_next_instr is not None if new_next_instr >= 0: next_instr = new_next_instr elif next_instr >= len(co_code): next_instr = -1
def explore_function_opcodes(self): next_instr = 0 co_code = self.py_func.__code__.co_code print(repr(co_code)) block_indexes = sorted( self.branch_indexes) # start indexes of basic blocks print('Prescan branch indexes: %r' % block_indexes) if block_indexes and block_indexes[0] == 0: del block_indexes[0] while True: last_instr = next_instr next_instr, opcode, oparg = opcode_decoder(co_code, next_instr) try: opcode_handler = CODE_GENERATOR_OPCODE_FUNCTIONS[opcode] except KeyError: self.warning("Skipped opcode %s @ %d" % (opname[opcode], last_instr)) action = ACTION_PROCESS_NEXT_OPCODE else: print("Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg)) self.next_instr_index = next_instr action = opcode_handler(self, oparg) assert action is not None if action == ACTION_PROCESS_NEXT_OPCODE: if next_instr in self.branch_indexes: # Notes: some code similar to ACTION_BRANCH # We are falling through into a new block # We need to inject branch code. print('Switched via fall through to new block @%d' % next_instr) block_indexes.remove(next_instr) branch_block = self.obtain_block_at( next_instr, 'fall_through') branch_block.incoming_blocks.append(self.current_block) self.builder.branch(branch_block.l_basic_block) # Set branch basic block as builder target self.builder.position_at_end(branch_block.l_basic_block) self.current_block = branch_block else: # next_instr has already been initialized, checks that last instruction # was a terminator if next_instr >= len(co_code): raise ValueError( 'Attempting to process instruction beyond end of code block.' ) elif action == ACTION_BRANCH: if block_indexes: next_instr = block_indexes.pop(0) # Set branch basic block as builder target print('Switched to new block @%d' % next_instr) #branch_block = self.blocks_by_target[next_instr] branch_block = self.obtain_block_at( next_instr, 'some_branch') self.builder.position_at_end(branch_block.l_basic_block) self.current_block = branch_block else: # Done, nothing to interpret break else: raise ValueError('Invalid action: %d' % action)
def scan_function_code(self): """Scan the function codes. Returns: list of constraint created while scanning the code. """ block_indexes = sorted( determine_branch_targets(self._ti_function.co_code)) # read-only next_instr = 0 co_code = self._ti_function.co_code print(repr(co_code)) print('Prescan branch indexes: %r' % block_indexes) if block_indexes and block_indexes[0] == 0: del block_indexes[0] self._switch_to_entry_branch() while True: last_instr = next_instr next_instr, opcode, oparg = opcode_decoder(co_code, next_instr) try: self.last_opcode_index = last_instr opcode_handler = FUNCTION_SCANNER_OPCODE_FUNCTIONS[opcode] except KeyError: self.warning("Skipped opcode %s @ %d" % (opname[opcode], last_instr)) action = ACTION_PROCESS_NEXT_OPCODE else: print("Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg)) self.next_instr_index = next_instr action = opcode_handler(self, oparg) assert action is not None if action == ACTION_PROCESS_NEXT_OPCODE: if next_instr in block_indexes: # Notes: some code similar to ACTION_BRANCH # We are falling through into a new block print('Switched via fall through to new branch @%d' % next_instr) block_indexes.remove(next_instr) self._switch_branch(next_instr, 'fall through') else: # next_instr has already been initialized, checks that last instruction # was a terminator if next_instr >= len(co_code): raise ValueError( 'Attempting to process instruction beyond end of code block.' ) elif action == ACTION_BRANCH: if block_indexes: next_instr = block_indexes.pop(0) # Set branch basic block as builder target print('Switched to new branch @%d' % next_instr) self._switch_branch(next_instr, 'some_branch') else: # Done, nothing to interpret break else: raise ValueError('Invalid action: %d' % action)
def explore_function_opcodes( self ): next_instr = 0 co_code = self.py_func.__code__.co_code print( repr(co_code) ) block_indexes = sorted( self.branch_indexes ) # start indexes of basic blocks print( 'Prescan branch indexes: %r' % block_indexes ) if block_indexes and block_indexes[0] == 0: del block_indexes[0] while True: last_instr = next_instr next_instr, opcode, oparg = opcode_decoder( co_code, next_instr ) try: opcode_handler = CODE_GENERATOR_OPCODE_FUNCTIONS[ opcode ] except KeyError: self.warning( "Skipped opcode %s @ %d" % (opname[opcode], last_instr) ) action = ACTION_PROCESS_NEXT_OPCODE else: print( "Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg) ) self.next_instr_index = next_instr action = opcode_handler( self, oparg ) assert action is not None if action == ACTION_PROCESS_NEXT_OPCODE: if next_instr in self.branch_indexes: # Notes: some code similar to ACTION_BRANCH # We are falling through into a new block # We need to inject branch code. print( 'Switched via fall through to new block @%d' % next_instr ) block_indexes.remove( next_instr ) branch_block = self.obtain_block_at(next_instr, 'fall_through') branch_block.incoming_blocks.append( self.current_block ) self.builder.branch( branch_block.l_basic_block ) # Set branch basic block as builder target self.builder.position_at_end( branch_block.l_basic_block ) self.current_block = branch_block else: # next_instr has already been initialized, checks that last instruction # was a terminator if next_instr >= len(co_code): raise ValueError( 'Attempting to process instruction beyond end of code block.' ) elif action == ACTION_BRANCH: if block_indexes: next_instr = block_indexes.pop(0) # Set branch basic block as builder target print( 'Switched to new block @%d' % next_instr ) #branch_block = self.blocks_by_target[next_instr] branch_block = self.obtain_block_at(next_instr, 'some_branch') self.builder.position_at_end( branch_block.l_basic_block ) self.current_block = branch_block else: # Done, nothing to interpret break else: raise ValueError( 'Invalid action: %d' % action )
def scan_function_code( self ): """Scan the function codes. Returns: list of constraint created while scanning the code. """ block_indexes = sorted( determine_branch_targets( self._ti_function.co_code ) )# read-only next_instr = 0 co_code = self._ti_function.co_code print( repr(co_code) ) print( 'Prescan branch indexes: %r' % block_indexes ) if block_indexes and block_indexes[0] == 0: del block_indexes[0] self._switch_to_entry_branch() while True: last_instr = next_instr next_instr, opcode, oparg = opcode_decoder( co_code, next_instr ) try: self.last_opcode_index = last_instr opcode_handler = FUNCTION_SCANNER_OPCODE_FUNCTIONS[ opcode ] except KeyError: self.warning( "Skipped opcode %s @ %d" % (opname[opcode], last_instr) ) action = ACTION_PROCESS_NEXT_OPCODE else: print( "Processing @%d: opcode %s, %d" % (last_instr, opname[opcode], oparg) ) self.next_instr_index = next_instr action = opcode_handler( self, oparg ) assert action is not None if action == ACTION_PROCESS_NEXT_OPCODE: if next_instr in block_indexes: # Notes: some code similar to ACTION_BRANCH # We are falling through into a new block print( 'Switched via fall through to new branch @%d' % next_instr ) block_indexes.remove( next_instr ) self._switch_branch( next_instr, 'fall through' ) else: # next_instr has already been initialized, checks that last instruction # was a terminator if next_instr >= len(co_code): raise ValueError( 'Attempting to process instruction beyond end of code block.' ) elif action == ACTION_BRANCH: if block_indexes: next_instr = block_indexes.pop(0) # Set branch basic block as builder target print( 'Switched to new branch @%d' % next_instr ) self._switch_branch( next_instr, 'some_branch') else: # Done, nothing to interpret break else: raise ValueError( 'Invalid action: %d' % action )