diff options
-rwxr-xr-x | main.py | 42 | ||||
-rw-r--r-- | resources/structsinclude.h | 27 | ||||
-rwxr-xr-x | run.sh | 3 | ||||
-rw-r--r-- | test/struct.json | 2 | ||||
-rw-r--r-- | text.py | 14 |
5 files changed, 82 insertions, 6 deletions
@@ -9,25 +9,31 @@ import readline from shutil import copy import signal import sys +from text import text +import datetime def SigHandler_SIGINT(signum, frame): print() sys.exit(0) +def get_full_path(path, name): + if path[-1] == "/": return path + name + else: return path + "/" + name + class Argparser(object): def __init__(self): parser = argparse.ArgumentParser() parser.add_argument("--targetname", type=str, help="main target name") parser.add_argument("--outdir", type=str, help="path to output dir") parser.add_argument("--structs", type=str, help="the structs json file") + parser.add_argument("--structsinclude", type=str, help="the path to the header that's going to be included by structs.h before structure declarations.") 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) self.args = parser.parse_args() def dupemake(path, main_name): copy("./resources/makefile", path) - makefile_path = str() - if path[-1] == "/": makefile_path = path + "makefile" - else: makefile_path = path + "/makefile" + makefile_path = get_full_path(path, "makefile") for line in fileinput.input(makefile_path, inplace=True): if "XXX" in line: line = line.replace("XXX", main_name) @@ -37,6 +43,7 @@ class CodeGen(object): def __init__(self, argparser): self.argparser = argparser self.struct_json = json.load(open(self.argparser.args.structs)) + self.dnt = datetime.datetime.now().isoformat() def init_hook(self): pass @@ -44,9 +51,38 @@ class CodeGen(object): def init(self): dupemake(self.argparser.args.outdir, self.argparser.args.targetname) + def gen_reader_funcs(self): + pass + + def gen_struct_header(self): + struct_source = open(get_full_path(self.argparser.args.outdir, "structs.h"), "w") + struct_source_c = open(get_full_path(self.argparser.args.outdir, "structs.c"), "w") + struct_source_c.write('#include "structs.h"') + struct_source.write(text.pre_header_guard) + struct_source.write(text.autogen_warning) + if self.argparser.args.datetime: struct_source.write("// " + self.dnt + "\n") + struct_source.write(text.header_guard_begin.replace("XXX", "structs".upper())) + struct_source.write(text.header_inttype) + if self.argparser.args.structsinclude: + copy(self.argparser.args.structsinclude, self.argparser.args.outdir) + pos = self.argparser.args.structsinclude.rfind("/") + sub = self.argparser.args.structsinclude[pos+1:] + struct_source.write('#include "' + sub + '"\n') + for k,v in self.struct_json.items(): + struct_name = k + field_names = v["field_name"] + field_typess = v["field_type"] + struct_source.write("typedef struct {\n") + for i, j in zip(field_names, field_typess): + struct_source.write("\t" + j + " " + i + ";\n") + struct_source.write("}" + struct_name + ";\n\n") + struct_source.write(text.pragma_endif) + struct_source.write(text.last_comment) + def run(self): self.init() self.init_hook() + self.gen_struct_header() # write code here def premain(argparser): diff --git a/resources/structsinclude.h b/resources/structsinclude.h new file mode 100644 index 0000000..47c3626 --- /dev/null +++ b/resources/structsinclude.h @@ -0,0 +1,27 @@ + +#ifndef WASM_H +#define WASM_H +#include <inttypes.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint8_t varint1; +typedef uint8_t varint7; +typedef uint32_t varint32; +typedef int8_t varuint1; +typedef int8_t varuint7; +typedef int32_t varuint32; + + enum value_type_t {f64_vt = -4, f32_vt, i64_vt, i32_vt}; + enum external_kind_t {Function, Table, Memory, Global}; + enum type_ctor_t {i32_ctor = -1, i64_ctor = -2, f32_ctor = -3, f64_ctor = -4, anyfunc_ctor = -16, func_ctor = -32, block_type_ctor = -64}; + +#ifdef __cplusplus +} +#endif // end of extern c +#endif // end of header guard +/**********************************************************************************************************************/ +/*last line intentionally left blank.*/ + @@ -1,2 +1,3 @@ #!/bin/sh -"./main.py" --targetname autowasm --outdir ./test/ --structs ./test/structs.json +cd $(dirname $0) +"./faultreiber.py" --targetname autowasm --outdir ./test/ --structs ./test/struct.json --datetime --structsinclude ./resources/structsinclude.h diff --git a/test/struct.json b/test/struct.json index 38fe5a7..cb7899c 100644 --- a/test/struct.json +++ b/test/struct.json @@ -15,7 +15,7 @@ "W_Import_Section_Entry": {"field_name":["module_length", "module_str", "field_len", "field_str", "kind", "type"], "field_type":["varuint32", "char*", "varuint32", "char*", "enum external_kind_t", "void*"], "lua_type": ["integer", "string", "integer", "string", "integer", "lightuserdata"], "methods": ["convert", "check", "push_self", "push_args", "new"]}, "W_Import_Section": - {"field_name":["count", "entries"], "field_type":["varuint32","W_Import_Section**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + {"field_name":["count", "entries"], "field_type":["varuint32","W_Import_Section_Entry**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, "W_Function_Section": {"field_name":["count", "types"], "field_type":["varuint32", "varuint32*"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, "W_Table_Section": @@ -1,4 +1,16 @@ # _*_ coding=utf-8 _*_ -class Text(): +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_inttype = "#include <inttypes.h>\n" main_sig = 'int main(int argc, char** argv)' + pragma_weak_main = '#pragma weak main' + pre_header_guard = "\n// first line intentionally left blank\n" + header_guard_begin = "#ifndef _AUTO_XXX_H\n#define _AUTO_XXX_H\n" + pragma_endif = "#endif\n" + autogen_warning = "// this file has been automatically generated by faultreiber\n" + last_comment = "// last line intentionally left blank\n\n" |