source: proto/Compiler/CCGO.py @ 2909

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

Nested while fix

File size: 8.5 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        self.offset_stack = []
72        carry_counter = 0
73        adv_counter = 0
74        for op_no in range(self.carryInfoSet.operation_count):
75          self.carryIndex[op_no] = carry_counter
76          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
77          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
78          else: 
79            self.advIndex[op_no] = adv_counter
80            adv_counter += 1
81        # Add a dummy entry for any possible final block that is empty.
82        self.carryIndex[self.carryInfoSet.operation_count] = carry_counter
83        self.advIndex[self.carryInfoSet.operation_count] = adv_counter
84        self.carry_count = carry_counter
85
86    # Helper
87    def CarryGroupAtt(self, attname, CarryGroupVarName=""):
88        if CarryGroupVarName == '': CarryGroupVarName = self.carryGroupVar
89        return ast.Attribute(ast.Name(CarryGroupVarName, ast.Load()), attname, ast.Load())
90    def GenerateCarryDecls(self):
91        carry_counter = 0
92        adv_n_counter = 0
93        for op_no in range(self.carryInfoSet.block_op_count[0]):
94          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
95          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
96          else: adv_n_counter += 1
97        return "CarryArray<%i, %i> %s;" % (carry_counter, adv_n_counter, self.carryGroupVar)
98    def GenerateInitializations(self):
99        carry_counter = 0
100        adv_n_counter = 0
101        inits = ""
102        for op_no in range(self.carryInfoSet.block_op_count[0]):
103          if op_no in self.carryInfoSet.init_one_list: inits += "carryQ.cq[%s] = carryQ.carry_flip(carryQ.cq[%s]);\n" % (carry_counter, carry_counter)
104          if not op_no in self.carryInfoSet.advance_amount.keys(): carry_counter += 1
105          elif self.carryInfoSet.advance_amount[op_no] == 1: carry_counter += 1
106          else: adv_n_counter += 1
107        return inits
108    def GenerateStreamFunctionDecls(self):  return ""
109    def GenerateCarryInAccess(self, operation_no):
110        carry_index = self.carryIndex[operation_no - self.operation_offset]
111        return mkCall(self.carryGroupVar + "." + 'get_carry_in', [ast.Num(carry_index)])
112    def GenerateCarryOutStore(self, operation_no, carry_out_expr):
113        carry_index = self.carryIndex[operation_no - self.operation_offset]
114        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(carry_index)), ast.Store())], 
115                          mkCall("bitblock::srli<%i>" % (self.BLOCK_SIZE - 1), [carry_out_expr]))]
116    def GenerateAdvanceInAccess(self, operation_no):
117        adv_index = self.advIndex[operation_no - self.operation_offset]
118        return mkCall(self.carryGroupVar + "." + 'get_pending64', [ast.Num(adv_index)])
119    def GenerateAdvanceOutStore(self, operation_no, adv_out_expr):
120        adv_index = self.advIndex[operation_no - self.operation_offset]
121        cq_index = adv_index + self.carry_count
122        return [ast.Assign([ast.Subscript(self.CarryGroupAtt('cq'), ast.Index(ast.Num(cq_index)), ast.Store())], 
123                           mkCall("bitblock::srli<64>", [adv_out_expr]))]
124
125    def GenerateTestAll(self, instance_name):
126        carry_count = self.carryInfoSet.block_op_count[0]
127        if carry_count == 0: return ast.Num(0)
128        else: 
129           #return "%s.carryQ.CarryTest(0, %i)" % (instance_name, carry_count)
130           return mkCall(ast.Attribute(ast.Attribute(ast.Name(instance_name, ast.Load()), self.carryGroupVar, ast.Load()), 'CarryTest', ast.Load()), 
131                         [ast.Num(0), ast.Num(carry_count)])
132    def GenerateCarryIfTest(self, block_no, ifTest):
133        carry_count = self.carryInfoSet.block_op_count[block_no]
134        if carry_count == 0: return ifTest
135        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
136        return ast.BoolOp(ast.Or(), [ifTest, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(ifIndex), ast.Num(carry_count)])])
137    def GenerateCarryElseFinalization(self, block_no): 
138        carry_count = self.carryInfoSet.block_op_count[block_no]
139        if carry_count == 0: return []
140        ifIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
141        return [mkCallStmt(self.CarryGroupAtt('CarryDequeueEnqueue'), [ast.Num(ifIndex), ast.Num(carry_count)])]
142    def GenerateLocalDeclare(self, block_no):
143        local_carryvar = ast.Name("sub" + self.carryGroupVar, ast.Load())
144        return [mkCallStmt('LocalCarryDeclare', [local_carryvar, ast.Num(self.carryInfoSet.block_op_count[block_no])])]
145    def GenerateCarryWhileTest(self, block_no, testExpr):
146        carry_count = self.carryInfoSet.block_op_count[block_no]
147        if carry_count == 0: return testExpr
148        carry0 = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
149        return ast.BoolOp(ast.Or(), [testExpr, mkCall(self.CarryGroupAtt('CarryTest'), [ast.Num(carry0), ast.Num(carry_count)])])
150    def EnterLocalWhileBlock(self, operation_offset): 
151        self.carryGroupVar = "sub" + self.carryGroupVar
152        self.offset_stack = [self.operation_offset] + self.offset_stack
153        self.operation_offset = operation_offset
154    def ExitLocalWhileBlock(self): 
155        self.operation_offset = self.offset_stack[0]
156        self.offset_stack = self.offset_stack[1:]
157        self.carryGroupVar = self.carryGroupVar[3:]
158    def GenerateCarryWhileFinalization(self, block_no): 
159        carry_count = self.carryInfoSet.block_op_count[block_no]
160        if carry_count == 0: return []
161        loopIndex = self.carryIndex[self.carryInfoSet.block_first_op[block_no]]       
162        return [mkCallStmt(self.CarryGroupAtt('CarryCombine'), [self.CarryGroupAtt('cq', "sub" + self.carryGroupVar), ast.Num(loopIndex), ast.Num(carry_count)])]
163    def GenerateStreamFunctionFinalization(self):
164        carry_count = self.carryInfoSet.carry_count + self.carryInfoSet.adv_1_count
165        if carry_count == 0: return []
166        else: return [mkCallStmt(self.CarryGroupAtt('CarryQ_Adjust'), [ast.Num(carry_count)])]
167
Note: See TracBrowser for help on using the repository browser.