I\'m willing to compile certain language to C or C++ for some speedups. My idea
ID: 655026 • Letter: I
Question
I'm willing to compile certain language to C or C++ for some speedups. My idea was, though, to generate readable code and use structures such std::vector and similar wherever I can, expecting CLANG to do the trick of optimizing it as much as it can. I notice that some languages that compile to C or C++ are nothing like that, though. Looking on the code generated by Chicken Scheme, for example, is scary.
This, for example, is a snippet generated from (fn (a b) (sqrt (* a a) (* b b))):
float f_(float a,float b){
float b_=a*a;
float c_=b*b;
float d_=b_+c_;
float e_=sqrt(d_);
return e_;
};
Is there a particular reason for those languages to do that? Is my approach wrong in some aspect?
Explanation / Answer
These compilers don't go out of their way to produce unreadable code, but they don't care too much about the code being readable either. Correctness and performance is more important. The snippet you showed is actually fairly simple, it only introduces a lot of intermediate variables. This might be an artifact of a three-address-IR used internally in the compiler, where large expressions are broken into a sequence of simple calculations. Such a format is good for compilers, as it's simpler to manipulate correctly. It's just not how humans write code.
There is nothing fundamentally wrong with your apparoach. But you have to ask yourself:
Is your language similar enough to C++ to be mapped to standard C++ constructs (without severe complications)? In all cases? Really?
Is the effort to make the generated code readable worthwhile? I hope you're not trying to generate code that could be maintained and extended in place of the original source code. That's a pipe dream in my experience. So the only remaining benefit of readable output code is slightly easier inspection of the generated code, which is not something most people do.
In most cases, the source language is rather different from C and C++, and the generated code is only ever read by compiler writers and optimization experts, who can deal with artificial code.