Saturday 21 October 2017

Compilation Process

                     |
                     |---->  Input is Source file(.c)
                     |
                     V
            +=================+
            |                 |
            | C Preprocessor  |
            |                 |
            +=================+
                     |
                     | ---> Pure C file ( comd:cc -E  )
                     |
                     V
            +=================+
            |                 |
            | Lexical Analyzer|
            |                 |
            +-----------------+
            |                 |
            | Syntax Analyzer |
            |                 |
            +-----------------+
            |                 |
            | Semantic Analyze|
            |                 |
            +-----------------+
            |                 |
            | Pre Optimization|
            |                 |
            +-----------------+
            |                 |
            | Code generation |
            |                 |
            +-----------------+
            |                 |
            | Post Optimize   |
            |                 |
            +=================+
                     |
                     |--->  Assembly code (comd: cc -S  )
                     |
                     V
            +=================+
            |                 |
            |   Assembler     |
            |                 |
            +=================+
                     |
                     |--->  Object file (.obj) (comd: cc -c )
                     |
                     V
            +=================+
            |     Linker      |
            |      and        |
            |     loader      |
            +=================+
                     |
                     |--->  Executable (.Exe/a.out) (com:cc  ) 
                     |
                     V
            Executable file(a.out)

C preprocessor :-

C preprocessing is the first step in the compilation. It handles:
  1. #define statements.
  2. #include statements.
  3. Conditional statements.
  4. Macros
The purpose of the unit is to convert the C source file into Pure C code file.

C compilation :

There are Six steps in the unit :

1) Lexical Analyzer:

2) Syntactic Analyzer:

This unit check for the syntax in the code.

3) Semantic Analyzer:

4) Pre-Optimization:

This unit is independent of the CPU, i.e., there are two types of optimization
  1. Preoptimization (CPU independent)
  2. Postoptimization (CPU dependent)
5) Code generation:
Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.
6) Post-Optimization:
Here the optimization is CPU dependent. 

Assembler    
An assembler translates assembly language programs into machine code. The output of a assembler is called an object file, which contains a combination of machine instruction as well as the data required to place these intstructions in memory.
 Linker   
Linker is  links and merges various object files together in order to make an executable file. All these files might have been compiled by separate assembler.
 Loader 
Loader is a part of operating system and is responsible for loading executable files into memory and execute them.
It calculates the size of a program (instructions and data) and create memory space for it. It initializes various registers to initiate execution.

No comments:

Post a Comment