def _update_hwire_namemap(href_instance, recursive, found, namemap): search_stack = [(href_instance, False)] name_stack = list() while search_stack: href_instance, visited = search_stack.pop() if visited: name_stack.pop() else: search_stack.append((href_instance, True)) name_stack.append(href_instance.item.name if href_instance.item.name else '') item = href_instance.item reference = item.reference if reference: for cable in reference.cables: hcable = HRef.from_parent_and_item(href_instance, cable) name_stack.append(cable.name if cable.name else '') cable_hname = '/'.join(name_stack[1:]) for wire_index, wire in enumerate(cable.wires): hwire = HRef.from_parent_and_item(hcable, wire) if hwire not in found: found.add(hwire) if cable.is_scalar: hname = cable_hname else: hname = "{}[{}]".format(cable_hname, cable.lower_index + wire_index) if hname not in namemap: namemap[hname] = list() namemap[hname].append(hwire) name_stack.pop() if recursive: for child in reference.children: if child.reference and child.reference.is_leaf() is False: href_child = HRef.from_parent_and_item(href_instance, child) search_stack.append((href_child, False))
def _update_hcable_namemap(href_instance, recursive, found, namemap): search_stack = [(href_instance, False)] name_stack = list() while search_stack: href_instance, visited = search_stack.pop() if visited: name_stack.pop() else: search_stack.append((href_instance, True)) name_stack.append( href_instance.item.name if href_instance.item.name else '') item = href_instance.item reference = item.reference if reference: for cable in reference.cables: hcable = HRef.from_parent_and_item(href_instance, cable) name_stack.append(cable.name if cable.name else '') cable_hname = '/'.join(name_stack[1:]) if hcable not in found: found.add(hcable) if cable_hname not in namemap: namemap[cable_hname] = list() namemap[cable_hname].append(hcable) name_stack.pop() if recursive: for child in reference.children: if child.reference and child.reference.is_leaf( ) is False: href_child = HRef.from_parent_and_item( href_instance, child) search_stack.append((href_child, False))
def _update_hport_namemap(href_instance, recursive, found, namemap): search_stack = [(href_instance, False)] name_stack = list() while search_stack: href_instance, visited = search_stack.pop() if visited: name_stack.pop() else: search_stack.append((href_instance, True)) name_stack.append(href_instance.item.name if href_instance.item.name else '') item = href_instance.item reference = item.reference if reference: for port in reference.ports: hport = HRef.from_parent_and_item(href_instance, port) name_stack.append(port.name if port.name else '') port_hname = '/'.join(name_stack[1:]) if hport not in found: found.add(hport) if port_hname not in namemap: namemap[port_hname] = list() namemap[port_hname].append(hport) name_stack.pop() if recursive: for child in reference.children: if child.reference: href_child = HRef.from_parent_and_item(href_instance, child) search_stack.append((href_child, False))
def _get_inner_hwire_from_hpin(hpin): wire = hpin.item.wire if wire: cable = wire.cable if cable: hport = hpin.parent hinst = hport.parent hcable = HRef.from_parent_and_item(hinst, cable) hwire = HRef.from_parent_and_item(hcable, wire) return hwire
def _get_outer_hwire_from_hpin(hpin): hport = hpin.parent hinst = hport.parent instance = hinst.item pin = hpin.item if pin in instance.pins: outer_pin = instance.pins[hpin.item] outer_wire = outer_pin.wire if outer_wire: cable = outer_wire.cable if cable: hcable = HRef.from_parent_and_item(hinst.parent, cable) hwire = HRef.from_parent_and_item(hcable, outer_wire) return hwire
def test_get_hwires_from_hrefs_of_cable_and_wire(self): library = self.netlist.libraries[1] definition = library.definitions[0] cable = definition.cables[0] wire = cable.wires[0] hrefs = list(sdn.get_hinstances(wire)) href_top = hrefs[0] from spydrnet.util.hierarchical_reference import HRef cable_href = HRef.from_parent_and_item(href_top, cable) wire_href = HRef.from_parent_and_item(cable_href, wire) href_result = next(sdn.get_hwires(cable_href), None) self.assertTrue(href_result is wire_href) href_result = next(sdn.get_hwires(wire_href), None) self.assertTrue(href_result is wire_href)
def test_get_hwires_from_hrefs_of_port_and_pin(self): library = self.netlist.libraries[1] definition = library.definitions[0] port = definition.ports[0] pin = port.pins[0] hrefs = list(sdn.get_hwires(pin)) href = hrefs[0] from spydrnet.util.hierarchical_reference import HRef port_href = HRef.from_parent_and_item(href.parent.parent, port) href_result = next(sdn.get_hwires(port_href), None) self.assertTrue(href_result is href) pin_href = HRef.from_parent_and_item(port_href, pin) href_result = next(sdn.get_hwires(pin_href), None) self.assertTrue(href_result is href)
def _update_namemap(href, recursive, found, namemap): currently_recursive = True search_stack = [(href, False)] name_stack = list() while search_stack: href, visited = search_stack.pop() if visited: name_stack.pop() else: search_stack.append((href, True)) name_stack.append(href.item.name if href.item.name else '') if len(name_stack) > 1: hname = '/'.join(name_stack[1:]) if hname not in namemap: namemap[hname] = list() namemap[hname].append(href) if currently_recursive: currently_recursive = recursive item = href.item reference = item.reference if reference: for child in reference.children: href_child = HRef.from_parent_and_item(href, child) if href_child not in found: found.add(href_child) search_stack.append((href_child, False))
def test_get_all_hrefs_of_item(self): netlist = sdn.Netlist() library = netlist.create_library() definition = library.create_definition() instance = sdn.Instance() instance.reference = definition netlist.top_instance = instance href = next(HRef.get_all_hrefs_of_item(instance)) self.assertTrue(href.item is instance) href = next(HRef.get_all_hrefs_of_item(definition)) self.assertTrue(href.item is instance) port = definition.create_port() href = next(HRef.get_all_hrefs_of_item(port)) self.assertTrue(href.is_valid and href.item is port)
def test_href_equality(self): top = sdn.Instance() top_def = sdn.Definition() top.reference = top_def middle = top_def.create_child() middle_def = sdn.Definition() middle.reference = middle_def leaf = middle_def.create_child() leaf_def = sdn.Definition() leaf.reference = leaf_def sequence = [top, middle, leaf] href1 = HRef.from_sequence(sequence) href2 = HRef.from_sequence(sequence) self.assertEqual(href1, href1) self.assertNotEqual(href1, None) self.assertEqual(href1, href2) self.assertNotEqual(href1, href2.parent) href3 = HRef.from_sequence(sequence[1:]) self.assertNotEqual(href1, href3)
def _get_hpins_from_hwire(hwire): hcable = hwire.parent hinst = hcable.parent for pin in hwire.item.pins: if isinstance(pin, InnerPin): port = pin.port if port: hport = HRef.from_parent_and_item(hinst, port) hpin = HRef.from_parent_and_item(hport, pin) yield hpin else: instance = pin.instance inner_pin = pin.inner_pin if instance and inner_pin: port = inner_pin.port if port: other_hinst = HRef.from_parent_and_item(hinst, instance) other_hport = HRef.from_parent_and_item(other_hinst, port) other_hpin = HRef.from_parent_and_item(other_hport, inner_pin) yield other_hpin
def test_flyweight(self): instance = sdn.Instance() href1 = HRef.from_parent_and_item(None, instance) href2 = HRef.from_parent_and_item(None, instance) self.assertTrue(href1 is href2) import weakref w_href1 = weakref.ref(href1) w_href2 = weakref.ref(href2) href1 = None href2 = None import gc gc.collect() self.assertIsNone(w_href1()) self.assertIsNone(w_href2()) instance2 = sdn.Instance() href1 = HRef.from_sequence([instance, instance2]) href2_parent = HRef(instance, None) href2 = HRef.from_parent_and_item(href2_parent, instance2) self.assertTrue(href1 is href2)
def test_href_pin_name(self): top = sdn.Instance() top.name = "TOP" top_def = sdn.Definition() top.reference = top_def port = top_def.create_port() port.name = "PORT" port.create_pins(1) port.is_array = True wire = port.pins[0] sequence = [top, port, wire] self.assertEqual("PORT[0]", HRef.from_sequence(sequence).name)
def test_href_wire_name(self): top = sdn.Instance() top.name = "TOP" top_def = sdn.Definition() top.reference = top_def cable = top_def.create_cable() cable.name = "CABLE" cable.create_wires(1) cable.is_array = True wire = cable.wires[0] sequence = [top, cable, wire] self.assertEqual("CABLE[0]", HRef.from_sequence(sequence).name)
def _update_hwire_namemap(href_instance, recursive, found, namemap): search_stack = [(href_instance, False)] name_stack = list() while search_stack: href_instance, visited = search_stack.pop() if visited: name_stack.pop() else: search_stack.append((href_instance, True)) name_stack.append( href_instance.item.name if href_instance.item.name else '') item = href_instance.item reference = item.reference if reference: for port in reference.ports: hport = HRef.from_parent_and_item(href_instance, port) name_stack.append(port.name if port.name else '') port_hname = '/'.join(name_stack[1:]) for pins_index, pin in enumerate(port.pins): hpin = HRef.from_parent_and_item(hport, pin) if hpin not in found: found.add(hpin) if port.is_scalar: hname = port_hname else: hname = "{}[{}]".format( port_hname, port.lower_index + pins_index) if hname not in namemap: namemap[hname] = list() namemap[hname].append(hpin) name_stack.pop() if recursive: for child in reference.children: if child.reference: href_child = HRef.from_parent_and_item( href_instance, child) search_stack.append((href_child, False))
def test_is_unique(self): netlist = sdn.Netlist() library = netlist.create_library() definition1 = library.create_definition() instance1 = definition1.create_child() definition2 = library.create_definition() instance2a = definition2.create_child() instance2a.reference = definition1 instance2b = definition2.create_child() instance2b.reference = definition1 instance3 = sdn.Instance() instance3.reference = definition2 netlist.top_instance = instance3 href1 = HRef.from_sequence([instance3, instance2a, instance1]) href2 = HRef.from_sequence([instance3, instance2b, instance1]) # Another test for test_flyweight stuffed in here :) self.assertTrue(href1.parent.parent is href2.parent.parent) self.assertTrue(href1.is_valid) self.assertTrue(href2.is_valid) self.assertFalse(href2.is_unique) self.assertFalse(href1.is_unique) definition2.remove_child(instance2b) self.assertFalse(href2.is_valid) self.assertFalse(href2.is_unique) self.assertTrue(href1.is_unique) definition4 = library.create_definition() definition4.add_child(instance2b) instance4 = sdn.Instance() instance4.reference = definition4 definition2.add_child(instance4) self.assertFalse(href1.is_unique)
def test_href_str_and_repr(self): netlist = sdn.Netlist() library = netlist.create_library() definition = library.create_definition() instance = sdn.Instance() instance.reference = definition netlist.top_instance = instance cable = definition.create_cable() cable.name = "MY_CABLE" cable.create_wires(8) href = HRef.from_sequence([instance, cable]) href_str = str(href) self.assertTrue(href_str == "MY_CABLE") href_repr = repr(href) self.assertTrue(HRef.__name__ in href_repr) self.assertTrue(cable.__class__.__name__ in href_repr)
def _get_instances_raw(object_collection, patterns, recursive, is_case, is_re): in_namemap = set() in_yield = set() namemap = dict() instance_search = set() while object_collection: obj = object_collection.pop() if isinstance(obj, Netlist): top_instance = obj.top_instance if top_instance: href = HRef.from_parent_and_item(None, top_instance) _update_namemap(href, recursive, in_namemap, namemap) elif isinstance(obj, HRef): if obj.is_valid is False: continue item = obj.item if isinstance(item, Instance): _update_namemap(obj, recursive, in_namemap, namemap) elif isinstance(item, (Port, Cable)): hinstance = obj.parent if hinstance and hinstance not in in_yield: in_yield.add(hinstance) yield hinstance elif isinstance(item, (InnerPin, Wire)): hcable_or_hport = obj.parent if hcable_or_hport: hinstance = hcable_or_hport.parent if hinstance and hinstance not in in_yield: in_yield.add(hinstance) yield hinstance elif isinstance(obj, Library): object_collection += obj.definitions elif isinstance(obj, Definition): instance_search |= obj.references elif isinstance(obj, Instance): instance_search.add(obj) elif isinstance(obj, (Port, Cable)): definition = obj.definition if definition: object_collection.append(definition) elif isinstance(obj, InnerPin): port = obj.port if port: definition = port.definition if definition: object_collection.append(definition) elif isinstance(obj, OuterPin): instance = obj.instance if instance: instance_search.add(instance) elif isinstance(obj, Wire): cable = obj.cable if cable: definition = cable.definition if definition: object_collection.append(definition) if instance_search: for href in HRef.get_all_hrefs_of_instances(instance_search): if href not in in_yield: in_yield.add(href) yield href for href in in_yield: in_namemap.discard(href) if in_namemap: for pattern in patterns: pattern_is_absolute = _is_pattern_absolute(pattern, is_case, is_re) if pattern_is_absolute: if pattern in namemap: result = namemap[pattern] for href in result: if href in in_namemap: in_namemap.remove(href) yield href else: for name in namemap: if _value_matches_pattern(name, pattern, is_case, is_re): result = namemap[name] for href in result: if href in in_namemap: in_namemap.remove(href) yield href
def test_get_hwire_of_invalid_reference(self): from spydrnet.util.hierarchical_reference import HRef invalid_href = HRef.from_parent_and_item(None, None) hrefs = list(sdn.get_hwires(invalid_href)) self.assertTrue(len(hrefs) == 0)
def test_href_valid(self): instance = sdn.Instance() href = HRef.from_parent_and_item(None, instance) self.assertFalse(href.is_valid) definition = sdn.Definition() instance.reference = definition self.assertFalse(href.is_valid) library = sdn.Library() library.add_definition(definition) self.assertFalse(href.is_valid) netlist = sdn.Netlist() netlist.add_library(library) self.assertFalse(href.is_valid) netlist.top_instance = instance self.assertTrue(href.is_valid) cable = sdn.Cable() wire = sdn.Wire() href = HRef.from_sequence([instance, cable, wire]) self.assertFalse(href.is_valid) cable.add_wire(wire) self.assertFalse(href.is_valid) definition.add_cable(cable) self.assertTrue(href.is_valid) instance.reference = None self.assertFalse(href.is_valid) port = sdn.Port() pin = sdn.InnerPin() href = HRef.from_sequence([instance, port, pin]) self.assertFalse(href.is_valid) port.add_pin(pin) self.assertFalse(href.is_valid) definition.add_port(port) self.assertFalse(href.is_valid) instance.reference = definition self.assertTrue(href.is_valid) higher_definition = library.create_definition() higher_definition.add_child(instance) self.assertTrue(href.is_valid) higher_instance = sdn.Instance() higher_instance.reference = higher_definition netlist.top_instance = higher_instance self.assertFalse(href.is_valid) href = HRef.from_sequence([higher_instance, instance, cable, wire]) self.assertTrue(href.is_valid) higher_instance.reference = None self.assertFalse(href.is_valid) higher_definition.remove_child(instance) self.assertFalse(href.is_valid) definition.remove_cable(cable) self.assertFalse(href.is_valid) cable.remove_wire(wire) self.assertFalse(href.is_valid) cable.add_wire(wire) href = HRef.from_sequence([cable, wire]) self.assertFalse(href.is_valid) cable.remove_wire(wire) new_cable = sdn.Cable() new_cable.add_wire(wire) self.assertFalse(href.is_valid) href = HRef.from_parent_and_item(None, wire) self.assertFalse(href.is_valid) port.remove_pin(pin) new_port = sdn.Port() new_port.add_pin(pin) href = HRef.from_sequence([port, pin]) self.assertFalse(href.is_valid) href = HRef.from_parent_and_item(None, pin) self.assertFalse(href.is_valid) href = HRef.from_parent_and_item(None, definition) self.assertFalse(href.is_valid)
def test_hRef_shortcut(self): item = Instance("myCable") def2 = Definition("Hello") item.reference = def2 hr = HRef(item) self.assertEqual(hr.item.name, item.name, 'Href item shorcut error')
def test_href_inst_name(self): instance = sdn.Instance() instance.name = "MY_INST" href = HRef.from_parent_and_item(None, instance) self.assertEqual('', href.name)
def _get_hports_raw(object_collection, patterns, recursive, is_case, is_re): in_namemap = set() in_yield = set() namemap = dict() bypass_namesearch = set() while object_collection: obj = object_collection.pop() if isinstance(obj, Netlist): top_instance = obj.top_instance if top_instance: href = HRef.from_parent_and_item(None, top_instance) object_collection.append(href) elif isinstance(obj, HRef): if obj.is_valid is False: continue item = obj.item if isinstance(item, Instance): if obj not in bypass_namesearch: _update_hport_namemap(obj, recursive, in_namemap, namemap) else: bypass_namesearch.discard(obj) reference = item.reference if reference: # Get all cables inside a hierarchical instance for port in reference.ports: hport = HRef.from_parent_and_item(obj, port) if hport not in in_yield: in_yield.add(hport) yield(hport) # get internal cables recursively if recursive: for child in reference.children: href_child = HRef.from_parent_and_item(obj, child) bypass_namesearch.add(href_child) object_collection.append(href_child) elif isinstance(item, Port): if obj not in in_yield: in_yield.add(obj) yield (obj) elif isinstance(item, Cable): for wire in item.wires: href_wire = HRef.from_parent_and_item(obj, wire) object_collection.append(href_wire) elif isinstance(item, Wire): href_parent_cable = obj.parent href_parent_instance = href_parent_cable.parent for pin in item.pins: if isinstance(pin, OuterPin): instance = pin.instance if instance: href_inst = HRef.from_parent_and_item(href_parent_instance, pin.instance) inner_pin = pin.inner_pin if inner_pin: inner_port = inner_pin.port if inner_port: href_port = HRef.from_parent_and_item(href_inst, inner_port) if href_port not in in_yield: in_yield.add(href_port) yield href_port else: port = pin.port if port: href_port = HRef.from_parent_and_item(href_parent_instance, port) if href_port not in in_yield: in_yield.add(href_port) yield href_port elif isinstance(item, InnerPin): hport = obj.parent if hport not in in_yield: in_yield.add(hport) yield hport elif isinstance(obj, Library): object_collection += obj.definitions elif isinstance(obj, Definition): hrefs = set(HRef.get_all_hrefs_of_instances(obj.references)) bypass_namesearch |= hrefs object_collection += hrefs elif isinstance(obj, Instance): hrefs = set(HRef.get_all_hrefs_of_instances(obj)) bypass_namesearch |= hrefs object_collection += hrefs elif isinstance(obj, (Port, Cable, InnerPin, OuterPin, Wire)): object_collection += HRef.get_all_hrefs_of_item(obj) for href in in_yield: in_namemap.discard(href) if in_namemap: # namemap is to cable for pattern in patterns: pattern_is_absolute = _is_pattern_absolute(pattern, is_case, is_re) if pattern_is_absolute: if pattern in namemap: result = namemap[pattern] for href in result: if href in in_namemap: in_namemap.remove(href) yield href else: for name in namemap: if _value_matches_pattern(name, pattern, is_case, is_re): result = namemap[name] for href in result: if href in in_namemap: in_namemap.remove(href) yield href
def _get_hwires_raw(object_collection, selection, patterns, recursive, is_case, is_re): in_namemap = set() in_yield = set() namemap = dict() hpin_search = set() bypass_namesearch = set() while object_collection: obj = object_collection.pop() if isinstance(obj, Netlist): top_instance = obj.top_instance if top_instance: href = HRef.from_parent_and_item(None, top_instance) object_collection.append(href) elif isinstance(obj, HRef): if obj.is_valid is False: continue item = obj.item if isinstance(item, Instance): if selection == Selection.INSIDE and obj not in bypass_namesearch: _update_hwire_namemap(obj, recursive, in_namemap, namemap) else: bypass_namesearch.discard(obj) reference = item.reference if reference: if selection in {Selection.INSIDE, Selection.ALL}: # Get all cables inside a hierarchical instance for cable in reference.cables: hcable = HRef.from_parent_and_item(obj, cable) for wire in cable.wires: hwire = HRef.from_parent_and_item(hcable, wire) if hwire not in in_yield: in_yield.add(hwire) yield(hwire) # get internal cables recursively if recursive or selection == Selection.ALL: for child in reference.children: href_child = HRef.from_parent_and_item(obj, child) bypass_namesearch.add(href_child) object_collection.append(href_child) if selection in {Selection.OUTSIDE, Selection.BOTH, Selection.ALL}: for port in reference.ports: href_port = HRef.from_parent_and_item(obj, port) for pin in port.pins: href_pin = HRef.from_parent_and_item(href_port, pin) hpin_search.add(href_pin) elif isinstance(item, Port): for pin in item.pins: href_pin = HRef.from_parent_and_item(obj, pin) hpin_search.add(href_pin) elif isinstance(item, Cable): for wire in item.wires: href_wire = HRef.from_parent_and_item(obj, wire) object_collection.append(href_wire) elif isinstance(item, Wire): if selection == Selection.INSIDE: if obj not in in_yield: in_yield.add(obj) yield obj elif selection == Selection.OUTSIDE: href_parent_cable = obj.parent href_parent_instance = href_parent_cable.parent for pin in item.pins: if isinstance(pin, OuterPin): href_inst = HRef.from_parent_and_item(href_parent_instance, pin.instance) inner_wire = pin.inner_pin.wire if inner_wire: inner_cable = inner_wire.cable href_cable = HRef.from_parent_and_item(href_inst, inner_cable) href_wire = HRef.from_parent_and_item(href_cable, inner_wire) if href_wire not in in_yield: in_yield.add(href_wire) yield href_wire else: href_parent = href_parent_instance.parent if href_parent: instance = href_parent_instance.item if pin in instance.pins: outer_pin = instance.pins[pin] outer_wire = outer_pin.wire if outer_wire: outer_cable = outer_wire.cable href_cable = HRef.from_parent_and_item(href_parent, outer_cable) href_wire = HRef.from_parent_and_item(href_cable, outer_wire) if href_wire not in in_yield: in_yield.add(href_wire) yield href_wire else: if obj not in in_yield: in_yield.add(obj) yield obj href_cable = obj.parent href_inst = href_cable.parent for pin in item.pins: if isinstance(pin, OuterPin): href_sub_inst = HRef.from_parent_and_item(obj.parent, pin.instance) inner_pin = pin.inner_pin port = inner_pin.port href_port = HRef.from_parent_and_item(href_sub_inst, port) href_pin = HRef.from_parent_and_item(href_port, pin) else: port = pin.port href_port = HRef.from_parent_and_item(href_inst, port) href_pin = HRef.from_parent_and_item(href_port, pin) object_collection.append(href_pin) elif isinstance(item, InnerPin): hpin_search.add(obj) elif isinstance(obj, Library): object_collection += obj.definitions elif isinstance(obj, Definition): hrefs = set(HRef.get_all_hrefs_of_instances(obj.references)) bypass_namesearch |= hrefs object_collection += hrefs elif isinstance(obj, Instance): hrefs = set(HRef.get_all_hrefs_of_instances(obj)) bypass_namesearch |= hrefs object_collection += hrefs elif isinstance(obj, (Port, Cable, InnerPin, OuterPin, Wire)): object_collection += HRef.get_all_hrefs_of_item(obj) if hpin_search: for hwire in _get_hwires_from_hpins(hpin_search, selection): if hwire not in in_yield: in_yield.add(hwire) yield hwire for href in in_yield: in_namemap.discard(href) if in_namemap: for pattern in patterns: pattern_is_absolute = _is_pattern_absolute(pattern, is_case, is_re) if pattern_is_absolute: if pattern in namemap: result = namemap[pattern] for href in result: if href in in_namemap: in_namemap.remove(href) yield href else: for name in namemap: if _value_matches_pattern(name, pattern, is_case, is_re): result = namemap[name] for href in result: if href in in_namemap: in_namemap.remove(href) yield href