source: proto/Compiler/CCGO.py @ 2801

Last change on this file since 2801 was 2801, checked in by cameron, 7 years ago

Add CarryThenFinalization? method for all CCGO objects - initially empty

File size: 8.3 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 GenerateCarryThenFinalization(self, block_no): return []
36    def GenerateCarryElseFinalization(self, block_no): return []
37    def GenerateLocalDeclare(self, block_no): return []
38    def GenerateCarryWhileTest(self, block_no, testExpr): return testExpr
39    def EnterLocalWhileBlock(self, operation_offset):  pass
40    def ExitLocalWhileBlock(self):  pass
41    def GenerateCarryWhileFinalization(self, block_no): return []
42    def GenerateStreamFunctionFinalization(self): return []
43
44#
45# Helpers
46def mkCall(fn_name, args):
47  if isinstance(fn_name, str): 
48        fn_name = ast.Name(fn_name, ast.Load())
49  return ast.Call(fn_name, args, [], None, None)
50
51def mkCallStmt(fn_name, args):
52  if isinstance(fn_name, str): fn_name = ast.Name(fn_name, ast.Load())
53  return ast.Expr(ast.Call(fn_name, args, [], None, None))
54 
55 
56#
57#  The testCCGO is a legacy class designed to duplicate the previous Pablo
58#  functionality of introducing a set of abstract macros into generated
59#  code (CarryArray, CarryTest, CarryDequeueEnqueue, CarryAdjust, etc.)
60#
61class testCCGO(CCGO):
62    def __init__(self, BLOCK_SIZE, carryInfoSet, carryGroupVarName='carryQ'):
63        self.BLOCK_SIZE = BLOCK_SIZE
64        self.carryInfoSet = carryInfoSet
65        self.carryGroupVar = carryGroupVarName
66
67    def allocate_all(self):
68        self.carryIndex = {}
69        self.advIndex = {}
70        self.operation_offset = 0
71        carry_counter = 0
72        adv_counter = 0
73        for op_no in range(self.carryInfoSet.operation_count):
74          self.carryIndex[op_no] = carry_counter
75          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
76          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
77          else: 
78            self.advIndex[op_no] = adv_counter
79            adv_counter += 1
80        # Add a dummy entry for any possible final block that is empty.
81        self.carryIndex[self.carryInfoSet.operation_count] = carry_counter
82        self.advIndex[self.carryInfoSet.operation_count] = adv_counter
83        self.carry_count = carry_counter
84
85    # Helper
86    def CarryGroupAtt(self, attname, CarryGroupVarName=""):
87        if CarryGroupVarName == '': CarryGroupVarName = self.carryGroupVar
88        return ast.Attribute(ast.Name(CarryGroupVarName, ast.Load()), attname, ast.Load())
89    def GenerateCarryDecls(self):
90        carry_counter = 0
91        adv_n_counter = 0
92        for op_no in range(self.carryInfoSet.block_op_count[0]):
93          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
94          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
95          else: adv_n_counter += 1
96        return "CarryArray<%i, %i> %s;" % (carry_counter, adv_n_counter, self.carryGroupVar)
97    def GenerateInitializations(self):
98        carry_counter = 0
99        adv_n_counter = 0
100        inits = ""
101        for op_no in range(self.carryInfoSet.block_op_count[0]):
102          if op_no in self.carryInfoSet.init_one_list: inits += "carryQ.cq[%s] = carryQ.carry_flip(carryQ.cq[%s]);\n" % (carry_counter, carry_counter)
103          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
104          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
105          else: adv_n_counter += 1
106        return inits
107    def GenerateStreamFunctionDecls(self):  return ""
108    def GenerateCarryInAccess(self, operation_no):
109        carry_index = self.carryIndex[operation_no - self.operation_offset]
110        return mkCall(self.carryGroupVar + "." + 'get_carry_in', [ast.Num(carry_index)])
111    def GenerateCarryOutStore(self, operation_no, carry_out_expr):
112        carry_index = self.carryIndex[operation_no - self.operation_offset]
113        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(carry_index)), ast.Store())], 
114                          mkCall("bitblock::srli<%i>" % (self.BLOCK_SIZE - 1), [carry_out_expr]))]
115    def GenerateAdvanceInAccess(self, operation_no):
116        adv_index = self.advIndex[operation_no - self.operation_offset]
117        return mkCall(self.carryGroupVar + "." + 'get_pending64', [ast.Num(adv_index)])
118    def GenerateAdvanceOutStore(self, operation_no, adv_out_expr):
119        adv_index = self.advIndex[operation_no - self.operation_offset]
120        cq_index = adv_index + self.carry_count
121        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(cq_index)), ast.Store())], 
122                           mkCall("bitblock::srli<64>", [adv_out_expr]))]
123
124    def GenerateTestAll(self, instance_name):
125        carry_count = self.carryInfoSet.block_op_count[0]
126        if carry_count == 0: return ast.Num(0)
127        else: 
128           #return "%s.carryQ.CarryTest(0, %i)" % (instance_name, carry_count)
129           return mkCall(ast.Attribute(ast.Attribute(ast.Name(instance_name, ast.Load()), self.carryGroupVar, ast.Load()), 'CarryTest', ast.Load()), 
130                         [ast.Num(0), ast.Num(carry_count)])
131    def GenerateCarryIfTest(self, block_no, ifTest):
132        carry_count = self.carryInfoSet.block_op_count[block_no]
133        if carry_count == 0: return ifTest
134        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
135        return ast.BoolOp(ast.Or(), [ifTest, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(ifIndex), ast.Num(carry_count)])])
136    def GenerateCarryElseFinalization(self, block_no): 
137        carry_count = self.carryInfoSet.block_op_count[block_no]
138        if carry_count == 0: return []
139        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
140        return [mkCallStmt(self.CarryGroupAtt('CarryDequeueEnqueue'), [ast.Num(ifIndex), ast.Num(carry_count)])]
141    def GenerateLocalDeclare(self, block_no):
142        local_carryvar = ast.Name("sub" + self.carryGroupVar, ast.Load())
143        return [mkCallStmt('LocalCarryDeclare', [local_carryvar, ast.Num(self.carryInfoSet.block_op_count[block_no])])]
144    def GenerateCarryWhileTest(self, block_no, testExpr):
145        carry_count = self.carryInfoSet.block_op_count[block_no]
146        if carry_count == 0: return testExpr
147        carry0 = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
148        return ast.BoolOp(ast.Or(), [testExpr, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(carry0), ast.Num(carry_count)])])
149    def EnterLocalWhileBlock(self, operation_offset): 
150        self.carryGroupVar = "sub" + self.carryGroupVar
151        self.operation_offset = operation_offset
152    def ExitLocalWhileBlock(self): 
153        self.operation_offset = 0
154        self.carryGroupVar = self.carryGroupVar[3:]
155    def GenerateCarryWhileFinalization(self, block_no): 
156        carry_count = self.carryInfoSet.block_op_count[block_no]
157        if carry_count == 0: return []
158        loopIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
159        return [mkCallStmt(self.CarryGroupAtt('CarryCombine'), [self.CarryGroupAtt('cq', "sub" + self.carryGroupVar), ast.Num(loopIndex), ast.Num(carry_count)])]
160    def GenerateStreamFunctionFinalization(self):
161        carry_count = self.carryInfoSet.carry_count + self.carryInfoSet.adv_1_count
162        if carry_count == 0: return []
163        else: return [mkCallStmt(self.CarryGroupAtt('CarryQ_Adjust'), [ast.Num(carry_count)])]
164
Note: See TracBrowser for help on using the repository browser.