Delphi.net internal implementation analysis
Delphi.net internal implementation analysis (1)
Delphi.net internal implementation analysis 0. Overview Since the M $ publishing .NET, industry vendor has a large phase. However, it is undeniable that under the guarantee of M $ strong and sufficient funds. Net architecture has gradually stabilized, and begging to attack Java and other stakeholders. As a leader of the development tool, Borland, in 2002, accompanied by Delphi 7, which did not have much new Delphi 7, released the delphi.net preview. This article is based on this preview version, analyzing its internal implementation principles, helping readers transition to the .NET era on the basis of Delphi. 1. Hello Delphi! 1.1. Premiere Delphi.net before detailed analysis of Delphi.Net implementation, let us first know it from the sense: // ----------------- ------------------------ HelloWorld.dpr - Program HelloWorld; {$ apptype console} begin writeln ('Hello Delphi!'); End./ /----------------------------------------HelloWorld.dpr - looks Very familiar? That's right, this is a standard Delphi console program, and you can also use delphi.net to compile into a real .NET program E: /borland/delphi.net/demos/hello> DCCIL HelloWorld.dprborland Delphi Version 16.0CopyRight (c) 1983, 2002 Borland Software CorporationConfidential Pre-Release Version Built Nov 14 2002 17: 05: 31HELLOWORLD.DPR (8) 9 Lines, 0.28 Seconds, 4816 Bytes Code, 0 Bytes Data.e: /borland/delphi.net/ . demos / Hello> peverify HelloWorld.exeMicrosoft (R) .NET Framework PE Verifier Version 1.0.3705.0Copyright (C) Microsoft Corporation 1998-2001 All rights reserved.All Classes and Methods in HelloWorld.exe VerifiedE: /Borland/Delphi.Net / DEMOS / HELLO> HelloWorldHello Delphi! First use the IL version of DCC32, compile Delphi's Object Pascal code into the IL middle language file of the .NET architecture in Java; use .NET Framework The peverify verification program with SDK is validable, confirming that this HelloWorld.exe is indeed .NET compatible program; finally run the HelloWorld.exe program to get the result of our expectations. It looks beautiful ... isn't it? However, behind this, Borland has made a large number of underlying work, providing Delphi to the maximum source code-level compatibility of the .NET architecture, and even add new language characteristics (Delphi starts from Delphi3 / 4, grammar It is already very stable, or it is already mature).
1.2. The structure of the ILDASM tool included with .NET Framework SDK opens the HelloWorld.exe file, you can see two namespace Borland.Delphi.system and HelloWorld exists, and Manifest is the first-level information of the accessory (askEMBLY), detailed meaning Participate in another series of articles "MS.NET CLR extension PE structure analysis", there is a detailed analysis. (.NET is the name of the .NET architecture in the M $, in the environment that implements the first level, running in the CLR Common Language Runtime, similar to the virtual machine VM in Java, so the first level is no longer in. Net Like Delphi, Delphi.net automatically references the contents of the System unit, but the unit name turns borland.delphi.system. This is one of the increased language features of Object Pascal - Namespace, is used to isolate class definitions in different scope to avoid name conflicts. The HelloWorld namespace is automatically generated according to the project or unit name, and the actual code is typically saved in the class of this namespace. Let's take a look at the HelloWorld name space where the code is located .Namespace HelloWorld {.class / * 0200000b * / public auto Ansi Unit Extends [mscorlib / * 23000001 * /] system.object / * 01000001 * / {...} // End of class unit} // end of namespace HelloWorld HelloWorld namespace only define a Unit class. Delphi.net, each unit (Unit) has an automatically generated Unit class under its namespace to implement global variables, global functions and so on. Because the CLR does not have an element other than the class, all elements must be organized in form, so support global variables such as Delphi.NET/c , the language of the function, must be used with an automatically generated pseudo class package. Delphi.net generates a Unit class for each unit, using other names such as
Because our HelloWorld.DPR does not define its own class, use the function directly, so in the HelloWorld namespace, there is only one Unit class, the content is as follows .Namespace HelloWorld {.class / * 0200000b * / public auto ANSI Unit Extends [mscorlib / * 23000001 * /] System.Object / * 01000001 * / {.method / * 06000027 * / private hidebysig specialname rtspecialname static void .cctor () cil managed .method / * 06000025 * / public hidebysig specialname rtspecialname instance void .ctor () cil managed .method / * 06000026 * / public hidebysig static void $ WakeUp () cil managed .method / * 06000023 * / public static void Finalization () cil managed .method / * 06000024 * / public static void HelloWorld () cil managed} } Where .cctor () and .ctor () functions are the static / dynamic constructor of the class Unit, which is responsible for the construction of classes and object levels for Unit. For Delphi, the level of constructor is located between classes and objects. TMYCLASS = Class Public Constructor Create; End; Var Obj: Tobject; Begin Obj: = Tmyclass.create; try // ... finally freeandnil (obj); end; end; CREATE This is defined by constructor keyword as constructor, Implement a similar class function (Class Function), implicit a pointer to its metaclass (Metaclass), and a logo, the relative common member function implies a pointer to its instance, which is well known. Self. In the constructor, the similar call class function is sent to its component pointer. The function is automatically added by the compiler. In Delphi.net, .ctor () is similar to the constructor in Delphi, which is used to construct the object instance after allocating memory space. When it is called, the memory required memory has been assigned, and all member variables are initialized to 0. False or NULL. That is to say, the function of the constructor in Delphi is divided into two parts, allocation, and initialization of the memory function by the IL command newobj, calling the user code initialization object function is completed by the newobj or initobj instruction, former Assign and initialize the object in the heap, the latter is used to invoke the constructor to initialize the allocated memory allocated on the stack. And .cctor () is a modern still-constructor concept in the CLR, and its code is guaranteed by CLR to call the static member of the class before any member of this class. Therefore, there may be more than one static constructor that does not use parameters.