def extractor(self, frame, event, arg): if event == "call": funcname = frame.f_code.co_name # skip certain functions if funcname in self.skipNames: self.skip +=1 if not self.skip: self.level += 1 elif event == "return": funcname = frame.f_code.co_name func = frame.f_globals.get(funcname) if func is None: # Didn't find a func in the global space, try the local "self" # argument and see if it has a method called *funcname* obj = frame.f_locals.get('self') if hasattr(obj, funcname): func = getattr(obj, funcname) if not self.skip: isGenSeq = _isGenSeq(arg) if isGenSeq: specs = {} for hdl in _userCodeMap: spec = "__%s__" % hdl if spec in frame.f_locals and frame.f_locals[spec]: specs[spec] = frame.f_locals[spec] spec = "%s_code" % hdl if func and hasattr(func, spec) and getattr(func, spec): specs[spec] = getattr(func, spec) spec = "%s_instance" % hdl if func and hasattr(func, spec) and getattr(func, spec): specs[spec] = getattr(func, spec) if specs: _addUserCode(specs, arg, funcname, func, frame) # building hierarchy only makes sense if there are generators if isGenSeq and arg: sigdict = {} memdict = {} argdict = {} if func: arglist = inspect.getargspec(func).args else: arglist = [] symdict = frame.f_globals.copy() symdict.update(frame.f_locals) cellvars = [] cellvars.extend(frame.f_code.co_cellvars) #All nested functions will be in co_consts if func: local_gens = [] consts = func.__code__.co_consts for item in _flatten(arg): genfunc = _genfunc(item) if genfunc.__code__ in consts: local_gens.append(item) if local_gens: objlist = _resolveRefs(symdict, local_gens) cellvars.extend(objlist) #for dict in (frame.f_globals, frame.f_locals): for n, v in symdict.items(): # extract signals and memories # also keep track of whether they are used in generators # only include objects that are used in generators ## if not n in cellvars: ## continue if isinstance(v, _Signal): sigdict[n] = v if n in cellvars: v._markUsed() if _isListOfSigs(v): m = _makeMemInfo(v) memdict[n] = m if n in cellvars: m._used = True # save any other variable in argdict if (n in arglist) and (n not in sigdict) and (n not in memdict): argdict[n] = v subs = [] for n, sub in frame.f_locals.items(): for elt in _inferArgs(arg): if elt is sub: subs.append((n, sub)) inst = _Instance(self.level, arg, subs, sigdict, memdict, func, argdict) self.hierarchy.append(inst) self.level -= 1 if funcname in self.skipNames: self.skip -= 1
def extractor(self, frame, event, arg): if event == "call": funcname = frame.f_code.co_name # skip certain functions if funcname in self.skipNames: self.skip += 1 if not self.skip: self.level += 1 elif event == "return": funcname = frame.f_code.co_name func = frame.f_globals.get(funcname) if func is None: # Didn't find a func in the global space, try the local "self" # argument and see if it has a method called *funcname* obj = frame.f_locals.get('self') if hasattr(obj, funcname): func = getattr(obj, funcname) if not self.skip: isGenSeq = _isGenSeq(arg) if isGenSeq: specs = {} for hdl in _userCodeMap: spec = "__%s__" % hdl if spec in frame.f_locals and frame.f_locals[spec]: specs[spec] = frame.f_locals[spec] spec = "%s_code" % hdl if func and hasattr(func, spec) and getattr( func, spec): specs[spec] = getattr(func, spec) spec = "%s_instance" % hdl if func and hasattr(func, spec) and getattr( func, spec): specs[spec] = getattr(func, spec) if specs: _addUserCode(specs, arg, funcname, func, frame) # building hierarchy only makes sense if there are generators if isGenSeq and arg: sigdict = {} memdict = {} symdict = frame.f_globals.copy() symdict.update(frame.f_locals) cellvars = [] # All nested functions will be in co_consts if func: local_gens = [] consts = func.__code__.co_consts for item in _flatten(arg): genfunc = _genfunc(item) if genfunc.__code__ in consts: local_gens.append(item) if local_gens: cellvarlist = _getCellVars(symdict, local_gens) cellvars.extend(cellvarlist) objlist = _resolveRefs(symdict, local_gens) cellvars.extend(objlist) # for dict in (frame.f_globals, frame.f_locals): for n, v in symdict.items(): # extract signals and memories # also keep track of whether they are used in generators # only include objects that are used in generators # if not n in cellvars: # continue if isinstance(v, _Signal): sigdict[n] = v if n in cellvars: v._markUsed() if _isListOfSigs(v): m = _makeMemInfo(v) memdict[n] = m if n in cellvars: m._used = True subs = [] for n, sub in frame.f_locals.items(): for elt in _inferArgs(arg): if elt is sub: subs.append((n, sub)) inst = _Instance(self.level, arg, subs, sigdict, memdict) self.hierarchy.append(inst) self.level -= 1 if funcname in self.skipNames: self.skip -= 1
def extractor(self, frame, event, arg): if event == "call": funcname = frame.f_code.co_name # skip certain functions if funcname in self.skipNames: self.skip += 1 if not self.skip: self.level += 1 elif event == "return": trace.push(False, '_HierExtr: return') funcname = frame.f_code.co_name func = frame.f_globals.get(funcname) if func is None: # Didn't find a func in the global space, try the local "self" # argument and see if it has a method called *funcname* obj = frame.f_locals.get('self') if hasattr(obj, funcname): func = getattr(obj, funcname) if not self.skip: isGenSeq = _isGenSeq(arg) if isGenSeq: specs = {} # collections.OrderedDict() #{} for hdl in _userCodeMap: spec = "__%s__" % hdl if spec in frame.f_locals and frame.f_locals[spec]: specs[spec] = frame.f_locals[spec] spec = "%s_code" % hdl if func and hasattr(func, spec) and getattr( func, spec): specs[spec] = getattr(func, spec) spec = "%s_instance" % hdl if func and hasattr(func, spec) and getattr( func, spec): specs[spec] = getattr(func, spec) if specs: _addUserCode(specs, arg, funcname, func, frame) # building hierarchy only makes sense if there are generators if isGenSeq and arg: sigdict = {} # collections.OrderedDict() #{} memdict = {} # collections.OrderedDict() #{} argdict = {} # collections.OrderedDict() #{} if func: arglist = inspect.getargspec(func).args else: arglist = [] symdict = frame.f_globals.copy() symdict.update(frame.f_locals) cellvars = [] # All nested functions will be in co_consts if func: local_gens = [] consts = func.__code__.co_consts for item in _flatten(arg): genfunc = _genfunc(item) if genfunc.__code__ in consts: local_gens.append(item) if local_gens: cellvarlist = _getCellVars(symdict, local_gens) cellvars.extend(cellvarlist) # TODO: must re-work this to let 'interfaces' work # as before objlist = _resolveRefs(symdict, local_gens) cellvars.extend(objlist) # the last one passing by is the top module ... # for n, v in nsymdict.items(): for n, v in symdict.items(): # extract signals and memories # also keep track of whether they are used in generators # only include objects that are used in generators # # if not n in cellvars: # # continue if isinstance(v, _Signal): trace.print( 'level {} Signal {} {}, used: {}, driven: {}, read: {}' .format(self.level, n, repr(v), v._used, v.driven, v._read)) sigdict[n] = v if n in cellvars: v._markUsed() elif isinstance(v, list): trace.print('level {} list {} {}'.format( self.level, n, v)) if len(v) > 0: levels, sizes, totalelements, element = m1Dinfo( v) if isinstance(element, (_Signal, Array, StructType)): m = _makeMemInfo(v, levels, sizes, totalelements, element) memdict[n] = m if n in cellvars: m._used = True if isinstance(element, (Array)): if isinstance(v[0], list): raise ValueError( 'don\'t handle nested lists', repr(v)) else: # instantiate every element separately for i, a in enumerate(v): m = _makeMemInfo( a, len(a.shape), a.shape, a.size, element) m.name = n + '({})'.format( str(i)) memdict[m.name] = m trace.print('\t', m.name, m) # for item in m.mem: # trace.print('\t', m.driven) if m.name in cellvars: m._used = True # else: # trace.print(repr(element)) elif isinstance(v, Array): trace.print('level {} Array {} {} {} {}'.format( self.level, n, repr(v), v.driven, v._read)) # only enter 'top' Arrays, i.e. not Arrays that are # a member of StructType(s) if '.' not in n: # we have all information handy in the Array # object m = _makeMemInfo(v, v.levels, v.shape, v.size, v.element) memdict[n] = m if n in cellvars: m._used = True m._driven = v.driven elif isinstance(v, StructType): trace.print('_HierExtr {} StructType {} {}'.format( self.level, n, v)) # only enter 'top' StructTypes, i.e. not the nested # StructType(s) if '.' not in n: # should also be entered in the memdict m = _makeMemInfo(v, 1, 1, 1, v) memdict[n] = m if n in cellvars: m._used = True m._driven = v.driven # save any other variable in argdict if (n in arglist) and (n not in sigdict) and ( n not in memdict): argdict[n] = v subs = [] for n, sub in frame.f_locals.items(): for elt in _inferArgs(arg): if elt is sub: subs.append((n, sub)) inst = _Instance(self.level, arg, subs, sigdict, memdict, func, argdict, funcname) self.hierarchy.append(inst) self.level -= 1 if funcname in self.skipNames: self.skip -= 1 trace.pop()