root/proto/parabix2/Compiler/bitstream_compiler.py @ 347

Revision 347, 7.4 KB (checked in by eamiri, 3 years ago)

minor changes

Line 
1
2# bitstream_compiler.py - compile unbounded bitstreams code into
3#   equivalent block-by-block code in C.
4#
5# (c) 2009 Robert D. Cameron and Ehsan Amiri
6# All rights reserved.
7# Licensed to International Characters, Inc. under Academic Free License 3.0
8#
9
10import ast, py2bitexpr, string
11
12class Program:
13    def __init__(self):
14        pass
15        self.templ_name = "template.c"
16        self.outfile_name = "code.c"
17
18    def output(self, decl, stmts, templ):
19        templ = string.replace(templ, '@decl', decl)
20        templ = string.replace(templ, '@stmts', stmts)
21        return templ
22
23    def read_template(self):
24        f=open(self.templ_name)
25        inp = ""
26        for line in f:
27            inp += line
28        f.close()
29        return inp
30
31    def write_final_code(self, code):
32        f=open(self.outfile_name, 'w')
33        f.write(code)
34        f.close()
35
36    def generate_code(self, s):
37        s = ast.parse(s)
38        s = s.body[0].body
39
40        s = py2bitexpr.translate_stmts(s)
41
42        st = py2bitexpr.gen_sym_table(s)
43
44        s=py2bitexpr.make_SSA(s, st)
45
46        s = py2bitexpr.partition2bb(s, )
47
48        s = py2bitexpr.apply_all_opt(s)
49        s = py2bitexpr.normalize(s)
50        py2bitexpr.simplify_tree(s)
51
52        livelist = ['u16hi[0]', 'u16hi[1]', 'u16hi[2]', 'u16hi[3]', 'u16hi[4]', 'u16hi[5]', 'u16hi[6]', 'u16hi[7]']
53        livelist += ['u16lo[0]','u16lo[1]','u16lo[2]','u16lo[3]','u16lo[4]','u16lo[5]','u16lo[6]','u16lo[7]', 'delmask', 'u8.error', 'error_mask', 'u8lastbyte', 'Cursor2',]
54       
55        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
56
57        s=py2bitexpr.factor_out(s)
58
59        s, livelist = py2bitexpr.process_while_loops(s)
60        declarations = py2bitexpr.gen_declarations(s)
61
62        templ = self.read_template()
63        templ = self.output(declarations, py2bitexpr.print_prog(s), templ)
64        self.write_final_code(templ)
65        return s
66
67###############################################################
68
69if __name__ == '__main__':
70        mycode=r"""def u8u16(u8bit):
71        temp1 = (u8bit[0] | u8bit[1]);
72        temp2 = (u8bit[2] & u8bit[3]);
73        temp3 = (temp2 &~ temp1);
74        temp4 = (u8bit[4] & u8bit[5]);
75        temp5 = (u8bit[6] | u8bit[7]);
76        temp6 = (temp4 &~ temp5);
77        LAngle =(temp3 & temp6);
78        temp7 = (u8bit[6] &~ u8bit[7]);
79        temp8 = (temp4 & temp7);
80        RAngle =(temp3 & temp8);
81        temp9 = (u8bit[7] &~ u8bit[6]);
82        temp10 =(temp4 & temp9);
83        Equal =(temp3 & temp10);
84        temp11 =(u8bit[2] &~ u8bit[3]);
85        temp12 =(temp11 &~ temp1);
86        temp13 =(u8bit[5] &~ u8bit[4]);
87        temp14 =(u8bit[6] & u8bit[7]);
88        temp15 =(temp13 & temp14);
89        SQuote =(temp12 & temp15);
90        temp16 =(u8bit[4] | u8bit[5]);
91        temp17 =(temp7 &~ temp16);
92        DQuote =(temp12 & temp17);
93        temp18 =(temp4 & temp14);
94        Slash = (temp12 & temp18);
95        temp19 =(temp16 | temp5);
96        temp20 =(temp12 &~ temp19);
97        temp21 =(u8bit[2] | u8bit[3]);
98        temp22 =(temp1 | temp21);
99        temp23 =(temp10 &~ temp22);
100        temp24 =(temp20 | temp23);
101        temp25 =(u8bit[4] &~ u8bit[5]);
102        temp26 =(temp25 & temp9);
103        temp27 =(temp26 &~ temp22);
104        temp28 =(temp24 | temp27);
105        temp29 =(temp25 & temp7);
106        temp30 =(temp29 &~ temp22);
107        WS = (temp28 | temp30);
108        temp31 = (temp14 &~ temp16);
109        temp32 = (temp12 & temp31);
110        temp33 = (temp32 | Equal);
111        temp34 = (temp33 | Slash);
112        temp35 = (temp34 | RAngle);
113        temp36 = (temp35 | LAngle);
114        temp37 = (u8bit[1] &~ u8bit[0]);
115        temp38 = (u8bit[3] &~ u8bit[2]);
116        temp39 = (temp37 & temp38);
117        temp40 = (temp39 & temp6);
118        temp41 = (temp36 | temp40);
119        temp42 = (temp41 | DQuote);
120        temp43 = (temp42 | SQuote);
121        temp44 = (temp25 & temp14);
122        temp45 = (temp3 & temp44);
123        temp46 = (temp43 | temp45);
124        temp47 = (temp46 | temp20);
125        temp48 = (temp47 | temp27);
126        temp49 = (temp48 | temp30);
127        temp50 = (temp49 | temp23);
128        temp51 = (temp13 & temp7);
129        temp52 = (temp12 & temp51);
130        temp53 = (temp50 | temp52);
131        temp54 = (temp12 & temp6);
132        temp55 = (temp53 | temp54);
133        temp56 = (temp37 & temp2);
134        temp57 = (temp56 & temp6);
135        temp58 = (temp55 | temp57);
136        temp59 = (temp9 &~ temp16);
137        temp60 = (temp12 & temp59);
138        temp61 = (temp58 | temp60);
139        temp62 = (temp3 & temp18);
140        temp63 = (temp61 | temp62);
141        temp64 = (temp39 & temp10);
142        NameDelim = (temp63 | temp64);
143
144        DQuoteDelim = DQuote | LAngle
145        SQuoteDelim = SQuote | LAngle
146        AttListDelim = Slash | RAngle
147       
148        LAngleFollow = bitutil.Advance(LAngle)
149        ElemNamePositions = LAngleFollow & ~Slash
150        EndTagSeconds = LAngleFollow & Slash
151
152        ElemNameFollows = bitutil.ScanThru(ElemNamePositions, ~NameDelim)
153        #ElemNames = ElemNameFollows - ElemNamePositions
154        ParseError = ElemNamePositions & ElemNameFollows
155
156        AttNameStarts = AllZero
157        AttNameFollows = AllZero
158        EqToCheck = AllZero
159        AttValStarts = AllZero
160        AttValEnds = AllZero
161        AttValFollows = AllZero
162
163        AfterWS = bitutil.ScanThru(ElemNameFollows, WS)
164        AttListEnd = AfterWS & AttListDelim
165        AttNameStart = AfterWS & ~AttListDelim
166        ParseError |= ElemNameFollows & AttNameStart
167
168        while AttNameStart > 0:
169            AttNameStarts |= AttNameStart
170            AttNameFollow = bitutil.ScanThru(AttNameStart, ~NameDelim)
171            AttNameFollows |= AttNameFollow
172
173            # Scan through WS to the expected '=' delimiter.
174            EqExpected = bitutil.ScanThru(AttNameFollow, WS)
175            EqToCheck |= EqExpected
176            AttValPos = bitutil.ScanThru(bitutil.Advance(EqExpected), WS)
177            AttValStarts |= AttValPos
178            DQuoteAttVal = AttValPos & DQuote
179            SQuoteAttVal = AttValPos & SQuote
180            DQuoteAttEnd = bitutil.ScanThru(bitutil.Advance(DQuoteAttVal), ~DQuoteDelim)
181            SQuoteAttEnd = ScanThru(bitutil.Advance(SQuoteAttVal), ~SQuoteDelim)
182            AttValEnd = DQuoteAttEnd | SQuoteAttEnd
183            AttValEnds |= AttValEnd
184            AttValFollow = bitutil.Advance(AttValEnd)
185            AttValFollows |= AttValFollow
186            AfterWS = bitutil.ScanThru(AttValFollow, WS)
187            AttListEnd |= AfterWS & AttListDelim
188            AttNameStart = AfterWS & ~AttListDelim
189
190        # No more attribute values to process when AttNameStart == 0.
191
192        #AttNames = AttNameFollows - AttNameStarts
193        #AttVals = AttValFollows - AttValStarts
194        STagEnds = AttListEnd & RAngle
195
196        # Mark any "/" characters found as the ends of empty element tags.
197        EmptyTagEnds = Advance(AttListEnd & Slash)
198        Tags = (STagEnds | EmptyTagEnds) - ElemNamePositions
199
200        # Check for errors.
201        ParseError |= AttValFollows & AttNameStarts
202        ParseError |= AttNameStarts & AttNameFollows
203        ParseError |= EqToCheck & ~Equal
204        ParseError |= AttValStarts & ~ (DQuote | SQuote)
205        ParseError |= AttValEnds & ~ (DQuote | SQuote)
206        ParseError |= EmptyTagEnds & ~RAngle
207
208        # End Tag Parsing
209        EndTagEnds = bitutil.ScanThru(bitutil.ScanThru(bitutil.Advance(EndTagSeconds), ~NameDelim), WS)
210        ParseError |= EndTagEnds &~ RAngle
211        error_mask=ParseError"""
212        s = Program().generate_code(mycode)
Note: See TracBrowser for help on using the browser.