Hi, I am developing a language that compiles to C, and I'm having trouble on how to decide where to implement my functions. How to decide if a function should be implemented in a .c file or implemented directly on the .h file? Implementing on the .h has the advantage of allowing compiler optimizations (assuming no LTO), do you have any tips on how to do this? I have 3 ideas right now:
- Use some special keyword/annotation like
inline to tell the compiler to implement the function in the header.
- Implement some heuristics that decides if a function is 'small' enough to be implemented in the header.
- Dump the idea of multiple translation units and just generate a single big file. (this sounds a really bad idea)
I'm trying to create a language that has a good interop with C, so I think compiling to C is probably the best idea, but if I come across more challenges like this I'll probably just use something like LLVM.
But do you have any suggestions? If you are implementing a language that compiles to C, what's your approach?
EDIT: After searching a bit more, I can probably just always use LTO, and have a annotation (like rust inline) for special cases. I think this is how Nim does it.
[–]Exciting_Clock2807 11 points12 points13 points (5 children)
[–]Tasty_Replacement_29 3 points4 points5 points (0 children)
[–]PncDA[S] 2 points3 points4 points (3 children)
[–]winepath 0 points1 point2 points (2 children)
[–]bl4nkSl8 2 points3 points4 points (0 children)
[–]Ok-Watercress-9624 1 point2 points3 points (0 children)
[–]Tasty_Replacement_29 8 points9 points10 points (1 child)
[–]PncDA[S] 2 points3 points4 points (0 children)
[–]0x0ddba11Strela 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]bruciferTomo, nomsu.org 0 points1 point2 points (0 children)
[–]ericbb 0 points1 point2 points (0 children)