source: proto/Compiler/pablo.py @ 760

Last change on this file since 760 was 760, checked in by cameron, 9 years ago

Handling stream_stmts initializations

File size: 12.2 KB
Line 
1#
2# Pablo.py - parallel bitstream to bitblock
3#  2nd generation compiler
4#
5# Copyright 2010, Robert D. Cameron
6# All rights reserved.
7#
8import ast, copy, sys
9import Cgen
10
11
12# HELPER functions for AST recognition/construction
13
14def is_Advance_Call(fncall):   # extracted from old py2bitexpr.py
15        if isinstance(fncall.func, ast.Name): iscall = fncall.func.id == 'Advance'
16        elif isinstance(fncall.func, ast.Attribute) and isinstance(fncall.func.value, ast.Name):
17                 iscall = fncall.func.value.id == 'bitutil' and fncall.func.attr == 'Advance'
18        return iscall and len(fncall.args) == 1 and fncall.kwargs == None and fncall.starargs == None
19
20def is_ScanThru_Call(fncall):  # extracted from old py2bitexpr.py
21        if isinstance(fncall.func, ast.Name): iscall = fncall.func.id == 'ScanThru'
22        elif isinstance(fncall.func, ast.Attribute) and isinstance(fncall.func.value, ast.Name):
23                 iscall = fncall.func.value.id == 'bitutil' and fncall.func.attr == 'ScanThru'
24        return iscall and len(fncall.args) == 2 and fncall.kwargs == None and fncall.starargs == None
25
26def mkQname(obj, field):
27  return ast.Attribute(ast.Name(obj, ast.Load()), field, ast.Load())
28
29def mkCall(fn_name, args):
30  if isinstance(fn_name, str): fn_name = ast.Name(fn_name, ast.Load())
31  return ast.Call(fn_name, args, [], None, None)
32
33def mkCallStmt(fn_name, args):
34  if isinstance(fn_name, str): fn_name = ast.Name(fn_name, ast.Load())
35  return ast.Expr(ast.Call(fn_name, args, [], None, None))
36
37#
38# Introducing BitBlock logical operations
39#
40class Bitwise_to_SIMD(ast.NodeTransformer):
41  """
42  Make the following substitutions:
43     x & y => simd_and(x, y)
44     x | y => simd_or(x, y)
45     x ^ y => simd_xor(x, y)
46     ~x    => simd_not(x)
47     0     => simd_const_1(0)
48     -1    => simd_const_1(1)
49     if x: => if bitblock_has_bit(x):
50  while x: => while bitblock_has_bit(x):
51  >>> ast_show(Bitwise_to_SIMD().xfrm(ast.parse("pfx = bit0 & bit1; sfx = bit0 &~ bit1")))
52 
53  pfx = simd_and(bit0, bit1)
54  sfx = simd_and(bit0, simd_not(bit1))
55  >>>
56  """
57  def xfrm(self, t):
58    return self.generic_visit(t)
59  def visit_UnaryOp(self, t):
60    self.generic_visit(t)
61    if isinstance(t.op, ast.Invert):
62      return mkCall('simd_not', [t.operand])
63    else: return t
64  def visit_BinOp(self, t):
65    self.generic_visit(t)
66    if isinstance(t.op, ast.BitOr):
67      return mkCall('simd_or', [t.left, t.right])
68    elif isinstance(t.op, ast.BitAnd):
69      return mkCall('simd_and', [t.left, t.right])
70    elif isinstance(t.op, ast.BitXor):
71      return mkCall('simd_xor', [t.left, t.right])
72    else: return t
73  def visit_Num(self, numnode):
74    n = numnode.n
75    if n == 0: return mkCall('simd_const_1', [numnode])
76    elif n == -1: return mkCall('simd_const_1', [ast.Num(1)])
77    else: return numnode
78  def visit_If(self, ifNode):
79    self.generic_visit(ifNode)
80    ifNode.test = mkCall('bitblock_has_bit', [ifNode.test])
81    return ifNode
82  def visit_While(self, whileNode):
83    self.generic_visit(whileNode)
84    whileNode.test = mkCall('bitblock_has_bit', [whileNode.test])
85    return whileNode
86  def visit_Subscript(self, numnode):
87    return numnode  # no recursive modifications of index expressions
88
89
90#
91#  Generating BitBlock declarations for Local Variables
92#
93#
94class LocalVars(ast.NodeVisitor):
95  def visit_Name(self, nm):
96    if isinstance(nm.ctx, ast.Param):
97      self.params.append(nm.id)
98    if isinstance(nm.ctx, ast.Store):
99      if nm.id not in self.stores: self.stores.append(nm.id)
100  def get(self, node): 
101    self.params=[]
102    self.stores=[]
103    self.generic_visit(node)
104    return [v for v in self.stores if not v in self.params]
105
106MAX_LINE_LENGTH = 80
107
108def BitBlock_decls_from_vars(varlist):
109  global MAX_LINE_LENGTH
110  decls = "  BitBlock"
111  pending = ""
112  linelgth = 10
113  for v in varlist:
114    if linelgth + len(v) + 2 <= MAX_LINE_LENGTH:
115      decls += pending + " " + v
116      linelgth += len(pending + v) + 1
117    else:
118      decls += ";\n  BitBlock " + v
119      linelgth = 11 + len(v)
120    pending = ","
121  decls += ";\n"
122  return decls
123
124def BitBlock_decls_of_fn(fndef):
125  return BitBlock_decls_from_vars(LocalVars().get(fndef))
126
127def BitBlock_header_of_fn(fndef):
128  Ccode = "static inline void " + fndef.name + "("
129  pending = ""
130  for arg in fndef.args.args:
131    if isinstance(arg, ast.Name):
132      Ccode += pending + arg.id.upper()[0] + arg.id[1:] + " & " + arg.id
133      pending = ", "
134  Ccode += ")"
135  return Ccode
136
137
138
139#
140#  Stream Initialization Statement Extraction
141#
142#  streamvar = 1 ==> streamvar = sisd_from_int(1) initially.
143class StreamInitializations(ast.NodeTransformer):
144  def xfrm(self, node):
145    self.stream_stmts = []
146    self.loop_post_inits = []
147    self.generic_visit(node)
148    return Cgen.py2C().gen(self.stream_stmts)
149  def visit_Assign(self, node):
150    if isinstance(node.value, ast.Num):
151      if node.value.n == 0: return node
152      elif node.value.n == -1: return node
153      else: 
154        stream_init = copy.deepcopy(node)
155        stream_init.value = mkCall('sisd_from_int', [node.value])
156        loop_init = copy.deepcopy(node)
157        loop_init.value.n = 0
158        self.stream_stmts.append(stream_init)
159        self.loop_post_inits.append(loop_init)
160        return None
161    else: return node
162  def visit_FunctionDef(self, node):
163    self.generic_visit(node)
164    node.body = node.body + self.loop_post_inits
165    return node
166
167
168#
169# Carry Introduction Transformation
170#
171
172class CarryCounter(ast.NodeVisitor):
173  def visit_Call(self, callnode):
174    self.generic_visit(callnode)
175    if is_Advance_Call(callnode) or is_ScanThru_Call(callnode): 
176      self.carry_count += 1
177  def visit_Binop(self, exprnode):
178    self.generic_visit(exprnode)
179    if isinstance(exprnode.op, ast.Sub):
180      self.carry_count += 1
181  def count(self, nodeToVisit):
182    self.carry_count = 0
183    self.generic_visit(nodeToVisit)
184    return self.carry_count
185
186
187CARRYQ_SIZE = 64
188
189def Carry_decls_of_fn_direct(fndef):
190  global CARRYQ_SIZE
191  carry_count = CarryCounter().count(fndef)
192  if carry_count == 0: return ""
193  Ccode = "  uint%i_t carryQ_0" % CARRYQ_SIZE
194  currentQ = 0
195  carry_count -= CARRYQ_SIZE
196  while carry_count > 0:
197    currentQ += 1
198    Ccode += ", carryQ_%i" % currentQ
199    carry_count -= CARRYQ_SIZE
200  return Ccode + ";\n"
201
202def Carry_decls_of_fn(fndef):
203  carry_count = CarryCounter().count(fndef)
204  if carry_count == 0: return ""
205  else: return "  DeclareCarryContainers(%i);\n" % carry_count
206
207                   
208class CarryIntro(ast.NodeTransformer):
209  def __init__(self, carryvar="carryQ", mode = "ci_co"):
210    self.carryvar = ast.Name(carryvar, ast.Load())
211    self.mode = mode
212  def any_carry_expr(self, fndef):
213    carry_count = CarryCounter().count(fndef)
214    carry_arglist = [self.carryvar, ast.Num(0), ast.Num(carry_count)]
215    return mkCall('CarryTest', carry_arglist)
216  def xfrm_fndef(self, fndef):
217    self.current_carry = 0
218    carry_count = CarryCounter().count(fndef)
219    if carry_count == 0: return fndef
220    self.generic_visit(fndef)
221    fndef.body.insert(0, mkCallStmt('CarryDeclare', [self.carryvar, ast.Num(carry_count)]))
222    return fndef
223  def generic_xfrm(self, node):
224    self.current_carry = 0
225    carry_count = CarryCounter().count(node)
226    if carry_count == 0: return node
227    self.generic_visit(node)
228    return node
229  def visit_Call(self, callnode):
230    self.generic_visit(callnode)
231    if is_Advance_Call(callnode):
232      rtn = "BitBlock_advance_%s" % self.mode
233      c = mkCall(rtn, [callnode.args[0], self.carryvar, ast.Num(self.current_carry)])
234      self.current_carry += 1
235      return c
236    elif is_ScanThru_Call(callnode): 
237      rtn = "BitBlock_scanthru_%s" % self.mode
238      c = mkCall(rtn, [callnode.args[0], callnode.args[1], self.carryvar, ast.Num(self.current_carry)])
239      self.current_carry += 1
240      return c
241    else: return callnode
242  def visit_Binop(self, exprnode):
243    self.generic_visit(exprnode)
244    if isinstance(exprnode.op, ast.Sub):
245      rtn = "BitBlock_sub_%s" % self.mode
246      c = mkCall(rtn, [exprnode.left, exprnode.right, self.carryvar, ast.Num(self.current_carry)])
247      self.current_carry += 1
248      return c
249    elif isinstance(exprnode.op, ast.Add):
250      rtn = "BitBlock_add_%s" % self.mode
251      c = mkCall(rtn, [exprnode.left, exprnode.right, self.carryvar, ast.Num(self.current_carry)])
252      self.current_carry += 1
253      return c
254    else: return exprnode
255  def visit_If(self, ifNode):
256    carry_base = self.current_carry
257    carries = CarryCounter().count(ifNode)
258    self.generic_visit(ifNode)
259    if carries == 0 or self.mode == "co": return ifNode
260    carry_arglist = [self.carryvar, ast.Num(carry_base), ast.Num(carries)]
261    new_test = ast.BoolOp(ast.Or(), [ifNode.test, mkCall('CarryTest', carry_arglist)])
262    new_else_part = [mkCallStmt('CarryDequeueEnqueue', carry_arglist)]
263    return ast.If(new_test, ifNode.body, new_else_part)
264  def visit_While(self, whileNode):
265    carry_base = self.current_carry
266    carries = CarryCounter().count(whileNode)
267    if carries == 0: return whileNode
268    carry_arglist = [self.carryvar, ast.Num(carry_base), ast.Num(carries)]
269    local_carryvar = 'sub'+self.carryvar.id
270    inner_while = CarryIntro(local_carryvar, 'co').generic_xfrm(copy.deepcopy(whileNode))
271    self.generic_visit(whileNode)
272    local_decl = mkCallStmt('CarryDeclare', [ast.Name(local_carryvar, ast.Load()), ast.Num(carries)])
273    inner_while.body.insert(0, local_decl)
274    final_combine = mkCallStmt('CarryCombine', [self.carryvar, ast.Name(local_carryvar, ast.Load()), ast.Num(carry_base), ast.Num(carries)])
275    inner_while.body.append(final_combine)
276    if self.mode == "co": new_test = whileNode.test
277    else: new_test = ast.BoolOp(ast.Or(), [whileNode.test, mkCall('CarryTest', carry_arglist)])
278    else_part = [mkCallStmt('CarryDequeueEnqueue', carry_arglist)]
279    return ast.If(new_test, whileNode.body + [inner_while], else_part)
280
281class StreamStructGen(ast.NodeVisitor):
282  """
283  Given a BitStreamSet subclass, generate the equivalent C struct.
284  >>> obj = ast.parse(r'''
285  ... class S1(BitStreamSet):
286  ...   a1 = 0
287  ...   a2 = 0
288  ...   a3 = 0
289  ...
290  ... class S2(BitStreamSet):
291  ...   x1 = 0
292  ...   x2 = 0
293  ... ''')
294  >>> print StreamStructGen().gen(obj)
295  struct S1 {
296    BitBlock a1;
297    BitBlock a2;
298    BitBlock a3;
299  }
300 
301  struct S2 {
302    BitBlock x1;
303    BitBlock x2;
304  }
305  """
306  def __init__(self):
307    pass
308  def gen(self, tree):
309    self.Ccode=""
310    self.generic_visit(tree)
311    return self.Ccode
312  def visit_ClassDef(self, node):
313#    self.Ccode += "typedef struct {\n"
314    self.Ccode += "struct {\n"
315    for stmt in node.body:
316      if isinstance(stmt, ast.Assign):
317        for v in stmt.targets:
318          if isinstance(v, ast.Name):
319            self.Ccode += "  BitBlock " + v.id + ";\n"
320    self.Ccode += "} " + node.name + ";\n\n"
321 
322class StreamFunctionDecl(ast.NodeVisitor):
323  def __init__(self):
324    pass
325  def gen(self, tree):
326    self.Ccode=""
327    self.generic_visit(tree)
328    return self.Ccode
329  def visit_FunctionDef(self, node):
330    self.Ccode += "static inline void " + node.name + "("
331    pending = ""
332    for arg in node.args.args:
333      if isinstance(arg, ast.Name):
334        self.Ccode += pending + arg.id.upper()[0] + arg.id[1:] + " & " + arg.id
335        pending = ", "
336    self.Ccode += ");\n"
337
338
339
340#  Translate a function
341#
342
343class FunctionXlator(ast.NodeVisitor):
344  def xlat(self, node):
345    self.Ccode=""
346    self.generic_visit(node)
347    return self.Ccode
348  def visit_FunctionDef(self, fndef):
349    Bitwise_to_SIMD().xfrm(fndef)
350    self.Ccode += BitBlock_header_of_fn(fndef) + " {\n"
351    self.Ccode += BitBlock_decls_of_fn(fndef)
352    CarryIntro().xfrm_fndef(fndef)
353    self.Ccode += Cgen.py2C().gen(fndef.body)
354    self.Ccode += "\n}\n"
355
356def main(infilename, outfile = sys.stdout):
357  t = ast.parse(file(infilename).read())
358  outfile.write(StreamStructGen().gen(t))
359  outfile.write(FunctionXlator().xlat(t))
360
361#
362#
363#  Routines for compatibility with the old compiler/template.
364#  Quick and dirty hacks for now - Dec. 2010.
365#
366def gen_declarations(main_module, main_fn):
367  Cdecls = StreamStructGen().gen(main_module)
368  Cdecls +=  BitBlock_decls_of_fn(main_fn)
369  return Cdecls
370
371def stream_stmts(main_fn):
372  return StreamInitializations().xfrm(main_fn)
373
374def block_stmts(main_fn):
375  Bitwise_to_SIMD().xfrm(main_fn)
376  CarryIntro().xfrm_fndef(main_fn)
377  return Cgen.py2C().gen(main_fn.body)
378
379def any_carry_expr(main_fn):
380  return Cgen.py2C().gen(CarryIntro().any_carry_expr(main_fn))
381
382if __name__ == "__main__":
383                import doctest
384                doctest.testmod()
385
386
Note: See TracBrowser for help on using the repository browser.