Using inline intermediate languages

Source: Internet
Author: User

Using inline intermediate language (inline IL)

Inline IL is defined directly in the intermediate language (intermediate language,il) of the function body, the intermediate language F # compiler language, which is primarily for language to be added to a lower ratio operator and function. instances, additions and functions of box and not. We rarely use this feature, as F # library FSharp.Core.dll has exposed all of the features that may be required to generate intermediate languages. However, there are some extremely rare cases. cannot be completed in F #, but only in intermediate languages. Inline intermediate language is a very good choice.

Using inline intermediate languages is very easy. Just put the middle language instruction in the middle of the parentheses and the # number, (# #).

The intermediate language instruction is placed in the middle of the string, using a standard notation that can be compiled by Ilasm.exe, which must be the correct intermediate language form, or compile an error, and then pass the parameters to the intermediate language instruction, which is pressed into the intermediate language evaluation stack (IL evaluation stack) ; You must also use a standard colon. Tells the compiler what type is returned. This is put in parentheses inside wearer, and you need to explicitly specify the type of the parameters. Because the compiler does not have a way out of their type.

Let's look at a demo sample that uses inline intermediate languages.

If for some reason, we want to use the addition and subtraction operators defined in F # Base library Fslib.dll. Replace them with our custom functions.

We define two functions add and sub, and the function body is defined in the intermediate language:

Declare add functionusing the IL add instruction

Let Add (x:int) (y:int) = (#"Add"x y: int #)

Declare Sub functionusing the IL sub instruction

Let Sub (x:int) (y:int) = (#"Sub"x y: int #)

Test these functions

Let x = Add 1 1

Let y = Sub 4 2

Print the results

PRINTFN "x:%i y:%i" x y

The execution results for the demo sample are as follows:

X:2 Y:2

Program apes should be careful to use this method, because it is not important to write a meaningless program, the compiler is not likely to issue a warning.

Consider the following program, we changed the previous demo example, the "Add" directive replaced with "ret" instruction, is the return value, in this context there is no meaning. This demo sample does not compile with errors and warnings, but executes with an error.

Create a faulty Add function

Let Add (x:int) (y:int) = (#"ret"x y: int #)

Attempt to use fault function

Let x = Add 1 1

The execution results for the demo sample are as follows:

Unhandled Exception:System.InvalidProgramException:Common Language Runtime

Detected an invalid program.

At Error.add (Int32 x, Int32 y)

Attention

A tool published with the. NET SDK can help detect such errors. This tool is called Peverify.exe, a lot of other information about this tool. You can also look at Http://msdn2.microsoft.com/en-us/library/62bwd2yd (vs.80). aspx.

Using inline intermediate languages

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.