source: proto/Compiler/bitstream_compiler.py @ 604

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

If statement introduced

File size: 4.0 KB
Line 
1# -*- coding: utf-8 -*-
2# bitstream_compiler.py - compile unbounded bitstreams code into
3#   equivalent block-by-block code in C.
4#
5# (c) 2009 Ehsan Amiri and Robert D. Cameron
6# All rights reserved.
7# Licensed to International Characters, Inc. under Academic Free License 3.0
8#
9
10import ast, py2bitexpr, string, sys, copy
11
12class Program:
13    def __init__(self, out_file, template_name = "template.c"):
14        pass
15        self.templ_name = template_name
16        self.outfile_name = out_file
17
18    def output(self, decl, stream_stmts , block_stmts, templ):
19        templ = string.replace(templ, '@decl', decl)
20        templ = string.replace(templ, '@stream_stmts', stream_stmts)
21        templ = string.replace(templ, '@block_stmts', block_stmts)
22        return templ
23
24    def read_template(self, file_path):
25        f=open(file_path + self.templ_name)
26        inp = ""
27        for line in f:
28            inp += line
29        f.close()
30        return inp
31
32    def write_final_code(self, code):
33        f=open(self.outfile_name, 'w')
34        f.write(code)
35        f.close()
36
37    def generate_code(self, s, file_path):
38        # FRONT END
39        s = ast.parse(s)
40        livelist,s = py2bitexpr.get_lives(s)
41        livelist.append('error_mask')
42        s = py2bitexpr.do_inlining(s, file_path)
43        s = py2bitexpr.translate_stmts(s)
44
45        # OPTIMIZATION
46        st = py2bitexpr.gen_sym_table(s)
47        s=py2bitexpr.make_SSA(s, st)
48        s = py2bitexpr.partition2bb(s, )
49        s = py2bitexpr.apply_all_opt(s)
50        s = py2bitexpr.normalize(s)
51        py2bitexpr.simplify_tree(s)
52        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
53        s = py2bitexpr.factor_out(s)
54        s, livelist = py2bitexpr.process_while_loops(s)
55
56        # BACK END
57        declarations = py2bitexpr.gen_declarations(s)
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), templ)
61        self.write_final_code(templ)
62        return s
63
64OPTION_TEMPLATE_NAME = '-t'
65
66
67def read_flags (params):
68    flags = []
69    non_flags = []
70    last_one_continues = False
71    l = len(params)
72    for i in range(l):
73        if params[i][0] == '-' or last_one_continues:
74            flags.append(params[i])
75            if params[i]  == OPTION_TEMPLATE_NAME:
76                last_one_continues = True
77            else:
78                last_one_continues = False
79        else:
80            non_flags.append(params[i])
81
82    return flags, non_flags
83
84def option_template(flags):
85    t_name = "template.c"
86    l = len(flags)
87    for i in range(l):
88        if flags[i] == '-t':
89            if len(flags) == i+1:
90                print flags
91                print "WARNING: -t flag used but no template name is given. Default name is used."
92            else:
93                t_name = flags[i+1]
94                return t_name, i
95
96    return t_name, -1
97
98def read_input(inp_file_name):
99    mycode = ""
100    inp_file = open(non_flags[1])
101    for line in inp_file:
102        mycode += line
103    inp_file.close()
104    return mycode
105
106def read_options(flags):
107    template_name, index = option_template(flags)
108    if index >= 0:
109        del flags[index+1]
110        del flags[index]
111   
112    if len(flags) > 0:
113        print "WARNING: Unrecognized/duplicated options found!"
114   
115    return template_name
116###############################################################
117
118if __name__ == '__main__':
119        if len(sys.argv) < 2:
120            print "Usage: python bitstream_compiler.py <input file> [output file] options"
121            #exit()
122
123        flags, non_flags = read_flags(sys.argv)
124        print non_flags
125        last_slash = non_flags[1].rfind("/")
126        file_path = non_flags[1][:last_slash+1]
127       
128        out_file = "code.c"
129        if len(non_flags) >= 3:
130            out_file = non_flags[2]
131       
132        template_name = read_options(flags)
133       
134        s = Program(out_file, template_name).generate_code(read_input(non_flags[1]), file_path)
Note: See TracBrowser for help on using the repository browser.