2012-10-31 10:59:12 -04:00
|
|
|
import inspect
|
|
|
|
import ast
|
|
|
|
|
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.pytholite import transel
|
|
|
|
|
|
|
|
class FinalizeError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class _AbstractLoad:
|
|
|
|
def __init__(self, target, source):
|
|
|
|
self.target = target
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
class _Register:
|
|
|
|
def __init__(self, name, nbits):
|
|
|
|
self.storage = Signal(BV(nbits), name=name)
|
|
|
|
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
|
|
|
|
self.sel = Signal(BV(bits_for(len(self.source_encoding) + 1)))
|
|
|
|
self.finalized = True
|
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
if not self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
# do nothing when sel == 0
|
2012-11-08 15:49:20 -05:00
|
|
|
cases = [(Constant(v, self.sel.bv),
|
|
|
|
self.storage.eq(k)) for k, v in self.source_encoding.items()]
|
2012-10-31 10:59:12 -04:00
|
|
|
sync = [Case(self.sel, *cases)]
|
|
|
|
return Fragment(sync=sync)
|
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
class _Compiler:
|
2012-10-31 10:59:12 -04:00
|
|
|
def __init__(self, symdict, registers):
|
|
|
|
self.symdict = symdict
|
|
|
|
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):
|
|
|
|
return self.visit_block(node.body[0].body)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
# blocks and statements
|
|
|
|
def visit_block(self, statements):
|
2012-11-08 15:49:20 -05:00
|
|
|
states = []
|
2012-11-06 07:52:19 -05:00
|
|
|
for statement in statements:
|
|
|
|
if isinstance(statement, ast.Assign):
|
2012-11-08 15:49:20 -05:00
|
|
|
op = self.visit_assign(statement)
|
|
|
|
if op:
|
|
|
|
states.append(op)
|
2012-10-31 10:59:12 -04:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2012-11-08 15:49:20 -05:00
|
|
|
return states
|
2012-11-06 07:52:19 -05:00
|
|
|
|
|
|
|
def visit_assign(self, node):
|
|
|
|
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
|
|
|
return []
|
|
|
|
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
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
# expressions
|
|
|
|
def visit_expr(self, node, allow_call=False):
|
|
|
|
if isinstance(node, ast.Call):
|
|
|
|
if allow_call:
|
|
|
|
return self.visit_expr_call(node)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
elif isinstance(node, ast.BinOp):
|
|
|
|
return self.visit_expr_binop(node)
|
|
|
|
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-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
|
|
|
|
|
|
|
|
def visit_expr_name(self, node):
|
|
|
|
r = self.symdict[node.id]
|
|
|
|
if isinstance(r, _Register):
|
|
|
|
r = r.storage
|
|
|
|
return r
|
|
|
|
|
|
|
|
def visit_expr_num(self, node):
|
|
|
|
return node.n
|
|
|
|
|
2012-10-31 10:59:12 -04:00
|
|
|
def make_pytholite(func):
|
|
|
|
tree = ast.parse(inspect.getsource(func))
|
|
|
|
symdict = func.__globals__.copy()
|
|
|
|
registers = []
|
|
|
|
|
2012-11-08 15:49:20 -05:00
|
|
|
states = _Compiler(symdict, registers).visit_top(tree)
|
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
print("compilation result:")
|
2012-11-08 15:49:20 -05:00
|
|
|
print(states)
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
print("registers:")
|
2012-10-31 10:59:12 -04:00
|
|
|
print(registers)
|
2012-11-06 07:52:19 -05:00
|
|
|
#print("symdict:")
|
|
|
|
#print(symdict)
|
2012-10-31 10:59:12 -04:00
|
|
|
|
2012-11-06 07:52:19 -05:00
|
|
|
print("ast:")
|
|
|
|
print(ast.dump(tree))
|
2012-11-08 15:49:20 -05:00
|
|
|
|
|
|
|
regf = Fragment()
|
|
|
|
for register in registers:
|
|
|
|
register.finalize()
|
|
|
|
regf += register.get_fragment()
|
|
|
|
return regf
|