source: proto/Compiler/bitstream_compiler.py @ 434

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

-t option added.

File size: 4.4 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, template_name = "template.c"):
13        pass
14        self.templ_name = template_name
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
64OPTION_TEMPLATE_NAME = '-t'
65OPTION_INT_CARRY = '-int_carry'
66OPTION_SIMD_CARRY = '-simd-carry'
67
68
69def read_flags (params):
70    flags = []
71    non_flags = []
72    last_one_continues = False
73    l = len(params)
74    for i in range(l):
75        if params[i][0] == '-' or last_one_continues:
76            flags.append(params[i])
77            if params[i]  == OPTION_TEMPLATE_NAME:
78                last_one_continues = True
79            else:
80                last_one_continues = False
81        else:
82            non_flags.append(params[i])
83
84    return flags, non_flags
85
86def option_carry(flags):
87    l = len(flags)
88    for i in range(l):
89        if flags[i] == OPTION_SIMD_CARRY:
90            return False, i
91        if flags[i] == OPTION_INT_CARRY:
92            return True, i
93    return True, -1 #default carry type is int
94
95def option_template(flags):
96    t_name = "template.c"
97    l = len(flags)
98    for i in range(l):
99        if flags[i] == '-t':
100            if len(flags) == i+1:
101                print flags
102                print "WARNING: -t flag used but no template name is given. Default name is used."
103            else:
104                t_name = flags[i+1]
105                return t_name, i
106
107    return t_name, -1
108
109def read_input(inp_file_name):
110    mycode = ""
111    inp_file = open(non_flags[1])
112    for line in inp_file:
113        mycode += line
114    inp_file.close()
115    return mycode
116
117def read_options(flags):
118    carry_option, index = option_carry(flags)
119    if index >= 0:
120        del flags[index]
121   
122    template_name, index = option_template(flags)
123    if index >= 0:
124        del flags[index+1]
125        del flags[index]
126   
127    if len(flags) > 0:
128        print "WARNING: Unrecognized/duplicated options found!"
129   
130    return carry_option, template_name
131###############################################################
132
133if __name__ == '__main__':
134        if len(sys.argv) < 2:
135            print "Usage: python bitstream_compiler.py <input file> [output file] options"
136            #exit()
137
138        flags, non_flags = read_flags(sys.argv)
139        print non_flags
140        last_slash = non_flags[1].rfind("/")
141        file_path = non_flags[1][:last_slash+1]
142       
143        out_file = "code.c"
144        if len(non_flags) >= 3:
145            out_file = non_flags[2]
146       
147        carry_option, template_name = read_options(flags)
148       
149        s = Program(out_file, template_name).generate_code(read_input(non_flags[1]), file_path, carry_option)
Note: See TracBrowser for help on using the repository browser.