??
Sun Guangdong 2015.5.20
Introduction to Il2cpp internal construction
Almost a year ago, unity began to talk about future scripts in unity. The new Il2cpp script backend Promise (highly-portable) brings unity to a high-performance, high-portable virtual machine. This January, Unity uses il2cpp, and the first platform to try is iOS 64-bit. Unity5 release brings another platform: WebGL. Due to the user input from our powerful community, we have shipped many version update patches for il2cpp, steadily improving its compilation and runtime.
We have no plans to stop raising il2cpp, but we think it might be a good idea to take a step back and tell you a little bit about il2cpp work from the inside out. Over the next few months, Unity intends to write about the following topics (and perhaps others) in this Il2cpp internals series of Posts:
1.The basics–toolchain and command line arguments (this post)
2.A Tour of generated code
3.Debugging Tips for generated code
4.Method calls (normal methods, virtual methods, etc.)
5.Generic Sharing Implementation
6.p/invoke wrappers for types and methods
7.Garbage Collection Integration
8.Testing Frameworks and usage
In order for this series of articles to become possible, we will discuss some of the implementation details about Il2cpp, which will certainly change in the future. Hopefully we can also provide some useful and interesting information.
What is Il2cpp?
Il2cpp's technology has two distinct parts.
· An ahead-of-time (AOT) compiler
· A Runtime Library to support the virtual machine
The AOT compiler will output a low level of C + + source code from the. NET Compiler for intermediate language (IL). The runtime Library provides services and abstraction mechanisms, such as garbage collector GC, that implement platform-independent access to threads and files and internal calls (native code directly modifies managed data structures).
AOT Compiler:
The il2cpp AOT compiler is named Il2cpp.exe. On Windows, you can find it in the Editor\data\il2cpp directory. On OSX It was the Contents/frameworks/il2cpp/build directory that was installed in unity. The Il2cpp.exe utility is a managed executable, written entirely in C #. Compile it with the. NET and mono compilers during the development of Unity's il2cpp.
The Il2cpp.exe utility accepts that the managed assembly is compiled by Mono, comes with unity and generates C + + code, which we will pass to the compiler of the platform-specific C + + compiler.
You can think of this il2cpp toolchain:
Run-Time Library:
Another part of Il2cpp technology is a run-time library to support virtual machines. We have almost completely used C + + code (it has a little bit of platform-specific assembly code, but let's remember these two between us) this library. We call the runtime Library Libil2cpp, and it is shipped as a static library linked to the executable file of the player. One of the main advantages of IL2CPP technology is this simple and portable runtime library.
You can find some clues about how the Libil2cpp code is organized, look at Libil2cpp's header files (you'll find them on Windows editor\data\playbackengines\webglsupport\ Buildtools\libraries\libil2cpp\include directory or in the Contents/frameworks/il2cpp/libil2cpp directory on OSX). For example, the interface between the C + + code generated by the Il2cpp.exe and Libil2cpp runtime is located in the code codegen/il2cpp-codegen.h header file.
A key part of the runtime is the garbage collector GC. We work here in Unity5 with the Libgc,boehm-demers-weiser garbage collector. However, Libil2cpp is designed to allow us to use other garbage collectors. For example, we are studying the integration of Microsoft GC as part of open source as CoreCLR. We will have more to say in our about garbage collector integration in this series of articles later in this article.
How is the Il2cpp.exe executed?
Let's take a look at an example. I'll be using Unity 5.0.1 on Windows, and I'll start with a new, empty project. So we had at least one user script to convert, I'll add this simple monobehaviour component to the Main Camera game Object
Let's take a look at an example. I will be on Windows, using Unity 5.0.1, will start with a new empty project. So, we have at least one user script that I will add to the main camera game object, which is the following simple Monobehaviour component:
Using Unityengine;
public class Helloworld:monobehaviour {
void Start () {
Debug.Log ("Hello, il2cpp!");
}
}
When I build the WebGL platform, I can use the Process Explorer to see the command line that Unity uses to run Il2cpp.exe:
"C:\Program files\unity\editor\data\monobleedingedge\bin\mono.exe" "C:\Program files\unity\editor\data\il2cpp/ Il2cpp.exe "--copy-level=none--enable-generic-sharing--enable-unity-event-support--output-format=compact-- extra-types.file= "C:\Program files\unity\editor\data\il2cpp\il2cpp_default_extra_types.txt" "C:\Users\Josh Peterson\documents\il2cpp Blog example\temp\stagingarea\data\managed\assembly-csharp.dll "" C:\Users\Josh Peterson\ Documents\il2cpp Blog example\temp\stagingarea\data\managed\unityengine.ui.dll "" C:\Users\Josh peterson\documents\ Il2cpp Blog Example\temp\stagingarea\data\il2cppoutput "
The command line is quite long and scary, so let's open it. First, Unity runs this executable file:
"C:\Program Files\unity\editor\data\monobleedingedge\bin\mono.exe"
Then the next parameter on the command line is the Il2cpp.exe utility itself.
"C:\Program Files\unity\editor\data\il2cpp/il2cpp.exe"
The remaining command-line arguments are passed to Il2cpp.exe, not mono.exe. Let's see them. First, unity will pass to the five tokens of Il2cpp.exe:
· –copy-level=none
· Specifies that the il2cpp.exe should not perform a special file copy of the generated C + + code.
· –enable-generic-sharing
· This is a code and binary size reduction feature. Il2cpp will share the execution of the generic method when it can.
· –enable-unity-event-support
· Special support to ensure unity events can be accessed through reflection, and the code is generated correctly.
· –output-format=compact
· The C + + code is generated in a format that requires fewer characters for the type and method names. This code is difficult to debug because the name in the IL code is not preserved, but it is often compiled faster because the C + + compiler can parse less code.
· –extra-types.file= "C:\Program files\unity\editor\data\il2cpp\il2cpp_default_extra_types.txt"
· Use the default (and empty) extra type files. Il2cpp.exe knows which generic or array type will be created at run time, but does not exist in IL code and can add this file to a unity project.
It is important to note that these command line parameters can be changed in a later release. We're not a point still there, we have a set of stable and supported Il2cpp.exe command-line arguments.
Finally, we have two files and a list of directories on the command line:
· "C:\Users\Josh peterson\documents\il2cpp Blog Example\temp\stagingarea\data\managed\assembly-csharp.dll"
· "C:\Users\Josh peterson\documents\il2cpp Blog Example\temp\stagingarea\data\managed\unityengine.ui.dll"
· "C:\Users\Josh peterson\documents\il2cpp Blog example\temp\stagingarea\data\il2cppoutput"
The Il2cpp.exe utility accepts a list of all IL assemblies that it should convert. In this case, they are my simple monobehaviour, Assembly-csharp.dll, and the GUI Assembly, UnityEngine.UI.dll. Please note that there are several conspicuous assembles missing here. Obviously, my script references UnityEngine.dll and references at least mscorlib.dll and possibly other assemblies. Where are they? In fact, these assemblies are parsed internally by Il2cpp.exe. They can be mentioned on the command line, but they are not necessary. Unity only needs to explicitly mention the root assembly (where any other assemblies that are not referenced).
The last parameter on the Il2cpp.exe command line is the directory in which the output C + + file is created. If you are curious to look at the files generated in that directory, they will be covered in the topics in the next article in this series. You knew before that although you might want to choose the WebGL "Development Player" option to generate the settings. This will remove the –output-format=compact command-line arguments and give you a better type and method name in the generated C + + code.
Please try changing the various options in WebGL or IOS Player settings. You should be able to see different command-line options passed to Il2cpp.exe, making different code-generation steps. For example, change the "Enable Exceptions" setting in WebGL Player settings to "full" value, add –emit-null-checks,–enable-stacktrace, and– Enable-array-bounds-check to il2cpp.exe command-line arguments.
What does Il2cpp do?
I would like to point out that we do not attempt to rewrite the C # standard library with Il2cpp. When you build a unity project that uses the Il2cpp script backend, all code in the C # Standard library mscorlib.dll System.dll, and so is the exact same code used for the script backend of Mono.
We rely on already well-known users and time-tested C # standard library code in Unity projects. So when we investigate bugs about il2cpp, we can be pretty confident that bugs are in the AOT compiler or runtime library, and elsewhere.
How we develop, test and ship Il2cpp
We have shipped 6 full versions and 7 patch versions (unified across versions 4.6 and 5.0) since Il2cpp version 4.6.1p5, released in January. We have corrected more than 100 bugs mentioned in the release notes.
In order for continuous improvement to occur, we internally unified the trunk branches for ship Alpha and beta versions for only one version of Il2cpp code development. Just before each version, our port il2cpp changed to a specific publishing branch, ran our tests, and verified that all our fixed bugs were corrected in that release. Our quality assurance and ongoing engineering team has done incredible work to make delivery at this rate possible. This means that our users will never be more than one weeks from the latest patch il2cpp bugs.
Our user community has proven invaluable and has submitted a number of high quality bug reports. We from our users to help constantly improve il2cpp, thanks to all the feedback we look forward to more.
More to come:
Next time, we'll delve into the code generated by Il2cpp.exe to see what your project actually looks like in the C + + compiler.
Source address of the article:
http://blogs.unity3d.com/2015/05/06/an-introduction-to-ilcpp-internals/
(vii) Unity5.0 new features------Introduction Il2cpp internal construction