|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This script is intended to modify the architecture description file to be compliant with |
| 5 | +the new format. |
| 6 | +
|
| 7 | +It moves the top level pb_types attributes and tags to the tiles high-level tag. |
| 8 | +
|
| 9 | +BEFORE: |
| 10 | +<complexblocklist> |
| 11 | + <pb_type name="BRAM" area="2" height="4" width="1" capacity="1"> |
| 12 | + <input ... /> |
| 13 | + <input ... /> |
| 14 | + <input ... /> |
| 15 | + <output ... /> |
| 16 | + <output ... /> |
| 17 | + <output ... /> |
| 18 | + <interconnect ... /> |
| 19 | + <fc ... /> |
| 20 | + <pinlocations ... /> |
| 21 | + <switchblock_locations ... /> |
| 22 | + </pb_type> |
| 23 | +</complexblocklist> |
| 24 | +
|
| 25 | +AFTER: |
| 26 | +<tiles> |
| 27 | + <tile name="BRAM" area="2" height="4" width="1" capacity="1"> |
| 28 | + <interconnect ... /> |
| 29 | + <fc ... /> |
| 30 | + <pinlocations ... /> |
| 31 | + <switchblock_locations ... /> |
| 32 | + </tile> |
| 33 | +</tiles> |
| 34 | +<complexblocklist |
| 35 | + <pb_type name="BRAM"> |
| 36 | + <input ... /> |
| 37 | + <input ... /> |
| 38 | + <input ... /> |
| 39 | + <output ... /> |
| 40 | + <output ... /> |
| 41 | + <output ... /> |
| 42 | + </pb_type> |
| 43 | +</complexblocklist> |
| 44 | +""" |
| 45 | + |
| 46 | +from lxml import etree as ET |
| 47 | +import argparse |
| 48 | + |
| 49 | +TAGS_TO_SWAP = ['fc', 'pinlocations', 'switchblock_locations'] |
| 50 | +ATTR_TO_REMOVE = ['area', 'height', 'width', 'capacity'] |
| 51 | + |
| 52 | +def swap_tags(tile, pb_type): |
| 53 | + # Moving tags from top level pb_type to tile |
| 54 | + for child in pb_type: |
| 55 | + if child.tag in TAGS_TO_SWAP: |
| 56 | + pb_type.remove(child) |
| 57 | + tile.append(child) |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + parser = argparse.ArgumentParser( |
| 62 | + description="Moves top level pb_types to tiles tag." |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + '--arch_xml', |
| 66 | + required=True, |
| 67 | + help="Input arch.xml that needs to be modified to move the top level pb_types to the `tiles` tag." |
| 68 | + ) |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + |
| 72 | + arch_xml = ET.ElementTree() |
| 73 | + root_element = arch_xml.parse(args.arch_xml) |
| 74 | + |
| 75 | + tiles = ET.SubElement(root_element, 'tiles') |
| 76 | + |
| 77 | + top_pb_types = [] |
| 78 | + for pb_type in root_element.iter('pb_type'): |
| 79 | + if pb_type.getparent().tag == 'complexblocklist': |
| 80 | + top_pb_types.append(pb_type) |
| 81 | + |
| 82 | + for pb_type in top_pb_types: |
| 83 | + tile = ET.SubElement(tiles, 'tile') |
| 84 | + attrs = pb_type.attrib |
| 85 | + |
| 86 | + for attr in attrs: |
| 87 | + tile.set(attr, pb_type.get(attr)) |
| 88 | + |
| 89 | + # Remove attributes of top level pb_types only |
| 90 | + for attr in ATTR_TO_REMOVE: |
| 91 | + pb_type.attrib.pop(attr, None) |
| 92 | + |
| 93 | + swap_tags(tile, pb_type) |
| 94 | + |
| 95 | + print(ET.tostring(arch_xml, pretty_print=True).decode('utf-8')) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + main() |
0 commit comments