Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
10 examples/pytholite/basic.py
@@ -7,14 +7,14 @@
layout = [("r", 32)]
-def number_gen():
- for i in range(10):
+def number_gen(n):
+ for i in range(n):
yield Token("result", {"r": i})
class SimNumberGen(SimActor):
def __init__(self):
self.result = Source(layout)
- SimActor.__init__(self, number_gen())
+ SimActor.__init__(self, number_gen(5))
def run_sim(ng):
g = DataFlowGraph()
@@ -23,11 +23,11 @@ def run_sim(ng):
c = CompositeActor(g)
sim = Simulator(c)
- sim.run(30)
+ sim.run(20)
del sim
def make_ng_pytholite():
- ng_pytholite = Pytholite(number_gen)
+ ng_pytholite = Pytholite(number_gen, 5)
ng_pytholite.result = Source(layout)
ng_pytholite.finalize()
return ng_pytholite
View
2  migen/fhdl/structure.py
@@ -136,7 +136,7 @@ def __init__(self, bits_sign=None, name=None, variable=False, reset=0, name_over
self.nbits, self.signed = bits_sign, False
assert(isinstance(self.nbits, int))
- self.variable = variable
+ self.variable = variable # deprecated
self.reset = reset
self.name_override = name_override
self.backtrace = tracer.trace_back(name)
View
1  migen/fhdl/tools.py
@@ -148,6 +148,7 @@ def __init__(self, clock_domains):
_Lowerer.__init__(self)
def visit_ArrayProxy(self, node):
+ # TODO: rewrite without variables
array_muxed = Signal(value_bits_sign(node), variable=True)
if self.target_context:
k = self.visit(node.key)
View
73 migen/pytholite/compiler.py
@@ -1,5 +1,6 @@
import inspect
import ast
+from collections import OrderedDict
from migen.fhdl.structure import *
from migen.fhdl.visit import TransformModule
@@ -17,6 +18,66 @@ def _is_name_used(node, name):
return True
return False
+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)
+
+ kwarg = OrderedDict()
+ for k, v in kwarg.items():
+ 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
+
class _Compiler:
def __init__(self, ioo, symdict, registers):
self.ioo = ioo
@@ -24,11 +85,13 @@ def __init__(self, ioo, symdict, registers):
self.registers = registers
self.ec = ExprCompiler(self.symdict)
- def visit_top(self, node):
+ def visit_top(self, node, args, kwargs):
if isinstance(node, ast.Module) \
and len(node.body) == 1 \
and isinstance(node.body[0], ast.FunctionDef):
- states, exit_states = self.visit_block(node.body[0].body)
+ function_def = node.body[0]
+ _process_function_args(self.symdict, function_def, args, kwargs)
+ states, exit_states = self.visit_block(function_def.body)
return states
else:
raise NotImplementedError
@@ -220,8 +283,10 @@ def visit_expr_statement(self, sa, node):
raise NotImplementedError
class Pytholite(UnifiedIOObject):
- def __init__(self, func):
+ def __init__(self, func, *args, **kwargs):
self.func = func
+ self.args = args
+ self.kwargs = kwargs
def do_finalize(self):
UnifiedIOObject.do_finalize(self)
@@ -240,7 +305,7 @@ def _compile(self):
symdict = self.func.__globals__.copy()
registers = []
- states = _Compiler(self, symdict, registers).visit_top(tree)
+ states = _Compiler(self, symdict, registers).visit_top(tree, self.args, self.kwargs)
for register in registers:
if register.source_encoding:

No commit comments for this range

Something went wrong with that request. Please try again.