Why do C++ compilers translate source code into Assembly before creating machine code? [duplicate]

a 夏天 提交于 2020-07-23 06:48:06

问题


I have started learning C++, and I have learned that a compiler turns source code from a program into machine code through compilation.

However, I've learned that C++ compilers actually translate the source code into Assembly as an interim step before translating the Assembly code into machine code. What is the purpose of this step?


回答1:


Why don`t they translate it directly into the machine code?

First of all: There is no need to write an intermediate assembly language representation. Every compiler vendor is free to emit machine code directly.

But there are a lot of good reasons to "write" an intermediate assembly and pass it to an assembler to generate the final executable file. Important is, that there is no need to really write a file to some kind of media, but the output can directly piped to the assembler itself.

Some of the reasons why vendors are using intermediate assembly language:

  • The assembler is already available and "knows" how to generate some executable file formats ( elf for example ).

  • Some tasks can be postponed until assembly level is reached. Resolving jump targets for example. This is possible because the intermediate assembly is often not only 1:1 representation but some kind of "macro-assembler" which can do a lot more than simply creating bits from mnomics.

  • the assembler level is followed by executing the linker. This must also be done if a compiler directly wants to create executable file formats. A lot of duplicated jobs if this must be coded again. As an example all the relocation of before "unknown addresses" must be done on the way to an executable file. Simply use the assembler/linker and the job is done.

  • The intermediate assembly is always useful for debugging purpose. So there is a more or less hard requirement to be able to do this intermediate step, even if it can be omitted if no debug output is requested from the user.

I believe there are are lot more...

The bad side is:

  • "writing" a text representation and parsing the program from the text takes longer as directly passing the information to the linker.



回答2:


Usually, compilers invoke the assembler (and the linker, or the archiver) on your behalf unless you ask it to do otherwise, because it is convenient.

But separating the distinct steps is useful because it allows you to swap the assembler (and linker and archiver) for another if you so desire or need to. And conversely, this assembler may potentially be used with other compilers.

The separation is also useful because assemblers already existed before the compiler did. By using a pre-existing assembler, there is no need to re-implement the machine code translation. This is still potentially relevant because occasionally there will be a need to boot-strap a new CPU architecture.



来源:https://stackoverflow.com/questions/62701333/why-do-c-compilers-translate-source-code-into-assembly-before-creating-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!