# Path: xform_utils.py
# def apply_iterative(func, args):
#     cnt = 0
#     while func(*args):
#         cnt += 1
#     _log.info("Ran %s %d times" % (func, cnt))
#     return cnt


from dom import *
from dataflow import *
from xform import *
from decomp import *
from xform_utils import apply_iterative


def do_propagate(cfg):
    analyze_reach_defs(cfg)
    make_du_chains(cfg)

    # Choose one, expr_propagation is required for true decompilation
    #const_propagation(cfg)
    #copy_propagation(cfg)
    #mem_propagation(cfg)
    expr_propagation(cfg)


def dce(cfg):
    analyze_live_vars(cfg)
    foreach_bblock(cfg, dead_code_elimination)


def structure(cfg):

    def _(res):
        return 1 if res else 0

    cnt = 1
    while cnt:
        cnt = 0
        cnt += _(match_if(cfg))
        # Opportunity for applying match_if_else_unjumped arises after
        # match_if pass, and we should match all such cases, or they
        # will be consumed by match_seq instead.

# Path: xform_utils.py
# def apply_iterative(func, args):
#     cnt = 0
#     while func(*args):
#         cnt += 1
#     _log.info("Ran %s %d times" % (func, cnt))
#     return cnt


# Path: script_decompile.py
from dom import *
from dataflow import *
from xform import *
from decomp import *
from xform_utils import apply_iterative



def do_propagate(cfg):
    analyze_reach_defs(cfg)
    make_du_chains(cfg)

    # Choose one, expr_propagation is required for true decompilation
    #const_propagation(cfg)
    #copy_propagation(cfg)
    #mem_propagation(cfg)
    expr_propagation(cfg)


def dce(cfg):
    analyze_live_vars(cfg)
    foreach_bblock(cfg, dead_code_elimination)


def structure(cfg):

    def _(res):
        return 1 if res else 0

    cnt = 1
    while cnt:
        cnt = 0
        cnt += _(match_if(cfg))
        # Opportunity for applying match_if_else_unjumped arises after
        # match_if pass, and we should match all such cases, or they
        # will be consumed by match_seq instead.

        cnt += apply_iterative(match_if_else_unjumped, (cfg,))