Skip to content
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

Improvement Meta-list v2 #228

Closed
10 tasks done
Gnimuc opened this issue Mar 30, 2019 · 10 comments
Closed
10 tasks done

Improvement Meta-list v2 #228

Gnimuc opened this issue Mar 30, 2019 · 10 comments

Comments

@Gnimuc
Copy link
Member

Gnimuc commented Mar 30, 2019

Up-for-grabs

Done

Long-shot Issues

This was referenced Mar 30, 2019
@Gnimuc Gnimuc pinned this issue Mar 30, 2019
@Gnimuc Gnimuc unpinned this issue Dec 9, 2020
@ViralBShah
Copy link
Contributor

ViralBShah commented Jan 2, 2021

Do we have support for _Generic? The discussion on discourse suggests we do not (but it was a while ago). If not, should it be added to the list above?

Came up in https://discourse.julialang.org/t/clang-jl-wrapper-generation-warnings-missing-parts/34093/7?u=viralbshah and https://twitter.com/DocSparse/status/1345479244466544641

@Gnimuc
Copy link
Member Author

Gnimuc commented Jan 3, 2021

Handing function-like macros with libclang is not easy because macros are processed by the preprocessor, there is actually no much information left in the cursors(AST). One needs to tokenize and retrieve the source code of the macro definition and then manually parse the raw string to do something meaningful.

As macro definitions may contain any valid C code, to support function-like macros in a generic way is equivalent to implement a C-to-Julia source code translator. But it looks like C11's Generic selection macros have a simple fixed pattern which, I think, could be supported in Clang.jl, I'm going to submit a PR later on(I revisited several old macro-related issues, it turned out that the current implementation for handing CLMacroDefinition needs an overhaul, this might take a bit longer).

@jpsamaroo
Copy link

Another item for the list: C bitfields. Even though according to Linus Torvalds bitfields are usually a terrible idea, they're still used in the Linux kernel's structs, which I need to be able to manipulate in BPFnative.jl.

Even though the layout of such structs are implementation-dependent, I thing we should try to implement support for these, using the host architecture as a hint as to the layout. We can use the Base.getproperty approach to generate accessors that implement the necessary bit-twiddling operations.

@Gnimuc
Copy link
Member Author

Gnimuc commented Apr 5, 2021

@jpsamaroo Could you elaborate more about how to implement the necessary bit-twiddling operations? libclang can tell the offset of the bitfield and the bitfield width.

UPDATE:

I implemented an ugly version here:

https://github.com/gridap/MiniQhull.jl/blob/db2e9f151b3bdf20ec47afa164a245678124c66f/src/LibQhull.jl#L167-L199

but I feel like there must be a more elegant implementation.

@jpsamaroo
Copy link

For a setproperty!: Based on the size of the bitfield, for each byte that the bitfield is a component of, you'd read the byte, left-shift the user-supplied value according to the bitfield's offset into that byte and the length of the bitfield, mask out (with bitwise and) the relevant portion of the byte with zeros, bitwise or the shifted supplied value with the read byte, and write the updated byte back into the struct.

@melonedo
Copy link
Contributor

For a setproperty!: Based on the size of the bitfield, for each byte that the bitfield is a component of, you'd read the byte, left-shift the user-supplied value according to the bitfield's offset into that byte and the length of the bitfield, mask out (with bitwise and) the relevant portion of the byte with zeros, bitwise or the shifted supplied value with the read byte, and write the updated byte back into the struct.

I am afraid the biggest issue is not bit manipulation but the internal layout. The C language does not define a standard layout for bit fields, unless explicit alignment directives (like pack(1) are used. For example:

struct S {
    unsigned b1 : 30;
    unsigned b2 : 4;
    unsigned b3 : 30;
};

how much space does S need? We have to figure out these implementation defined behavior to ensure bit fields are handled correctly.

@Gnimuc
Copy link
Member Author

Gnimuc commented Jul 12, 2021

That info can be queried from libclang, specifically, clang_Type_getSizeOf & clang_Type_getOffsetOf.

@melonedo
Copy link
Contributor

That info can be queried from libclang, specifically, clang_Type_getSizeOf & clang_Type_getOffsetOf.

Thanks! I didn't expect it to return offset in bits instead of bytes. However, as this information is only from Clang, other compilers might have different implementation. Anyway, having them is a good starting ground.

@Gnimuc
Copy link
Member Author

Gnimuc commented Jul 12, 2021

However, as this information is only from Clang, other compilers might have different implementation.

Clang should be ABI compatible with gcc(at least that's the goal). As this package is mainly for generating static bindings for Julia JLL packages where only mingw/gcc/clang is used in the build environment, so it's ok to make such an assumption.

BTW, this is the implementation used in Clang, which may serve as a good reference for the impl of getproperty/setproperty!.

@Gnimuc
Copy link
Member Author

Gnimuc commented Jul 12, 2021

Well... I just give up reading gcc's implementation to check whether it's compatible with Clang's or not.

@Gnimuc Gnimuc closed this as completed Sep 23, 2021
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

No branches or pull requests

4 participants