Add TimeZoneInfo Serialization
[mono.git] / mono / metadata / method-builder.c
1 /*
2  * method-builder.c: Functions for creating IL methods at runtime.
3  * 
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  */
10
11 #include "config.h"
12 #include "loader.h"
13 #include "mono/metadata/method-builder.h"
14 #include "mono/metadata/tabledefs.h"
15 #include "mono/metadata/exception.h"
16 #include "mono/metadata/appdomain.h"
17 #include "mono/metadata/debug-helpers.h"
18 #include "mono/metadata/metadata-internals.h"
19 #include "mono/metadata/domain-internals.h"
20 #include <string.h>
21 #include <errno.h>
22
23 /* #define DEBUG_RUNTIME_CODE */
24
25 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
26         a = i,
27
28 enum {
29 #include "mono/cil/opcode.def"
30         LAST = 0xff
31 };
32 #undef OPDEF
33
34 #ifdef DEBUG_RUNTIME_CODE
35 static char*
36 indenter (MonoDisHelper *dh, MonoMethod *method, guint32 ip_offset)
37 {
38         return g_strdup (" ");
39 }
40
41 static MonoDisHelper marshal_dh = {
42         "\n",
43         "IL_%04x: ",
44         "IL_%04x",
45         indenter, 
46         NULL,
47         NULL
48 };
49 #endif 
50
51 static MonoMethodBuilder *
52 mono_mb_new_base (MonoClass *klass, MonoWrapperType type)
53 {
54         MonoMethodBuilder *mb;
55         MonoMethod *m;
56
57         g_assert (klass != NULL);
58
59         mb = g_new0 (MonoMethodBuilder, 1);
60
61         mb->method = m = (MonoMethod *)g_new0 (MonoMethodWrapper, 1);
62
63         m->klass = klass;
64         m->inline_info = 1;
65         m->wrapper_type = type;
66
67 #ifndef DISABLE_JIT
68         mb->code_size = 40;
69         mb->code = g_malloc (mb->code_size);
70 #endif
71         /* placeholder for the wrapper always at index 1 */
72         mono_mb_add_data (mb, NULL);
73
74         return mb;
75 }
76
77 MonoMethodBuilder *
78 mono_mb_new_no_dup_name (MonoClass *klass, const char *name, MonoWrapperType type)
79 {
80         MonoMethodBuilder *mb = mono_mb_new_base (klass, type);
81         mb->name = (char*)name;
82         mb->no_dup_name = TRUE;
83         return mb;
84 }
85
86 MonoMethodBuilder *
87 mono_mb_new (MonoClass *klass, const char *name, MonoWrapperType type)
88 {
89         MonoMethodBuilder *mb = mono_mb_new_base (klass, type);
90         mb->name = g_strdup (name);
91         return mb;
92 }
93
94 void
95 mono_mb_free (MonoMethodBuilder *mb)
96 {
97 #ifndef DISABLE_JIT
98         g_list_free (mb->locals_list);
99         if (!mb->dynamic) {
100                 g_free (mb->method);
101                 if (!mb->no_dup_name)
102                         g_free (mb->name);
103                 g_free (mb->code);
104         }
105 #else
106         g_free (mb->method);
107         if (!mb->no_dup_name)
108                 g_free (mb->name);
109 #endif
110         g_free (mb);
111 }
112
113 /**
114  * mono_mb_create_method:
115  *
116  * Create a MonoMethod from this method builder.
117  * Returns: the newly created method.
118  *
119  * LOCKING: Takes the loader lock.
120  */
121 MonoMethod *
122 mono_mb_create_method (MonoMethodBuilder *mb, MonoMethodSignature *signature, int max_stack)
123 {
124 #ifndef DISABLE_JIT
125         MonoMethodHeader *header;
126 #endif
127         MonoMethodWrapper *mw;
128         MonoImage *image;
129         MonoMethod *method;
130         GList *l;
131         int i;
132
133         g_assert (mb != NULL);
134
135         image = mb->method->klass->image;
136
137         mono_loader_lock (); /*FIXME I think this lock can go.*/
138 #ifndef DISABLE_JIT
139         if (mb->dynamic) {
140                 method = mb->method;
141                 mw = (MonoMethodWrapper*)method;
142
143                 method->name = mb->name;
144                 method->dynamic = TRUE;
145
146                 mw->header = header = (MonoMethodHeader *) 
147                         g_malloc0 (MONO_SIZEOF_METHOD_HEADER + mb->locals * sizeof (MonoType *));
148
149                 header->code = mb->code;
150
151                 for (i = 0, l = mb->locals_list; l; l = l->next, i++) {
152                         header->locals [i] = mono_metadata_type_dup (NULL, (MonoType*)l->data);
153                 }
154         } else
155 #endif
156         {
157                 /* Realloc the method info into a mempool */
158
159                 method = mono_image_alloc0 (image, sizeof (MonoMethodWrapper));
160                 memcpy (method, mb->method, sizeof (MonoMethodWrapper));
161                 mw = (MonoMethodWrapper*) method;
162
163                 if (mb->no_dup_name)
164                         method->name = mb->name;
165                 else
166                         method->name = mono_image_strdup (image, mb->name);
167
168 #ifndef DISABLE_JIT
169                 mw->header = header = (MonoMethodHeader *) 
170                         mono_image_alloc0 (image, MONO_SIZEOF_METHOD_HEADER + mb->locals * sizeof (MonoType *));
171
172                 header->code = mono_image_alloc (image, mb->pos);
173                 memcpy ((char*)header->code, mb->code, mb->pos);
174
175                 for (i = 0, l = mb->locals_list; l; l = l->next, i++) {
176                         header->locals [i] = (MonoType *)l->data;
177                 }
178 #endif
179         }
180
181         method->signature = signature;
182
183 #ifndef DISABLE_JIT
184         if (max_stack < 8)
185                 max_stack = 8;
186
187         header->max_stack = max_stack;
188
189         header->code_size = mb->pos;
190         header->num_locals = mb->locals;
191         header->init_locals = TRUE;
192
193         header->num_clauses = mb->num_clauses;
194         header->clauses = mb->clauses;
195
196         method->skip_visibility = mb->skip_visibility;
197 #endif
198
199         i = g_list_length (mw->method_data);
200         if (i) {
201                 GList *tmp;
202                 void **data;
203                 l = g_list_reverse (mw->method_data);
204                 if (method->dynamic)
205                         data = g_malloc (sizeof (gpointer) * (i + 1));
206                 else
207                         data = mono_image_alloc (image, sizeof (gpointer) * (i + 1));
208                 /* store the size in the first element */
209                 data [0] = GUINT_TO_POINTER (i);
210                 i = 1;
211                 for (tmp = l; tmp; tmp = tmp->next) {
212                         data [i++] = tmp->data;
213                 }
214                 g_list_free (l);
215
216                 mw->method_data = data;
217         }
218
219 #ifndef DISABLE_JIT
220         /*{
221                 static int total_code = 0;
222                 static int total_alloc = 0;
223                 total_code += mb->pos;
224                 total_alloc += mb->code_size;
225                 g_print ("code size: %d of %d (allocated: %d)\n", mb->pos, total_code, total_alloc);
226         }*/
227
228 #ifdef DEBUG_RUNTIME_CODE
229         printf ("RUNTIME CODE FOR %s\n", mono_method_full_name (method, TRUE));
230         printf ("%s\n", mono_disasm_code (&marshal_dh, method, mb->code, mb->code + mb->pos));
231 #endif
232
233         if (mb->param_names) {
234                 char **param_names = mono_image_alloc0 (image, signature->param_count * sizeof (gpointer));
235                 for (i = 0; i < signature->param_count; ++i)
236                         param_names [i] = mono_image_strdup (image, mb->param_names [i]);
237
238                 mono_image_lock (image);
239                 if (!image->wrapper_param_names)
240                         image->wrapper_param_names = g_hash_table_new (NULL, NULL);
241                 g_hash_table_insert (image->wrapper_param_names, method, param_names);
242                 mono_image_unlock (image);
243         }
244 #endif
245
246         mono_loader_unlock ();
247         return method;
248 }
249
250 guint32
251 mono_mb_add_data (MonoMethodBuilder *mb, gpointer data)
252 {
253         MonoMethodWrapper *mw;
254
255         g_assert (mb != NULL);
256
257         mw = (MonoMethodWrapper *)mb->method;
258
259         /* one O(n) is enough */
260         mw->method_data = g_list_prepend (mw->method_data, data);
261
262         return g_list_length (mw->method_data);
263 }
264
265 #ifndef DISABLE_JIT
266
267 int
268 mono_mb_add_local (MonoMethodBuilder *mb, MonoType *type)
269 {
270         int res;
271
272         g_assert (mb != NULL);
273         g_assert (type != NULL);
274
275         res = mb->locals;
276         mb->locals_list = g_list_append (mb->locals_list, type);
277         mb->locals++;
278
279         return res;
280 }
281
282 void
283 mono_mb_patch_addr (MonoMethodBuilder *mb, int pos, int value)
284 {
285         mb->code [pos] = value & 0xff;
286         mb->code [pos + 1] = (value >> 8) & 0xff;
287         mb->code [pos + 2] = (value >> 16) & 0xff;
288         mb->code [pos + 3] = (value >> 24) & 0xff;
289 }
290
291 void
292 mono_mb_patch_addr_s (MonoMethodBuilder *mb, int pos, gint8 value)
293 {
294         *((gint8 *)(&mb->code [pos])) = value;
295 }
296
297 void
298 mono_mb_emit_byte (MonoMethodBuilder *mb, guint8 op)
299 {
300         if (mb->pos >= mb->code_size) {
301                 mb->code_size += mb->code_size >> 1;
302                 mb->code = g_realloc (mb->code, mb->code_size);
303         }
304
305         mb->code [mb->pos++] = op;
306 }
307
308 void
309 mono_mb_emit_ldflda (MonoMethodBuilder *mb, gint32 offset)
310 {
311         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
312         mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
313
314         if (offset) {
315                 mono_mb_emit_icon (mb, offset);
316                 mono_mb_emit_byte (mb, CEE_ADD);
317         }
318 }
319
320 void
321 mono_mb_emit_i4 (MonoMethodBuilder *mb, gint32 data)
322 {
323         if ((mb->pos + 4) >= mb->code_size) {
324                 mb->code_size += mb->code_size >> 1;
325                 mb->code = g_realloc (mb->code, mb->code_size);
326         }
327
328         mono_mb_patch_addr (mb, mb->pos, data);
329         mb->pos += 4;
330 }
331
332 void
333 mono_mb_emit_i2 (MonoMethodBuilder *mb, gint16 data)
334 {
335         if ((mb->pos + 2) >= mb->code_size) {
336                 mb->code_size += mb->code_size >> 1;
337                 mb->code = g_realloc (mb->code, mb->code_size);
338         }
339
340         mb->code [mb->pos] = data & 0xff;
341         mb->code [mb->pos + 1] = (data >> 8) & 0xff;
342         mb->pos += 2;
343 }
344
345 void
346 mono_mb_emit_op (MonoMethodBuilder *mb, guint8 op, gpointer data)
347 {
348         mono_mb_emit_byte (mb, op);
349         mono_mb_emit_i4 (mb, mono_mb_add_data (mb, data));
350 }
351
352 void
353 mono_mb_emit_ldstr (MonoMethodBuilder *mb, char *str)
354 {
355         mono_mb_emit_op (mb, CEE_LDSTR, str);
356 }
357
358 void
359 mono_mb_emit_ldarg (MonoMethodBuilder *mb, guint argnum)
360 {
361         if (argnum < 4) {
362                 mono_mb_emit_byte (mb, CEE_LDARG_0 + argnum);
363         } else if (argnum < 256) {
364                 mono_mb_emit_byte (mb, CEE_LDARG_S);
365                 mono_mb_emit_byte (mb, argnum);
366         } else {
367                 mono_mb_emit_byte (mb, CEE_PREFIX1);
368                 mono_mb_emit_byte (mb, CEE_LDARG);
369                 mono_mb_emit_i2 (mb, argnum);
370         }
371 }
372
373 void
374 mono_mb_emit_ldarg_addr (MonoMethodBuilder *mb, guint argnum)
375 {
376         if (argnum < 256) {
377                 mono_mb_emit_byte (mb, CEE_LDARGA_S);
378                 mono_mb_emit_byte (mb, argnum);
379         } else {
380                 mono_mb_emit_byte (mb, CEE_PREFIX1);
381                 mono_mb_emit_byte (mb, CEE_LDARGA);
382                 mono_mb_emit_i2 (mb, argnum);
383         }
384 }
385
386 void
387 mono_mb_emit_ldloc_addr (MonoMethodBuilder *mb, guint locnum)
388 {
389         if (locnum < 256) {
390                 mono_mb_emit_byte (mb, CEE_LDLOCA_S);
391                 mono_mb_emit_byte (mb, locnum);
392         } else {
393                 mono_mb_emit_byte (mb, CEE_PREFIX1);
394                 mono_mb_emit_byte (mb, CEE_LDLOCA);
395                 mono_mb_emit_i2 (mb, locnum);
396         }
397 }
398
399 void
400 mono_mb_emit_ldloc (MonoMethodBuilder *mb, guint num)
401 {
402         if (num < 4) {
403                 mono_mb_emit_byte (mb, CEE_LDLOC_0 + num);
404         } else if (num < 256) {
405                 mono_mb_emit_byte (mb, CEE_LDLOC_S);
406                 mono_mb_emit_byte (mb, num);
407         } else {
408                 mono_mb_emit_byte (mb, CEE_PREFIX1);
409                 mono_mb_emit_byte (mb, CEE_LDLOC);
410                 mono_mb_emit_i2 (mb, num);
411         }
412 }
413
414 void
415 mono_mb_emit_stloc (MonoMethodBuilder *mb, guint num)
416 {
417         if (num < 4) {
418                 mono_mb_emit_byte (mb, CEE_STLOC_0 + num);
419         } else if (num < 256) {
420                 mono_mb_emit_byte (mb, CEE_STLOC_S);
421                 mono_mb_emit_byte (mb, num);
422         } else {
423                 mono_mb_emit_byte (mb, CEE_PREFIX1);
424                 mono_mb_emit_byte (mb, CEE_STLOC);
425                 mono_mb_emit_i2 (mb, num);
426         }
427 }
428
429 void
430 mono_mb_emit_icon (MonoMethodBuilder *mb, gint32 value)
431 {
432         if (value >= -1 && value < 8) {
433                 mono_mb_emit_byte (mb, CEE_LDC_I4_0 + value);
434         } else if (value >= -128 && value <= 127) {
435                 mono_mb_emit_byte (mb, CEE_LDC_I4_S);
436                 mono_mb_emit_byte (mb, value);
437         } else {
438                 mono_mb_emit_byte (mb, CEE_LDC_I4);
439                 mono_mb_emit_i4 (mb, value);
440         }
441 }
442
443 int
444 mono_mb_get_label (MonoMethodBuilder *mb)
445 {
446         return mb->pos;
447 }
448
449 int
450 mono_mb_get_pos (MonoMethodBuilder *mb)
451 {
452         return mb->pos;
453 }
454
455 guint32
456 mono_mb_emit_branch (MonoMethodBuilder *mb, guint8 op)
457 {
458         guint32 res;
459         mono_mb_emit_byte (mb, op);
460         res = mb->pos;
461         mono_mb_emit_i4 (mb, 0);
462         return res;
463 }
464
465 guint32
466 mono_mb_emit_short_branch (MonoMethodBuilder *mb, guint8 op)
467 {
468         guint32 res;
469         mono_mb_emit_byte (mb, op);
470         res = mb->pos;
471         mono_mb_emit_byte (mb, 0);
472
473         return res;
474 }
475
476 void
477 mono_mb_emit_branch_label (MonoMethodBuilder *mb, guint8 op, guint32 label)
478 {
479         mono_mb_emit_byte (mb, op);
480         mono_mb_emit_i4 (mb, label - (mb->pos + 4));
481 }
482
483 void
484 mono_mb_patch_branch (MonoMethodBuilder *mb, guint32 pos)
485 {
486         mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
487 }
488
489 void
490 mono_mb_patch_short_branch (MonoMethodBuilder *mb, guint32 pos)
491 {
492         mono_mb_patch_addr_s (mb, pos, mb->pos - (pos + 1));
493 }
494
495 void
496 mono_mb_emit_ptr (MonoMethodBuilder *mb, gpointer ptr)
497 {
498         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
499         mono_mb_emit_op (mb, CEE_MONO_LDPTR, ptr);
500 }
501
502 void
503 mono_mb_emit_calli (MonoMethodBuilder *mb, MonoMethodSignature *sig)
504 {
505         mono_mb_emit_op (mb, CEE_CALLI, sig);
506 }
507
508 void
509 mono_mb_emit_managed_call (MonoMethodBuilder *mb, MonoMethod *method, MonoMethodSignature *opt_sig)
510 {
511         mono_mb_emit_op (mb, CEE_CALL, method);
512 }
513
514 void
515 mono_mb_emit_native_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, gpointer func)
516 {
517         mono_mb_emit_ptr (mb, func);
518         mono_mb_emit_calli (mb, sig);
519 }
520
521 void
522 mono_mb_emit_icall (MonoMethodBuilder *mb, gpointer func)
523 {
524         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
525         mono_mb_emit_op (mb, CEE_MONO_ICALL, func);
526 }
527
528 void
529 mono_mb_emit_exception_full (MonoMethodBuilder *mb, const char *exc_nspace, const char *exc_name, const char *msg)
530 {
531         MonoMethod *ctor = NULL;
532
533         MonoClass *mme = mono_class_from_name (mono_defaults.corlib, exc_nspace, exc_name);
534         mono_class_init (mme);
535         ctor = mono_class_get_method_from_name (mme, ".ctor", 0);
536         g_assert (ctor);
537         mono_mb_emit_op (mb, CEE_NEWOBJ, ctor);
538         if (msg != NULL) {
539                 mono_mb_emit_byte (mb, CEE_DUP);
540                 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoException, message));
541                 mono_mb_emit_ldstr (mb, (char*)msg);
542                 mono_mb_emit_byte (mb, CEE_STIND_REF);
543         }
544         mono_mb_emit_byte (mb, CEE_THROW);
545 }
546
547 void
548 mono_mb_emit_exception (MonoMethodBuilder *mb, const char *exc_name, const char *msg)
549 {
550         mono_mb_emit_exception_full (mb, "System", exc_name, msg);
551 }
552
553 void
554 mono_mb_emit_add_to_local (MonoMethodBuilder *mb, guint16 local, gint32 incr)
555 {
556         mono_mb_emit_ldloc (mb, local); 
557         mono_mb_emit_icon (mb, incr);
558         mono_mb_emit_byte (mb, CEE_ADD);
559         mono_mb_emit_stloc (mb, local); 
560 }
561
562 void
563 mono_mb_set_clauses (MonoMethodBuilder *mb, int num_clauses, MonoExceptionClause *clauses)
564 {
565         mb->num_clauses = num_clauses;
566         mb->clauses = clauses;
567 }
568
569 /*
570  * mono_mb_set_param_names:
571  *
572  *   PARAM_NAMES should have length equal to the sig->param_count, the caller retains
573  * ownership of the array, and its entries.
574  */
575 void
576 mono_mb_set_param_names (MonoMethodBuilder *mb, const char **param_names)
577 {
578         mb->param_names = param_names;
579 }
580
581 #endif /* DISABLE_JIT */