source: proto/Compiler/CCGO.py @ 2798

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

Add CCGO GenerateTestAll? method for @any_carry support

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