diff options
-rwxr-xr-x | main.py | 57 | ||||
-rw-r--r--[-rwxr-xr-x] | misc.py | 0 | ||||
-rw-r--r-- | resources/wasm.xml | 2 | ||||
-rw-r--r-- | text.py | 21 |
4 files changed, 70 insertions, 10 deletions
@@ -14,6 +14,7 @@ import datetime import xml.etree.ElementTree from misc import * +# TODO-doesnt support non-byte-sized reads def type_resolver(elem, elem_list): type_str = elem.attrib["type"] type_name = elem.attrib["name"] @@ -54,10 +55,17 @@ def type_resolver(elem, elem_list): elif type_str.find("self::") == 0: for node in elem_list: if elem.attrib["type"][6:] == node.tag: - print(node.tag) return node.attrib["name"] else: return type_str +def get_def_node(type_str, elem_list): + for node in elem_list: + if type_str == node.attrib["name"]: + return node + +def reader_generator(elem, elem_list): + pass + def SigHandler_SIGINT(signum, frame): print() sys.exit(0) @@ -94,6 +102,10 @@ class CodeGen(object): self.struct_json = json.load(open(self.argparser.args.structs)) self.dnt = datetime.datetime.now().isoformat() self.elems = [] + self.def_elems = [] + self.read_elems = [] + self.read_iter = [] + self.def_iter = [] def init_hook(self): pass @@ -101,8 +113,37 @@ class CodeGen(object): def init(self): dupemake(self.argparser.args.outdir, self.argparser.args.targetname) + def dump_elems(self): + for elem in self.elems: + print("XXXX " + elem.tag) + print(elem.attrib) + + def dump_def_elems(self): + for elem in self.def_elems: + print("XXXX " + elem.tag) + print(elem.attrib) + + def dump_read_elems(self): + for elem in self.read_elems: + print("XXXX " + elem.tag) + print(elem.attrib) + def gen_reader_funcs(self): - pass + read_source = open(self.argparser.args.outdir + "/read.c", "w") + read_source.write(text.header_list) + for elem in self.read_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(text.c_read_elem_sig_1.replace("XXX", ref_node.attrib["name"]) + ";\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") def read_xml(self): if self.argparser.args.xml: @@ -113,13 +154,18 @@ class CodeGen(object): read_tree = xml.etree.ElementTree.Element("read") def_tree = xml.etree.ElementTree.Element("def") for child in root: - print(child.tag + "--" + repr(child.attrib)) if child.tag == "Read": read_tree = child if child.tag == "Definition": def_tree = child + for child in read_tree: + self.read_elems.append(child) + for child in def_tree: + self.def_elems.append(child) read_iter = read_tree.iter(tag=None) def_iter = def_tree.iter(tag=None) + self.read_iter = read_iter + self.def_iter = def_iter for child in def_iter: self.elems.append(child) if "isaggregate" in child.attrib: @@ -133,7 +179,6 @@ class CodeGen(object): if "isaggregate" in child.attrib: def_header.write("typedef struct {\n") for childerer in child: - print(childerer.tag) c_type = type_resolver(childerer, self.elems) def_header.write("\t" + c_type + " " + childerer.attrib["name"] + ";\n") def_header.write("}" + child.attrib["name"] + ";\n\n") @@ -168,6 +213,10 @@ class CodeGen(object): self.init_hook() self.gen_struct_header() self.read_xml() + self.gen_reader_funcs() + #self.dump_def_elems() + #print("") + #self.dump_read_elems() # write code here def premain(argparser): diff --git a/resources/wasm.xml b/resources/wasm.xml index 5b1220c..6748ecb 100644 --- a/resources/wasm.xml +++ b/resources/wasm.xml @@ -25,7 +25,7 @@ </Memory_Section> <Global_Section name="W_Global_Section" count="1" type="" isaggregate="true"> <Count name="count" encoding="leb128u" type="uint32" count="1"/> - <Globals name="globals" type="this::Global_Entry" count="self::Count"/> + <Globals name="globals" type="self::Global_Entry" count="self::Count"/> </Global_Section> <Export_Section name="W_Export_Section" count="1" type="" isaggregate="true"> <Count name="count" encoding="leb128u" type="uint32" count="1"/> @@ -1,11 +1,7 @@ # _*_ coding=utf-8 _*_ class text(): - header_list = """#include <fcntl.h>\n - #include <inttypes.h>\n - #include <stdio.h>\n - #include <stdlib.h>\n - #include <unistd.h>\n""" + header_list = """#include <fcntl.h>\n#include <inttypes.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <unistd.h>\n""" header_inttype = "#include <inttypes.h>\n" main_sig = 'int main(int argc, char** argv)' pragma_weak_main = '#pragma weak main' @@ -15,3 +11,18 @@ class text(): autogen_warning = "// this file has been automatically generated by faultreiber\n" last_comment = "// last line intentionally left blank\n\n" read_func_sig = "int read_structured_file(char* path)" + c_read_elem_sig = "XXX ft_read_YYY(char* _ft_file_path, int _fd) {\n" + c_read_elem_sig_1 = "ft_read_XXX(_ft_file_path, _fd)" + c_open_file = "int ft_read_file = open(_ft_file_path, RDONLY);\n" + c_function_close = "}\n" + c_function_dummy_dec = "XXX dummy;\n" + c_function_return_type = "return dummy;\n" + c_read_def_1 = "uint8_t XXX;\n" + c_read_def_2 = "uint16_t XXX;\n" + c_read_def_4 = "uint32_t XXX;\n" + c_read_def_8 = "uint64_t XXX;\n" + c_read_1 = "read(_fd, &XXX, sizeof(uint8_t));\n" + c_read_2 = "read(_fd, &XXX, sizeof(uint16_t));\n" + c_read_4 = "read(_fd, &XXX, sizeof(uint32_t));\n" + c_read_8 = "read(_fd, &XXX, sizeof(uint64_t));\n" + c_assign_struct = "XXX.YYY = ZZZ;\n" |