source: proto/Compiler/bitstream_compiler.py @ 421

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

input file can be in a different directory than the compiler

File size: 2.5 KB
Line 
1# bitstream_compiler.py - compile unbounded bitstreams code into
2#   equivalent block-by-block code in C.
3#
4# (c) 2009 Ehsan Amiri and Robert D. Cameron
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(sys.path[0] + "/" + 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, file_path):
36        # FRONT END
37        s = ast.parse(s)
38        livelist,s = py2bitexpr.get_lives(s)
39        livelist.append('error_mask')
40        s = py2bitexpr.do_inlining(s, file_path)
41        s = py2bitexpr.translate_stmts(s)
42
43        # OPTIMIZATION
44        st = py2bitexpr.gen_sym_table(s)
45        s=py2bitexpr.make_SSA(s, st)
46        s = py2bitexpr.partition2bb(s, )
47        s = py2bitexpr.apply_all_opt(s)
48        s = py2bitexpr.normalize(s)
49        py2bitexpr.simplify_tree(s)
50        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
51        s = py2bitexpr.factor_out(s)
52        s, livelist = py2bitexpr.process_while_loops(s)
53
54        # BACK END
55        declarations = py2bitexpr.gen_declarations(s)
56        templ = self.read_template()
57
58        templ = self.output(declarations, py2bitexpr.print_prog(s), templ)
59        self.write_final_code(templ)
60        return s
61
62###############################################################
63
64if __name__ == '__main__':
65        mycode = ""
66        if len(sys.argv) < 2:
67            print "Usage: python bitstream_compiler.py <input file> [output file]"
68            #exit()
69
70        #inp_file = open("parabix2.py")
71        inp_file = open(sys.argv[1])
72        last_slash = sys.argv[1].rfind("/")
73        file_path = sys.argv[1][:last_slash+1]
74       
75       
76       
77        for line in inp_file:
78            mycode += line
79        inp_file.close()
80
81        out_file = "code.c"
82        if len(sys.argv) >= 3:
83            out_file = sys.argv[2]
84        s = Program(out_file).generate_code(mycode, file_path)
Note: See TracBrowser for help on using the repository browser.