This article brings you an in-depth analysis (pictures and texts) of C# execution principles. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Why can Unity3D run C#? What is the relationship between C# and Mono? What is the relationship between Mono and .Net Framework? Let’s talk about this topic in depth!
Start with the principle of compilation
Introduce the compiler in one sentence: The compiler converts the source code (source language) written in a certain programming language into another programming language (target language) equivalent form of program. Usually we convert a certain high-level language (such as C, C, C#, Java) into a low-level language (assembly language, machine language).
The compiler works in the form of a pipeline and is divided into several stages: source code → lexical analysis → syntax analysis → semantic analysis → target code → link → executable file. Modern compilers will be more complex, and more processing will be added in the middle, such as preprocessors, intermediate code generation, code optimization, etc.
What is a virtual machine?
Virtual machine (VM), simply understood, can execute specific A program of instructions. In order to execute instructions, some supporting facilities are also needed, such as registers, stacks, etc. A virtual machine can be very complex, so complex that it simulates real computer hardware, or it can be so simple that it can only do addition, subtraction, multiplication and division.
In the field of compilers, virtual machines usually execute a language called intermediate code. The intermediate code is converted from a high-level language to Take Java as an example. What is generated after Java compilation is not an executable file, but a ByteCode (bytecode) file, which contains the code from Java Source code is converted into equivalent code in bytecode form. The Java Virtual Machine (JVM) is responsible for executing this file.
There are two ways for a virtual machine to execute intermediate code: interpretation execution and JIT (just in time compilation). Interpretation and execution means executing each instruction one by one, JIT The intermediate code is first compiled into machine code when it starts running, and then the machine code is executed. Since the intermediate code is executed, different virtual machines implemented on different platforms can execute the same intermediate code, thus achieving cross-platform.
int run(context* ctx, code* c) { for (cmd in c->cmds) { switch (cmd.type) { case ADD: // todo add break; case SUB: // todo subtract break; // ... } } return 0; }
To summarize, the virtual machine itself is not cross-platform, but the language is cross-platform. For developers, they only need to care about the development language, and do not need to care about how the virtual machine is implemented. This is also The reason why Java can be cross-platform is the same as C#. By extension, in theory any language can be cross-platform, as long as supporting facilities such as a compiler or virtual machine are implemented on the corresponding platform.
What is C# and what is IL?
C# is a high-level object-oriented programming language launched by Microsoft based on the .NET framework. Microsoft released this language in 2000, hoping to use this language to replace Java. For more detailed introduction, please refer to the C# Wiki.
C# is a language. Microsoft has customized a language specification for it, providing a complete one-stop service from development, compilation, deployment and execution. The latest specification will be released every once in a while. , adding some new language features. From a grammatical level, C# is a very complete language that is very comfortable to write.
C# Similar to Java, C# will be compiled into an intermediate language (CIL, Common Intermediate Language, also called MSIL). CIL It is also a high-level language, and the virtual machine running CIL is called CLR (Common Language Runtime). Usually we put C#, CIL, CLR, plus a set of basic class libraries provided by Microsoft called .Net Framework.
#C# was born to conquer the universe, but unfortunately, due to Microsoft's closure, this goal has not been achieved. Of course C# is still doing well now, revitalized because of games, because of Unity3D, because of Mono.
.Net Framework vs Mono
Mono is a cross-platform implementation of the .Net Framework. Mono has done a great thing, reimplementing the CLR on all supported platforms and reimplementing the basic class library provided by the .Net Framework.
Above, the work of Compile Time can actually directly use Microsoft’s existing achievements. As long as the CLR of Runtime is implemented on other platforms, this work The quantity is not only large, but also compatibility needs to be ensured. It is a very huge project. Mono has done it. Congratulations!
C
in Unity3DUnity3D embeds a Mono virtual machine. From the above, we can know that when a virtual machine of a certain platform is implemented, the language can run on that platform. Therefore, strictly speaking, Unity3D uses the Mono virtual machine. Run the C# generated IL code compiled by the compiler.
Unity3D uses C# as the development language by default. In addition, it also supports JS and BOO, because Unity3D has developed corresponding compilers to compile JS and BOO into IL.
Summary
C# Under Windows, the IL code is generated through Microsoft's C# compiler and runs in the CLR.
C# On platforms other than Windows, the IL code is generated through the Mono compiler and runs in the Mono virtual machine. You can also directly run the compiled IL code (compiled through any platform ).
Theoretically, if you create a language, implement a compiler for a certain platform, and then implement a virtual machine that conforms to the language specifications for all platforms, your language can run on any platform. .
The above is the detailed content of In-depth analysis of C# execution principles (pictures and texts). For more information, please follow other related articles on the PHP Chinese website!