source: proto/Compiler/CCGO.py @ 2709

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

Elim countBlockCarrysWithAdv1

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