Stages of Compilation

There are four stages of compilation in C Programming, they are Pre processing, compiling, assembling and linking these are explained as below

Pre processing stage includes


a. Comment Section Removal
b. Macro Substitution with their Values
c. Merging Header Files and Code

/*

This is a multi line comment section

*/

// C Program to print Hello World !

#include <stdio.h>

int main() {
    printf("%s", "Hello World !");
    return 0;
}


# gcc -E hello.c
.....
.....
.....
.....

# 1 "/usr/include/bits/sys_errlist.h" 1 3 4
# 26 "/usr/include/bits/sys_errlist.h" 3 4
extern int sys_nerr;
extern const char *const sys_errlist[];
# 788 "/usr/include/stdio.h" 2 3 4

extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;

extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
# 806 "/usr/include/stdio.h" 3 4
extern FILE *popen (const char *__command, const char *__modes) ;

extern int pclose (FILE *__stream);

extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
# 846 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));

extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;

extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 864 "/usr/include/stdio.h" 3 4
extern int __uflow (FILE *);
extern int __overflow (FILE *, int);
# 879 "/usr/include/stdio.h" 3 4

# 10 "hello.c" 2

# 11 "hello.c"
int main() {
    printf("%s", "Hello World !");
    return 0;
}


This stage removes the comment section, substitutes the define macro in to their values. The source code including the required headers files are retrieved and merged together to form as single (.i extension) file.
Some part of High Level Optimization line loop elimination and reordering(Strength Reduction), function and macro in lining, removing redundant variables and variable assignments (Constant Propagation), tail recursion elimination, jump elimination etc are done

Compiling


a. Check syntax Errors
b. Translate the Code to Assemble Level Language

gcc -S hello.c

#
# gcc -S hello.c
# ls
hello.c  hello.s
# cat hello.s
    .file    "hello.c"
    .text
    .section    .rodata
.LC0:
    .string    "Hello World !"
.LC1:
    .string    "%s"
    .text
    .globl    main
    .type    main, @function
main:
.LFB0:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $.LC0, %esi
    movl    $.LC1, %edi
    movl    $0, %eax
    call    printf
    movl    $0, %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size    main, .-main
    .ident    "GCC: (GNU) 8.3.1 20191121 (Red Hat 8.3.1-5)"
    .section    .note.GNU-stack,"",@progbits
#


This stage of Compiling include the checking of syntax Errors and then the compiler processes the standalone (.i extension) file and produces an assembly source code .a file targeted to the configured platform
Some part of Low Level Optimization is done here like register setting, instruction scheduling, branch prediction, condition, predictive and speculative execution are done.

Assembler


a. Converts Assembly Level Language to low level machine code

#
# gcc -c hello.c
# ls
hello.c  hello.o
#


The assembly level language code (.s extention) is compiled to binary code (.o extention) object file is generated.

Linker


a. Link the Binary Object File to Static Libraries or Other Object Files

#
# gcc -o hello hello.c
# ./hello
Hello World !
#


The object file is linked against the requested libraries and possibly against other object files

Post a Comment

0 Comments