+// Buffer based
+
+static void gen_buffer_serializers(FILE *c_fp)
+{
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static void buf_write_u%d(u8 **buf, size_t *n, u%d v)\n";
+ char *fmt_s = "__attribute__((unused)) static void buf_write_s%d(u8 **buf, size_t *n, s%d v)\n";
+
+ fprintf(c_fp, fmt_u, bits, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tu%d be = htobe%d(v);\n", bits, bits);
+ fprintf(c_fp, "\tdragonnet_write_raw(buf, n, &be, sizeof be);\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, bits, bits, "");
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tbuf_write_u%d(buf, n, (u%d) v);\n", bits, bits);
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "static void buf_write_f%d(u8 **buf, size_t *n, f%d v)\n";
+
+ fprintf(c_fp, fmt_f, bits, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tbuf_write_u%d(buf, n, (u%d) v);\n", bits, bits);
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+
+ for (u8 elems = 2; elems <= 4; ++elems) {
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static void buf_write_v%du%d(u8 **buf, size_t *n, v%du%d v)\n";
+ char *fmt_s = "__attribute__((unused)) static void buf_write_v%ds%d(u8 **buf, size_t *n, v%ds%d v)\n";
+
+ fprintf(c_fp, fmt_u, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tu%d *ptr = &v.x;\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\tbuf_write_u%d(buf, n, *ptr++);\n", bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\ts%d *ptr = &v.x;\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\tbuf_write_s%d(buf, n, *ptr++);\n", bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "__attribute__((unused)) static void buf_write_v%df%d(u8 **buf, size_t *n, v%df%d v)\n";
+
+ fprintf(c_fp, fmt_f, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tf%d *ptr = &v.x;\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\tbuf_write_f%d(buf, n, *ptr++);\n", bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+ }
+
+ for (u8 elems = 2; elems <= 4; ++elems) {
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static void buf_write_aabb%du%d(u8 **buf, size_t *n, aabb%du%d v)\n";
+ char *fmt_s = "__attribute__((unused)) static void buf_write_aabb%ds%d(u8 **buf, size_t *n, aabb%ds%d v)\n";
+
+ fprintf(c_fp, fmt_u, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%du%d *ptr = &v.min;\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\tbuf_write_v%du%d(buf, n, *ptr++);\n", elems, bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%ds%d *ptr = &v.min;\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\tbuf_write_v%ds%d(buf, n, *ptr++);\n", elems, bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "__attribute__((unused)) static void buf_write_aabb%df%d(u8 **buf, size_t *n, aabb%df%d v)\n";
+
+ fprintf(c_fp, fmt_f, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%df%d *ptr = &v.min;\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\tbuf_write_v%df%d(buf, n, *ptr++);\n", elems, bits);
+ fprintf(c_fp, "\t}\n");
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+ }
+
+ fprintf(c_fp, "__attribute__((unused)) static void buf_write_string(u8 **buf, size_t *n, string v)\n");
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tdragonnet_write_raw(buf, n, v, strlen(v));\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, "__attribute__((unused)) static void buf_write_Blob(u8 **buf, size_t *n, Blob *v)\n\n");
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tbuf_write_u32(buf, n, v->siz);\n");
+ fprintf(c_fp, "\tdragonnet_write_raw(buf, n, v->data, v->siz);\n");
+ fprintf(c_fp, "}\n\n");
+}
+
+static void gen_buffer_deserializers(FILE *c_fp)
+{
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static u%d buf_read_u%d(u8 **buf, size_t *n)\n";
+ char *fmt_s = "__attribute__((unused)) static s%d buf_read_s%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_u, bits, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tu%d be;\n", bits);
+ fprintf(c_fp, "\tdragonnet_read_raw(buf, n, &be, sizeof be);\n");
+ fprintf(c_fp, "\treturn be%dtoh(be);\n", bits);
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, bits, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\treturn (s%d) buf_read_u%d(buf, n);\n", bits, bits);
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "__attribute__((unused)) static f%d buf_read_f%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_f, bits, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\treturn (f%d) buf_read_u%d(buf, n);\n", bits, bits);
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+
+ for (u8 elems = 2; elems <= 4; ++elems) {
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static v%du%d buf_read_v%du%d(u8 **buf, size_t *n)\n";
+ char *fmt_s = "__attribute__((unused)) static v%ds%d buf_read_v%ds%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_u, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%du%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\tu%d *ptr = &v.x;\n\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_u%d(buf, n);\n", bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%ds%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\ts%d *ptr = &v.x;\n\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_s%d(buf, n);\n", bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "__attribute__((unused)) static v%df%d buf_read_v%df%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_f, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tv%df%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\tf%d *ptr = &v.x;\n\n", bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < %d; ++i) {\n", elems);
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_f%d(buf, n);\n", bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+ }
+
+ for (u8 elems = 2; elems <= 4; ++elems) {
+ for (u8 bits = 8; bits <= 64; bits *= 2) {
+ char *fmt_u = "__attribute__((unused)) static aabb%du%d buf_read_aabb%du%d(u8 **buf, size_t *n)\n";
+ char *fmt_s = "__attribute__((unused)) static aabb%ds%d buf_read_aabb%ds%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_u, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\taabb%du%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\tv%du%d *ptr = &v.min;\n\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_v%du%d(buf, n);\n", elems, bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, fmt_s, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\taabb%ds%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\tv%ds%d *ptr = &v.min;\n\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_v%ds%d(buf, n);\n", elems, bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+
+ if (bits >= 32) {
+ char *fmt_f = "__attribute__((unused)) static aabb%df%d buf_read_aabb%df%d(u8 **buf, size_t *n)\n";
+
+ fprintf(c_fp, fmt_f, elems, bits, elems, bits);
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\taabb%df%d v = {0};\n", elems, bits);
+ fprintf(c_fp, "\tv%df%d *ptr = &v.min;\n\n", elems, bits);
+ fprintf(c_fp, "\tfor (u8 i = 0; i < 2; ++i) {\n");
+ fprintf(c_fp, "\t\t*ptr++ = buf_read_v%df%d(buf, n);\n", elems, bits);
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+ }
+ }
+ }
+
+ fprintf(c_fp, "__attribute__((unused)) static string buf_read_string(u8 **buf, size_t *n)\n");
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tstring v = malloc(sizeof(u16));\n\n");
+ fprintf(c_fp, "\tchar ch;\n");
+ fprintf(c_fp, "\tfor (u16 i = 0; ch != '\\0'; ++i) {\n");
+ fprintf(c_fp, "\t\tch = buf_read_s8(buf, n);\n");
+ fprintf(c_fp, "\t\tv[i] = ch;\n");
+ fprintf(c_fp, "\t}\n\n");
+ fprintf(c_fp, "\tv = realloc(v, strlen(v));\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+
+ fprintf(c_fp, "__attribute__((unused)) static Blob *buf_read_Blob(u8 **buf, size_t *n)\n\n");
+ fprintf(c_fp, "{\n");
+ fprintf(c_fp, "\tBlob *v = malloc(sizeof *v);\n");
+ fprintf(c_fp, "\tv->siz = buf_read_u32(buf, n);\n");
+ fprintf(c_fp, "\tv->data = malloc(v->siz);\n");
+ fprintf(c_fp, "\tdragonnet_read_raw(buf, n, v->data, v->siz);\n\n");
+ fprintf(c_fp, "\treturn v;\n");
+ fprintf(c_fp, "}\n\n");
+}
+