r/ProgrammingLanguages • u/celestabesta • Jun 03 '26
Any guide to establishing C-Interop?
At the moment my language is able to call C functions perfectly fine, with the exception of not supporting structs. I currently use an LLVM backend, and was surprised to discover that it does not handle the C struct ABI.
I now know that is something I'd have to manually implement, but it seems daunting. Can anyone give some advice, or maybe recommend another backend which does this natively?
Edit: For context my language is a very young aot compiled c-like language.
15
Upvotes
9
u/tsanderdev Jun 03 '26
C struct layout is mostly the same everywhere. For calls you need to look at the platform's ABI, like sysv on linux.
5
u/Germisstuck CrabStar Jun 03 '26
I don't know if this is applicable as I am writing my own backend, but looking at the libffi source for each ABI and translating to my backend was very helpful, got Windows and SysV working in a short amount of time
9
u/R-O-B-I-N Jun 03 '26
Everyone does it differently (except when they don't (except when they do)) so you usually need to study what a C compiler outputs for each implementation pn each CPU architecture.
Then you would build support for specific commonly used configurations like MSVC+Windows+AMD64, LLVM+OSX+AArch64, or GCC+Linux+AMD64.
The godbolt compiler explorer is useful for looking at what each compiler outputs for each OS on each CPU arch.
1
u/EggplantExtra4946 Jun 03 '26
You mean that it would be dauting to write the entire compiler backend? I can't see how you could implement the calling convention without also implementing the entire code generation phase and the generation of executables.
1
u/Tasty_Replacement_29 Bau Jun 04 '26
> I currently use an LLVM backend
For my language, I transpile to C. It is much simpler than using LLVM directly. I see there are advantages to use LLVM directly, for example you have better access to SIMD, or very advanced optimizations etc.
I wonder, what are your reasons to use LLVM?
3
u/yorickpeterse Inko Jun 03 '26
This article may prove useful when it comes to the struct ABI, specifically when returning structs.
2
u/awoocent Jun 03 '26
It's not so terrible to implement, just annoying and kind of error prone. Especially if you're building on top of LLVM you might have less to worry about? In terms of specifically which registers get picked. You could just add some instructions at the top of every function unpacking the arguments from the ABI and assume the optimizer will see through it (it probably should?).
2
u/un_virus_SDF Jun 04 '26
The best solution (for structs and type maping) is to have a c file that output the sizes of the default types (or know them, they are platform specific) and read the alignement rules in the standard
2
u/LordKisuke Jun 06 '26
honestly just throw a small struct into godbolt with clang and look at the IR it spits out, that's basically the spec lol
the tldr is: split the struct into 8-byte chunks, pass each as a separate i64 or double depending on the type. over 16 bytes? just pass a pointer instead. it's just a preprocessing step before your call emission, you're not rewriting the whole backend
3
u/TheChief275 Jun 03 '26
Another thing people rarely mention is that you are basically forced to have some implementation of C's shitty ass built-in integer types: short, int, long, long long, which are basically int_least16_t, int_fast16_t, int_least32_t, int_least64_t, that is, who the fuck knows what size these all are? It completely differs per architecture and compiler.
The best solution I have found is to either query a C compiler, or to use gccjit as your backend, which again you can query for the size and alignment of types. If you don't need this information at compile time (for some form of dependent types or meta-programming), then there is another option of simply emitting C code.
Of course this is all required anyway if you choose to have some form of register/word/pointer-sized integer, and of course for the size of pointers in general, but these can be more easily obtained and are only specific to architectures.
7
u/[deleted] Jun 03 '26
[removed] — view removed comment