def _analyzeGens(top, absnames): genlist = [] for g in top: if isinstance(g, _UserCode): tree = g elif isinstance(g, (_AlwaysComb, _AlwaysSeq, _Always)): f = g.func s = inspect.getsource(f) s = _dedent(s) tree = ast.parse(s) #print ast.dump(tree) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1]-1 tree.symdict = f.func_globals.copy() tree.callstack = [] # handle free variables tree.nonlocaldict = {} if f.func_code.co_freevars: for n, c in zip(f.func_code.co_freevars, f.func_closure): obj = _cell_deref(c) if isinstance(g, _AlwaysComb): if not ( isinstance(obj, (int, long, EnumType,_Signal)) or \ _isMem(obj) or _isTupleOfInts(obj) ): info = "File %s, line %s: " % (tree.sourcefile, tree.lineoffset) print type(obj) raise ConversionError(_error.UnsupportedType, n, info) tree.symdict[n] = obj # currently, only intbv as automatic nonlocals (until Python 3.0) if isinstance(obj, intbv): tree.nonlocaldict[n] = obj tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _FirstPassVisitor(tree) v.visit(tree) if isinstance(g, _AlwaysComb): v = _AnalyzeAlwaysCombVisitor(tree, g.senslist) elif isinstance(g, _AlwaysSeq): v = _AnalyzeAlwaysSeqVisitor(tree, g.senslist, g.reset, g.sigregs, g.varregs) else: v = _AnalyzeAlwaysDecoVisitor(tree, g.senslist) v.visit(tree) else: # @instance f = g.gen.gi_frame s = inspect.getsource(f) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1]-1 tree.symdict = f.f_globals.copy() tree.symdict.update(f.f_locals) tree.nonlocaldict = {} tree.callstack = [] tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper() v = _FirstPassVisitor(tree) v.visit(tree) v = _AnalyzeBlockVisitor(tree) v.visit(tree) genlist.append(tree) return genlist
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
def __init__(self, func, symdict): self.func = func self.symdict = symdict s = inspect.getsource(func) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) v = _AttrRefTransformer(self) v.visit(tree) v = _SigNameVisitor(self.symdict) v.visit(tree) self.inputs = v.inputs self.outputs = v.outputs senslist = [] for n in self.inputs: s = self.symdict[n] if isinstance(s, _Signal): senslist.append(s) else: # list of sigs senslist.extend(s) self.senslist = tuple(senslist) self.gen = self.genfunc() if len(self.senslist) == 0: raise AlwaysCombError(_error.EmptySensitivityList) if len(self.senslist) == 1: W = _SignalWaiter else: W = _SignalTupleWaiter self.waiter = W(self.gen)
def __init__(self, func, symdict): self.symdict = symdict s = inspect.getsource(func) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) v = _AttrRefTransformer(self) v.visit(tree) v = _SigNameVisitor(self.symdict) v.visit(tree) self.inputs = v.results["input"] self.outputs = v.results["output"] inouts = v.results["inout"] | self.inputs.intersection(self.outputs) if inouts: raise AlwaysCombError(_error.SignalAsInout % inouts) if v.results["embedded_func"]: raise AlwaysCombError(_error.EmbeddedFunction) senslist = [] for n in self.inputs: s = self.symdict[n] if isinstance(s, _Signal): senslist.append(s) elif _isListOfSigs(s): senslist.extend(s) self.senslist = tuple(senslist) if len(self.senslist) == 0: raise AlwaysCombError(_error.EmptySensitivityList) super(_AlwaysComb, self).__init__(func, senslist)
def __init__(self, func): senslist = [] super(_AlwaysComb, self).__init__(func, senslist) s = inspect.getsource(func) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) v = _AttrRefTransformer(self) v.visit(tree) v = _SigNameVisitor(self.symdict) v.visit(tree) self.inputs = v.results['input'] self.outputs = v.results['output'] inouts = v.results['inout'] | self.inputs.intersection(self.outputs) if inouts: raise AlwaysCombError(_error.SignalAsInout % inouts) if v.results['embedded_func']: raise AlwaysCombError(_error.EmbeddedFunction) for n in self.inputs: s = self.symdict[n] if isinstance(s, _Signal): senslist.append(s) elif _isListOfSigs(s): senslist.extend(s) self.senslist = tuple(senslist) if len(self.senslist) == 0: raise AlwaysCombError(_error.EmptySensitivityList)
def __init__(self, func, symdict): self.func = func self.symdict = symdict s = inspect.getsource(func) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) v = _SigNameVisitor(symdict) v.visit(tree) self.inputs = v.inputs self.outputs = v.outputs senslist = [] for n in self.inputs: s = self.symdict[n] if isinstance(s, _Signal): senslist.append(s) else: # list of sigs senslist.extend(s) self.senslist = tuple(senslist) self.gen = self.genfunc() if len(self.senslist) == 0: raise AlwaysCombError(_error.EmptySensitivityList) if len(self.senslist) == 1: W = _SignalWaiter else: W = _SignalTupleWaiter self.waiter = W(self.gen)
def _inferWaiter(gen): f = gen.gi_frame s = inspect.getsource(f) s = _dedent(s) root = ast.parse(s) root.symdict = f.f_globals.copy() root.symdict.update(f.f_locals) # print ast.dump(root) v = _YieldVisitor(root) v.visit(root) if v.kind == _kind.EDGE_TUPLE: return _EdgeTupleWaiter(gen) if v.kind == _kind.SIGNAL_TUPLE: return _SignalTupleWaiter(gen) if v.kind == _kind.DELAY: return _DelayWaiter(gen) if v.kind == _kind.EDGE: return _EdgeWaiter(gen) if v.kind == _kind.SIGNAL: return _SignalWaiter(gen) # default return _Waiter(gen)
for n, v in func.func_globals.items(): if n not in varnames: symdict[n] = v # handle free variables if func.func_code.co_freevars: for n, c in zip(func.func_code.co_freevars, func.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) tree = ast.parse(s) # print ast.dump(tree) v = _AttrRefTransformer(self) v.visit(tree) v = _SigNameVisitor(self.symdict) v.visit(tree) sigregs = self.sigregs = [] varregs = self.varregs = [] for n in v.outputs: reg = self.symdict[n] if isinstance(reg, _Signal): sigregs.append(reg) elif isinstance(reg, intbv): varregs.append((n, reg, int(reg))) else:
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) tree = ast.parse(s) # print ast.dump(tree) v = _AttrRefTransformer(self) v.visit(tree) v = _SigNameVisitor(self.symdict) v.visit(tree) if v.results['inout']: raise AlwaysSeqError(_error.SigAugAssign, v.results['inout']) if v.results['embedded_func']: raise AlwaysSeqError(_error.EmbeddedFunction) sigregs = self.sigregs = [] varregs = self.varregs = []
def visit_Call(self, node): self.visit(node.func) self.access = _access.UNKNOWN for arg in node.args: self.visit(arg) for kw in node.keywords: self.visit(kw) self.access = _access.INPUT argsAreInputs = True f = self.getObj(node.func) node.obj = None if type(f) is type and issubclass(f, intbv): node.obj = self.getVal(node) elif f is concat: node.obj = self.getVal(node) elif f is len: self.access = _access.UNKNOWN node.obj = int(0) # XXX elif f is bool: node.obj = bool() elif f in (int, long, ord): node.obj = int(-1) ## elif f in (posedge , negedge): ## node.obj = _EdgeDetector() elif f is delay: node.obj = delay(0) ### suprize: identity comparison on unbound methods doesn't work in python 2.5?? elif f == intbv.signed: node.obj = int(-1) elif f in myhdlObjects: pass elif f in builtinObjects: pass elif type(f) is FunctionType: argsAreInputs = False s = inspect.getsource(f) s = _dedent(s) tree = ast.parse(s) # print ast.dump(tree) # print tree fname = f.__name__ tree.name = _Label(fname) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1]-1 tree.symdict = f.func_globals.copy() tree.nonlocaldict = {} if fname in self.tree.callstack: self.raiseError(node, _error.NotSupported, "Recursive call") tree.callstack = self.tree.callstack[:] tree.callstack.append(fname) # handle free variables if f.func_code.co_freevars: for n, c in zip(f.func_code.co_freevars, f.func_closure): obj = _cell_deref(c) if not isinstance(obj, (int, long, _Signal)): self.raiseError(node, _error.FreeVarTypeError, n) tree.symdict[n] = obj v = _FirstPassVisitor(tree) v.visit(tree) v = _AnalyzeFuncVisitor(tree, node.args, node.keywords) v.visit(tree) node.obj = tree.returnObj node.tree = tree tree.argnames = argnames = [arg.id for arg in tree.body[0].args.args] # extend argument list with keyword arguments on the correct position node.args.extend([None]*len(node.keywords)) for kw in node.keywords: node.args[argnames.index(kw.arg)] = kw.value for n, arg in zip(argnames, node.args): if n in tree.outputs: self.access = _access.OUTPUT self.visit(arg) self.access = _access.INPUT if n in tree.inputs: self.visit(arg) elif type(f) is MethodType: self.raiseError(node,_error.NotSupported, "method call: '%s'" % f.__name__) else: debug_info = [e for e in ast.iter_fields(node.func)] raise AssertionError("Unexpected callable %s" % str(debug_info)) if argsAreInputs: for arg in node.args: self.visit(arg)
def __init__(self, func): def senslistexpand(senslist, reg): # print(repr(reg)) if isinstance(reg, _Signal): senslist.append(reg) elif isinstance(reg, StructType): # print('senslistexpand StructType', repr(reg)) refs = vars(reg) # print(refs) for k in refs: # print(refs[k]) if isinstance(refs[k], _Signal): senslist.append(refs[k]) elif isinstance(refs[k], (list, Array)): if len(refs[k]) != 0: senslistexpand(senslist, refs[k]) elif isinstance(refs[k], StructType): senslistexpand(senslist, refs[k]) else: # print('senslistexpand passing {}?'.format(k)) pass elif isinstance(reg, (list, Array)): if isinstance(reg[0], (list, Array)): for r in reg: senslistexpand(senslist, r) else: if isinstance(reg[0], StructType): for rr in reg: senslistexpand(senslist, rr) elif isinstance(reg[0], _Signal): senslist.extend(reg) # elif isinstance(reg, Array): # senslist.extend(reg._array) # elif isinstance(reg, list): # senslist.extend(reg) # self.func = func # self.symdict = symdict senslist = [] super(_AlwaysComb, self).__init__(func, senslist) # print('_AlwaysComb', senslist) s = inspect.getsource(func) s = _dedent(s) tree = ast.parse(s) # print(ast.dump(tree)) # for item in ast.dump(tree).split(): # print(item) v = _AttrRefTransformer(self) v.visit(tree) # print(ast.dump(tree)) # for item in ast.dump(tree).split(): # print(item) v = _SigNameVisitor(self.symdict) v.visit(tree) # print(ast.dump(tree)) # for item in ast.dump(tree).split(): # print(item) self.inputs = v.results['input'] self.outputs = v.results['output'] # print(v.results) # print('inputs:', self.inputs) # print('outputs:', self.outputs) # inouts = v.results['inout'] | self.inputs.intersection(self.outputs) # if inouts: # raise AlwaysCombError(_error.SignalAsInout % inouts) if v.results['embedded_func']: raise AlwaysCombError(_error.EmbeddedFunction) # print(self.inputs) # print('2', self.senslist) for n in self.inputs: s = self.symdict[n] # print(n,s) # print(n, s, isinstance(s, StructType), isinstance(s, (list, Array))) senslistexpand(senslist, s) # if isinstance(s, _Signal): # senslist.append(s) # elif isinstance(s, list): # # print(repr(s)) # # list or Array of sigs # senslistexpand(senslist, s) # elif isinstance(s, Array): # # Array of sigs # if not isinstance(s.element, intbv): # senslistexpand(senslist, s) # elif isinstance(s, StructType): # senslistexpand(senslist, s) # elif _isListOfSigs(s): # senslist.extend(s) # else : # # print('_always_comb', n) # pass # print('3', self.senslist) self.senslist = tuple(senslist) # print('4', self.senslist) self.gen = self.genfunc() # for item in self.senslist: # print(item._name, repr(item)) # print() if len(self.senslist) == 0: raise AlwaysCombError(_error.EmptySensitivityList)