This commit is contained in:
Sebastien Bourdeauducq 2011-12-05 19:25:32 +01:00
parent ec51f09c98
commit 4340680704
3 changed files with 18 additions and 61 deletions

0
migen/bank/__init__.py Normal file
View File

View File

@ -86,36 +86,16 @@ class Operator(Value):
self.op = op self.op = op
self.operands = list(map(_cst, operands)) self.operands = list(map(_cst, operands))
def __str__(self):
arity = len(self.operands)
if arity == 1:
r = self.op + str(self.operands[0])
elif arity == 2:
r = str(self.operands[0]) + " " + self.op + " " + str(self.operands[1])
else:
r = self.op + "(" + ", ".join(map(str, self.operands)) + ")"
return "(" + r + ")"
class Slice(Value): class Slice(Value):
def __init__(self, value, start, stop): def __init__(self, value, start, stop):
self.value = value self.value = value
self.start = start self.start = start
self.stop = stop self.stop = stop
def __str__(self):
if self.start + 1 == self.stop:
sr = "[" + str(self.start) + "]"
else:
sr = "[" + str(self.start) + ":" + str(self.stop) + "]"
return str(self.value) + sr
class Cat(Value): class Cat(Value):
def __init__(self, *args): def __init__(self, *args):
self.l = list(map(_cst, args)) self.l = list(map(_cst, args))
def __str__(self):
return "{" + ", ".join(map(str, self.l)) + "}"
class Constant(Value): class Constant(Value):
def __init__(self, n, bv=None): def __init__(self, n, bv=None):
if bv == None: if bv == None:
@ -124,12 +104,6 @@ class Constant(Value):
Value.__init__(self, bv) Value.__init__(self, bv)
self.n = n self.n = n
def __str__(self):
if self.n >= 0:
return str(self.bv) + str(self.n)
else:
return "-" + str(self.bv) + str(-self.n)
def _cst(x): def _cst(x):
if isinstance(x, int): if isinstance(x, int):
return Constant(x) return Constant(x)
@ -144,9 +118,6 @@ class Signal(Value):
self.name = name self.name = name
self.reset = Constant(reset, bv) self.reset = Constant(reset, bv)
def __str__(self):
return self.name
def __hash__(self): def __hash__(self):
return id(self) return id(self)
@ -160,40 +131,22 @@ class Assign:
self.l = l self.l = l
self.r = _cst(r) self.r = _cst(r)
def __str__(self):
return str(self.l) + " = " + str(self.r)
class StatementList: class StatementList:
def __init__(self, l=[]): def __init__(self, l=[]):
self.l = l self.l = l
def __str__(self):
return "\n".join(map(str, self.l))
def _sl(x): def _sl(x):
if isinstance(x, list): if isinstance(x, list):
return StatementList(x) return StatementList(x)
else: else:
return x return x
def _indent(s):
if s:
return "\t" + s.replace("\n", "\n\t")
else:
return ""
class If: class If:
def __init__(self, cond, t, f=StatementList()): def __init__(self, cond, t, f=StatementList()):
self.cond = cond self.cond = cond
self.t = _sl(t) self.t = _sl(t)
self.f = _sl(f) self.f = _sl(f)
def __str__(self):
r = "if " + str(self.cond) + ":\n" + _indent(str(self.t))
if self.f.l:
r += "\nelse:\n" + _indent(str(self.f))
return r
class Case: class Case:
def __init__(self, test, cases=[], default=StatementList()): def __init__(self, test, cases=[], default=StatementList()):
self.test = test self.test = test
@ -207,8 +160,5 @@ class Fragment:
self.comb = _sl(comb) self.comb = _sl(comb)
self.sync = _sl(sync) self.sync = _sl(sync)
def __str__(self):
return "Comb:\n" + _indent(str(self.comb)) + "\nSync:\n" + _indent(str(self.sync))
def __add__(self, other): def __add__(self, other):
return Fragment(self.comb.l + other.comb.l, self.sync.l + other.sync.l) return Fragment(self.comb.l + other.comb.l, self.sync.l + other.sync.l)

