diff options
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 94 |
1 files changed, 61 insertions, 33 deletions
@@ -13,6 +13,7 @@ from text import text import datetime import xml.etree.ElementTree from misc import * +import datetime # TODO-doesnt support non-byte-sized reads def type_resolver(elem, elem_list): @@ -63,6 +64,11 @@ def get_def_node(type_str, elem_list): if type_str == node.attrib["name"]: return node +def pointer_remover(name:str): + if name[-1] == '*': return name[0:-1] + '_p' + else: return name + + def reader_generator(elem, elem_list): pass @@ -74,6 +80,24 @@ def get_full_path(path, name): if path[-1] == "/": return path + name else: return path + "/" + name +def get_elem_count(elem, elems): + if "count" in elem.attrib: + print(elem.attrib["count"]) + try: + if str(int(elem.attrib["count"])) == elem.attrib["count"]: + return int(elem.attrib["count"]) + except ValueError: + return -1 + else: + return 1 + +def gen_count_reader(source_file, elem, elems): + count_simple = get_elem_count(elem, elems) + if count_simple > -1: + return True,count_simple + else: + return False,-1 + class Argparser(object): def __init__(self): parser = argparse.ArgumentParser() @@ -84,7 +108,8 @@ class Argparser(object): parser.add_argument("--xml", type=str, help="paht to the xml file") parser.add_argument("--dbg", action="store_true", help="debug", default=False) parser.add_argument("--datetime", action="store_true", help="print date and time in autogen files", default=False) - parser.add_argument("--inline", action="store_true", help="put all reads in sequentially", default=False) + parser.add_argument("--inline", action="store_true", help="inlines reader funcs", default=False) + parser.add_argument("--static", action="store_true", help="statics reader funcs", default=False) parser.add_argument("--verbose", action="store_true", help="verbose", default=False) self.args = parser.parse_args() @@ -130,41 +155,42 @@ class CodeGen(object): def gen_reader_funcs(self): read_source = open(self.argparser.args.outdir + "/read.c", "w") + read_source.write("\n// automatically generated by faultrieber\n") + read_source.write("// " + self.dnt + "\n") read_source.write(text.header_list) - for elem in self.def_elems: - read_source.write(text.c_read_elem_sig.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"])) - read_source.write(text.c_function_dummy_dec.replace("XXX", elem.attrib["name"])) - if "isaggregate" in elem.attrib: - for child in elem: - ref_node_name = type_resolver(child, self.def_elems) - ref_node = get_def_node(ref_node_name, self.def_elems) - if ref_node: - read_source.write(ref_node.attrib["name"] + " " + ref_node.attrib["name"] + "_ins" + "=") - read_source.write(text.c_read_elem_sig_1.replace("XXX", ref_node.attrib["name"]) + ";\n") - read_source.write("dummy." + child.attrib["name"] + "=" +ref_node.attrib["name"]+"_ins" + ";\n") - else: - read_source.write(ref_node_name + " " + ref_node_name+"_ins" + ";\n") - read_source.write(text.c_read_gen.replace("XXX", ref_node_name+"_ins").replace("YYY", ref_node_name)) - read_source.write("dummy." + child.attrib["name"] + "=" +ref_node_name+"_ins" + ";\n") - else: - read_source.write(type_resolver(elem, self.elems) + " " + elem.attrib["name"] + ";\n") - read_source.write(text.c_function_return_type) - read_source.write(text.c_function_close + "\n") - for elem in self.read_elems: - read_source.write(text.c_read_elem_sig.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"])) + inline = "inline " if self.argparser.args.inline else "" + static = "static " if self.argparser.args.static else "" + for elem in self.def_elems+ self.read_elems: + read_source.write(static + inline + text.c_read_elem_sig.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"])) read_source.write(text.c_function_dummy_dec.replace("XXX", elem.attrib["name"])) if "isaggregate" in elem.attrib: - for child in elem: - ref_node_name = type_resolver(child, self.def_elems) - ref_node = get_def_node(ref_node_name, self.def_elems) - if ref_node: - read_source.write(ref_node.attrib["name"] + " " + ref_node.attrib["name"] + "_ins" + "=") - read_source.write(text.c_read_elem_sig_1.replace("XXX", ref_node.attrib["name"]) + ";\n") - read_source.write("dummy." + child.attrib["name"] + "=" +ref_node.attrib["name"]+"_ins" + ";\n") - else: - read_source.write(ref_node_name + " " + ref_node_name+"_ins" + ";\n") - read_source.write(text.c_read_gen.replace("XXX", ref_node_name+"_ins").replace("YYY", ref_node_name)) - read_source.write("dummy." + child.attrib["name"] + "=" +ref_node_name+"_ins" + ";\n") + count = get_elem_count(elem, self.read_elems+self.def_elems) + if count == 1: + for child in elem: + child_count = get_elem_count(child, self.def_elems + self.read_elems) + ref_node_name = type_resolver(child, self.def_elems) + ref_node = get_def_node(ref_node_name, self.def_elems) + if ref_node: + ref_node_name = pointer_remover(ref_node.attrib["name"]) + for_dummy_declare = ref_node.attrib["name"] + " " + ref_node_name + "_ins" + "=" + for_read = text.c_read_elem_sig_1.replace("XXX", ref_node_name) + ";\n" + if child_count == 1: + for_dummy_assign = "dummy." + child.attrib["name"] + "=" +ref_node_name+"_ins" + ";\n" + read_source.write(for_dummy_declare+for_read+for_dummy_assign) + else: + for_dummy_assign = "dummy." + child.attrib["name"] + "[i]" + "=" +ref_node_name+"_ins" + ";\n" + read_source.write(text.simple_loop.replace("YYY", for_dummy_declare+for_read+for_dummy_assign)) + else: + for_dummy_declare = ref_node_name + " " + pointer_remover(ref_node_name)+"_ins" + ";\n" + for_read = text.c_read_gen.replace("XXX", pointer_remover(ref_node_name)+"_ins").replace("YYY", pointer_remover(ref_node_name)) + if child_count == 1: + for_dummy_assign = "dummy." + child.attrib["name"] + "=" +pointer_remover(ref_node_name)+"_ins" + ";\n" + read_source.write(for_dummy_declare+for_read+for_dummy_assign) + else: + for_dummy_assign = "dummy." + child.attrib["name"] + "[i]" + "=" +pointer_remover(ref_node_name)+"_ins" + ";\n" + read_source.write(text.simple_loop.replace("YYY", for_dummy_declare+for_read+for_dummy_assign)) + else: + pass else: read_source.write(type_resolver(elem, self.elems) + " " + elem.attrib["name"] + ";\n") read_source.write(text.c_function_return_type) @@ -173,6 +199,8 @@ class CodeGen(object): def read_xml(self): if self.argparser.args.xml: def_header = open(self.argparser.args.outdir + "/defines.h", "w") + def_header.write("\n// automatically generated by faultreiber\n") + def_header.write("// " + self.dnt + "\n") def_header.write(text.header_inttype + "\n") tree = xml.etree.ElementTree.parse(self.argparser.args.xml) root = tree.getroot() |