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 protected class GenericInfo {
22 public ArrayList ConstraintList;
25 private PEAPI.MethAttr meth_attr;
26 private PEAPI.CallConv call_conv;
27 private PEAPI.ImplAttr impl_attr;
29 private string signature;
30 private Hashtable vararg_sig_table;
31 private ITypeRef ret_type;
32 private ArrayList typar_list;
33 private ArrayList param_list;
34 private ArrayList inst_list;
35 private ArrayList customattr_list;
36 private Hashtable label_table;
37 private Hashtable labelref_table;
38 private ArrayList label_list;
39 private PEAPI.MethodDef methoddef;
40 private bool entry_point;
41 private bool zero_init;
42 private bool is_resolved;
43 private bool is_defined;
44 private ArrayList local_list;
45 private Hashtable named_local_table;
46 private bool init_locals;
47 private int max_stack;
48 private bool pinvoke_info;
49 private ExternModule pinvoke_mod;
50 private string pinvoke_name;
51 private PEAPI.PInvokeAttr pinvoke_attr;
52 private SourceMethod source;
54 public MethodDef (CodeGen codegen, PEAPI.MethAttr meth_attr,
55 PEAPI.CallConv call_conv, PEAPI.ImplAttr impl_attr,
56 string name, ITypeRef ret_type, ArrayList param_list,
59 this.meth_attr = meth_attr;
60 this.call_conv = call_conv;
61 this.impl_attr = impl_attr;
63 this.ret_type = ret_type;
64 this.param_list = param_list;
66 inst_list = new ArrayList ();
67 customattr_list = new ArrayList ();
68 label_table = new Hashtable ();
69 labelref_table = new Hashtable ();
70 label_list = new ArrayList ();
71 local_list = new ArrayList ();
72 named_local_table = new Hashtable ();
84 codegen.BeginMethodDef (this);
86 if (codegen.SymbolWriter != null)
87 source = codegen.SymbolWriter.BeginMethod (this, start);
94 public string Signature {
95 get { return signature; }
98 public ITypeRef RetType {
99 get { return ret_type; }
102 public PEAPI.CallConv CallConv {
103 get { return call_conv; }
106 public PEAPI.MethodDef PeapiMethodDef {
107 get { return methoddef; }
110 public PEAPI.MethAttr Attributes {
111 get { return meth_attr; }
112 set { meth_attr = value; }
115 public bool IsVararg {
116 get { return (call_conv & PEAPI.CallConv.Vararg) != 0; }
119 public bool IsStatic {
120 get { return (meth_attr & PEAPI.MethAttr.Static) != 0; }
123 public bool IsVirtual {
124 get { return (meth_attr & PEAPI.MethAttr.Virtual) != 0; }
127 public bool IsAbstract {
128 get { return (meth_attr & PEAPI.MethAttr.Abstract) != 0; }
131 public ITypeRef[] ParamTypeList () {
133 if (param_list == null)
134 return new ITypeRef[0];
136 ITypeRef[] type_list = new ITypeRef[param_list.Count];
137 foreach (ParamDef param in param_list) {
138 type_list[count++] = param.Type;
143 public void AddPInvokeInfo (PEAPI.PInvokeAttr pinvoke_attr, ExternModule pinvoke_mod,
146 this.pinvoke_attr = pinvoke_attr;
147 this.pinvoke_mod = pinvoke_mod;
148 this.pinvoke_name = pinvoke_name;
152 public void AddGenericParam (string id)
154 if (typar_list == null)
155 typar_list = new ArrayList ();
157 GenericInfo gi = new GenericInfo ();
163 public void AddGenericConstraint (int index, ITypeRef constraint)
165 GenericInfo gi = (GenericInfo) typar_list[index];
167 if (gi.ConstraintList == null)
168 gi.ConstraintList = new ArrayList ();
169 gi.ConstraintList.Add (constraint);
172 public void AddParamDefaultValue (int index, PEAPI.Constant defval)
174 if (param_list [index] != null) {
175 ((ParamDef)param_list [index]).AddDefaultValue (defval);
179 public void AddLocals (ArrayList local_list)
181 int slot_pos = this.local_list.Count;
183 foreach (Local local in local_list) {
184 if (local.Slot == -1) {
185 local.Slot = slot_pos;
188 if (local.Name == null)
190 if(!named_local_table.Contains(local.Name))
191 named_local_table.Add (local.Name, local);
194 this.local_list.AddRange (local_list);
197 public Local GetNamedLocal (string name)
199 return (Local) named_local_table[name];
202 public int GetNamedLocalSlot (string name)
204 Local local = (Local) named_local_table[name];
209 public int GetNamedParamPos (string name)
212 foreach (ParamDef param in param_list) {
214 if (param.Name.CompareTo (name) == 0)
221 public void InitLocals ()
226 public void EntryPoint ()
231 public void ZeroInit ()
236 public void SetMaxStack (int max_stack)
238 this.max_stack = max_stack;
241 public void AddCustomAttr (CustomAttr customattr)
243 customattr_list.Add (customattr);
246 public PEAPI.MethodDef Resolve (CodeGen code_gen)
251 PEAPI.Param [] param_array = GenerateParams (code_gen);
253 ret_type.Resolve (code_gen);
255 methoddef = code_gen.PEFile.AddMethod (meth_attr, impl_attr,
256 name, ret_type.PeapiType, param_array);
258 methoddef.AddCallConv (call_conv);
264 public PEAPI.MethodDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
269 PEAPI.Param [] param_array = GenerateParams (code_gen);
271 ret_type.Resolve (code_gen);
273 methoddef = classdef.AddMethod (meth_attr, impl_attr,
274 name, ret_type.PeapiType, param_array);
276 methoddef.AddCallConv (call_conv);
282 private PEAPI.Param [] GenerateParams (CodeGen code_gen)
284 PEAPI.Param[] param_array;
286 if (param_list != null && param_list.Count > 0) {
287 int param_count = param_list.Count;
289 // Remove the last param if its the sentinel, not sure what
290 // should happen with more then one sentinel
291 ParamDef last = (ParamDef) param_list [param_count-1];
292 if (last.IsSentinel ())
295 param_array = new PEAPI.Param [param_count];
296 for (int i = 0; i < param_count; i++) {
297 ParamDef paramdef = (ParamDef) param_list [i];
298 paramdef.Define (code_gen);
299 param_array [i] = paramdef.PeapiParam;
303 param_array = new PEAPI.Param [0];
309 public PEAPI.MethodRef GetVarargSig (PEAPI.Type[] opt)
312 throw new Exception ("Methods must be resolved before a vararg sig can be created.");
314 PEAPI.MethodRef methref = null;
315 StringBuilder sigbuilder = new StringBuilder ();
317 foreach (PEAPI.Type t in opt)
318 sigbuilder.Append (opt + ", ");
319 sig = sigbuilder.ToString ();
321 if (vararg_sig_table == null) {
322 vararg_sig_table = new Hashtable ();
324 methref = vararg_sig_table [sig] as PEAPI.MethodRef;
327 if (methref == null) {
328 methref = methoddef.MakeVarArgSignature (opt);
329 vararg_sig_table [sig] = methref;
336 /// Define a global method
338 public void Define (CodeGen code_gen)
345 WriteCode (code_gen, methoddef);
347 //code_gen.Report.Message (String.Format ("Assembled method '<Module>'::{0}", name));
352 /// Define a member method
354 public void Define (CodeGen code_gen, TypeDef typedef)
359 Resolve (code_gen, (PEAPI.ClassDef) typedef.ClassDef);
360 WriteCode (code_gen, methoddef);
362 //code_gen.Report.Message (String.Format ("Assembled method {0}::{1}", typedef.FullName, name)); is_defined = true;
365 public void AddInstr (IInstr instr)
367 inst_list.Add (instr);
370 protected void WriteCode (CodeGen code_gen, PEAPI.MethodDef methoddef)
376 methoddef.DeclareEntryPoint ();
378 if (local_list.Count > 0) {
379 int ec = code_gen.Report.ErrorCount;
380 PEAPI.Local[] local_array = new PEAPI.Local[local_list.Count];
383 foreach (Local local in local_list)
384 local_array[local.Slot] = local.GetPeapiLocal (code_gen);
386 if (code_gen.Report.ErrorCount > ec)
392 methoddef.AddLocals (local_array, init_locals);
395 /// Nothing seems to work if maxstack is not set,
396 /// i need to find out if this NEEDs to be set
397 /// and what its default value should be
400 methoddef.SetMaxStack (max_stack);
402 /// Add the custrom attributes to this method
403 foreach (CustomAttr customattr in customattr_list)
404 customattr.AddTo (code_gen, methoddef);
407 methoddef.AddPInvokeInfo (pinvoke_mod.ModuleRef,
408 (pinvoke_name != null ? pinvoke_name : name), pinvoke_attr);
412 if (inst_list.Count < 1)
415 PEAPI.CILInstructions cil = methoddef.CreateCodeBuffer ();
416 /// Create all the labels
417 /// TODO: Most labels don't actually need to be created so we could
418 /// probably only create the ones that need to be
419 LabelInfo[] label_info = new LabelInfo[label_table.Count + label_list.Count];
420 label_table.Values.CopyTo (label_info, 0);
421 label_list.CopyTo (label_info, label_table.Count);
422 int previous_pos = -1;
423 LabelInfo previous_label = null;
424 Array.Sort (label_info);
426 foreach (LabelInfo label in label_info) {
427 if (label.UseOffset) {
428 label.Define (new PEAPI.CILLabel (label.Offset));
431 if (label.Pos == previous_pos)
432 label.Label = previous_label.Label;
434 label.Define (cil.NewLabel ());
436 previous_label = label;
437 previous_pos = label.Pos;
440 // Set all the label refs
441 foreach (LabelInfo label in labelref_table.Values) {
442 LabelInfo def = (LabelInfo) label_table[label.Name];
444 code_gen.Report.Error ("Undefined Label: " + label);
447 label.Label = def.Label;
451 int next_label_pos = (label_info.Length > 0 ? label_info[0].Pos : -1);
453 for (int i=0; i<inst_list.Count; i++) {
454 IInstr instr = (IInstr) inst_list[i];
455 if (next_label_pos == i) {
456 cil.CodeLabel (label_info[label_pos].Label);
457 if (label_pos < label_info.Length) {
458 while (next_label_pos == i && ++label_pos < label_info.Length) {
459 if (label_info[label_pos].UseOffset)
460 cil.CodeLabel (label_info[label_pos].Label);
461 next_label_pos = label_info[label_pos].Pos;
464 if (label_pos >= label_info.Length)
468 source.MarkLocation (instr.Location.line, cil.Offset);
469 instr.Emit (code_gen, this, cil);
473 source.MarkLocation (source.EndLine, cil.Offset);
475 // Generic type parameters
476 if (typar_list != null) {
478 foreach (GenericInfo gi in typar_list) {
479 PEAPI.GenericParameter gp = methoddef.AddGenericParameter (index++, gi.Id);
480 if (gi.ConstraintList != null) {
481 foreach (ITypeRef cnst in gi.ConstraintList) {
482 cnst.Resolve (code_gen);
483 gp.AddConstraint (cnst.PeapiType);
490 public LabelInfo AddLabel (string name)
492 LabelInfo label_info = (LabelInfo) label_table[name];
493 if (label_info != null)
495 label_info = new LabelInfo (name, inst_list.Count);
496 label_table.Add (name, label_info);
500 public LabelInfo AddLabelRef (string name)
502 LabelInfo label_info = (LabelInfo) label_table[name];
503 if (label_info != null)
505 label_info = (LabelInfo) labelref_table[name];
506 if (label_info != null)
508 label_info = new LabelInfo (name, -1);
509 labelref_table.Add (name, label_info);
513 public LabelInfo AddLabel (int offset)
515 // We go pos + 1 so this line is not counted
516 LabelInfo label_info = new LabelInfo (null, inst_list.Count+1, (uint) offset);
517 label_list.Add (label_info);
521 public LabelInfo AddLabel ()
523 int pos = inst_list.Count;
524 LabelInfo label_info = new LabelInfo (null, inst_list.Count);
525 label_list.Add (label_info);
529 public PEAPI.CILLabel GetLabelDef (string name)
531 LabelInfo label_info = (LabelInfo) label_table[name];
533 return label_info.Label;
536 public PEAPI.CILLabel GetLabelDef (int pos)
538 foreach (LabelInfo li in label_list) {
545 private void CreateSignature ()
548 signature = CreateVarargSignature (RetType, name, param_list);
550 signature = CreateSignature (RetType, name, param_list);
553 public static string CreateSignature (ITypeRef RetType, string name, IList param_list)
555 StringBuilder builder = new StringBuilder ();
557 builder.Append (RetType.FullName);
558 builder.Append (" ");
559 builder.Append (name);
560 builder.Append ('(');
562 if (param_list != null) {
564 foreach (ParamDef paramdef in param_list) {
566 builder.Append (',');
567 builder.Append (paramdef.TypeName);
571 builder.Append (')');
573 return builder.ToString ();
576 public static string CreateVarargSignature (ITypeRef RetType, string name, IList param_list)
578 StringBuilder builder = new StringBuilder ();
579 ParamDef last = null;
581 builder.Append (RetType.FullName);
582 builder.Append (" ");
583 builder.Append (name);
584 builder.Append ('(');
587 if (param_list != null) {
588 foreach (ParamDef paramdef in param_list) {
590 builder.Append (',');
591 builder.Append (paramdef.TypeName);
594 last = (ParamDef) param_list[param_list.Count - 1];
598 if (last == null || !last.IsSentinel ()) {
600 builder.Append (',');
601 builder.Append ("...");
604 builder.Append (')');
606 return builder.ToString ();
609 public static string CreateVarargSignature (ITypeRef RetType, string name, ITypeRef [] param_list)
611 StringBuilder builder = new StringBuilder ();
612 ITypeRef last = null;
614 builder.Append (RetType.FullName);
615 builder.Append (" ");
616 builder.Append (name);
617 builder.Append ('(');
620 if (param_list != null && param_list.Length > 0) {
621 foreach (ITypeRef param in param_list) {
623 builder.Append (',');
624 builder.Append (param.FullName);
627 if (param is SentinelTypeRef)
633 if (last == null || !(last is SentinelTypeRef)) {
635 builder.Append (',');
636 builder.Append ("...");
639 builder.Append (')');
641 return builder.ToString ();
644 public static string CreateSignature (ITypeRef RetType, string name, ITypeRef[] param_list)
646 StringBuilder builder = new StringBuilder ();
648 builder.Append (RetType.FullName);
649 builder.Append (" ");
650 builder.Append (name);
651 builder.Append ('(');
653 if (param_list != null) {
655 foreach (ITypeRef param in param_list) {
657 builder.Append (',');
658 builder.Append (param.FullName);
660 if (param is SentinelTypeRef)
664 builder.Append (')');
666 return builder.ToString ();
669 private void FixAttributes ()
671 if (name == ".ctor" || name == ".cctor")
672 meth_attr |= PEAPI.MethAttr.SpecialName | PEAPI.MethAttr.RTSpecialName;
673 // If methods aren't flagged as static they are instance
674 if ((PEAPI.MethAttr.Static & meth_attr) == 0)
675 call_conv |= PEAPI.CallConv.Instance;