This is a creation in Article, where the information may have evolved or changed.
Here's the original.
———— Translation Divider Line ————
Compiler (2)-compile, translate, and interpret
The first part is an introduction to this series of articles.
In the second part, I'll outline some of the definitions before actually going into the actual steps of compiling.
Compile
Compiling is the process of translating code written directly from one language into another lower-level language. A C compiler will not actually output the machine code directly. Instead, the C code is translated into assembly language. The assembler compiler gets these content compiled into machine code. C # and Java are translated into bytecode. Bytecode is converted to machine code when the virtual machine is running.
It is important to understand the differences.
Compilation is often accompanied by the use of intermediate code (IR) or intermediate language. Compilation is a very common intermediate language. LLVM ir is often called LLVM ir. C also appears as an intermediate language.
Translated
In contrast, translation is a language that translates code from one language to another at the same level. For example, translate Go to Javascript.
Don't confuse it with the behavior of a language like Scala or Clojure. These languages are compiled directly into Java bytecode and then used by the JVM to execute them. Because Java bytecode is a lower-level abstraction, they are not considered to be translatable. Translated into Java before being compiled into bytecode, this can be thought of as a language that uses translation.
Translating go or Lisp to C is not a true translation, though the boundaries are not very clear. C Although the hierarchy is higher for the Assembly, it is still a low-level language.
Explain
Interpreters are usually associated with scripting languages. It is often interpreted directly rather than translating one language into another language or IR.
In some cases, this means literal translation, and the scan code then interprets execution until an error is reached. In other cases, it means scanning the entire code, validating and saving all the information to a tree-like data structure for execution.
In a sense, this type of interpreter, at run time, has to "compile" the script or source code each time, which slows it down. This is because each time it executes, it needs to complete all the steps of the compilation process, rather than just running it once.
However modern interpreters do not usually do so. If you are interested in details, you may need to study the following JIT (Just in Time) compilation. Simply put, the script is interpreted only once, just like the compiler, and then stored in some intermediate tables, which can be loaded faster. This process also allows the designer to optimize to further speed up the code.
Binary compilation
The goal of the compiler is usually to create an executable binary, or some object that it can read and execute.
But it's not just about creating a machine code so simple. Compiling is actually just the first step.
Here's a basic step for GCC and Go to compile their code:
Using GCC's C:
- C is translated into a compilation through the GNU C compiler (GCC);
- The assembly is translated into machine code through the GNU Assembler (gas);
- The GNU Linker (LD) links the machine code to the standard library to generate binary files in a specific architecture.
On x86-32 go use the Go compiler:
- Translate go into a compilation via the Go Compiler (8g);
- Translating the assembly into machine code via the Plan 9 assembler (8a);
- The Plan 9 Linker (8L) links the machine code to the standard library to generate binary files in a specific schema.
As you can see, the compiler is just the first step in the process. In this series of articles, we will only deal with the first step. Compilation and linking are beyond the scope of these articles.
Don't worry! Our compiler will generate executable binaries!
Next
Now that we have a look at some of the definitions and understand the process of getting the code to execute, you can see some more details in the third part of the compiler.