Changeset 545 for proto/Compiler


Ignore:
Timestamp:
Jul 28, 2010, 4:46:27 PM (9 years ago)
Author:
eamiri
Message:

If statement introduced

Location:
proto/Compiler
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/bitexpr.py

    r454 r545  
     1# -*- coding: utf-8 -*-
    12
    23class Pragma:
     
    148149
    149150class WhileLoop(StmtList):
    150     def __init__(self, expr, stmts):
     151    def __init__(self, expr, stmts, fake = False):
    151152        self.control_expr = expr
    152153        self.carry_expr = None
    153154        StmtList.__init__(self, stmts)
     155        self.fake = fake
    154156        #self.stmt = stmts
    155157    def show(self):
    156158        rslt = ''
    157159        for s in self.loop_body: rslt += s.show() + '\n'
    158         return 'while (%s) {%s}' % (self.control_expr.show(), rslt)
     160        if (not fake): return 'while (%s) {%s}' % (self.control_expr.show(), rslt)
     161        else: return 'if (%s) {%s}' % (self.control_expr.show(), rslt)
    159162
    160163
  • proto/Compiler/bitstream_compiler.py

    r512 r545  
     1# -*- coding: utf-8 -*-
    12# bitstream_compiler.py - compile unbounded bitstreams code into
    23#   equivalent block-by-block code in C.
     
    5253        s = py2bitexpr.factor_out(s)
    5354        s, livelist = py2bitexpr.process_while_loops(s)
    54        
    5555
    5656        # BACK END
  • proto/Compiler/py2bitexpr.py

    r512 r545  
     1# -*- coding: utf-8 -*-
    12import ast, bitexpr, copy, basic_block
    23
     
    1112ARRAY_TEMPLATE="array_%s__%s_"
    1213
    13 
    1414class PyBitError(Exception):
    1515        pass
     
    1818## Extracting list of live variables
    1919#############################################################################################
     20
    2021def get_lives(s):
    2122    for item in s.body:
     
    3536## Function Inlining
    3637#############################################################################################
     38
    3739TEMP_VAR_TEMPLATE = "InlineTemp%i"
    3840
     
    8486    to_remove = []
    8587    for index, loc in enumerate(callee.body):
    86         if isinstance(loc, ast.While):
     88        if isinstance(loc, ast.While) or isinstance(loc, ast.If):
    8789            if not main:
    8890                to_remove.append(index)
     
    367369            del callee.body[-1]
    368370        main.body = main.body[:line_no]+callee.body+main.body[line_no:]
     371       
     372        #if callee.name == 'strct_byteclass__classify_bytes_':
     373        #  for item in module.body[func_dict["main"]].body:
     374        #      if isinstance(item, ast.Assign):
     375        #       if isinstance(item.targets[0], ast.Name):
     376        #           print item.targets[0].id
     377         
     378    #for item in module.body[func_dict["main"]].body:
     379    #   if isinstance(item, ast.Assign):
     380    #      if isinstance(item.targets[0], ast.Name):
     381    #         print item.targets[0].id
     382   
    369383    return module.body[func_dict["main"]].body
    370384
     
    442456                else:
    443457                    raise PyBitError("Bad PyBit function call: %s\n" % ast.dump(ast_expr))
    444         elif isinstance(ast_expr, ast.Compare):
     458        elif isinstance(ast_expr, ast.Compare):
    445459                if (isinstance(ast_expr.ops[0], ast.Gt) and (ast_expr.comparators[0].n==0)):
    446460                        e0 = translate(ast_expr.left)
     
    492506                        body = translate_stmts(s.body)
    493507                        translated.append(bitexpr.WhileLoop(bitexpr.isNoneZero(e), body))
     508                elif isinstance(s, ast.If):
     509                        e = translate(s.test)
     510                        body = translate_stmts(s.body)
     511                        translated.append(bitexpr.WhileLoop(bitexpr.isNoneZero(e), body, fake = True ))
    494512                else: raise PyBitError("Unknown PyBit statement type %s\n" % ast.dump(s))
    495513        return translated
     
    12861304        if isinstance(loc.RHS, bitexpr.Add):
    12871305            carries.append(loc.RHS.carry)
     1306        if isinstance(loc.RHS, bitexpr.Sub):
     1307            carries.append(loc.RHS.brw)
    12881308    for item in carries:
    12891309        newvar = bitexpr.Var(item+carry_suffix)
    1290         loop.stmts.append(bitexpr.BitAssign(newvar, bitexpr.Or(newvar, bitexpr.Var(item), carry_type)))
     1310        if (not loop.fake): loop.stmts.append(bitexpr.BitAssign(newvar, bitexpr.Or(newvar, bitexpr.Var(item), carry_type)))
    12911311    for item in carries:
    1292         loop.stmts.append(bitexpr.BitAssign( bitexpr.Var(item), bitexpr.FalseLiteral(carry_type) ))
     1312        if (not loop.fake): loop.stmts.append(bitexpr.BitAssign( bitexpr.Var(item), bitexpr.FalseLiteral(carry_type) ))
    12931313
    12941314    return loop, carries
     
    13251345            code.insert(key+1, bitexpr.BitAssign(carry_variable, bitexpr.Or(bitexpr.Var(update[key][1][0]), bitexpr.Var(update[key][1][1]), carry_type)))
    13261346        for item in update[key][1]:
    1327             code.insert(key+1, bitexpr.BitAssign(bitexpr.Var(item+carry_suffix), bitexpr.FalseLiteral(carry_type)))
     1347            if (not code[key].fake): code.insert(key+1, bitexpr.BitAssign(bitexpr.Var(item+carry_suffix), bitexpr.FalseLiteral(carry_type)))
    13281348        for item in update[key][1]:
    1329             code.insert(key+1, bitexpr.BitAssign(bitexpr.Var(item), bitexpr.Var(item+carry_suffix)))
     1349            if (not code[key].fake): code.insert(key+1, bitexpr.BitAssign(bitexpr.Var(item), bitexpr.Var(item+carry_suffix)))
    13301350
    13311351        code[key].carry_expr = bitexpr.isNoneZero(carry_variable)
     
    14701490        code += " "*indent+"}\n"
    14711491
    1472     elif isinstance(s[0], bitexpr.WhileLoop):
     1492    elif isinstance(s[0], bitexpr.WhileLoop) and (not s[0].fake):
    14731493        code = "\n%s((%s)||(test_carry(%s))) {\n"%(" "*indent+"while"+" ", generate_condition(s[0].control_expr), s[0].carry_expr.var.varname)
     1494        code += print_block_stmts(s[0].stmts, indent+indent_unit)
     1495        code += " "*indent+"}\n"
     1496    elif isinstance(s[0], bitexpr.WhileLoop) and (s[0].fake):
     1497        code = "\n%s((%s)||(test_carry(%s))) {\n"%(" "*indent+"if"+" ", generate_condition(s[0].control_expr), s[0].carry_expr.var.varname)
    14741498        code += print_block_stmts(s[0].stmts, indent+indent_unit)
    14751499        code += " "*indent+"}\n"
Note: See TracChangeset for help on using the changeset viewer.