1 / 34

Common Language Runtime

Common Language Runtime. Mauro Araújo e Rafael Borges {mscla, rmb2}@cin.ufpe.br. Sumário. Componentes COM CLR Assemblies Assembly metadata Type metadata MSIL Modelo de Execução Chamada de Métodos Interoperabilidade. Componentes. Características Genéricos Composicionais

indra
Download Presentation

Common Language Runtime

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common Language Runtime Mauro Araújo e Rafael Borges{mscla, rmb2}@cin.ufpe.br

  2. Sumário • Componentes • COM • CLR • Assemblies • Assembly metadata • Type metadata • MSIL • Modelo de Execução • Chamada de Métodos • Interoperabilidade

  3. Componentes • Características • Genéricos • Composicionais • Encapsulados • Unidades Independentes • Implantação • Versionamento • Modelo Atual • Contratos • Definições de Tipos

  4. COM • Especificação dos Contratos • Sem padrão • IDL, TLB • Dependências • Sem descrição • DLL Hell • Contratos • Físicos • Sem extensibilidade

  5. CLR • Evolução do COM • Assemblies e Contratos • Auto-descritíveis (via Metadados) • Extensíveis (via Atributos) • Descrevem dependências • Side-by-side execution • Virtualização dos Contratos • Referência por Assinatura • Independente de plataforma • Abstrai representação física

  6. Assemblies Assembly metadata Type metadata

  7. Assemblies

  8. Modelo de Execução EXE/DLL (MSIL + METADATA) Source Code Compiler Class Library(MSIL + Metadata) Class Loader C#, VB, C++, Haskell, ... JIT Trustedpre-JITedcode only Call to uncompiled method ManagedNativeCode Execution Security Checks

  9. Execution State Evaluation stack Activation record Incomingarguments top Localvariables Evaluation stack

  10. 0 0 0 1 1 1 2 2 2 ... ... ... 0 0 0 1 1 1 2 2 2 ... ... ... Activation Record • Alocados na pilha de execução • Coleção de variáveis locais • Coleção de argumentos do método • Numeração lógica • Ordem de declaração

  11. Onde a computação ocorre de fato Alvo das instruções da CLR Pilha lógica Elementos não possuem um tamanho particular Tipos primitivos  Tipos Referência  Structs  ... Evaluation Stack

  12. Evaluation Stack ldloc n top top Cópia do valor da n-ésima variável local add top v2 top v1 + v2 v1

  13. top top Evaluation Stack stloc n top arg2 top return value arg1 call int32 [mscorlib] System.Math::Max(int32, int32)

  14. top top Evaluation Stack ldelem.u2 array index array elem. array ref.

  15. Carregando campos e variáveis Type Class::field-name [mscorlib]System.Math :: PI

  16. Carregando constantes

  17. Carregando endereços

  18. Armazenando campos e variáveis Type Class::field-name [mscorlib]System.Math :: PI

  19. Instruções aritméticas

  20. top top clt right operand Bool result left operand Instruções de desvio e comparação • Gerando valores booleanos • clt, ceq, cgt

  21. Instruções de desvio e comparação

  22. Instruções de desvio e comparação • Executando o desvio • brfalse • "Branch on false, zero or null" • brtrue • "Branch on nonfalse or nonnull"

  23. Instruções de desvio e comparação

  24. Hello World .assembly extern mscorlib {}.assembly hellocs {}.module hellocs.exe .class private auto ansi HelloWorld extends [mscorlib]System.Object { .method public hidebysig static void Main() il managed { .entrypoint .maxstack 1 ldstr "Hello World" call void [mscorlib]System.Console::WriteLine(string) ret } .method public hidebysig specialname rtspecialname instance void .ctor() il managed { .maxstack 1 ldarg.0 call instance void [mscorlib]System.Object:: .ctor() ret } } class HelloWorld { public static void Main() { System.Console.WriteLine("Hello World"); } }

  25. Desvios, loops,... public class Console{ public static void WriteLn(){ System.Console.WriteLine(); } public static void WriteString(char[] str){ int len = str.Length; for( int i = 0; i < len && str[i] != '\0'; i++) System.Console.Write(str[i]); } } public class ProgArgs { public static System.String[] argList = null; public static int ArgNumber() { if (ProgArgs.argList == null) return 0; else return argList.Length; } }

  26. Desvios, loops,... // public static int ArgNumber() { // if (ProgArgs.argList = = null) // return 0; // else // return argList.Length; // } .locals ([0] int32) ldsfld string[] ProgArgs::argList brtrue.s lb01 ldc.i4.0 stloc.0 br.s lb02 lb01: ldsfld string[] ProgArgs::argList ldlen conv.i4 stloc.0 lb02: ldloc.0 ret

  27. // public static void WriteString(char[] str){// int len = str.Length; // for( int i = 0; i < len && str[i] != '\0'; i++) // System.Console.Write(str[i]); // } .locals ([0] int32 len, [1] int32 i) ldarg.0 // load argument str ldlen // get array length conv.i4 stloc.0 // store in len ldc.i4.0 stloc.1 // set i to 0 br.s lb03 // jump to test lb05: ldarg.0 ldloc.1 ldelem.u2 call void [mscorlib]System.Console::Write(wchar) ldloc.1 // load i and increment ldc.i4.1 add stloc.1 // store new value of i lb03: ldloc.1 ldloc.0 bge.s lb04 // test i >= len ldarg.0 ldloc.1 ldelem.u2 brtrue.s lb05 // test str[i] != 0 lb04: ret

  28. Criando Objetos e arrays class Class1 { public int a; public string b; Class1(int n, string s){ a = n; b = s; } public int[] CreateArrayOfInt() { int[] array = new int[this.a]; return array; } static void Main() { Class1 c = new Class1(3,"if724"); int[] array = c.CreateArrayOfInt(); } }

  29. Criando Objetos e arrays .method public hidebysig instance int32[] CreateArrayOfInt() cil managed { .maxstack 1 .locals init ([0] int32[] 'array') ldarg.0 // this ldfld int32 Class1::a newarr [mscorlib]System.Int32 stloc.0 ldloc.0 ret }

  30. Criando Objetos e arrays .method private hidebysig static void Main() cil managed { .entrypoint .maxstack 3 .locals init ([0] class Class1 c, [1] int32[] 'array') ldc.i4.3 ldstr "if724" newobj instance void Class1::.ctor(int32, string) stloc.0 ldloc.0 callvirt instance int32[] Class1::CreateArrayOfInt() stloc.1 ret }

  31. Chamando métodos • Métodos estáticos • call void [mscorlib]System.Console::WriteLine(string) • Métodos de instância não-virtuais • call instance void [mydll]MyNamespace.Class:: Method(int32) • Métodos de instância virtuais • callvirt instance string [mscorlib]System.String::Insert(int32,string)

  32. Chamada de métodos

  33. Interoperabilidade • Linguagem intermediária comum • Especificação de subconjunto mínimo que deve ser suportados por todas as linguagens • CLS

  34. Referências • Essential .NET – Volume 1 • Don Box, with Chris Sells • Compiling for the .NET Common Language Runtime • John Gough • Inside .NET Framework • msdn.microsoft.com/library

More Related