4 ms·
I think generating C code is great, I'm a big fan of it, but I'm lukewarm about this approach since it seems to both obscure the code more and looks rather limi
by rwmj 6y ago
I think generating C code is great, I'm a big fan of it, but I'm lukewarm about this approach since it seems to both obscure the code more and looks rather limited.
Instead we've been using OCaml to generate C files, with the rule that the generated C code must be readable (as if hand-written) and thus something you can follow by reading or in a debugger. Example of this approach here: https://github.com/libguestfs/libnbd/tree/master/generator https://github.com/libguestfs/libnbd/tree/master/generator (Unfortunately for my example, the generated code is not stored in git, but I've put a few of the generated files online here: http://oirase.annexia.org/tmp/api.c http://oirase.annexia.org/tmp/api.c http://oirase.annexia.org/tmp/libnbd.h http://oirase.annexia.org/tmp/libnbd.h http://oirase.annexia.org/tmp/methods.c http://oirase.annexia.org/tmp/methods.c)
- throwaway_pdp09 6y agoThe code is not what I would consider acceptable from a human (no offence!). For example a first sight shows repetition eg. can_zero_in_permitted_state and can_fast_zero_in_permitted_state are identical in body. I get that it's output so that repetition is in a sense ok because it's doubtless correct, but even if it's meant to be read only and never human-edited it could have that cleaned up.
- rwmj 6y agoTo a certain extent that's kind of unavoidable with generated code. The code is still generated even if we make it look hand-written. (I guess it's actually possible that a "sufficiently smart" compiler will combine those functions - I imagine C++ compilers will do this since repetition in header files / templates is very common there.)
- jey 6y agoI'm pretty sure that C++ compilers don't do this either. In C++ this is resolved by leaning on the "One Definition Rule" which authorizes the linker to assume a symbol that's defined in multiple translation units is identical in all of them and so it can arbitrarily pick one of them to keep in the output.
- Karliss 6y agoODR and linker removing multiple copies of symbol with the same name and deduplicating function with different names but identical bodies are two different techniques for two different purpose. Modern C++ toolchains will do both. Linker choosing one of the copies for inline function is means of dealing with multiple definition problem, deduplicating identical functions is size optimization. Here an example GCC deduplicating can_zero_in_permitted_state and can_fast_zero_in_permitted_state by inserting jump from one to another https://godbolt.org/z/7xYrxG https://godbolt.org/z/7xYrxG. In the example static was removed as otherwise they would be removed completely.
- throwaway_pdp09 6y agoWith respect to all, I feel this is all missing the point. Expecting a compiler to remove dups just isn't sensible when you can do it yourself so easily on the generation side. Just drop the AST/equivalent data structure into a hash table, with a quick exists check before you add the next one. Those that match, turn that code into a sub-function which all the rest call.
- rwmj 6y agoI think we can all be right. A benefit of generating the code is that this deduplication is much easier than if all the code was hand-written (especially w.r.t keeping the code updated as new API functions get added).