Skip to content

feat: bitfield struct translate #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

GreyElaina
Copy link

Basically copied from https://github.com/mahkoh/repr-c/tree/master/repc/impl/src/builder.

Required to build a cross-verifier to fit existing behavior.

@Vexu
Copy link
Member

Vexu commented Jul 15, 2025

C structs cannot be translated as packed structs, instead bitfields that are part of the same host byte should be translated as a packed struct that is part of the larger struct:

struct S {
    int arr[4];
    int a: 12;
    int b: 12;
    int :0;
    int c: 12;
};
pub const struct_S = extern struct {
    arr: [4]c_int = @import("std").mem.zeroes([4]c_int),
    bitfield: packed struct(c_int) {
        a: i12 = 0,
        b: i12 = 0,
        _pad: u8 = 0,
    } = .{},
    bitfield1: packed struct(c_int) {
        c: i12 = 0,
        _pad: u20 = 0,
    } = .{},
};

@GreyElaina
Copy link
Author

GreyElaina commented Jul 15, 2025

Even just the work of creating documentation for what the C ABI for bit fields is for the various compilers would be 60%+ of the work of solving this issue.

So is it. Any helpful reference could we have? or more testcases.

@GreyElaina
Copy link
Author

ok i'm stupid, arocc already ship it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants