1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
//
// Mono.ILASM.MethodDef
//
// Author(s):
// Jackson Harper (Jackson@LatitudeGeo.com)
//
// (C) 2003 Jackson Harper, All rights reserved
//
using System;
using System.Text;
using System.Collections;
using System.Security;
using Mono.CompilerServices.SymbolWriter;
namespace Mono.ILASM {
public class MethodDef : ICustomAttrTarget, IDeclSecurityTarget {
private PEAPI.MethAttr meth_attr;
private PEAPI.CallConv call_conv;
private PEAPI.ImplAttr impl_attr;
private string name;
private string signature;
private Hashtable vararg_sig_table;
private ParamDef ret_param;
private ArrayList param_list;
private ArrayList inst_list;
private ArrayList customattr_list;
private DeclSecurity decl_sec;
private Hashtable label_table;
private Hashtable labelref_table;
private ArrayList label_list;
private PEAPI.MethodDef methoddef;
private bool entry_point;
private bool zero_init;
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;
private bool pinvoke_info;
private ExternModule pinvoke_mod;
private string pinvoke_name;
private PEAPI.PInvokeAttr pinvoke_attr;
private SourceMethod source;
private TypeDef type_def;
private GenericParameters gen_params;
private Location start;
private CodeGen codegen;
public MethodDef (CodeGen codegen, PEAPI.MethAttr meth_attr,
PEAPI.CallConv call_conv, PEAPI.ImplAttr impl_attr,
string name, BaseTypeRef ret_type, ArrayList param_list,
Location start, GenericParameters gen_params, TypeDef type_def)
{
this.codegen = codegen;
this.meth_attr = meth_attr;
this.call_conv = call_conv;
this.impl_attr = impl_attr;
this.name = name;
this.param_list = param_list;
this.type_def = type_def;
this.gen_params = gen_params;
this.ret_param = new ParamDef (PEAPI.ParamAttr.Default, "", ret_type);
this.start = (Location) start.Clone ();
inst_list = new ArrayList ();
label_table = new Hashtable ();
labelref_table = new Hashtable ();
label_list = new ArrayList ();
local_list = new ArrayList ();
named_local_table = new Hashtable ();
entry_point = false;
zero_init = false;
init_locals = false;
max_stack = -1;
pinvoke_info = false;
is_defined = false;
is_resolved = false;
ResolveGenParams ();
CreateSignature ();
codegen.BeginMethodDef (this);
if (codegen.SymbolWriter != null)
source = codegen.SymbolWriter.BeginMethod (this, start);
}
public string Name {
get { return name; }
}
public string Signature {
get { return signature; }
}
public BaseTypeRef RetType {
get { return ret_param.Type; }
}
public PEAPI.CallConv CallConv {
get { return call_conv; }
}
public PEAPI.MethodDef PeapiMethodDef {
get { return methoddef; }
}
public PEAPI.MethAttr Attributes {
get { return meth_attr; }
set { meth_attr = value; }
}
public bool IsVararg {
get { return (call_conv & PEAPI.CallConv.Vararg) != 0; }
}
public bool IsStatic {
get { return (meth_attr & PEAPI.MethAttr.Static) != 0; }
}
public bool IsVirtual {
get { return (meth_attr & PEAPI.MethAttr.Virtual) != 0; }
}
public bool IsAbstract {
get { return (meth_attr & PEAPI.MethAttr.Abstract) != 0; }
}
public Location StartLocation {
get { return start; }
}
public DeclSecurity DeclSecurity {
get {
if (decl_sec == null)
decl_sec = new DeclSecurity ();
return decl_sec;
}
}
public string FullName {
get {
if (type_def == null)
return Name;
return type_def.FullName + "." + Name;
}
}
public BaseTypeRef[] ParamTypeList () {
if (param_list == null)
return new BaseTypeRef[0];
int count = 0;
BaseTypeRef[] type_list = new BaseTypeRef[param_list.Count];
foreach (ParamDef param in param_list) {
type_list[count++] = param.Type;
}
return type_list;
}
public void AddPInvokeInfo (PEAPI.PInvokeAttr pinvoke_attr, ExternModule pinvoke_mod,
string pinvoke_name)
{
this.pinvoke_attr = pinvoke_attr;
this.pinvoke_mod = pinvoke_mod;
this.pinvoke_name = pinvoke_name;
pinvoke_info = true;
}
public int GenParamCount {
get { return (gen_params != null ? gen_params.Count : 0); }
}
public GenericParameter GetGenericParam (string id)
{
if (gen_params == null)
return null;
return gen_params.GetGenericParam (id);
}
public GenericParameter GetGenericParam (int index)
{
if (gen_params == null || index < 0 || index >= gen_params.Count)
return null;
return gen_params [index];
}
public int GetGenericParamNum (string id)
{
if (gen_params == null)
return -1;
return gen_params.GetGenericParamNum (id);
}
public void AddCustomAttribute (CustomAttr customattr)
{
if (customattr_list == null)
customattr_list = new ArrayList ();
customattr_list.Add (customattr);
}
public void AddRetTypeMarshalInfo (PEAPI.NativeType native_type)
{
this.ret_param.AddMarshalInfo (native_type);
}
public void AddLocals (ArrayList local_list)
{
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;
if(!named_local_table.Contains(local.Name))
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 int GetNamedLocalSlot (string name)
{
Local local = (Local) named_local_table[name];
return local.Slot;
}
public int GetNamedParamPos (string name)
{
int pos = -1;
if (param_list == null)
return -1;
if (!IsStatic)
pos ++;
foreach (ParamDef param in param_list) {
pos ++;
if (param.Name.CompareTo (name) == 0)
return pos;
}
return pos;
}
public LocalVariableEntry[] GetLocalVars()
{
System.IO.MemoryStream str = new System.IO.MemoryStream();
int i = 0;
int num_locals = named_local_table.Count;
LocalVariableEntry[] locals = new LocalVariableEntry[num_locals];
foreach (Local local in local_list) {
if (local.Name != null) { // only named variables
locals[i++] = new LocalVariableEntry(local.Slot, local.Name, 0);
}
}
return locals;
}
/* index - 0: return type
* 1: params start from this
*/
public ParamDef GetParam (int index)
{
if (index == 0)
return ret_param;
if ((param_list == null) || (index < 0) || (index > param_list.Count))
return null;
index --; /* param_list has params zero-based */
if (param_list [index] != null)
return (ParamDef)param_list [index];
else
return null;
}
public void InitLocals ()
{
init_locals = true;
}
public void EntryPoint ()
{
if (!IsStatic)
Report.Error ("Non-static method as entrypoint.");
entry_point = true;
}
public void ZeroInit ()
{
zero_init = true;
}
public void SetMaxStack (int max_stack)
{
this.max_stack = max_stack;
}
public void ResolveGenParam (PEAPI.GenParam gpar)
{
if (gpar.Index != -1)
return;
if (gpar.Type == PEAPI.GenParamType.MVar)
gpar.Index = GetGenericParamNum (gpar.Name);
else
gpar.Index = type_def.GetGenericParamNum (gpar.Name);
if (gpar.Index < 0)
Report.Error (String.Format ("Invalid {0}type parameter '{1}'",
(gpar.Type == PEAPI.GenParamType.MVar ? "method " : ""),
gpar.Name));
}
public void ResolveGenParams ()
{
GenericParameters type_params = (type_def != null) ? type_def.TypeParameters : null;
if (gen_params == null && type_params == null)
return;
if (gen_params != null)
gen_params.ResolveConstraints (type_params, gen_params);
BaseGenericTypeRef gtr = RetType as BaseGenericTypeRef;
if (gtr != null)
gtr.Resolve (type_params, gen_params);
if (param_list == null)
return;
foreach (ParamDef param in param_list) {
gtr = param.Type as BaseGenericTypeRef;
if (gtr != null)
gtr.Resolve (type_params, gen_params);
}
}
public PEAPI.MethodDef Resolve (CodeGen code_gen)
{
return Resolve (code_gen, null);
}
public PEAPI.MethodDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
{
if (is_resolved)
return methoddef;
PEAPI.Param [] param_array = GenerateParams (code_gen);
FixAttributes ();
ret_param.Define (code_gen);
if (classdef == null)
methoddef = code_gen.PEFile.AddMethod (meth_attr, impl_attr,
name, ret_param.PeapiParam, param_array);
else
methoddef = classdef.AddMethod (meth_attr, impl_attr,
name, ret_param.PeapiParam, param_array);
methoddef.AddCallConv (call_conv);
is_resolved = true;
return methoddef;
}
private PEAPI.Param [] GenerateParams (CodeGen code_gen)
{
PEAPI.Param[] param_array;
if (param_list != null && param_list.Count > 0) {
int param_count = param_list.Count;
// Remove the last param if its the sentinel, not sure what
// should happen with more then one sentinel
ParamDef last = (ParamDef) param_list [param_count-1];
if (last.IsSentinel ())
param_count--;
param_array = new PEAPI.Param [param_count];
for (int i = 0; i < param_count; i++) {
ParamDef paramdef = (ParamDef) param_list [i];
paramdef.Define (code_gen);
param_array [i] = paramdef.PeapiParam;
}
} else {
param_array = new PEAPI.Param [0];
}
return param_array;
}
public PEAPI.MethodRef GetVarargSig (PEAPI.Type[] opt)
{
if (!is_resolved)
throw new InternalErrorException ("Methods must be resolved before a vararg sig can be created.");
PEAPI.MethodRef methref = null;
StringBuilder sigbuilder = new StringBuilder ();
string sig;
foreach (PEAPI.Type t in opt)
sigbuilder.Append (opt + ", ");
sig = sigbuilder.ToString ();
if (vararg_sig_table == null) {
vararg_sig_table = new Hashtable ();
} else {
methref = vararg_sig_table [sig] as PEAPI.MethodRef;
}
if (methref == null) {
methref = methoddef.MakeVarArgSignature (opt);
vararg_sig_table [sig] = methref;
}
return methref;
}
/// <summary>
/// Define a member method
/// </summary>
public void Define (CodeGen code_gen)
{
if (is_defined)
return;
if (type_def == null)
/* Global method */
Resolve (code_gen, null);
else
Resolve (code_gen, (PEAPI.ClassDef) type_def.ClassDef);
WriteCode (code_gen, methoddef);
//code_gen.Report.Message (String.Format ("Assembled method {0}::{1}", typedef.FullName, name));
is_defined = true;
}
public void AddInstr (IInstr instr)
{
inst_list.Add (instr);
}
protected void WriteCode (CodeGen code_gen, PEAPI.MethodDef methoddef)
{
/// Add the custrom attributes to this method
if (customattr_list != null)
foreach (CustomAttr customattr in customattr_list) {
customattr.AddTo (code_gen, methoddef);
if (customattr.IsSuppressUnmanaged (code_gen))
methoddef.AddMethAttribute (PEAPI.MethAttr.HasSecurity);
}
/// Add declarative security to this method
if (decl_sec != null) {
decl_sec.AddTo (code_gen, methoddef);
methoddef.AddMethAttribute (PEAPI.MethAttr.HasSecurity);
}
// Generic type parameters
if (gen_params != null)
gen_params.Resolve (code_gen, methoddef);
if (type_def == null) {
//Global method
meth_attr &= ~PEAPI.MethAttr.Abstract;
meth_attr |= PEAPI.MethAttr.Static;
} else {
if ((inst_list.Count > 0) && type_def.IsInterface && !IsStatic)
Report.Error (start, "Method cannot have body if it is non-static declared in an interface");
if (IsAbstract) {
if (!type_def.IsAbstract)
Report.Error (start, String.Format ("Abstract method '{0}' in non-abstract class '{1}'",
Name, type_def.FullName));
if (inst_list.Count > 0)
Report.Error (start, "Method cannot have body if it is abstract.");
return;
}
}
if (entry_point)
methoddef.DeclareEntryPoint ();
if (local_list.Count > 0) {
int ec = Report.ErrorCount;
PEAPI.Local[] local_array = new PEAPI.Local[local_list.Count];
foreach (Local local in local_list)
local_array[local.Slot] = local.GetPeapiLocal (code_gen);
if (Report.ErrorCount > ec)
return;
if (zero_init)
init_locals = true;
methoddef.AddLocals (local_array, init_locals);
}
/// Nothing seems to work if maxstack is not set,
/// i need to find out if this NEEDs to be set
/// and what its default value should be
if (max_stack < 0)
max_stack = 8;
methoddef.SetMaxStack (max_stack);
if (pinvoke_info) {
methoddef.AddPInvokeInfo (pinvoke_mod.ModuleRef,
(pinvoke_name != null ? pinvoke_name : name), pinvoke_attr);
}
if ((impl_attr & PEAPI.ImplAttr.Runtime) == PEAPI.ImplAttr.Runtime) {
if (inst_list.Count > 0)
Report.Error (start, String.Format ("Method cannot have body if it is non-IL runtime-supplied, '{0}'",
FullName));
} else {
if (((impl_attr & PEAPI.ImplAttr.Native) != 0) ||
((impl_attr & PEAPI.ImplAttr.Unmanaged) != 0))
Report.Error (start, String.Format ("Cannot compile native/unmanaged method, '{0}'",
FullName));
}
if (inst_list.Count > 0) {
/* Has body */
if ((impl_attr & PEAPI.ImplAttr.InternalCall) != 0)
Report.Error (start, String.Format ("Method cannot have body if it is an internal call, '{0}'",
FullName));
if (pinvoke_info)
Report.Error (start, String.Format ("Method cannot have body if it is pinvoke, '{0}'",
FullName));
} else {
if (pinvoke_info ||
((impl_attr & PEAPI.ImplAttr.Runtime) != 0) ||
((impl_attr & PEAPI.ImplAttr.InternalCall) != 0))
/* No body required */
return;
Report.Warning (start, "Method has no body, 'ret' emitted.");
AddInstr (new SimpInstr (PEAPI.Op.ret, start));
}
PEAPI.CILInstructions cil = methoddef.CreateCodeBuffer ();
/// Create all the labels
/// TODO: Most labels don't actually need to be created so we could
/// probably only create the ones that need to be
LabelInfo[] label_info = new LabelInfo[label_table.Count + label_list.Count];
label_table.Values.CopyTo (label_info, 0);
label_list.CopyTo (label_info, label_table.Count);
int previous_pos = -1;
LabelInfo previous_label = null;
Array.Sort (label_info);
foreach (LabelInfo label in label_info) {
if (label.UseOffset) {
label.Define (new PEAPI.CILLabel (label.Offset));
continue;
}
if (label.Pos == previous_pos)
label.Label = previous_label.Label;
else
label.Define (cil.NewLabel ());
previous_label = label;
previous_pos = label.Pos;
}
// Set all the label refs
foreach (LabelInfo label in labelref_table.Values) {
LabelInfo def = (LabelInfo) label_table[label.Name];
if (def == null) {
Report.Error ("Undefined Label: " + label);
return;
}
label.Label = def.Label;
}
int label_pos = 0;
int next_label_pos = (label_info.Length > 0 ? label_info[0].Pos : -1);
for (int i=0; i<inst_list.Count; i++) {
IInstr instr = (IInstr) inst_list[i];
if (next_label_pos == i) {
cil.CodeLabel (label_info[label_pos].Label);
if (label_pos < label_info.Length) {
while (next_label_pos == i && ++label_pos < label_info.Length) {
if (label_info[label_pos].UseOffset)
cil.CodeLabel (label_info[label_pos].Label);
next_label_pos = label_info[label_pos].Pos;
}
}
if (label_pos >= label_info.Length)
next_label_pos = -1;
}
if (source != null)
source.MarkLocation (instr.Location.line, cil.Offset);
instr.Emit (code_gen, this, cil);
}
if (source != null)
source.MarkLocation (source.EndLine, cil.Offset);
}
public LabelInfo AddLabel (string name)
{
LabelInfo label_info = (LabelInfo) label_table[name];
if (label_info != null)
Report.Error ("Duplicate label '" + name + "'");
label_info = new LabelInfo (name, inst_list.Count);
label_table [name] = label_info;
return label_info;
}
public LabelInfo AddLabelRef (string name)
{
LabelInfo label_info = (LabelInfo) label_table[name];
if (label_info != null)
return label_info;
label_info = (LabelInfo) labelref_table[name];
if (label_info != null)
return label_info;
label_info = new LabelInfo (name, -1);
labelref_table.Add (name, label_info);
return label_info;
}
public LabelInfo AddLabel (int offset)
{
// We go pos + 1 so this line is not counted
LabelInfo label_info = new LabelInfo (null, inst_list.Count+1, (uint) offset);
label_list.Add (label_info);
return label_info;
}
public LabelInfo AddLabel ()
{
LabelInfo label_info = new LabelInfo (null, inst_list.Count);
label_list.Add (label_info);
return label_info;
}
public PEAPI.CILLabel GetLabelDef (string name)
{
LabelInfo label_info = (LabelInfo) label_table[name];
return label_info.Label;
}
public PEAPI.CILLabel GetLabelDef (int pos)
{
foreach (LabelInfo li in label_list) {
if (li.Pos == pos)
return li.Label;
}
return null;
}
private void CreateSignature ()
{
if (IsVararg)
signature = CreateVarargSignature (RetType, name, param_list);
else
signature = CreateSignature (RetType, name, param_list, GenParamCount);
}
public static string CreateSignature (BaseTypeRef RetType, string name, IList param_list, int gen_param_count)
{
StringBuilder builder = new StringBuilder ();
builder.Append (RetType.FullName);
builder.Append (" ");
builder.Append (name);
if (gen_param_count > 0)
builder.AppendFormat ("`{0}", gen_param_count);
builder.Append ('(');
if (param_list != null) {
bool first = true;
foreach (ParamDef paramdef in param_list) {
if (!first)
builder.Append (',');
builder.Append (paramdef.TypeName);
first = false;
}
}
builder.Append (')');
return builder.ToString ();
}
public static string CreateVarargSignature (BaseTypeRef RetType, string name, IList param_list)
{
StringBuilder builder = new StringBuilder ();
ParamDef last = null;
builder.Append (RetType.FullName);
builder.Append (" ");
builder.Append (name);
builder.Append ('(');
bool first = true;
if (param_list != null) {
foreach (ParamDef paramdef in param_list) {
if (!first)
builder.Append (',');
builder.Append (paramdef.TypeName);
first = false;
}
last = (ParamDef) param_list[param_list.Count - 1];
}
if (last == null || !last.IsSentinel ()) {
if (!first)
builder.Append (',');
builder.Append ("...");
}
builder.Append (')');
return builder.ToString ();
}
public static string CreateVarargSignature (BaseTypeRef RetType, string name, BaseTypeRef [] param_list)
{
StringBuilder builder = new StringBuilder ();
BaseTypeRef last = null;
builder.Append (RetType.FullName);
builder.Append (" ");
builder.Append (name);
builder.Append ('(');
bool first = true;
if (param_list != null && param_list.Length > 0) {
foreach (BaseTypeRef param in param_list) {
if (!first)
builder.Append (',');
builder.Append (param.FullName);
first = false;
last = param;
if (param is SentinelTypeRef)
break;
}
}
if (last == null || !(last is SentinelTypeRef)) {
if (!first)
builder.Append (',');
builder.Append ("...");
}
builder.Append (')');
return builder.ToString ();
}
public static string CreateSignature (BaseTypeRef RetType, string name, BaseTypeRef[] param_list, int gen_param_count)
{
StringBuilder builder = new StringBuilder ();
builder.Append (RetType.FullName);
builder.Append (" ");
builder.Append (name);
if (gen_param_count > 0)
builder.AppendFormat ("`{0}", gen_param_count);
builder.Append ('(');
if (param_list != null) {
bool first = true;
foreach (BaseTypeRef param in param_list) {
if (!first)
builder.Append (',');
builder.Append (param.FullName);
first = false;
if (param is SentinelTypeRef)
break;
}
}
builder.Append (')');
return builder.ToString ();
}
private void FixAttributes ()
{
if (name == ".ctor" || name == ".cctor")
meth_attr |= PEAPI.MethAttr.SpecialName | PEAPI.MethAttr.RTSpecialName;
// If methods aren't flagged as static they are instance
if ((PEAPI.MethAttr.Static & meth_attr) == 0)
call_conv |= PEAPI.CallConv.Instance;
}
}
}
|