Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a C compiler compile to generic assembly?

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 II 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?

like image 550
Caleb Jares Avatar asked Sep 26 '11 16:09

Caleb Jares


1 Answers

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.

like image 81
Mark H Avatar answered Oct 05 '22 17:10

Mark H