Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 812
user1233963 Avatar asked Feb 17 '26 13:02

user1233963


2 Answers

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.

like image 185
Sdra Avatar answered Feb 19 '26 03:02

Sdra


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/

like image 28
Vaughn Cato Avatar answered Feb 19 '26 04:02

Vaughn Cato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!