An example of head first il intermediate language

Source: Internet
Author: User
Tags mscorlib
In the previous post, I introduced the most basic commands and Their parameter usage methods in the Il language with a representative il base instruction and LDC commands.
Here, I will write another blog article to illustrate the syntax, basic operating mechanism and principle of the Il language in the CLR environment with an application instance.

First, find an example of an il language and find a piece of IL program from codeproject to modify it.
. Method static void main () cel managed
{
. Maxstack 2
. Entrypoint
. Locals Init (int32, temp)

Newobj instance void Donis. csharpbook. ZClass:. ctor ()
DUP
Call instance int32 Donis. csharpbook. ZClass: addfields ()
Stloc.0
Ldstr "the total is {0 }"
Ldloc.0
Box int32
Call void [mscorlib] system. Console: writeline (string, object)
Call instance int32 Donis. csharpbook. ZClass: subtractfields ()
Stloc.0
Ldstr "the difference is {0 }"
Ldloc.0
Box int32
Call void [mscorlib] system. Console: writeline (string, object)

}

In this process, the depth is different, the more things worth pondering. Next, I will analyze the code:

Let's skip the definition and start with this sentence:
Newobj instance void Donis. csharpbook. ZClass:. ctor ()
Here, a newobj command is used to allocate and generate an uninitialized object or value type, and call ctor () constructor.
Here, some friends will be depressed, why can this command allocate a value type? This is indeed the function of this command, and it is quite special.
The ECMA standard document describes this instruction as follows ):
-------------------------- Reference --------------------------------
The newobj instruction creates a new object or a new instance of a value type.CtorIs a metadata token that indicates the name, class, and signature of the constructor to call. If a constructor exactly matching the indicated name, class and signature cannot be found, missingmethodexception is thrown.

The newobj instruction allocates a new instance of the class associatedCtorAnd initializes all the fields in the new instance to 0 (of the proper type) or null as appropriate. it then callthe constructor with the given arguments along with the newly created instance. after the constructor has been called, the now initialized object reference is pushed on the stack.

From the constructor's point of view, the uninitialized object is argument 0 and the other arguments passed to newobj follow in order. value types are not usually created using newobj. they are usually allocated either as arguments or local variables, using newarr (for zero-based, one-dimen1_arrays), or as fields of objects. once allocated, they are initialized using initobj. however, the newobj instruction can be used to create a new instance of a value type on the stack, that can then be passed as an argument, stored in a local, etc.
-------------------------- Reference --------------------------------
Here, we 'd better read it to help you understand it below.
Note: "After the constructor has been called, the now initialized object reference is pushed on the stack ."
This sentence will help you understand the DUP commands and the command optimization and multi-method calling of the Il compiler.

OK, followed by the DUP command. part 3 of ECMA's CLR document explains this command very easily. There is only one sentence: duplicate the value on the top of the stack. in fact, the usage is actually quite simple.
Some of the complexity is that some of the commands of the intermediate language complier have turned around a little bit.

Next, let's explain this sentence:
Call instance int32 Donis. csharpbook. ZClass: addfields ()
Sorry, I have to quote another E Article:
-------------------------- Reference --------------------------------
The call instruction callthe method indicated by the descriptorMethod.MethodIs a metadata token (A methodref, methoddef, or methodspec) that indicates the method to call, and the number, type, and order of the arguments that have been placed on the stack to be passed to that method, as well as the calling convention to be used. the call instruction can be immediately preceded by a tail. prefix to specify that the current method state shocould be released before transferring control.
-------------------------- Reference --------------------------------
It is necessary to add that when the call method is executed again, the stack is calculated. Note that the stack is not managed heap,
Starting from the top of the stack, you should first put the referece of the instance, that is, the ZClass reference in the managed heap, and then put the parameters that need to be passed into the addfields method in sequence. in our example, parameters are not passed.

Then:
Stloc.0
Ldstr "the total is {0 }"
Ldloc.0
In the first command, pop the int32 variable returned after the call statement is executed to the first of the local variable set. load a string, and finally put the first in the local variable, that is, the top of the temp variable value stack.
Note: In the ldstr sentence, the string is saved in the managed heap. load to the stack, just a reference of heap. that is to say, this is only a reference type. understanding this helps you understand the following commands.

Next, the highlights of this article are as follows:
Here we will introduce the two commands together:
Box int32
Call void [mscorlib] system. Console: writeline (string, object)
The box command is used to perform the packing operation, which prevents the exception of the command. it copies all fields of a value-type instance from the computing stack, and then creates an object to pack the value type on the managed stack, and the reference of the new object is stored in the computing stack. in this case, the original value type is replaced by a reference type. the Unbox command executes the reverse Unbox operation.
Why use the box command? Someone may ask this question.
Here, because the following writeline method calls two parameters of the reference type, you need to convert a value type of the int32 type to the reference type of an object.
Note: The return type is void. Readers can think about it. What is the top of the stack ??

Finally, this command gives a special introduction:
Call instance int32 Donis. csharpbook. ZClass: subtractfields ()
This command has been described in detail previously. We will discuss it again here.
Some may ask why ZClass reference should be placed on the top of the stack for the call command. Why not use DUP or other commands here.
Aha, this is because in the preceding command, newobj places the ZClass reference in the stack, and DUP copies this value to the stack again, there are two references in the stack.
This also involves the compilation optimization of the Il compiler. We will not discuss this topic in depth here.

It is worth mentioning that before I write this article, I have seen many discussions on similar topics. Many people are confused about how to use DUP commands, sometimes you don't need it? Why do I need to use it later? Then various conjecture assumptions come one after another .........
Here, it is easy to solve this problem by combining the newobj command with the call command.

Well, let's get over it. I'm tired. I will provide more exciting content in the future. ^_^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.