2012-10-31 10:59:12 -04:00
|
|
|
import inspect
|
|
|
|
import ast
|
2013-07-03 10:35:07 -04:00
|
|
|
from collections import OrderedDict
|
2012-10-31 10:59:12 -04:00
|
|
|
|
|
|
|
from migen.fhdl.structure import *
|
2013-04-10 17:42:46 -04:00
|
|
|
from migen.fhdl.visit import TransformModule
|
|
|
|
from migen.fhdl.specials import Memory
|
|
|
|
from migen.genlib.ioo import UnifiedIOObject
|
2012-11-11 17:48:23 -05:00
|
|
|
from migen.pytholite.reg import *
|
|
|
|
from migen.pytholite.expr import *
|
2012-10-31 10:59:12 -04:00
|
|
|
from migen.pytholite import transel
|
2013-04-10 17:42:46 -04:00
|
|
|
from migen.pytholite.io import gen_io
|
2013-06-28 13:03:55 -04:00
|
|
|
from migen.pytholite.util import *
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-11 12:03:16 -05:00
|
|
|
def _is_name_used(node, name):
|
|
|
|
for n in ast.walk(node):
|
|
|
|
if isinstance(n, ast.Name) and n.id == name:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-07-03 10:35:07 -04:00
|
|
|
def _make_function_args_dict(undefined, symdict, args, defaults):
|
|
|
|
d = OrderedDict()
|
|
|
|
for argument in args:
|
|
|
|
d[argument.arg] = undefined
|
|
|
|
for default, argname in zip(defaults, reversed(list(d.keys()))):
|
|
|
|
default_val = eval_ast(default, symdict)
|
|
|
|
d[argname] = default_val
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _process_function_args(symdict, function_def, args, kwargs):
|
|
|
|
defargs = function_def.args
|
|
|
|
undefined = object()
|
|
|
|
|
|
|
|
ad_positional = _make_function_args_dict(undefined, symdict, defargs.args, defargs.defaults)
|
|
|
|
vararg_name = defargs.vararg
|
|
|
|
kwarg_name = defargs.kwarg
|
|
|
|
ad_kwonly = _make_function_args_dict(undefined, symdict, defargs.kwonlyargs, defargs.kw_defaults)
|
|
|
|
|
|
|
|
# grab argument values
|
|
|
|
current_argvalue = iter(args)
|
|
|
|
try:
|
|
|
|
for argname in ad_positional.keys():
|
|
|
|
ad_positional[argname] = next(current_argvalue)
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
|
|
|
vararg = tuple(current_argvalue)
|
|
|
|
|
2013-07-03 11:20:05 -04:00
|
|
|
kwarg = dict()
|
|
|
|
for k, v in kwargs.items():
|
2013-07-03 10:35:07 -04:00
|
|
|
if k in ad_positional:
|
|
|
|
ad_positional[k] = v
|
|
|
|
elif k in ad_kwonly:
|
|
|
|
ad_kwonly[k] = v
|
|
|
|
else:
|
|
|
|
kwarg[k] = v
|
|
|
|
|
|
|
|
# check
|
|
|
|
undefined_pos = [k for k, v in ad_positional.items() if v is undefined]
|
|
|
|
if undefined_pos:
|
|
|
|
formatted = " and ".join("'" + k + "'" for k in undefined_pos)
|
|
|
|
raise TypeError("Missing required positional arguments: " + formatted)
|
|
|
|
if vararg and vararg_name is None:
|
|
|
|
raise TypeError("Function takes {} positional arguments but {} were given".format(len(ad_positional),
|
|
|
|
len(ad_positional) + len(vararg)))
|
|
|
|
ad_kwonly = [k for k, v in ad_positional.items() if v is undefined]
|
|
|
|
if undefined_pos:
|
|
|
|
formatted = " and ".join("'" + k + "'" for k in undefined_pos)
|
|
|
|
raise TypeError("Missing required keyword-only arguments: " + formatted)
|
|
|
|
if kwarg and kwarg_name is None:
|
|
|
|
formatted = " and ".join("'" + k + "'" for k in kwarg.keys())
|
|
|
|
raise TypeError("Got unexpected keyword arguments: " + formatted)
|
|
|
|
|
|
|
|
# update symdict
|
|
|
|
symdict.update(ad_positional)
|
|
|
|
if vararg_name is not None:
|
|
|
|
symdict[vararg_name] = vararg
|
|
|
|
symdict.update(ad_kwonly)
|
|
|
|
if kwarg_name is not None:
|
|
|
|
symdict[kwarg_name] = kwarg
|
|
|
|
|
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-11 17:48:23 -05:00
|
|
|
self.ec = ExprCompiler(self.symdict)
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2013-07-03 10:35:07 -04:00
|
|
|
def visit_top(self, node, args, kwargs):
|
2012-11-06 07:52:19 -05:00
|
|
|
if isinstance(node, ast.Module) \
|
|
|
|
and len(node.body) == 1 \
|
|
|
|
and isinstance(node.body[0], ast.FunctionDef):
|
2013-07-03 10:35:07 -04:00
|
|
|
function_def = node.body[0]
|
|
|
|
_process_function_args(self.symdict, function_def, args, kwargs)
|
|
|
|
states, exit_states = self.visit_block(function_def.body)
|
2012-11-09 14:12:15 -05:00
|
|
|
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:30:25 -05:00
|
|
|
sa = StateAssembler()
|
2012-11-11 08:17:52 -05:00
|
|
|
statements = iter(statements)
|
2012-11-11 09:52:06 -05:00
|
|
|
statement = None
|
2012-11-11 08:17:52 -05:00
|
|
|
while True:
|
2012-11-11 09:52:06 -05:00
|
|
|
if statement is None:
|
|
|
|
try:
|
|
|
|
statement = next(statements)
|
|
|
|
except StopIteration:
|
|
|
|
return sa.ret()
|
2012-11-11 08:17:52 -05:00
|
|
|
if isinstance(statement, ast.Assign):
|
2012-11-11 09:52:06 -05:00
|
|
|
# visit_assign can recognize a I/O pattern, consume several
|
|
|
|
# statements from the iterator and return the first statement
|
|
|
|
# that is not part of the I/O pattern anymore.
|
|
|
|
statement = self.visit_assign(sa, statement, statements)
|
2012-10-31 10:59:12 -04:00
|
|
|
else:
|
2012-11-11 09:52:06 -05:00
|
|
|
if 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:
|
|
|
|
raise NotImplementedError
|
2012-11-11 09:52:06 -05:00
|
|
|
statement = None
|
|
|
|
|
|
|
|
def visit_assign(self, sa, node, statements):
|
|
|
|
if isinstance(node.value, ast.Call):
|
2012-11-11 17:48:23 -05:00
|
|
|
is_special = False
|
2012-11-11 09:52:06 -05:00
|
|
|
try:
|
2012-11-11 17:48:23 -05:00
|
|
|
value = self.ec.visit_expr_call(node.value)
|
2012-11-11 09:52:06 -05:00
|
|
|
except NotImplementedError:
|
2012-11-11 17:48:23 -05:00
|
|
|
is_special = True
|
|
|
|
if is_special:
|
2012-11-11 09:52:06 -05:00
|
|
|
return self.visit_assign_special(sa, node, statements)
|
|
|
|
else:
|
2012-11-11 17:48:23 -05:00
|
|
|
value = self.ec.visit_expr(node.value)
|
2012-12-19 10:21:57 -05:00
|
|
|
if isinstance(value, (int, bool, Value)):
|
2012-11-06 07:52:19 -05:00
|
|
|
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
|
2012-11-11 09:52:06 -05:00
|
|
|
|
|
|
|
def visit_assign_special(self, sa, node, statements):
|
|
|
|
value = node.value
|
|
|
|
assert(isinstance(value, ast.Call))
|
|
|
|
if isinstance(value.func, ast.Name):
|
|
|
|
callee = self.symdict[value.func.id]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
if callee == transel.Register:
|
|
|
|
if len(value.args) != 1:
|
|
|
|
raise TypeError("Register() takes exactly 1 argument")
|
2013-06-28 13:03:55 -04:00
|
|
|
bits_sign = eval_ast(value.args[0], self.symdict)
|
2012-11-11 09:52:06 -05:00
|
|
|
if isinstance(node.targets[0], ast.Name):
|
|
|
|
targetname = node.targets[0].id
|
|
|
|
else:
|
|
|
|
targetname = "unk"
|
2012-11-30 11:07:12 -05:00
|
|
|
reg = ImplRegister(targetname, bits_sign)
|
2012-11-11 09:52:06 -05:00
|
|
|
self.registers.append(reg)
|
|
|
|
for target in node.targets:
|
|
|
|
if isinstance(target, ast.Name):
|
|
|
|
self.symdict[target.id] = reg
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
2012-11-23 06:40:57 -05:00
|
|
|
return self.visit_io_pattern(sa, node.targets, callee, value.args, value.keywords, statements)
|
2012-11-11 12:03:16 -05:00
|
|
|
|
2012-11-23 06:40:57 -05:00
|
|
|
def visit_io_pattern(self, sa, targets, model, args, keywords, statements):
|
2012-11-11 12:03:16 -05:00
|
|
|
# first statement is <modelname> = <model>(<args>)
|
|
|
|
if len(targets) != 1 or not isinstance(targets[0], ast.Name):
|
|
|
|
raise NotImplementedError("Unrecognized I/O pattern")
|
|
|
|
modelname = targets[0].id
|
|
|
|
if modelname in self.symdict:
|
|
|
|
raise NotImplementedError("I/O model name is not free")
|
|
|
|
|
|
|
|
# second statement must be yield <modelname>
|
|
|
|
try:
|
|
|
|
ystatement = next(statements)
|
|
|
|
except StopIteration:
|
|
|
|
raise NotImplementedError("Incomplete or fragmented I/O pattern")
|
|
|
|
if not isinstance(ystatement, ast.Expr) \
|
|
|
|
or not isinstance(ystatement.value, ast.Yield) \
|
|
|
|
or not isinstance(ystatement.value.value, ast.Name) \
|
|
|
|
or ystatement.value.value.id != modelname:
|
2012-11-11 17:48:23 -05:00
|
|
|
print(ast.dump(ystatement))
|
2012-11-11 12:03:16 -05:00
|
|
|
raise NotImplementedError("Unrecognized I/O pattern")
|
|
|
|
|
|
|
|
# following optional statements are assignments to registers
|
|
|
|
# with <modelname> used in expressions.
|
|
|
|
from_model = []
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
fstatement = next(statements)
|
|
|
|
except StopIteration:
|
|
|
|
fstatement = None
|
|
|
|
if not isinstance(fstatement, ast.Assign) \
|
|
|
|
or not _is_name_used(fstatement.value, modelname):
|
|
|
|
break
|
|
|
|
tregs = []
|
|
|
|
for target in fstatement.targets:
|
|
|
|
if isinstance(target, ast.Attribute) and target.attr == "store":
|
|
|
|
if isinstance(target.value, ast.Name):
|
|
|
|
tregs.append(self.symdict[target.value.id])
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
from_model.append((tregs, fstatement.value))
|
|
|
|
|
2012-11-23 06:40:57 -05:00
|
|
|
states, exit_states = gen_io(self, modelname, model, args, keywords, from_model)
|
2012-11-11 12:03:16 -05:00
|
|
|
sa.assemble(states, exit_states)
|
|
|
|
return fstatement
|
2012-11-11 09:52:06 -05:00
|
|
|
|
2012-11-11 08:17:52 -05:00
|
|
|
def visit_if(self, sa, node):
|
2012-11-11 17:48:23 -05:00
|
|
|
test = self.ec.visit_expr(node.test)
|
2012-11-11 08:17:52 -05:00
|
|
|
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
|
|
|
|
|
2013-06-25 16:17:39 -04:00
|
|
|
test_state_stmt = If(test, id_next_state(states_t[0]))
|
2012-11-11 08:17:52 -05:00
|
|
|
test_state = [test_state_stmt]
|
|
|
|
if states_f:
|
2013-06-25 16:17:39 -04:00
|
|
|
test_state_stmt.Else(id_next_state(states_f[0]))
|
2012-11-11 08:17:52 -05:00
|
|
|
else:
|
|
|
|
exit_states.append(test_state)
|
|
|
|
|
|
|
|
sa.assemble([test_state] + states_t + states_f,
|
|
|
|
exit_states)
|
|
|
|
|
|
|
|
def visit_while(self, sa, node):
|
2012-11-11 17:48:23 -05:00
|
|
|
test = self.ec.visit_expr(node.test)
|
2012-11-11 08:17:52 -05:00
|
|
|
states_b, exit_states_b = self.visit_block(node.body)
|
|
|
|
|
2013-06-25 16:17:39 -04:00
|
|
|
test_state = [If(test, id_next_state(states_b[0]))]
|
2012-11-11 08:17:52 -05:00
|
|
|
for exit_state in exit_states_b:
|
2013-06-25 16:17:39 -04:00
|
|
|
exit_state.insert(0, id_next_state(test_state))
|
2012-11-11 08:17:52 -05:00
|
|
|
|
|
|
|
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:
|
2013-06-25 16:17:39 -04:00
|
|
|
exit_state.insert(0, id_next_state(states_b[0]))
|
2012-11-11 08:17:52 -05:00
|
|
|
last_exit_states = exit_states_b
|
|
|
|
states += states_b
|
|
|
|
del self.symdict[target]
|
|
|
|
sa.assemble(states, last_exit_states)
|
|
|
|
|
|
|
|
def visit_iterator(self, node):
|
2013-06-28 13:03:55 -04:00
|
|
|
return eval_ast(node, self.symdict)
|
|
|
|
|
2012-11-11 08:17:52 -05:00
|
|
|
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):
|
2012-11-11 12:03:16 -05:00
|
|
|
raise NotImplementedError("Unrecognized I/O pattern")
|
2012-11-11 08:17:52 -05:00
|
|
|
callee = self.symdict[yvalue.func.id]
|
2012-11-23 06:40:57 -05:00
|
|
|
states, exit_states = gen_io(self, None, callee, yvalue.args, yvalue.keywords, [])
|
2012-11-11 08:17:52 -05:00
|
|
|
sa.assemble(states, exit_states)
|
2012-11-06 07:52:19 -05:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-04-10 17:42:46 -04:00
|
|
|
class Pytholite(UnifiedIOObject):
|
2013-07-03 10:35:07 -04:00
|
|
|
def __init__(self, func, *args, **kwargs):
|
2013-04-10 17:42:46 -04:00
|
|
|
self.func = func
|
2013-07-03 10:35:07 -04:00
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
2013-04-10 17:42:46 -04:00
|
|
|
|
|
|
|
def do_finalize(self):
|
|
|
|
UnifiedIOObject.do_finalize(self)
|
|
|
|
if self.get_dataflow():
|
|
|
|
self.busy.reset = 1
|
2013-05-28 10:11:34 -04:00
|
|
|
self.memory_ports = dict()
|
|
|
|
for mem in self.__dict__.values():
|
|
|
|
if isinstance(mem, Memory):
|
|
|
|
port = mem.get_port(write_capable=True, we_granularity=8)
|
|
|
|
self.specials += port
|
|
|
|
self.memory_ports[mem] = port
|
2013-04-10 17:42:46 -04:00
|
|
|
self._compile()
|
|
|
|
|
|
|
|
def _compile(self):
|
|
|
|
tree = ast.parse(inspect.getsource(self.func))
|
|
|
|
symdict = self.func.__globals__.copy()
|
|
|
|
registers = []
|
|
|
|
|
2013-07-03 10:35:07 -04:00
|
|
|
states = _Compiler(self, symdict, registers).visit_top(tree, self.args, self.kwargs)
|
2013-04-10 17:42:46 -04:00
|
|
|
|
|
|
|
for register in registers:
|
|
|
|
if register.source_encoding:
|
|
|
|
register.finalize()
|
|
|
|
self.submodules += register
|
|
|
|
|
|
|
|
fsm = implement_fsm(states)
|
|
|
|
self.submodules += TransformModule(LowerAbstractLoad().visit, fsm)
|