source: proto/Compiler/bitstream_compiler.py @ 429

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

-simd-carry option added

File size: 3.2 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, copy
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, stream_stmts , block_stmts, templ):
18        templ = string.replace(templ, '@decl', decl)
19        templ = string.replace(templ, '@stream_stmts', stream_stmts)
20        templ = string.replace(templ, '@block_stmts', block_stmts)
21        return templ
22
23    def read_template(self, file_path):
24        f=open(file_path + 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, file_path, int_carry):
37        # FRONT END
38        s = ast.parse(s)
39        livelist,s = py2bitexpr.get_lives(s)
40        livelist.append('error_mask')
41        s = py2bitexpr.do_inlining(s, file_path)
42        s = py2bitexpr.translate_stmts(s)
43
44        # OPTIMIZATION
45        st = py2bitexpr.gen_sym_table(s)
46        s=py2bitexpr.make_SSA(s, st)
47        s = py2bitexpr.partition2bb(s, )
48        s = py2bitexpr.apply_all_opt(s)
49        s = py2bitexpr.normalize(s)
50        py2bitexpr.simplify_tree(s)
51        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
52        s = py2bitexpr.factor_out(s)
53        s, livelist = py2bitexpr.process_while_loops(s, int_carry)
54       
55
56        # BACK END
57        declarations = py2bitexpr.gen_declarations(s, int_carry)
58        templ = self.read_template(file_path)
59
60        templ = self.output(declarations, py2bitexpr.print_stream_stmts(copy.deepcopy(s)), py2bitexpr.print_block_stmts(s, int_carry), templ)
61        self.write_final_code(templ)
62        return s
63
64
65def read_flags (params):
66    flags = []
67    non_flags = []
68    l = len(params)
69    for i in range(l):
70        if params[i][0] == '-':
71            flags.append(params[i])
72        else:
73            non_flags.append(params[i])
74
75    return flags, non_flags
76
77def int_carry(flags):
78    l = len(flags)
79    for i in range(l):
80        if flags[i] == '-simd-carry':
81            return False
82        if flags[i] == '-int-carry':
83            return True
84    return True #default carry type is int
85
86def read_input(inp_file_name):
87    mycode = ""
88    inp_file = open(non_flags[1])
89    for line in inp_file:
90        mycode += line
91    inp_file.close()
92    return mycode
93###############################################################
94
95if __name__ == '__main__':
96        if len(sys.argv) < 2:
97            print "Usage: python bitstream_compiler.py <input file> [output file] options"
98            #exit()
99
100        flags, non_flags = read_flags(sys.argv)
101        last_slash = non_flags[1].rfind("/")
102        file_path = non_flags[1][:last_slash+1]
103       
104        out_file = "code.c"
105        if len(non_flags) >= 3:
106            out_file = non_flags[2]
107       
108        s = Program(out_file).generate_code(read_input(non_flags[1]), file_path, int_carry(flags))
Note: See TracBrowser for help on using the repository browser.