Skip to content

Commit

Permalink
Merge pull request #129 from jck/symbols
Browse files Browse the repository at this point in the history
always_seq, always_comb improvements
  • Loading branch information
jandecaluwe committed Oct 7, 2015
2 parents 0bc41cc + 5c9b9e1 commit ddcc815
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 63 deletions.
11 changes: 11 additions & 0 deletions myhdl/_always.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def __init__(self, func, senslist):
self.func = func
self.senslist = tuple(senslist)
super(_Always, self).__init__(self.genfunc)
varnames = func.__code__.co_varnames
symdict = {}
for n, v in func.__globals__.items():
if n not in varnames:
symdict[n] = v
# handle free variables
freevars = func.__code__.co_freevars
if freevars:
closure = (c.cell_contents for c in func.__closure__)
symdict.update(zip(freevars, closure))
self.symdict = symdict

def _waiter(self):
# infer appropriate waiter class
Expand Down
25 changes: 5 additions & 20 deletions myhdl/_always_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from myhdl import AlwaysCombError
from myhdl._Signal import _Signal, _isListOfSigs
from myhdl._util import _isGenFunc, _dedent
from myhdl._cell_deref import _cell_deref
from myhdl._Waiter import _Waiter, _SignalWaiter, _SignalTupleWaiter
from myhdl._instance import _Instantiator
from myhdl._always import _Always
Expand All @@ -52,20 +51,7 @@ def always_comb(func):
raise AlwaysCombError(_error.ArgType)
if func.__code__.co_argcount > 0:
raise AlwaysCombError(_error.NrOfArgs)
varnames = func.__code__.co_varnames
symdict = {}
for n, v in func.__globals__.items():
if n not in varnames:
symdict[n] = v
# handle free variables
if func.__code__.co_freevars:
for n, c in zip(func.__code__.co_freevars, func.__closure__):
try:
obj = _cell_deref(c)
symdict[n] = obj
except NameError:
raise NameError(n)
c = _AlwaysComb(func, symdict)
c = _AlwaysComb(func)
return c


Expand Down Expand Up @@ -101,8 +87,10 @@ class _AlwaysComb(_Always):
# W = _SignalTupleWaiter
# self.waiter = W(self.gen)

def __init__(self, func, symdict):
self.symdict = symdict
def __init__(self, func):
senslist = []
super(_AlwaysComb, self).__init__(func, senslist)

s = inspect.getsource(func)
s = _dedent(s)
tree = ast.parse(s)
Expand All @@ -121,7 +109,6 @@ def __init__(self, func, symdict):
if v.results['embedded_func']:
raise AlwaysCombError(_error.EmbeddedFunction)

senslist = []
for n in self.inputs:
s = self.symdict[n]
if isinstance(s, _Signal):
Expand All @@ -132,8 +119,6 @@ def __init__(self, func, symdict):
if len(self.senslist) == 0:
raise AlwaysCombError(_error.EmptySensitivityList)

super(_AlwaysComb, self).__init__(func, senslist)

def genfunc(self):
senslist = self.senslist
if len(senslist) == 1:
Expand Down
18 changes: 0 additions & 18 deletions myhdl/_always_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

from myhdl import AlwaysError, intbv
from myhdl._util import _isGenFunc, _dedent
from myhdl._cell_deref import _cell_deref
from myhdl._delay import delay
from myhdl._Signal import _Signal, _WaiterList,_isListOfSigs
from myhdl._Waiter import _Waiter, _EdgeWaiter, _EdgeTupleWaiter
Expand Down Expand Up @@ -102,23 +101,6 @@ def __init__(self, func, edge, reset):

super(_AlwaysSeq, self).__init__(func, senslist)

# find symdict
# similar to always_comb, but in class constructor
varnames = func.__code__.co_varnames
symdict = {}
for n, v in func.__globals__.items():
if n not in varnames:
symdict[n] = v
# handle free variables
if func.__code__.co_freevars:
for n, c in zip(func.__code__.co_freevars, func.__closure__):
try:
obj = _cell_deref(c)
symdict[n] = obj
except NameError:
raise NameError(n)
self.symdict = symdict

# now infer outputs to be reset
s = inspect.getsource(func)
s = _dedent(s)
Expand Down
22 changes: 0 additions & 22 deletions myhdl/_cell_deref.py

This file was deleted.

5 changes: 2 additions & 3 deletions myhdl/conversion/_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import myhdl
from myhdl import *
from myhdl import ConversionError
from myhdl._cell_deref import _cell_deref
from myhdl._always_comb import _AlwaysComb
from myhdl._always_seq import _AlwaysSeq
from myhdl._always import _Always
Expand Down Expand Up @@ -149,7 +148,7 @@ def _analyzeGens(top, absnames):
tree.nonlocaldict = {}
if f.__code__.co_freevars:
for n, c in zip(f.__code__.co_freevars, f.__closure__):
obj = _cell_deref(c)
obj = c.cell_contents
tree.symdict[n] = obj
# currently, only intbv as automatic nonlocals (until Python 3.0)
if isinstance(obj, intbv):
Expand Down Expand Up @@ -592,7 +591,7 @@ def visit_Call(self, node):
# handle free variables
if f.__code__.co_freevars:
for n, c in zip(f.__code__.co_freevars, f.__closure__):
obj = _cell_deref(c)
obj = c.cell_contents
if not isinstance(obj, (integer_types, _Signal)):
self.raiseError(node, _error.FreeVarTypeError, n)
tree.symdict[n] = obj
Expand Down

0 comments on commit ddcc815

Please sign in to comment.