2 // Mono.ILASM.MethodDef
5 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // (C) 2003 Jackson Harper, All rights reserved
13 using System.Collections;
16 namespace Mono.ILASM {
18 public class MethodDef {
20 private PEAPI.MethAttr meth_attr;
21 private PEAPI.CallConv call_conv;
22 private PEAPI.ImplAttr impl_attr;
24 private string signature;
25 private ITypeRef ret_type;
26 private ArrayList param_list;
27 private Hashtable named_param_table;
28 private ArrayList inst_list;
29 private ArrayList customattr_list;
30 private Hashtable label_table;
31 private Hashtable labelref_table;
32 private ArrayList label_list;
33 private PEAPI.MethodDef methoddef;
34 private bool entry_point;
35 private bool is_resolved;
36 private bool is_defined;
37 private ArrayList local_list;
38 private Hashtable named_local_table;
39 private bool init_locals;
40 private int max_stack;
41 private Random label_random;
43 public MethodDef (PEAPI.MethAttr meth_attr, PEAPI.CallConv call_conv,
44 PEAPI.ImplAttr impl_attr, string name,
45 ITypeRef ret_type, ArrayList param_list)
47 this.meth_attr = meth_attr;
48 this.call_conv = call_conv;
49 this.impl_attr = impl_attr;
51 this.ret_type = ret_type;
52 this.param_list = param_list;
54 inst_list = new ArrayList ();
55 customattr_list = new ArrayList ();
56 label_table = new Hashtable ();
57 labelref_table = new Hashtable ();
58 label_list = new ArrayList ();
59 local_list = new ArrayList ();
60 named_local_table = new Hashtable ();
61 named_param_table = new Hashtable ();
62 label_random = new Random ();
70 CreateNamedParamTable ();
77 public string Signature {
78 get { return signature; }
81 public PEAPI.MethodDef PeapiMethodDef {
82 get { return methoddef; }
85 public bool IsVararg {
86 get { return (call_conv & PEAPI.CallConv.Vararg) != 0; }
89 public void AddLocals (ArrayList local_list)
91 int slot_pos = this.local_list.Count;
93 foreach (Local local in local_list) {
94 if (local.Slot == -1) {
95 local.Slot = slot_pos;
98 if (local.Name == null)
100 named_local_table.Add (local.Name, local);
103 this.local_list.AddRange (local_list);
106 public Local GetNamedLocal (string name)
108 return (Local) named_local_table[name];
111 public int GetNamedLocalSlot (string name)
113 Local local = (Local) named_local_table[name];
118 public int GetNamedParamPos (string name)
120 int pos = (int) named_param_table[name];
125 public void InitLocals ()
130 public void EntryPoint ()
135 public void SetMaxStack (int max_stack)
137 this.max_stack = max_stack;
140 public void AddCustomAttr (CustomAttr customattr)
142 customattr_list.Add (customattr);
145 public PEAPI.MethodDef Resolve (CodeGen code_gen)
150 PEAPI.Param[] param_array;
152 if (param_list != null) {
153 int param_count = param_list.Count;
154 if (IsVararg && param_list[param_count-1] == ParamDef.Ellipsis)
156 param_array = new PEAPI.Param[param_count];
159 foreach (ParamDef paramdef in param_list) {
160 if (paramdef == ParamDef.Ellipsis)
162 paramdef.Define (code_gen);
163 param_array[count++] = paramdef.PeapiParam;
166 param_array = new PEAPI.Param[0];
170 ret_type.Resolve (code_gen);
172 methoddef = code_gen.PEFile.AddMethod (meth_attr, impl_attr,
173 name, ret_type.PeapiType, param_array);
175 methoddef.AddCallConv (call_conv);
181 public PEAPI.MethodDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
186 PEAPI.Param[] param_array;
188 if (param_list != null) {
189 int param_count = param_list.Count;
190 if (IsVararg && param_list[param_count-1] == ParamDef.Ellipsis)
192 param_array = new PEAPI.Param[param_count];
195 foreach (ParamDef paramdef in param_list) {
196 if (paramdef == ParamDef.Ellipsis)
198 paramdef.Define (code_gen);
199 param_array[count++] = paramdef.PeapiParam;
202 param_array = new PEAPI.Param[0];
206 ret_type.Resolve (code_gen);
208 methoddef = classdef.AddMethod (meth_attr, impl_attr,
209 name, ret_type.PeapiType, param_array);
211 methoddef.AddCallConv (call_conv);
217 public PEAPI.MethodRef GetVarargSig (PEAPI.Type[] opt)
220 throw new Exception ("Methods must be resolved before a vararg sig can be created.");
222 return methoddef.MakeVarArgSignature (opt);
226 /// Define a global method
228 public void Define (CodeGen code_gen)
235 WriteCode (code_gen, methoddef);
241 /// Define a member method
243 public void Define (CodeGen code_gen, PEAPI.ClassDef classdef)
248 Resolve (code_gen, classdef);
249 WriteCode (code_gen, methoddef);
254 public void AddInstr (IInstr instr)
256 inst_list.Add (instr);
259 protected void WriteCode (CodeGen code_gen, PEAPI.MethodDef methoddef)
262 methoddef.DeclareEntryPoint ();
264 if (local_list != null) {
265 PEAPI.Local[] local_array = new PEAPI.Local[local_list.Count];
268 foreach (Local local in local_list)
269 local_array[local.Slot] = local.GetPeapiLocal (code_gen);
271 methoddef.AddLocals (local_array, init_locals);
274 /// Nothing seems to work if maxstack is not set,
275 /// i need to find out if this NEEDs to be set
276 /// and what its default value should be
279 methoddef.SetMaxStack (max_stack);
281 /// Add the custrom attributes to this method
282 foreach (CustomAttr customattr in customattr_list)
283 customattr.AddTo (code_gen, methoddef);
285 if (inst_list.Count < 1)
288 PEAPI.CILInstructions cil = methoddef.CreateCodeBuffer ();
289 /// Create all the labels
290 /// TODO: Most labels don't actually need to be created so we could
291 /// probably only create the ones that need to be
292 LabelInfo[] label_info = new LabelInfo[label_table.Count + label_list.Count];
293 label_table.Values.CopyTo (label_info, 0);
294 label_list.CopyTo (label_info, label_table.Count);
295 int previous_pos = -1;
296 LabelInfo previous_label = null;
297 Array.Sort (label_info);
299 foreach (LabelInfo label in label_info) {
300 if (label.UseOffset) {
301 label.Define (new PEAPI.CILLabel (label.Offset));
304 if (label.Pos == previous_pos)
305 label.Label = previous_label.Label;
307 label.Define (cil.NewLabel ());
309 previous_label = label;
310 previous_pos = label.Pos;
313 // Set all the label refs
314 foreach (LabelInfo label in labelref_table.Values) {
315 LabelInfo def = (LabelInfo) label_table[label.Name];
317 Console.WriteLine ("Undefined Label: " + label);
320 label.Label = def.Label;
324 int next_label_pos = (label_info.Length > 0 ? label_info[0].Pos : -1);
326 for (int i=0; i<inst_list.Count; i++) {
327 IInstr instr = (IInstr) inst_list[i];
328 if (next_label_pos == i) {
329 cil.CodeLabel (label_info[label_pos].Label);
330 if (label_pos < label_info.Length) {
331 while (next_label_pos == i && ++label_pos < label_info.Length) {
332 if (label_info[label_pos].UseOffset)
333 cil.CodeLabel (label_info[label_pos].Label);
334 next_label_pos = label_info[label_pos].Pos;
337 if (label_pos >= label_info.Length)
340 instr.Emit (code_gen, this, cil);
345 public LabelInfo AddLabel (string name)
347 LabelInfo label_info = (LabelInfo) label_table[name];
348 if (label_info != null)
350 label_info = new LabelInfo (name, inst_list.Count);
351 label_table.Add (name, label_info);
355 public LabelInfo AddLabelRef (string name)
357 LabelInfo label_info = (LabelInfo) label_table[name];
358 if (label_info != null)
360 label_info = (LabelInfo) labelref_table[name];
361 if (label_info != null)
363 label_info = new LabelInfo (name, -1);
364 labelref_table.Add (name, label_info);
368 public LabelInfo AddLabel (int offset)
370 // We go pos + 1 so this line is not counted
371 LabelInfo label_info = new LabelInfo (null, inst_list.Count+1, (uint) offset);
372 label_list.Add (label_info);
376 public LabelInfo AddLabel ()
378 int pos = inst_list.Count;
379 LabelInfo label_info = new LabelInfo (null, inst_list.Count);
380 label_list.Add (label_info);
384 public PEAPI.CILLabel GetLabelDef (string name)
386 LabelInfo label_info = (LabelInfo) label_table[name];
388 return label_info.Label;
391 public PEAPI.CILLabel GetLabelDef (int pos)
393 foreach (LabelInfo li in label_list) {
400 private void CreateSignature ()
402 signature = CreateSignature (name, param_list);
405 public static string CreateSignature (string name, IList param_list)
407 StringBuilder builder = new StringBuilder ();
409 builder.Append (name);
410 builder.Append ('(');
412 if (param_list != null) {
414 foreach (ParamDef paramdef in param_list) {
415 if (ParamDef.Ellipsis == paramdef)
418 builder.Append (',');
419 builder.Append (paramdef.TypeName);
423 builder.Append (')');
425 return builder.ToString ();
428 public static string CreateSignature (string name, ITypeRef[] param_list)
430 StringBuilder builder = new StringBuilder ();
432 builder.Append (name);
433 builder.Append ('(');
435 if (param_list != null) {
437 foreach (ITypeRef param in param_list) {
438 if (param == TypeRef.Ellipsis)
441 builder.Append (',');
442 builder.Append (param.FullName);
446 builder.Append (')');
448 return builder.ToString ();
451 private void CreateNamedParamTable ()
453 if (param_list == null)
457 foreach (ParamDef param in param_list) {
458 if (param.Name != null)
459 named_param_table.Add (param.Name, count);
464 private void FixAttributes ()
466 if (name == ".ctor" || name == ".cctor")
467 meth_attr |= PEAPI.MethAttr.SpecialName | PEAPI.MethAttr.RTSpecialName;
468 // If methods aren't flagged as static they are instance
469 if ((PEAPI.MethAttr.Static & meth_attr) == 0)
470 call_conv |= PEAPI.CallConv.Instance;