assemblycompiler-constructioncpu-architecturelow-level

Do modern compilers need assembler?


If I am not wrong, all compilers have an assembler(ml, as, etc..) and they use it to translate high-level code into machine language in the background(c/c++ code -> asm code -> machine code). But I wonder the modern compilers work like that too or compile the high-level source code into direct machine code? So in short, does MSVC using ml.exe or GCC using ./as in the background?


Solution

  • It varies.

    Note that if a compiler is going to support inline asm (as gcc and clang both do), then it can't very easily skip an assembler pass completely. Some stage of the process still has to know how to assemble every instruction mnemonic into machine code. In some cases, inline asm might expect to be able to interact with asm defined elsewhere in the file, and this is hard to support unless you have a pass where you truly generate the entire module into assembly, or at least into some pre-parsed asm-equivalent internal representation.

    MSVC does not support inline assembly on x64, so it would not have this issue. Indeed, this might have been part of the reason not to support it.

    So it really just comes down to a design decision. There are some benefits to compiling directly to machine code:

    and some benefits to an external assembler: