source: proto/parabix2/Compiler/bitstream_compiler.py @ 352

Last change on this file since 352 was 352, checked in by eamiri, 9 years ago

arrays indexed by string are supported

File size: 2.3 KB
Line 
1# bitstream_compiler.py - compile unbounded bitstreams code into
2#   equivalent block-by-block code in C.
3#
4# (c) 2009 Robert D. Cameron and Ehsan Amiri
5# All rights reserved.
6# Licensed to International Characters, Inc. under Academic Free License 3.0
7#
8
9import ast, py2bitexpr, string, sys
10
11class Program:
12    def __init__(self, out_file):
13        pass
14        self.templ_name = "template.c"
15        self.outfile_name = out_file
16
17    def output(self, decl, stmts, templ):
18        templ = string.replace(templ, '@decl', decl)
19        templ = string.replace(templ, '@stmts', stmts)
20        return templ
21
22    def read_template(self):
23        f=open(self.templ_name)
24        inp = ""
25        for line in f:
26            inp += line
27        f.close()
28        return inp
29
30    def write_final_code(self, code):
31        f=open(self.outfile_name, 'w')
32        f.write(code)
33        f.close()
34
35    def generate_code(self, s):
36        s = ast.parse(s)
37        c, s = py2bitexpr.do_inlining(s)
38       
39        #return s
40        #s = s.body[0].body
41
42        #FUNCTION INLINING
43
44        s = py2bitexpr.translate_stmts(s)
45
46        st = py2bitexpr.gen_sym_table(s)
47
48        s=py2bitexpr.make_SSA(s, st)
49
50        s = py2bitexpr.partition2bb(s, )
51
52        s = py2bitexpr.apply_all_opt(s)
53        s = py2bitexpr.normalize(s)
54        py2bitexpr.simplify_tree(s)
55
56        livelist = ['error_mask', 'u16','u8_pre', 'u8_butlast', 'u8_last']
57
58        #all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
59
60        s=py2bitexpr.factor_out(s)
61
62        s, livelist = py2bitexpr.process_while_loops(s)
63        declarations = py2bitexpr.gen_declarations(s)
64
65
66        #print py2bitexpr.print_prog(s)
67        templ = self.read_template()
68        templ = self.output(declarations, py2bitexpr.print_prog(s), templ)
69        self.write_final_code(templ)
70        return s
71
72###############################################################
73
74if __name__ == '__main__':
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()
84
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 TracBrowser for help on using the repository browser.