Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
10  examples/pytholite/basic.py
@@ -7,14 +7,14 @@
7 7
 
8 8
 layout = [("r", 32)]
9 9
 
10  
-def number_gen():
11  
-	for i in range(10):
  10
+def number_gen(n):
  11
+	for i in range(n):
12 12
 		yield Token("result", {"r": i})
13 13
 
14 14
 class SimNumberGen(SimActor):
15 15
 	def __init__(self):
16 16
 		self.result = Source(layout)
17  
-		SimActor.__init__(self, number_gen())
  17
+		SimActor.__init__(self, number_gen(5))
18 18
 
19 19
 def run_sim(ng):
20 20
 	g = DataFlowGraph()
@@ -23,11 +23,11 @@ def run_sim(ng):
23 23
 	
24 24
 	c = CompositeActor(g)
25 25
 	sim = Simulator(c)
26  
-	sim.run(30)
  26
+	sim.run(20)
27 27
 	del sim
28 28
 
29 29
 def make_ng_pytholite():
30  
-	ng_pytholite = Pytholite(number_gen)
  30
+	ng_pytholite = Pytholite(number_gen, 5)
31 31
 	ng_pytholite.result = Source(layout)
32 32
 	ng_pytholite.finalize()
33 33
 	return ng_pytholite
2  migen/fhdl/structure.py
@@ -136,7 +136,7 @@ def __init__(self, bits_sign=None, name=None, variable=False, reset=0, name_over
136 136
 				self.nbits, self.signed = bits_sign, False
137 137
 		assert(isinstance(self.nbits, int))
138 138
 		
139  
-		self.variable = variable
  139
+		self.variable = variable # deprecated
140 140
 		self.reset = reset
141 141
 		self.name_override = name_override
142 142
 		self.backtrace = tracer.trace_back(name)
1  migen/fhdl/tools.py
@@ -148,6 +148,7 @@ def __init__(self, clock_domains):
148 148
 		_Lowerer.__init__(self)
149 149
 
150 150
 	def visit_ArrayProxy(self, node):
  151
+		# TODO: rewrite without variables
151 152
 		array_muxed = Signal(value_bits_sign(node), variable=True)
152 153
 		if self.target_context:
153 154
 			k = self.visit(node.key)
73  migen/pytholite/compiler.py
... ...
@@ -1,5 +1,6 @@
1 1
 import inspect
2 2
 import ast
  3
+from collections import OrderedDict
3 4
 
4 5
 from migen.fhdl.structure import *
5 6
 from migen.fhdl.visit import TransformModule
@@ -17,6 +18,66 @@ def _is_name_used(node, name):
17 18
 			return True
18 19
 	return False
19 20
 
  21
+def _make_function_args_dict(undefined, symdict, args, defaults):
  22
+	d = OrderedDict()
  23
+	for argument in args:
  24
+		d[argument.arg] = undefined
  25
+	for default, argname in zip(defaults, reversed(list(d.keys()))):
  26
+		default_val = eval_ast(default, symdict)
  27
+		d[argname] = default_val
  28
+	return d
  29
+
  30
+def _process_function_args(symdict, function_def, args, kwargs):
  31
+	defargs = function_def.args
  32
+	undefined = object()
  33
+
  34
+	ad_positional = _make_function_args_dict(undefined, symdict, defargs.args, defargs.defaults)
  35
+	vararg_name = defargs.vararg
  36
+	kwarg_name = defargs.kwarg
  37
+	ad_kwonly = _make_function_args_dict(undefined, symdict, defargs.kwonlyargs, defargs.kw_defaults)
  38
+
  39
+	# grab argument values
  40
+	current_argvalue = iter(args)
  41
+	try:
  42
+		for argname in ad_positional.keys():
  43
+			ad_positional[argname] = next(current_argvalue)
  44
+	except StopIteration:
  45
+		pass
  46
+	vararg = tuple(current_argvalue)
  47
+
  48
+	kwarg = OrderedDict()
  49
+	for k, v in kwarg.items():
  50
+		if k in ad_positional:
  51
+			ad_positional[k] = v
  52
+		elif k in ad_kwonly:
  53
+			ad_kwonly[k] = v
  54
+		else:
  55
+			kwarg[k] = v
  56
+
  57
+	# check
  58
+	undefined_pos = [k for k, v in ad_positional.items() if v is undefined]
  59
+	if undefined_pos:
  60
+		formatted = " and ".join("'" + k + "'" for k in undefined_pos)
  61
+		raise TypeError("Missing required positional arguments: " + formatted)
  62
+	if vararg and vararg_name is None:
  63
+		raise TypeError("Function takes {} positional arguments but {} were given".format(len(ad_positional),
  64
+			len(ad_positional) + len(vararg)))
  65
+	ad_kwonly = [k for k, v in ad_positional.items() if v is undefined]
  66
+	if undefined_pos:
  67
+		formatted = " and ".join("'" + k + "'" for k in undefined_pos)
  68
+		raise TypeError("Missing required keyword-only arguments: " + formatted)
  69
+	if kwarg and kwarg_name is None:
  70
+		formatted = " and ".join("'" + k + "'" for k in kwarg.keys())
  71
+		raise TypeError("Got unexpected keyword arguments: " + formatted)
  72
+
  73
+	# update symdict
  74
+	symdict.update(ad_positional)
  75
+	if vararg_name is not None:
  76
+		symdict[vararg_name] = vararg
  77
+	symdict.update(ad_kwonly)
  78
+	if kwarg_name is not None:
  79
+		symdict[kwarg_name] = kwarg
  80
+
20 81
 class _Compiler:
21 82
 	def __init__(self, ioo, symdict, registers):
22 83
 		self.ioo = ioo
@@ -24,11 +85,13 @@ def __init__(self, ioo, symdict, registers):
24 85
 		self.registers = registers
25 86
 		self.ec = ExprCompiler(self.symdict)
26 87
 	
27  
-	def visit_top(self, node):
  88
+	def visit_top(self, node, args, kwargs):
28 89
 		if isinstance(node, ast.Module) \
29 90
 		  and len(node.body) == 1 \
30 91
 		  and isinstance(node.body[0], ast.FunctionDef):
31  
-			states, exit_states = self.visit_block(node.body[0].body)
  92
+			function_def = node.body[0]
  93
+			_process_function_args(self.symdict, function_def, args, kwargs)
  94
+			states, exit_states = self.visit_block(function_def.body)
32 95
 			return states
33 96
 		else:
34 97
 			raise NotImplementedError
@@ -220,8 +283,10 @@ def visit_expr_statement(self, sa, node):
220 283
 			raise NotImplementedError
221 284
 
222 285
 class Pytholite(UnifiedIOObject):
223  
-	def __init__(self, func):
  286
+	def __init__(self, func, *args, **kwargs):
224 287
 		self.func = func
  288
+		self.args = args
  289
+		self.kwargs = kwargs
225 290
 
226 291
 	def do_finalize(self):
227 292
 		UnifiedIOObject.do_finalize(self)
@@ -240,7 +305,7 @@ def _compile(self):
240 305
 		symdict = self.func.__globals__.copy()
241 306
 		registers = []
242 307
 		
243  
-		states = _Compiler(self, symdict, registers).visit_top(tree)
  308
+		states = _Compiler(self, symdict, registers).visit_top(tree, self.args, self.kwargs)
244 309
 		
245 310
 		for register in registers:
246 311
 			if register.source_encoding:

No commit comments for this range

Something went wrong with that request. Please try again.