Learning Log
From C to the CPU: Compilers and the ISA
We use a higher-level language such as C to express our intentions, then use a compiler to translate those intentions into executable low-level operations. Layer 1: We express our intentions in a programming language and produce a C source file (using C as the example here). Layer 2: The compiler compiles the source code we provide. At this layer, it checks types, selects instructions for operations, implements conditions, arranges branches, handles data access, and so on. The compiler chooses an implementation here. A CPU may have several ways to achieve the same goal expressed in C source code, so the compiler selects an approach, and the CPU later executes the approach the compiler has provided. This layer produces an assembly representation or target machine code. Layer 3: Assembly and linking. The assembler encodes machine instructions, and the linker connects object files and the required libraries. This produces an executable file. Layer 4: The CPU executes machine instructions according to the ISA. Fetch → decode → execute → update state → continue. The results include changes to data, progress in execution, output, or other effects. From this, we can see that: 1. The programmer decides what behavior the program should implement. 2. The compiler decides how to use the target machine’s capabilities to implement that behavior. 3. The CPU executes the generated instructions at runtime. Take types as an example: types let programmers express their intentions in a higher-level language, and the compiler translates those intentions into executable low-level operations. At the lowest level, the CPU treats bits alike; they have no inherent meaning. The programmer defines types to express the rules they want the data to follow. The compiler understands the type rules, chooses an implementation the target machine can execute, and translates human instructions into machine instructions that let the CPU carry out human intentions. The CPU processes those machine instructions according to the ISA’s rules. Every concept in the upper layer (the C source file) must ultimately be expressed using ISA instructions. The compiler does that expressing, and the CPU ultimately processes those machine instructions according to the ISA’s rules. The ISA is the complete vocabulary this machine can understand, as well as the contract between the compiler and the CPU. Now let’s connect this to Week 1 of CS107E. CS107E involves bare-metal programming. We write a program on our own computer that we want to run on the Mango Pi, then use a cross-compiler on our computer to compile that source program into machine code the Mango Pi’s CPU can execute, following the ISA rules that CPU obeys. During compilation, the compiler has already chosen the implementation. The CPU then follows the ISA’s rules to carry out those instructions.
我们使用 C 语言这种更高层的语言表达意图,再用编译器把这些意图转换成底层可执行的操作。 第一层、我们使用编程语言表达我们的意图,产出 C 源代码文件(这里以 C 为例)。 第二层、编译器根据我们提供的源代码进行编译,在这一层,编译器进行类型检查、选择运算指令、实现条件、分支安排、数据访问等等。编译器在这一层选择实现方案。因为对于 C 源代码的同一个目的,CPU 可能有多个方案去实现,而编译器在这里便是选择方案,然后后面 CPU 直接根据编译器提供的方案执行。在这一层,产出汇编表示或目标机器代码。 第三层、汇编与链接,汇编器编码机器指令,链接器连接目标文件及所需的库。在这里,产出可执行文件。 第四层、CPU 按 ISA 执行机器指令。取指令 → 译码 → 执行 → 更新状态 → 继续,结果:数据改变、流程推进、产生输出或其他效果。 在这里我们可以得到: 1. 程序员决定程序要实现什么行为; 2. 编译器决定如何用目标机器的能力来实现这一行为; 3. CPU 在运行时执行生成的指令。 就以类型为例,类型,让程序员能够用更高层的语言表达意图,再用编译器把这些意图换成底层可执行的操作。 在最底层 CPU 眼里是比特全都一视同仁,没有含义。是程序员规定类型,表达出希望这些数据遵循什么规则的意图;编译器理解类型规则,选择目标机器能够执行的实现方案,将人类指令转换成让 CPU 去实现人类意图的机器指令;CPU 按 ISA 规则处理这些机器指令。 在这里,上层(C 源代码文件)的每个概念最终都要用 ISA 的指令说出来,替它说出来的是编译器,最终,CPU 按照 ISA 规则处理这些机器指令。ISA 就是这台机器能够听懂的全部词汇,也是编译器和 CPU 之间的契约。 再根据上面串联一下 CS107E 的 week1。 在 CS107E 中,是裸机编程,我们在自己电脑写了想要用芒果派去运行的程序,并在自己电脑上用交叉编译器按照芒果派上的 CPU 遵守的 ISA 规则将这个源代码程序编译为芒果派上的 CPU 能够执行的机器码。在编译的那一步,编译器已经选择好了实现方案。CPU 就会按照 ISA 规则去实现这些指令。