add memory get/set
[soc.git] / src / soc / decoder / pseudo / parser.py
1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
3
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
6 #
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
9
10 # Modifications for inclusion in PLY distribution
11 from pprint import pprint
12 from ply import lex, yacc
13 import astor
14
15 from soc.decoder.power_decoder import create_pdecode
16 from soc.decoder.pseudo.lexer import IndentLexer
17 from soc.decoder.orderedset import OrderedSet
18
19 # I use the Python AST
20 #from compiler import ast
21 import ast
22
23 # Helper function
24
25
26 def Assign(left, right):
27 names = []
28 print("Assign", left, right)
29 if isinstance(left, ast.Name):
30 # Single assignment on left
31 # XXX when doing IntClass, which will have an "eq" function,
32 # this is how to access it
33 # eq = ast.Attribute(left, "eq") # get eq fn
34 # return ast.Call(eq, [right], []) # now call left.eq(right)
35 return ast.Assign([ast.Name(left.id, ast.Store())], right)
36 elif isinstance(left, ast.Tuple):
37 # List of things - make sure they are Name nodes
38 names = []
39 for child in left.getChildren():
40 if not isinstance(child, ast.Name):
41 raise SyntaxError("that assignment not supported")
42 names.append(child.name)
43 ass_list = [ast.AssName(name, 'OP_ASSIGN') for name in names]
44 return ast.Assign([ast.AssTuple(ass_list)], right)
45 elif isinstance(left, ast.Subscript):
46 return ast.Assign([left], right)
47 # XXX HMMM probably not needed...
48 ls = left.slice
49 if isinstance(ls, ast.Slice):
50 lower, upper, step = ls.lower, ls.upper, ls.step
51 print("slice assign", lower, upper, step)
52 if step is None:
53 ls = (lower, upper, None)
54 else:
55 ls = (lower, upper, step)
56 ls = ast.Tuple(ls)
57 return ast.Call(ast.Name("selectassign"),
58 [left.value, ls, right], [])
59 else:
60 print("Assign fail")
61 raise SyntaxError("Can't do that yet")
62
63
64 # I implemented INDENT / DEDENT generation as a post-processing filter
65
66 # The original lex token stream contains WS and NEWLINE characters.
67 # WS will only occur before any other tokens on a line.
68
69 # I have three filters. One tags tokens by adding two attributes.
70 # "must_indent" is True if the token must be indented from the
71 # previous code. The other is "at_line_start" which is True for WS
72 # and the first non-WS/non-NEWLINE on a line. It flags the check so
73 # see if the new line has changed indication level.
74
75
76 # No using Python's approach because Ply supports precedence
77
78 # comparison: expr (comp_op expr)*
79 # arith_expr: term (('+'|'-') term)*
80 # term: factor (('*'|'/'|'%'|'//') factor)*
81 # factor: ('+'|'-'|'~') factor | power
82 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
83
84 def make_le_compare(arg):
85 (left, right) = arg
86 return ast.Compare(left, [ast.LtE()], [right])
87
88
89 def make_ge_compare(arg):
90 (left, right) = arg
91 return ast.Compare(left, [ast.GtE()], [right])
92
93
94 def make_lt_compare(arg):
95 (left, right) = arg
96 return ast.Compare(left, [ast.Lt()], [right])
97
98
99 def make_gt_compare(arg):
100 (left, right) = arg
101 return ast.Compare(left, [ast.Gt()], [right])
102
103
104 def make_eq_compare(arg):
105 (left, right) = arg
106 return ast.Compare(left, [ast.Eq()], [right])
107
108
109 binary_ops = {
110 "&": ast.BitAnd(),
111 "|": ast.BitOr(),
112 "+": ast.Add(),
113 "-": ast.Sub(),
114 "*": ast.Mult(),
115 "/": ast.Div(),
116 "%": ast.Mod(),
117 "<=": make_le_compare,
118 ">=": make_ge_compare,
119 "<": make_lt_compare,
120 ">": make_gt_compare,
121 "=": make_eq_compare,
122 }
123 unary_ops = {
124 "+": ast.UAdd(),
125 "-": ast.USub(),
126 "¬": ast.Invert(),
127 }
128
129
130 def check_concat(node): # checks if the comparison is already a concat
131 print("check concat", node)
132 if not isinstance(node, ast.Call):
133 return [node]
134 print("func", node.func.id)
135 if node.func.id != 'concat':
136 return [node]
137 if node.keywords: # a repeated list-constant, don't optimise
138 return [node]
139 return node.args
140
141
142 # identify SelectableInt pattern
143 def identify_sint_mul_pattern(p):
144 if not isinstance(p[3], ast.Constant):
145 return False
146 if not isinstance(p[1], ast.List):
147 return False
148 l = p[1].elts
149 if len(l) != 1:
150 return False
151 elt = l[0]
152 return isinstance(elt, ast.Constant)
153
154
155 ########## Parser (tokens -> AST) ######
156
157 # also part of Ply
158 #import yacc
159
160 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
161 # python operator precedence
162 # Highest precedence at top, lowest at bottom.
163 # Operators in the same box evaluate left to right.
164 #
165 # Operator Description
166 # () Parentheses (grouping)
167 # f(args...) Function call
168 # x[index:index] Slicing
169 # x[index] Subscription
170 # x.attribute Attribute reference
171 # ** Exponentiation
172 # ~x Bitwise not
173 # +x, -x Positive, negative
174 # *, /, % mul, div, remainder
175 # +, - Addition, subtraction
176 # <<, >> Bitwise shifts
177 # & Bitwise AND
178 # ^ Bitwise XOR
179 # | Bitwise OR
180 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
181 # not x Boolean NOT
182 # and Boolean AND
183 # or Boolean OR
184 # lambda Lambda expression
185
186 class PowerParser:
187
188 precedence = (
189 ("left", "EQ", "GT", "LT", "LE", "GE", "LTU", "GTU"),
190 ("left", "BITOR"),
191 ("left", "BITAND"),
192 ("left", "PLUS", "MINUS"),
193 ("left", "MULT", "DIV", "MOD"),
194 ("left", "INVERT"),
195 )
196
197 def __init__(self):
198 self.gprs = {}
199 for rname in ['RA', 'RB', 'RC', 'RT', 'RS']:
200 self.gprs[rname] = None
201 self.read_regs = OrderedSet()
202 self.uninit_regs = OrderedSet()
203 self.write_regs = OrderedSet()
204
205 # The grammar comments come from Python's Grammar/Grammar file
206
207 # NB: compound_stmt in single_input is followed by extra NEWLINE!
208 # file_input: (NEWLINE | stmt)* ENDMARKER
209
210 def p_file_input_end(self, p):
211 """file_input_end : file_input ENDMARKER"""
212 print("end", p[1])
213 p[0] = p[1]
214
215 def p_file_input(self, p):
216 """file_input : file_input NEWLINE
217 | file_input stmt
218 | NEWLINE
219 | stmt"""
220 if isinstance(p[len(p)-1], str):
221 if len(p) == 3:
222 p[0] = p[1]
223 else:
224 p[0] = [] # p == 2 --> only a blank line
225 else:
226 if len(p) == 3:
227 p[0] = p[1] + p[2]
228 else:
229 p[0] = p[1]
230
231 # funcdef: [decorators] 'def' NAME parameters ':' suite
232 # ignoring decorators
233
234 def p_funcdef(self, p):
235 "funcdef : DEF NAME parameters COLON suite"
236 p[0] = ast.FunctionDef(p[2], p[3], p[5], ())
237
238 # parameters: '(' [varargslist] ')'
239 def p_parameters(self, p):
240 """parameters : LPAR RPAR
241 | LPAR varargslist RPAR"""
242 if len(p) == 3:
243 args = []
244 else:
245 args = p[2]
246 p[0] = ast.arguments(args=args, vararg=None, kwarg=None, defaults=[])
247
248 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
249 # '**' NAME) |
250 # highly simplified
251
252 def p_varargslist(self, p):
253 """varargslist : varargslist COMMA NAME
254 | NAME"""
255 if len(p) == 4:
256 p[0] = p[1] + p[3]
257 else:
258 p[0] = [p[1]]
259
260 # stmt: simple_stmt | compound_stmt
261 def p_stmt_simple(self, p):
262 """stmt : simple_stmt"""
263 # simple_stmt is a list
264 p[0] = p[1]
265
266 def p_stmt_compound(self, p):
267 """stmt : compound_stmt"""
268 p[0] = [p[1]]
269
270 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
271 def p_simple_stmt(self, p):
272 """simple_stmt : small_stmts NEWLINE
273 | small_stmts SEMICOLON NEWLINE"""
274 p[0] = p[1]
275
276 def p_small_stmts(self, p):
277 """small_stmts : small_stmts SEMICOLON small_stmt
278 | small_stmt"""
279 if len(p) == 4:
280 p[0] = p[1] + [p[3]]
281 else:
282 p[0] = [p[1]]
283
284 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
285 # import_stmt | global_stmt | exec_stmt | assert_stmt
286 def p_small_stmt(self, p):
287 """small_stmt : flow_stmt
288 | break_stmt
289 | expr_stmt"""
290 if isinstance(p[1], ast.Call):
291 p[0] = ast.Expr(p[1])
292 else:
293 p[0] = p[1]
294
295 # expr_stmt: testlist (augassign (yield_expr|testlist) |
296 # ('=' (yield_expr|testlist))*)
297 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
298 # '<<=' | '>>=' | '**=' | '//=')
299 def p_expr_stmt(self, p):
300 """expr_stmt : testlist ASSIGN testlist
301 | testlist """
302 print("expr_stmt", p)
303 if len(p) == 2:
304 # a list of expressions
305 #p[0] = ast.Discard(p[1])
306 p[0] = p[1]
307 else:
308 name = None
309 if isinstance(p[1], ast.Name):
310 name = p[1].id
311 elif isinstance(p[1], ast.Subscript):
312 name = p[1].value.id
313 if name in self.gprs:
314 # add to list of uninitialised
315 self.uninit_regs.add(name)
316 elif isinstance(p[1], ast.Call) and p[1].func.id == 'GPR':
317 print(astor.dump_tree(p[1]))
318 # replace GPR(x) with GPR[x]
319 idx = p[1].args[0]
320 p[1] = ast.Subscript(p[1].func, idx)
321 elif isinstance(p[1], ast.Call) and p[1].func.id == 'MEM':
322 print ("mem assign")
323 print(astor.dump_tree(p[1]))
324 p[1].func.id = "memassign" # change function name to set
325 p[1].args.append(p[3])
326 p[0] = p[1]
327 print ("mem rewrite")
328 print(astor.dump_tree(p[0]))
329 return
330 else:
331 print ("help, help")
332 print(astor.dump_tree(p[1]))
333 print("expr assign", name, p[1])
334 if name and name in self.gprs:
335 self.write_regs.add(name) # add to list of regs to write
336 p[0] = Assign(p[1], p[3])
337
338 def p_flow_stmt(self, p):
339 "flow_stmt : return_stmt"
340 p[0] = p[1]
341
342 # return_stmt: 'return' [testlist]
343 def p_return_stmt(self, p):
344 "return_stmt : RETURN testlist"
345 p[0] = ast.Return(p[2])
346
347 def p_compound_stmt(self, p):
348 """compound_stmt : if_stmt
349 | while_stmt
350 | for_stmt
351 | funcdef
352 """
353 p[0] = p[1]
354
355 def p_break_stmt(self, p):
356 """break_stmt : BREAK
357 """
358 p[0] = ast.Break()
359
360 def p_for_stmt(self, p):
361 """for_stmt : FOR test EQ test TO test COLON suite
362 """
363 p[0] = ast.While(p[2], p[4], [])
364 # auto-add-one (sigh) due to python range
365 start = p[4]
366 end = ast.BinOp(p[6], ast.Add(), ast.Constant(1))
367 it = ast.Call(ast.Name("range"), [start, end], [])
368 p[0] = ast.For(p[2], it, p[8], [])
369
370 def p_while_stmt(self, p):
371 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
372 | DO WHILE test COLON suite
373 """
374 if len(p) == 6:
375 p[0] = ast.While(p[3], p[5], [])
376 else:
377 p[0] = ast.While(p[3], p[5], p[8])
378
379 def p_if_stmt(self, p):
380 """if_stmt : IF test COLON suite ELSE COLON if_stmt
381 | IF test COLON suite ELSE COLON suite
382 | IF test COLON suite
383 """
384 if len(p) == 8 and isinstance(p[7], ast.If):
385 p[0] = ast.If(p[2], p[4], [p[7]])
386 elif len(p) == 5:
387 p[0] = ast.If(p[2], p[4], [])
388 else:
389 p[0] = ast.If(p[2], p[4], p[7])
390
391 def p_suite(self, p):
392 """suite : simple_stmt
393 | NEWLINE INDENT stmts DEDENT"""
394 if len(p) == 2:
395 p[0] = p[1]
396 else:
397 p[0] = p[3]
398
399 def p_stmts(self, p):
400 """stmts : stmts stmt
401 | stmt"""
402 if len(p) == 3:
403 p[0] = p[1] + p[2]
404 else:
405 p[0] = p[1]
406
407 def p_comparison(self, p):
408 """comparison : comparison PLUS comparison
409 | comparison MINUS comparison
410 | comparison MULT comparison
411 | comparison DIV comparison
412 | comparison MOD comparison
413 | comparison EQ comparison
414 | comparison LE comparison
415 | comparison GE comparison
416 | comparison LTU comparison
417 | comparison GTU comparison
418 | comparison LT comparison
419 | comparison GT comparison
420 | comparison BITOR comparison
421 | comparison BITAND comparison
422 | PLUS comparison
423 | comparison MINUS
424 | INVERT comparison
425 | comparison APPEND comparison
426 | power"""
427 if len(p) == 4:
428 print(list(p))
429 if p[2] == '<u':
430 p[0] = ast.Call(ast.Name("ltu"), (p[1], p[3]), [])
431 elif p[2] == '>u':
432 p[0] = ast.Call(ast.Name("gtu"), (p[1], p[3]), [])
433 elif p[2] == '||':
434 l = check_concat(p[1]) + check_concat(p[3])
435 p[0] = ast.Call(ast.Name("concat"), l, [])
436 elif p[2] in ['<', '>', '=', '<=', '>=']:
437 p[0] = binary_ops[p[2]]((p[1], p[3]))
438 elif identify_sint_mul_pattern(p):
439 keywords=[ast.keyword(arg='repeat', value=p[3])]
440 l = p[1].elts
441 p[0] = ast.Call(ast.Name("concat"), l, keywords)
442 else:
443 p[0] = ast.BinOp(p[1], binary_ops[p[2]], p[3])
444 elif len(p) == 3:
445 if isinstance(p[2], str) and p[2] == '-':
446 p[0] = ast.UnaryOp(unary_ops[p[2]], p[1])
447 else:
448 p[0] = ast.UnaryOp(unary_ops[p[1]], p[2])
449 else:
450 p[0] = p[1]
451
452 # power: atom trailer* ['**' factor]
453 # trailers enables function calls (and subscripts).
454 # I only allow one level of calls
455 # so this is 'trailer'
456 def p_power(self, p):
457 """power : atom
458 | atom trailer"""
459 if len(p) == 2:
460 p[0] = p[1]
461 else:
462 if p[2][0] == "CALL":
463 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
464 p[0] = ast.Call(p[1], p[2][1], [])
465 # if p[1].id == 'print':
466 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
467 # else:
468 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
469 else:
470 print("subscript atom", p[2][1])
471 #raise AssertionError("not implemented %s" % p[2][0])
472 subs = p[2][1]
473 if len(subs) == 1:
474 idx = subs[0]
475 else:
476 idx = ast.Slice(subs[0], subs[1], None)
477 p[0] = ast.Subscript(p[1], idx, ast.Load())
478
479 def p_atom_name(self, p):
480 """atom : NAME"""
481 p[0] = ast.Name(id=p[1], ctx=ast.Load())
482
483 def p_atom_number(self, p):
484 """atom : BINARY
485 | NUMBER
486 | STRING"""
487 p[0] = ast.Constant(p[1])
488
489 # '[' [listmaker] ']' |
490
491 def p_atom_listmaker(self, p):
492 """atom : LBRACK listmaker RBRACK"""
493 p[0] = p[2]
494
495 def p_listmaker(self, p):
496 """listmaker : test COMMA listmaker
497 | test
498 """
499 if len(p) == 2:
500 p[0] = ast.List([p[1]])
501 else:
502 p[0] = ast.List([p[1]] + p[3].nodes)
503
504 def p_atom_tuple(self, p):
505 """atom : LPAR testlist RPAR"""
506 print("tuple", p[2])
507 print("astor dump")
508 print(astor.dump_tree(p[2]))
509
510 if isinstance(p[2], ast.Name):
511 print("tuple name", p[2].id)
512 if p[2].id in self.gprs:
513 self.read_regs.add(p[2].id) # add to list of regs to read
514 #p[0] = ast.Subscript(ast.Name("GPR"), ast.Str(p[2].id))
515 # return
516 p[0] = p[2]
517 elif isinstance(p[2], ast.BinOp):
518 if isinstance(p[2].left, ast.Name) and \
519 isinstance(p[2].right, ast.Constant) and \
520 p[2].right.value == 0 and \
521 p[2].left.id in self.gprs:
522 rid = p[2].left.id
523 self.read_regs.add(rid) # add to list of regs to read
524 # create special call to GPR.getz
525 gprz = ast.Name("GPR")
526 gprz = ast.Attribute(gprz, "getz") # get testzero function
527 # *sigh* see class GPR. we need index itself not reg value
528 ridx = ast.Name("_%s" % rid)
529 p[0] = ast.Call(gprz, [ridx], [])
530 print("tree", astor.dump_tree(p[0]))
531 else:
532 p[0] = p[2]
533 else:
534 p[0] = p[2]
535
536 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
537 def p_trailer(self, p):
538 """trailer : trailer_arglist
539 | trailer_subscript
540 """
541 p[0] = p[1]
542
543 def p_trailer_arglist(self, p):
544 "trailer_arglist : LPAR arglist RPAR"
545 p[0] = ("CALL", p[2])
546
547 def p_trailer_subscript(self, p):
548 "trailer_subscript : LBRACK subscript RBRACK"
549 p[0] = ("SUBS", p[2])
550
551 # subscript: '.' '.' '.' | test | [test] ':' [test]
552
553 def p_subscript(self, p):
554 """subscript : test COLON test
555 | test
556 """
557 if len(p) == 4:
558 # add one to end
559 if isinstance(p[3], ast.Constant):
560 end = ast.Constant(p[3].value+1)
561 else:
562 end = ast.BinOp(p[3], ast.Add(), ast.Constant(1))
563 p[0] = [p[1], end]
564 else:
565 p[0] = [p[1]]
566
567 # testlist: test (',' test)* [',']
568 # Contains shift/reduce error
569
570 def p_testlist(self, p):
571 """testlist : testlist_multi COMMA
572 | testlist_multi """
573 if len(p) == 2:
574 p[0] = p[1]
575 else:
576 # May need to promote singleton to tuple
577 if isinstance(p[1], list):
578 p[0] = p[1]
579 else:
580 p[0] = [p[1]]
581 # Convert into a tuple?
582 if isinstance(p[0], list):
583 p[0] = ast.Tuple(p[0])
584
585 def p_testlist_multi(self, p):
586 """testlist_multi : testlist_multi COMMA test
587 | test"""
588 if len(p) == 2:
589 # singleton
590 p[0] = p[1]
591 else:
592 if isinstance(p[1], list):
593 p[0] = p[1] + [p[3]]
594 else:
595 # singleton -> tuple
596 p[0] = [p[1], p[3]]
597
598 # test: or_test ['if' or_test 'else' test] | lambdef
599 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
600
601 def p_test(self, p):
602 "test : comparison"
603 p[0] = p[1]
604
605 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
606 # | '**' test)
607 # XXX INCOMPLETE: this doesn't allow the trailing comma
608
609 def p_arglist(self, p):
610 """arglist : arglist COMMA argument
611 | argument"""
612 if len(p) == 4:
613 p[0] = p[1] + [p[3]]
614 else:
615 p[0] = [p[1]]
616
617 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
618 def p_argument(self, p):
619 "argument : test"
620 p[0] = p[1]
621
622 def p_error(self, p):
623 # print "Error!", repr(p)
624 raise SyntaxError(p)
625
626
627 class GardenSnakeParser(PowerParser):
628 def __init__(self, lexer=None):
629 PowerParser.__init__(self)
630 if lexer is None:
631 lexer = IndentLexer(debug=0)
632 self.lexer = lexer
633 self.tokens = lexer.tokens
634 self.parser = yacc.yacc(module=self, start="file_input_end",
635 debug=False, write_tables=False)
636
637 self.sd = create_pdecode()
638
639 def parse(self, code):
640 # self.lexer.input(code)
641 result = self.parser.parse(code, lexer=self.lexer, debug=False)
642 return ast.Module(result)
643
644
645 ###### Code generation ######
646
647 #from compiler import misc, syntax, pycodegen
648
649 class GardenSnakeCompiler(object):
650 def __init__(self):
651 self.parser = GardenSnakeParser()
652
653 def compile(self, code, mode="exec", filename="<string>"):
654 tree = self.parser.parse(code)
655 print("snake")
656 pprint(tree)
657 return tree
658 #misc.set_filename(filename, tree)
659 return compile(tree, mode="exec", filename="<string>")
660 # syntax.check(tree)
661 gen = pycodegen.ModuleCodeGenerator(tree)
662 code = gen.getCode()
663 return code