Ignore:
Timestamp:
Mar 1, 2010, 3:25:39 PM (9 years ago)
Author:
eamiri
Message:

bug fix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/py2bitexpr.py

    r365 r368  
    8383def remove_unsupported_code(callee, fn_names, predefined, main=False):
    8484    to_remove = []
    85    
    8685    for index, loc in enumerate(callee.body):
    87         if not (isinstance(loc, ast.Assign) or isinstance(loc, ast.AugAssign) or isinstance(loc, ast.Return)):
    88             if isinstance(loc, ast.While):
    89                 if not main:
     86        if isinstance(loc, ast.While):
     87            if not main:
     88                to_remove.append(index)
     89            continue
     90
     91        if isinstance(loc, ast.Expr):
     92            if isinstance(loc.value, ast.Call):
     93                if main and not (translate_var(loc.value.func) in fn_names+predefined):
     94                    to_remove.append(index)
     95                if not main and not translate_var(loc.value.func) in predefined:
    9096                    to_remove.append(index)
    9197
    9298            else:
    9399                to_remove.append(index)
    94        
     100
     101            continue
     102
     103        if not (isinstance(loc, ast.Assign) or isinstance(loc, ast.AugAssign) or isinstance(loc, ast.Return)):
     104            to_remove.append(index)
     105
    95106        #we do not support AugAssign with function calls
    96107        if isinstance(loc, ast.AugAssign):
     
    110121    for line_no in to_remove:
    111122        del callee.body[line_no]
     123
    112124    return callee
    113125
     
    119131            new.append(remove_unsupported_code(item, fn_names, predefined, item.name == "main"))
    120132        else:
    121              new.append(item)
     133            new.append(item)
    122134    module.body = new
    123135    return module
     
    320332        replace_code = {}
    321333        if isinstance(item, ast.FunctionDef):
    322             for index, loc in enumerate(item.body):
     334            for index,loc in enumerate(item.body):
    323335                if isinstance(loc, ast.Assign):
    324336                    if isinstance(loc.value, ast.List):
     
    329341            for line_no in keys:
    330342                item.body = item.body[:line_no]+replace_code[line_no]+item.body[line_no+1:]
    331    
     343
    332344    return module
    333345
     
    335347    advance = ast.Attribute(value=ast.Name(id="bitutil"), attr="Advance")
    336348    scan_thru = ast.Attribute(value=ast.Name(id="bitutil"), attr="ScanThru")
    337     return [translate_var(advance), translate_var(scan_thru)]
     349    optimize = ast.Attribute(value=ast.Name(id="bitutil"), attr="Optimize")
     350    return [translate_var(advance), translate_var(scan_thru), translate_var(optimize)]
    338351
    339352def do_inlining(module):
     
    341354    func_dict = collect_names(module.body)
    342355    module = expand_list_init(module)
    343    
    344    
     356
    345357    valid_fn = [name for name in func_dict]
    346358    predef_fn = get_predefined_funcs()
    347359    module = clean_functions(module, valid_fn, predef_fn)
     360
    348361    main = module.body[func_dict["main"]]
    349362    call_list = get_all_calls(main, [func for func in func_dict])
     
    469482        for s in ast_stmts:
    470483                if isinstance(s, ast.Expr):
    471                         if (s.value.func.id=='optimize'):
     484                        print translate_var(s.value.func)
     485                        if (translate_var(s.value.func)=='strct_bitutil__Optimize_'):
    472486                            target = translate_var(s.value.args[0])
    473487                            replace = translate_var(s.value.args[1])
     
    797811def chop_code(s, start, stop):
    798812    line = 0
     813
    799814    if start == -1:
    800815        cut1 = -1
    801    
     816
    802817    if stop == len(s):
    803818        cut2 = len(s)
    804    
     819
    805820    for index, stmt in enumerate(s):
    806821        if isinstance(stmt, bitexpr.BitAssign):
     
    863878    #use use1[False], def1[False], opt1[False] to construct the block after if-then-else and recurse on that block
    864879    first, second, third = chop_code(s, earliest, latest)
     880
    865881    the_opt = None
    866882    the_index = None
     
    875891    second = gen_bb(second, opt1[True], def1[True],use1[True])
    876892
    877     if the_opt[1] == 'allzero':
     893    if the_opt[1] == 'AllZero' or the_opt[1] == 'allzero':
    878894        cond_obj = bitexpr.isAllZero(the_opt[0])
    879     if the_opt[1] == 'allone':
     895    if the_opt[1] == 'AllOne' or the_opt[1] == 'allone':
    880896        cond_obj = bitexpr.isAllOne(the_opt[0])
    881897
     
    892908        third = gen_bb(third, opt1[False], new_def, new_use)
    893909
    894     result = first + [bitexpr.If(cond_obj, second, copy.deepcopy(second))]+third
     910    result = first + [bitexpr.If(cond_obj, second, copy.deepcopy(second))] + third
    895911
    896912    return result
     
    905921    st = gen_sym_table(s, True)
    906922    st2 = gen_sym_table(s)
    907     total_lines = count_lines(s)
     923    #total_lines = count_lines(s)
     924    total_lines = len(s)
    908925
    909926    for item in opt_list:
     
    12581275   
    12591276def check_loc(loc, must_liv):
    1260    
     1277
    12611278    effective_name = get_effective_name(loc.LHS.varname)
    1262     #if effective_name == "lex":
    1263            
    12641279    if effective_name in must_liv or loc.LHS.varname in must_liv:
    12651280        if isinstance(loc.RHS, bitexpr.Not):
     
    12911306def remove_dead(bb, must_live):
    12921307    #eliminates dead code from a basic block
    1293     bb = remove_copies(bb)
     1308    #bb = remove_copies(bb)
    12941309    my_alives = set([])
    12951310    dead = []
     
    12971312    for line, loc in reversed(list(enumerate(bb))):
    12981313        #print line, my_alives.union(must_live)
     1314        #print "---------------------------------------------------"
    12991315        new_lives, removable = check_loc(loc, my_alives.union(must_live))
     1316        #print "---------------------------------------------------"
    13001317
    13011318        if removable:
     
    13091326    return my_alives, bb
    13101327
     1328
     1329def get_loop_variables(code):
     1330    must_live = set([])
     1331    for loc in code:
     1332        if isinstance(loc, bitexpr.WhileLoop):
     1333            if not loc.carry_expr is None:
     1334                must_live.add(loc.carry_expr.var.varname)
     1335        if isinstance(loc, bitexpr.If):
     1336            true_lives = get_loop_variables(loc.true_branch)
     1337            false_lives = get_loop_variables(loc.false_branch)
     1338            must_live = must_live.union(true_lives)
     1339            must_live = must_live.union(false_lives)
     1340    return must_live
     1341
    13111342def eliminate_dead_code(tree, must_live):
    1312 
    13131343    if len(tree) == 0:
    1314         return [], []
     1344        return must_live, []
    13151345
    13161346    last = len(tree) - 1
     
    13181348    bb = []
    13191349
    1320     for loc in tree:
    1321         if isinstance(loc, bitexpr.WhileLoop):
    1322             if not loc.carry_expr is None:
    1323                 must_live.add(loc.carry_expr.var.varname)
     1350    must_live = must_live.union(get_loop_variables(tree))
    13241351
    13251352    if isinstance(tree[-1], bitexpr.BitAssign):
     
    13291356                first = i+1
    13301357                break
     1358
    13311359        new_alives, bb = remove_dead(tree[first:], must_live)
    13321360        last = first
     
    13341362    elif isinstance(tree[-1], bitexpr.If):
    13351363        new_alives, tree[-1].true_branch = eliminate_dead_code(tree[-1].true_branch, must_live)
    1336 
    13371364        new_alives, tree[-1].false_branch = eliminate_dead_code(tree[-1].false_branch, must_live)
    13381365        bb = [tree[-1]]
     
    13451372
    13461373    all_alives = new_alives.union(must_live)
    1347     all_lives, new_tree = eliminate_dead_code(tree[:last], all_alives)
     1374    #if "Ct_starts" in all_alives:
     1375    #    print "IT IS ALIVE"
     1376    dummy, new_tree = eliminate_dead_code(tree[:last], all_alives)
    13481377    tree = new_tree+bb
    1349     return all_alives, tree
     1378    return all_alives.union(dummy), tree
    13501379
    13511380#################################################################################################################
     
    13761405                all.append(i)
    13771406                all.append(i+carry_suffix)
     1407
     1408        if isinstance(loc, bitexpr.If):
     1409            loc.true_branch, live_list = process_while_loops(loc.true_branch)
     1410            loc.false_branch, live_list = process_while_loops(loc.false_branch)
    13781411
    13791412    keys = [k for k in update]
Note: See TracChangeset for help on using the changeset viewer.