3 // System.Reflection.Emit/ILGenerator.cs
6 // Paolo Molaro (lupus@ximian.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 using System.Diagnostics.SymbolStore;
14 namespace System.Reflection.Emit {
16 internal struct ILExceptionBlock {
17 public const int CATCH = 0;
18 public const int FILTER = 1;
19 public const int FINALLY = 2;
20 public const int FAULT = 4;
26 internal int filter_offset;
28 internal void Debug () {
29 System.Console.Write ("\ttype="+type.ToString()+" start="+start.ToString());
30 System.Console.WriteLine (" len="+len.ToString()+" extype="+extype.ToString());
33 internal struct ILExceptionInfo {
34 ILExceptionBlock[] handlers;
39 internal void AddCatch (Type extype, int offset) {
42 i = handlers.Length - 1;
43 handlers [i].type = ILExceptionBlock.CATCH;
44 handlers [i].start = offset;
45 handlers [i].extype = extype;
48 internal void End (int offset) {
49 int i = handlers.Length - 1;
50 handlers [i].len = offset - handlers [i].start;
53 internal void Debug () {
54 System.Console.WriteLine ("Handler at "+start.ToString()+ " len: "+len.ToString());
55 for (int i = 0; i < handlers.Length; ++i)
56 handlers [i].Debug ();
59 void add_block (int offset) {
60 if (handlers != null) {
61 int i = handlers.Length;
62 ILExceptionBlock[] new_b = new ILExceptionBlock [i + 1];
63 System.Array.Copy (handlers, new_b, i);
65 handlers [i].len = offset - handlers [i].start;
67 handlers = new ILExceptionBlock [1];
73 public class ILGenerator: Object {
74 private struct LabelFixup {
80 private MethodBase mbuilder; /* a MethodBuilder or ConstructorBuilder */
82 private int max_stack;
83 private int cur_stack;
84 private LocalBuilder[] locals;
85 private ILExceptionInfo[] ex_handlers;
86 private int[] label_to_addr;
87 private int num_labels;
88 private LabelFixup[] fixups;
89 private int num_fixups;
90 private AssemblyBuilder abuilder;
91 private int cur_block;
93 internal ILGenerator (MethodBase mb, int size) {
97 code = new byte [size];
99 cur_stack = max_stack = 0;
100 num_fixups = num_labels = 0;
102 label_to_addr = new int [16];
103 fixups = new LabelFixup [16];
104 if (mb is MethodBuilder) {
105 abuilder = (AssemblyBuilder)((MethodBuilder)mb).TypeBuilder.Module.Assembly;
106 } else if (mb is ConstructorBuilder) {
107 abuilder = (AssemblyBuilder)((ConstructorBuilder)mb).TypeBuilder.Module.Assembly;
111 private void make_room (int nbytes) {
112 if (code_len + nbytes < code.Length)
114 byte[] new_code = new byte [code.Length * 2 + 128];
115 System.Array.Copy (code, 0, new_code, 0, code.Length);
118 private void emit_int (int val) {
119 code [code_len++] = (byte) (val & 0xFF);
120 code [code_len++] = (byte) ((val >> 8) & 0xFF);
121 code [code_len++] = (byte) ((val >> 16) & 0xFF);
122 code [code_len++] = (byte) ((val >> 24) & 0xFF);
124 /* change to pass by ref to avoid copy */
125 private void ll_emit (OpCode opcode) {
127 * there is already enough room allocated in code.
129 // access op1 and op2 directly since the Value property is useless
130 if (opcode.Size == 2)
131 code [code_len++] = opcode.op1;
132 code [code_len++] = opcode.op2;
134 * We should probably keep track of stack needs here.
135 * Or we may want to run the verifier on the code before saving it
136 * (this may be needed anyway when the ILGenerator is not used...).
138 switch (opcode.StackBehaviourPush) {
139 case StackBehaviour.Push1:
140 case StackBehaviour.Pushi:
141 case StackBehaviour.Pushi8:
142 case StackBehaviour.Pushr4:
143 case StackBehaviour.Pushr8:
144 case StackBehaviour.Pushref:
145 case StackBehaviour.Varpush: /* again we are conservative and assume it pushes 1 */
148 case StackBehaviour.Push1_push1:
152 if (max_stack < cur_stack)
153 max_stack = cur_stack;
155 * Note that we adjust for the pop behaviour _after_ setting max_stack.
157 switch (opcode.StackBehaviourPop) {
158 case StackBehaviour.Varpop:
159 break; /* we are conservative and assume it doesn't decrease the stack needs */
160 case StackBehaviour.Pop1:
161 case StackBehaviour.Popi:
162 case StackBehaviour.Popref:
165 case StackBehaviour.Pop1_pop1:
166 case StackBehaviour.Popi_pop1:
167 case StackBehaviour.Popi_popi:
168 case StackBehaviour.Popi_popi8:
169 case StackBehaviour.Popi_popr4:
170 case StackBehaviour.Popi_popr8:
171 case StackBehaviour.Popref_pop1:
172 case StackBehaviour.Popref_popi:
175 case StackBehaviour.Popi_popi_popi:
176 case StackBehaviour.Popref_popi_popi:
177 case StackBehaviour.Popref_popi_popi8:
178 case StackBehaviour.Popref_popi_popr4:
179 case StackBehaviour.Popref_popi_popr8:
180 case StackBehaviour.Popref_popi_popref:
186 private static int target_len (OpCode opcode) {
187 if (opcode.operandType == OperandType.InlineBrTarget)
192 public virtual void BeginCatchBlock (Type exceptionType) {
194 throw new NotSupportedException ("Not in an exception block");
195 // how could we optimize code size here?
196 Emit (OpCodes.Leave, ex_handlers [cur_block].end);
197 ex_handlers [cur_block].AddCatch (exceptionType, code_len);
198 System.Console.WriteLine ("Begin catch Block: "+exceptionType.ToString());
199 //throw new NotImplementedException ();
201 public virtual void BeginExceptFilterBlock () {
202 throw new NotImplementedException ();
204 public virtual Label BeginExceptionBlock () {
205 System.Console.WriteLine ("Begin Block");
208 if (ex_handlers != null) {
209 ILExceptionInfo[] new_ex = new ILExceptionInfo [cur_block + 1];
210 System.Array.Copy (ex_handlers, new_ex, cur_block);
211 ex_handlers = new_ex;
213 ex_handlers = new ILExceptionInfo [1];
215 ex_handlers [cur_block].start = code_len;
216 return ex_handlers [cur_block].end = DefineLabel ();
218 public virtual void BeginFaultBlock() {
220 throw new NotSupportedException ("Not in an exception block");
221 System.Console.WriteLine ("Begin fault Block");
222 //throw new NotImplementedException ();
224 public virtual void BeginFinallyBlock() {
226 throw new NotSupportedException ("Not in an exception block");
227 System.Console.WriteLine ("Begin finally Block");
228 //throw new NotImplementedException ();
230 public virtual void BeginScope () {
231 throw new NotImplementedException ();
233 public virtual LocalBuilder DeclareLocal (Type localType) {
234 LocalBuilder res = new LocalBuilder (localType);
235 if (locals != null) {
236 LocalBuilder[] new_l = new LocalBuilder [locals.Length + 1];
237 System.Array.Copy (locals, new_l, locals.Length);
238 new_l [locals.Length] = res;
241 locals = new LocalBuilder [1];
244 res.position = locals.Length - 1;
247 public virtual Label DefineLabel () {
248 if (num_labels >= label_to_addr.Length) {
249 int[] new_l = new int [label_to_addr.Length + 16];
250 System.Array.Copy (label_to_addr, new_l, label_to_addr.Length);
251 label_to_addr = new_l;
253 label_to_addr [num_labels] = -1;
254 return new Label (num_labels++);
256 public virtual void Emit (OpCode opcode) {
260 public virtual void Emit (OpCode opcode, Byte val) {
263 code [code_len++] = val;
265 public virtual void Emit (OpCode opcode, ConstructorInfo constructor) {
266 int token = abuilder.GetToken (constructor);
271 public virtual void Emit (OpCode opcode, Double val) {
272 byte[] s = System.BitConverter.GetBytes (val);
275 System.Array.Copy (s, 0, code, code_len, 8);
278 public virtual void Emit (OpCode opcode, FieldInfo field) {
279 int token = abuilder.GetToken (field);
284 public virtual void Emit (OpCode opcode, Int16 val) {
287 code [code_len++] = (byte) (val & 0xFF);
288 code [code_len++] = (byte) ((val >> 8) & 0xFF);
290 public virtual void Emit (OpCode opcode, Int32 val) {
295 public virtual void Emit (OpCode opcode, Int64 val) {
298 code [code_len++] = (byte) (val & 0xFF);
299 code [code_len++] = (byte) ((val >> 8) & 0xFF);
300 code [code_len++] = (byte) ((val >> 16) & 0xFF);
301 code [code_len++] = (byte) ((val >> 24) & 0xFF);
302 code [code_len++] = (byte) ((val >> 32) & 0xFF);
303 code [code_len++] = (byte) ((val >> 40) & 0xFF);
304 code [code_len++] = (byte) ((val >> 48) & 0xFF);
305 code [code_len++] = (byte) ((val >> 56) & 0xFF);
307 public virtual void Emit (OpCode opcode, Label label) {
308 int tlen = target_len (opcode);
311 if (num_fixups >= fixups.Length) {
312 LabelFixup[] newf = new LabelFixup [fixups.Length + 16];
313 System.Array.Copy (fixups, newf, fixups.Length);
316 fixups [num_fixups].size = tlen;
317 fixups [num_fixups].pos = code_len;
318 fixups [num_fixups].label_idx = label.label;
323 public virtual void Emit (OpCode opcode, Label[] labels) {
324 /* opcode needs to be switch. */
325 int count = labels.Length;
326 make_room (6 + count * 4);
329 if (num_fixups + count >= fixups.Length) {
330 LabelFixup[] newf = new LabelFixup [fixups.Length + count + 16];
331 System.Array.Copy (fixups, newf, fixups.Length);
334 for (int i = 0; i < count; ++i) {
335 fixups [num_fixups].size = 4;
336 fixups [num_fixups].pos = code_len;
337 fixups [num_fixups].label_idx = labels [i].label;
342 public virtual void Emit (OpCode opcode, LocalBuilder lbuilder) {
345 code [code_len++] = (byte) (lbuilder.position & 0xFF);
346 if (opcode.operandType == OperandType.InlineVar) {
347 code [code_len++] = (byte) ((lbuilder.position >> 8) & 0xFF);
350 public virtual void Emit (OpCode opcode, MethodInfo method) {
351 int token = abuilder.GetToken (method);
356 [CLSCompliant(false)]
357 public virtual void Emit (OpCode opcode, sbyte val) {
360 code [code_len++] = (byte)val;
364 public virtual void Emit (OpCode opcode, SignatureHelper shelper) {
365 int token = 0; // FIXME: request a token from the modulebuilder
370 public virtual void Emit (OpCode opcode, float val) {
371 byte[] s = System.BitConverter.GetBytes (val);
374 System.Array.Copy (s, 0, code, code_len, 4);
377 public virtual void Emit (OpCode opcode, string val) {
378 int token = abuilder.GetToken (val);
383 public virtual void Emit (OpCode opcode, Type type) {
386 emit_int (abuilder.GetToken (type));
389 public void EmitCall (OpCode opcode, MethodInfo methodinfo, Type[] optionalParamTypes) {
390 throw new NotImplementedException ();
392 public void EmitCalli (OpCode opcode, CallingConventions call_conv, Type returnType, Type[] paramTypes, Type[] optionalParamTypes) {
393 throw new NotImplementedException ();
396 public virtual void EmitWriteLine (FieldInfo field) {
397 throw new NotImplementedException ();
399 public virtual void EmitWriteLine (LocalBuilder lbuilder) {
400 throw new NotImplementedException ();
402 public virtual void EmitWriteLine (string val) {
403 throw new NotImplementedException ();
406 public virtual void EndExceptionBlock () {
408 throw new NotSupportedException ("Not in an exception block");
409 // how could we optimize code size here?
410 Emit (OpCodes.Leave, ex_handlers [cur_block].end);
411 MarkLabel (ex_handlers [cur_block].end);
412 ex_handlers [cur_block].End (code_len);
413 ex_handlers [cur_block].Debug ();
414 System.Console.WriteLine ("End Block");
415 //throw new NotImplementedException ();
417 public virtual void EndScope () {
418 throw new NotImplementedException ();
420 public virtual void MarkLabel (Label loc) {
421 if (loc.label < 0 || loc.label >= num_labels)
422 throw new System.ArgumentException ("The label is not valid");
423 if (label_to_addr [loc.label] >= 0)
424 throw new System.ArgumentException ("The label was already defined");
425 label_to_addr [loc.label] = code_len;
427 public virtual void MarkSequencePoint (ISymbolDocumentWriter document, int startLine, int startColumn, int endLine, int EndColumn) {
428 throw new NotImplementedException ();
430 public virtual void ThrowException (Type exceptionType) {
431 throw new NotImplementedException ();
433 public virtual void UsingNamespace (String usingNamespace) {
434 throw new NotImplementedException ();
437 internal void label_fixup () {
439 for (i = 0; i < num_fixups; ++i) {
440 int diff = label_to_addr [fixups [i].label_idx] - fixups [i].pos;
441 if (fixups [i].size == 1) {
442 code [fixups [i].pos] = (byte)((sbyte) diff - 1);
444 int old_cl = code_len;
445 code_len = fixups [i].pos;