1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
10 # Modifications for inclusion in PLY distribution
11 from pprint
import pprint
12 from ply
import lex
, yacc
14 from copy
import deepcopy
16 from openpower
.decoder
.power_decoder
import create_pdecode
17 from openpower
.decoder
.pseudo
.lexer
import IndentLexer
18 from openpower
.decoder
.orderedset
import OrderedSet
20 # I use the Python AST
21 #from compiler import ast
26 regs
= ['RA', 'RS', 'RB', 'RC', 'RT']
27 fregs
= ['FRA', 'FRS', 'FRB', 'FRC', 'FRT', 'FRS']
28 SPECIAL_HELPERS
= {'concat', 'MEM', 'GPR', 'FPR', 'SPR'}
30 def Assign(autoassign
, assignname
, left
, right
, iea_mode
):
32 print("Assign", autoassign
, assignname
, left
, right
)
33 if isinstance(left
, ast
.Name
):
34 # Single assignment on left
35 # XXX when doing IntClass, which will have an "eq" function,
36 # this is how to access it
37 # eq = ast.Attribute(left, "eq") # get eq fn
38 # return ast.Call(eq, [right], []) # now call left.eq(right)
39 return ast
.Assign([ast
.Name(left
.id, ast
.Store())], right
)
40 elif isinstance(left
, ast
.Tuple
):
41 # List of things - make sure they are Name nodes
43 for child
in left
.getChildren():
44 if not isinstance(child
, ast
.Name
):
45 raise SyntaxError("that assignment not supported")
46 names
.append(child
.name
)
47 ass_list
= [ast
.AssName(name
, 'OP_ASSIGN') for name
in names
]
48 return ast
.Assign([ast
.AssTuple(ass_list
)], right
)
49 elif isinstance(left
, ast
.Subscript
):
51 # XXX changing meaning of "undefined" to a function
52 #if (isinstance(ls, ast.Slice) and isinstance(right, ast.Name) and
53 # right.id == 'undefined'):
54 # # undefined needs to be copied the exact same slice
55 # right = ast.Subscript(right, ls, ast.Load())
56 # return ast.Assign([left], right)
57 res
= ast
.Assign([left
], right
)
58 if autoassign
and isinstance(ls
, ast
.Slice
):
59 # hack to create a variable pre-declared based on a slice.
60 # dividend[0:32] = (RA)[0:32] will create
62 # dividend[0:32] = (RA)[0:32]
63 # the declaration makes the slice-assignment "work"
64 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
65 print("lower, upper, step", repr(lower
), repr(upper
), step
)
66 # XXX relax constraint that indices on auto-assign have
67 # to be constants x[0:32]
68 #if not isinstance(lower, ast.Constant) or \
69 # not isinstance(upper, ast.Constant):
71 qty
= ast
.BinOp(upper
, binary_ops
['-'], lower
)
72 keywords
= [ast
.keyword(arg
='repeat', value
=qty
)]
74 right
= ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
75 declare
= ast
.Assign([ast
.Name(assignname
, ast
.Store())], right
)
78 # XXX HMMM probably not needed...
80 if isinstance(ls
, ast
.Slice
):
81 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
82 print("slice assign", lower
, upper
, step
)
84 ls
= (lower
, upper
, None)
86 ls
= (lower
, upper
, step
)
88 return ast
.Call(ast
.Name("selectassign", ast
.Load()),
89 [left
.value
, ls
, right
], [])
92 raise SyntaxError("Can't do that yet")
95 # I implemented INDENT / DEDENT generation as a post-processing filter
97 # The original lex token stream contains WS and NEWLINE characters.
98 # WS will only occur before any other tokens on a line.
100 # I have three filters. One tags tokens by adding two attributes.
101 # "must_indent" is True if the token must be indented from the
102 # previous code. The other is "at_line_start" which is True for WS
103 # and the first non-WS/non-NEWLINE on a line. It flags the check so
104 # see if the new line has changed indication level.
107 # No using Python's approach because Ply supports precedence
109 # comparison: expr (comp_op expr)*
110 # arith_expr: term (('+'|'-') term)*
111 # term: factor (('*'|'/'|'%'|'//') factor)*
112 # factor: ('+'|'-'|'~') factor | power
113 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
115 def make_le_compare(arg
):
117 return ast
.Call(ast
.Name("le", ast
.Load()), (left
, right
), [])
120 def make_ge_compare(arg
):
122 return ast
.Call(ast
.Name("ge", ast
.Load()), (left
, right
), [])
125 def make_lt_compare(arg
):
127 return ast
.Call(ast
.Name("lt", ast
.Load()), (left
, right
), [])
130 def make_gt_compare(arg
):
132 return ast
.Call(ast
.Name("gt", ast
.Load()), (left
, right
), [])
135 def make_eq_compare(arg
):
137 return ast
.Call(ast
.Name("eq", ast
.Load()), (left
, right
), [])
140 def make_ne_compare(arg
):
142 return ast
.Call(ast
.Name("ne", ast
.Load()), (left
, right
), [])
154 "<=": make_le_compare
,
155 ">=": make_ge_compare
,
156 "<": make_lt_compare
,
157 ">": make_gt_compare
,
158 "=": make_eq_compare
,
159 "!=": make_ne_compare
,
168 def check_concat(node
): # checks if the comparison is already a concat
169 print("check concat", node
)
170 if not isinstance(node
, ast
.Call
):
172 print("func", node
.func
.id)
173 if node
.func
.id != 'concat':
175 if node
.keywords
: # a repeated list-constant, don't optimise
180 # identify SelectableInt pattern [something] * N
181 # must return concat(something, repeat=N)
182 # here is a TEMPORARY hack to support minimal expressions
183 # such as (XLEN-16), looking for ast.BinOp
184 # have to keep an eye on this
185 def identify_sint_mul_pattern(p
):
186 """here we are looking for patterns of this type:
188 these must specifically be returned as concat(item, repeat=something)
190 #print ("identify_sint_mul_pattern")
192 # print(" ", astor.dump_tree(pat))
193 if p
[2] != '*': # multiply
195 if (not isinstance(p
[3], ast
.Constant
) and # rhs = Num
196 not isinstance(p
[3], ast
.BinOp
) and # rhs = (XLEN-something)
197 not isinstance(p
[3], ast
.Name
)): # rhs = XLEN
199 if not isinstance(p
[1], ast
.List
): # lhs is a list
202 if len(l
) != 1: # lhs is a list of length 1
204 return True # yippee!
207 def apply_trailer(atom
, trailer
, read_regs
):
208 if trailer
[0] == "TLIST":
209 # assume depth of one
210 atom
= apply_trailer(atom
, trailer
[1], read_regs
)
212 if trailer
[0] == "CALL":
213 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
214 for arg
in trailer
[1]:
215 if isinstance(arg
, ast
.Name
):
217 if name
in regs
+ fregs
:
219 # special-case, these functions must NOT be made "self.xxxx"
220 if atom
.id not in SPECIAL_HELPERS
:
221 name
= ast
.Name("self", ast
.Load())
222 atom
= ast
.Attribute(name
, atom
, ast
.Load())
223 return ast
.Call(atom
, trailer
[1], [])
224 # if p[1].id == 'print':
225 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
227 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
229 print("subscript atom", trailer
[1])
230 #raise AssertionError("not implemented %s" % p[2][0])
234 if isinstance(idx
, ast
.Name
) and idx
.id in regs
+ fregs
:
235 read_regs
.add(idx
.id)
236 if isinstance(idx
, ast
.Name
) and idx
.id in regs
:
237 print ("single atom subscript, underscored", idx
.id)
238 idx
= ast
.Name("_%s" % idx
.id, ast
.Load())
240 idx
= ast
.Slice(subs
[0], subs
[1], None)
241 # if isinstance(atom, ast.Name) and atom.id == 'CR':
242 # atom.id = 'CR' # bad hack
243 #print ("apply_trailer Subscript", atom.id, idx)
244 return ast
.Subscript(atom
, idx
, ast
.Load())
246 ########## Parser (tokens -> AST) ######
251 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
252 # python operator precedence
253 # Highest precedence at top, lowest at bottom.
254 # Operators in the same box evaluate left to right.
256 # Operator Description
257 # () Parentheses (grouping)
258 # f(args...) Function call
259 # x[index:index] Slicing
260 # x[index] Subscription
261 # x.attribute Attribute reference
264 # +x, -x Positive, negative
265 # *, /, % mul, div, remainder
266 # +, - Addition, subtraction
267 # <<, >> Bitwise shifts
271 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
275 # lambda Lambda expression
281 ("left", "EQ", "NE", "GT", "LT", "LE", "GE", "LTU", "GTU"),
285 ("left", "PLUS", "MINUS"),
286 ("left", "MULT", "DIV", "MOD"),
290 def __init__(self
, form
, include_carry_in_write
=False, helper
=False):
291 self
.include_ca_in_write
= include_carry_in_write
295 form
= self
.sd
.sigforms
[form
]
297 formkeys
= form
._asdict
().keys()
300 self
.declared_vars
= set()
301 self
.fnparm_vars
= set()
302 for rname
in regs
+ fregs
:
303 self
.gprs
[rname
] = None
304 self
.declared_vars
.add(rname
)
305 self
.available_op_fields
= set()
307 if k
not in self
.gprs
:
308 if k
== 'SPR': # sigh, lower-case to not conflict
310 self
.available_op_fields
.add(k
)
311 self
.op_fields
= OrderedSet()
312 self
.read_regs
= OrderedSet()
313 self
.uninit_regs
= OrderedSet()
314 self
.write_regs
= OrderedSet()
315 self
.special_regs
= OrderedSet() # see p_atom_name
317 # The grammar comments come from Python's Grammar/Grammar file
319 # NB: compound_stmt in single_input is followed by extra NEWLINE!
320 # file_input: (NEWLINE | stmt)* ENDMARKER
322 def p_file_input_end(self
, p
):
323 """file_input_end : file_input ENDMARKER"""
327 def p_file_input(self
, p
):
328 """file_input : file_input NEWLINE
332 if isinstance(p
[len(p
)-1], str):
336 p
[0] = [] # p == 2 --> only a blank line
343 # funcdef: [decorators] 'def' NAME parameters ':' suite
344 # ignoring decorators
346 def p_funcdef(self
, p
):
347 "funcdef : DEF NAME parameters COLON suite"
348 p
[0] = ast
.FunctionDef(p
[2], p
[3], p
[5], ())
349 # reset function parameters after suite is identified
350 self
.fnparm_vars
= set()
352 # parameters: '(' [varargslist] ')'
353 def p_parameters(self
, p
):
354 """parameters : LPAR RPAR
355 | LPAR varargslist RPAR"""
361 p
[0] = ast
.arguments(args
=args
, vararg
=None, kwarg
=None, defaults
=[])
362 # during the time between parameters identified and suite is not
363 # there is a window of opportunity to declare the function parameters
364 # in-scope, for use to not overwrite them with auto-assign
365 self
.fnparm_vars
= set()
367 print ("adding fn parm", arg
)
368 self
.fnparm_vars
.add(arg
)
370 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
374 def p_varargslist(self
, p
):
375 """varargslist : varargslist COMMA NAME
383 # stmt: simple_stmt | compound_stmt
384 def p_stmt_simple(self
, p
):
385 """stmt : simple_stmt"""
386 # simple_stmt is a list
389 def p_stmt_compound(self
, p
):
390 """stmt : compound_stmt"""
393 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
394 def p_simple_stmt(self
, p
):
395 """simple_stmt : small_stmts NEWLINE
396 | small_stmts SEMICOLON NEWLINE"""
399 def p_small_stmts(self
, p
):
400 """small_stmts : small_stmts SEMICOLON small_stmt
404 elif isinstance(p
[1], list):
409 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
410 # import_stmt | global_stmt | exec_stmt | assert_stmt
411 def p_small_stmt(self
, p
):
412 """small_stmt : flow_stmt
415 if isinstance(p
[1], ast
.Call
):
416 p
[0] = ast
.Expr(p
[1])
417 elif isinstance(p
[1], ast
.Name
) and p
[1].id not in SPECIAL_HELPERS
:
419 name
= ast
.Name("self", ast
.Load())
420 name
= ast
.Attribute(name
, fname
, ast
.Load())
421 p
[0] = ast
.Call(name
, [], [])
425 # expr_stmt: testlist (augassign (yield_expr|testlist) |
426 # ('=' (yield_expr|testlist))*)
427 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
428 # '<<=' | '>>=' | '**=' | '//=')
429 def p_expr_stmt(self
, p
):
430 """expr_stmt : testlist ASSIGNEA testlist
431 | testlist ASSIGN testlist
433 print("expr_stmt", p
)
435 # a list of expressions
436 #p[0] = ast.Discard(p[1])
439 iea_mode
= p
[2] == '<-iea'
442 if isinstance(p
[1], ast
.Name
):
444 elif isinstance(p
[1], ast
.Subscript
):
445 print ("assign subscript", p
[1].value
,
449 print(astor
.dump_tree(p
[1]))
450 if isinstance(p
[1].value
, ast
.Name
):
452 print ("assign subscript value to name", name
)
453 if name
in self
.gprs
:
454 # add to list of uninitialised
455 self
.uninit_regs
.add(name
)
456 # work out if this is an ininitialised variable
457 # that should be auto-assigned simply by being "sliced"
458 autoassign
= (name
not in self
.declared_vars
and
459 name
not in self
.fnparm_vars
and
460 name
not in self
.special_regs
)
461 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id in \
462 ['GPR', 'FPR', 'SPR']:
463 print(astor
.dump_tree(p
[1]))
464 # replace GPR(x) with GPR[x]
465 idx
= p
[1].args
[0].id
466 if idx
in regs
+ fregs
:
467 ridx
= ast
.Name("_%s" % idx
, ast
.Load())
469 ridx
= ast
.Name(idx
, ast
.Load())
470 p
[1] = ast
.Subscript(p
[1].func
, ridx
, ast
.Load())
472 self
.read_regs
.add(idx
) # add to list of regs to read
473 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'MEM':
475 print(astor
.dump_tree(p
[1]))
476 p
[1].func
.id = "memassign" # change function name to set
477 p
[1].args
.append(p
[3])
480 print(astor
.dump_tree(p
[0]))
484 print(astor
.dump_tree(p
[1]))
485 print("expr assign", name
, p
[1], "to", p
[3])
486 if isinstance(p
[3], ast
.Name
):
488 if toname
in self
.gprs
:
489 self
.read_regs
.add(toname
)
490 if name
and name
in self
.gprs
:
491 self
.write_regs
.add(name
) # add to list of regs to write
492 p
[0] = Assign(autoassign
, name
, p
[1], p
[3], iea_mode
)
494 self
.declared_vars
.add(name
)
496 def p_flow_stmt(self
, p
):
497 "flow_stmt : return_stmt"
500 # return_stmt: 'return' [testlist]
501 def p_return_stmt(self
, p
):
502 "return_stmt : RETURN testlist"
503 p
[0] = ast
.Return(p
[2])
505 def p_compound_stmt(self
, p
):
506 """compound_stmt : if_stmt
514 def p_break_stmt(self
, p
):
515 """break_stmt : BREAK
519 def p_for_stmt(self
, p
):
520 """for_stmt : FOR atom EQ comparison TO comparison COLON suite
521 | DO atom EQ comparison TO comparison COLON suite
525 it
= ast
.Call(ast
.Name("RANGE", ast
.Load()), (start
, end
), [])
526 p
[0] = ast
.For(p
[2], it
, p
[8], [])
528 def p_while_stmt(self
, p
):
529 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
530 | DO WHILE test COLON suite
533 p
[0] = ast
.While(p
[3], p
[5], [])
535 p
[0] = ast
.While(p
[3], p
[5], p
[8])
537 def p_switch_smt(self
, p
):
538 """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
542 print(astor
.dump_tree(p
[1]))
545 current_cases
= [] # for deferral
546 for (case
, suite
) in p
[8]:
547 print("for", case
, suite
)
550 current_cases
.append(ast
.Num(c
))
552 if case
== 'default': # last
555 current_cases
.append(ast
.Num(c
))
556 print("cases", current_cases
)
557 compare
= ast
.Compare(switchon
, [ast
.In()],
558 [ast
.List(current_cases
, ast
.Load())])
560 cases
.append((compare
, suite
))
562 print("ended", case
, current_cases
)
563 if case
== 'default':
565 compare
= ast
.Compare(switchon
, [ast
.In()],
566 [ast
.List(current_cases
, ast
.Load())])
567 cases
.append((compare
, suite
))
568 cases
.append((None, suite
))
572 for compare
, suite
in cases
:
573 print("after rev", compare
, suite
)
575 assert len(res
) == 0, "last case should be default"
578 if not isinstance(res
, list):
580 res
= ast
.If(compare
, suite
, res
)
583 def p_switches(self
, p
):
584 """switches : switch_list switch_default
592 def p_switch_list(self
, p
):
593 """switch_list : switch_case switch_list
601 def p_switch_case(self
, p
):
602 """switch_case : CASE LPAR atomlist RPAR COLON suite
605 if isinstance(p
[6][0], ast
.Name
) and p
[6][0].id == 'fallthrough':
609 def p_switch_default(self
, p
):
610 """switch_default : DEFAULT COLON suite
612 p
[0] = ('default', p
[3])
614 def p_atomlist(self
, p
):
615 """atomlist : atom COMMA atomlist
618 assert isinstance(p
[1], ast
.Constant
), "case must be numbers"
620 p
[0] = [p
[1].value
] + p
[3]
624 def p_if_stmt(self
, p
):
625 """if_stmt : IF test COLON suite ELSE COLON if_stmt
626 | IF test COLON suite ELSE COLON suite
627 | IF test COLON suite
629 if len(p
) == 8 and isinstance(p
[7], ast
.If
):
630 p
[0] = ast
.If(p
[2], p
[4], [p
[7]])
632 p
[0] = ast
.If(p
[2], p
[4], [])
634 p
[0] = ast
.If(p
[2], p
[4], p
[7])
636 def p_suite(self
, p
):
637 """suite : simple_stmt
638 | NEWLINE INDENT stmts DEDENT"""
644 def p_stmts(self
, p
):
645 """stmts : stmts stmt
652 def p_comparison(self
, p
):
653 """comparison : comparison PLUS comparison
654 | comparison MINUS comparison
655 | comparison MULT comparison
656 | comparison DIV comparison
657 | comparison MOD comparison
658 | comparison EQ comparison
659 | comparison NE comparison
660 | comparison LE comparison
661 | comparison GE comparison
662 | comparison LTU comparison
663 | comparison GTU comparison
664 | comparison LT comparison
665 | comparison GT comparison
666 | comparison BITOR comparison
667 | comparison BITXOR comparison
668 | comparison BITAND comparison
672 | comparison APPEND comparison
677 p
[0] = ast
.Call(ast
.Name("ltu", ast
.Load()), (p
[1], p
[3]), [])
679 p
[0] = ast
.Call(ast
.Name("gtu", ast
.Load()), (p
[1], p
[3]), [])
681 l
= check_concat(p
[1]) + check_concat(p
[3])
682 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, [])
683 elif p
[2] in ['/', '%']:
684 # bad hack: if % or / used anywhere other than div/mod ops,
685 # do % or /. however if the argument names are "dividend"
686 # we must call the special trunc_divs and trunc_rems functions
688 # actual call will be "dividend / divisor" - just check
690 # XXX DISABLE BAD HACK (False)
691 if False and isinstance(l
, ast
.Name
) and l
.id == 'dividend':
696 # return "function trunc_xxx(l, r)"
697 p
[0] = ast
.Call(ast
.Name(fn
, ast
.Load()), (l
, r
), [])
699 # return "l {binop} r"
700 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
701 elif p
[2] in ['<', '>', '=', '<=', '>=', '!=']:
702 p
[0] = binary_ops
[p
[2]]((p
[1], p
[3]))
703 elif identify_sint_mul_pattern(p
):
704 keywords
= [ast
.keyword(arg
='repeat', value
=p
[3])]
706 p
[0] = ast
.Call(ast
.Name("concat", ast
.Load()), l
, keywords
)
708 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
710 if isinstance(p
[2], str) and p
[2] == '-':
711 p
[0] = ast
.UnaryOp(unary_ops
[p
[2]], p
[1])
713 p
[0] = ast
.UnaryOp(unary_ops
[p
[1]], p
[2])
717 # power: atom trailer* ['**' factor]
718 # trailers enables function calls (and subscripts).
719 # so this is 'trailerlist'
720 def p_power(self
, p
):
722 | atom trailerlist"""
724 print("power dump atom notrailer")
725 print(astor
.dump_tree(p
[1]))
728 print("power dump atom")
729 print(astor
.dump_tree(p
[1]))
730 print("power dump trailerlist")
731 print(astor
.dump_tree(p
[2]))
732 p
[0] = apply_trailer(p
[1], p
[2], self
.read_regs
)
733 if isinstance(p
[1], ast
.Name
):
735 if name
in regs
+ fregs
:
736 self
.read_regs
.add(name
)
738 def p_atom_name(self
, p
):
741 if name
in self
.available_op_fields
:
742 self
.op_fields
.add(name
)
743 if name
== 'overflow':
744 self
.write_regs
.add(name
)
745 if self
.include_ca_in_write
:
746 if name
in ['CA', 'CA32']:
747 self
.write_regs
.add(name
)
748 if name
in ['CR', 'LR', 'CTR', 'TAR', 'FPSCR', 'MSR',
749 'SVSTATE', 'SVREMAP',
750 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3']:
751 self
.special_regs
.add(name
)
752 self
.write_regs
.add(name
) # and add to list to write
753 p
[0] = ast
.Name(id=name
, ctx
=ast
.Load())
755 def p_atom_number(self
, p
):
760 p
[0] = ast
.Constant(p
[1])
762 # '[' [listmaker] ']' |
764 def p_atom_listmaker(self
, p
):
765 """atom : LBRACK listmaker RBRACK"""
768 def p_listmaker(self
, p
):
769 """listmaker : test COMMA listmaker
773 p
[0] = ast
.List([p
[1]], ast
.Load())
775 p
[0] = ast
.List([p
[1]] + p
[3].nodes
, ast
.Load())
777 def p_atom_tuple(self
, p
):
778 """atom : LPAR testlist RPAR"""
781 print(astor
.dump_tree(p
[2]))
783 if isinstance(p
[2], ast
.Name
):
785 print("tuple name", name
)
786 if name
in self
.gprs
:
787 self
.read_regs
.add(name
) # add to list of regs to read
788 #p[0] = ast.Subscript(ast.Name("GPR", ast.Load()), ast.Str(p[2].id))
791 elif isinstance(p
[2], ast
.BinOp
):
792 if isinstance(p
[2].left
, ast
.Name
) and \
793 isinstance(p
[2].right
, ast
.Constant
) and \
794 p
[2].right
.value
== 0 and \
795 p
[2].left
.id in self
.gprs
:
797 self
.read_regs
.add(rid
) # add to list of regs to read
798 # create special call to GPR.getz or FPR.getz
800 gprz
= ast
.Name("FPR", ast
.Load())
802 gprz
= ast
.Name("GPR", ast
.Load())
803 # get testzero function
804 gprz
= ast
.Attribute(gprz
, "getz", ast
.Load())
805 # *sigh* see class GPR. we need index itself not reg value
806 ridx
= ast
.Name("_%s" % rid
, ast
.Load())
807 p
[0] = ast
.Call(gprz
, [ridx
], [])
808 print("tree", astor
.dump_tree(p
[0]))
814 def p_trailerlist(self
, p
):
815 """trailerlist : trailer trailerlist
821 p
[0] = ("TLIST", p
[1], p
[2])
823 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
824 def p_trailer(self
, p
):
825 """trailer : trailer_arglist
830 def p_trailer_arglist(self
, p
):
831 "trailer_arglist : LPAR arglist RPAR"
832 p
[0] = ("CALL", p
[2])
834 def p_trailer_subscript(self
, p
):
835 "trailer_subscript : LBRACK subscript RBRACK"
836 p
[0] = ("SUBS", p
[2])
838 # subscript: '.' '.' '.' | test | [test] ':' [test]
840 def p_subscript(self
, p
):
841 """subscript : test COLON test
846 if isinstance(p
[3], ast
.Constant
):
847 end
= ast
.Constant(p
[3].value
+1)
849 end
= ast
.BinOp(p
[3], ast
.Add(), ast
.Constant(1))
854 # testlist: test (',' test)* [',']
855 # Contains shift/reduce error
857 def p_testlist(self
, p
):
858 """testlist : testlist_multi COMMA
863 # May need to promote singleton to tuple
864 if isinstance(p
[1], list):
868 # Convert into a tuple?
869 if isinstance(p
[0], list):
870 p
[0] = ast
.Tuple(p
[0])
872 def p_testlist_multi(self
, p
):
873 """testlist_multi : testlist_multi COMMA test
879 if isinstance(p
[1], list):
885 # test: or_test ['if' or_test 'else' test] | lambdef
886 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
892 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
894 # XXX INCOMPLETE: this doesn't allow the trailing comma
896 def p_arglist(self
, p
):
897 """arglist : arglist COMMA argument
904 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
905 def p_argument(self
, p
):
909 def p_error(self
, p
):
910 # print "Error!", repr(p)
914 class GardenSnakeParser(PowerParser
):
915 def __init__(self
, lexer
=None, debug
=False, form
=None, incl_carry
=False, helper
=False):
917 self
.sd
= create_pdecode()
918 PowerParser
.__init
__(self
, form
, incl_carry
, helper
=helper
)
921 lexer
= IndentLexer(debug
=0)
923 self
.tokens
= lexer
.tokens
924 self
.parser
= yacc
.yacc(module
=self
, start
="file_input_end",
925 debug
=debug
, write_tables
=False)
927 def parse(self
, code
):
928 # self.lexer.input(code)
929 result
= self
.parser
.parse(code
, lexer
=self
.lexer
, debug
=self
.debug
)
931 result
= [ast
.ClassDef("ISACallerFnHelper", ["ISACallerHelper"], [], result
, decorator_list
=[])]
932 return ast
.Module(result
)
935 ###### Code generation ######
937 #from compiler import misc, syntax, pycodegen
940 _CACHE_PARSERS
= True
943 class GardenSnakeCompiler(object):
944 def __init__(self
, debug
=False, form
=None, incl_carry
=False, helper
=False):
947 parser
= _CACHED_PARSERS
[debug
, form
, incl_carry
, helper
]
949 parser
= GardenSnakeParser(debug
=debug
, form
=form
,
950 incl_carry
=incl_carry
, helper
=helper
)
951 _CACHED_PARSERS
[debug
, form
, incl_carry
, helper
] = parser
953 self
.parser
= deepcopy(parser
)
955 self
.parser
= GardenSnakeParser(debug
=debug
, form
=form
,
956 incl_carry
=incl_carry
, helper
=helper
)
958 def compile(self
, code
, mode
="exec", filename
="<string>"):
959 tree
= self
.parser
.parse(code
)
963 #misc.set_filename(filename, tree)
964 return compile(tree
, mode
="exec", filename
="<string>")
966 gen
= pycodegen
.ModuleCodeGenerator(tree
)