def do_jump_next(src):
    to_remove = True
    while to_remove:
        to_remove = []
        for i in range(len(src)):
            code = src[i]
            if code[0] in ['if', 'ifnot']:
                if code[2] == i+1:
                    to_remove.append(i)
            elif code[0] == 'jmp':
                if code[1] == i+1:
                    to_remove.append(i)
        src = remove_lines(src, to_remove)
    return src
Beispiel #2
0
def do_dead_code(src):
    def merge(states):
        return any(states)
    def step(old_state,code,line_num):
        return old_state
    
    (state, state_out) = analyzer.analyze_forward(src, merge, step, True, False)
    
    to_remove = []
    for i in range(len(src)):
        if not state[i]:
            to_remove.append(i)
            continue
        code = src[i]
        if analyzer.must_take(code):
            src[i] = code = ('jmp', code[2])
        if analyzer.must_not_take(code):
            to_remove.append(i)
            continue
    
    return remove_lines(src, to_remove)
def do_unused_variable(src, final_usage):
    states, states_out = analyzer.analyze_backward(src, merge, step, final_usage)
    
    to_remove = []
    for i in range(len(src)):
        code = src[i]
        if code[0] in BINARY_OPERATORS:
            if code[1][1] not in states[i]:
                to_remove.append(i)
        elif code[0] in UNARY_OPERATORS:
            if code[1][1] not in states[i]:
                to_remove.append(i)
        elif code[0] == 'call':
            if code[1] is not None and code[1][1] not in states[i]:
                code = list(code)
                code[1] = None
                src[i] = tuple(code)
        elif code[0] not in ['jmp','if','ifnot']:
            raise Exception("Unhandled op: " + code[0])
    
    return remove_lines(src, to_remove)
def do_unused_variable(src, final_usage):
    states, states_out = analyzer.analyze_backward(src, merge, step,
                                                   final_usage)

    to_remove = []
    for i in range(len(src)):
        code = src[i]
        if code[0] in BINARY_OPERATORS:
            if code[1][1] not in states[i]:
                to_remove.append(i)
        elif code[0] in UNARY_OPERATORS:
            if code[1][1] not in states[i]:
                to_remove.append(i)
        elif code[0] == 'call':
            if code[1] is not None and code[1][1] not in states[i]:
                code = list(code)
                code[1] = None
                src[i] = tuple(code)
        elif code[0] not in ['jmp', 'if', 'ifnot']:
            raise Exception("Unhandled op: " + code[0])

    return remove_lines(src, to_remove)
def do_dead_code(src):
    def merge(states):
        return any(states)

    def step(old_state, code, line_num):
        return old_state

    (state, state_out) = analyzer.analyze_forward(src, merge, step, True,
                                                  False)

    to_remove = []
    for i in range(len(src)):
        if not state[i]:
            to_remove.append(i)
            continue
        code = src[i]
        if analyzer.must_take(code):
            src[i] = code = ('jmp', code[2])
        if analyzer.must_not_take(code):
            to_remove.append(i)
            continue

    return remove_lines(src, to_remove)