I'm writing a standard library for my programming language which is compiled to LLVM IR. I've also stumbled upon this interesting post Custom Programming Language ~ How to interact with the Operating System but I'm stuck on implementing interaction with the filesystem.
My language is quite similar to C, so I will use C for examples.
In C we have a struct called FILE
and we can pass it to fopen
, fclose
etc...
How can I implement this datatype myself? Do I just create a struct with similar fields and it will work?
How do popular LLVM-based languages implement this? For example Jai, Zig, Swift ...
Generally, you pick the smallest possible base platform and then define matching function and struct types for that interface only. For example, "my language only works on linux for now and my platform is those syscalls I need, and no more" or "my language only uses the exported symbols in this C library that I write myself". I've seen both. The goal is to have a small cross-language interface.
The kernel will use lots of code, the C library may use lots of other libraries or the kernel, but the cross-language interface is kept small, since cross-language calls/types are so ticklish.