utils/vlog: Refactor and improve XML include generation Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/vlog_to_model.py b/vlog_to_model.py index c781fb2..a382e7b 100755 --- a/vlog_to_model.py +++ b/vlog_to_model.py
@@ -23,7 +23,7 @@ import yosys.run from yosys.json import YosysJSON - +import xmlinc parser = argparse.ArgumentParser(description=__doc__.strip()) parser.add_argument( @@ -49,6 +49,10 @@ args = parser.parse_args() iname = os.path.basename(args.infiles[0]) +outfile = "model.xml" +if "o" in args and args.o is not None: + outfile = args.o + aig_json = yosys.run.vlog_to_json(args.infiles, True, True) if args.top is not None: @@ -67,15 +71,10 @@ print("ERROR: more than one module in design, cannot detect top level. Manually specify the top level module using --top") sys.exit(1) -xi_url = "http://www.w3.org/2001/XInclude" -ET.register_namespace('xi', xi_url) -xi_include = "{%s}include" % xi_url - -def include_xml(parent, href, xptr): - return ET.SubElement(parent, xi_include, {'href': href, 'xpointer': xptr}) +ET.register_namespace('xi', xmlinc.xi_url) tmod = yj.top_module -models_xml = ET.Element("models", nsmap = {'xi': xi_url}) +models_xml = ET.Element("models", nsmap = {'xi': xmlinc.xi_url}) inc_re = re.compile(r'^\s*`include\s+"([^"]+)"') @@ -98,7 +97,7 @@ model_path = "%s/%s.model.xml" % (module_path, wm.group(1).lower()) else: assert False - include_xml(models_xml, model_path, "xpointer(models/child::node())") + xmlinc.include_xml(models_xml, model_path, outfile, "xpointer(models/child::node())") else: # Is a leaf model topname = tmod.attr("MODEL_NAME", top) @@ -132,9 +131,6 @@ else: assert(False) #how does VPR specify inout (only applicable for PACKAGEPIN of an IO primitive) -outfile = "model.xml" -if "o" in args and args.o is not None: - outfile = args.o f = open(outfile, 'w') f.write(ET.tostring(models_xml, pretty_print=True).decode('utf-8'))
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py index 1808f1b..683b301 100755 --- a/vlog_to_pbtype.py +++ b/vlog_to_pbtype.py
@@ -52,6 +52,7 @@ import yosys.run from yosys.json import YosysJSON +import xmlinc parser = argparse.ArgumentParser(description=__doc__.strip(), formatter_class=argparse.RawTextHelpFormatter) parser.add_argument( @@ -77,16 +78,15 @@ args = parser.parse_args() iname = os.path.basename(args.infiles[0]) +outfile = "pb_type.xml" +if "o" in args and args.o is not None: + outfile = args.o + yosys.run.add_define("PB_TYPE") vjson = yosys.run.vlog_to_json(args.infiles, False, False) yj = YosysJSON(vjson) -xi_url = "http://www.w3.org/2001/XInclude" -ET.register_namespace('xi', xi_url) -xi_include = "{%s}include" % xi_url - -def include_xml(parent, href): - return ET.SubElement(parent, xi_include, {'href': href}) +ET.register_namespace('xi', xmlinc.xi_url) if args.top is not None: top = args.top @@ -167,7 +167,7 @@ pb_type_path = "%s/%s.pb_type.xml" % (module_path, wm.group(1).lower()) else: pb_type_path = "%s/pb_type.xml" % module_path - include_xml(xml_parent, pb_type_path) + xmlinc.include_xml(xml_parent, pb_type_path, outfile) # In order to avoid overspecifying interconnect, there are two directions we currently # consider. All interconnect going INTO a cell, and interconnect going out of a cell # into a top level output - or all outputs if "mode" is used. @@ -282,7 +282,7 @@ #TODO: might not always be the case? should be use Verilog `generate`s to detect this? pb_xml_attrs["num_pb"] = "1" - pb_type_xml = ET.Element("pb_type", pb_xml_attrs, nsmap = {'xi': xi_url}) + pb_type_xml = ET.Element("pb_type", pb_xml_attrs, nsmap = {'xi': xmlinc.xi_url}) # Process IOs clocks = yosys.run.list_clocks(args.infiles, mod.name) @@ -315,9 +315,6 @@ pb_type_xml = make_pb_type(tmod) -outfile = "pb_type.xml" -if "o" in args and args.o is not None: - outfile = args.o f = open(outfile, 'w') f.write(ET.tostring(pb_type_xml, pretty_print=True).decode('utf-8'))
diff --git a/xmlinc.py b/xmlinc.py new file mode 100644 index 0000000..eb546b8 --- /dev/null +++ b/xmlinc.py
@@ -0,0 +1,24 @@ +import lxml.etree as ET +import os + +xi_url = "http://www.w3.org/2001/XInclude" + +xi_include = "{%s}include" % xi_url + +def include_xml(parent, href, outfile, xptr = None): + """ + Generate an XML include, using a relative path. + + Inputs + ------ + parent : XML element to insert include into + href : path to included file + outfile : path to output file, for relative path generation + xptr : optional value for xpointer attribute + """ + outpath = os.path.dirname(outfile) + relhref = os.path.relpath(href, outpath) + xattrs = {'href': relhref} + if xptr is not None: + xattrs["xpointer"] = xptr + return ET.SubElement(parent, xi_include, xattrs)