4 ms·
C source-to-source compiler enhancement from within
- acqq 6y agoThe approach presented introduces #pragmas which practically contain the code written in the language which extends the C language, as in the example: double A[] = { #pragma CMOD amend foreach ANIMAL:N = goose dog cat [${ANIMAL }] = 2*${N}, #pragma CMOD done }; Personally, I don't like such an approach, as the convention through the decades was that #pragmas don't contain the code but just the "directives", that is, that it should be able to "read" the code without the #pragmas, which is here impossible (one would completely miss the "foreach" and "goose", "dog" and "cat"). As the idea is to have a preprocessing step before the result is fed to the compiler, a little better preprocessor could be written which would remove the requirement of having such pragmas. Note that such kind of a bit stronger preprocessor was how the initial forms of C++ were implemented (1983) 37 years ago: https://en.wikipedia.org/wiki/Cfront https://en.wikipedia.org/wiki/Cfront "The preprocessor did not understand all of the language and much of the code was written via translations." Even older preprocessor (1974), 46 years ago, also implemented in Bell Labs, was Ratfor: https://en.wikipedia.org/wiki/Ratfor https://en.wikipedia.org/wiki/Ratfor
- amir734jj 6y ago#pragmas don't contain the code but just the "directives" Agreed. I am using pragmas in my Ph.D. thesis. I think pragma should contain some information for the compiler and the compiler can apply binding phase for things referenced in pragma. No more. Having code in pragma is too much.
- rwmj 6y agoI 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.