* IMethodRef.cs: New file - Interface that method references must
authorJackson Harper <jackson@novell.com>
Sat, 10 May 2003 19:30:52 +0000 (19:30 -0000)
committerJackson Harper <jackson@novell.com>
Sat, 10 May 2003 19:30:52 +0000 (19:30 -0000)
implement
* ExternMethodRef.cs: New file - Reference to a method in another
assembly
* Local.cs: New file - A Local variable
* MethodInstr.cs: New file - an instruction that takes a method
reference operand
* IClassRef.cs: Add method to get a method reference from a class
reference
* MethodDef.cs: Add ability to resolve methods before defining
them, add max stack, locals, and entry point. Make CreateSignature
method public and static so other classes can use it.
* TypeDef.cs: Store methods and fields in hashtables so they can
be easily retrieved, add method to resolve member methods.
* TypeRef.cs: Add method for resolving member methods.

svn path=/trunk/mcs/; revision=14458

mcs/ilasm/codegen/ChangeLog
mcs/ilasm/codegen/ExternMethodRef.cs [new file with mode: 0644]
mcs/ilasm/codegen/IClassRef.cs
mcs/ilasm/codegen/IMethodRef.cs [new file with mode: 0644]
mcs/ilasm/codegen/Local.cs [new file with mode: 0644]
mcs/ilasm/codegen/MethodDef.cs
mcs/ilasm/codegen/MethodInstr.cs [new file with mode: 0644]
mcs/ilasm/codegen/TypeDef.cs
mcs/ilasm/codegen/TypeRef.cs

index 5251ab12714dab5da40c403d585772586b5cc1c0..698966cf11bb016a67bfe44470b83982dbebfedc 100644 (file)
@@ -1,3 +1,22 @@
+2003-05-10 Jackson Harper <jackson@latitudegeo.com>
+
+       * IMethodRef.cs: New file - Interface that method references must
+       implement
+       * ExternMethodRef.cs: New file - Reference to a method in another
+       assembly
+       * Local.cs: New file - A Local variable
+       * MethodInstr.cs: New file - an instruction that takes a method
+       reference operand
+       * IClassRef.cs: Add method to get a method reference from a class
+       reference
+       * MethodDef.cs: Add ability to resolve methods before defining
+       them, add max stack, locals, and entry point. Make CreateSignature
+       method public and static so other classes can use it.
+       * TypeDef.cs: Store methods and fields in hashtables so they can
+       be easily retrieved, add method to resolve member methods.
+       * TypeRef.cs: Add method for resolving member methods.
+       
+               
 2003-05-07 Jackson Harper <jackson@latitudegeo.com>
 
        * TypeInstr.cs: New file - implementation of instructions that
diff --git a/mcs/ilasm/codegen/ExternMethodRef.cs b/mcs/ilasm/codegen/ExternMethodRef.cs
new file mode 100644 (file)
index 0000000..750ffed
--- /dev/null
@@ -0,0 +1,60 @@
+//
+// Mono.ILASM.ExternMethodRef
+//
+// Author(s):
+//  Jackson Harper (Jackson@LatitudeGeo.com)
+//
+// (C) 2003 Jackson Harper, All rights reserved
+//
+
+
+using System;
+
+namespace Mono.ILASM {
+
+        public class ExternMethodRef : IMethodRef {
+
+                private ExternTypeRef owner;
+                private ITypeRef ret_type;
+                private string name;
+                private ITypeRef[] param;
+
+                private PEAPI.MethodRef peapi_method;
+
+                public ExternMethodRef (ExternTypeRef owner,
+                        ITypeRef ret_type, string name, ITypeRef[] param)
+                {
+                        this.owner = owner;
+                        this.ret_type = ret_type;
+                        this.name = name;
+                        this.param = param;
+                }
+
+                public PEAPI.Method PeapiMethod {
+                        get { return peapi_method; }
+                }
+
+                public void Resolve (CodeGen code_gen)
+                {
+                        PEAPI.Type[] param_list = new PEAPI.Type[param.Length];
+                        PEAPI.ClassRef owner_ref;
+
+                        ret_type.Resolve (code_gen);
+
+                        int count = 0;
+                        foreach (ITypeRef typeref in param) {
+                                typeref.Resolve (code_gen);
+                                param_list[count++] = typeref.PeapiType;
+                        }
+
+                        owner.Resolve (code_gen);
+                        owner_ref = owner.PeapiClassRef;
+
+                        peapi_method = owner_ref.AddMethod (name,
+                                        ret_type.PeapiType, param_list);
+                }
+        }
+
+}
+
+
index af434fe08326de94972b64c5b9d9f042cd3f93ca..e3563001620fe7bad334c58f0e3e82f694d68457 100644 (file)
@@ -15,6 +15,7 @@ namespace Mono.ILASM {
 
                 PEAPI.Class PeapiClass { get; }
 
+                IMethodRef GetMethodRef (ITypeRef ret_type, string name, ITypeRef[] param);
         }
 
 }
