3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // System.Reflection.Emit/ILGenerator.cs
29 // Paolo Molaro (lupus@ximian.com)
31 // (C) 2001 Ximian, Inc. http://www.ximian.com
35 using System.Collections;
36 using System.Diagnostics.SymbolStore;
37 using System.Runtime.InteropServices;
39 namespace System.Reflection.Emit {
41 internal struct ILExceptionBlock {
42 public const int CATCH = 0;
43 public const int FILTER = 1;
44 public const int FINALLY = 2;
45 public const int FAULT = 4;
51 internal int filter_offset;
53 internal void Debug () {
55 System.Console.Write ("\ttype="+type.ToString()+" start="+start.ToString()+" len="+len.ToString());
57 System.Console.WriteLine (" extype="+extype.ToString());
59 System.Console.WriteLine ("");
63 internal struct ILExceptionInfo {
64 ILExceptionBlock[] handlers;
69 internal int NumHandlers ()
71 return handlers.Length;
74 internal void AddCatch (Type extype, int offset)
79 i = handlers.Length - 1;
80 handlers [i].type = ILExceptionBlock.CATCH;
81 handlers [i].start = offset;
82 handlers [i].extype = extype;
85 internal void AddFinally (int offset)
90 i = handlers.Length - 1;
91 handlers [i].type = ILExceptionBlock.FINALLY;
92 handlers [i].start = offset;
93 handlers [i].extype = null;
96 internal void AddFault (int offset)
101 i = handlers.Length - 1;
102 handlers [i].type = ILExceptionBlock.FAULT;
103 handlers [i].start = offset;
104 handlers [i].extype = null;
107 internal void AddFilter (int offset)
112 i = handlers.Length - 1;
113 handlers [i].type = ILExceptionBlock.FILTER;
114 handlers [i].extype = null;
115 handlers [i].filter_offset = offset;
118 internal void End (int offset)
120 if (handlers == null)
122 int i = handlers.Length - 1;
124 handlers [i].len = offset - handlers [i].start;
127 internal int LastClauseType ()
129 if (handlers != null)
130 return handlers [handlers.Length-1].type;
132 return ILExceptionBlock.CATCH;
135 internal void PatchLastClauseStart (int start)
137 if (handlers != null && handlers.Length > 0)
138 handlers [handlers.Length - 1].start = start;
141 internal void Debug (int b)
144 System.Console.WriteLine ("Handler {0} at {1}, len: {2}", b, start, len);
145 for (int i = 0; i < handlers.Length; ++i)
146 handlers [i].Debug ();
150 void add_block (int offset)
152 if (handlers != null) {
153 int i = handlers.Length;
154 ILExceptionBlock[] new_b = new ILExceptionBlock [i + 1];
155 System.Array.Copy (handlers, new_b, i);
157 handlers [i].len = offset - handlers [i].start;
159 handlers = new ILExceptionBlock [1];
160 len = offset - start;
165 internal struct ILTokenInfo {
166 public MemberInfo member;
170 internal interface TokenGenerator {
171 int GetToken (string str);
173 int GetToken (MemberInfo member);
175 int GetToken (MethodInfo method, Type[] opt_param_types);
177 int GetToken (SignatureHelper helper);
183 [ClassInterface (ClassInterfaceType.None)]
184 public class ILGenerator: _ILGenerator {
185 private struct LabelFixup {
186 public int offset; // The number of bytes between pos and the
187 // offset of the jump
188 public int pos; // Where offset of the label is placed
189 public int label_idx; // The label to jump to
193 public LabelData (int addr, int maxStack)
196 this.maxStack = maxStack;
203 static readonly Type void_type = typeof (void);
204 #region Sync with reflection.h
206 private int code_len;
207 private int max_stack;
208 private int cur_stack;
209 private LocalBuilder[] locals;
210 private ILExceptionInfo[] ex_handlers;
211 private int num_token_fixups;
212 private ILTokenInfo[] token_fixups;
215 private LabelData [] labels;
216 private int num_labels;
217 private LabelFixup[] fixups;
218 private int num_fixups;
219 internal Module module;
220 private Stack scopes;
221 private int cur_block;
222 private Stack open_blocks;
223 private TokenGenerator token_gen;
225 const int defaultFixupSize = 8;
226 const int defaultLabelsSize = 8;
227 ArrayList sequencePointLists;
228 SequencePointList currentSequence;
230 internal ILGenerator (Module m, TokenGenerator token_gen, int size)
235 code = new byte [size];
236 cur_stack = max_stack = 0;
237 num_fixups = num_labels = 0;
238 token_fixups = new ILTokenInfo [8];
239 num_token_fixups = 0;
241 open_blocks = new Stack ();
242 this.token_gen = token_gen;
245 private void add_token_fixup (MemberInfo mi)
247 if (num_token_fixups == token_fixups.Length) {
248 ILTokenInfo[] ntf = new ILTokenInfo [num_token_fixups * 2];
249 token_fixups.CopyTo (ntf, 0);
252 token_fixups [num_token_fixups].member = mi;
253 token_fixups [num_token_fixups++].code_pos = code_len;
256 private void make_room (int nbytes)
258 if (code_len + nbytes < code.Length)
260 byte[] new_code = new byte [(code_len + nbytes) * 2 + 128];
261 System.Array.Copy (code, 0, new_code, 0, code.Length);
265 private void emit_int (int val)
267 code [code_len++] = (byte) (val & 0xFF);
268 code [code_len++] = (byte) ((val >> 8) & 0xFF);
269 code [code_len++] = (byte) ((val >> 16) & 0xFF);
270 code [code_len++] = (byte) ((val >> 24) & 0xFF);
273 /* change to pass by ref to avoid copy */
274 private void ll_emit (OpCode opcode)
277 * there is already enough room allocated in code.
279 // access op1 and op2 directly since the Value property is useless
280 if (opcode.Size == 2)
281 code [code_len++] = opcode.op1;
282 code [code_len++] = opcode.op2;
284 * We should probably keep track of stack needs here.
285 * Or we may want to run the verifier on the code before saving it
286 * (this may be needed anyway when the ILGenerator is not used...).
288 switch (opcode.StackBehaviourPush) {
289 case StackBehaviour.Push1:
290 case StackBehaviour.Pushi:
291 case StackBehaviour.Pushi8:
292 case StackBehaviour.Pushr4:
293 case StackBehaviour.Pushr8:
294 case StackBehaviour.Pushref:
295 case StackBehaviour.Varpush: /* again we are conservative and assume it pushes 1 */
298 case StackBehaviour.Push1_push1:
302 if (max_stack < cur_stack)
303 max_stack = cur_stack;
306 * Note that we adjust for the pop behaviour _after_ setting max_stack.
308 switch (opcode.StackBehaviourPop) {
309 case StackBehaviour.Varpop:
310 break; /* we are conservative and assume it doesn't decrease the stack needs */
311 case StackBehaviour.Pop1:
312 case StackBehaviour.Popi:
313 case StackBehaviour.Popref:
316 case StackBehaviour.Pop1_pop1:
317 case StackBehaviour.Popi_pop1:
318 case StackBehaviour.Popi_popi:
319 case StackBehaviour.Popi_popi8:
320 case StackBehaviour.Popi_popr4:
321 case StackBehaviour.Popi_popr8:
322 case StackBehaviour.Popref_pop1:
323 case StackBehaviour.Popref_popi:
326 case StackBehaviour.Popi_popi_popi:
327 case StackBehaviour.Popref_popi_popi:
328 case StackBehaviour.Popref_popi_popi8:
329 case StackBehaviour.Popref_popi_popr4:
330 case StackBehaviour.Popref_popi_popr8:
331 case StackBehaviour.Popref_popi_popref:
337 private static int target_len (OpCode opcode)
339 if (opcode.OperandType == OperandType.InlineBrTarget)
344 private void InternalEndClause ()
346 switch (ex_handlers [cur_block].LastClauseType ()) {
347 case ILExceptionBlock.CATCH:
348 case ILExceptionBlock.FILTER:
349 // how could we optimize code size here?
350 Emit (OpCodes.Leave, ex_handlers [cur_block].end);
352 case ILExceptionBlock.FAULT:
353 case ILExceptionBlock.FINALLY:
354 Emit (OpCodes.Endfinally);
359 public virtual void BeginCatchBlock (Type exceptionType)
361 if (open_blocks.Count <= 0)
362 throw new NotSupportedException ("Not in an exception block");
364 if (ex_handlers [cur_block].LastClauseType () == ILExceptionBlock.FILTER) {
365 if (exceptionType != null)
366 throw new ArgumentException ("Do not supply an exception type for filter clause");
367 Emit (OpCodes.Endfilter);
368 ex_handlers [cur_block].PatchLastClauseStart (code_len);
370 InternalEndClause ();
371 ex_handlers [cur_block].AddCatch (exceptionType, code_len);
374 cur_stack = 1; // the exception object is on the stack by default
375 if (max_stack < cur_stack)
376 max_stack = cur_stack;
378 //System.Console.WriteLine ("Begin catch Block: {0} {1}",exceptionType.ToString(), max_stack);
381 public virtual void BeginExceptFilterBlock ()
383 if (open_blocks.Count <= 0)
384 throw new NotSupportedException ("Not in an exception block");
385 InternalEndClause ();
387 ex_handlers [cur_block].AddFilter (code_len);
390 public virtual Label BeginExceptionBlock ()
392 //System.Console.WriteLine ("Begin Block");
394 if (ex_handlers != null) {
395 cur_block = ex_handlers.Length;
396 ILExceptionInfo[] new_ex = new ILExceptionInfo [cur_block + 1];
397 System.Array.Copy (ex_handlers, new_ex, cur_block);
398 ex_handlers = new_ex;
400 ex_handlers = new ILExceptionInfo [1];
403 open_blocks.Push (cur_block);
404 ex_handlers [cur_block].start = code_len;
405 return ex_handlers [cur_block].end = DefineLabel ();
408 public virtual void BeginFaultBlock()
410 if (open_blocks.Count <= 0)
411 throw new NotSupportedException ("Not in an exception block");
412 InternalEndClause ();
413 //System.Console.WriteLine ("Begin fault Block");
414 ex_handlers [cur_block].AddFault (code_len);
417 public virtual void BeginFinallyBlock()
419 if (open_blocks.Count <= 0)
420 throw new NotSupportedException ("Not in an exception block");
421 InternalEndClause ();
422 //System.Console.WriteLine ("Begin finally Block");
423 ex_handlers [cur_block].AddFinally (code_len);
426 public virtual void BeginScope ()
429 public LocalBuilder DeclareLocal (Type localType)
431 return DeclareLocal (localType, false);
440 LocalBuilder DeclareLocal (Type localType, bool pinned)
442 if (localType == null)
443 throw new ArgumentNullException ("localType");
445 LocalBuilder res = new LocalBuilder (localType, this);
446 res.is_pinned = pinned;
448 if (locals != null) {
449 LocalBuilder[] new_l = new LocalBuilder [locals.Length + 1];
450 System.Array.Copy (locals, new_l, locals.Length);
451 new_l [locals.Length] = res;
454 locals = new LocalBuilder [1];
457 res.position = (ushort)(locals.Length - 1);
461 public virtual Label DefineLabel ()
464 labels = new LabelData [defaultLabelsSize];
465 else if (num_labels >= labels.Length) {
466 LabelData [] t = new LabelData [labels.Length * 2];
467 Array.Copy (labels, t, labels.Length);
471 labels [num_labels] = new LabelData (-1, 0);
473 return new Label (num_labels++);
476 public virtual void Emit (OpCode opcode)
482 public virtual void Emit (OpCode opcode, Byte val)
486 code [code_len++] = val;
492 public virtual void Emit (OpCode opcode, ConstructorInfo constructor)
494 int token = token_gen.GetToken (constructor);
497 if (constructor.DeclaringType.Module == module)
498 add_token_fixup (constructor);
501 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
502 cur_stack -= constructor.GetParameterCount ();
505 public virtual void Emit (OpCode opcode, double val)
507 Double.AssertEndianity (out val);
509 byte[] s = System.BitConverter.GetBytes (val);
512 if (BitConverter.IsLittleEndian){
513 System.Array.Copy (s, 0, code, code_len, 8);
516 code [code_len++] = s [7];
517 code [code_len++] = s [6];
518 code [code_len++] = s [5];
519 code [code_len++] = s [4];
520 code [code_len++] = s [3];
521 code [code_len++] = s [2];
522 code [code_len++] = s [1];
523 code [code_len++] = s [0];
527 public virtual void Emit (OpCode opcode, FieldInfo field)
529 int token = token_gen.GetToken (field);
532 if (field.DeclaringType.Module == module)
533 add_token_fixup (field);
537 public virtual void Emit (OpCode opcode, Int16 val)
541 code [code_len++] = (byte) (val & 0xFF);
542 code [code_len++] = (byte) ((val >> 8) & 0xFF);
545 public virtual void Emit (OpCode opcode, int val)
552 public virtual void Emit (OpCode opcode, long val)
556 code [code_len++] = (byte) (val & 0xFF);
557 code [code_len++] = (byte) ((val >> 8) & 0xFF);
558 code [code_len++] = (byte) ((val >> 16) & 0xFF);
559 code [code_len++] = (byte) ((val >> 24) & 0xFF);
560 code [code_len++] = (byte) ((val >> 32) & 0xFF);
561 code [code_len++] = (byte) ((val >> 40) & 0xFF);
562 code [code_len++] = (byte) ((val >> 48) & 0xFF);
563 code [code_len++] = (byte) ((val >> 56) & 0xFF);
566 public virtual void Emit (OpCode opcode, Label label)
568 int tlen = target_len (opcode);
571 if (cur_stack > labels [label.label].maxStack)
572 labels [label.label].maxStack = cur_stack;
575 fixups = new LabelFixup [defaultFixupSize];
576 else if (num_fixups >= fixups.Length) {
577 LabelFixup[] newf = new LabelFixup [fixups.Length + 16];
578 System.Array.Copy (fixups, newf, fixups.Length);
581 fixups [num_fixups].offset = tlen;
582 fixups [num_fixups].pos = code_len;
583 fixups [num_fixups].label_idx = label.label;
589 public virtual void Emit (OpCode opcode, Label[] labels)
591 /* opcode needs to be switch. */
592 int count = labels.Length;
593 make_room (6 + count * 4);
596 for (int i = 0; i < count; ++i)
597 if (cur_stack > this.labels [labels [i].label].maxStack)
598 this.labels [labels [i].label].maxStack = cur_stack;
602 fixups = new LabelFixup [defaultFixupSize + count];
603 else if (num_fixups + count >= fixups.Length) {
604 LabelFixup[] newf = new LabelFixup [fixups.Length + count + 16];
605 System.Array.Copy (fixups, newf, fixups.Length);
609 // ECMA 335, Partition III, p94 (7-10)
611 // The switch instruction implements a jump table. The format of
612 // the instruction is an unsigned int32 representing the number of targets N,
613 // followed by N int32 values specifying jump targets: these targets are
614 // represented as offsets (positive or negative) from the beginning of the
615 // instruction following this switch instruction.
617 // We must make sure it gets an offset from the *end* of the last label
618 // (eg, the beginning of the instruction following this).
620 // remaining is the number of bytes from the current instruction to the
621 // instruction that will be emitted.
623 for (int i = 0, remaining = count * 4; i < count; ++i, remaining -= 4) {
624 fixups [num_fixups].offset = remaining;
625 fixups [num_fixups].pos = code_len;
626 fixups [num_fixups].label_idx = labels [i].label;
632 public virtual void Emit (OpCode opcode, LocalBuilder lbuilder)
634 uint pos = lbuilder.position;
635 bool load_addr = false;
636 bool is_store = false;
639 if (lbuilder.ilgen != this)
640 throw new Exception ("Trying to emit a local from a different ILGenerator.");
642 /* inline the code from ll_emit () to optimize il code size */
643 if (opcode.StackBehaviourPop == StackBehaviour.Pop1) {
648 if (cur_stack > max_stack)
649 max_stack = cur_stack;
650 load_addr = opcode.StackBehaviourPush == StackBehaviour.Pushi;
654 code [code_len++] = (byte)0x12;
655 code [code_len++] = (byte)pos;
657 code [code_len++] = (byte)0xfe;
658 code [code_len++] = (byte)0x0d;
659 code [code_len++] = (byte)(pos & 0xff);
660 code [code_len++] = (byte)((pos >> 8) & 0xff);
665 code [code_len++] = (byte)(0x0a + pos);
666 } else if (pos < 256) {
667 code [code_len++] = (byte)0x13;
668 code [code_len++] = (byte)pos;
670 code [code_len++] = (byte)0xfe;
671 code [code_len++] = (byte)0x0e;
672 code [code_len++] = (byte)(pos & 0xff);
673 code [code_len++] = (byte)((pos >> 8) & 0xff);
677 code [code_len++] = (byte)(0x06 + pos);
678 } else if (pos < 256) {
679 code [code_len++] = (byte)0x11;
680 code [code_len++] = (byte)pos;
682 code [code_len++] = (byte)0xfe;
683 code [code_len++] = (byte)0x0c;
684 code [code_len++] = (byte)(pos & 0xff);
685 code [code_len++] = (byte)((pos >> 8) & 0xff);
691 public virtual void Emit (OpCode opcode, MethodInfo method)
694 throw new ArgumentNullException ("method");
696 int token = token_gen.GetToken (method);
699 if (method.DeclaringType.Module == module)
700 add_token_fixup (method);
702 if (method.ReturnType != void_type)
705 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
706 cur_stack -= method.GetParameterCount ();
709 private void Emit (OpCode opcode, MethodInfo method, int token)
713 if (method.DeclaringType.Module == module)
714 add_token_fixup (method);
716 if (method.ReturnType != void_type)
719 if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
720 cur_stack -= method.GetParameterCount ();
723 [CLSCompliant(false)]
724 public void Emit (OpCode opcode, sbyte val)
728 code [code_len++] = (byte)val;
731 public virtual void Emit (OpCode opcode, SignatureHelper shelper)
733 int token = token_gen.GetToken (shelper);
739 public virtual void Emit (OpCode opcode, float val)
741 byte[] s = System.BitConverter.GetBytes (val);
744 if (BitConverter.IsLittleEndian){
745 System.Array.Copy (s, 0, code, code_len, 4);
748 code [code_len++] = s [3];
749 code [code_len++] = s [2];
750 code [code_len++] = s [1];
751 code [code_len++] = s [0];
755 public virtual void Emit (OpCode opcode, string val)
757 int token = token_gen.GetToken (val);
763 public virtual void Emit (OpCode opcode, Type type)
767 emit_int (token_gen.GetToken (type));
770 [MonoTODO ("Do something about varargs method")]
771 public void EmitCall (OpCode opcode, MethodInfo methodinfo, Type[] optionalParamTypes)
773 if (methodinfo == null)
774 throw new ArgumentNullException ("methodinfo can not be null");
775 short value = opcode.Value;
776 if (!(value == OpCodes.Call.Value || value == OpCodes.Callvirt.Value))
777 throw new NotSupportedException ("Only Call and CallVirt are allowed");
778 if (optionalParamTypes != null){
779 if ((methodinfo.CallingConvention & CallingConventions.VarArgs) == 0){
780 throw new InvalidOperationException ("Method is not VarArgs method and optional types were passed");
783 int token = token_gen.GetToken (methodinfo, optionalParamTypes);
784 Emit (opcode, methodinfo, token);
787 Emit (opcode, methodinfo);
790 public void EmitCalli (OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] paramTypes)
792 SignatureHelper helper
793 = SignatureHelper.GetMethodSigHelper (module, 0, unmanagedCallConv, returnType, paramTypes);
794 Emit (opcode, helper);
797 public void EmitCalli (OpCode opcode, CallingConventions callConv, Type returnType, Type[] paramTypes, Type[] optionalParamTypes)
799 if (optionalParamTypes != null)
800 throw new NotImplementedException ();
802 SignatureHelper helper
803 = SignatureHelper.GetMethodSigHelper (module, callConv, 0, returnType, paramTypes);
804 Emit (opcode, helper);
807 public virtual void EmitWriteLine (FieldInfo field)
810 throw new ArgumentNullException ("field");
812 // The MS implementation does not check for valuetypes here but it
813 // should. Also, it should check that if the field is not static,
814 // then it is a member of this type.
816 Emit (OpCodes.Ldsfld, field);
818 Emit (OpCodes.Ldarg_0);
819 Emit (OpCodes.Ldfld, field);
822 typeof (Console).GetMethod ("WriteLine",
823 new Type[1] { field.FieldType }));
826 public virtual void EmitWriteLine (LocalBuilder lbuilder)
828 if (lbuilder == null)
829 throw new ArgumentNullException ("lbuilder");
830 if (lbuilder.LocalType is TypeBuilder)
831 throw new ArgumentException ("Output streams do not support TypeBuilders.");
832 // The MS implementation does not check for valuetypes here but it
834 Emit (OpCodes.Ldloc, lbuilder);
836 typeof (Console).GetMethod ("WriteLine",
837 new Type[1] { lbuilder.LocalType }));
840 public virtual void EmitWriteLine (string val)
842 Emit (OpCodes.Ldstr, val);
844 typeof (Console).GetMethod ("WriteLine",
845 new Type[1] { typeof(string)}));
848 public virtual void EndExceptionBlock ()
850 if (open_blocks.Count <= 0)
851 throw new NotSupportedException ("Not in an exception block");
852 InternalEndClause ();
853 MarkLabel (ex_handlers [cur_block].end);
854 ex_handlers [cur_block].End (code_len);
855 ex_handlers [cur_block].Debug (cur_block);
856 //System.Console.WriteLine ("End Block {0} (handlers: {1})", cur_block, ex_handlers [cur_block].NumHandlers ());
858 if (open_blocks.Count > 0)
859 cur_block = (int)open_blocks.Peek ();
860 //Console.WriteLine ("curblock restored to {0}", cur_block);
861 //throw new NotImplementedException ();
864 public virtual void EndScope ()
867 public virtual void MarkLabel (Label loc)
869 if (loc.label < 0 || loc.label >= num_labels)
870 throw new System.ArgumentException ("The label is not valid");
871 if (labels [loc.label].addr >= 0)
872 throw new System.ArgumentException ("The label was already defined");
873 labels [loc.label].addr = code_len;
874 if (labels [loc.label].maxStack > cur_stack)
875 cur_stack = labels [loc.label].maxStack;
878 public virtual void MarkSequencePoint (ISymbolDocumentWriter document, int startLine,
879 int startColumn, int endLine, int endColumn)
881 if (currentSequence == null || currentSequence.Document != document) {
882 if (sequencePointLists == null)
883 sequencePointLists = new ArrayList ();
884 currentSequence = new SequencePointList (document);
885 sequencePointLists.Add (currentSequence);
888 currentSequence.AddSequencePoint (code_len, startLine, startColumn, endLine, endColumn);
891 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
893 if (sequencePointLists != null) {
894 SequencePointList first = (SequencePointList) sequencePointLists [0];
895 SequencePointList last = (SequencePointList) sequencePointLists [sequencePointLists.Count - 1];
896 symbolWriter.SetMethodSourceRange (first.Document, first.StartLine, first.StartColumn, last.Document, last.EndLine, last.EndColumn);
898 foreach (SequencePointList list in sequencePointLists)
899 symbolWriter.DefineSequencePoints (list.Document, list.GetOffsets(), list.GetLines(), list.GetColumns(), list.GetEndLines(), list.GetEndColumns());
901 if (locals != null) {
902 foreach (LocalBuilder local in locals) {
903 if (local.Name != null && local.Name.Length > 0) {
904 SignatureHelper sighelper = SignatureHelper.GetLocalVarSigHelper (module);
905 sighelper.AddArgument (local.LocalType);
906 byte[] signature = sighelper.GetSignature ();
907 symbolWriter.DefineLocalVariable (local.Name, FieldAttributes.Public, signature, SymAddressKind.ILOffset, local.position, 0, 0, local.StartOffset, local.EndOffset);
911 sequencePointLists = null;
915 internal bool HasDebugInfo
917 get { return sequencePointLists != null; }
920 public virtual void ThrowException (Type exceptionType)
922 if (exceptionType == null)
923 throw new ArgumentNullException ("exceptionType");
924 if (! ((exceptionType == typeof (Exception)) ||
925 exceptionType.IsSubclassOf (typeof (Exception))))
926 throw new ArgumentException ("Type should be an exception type", "exceptionType");
927 ConstructorInfo ctor = exceptionType.GetConstructor (new Type[0]);
929 throw new ArgumentException ("Type should have a default constructor", "exceptionType");
930 Emit (OpCodes.Newobj, ctor);
931 Emit (OpCodes.Throw);
935 public void UsingNamespace (String usingNamespace)
937 throw new NotImplementedException ();
940 internal void label_fixup ()
942 for (int i = 0; i < num_fixups; ++i) {
944 // Diff is the offset from the end of the jump instruction to the address of the label
945 int diff = labels [fixups [i].label_idx].addr - (fixups [i].pos + fixups [i].offset);
946 if (fixups [i].offset == 1) {
947 code [fixups [i].pos] = (byte)((sbyte) diff);
949 int old_cl = code_len;
950 code_len = fixups [i].pos;
957 internal static int Mono_GetCurrentOffset (ILGenerator ig)
962 void _ILGenerator.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
964 throw new NotImplementedException ();
967 void _ILGenerator.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
969 throw new NotImplementedException ();
972 void _ILGenerator.GetTypeInfoCount (out uint pcTInfo)
974 throw new NotImplementedException ();
977 void _ILGenerator.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
979 throw new NotImplementedException ();
983 internal class SequencePointList
985 ISymbolDocumentWriter doc;
986 SequencePoint[] points;
988 const int arrayGrow = 10;
990 public SequencePointList (ISymbolDocumentWriter doc)
995 public ISymbolDocumentWriter Document {
999 public int[] GetOffsets()
1001 int[] data = new int [count];
1002 for (int n=0; n<count; n++) data [n] = points[n].Offset;
1005 public int[] GetLines()
1007 int[] data = new int [count];
1008 for (int n=0; n<count; n++) data [n] = points[n].Line;
1011 public int[] GetColumns()
1013 int[] data = new int [count];
1014 for (int n=0; n<count; n++) data [n] = points[n].Col;
1017 public int[] GetEndLines()
1019 int[] data = new int [count];
1020 for (int n=0; n<count; n++) data [n] = points[n].EndLine;
1023 public int[] GetEndColumns()
1025 int[] data = new int [count];
1026 for (int n=0; n<count; n++) data [n] = points[n].EndCol;
1029 public int StartLine {
1030 get { return points[0].Line; }
1032 public int EndLine {
1033 get { return points[count - 1].Line; }
1035 public int StartColumn {
1036 get { return points[0].Col; }
1038 public int EndColumn {
1039 get { return points[count - 1].Col; }
1042 public void AddSequencePoint (int offset, int line, int col, int endLine, int endCol)
1044 SequencePoint s = new SequencePoint ();
1048 s.EndLine = endLine;
1051 if (points == null) {
1052 points = new SequencePoint [arrayGrow];
1053 } else if (count >= points.Length) {
1054 SequencePoint[] temp = new SequencePoint [count + arrayGrow];
1055 Array.Copy (points, temp, points.Length);
1064 struct SequencePoint {