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.

File:
1 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)
Note: See TracChangeset for help on using the changeset viewer.