From 940f6d8e4d53e1a266256c2617975a14e8b4ff9a Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 2 Apr 2020 17:30:39 +0100 Subject: [PATCH] start parsing all source files, output python --- libreriscv | 2 +- src/soc/decoder/power_pseudo.py | 11 ++++++++++- src/soc/decoder/pseudo/lexer.py | 4 +++- src/soc/decoder/pseudo/pagereader.py | 6 ++++++ src/soc/decoder/pseudo/parser.py | 7 ++++--- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/libreriscv b/libreriscv index 03177977..620efa21 160000 --- a/libreriscv +++ b/libreriscv @@ -1 +1 @@ -Subproject commit 03177977f70255421290a59caa3db84658e7f3e5 +Subproject commit 620efa218f922eba23b19189109bec785a624a82 diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index 104edd98..76e50906 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -13,6 +13,7 @@ from pprint import pprint from copy import copy from ply import lex, yacc import astor +import ast from soc.decoder.power_decoder import create_pdecode from nmigen.back.pysim import Simulator, Delay @@ -102,6 +103,15 @@ class GPR(dict): return self.regfile[rnum] +def convert_to_python(pcode): + + gsc = GardenSnakeCompiler() + + tree = gsc.compile(pcode, mode="exec", filename="string") + tree = ast.fix_missing_locations(tree) + return astor.to_source(tree) + + def test(): gsc = GardenSnakeCompiler() @@ -115,7 +125,6 @@ def test(): _compile = gsc.compile tree = _compile(code, mode="single", filename="string") - import ast tree = ast.fix_missing_locations(tree) print ( ast.dump(tree) ) diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index 9076672d..ba517c4f 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -133,7 +133,7 @@ def indentation_filter(tokens): depth = 0 prev_was_ws = False for token in tokens: - if 1: + if 0: print ("Process", depth, token.indent, token,) if token.at_line_start: print ("at_line_start",) @@ -367,6 +367,7 @@ class PowerLexer: print ("Skipping", repr(t.value[0])) t.lexer.skip(1) + # Combine Ply and my filters into a new lexer class IndentLexer(PowerLexer): @@ -374,6 +375,7 @@ class IndentLexer(PowerLexer): self.build(debug=debug, optimize=optimize, lextab=lextab, reflags=reflags) self.token_stream = None + def input(self, s, add_endmarker=True): self.lexer.paren_count = 0 self.lexer.brack_count = 0 diff --git a/src/soc/decoder/pseudo/pagereader.py b/src/soc/decoder/pseudo/pagereader.py index 61b7838f..aa0557bc 100644 --- a/src/soc/decoder/pseudo/pagereader.py +++ b/src/soc/decoder/pseudo/pagereader.py @@ -66,6 +66,7 @@ class ISA: def __init__(self): self.instr = OrderedDict() self.forms = {} + self.page = {} for pth in os.listdir(os.path.join(get_isa_dir())): print (get_isa_dir(), pth) assert pth.endswith(".mdwn"), "only %s in isa dir" % pth @@ -161,6 +162,11 @@ class ISA: fl = self.forms.get(form, []) self.forms[form] = fl + [opcode] + # create list of instructions by page + page = op['page'] + pl = self.page.get(page, []) + self.page[page] = pl + [opcode] + def pprint_ops(self): for k, v in self.instr.items(): print ("# %s %s" % (v.opcode, v.desc)) diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index 69fe6aa9..8f76f327 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -470,7 +470,7 @@ class GardenSnakeParser(PowerParser): def __init__(self, lexer = None): PowerParser.__init__(self) if lexer is None: - lexer = IndentLexer(debug=1) + lexer = IndentLexer(debug=0) self.lexer = lexer self.tokens = lexer.tokens self.parser = yacc.yacc(module=self, start="file_input_end", @@ -479,8 +479,8 @@ class GardenSnakeParser(PowerParser): self.sd = create_pdecode() def parse(self, code): - self.lexer.input(code) - result = self.parser.parse(lexer = self.lexer, debug=False) + #self.lexer.input(code) + result = self.parser.parse(code, lexer=self.lexer, debug=False) return ast.Module(result) @@ -491,6 +491,7 @@ class GardenSnakeParser(PowerParser): class GardenSnakeCompiler(object): def __init__(self): self.parser = GardenSnakeParser() + def compile(self, code, mode="exec", filename=""): tree = self.parser.parse(code) print ("snake") -- 2.30.2