source: proto/Compiler/bitstream_compiler.py @ 512

Last change on this file since 512 was 512, checked in by cameron, 9 years ago

Eliminate int_carry parameter

File size: 3.9 KB
RevLine 
[306]1# bitstream_compiler.py - compile unbounded bitstreams code into
2#   equivalent block-by-block code in C.
3#
[365]4# (c) 2009 Ehsan Amiri and Robert D. Cameron
[306]5# All rights reserved.
6# Licensed to International Characters, Inc. under Academic Free License 3.0
7#
8
[429]9import ast, py2bitexpr, string, sys, copy
[297]10
[344]11class Program:
[434]12    def __init__(self, out_file, template_name = "template.c"):
[344]13        pass
[434]14        self.templ_name = template_name
[349]15        self.outfile_name = out_file
[308]16
[429]17    def output(self, decl, stream_stmts , block_stmts, templ):
[344]18        templ = string.replace(templ, '@decl', decl)
[429]19        templ = string.replace(templ, '@stream_stmts', stream_stmts)
20        templ = string.replace(templ, '@block_stmts', block_stmts)
[344]21        return templ
[319]22
[422]23    def read_template(self, file_path):
24        f=open(file_path + self.templ_name)
[344]25        inp = ""
26        for line in f:
27            inp += line
28        f.close()
29        return inp
[319]30
[344]31    def write_final_code(self, code):
32        f=open(self.outfile_name, 'w')
33        f.write(code)
34        f.close()
[308]35
[512]36    def generate_code(self, s, file_path):
[367]37        # FRONT END
[344]38        s = ast.parse(s)
[363]39        livelist,s = py2bitexpr.get_lives(s)
40        livelist.append('error_mask')
[421]41        s = py2bitexpr.do_inlining(s, file_path)
[344]42        s = py2bitexpr.translate_stmts(s)
[368]43
[367]44        # OPTIMIZATION
[344]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)
[354]51        all_lives, s = py2bitexpr.eliminate_dead_code(s, set(livelist))
[369]52        s = py2bitexpr.factor_out(s)
[512]53        s, livelist = py2bitexpr.process_while_loops(s)
[429]54       
[297]55
[367]56        # BACK END
[512]57        declarations = py2bitexpr.gen_declarations(s)
[422]58        templ = self.read_template(file_path)
[297]59
[512]60        templ = self.output(declarations, py2bitexpr.print_stream_stmts(copy.deepcopy(s)), py2bitexpr.print_block_stmts(s), templ)
[344]61        self.write_final_code(templ)
62        return s
[319]63
[434]64OPTION_TEMPLATE_NAME = '-t'
[429]65
[434]66
[429]67def read_flags (params):
68    flags = []
69    non_flags = []
[434]70    last_one_continues = False
[429]71    l = len(params)
72    for i in range(l):
[434]73        if params[i][0] == '-' or last_one_continues:
[429]74            flags.append(params[i])
[434]75            if params[i]  == OPTION_TEMPLATE_NAME:
76                last_one_continues = True
77            else:
78                last_one_continues = False
[429]79        else:
80            non_flags.append(params[i])
81
82    return flags, non_flags
83
[434]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
[429]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
[434]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   
[512]115    return template_name
[344]116###############################################################
[319]117
[368]118if __name__ == '__main__':
[349]119        if len(sys.argv) < 2:
[429]120            print "Usage: python bitstream_compiler.py <input file> [output file] options"
[363]121            #exit()
122
[429]123        flags, non_flags = read_flags(sys.argv)
[434]124        print non_flags
[429]125        last_slash = non_flags[1].rfind("/")
126        file_path = non_flags[1][:last_slash+1]
[421]127       
[429]128        out_file = "code.c"
129        if len(non_flags) >= 3:
130            out_file = non_flags[2]
[421]131       
[512]132        template_name = read_options(flags)
[434]133       
[512]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.