Compiling and Executing Code in .Net

basiliskcanoeSoftware and s/w Development

Nov 2, 2013 (3 years and 11 months ago)

92 views

Compiling and Executing Code in .Net

Microsoft Intermediate Language and
Common Language Runtime

COSC4301 Assignment 3, Part 1

2

Compiled vs. Interpreted


Compiled (e.g. C/C++, FORTRAN and Pascal)


Executes faster


Occupies less main memory (RAM)


Interpreted (e.g. LISP, Perl and Python)


Provides debugging support


Provides run
-
time error and security checking


Allows platform independence


Produces smaller code

COSC4301 Assignment 3, Part 1

3

.Net Approach


Compiles code to the Microsoft Intermediate
Language (MSIL, CIL or IL)


Just
-
In
-
Time (JIT) Compilation


Common Language Runtime (CLR)
compiles the IL code to the host machine’s
language and then executes it


Compiled code is cached

COSC4301 Assignment 3, Part 1

4

Advantages of the .Net Approach


Architecture independence


Implementations of the CLR are available for
PCs, PDAs and mobile phones


Language independence and interoperability


.Net supports a plethora of languages such as
APL, C++, C#, COBOL, Eiffel, FORTRAN,
Java, Pascal, Perl, Python and Visual Basic


Source code can be compiled to IL and libraries
can be accessed by programmes compiled from
a different language

COSC4301 Assignment 3, Part 1

5

Similarity to Java


Intermediate compilation to provide
architecture independence


.Net compiles source code into IL code,
while Java compiles into Byte Code


.Net executes on the CLR (
mscorsvr.dll or
mscorwks.dll
), while Java executes on the
Java Virtual Machine (
jvm.dll
)

COSC4301 Assignment 3, Part 1

6

Common Language Runtime (CLR)

Common Type System (CTS)

Security

Execution

Support

IL Compiler

Garbage Collection

Class Loader

COSC4301 Assignment 3, Part 1

7

Common Language Runtime (CLR)
continued


Reliability


Runtime checking (e.g. array index
bounds, external library availability)


Memory management (e.g. garbage
collection)

COSC4301 Assignment 3, Part 1

8

Common Language Runtime (CLR)
continued (2)


Security


Code verification security


Programmer runtime support


Debugging


Exception handling

COSC4301 Assignment 3, Part 1

9

Microsoft Intermediate Language
(MSIL, CIL or IL)


Takes advantage of the .Net Common Language
Infrastructure (CLI)


Common Type System (CTS)


All primitives based on a common type


Common Language Specification (CLS)


Details how modules from one language can
be accessed, used and extended by modules
from other languages

COSC4301 Assignment 3, Part 1

10

Microsoft Intermediate Language
(MSIL, CIL or IL) continued


Simplified development


Common libraries


Simplified deployment


Common runtime environment


Smaller executables

COSC4301 Assignment 3, Part 1

11

C# Example

using System;




// C# source code

class MyClass {


public static void Main(String[] args) {



Console.WriteLine( myMethod( 14, 88 ) );


}


public static int myMethod(int a, int b) {



return a + b + 10;


}

}

COSC4301 Assignment 3, Part 1

12

J# Example

class MyClass {



// J# source code


public static void main(String[] args) {



System.out.println( myMethod( 14, 88 ) );


}


public static int myMethod(int a, int b) {



return a + b + 10;


}

}

COSC4301 Assignment 3, Part 1

13

C#: MyClass.cs

G:
\
COSC4301>csc /out:MyClass_cs.exe MyClass.cs

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4

for Microsoft (R) .NET Framework version 1.1.4322

Copyright (C) Microsoft Corporation 2001
-
2002. All rights
reserved.


G:
\
COSC4301>MyClass_cs

112

COSC4301 Assignment 3, Part 1

14

J#: MyClass.java

G:
\
COSC4301>vjc /out:MyClass_js.exe MyClass.java

Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0

for Microsoft (R) .NET Framework version 1.1.4322

Copyright (C) Microsoft Corp 2000
-
2002. All rights reserved.


G:
\
COSC4301>MyClass_js

112

COSC4301 Assignment 3, Part 1

15

Comparison of MSIL Assemblies: C#

.method public hidebysig static int32 myMethod(int32 a, int32 b) cil managed

{


// Code size

11 (0xb)


.maxstack 2


.locals init (int32 V_0)


IL_0000:

ldarg.0


IL_0001:

ldarg.1


IL_0002:

add


IL_0003:

ldc.i4.s


10


IL_0005:

add


IL_0006:

stloc.0


IL_0007:

br.s


IL_0009


IL_0009:

ldloc.0


IL_000a:

ret

} // end of method MyClass::myMethod

COSC4301 Assignment 3, Part 1

16

Comparison of MSIL Assemblies: J#

.method public hidebysig static int32 myMethod(int32 a, int32 b) cil managed

{


// Code size

7 (0x7)


.maxstack 2


IL_0000:

ldarg.0


IL_0001:

ldarg.1


IL_0002:

add


IL_0003:

ldc.i4.s


10


IL_0005:

add


IL_0006:

ret

} // end of method MyClass::myMethod

COSC4301 Assignment 3, Part 1

17

References


msdn.microsoft.com/msdnmag/issues/01/03/clr/default.aspx


msdn.microsoft.com/netframework/technologyinfo/overview/


www.codeproject.com/dotnet/clr.asp


Partition I Architecture (Microsoft Visual Studio .NET 2003 SDK v1.1
Tool Developers Guide)


Partition III CIL (Microsoft Visual Studio .NET 2003 SDK v1.1 Tool
Developers Guide)


Microsoft .NET (core reference) by Jeff

Prosise, ISBN: 0735613761


.NET Framework Essentials, 3rd Edition by Hoang

Lam, Thuan L.

Thai,
ISBN: 0596005059