source: proto/Compiler/CCGO.py @ 2799

Last change on this file since 2799 was 2799, checked in by cameron, 6 years ago

Separate allocation from init, add BLOCK_SIZE parameter, tidy

File size: 8.2 KB
Line 
1#
2# CCGO.py
3#
4# Copyright 2012, Robert D. Cameron
5# All rights reserved.
6#
7# Carry Code Generator Objects (CCGOs)
8#
9# CCGOs are objects which implement a particular strategy for incorporating
10# carry processing into generated Pablo code.
11#
12# A CCGO consists of methods that can be used to insert carry processing
13# code at particular locations in a Pablo program.   The CCGO is intended
14# to be a parameter to a CarryIntro tree transformer, abstracting away
15# the strategy from the tree traversal to apply the strategy to
16# a particular stream function.
17#
18import ast
19#
20# The base CCGO Class is also the null class, suitable for stream
21# functions that have no carry-generating operations.
22#
23class CCGO:
24    def __init__(self): pass
25    def allocate_all(self): pass
26    def GenerateCarryDecls(self): return ""
27    def GenerateInitializations(self): return ""
28    def GenerateStreamFunctionDecls(self):  return ""
29    def GenerateCarryInAccess(self, operation_no): return None
30    def GenerateCarryOutStore(self, operation_no, carry_out_expr): return []
31    def GenerateAdvanceInAccess(self, operation_no): return None
32    def GenerateAdvanceOutStore(self, operation_no, carry_out_expr): return []
33    def GenerateTestAll(self, instance_name): return ast.Num(0)
34    def GenerateCarryIfTest(self, block_no, ifTest): return ifTest
35    def GenerateCarryElseFinalization(self, block_no): return []
36    def GenerateLocalDeclare(self, block_no): return []
37    def GenerateCarryWhileTest(self, block_no, testExpr): return testExpr
38    def EnterLocalWhileBlock(self, operation_offset):  pass
39    def ExitLocalWhileBlock(self):  pass
40    def GenerateCarryWhileFinalization(self, block_no): return []
41    def GenerateStreamFunctionFinalization(self): return []
42
43#
44# Helpers
45def mkCall(fn_name, args):
46  if isinstance(fn_name, str): 
47        fn_name = ast.Name(fn_name, ast.Load())
48  return ast.Call(fn_name, args, [], None, None)
49
50def mkCallStmt(fn_name, args):
51  if isinstance(fn_name, str): fn_name = ast.Name(fn_name, ast.Load())
52  return ast.Expr(ast.Call(fn_name, args, [], None, None))
53 
54 
55#
56#  The testCCGO is a legacy class designed to duplicate the previous Pablo
57#  functionality of introducing a set of abstract macros into generated
58#  code (CarryArray, CarryTest, CarryDequeueEnqueue, CarryAdjust, etc.)
59#
60class testCCGO(CCGO):
61    def __init__(self, BLOCK_SIZE, carryInfoSet, carryGroupVarName='carryQ'):
62        self.BLOCK_SIZE = BLOCK_SIZE
63        self.carryInfoSet = carryInfoSet
64        self.carryGroupVar = carryGroupVarName
65
66    def allocate_all(self):
67        self.carryIndex = {}
68        self.advIndex = {}
69        self.operation_offset = 0
70        carry_counter = 0
71        adv_counter = 0
72        for op_no in range(self.carryInfoSet.operation_count):
73          self.carryIndex[op_no] = carry_counter
74          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
75          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
76          else: 
77            self.advIndex[op_no] = adv_counter
78            adv_counter += 1
79        # Add a dummy entry for any possible final block that is empty.
80        self.carryIndex[self.carryInfoSet.operation_count] = carry_counter
81        self.advIndex[self.carryInfoSet.operation_count] = adv_counter
82        self.carry_count = carry_counter
83
84    # Helper
85    def CarryGroupAtt(self, attname, CarryGroupVarName=""):
86        if CarryGroupVarName == '': CarryGroupVarName = self.carryGroupVar
87        return ast.Attribute(ast.Name(CarryGroupVarName, ast.Load()), attname, ast.Load())
88    def GenerateCarryDecls(self):
89        carry_counter = 0
90        adv_n_counter = 0
91        for op_no in range(self.carryInfoSet.block_op_count[0]):
92          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
93          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
94          else: adv_n_counter += 1
95        return "CarryArray<%i, %i> %s;" % (carry_counter, adv_n_counter, self.carryGroupVar)
96    def GenerateInitializations(self):
97        carry_counter = 0
98        adv_n_counter = 0
99        inits = ""
100        for op_no in range(self.carryInfoSet.block_op_count[0]):
101          if op_no in self.carryInfoSet.init_one_list: inits += "carryQ.cq[%s] = carryQ.carry_flip(carryQ.cq[%s]);\n" % (carry_counter, carry_counter)
102          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
103          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
104          else: adv_n_counter += 1
105        return inits
106    def GenerateStreamFunctionDecls(self):  return ""
107    def GenerateCarryInAccess(self, operation_no):
108        carry_index = self.carryIndex[operation_no - self.operation_offset]
109        return mkCall(self.carryGroupVar + "." + 'get_carry_in', [ast.Num(carry_index)])
110    def GenerateCarryOutStore(self, operation_no, carry_out_expr):
111        carry_index = self.carryIndex[operation_no - self.operation_offset]
112        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(carry_index)), ast.Store())], 
113                          mkCall("bitblock::srli<%i>" % (self.BLOCK_SIZE - 1), [carry_out_expr]))]
114    def GenerateAdvanceInAccess(self, operation_no):
115        adv_index = self.advIndex[operation_no - self.operation_offset]
116        return mkCall(self.carryGroupVar + "." + 'get_pending64', [ast.Num(adv_index)])
117    def GenerateAdvanceOutStore(self, operation_no, adv_out_expr):
118        adv_index = self.advIndex[operation_no - self.operation_offset]
119        cq_index = adv_index + self.carry_count
120        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(cq_index)), ast.Store())], 
121                           mkCall("bitblock::srli<64>", [adv_out_expr]))]
122
123    def GenerateTestAll(self, instance_name):
124        carry_count = self.carryInfoSet.block_op_count[0]
125        if carry_count == 0: return ast.Num(0)
126        else: 
127           #return "%s.carryQ.CarryTest(0, %i)" % (instance_name, carry_count)
128           return mkCall(ast.Attribute(ast.Attribute(ast.Name(instance_name, ast.Load()), self.carryGroupVar, ast.Load()), 'CarryTest', ast.Load()), 
129                         [ast.Num(0), ast.Num(carry_count)])
130    def GenerateCarryIfTest(self, block_no, ifTest):
131        carry_count = self.carryInfoSet.block_op_count[block_no]
132        if carry_count == 0: return ifTest
133        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
134        return ast.BoolOp(ast.Or(), [ifTest, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(ifIndex), ast.Num(carry_count)])])
135    def GenerateCarryElseFinalization(self, block_no): 
136        carry_count = self.carryInfoSet.block_op_count[block_no]
137        if carry_count == 0: return []
138        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
139        return [mkCallStmt(self.CarryGroupAtt('CarryDequeueEnqueue'), [ast.Num(ifIndex), ast.Num(carry_count)])]
140    def GenerateLocalDeclare(self, block_no):
141        local_carryvar = ast.Name("sub" + self.carryGroupVar, ast.Load())
142        return [mkCallStmt('LocalCarryDeclare', [local_carryvar, ast.Num(self.carryInfoSet.block_op_count[block_no])])]
143    def GenerateCarryWhileTest(self, block_no, testExpr):
144        carry_count = self.carryInfoSet.block_op_count[block_no]
145        if carry_count == 0: return testExpr
146        carry0 = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
147        return ast.BoolOp(ast.Or(), [testExpr, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(carry0), ast.Num(carry_count)])])
148    def EnterLocalWhileBlock(self, operation_offset): 
149        self.carryGroupVar = "sub" + self.carryGroupVar
150        self.operation_offset = operation_offset
151    def ExitLocalWhileBlock(self): 
152        self.operation_offset = 0
153        self.carryGroupVar = self.carryGroupVar[3:]
154    def GenerateCarryWhileFinalization(self, block_no): 
155        carry_count = self.carryInfoSet.block_op_count[block_no]
156        if carry_count == 0: return []
157        loopIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
158        return [mkCallStmt(self.CarryGroupAtt('CarryCombine'), [self.CarryGroupAtt('cq', "sub" + self.carryGroupVar), ast.Num(loopIndex), ast.Num(carry_count)])]
159    def GenerateStreamFunctionFinalization(self):
160        carry_count = self.carryInfoSet.carry_count + self.carryInfoSet.adv_1_count
161        if carry_count == 0: return []
162        else: return [mkCallStmt(self.CarryGroupAtt('CarryQ_Adjust'), [ast.Num(carry_count)])]
163
Note: See TracBrowser for help on using the repository browser.