2012-10-31 10:59:12 -04:00
|
|
|
import inspect
|
|
|
|
import ast
|
2012-11-09 11:37:42 -05:00
|
|
|
from operator import itemgetter
|
2012-10-31 10:59:12 -04:00
|
|
|
|
|
|
|
from migen.fhdl.structure import *
|
2012-11-10 12:04:05 -05:00
|
|
|
from migen.fhdl.structure import _Slice
|
2012-11-09 11:37:42 -05:00
|
|
|
from migen.fhdl import visit as fhdl
|
|
|
|
from migen.corelogic.fsm import FSM
|
2012-10-31 10:59:12 -04:00
|
|
|
from migen.pytholite import transel
|
2012-11-10 16:59:14 -05:00
|
|
|
from migen.pytholite.io import make_io_object, gen_io
|
2012-10-31 10:59:12 -04:00
|
|
|
|
|
|
|
class FinalizeError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class _AbstractLoad:
|
|
|
|
def __init__(self, target, source):
|
|
|
|
self.target = target
|
|
|
|
self.source = source
|
2012-11-09 11:37:42 -05:00
|
|
|
|
|
|
|
def lower(self):
|
|
|
|
if not self.target.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
return self.target.sel.eq(self.target.source_encoding[self.source])
|
|
|
|
|
|
|
|
class _LowerAbstractLoad(fhdl.NodeTransformer):
|
|
|
|
def visit_unknown(self, node):
|
|
|
|
if isinstance(node, _AbstractLoad):
|
|
|
|
return node.lower()
|
|
|
|
else:
|
|
|
|
return node
|
2012-10-31 10:59:12 -04:00
|
|
|
|
|
|
|
class _Register:
|
|
|
|
def __init__(self, name, nbits):
|
2012-11-09 14:19:22 -05:00
|
|
|
self.name = name
|
|
|
|
self.storage = Signal(BV(nbits), name=self.name)
|
2012-10-31 10:59:12 -04:00
|
|
|
self.source_encoding = {}
|
|
|
|
self.finalized = False
|
|
|
|
|
|
|
|
def load(self, source):
|
|
|
|
if source not in self.source_encoding:
|
|
|
|
self.source_encoding[source] = len(self.source_encoding) + 1
|
|
|
|
return _AbstractLoad(self, source)
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
if self.finalized:
|
|
|
|
raise FinalizeError
|
2012-11-09 14:19:22 -05:00
|
|
|
self.sel = Signal(BV(bits_for(len(self.source_encoding) + 1)), name="pl_regsel_"+self.name)
|
2012-10-31 10:59:12 -04:00
|
|
|
self.finalized = True
|
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
if not self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
# do nothing when sel == 0
|
2012-11-09 11:37:42 -05:00
|
|
|
items = sorted(self.source_encoding.items(), key=itemgetter(1))
|
2012-11-08 15:49:20 -05:00
|
|
|
cases = [(Constant(v, self.sel.bv),
|
2012-11-09 11:37:42 -05:00
|
|
|
self.storage.eq(k)) for k, v in items]
|
2012-10-31 10:59:12 -04:00
|
|
|
sync = [Case(self.sel, *cases)]
|
|
|
|
return Fragment(sync=sync)
|
|
|
|
|
2012-11-09 13:37:52 -05:00
|
|
|
class _AbstractNextState:
|
|
|
|
def __init__(self, target_state):
|
|
|
|
self.target_state = target_state
|
|
|
|
|
2012-11-11 08:17:52 -05:00
|
|
|
# entry state is first state returned
|
|
|
|
class _StateAssembler:
|
|
|
|
def __init__(self):
|
|
|
|
self.states = []
|
|
|
|
self.exit_states = []
|
|
|
|
|
|
|
|
def assemble(self, n_states, n_exit_states):
|
|
|
|
self.states += n_states
|
|
|
|
for exit_state in self.exit_states:
|
|
|
|
exit_state.insert(0, _AbstractNextState(n_states[0]))
|
|
|
|
self.exit_states = n_exit_states
|
|
|
|
|
|
|
|
def ret(self):
|
|
|
|
return self.states, self.exit_states
|
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
class _Compiler:
|
2012-11-10 16:59:14 -05:00
|
|
|
def __init__(self, ioo, symdict, registers):
|
|
|
|
self.ioo = ioo
|
2012-11-10 06:09:45 -05:00
|
|
|
self.symdict = symdict
|
2012-10-31 10:59:12 -04:00
|
|
|
self.registers = registers
|
2012-11-06 07:52:19 -05:00
|
|
|
self.targetname = ""
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
def visit_top(self, node):
|
|
|
|
if isinstance(node, ast.Module) \
|
|
|
|
and len(node.body) == 1 \
|
|
|
|
and isinstance(node.body[0], ast.FunctionDef):
|
2012-11-09 14:12:15 -05:00
|
|
|
states, exit_states = self.visit_block(node.body[0].body)
|
|
|
|
return states
|
2012-11-06 07:52:19 -05:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
# blocks and statements
|
|
|
|
def visit_block(self, statements):
|
2012-11-11 08:17:52 -05:00
|
|
|
sa = _StateAssembler()
|
|
|
|
statements = iter(statements)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
statement = next(statements)
|
|
|
|
except StopIteration:
|
|
|
|
return sa.ret()
|
|
|
|
if isinstance(statement, ast.Assign):
|
|
|
|
self.visit_assign(sa, statement)
|
|
|
|
elif isinstance(statement, ast.If):
|
|
|
|
self.visit_if(sa, statement)
|
|
|
|
elif isinstance(statement, ast.While):
|
|
|
|
self.visit_while(sa, statement)
|
|
|
|
elif isinstance(statement, ast.For):
|
|
|
|
self.visit_for(sa, statement)
|
|
|
|
elif isinstance(statement, ast.Expr):
|
|
|
|
self.visit_expr_statement(sa, statement)
|
2012-10-31 10:59:12 -04:00
|
|
|
else:
|
2012-11-10 09:02:55 -05:00
|
|
|
raise NotImplementedError
|
2012-11-10 09:26:13 -05:00
|
|
|
|
2012-11-11 08:17:52 -05:00
|
|
|
def visit_assign(self, sa, node):
|
2012-11-06 07:52:19 -05:00
|
|
|
if isinstance(node.targets[0], ast.Name):
|
|
|
|
self.targetname = node.targets[0].id
|
|
|
|
value = self.visit_expr(node.value, True)
|
|
|
|
self.targetname = ""
|
|
|
|
|
|
|
|
if isinstance(value, _Register):
|
2012-10-31 10:59:12 -04:00
|
|
|
self.registers.append(value)
|
|
|
|
for target in node.targets:
|
|
|
|
if isinstance(target, ast.Name):
|
|
|
|
self.symdict[target.id] = value
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2012-11-06 07:52:19 -05:00
|
|
|
elif isinstance(value, Value):
|
|
|
|
r = []
|
|
|
|
for target in node.targets:
|
|
|
|
if isinstance(target, ast.Attribute) and target.attr == "store":
|
|
|
|
treg = target.value
|
|
|
|
if isinstance(treg, ast.Name):
|
|
|
|
r.append(self.symdict[treg.id].load(value))
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2012-11-11 08:17:52 -05:00
|
|
|
sa.assemble([r], [r])
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def visit_if(self, sa, node):
|
|
|
|
test = self.visit_expr(node.test)
|
|
|
|
states_t, exit_states_t = self.visit_block(node.body)
|
|
|
|
states_f, exit_states_f = self.visit_block(node.orelse)
|
|
|
|
exit_states = exit_states_t + exit_states_f
|
|
|
|
|
|
|
|
test_state_stmt = If(test, _AbstractNextState(states_t[0]))
|
|
|
|
test_state = [test_state_stmt]
|
|
|
|
if states_f:
|
|
|
|
test_state_stmt.Else(_AbstractNextState(states_f[0]))
|
|
|
|
else:
|
|
|
|
exit_states.append(test_state)
|
|
|
|
|
|
|
|
sa.assemble([test_state] + states_t + states_f,
|
|
|
|
exit_states)
|
|
|
|
|
|
|
|
def visit_while(self, sa, node):
|
|
|
|
test = self.visit_expr(node.test)
|
|
|
|
states_b, exit_states_b = self.visit_block(node.body)
|
|
|
|
|
|
|
|
test_state = [If(test, _AbstractNextState(states_b[0]))]
|
|
|
|
for exit_state in exit_states_b:
|
|
|
|
exit_state.insert(0, _AbstractNextState(test_state))
|
|
|
|
|
|
|
|
sa.assemble([test_state] + states_b, [test_state])
|
|
|
|
|
|
|
|
def visit_for(self, sa, node):
|
|
|
|
if not isinstance(node.target, ast.Name):
|
|
|
|
raise NotImplementedError
|
|
|
|
target = node.target.id
|
|
|
|
if target in self.symdict:
|
|
|
|
raise NotImplementedError("For loop target must use an available name")
|
|
|
|
it = self.visit_iterator(node.iter)
|
|
|
|
states = []
|
|
|
|
last_exit_states = []
|
|
|
|
for iteration in it:
|
|
|
|
self.symdict[target] = iteration
|
|
|
|
states_b, exit_states_b = self.visit_block(node.body)
|
|
|
|
for exit_state in last_exit_states:
|
|
|
|
exit_state.insert(0, _AbstractNextState(states_b[0]))
|
|
|
|
last_exit_states = exit_states_b
|
|
|
|
states += states_b
|
|
|
|
del self.symdict[target]
|
|
|
|
sa.assemble(states, last_exit_states)
|
|
|
|
|
|
|
|
def visit_iterator(self, node):
|
|
|
|
if isinstance(node, ast.List):
|
|
|
|
return ast.literal_eval(node)
|
|
|
|
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
|
|
|
funcname = node.func.id
|
|
|
|
args = map(ast.literal_eval, node.args)
|
|
|
|
if funcname == "range":
|
|
|
|
return range(*args)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def visit_expr_statement(self, sa, node):
|
|
|
|
if isinstance(node.value, ast.Yield):
|
|
|
|
yvalue = node.value.value
|
|
|
|
if not isinstance(yvalue, ast.Call) or not isinstance(yvalue.func, ast.Name):
|
|
|
|
raise NotImplementedError("Unrecognized I/O sequence")
|
|
|
|
callee = self.symdict[yvalue.func.id]
|
|
|
|
states, exit_states = gen_io(self, callee, yvalue.args, [])
|
|
|
|
sa.assemble(states, exit_states)
|
2012-11-06 07:52:19 -05:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
# expressions
|
2012-11-10 12:04:05 -05:00
|
|
|
def visit_expr(self, node, allow_registers=False):
|
2012-11-06 07:52:19 -05:00
|
|
|
if isinstance(node, ast.Call):
|
2012-11-10 12:04:05 -05:00
|
|
|
r = self.visit_expr_call(node)
|
|
|
|
if not allow_registers and isinstance(r, _Register):
|
2012-11-06 07:52:19 -05:00
|
|
|
raise NotImplementedError
|
2012-11-10 12:04:05 -05:00
|
|
|
return r
|
2012-11-06 07:52:19 -05:00
|
|
|
elif isinstance(node, ast.BinOp):
|
|
|
|
return self.visit_expr_binop(node)
|
2012-11-09 12:41:32 -05:00
|
|
|
elif isinstance(node, ast.Compare):
|
|
|
|
return self.visit_expr_compare(node)
|
2012-11-06 07:52:19 -05:00
|
|
|
elif isinstance(node, ast.Name):
|
|
|
|
return self.visit_expr_name(node)
|
|
|
|
elif isinstance(node, ast.Num):
|
|
|
|
return self.visit_expr_num(node)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def visit_expr_call(self, node):
|
2012-10-31 10:59:12 -04:00
|
|
|
if isinstance(node.func, ast.Name):
|
|
|
|
callee = self.symdict[node.func.id]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
if callee == transel.Register:
|
|
|
|
if len(node.args) != 1:
|
|
|
|
raise TypeError("Register() takes exactly 1 argument")
|
|
|
|
nbits = ast.literal_eval(node.args[0])
|
2012-11-06 07:52:19 -05:00
|
|
|
return _Register(self.targetname, nbits)
|
2012-11-10 12:04:05 -05:00
|
|
|
elif callee == transel.bitslice:
|
|
|
|
if len(node.args) != 2 and len(node.args) != 3:
|
|
|
|
raise TypeError("bitslice() takes 2 or 3 arguments")
|
|
|
|
val = self.visit_expr(node.args[0])
|
|
|
|
low = ast.literal_eval(node.args[1])
|
|
|
|
if len(node.args) == 3:
|
|
|
|
up = ast.literal_eval(node.args[2])
|
|
|
|
else:
|
|
|
|
up = low + 1
|
|
|
|
return _Slice(val, low, up)
|
2012-10-31 10:59:12 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
def visit_expr_binop(self, node):
|
|
|
|
left = self.visit_expr(node.left)
|
|
|
|
right = self.visit_expr(node.right)
|
|
|
|
if isinstance(node.op, ast.Add):
|
|
|
|
return left + right
|
|
|
|
elif isinstance(node.op, ast.Sub):
|
|
|
|
return left - right
|
|
|
|
elif isinstance(node.op, ast.Mult):
|
|
|
|
return left * right
|
|
|
|
elif isinstance(node.op, ast.LShift):
|
|
|
|
return left << right
|
|
|
|
elif isinstance(node.op, ast.RShift):
|
|
|
|
return left >> right
|
|
|
|
elif isinstance(node.op, ast.BitOr):
|
|
|
|
return left | right
|
|
|
|
elif isinstance(node.op, ast.BitXor):
|
|
|
|
return left ^ right
|
|
|
|
elif isinstance(node.op, ast.BitAnd):
|
|
|
|
return left & right
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-11-09 12:41:32 -05:00
|
|
|
def visit_expr_compare(self, node):
|
2012-11-09 13:37:52 -05:00
|
|
|
test = self.visit_expr(node.left)
|
2012-11-09 12:41:32 -05:00
|
|
|
r = None
|
|
|
|
for op, rcomparator in zip(node.ops, node.comparators):
|
2012-11-09 13:37:52 -05:00
|
|
|
comparator = self.visit_expr(rcomparator)
|
2012-11-09 12:41:32 -05:00
|
|
|
if isinstance(op, ast.Eq):
|
|
|
|
comparison = test == comparator
|
|
|
|
elif isinstance(op, ast.NotEq):
|
|
|
|
comparison = test != comparator
|
|
|
|
elif isinstance(op, ast.Lt):
|
|
|
|
comparison = test < comparator
|
|
|
|
elif isinstance(op, ast.LtE):
|
|
|
|
comparison = test <= comparator
|
|
|
|
elif isinstance(op, ast.Gt):
|
|
|
|
comparison = test > comparator
|
|
|
|
elif isinstance(op, ast.GtE):
|
|
|
|
comparison = test >= comparator
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
if r is None:
|
|
|
|
r = comparison
|
|
|
|
else:
|
|
|
|
r = r & comparison
|
|
|
|
test = comparator
|
|
|
|
return r
|
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
def visit_expr_name(self, node):
|
2012-11-10 08:37:33 -05:00
|
|
|
if node.id == "True":
|
|
|
|
return Constant(1)
|
|
|
|
if node.id == "False":
|
|
|
|
return Constant(0)
|
2012-11-06 07:52:19 -05:00
|
|
|
r = self.symdict[node.id]
|
|
|
|
if isinstance(r, _Register):
|
|
|
|
r = r.storage
|
2012-11-10 09:02:55 -05:00
|
|
|
if isinstance(r, int):
|
|
|
|
r = Constant(r)
|
2012-11-06 07:52:19 -05:00
|
|
|
return r
|
|
|
|
|
|
|
|
def visit_expr_num(self, node):
|
2012-11-09 14:17:57 -05:00
|
|
|
return Constant(node.n)
|
2012-11-06 07:52:19 -05:00
|
|
|
|
2012-11-09 13:37:52 -05:00
|
|
|
# like list.index, but using "is" instead of comparison
|
|
|
|
def _index_is(l, x):
|
|
|
|
for i, e in enumerate(l):
|
|
|
|
if e is x:
|
|
|
|
return i
|
|
|
|
|
|
|
|
class _LowerAbstractNextState(fhdl.NodeTransformer):
|
|
|
|
def __init__(self, fsm, states, stnames):
|
|
|
|
self.fsm = fsm
|
|
|
|
self.states = states
|
|
|
|
self.stnames = stnames
|
|
|
|
|
|
|
|
def visit_unknown(self, node):
|
|
|
|
if isinstance(node, _AbstractNextState):
|
|
|
|
index = _index_is(self.states, node.target_state)
|
|
|
|
estate = getattr(self.fsm, self.stnames[index])
|
|
|
|
return self.fsm.next_state(estate)
|
|
|
|
else:
|
|
|
|
return node
|
|
|
|
|
2012-11-09 11:37:42 -05:00
|
|
|
def _create_fsm(states):
|
|
|
|
stnames = ["S" + str(i) for i in range(len(states))]
|
|
|
|
fsm = FSM(*stnames)
|
2012-11-09 13:37:52 -05:00
|
|
|
lans = _LowerAbstractNextState(fsm, states, stnames)
|
2012-11-09 11:37:42 -05:00
|
|
|
for i, state in enumerate(states):
|
2012-11-09 13:37:52 -05:00
|
|
|
actions = lans.visit(state)
|
2012-11-09 11:37:42 -05:00
|
|
|
fsm.act(getattr(fsm, stnames[i]), *actions)
|
|
|
|
return fsm
|
|
|
|
|
2012-11-10 15:51:19 -05:00
|
|
|
def make_pytholite(func, **ioresources):
|
2012-11-10 16:59:14 -05:00
|
|
|
ioo = make_io_object(**ioresources)
|
2012-11-10 15:51:19 -05:00
|
|
|
|
2012-10-31 10:59:12 -04:00
|
|
|
tree = ast.parse(inspect.getsource(func))
|
|
|
|
symdict = func.__globals__.copy()
|
|
|
|
registers = []
|
|
|
|
|
2012-11-10 16:59:14 -05:00
|
|
|
states = _Compiler(ioo, symdict, registers).visit_top(tree)
|
2012-11-08 15:49:20 -05:00
|
|
|
|
|
|
|
regf = Fragment()
|
|
|
|
for register in registers:
|
|
|
|
register.finalize()
|
|
|
|
regf += register.get_fragment()
|
2012-11-09 11:37:42 -05:00
|
|
|
|
|
|
|
fsm = _create_fsm(states)
|
|
|
|
fsmf = _LowerAbstractLoad().visit(fsm.get_fragment())
|
|
|
|
|
2012-11-10 16:59:14 -05:00
|
|
|
ioo.fragment = regf + fsmf
|
|
|
|
return ioo
|