diff options
Diffstat (limited to 'extra-tools')
-rwxr-xr-x | extra-tools/luatablegen.py | 121 | ||||
-rwxr-xr-x | extra-tools/tablegen-test/run.sh | 10 | ||||
-rw-r--r-- | extra-tools/wasmtablegen.json | 56 |
3 files changed, 164 insertions, 23 deletions
diff --git a/extra-tools/luatablegen.py b/extra-tools/luatablegen.py index a4569ca..7443c39 100755 --- a/extra-tools/luatablegen.py +++ b/extra-tools/luatablegen.py @@ -62,10 +62,20 @@ TABLE_REGISTER = ['int XXX_register(lua_State* __ls) {\n', SOURCE_FILE_NAME='XXX_luatablegen.c' HEADER_FILE_NAME='XXX_luatablegen.h' +LUA_LIB = ["local wasm = {}\n\n", "return wasm\n"] +LUA_SETMETA_NEW = ["setmetatable(XXX, {__call =\n", "\tfunction(selfAAA)\n", + "\t\tlocal t = self.new(AAA)\n", "\t\treturn t\n\tend\n\t}\n)\n"] + def SigHandler_SIGINT(signum, frame): print() sys.exit(0) +def get_filename(filename): + if filename[-1] == "/": + c_source = filename + c_filename + else: + c_source = "/" + c_filename + class Argparser(object): def __init__(self): parser = argparse.ArgumentParser() @@ -75,14 +85,21 @@ class Argparser(object): parser.add_argument("--post", type=str, help="path to source code file to add before header guard/extern c end") parser.add_argument("--luaheader", type=str, help="path to lua header files") parser.add_argument("--dbg", action="store_true", help="debug", default=False) + parser.add_argument("--singlefile", action="store_true", help="should all the generated code be added to a single file", default=False) + parser.add_argument("--makemacro", action="store_true", help="generate a makefile containing all objects in a macro to be included by abother makefile", default=False) + parser.add_argument("--outfile", type=str, help="name of the output file if signlefile is set, ignored otherwise") + parser.add_argument("--headeraggr", type=str, help="header aggregate file name") + parser.add_argument("--lualibpath", type=str, help="where the lua module file will be placed") self.args = parser.parse_args() class TbgParser(object): - def __init__(self, tbg, out, argparser): - self.tbg_file = json.load(open(tbg)) + def __init__(self, argparser): + self.tbg_file = json.load(open(argparser.args.tbg)) self.argparser = argparser - def begin(self, c_source, h_filename, struct_name): + def begin(self, c_source, struct_name, h_filename, is_source): + c_source.write("\n") + c_source.write("// automatically generated by luatablegen\n") for header in HEADER_LIST: if self.argparser.args.luaheader: c_source.write(header.replace("HHH", self.argparser.args.luaheader+"/")) @@ -90,14 +107,14 @@ class TbgParser(object): c_source.write(header.replace("HHH", "")) c_source.write(HEADER_GUARD[0].replace("XXX", struct_name)) c_source.write(EXTERN_C[0]) - #c_source.write('#include "./'+h_filename+'"\n') + if is_source: c_source.write("#include " + '"./' +h_filename+ '"\n') c_source.write("\n") if self.argparser.args.pre: pre_file = open(self.argparser.args.pre) for line in pre_file: c_source.write(line) + pre_file.close() c_source.write("\n") - pre_file.close() def struct(self, c_source, field_names, field_types, struct_name): c_source.write("typedef struct {\n") @@ -148,7 +165,7 @@ class TbgParser(object): if lua_type == "integer": dummy = "\t"+field_type +" "+field_name+" = "+"luaL_optinteger(__ls,"+repr(rev_counter)+",0);\n" elif lua_type == "lightuserdata": dummy = "\t"+field_type +" "+field_name+" = "+"lua_touserdata(__ls,"+repr(rev_counter)+");\n" elif lua_type == "number": pass - elif lua_type == "string":dummy = "\t"+field_type +" "+field_name+" = "+"lua_tostring(__ls,"+repr(rev_counter)+",0);\n" + elif lua_type == "string":dummy = "\t"+field_type +" "+field_name+" = "+"lua_tostring(__ls,"+repr(rev_counter)+");\n" elif lua_type == "boolean": pass else: print("bad lua_type entry in the json file") @@ -230,26 +247,61 @@ class TbgParser(object): post_file = open(self.argparser.args.post) for line in post_file: c_source.write(line) + post_file.clsoe() c_source.write("\n") c_source.write(EXTERN_C[1]) c_source.write(HEADER_GUARD[1]) c_source.write("\n") + def luagen(self): + l_source = open(self.argparser.args.lualibpath, "w") + l_source.write("-- automatically generated by luatablegen\n") + l_source.write(LUA_LIB[0]) + for k, v in self.tbg_file.items(): + struct_name = k + field_names = v['field_name'] + field_types = v['field_type'] + lua_types = v['lua_type'] + methods = v['methods'] + l_source.write(LUA_SETMETA_NEW[0].replace("XXX", struct_name)) + arg_list_str = str() + for i in range(0, len(field_names)): + arg_list_str += ", arg" + repr(i) + l_source.write(LUA_SETMETA_NEW[1].replace("AAA", arg_list_str)) + l_source.write(LUA_SETMETA_NEW[2].replace("AAA", arg_list_str[2:])) + l_source.write(LUA_SETMETA_NEW[3]) + arg_list_str = str() + l_source.write("\n") + + l_source.write(LUA_LIB[1]) + def run(self): + header_aggr_list = [] + table_reg_list = [] + if self.argparser.args.singlefile: + c_source = open(self.argparser.args.outfile, "w") for k, v in self.tbg_file.items(): struct_name = k field_names = v['field_name'] field_types = v['field_type'] lua_types = v['lua_type'] methods = v['methods'] - c_filename = struct_name + "_tablegen.h" - h_filename = struct_name + "_tablegen.h" - if self.argparser.args.out[-1] == "/": - c_source = open(self.argparser.args.out + c_filename, "w") - else: - c_source = open(self.argparser.args.out + "/" + c_filename, "w") - self.begin(c_source, h_filename, struct_name) - self.struct(c_source, field_names, field_types, struct_name) + if not self.argparser.args.singlefile: + c_filename = struct_name + "_tablegen.c" + h_filename = struct_name + "_tablegen.h" + if self.argparser.args.out[-1] == "/": + c_source = open(self.argparser.args.out + c_filename, "w") + header_aggr_list.append("./" + h_filename) + h_source = open(self.argparser.args.out + h_filename, "w") + else: + c_source = open(self.argparser.args.out + "/" + c_filename, "w") + header_aggr_list.append("./" + h_filename) + h_source = open(self.argparser.args.out + "/" + h_filename, "w") + # source file + # TODO - the c source file is getting a header guard and cpp + # inclusion macros + self.begin(c_source, struct_name, h_filename, True) + #self.struct(c_source, field_names, field_types, struct_name) self.convert(c_source, struct_name) self.check(c_source, struct_name) self.push_self(c_source, struct_name) @@ -261,13 +313,50 @@ class TbgParser(object): self.register_table_meta(c_source, struct_name) self.register_table(c_source, struct_name) self.end(c_source) - c_source.close() + if not self.argparser.args.singlefile: c_source.close() + # header file + self.begin(h_source, struct_name, h_filename, False) + h_source.write(CONVERT[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + h_source.write(CHECK[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + h_source.write(PUSH_SELF[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + h_source.write(PUSH_ARGS[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + h_source.write(NEW[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + for field_name, lua_type in zip(field_names, lua_types): + h_source.write(GETTER_GEN[0].replace("XXX", struct_name).replace("YYY", field_name).replace(" {\n", ";\n")) + for field_name, lua_type in zip(field_names, lua_types): + h_source.write(SETTER_GEN[0].replace("XXX", struct_name).replace("YYY", field_name).replace(" {\n", ";\n")) + table_reg_list.append(struct_name + "_register(__ls);\n") + h_source.write(TABLE_REGISTER[0].replace("XXX", struct_name).replace(" {\n", ";\n")) + self.end(h_source) + if self.argparser.args.headeraggr: + aggr_header = open(self.argparser.args.headeraggr, "w") + aggr_header.write(HEADER_GUARD[0].replace("XXX", "WASM_TABLES_AGGR")) + aggr_header.write(EXTERN_C[0]) + aggr_header.write("\n") + for item in header_aggr_list: + aggr_header.write("#include " + '"' + item + '"\n') + aggr_header.write("\n") + aggr_header.write("void reg_tablegen_tables(lua_State* __ls) {\n") + for func_sig in table_reg_list: + aggr_header.write("\t" + func_sig) + aggr_header.write("\t" + "lua_pop(__ls, 1);\n") + aggr_header.write("}\n") + aggr_header.write(EXTERN_C[1]) + aggr_header.write(HEADER_GUARD[1]) + aggr_header.write("\n") + if self.argparser.args.makemacro: + if self.argparser.args.out[-1] == "/": + m_source = open(self.argparser.args.out + "tablegen.mk", "w") + else: + m_source = open(self.argparser.args.out + "/" + "tablegen.mk", "w") + # generate lua module + self.luagen() # write code here def premain(argparser): signal.signal(signal.SIGINT, SigHandler_SIGINT) #here - parser = TbgParser(argparser.args.tbg, argparser.args.out, argparser) + parser = TbgParser(argparser) parser.run() def main(): diff --git a/extra-tools/tablegen-test/run.sh b/extra-tools/tablegen-test/run.sh index f9eda4d..c70e23b 100755 --- a/extra-tools/tablegen-test/run.sh +++ b/extra-tools/tablegen-test/run.sh @@ -1,4 +1,10 @@ #!/usr/bin/bash cd $(dirname $0) -../luatablegen.py --tbg ../wasmtablegen.json --out ./ --luaheader ../../bruiser/lua-5.3.4/src --pre ./pre.txt --post ./post.txt -less ./jmp_s_t_tablegen.h +../luatablegen.py --tbg ../wasmtablegen.json --out ../../bruiser/luatablegen --luaheader ../../bruiser/lua-5.3.4/src --pre ./wasmheader.txt --headeraggr ../../bruiser/luatablegen/wasm_tables.h --lualibpath ../../bruiser/lua-scripts/wasm.lua +#../luatablegen.py --tbg ../wasmtablegen.json --out ../../bruiser/luatablegen --luaheader ../../bruiser/lua-5.3.4/src --pre ./wasmheader.txt --singlefile --outfile ../../bruiser/luatablegen/wasmtablegen.h +for filename in ../../bruiser/luatablegen/*.c; do + gcc -c $filename > /dev/null 2>&1 + if [[ $? != 0 ]]; then + echo $filename did not compile. + fi +done diff --git a/extra-tools/wasmtablegen.json b/extra-tools/wasmtablegen.json index 8647c57..38fe5a7 100644 --- a/extra-tools/wasmtablegen.json +++ b/extra-tools/wasmtablegen.json @@ -1,5 +1,51 @@ -{"jmp_s_t": {"field_name": ["type", "location", "size", "next", "next_y", "next_n", "address", "address_y", "address_n", "y", "n", "z"], - "field_type": ["JMP_T", "uint64_t", "uint8_t", "struct jmp_s_t*", "struct jmp_s_t*", "struct jmp_s_t*", "uint64_t", "uint64_t", "uint64_t", "bool", "bool", "bool"], - "methods": ["convert", "check", "push_self","push_args", "new", "gc", "tostring" ], - "lua_type": ["integer", "integer", "integer", "lightuserdata", "lightuserdata", "lightuserdata", "integer", "integer", "integer", "integer", "integer", "integer"] -}} +{ "init_expr_t": + {"field_name": ["size", "code"], "field_type": ["varuint32", "char*"], "lua_type": ["integer", "string"], "methods": ["convert", "check", "push_self", "push_args", "new"]}, + "resizable_limit_t": + {"field_name":["flags", "initial", "maximum"], "field_type": ["varuint1", "varuint32", "varuint32"], "lua_type": ["integer", "integer", "integer"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "global_type_t": + {"field_name": ["value_type", "mutability"], "field_type": ["enum value_type_t", "varuint1"], "lua_type":["integer", "integer"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "table_type_t": + {"field_name": ["element_type", "resizable_limit"],"field_type": ["varint7", "resizable_limit_t*"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "memory_type_t": + {"field_name":["resizable_limit"], "field_type":["resizable_limit_t*"], "lua_type":["lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Type_Section_Entry": + {"field_name": ["form", "param_count", "param_types", "return_count", "return_types"], "field_type":["varint7", "varuint32", "varint7*", "varuint1", "varint7*"], "lua_type":["integer", "integer", "lightuserdata", "integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args","new"]}, + "W_Type_Section": + {"field_name":["count", "entries"], "field_type": ["varuint32", "W_Type_Section_Entry**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "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"]}, + "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": + {"field_name":["count", "entries"], "field_type":["varuint32", "table_type_t**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Memory_Section": + {"field_name":["count", "entries"], "field_type":["varuint32", "memory_type_t**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Global_Entry": + {"field_name":["type", "init"], "field_type":["global_type_t*", "init_expr_t*"], "lua_type":["lightuserdata", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Global_Section": + {"field_name":["count", "globals"], "field_type":["varuint32", "W_Global_Entry**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Export_Entry": + {"field_name":["field_len", "field_str", "kind", "index"], "field_type":["varuint32", "char*", "enum external_kind_t", "varuint32"], "lua_type":["integer", "string", "integer", "integer"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Export_Section": + {"field_name":["count", "entries"], "field_type": ["varuint32", "W_Export_Entry**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Start_Section": + {"field_name":["index"], "field_type":["varuint32"], "lua_type":["integer"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Elem_Segment": + {"field_name":["index", "offset", "num_length", "elems"], "field_type":["varuint32", "init_expr_t*", "varuint32", "varuint32*"], "lua_type":["integer", "lightuserdata", "integer", "integer"], "methods": ["convert", "check", "push_self", "push_args", "new"]}, + "W_Element_Section": + {"field_name":["count", "entries"], "field_type":["varuint32", "W_Elem_Segment**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Local_Entry": + {"field_name":["count", "type"], "field_type":["varuint32", "enum value_type_t"], "lua_type":["integer", "integer"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Function_Body": + {"field_name":["body_size", "local_count", "locals", "code"], "field_type":["varuint32", "varuint32", "W_Local_Entry**", "char*"], "lua_type":["integer", "integer", "lightuserdata", "string"], "methods": ["convert", "check", "push_self", "push_args", "new"]}, + "W_Code_Section": + {"field_name":["count", "bodies"], "field_type":["varuint32", "W_Function_Body**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Data_Segment": + {"field_name":["index", "offset", "size", "data"], "field_type":["varuint32", "init_expr_t*", "varuint32", "char*"], "lua_type":["integer", "lightuserdata", "integer", "string"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "W_Data_Section": + {"field_name": ["count", "entries"], "field_type":["varuint32", "W_Data_Segment**"], "lua_type":["integer", "lightuserdata"], "methods":["convert", "check", "push_self", "push_args", "new"]}, + "Wasm_Module": + {"field_name":["type_section", "import_section", "function_section", "table_section", "memory_section", "global_section", "export_section", "start_section", "element_section", "code_section", "data_section", "W_Custom_Sections", "name"], "field_type":["W_Type_Section*", "W_Import_Section*", "W_Function_Section*", "W_Table_Section*", "W_Memory_Section*", "W_Global_Section*", "W_Export_Section*", "W_Start_Section*", "W_Element_Section*", "W_Code_Section*", "W_Data_Section*", "void**", "char*"], "lua_type":["lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata", "lightuserdata","string"], "methods":["convert", "check", "push_self", "push_args", "new"]} +} |