Пример #1
0
    def _patch_var_dels(self,
                        internal_dead_map,
                        escaping_dead_map,
                        extend_lifetimes=False):
        """
        Insert delete in each block
        """
        for offset, ir_block in self.func_ir.blocks.items():
            # for each internal var, insert delete after the last use
            internal_dead_set = internal_dead_map[offset].copy()
            delete_pts = []
            # for each statement in reverse order
            for stmt in reversed(ir_block.body[:-1]):
                # internal vars that are used here
                live_set = set(v.name for v in stmt.list_vars())
                dead_set = live_set & internal_dead_set
                for T, def_func in ir_extension_insert_dels.items():
                    if isinstance(stmt, T):
                        done_dels = def_func(stmt, dead_set)
                        dead_set -= done_dels
                        internal_dead_set -= done_dels
                # used here but not afterwards
                delete_pts.append((stmt, dead_set))
                internal_dead_set -= dead_set

            # rewrite body and insert dels
            body = []
            lastloc = ir_block.loc
            del_store = []
            for stmt, delete_set in reversed(delete_pts):
                # If using extended lifetimes then the Dels are all put at the
                # block end just ahead of the terminator, so associate their
                # location with the terminator.
                if extend_lifetimes:
                    lastloc = ir_block.body[-1].loc
                else:
                    lastloc = stmt.loc
                # Ignore dels (assuming no user inserted deletes)
                if not isinstance(stmt, ir.Del):
                    body.append(stmt)
                # note: the reverse sort is not necessary for correctness
                #       it is just to minimize changes to test for now
                for var_name in sorted(delete_set, reverse=True):
                    delnode = ir.Del(var_name, loc=lastloc)
                    if extend_lifetimes:
                        del_store.append(delnode)
                    else:
                        body.append(delnode)
            if extend_lifetimes:
                body.extend(del_store)
            body.append(ir_block.body[-1])  # terminator
            ir_block.body = body

            # vars to delete at the start
            escape_dead_set = escaping_dead_map[offset]
            for var_name in sorted(escape_dead_set):
                ir_block.prepend(ir.Del(var_name, loc=ir_block.body[0].loc))
Пример #2
0
 def op_DELETE_FAST(self, inst):
     dstname = self.code_locals[inst.arg]
     self.current_block.append(ir.Del(dstname, loc=self.loc))
Пример #3
0
 def test_del(self):
     a = ir.Del(self.var_a.name, self.loc1)
     b = ir.Del(self.var_a.name, self.loc1)
     c = ir.Del(self.var_a.name, self.loc2)
     d = ir.Del(self.var_b.name, self.loc1)
     self.check(a, same=[b, c], different=[d])