1. 从源码到可执行件: 4 步
hello.c
$ gcc hello.c
$ ./a.out
gcc 命令 是1层 wrap, 据不同参数 (-E / -S / -c 与 -o)
调 cc1 (预编译编译器) / as (汇编器) / ld (链接器)
(1) 预编译
$ gcc -E hello.c -o hello.i
5 规则
————————————————————————————————————————————————————
#define | 删 + 宏定义 展开
————————————————————————————————————————————————————
条件编译指令 #ifdef 等 | 处理
————————————————————————————————————————————————————
#include 预编译指令 | 递归 (多层包含) 插入 文件
————————————————————————————————————————————————————
#pragma | 保留 -> 给 Compiler 用
————————————————————————————————————————————————————
注释 | 删
————————————————————————————————————————————————————
(2) 编译
$ gcc -S hello.i -o hello.s
$ ... cc1 hello.c
// 预编译 + 编译
$ gcc -S hello.c -o hello.s
6 步
词法分析(扫描) / 语法分析 / 语义分析 /
源代码优化 / 代码生成 / 目标代码优化
(3) 汇编
$ gcc -c hello.s -o hello.o
$ as hello.s -o hello.o
// 预编译 + 编译 + 汇编
$ gcc -c hello.c -o hello.c
(4) 链接
ld
2. 编译器 做了什么
array[index] = (index + 4) * (2 + 6)
(1) 词法分析
有限状态机 算法
源码 -> 分割 -> `记号 (Token)`: 关键字 / 标识符 / 字面量 (数字, 字符串) / 特殊符号 ( + = )
工具: lex
(2) 语法分析
上下文无关语法
=> `语法树`: `表达式 (Expression)` 作 节点
工具: yacc
(3) 静态(编译期可确定)语义(含类型信息)分析
1) 匹配 声明与类型
2) 类型转换
(4) 源码优化
[1] 2 + 6 编译期可确定 -> 优化 为 8
[2] 去掉不必要的 变量 t1, t3: 据 三地址码 x = y op z
t1 = 2 + 6;
t2 = index + 4;
t3 = t2 * t1;
array[index] = t3;
|
|/
t2 = index + 4;
t2 = t2 * 8;
array[index] = t2;
(5) 生成汇编代码
(6) 优化汇编代码
1) 位移 替 乘法
2) 更合适的寻址
3) 删多余指令
3. 链接器 比 编译器 早出现
(1) 机器指令 + 绝对地址
-> code 可能变动, 每次执行, 靠 coder
去 重定位
————————————————————————————————
指令 addr | content
————————————————————————————————
0 | 0001 0100 第 1 条指令: 跳转指令 + 绝对地址 4
. |
4 | 1000 0111 第 5 条指令: 程序 foo 的开始
————————————————————————————————
(2) 汇编器 + 符号 (Symbol): 让 汇编器 去 重定位
第1条汇编指令: jmp foo
(3) 模块: 一组 var + func -> 目标文件 .obj
1) 模块间 通信 = 模块间 符号的引用: 函数调用 + 变量访问
2) 模块间拼合
`定义/引用 符号的模块: 多/少 了一块区域` -> 两者 `拼接 : 链接`
4 静态链接 —— 模块拼装
重定位:
修正 指令对 other 模块 符号(func/var)地址
的 引用
(两者思路类似)
模块 main.o 调 func.o 中 foo()
compiler 生成 main.o 时, 不知道 `call foo 指令` 中 `foo 地址`
1) 先 搁置
2) link 时, 链接器 去 foo.o 查 foo 地址 -> 修正到 main.o
![](https://img.haomeiwen.com/i20172887/a6cf4e72e7f5f57b.jpg)
![](https://img.haomeiwen.com/i20172887/7da9dbd1ca54eec7.jpg)
![](https://img.haomeiwen.com/i20172887/f076787d0a5953ac.jpg)
![](https://img.haomeiwen.com/i20172887/28f5e8e6547d4a18.jpg)
![](https://img.haomeiwen.com/i20172887/52f553adbac3a995.jpg)
![](https://img.haomeiwen.com/i20172887/e0a0f5180a81e859.jpg)
网友评论