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
.outputfile
= open(outputfn
, "w")
50 self
.outputfile
.write(preamble
)
55 self
.outputfile
.write(str(p
)+"\n")
61 # m.d.comb += [l.eq(r)]
63 def indent(self
, count
):
65 return Leaf(token
.INDENT
, '>>> '*count
)
67 return Leaf(token
.INDENT
, ' '*4*count
)
69 def dedent(self
, count
):
70 return Leaf(token
.DEDENT
, '')
73 return Leaf(token
.NEWLINE
, '\n')
75 def port_decl(self
, comment
, dt
, name
):
76 port
= PortDecl(comment
, dt
, name
)
80 def initFunc(self
, ports
, params
):
81 params
= [Leaf(token
.LPAR
, '('), Leaf(
82 token
.NAME
, "self")] + [Leaf(token
.RPAR
, ')')]
83 # TODO handle sv params
84 fn
= [Leaf(token
.NAME
, 'def'),
85 Leaf(token
.NAME
, '__init__', prefix
=' '),
86 Node(syms
.parameters
, params
),
87 Leaf(token
.COLON
, ':'),
90 fndef
= Node(syms
.funcdef
, fn
)
91 stmts
= Node(syms
.stmt
, [fndef
])
93 stmts
.children
.append(self
.indent(2))
94 stmts
.children
.append(port
.initNode())
95 stmts
.children
.append(self
.nl())
98 def elaborateFunc(self
):
99 params
= [Leaf(token
.LPAR
, '('), Leaf(
100 token
.NAME
, "self, platform=None"), Leaf(token
.RPAR
, ')')]
101 fn
= [Leaf(token
.NAME
, 'def'),
102 Leaf(token
.NAME
, 'elaborate', prefix
=' '),
103 Node(syms
.parameters
, params
),
104 Leaf(token
.COLON
, ':'),
107 fndef
= Node(syms
.funcdef
, fn
)
108 stmts
= Node(syms
.stmt
, [fndef
])
109 stmts
.children
.append(self
.indent(2))
110 stmts
.children
.append(Leaf(token
.STRING
, "m = Module()"))
111 stmts
.children
.append(self
.nl())
113 for a
in self
.assign
:
114 stmts
.children
.append(self
.indent(2))
115 # m.d.sync += self.left.eq(right)
116 stmts
.children
.append(Leaf(token
.STRING
, "m.d.comb += self."))
117 stmts
.children
.append(Leaf(token
.STRING
, a
.left
))
118 stmts
.children
.append(Leaf(token
.STRING
, ".eq(self."))
119 stmts
.children
.append(Leaf(token
.STRING
, a
.right
))
120 stmts
.children
.append(Leaf(token
.STRING
, ")"))
121 stmts
.children
.append(self
.nl())
123 # for a in self.assign:
129 stmts
.children
.append(self
.indent(2))
130 stmts
.children
.append(Leaf(token
.STRING
, "return m"))
131 stmts
.children
.append(self
.nl())
134 def module_1(self
, p
):
137 clsname
= [Leaf(token
.NAME
, 'class'),
138 Leaf(token
.NAME
, p
[4], prefix
=' '),
139 Leaf(token
.LPAR
, '('),
140 Leaf(token
.NAME
, 'Elaboratable'),
141 Leaf(token
.LPAR
, ')'),
142 Leaf(token
.COLON
, ':'),
146 suite
= Node(syms
.suite
, [Leaf(token
.NEWLINE
, '\n'),
148 self
.initFunc(ports
, params
),
153 clsdecl
= Node(syms
.classdef
, clsname
+ [suite
])
154 clsdecl
= Node(syms
.compound_stmt
, [clsdecl
])
156 self
.printpy(str(clsdecl
))
157 print("=====================")
161 def appendComments(self
, data
):
162 self
.outputfile
.write(data
)
163 #lines = data.split("\n")
165 # self.printpy("#"+line)
167 # combinatorical assign
168 def cont_assign_1(self
, p
):
169 # print("#ASSIGN:BROKEN"+str(list(p)))
170 self
.assign
+= [Assignment(p
[1], p
[2], p
[3])]