I wanted to know in depth meaning and working of compiler, linker and loader. With reference to any language preferably c++.
=====> COMPILATION PROCESS <======
|
|----> Input is Source file(.c)
|
V
+=================+
| |
| C Preprocessor |
| |
+=================+
|
| ---> Pure C file ( comd:cc -E <file.name> )
|
V
+=================+
| |
| Lexical Analyzer|
| |
+-----------------+
| |
| Syntax Analyzer |
| |
+-----------------+
| |
| Semantic Analyze|
| |
+-----------------+
| |
| Pre Optimization|
| |
+-----------------+
| |
| Code generation |
| |
+-----------------+
| |
| Post Optimize |
| |
+=================+
|
|---> Assembly code (comd: cc -S <file.name> )
|
V
+=================+
| |
| Assembler |
| |
+=================+
|
|---> Object file (.obj) (comd: cc -c <file.name>)
|
V
+=================+
| Linker |
| and |
| loader |
+=================+
|
|---> Executable (.Exe/a.out) (com:cc <file.name> )
|
V
Executable file(a.out)
C preprocessing is the first step in the compilation. It handles:
#define
statements.#include
statements.The purpose of the unit is to convert the C source file into Pure C code file.
There are Six steps in the unit :
It combines characters in the source file, to form a "TOKEN". A token is a set of characters that does not have 'space', 'tab' and 'new line'. Therefore this unit of compilation is also called "TOKENIZER". It also removes the comments, generates symbol table and relocation table entries.
This unit check for the syntax in the code. For ex:
{
int a;
int b;
int c;
int d;
d = a + b - c * ;
}
The above code will generate the parse error because the equation is not balanced. This unit checks this internally by generating the parser tree as follows:
=
/ \
d -
/ \
+ *
/ \ / \
a b c ?
Therefore this unit is also called PARSER.
This unit checks the meaning in the statements. For ex:
{
int i;
int *p;
p = i;
-----
-----
-----
}
The above code generates the error "Assignment of incompatible type".
This unit is independent of the CPU, i.e., there are two types of optimization
This unit optimizes the code in following forms:
For ex:
{
int a = 10;
if ( a > 5 ) {
/*
...
*/
} else {
/*
...
*/
}
}
Here, the compiler knows the value of 'a' at compile time, therefore it also knows that the if condition is always true. Hence it eliminates the else part in the code.
For ex:
{
int a, b, c;
int x, y;
/*
...
*/
x = a + b;
y = a + b + c;
/*
...
*/
}
can be optimized as follows:
{
int a, b, c;
int x, y;
/*
...
*/
x = a + b;
y = x + c; // a + b is replaced by x
/*
...
*/
}
For ex:
{
int a;
for (i = 0; i < 1000; i++ ) {
/*
...
*/
a = 10;
/*
...
*/
}
}
In the above code, if 'a' is local and not used in the loop, then it can be optimized as follows:
{
int a;
a = 10;
for (i = 0; i < 1000; i++ ) {
/*
...
*/
}
}
Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.
Here the optimization is CPU dependent. Suppose if there are more than one jumps in the code then they are converted to one as:
-----
jmp:<addr1>
<addr1> jmp:<addr2>
-----
-----
The control jumps to the directly.
Then the last phase is Linking (which creates executable or library). When the executable is run, the libraries it requires are Loaded.