diff options
Diffstat (limited to 'main.py')
-rwxr-xr-x | main.py | 85 |
1 files changed, 61 insertions, 24 deletions
@@ -135,7 +135,7 @@ def get_full_path(path, name): if path[-1] == "/": return path + name else: return path + "/" + name -def get_elem_count(elem, elems): +def get_elem_count(elem): if "count" in elem.attrib: try: if str(int(elem.attrib["count"])) == elem.attrib["count"]: @@ -146,7 +146,7 @@ def get_elem_count(elem, elems): else: return 1 -def get_elem_size(elem, elems): +def get_elem_size(elem): if "size" in elem.attrib: try: if str(int(elem.attrib["size"])) == elem.attrib["size"]: @@ -203,6 +203,8 @@ class CodeGen(object): self.read_iter = [] self.def_iter = [] self.mem_size = {} + self.tree = xml.etree.ElementTree.parse(self.argparser.args.xml) + self.root = self.tree.getroot() def init_hook(self): pass @@ -229,11 +231,17 @@ class CodeGen(object): for key, value in self.mem_size.items(): print(key + ".." + value) + def dump_all_childs(self): + for node in self.root.iter(): + print(node.tag) + def gen_reader_funcs(self): temp_dec_list = [] 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\n") + read_source.write("#ifndef FT_READ_H\n#define FT_READ_H\n") + read_source.write('#ifdef __cplusplus\nextern "C" {\n#endif\n') read_source.write(text.header_list) read_source.write('#include "./structs.h"\n\n') inline = "inline " if self.argparser.args.inline else "" @@ -252,13 +260,13 @@ class CodeGen(object): if "isaggregate" in elem.attrib: dummy_string += ", " + elem.attrib["name"] + "*" + " dummy_" + elem.attrib["name"] read_source.write(static + inline + text.c_read_elem_sig.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"]+pointer)) - count = get_elem_count(elem, self.read_elems+self.def_elems) + count = get_elem_count(elem) if count == 1: for child in elem: - child_count = get_elem_count(child, self.def_elems + self.read_elems) + child_count = get_elem_count(child) ref_node_name = type_resolver(child, self.def_elems) ref_node = get_def_node(ref_node_name, self.def_elems) - size = get_elem_size(child, self.def_elems + self.read_elems) + size = get_elem_size(child) read_size_replacement = str() if size > 0: read_size_replacement = str(size) @@ -310,27 +318,52 @@ class CodeGen(object): read_source.write(text.c_read_gen.replace("XXX", "dummy->" + elem.attrib["name"]).replace("YYY", type_resolver(elem, self.def_elems))) #read_source.write(text.c_function_return_type) read_source.write(text.c_function_close + "\n") + read_source.write('#ifdef __cplusplus\n}#endif\n') + read_source.write("#endif //end of header guard\n\n") def gen_void_train(self): - void_source = open(self.argparser.args.outdir + "/void.h", "w") + #void_source = open(self.argparser.args.outdir + "/void.h", "w") + void_source = open(self.argparser.args.outdir + "/aggregate.h", "w") void_source.write("\n// automatically generated by faultreiber\n") void_source.write("// " + self.dnt + "\n") void_source.write('#include "./structs.h"\n') void_source.write('#include "./read.c"\n') void_source.write("#include <stdlib.h>\n") - void_source.write("void** void_train(void) {\n") + #void_source.write("void** void_train(void) {\n") + void_source.write('#ifndef FT_AGGREGATE_H\n#define FT_AGGREGATE_H\n') + void_source.write('#ifdef __cplusplus\nextern "C" {\n#endif\n') + void_source.write("void read_aggr(int _fd) {\n") + for elem in self.read_elems: + count = get_elem_count(elem) + size = get_elem_size(elem) + if count != 1: + void_source.write(elem.attrib["name"] + "** " + elem.attrib["name"] + "_container;\n") + else: + void_source.write(elem.attrib["name"] + "* " + elem.attrib["name"] + "_container;\n") + 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: + count = get_elem_count(child) + size = get_elem_size(child) + if count != 1: + void_source.write(ref_node.attrib["name"] + "** " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container;\n") + else: + void_source.write(ref_node.attrib["name"] + "* " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container;\n") count_int = int() count_void = int() read_count = len(self.read_elems) + void_source.write("//TODO-assign sub-containers to contrainers here\n") # FIXME-count and size present together is not being handled at all - for elem in self.read_elems + self.def_elems: + for elem in self.read_elems: + #for elem in self.read_elems + self.def_elems: 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: count_void+=1 - count = get_elem_count(child, self.def_elems + self.read_elems) - size = get_elem_size(child, self.def_elems + self.read_elems) + count = get_elem_count(child) + size = get_elem_size(child) type_width = get_type_width(child) #print(elem.tag + " " + child.tag + " " + "count:" + str(count) + " " + "size:" + str(size) + " " + "typ_width:" + str(type_width)) if count > 0: count_int+=count*type_width @@ -339,7 +372,8 @@ class CodeGen(object): if size < 0: count_void+=1 sizeof = (str(count_int) if count_int > 0 else ("")) + ("+" if count_void>0 and count_int>0 else "") + ((str(count_void)+"*"+"sizeof(void*)") if count_void > 0 else "") self.mem_size[elem.attrib["name"]] = text.c_reserve_void_ptr.replace("XXX", sizeof) - void_source.write(elem.attrib["name"] + "* = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") + #void_source.write(elem.attrib["name"] + "* " + elem.attrib["name"] + "_container" + " = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") + void_source.write(elem.attrib["name"] + "_container" + " = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") count_int = 0 count_void = 0 else: @@ -347,30 +381,32 @@ class CodeGen(object): ref_node = get_def_node(ref_node_name, self.def_elems) if ref_node: count_void+=1 if "size" in elem.attrib: - count = get_elem_count(elem, self.def_elems + self.read_elems) + count = get_elem_count(elem) if count > 0: count_int+= count else: count_void+=1 if "count" in elem.attrib: - size = get_elem_size(elem, self.def_elems + self.read_elems) + size = get_elem_size(elem) if size > 0: count_int+=size else: count_void+=1 sizeof = (str(count_int)+"+" if count_int > 0 else "") + (str(count_void)+"*"+"sizeof(void*)") if count_void > 0 else "" self.mem_size[elem.attrib["name"]] = text.c_reserve_void_ptr.replace("XXX", sizeof) - void_source.write(elem.attrib["name"] + "* = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") + #void_source.write(elem.attrib["name"] + "* " + elem.attrib["name"] + "_container" + " = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") + void_source.write(elem.attrib["name"] + "_container" + " = " + text.c_reserve_void_ptr.replace("XXX", sizeof) + ";\n") count_int = 0 count_void = 0 - void_source.write("}") + #void_source.write("}") def gen_aggregate_read(self): - agg_source = open(self.argparser.args.outdir + "/aggregate.h", "w") - agg_source.write("\n// automatically generated by faultreiber\n") - agg_source.write("// " + self.dnt + "\n") - agg_source.write('#include "./structs.h"\n') - agg_source.write('#include "./read.c"\n') - agg_source.write("void read_aggr(void) {\n") + agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a") + #agg_source.write("\n// automatically generated by faultreiber\n") + #agg_source.write("// " + self.dnt + "\n") + #agg_source.write('#include "./structs.h"\n') + #agg_source.write('#include "./read.c"\n') for elem in self.read_elems: - agg_source.write("ft_read_" + elem.attrib["name"] + "(_fd, );\n") - agg_source.write("}") + agg_source.write("ft_read_" + elem.attrib["name"] + "(_fd," + elem.attrib["name"] + "_container" + ");\n") + agg_source.write("}\n") + agg_source.write('#ifdef __cplusplus\n}\n#endif\n') + agg_source.write("#endif //end of header guard\n\n") def read_xml(self): if self.argparser.args.xml: @@ -493,7 +529,8 @@ class CodeGen(object): #self.dump_read_elems() self.gen_void_train() self.gen_aggregate_read() - self.dump_mem_dict() + #self.dump_mem_dict() + #self.dump_all_childs() # write code here def premain(argparser): |