Ignore:
Timestamp:
Aug 13, 2010, 6:25:09 PM (9 years ago)
Author:
eamiri
Message:

some comments added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/py2bitexpr.py

    r555 r576  
    1919#############################################################################################
    2020
     21# Extracs the list of live variables from the very last line of function "main"
     22# This must be a "return" statement
    2123def get_lives(s):
    2224    for item in s.body:
     
    5658    pass
    5759
     60# collects all functions in this module and in imported modules.
     61# It starts by collecting a list of imported modules
    5862def collect_functions(module, file_path):
    5963    imps = []
     
    8387
    8488
     89#Given a function it removes any line of code in that function that is not supported by the compiler
    8590def remove_unsupported_code(callee, fn_names, predefined, main=False):
    8691    to_remove = []
     
    127132
    128133
     134# Iterates over all functions and cleans them up from code that is not supported by the compiler
    129135def clean_functions(module, fn_names, predefined):
    130136    new = []
     
    137143    return module
    138144
     145#creates a dictionary of functions and their index in the list of statements.
    139146def collect_names(body):
    140147    names = {}
     
    144151
    145152    return names
     153
    146154
    147155def modify_name(var, args, cargs, unique_prefix):
     
    184192    return exp
    185193
     194#generates a list of all function calls within function "main".
    186195def get_all_calls(main, all_func):
    187196    call_list = []
     
    197206    return call_list
    198207
     208#modifies names of variables in "callee" by adding a prefix. This is a necessary part of inlining.
    199209def update_var_names(callee, args):
    200210    unique_prefix = '_'+callee.name.replace('.', '_')+'_'
     
    227237        assert(1==0)
    228238
     239
    229240def modify_assignments_in_caller(main, line_no, return_list, prefix, callee):
    230241    if isinstance(main.body[line_no].targets[0], ast.Tuple):
     
    264275    return main
    265276
     277#returns a list of all variables returned by the given function
    266278def get_return_list(callee):
    267279    ret_list = []
     
    279291    return ret_list
    280292
     293# returns a list of variables that are updated when the given line of code(loc) is run.
    281294def get_updated(loc):
    282295    if isinstance(loc, ast.AugAssign):
     
    291304    return updated
    292305
     306#Extracts information about variables returned by the given function. The second argument ret_list
     307# is a list of variables that function "callee" reutrns.
    293308def get_update_details(callee, ret_list):
    294309    field_info = {}
     
    307322    return field_info
    308323
     324#Extracts some information about variables that "callee" returns. This is necessary for inlining.
    309325def process_return(callee):
    310326    if not isinstance(callee.body[-1], ast.Return):
     
    314330    return field_info
    315331
     332#Generates the code equivalent to initialization of a list.
    316333def generate_expanded_code(loc):
    317334    new_code = []
     
    323340    return new_code
    324341
     342#List initializations are expanded to a number of single variable initializations
    325343def expand_list_init(module):
    326344    for item in module.body:
     
    339357    return module
    340358
     359#Returns a list of predefined functions. These are usually primitives on bit streams.
    341360def get_predefined_funcs():
    342361    advance = ast.Attribute(value=ast.Name(id="bitutil"), attr="Advance")
     
    345364    return [translate_var(advance), translate_var(scan_thru), translate_var(optimize)]
    346365
     366
     367#This function is called to do inlining.
    347368def do_inlining(module, file_path):
    348369    module = collect_functions(module, file_path)
     
    403424        return iscall and len(fncall.args) == 2 and fncall.kwargs == None and fncall.starargs == None
    404425
     426#Convertnig Python AST to our own AST
    405427def translate(ast_expr):
    406428        if isinstance(ast_expr, ast.Name):
     
    512534        return translated
    513535#############################################################################################
    514 ## Conversion to SSA form --- Pass 3
     536## Conversion to SSA form
    515537#############################################################################################
    516538
     
    542564    return table
    543565
     566#generating symbol table. goInside is a flag that determines whether or not
     567#the symbol table cover body of a while loop
    544568def gen_sym_table(code, goInside = False):
    545569    """Generate a simple symbol table for a three address code
     
    612636            assert(1==0)
    613637
     638#Variable name changes in order to convert the code to SSA
     639#This function applies the change to the line that defines a variable/
    614640def update_def(code, var, line, suffix_num):
    615641    loc = code[line]
     
    621647        pass
    622648
     649# variable name  (for a variable that appears on the right side of an assignment)
     650# is replaced by its new name
    623651def update_rhs(rhs, varname, newname):
    624652    if isinstance(rhs, bitexpr.Var):
     
    633661        rhs.operand1 = update_rhs(rhs.operand1, varname, newname)
    634662        return rhs
    635     #So it is a binary operation WHAT ABOUT ADD AND SUBTRACT?
     663       
    636664    rhs.operand1 = update_rhs(rhs.operand1, varname, newname)
    637665    rhs.operand2 = update_rhs(rhs.operand2, varname, newname)
    638666    return rhs
    639667
     668#Changing the name of a variable
    640669def update_use(code, var, line, suffix_num):
    641670    loc = code[line]
     
    675704    assert(1==0)
    676705
     706#pairs([1,2,3,4,5]) ==> [(1,2), (2,3), (3,4), (4,5), (5,1)]
    677707def pairs(lst):
    678708    if lst == []:
     
    704734
    705735#################################################################################################################
    706 ## Breaking the code into basic blocks depending on where optimize pragma appears --- Pass 4
     736## Breaking the code into basic blocks depending on where optimize pragma appears
    707737#################################################################################################################
    708738def get_opt_list(s):
     
    772802    return is_defined
    773803
     804#
    774805def adjust_earliest(s, earliest, varname):
    775806    if earliest == -1:
     
    784815    return earliest
    785816
     817#breaking the code into three segments
     818#currently the third segment should be empty because stop is
     819#chosen to be the last line of the code
    786820def chop_code(s, start, stop):
    787821    line = 0
     
    810844    return s[:cut1+1], s[cut1+1:cut2+1], s[cut2+1:]
    811845
     846#counting lines of code
    812847def count_lines(code):
    813848    cnt = len(code)
     
    817852    return cnt
    818853
    819 
     854#generates the condition of the if statement
    820855def gen_cond_obj(opt_pragma):
    821856    if opt_pragma[1] == 'AllZero' or opt_pragma[1] == 'allzero':
     
    825860    return cond_obj
    826861
     862
     863
    827864def gen_bb(s, opt_list, def_line):
    828865    assert (len(opt_list) == len(def_line))
     
    831868
    832869    earliest = min(def_line)
     870   
     871    #latest is set to length of the code
     872    #This means up to the end of the code is considered for optimization
    833873    latest = len(s)
    834874    earliest_index = def_line.index(earliest)
     
    850890    return result
    851891
     892#Creating basic blocks required for implementation of optimize pragma (if..then..else statements)
    852893def partition2bb(s):
    853894    basicblocks = []
     
    858899    st2 = gen_sym_table(s)
    859900
     901    # This loop iterates over variables that has appeared in an optimize pragma.
     902    # The line in which each of these variables is defined is found and appended to "def_line"
    860903    for item in opt_list:
    861904        if len(st2[item[0]][0]) > 0:
     
    903946
    904947#################################################################################################################
    905 ## Generating declarations for variables. This pass is based on the syntax of C programming language --- Pass 5
     948## Generating declarations for variables. This pass is based on the syntax of C programming language
    906949#################################################################################################################
    907950def get_vars(stmt):
     
    10301073    vd = gen_var_dic(s)
    10311074    return gen_output(vd)
     1075
    10321076#################################################################################################################
    10331077## This class replaces all occurrences of a reduced variable to its value --- Pass 7
     
    10991143 
    11001144#################################################################################################################
    1101 ## Simplifying conditions-tree by applying various optimizations like: constant and copy propagation. --- Pass 8
     1145## Simplifying conditions-tree by applying various optimizations like: constant and copy propagation.
    11021146## method prune is supposed to remove unreachable branches but it is incomplete
    11031147#################################################################################################################
    11041148
     1149#NOT USED
    11051150def prune(fixed, tree):
    11061151    """removes unreachable branches of the tree"""
     
    11441189    for i in empty_list:
    11451190        del tree[i]
     1191       
     1192
    11461193def filter_fixed(fixed, stmts):
    11471194    for loc in stmts:
     
    11551202    return fixed
    11561203
     1204#Optimizing the code using constant and copy propagation and rules of boolean algebra.
    11571205def simplify_tree(code, fixed = {}, prev = []):
    11581206    if len(code) == 0:
     
    11881236## Dead Code Elimination
    11891237#################################################################################################################
     1238#NOT USED
    11901239def get_effective_name(varname):
    11911240    if varname.startswith("array") or varname.startswith("strct"):
     
    11971246    return varname
    11981247
     1248#Checks whether or not this line of code can be removed.
     1249#It may also return new variables that must be kept and not removed by
     1250#dead code elimination
    11991251def check_loc(loc, must_liv):
    1200 
    1201     #effective_name = get_effective_name(loc.LHS.varname)
    12021252    effective_name = loc.LHS.varname
    1203     if effective_name in must_liv or loc.LHS.varname in must_liv:
     1253    if effective_name in must_liv:
    12041254        if isinstance(loc.RHS, bitexpr.Not):
    12051255            return set([loc.RHS.operand1.varname]), False
     
    12281278    return bb
    12291279
     1280#Actually removes the dead  code  from a basic block
    12301281def remove_dead(bb, must_live):
    12311282    bb = remove_copies(bb)
     
    12461297    return my_alives, bb
    12471298
     1299#Get variables that are used in the loop and must not be removed. For example
     1300#variables that are used in the condition of a loop.
    12481301def get_loop_variables(code):
    12491302    must_live = set([])
     
    12591312    return must_live
    12601313
     1314
     1315#Traverses code from the end to the begniing, one basic block at a time, to detect dead code and remove it.
    12611316def eliminate_dead_code(tree, must_live):
    12621317    if len(tree) == 0:
     
    12961351#################################################################################################################
    12971352## This pass processes the code in the while loop and adds extra code required for handling carry variables
     1353## It also updates condition of the loop.
    12981354#################################################################################################################
    12991355carry_suffix = "_i"
     
    14361492    return code
    14371493
     1494#Factors out the common code at the end of two basic blocksof two branches of an if..then..else statement
    14381495def factor_out(code):
    14391496    for index, loc in enumerate(code):
     
    14531510
    14541511#################################################################################################################
    1455 ## Generates C code given conditions-tree, by a recursive traversal of the tree --- Pass 10
    1456 ## ***Changes needed here are the same as Pass 7 and Pass 10***
     1512## Generates C code given conditions-tree, by a recursive traversal of the tree
    14571513#################################################################################################################
    14581514def generate_condition(expr):
Note: See TracChangeset for help on using the changeset viewer.