1 from lib2to3
.pytree
import Node
, Leaf
2 from lib2to3
.pgen2
import token
3 from lib2to3
.pygram
import python_symbols
as syms
5 preamble
= """# this file has been generated by sv2nmigen
7 from nmigen import Signal, Module, Const, Cat, Elaboratable
17 self
.outputfile
= open("output.py","w")
18 self
.outputfile
.write(preamble
)
21 self
.outputfile
.write(str(p
)+"\n")
26 # m.d.comb += [l.eq(r)]
27 def indent(self
,count
):
29 return Leaf(token
.INDENT
, '>>> '*count
)
31 return Leaf(token
.INDENT
, ' '*4*count
)
33 def dedent(self
,count
):
34 return Leaf(token
.DEDENT
, '')
36 return Leaf(token
.NEWLINE
, '\n')
38 def port_decl(self
,comment
, dt
, name
):
41 def initPorts(self
,params
,ports
):
42 pass_stmt
= Node(syms
.pass_stmt
,[Leaf(token
.NAME
, "def __init__(self):#FIXME")])
44 params
= [Leaf(token
.LPAR
, '(')] + params
+ [Leaf(token
.RPAR
, ')')]
45 fn
= [Leaf(token
.NAME
, 'def'),
46 Leaf(token
.NAME
, '__initXXX__', prefix
=' '),
47 Node(syms
.parameters
, params
),
48 Leaf(token
.COLON
, ':')]
49 fndef
= Node(syms
.funcdef
, fn
)
50 stmts
= Node(syms
.stmt
, [fndef
])
52 stmts
= Node(syms
.small_stmt
, [pass_stmt
, Leaf(token
.NEWLINE
, '\n')])
53 stmts
= Node(syms
.stmt
, [stmts
])
56 stmts
.children
.append(self
.indent(2))
57 stmts
.children
.append(port
)
58 stmts
.children
.append(self
.nl())
62 def initFunc(self
,ports
,params
):
63 params
= [Leaf(token
.LPAR
, '('),Leaf(token
.NAME
,"self")] + [Leaf(token
.RPAR
, ')')]
64 # TODO handle sv params
65 fn
= [Leaf(token
.NAME
, 'def'),
66 Leaf(token
.NAME
, '__init__', prefix
=' '),
67 Node(syms
.parameters
, params
),
68 Leaf(token
.COLON
, ':'),
71 fndef
= Node(syms
.funcdef
, fn
)
72 stmts
= Node(syms
.stmt
, [fndef
])
74 stmts
.children
.append(self
.indent(2))
75 stmts
.children
.append(port
)
76 stmts
.children
.append(self
.nl())
79 def elaborateFunc(self
):
80 params
= [Leaf(token
.LPAR
, '('),Leaf(token
.NAME
,"self, platform=None"),Leaf(token
.RPAR
, ')')]
81 fn
= [Leaf(token
.NAME
, 'def'),
82 Leaf(token
.NAME
, 'elaborate', prefix
=' '),
83 Node(syms
.parameters
, params
),
84 Leaf(token
.COLON
, ':'),
87 fndef
= Node(syms
.funcdef
, fn
)
88 stmts
= Node(syms
.stmt
, [fndef
])
89 stmts
.children
.append(self
.indent(2))
90 stmts
.children
.append(Leaf(token
.STRING
,"m = Module()"))
91 stmts
.children
.append(self
.nl())
94 stmts
.children
.append(self
.indent(2))
95 stmts
.children
.append(Leaf(token
.STRING
,"#FIXME_ASSIGN"+str(list(a
[8]))))
96 stmts
.children
.append(self
.nl())
98 stmts
.children
.append(self
.indent(2))
99 stmts
.children
.append(Leaf(token
.STRING
,"return m"))
100 stmts
.children
.append(self
.nl())
103 def module_1(self
,p
):
106 clsname
= [Leaf(token
.NAME
, 'class'),
107 Leaf(token
.NAME
, p
[4], prefix
=' '),
108 Leaf(token
.LPAR
,'('),
109 Leaf(token
.NAME
, 'Elaboratable'),
110 Leaf(token
.LPAR
,')'),
111 Leaf(token
.COLON
, ':'),
115 suite
= Node(syms
.suite
, [Leaf(token
.NEWLINE
, '\n'),
117 self
.initFunc(ports
,params
),
122 clsdecl
= Node(syms
.classdef
, clsname
+ [suite
])
123 clsdecl
= Node(syms
.compound_stmt
, [clsdecl
])
125 self
.printpy(str(clsdecl
))
128 # combinatorical assign
129 def cont_assign_1(self
,p
):
130 #self.printpy("#ASSIGN"+str(list(p)))