X-Git-Url: https://git.libre-soc.org/?p=sv2nmigen.git;a=blobdiff_plain;f=absyn.py;h=d52250206719fbefcd53b2dba3c660b529f8ef9a;hp=e1213573921bc97953feedeaaf9b02be81121d3a;hb=c2475e188a965c7f34e28d3d022205f9a97ed215;hpb=71d1f25d91fd5bfa2396591c0833dfa56f3f6f3a diff --git a/absyn.py b/absyn.py index e121357..d522502 100644 --- a/absyn.py +++ b/absyn.py @@ -10,12 +10,40 @@ from nmigen import Signal, Module, Const, Cat, Elaboratable """ +def port_decl_do_not_use(comment, dt, name): + if dt is None or dt.dims is None: + width = '' # width: 1 + else: + width = dt.dims + # XXX TODO, better checking, should be using data structure... *sigh* + width = width[1:-1] # strip brackets + width = width.split(':') + assert width[0] == '0' + width = width[1] + return 'self.%s = Signal(%s) # %s' % (name, width, comment) + indent_debug = 0 +class PortDecl: + def __init__(self,comment,dt,name): + self.comment = comment + self.dt=dt + self.name=name + def initNode(self): + return port_decl_do_not_use(self.comment,self.dt,self.name) + +class Assignment: + def __init__(self,left,op,right): + self.left = left + self.op = op + self.right = right + class Absyn: def __init__(self): self.outputfile = open("output.py","w") self.outputfile.write(preamble) + self.assign = [] + self.ports = [] def printpy(self,p): self.outputfile.write(str(p)+"\n") def assign(self,p): @@ -32,29 +60,65 @@ class Absyn: def dedent(self,count): return Leaf(token.DEDENT, '') def nl(self): - return Leaf(token.NEWLINE, '#\n') - - def initPorts(self,params,ports): - pass_stmt = Node(syms.pass_stmt ,[Leaf(token.NAME, "def __init__(self):")]) - if params: - params = [Leaf(token.LPAR, '(')] + params + [Leaf(token.RPAR, ')')] - fn = [Leaf(token.NAME, 'def'), - Leaf(token.NAME, '__initXXX__', prefix=' '), - Node(syms.parameters, params), - Leaf(token.COLON, ':')] - fndef = Node(syms.funcdef, fn) - stmts = Node(syms.stmt, [fndef]) - else: - stmts = Node(syms.small_stmt, [pass_stmt, Leaf(token.NEWLINE, '\n')]) - stmts = Node(syms.stmt, [stmts]) + return Leaf(token.NEWLINE, '\n') + def port_decl(self,comment, dt, name): + port = PortDecl(comment,dt,name) + self.ports += [port] + return port + + def initFunc(self,ports,params): + params = [Leaf(token.LPAR, '('),Leaf(token.NAME,"self")] + [Leaf(token.RPAR, ')')] + # TODO handle sv params + fn = [Leaf(token.NAME, 'def'), + Leaf(token.NAME, '__init__', prefix=' '), + Node(syms.parameters, params), + Leaf(token.COLON, ':'), + self.nl() + ] + fndef = Node(syms.funcdef, fn) + stmts = Node(syms.stmt, [fndef]) for port in ports: stmts.children.append(self.indent(2)) - stmts.children.append(port) + stmts.children.append(port.initNode()) stmts.children.append(self.nl()) - return stmts + + def elaborateFunc(self): + params = [Leaf(token.LPAR, '('),Leaf(token.NAME,"self, platform=None"),Leaf(token.RPAR, ')')] + fn = [Leaf(token.NAME, 'def'), + Leaf(token.NAME, 'elaborate', prefix=' '), + Node(syms.parameters, params), + Leaf(token.COLON, ':'), + self.nl() + ] + fndef = Node(syms.funcdef, fn) + stmts = Node(syms.stmt, [fndef]) + stmts.children.append(self.indent(2)) + stmts.children.append(Leaf(token.STRING,"m = Module()")) + stmts.children.append(self.nl()) + + for a in self.assign: + stmts.children.append(self.indent(2)) + # m.d.sync += self.left.eq(right) + stmts.children.append(Leaf(token.STRING,"m.d.comb += self.")) + stmts.children.append(Leaf(token.STRING,a.left)) + stmts.children.append(Leaf(token.STRING,".eq(self.")) + stmts.children.append(Leaf(token.STRING,a.right)) + stmts.children.append(Leaf(token.STRING,")")) + stmts.children.append(self.nl()) + + #for a in self.assign: + # + # + #ports = a[8] + # + + stmts.children.append(self.indent(2)) + stmts.children.append(Leaf(token.STRING,"return m")) + stmts.children.append(self.nl()) + return stmts def module_1(self,p): params = p[7] @@ -65,16 +129,25 @@ class Absyn: Leaf(token.NAME, 'Elaboratable'), Leaf(token.LPAR,')'), Leaf(token.COLON, ':'), + self.nl(), ] suite = Node(syms.suite, [Leaf(token.NEWLINE, '\n'), self.indent(1), - self.initPorts(params,ports), - self.dedent(1) + self.initFunc(ports,params), + self.indent(1), + self.elaborateFunc() + ]) clsdecl = Node(syms.classdef, clsname + [suite]) clsdecl = Node(syms.compound_stmt, [clsdecl]) - #.printpy("#clsdecl"+ repr(clsdecl)) - #absyn.printpy("#clsstr:") + self.printpy(str(clsdecl)) + print("=====================") + print(str(clsdecl)) return clsdecl + + # combinatorical assign + def cont_assign_1(self,p): + print("#ASSIGN:BROKEN"+str(list(p))) + self.assign += [Assignment(p[1],p[2],p[3])]