diff options
Diffstat (limited to 'lib9p/protogen/c_unmarshal.py')
-rw-r--r-- | lib9p/protogen/c_unmarshal.py | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/lib9p/protogen/c_unmarshal.py b/lib9p/protogen/c_unmarshal.py index ea484b0..018d750 100644 --- a/lib9p/protogen/c_unmarshal.py +++ b/lib9p/protogen/c_unmarshal.py @@ -25,28 +25,28 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str: """ ret += cutil.macro( "#define UNMARSHAL_BYTES(ctx, data_lvalue, len)\n" - "\tdata_lvalue = (char *)&ctx->net_bytes[ctx->net_offset];\n" - "\tctx->net_offset += len;\n" + "\tdata_lvalue = (char *)&net_bytes[net_offset];\n" + "\tnet_offset += len;\n" ) ret += cutil.macro( "#define UNMARSHAL_U8LE(ctx, val_lvalue)\n" - "\tval_lvalue = ctx->net_bytes[ctx->net_offset];\n" - "\tctx->net_offset += 1;\n" + "\tval_lvalue = net_bytes[net_offset];\n" + "\tnet_offset += 1;\n" ) ret += cutil.macro( "#define UNMARSHAL_U16LE(ctx, val_lvalue)\n" - "\tval_lvalue = uint16le_decode(&ctx->net_bytes[ctx->net_offset]);\n" - "\tctx->net_offset += 2;\n" + "\tval_lvalue = uint16le_decode(&net_bytes[net_offset]);\n" + "\tnet_offset += 2;\n" ) ret += cutil.macro( "#define UNMARSHAL_U32LE(ctx, val_lvalue)\n" - "\tval_lvalue = uint32le_decode(&ctx->net_bytes[ctx->net_offset]);\n" - "\tctx->net_offset += 4;\n" + "\tval_lvalue = uint32le_decode(&net_bytes[net_offset]);\n" + "\tnet_offset += 4;\n" ) ret += cutil.macro( "#define UNMARSHAL_U64LE(ctx, val_lvalue)\n" - "\tval_lvalue = uint64le_decode(&ctx->net_bytes[ctx->net_offset]);\n" - "\tctx->net_offset += 8;\n" + "\tval_lvalue = uint64le_decode(&net_bytes[net_offset]);\n" + "\tnet_offset += 8;\n" ) class IndentLevel(typing.NamedTuple): @@ -97,17 +97,15 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str: if child.typ.static_size == 1: # SPECIAL (zerocopy) ret += f"{'\t'*indent_lvl()}UNMARSHAL_BYTES(ctx, {path.c_str('out->')[:-3]}, {cnt_path.c_str('out->')});\n" return idlutil.WalkCmd.KEEP_GOING, pop - ret += f"{'\t'*indent_lvl()}{path.c_str('out->')[:-3]} = ctx->extra;\n" - ret += f"{'\t'*indent_lvl()}ctx->extra += sizeof({path.c_str('out->')[:-3]}[0]) * {cnt_path.c_str('out->')};\n" + ret += f"{'\t'*indent_lvl()}{path.c_str('out->')[:-3]} = extra;\n" + ret += f"{'\t'*indent_lvl()}extra += sizeof({path.c_str('out->')[:-3]}[0]) * {cnt_path.c_str('out->')};\n" loopdepth = sum(1 for elem in path.elems if elem.cnt) loopvar = chr(ord("i") + loopdepth - 1) ret += f"{'\t'*indent_lvl()}for ({c9util.typename(child.cnt.typ)} {loopvar} = 0; {loopvar} < {cnt_path.c_str('out->')}; {loopvar}++) {{\n" indent_stack.append(IndentLevel(ifdef=False)) if not isinstance(child.typ, idl.Struct): if child.val: - ret += ( - f"{'\t'*indent_lvl()}ctx->net_offset += {child.typ.static_size};\n" - ) + ret += f"{'\t'*indent_lvl()}net_offset += {child.typ.static_size};\n" else: ret += f"{'\t'*indent_lvl()}UNMARSHAL_U{child.typ.static_size*8}LE(ctx, {path.c_str('out->')});\n" return idlutil.WalkCmd.KEEP_GOING, pop @@ -120,7 +118,10 @@ def gen_c_unmarshal(versions: set[str], typs: list[idl.UserType]) -> str: assert isinstance(typ, idl.Struct) ret += "\n" ret += cutil.ifdef_push(1, c9util.ver_ifdef(typ.in_versions)) - ret += f"static void unmarshal_{typ.typname}(struct _unmarshal_ctx *ctx, {c9util.typename(typ)} *out) {{\n" + ret += f"static void unmarshal_{typ.typname}([[gnu::unused]] struct lib9p_ctx *ctx, uint8_t *net_bytes, void *out_buf) {{\n" + ret += f"\t{c9util.typename(typ)} *out = out_buf;\n" + ret += "\t[[gnu::unused]] void *extra = &out[1];\n" + ret += "\tuint32_t net_offset = 0;\n" indent_stack = [IndentLevel(ifdef=True)] idlutil.walk(typ, handle) |