White [10] series to create a compiler compiler back-end technology

We have already discussed in the compiler  front-end technology  . Its focus is to allow the compiler to be able to read the program. Unstructured text code, after subsequent processing of the front end, it becomes structured information Token, AST and semantic attribute, the symbol table and the like. Based on this information, we can achieve a simple  script interpreter  .

The compiler back-end problem to be solved: You are given a computer, how do you build a program that can run, and then let the program can correctly and efficiently run on a computer?


Running mechanism

Our concern is two underlying hardware:

  • The CPU : it can receive data and machine instructions, and calculations. Inside it there is a register, cache and an operation unit, and the full use of the capability register cache system will be greatly enhanced.
  • Memory : We want to save in memory the compiled code and data, but also to design a mechanism to make the most efficient use of the program memory.

Generate code

The end result of the back-end compiler, is to generate the object code . If the goal is to run directly on the computer, like the C language program as the object code that refers to the assembly code . And if the goal is to run a Java Virtual Machine, that this object code refers to JVM bytecode .

Write compilation with many different high-level languages, one of which is to care about CPU and memory such specific hardware. For example, we need to understand the difference between different CPU instruction set, also need to know CPU is 64-bit or 32-bit, there are several registers that each instruction can be used for anything, and so on. But this has led to the problem is that each language for each different hardware, different assembly code to be generated.

Therefore, in order to reduce the workload of the rear end, to improve the reusability of software, it is necessary to introduce  intermediate code (Intermediate Representation, IR) mechanism, which is independent of the specific hardware, a code format. The distal end of each first language can be translated into IR, and then translated into the assembler code from the IR different hardware architectures. If there are n front-end language, m back-end architecture, would need to do m * n translation program, now we need only one of the m + n. This greatly reduces the overall workload.

Overall, the variety of languages ​​we can translate into intermediate code, and then for each target architecture, through a program to translate intermediate code into the corresponding assembly code on it.


Code Analysis and Optimization

Generate the correct code can be executed relatively simple, low efficiency of such code execution, because the code generated by the direct translation is often simple enough, such will generate a lot of temporary variables, the number of instructions is greater. Because first of all take care of the translation program correctness is difficult taking into account the adequacy of optimization, which is on the one hand. On the other hand, due to the high-level language programmer's own limitations and programming practice, the code can lead to less than optimal, we can not give full play to the computer's performance. So we have to do to optimize the code. In comparing the various language programmer time, we will compare their performance differences. The poor performance of a language, it will affect its use and popularity.

Optimization work is divided into " machine-independent optimization " and " machine-dependent optimizations " two.

  • Machine independent optimization is performed based on IR. It is by analyzing the code, instead of the original code in a more efficient code.
  • Machine dependent optimizations, is hardware dependent features.
    • Register optimization. For variables frequently accessed, preferably in registers, and try to maximize the use of registers, to prevent some empty.
    • Make full use of the cache.
    • Parallelism. Modern computers have multiple cores, parallel computing.
    • assembly line. In dealing with different CPU instruction, the need to wait for a period of time is not the same, in fact, can also execute other instructions while waiting for some instructions to finish in.
    • Instruction selection. Sometimes, CPU perform one function, there are multiple commands to choose from. And for a specific demand, the use instruction A instruction B may be higher than the efficiency of times.
    • Other optimization. For example, it can do for a dedicated AI and GPU chip optimized to provide AI computing power, and so on.

to sum up

Back-end technology, is to generate a running program, and support him properly on the machine and efficient operation. It is divided into two steps. First generate intermediate code, in order to facilitate the expansion and improve the software to take, reduce workload. On the other hand, is the premise of guaranteeing the program to run properly can optimize the code. Optimization is divided into two division manner, independently of the machine is optimized for the intermediate code, machine dependent optimizations will be characteristic for the machine hardware.


Reference: "Geek Time - compiler theory of beauty"

Published 62 original articles · won praise 34 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41960890/article/details/105306220