Ignore:
Timestamp:
Dec 5, 2010, 5:39:43 AM (9 years ago)
Author:
cameron
Message:

Better encapsulation with MainLoopTransformer?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/pablo.py

    r772 r777  
    203203
    204204
    205 def Carry_decl_of_fn(fndef):
    206   carry_count = CarryCounter().count(fndef)
    207   if carry_count == 0: return ""
    208   else: return "  CarryDeclare(carryQ, %i);\n" % carry_count
    209 
    210                    
    211205class CarryIntro(ast.NodeTransformer):
    212206  def __init__(self, carryvar="carryQ", mode = "ci_co"):
     
    271265    self.generic_visit(whileNode)
    272266    local_decl = mkCallStmt('CarryDeclare', [ast.Name(local_carryvar, ast.Load()), ast.Num(carries)])
     267    local_init = mkCallStmt('CarryInit', [ast.Name(local_carryvar, ast.Load()), ast.Num(carries)])
    273268    inner_while.body.insert(0, local_decl)
     269    inner_while.body.insert(1, local_init)
    274270    final_combine = mkCallStmt('CarryCombine', [self.carryvar, ast.Name(local_carryvar, ast.Load()), ast.Num(carry_base), ast.Num(carries)])
    275271    inner_while.body.append(final_combine)
     
    365361#  Quick and dirty hacks for now - Dec. 2010.
    366362#
    367 def gen_declarations(main_module, main_fn):
    368   Cdecls = StreamStructGen().gen(main_module)
    369   Cdecls += BitBlock_decls_of_fn(main_fn)
    370   Cdecls += Carry_decl_of_fn(main_fn)
    371   return Cdecls
    372 
    373 def stream_stmts(main_fn):
    374   return StreamInitializations().xfrm(main_fn)
    375 
    376 def block_stmts(main_fn):
    377   AugAssignRemoval().xfrm(main_fn)
    378   Bitwise_to_SIMD().xfrm(main_fn)
    379   CarryIntro().xfrm_fndef(main_fn)
    380   return Cgen.py2C().gen(main_fn.body)
    381 
    382 def any_carry_expr(carryvar, carry_count):
    383   carry_arglist = [ast.Name(carryvar, ast.Load()), ast.Num(0), ast.Num(carry_count)] 
    384   return Cgen.py2C().gen(mkCall('CarryTest', carry_arglist))
     363
     364class MainLoopTransformer:
     365  def __init__(self, main_module, carry_var = "carryQ"):
     366    self.main_module = main_module
     367    self.main_fn = main_module.body[-1]
     368    assert (isinstance(self.main_fn, ast.FunctionDef))
     369    self.carry_count = CarryCounter().count(self.main_fn)
     370    self.carry_var = carry_var
     371  def any_carry_expr(self):
     372    if self.carry_count == 0: return "1"
     373    else: return "CarryTest(%s, 0, %i);\n" % (self.carry_var, self.carry_count)
     374
     375  def gen_declarations(self):
     376    self.Cdecls = StreamStructGen().gen(self.main_module)
     377    self.Cdecls += BitBlock_decls_of_fn(self.main_fn)
     378    if self.carry_count > 0: self.Cdecls += "CarryDeclare(%s, %i);\n" % (self.carry_var, self.carry_count)
     379  def gen_initializations(self):
     380    self.Cinits = ""
     381    if self.carry_count > 0: self.Cinits += "CarryInit(%s, %i);\n" % (self.carry_var, self.carry_count)
     382    self.Cinits += StreamInitializations().xfrm(self.main_module)
     383  def xfrm_block_stmts(self):
     384    AugAssignRemoval().xfrm(self.main_fn)
     385    Bitwise_to_SIMD().xfrm(self.main_fn)
     386    CarryIntro().xfrm_fndef(self.main_fn)
     387  def getCdecls(self):
     388    return self.Cdecls
     389  def getCinits(self):
     390    return self.Cinits
     391  def getCstmts(self):
     392    return Cgen.py2C().gen(self.main_fn.body)
    385393
    386394if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.