-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
translate-c: support for bitfields with clang frontend #20896
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
Conversation
ef81de5
to
bc1a098
Compare
bc1a098
to
86179a2
Compare
2198c80
to
e304181
Compare
@ehaas Thanks for reviewing! Now the feature is implemented, could you take a look again? |
e304181
to
9ba0ea4
Compare
Related issue: #1499 |
9ba0ea4
to
1352f03
Compare
The regression for windows has been fixed. |
1352f03
to
7add8af
Compare
Using this patch, instead of the
The generated bit looks as follows: const struct_unnamed_128 = @import("std").zig.c_translation.EmulateBitfieldStruct(extern struct {
reprioritize_blocking_assets: u1 = @import("std").mem.zeroes(u1),
push_preload: u1 = @import("std").mem.zeroes(u1),
allow_cross_origin_push: u1 = @import("std").mem.zeroes(u1),
casper: h2o_casper_conf_t = @import("std").mem.zeroes(h2o_casper_conf_t),
}, &([4]type{
c_uint,
c_uint,
c_uint,
void,
}), .{}); Which corresponds to the following code: https://github.com/h2o/h2o/blob/master/include/h2o.h#L292-L310 Not sure if this is in the scope of this PR or if it's not working as intended. Thanks! :) |
@NefixEstrada oops, I changed the protocol I will fix it soon, Thanks for pointing out! |
7add8af
to
b038eaa
Compare
Sending a new patch which fixed the problem that the translate-c emitted code does not match |
Sadly, I'm getting a different (but I think related) error:
The generated code looks as follows: pub const struct_st_h2o_casper_conf_t = extern struct {
capacity_bits: c_uint = @import("std").mem.zeroes(c_uint),
track_all_types: c_int = @import("std").mem.zeroes(c_int),
};
pub const h2o_casper_conf_t = struct_st_h2o_casper_conf_t;
const struct_unnamed_128 = @import("std").zig.c_translation.EmulateBitfieldStruct(packed struct {
reprioritize_blocking_assets: u1 = @import("std").mem.zeroes(u1),
push_preload: u1 = @import("std").mem.zeroes(u1),
allow_cross_origin_push: u1 = @import("std").mem.zeroes(u1),
casper: h2o_casper_conf_t = @import("std").mem.zeroes(h2o_casper_conf_t),
}, &([4]type{
c_uint,
c_uint,
c_uint,
void,
}), .{});
|
@NefixEstrada I have assume that the packed struct can store any type of data, clearly I was wrong there. That error is about the DSL describing the structure. It might be easy to fix. The another problem I found there is that the I will try to figure it out, maybe the structure returned from |
@NefixEstrada After hours of work, I believe this use case could not be supported. Clearly:
I have tried these ideas: struct word {
unsigned b0: 1;
unsigned b1: 2;
unsigned f0;
}; The extern struct {
bitfileds_0: packed struct { b0: u1, b1: u1, ...},
f0: c_uint,
} Two problems with this approach:
b) Flatten the fields into a single extern struct: we could not control the memory layout. c) Use I am running out of ideas. I think this use case is impossible to do at the moment. The best I can do is to emit a warning and return an opaque. |
b038eaa
to
f6eab64
Compare
The new patch rejects the nested structure use case and emits warning for structures with any bitfield. |
f6eab64
to
dfc4a9e
Compare
dfc4a9e
to
766d6ca
Compare
typedef struct ngx_file_s ngx_file_t;
struct ngx_file_s {
intptr_t (*thread_handler)(ngx_file_t *file);
unsigned directio:1;
}; pub const struct_ngx_file_s = @import("std").zig.c_translation.EmulateBitfieldStruct(struct {
thread_handler: ?*const fn (?*ngx_file_t) callconv(.C) isize = @import("std").mem.zeroes(?*const fn (?*ngx_file_t) callconv(.C) isize),
directio: u1 = @import("std").mem.zeroes(u1),
}, &([2]type{
void,
c_uint,
}), .{});
pub const ngx_file_t = struct_ngx_file_s; ^ I tried, c-translate nginx headers.
|
766d6ca
to
028a2c6
Compare
@patryk4815 This problem is about the analysis (it even happens in the structures don't use C bit fields). |
7192578
to
dda172a
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
@thislight I see. It is issue with Maybe c-translate should check if this is 'same' struct referencing itself, and replace it with pub const ngx_file_s = EmulateBitfieldStruct(struct {
thread_handler: ?fn (?*ngx_file_t)
});
pub const ngx_file_t = ngx_file_s; -> pub const ngx_file_s = EmulateBitfieldStruct(struct {
thread_handler: ?fn (?*@This())
});
pub const ngx_file_t = ngx_file_s; |
@patryk4815 It may not achieve the goal. When generating the final type, the replaced Our best chance may be to rework the DSL syntax for // c_translation.zig
pub const Bitfiled = struct {
name: []const u8,
@"type": type,
backing_integer: ?type = null,
is_pointer: bool = false,
};
pub fn EmulateBitfieldStruct(fields: []const Bitfield) type {
// ...
} Consider this case: pub const ngx_file_s = EmulateBitfieldStruct(&.{
.{ .name = "thread_handler", .@"type" = ?*const fn (?*ngx_file_t) isize, .is_pointer = true },
.{ .name = "directio", .@"type" = u1, .backing_integer = c_uint },
});
pub const ngx_file_t = ngx_file_s; When My concern is, even we know the compiler resolves types lazily, I don't know exactly if it's lazy in this case. |
dda172a
to
1dc3025
Compare
I have done the rewrite, seems that the approach I suggested does not solve this problem. cc @patryk4815 . |
Probably this topic unfortunately #6709 (comment) |
1dc3025
to
9842898
Compare
This PR is going to be closed because it's blocked by some key problems. I have learnt much from this PR and I hope this experience can also help others on this topic. Thanks all who contributes! |
The Goals
Make translate-c translating bitfields with clang frontend.
Implementation Details
The implementation includes two parts:
a)
std.zig.c_translation.EmulateBitfieldStruct
, a comptime function do the heavy work, transform structs for C ABI;b) translate-c, emits code to call
EmulateBitfieldStruct
.Discovered Limitation
extern struct
in a bitfield struct is unsupported. translate-c: support for bitfields with clang frontend #20896 (comment)