1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00

Forgot to add these files

--HG--
branch : mep107
This commit is contained in:
Keerthan Jaic 2013-07-04 23:09:10 -04:00
parent 303fa28ed4
commit 7d899b8e6d
2 changed files with 68 additions and 0 deletions

11
myhdl/_convutils.py Normal file
View File

@ -0,0 +1,11 @@
import inspect
import ast
from myhdl._util import _dedent
def _makeAST(f):
s = inspect.getsource(f)
s = _dedent(s)
tree = ast.parse(s)
tree.sourcefile = inspect.getsourcefile(f)
tree.lineoffset = inspect.getsourcelines(f)[1]-1
return tree

57
myhdl/_resolverefs.py Normal file
View File

@ -0,0 +1,57 @@
import ast
from myhdl._convutils import _makeAST
from myhdl._util import _flatten
class Data():
pass
def _resolveRefs(symdict, arg):
#temporary hack to prevent circular dependencies
from myhdl._always_comb import _AlwaysComb
from myhdl._always_seq import _AlwaysSeq
from myhdl._always import _Always
gens = _flatten(arg)
data = Data()
data.symdict = symdict
v = _AttrRefTransformer(data)
for gen in gens:
if isinstance(gen, (_AlwaysComb, _AlwaysSeq, _Always)):
func = gen.func
else:
func = gen.gen.gi_frame
tree = _makeAST(func)
v.visit(tree)
return data.objlist
class _AttrRefTransformer(ast.NodeTransformer):
def __init__(self, data):
self.data = data
self.data.objlist = []
def visit_Attribute(self, node):
reserved = ('next', 'posedge', 'negedge', 'max', 'min', 'val', 'signed')
self.generic_visit(node)
if node.attr in reserved:
return node
else:
obj = self.data.symdict[node.value.id]
attrobj = getattr(obj, node.attr)
new_name = node.value.id+'.'+node.attr
if new_name not in self.data.symdict:
self.data.symdict[new_name] = attrobj
self.data.objlist.append(new_name)
else:
pass
#assert self.data.symdict[new_name] == attrobj
new_node = ast.Name(id=new_name, ctx=node.value.ctx)
return ast.copy_location(new_node, node)
def visit_FunctionDef(self, node):
nodes = _flatten(node.body, node.args)
for n in nodes:
self.visit(n)
self.visit(node.args)
return node