Saturday, December 17, 2011

Compilation Process (C and C++ particular)


Compilation starts with preprocessor. Preprocessor expands our source code and often writes it into intermediate file.

There are two passes in compilation:
First pass parses pre-processed code i.e. breaks code into small units. For example if statement is “A+B” then elements are ‘A’,’+’,’B’. Global optimizer is used between first and second pass to produce smaller and faster code.

In second pass, code generator walks through parse tree, which is generated in first pass, and generates either assembly language code or machine code for nodes of tree.
For assembly code assembler should run.

Ultimate result of both cases is an object module (.o or .obj) (.o file generated here is different than finally generated a.out file)

Peephole optimizer used in second pass to look for redundant assembly language statement.

The linker combines a list of object modules into an executable program that and run by OS
Linker is useful when function of one object module makes reference to function or variable in another object module. These references are resolved by linker. Linker takes help of libraries to resolve these references.

Compiler performs type checking during first pass. As this type checking occurs during compile time, this is called as static type checking.

[Courtesy : Rajesh Deshpande]

2 comments:

  1. what is the meaning of parses..??

    ReplyDelete
  2. parse means break the statement into small units. I've given example of A+B

    ReplyDelete