什么是编译器、链接器、加载器? [英] What is compiler, linker, loader?

查看:35
本文介绍了什么是编译器、链接器、加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想深入了解编译器、链接器和加载器的含义和工作原理.参考任何语言,最好是 c++.

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 预处理器:-

C 预处理是编译的第一步.它处理:

C preprocessor :-

C preprocessing is the first step in the compilation. It handles:

  1. #define 语句.
  2. #include 语句.
  3. 条件语句.

本单元的目的是将C源文件转换为纯C代码文件.

The purpose of the unit is to convert the C source file into Pure C code file.

单元中有六个步骤:

它将源文件中的字符组合起来,形成一个TOKEN".一个token 是一组没有空格"、制表符"和换行符"的字符.因此这个编译单元也被称为TOKENIZER".它还删除注释,生成符号表和重定位表条目.

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;
    -----
    -----
    -----
}

以上代码生成错误Assignment of incompatible type".

The above code generates the error "Assignment of incompatible type".

这个单元独立于CPU,即有两种优化方式

This unit is independent of the CPU, i.e., there are two types of optimization

  1. 预优化(与 CPU 无关)
  2. 后优化(取决于 CPU)

本单元将代码优化成以下形式:

This unit optimizes the code in following forms:

  • I) 死代码消除
  • 二)子代码消除
  • III) 循环优化

例如:

{
    int a = 10;
    if ( a > 5 ) {
        /*
        ...
        */
    } else {
       /*
       ...
       */
    }
}

这里,编译器在编译时就知道 'a' 的值,因此它也知道 if 条件始终为真.因此它消除了其他部分代码.

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.

例如:

{
    int a, b, c;
    int x, y;

    /*
    ...
    */

    x = a + b;
    y = a + b + c;

    /*
    ...
    */
}

可以优化如下:

{
    int a, b, c;
    int x, y;

    /*
     ...
    */

    x = a + b;
    y = x + c;      // a + b is replaced by x

    /*
     ...
    */
}

三)循环优化:

例如:

{
    int a;
    for (i = 0; i < 1000; i++ ) {

    /*
     ...
    */

    a = 10;

    /*
     ...
    */
    }
}

在上面的代码中,如果'a'是本地的并且没有在循环中使用,那么它可以是优化如下:

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++ ) {
        /*
        ...
        */
    }
}

5) 代码生成:

在这里,编译器生成汇编代码,以便更多常用的变量存放在寄存器中.

5) Code generation:

Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.

这里的优化取决于 CPU.假设如果有多个在代码中跳转,然后将它们转换为:

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>
            -----
            -----

控件直接跳转到.

最后一个阶段是链接(创建可执行文件或库).当可执行文件运行时,它需要的库被加载.

Then the last phase is Linking (which creates executable or library). When the executable is run, the libraries it requires are Loaded.

这篇关于什么是编译器、链接器、加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