summaryrefslogtreecommitdiff
path: root/lib9p/types.gen
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-28 20:50:36 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-09-28 20:54:43 -0600
commitf410026b7bc96dbb42fec3839dc5d2e41b12f4a4 (patch)
tree22e831eb47101ee4e3635fcaf2729a81a178823c /lib9p/types.gen
parentf898850c2b4fef03f0d175ec052b3725bd406496 (diff)
misc
Diffstat (limited to 'lib9p/types.gen')
-rwxr-xr-xlib9p/types.gen58
1 files changed, 42 insertions, 16 deletions
diff --git a/lib9p/types.gen b/lib9p/types.gen
index 15936e7..590e707 100755
--- a/lib9p/types.gen
+++ b/lib9p/types.gen
@@ -241,14 +241,16 @@ def gen_h(idprefix: str, versions: set[str], structs: list[Struct]) -> str:
/* versions *******************************************************************/
enum {idprefix}version {{
- {idprefix.upper()}VER_UNINITIALIZED = 0,
"""
- verwidth = max(len(v) for v in versions)
- for ver in sorted(versions):
+ fullversions = ["unknown = 0", *sorted(versions)]
+ verwidth = max(len(v) for v in fullversions)
+ for ver in fullversions:
ret += f"\t{c_verenum(idprefix, ver)},"
- ret += (" " * (verwidth - len(ver))) + ' /* "' + ver + '" */\n'
- ret += f"\t{idprefix.upper()}VER_NUM,\n"
+ ret += (" " * (verwidth - len(ver))) + ' /* "' + ver.split()[0] + '" */\n'
+ ret += f"\t{c_verenum(idprefix, 'NUM')},\n"
ret += "};\n"
+ ret += "\n"
+ ret += f"const char *{idprefix}version_str(enum {idprefix}version);\n"
ret += """
/* non-message structs ********************************************************/
@@ -287,16 +289,8 @@ enum {idprefix}version {{
ret += " " + comment
ret += "\n"
ret += "};\n"
-
ret += "\n"
- ret += f"#define {idprefix.upper()}TYPECODE_FOR_CTYPE(msg) _Generic((msg)"
- for msg in structs:
- if msg.msgid is None:
- continue
- ret += (
- f", \\\n\t\t{c_typename(idprefix, msg)}: {idprefix.upper()}TYP_{msg.name}"
- )
- ret += ")\n"
+ ret += f"const char *{idprefix}msg_type_str(enum {idprefix}msg_type);\n"
for msg in structs:
if msg.msgid is None:
@@ -333,6 +327,7 @@ enum {idprefix}version {{
def gen_c(idprefix: str, versions: set[str], structs: list[Struct]) -> str:
ret = f"""/* Generated by `{' '.join(sys.argv)}`. DO NOT EDIT! */
+#include <assert.h>
#include <stdbool.h>
#include <stddef.h> /* for size_t */
#include <inttypes.h> /* for PRI* macros */
@@ -349,6 +344,37 @@ def gen_c(idprefix: str, versions: set[str], structs: list[Struct]) -> str:
def unused(arg: str) -> str:
return f"UNUSED({arg})"
+ # strings ##################################################################
+ ret += f"""
+/* strings ********************************************************************/
+
+static const char *version_strs[{c_verenum(idprefix, 'NUM')}] = {{
+"""
+ for ver in ["unknown", *sorted(versions)]:
+ ret += f'\t[{c_verenum(idprefix, ver)}] = "{ver}",\n'
+ ret += "};\n"
+ ret += f"""
+const char *{idprefix}version_str(enum {idprefix}version ver) {{
+ assert(0 <= ver && ver < {c_verenum(idprefix, 'NUM')});
+ return version_strs[ver];
+}}
+
+static const char *msg_type_strs[0xFF] = {{
+"""
+ id2name: dict[int, str] = {}
+ for msg in structs:
+ if msg.msgid is not None:
+ id2name[msg.msgid] = msg.name
+ for n in range(0, 0x100):
+ ret += '\t[0x{:02X}] = "{}",\n'.format(n, id2name.get(n, "0x{:02X}".format(n)))
+ ret += "};\n"
+ ret += f"""
+const char *{idprefix}msg_type_str(enum {idprefix}msg_type typ) {{
+ assert(0 <= typ && typ <= 0xFF);
+ return msg_type_strs[typ];
+}}
+"""
+
# checksize_* ##############################################################
ret += """
/* checksize_* (internals of unmarshal_size()) ********************************/
@@ -591,10 +617,10 @@ static inline bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) {
.marshal = (_marshal_fn_t)marshal_##typ, \\
}}
-struct _vtable_version _{idprefix}vtables[LIB9P_VER_NUM] = {{
+struct _vtable_version _{idprefix}vtables[{c_verenum(idprefix, 'NUM')}] = {{
"""
- ret += f"\t[{idprefix.upper()}VER_UNINITIALIZED] = {{ .msgs = {{\n"
+ ret += f"\t[{c_verenum(idprefix, 'unknown')}] = {{ .msgs = {{\n"
for msg in structs:
if msg.name in ["Tversion", "Rversion", "Rerror"]: # SPECIAL
ret += f"\t\t_MSG({msg.name}),\n"