summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib9p/protogen/h.py190
1 files changed, 99 insertions, 91 deletions
diff --git a/lib9p/protogen/h.py b/lib9p/protogen/h.py
index 7785ca1..c84ff02 100644
--- a/lib9p/protogen/h.py
+++ b/lib9p/protogen/h.py
@@ -268,99 +268,11 @@ enum {c9util.ident('version')} {{
match typ:
case idl.Number():
- ret += f"typedef {c9util.typename(typ.prim)} {c9util.typename(typ)};\n"
- prefix = f"{c9util.IDENT(typ.typname)}_"
- namewidth = max(len(name) for name in typ.vals)
- for name, val in typ.vals.items():
- ret += f"#define {prefix}{name:<{namewidth}} (({c9util.typename(typ)})UINT{typ.static_size*8}_C({val}))\n"
+ ret += gen_number(typ)
case idl.Bitfield():
- ret += f"typedef {c9util.typename(typ.prim)} {c9util.typename(typ)};\n"
-
- def bitname(val: idl.Bit | idl.BitAlias) -> str:
- s = val.bitname
- match val:
- case idl.Bit(cat=idl.BitCat.RESERVED):
- s = "_RESERVED_" + s
- case idl.Bit(cat=idl.BitCat.SUBFIELD):
- assert isinstance(typ, idl.Bitfield)
- n = sum(
- 1
- for b in typ.bits[: val.num]
- if b.cat == idl.BitCat.SUBFIELD
- and b.bitname == val.bitname
- )
- s = f"_{s}_{n}"
- case idl.Bit(cat=idl.BitCat.UNUSED):
- return ""
- return c9util.Ident(c9util.add_prefix(typ.typname.upper() + "_", s))
-
- namewidth = max(
- len(bitname(val)) for val in [*typ.bits, *typ.names.values()]
- )
-
- ret += "\n"
- for bit in reversed(typ.bits):
- vers = bit.in_versions
- if bit.cat == idl.BitCat.UNUSED:
- vers = typ.in_versions
- ret += cutil.ifdef_push(2, c9util.ver_ifdef(vers))
-
- # It is important all of the `beg` strings have
- # the same length.
- end = ""
- match bit.cat:
- case (
- idl.BitCat.USED | idl.BitCat.RESERVED | idl.BitCat.SUBFIELD
- ):
- if cutil.ifdef_leaf_is_noop():
- beg = "#define "
- else:
- beg = "# define"
- case idl.BitCat.UNUSED:
- beg = "/* unused"
- end = " */"
-
- c_name = bitname(bit)
- c_val = f"1<<{bit.num}"
- ret += f"{beg} {c_name:<{namewidth}} (({c9util.typename(typ)})({c_val})){end}\n"
- if aliases := [
- alias
- for alias in typ.names.values()
- if isinstance(alias, idl.BitAlias)
- ]:
- ret += "\n"
-
- for alias in aliases:
- ret += cutil.ifdef_push(2, c9util.ver_ifdef(alias.in_versions))
-
- end = ""
- if cutil.ifdef_leaf_is_noop():
- beg = "#define "
- else:
- beg = "# define"
-
- c_name = bitname(alias)
- c_val = alias.val
- ret += f"{beg} {c_name:<{namewidth}} (({c9util.typename(typ)})({c_val})){end}\n"
- ret += cutil.ifdef_pop(1)
- del bitname
+ ret += gen_bitfield(typ)
case idl.Struct(): # and idl.Message():
- ret += c9util.typename(typ) + " {"
- if not typ.members:
- ret += "};\n"
- continue
- ret += "\n"
-
- typewidth = max(len(c9util.typename(m.typ, m)) for m in typ.members)
-
- for member in typ.members:
- if member.val:
- continue
- ret += cutil.ifdef_push(2, c9util.ver_ifdef(member.in_versions))
- ret += f"\t{c9util.typename(member.typ, member):<{typewidth}} {'*' if member.cnt else ' '}{member.membname};\n"
- ret += cutil.ifdef_pop(1)
- ret += "};\n"
- del typ
+ ret += gen_struct(typ)
ret += cutil.ifdef_pop(0)
ret += """
@@ -445,3 +357,99 @@ enum {c9util.ident('version')} {{
ret += "};\n"
return ret
+
+
+def gen_number(typ: idl.Number) -> str:
+ ret = f"typedef {c9util.typename(typ.prim)} {c9util.typename(typ)};\n"
+ prefix = f"{c9util.IDENT(typ.typname)}_"
+ namewidth = max(len(name) for name in typ.vals)
+ for name, val in typ.vals.items():
+ ret += f"#define {prefix}{name:<{namewidth}} (({c9util.typename(typ)})UINT{typ.static_size*8}_C({val}))\n"
+ return ret
+
+
+def gen_bitfield(typ: idl.Bitfield) -> str:
+ ret = f"typedef {c9util.typename(typ.prim)} {c9util.typename(typ)};\n"
+
+ def bitname(val: idl.Bit | idl.BitAlias) -> str:
+ s = val.bitname
+ match val:
+ case idl.Bit(cat=idl.BitCat.RESERVED):
+ s = "_RESERVED_" + s
+ case idl.Bit(cat=idl.BitCat.SUBFIELD):
+ assert isinstance(typ, idl.Bitfield)
+ n = sum(
+ 1
+ for b in typ.bits[: val.num]
+ if b.cat == idl.BitCat.SUBFIELD and b.bitname == val.bitname
+ )
+ s = f"_{s}_{n}"
+ case idl.Bit(cat=idl.BitCat.UNUSED):
+ return ""
+ return c9util.Ident(c9util.add_prefix(typ.typname.upper() + "_", s))
+
+ namewidth = max(len(bitname(val)) for val in [*typ.bits, *typ.names.values()])
+
+ ret += "\n"
+ for bit in reversed(typ.bits):
+ vers = bit.in_versions
+ if bit.cat == idl.BitCat.UNUSED:
+ vers = typ.in_versions
+ ret += cutil.ifdef_push(2, c9util.ver_ifdef(vers))
+
+ # It is important all of the `beg` strings have
+ # the same length.
+ end = ""
+ match bit.cat:
+ case idl.BitCat.USED | idl.BitCat.RESERVED | idl.BitCat.SUBFIELD:
+ if cutil.ifdef_leaf_is_noop():
+ beg = "#define "
+ else:
+ beg = "# define"
+ case idl.BitCat.UNUSED:
+ beg = "/* unused"
+ end = " */"
+
+ c_name = bitname(bit)
+ c_val = f"1<<{bit.num}"
+ ret += (
+ f"{beg} {c_name:<{namewidth}} (({c9util.typename(typ)})({c_val})){end}\n"
+ )
+ if aliases := [
+ alias for alias in typ.names.values() if isinstance(alias, idl.BitAlias)
+ ]:
+ ret += "\n"
+
+ for alias in aliases:
+ ret += cutil.ifdef_push(2, c9util.ver_ifdef(alias.in_versions))
+
+ end = ""
+ if cutil.ifdef_leaf_is_noop():
+ beg = "#define "
+ else:
+ beg = "# define"
+
+ c_name = bitname(alias)
+ c_val = alias.val
+ ret += f"{beg} {c_name:<{namewidth}} (({c9util.typename(typ)})({c_val})){end}\n"
+ ret += cutil.ifdef_pop(1)
+ return ret
+
+
+def gen_struct(typ: idl.Struct) -> str: # and idl.Message
+ ret = c9util.typename(typ) + " {"
+ if not typ.members:
+ ret += "};\n"
+ return ret
+ ret += "\n"
+
+ typewidth = max(len(c9util.typename(m.typ, m)) for m in typ.members)
+
+ for member in typ.members:
+ if member.val:
+ continue
+ ret += cutil.ifdef_push(2, c9util.ver_ifdef(member.in_versions))
+ ret += f"\t{c9util.typename(member.typ, member):<{typewidth}} {'*' if member.cnt else ' '}{member.membname};\n"
+ ret += cutil.ifdef_pop(1)
+ ret += "};\n"
+ return ret