On Wed, 2021-04-21 at 08:23 +0100, Jonathan Wakely via Gcc-help wrote: > On Wed, 21 Apr 2021, 08:04 Tadeus Prastowo wrote: > > > On Wed, Apr 21, 2021 at 6:33 AM Peng Yu <pengyu.ut@xxxxxxxxx> wrote: > > > > > > > > > Since it is *link*-time optimization, I suppose it is ld who does > > > the > > > actual work? > > > > Yes. > > > > > Well, sort of. > > The linker uses a plug-in to invoke the compiler again. The linker > doesn't > do the optimization itself, because it doesn't know how. > > So the gcc driver runs collect2 which runs ld which uses the lto- > plugin.so > to run the compiler again, then ld finishes linking. > > The optimization happens *during* linking, but it's still done by the > compiler. > > > > > > If it were implemented in a shell language, it would be easier to > > > see > > > what is going on below the surface, instead of relying on the -v > > > option. > > > > Yes. > > > > > (Probably it is easier to maintain shell scripts rather than C > > > code? Also, just for calling others programs, it seems shell is a > > > better implementation language than C.) > > > > Maybe. GCC is never designed to be a tool for teaching or learning. It's a compiler, for compiling programs in real-life. Basically -v is only intended to debug GCC itself. For learning, as I insisted you need a textbook, instead of doing some experiment on a complex system and trying to parse something out of the result. The "features" or "difficulties" in a real system often makes learning hard. It's just like you can't learn classical mechanics by doing some experiments on your car, and you shouldn't say "it would be better to have a transparent hood for the car". Unfortunately, while every physics teacher knows he/she shouldn't use a car to teach mechanics, many CS101 teachers keep telling the student "if you don't understand something, just do a experiment". IMO it's pure laziness. And the GCC driver is not only calling other programs. For example, the driver handles -march=. For -march=native the driver checks local CPU feature and rewrite a value like -march=icelake-client. It also sets flags like -mavx2 for different -march= values. The driver also parses the spec file (I think there is no way to parse a complex file with shell). Even if using a shell script is possible, it will be very difficult and may introduce many bugs. Some of those bugs may be extremely dangerous: I remember several bugs occured in shell scripts, invoking "rm -rf /*" for some unexpected corner case. You can read libtool code, for example. Libtool is basically a compatibility wrapper for gcc/ld/ar. I'm sure everyone will feel disgusting by reading 12562 lines of shell code in it. -- Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University