Overview of the .NET

Содержание

Слайд 2

Overview

Compilation and interpretation
Virtual machines
Simple C# program
CIL, ildasm util
CLR
.NET Framework
JIT, NGEN
CLS
.NET 6
Compare C

Overview Compilation and interpretation Virtual machines Simple C# program CIL, ildasm util
++, C # (.NET) method call performance

Слайд 3

Compilation (Ahead-of-Time) and interpretation

A program written in a high level language can

Compilation (Ahead-of-Time) and interpretation A program written in a high level language
run in two ways
Compiled into a program in the native machine language and then run on the target machine.
Directly interpreted and the execution is simulated within an interpreter.

Слайд 4

Compilation and interpretation
How is a C++ program executed on linprog?
cl try.cpp ?

Compilation and interpretation How is a C++ program executed on linprog? cl
compiling the program into machine code
Try.exe ? running the machine code
How is a JavaScript program executed?
cscript.exe try.js
The program just runs, no compilation phase
The program cscript is the software environment that understands JavaScript language. The program try.js is executed (interpreted) within the environment.
In general, which approach is more efficient?

Слайд 5

Compilation and interpretation

In general, which approach is more efficient?
A[i][j] = 1;

Compilation:
mov eax,

Compilation and interpretation In general, which approach is more efficient? A[i][j] =
DWORD PTR _i$[ebp]
imul eax, 20
lea ecx, DWORD PTR _A$[ebp+eax]
mov edx, DWORD PTR _j$[ebp]
mov DWORD PTR [ecx+edx*4], 1

Interpretation:
create a software environment that understand the language
put 1 in the array entry A[i][j];

Слайд 6

Compilation and interpretation

In general, which approach is more efficient?
A[i][j] = 1;

Compilation:
mov eax,

Compilation and interpretation In general, which approach is more efficient? A[i][j] =
DWORD PTR _i$[ebp]
imul eax, 20
lea ecx, DWORD PTR _A$[ebp+eax]
mov edx, DWORD PTR _j$[ebp]
mov DWORD PTR [ecx+edx*4], 1

Interpretation:
create a software environment that understand the language
put 1 in the array entry A[i][j];
For the machine to put 1 in the array entry A[i][j], that code sequence still needs to be executed.
Most interpreter does a little more than the barebone “real work.”

Compilation is always more efficient!!
Interpretation provides more functionality. E.g. for debugging
One can modify the value of a variable during execution.

Слайд 7

Compilers versus Interpreters

Compilers “try to be as smart as possible” to fix

Compilers versus Interpreters Compilers “try to be as smart as possible” to
decisions that can be taken at compile time to avoid to generate code that makes this decision at run time
Type checking at compile time vs. runtime
Static allocation
Static linking
Code optimization
Compilation leads to better performance in general
Allocation of variables without variable lookup at run time
Aggressive code optimization to exploit hardware features

Слайд 8

Compilers versus Interpreters

Benefit of interpretation?
Interpretation facilitates interactive debugging and testing
Interpretation leads to

Compilers versus Interpreters Benefit of interpretation? Interpretation facilitates interactive debugging and testing
better diagnostics of a programming problem
Procedures can be invoked from command line by a user
Variable values can be inspected and modified by a user
Some programming languages cannot be purely compiled into machine code alone
Some languages allow programs to rewrite/add code to the code base dynamically
Some languages allow programs to translate data to code for execution (interpretation)
JavaScript Eval() function var x = 10; var y = 20; var a = eval("x * y") + " "; var b = eval("2 + 2") + " "; var c = eval("x + 17") + " "; var res = a + b + c; The result of res will be: "200 4 27 "

Слайд 9

Virtual Machines (for programming language)

A virtual machine executes an instruction stream in

