From 9543ff6630b5006242331d6db3762c5d7c762f52 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sun, 5 Apr 2020 17:05:43 +0100 Subject: [PATCH] add start on switch/case/default in parser --- src/soc/decoder/power_pseudo.py | 13 ++++++- src/soc/decoder/pseudo/lexer.py | 38 ++++++++++++++------ src/soc/decoder/pseudo/parser.py | 56 ++++++++++++++++++++++++++++++ src/soc/decoder/pseudo/pywriter.py | 3 +- 4 files changed, 97 insertions(+), 13 deletions(-) diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index 2de3f5b8..7005bd08 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -166,7 +166,18 @@ _lswx = """ GPR(r)[x] <- 1 """ -code = lswx +switchtest = """ +switch (n) + case(1): x <- 5 + case(2): fallthrough + case(3): + x <- 3 + default: + x <- 9 +""" + +code = switchtest +#code = lswx #code = testcond #code = testdo #code = _bpermd diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index 1bd5d79e..2e637365 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -419,20 +419,32 @@ class IndentLexer(PowerLexer): except StopIteration: return None +switchtest = """ +switch (n) + case(1): x <- 5 + case(2): fallthrough + case(3): + x <- 3 + default: + x <- 9 +print (5) +""" + +cnttzd = """ +n <- 0 +do while n < 64 + if (RS)[63-n] = 0b1 then + leave + n <- n + 1 +RA <- EXTZ64(n) +print (RA) +""" + if __name__ == '__main__': # quick test/demo - cnttzd = """ - n <- 0 - do while n < 64 - if (RS)[63-n] = 0b1 then - leave - n <- n + 1 - RA <- EXTZ64(n) - print (RA) - """ - - code = cnttzd + #code = cnttzd + code = switchtest lexer = IndentLexer(debug=1) # Give the lexer some input @@ -440,3 +452,7 @@ if __name__ == '__main__': print (code) lexer.input(code) + tokens = iter(lexer.token, None) + for token in tokens: + print (token) + diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index a65581d5..7906e825 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -382,6 +382,7 @@ class PowerParser: def p_compound_stmt(self, p): """compound_stmt : if_stmt | while_stmt + | switch_stmt | for_stmt | funcdef """ @@ -411,6 +412,61 @@ class PowerParser: else: p[0] = ast.While(p[3], p[5], p[8]) + def p_switch_smt(self, p): + """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT + """ + switchon = p[3] + print("switch stmt") + print(astor.dump_tree(p[1])) + + cases = [] + current_cases = [] + for (case, suite) in p[8]: + print (case, suite) + res = [] + p[0] = ast.If([], [], []) + + def p_switches(self, p): + """switches : switch_list switch_default + | switch_default + """ + if len(p) == 3: + p[0] = p[1] + [p[2]] + else: + p[0] = [p[1]] + + def p_switch_list(self, p): + """switch_list : switch_case switch_list + | switch_case + """ + if len(p) == 3: + p[0] = [p[1]] + p[2] + else: + p[0] = [p[1]] + + def p_switch_case(self, p): + """switch_case : CASE LPAR atomlist RPAR COLON suite + """ + # XXX bad hack + if isinstance(p[6][0], ast.Name) and p[6][0].id == 'fallthrough': + p[6] = None + p[0] = (p[3], p[6]) + + def p_switch_default(self, p): + """switch_default : DEFAULT COLON suite + """ + p[0] = ('default', p[3]) + + def p_atomlist(self, p): + """atomlist : atom COMMA atomlist + | atom + """ + assert isinstance(p[1], ast.Constant), "case must be numbers" + if len(p) == 4: + p[0] = [p[1].value] + p[3] + else: + p[0] = [p[1].value] + def p_if_stmt(self, p): """if_stmt : IF test COLON suite ELSE COLON if_stmt | IF test COLON suite ELSE COLON suite diff --git a/src/soc/decoder/pseudo/pywriter.py b/src/soc/decoder/pseudo/pywriter.py index 052ee95e..5002ce4c 100644 --- a/src/soc/decoder/pseudo/pywriter.py +++ b/src/soc/decoder/pseudo/pywriter.py @@ -72,7 +72,8 @@ class PyISAWriter(ISA): if __name__ == '__main__': isa = PyISAWriter() - isa.write_pysource('system') + isa.write_pysource('sprset') + #isa.write_pysource('system') exit(0) isa.write_pysource('stringldst') isa.write_pysource('fixedshift') -- 2.30.2