diff --git a/mcs/ilasm/codegen/IMethodRef.cs b/mcs/ilasm/codegen/IMethodRef.cs
new file mode 100644 (file)
index 0000000..1d13395
--- /dev/null
@@ -0,0 +1,28 @@
+//
+// Mono.ILASM.IMethodRef
+//
+// Author(s):
+//  Jackson Harper (Jackson@LatitudeGeo.com)
+//
+// (C) 2003 Jackson Harper, All rights reserved
+//
+
+
+using System;
+
+
+namespace Mono.ILASM {
+
+        public interface IMethodRef {
+
+                PEAPI.Method PeapiMethod {
+                        get;
+                }
+
+                void Resolve (CodeGen code_gen);
+        }
+
+}
+
+
+
diff --git a/mcs/ilasm/codegen/Local.cs b/mcs/ilasm/codegen/Local.cs
new file mode 100644 (file)
index 0000000..d2b6de4
--- /dev/null
@@ -0,0 +1,54 @@
+//
+// Mono.ILASM.Local
+//
+// Author(s):
+//  Jackson Harper (Jackson@LatitudeGeo.com)
+//
+// (C) 2003 Jackson Harper, All rights reserved
+//
+
+
+using System;
+
+
+namespace Mono.ILASM {
+
+        public class Local {
+
+                private int slot;
+                private string name;
+                private ITypeRef type;
+
+                public Local (int slot, ITypeRef type) : this (slot, null, type) {
+
+                }
+
+                public Local (int slot, string name, ITypeRef type) {
+                        this.slot = slot;
+                        this.name = name;
+                        this.type = type;
+                }
+
+                public int Slot {
+                        get { return slot; }
+                        set { slot = value; }
+                }
+
+                public string Name {
+                        get { return name; }
+                }
+
+                public ITypeRef Type {
+                        get { return type; }
+                }
+
+                public PEAPI.Local GetPeapiLocal (CodeGen code_gen)
+                {
+                        type.Resolve (code_gen);
+
+                        return new PEAPI.Local (name, type.PeapiType);
+                }
+        }
+
+}
+
index 05555b816b33f522b3edeed91966242af8ab466d..e0b95c9c2b98f1bcfb0fd30829d7f74d1058bef7 100644 (file)
@@ -55,7 +55,13 @@ namespace Mono.ILASM {
                 private ArrayList inst_list;
                 private Hashtable label_table;
                 private PEAPI.MethodDef methoddef;
+                private bool entry_point;
+                private bool is_resolved;
                 private bool is_defined;
+                private ArrayList local_list;
+                private Hashtable named_local_table;
+                private bool init_locals;
+                private int max_stack;
 
                 public MethodDef (PEAPI.MethAttr meth_attr, PEAPI.ImplAttr impl_attr,
                                 string name, ITypeRef ret_type, ArrayList param_list)
@@ -68,8 +74,14 @@ namespace Mono.ILASM {
 
                         inst_list = new ArrayList ();
                         label_table = new Hashtable ();
+                        local_list = new ArrayList ();
+                        named_local_table = new Hashtable ();
+                        entry_point = false;
+                        init_locals = false;
+                        max_stack = -1;
 
                         is_defined = false;
+                        is_resolved = false;
                         CreateSignature ();
                 }
 
@@ -85,13 +97,47 @@ namespace Mono.ILASM {
                         get { return methoddef; }
                 }
 
-                /// <summary>
-                ///  Define a global method
-                /// </summary>
-                public void Define (CodeGen code_gen)
+                public void AddLocals (ArrayList local_list)
                 {
-                        if (is_defined)
-                                return;
+                        int slot_pos = this.local_list.Count;
+
+                        foreach (Local local in local_list) {
+                                if (local.Slot == -1) {
+                                        local.Slot = slot_pos;
+                                }
+                                slot_pos++;
+                                if (local.Name == null)
+                                        continue;
+                                named_local_table.Add (local.Name, local);
+                        }
+
+                        this.local_list.AddRange (local_list);
+                }
+
+                public Local GetNamedLocal (string name)
+                {
+                        return (Local) named_local_table[name];
+                }
+
+                public void InitLocals ()
+                {
+                        init_locals = true;
+                }
+
+                public void EntryPoint ()
+                {
+                        entry_point = true;
+                }
+
+                public void SetMaxStack (int max_stack)
+                {
+                        this.max_stack = max_stack;
+                }
+
+                public PEAPI.MethodDef Resolve (CodeGen code_gen)
+                {
+                        if (is_resolved)
+                                return methoddef;
 
                         PEAPI.Param[] param_array = new PEAPI.Param[param_list.Count];
                         int count = 0;
@@ -105,18 +151,15 @@ namespace Mono.ILASM {
                         methoddef = code_gen.PEFile.AddMethod (meth_attr, impl_attr,
                                         name, ret_type.PeapiType, param_array);
 
-                        WriteCode (code_gen, methoddef);
+                        is_resolved = true;
 
-                        is_defined = true;
+                        return methoddef;
                 }
 
-                /// <summary>
-                ///  Define a member method
-                /// </summary>
-                public void Define (CodeGen code_gen, PEAPI.ClassDef classdef)
+                public PEAPI.MethodDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
                 {
-                        if (is_defined)
-                                return;
+                        if (is_resolved)
+                                return methoddef;
 
                         PEAPI.Param[] param_array = new PEAPI.Param[param_list.Count];
                         int count = 0;
@@ -130,6 +173,36 @@ namespace Mono.ILASM {
                         methoddef = classdef.AddMethod (meth_attr, impl_attr,
                                         name, ret_type.PeapiType, param_array);
 
+                        is_resolved = true;
+
+                        return methoddef;
+                }
+
+                /// <summary>
+                ///  Define a global method
+                /// </summary>
+                public void Define (CodeGen code_gen)
+                {
+                        if (is_defined)
+                                return;
+
+                        Resolve (code_gen);
+
+                        WriteCode (code_gen, methoddef);
+
+                        is_defined = true;
+                }
+
+                /// <summary>
+                ///  Define a member method
+                /// </summary>
+                public void Define (CodeGen code_gen, PEAPI.ClassDef classdef)
+                {
+                        if (is_defined)
+                                return;
+
+                        Resolve (code_gen, classdef);
+
                         WriteCode (code_gen, methoddef);
 
                         is_defined = true;
@@ -142,6 +215,22 @@ namespace Mono.ILASM {
 
                 protected void WriteCode (CodeGen code_gen, PEAPI.MethodDef methoddef)
                 {
+                        if (entry_point)
+                                methoddef.DeclareEntryPoint ();
+
+                        if (local_list != null) {
+                                PEAPI.Local[] local_array = new PEAPI.Local[local_list.Count];
+                                int i = 0;
+
+                                foreach (Local local in local_list)
+                                        local_array[local.Slot]  = local.GetPeapiLocal (code_gen);
+
+                                methoddef.AddLocals (local_array, init_locals);
+                        }
+
+                        if (max_stack >= 0)
+                                methoddef.SetMaxStack (max_stack);
+
                         if (inst_list.Count < 1)
                                 return;
 
@@ -188,11 +277,14 @@ namespace Mono.ILASM {
                 }
 
                 private void CreateSignature ()
+                {
+                        signature = CreateSignature (name, param_list);
+                }
+
+                public static string CreateSignature (string name, ICollection param_list)
                 {
                         StringBuilder builder = new StringBuilder ();
 
-                        builder.Append (ret_type.FullName);
-                        builder.Append ('_');
                         builder.Append (name);
                         builder.Append ('(');
 
@@ -205,7 +297,7 @@ namespace Mono.ILASM {
                         builder.Append (')');
 
 
-                        signature = builder.ToString ();
+                        return builder.ToString ();
                 }
         }
 
diff --git a/mcs/ilasm/codegen/MethodInstr.cs b/mcs/ilasm/codegen/MethodInstr.cs
new file mode 100644 (file)
index 0000000..2f0d9f9
--- /dev/null
@@ -0,0 +1,36 @@
+//
+// Mono.ILASM.MethodInstr
+//
+// Author(s):
+//  Jackson Harper (Jackson@LatitudeGeo.com)
+//
+// (C) 2003 Jackson Harper, All rights reserved
+//
+
+
+using System;
+
+
+namespace Mono.ILASM {
+
+        public class MethodInstr : IInstr {
+
+                private PEAPI.MethodOp op;
+                private IMethodRef operand;
+
+                public MethodInstr (PEAPI.MethodOp op, IMethodRef operand)
+                {
+                        this.op = op;
+                        this.operand = operand;
+                }
+
+                public void Emit (CodeGen code_gen, PEAPI.CILInstructions cil)
+                {
+                        operand.Resolve (code_gen);
+                        cil.MethInst (op, operand.PeapiMethod);
+                }
+        }
+
+}
+
+
index 3bc96a8a247c58862098e6181cbc38458891a3fb..444ddcc1575613d838898e11ecd1a0615a1728af 100644 (file)
@@ -23,8 +23,8 @@ namespace Mono.ILASM {
                 private IClassRef parent;
                 private ArrayList impl_list;
                 private PEAPI.ClassDef classdef;
-                private ArrayList field_list;
-                private ArrayList method_list;
+                private Hashtable field_table;
+                private Hashtable method_table;
                 private ArrayList data_list;
                 private TypeDef outer;
 
@@ -36,8 +36,8 @@ namespace Mono.ILASM {
                         this.name = name;
                         this.parent = parent;
                         this.impl_list = impl_list;
-                        field_list = new ArrayList ();
-                        method_list = new ArrayList ();
+                        field_table = new Hashtable ();
+                        method_table = new Hashtable ();
                         data_list = new ArrayList ();
 
                         is_defined = false;
@@ -71,7 +71,7 @@ namespace Mono.ILASM {
 
                 public void AddFieldDef (FieldDef fielddef)
                 {
-                        field_list.Add (fielddef);
+                        field_table.Add (fielddef.Name, fielddef);
                 }
 
                 public void AddDataDef (DataDef datadef)
@@ -81,7 +81,7 @@ namespace Mono.ILASM {
 
                 public void AddMethodDef (MethodDef methoddef)
                 {
-                        method_list.Add (methoddef);
+                        method_table.Add (methoddef.Signature, methoddef);
                 }
 
                 public void Define (CodeGen code_gen)
@@ -131,15 +131,22 @@ namespace Mono.ILASM {
 
                 public void DefineContents (CodeGen code_gen)
                 {
-                        foreach (FieldDef fielddef in field_list) {
+                        foreach (FieldDef fielddef in field_table.Values) {
                                 fielddef.Define (code_gen, classdef);
                         }
 
-                        foreach (MethodDef methoddef in method_list) {
+                        foreach (MethodDef methoddef in method_table.Values) {
                                 methoddef.Define (code_gen, classdef);
                         }
                 }
 
+                public PEAPI.Method ResolveMethod (string signature, CodeGen code_gen)
+                {
+                        MethodDef methoddef = (MethodDef) method_table[signature];
+
+                        return methoddef.Resolve (code_gen, classdef);
+                }
+
                 private string MakeFullName ()
                 {
                         if (name_space == null || name_space == String.Empty)
index 7000ee589116433ab388c2c00e278cf89bf5b656..3a0a80c53c95f2d77e554696e2045a85dfcd5514 100644 (file)
@@ -94,6 +94,12 @@ namespace Mono.ILASM {
                         is_pinned = true;
                 }
 
+                public  IMethodRef GetMethodRef (ITypeRef ret_type,
+                                string name, ITypeRef[] param)
+                {
+                        return new MethodRef (this, ret_type, name, param);
+                }
+
                 public void Resolve (CodeGen code_gen)
                 {
                         if (is_resolved)