Virtual Machines (for programming language) A virtual machine executes an instruction stream
software
Adopted by Pascal, Java, Smalltalk-80, C#, functional and logic languages, and some scripting languages
Pascal compilers generate P-code that can be interpreted or compiled into object code
Java compilers generate bytecode that is interpreted by the Java virtual machine (JVM)
C#, VB.NET compilers generate CIL (Common Intermediate Language) that is interpreted by the CLR virtual machine
The CLR may translate CIL into machine code by just-in-time (JIT) compilation

Слайд 10

Compilation and Execution on Virtual Machines

Compiler generates intermediate program
Virtual machine interprets the

Compilation and Execution on Virtual Machines Compiler generates intermediate program Virtual machine
intermediate program

Virtual Machine

Compiler

Source
Program

Intermediate
Program

Input

Output

Run on VM

Compile on X

Run on X, Y, Z, …

Слайд 11

Two Steps Compilation Process

Compilation is done in two steps:
At compile time: compile

Two Steps Compilation Process Compilation is done in two steps: At compile
each language (C#,VB.Net, C++, etc) to Common Intermediate Language (CIL)
At runtime: Common Language Runtime (CLR) uses a Just In Time (JIT) compiler to compile the CIL code to the native code for the device used

Run Time

Compile Time

Слайд 12

Simple C# program

namespace SimpleConsoleApplication
{
class Program
{
static void Main(string[]

Simple C# program namespace SimpleConsoleApplication { class Program { static void Main(string[]
args)
{
int init =10;
int rate =5;
int pos = init + rate * 60;
System.Console.WriteLine(pos);
}
}
}

Слайд 13

C# -> CIL Using ildasm

.method private hidebysig static void Main(string[] args) cil

C# -> CIL Using ildasm .method private hidebysig static void Main(string[] args)
managed
{ .entrypoint
.maxstack 3
.locals init ([0] int32 'init', [1] int32 rate, [2] int32 pos)
ldc.i4.s 10
stloc.0
ldc.i4.5
stloc.1
ldloc.0
ldloc.1
ldc.i4.s 60
mul
add
stloc.2
ldloc.2
call void [mscorlib]System.Console::WriteLine(int32)
ret }

Слайд 14

.maxstack 3
.locals init ([0] int32 'init', [1] int32 rate, [2]

.maxstack 3 .locals init ([0] int32 'init', [1] int32 rate, [2] int32 pos) ldc.i4.s 10
int32 pos)

ldc.i4.s 10

Слайд 16

ldc.i4.5

ldc.i4.5

Слайд 18

ldloc.0
ldloc.1
.1

ldloc.0 ldloc.1 .1

Слайд 19

ldc.i4.s 60

ldc.i4.s 60

Слайд 24

call void mscorlib]System.Console::WriteLine(int32)
ret

call void mscorlib]System.Console::WriteLine(int32) ret

Слайд 25

Common Intermediate Language (CIL)

Much like the native languages of devices.
CIL was originally

Common Intermediate Language (CIL) Much like the native languages of devices. CIL
known as Microsoft Intermediate Language (MSIL).
CIL is a CPU- and platform-independent instruction set.
It can be executed in any environment supporting the .NET framework

Слайд 26

Common Language Runtime (CLR)

The Common Language Runtime (CLR) manages the execution of

Common Language Runtime (CLR) The Common Language Runtime (CLR) manages the execution
code.
CLR uses Just-In-Time (JIT) compiler to compile the CIL code to the native code for device used.
Through the runtime compilation process CIL code is verified for safety during runtime, providing better security and reliability than natively compiled binaries.
Native image generator compilation (NGEN) can be used to produces a native binary image for the a specific environment. What is the point?

Слайд 27

Compilation Process

So if we have 3 programming languages and 3 devices, how

Compilation Process So if we have 3 programming languages and 3 devices,
many compilers do we need?

CLR

VB

Source code

VB Compiler

C++

C#

CIL

CIL

CIL

Operating System Services

Common Language Runtime JIT Compiler

C# Compiler

C++ Compiler

Native
code

Managed
Code

