I'm in a Computer Organization class, and we're programming stuff in assembly. However, since this is a class, I'm not getting a broader sense of anything, or any real-world use. We're using Altera's Nios I开发者_StackOverflow中文版I Assembly language. The professor hasn't told us anything about which assembly languages are used in current production, and what the semantics are, or how C code compiles to ALL of the assembly languages.
Following that brief intro, am I correct in assuming that there are several assembly languages that C code compiles to? If so, what does it do to reach all of those assembly languages - parse it into a generic assembly language and then translate it from there? Or is there a separate process for each different assembly language?
There is no requirement to compile C to any specific assemblies or any assembly at all, those are left to the implementor of a compiler, and not part of the language specification. Typically, every CPU manufacturer will develop a C compiler to target their specific architecture.
There are more generic compilers like GCC and Clang though, which can target many different instruction sets.
To use Clang as an example, it is based on the Low Level Virtual Machine, which is an abstract machine with an "intermediate representation" language, LLVM IR. A back-end is written for each architecture that LLVM can target for converting LLVM IR to the instruction set, and then any compiler which compiles to LLVM IR can then target the CPUs supported by LLVM.
The compiler will decide which back-end to target at runtime based on the arguments you pass to it. The compiler typically has a default back-end which is set when building the compiler itself, through the configuration (which will probably default to the architecture you're building the compiler on).
GCC probably uses a similar approach with some intermediate representation, but I'm not sure of the details. There's also a GCC back-end which can target LLVM too.
C can be converted into any kind of assembler the compiler(s) support. Whether there's an intermediate representation is up to the compiler as well.
Note that if you're writing direct assembler inside your C code (e.g. you're writing a hardware driver in C but need to do some very low-level stuff), then you've locked your code to that particular platform's assembler language. C (and the compiler) will NOT take some code with embedded x86 assembler and translate that into (say) MIPS or PPC.
精彩评论