View File

@ -20,17 +20,20 @@ def Convert(f, outs=set(), ins=set(), name="top", clkname="sys_clk", rstname="sy
n += ns.GetName(s) n += ns.GetName(s)
return n return n
def printnode(level, node): def printexpr(node):
if isinstance(node, Constant): if isinstance(node, Constant):
return str(node) if node.n >= 0:
return str(node.bv) + str(node.n)
else:
return "-" + str(node.bv) + str(-self.n)
elif isinstance(node, Signal): elif isinstance(node, Signal):
return ns.GetName(node) return ns.GetName(node)
elif isinstance(node, Operator): elif isinstance(node, Operator):
arity = len(node.operands) arity = len(node.operands)
if arity == 1: if arity == 1:
r = self.op + str(node.operands[0]) r = self.op + printexpr(node.operands[0])
elif arity == 2: elif arity == 2:
r = printnode(level, node.operands[0]) + " " + node.op + " " + printnode(level, node.operands[1]) r = printexpr(node.operands[0]) + " " + node.op + " " + printexpr(node.operands[1])
else: else:
raise TypeError raise TypeError
return "(" + r + ")" return "(" + r + ")"
@ -39,18 +42,22 @@ def Convert(f, outs=set(), ins=set(), name="top", clkname="sys_clk", rstname="sy
sr = "[" + str(node.start) + "]" sr = "[" + str(node.start) + "]"
else: else:
sr = "[" + str(node.stop-1) + ":" + str(node.start) + "]" sr = "[" + str(node.stop-1) + ":" + str(node.start) + "]"
return str(node.value) + sr return printexpr(node.value) + sr
elif isinstance(node, Cat): elif isinstance(node, Cat):
l = list(map(partial(printnode, level), node.l)) l = list(map(printexpr, node.l))
l.reverse() l.reverse()
return "{" + ", ".join(l) + "}" return "{" + ", ".join(l) + "}"
elif isinstance(node, Assign): else:
raise TypeError
def printnode(level, node):
if isinstance(node, Assign):
# TODO: variables # TODO: variables
return "\t"*level + printnode(level, node.l) + " <= " + printnode(level, node.r) + ";\n" return "\t"*level + printexpr(node.l) + " <= " + printexpr(node.r) + ";\n"
elif isinstance(node, StatementList): elif isinstance(node, StatementList):
return "".join(list(map(partial(printnode, level), node.l))) return "".join(list(map(partial(printnode, level), node.l)))
elif isinstance(node, If): elif isinstance(node, If):
r = "\t"*level + "if (" + printnode(level, node.cond) + ") begin\n" r = "\t"*level + "if (" + printexpr(node.cond) + ") begin\n"
r += printnode(level + 1, node.t) r += printnode(level + 1, node.t)
if node.f.l: if node.f.l:
r += "\t"*level + "end else begin\n" r += "\t"*level + "end else begin\n"
@ -58,9 +65,9 @@ def Convert(f, outs=set(), ins=set(), name="top", clkname="sys_clk", rstname="sy
r += "\t"*level + "end\n" r += "\t"*level + "end\n"
return r return r
elif isinstance(node, Case): elif isinstance(node, Case):
r = "\t"*level + "case (" + printnode(level, node.test) + ")\n" r = "\t"*level + "case (" + printexpr(node.test) + ")\n"
for case in node.cases: for case in node.cases:
r += "\t"*(level + 1) + printnode(level, case[0]) + ": begin\n" r += "\t"*(level + 1) + printexpr(case[0]) + ": begin\n"
r += printnode(level + 2, case[1]) r += printnode(level + 2, case[1])
r += "\t"*(level + 1) + "end\n" r += "\t"*(level + 1) + "end\n"
r += "\t"*level + "endcase\n" r += "\t"*level + "endcase\n"