Managed
Code

Managed
Code

Unmanaged
Code

CLR Services

Executes under the management of a virtual machine.

Слайд 28

Platform and Language Independent

What we have described so far will lead us

Platform and Language Independent What we have described so far will lead
to Platform independent environment. How?
Can we use compiled classes written in X language in a program written in Y language?
VB.NET + C#.NET code

Слайд 29

Language interoperability

All .NET languages can interoperate

C# calling
VB.NET

class Hello
{
static void Main()
{

Language interoperability All .NET languages can interoperate C# calling VB.NET class Hello
System.Console.WriteLine(Greeting.Message());
}
}

Class Greeting
Shared Function Message() As String
Return "hello"
End Function
End Class

Слайд 30

CLR

Execution engine

Common Language Runtime (CLR) is the execution engine
loads IL
compiles IL
executes resulting

CLR Execution engine Common Language Runtime (CLR) is the execution engine loads
machine code

IL

Runtime
compiler

Execute

machine code

Слайд 31

Cache

JIT runtime compile

CIL is compiled into machine code at runtime by the

Cache JIT runtime compile CIL is compiled into machine code at runtime
CLR
compiles methods as needed
called just in time (JIT) compile
JIT compilation model:
first time method is called the IL is compiled and optimized
compiled machine code is cached in transient memory
cached copy used for subsequent calls

CIL code
F()
G()
H()

JIT runtime
compiler

Execute

machine code for F()

Слайд 32

NGEN install time compile

Can compile CIL into machine code when app installed
use

NGEN install time compile Can compile CIL into machine code when app
native image generator ngen.exe
can speed startup time since code pre-compiled
but cannot do as many optimizations
original IL must still be available for type information

CLR

IL

ngen

Execute

native
image
cache

machine code

Слайд 33

C#

VB.NET

Language variability

Not all .NET languages have exactly the same capabilities
differ in small

C# VB.NET Language variability Not all .NET languages have exactly the same
but important ways

class Hello
{
static void Main()
{
int i;
uint u;
}
}

Class Greeting
Shared Sub Main()
Dim i as Integer
End Sub
End Class

signed integer

unsigned integer

signed integer only

Слайд 34

Common Language Specification

Common Language Specification (CLS) defines type subset
required to be supported

Common Language Specification Common Language Specification (CLS) defines type subset required to
by all .NET languages
limiting code to CLS maximizes language interoperability
code limited to CLS called CLS compliant

public class Calculator
{
public uint Add(uint a, uint b)
{
return a + b;
}
}

not CLS compliant
to use uint in public
interface of public class

Слайд 35

CLS,CLR/CTS & Languages

Languages offer a subset of the CLR/CTS and a superset

CLS,CLR/CTS & Languages Languages offer a subset of the CLR/CTS and a
of the CLS
(but not necessarily the same superset).

Слайд 39

Method call performance

Let's compare C ++, C # (.NET) method call performance
C++

Method call performance Let's compare C ++, C # (.NET) method call
Function
C++ Virtual Function
C# (.NET) Method

Слайд 42

Deitel & Deitel, Fig 24.24

Deitel & Deitel, Fig 24.24

Слайд 43

Calling a method for the first time

Managed EXE

Shared Sub Main()
Console.WriteLine(“Paul”)
Console.WriteLine(“Cross”)
End

Calling a method for the first time Managed EXE Shared Sub Main()
Sub

Console
Shared Sub WriteLine()
Shared Sub WriteLine(String)
(remaining members)

Jitter

Jitter


MSCorEE.dll

Function Jitter
In the assembly that implements the type (Console), look up the method (WriteLine) being called in the metadata.
From the metadata, get the IL for this method.
Allocate a block of memory.
Compile the IL into native code; save the code in the memory allocated in step 3.
Modify the method’s entry in the Type’s table so that it now points to the memory block allocated in step 3.
Jump to the native code contained inside the memory block.
End Function

Native code