Changeset 349 for proto/parabix2


Ignore:
Timestamp:
Jan 18, 2010, 6:24:02 PM (10 years ago)
Author:
eamiri
Message:

Simple inlining mechanism added. The input is now read from a separate file.

Location:
proto/parabix2/Compiler
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • proto/parabix2/Compiler/bitstream_compiler.py

    r348 r349  
    1 
    21# bitstream_compiler.py - compile unbounded bitstreams code into
    32#   equivalent block-by-block code in C.
     
    87#
    98
    10 import ast, py2bitexpr, string
     9import ast, py2bitexpr, string, sys
    1110
    1211class Program:
    13     def __init__(self):
     12    def __init__(self, out_file):
    1413        pass
    1514        self.templ_name = "template.c"
    16         self.outfile_name = "code.c"
     15        self.outfile_name = out_file
    1716
    1817    def output(self, decl, stmts, templ):
     
    3635    def generate_code(self, s):
    3736        s = ast.parse(s)
    38         s = s.body[0].body
     37        c, s = py2bitexpr.do_inlining(s)
     38       
     39        #return s
     40        #s = s.body[0].body
     41
     42        #FUNCTION INLINING
    3943
    4044        s = py2bitexpr.translate_stmts(s)
     
    5054        py2bitexpr.simplify_tree(s)
    5155
    52         livelist = ['error_mask']
    53        
     56        livelist = ['error_mask', 'd']
     57
    5458        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
    5559
     
    5963        declarations = py2bitexpr.gen_declarations(s)
    6064
     65
     66        #print py2bitexpr.print_prog(s)
    6167        templ = self.read_template()
    6268        templ = self.output(declarations, py2bitexpr.print_prog(s), templ)
     
    6773
    6874if __name__ == '__main__':
    69         mycode=r"""def u8u16(u8bit):
    70         temp1 = (u8bit[0] | u8bit[1]);
    71         temp2 = (u8bit[2] & u8bit[3]);
    72         temp3 = (temp2 &~ temp1);
    73         temp4 = (u8bit[4] & u8bit[5]);
    74         temp5 = (u8bit[6] | u8bit[7]);
    75         temp6 = (temp4 &~ temp5);
    76         LAngle =(temp3 & temp6);
    77         temp7 = (u8bit[6] &~ u8bit[7]);
    78         temp8 = (temp4 & temp7);
    79         RAngle =(temp3 & temp8);
    80         temp9 = (u8bit[7] &~ u8bit[6]);
    81         temp10 =(temp4 & temp9);
    82         Equal =(temp3 & temp10);
    83         temp11 =(u8bit[2] &~ u8bit[3]);
    84         temp12 =(temp11 &~ temp1);
    85         temp13 =(u8bit[5] &~ u8bit[4]);
    86         temp14 =(u8bit[6] & u8bit[7]);
    87         temp15 =(temp13 & temp14);
    88         SQuote =(temp12 & temp15);
    89         temp16 =(u8bit[4] | u8bit[5]);
    90         temp17 =(temp7 &~ temp16);
    91         DQuote =(temp12 & temp17);
    92         temp18 =(temp4 & temp14);
    93         Slash = (temp12 & temp18);
    94         temp19 =(temp16 | temp5);
    95         temp20 =(temp12 &~ temp19);
    96         temp21 =(u8bit[2] | u8bit[3]);
    97         temp22 =(temp1 | temp21);
    98         temp23 =(temp10 &~ temp22);
    99         temp24 =(temp20 | temp23);
    100         temp25 =(u8bit[4] &~ u8bit[5]);
    101         temp26 =(temp25 & temp9);
    102         temp27 =(temp26 &~ temp22);
    103         temp28 =(temp24 | temp27);
    104         temp29 =(temp25 & temp7);
    105         temp30 =(temp29 &~ temp22);
    106         WS = (temp28 | temp30);
    107         temp31 = (temp14 &~ temp16);
    108         temp32 = (temp12 & temp31);
    109         temp33 = (temp32 | Equal);
    110         temp34 = (temp33 | Slash);
    111         temp35 = (temp34 | RAngle);
    112         temp36 = (temp35 | LAngle);
    113         temp37 = (u8bit[1] &~ u8bit[0]);
    114         temp38 = (u8bit[3] &~ u8bit[2]);
    115         temp39 = (temp37 & temp38);
    116         temp40 = (temp39 & temp6);
    117         temp41 = (temp36 | temp40);
    118         temp42 = (temp41 | DQuote);
    119         temp43 = (temp42 | SQuote);
    120         temp44 = (temp25 & temp14);
    121         temp45 = (temp3 & temp44);
    122         temp46 = (temp43 | temp45);
    123         temp47 = (temp46 | temp20);
    124         temp48 = (temp47 | temp27);
    125         temp49 = (temp48 | temp30);
    126         temp50 = (temp49 | temp23);
    127         temp51 = (temp13 & temp7);
    128         temp52 = (temp12 & temp51);
    129         temp53 = (temp50 | temp52);
    130         temp54 = (temp12 & temp6);
    131         temp55 = (temp53 | temp54);
    132         temp56 = (temp37 & temp2);
    133         temp57 = (temp56 & temp6);
    134         temp58 = (temp55 | temp57);
    135         temp59 = (temp9 &~ temp16);
    136         temp60 = (temp12 & temp59);
    137         temp61 = (temp58 | temp60);
    138         temp62 = (temp3 & temp18);
    139         temp63 = (temp61 | temp62);
    140         temp64 = (temp39 & temp10);
    141         NameDelim = (temp63 | temp64);
     75        mycode = ""
     76        if len(sys.argv) < 2:
     77            print "Usage: python bitstream_compiler.py <input file> [output file]"
     78            exit()
     79       
     80        inp_file = open(sys.argv[1])
     81        for line in inp_file:
     82            mycode += line
     83        inp_file.close()
    14284
    143         DQuoteDelim = DQuote | LAngle
    144         SQuoteDelim = SQuote | LAngle
    145         AttListDelim = Slash | RAngle
    146        
    147         LAngleFollow = bitutil.Advance(LAngle)
    148         ElemNamePositions = LAngleFollow & ~Slash
    149         EndTagSeconds = LAngleFollow & Slash
    150 
    151         ElemNameFollows = bitutil.ScanThru(ElemNamePositions, ~NameDelim)
    152         #ElemNames = ElemNameFollows - ElemNamePositions
    153         ParseError = ElemNamePositions & ElemNameFollows
    154 
    155         AttNameStarts = AllZero
    156         AttNameFollows = AllZero
    157         EqToCheck = AllZero
    158         AttValStarts = AllZero
    159         AttValEnds = AllZero
    160         AttValFollows = AllZero
    161 
    162         AfterWS = bitutil.ScanThru(ElemNameFollows, WS)
    163         AttListEnd = AfterWS & AttListDelim
    164         AttNameStart = AfterWS & ~AttListDelim
    165         ParseError |= ElemNameFollows & AttNameStart
    166 
    167         while AttNameStart > 0:
    168             AttNameStarts |= AttNameStart
    169             AttNameFollow = bitutil.ScanThru(AttNameStart, ~NameDelim)
    170             AttNameFollows |= AttNameFollow
    171 
    172             # Scan through WS to the expected '=' delimiter.
    173             EqExpected = bitutil.ScanThru(AttNameFollow, WS)
    174             EqToCheck |= EqExpected
    175             AttValPos = bitutil.ScanThru(bitutil.Advance(EqExpected), WS)
    176             AttValStarts |= AttValPos
    177             DQuoteAttVal = AttValPos & DQuote
    178             SQuoteAttVal = AttValPos & SQuote
    179             DQuoteAttEnd = bitutil.ScanThru(bitutil.Advance(DQuoteAttVal), ~DQuoteDelim)
    180             SQuoteAttEnd = ScanThru(bitutil.Advance(SQuoteAttVal), ~SQuoteDelim)
    181             AttValEnd = DQuoteAttEnd | SQuoteAttEnd
    182             AttValEnds |= AttValEnd
    183             AttValFollow = bitutil.Advance(AttValEnd)
    184             AttValFollows |= AttValFollow
    185             AfterWS = bitutil.ScanThru(AttValFollow, WS)
    186             AttListEnd |= AfterWS & AttListDelim
    187             AttNameStart = AfterWS & ~AttListDelim
    188 
    189         # No more attribute values to process when AttNameStart == 0.
    190 
    191         #AttNames = AttNameFollows - AttNameStarts
    192         #AttVals = AttValFollows - AttValStarts
    193         STagEnds = AttListEnd & RAngle
    194 
    195         # Mark any "/" characters found as the ends of empty element tags.
    196         EmptyTagEnds = Advance(AttListEnd & Slash)
    197         Tags = (STagEnds | EmptyTagEnds) - ElemNamePositions
    198 
    199         # Check for errors.
    200         ParseError |= AttValFollows & AttNameStarts
    201         ParseError |= AttNameStarts & AttNameFollows
    202         ParseError |= EqToCheck & ~Equal
    203         ParseError |= AttValStarts & ~ (DQuote | SQuote)
    204         ParseError |= AttValEnds & ~ (DQuote | SQuote)
    205         ParseError |= EmptyTagEnds & ~RAngle
    206 
    207         # End Tag Parsing
    208         EndTagEnds = bitutil.ScanThru(bitutil.ScanThru(bitutil.Advance(EndTagSeconds), ~NameDelim), WS)
    209         ParseError |= EndTagEnds &~ RAngle
    210         error_mask=ParseError"""
    211         s = Program().generate_code(mycode)
     85        out_file = "code.c"
     86        if len(sys.argv) >= 3:
     87            out_file = sys.argv[2]
     88        s = Program(out_file).generate_code(mycode)
  • proto/parabix2/Compiler/py2bitexpr.py

    r344 r349  
    2222
    2323#############################################################################################
     24## Function Inlining
     25#############################################################################################
     26TEMP_VAR_TEMPLATE = "InlineTemp%i"
     27
     28
     29def replace_in_exp(exp, translation):
     30    if isinstance(exp, ast.BinOp):
     31        exp.left = replace_in_exp(exp.left, translation)
     32        exp.right = replace_in_exp(exp.right, translation)
     33    elif isinstance(exp, ast.UnaryOp):
     34        exp.operand = replace_in_exp(ast.UnaryOp)
     35    elif isinstance(exp, ast.Name):
     36        exp.id = translation.setdefault(exp.id, exp.id)
     37    elif isinstance(exp.ast.Attribute):
     38        exp.value.id = translation.setdefault(exp.value.id, exp.value.id)
     39    elif isinstance(exp, ast.Subscript):
     40        exp.value.id = translation.setdefault(exp.value.id, exp.value.id)
     41    else:
     42        assert(1==0)
     43
     44    return exp
     45
     46def get_all_calls(main):
     47    call_list = []
     48    for index, loc in enumerate(main.body):
     49        if isinstance(loc, ast.Assign) and isinstance(loc.value, ast.Call):
     50            call_list.append(index)
     51        if isinstance(loc, ast.AugAssign) and isinstance(loc.value, ast.Call):
     52            call_list.append(index)
     53    return call_list
     54
     55
     56def prepare_code(callee, args, unique_num):
     57    #check number of args matches
     58    assert (len(args)==len(callee.args.args))
     59    translation = {}
     60    for index in range(len(args)):
     61        translation[translate_var(callee.args.args[index])] = translate_var(args[index])
     62
     63    #print translation
     64    rets = {}
     65    for index, loc in enumerate(callee.body):
     66        if isinstance(loc, ast.Assign):
     67            for index, var in enumerate(loc.targets):
     68                var = ast.Name(id=translation.setdefault(translate_var(var), translate_var(var)))
     69            loc.value = replace_in_exp(loc.value, translation)
     70        elif isinstance(loc, ast.AugAssign):
     71            loc.target = ast.Name(id=translation.setdefault(translate_var(loc.target), translate_var(loc.target)))
     72            loc.value = replace_in_exp(loc.value, translation)
     73        elif isinstance(loc, ast.Return):
     74            rets[index] = ast.Assign([ast.Name(id=TEMP_VAR_TEMPLATE%unique_num)], loc.value)
     75
     76        else:
     77            assert(1==0)
     78
     79        for key in rets:
     80            callee.body[key] = rets[index]
     81
     82    return callee
     83
     84def finalize(main, inline_code, call_list):
     85    for list_index, main_index in enumerate(reversed(call_list)):
     86        main.body[main_index].value = ast.Name(id=TEMP_VAR_TEMPLATE%main_index)
     87        main.body = main.body[:main_index]+inline_code[-list_index-1].body+main.body[main_index:]
     88    return main
     89   
     90def do_inlining(module):
     91    func_dict = {}
     92    for index, func in enumerate(module.body):
     93        func_dict[func.name] = index
     94
     95    main = module.body[func_dict["main"]]
     96    call_list = get_all_calls(main)
     97
     98    inline_code = []
     99    for line_no in call_list:
     100        callee = copy.deepcopy(module.body[func_dict[main.body[line_no].value.func.id]])
     101        args = main.body[line_no].value.args
     102        inline_code.append(prepare_code(callee, args, line_no))
     103    main = finalize(main, inline_code, call_list)
     104
     105    return call_list, module.body[func_dict["main"]].body
     106
     107#############################################################################################
    24108## Translation to bitExpr classes --- Pass 2
    25109#############################################################################################
     
    29113        elif isinstance(ast_value, ast.Num): return repr(ast_value.n)
    30114        else: raise PyBitError("Unknown value %s\n" % ast.dump(ast_value))
    31 
    32115
    33116def is_Advance_Call(fncall):
     
    9741057        bb = [tree[-1]]
    9751058        new_alives.add(tree[-1].control_expr.var.varname)
    976  
     1059
    9771060    elif isinstance(tree[-1], bitexpr.WhileLoop):
    9781061        must_live.add(tree[-1].control_expr.var.varname)
    9791062        new_alives, tree[-1].stmts = eliminate_dead_code(tree[-1].stmts, must_live)
    9801063        bb = [tree[-1]]
    981  
     1064
    9821065    all_alives = new_alives.union(must_live)
    9831066    all_lives, new_tree = eliminate_dead_code(tree[:last], all_alives)
    9841067    tree = new_tree+bb
    9851068    return all_alives, tree
    986 
    9871069
    9881070#################################################################################################################
     
    10011083    for item in carries:
    10021084        loop.stmts.append(bitexpr.BitAssign( bitexpr.Var(item), bitexpr.FalseLiteral("int") ))
    1003    
     1085
    10041086    return loop, carries
    10051087
     
    10131095                all.append(i)
    10141096                all.append(i+carry_suffix)
    1015            
     1097
    10161098    keys = [k for k in update]
    10171099    keys.sort(reverse=True)
Note: See TracChangeset for help on using the changeset viewer.