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
14 def port_decl_do_not_use(comment
, dt
, name
):
15 if dt
is None or dt
.dims
is None:
19 # XXX TODO, better checking, should be using data structure... *sigh*
20 width
= width
[1:-1] # strip brackets
21 width
= width
.split(':')
22 assert width
[0] == '0'
24 return 'self.%s = Signal(%s) # %s' % (name
, width
, comment
)
31 def __init__(self
, comment
, dt
, name
):
32 self
.comment
= comment
37 return port_decl_do_not_use(self
.comment
, self
.dt
, self
.name
)
41 def __init__(self
, left
, op
, right
):
48 def __init__(self
, outputfn
):
49 self
.outputfn
= outputfn
50 self
.outputfile
= None
55 if(self
.outputfile
is None):
56 self
.outputfile
= open(self
.outputfn
, "w")
57 self
.outputfile
.write(preamble
)
61 self
.outputfile
.write(str(p
)+"\n")
67 # m.d.comb += [l.eq(r)]
69 def indent(self
, count
):
71 return Leaf(token
.INDENT
, '>>> '*count
)
73 return Leaf(token
.INDENT
, ' '*4*count
)
75 def dedent(self
, count
):
76 return Leaf(token
.DEDENT
, '')
79 return Leaf(token
.NEWLINE
, '\n')
81 def port_decl(self
, comment
, dt
, name
):
82 port
= PortDecl(comment
, dt
, name
)
86 def initFunc(self
, ports
, params
):
87 params
= [Leaf(token
.LPAR
, '('), Leaf(
88 token
.NAME
, "self")] + [Leaf(token
.RPAR
, ')')]
89 # TODO handle sv params
90 fn
= [Leaf(token
.NAME
, 'def'),
91 Leaf(token
.NAME
, '__init__', prefix
=' '),
92 Node(syms
.parameters
, params
),
93 Leaf(token
.COLON
, ':'),
96 fndef
= Node(syms
.funcdef
, fn
)
97 stmts
= Node(syms
.stmt
, [fndef
])
99 stmts
.children
.append(self
.indent(2))
100 stmts
.children
.append(port
.initNode())
101 stmts
.children
.append(self
.nl())
104 def elaborateFunc(self
):
105 params
= [Leaf(token
.LPAR
, '('), Leaf(
106 token
.NAME
, "self, platform=None"), Leaf(token
.RPAR
, ')')]
107 fn
= [Leaf(token
.NAME
, 'def'),
108 Leaf(token
.NAME
, 'elaborate', prefix
=' '),
109 Node(syms
.parameters
, params
),
110 Leaf(token
.COLON
, ':'),
113 fndef
= Node(syms
.funcdef
, fn
)
114 stmts
= Node(syms
.stmt
, [fndef
])
115 stmts
.children
.append(self
.indent(2))
116 stmts
.children
.append(Leaf(token
.STRING
, "m = Module()"))
117 stmts
.children
.append(self
.nl())
119 for a
in self
.assign
:
120 stmts
.children
.append(self
.indent(2))
121 # m.d.sync += self.left.eq(right)
122 stmts
.children
.append(Leaf(token
.STRING
, "m.d.comb += self."))
123 stmts
.children
.append(Leaf(token
.STRING
, a
.left
))
124 stmts
.children
.append(Leaf(token
.STRING
, ".eq(self."))
125 stmts
.children
.append(Leaf(token
.STRING
, a
.right
))
126 stmts
.children
.append(Leaf(token
.STRING
, ")"))
127 stmts
.children
.append(self
.nl())
129 # for a in self.assign:
135 stmts
.children
.append(self
.indent(2))
136 stmts
.children
.append(Leaf(token
.STRING
, "return m"))
137 stmts
.children
.append(self
.nl())
140 def module_1(self
, p
):
143 clsname
= [Leaf(token
.NAME
, 'class'),
144 Leaf(token
.NAME
, p
[4], prefix
=' '),
145 Leaf(token
.LPAR
, '('),
146 Leaf(token
.NAME
, 'Elaboratable'),
147 Leaf(token
.LPAR
, ')'),
148 Leaf(token
.COLON
, ':'),
152 suite
= Node(syms
.suite
, [Leaf(token
.NEWLINE
, '\n'),
154 self
.initFunc(ports
, params
),
159 clsdecl
= Node(syms
.classdef
, clsname
+ [suite
])
160 clsdecl
= Node(syms
.compound_stmt
, [clsdecl
])
162 self
.printpy(str(clsdecl
))
165 def appendComments(self
, data
):
167 self
.outputfile
.write(data
)
168 #lines = data.split("\n")
170 # self.printpy("#"+line)
172 # combinatorical assign
173 def cont_assign_1(self
, p
):
174 # print("#ASSIGN:BROKEN"+str(list(p)))
175 self
.assign
+= [Assignment(p
[1], p
[2], p
[3])]