(7) New Features of Unity5.0 ------ introduce IL2CPP internal build, unity5.0il2cpp
Sun Guangdong 2015.5.20
Introduction to IL2CPP internal build
About a year ago, Unity began to talk about future scripts in Unity. The new IL2CPP script backend promise (highly-portable) brings high performance and high portable Virtual Machine to Unity. In January this year, Unity used IL2CPP. The first platform to try was iOS 64-bit. The Unity5 Release brings another platform: WebGL. Thanks to our powerful community and user input, we have shipped many version update patches for IL2CPP to steadily increase their compilation and runtime.
We didn't plan to stop improving IL2CPP, but we thought it could be a good idea. Let's move back and tell you something about IL2CPP's work from the inside out. In the next few months, UNity intends to write a post on the following topics (and maybe someone else) in this IL2CPP Internals series:
1. The basics-toolchain and command line arguments (this post)
2. A tour of generated code
3. Debugging tips for generated code
4. Method CILS (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
To make this series of articles possible, we will discuss some implementation details about IL2CPP, which will definitely change in the future. We hope we can provide some useful and interesting information.
What is IL2CPP?
The IL2CPP technology has two completely different parts.
· An ahead-of-time (AOT) compiler
· A runtime library to support the virtual machine
The AOT compiler will be an intermediate language (IL) that outputs a low level for c ++ source code from the. NET compiler. The Runtime Library provides services and abstraction mechanisms, such as garbage collector GC, to implement platform-independent access to threads and files and internal calls (Local Code directly modifies hosted 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 is the Contents/Frameworks/il2cpp/build directory installed in Unity. The Il2cpp.exe Utility is a hosted executable file that is fully written in C. Use the. NET and Mono compilers to compile Unity IL2CPP during its development.
The Il2cpp.exe utility is compiled by Mono by the hosted assembly and comes with Unity and generates c ++ code. We will pass it to the platform-specific c ++ compiler for compilation.
You can think about this IL2CPP tool chain:
Runtime Library:
Another part of IL2CPP technology is a Runtime Library to support virtual machines. We have almost completely used the c ++ code (it has a little bit of platform-specific assembly code, but let's remember between these two) this library. We call the Runtime Library libil2cpp, and it operates as a static library to link to the player of the executable file. One of the main advantages of IL2CPP technology is its simple and Portable Runtime Library.
You can find out how to organize the libil2cpp code, check the header file of libil2cpp (you will find that they are in the Editor \ Data \ PlaybackEngines \ webglsupport \ BuildTools \ Libraries \ libil2cpp \ include directory on Windows or OSX Contents/Frameworks/il2cpp on Windows /libil2cpp directory ). For example, the interface between c ++ code generated when il2cpp.exe and libil2cpp are located in the Code codegen/il2cpp-codegen.h header file.
A key part of the runtime is the garbage collector GC. Here we work in Unity5 with 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 an open source as part of CoreCLR. For more information, see our article on garbage collector integration in this series.
How is 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 that we have at least one user script to convert, I'll add this simple MonoBehaviour component to the Main Camera game object:
Let's look at an example. On Windows, I will use Unity 5.0.1 to start with a new empty project. Therefore, we have at least one user script. I will add it to the Main Camera game object, which is the simple MonoBehaviour component as follows:
Using UnityEngine;
Public class HelloWorld: MonoBehaviour {
Void Start (){
Debug. Log ("Hello, IL2CPP! ");
}
}
When I build the WebGL platform, I can use the process resource manager to view 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"
This command line is quite long and terrible, so let's open it. First, Unity runs the executable file:
"C: \ Program Files \ Unity \ Editor \ Data \ MonoBleedingEdge \ bin \ mono.exe"
The next parameter on the command line is the il2cpp.exe utility itself.
"C: \ Program Files \ Unity \ Editor \ Data \ il2cpp/il2cpp.exe"
Other command line parameters are passed to il2cpp.exe instead of mono.exe. Let's look at them. First, Unity will pass the five tags to il2cpp.exe:
·-Copy-level = None
· Specify that il2cpp.exe should not execute a special copy of the c ++ code generated.
·-Enable-generic-sharing
· This is a code and binary size reduction function. IL2CPP can be used to share execution of generic methods.
·-Enable-unity-event-support
· Special support to ensure that all Unity events can be accessed through reflection and the code is correctly generated.
·-Output-format = Compact
· Requires less character types and method name formats to generate c ++ code. This code is difficult to debug, because the name in the IL code is not retained, 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 value (and null) for additional types of files. Il2cpp.exe knows which generic or array type will be created at runtime, but does not exist in the IL code. You can add this file to a Unity project.
It is important to note that these command line parameters can be changed in later versions. We are not a point there. We have a set of stable and supported command line parameters for il2cpp.exe.
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 the IL Assembly it should convert. In this case, they are my simple MonoBehaviour, Assembly-CSharp.dll, and the GUI assembly, UnityEngine. UI. dll. Note that several conspicuous assembles are missing here. Obviously, my script references UnityEngine. dll and references at least other assemblies in mscorlib. dll. Where are they? In the upper part, il2cpp.exe parses the Assembly internally. They can be mentioned on the command line, but they are not necessary. Unity only needs to explicitly mention the root assembly (which contains any other assembly that is not referenced ).
The last parameter on the Il2cpp.exe command line is the directory in which the c ++ file is output. If you are curious, look at the files generated in this directory. They will be introduced in the topic of the next article in this series. You have known before that although you may want to select the WebGL "Development Player" option to generate the settings. This will delete the command line parameter-output-format = Compact, and give you a better type and method name to generate c ++ code.
Please try to change the various options in WebGL or iOS Player Settings. You should be able to see different command line options passed to il2cpp.exe to make different code generation steps. For example, change "Enable Exceptions" in WebGL Player Settings to "Full", add-emit-null-checks,-enable-stacktrace, and-enable-array-bounds-check to the il2cpp.exe command line parameter.
What is IL2CPP?
I would like to point out that we do not try to rewrite the C # standard library with IL2CPP. When you create a Unity project and use the IL2CPP script backend, all the code in the C # standard library mscorlib. dll System. dll, and so on are exactly the same code used for Mono script backend.
We rely on C # standard library code that has been well-known users and has been tested for a long time in the Unity project. So when we look into the bug about IL2CPP, we can be quite confident that the bug is in the AOT compiler or Runtime Library, and elsewhere.
How do we develop, test, and ship IL2CPP
Since IL2CPP 4.6.1p5 was released for the first time in March, we have shipped six complete versions and seven patch versions (cross-version 4.6 and 5.0 unification ). We have corrected more than 100 bugs mentioned in the release notes.
To enable continuous improvement, we internally use the trunk branches for the ship alpha and beta versions for only one version of IL2CPP code development. Only in each previous version, our port IL2CPP is changed to a specific release branch, run our test, and verify that all our fixed bugs are corrected in this version. Our quality assurance and continuous engineering teams have done incredible work to make delivery possible at this speed. This means that our users will never have more than a week from the latest fix IL2CPP bug.
Our user community has proved to be very valuable and has submitted many high-quality bug reports. We hope to help our users continuously improve IL2CPP. Thanks to all the feedback, we look forward to more.
More to come:
Next time, we will go deep into il2cpp.exe to see what your project actually looks like the code generated by the c ++ compiler.
Source Address of the article:
Http://blogs.unity3d.com/2015/05/06/an-introduction-to-ilcpp-internals/