user1233963
user1233963

Reputation: 1490

Code generation

How does a compiler generate binary code to a separate file? A reference to what file(s) in the source code of GCC handle this would be of great help. What I want to know is how exactly do compilers (gcc in particular) generate machine code from ASM?

Upvotes: 2

Views: 590

Answers (2)

Sdra
Sdra

Reputation: 2347

To answer this question one would need books, not just a few lines.

The extremely short version is:

A compiler is divided in 2 parts:

  1. a Front-end that translates a specific language (like C) into a syntax-tree (generic way to represent a program)

  2. and a Back-end which translates the syntax-tree into machine specific (X86, ARM, ...) code.

There are several steps involved:

  1. Lexical analysis (converting keywords like "switch" into codes)
  2. Syntax analysis (converting from the previous step into a syntax-tree)
  3. Code optimization (modifies the syntax-tree to improve performance)
  4. Code generation (generates machine code from the syntax-tree)
  5. Linking (combines together more execution units into one binary file)

Googling around can give you more detailed info.

Upvotes: 9

Vaughn Cato
Vaughn Cato

Reputation: 64308

The assembler is a separate utility. You can find more information here: http://en.wikipedia.org/wiki/GNU_Assembler

The source code is part of the binutils package. You can find it here: ftp://ftp.gnu.org/gnu/binutils/

Upvotes: 6

Related Questions