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()+" len="+len.ToString());
31 System.Console.WriteLine (" extype="+extype.ToString());
33 System.Console.WriteLine ("");
36 internal struct ILExceptionInfo {
37 ILExceptionBlock[] handlers;
42 internal void AddCatch (Type extype, int offset) {
46 i = handlers.Length - 1;
47 handlers [i].type = ILExceptionBlock.CATCH;
48 handlers [i].start = offset;
49 handlers [i].extype = extype;
52 internal void AddFinally (int offset) {
56 i = handlers.Length - 1;
57 handlers [i].type = ILExceptionBlock.FINALLY;
58 handlers [i].start = offset;
59 handlers [i].extype = null;
62 internal void End (int offset) {
65 int i = handlers.Length - 1;
67 handlers [i].len = offset - handlers [i].start;
70 internal int LastClauseType () {
72 return handlers [handlers.Length-1].type;
74 return ILExceptionBlock.CATCH;
77 internal void Debug () {
79 System.Console.WriteLine ("Handler at "+start.ToString()+ " len: "+len.ToString());
80 for (int i = 0; i < handlers.Length; ++i)
81 handlers [i].Debug ();
85 void add_block (int offset) {
86 if (handlers != null) {
87 int i = handlers.Length;
88 ILExceptionBlock[] new_b = new ILExceptionBlock [i + 1];
89 System.Array.Copy (handlers, new_b, i);
91 handlers [i].len = offset - handlers [i].start;
93 handlers = new ILExceptionBlock [1];
99 public class ILGenerator: Object {
100 private struct LabelFixup {
103 public int label_idx;
106 private MethodBase mbuilder; /* a MethodBuilder or ConstructorBuilder */
107 private int code_len;
108 private int max_stack;
109 private int cur_stack;
110 private LocalBuilder[] locals;
111 private ILExceptionInfo[] ex_handlers;
112 private int[] label_to_addr;
113 private int num_labels;
114 private LabelFixup[] fixups;
115 private int num_fixups;
116 private AssemblyBuilder abuilder;
117 private int cur_block;
118 private int open_blocks;
120 internal ILGenerator (MethodBase mb, int size) {
124 code = new byte [size];
126 cur_stack = max_stack = 0;
127 num_fixups = num_labels = 0;
128 label_to_addr = new int [16];
129 fixups = new LabelFixup [16];
130 if (mb is MethodBuilder) {
131 abuilder = (AssemblyBuilder)((MethodBuilder)mb).TypeBuilder.Module.Assembly;
132 } else if (mb is ConstructorBuilder) {
133 abuilder = (AssemblyBuilder)((ConstructorBuilder)mb).TypeBuilder.Module.Assembly;
137 private void make_room (int nbytes) {
138 if (code_len + nbytes < code.Length)
140 byte[] new_code = new byte [code.Length * 2 + 128];
141 System.Array.Copy (code, 0, new_code, 0, code.Length);
144 private void emit_int (int val) {
145 code [code_len++] = (byte) (val & 0xFF);
146 code [code_len++] = (byte) ((val >> 8) & 0xFF);
147 code [code_len++] = (byte) ((val >> 16) & 0xFF);
148 code [code_len++] = (byte) ((val >> 24) & 0xFF);
150 /* change to pass by ref to avoid copy */
151 private void ll_emit (OpCode opcode) {
153 * there is already enough room allocated in code.
155 // access op1 and op2 directly since the Value property is useless
156 if (opcode.Size == 2)
157 code [code_len++] = opcode.op1;
158 code [code_len++] = opcode.op2;
160 * We should probably keep track of stack needs here.
161 * Or we may want to run the verifier on the code before saving it
162 * (this may be needed anyway when the ILGenerator is not used...).
164 switch (opcode.StackBehaviourPush) {
165 case StackBehaviour.Push1:
166 case StackBehaviour.Pushi:
167 case StackBehaviour.Pushi8:
168 case StackBehaviour.Pushr4:
169 case StackBehaviour.Pushr8:
170 case StackBehaviour.Pushref:
171 case StackBehaviour.Varpush: /* again we are conservative and assume it pushes 1 */
174 case StackBehaviour.Push1_push1:
178 if (max_stack < cur_stack)
179 max_stack = cur_stack;
181 * Note that we adjust for the pop behaviour _after_ setting max_stack.
183 switch (opcode.StackBehaviourPop) {
184 case StackBehaviour.Varpop:
185 break; /* we are conservative and assume it doesn't decrease the stack needs */
186 case StackBehaviour.Pop1:
187 case StackBehaviour.Popi:
188 case StackBehaviour.Popref:
191 case StackBehaviour.Pop1_pop1:
192 case StackBehaviour.Popi_pop1:
193 case StackBehaviour.Popi_popi:
194 case StackBehaviour.Popi_popi8:
195 case StackBehaviour.Popi_popr4:
196 case StackBehaviour.Popi_popr8:
197 case StackBehaviour.Popref_pop1:
198 case StackBehaviour.Popref_popi:
201 case StackBehaviour.Popi_popi_popi:
202 case StackBehaviour.Popref_popi_popi:
203 case StackBehaviour.Popref_popi_popi8:
204 case StackBehaviour.Popref_popi_popr4:
205 case StackBehaviour.Popref_popi_popr8:
206 case StackBehaviour.Popref_popi_popref:
212 private static int target_len (OpCode opcode) {
213 if (opcode.operandType == OperandType.InlineBrTarget)
218 private void InternalEndClause () {
219 switch (ex_handlers [cur_block].LastClauseType ()) {
220 case ILExceptionBlock.CATCH:
221 // how could we optimize code size here?
222 Emit (OpCodes.Leave, ex_handlers [cur_block].end);
224 case ILExceptionBlock.FAULT:
225 case ILExceptionBlock.FINALLY:
226 Emit (OpCodes.Endfinally);
228 case ILExceptionBlock.FILTER:
229 Emit (OpCodes.Endfilter);
234 public virtual void BeginCatchBlock (Type exceptionType) {
235 if (open_blocks <= 0)
236 throw new NotSupportedException ("Not in an exception block");
237 InternalEndClause ();
238 ex_handlers [cur_block].AddCatch (exceptionType, code_len);
239 //System.Console.WriteLine ("Begin catch Block: "+exceptionType.ToString());
240 //throw new NotImplementedException ();
242 public virtual void BeginExceptFilterBlock () {
243 throw new NotImplementedException ();
245 public virtual Label BeginExceptionBlock () {
246 //System.Console.WriteLine ("Begin Block");
248 if (ex_handlers != null) {
249 cur_block = ex_handlers.Length;
250 ILExceptionInfo[] new_ex = new ILExceptionInfo [cur_block + 1];
251 System.Array.Copy (ex_handlers, new_ex, cur_block);
252 ex_handlers = new_ex;
254 ex_handlers = new ILExceptionInfo [1];
258 ex_handlers [cur_block].start = code_len;
259 return ex_handlers [cur_block].end = DefineLabel ();
261 public virtual void BeginFaultBlock() {
262 if (open_blocks <= 0)
263 throw new NotSupportedException ("Not in an exception block");
264 //System.Console.WriteLine ("Begin fault Block");
265 //throw new NotImplementedException ();
267 public virtual void BeginFinallyBlock() {
268 if (open_blocks <= 0)
269 throw new NotSupportedException ("Not in an exception block");
270 //System.Console.WriteLine ("Begin finally Block");
271 InternalEndClause ();
272 ex_handlers [cur_block].AddFinally (code_len);
273 //throw new NotImplementedException ();
275 public virtual void BeginScope () {
276 throw new NotImplementedException ();
278 public virtual LocalBuilder DeclareLocal (Type localType) {
279 LocalBuilder res = new LocalBuilder (localType);
280 if (locals != null) {
281 LocalBuilder[] new_l = new LocalBuilder [locals.Length + 1];
282 System.Array.Copy (locals, new_l, locals.Length);
283 new_l [locals.Length] = res;
286 locals = new LocalBuilder [1];
289 res.position = locals.Length - 1;
292 public virtual Label DefineLabel () {
293 if (num_labels >= label_to_addr.Length) {
294 int[] new_l = new int [label_to_addr.Length + 16];
295 System.Array.Copy (label_to_addr, new_l, label_to_addr.Length);
296 label_to_addr = new_l;
298 label_to_addr [num_labels] = -1;
299 return new Label (num_labels++);
301 public virtual void Emit (OpCode opcode) {
305 public virtual void Emit (OpCode opcode, Byte val) {
308 code [code_len++] = val;
310 public virtual void Emit (OpCode opcode, ConstructorInfo constructor) {
311 int token = abuilder.GetToken (constructor);
316 public virtual void Emit (OpCode opcode, Double val) {
317 byte[] s = System.BitConverter.GetBytes (val);
320 System.Array.Copy (s, 0, code, code_len, 8);
323 public virtual void Emit (OpCode opcode, FieldInfo field) {
324 int token = abuilder.GetToken (field);
329 public virtual void Emit (OpCode opcode, Int16 val) {
332 code [code_len++] = (byte) (val & 0xFF);
333 code [code_len++] = (byte) ((val >> 8) & 0xFF);
335 public virtual void Emit (OpCode opcode, Int32 val) {
340 public virtual void Emit (OpCode opcode, Int64 val) {
343 code [code_len++] = (byte) (val & 0xFF);
344 code [code_len++] = (byte) ((val >> 8) & 0xFF);
345 code [code_len++] = (byte) ((val >> 16) & 0xFF);
346 code [code_len++] = (byte) ((val >> 24) & 0xFF);
347 code [code_len++] = (byte) ((val >> 32) & 0xFF);
348 code [code_len++] = (byte) ((val >> 40) & 0xFF);
349 code [code_len++] = (byte) ((val >> 48) & 0xFF);
350 code [code_len++] = (byte) ((val >> 56) & 0xFF);
352 public virtual void Emit (OpCode opcode, Label label) {
353 int tlen = target_len (opcode);
356 if (num_fixups >= fixups.Length) {
357 LabelFixup[] newf = new LabelFixup [fixups.Length + 16];
358 System.Array.Copy (fixups, newf, fixups.Length);
361 fixups [num_fixups].size = tlen;
362 fixups [num_fixups].pos = code_len;
363 fixups [num_fixups].label_idx = label.label;
368 public virtual void Emit (OpCode opcode, Label[] labels) {
369 /* opcode needs to be switch. */
370 int count = labels.Length;
371 make_room (6 + count * 4);
374 if (num_fixups + count >= fixups.Length) {
375 LabelFixup[] newf = new LabelFixup [fixups.Length + count + 16];
376 System.Array.Copy (fixups, newf, fixups.Length);
379 for (int i = 0; i < count; ++i) {
380 fixups [num_fixups].size = 4;
381 fixups [num_fixups].pos = code_len;
382 fixups [num_fixups].label_idx = labels [i].label;
387 public virtual void Emit (OpCode opcode, LocalBuilder lbuilder) {
390 code [code_len++] = (byte) (lbuilder.position & 0xFF);
391 if (opcode.operandType == OperandType.InlineVar) {
392 code [code_len++] = (byte) ((lbuilder.position >> 8) & 0xFF);
395 public virtual void Emit (OpCode opcode, MethodInfo method) {
396 int token = abuilder.GetToken (method);
401 [CLSCompliant(false)]
402 public virtual void Emit (OpCode opcode, sbyte val) {
405 code [code_len++] = (byte)val;
409 public virtual void Emit (OpCode opcode, SignatureHelper shelper) {
410 int token = 0; // FIXME: request a token from the modulebuilder
415 public virtual void Emit (OpCode opcode, float val) {
416 byte[] s = System.BitConverter.GetBytes (val);
419 System.Array.Copy (s, 0, code, code_len, 4);
422 public virtual void Emit (OpCode opcode, string val) {
423 int token = abuilder.GetToken (val);
428 public virtual void Emit (OpCode opcode, Type type) {
431 emit_int (abuilder.GetToken (type));
434 public void EmitCall (OpCode opcode, MethodInfo methodinfo, Type[] optionalParamTypes) {
435 throw new NotImplementedException ();
437 public void EmitCalli (OpCode opcode, CallingConventions call_conv, Type returnType, Type[] paramTypes, Type[] optionalParamTypes) {
438 throw new NotImplementedException ();
441 public virtual void EmitWriteLine (FieldInfo field) {
442 throw new NotImplementedException ();
444 public virtual void EmitWriteLine (LocalBuilder lbuilder) {
445 throw new NotImplementedException ();
447 public virtual void EmitWriteLine (string val) {
448 throw new NotImplementedException ();
451 public virtual void EndExceptionBlock () {
452 if (open_blocks <= 0)
453 throw new NotSupportedException ("Not in an exception block");
454 InternalEndClause ();
455 MarkLabel (ex_handlers [cur_block].end);
456 ex_handlers [cur_block].End (code_len);
457 ex_handlers [cur_block].Debug ();
460 //System.Console.WriteLine ("End Block");
461 //throw new NotImplementedException ();
463 public virtual void EndScope () {
464 throw new NotImplementedException ();
466 public virtual void MarkLabel (Label loc) {
467 if (loc.label < 0 || loc.label >= num_labels)
468 throw new System.ArgumentException ("The label is not valid");
469 if (label_to_addr [loc.label] >= 0)
470 throw new System.ArgumentException ("The label was already defined");
471 label_to_addr [loc.label] = code_len;
473 public virtual void MarkSequencePoint (ISymbolDocumentWriter document, int startLine, int startColumn, int endLine, int EndColumn) {
474 throw new NotImplementedException ();
476 public virtual void ThrowException (Type exceptionType) {
477 throw new NotImplementedException ();
479 public virtual void UsingNamespace (String usingNamespace) {
480 throw new NotImplementedException ();
483 internal void label_fixup () {
485 for (i = 0; i < num_fixups; ++i) {
486 int diff = label_to_addr [fixups [i].label_idx] - fixups [i].pos;
487 if (fixups [i].size == 1) {
488 code [fixups [i].pos] = (byte)((sbyte) diff - 1);
490 int old_cl = code_len;
491 code_len = fixups [i].pos;