aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xmain.py71
-rwxr-xr-xrun.sh2
-rw-r--r--test/autowasm.c35
-rw-r--r--text.py1
4 files changed, 57 insertions, 52 deletions
diff --git a/main.py b/main.py
index fbf317e..b39aa83 100755
--- a/main.py
+++ b/main.py
@@ -177,12 +177,13 @@ class Argparser(object):
parser.add_argument("--static", action="store_true", help="statics reader funcs", default=False)
parser.add_argument("--verbose", action="store_true", help="verbose", default=False)
# TODO
- parser.add_argument("--forcenullterm", action="store_true", help="terminate all strings with null even if they are not null-terminated", default=False)
+ parser.add_argument("--forcenullterm", action="store_true", help="terminate all strings with null even if they are not originally null-terminated", default=False)
parser.add_argument("--strbuffersize", type=int, help="the size of the buffer for string reads", default=100)
parser.add_argument("--strbuffgrowfactor", type=float, help="the factor by which the strbuffer will grow", default=1.6)
parser.add_argument("--voidbuffersize", type=int, help="the size of the buffer for void* buffer", default=100)
parser.add_argument("--voidbuffgrowfactor", type=float, help="the factor by which the voidbuffer will grow", default=1.6)
parser.add_argument("--singlefile", action="store_true", help="the generated code will be put in a single file", default=False)
+ parser.add_argument("--singlefilename", type=str, help="name of the single file")
self.args = parser.parse_args()
def dupemake(path, main_name):
@@ -206,6 +207,26 @@ class CodeGen(object):
self.mem_size = {}
self.tree = xml.etree.ElementTree.parse(self.argparser.args.xml)
self.root = self.tree.getroot()
+ self.aggregate_source = ""
+ self.aggregate_source_h = ""
+ self.aggregate_flags = ""
+ self.read_source = ""
+ self.read_flags = ""
+ self.struct_source = ""
+ self.struct_flags = ""
+
+ def file_manager(self):
+ if self.argparser.args.singlefile:
+ name = self.argparser.args.singlefilename
+ self.read_source = self.argparser.args.outdir + "/" + name
+ self.aggregate_source = self.argparser.args.outdir + "/" + name
+ self.struct_source = self.argparser.args.outdir + "/" + name
+ else:
+ self.read_source = self.argparser.args.outdir + "/read.c"
+ self.aggregate_source = self.argparser.args.outdir + "/aggregate.c"
+ self.aggregate_source_h = self.argparser.args.outdir + "/aggregate.h"
+ self.struct_source_h = self.argparser.args.outdir + "/structs.h"
+ self.struct_source = self.argparser.args.outdir + "/structs.c"
def init_hook(self):
pass
@@ -238,12 +259,12 @@ class CodeGen(object):
def gen_reader_funcs(self):
temp_dec_list = []
- read_source = open(self.argparser.args.outdir + "/read.c", "w")
+
+ read_source = open(self.read_source, "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 "./read.h"\n')
read_source.write('#include "./structs.h"\n\n')
inline = "inline " if self.argparser.args.inline else ""
static = "static " if self.argparser.args.static else ""
@@ -319,20 +340,27 @@ 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")
+ read_source_header = open(self.argparser.args.outdir + "/read.h", "w")
+ read_source_header.write("#ifndef FT_READ_H\n#define FT_READ_H\n")
+ read_source_header.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
+ read_source_header.write('#include "./structs.h"\n')
+ for elem in self.def_elems + self.read_elems:
+ read_source_header.write(static + inline + text.c_read_elem_sig_h.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"]))
+ read_source_header.write('#ifdef __cplusplus\n}\n#endif\n')
+ read_source_header.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 + "/aggregate.h", "w")
+ void_source = open(self.aggregate_source, "w")
+ void_source_h = open(self.aggregate_source_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('#ifndef FT_AGGREGATE_H\n#define FT_AGGREGATE_H\n')
- void_source.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
+ void_source_h.write('#ifndef FT_AGGREGATE_H\n#define FT_AGGREGATE_H\n')
+ void_source_h.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
for elem in self.read_elems:
count = get_elem_count(elem)
size = get_elem_size(elem)
@@ -351,6 +379,7 @@ class CodeGen(object):
else:
void_source.write(ref_node.attrib["name"] + "* " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container;\n")
void_source.write("void malloc_all(void) {\n")
+ void_source_h.write("void malloc_all(void);\n")
count_int = int()
count_void = int()
read_count = len(self.read_elems)
@@ -408,31 +437,33 @@ class CodeGen(object):
void_source.write(elem.attrib["name"] + "_container->" + child.attrib["name"] + " = " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container" + ";\n")
def gen_aggregate_read(self):
- 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')
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
+ agg_source_h.write("void read_aggr(int _fd);\n")
for elem in self.read_elems:
agg_source.write("ft_read_" + elem.attrib["name"] + "(_fd," + elem.attrib["name"] + "_container" + ");\n")
agg_source.write("}\n")
#FIXME-not handling double pointers
def gen_release(self):
- agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a")
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
+ agg_source_h.write("void release_all(void);\n")
agg_source.write("void release_all(void) {\n")
for elem in self.read_elems:
agg_source.write("free(" + elem.attrib["name"] + "_container);\n")
agg_source.write("}\n")
def gen_return(self):
- agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a")
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
for elem in self.read_elems:
agg_source.write(elem.attrib["name"] + "* ft_ret_" + elem.attrib["name"] + "(void) {\n")
agg_source.write("return " + 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")
+ agg_source_h.write(elem.attrib["name"] + "* ft_ret_" + elem.attrib["name"] + "(void);\n")
+ agg_source_h.write('#ifdef __cplusplus\n}\n#endif\n')
+ agg_source_h.write("#endif //end of header guard\n\n")
def read_xml(self):
if self.argparser.args.xml:
@@ -475,7 +506,7 @@ class CodeGen(object):
def_header.write("}" + child.attrib["name"] + ";\n\n")
def gen_struct_header_xml(self):
- struct_source = open(get_full_path(self.argparser.args.outdir, "structs.h"), "w")
+ struct_source = open(self.struct_source, "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)
@@ -547,6 +578,7 @@ class CodeGen(object):
def run(self):
self.init()
self.init_hook()
+ self.file_manager()
#self.gen_struct_header()
self.read_xml()
self.gen_reader_funcs()
@@ -560,6 +592,7 @@ class CodeGen(object):
self.gen_release()
self.gen_return()
+
# write code here
def premain(argparser):
signal.signal(signal.SIGINT, SigHandler_SIGINT)
diff --git a/run.sh b/run.sh
index f06221f..d0e91d2 100755
--- a/run.sh
+++ b/run.sh
@@ -1,5 +1,5 @@
#!/bin/sh
cd $(dirname $0)
-"./faultreiber.py" --targetname autowasm --outdir ./test/ --structs ./test/struct.json --datetime --structsinclude ./resources/structsinclude.h --xml ./resources/wasm.xml --inline --static
+"./faultreiber.py" --targetname autowasm --outdir ./test/ --structs ./test/struct.json --datetime --structsinclude ./resources/structsinclude.h --xml ./resources/wasm.xml
"clang-format" -i ./test/read.c ./test/structs.h
#"less" ./test/structs.h
diff --git a/test/autowasm.c b/test/autowasm.c
index 02e0379..7e4150a 100644
--- a/test/autowasm.c
+++ b/test/autowasm.c
@@ -5,39 +5,10 @@
#include <stdlib.h>
#include <unistd.h>
-#include "./read.c"
+#include "./structs.h"
+#include "./read.h"
#include "./aggregate.h"
-uint64_t read_leb128_u(int _fd, int max_size) {
- uint8_t read_bytes = 0U;
- uint8_t byte = 0;
- uint64_t result = 0U;
- uint32_t shift = 0U;
- do {
- read(_fd, &byte, 1);read_bytes++;read_bytes++;
- result |= (byte & 0x7f) << shift;
- shift += 7;
- } while(((byte & 0x80) != 0) && (read_bytes < max_size));
- return result;
-}
-
-int64_t read_leb128_s(int _fd, int max_size) {
- uint8_t byte;
- uint8_t read_bytes = 0U;
- uint8_t last_byte;
- int64_t result = 0;
- uint32_t shift = 0U;
- read(_fd, &byte, 1);
- do {
- read(_fd, &byte, 1);read_bytes++;
- result |= (byte & 0x7f) << shift;
- last_byte = byte;
- shift += 7;
- } while(((byte & 0x80) != 0) && read_bytes < max_size);
- if ((last_byte & 0x40) != 0) result |= -(1 << shift);
- return result;
-}
-
#pragma weak main
int main (int argc, char** argv) {
int wasm = open("./test.wasm", O_RDONLY);
@@ -56,7 +27,7 @@ int main (int argc, char** argv) {
printf("test_byte:%08x\n", word);
lseek(wasm, 9, SEEK_SET);
- test_u = READ_VAR_UINT_32(wasm);
+ test_u = read_leb_128_u(wasm, 5);
printf("read u res is: %lu.\n", test_u);
lseek(wasm, 0, SEEK_SET);
while(read(wasm, &word, sizeof(uint32_t))) {
diff --git a/text.py b/text.py
index ea3a024..f976327 100644
--- a/text.py
+++ b/text.py
@@ -14,6 +14,7 @@ class text():
#c_read_elem_sig = "XXX ft_read_YYY(int _fd) {\n"
#c_read_elem_sig = "void ft_read_YYY(int _fd, XXX* dummyZZZ) {\n"
c_read_elem_sig = "void ft_read_YYY(int _fd, XXX* dummy) {\n"
+ c_read_elem_sig_h = "void ft_read_YYY(int _fd, XXX* dummy);\n"
c_read_elem_sig_1 = "ft_read_XXX(_fd)"
c_read_elem_sig_2 = "ft_read_XXX(_fd, YYY)"
#c_read_elem_sig_2 = "ft_read_XXX(_fd, &YYY)"