diff options
Diffstat (limited to 'lib9p/idl.gen')
-rwxr-xr-x | lib9p/idl.gen | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/lib9p/idl.gen b/lib9p/idl.gen index f154cb1..0b86246 100755 --- a/lib9p/idl.gen +++ b/lib9p/idl.gen @@ -40,12 +40,8 @@ def add_prefix(p: str, s: str) -> str: return p + s -def join_lines(*args: str) -> str: - return "\n".join([a.rstrip() for a in args]).rstrip() + "\n" - - -def c_macro(*args: str) -> str: - full = join_lines(*args).rstrip() +def c_macro(full: str) -> str: + full = full.rstrip() assert "\n" in full lines = [l.rstrip() for l in full.split("\n")] width = max(len(l.expandtabs(tabsize=8)) for l in lines[:-1]) @@ -73,7 +69,9 @@ def c_ver_cond(versions: set[str]) -> str: def c_typename(typ: idl.Type, parent: idl.Type | None = None) -> str: match typ: case idl.Primitive(): - if typ.value == 1 and parent and parent.name in ["d", "s"]: # SPECIAL + if ( + typ.value == 1 and parent and parent.name in ["d", "d_signed", "s"] + ): # SPECIAL return "[[gnu::nonstring]] char" return f"uint{typ.value*8}_t" case idl.Number(): @@ -279,12 +277,11 @@ enum {idprefix}version {{ case idl.Bitfield(): ret += f"typedef {c_typename(typ.prim)} {c_typename(typ)};\n" names = [ - *reversed( - [typ.bits[n] or f" {n}" for n in range(0, len(typ.bits))] - ), - "", - *[k for k in typ.names if k not in typ.bits], + typ.bits[n] or f" {n}" for n in reversed(range(0, len(typ.bits))) ] + if aliases := [k for k in typ.names if k not in typ.bits]: + names.append("") + names.extend(aliases) prefix = f"{idprefix.upper()}{typ.name.upper()}_" namewidth = max(len(add_prefix(prefix, name)) for name in names) @@ -499,7 +496,7 @@ LM_ALWAYS_INLINE static bool _validate_list(struct _validate_ctx *ctx, ret += ifdef_push(1, c_ver_ifdef(typ.in_versions)) ret += f"{inline} static bool validate_{typ.name}(struct _validate_ctx *{argfn('ctx')}) {{\n" - if typ.name == "d": # SPECIAL + if typ.name == "d" or typ.name == "d_signed": # SPECIAL # Optimize... maybe the compiler could figure out to do # this, but let's make it obvious. ret += "\tuint32_t base_offset = ctx->net_offset;\n" @@ -672,7 +669,7 @@ LM_ALWAYS_INLINE static void unmarshal_8(struct _unmarshal_ctx *ctx, uint64_t *o ret += f"out->{member.name} = ctx->extra;\n" ret += f"{prefix}ctx->extra += sizeof(out->{member.name}[0]) * out->{member.cnt.name};\n" ret += f"{prefix}for (typeof(out->{member.cnt.name}) i = 0; i < out->{member.cnt.name}; i++)\n" - if typ.name in ["d", "s"]: # SPECIAL + if typ.name in ["d", "d_signed", "s"]: # SPECIAL # Special-case is that we cast from `char` to `uint8_t`. ret += f"{prefix}\tunmarshal_{member.typ.name}(ctx, (uint8_t *)&out->{member.name}[i]);\n" else: @@ -775,7 +772,7 @@ LM_ALWAYS_INLINE static bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) ret += "({ bool err = false;\n" ret += f"\t for (typeof(val->{member.cnt.name}) i = 0; i < val->{member.cnt.name} && !err; i++)\n" ret += "\t \terr = " - if typ.name in ["d", "s"]: # SPECIAL + if typ.name in ["d", "d_signed", "s"]: # SPECIAL # Special-case is that we cast from `char` to `uint8_t`. ret += f"marshal_{member.typ.name}(ctx, (uint8_t *)&val->{member.name}[i]);\n" else: @@ -816,16 +813,16 @@ LM_ALWAYS_INLINE static bool marshal_8(struct _marshal_ctx *ctx, uint64_t *val) """ ret += c_macro( - f"#define _MSG_RECV(typ) [{idprefix.upper()}TYP_##typ/2] = {{", - f"\t\t.basesize = sizeof(struct {idprefix}msg_##typ),", - f"\t\t.validate = validate_##typ,", - f"\t\t.unmarshal = (_unmarshal_fn_t)unmarshal_##typ,", - f"\t}}", + f"#define _MSG_RECV(typ) [{idprefix.upper()}TYP_##typ/2] = {{\n" + f"\t\t.basesize = sizeof(struct {idprefix}msg_##typ),\n" + f"\t\t.validate = validate_##typ,\n" + f"\t\t.unmarshal = (_unmarshal_fn_t)unmarshal_##typ,\n" + f"\t}}\n" ) ret += c_macro( - f"#define _MSG_SEND(typ) [{idprefix.upper()}TYP_##typ/2] = {{", - f"\t\t.marshal = (_marshal_fn_t)marshal_##typ,", - f"\t}}", + f"#define _MSG_SEND(typ) [{idprefix.upper()}TYP_##typ/2] = {{\n" + f"\t\t.marshal = (_marshal_fn_t)marshal_##typ,\n" + f"\t}}\n" ) ret += "\n" ret += msg_table("Tmsg", "recv", f"struct _{idprefix}recv_tentry", (0, 0x100, 2)) |