????.net???е?CLR
?????????????????.NET????C#????????? ????C#??VB.net????????е?????
????CLR?????????????????? .NET Framework??????????????????????????????????????????????????????????????????????飬???????????????
????????????????(Type Checker)???????????Garbage Collector??????????(Exception Manager)?????????(COM Marshaler)????C#?е??????????CLR???????
????????IL
????.NET Framework??????Windows???????????????????????????????2?Windows??????????????????????Linux??????????????÷???CLS(Common Language Specification?????????淶)??.NET????????????Mono????????????????????????C#??????????????????
????C#??????????Java??????????????????????????????????Java?????C#??д????????????????C#????????????????????????? (Microsoft Intermediate Language??MSIL?????)?м????????????????????????????JIT????????Just In tIME?? JITer??????????????????????????С???????JIT??????????????????????????
????IL??????м????? ??.NET?????????????????C#??VB??F#??????????????????????????????IL??????????????????????????IL????????
????CLR??????IL????????????????α????????????JIT??IL??????б????????????????????????????????????????????????????????????????????????Щ????????????“?????”??
?????????????IL??JIT???????????CPU???????x86/IA64?????????????????
????C????????????IL?м????

//hidebysig??????????????????????????????????????????
//cil managed???????????е??????IL?????????й????????????CLR???п???????
.method private hidebysig static void Main(string[] args)cil managed
{
.entrypoint //????????ú?????????????????????й???ó?????????????????????CLR???????????????.entrypoint?????????С?
.maxstack 2 //??й?????????????????????????????????????????????淽?????????????????????????????????????????н??????????????洢??????????
.locals init (
[0] int32 num??
[1] int32 num2??
[2] int32 num3) //???????int????????????????????num??num2??num3???洢????????
L_0000: nop  //No operation?????????????κβ?????
L_0001: ldc.i4.1  //??“1”?????????????“1”????????????????
L_0002: stloc.0  //??????????????????е????????????????????0??????num??
L_0003: ldc.i4.2
L_0004: stloc.1
L_0005: ldc.i4.3
L_0006: stloc.2 //??.locals init??L_0006??????C#??????i??j??k?????
L_0007: ldloc.0  //????????λ???0????????????????i???????
L_0008: ldloc.1  //????????λ???1????????????????j???????
L_0009: add  //?????????
L_000a: ldloc.2 //????????λ???2????????????????k???????
L_000b: add  //?????????
L_000c: call void [mscorlib]System.Console::WriteLine(int32) //???????????
L_0011: nop  //No Operation
L_0012: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey() //????ReadKey????
L_0017: pop //????????????????
L_0018: ret  //return ???????
} //Main????????
???????????????????????????£? .maxstack:?????б??????????????Call Stack??????ü???λ??? .locals int(……)?????????????????????????У?Call Stack???? nop??No Operation??????κβ????? ldstr??Load String???????????????????Evaluation Stack???У?
????ldc.i4.1???????2??4????????????????????????? stloc???????????Evaluation???е?????????????????У?Call Stack???? ldloc??????????Call Stack???????λ?????????Copy????????????Evaluation Stack???У? call???????????????????????????????t??????????callvir????????????????????? ret??return ?????????
??????????????????
namespace TestConsole
{
class Program
{
[MethodImpl(MethodImplOptions.NoInlining)]
private static void SomeMethod()
{
Console.WriteLine("Hello World!");
}
static void Main(string[] args)
{
Console.WriteLine("Before JITed.");
Console.ReadLine();
SomeMethod();
Console.WriteLine("After JITed");
Console.ReadLine();
}
}
}
?????????????main????IL????
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 8
// ?????????"Before JITed"
L_0000: ldstr "Before JITed."
// ????Console.WriteLine????
L_0005: call void [mscorlib]System.Console::WriteLine(string)
// ????Console.ReadLine????
L_000a: call string [mscorlib]System.Console::ReadLine()
L_000f: pop
// ????Program.SomeMethod????
L_0010: call void TestConsole.Program::SomeMethod()
// ?????????"After JITed"
L_0015: ldstr "After JITed"
// ????Console.WriteLine????
L_001a: call void [mscorlib]System.Console::WriteLine(string)
// ????Console.ReadLine????
L_001f: call string [mscorlib]System.Console::ReadLine()
L_0024: pop
L_0025: ret
}