+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
--- /dev/null
+//
+// 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);
+ }
+ }
+
+}
+
+
PEAPI.Class PeapiClass { get; }
+ IMethodRef GetMethodRef (ITypeRef ret_type, string name, ITypeRef[] param);
}
}
--- /dev/null
+//
+// 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);
+ }
+
+}
+
+
+
--- /dev/null
+//
+// 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);
+ }
+ }
+
+}
+
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)
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 ();
}
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;
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;
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;
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;
}
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 ('(');
builder.Append (')');
- signature = builder.ToString ();
+ return builder.ToString ();
}
}
--- /dev/null
+//
+// 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);
+ }
+ }
+
+}
+
+
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;
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;
public void AddFieldDef (FieldDef fielddef)
{
- field_list.Add (fielddef);
+ field_table.Add (fielddef.Name, fielddef);
}
public void AddDataDef (DataDef datadef)
public void AddMethodDef (MethodDef methoddef)
{
- method_list.Add (methoddef);
+ method_table.Add (methoddef.Signature, methoddef);
}
public void Define (CodeGen code_gen)
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)
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)