2003-06-17 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / icall.c
1 /*
2  * icall.c:
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *       Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 #if defined (PLATFORM_WIN32)
19 #include <stdlib.h>
20 #endif
21
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/monitor.h>
25 #include <mono/metadata/reflection.h>
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/file-io.h>
30 #include <mono/metadata/socket-io.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/unicode.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/marshal.h>
36 #include <mono/metadata/gc-internal.h>
37 #include <mono/metadata/rand.h>
38 #include <mono/metadata/sysmath.h>
39 #include <mono/metadata/string-icalls.h>
40 #include <mono/metadata/mono-debug-debugger.h>
41 #include <mono/metadata/process.h>
42 #include <mono/metadata/environment.h>
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/io-layer/io-layer.h>
45 #include <mono/utils/strtod.h>
46
47 #if defined (PLATFORM_WIN32)
48 #include <windows.h>
49 #endif
50 #include "decimal.h"
51
52 static MonoReflectionAssembly* ves_icall_System_Reflection_Assembly_GetCallingAssembly (void);
53
54
55 /*
56  * We expect a pointer to a char, not a string
57  */
58 static double
59 mono_double_ParseImpl (char *ptr)
60 {
61         gchar *endptr = NULL;
62         gdouble result;
63
64         MONO_ARCH_SAVE_REGS;
65
66         if (*ptr)
67                 result = bsd_strtod (ptr, &endptr);
68
69         if (!*ptr || (endptr && *endptr))
70                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib,
71                                                                 "System",
72                                                                 "FormatException"));
73         
74         return result;
75 }
76
77 static MonoObject *
78 ves_icall_System_Array_GetValueImpl (MonoObject *this, guint32 pos)
79 {
80         MonoClass *ac;
81         MonoArray *ao;
82         gint32 esize;
83         gpointer *ea;
84
85         MONO_ARCH_SAVE_REGS;
86
87         ao = (MonoArray *)this;
88         ac = (MonoClass *)ao->obj.vtable->klass;
89
90         esize = mono_array_element_size (ac);
91         ea = (gpointer*)((char*)ao->vector + (pos * esize));
92
93         if (ac->element_class->valuetype)
94                 return mono_value_box (this->vtable->domain, ac->element_class, ea);
95         else
96                 return *ea;
97 }
98
99 static MonoObject *
100 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
101 {
102         MonoClass *ac, *ic;
103         MonoArray *ao, *io;
104         gint32 i, pos, *ind;
105
106         MONO_ARCH_SAVE_REGS;
107
108         MONO_CHECK_ARG_NULL (idxs);
109
110         io = (MonoArray *)idxs;
111         ic = (MonoClass *)io->obj.vtable->klass;
112         
113         ao = (MonoArray *)this;
114         ac = (MonoClass *)ao->obj.vtable->klass;
115
116         g_assert (ic->rank == 1);
117         if (io->bounds != NULL || io->max_length !=  ac->rank)
118                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
119
120         ind = (guint32 *)io->vector;
121
122         if (ao->bounds == NULL) {
123                 if (*ind < 0 || *ind >= ao->max_length)
124                         mono_raise_exception (mono_get_exception_index_out_of_range ());
125
126                 return ves_icall_System_Array_GetValueImpl (this, *ind);
127         }
128         
129         for (i = 0; i < ac->rank; i++)
130                 if ((ind [i] < ao->bounds [i].lower_bound) ||
131                     (ind [i] >= ao->bounds [i].length + ao->bounds [i].lower_bound))
132                         mono_raise_exception (mono_get_exception_index_out_of_range ());
133
134         pos = ind [0] - ao->bounds [0].lower_bound;
135         for (i = 1; i < ac->rank; i++)
136                 pos = pos*ao->bounds [i].length + ind [i] - 
137                         ao->bounds [i].lower_bound;
138
139         return ves_icall_System_Array_GetValueImpl (this, pos);
140 }
141
142 static void
143 ves_icall_System_Array_SetValueImpl (MonoArray *this, MonoObject *value, guint32 pos)
144 {
145         MonoClass *ac, *vc, *ec;
146         gint32 esize, vsize;
147         gpointer *ea, *va;
148
149         guint64 u64;
150         gint64 i64;
151         gdouble r64;
152
153         MONO_ARCH_SAVE_REGS;
154
155         if (value)
156                 vc = value->vtable->klass;
157         else
158                 vc = NULL;
159
160         ac = this->obj.vtable->klass;
161         ec = ac->element_class;
162
163         esize = mono_array_element_size (ac);
164         ea = (gpointer*)((char*)this->vector + (pos * esize));
165         va = (gpointer*)((char*)value + sizeof (MonoObject));
166
167         if (!value) {
168                 memset (ea, 0,  esize);
169                 return;
170         }
171
172 #define NO_WIDENING_CONVERSION G_STMT_START{\
173         mono_raise_exception (mono_get_exception_argument ( \
174                 "value", "not a widening conversion")); \
175 }G_STMT_END
176
177 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{\
178         if (esize < vsize + (extra)) \
179                 mono_raise_exception (mono_get_exception_argument ( \
180                         "value", "not a widening conversion")); \
181 }G_STMT_END
182
183 #define INVALID_CAST G_STMT_START{\
184         mono_raise_exception (mono_get_exception_invalid_cast ()); \
185 }G_STMT_END
186
187         /* Check element (destination) type. */
188         switch (ec->byval_arg.type) {
189         case MONO_TYPE_STRING:
190                 switch (vc->byval_arg.type) {
191                 case MONO_TYPE_STRING:
192                         break;
193                 default:
194                         INVALID_CAST;
195                 }
196                 break;
197         case MONO_TYPE_BOOLEAN:
198                 switch (vc->byval_arg.type) {
199                 case MONO_TYPE_BOOLEAN:
200                         break;
201                 case MONO_TYPE_CHAR:
202                 case MONO_TYPE_U1:
203                 case MONO_TYPE_U2:
204                 case MONO_TYPE_U4:
205                 case MONO_TYPE_U8:
206                 case MONO_TYPE_I1:
207                 case MONO_TYPE_I2:
208                 case MONO_TYPE_I4:
209                 case MONO_TYPE_I8:
210                 case MONO_TYPE_R4:
211                 case MONO_TYPE_R8:
212                         NO_WIDENING_CONVERSION;
213                 default:
214                         INVALID_CAST;
215                 }
216                 break;
217         }
218
219         if (!ec->valuetype) {
220                 *ea = (gpointer)value;
221                 return;
222         }
223
224         if (mono_object_isinst (value, ec)) {
225                 memcpy (ea, (char *)value + sizeof (MonoObject), esize);
226                 return;
227         }
228
229         if (!vc->valuetype)
230                 INVALID_CAST;
231
232         vsize = mono_class_instance_size (vc) - sizeof (MonoObject);
233
234 #if 0
235         g_message (G_STRLOC ": %d (%d) <= %d (%d)",
236                    ec->byval_arg.type, esize,
237                    vc->byval_arg.type, vsize);
238 #endif
239
240 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
241         switch (vc->byval_arg.type) { \
242         case MONO_TYPE_U1: \
243         case MONO_TYPE_U2: \
244         case MONO_TYPE_U4: \
245         case MONO_TYPE_U8: \
246         case MONO_TYPE_CHAR: \
247                 CHECK_WIDENING_CONVERSION(0); \
248                 *(etype *) ea = (etype) u64; \
249                 return; \
250         /* You can't assign a signed value to an unsigned array. */ \
251         case MONO_TYPE_I1: \
252         case MONO_TYPE_I2: \
253         case MONO_TYPE_I4: \
254         case MONO_TYPE_I8: \
255         /* You can't assign a floating point number to an integer array. */ \
256         case MONO_TYPE_R4: \
257         case MONO_TYPE_R8: \
258                 NO_WIDENING_CONVERSION; \
259         } \
260 }G_STMT_END
261
262 #define ASSIGN_SIGNED(etype) G_STMT_START{\
263         switch (vc->byval_arg.type) { \
264         case MONO_TYPE_I1: \
265         case MONO_TYPE_I2: \
266         case MONO_TYPE_I4: \
267         case MONO_TYPE_I8: \
268                 CHECK_WIDENING_CONVERSION(0); \
269                 *(etype *) ea = (etype) i64; \
270                 return; \
271         /* You can assign an unsigned value to a signed array if the array's */ \
272         /* element size is larger than the value size. */ \
273         case MONO_TYPE_U1: \
274         case MONO_TYPE_U2: \
275         case MONO_TYPE_U4: \
276         case MONO_TYPE_U8: \
277         case MONO_TYPE_CHAR: \
278                 CHECK_WIDENING_CONVERSION(1); \
279                 *(etype *) ea = (etype) u64; \
280                 return; \
281         /* You can't assign a floating point number to an integer array. */ \
282         case MONO_TYPE_R4: \
283         case MONO_TYPE_R8: \
284                 NO_WIDENING_CONVERSION; \
285         } \
286 }G_STMT_END
287
288 #define ASSIGN_REAL(etype) G_STMT_START{\
289         switch (vc->byval_arg.type) { \
290         case MONO_TYPE_R4: \
291         case MONO_TYPE_R8: \
292                 CHECK_WIDENING_CONVERSION(0); \
293                 *(etype *) ea = (etype) r64; \
294                 return; \
295         /* All integer values fit into a floating point array, so we don't */ \
296         /* need to CHECK_WIDENING_CONVERSION here. */ \
297         case MONO_TYPE_I1: \
298         case MONO_TYPE_I2: \
299         case MONO_TYPE_I4: \
300         case MONO_TYPE_I8: \
301                 *(etype *) ea = (etype) i64; \
302                 return; \
303         case MONO_TYPE_U1: \
304         case MONO_TYPE_U2: \
305         case MONO_TYPE_U4: \
306         case MONO_TYPE_U8: \
307         case MONO_TYPE_CHAR: \
308                 *(etype *) ea = (etype) u64; \
309                 return; \
310         } \
311 }G_STMT_END
312
313         switch (vc->byval_arg.type) {
314         case MONO_TYPE_U1:
315                 u64 = *(guint8 *) va;
316                 break;
317         case MONO_TYPE_U2:
318                 u64 = *(guint16 *) va;
319                 break;
320         case MONO_TYPE_U4:
321                 u64 = *(guint32 *) va;
322                 break;
323         case MONO_TYPE_U8:
324                 u64 = *(guint64 *) va;
325                 break;
326         case MONO_TYPE_I1:
327                 i64 = *(gint8 *) va;
328                 break;
329         case MONO_TYPE_I2:
330                 i64 = *(gint16 *) va;
331                 break;
332         case MONO_TYPE_I4:
333                 i64 = *(gint32 *) va;
334                 break;
335         case MONO_TYPE_I8:
336                 i64 = *(gint64 *) va;
337                 break;
338         case MONO_TYPE_R4:
339                 r64 = *(gfloat *) va;
340                 break;
341         case MONO_TYPE_R8:
342                 r64 = *(gdouble *) va;
343                 break;
344         case MONO_TYPE_CHAR:
345                 u64 = *(guint16 *) va;
346                 break;
347         case MONO_TYPE_BOOLEAN:
348                 /* Boolean is only compatible with itself. */
349                 switch (ec->byval_arg.type) {
350                 case MONO_TYPE_CHAR:
351                 case MONO_TYPE_U1:
352                 case MONO_TYPE_U2:
353                 case MONO_TYPE_U4:
354                 case MONO_TYPE_U8:
355                 case MONO_TYPE_I1:
356                 case MONO_TYPE_I2:
357                 case MONO_TYPE_I4:
358                 case MONO_TYPE_I8:
359                 case MONO_TYPE_R4:
360                 case MONO_TYPE_R8:
361                         NO_WIDENING_CONVERSION;
362                 default:
363                         INVALID_CAST;
364                 }
365                 break;
366         }
367
368         /* If we can't do a direct copy, let's try a widening conversion. */
369         switch (ec->byval_arg.type) {
370         case MONO_TYPE_CHAR:
371                 ASSIGN_UNSIGNED (guint16);
372         case MONO_TYPE_U1:
373                 ASSIGN_UNSIGNED (guint8);
374         case MONO_TYPE_U2:
375                 ASSIGN_UNSIGNED (guint16);
376         case MONO_TYPE_U4:
377                 ASSIGN_UNSIGNED (guint32);
378         case MONO_TYPE_U8:
379                 ASSIGN_UNSIGNED (guint64);
380         case MONO_TYPE_I1:
381                 ASSIGN_SIGNED (gint8);
382         case MONO_TYPE_I2:
383                 ASSIGN_SIGNED (gint16);
384         case MONO_TYPE_I4:
385                 ASSIGN_SIGNED (gint32);
386         case MONO_TYPE_I8:
387                 ASSIGN_SIGNED (gint64);
388         case MONO_TYPE_R4:
389                 ASSIGN_REAL (gfloat);
390         case MONO_TYPE_R8:
391                 ASSIGN_REAL (gdouble);
392         }
393
394         INVALID_CAST;
395         /* Not reached, INVALID_CAST does not return. Just to avoid a compiler warning ... */
396         return;
397
398 #undef INVALID_CAST
399 #undef NO_WIDENING_CONVERSION
400 #undef CHECK_WIDENING_CONVERSION
401 #undef ASSIGN_UNSIGNED
402 #undef ASSIGN_SIGNED
403 #undef ASSIGN_REAL
404 }
405
406 static void 
407 ves_icall_System_Array_SetValue (MonoArray *this, MonoObject *value,
408                                  MonoArray *idxs)
409 {
410         MonoClass *ac, *ic;
411         gint32 i, pos, *ind;
412
413         MONO_ARCH_SAVE_REGS;
414
415         MONO_CHECK_ARG_NULL (idxs);
416
417         ic = idxs->obj.vtable->klass;
418         ac = this->obj.vtable->klass;
419
420         g_assert (ic->rank == 1);
421         if (idxs->bounds != NULL || idxs->max_length != ac->rank)
422                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
423
424         ind = (guint32 *)idxs->vector;
425
426         if (this->bounds == NULL) {
427                 if (*ind < 0 || *ind >= this->max_length)
428                         mono_raise_exception (mono_get_exception_index_out_of_range ());
429
430                 ves_icall_System_Array_SetValueImpl (this, value, *ind);
431                 return;
432         }
433         
434         for (i = 0; i < ac->rank; i++)
435                 if ((ind [i] < this->bounds [i].lower_bound) ||
436                     (ind [i] >= this->bounds [i].length + this->bounds [i].lower_bound))
437                         mono_raise_exception (mono_get_exception_index_out_of_range ());
438
439         pos = ind [0] - this->bounds [0].lower_bound;
440         for (i = 1; i < ac->rank; i++)
441                 pos = pos * this->bounds [i].length + ind [i] - 
442                         this->bounds [i].lower_bound;
443
444         ves_icall_System_Array_SetValueImpl (this, value, pos);
445 }
446
447 static MonoArray *
448 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *lengths, MonoArray *bounds)
449 {
450         MonoClass *aklass;
451         MonoArray *array;
452         gint32 *sizes, i;
453
454         MONO_ARCH_SAVE_REGS;
455
456         MONO_CHECK_ARG_NULL (type);
457         MONO_CHECK_ARG_NULL (lengths);
458
459         MONO_CHECK_ARG (lengths, mono_array_length (lengths) > 0);
460         if (bounds)
461                 MONO_CHECK_ARG (bounds, mono_array_length (lengths) == mono_array_length (bounds));
462
463         for (i = 0; i < mono_array_length (lengths); i++)
464                 if (mono_array_get (lengths, gint32, i) < 0)
465                         mono_raise_exception (mono_get_exception_argument_out_of_range (NULL));
466
467         aklass = mono_array_class_get (type->type, mono_array_length (lengths));
468
469         sizes = alloca (aklass->rank * sizeof(guint32) * 2);
470         for (i = 0; i < aklass->rank; ++i) {
471                 sizes [i] = mono_array_get (lengths, gint32, i);
472                 if (bounds)
473                         sizes [i + aklass->rank] = mono_array_get (bounds, gint32, i);
474                 else
475                         sizes [i + aklass->rank] = 0;
476         }
477
478         array = mono_array_new_full (mono_object_domain (type), aklass, sizes, sizes + aklass->rank);
479
480         return array;
481 }
482
483 static gint32 
484 ves_icall_System_Array_GetRank (MonoObject *this)
485 {
486         MONO_ARCH_SAVE_REGS;
487
488         return this->vtable->klass->rank;
489 }
490
491 static gint32
492 ves_icall_System_Array_GetLength (MonoArray *this, gint32 dimension)
493 {
494         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
495
496         MONO_ARCH_SAVE_REGS;
497
498         if ((dimension < 0) || (dimension >= rank))
499                 mono_raise_exception (mono_get_exception_index_out_of_range ());
500         
501         if (this->bounds == NULL)
502                 return this->max_length;
503         
504         return this->bounds [dimension].length;
505 }
506
507 static gint32
508 ves_icall_System_Array_GetLowerBound (MonoArray *this, gint32 dimension)
509 {
510         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
511
512         MONO_ARCH_SAVE_REGS;
513
514         if ((dimension < 0) || (dimension >= rank))
515                 mono_raise_exception (mono_get_exception_index_out_of_range ());
516         
517         if (this->bounds == NULL)
518                 return 0;
519         
520         return this->bounds [dimension].lower_bound;
521 }
522
523 static gboolean
524 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
525 {
526         int element_size;
527         void * dest_addr;
528         void * source_addr;
529         MonoClass *src_class;
530         MonoClass *dest_class;
531         int i;
532
533         MONO_ARCH_SAVE_REGS;
534
535         if (source->obj.vtable->klass->rank != dest->obj.vtable->klass->rank)
536                 return FALSE;
537
538         if (source->bounds || dest->bounds)
539                 return FALSE;
540
541         if ((dest_idx + length > mono_array_length (dest)) ||
542                 (source_idx + length > mono_array_length (source)))
543                 return FALSE;
544
545         element_size = mono_array_element_size (source->obj.vtable->klass);
546         dest_addr = mono_array_addr_with_size (dest, element_size, dest_idx);
547         source_addr = mono_array_addr_with_size (source, element_size, source_idx);
548
549         src_class = source->obj.vtable->klass->element_class;
550         dest_class = dest->obj.vtable->klass->element_class;
551
552         /*
553          * Handle common cases.
554          */
555
556         /* Case1: object[] -> valuetype[] (ArrayList::ToArray) */
557         if (src_class == mono_defaults.object_class && dest_class->valuetype) {
558                 for (i = source_idx; i < source_idx + length; ++i) {
559                         MonoObject *elem = mono_array_get (source, MonoObject*, i);
560                         if (elem && !mono_object_isinst (elem, dest_class))
561                                 return FALSE;
562                 }
563
564                 element_size = mono_array_element_size (dest->obj.vtable->klass);
565                 for (i = 0; i < length; ++i) {
566                         MonoObject *elem = mono_array_get (source, MonoObject*, source_idx + i);
567                         void *addr = mono_array_addr_with_size (dest, element_size, dest_idx + i);
568                         if (!elem)
569                                 memset (addr, 0, element_size);
570                         else
571                                 memcpy (addr, (char *)elem + sizeof (MonoObject), element_size);
572                 }
573                 return TRUE;
574         }
575
576         if (src_class != dest_class) {
577                 if (dest_class->valuetype || dest_class->enumtype || src_class->valuetype || src_class->enumtype)
578                         return FALSE;
579
580                 if (mono_class_is_subclass_of (src_class, dest_class, FALSE))
581                         ;
582                 /* Case2: object[] -> reftype[] (ArrayList::ToArray) */
583                 else if (mono_class_is_subclass_of (dest_class, src_class, FALSE))
584                         for (i = source_idx; i < source_idx + length; ++i) {
585                                 MonoObject *elem = mono_array_get (source, MonoObject*, i);
586                                 if (elem && !mono_object_isinst (elem, dest_class))
587                                         return FALSE;
588                         }
589                 else
590                         return FALSE;
591         }
592
593         memmove (dest_addr, source_addr, element_size * length);
594
595         return TRUE;
596 }
597
598 static void
599 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArray *array, MonoClassField *field_handle)
600 {
601         MonoClass *klass = array->obj.vtable->klass;
602         guint32 size = mono_array_element_size (klass);
603         int i;
604
605         MONO_ARCH_SAVE_REGS;
606
607         if (array->bounds == NULL)
608                 size *= array->max_length;
609         else
610                 for (i = 0; i < klass->rank; ++i) 
611                         size *= array->bounds [i].length;
612
613         memcpy (mono_array_addr (array, char, 0), field_handle->data, size);
614
615 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
616 #define SWAP(n) {\
617         gint i; \
618         guint ## n tmp; \
619         guint ## n *data = (guint ## n *) mono_array_addr (array, char, 0); \
620 \
621         for (i = 0; i < size; i += n/8, data++) { \
622                 tmp = read ## n (data); \
623                 *data = tmp; \
624         } \
625 }
626
627         /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
628
629         switch (klass->element_class->byval_arg.type) {
630         case MONO_TYPE_CHAR:
631         case MONO_TYPE_I2:
632         case MONO_TYPE_U2:
633                 SWAP (16);
634                 break;
635         case MONO_TYPE_I4:
636         case MONO_TYPE_U4:
637                 SWAP (32);
638                 break;
639         case MONO_TYPE_I8:
640         case MONO_TYPE_U8:
641                 SWAP (64);
642                 break;
643         }
644                  
645 #endif
646 }
647
648 static gint
649 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
650 {
651         MONO_ARCH_SAVE_REGS;
652
653         return offsetof (MonoString, chars);
654 }
655
656 static MonoObject *
657 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObject *obj)
658 {
659         MONO_ARCH_SAVE_REGS;
660
661         if ((obj == NULL) || (! (obj->vtable->klass->valuetype)))
662                 return obj;
663         else
664                 return mono_object_clone (obj);
665 }
666
667 static void
668 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle)
669 {
670         MonoClass *klass;
671
672         MONO_ARCH_SAVE_REGS;
673
674         MONO_CHECK_ARG_NULL (handle);
675
676         klass = mono_class_from_mono_type (handle);
677         MONO_CHECK_ARG (handle, klass);
678
679         /* This will call the type constructor */
680         if (! (klass->flags & TYPE_ATTRIBUTE_INTERFACE))
681                 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), klass));
682 }
683
684 static MonoObject *
685 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
686 {
687         MONO_ARCH_SAVE_REGS;
688
689         return mono_object_clone (this);
690 }
691
692 #if HAVE_BOEHM_GC
693 #define MONO_OBJECT_ALIGNMENT_SHIFT     3
694 #else
695 #define MONO_OBJECT_ALIGNMENT_SHIFT     2
696 #endif
697
698 /*
699  * Return hashcode based on object address. This function will need to be
700  * smarter in the presence of a moving garbage collector, which will cache
701  * the address hash before relocating the object.
702  *
703  * Wang's address-based hash function:
704  *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
705  */
706 static gint32
707 ves_icall_System_Object_GetHashCode (MonoObject *this)
708 {
709         register guint32 key;
710
711         MONO_ARCH_SAVE_REGS;
712
713         key = (GPOINTER_TO_UINT (this) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
714
715         return key & 0x7fffffff;
716 }
717
718 /*
719  * A hash function for value types. I have no idea if this is a good hash 
720  * function (its similar to g_str_hash).
721  */
722 static gint32
723 ves_icall_System_ValueType_GetHashCode (MonoObject *this)
724 {
725         gint32 i, size;
726         const char *p;
727         guint h = 0;
728
729         MONO_ARCH_SAVE_REGS;
730
731         MONO_CHECK_ARG_NULL (this);
732
733         size = this->vtable->klass->instance_size - sizeof (MonoObject);
734
735         p = (const char *)this + sizeof (MonoObject);
736
737         for (i = 0; i < size; i++) {
738                 h = (h << 5) - h + *p;
739                 p++;
740         }
741
742         return h;
743 }
744
745 static MonoBoolean
746 ves_icall_System_ValueType_Equals (MonoObject *this, MonoObject *that)
747 {
748         gint32 size;
749         const char *p, *s;
750
751         MONO_ARCH_SAVE_REGS;
752
753         MONO_CHECK_ARG_NULL (that);
754
755         if (this->vtable != that->vtable)
756                 return FALSE;
757
758         size = this->vtable->klass->instance_size - sizeof (MonoObject);
759
760         p = (const char *)this + sizeof (MonoObject);
761         s = (const char *)that + sizeof (MonoObject);
762
763         return memcmp (p, s, size)? FALSE: TRUE;
764 }
765
766 static MonoReflectionType *
767 ves_icall_System_Object_GetType (MonoObject *obj)
768 {
769         MONO_ARCH_SAVE_REGS;
770
771         return mono_type_get_object (mono_object_domain (obj), &obj->vtable->klass->byval_arg);
772 }
773
774 static void
775 mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
776 {
777         MONO_ARCH_SAVE_REGS;
778
779         mtype->type = &obj->vtable->klass->byval_arg;
780         g_assert (mtype->type->type);
781 }
782
783 static gint32
784 ves_icall_AssemblyBuilder_getToken (MonoReflectionAssemblyBuilder *assb, MonoObject *obj)
785 {
786         MONO_ARCH_SAVE_REGS;
787
788         return mono_image_create_token (assb->dynamic_assembly, obj);
789 }
790
791 static gint32
792 ves_icall_AssemblyBuilder_getDataChunk (MonoReflectionAssemblyBuilder *assb, MonoArray *buf, gint32 offset)
793 {
794         int count;
795         MonoDynamicAssembly *ass = assb->dynamic_assembly;
796         char *p = mono_array_addr (buf, char, 0);
797
798         MONO_ARCH_SAVE_REGS;
799
800         mono_image_create_pefile (assb);
801
802         if (offset >= ass->pefile.index)
803                 return 0;
804         count = mono_array_length (buf);
805         count = MIN (count, ass->pefile.index - offset);
806         
807         memcpy (p, ass->pefile.data + offset, count);
808
809         return count;
810 }
811
812 static void
813 ves_icall_AssemblyBuilder_build_metadata (MonoReflectionAssemblyBuilder *assb)
814 {
815         MONO_ARCH_SAVE_REGS;
816
817         mono_image_build_metadata (assb);
818 }
819
820 static MonoReflectionType*
821 ves_icall_type_from_name (MonoString *name,
822                           MonoBoolean throwOnError,
823                           MonoBoolean ignoreCase)
824 {
825         gchar *str;
826         MonoType *type = NULL;
827         MonoAssembly *assembly;
828         MonoTypeNameParse info;
829
830         MONO_ARCH_SAVE_REGS;
831
832         str = mono_string_to_utf8 (name);
833         if (!mono_reflection_parse_type (str, &info)) {
834                 g_free (str);
835                 g_list_free (info.modifiers);
836                 g_list_free (info.nested);
837                 if (throwOnError) /* uhm: this is a parse error, though... */
838                         mono_raise_exception (mono_get_exception_type_load ());
839
840                 return NULL;
841         }
842
843         if (info.assembly.name) {
844                 assembly = mono_assembly_load (&info.assembly, NULL, NULL);
845         } else {
846                 MonoReflectionAssembly *refass;
847
848                 refass = ves_icall_System_Reflection_Assembly_GetCallingAssembly  ();
849                 assembly = refass->assembly;
850         }
851
852         if (assembly)
853                 type = mono_reflection_get_type (assembly->image, &info, ignoreCase);
854         
855         if (!info.assembly.name && !type) /* try mscorlib */
856                 type = mono_reflection_get_type (NULL, &info, ignoreCase);
857
858         g_free (str);
859         g_list_free (info.modifiers);
860         g_list_free (info.nested);
861         if (!type) {
862                 if (throwOnError)
863                         mono_raise_exception (mono_get_exception_type_load ());
864
865                 return NULL;
866         }
867
868         return mono_type_get_object (mono_domain_get (), type);
869 }
870
871 static MonoReflectionType*
872 ves_icall_type_from_handle (MonoType *handle)
873 {
874         MonoDomain *domain = mono_domain_get (); 
875         MonoClass *klass = mono_class_from_mono_type (handle);
876
877         MONO_ARCH_SAVE_REGS;
878
879         mono_class_init (klass);
880         return mono_type_get_object (domain, handle);
881 }
882
883 static guint32
884 ves_icall_type_Equals (MonoReflectionType *type, MonoReflectionType *c)
885 {
886         MONO_ARCH_SAVE_REGS;
887
888         if (type->type && c->type)
889                 return mono_metadata_type_equal (type->type, c->type);
890         g_print ("type equals\n");
891         return 0;
892 }
893
894 /* System.TypeCode */
895 typedef enum {
896         TYPECODE_EMPTY,
897         TYPECODE_OBJECT,
898         TYPECODE_DBNULL,
899         TYPECODE_BOOLEAN,
900         TYPECODE_CHAR,
901         TYPECODE_SBYTE,
902         TYPECODE_BYTE,
903         TYPECODE_INT16,
904         TYPECODE_UINT16,
905         TYPECODE_INT32,
906         TYPECODE_UINT32,
907         TYPECODE_INT64,
908         TYPECODE_UINT64,
909         TYPECODE_SINGLE,
910         TYPECODE_DOUBLE,
911         TYPECODE_DECIMAL,
912         TYPECODE_DATETIME,
913         TYPECODE_STRING = 18
914 } TypeCode;
915
916 static guint32
917 ves_icall_type_GetTypeCode (MonoReflectionType *type)
918 {
919         int t = type->type->type;
920
921         MONO_ARCH_SAVE_REGS;
922
923 handle_enum:
924         switch (t) {
925         case MONO_TYPE_VOID:
926                 return TYPECODE_OBJECT;
927         case MONO_TYPE_BOOLEAN:
928                 return TYPECODE_BOOLEAN;
929         case MONO_TYPE_U1:
930                 return TYPECODE_BYTE;
931         case MONO_TYPE_I1:
932                 return TYPECODE_SBYTE;
933         case MONO_TYPE_U2:
934                 return TYPECODE_UINT16;
935         case MONO_TYPE_I2:
936                 return TYPECODE_INT16;
937         case MONO_TYPE_CHAR:
938                 return TYPECODE_CHAR;
939         case MONO_TYPE_PTR:
940         case MONO_TYPE_U:
941         case MONO_TYPE_I:
942                 return TYPECODE_OBJECT;
943         case MONO_TYPE_U4:
944                 return TYPECODE_UINT32;
945         case MONO_TYPE_I4:
946                 return TYPECODE_INT32;
947         case MONO_TYPE_U8:
948                 return TYPECODE_UINT64;
949         case MONO_TYPE_I8:
950                 return TYPECODE_INT64;
951         case MONO_TYPE_R4:
952                 return TYPECODE_SINGLE;
953         case MONO_TYPE_R8:
954                 return TYPECODE_DOUBLE;
955         case MONO_TYPE_VALUETYPE:
956                 if (type->type->data.klass->enumtype) {
957                         t = type->type->data.klass->enum_basetype->type;
958                         goto handle_enum;
959                 } else {
960                         MonoClass *k =  type->type->data.klass;
961                         if (strcmp (k->name_space, "System") == 0) {
962                                 if (strcmp (k->name, "Decimal") == 0)
963                                         return TYPECODE_DECIMAL;
964                                 else if (strcmp (k->name, "DateTime") == 0)
965                                         return TYPECODE_DATETIME;
966                         }
967                 }
968                 return TYPECODE_OBJECT;
969         case MONO_TYPE_STRING:
970                 return TYPECODE_STRING;
971         case MONO_TYPE_SZARRAY:
972         case MONO_TYPE_ARRAY:
973         case MONO_TYPE_OBJECT:
974                 return TYPECODE_OBJECT;
975         case MONO_TYPE_CLASS:
976                 {
977                         MonoClass *k =  type->type->data.klass;
978                         if (strcmp (k->name_space, "System") == 0) {
979                                 if (strcmp (k->name, "DBNull") == 0)
980                                         return TYPECODE_DBNULL;
981                         }
982                 }
983                 return TYPECODE_OBJECT;
984         default:
985                 g_error ("type 0x%02x not handled in GetTypeCode()", t);
986         }
987         return 0;
988 }
989
990 static guint32
991 ves_icall_type_is_subtype_of (MonoReflectionType *type, MonoReflectionType *c, MonoBoolean check_interfaces)
992 {
993         MonoDomain *domain; 
994         MonoClass *klass;
995         MonoClass *klassc;
996
997         MONO_ARCH_SAVE_REGS;
998
999         g_assert (type != NULL);
1000         
1001         domain = ((MonoObject *)type)->vtable->domain;
1002
1003         if (!c) /* FIXME: dont know what do do here */
1004                 return 0;
1005
1006         klass = mono_class_from_mono_type (type->type);
1007         klassc = mono_class_from_mono_type (c->type);
1008
1009         return mono_class_is_subclass_of (klass, klassc, check_interfaces);
1010 }
1011
1012 static guint32
1013 ves_icall_get_attributes (MonoReflectionType *type)
1014 {
1015         MonoClass *klass = mono_class_from_mono_type (type->type);
1016
1017         MONO_ARCH_SAVE_REGS;
1018
1019         return klass->flags;
1020 }
1021
1022 static MonoFieldInfo*
1023 ves_icall_System_Reflection_FieldInfo_internal_from_handle (MonoClassField *handle)
1024 {
1025         MONO_ARCH_SAVE_REGS;
1026
1027         g_assert (handle);
1028
1029         return (MonoFieldInfo*) mono_field_get_object (mono_domain_get (), 
1030                                                                                                    handle->parent,
1031                                                                                                    handle);
1032 }
1033
1034 static void
1035 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
1036 {
1037         MonoDomain *domain = mono_domain_get ();
1038
1039         MONO_ARCH_SAVE_REGS;
1040
1041         info->parent = mono_type_get_object (domain, &method->klass->byval_arg);
1042         info->ret = mono_type_get_object (domain, method->signature->ret);
1043         info->attrs = method->flags;
1044         info->implattrs = method->iflags;
1045 }
1046
1047 static MonoArray*
1048 ves_icall_get_parameter_info (MonoMethod *method)
1049 {
1050         MonoDomain *domain = mono_domain_get (); 
1051         MonoArray *res;
1052         static MonoClass *System_Reflection_ParameterInfo;
1053         MonoReflectionParameter** args;
1054         int i;
1055
1056         MONO_ARCH_SAVE_REGS;
1057
1058         args = mono_param_get_objects (domain, method);
1059         if (!System_Reflection_ParameterInfo)
1060                 System_Reflection_ParameterInfo = mono_class_from_name (
1061                         mono_defaults.corlib, "System.Reflection", "ParameterInfo");
1062         res = mono_array_new (domain, System_Reflection_ParameterInfo, method->signature->param_count);
1063         for (i = 0; i < method->signature->param_count; ++i) {
1064                 mono_array_set (res, gpointer, i, args [i]);
1065         }
1066         return res;
1067 }
1068
1069 static void
1070 ves_icall_get_field_info (MonoReflectionField *field, MonoFieldInfo *info)
1071 {
1072         MonoDomain *domain = mono_object_domain (field); 
1073
1074         MONO_ARCH_SAVE_REGS;
1075
1076         info->parent = mono_type_get_object (domain, &field->klass->byval_arg);
1077         info->type = mono_type_get_object (domain, field->field->type);
1078         info->name = mono_string_new (domain, field->field->name);
1079         info->attrs = field->field->type->attrs;
1080 }
1081
1082 static MonoObject *
1083 ves_icall_MonoField_GetValueInternal (MonoReflectionField *field, MonoObject *obj)
1084 {       
1085         MonoObject *o;
1086         MonoClassField *cf = field->field;
1087         MonoClass *klass;
1088         MonoVTable *vtable;
1089         MonoDomain *domain = mono_object_domain (field); 
1090         gchar *v;
1091         gboolean is_static = FALSE;
1092         gboolean is_ref = FALSE;
1093
1094         MONO_ARCH_SAVE_REGS;
1095
1096         mono_class_init (field->klass);
1097
1098         switch (cf->type->type) {
1099         case MONO_TYPE_STRING:
1100         case MONO_TYPE_OBJECT:
1101         case MONO_TYPE_CLASS:
1102         case MONO_TYPE_ARRAY:
1103         case MONO_TYPE_SZARRAY:
1104                 is_ref = TRUE;
1105                 break;
1106         case MONO_TYPE_U1:
1107         case MONO_TYPE_I1:
1108         case MONO_TYPE_BOOLEAN:
1109         case MONO_TYPE_U2:
1110         case MONO_TYPE_I2:
1111         case MONO_TYPE_CHAR:
1112         case MONO_TYPE_U:
1113         case MONO_TYPE_I:
1114         case MONO_TYPE_U4:
1115         case MONO_TYPE_I4:
1116         case MONO_TYPE_R4:
1117         case MONO_TYPE_U8:
1118         case MONO_TYPE_I8:
1119         case MONO_TYPE_R8:
1120         case MONO_TYPE_VALUETYPE:
1121                 is_ref = cf->type->byref;
1122                 break;
1123         default:
1124                 g_error ("type 0x%x not handled in "
1125                          "ves_icall_Monofield_GetValue", cf->type->type);
1126                 return NULL;
1127         }
1128
1129         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1130                 is_static = TRUE;
1131                 vtable = mono_class_vtable (domain, field->klass);
1132                 if (!vtable->initialized)
1133                         mono_runtime_class_init (vtable);
1134         }
1135         
1136         if (is_ref) {
1137                 if (is_static) {
1138                         mono_field_static_get_value (vtable, cf, &o);
1139                 } else {
1140                         mono_field_get_value (obj, cf, &o);
1141                 }
1142                 return o;
1143         }
1144
1145         /* boxed value type */
1146         klass = mono_class_from_mono_type (cf->type);
1147         o = mono_object_new (domain, klass);
1148         v = ((gchar *) o) + sizeof (MonoObject);
1149         if (is_static) {
1150                 mono_field_static_get_value (vtable, cf, v);
1151         } else {
1152                 mono_field_get_value (obj, cf, v);
1153         }
1154
1155         return o;
1156 }
1157
1158 static void
1159 ves_icall_FieldInfo_SetValueInternal (MonoReflectionField *field, MonoObject *obj, MonoObject *value)
1160 {
1161         MonoClassField *cf = field->field;
1162         gchar *v;
1163
1164         MONO_ARCH_SAVE_REGS;
1165
1166         v = (gchar *) value;
1167         if (!cf->type->byref) {
1168                 switch (cf->type->type) {
1169                 case MONO_TYPE_U1:
1170                 case MONO_TYPE_I1:
1171                 case MONO_TYPE_BOOLEAN:
1172                 case MONO_TYPE_U2:
1173                 case MONO_TYPE_I2:
1174                 case MONO_TYPE_CHAR:
1175                 case MONO_TYPE_U:
1176                 case MONO_TYPE_I:
1177                 case MONO_TYPE_U4:
1178                 case MONO_TYPE_I4:
1179                 case MONO_TYPE_R4:
1180                 case MONO_TYPE_U8:
1181                 case MONO_TYPE_I8:
1182                 case MONO_TYPE_R8:
1183                 case MONO_TYPE_VALUETYPE:
1184                         v += sizeof (MonoObject);
1185                         break;
1186                 case MONO_TYPE_STRING:
1187                 case MONO_TYPE_OBJECT:
1188                 case MONO_TYPE_CLASS:
1189                 case MONO_TYPE_ARRAY:
1190                 case MONO_TYPE_SZARRAY:
1191                         /* Do nothing */
1192                         break;
1193                 default:
1194                         g_error ("type 0x%x not handled in "
1195                                  "ves_icall_FieldInfo_SetValueInternal", cf->type->type);
1196                         return;
1197                 }
1198         }
1199
1200         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1201                 MonoVTable *vtable = mono_class_vtable (mono_object_domain (field), field->klass);
1202                 if (!vtable->initialized)
1203                         mono_runtime_class_init (vtable);
1204                 mono_field_static_set_value (vtable, cf, v);
1205         } else {
1206                 mono_field_set_value (obj, cf, v);
1207         }
1208 }
1209
1210 static void
1211 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
1212 {
1213         MonoDomain *domain = mono_object_domain (property); 
1214
1215         MONO_ARCH_SAVE_REGS;
1216
1217         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
1218         info->name = mono_string_new (domain, property->property->name);
1219         info->attrs = property->property->attrs;
1220         info->get = property->property->get ? mono_method_get_object (domain, property->property->get, NULL): NULL;
1221         info->set = property->property->set ? mono_method_get_object (domain, property->property->set, NULL): NULL;
1222         /* 
1223          * There may be other methods defined for properties, though, it seems they are not exposed 
1224          * in the reflection API 
1225          */
1226 }
1227
1228 static void
1229 ves_icall_get_event_info (MonoReflectionEvent *event, MonoEventInfo *info)
1230 {
1231         MonoDomain *domain = mono_object_domain (event); 
1232
1233         MONO_ARCH_SAVE_REGS;
1234
1235         info->parent = mono_type_get_object (domain, &event->klass->byval_arg);
1236         info->name = mono_string_new (domain, event->event->name);
1237         info->attrs = event->event->attrs;
1238         info->add_method = event->event->add ? mono_method_get_object (domain, event->event->add, NULL): NULL;
1239         info->remove_method = event->event->remove ? mono_method_get_object (domain, event->event->remove, NULL): NULL;
1240         info->raise_method = event->event->raise ? mono_method_get_object (domain, event->event->raise, NULL): NULL;
1241 }
1242
1243 static MonoArray*
1244 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
1245 {
1246         MonoDomain *domain = mono_object_domain (type); 
1247         MonoArray *intf;
1248         int ninterf, i;
1249         MonoClass *class = mono_class_from_mono_type (type->type);
1250         MonoClass *parent;
1251
1252         MONO_ARCH_SAVE_REGS;
1253
1254         ninterf = 0;
1255         for (parent = class; parent; parent = parent->parent) {
1256                 ninterf += parent->interface_count;
1257         }
1258         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
1259         ninterf = 0;
1260         for (parent = class; parent; parent = parent->parent) {
1261                 for (i = 0; i < parent->interface_count; ++i) {
1262                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
1263                         ++ninterf;
1264                 }
1265         }
1266         return intf;
1267 }
1268
1269 static void
1270 ves_icall_Type_GetInterfaceMapData (MonoReflectionType *type, MonoReflectionType *iface, MonoArray **targets, MonoArray **methods)
1271 {
1272         MonoClass *class = mono_class_from_mono_type (type->type);
1273         MonoClass *iclass = mono_class_from_mono_type (iface->type);
1274         MonoReflectionMethod *member;
1275         int i, len, ioffset;
1276         MonoDomain *domain;
1277
1278         MONO_ARCH_SAVE_REGS;
1279
1280         /* type doesn't implement iface: the exception is thrown in managed code */
1281         if ((iclass->interface_id > class->max_interface_id) || !class->interface_offsets [iclass->interface_id])
1282                         return;
1283
1284         len = iclass->method.count;
1285         ioffset = class->interface_offsets [iclass->interface_id];
1286         domain = mono_object_domain (type);
1287         *targets = mono_array_new (domain, mono_defaults.method_info_class, len);
1288         *methods = mono_array_new (domain, mono_defaults.method_info_class, len);
1289         for (i = 0; i < len; ++i) {
1290                 member = mono_method_get_object (domain, iclass->methods [i], iclass);
1291                 mono_array_set (*methods, gpointer, i, member);
1292                 member = mono_method_get_object (domain, class->vtable [i + ioffset], class);
1293                 mono_array_set (*targets, gpointer, i, member);
1294         }
1295 }
1296
1297 static MonoReflectionType*
1298 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
1299 {
1300         MonoClass *class = mono_class_from_mono_type (type->type);
1301
1302         MONO_ARCH_SAVE_REGS;
1303
1304         if (type->type->byref)
1305                 return mono_type_get_object (mono_object_domain (type), &class->byval_arg);
1306         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1307                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
1308         else if (class->element_class)
1309                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
1310         else
1311                 return NULL;
1312 }
1313
1314 static MonoReflectionType*
1315 ves_icall_get_type_parent (MonoReflectionType *type)
1316 {
1317         MonoClass *class = mono_class_from_mono_type (type->type);
1318
1319         MONO_ARCH_SAVE_REGS;
1320
1321         return class->parent ? mono_type_get_object (mono_object_domain (type), &class->parent->byval_arg): NULL;
1322 }
1323
1324 static MonoBoolean
1325 ves_icall_type_ispointer (MonoReflectionType *type)
1326 {
1327         MONO_ARCH_SAVE_REGS;
1328
1329         return type->type->type == MONO_TYPE_PTR;
1330 }
1331
1332 static MonoBoolean
1333 ves_icall_type_isbyref (MonoReflectionType *type)
1334 {
1335         MONO_ARCH_SAVE_REGS;
1336
1337         return type->type->byref;
1338 }
1339
1340 static MonoReflectionModule*
1341 ves_icall_MonoType_get_Module (MonoReflectionType *type)
1342 {
1343         MonoClass *class = mono_class_from_mono_type (type->type);
1344
1345         MONO_ARCH_SAVE_REGS;
1346
1347         return mono_module_get_object (mono_object_domain (type), class->image);
1348 }
1349
1350 static void
1351 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
1352 {
1353         MonoDomain *domain = mono_domain_get (); 
1354         MonoClass *class = mono_class_from_mono_type (type);
1355
1356         MONO_ARCH_SAVE_REGS;
1357
1358         info->nested_in = class->nested_in ? mono_type_get_object (domain, &class->nested_in->byval_arg): NULL;
1359         info->name = mono_string_new (domain, class->name);
1360         info->name_space = mono_string_new (domain, class->name_space);
1361         info->rank = class->rank;
1362         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
1363         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1364                 info->etype = mono_type_get_object (domain, class->enum_basetype);
1365         else if (class->element_class)
1366                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
1367         else
1368                 info->etype = NULL;
1369
1370         info->isprimitive = (!type->byref && (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8));
1371 }
1372
1373 static MonoObject *
1374 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
1375 {
1376         /* 
1377          * Invoke from reflection is supposed to always be a virtual call (the API
1378          * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
1379          * greater flexibility.
1380          */
1381         MonoMethod *m = method->method;
1382         int pcount;
1383
1384         MONO_ARCH_SAVE_REGS;
1385
1386         if (this) {
1387                 if (!mono_object_isinst (this, m->klass))
1388                         mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1389                 m = mono_object_get_virtual_method (this, m);
1390         } else if (!(m->flags & METHOD_ATTRIBUTE_STATIC) && strcmp (m->name, ".ctor"))
1391                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1392
1393         pcount = params? mono_array_length (params): 0;
1394         if (pcount != m->signature->param_count)
1395                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException"));
1396
1397         if (m->klass->rank && !strcmp (m->name, ".ctor")) {
1398                 int i;
1399                 guint32 *lengths;
1400                 guint32 *lower_bounds;
1401                 pcount = mono_array_length (params);
1402                 lengths = alloca (sizeof (guint32) * pcount);
1403                 for (i = 0; i < pcount; ++i)
1404                         lengths [i] = *(gint32*) ((char*)mono_array_get (params, gpointer, i) + sizeof (MonoObject));
1405
1406                 if (m->klass->rank == pcount) {
1407                         /* Only lengths provided. */
1408                         lower_bounds = NULL;
1409                 } else {
1410                         g_assert (pcount == (m->klass->rank * 2));
1411                         /* lower bounds are first. */
1412                         lower_bounds = lengths;
1413                         lengths += m->klass->rank;
1414                 }
1415
1416                 return (MonoObject*)mono_array_new_full (mono_object_domain (params), m->klass, lengths, lower_bounds);
1417         }
1418         return mono_runtime_invoke_array (m, this, params, NULL);
1419 }
1420
1421 static MonoObject *
1422 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
1423 {
1424         MonoDomain *domain = mono_object_domain (method); 
1425         MonoMethod *m = method->method;
1426         MonoMethodSignature *sig = m->signature;
1427         MonoArray *out_args;
1428         MonoObject *result;
1429         int i, j, outarg_count = 0;
1430
1431         MONO_ARCH_SAVE_REGS;
1432
1433         if (m->klass == mono_defaults.object_class) {
1434
1435                 if (!strcmp (m->name, "FieldGetter")) {
1436                         MonoClass *k = this->vtable->klass;
1437                         MonoString *name = mono_array_get (params, MonoString *, 1);
1438                         char *str;
1439
1440                         str = mono_string_to_utf8 (name);
1441                 
1442                         for (i = 0; i < k->field.count; i++) {
1443                                 if (!strcmp (k->fields [i].name, str)) {
1444                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1445                                         if (field_klass->valuetype)
1446                                                 result = mono_value_box (domain, field_klass,
1447                                                                          (char *)this + k->fields [i].offset);
1448                                         else 
1449                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
1450                                 
1451                                         g_assert (result);
1452                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
1453                                         *outArgs = out_args;
1454                                         mono_array_set (out_args, gpointer, 0, result);
1455                                         g_free (str);
1456                                         return NULL;
1457                                 }
1458                         }
1459
1460                         g_free (str);
1461                         g_assert_not_reached ();
1462
1463                 } else if (!strcmp (m->name, "FieldSetter")) {
1464                         MonoClass *k = this->vtable->klass;
1465                         MonoString *name = mono_array_get (params, MonoString *, 1);
1466                         int size, align;
1467                         char *str;
1468
1469                         str = mono_string_to_utf8 (name);
1470                 
1471                         for (i = 0; i < k->field.count; i++) {
1472                                 if (!strcmp (k->fields [i].name, str)) {
1473                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1474                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1475
1476                                         if (field_klass->valuetype) {
1477                                                 size = mono_type_size (k->fields [i].type, &align);
1478                                                 memcpy ((char *)this + k->fields [i].offset, 
1479                                                         ((char *)val) + sizeof (MonoObject), size);
1480                                         } else 
1481                                                 *(MonoObject**)((char *)this + k->fields [i].offset) = val;
1482                                 
1483                                         out_args = mono_array_new (domain, mono_defaults.object_class, 0);
1484                                         *outArgs = out_args;
1485
1486                                         g_free (str);
1487                                         return NULL;
1488                                 }
1489                         }
1490
1491                         g_free (str);
1492                         g_assert_not_reached ();
1493
1494                 }
1495         }
1496
1497         for (i = 0; i < mono_array_length (params); i++) {
1498                 if (sig->params [i]->byref) 
1499                         outarg_count++;
1500         }
1501
1502         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1503         
1504         /* fixme: handle constructors? */
1505         if (!strcmp (method->method->name, ".ctor"))
1506                 g_assert_not_reached ();
1507
1508         result = mono_runtime_invoke_array (method->method, this, params, NULL);
1509
1510         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1511                 if (sig->params [i]->byref) {
1512                         gpointer arg;
1513                         arg = mono_array_get (params, gpointer, i);
1514                         mono_array_set (out_args, gpointer, j, arg);
1515                         j++;
1516                 }
1517         }
1518
1519         *outArgs = out_args;
1520
1521         return result;
1522 }
1523
1524 static MonoObject *
1525 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1526 {
1527         MonoDomain *domain; 
1528         MonoClass *enumc, *objc;
1529         gint32 s1, s2;
1530         MonoObject *res;
1531         
1532         MONO_ARCH_SAVE_REGS;
1533
1534         MONO_CHECK_ARG_NULL (type);
1535         MONO_CHECK_ARG_NULL (obj);
1536
1537         domain = mono_object_domain (type); 
1538         enumc = mono_class_from_mono_type (type->type);
1539         objc = obj->vtable->klass;
1540
1541         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1542         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1543                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1544         
1545         s1 = mono_class_value_size (enumc, NULL);
1546         s2 = mono_class_value_size (objc, NULL);
1547
1548         res = mono_object_new (domain, enumc);
1549
1550 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1551         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1552 #else
1553         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1554                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1555                 MIN (s1, s2));
1556 #endif
1557         return res;
1558 }
1559
1560 static MonoObject *
1561 ves_icall_System_Enum_get_value (MonoObject *this)
1562 {
1563         MonoObject *res;
1564         MonoClass *enumc;
1565         gpointer dst;
1566         gpointer src;
1567         int size;
1568
1569         MONO_ARCH_SAVE_REGS;
1570
1571         if (!this)
1572                 return NULL;
1573
1574         g_assert (this->vtable->klass->enumtype);
1575         
1576         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1577         res = mono_object_new (mono_object_domain (this), enumc);
1578         dst = (char *)res + sizeof (MonoObject);
1579         src = (char *)this + sizeof (MonoObject);
1580         size = mono_class_value_size (enumc, NULL);
1581
1582         memcpy (dst, src, size);
1583
1584         return res;
1585 }
1586
1587 static void
1588 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1589 {
1590         MonoDomain *domain = mono_object_domain (type); 
1591         MonoClass *enumc = mono_class_from_mono_type (type->type);
1592         guint i, j, nvalues, crow;
1593         MonoClassField *field;
1594         
1595         MONO_ARCH_SAVE_REGS;
1596
1597         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1598         nvalues = enumc->field.count - 1;
1599         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1600         info->values = mono_array_new (domain, enumc, nvalues);
1601         
1602         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1603                 field = &enumc->fields [i];
1604                 if (strcmp ("value__", field->name) == 0)
1605                         continue;
1606                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1607                 if (!field->data) {
1608                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1609                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1610                         /* 1 is the length of the blob */
1611                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1612                 }
1613                 switch (enumc->enum_basetype->type) {
1614                 case MONO_TYPE_U1:
1615                 case MONO_TYPE_I1:
1616                         mono_array_set (info->values, gchar, j, *field->data);
1617                         break;
1618                 case MONO_TYPE_CHAR:
1619                 case MONO_TYPE_U2:
1620                 case MONO_TYPE_I2:
1621                         mono_array_set (info->values, gint16, j, read16 (field->data));
1622                         break;
1623                 case MONO_TYPE_U4:
1624                 case MONO_TYPE_I4:
1625                         mono_array_set (info->values, gint32, j, read32 (field->data));
1626                         break;
1627                 case MONO_TYPE_U8:
1628                 case MONO_TYPE_I8:
1629                         mono_array_set (info->values, gint64, j, read64 (field->data));
1630                         break;
1631                 default:
1632                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1633                 }
1634                 ++j;
1635         }
1636 }
1637
1638 enum {
1639         BFLAGS_IgnoreCase = 1,
1640         BFLAGS_DeclaredOnly = 2,
1641         BFLAGS_Instance = 4,
1642         BFLAGS_Static = 8,
1643         BFLAGS_Public = 0x10,
1644         BFLAGS_NonPublic = 0x20,
1645         BFLAGS_InvokeMethod = 0x100,
1646         BFLAGS_CreateInstance = 0x200,
1647         BFLAGS_GetField = 0x400,
1648         BFLAGS_SetField = 0x800,
1649         BFLAGS_GetProperty = 0x1000,
1650         BFLAGS_SetProperty = 0x2000,
1651         BFLAGS_ExactBinding = 0x10000,
1652         BFLAGS_SuppressChangeType = 0x20000,
1653         BFLAGS_OptionalParamBinding = 0x40000
1654 };
1655
1656 static MonoFieldInfo *
1657 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
1658 {
1659         MonoDomain *domain; 
1660         MonoClass *startklass, *klass;
1661         int i, match;
1662         MonoClassField *field;
1663         char *utf8_name;
1664         domain = ((MonoObject *)type)->vtable->domain;
1665         klass = startklass = mono_class_from_mono_type (type->type);
1666
1667         MONO_ARCH_SAVE_REGS;
1668
1669         if (!name)
1670                 mono_raise_exception (mono_get_exception_argument_null ("name"));
1671
1672 handle_parent:  
1673         for (i = 0; i < klass->field.count; ++i) {
1674                 match = 0;
1675                 field = &klass->fields [i];
1676                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1677                         if (bflags & BFLAGS_Public)
1678                                 match++;
1679                 } else {
1680                         if (bflags & BFLAGS_NonPublic)
1681                                 match++;
1682                 }
1683                 if (!match)
1684                         continue;
1685                 match = 0;
1686                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1687                         if (bflags & BFLAGS_Static)
1688                                 match++;
1689                 } else {
1690                         if (bflags & BFLAGS_Instance)
1691                                 match++;
1692                 }
1693
1694                 if (!match)
1695                         continue;
1696                 
1697                 utf8_name = mono_string_to_utf8 (name);
1698
1699                 if (strcmp (field->name, utf8_name)) {
1700                         g_free (utf8_name);
1701                         continue;
1702                 }
1703                 g_free (utf8_name);
1704                 
1705                 return (MonoFieldInfo *)mono_field_get_object (domain, klass, field);
1706         }
1707         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1708                 goto handle_parent;
1709
1710         return NULL;
1711 }
1712
1713 static MonoArray*
1714 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1715 {
1716         MonoDomain *domain; 
1717         GSList *l = NULL, *tmp;
1718         MonoClass *startklass, *klass;
1719         MonoArray *res;
1720         MonoObject *member;
1721         int i, len, match;
1722         MonoClassField *field;
1723
1724         MONO_ARCH_SAVE_REGS;
1725
1726         domain = ((MonoObject *)type)->vtable->domain;
1727         klass = startklass = mono_class_from_mono_type (type->type);
1728
1729 handle_parent:  
1730         for (i = 0; i < klass->field.count; ++i) {
1731                 match = 0;
1732                 field = &klass->fields [i];
1733                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1734                         if (bflags & BFLAGS_Public)
1735                                 match++;
1736                 } else {
1737                         if (bflags & BFLAGS_NonPublic)
1738                                 match++;
1739                 }
1740                 if (!match)
1741                         continue;
1742                 match = 0;
1743                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1744                         if (bflags & BFLAGS_Static)
1745                                 match++;
1746                 } else {
1747                         if (bflags & BFLAGS_Instance)
1748                                 match++;
1749                 }
1750
1751                 if (!match)
1752                         continue;
1753                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1754                 l = g_slist_prepend (l, member);
1755         }
1756         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1757                 goto handle_parent;
1758         len = g_slist_length (l);
1759         res = mono_array_new (domain, mono_defaults.field_info_class, len);
1760         i = 0;
1761         tmp = g_slist_reverse (l);
1762         for (; tmp; tmp = tmp->next, ++i)
1763                 mono_array_set (res, gpointer, i, tmp->data);
1764         g_slist_free (l);
1765         return res;
1766 }
1767
1768 static MonoArray*
1769 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1770 {
1771         MonoDomain *domain; 
1772         GSList *l = NULL, *tmp;
1773         MonoClass *startklass, *klass;
1774         MonoArray *res;
1775         MonoMethod *method;
1776         MonoObject *member;
1777         int i, len, match;
1778         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1779                 
1780         MONO_ARCH_SAVE_REGS;
1781
1782         domain = ((MonoObject *)type)->vtable->domain;
1783         klass = startklass = mono_class_from_mono_type (type->type);
1784         len = 0;
1785
1786 handle_parent:
1787         for (i = 0; i < klass->method.count; ++i) {
1788                 match = 0;
1789                 method = klass->methods [i];
1790                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1791                         continue;
1792                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1793                         if (bflags & BFLAGS_Public)
1794                                 match++;
1795                 } else {
1796                         if (bflags & BFLAGS_NonPublic)
1797                                 match++;
1798                 }
1799                 if (!match)
1800                         continue;
1801                 match = 0;
1802                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1803                         if (bflags & BFLAGS_Static)
1804                                 match++;
1805                 } else {
1806                         if (bflags & BFLAGS_Instance)
1807                                 match++;
1808                 }
1809
1810                 if (!match)
1811                         continue;
1812                 match = 0;
1813                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1814                         continue;
1815                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
1816                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1817                 
1818                 l = g_slist_prepend (l, member);
1819                 len++;
1820         }
1821         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1822                 goto handle_parent;
1823         res = mono_array_new (domain, mono_defaults.method_info_class, len);
1824         i = 0;
1825         tmp = l;
1826         for (; tmp; tmp = tmp->next, ++i)
1827                 mono_array_set (res, gpointer, i, tmp->data);
1828         g_slist_free (l);
1829         g_hash_table_destroy (method_slots);
1830         return res;
1831 }
1832
1833 static MonoArray*
1834 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1835 {
1836         MonoDomain *domain; 
1837         GSList *l = NULL, *tmp;
1838         static MonoClass *System_Reflection_ConstructorInfo;
1839         MonoClass *startklass, *klass;
1840         MonoArray *res;
1841         MonoMethod *method;
1842         MonoObject *member;
1843         int i, len, match;
1844
1845         MONO_ARCH_SAVE_REGS;
1846
1847         domain = ((MonoObject *)type)->vtable->domain;
1848         klass = startklass = mono_class_from_mono_type (type->type);
1849
1850         for (i = 0; i < klass->method.count; ++i) {
1851                 match = 0;
1852                 method = klass->methods [i];
1853                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1854                         continue;
1855                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1856                         if (bflags & BFLAGS_Public)
1857                                 match++;
1858                 } else {
1859                         if (bflags & BFLAGS_NonPublic)
1860                                 match++;
1861                 }
1862                 if (!match)
1863                         continue;
1864                 match = 0;
1865                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1866                         if (bflags & BFLAGS_Static)
1867                                 match++;
1868                 } else {
1869                         if (bflags & BFLAGS_Instance)
1870                                 match++;
1871                 }
1872
1873                 if (!match)
1874                         continue;
1875                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1876                         
1877                 l = g_slist_prepend (l, member);
1878         }
1879         len = g_slist_length (l);
1880         if (!System_Reflection_ConstructorInfo)
1881                 System_Reflection_ConstructorInfo = mono_class_from_name (
1882                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1883         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1884         i = 0;
1885         tmp = g_slist_reverse (l);
1886         for (; tmp; tmp = tmp->next, ++i)
1887                 mono_array_set (res, gpointer, i, tmp->data);
1888         g_slist_free (l);
1889         return res;
1890 }
1891
1892 static MonoArray*
1893 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1894 {
1895         MonoDomain *domain; 
1896         GSList *l = NULL, *tmp;
1897         static MonoClass *System_Reflection_PropertyInfo;
1898         MonoClass *startklass, *klass;
1899         MonoArray *res;
1900         MonoMethod *method;
1901         MonoProperty *prop;
1902         int i, match;
1903         int len = 0;
1904         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1905
1906         MONO_ARCH_SAVE_REGS;
1907
1908         domain = ((MonoObject *)type)->vtable->domain;
1909         klass = startklass = mono_class_from_mono_type (type->type);
1910
1911 handle_parent:
1912         for (i = 0; i < klass->property.count; ++i) {
1913                 prop = &klass->properties [i];
1914                 match = 0;
1915                 method = prop->get;
1916                 if (!method)
1917                         method = prop->set;
1918                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1919                         if (bflags & BFLAGS_Public)
1920                                 match++;
1921                 } else {
1922                         if (bflags & BFLAGS_NonPublic)
1923                                 match++;
1924                 }
1925                 if (!match)
1926                         continue;
1927                 match = 0;
1928                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1929                         if (bflags & BFLAGS_Static)
1930                                 match++;
1931                 } else {
1932                         if (bflags & BFLAGS_Instance)
1933                                 match++;
1934                 }
1935
1936                 if (!match)
1937                         continue;
1938                 match = 0;
1939
1940                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1941                         continue;
1942                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), prop);
1943
1944                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1945                 len++;
1946         }
1947         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1948                 goto handle_parent;
1949         if (!System_Reflection_PropertyInfo)
1950                 System_Reflection_PropertyInfo = mono_class_from_name (
1951                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1952         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1953         i = 0;
1954         tmp = l;
1955         for (; tmp; tmp = tmp->next, ++i)
1956                 mono_array_set (res, gpointer, i, tmp->data);
1957         g_slist_free (l);
1958         g_hash_table_destroy (method_slots);
1959         return res;
1960 }
1961
1962 static MonoReflectionEvent *
1963 ves_icall_MonoType_GetEvent (MonoReflectionType *type, MonoString *name, guint32 bflags)
1964 {
1965         MonoDomain *domain;
1966         MonoClass *klass;
1967         gint i;
1968         MonoEvent *event;
1969         MonoMethod *method;
1970         gchar *event_name;
1971
1972         MONO_ARCH_SAVE_REGS;
1973
1974         event_name = mono_string_to_utf8 (name);
1975         klass = mono_class_from_mono_type (type->type);
1976         domain = mono_object_domain (type);
1977
1978 handle_parent:  
1979         for (i = 0; i < klass->event.count; i++) {
1980                 event = &klass->events [i];
1981                 if (strcmp (event->name, event_name))
1982                         continue;
1983
1984                 method = event->add;
1985                 if (!method)
1986                         method = event->remove;
1987
1988                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1989                         if (!(bflags & BFLAGS_Public))
1990                                 continue;
1991                 } else {
1992                         if (!(bflags & BFLAGS_NonPublic))
1993                                 continue;
1994                 }
1995
1996                 g_free (event_name);
1997                 return mono_event_get_object (domain, klass, event);
1998         }
1999
2000         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2001                 goto handle_parent;
2002
2003         g_free (event_name);
2004         return NULL;
2005 }
2006
2007 static MonoArray*
2008 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
2009 {
2010         MonoDomain *domain; 
2011         GSList *l = NULL, *tmp;
2012         static MonoClass *System_Reflection_EventInfo;
2013         MonoClass *startklass, *klass;
2014         MonoArray *res;
2015         MonoMethod *method;
2016         MonoEvent *event;
2017         int i, len, match;
2018
2019         MONO_ARCH_SAVE_REGS;
2020
2021         domain = ((MonoObject *)type)->vtable->domain;
2022         klass = startklass = mono_class_from_mono_type (type->type);
2023
2024 handle_parent:  
2025         for (i = 0; i < klass->event.count; ++i) {
2026                 event = &klass->events [i];
2027                 match = 0;
2028                 method = event->add;
2029                 if (!method)
2030                         method = event->remove;
2031                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2032                         if (bflags & BFLAGS_Public)
2033                                 match++;
2034                 } else {
2035                         if (bflags & BFLAGS_NonPublic)
2036                                 match++;
2037                 }
2038                 if (!match)
2039                         continue;
2040                 match = 0;
2041                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2042                         if (bflags & BFLAGS_Static)
2043                                 match++;
2044                 } else {
2045                         if (bflags & BFLAGS_Instance)
2046                                 match++;
2047                 }
2048
2049                 if (!match)
2050                         continue;
2051                 match = 0;
2052                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
2053         }
2054         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2055                 goto handle_parent;
2056         len = g_slist_length (l);
2057         if (!System_Reflection_EventInfo)
2058                 System_Reflection_EventInfo = mono_class_from_name (
2059                         mono_defaults.corlib, "System.Reflection", "EventInfo");
2060         res = mono_array_new (domain, System_Reflection_EventInfo, len);
2061         i = 0;
2062         tmp = l;
2063         for (; tmp; tmp = tmp->next, ++i)
2064                 mono_array_set (res, gpointer, i, tmp->data);
2065         g_slist_free (l);
2066         return res;
2067 }
2068
2069 static MonoReflectionType *
2070 ves_icall_Type_GetNestedType (MonoReflectionType *type, MonoString *name, guint32 bflags)
2071 {
2072         MonoDomain *domain; 
2073         MonoClass *startklass, *klass;
2074         MonoClass *nested;
2075         GList *tmpn;
2076         char *str;
2077         
2078         MONO_ARCH_SAVE_REGS;
2079
2080         domain = ((MonoObject *)type)->vtable->domain;
2081         klass = startklass = mono_class_from_mono_type (type->type);
2082         str = mono_string_to_utf8 (name);
2083
2084  handle_parent:
2085         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2086                 int match = 0;
2087                 nested = tmpn->data;
2088                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2089                         if (bflags & BFLAGS_Public)
2090                                 match++;
2091                 } else {
2092                         if (bflags & BFLAGS_NonPublic)
2093                                 match++;
2094                 }
2095                 if (!match)
2096                         continue;
2097                 if (strcmp (nested->name, str) == 0){
2098                         g_free (str);
2099                         return mono_type_get_object (domain, &nested->byval_arg);
2100                 }
2101         }
2102         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2103                 goto handle_parent;
2104         g_free (str);
2105         return NULL;
2106 }
2107
2108 static MonoArray*
2109 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
2110 {
2111         MonoDomain *domain; 
2112         GSList *l = NULL, *tmp;
2113         GList *tmpn;
2114         MonoClass *startklass, *klass;
2115         MonoArray *res;
2116         MonoObject *member;
2117         int i, len, match;
2118         MonoClass *nested;
2119
2120         MONO_ARCH_SAVE_REGS;
2121
2122         domain = ((MonoObject *)type)->vtable->domain;
2123         klass = startklass = mono_class_from_mono_type (type->type);
2124
2125         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2126                 match = 0;
2127                 nested = tmpn->data;
2128                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2129                         if (bflags & BFLAGS_Public)
2130                                 match++;
2131                 } else {
2132                         if (bflags & BFLAGS_NonPublic)
2133                                 match++;
2134                 }
2135                 if (!match)
2136                         continue;
2137                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
2138                 l = g_slist_prepend (l, member);
2139         }
2140         len = g_slist_length (l);
2141         res = mono_array_new (domain, mono_defaults.monotype_class, len);
2142         i = 0;
2143         tmp = g_slist_reverse (l);
2144         for (; tmp; tmp = tmp->next, ++i)
2145                 mono_array_set (res, gpointer, i, tmp->data);
2146         g_slist_free (l);
2147         return res;
2148 }
2149
2150 static MonoReflectionType*
2151 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
2152 {
2153         gchar *str;
2154         MonoType *type;
2155         MonoTypeNameParse info;
2156
2157         MONO_ARCH_SAVE_REGS;
2158
2159         str = mono_string_to_utf8 (name);
2160         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
2161         if (!mono_reflection_parse_type (str, &info)) {
2162                 g_free (str);
2163                 g_list_free (info.modifiers);
2164                 g_list_free (info.nested);
2165                 if (throwOnError) /* uhm: this is a parse error, though... */
2166                         mono_raise_exception (mono_get_exception_type_load ());
2167                 /*g_print ("failed parse\n");*/
2168                 return NULL;
2169         }
2170
2171         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
2172         g_free (str);
2173         g_list_free (info.modifiers);
2174         g_list_free (info.nested);
2175         if (!type) {
2176                 if (throwOnError)
2177                         mono_raise_exception (mono_get_exception_type_load ());
2178                 /* g_print ("failed find\n"); */
2179                 return NULL;
2180         }
2181         /* g_print ("got it\n"); */
2182         return mono_type_get_object (mono_object_domain (assembly), type);
2183
2184 }
2185
2186 static MonoString *
2187 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
2188 {
2189         MonoDomain *domain = mono_object_domain (assembly); 
2190         MonoAssembly *mass = assembly->assembly;
2191         MonoString *res;
2192         gchar *uri;
2193         gchar *absolute;
2194         
2195         MONO_ARCH_SAVE_REGS;
2196
2197         absolute = g_build_filename (mass->basedir, mass->image->module_name, NULL);
2198         uri = g_filename_to_uri (absolute, NULL, NULL);
2199         res = mono_string_new (domain, uri);
2200         g_free (uri);
2201         g_free (absolute);
2202         return res;
2203 }
2204
2205 static MonoString *
2206 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
2207 {
2208         MonoDomain *domain = mono_object_domain (assembly); 
2209         MonoString *res;
2210         char *name = g_build_filename (
2211                 assembly->assembly->basedir,
2212                 assembly->assembly->image->module_name, NULL);
2213
2214         MONO_ARCH_SAVE_REGS;
2215
2216         res = mono_string_new (domain, name);
2217         g_free (name);
2218         return res;
2219 }
2220
2221 static MonoReflectionMethod*
2222 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) 
2223 {
2224         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
2225
2226         MONO_ARCH_SAVE_REGS;
2227
2228         if (!token)
2229                 return NULL;
2230         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
2231 }
2232
2233 static MonoArray*
2234 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) 
2235 {
2236         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2237         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2238         int i;
2239         const char *val;
2240
2241         MONO_ARCH_SAVE_REGS;
2242
2243         for (i = 0; i < table->rows; ++i) {
2244                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
2245                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
2246         }
2247         return result;
2248 }
2249
2250 static MonoArray*
2251 ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAssembly *assembly) 
2252 {
2253         static MonoClass *System_Reflection_AssemblyName;
2254         MonoArray *result;
2255         MonoAssembly **ptr;
2256         MonoDomain *domain = mono_object_domain (assembly);
2257         int i, count = 0;
2258
2259         MONO_ARCH_SAVE_REGS;
2260
2261         if (!System_Reflection_AssemblyName)
2262                 System_Reflection_AssemblyName = mono_class_from_name (
2263                         mono_defaults.corlib, "System.Reflection", "AssemblyName");
2264
2265         for (ptr = assembly->assembly->image->references; ptr && *ptr; ptr++)
2266                 count++;
2267
2268         result = mono_array_new (mono_object_domain (assembly), System_Reflection_AssemblyName, count);
2269
2270         for (i = 0; i < count; i++) {
2271                 MonoAssembly *assem = assembly->assembly->image->references [i];
2272                 MonoReflectionAssemblyName *aname;
2273                 char *codebase, *absolute;
2274
2275                 aname = (MonoReflectionAssemblyName *) mono_object_new (
2276                         domain, System_Reflection_AssemblyName);
2277
2278                 if (strcmp (assem->aname.name, "corlib") == 0)
2279                         aname->name = mono_string_new (domain, "mscorlib");
2280                 else
2281                         aname->name = mono_string_new (domain, assem->aname.name);
2282                 aname->major = assem->aname.major;
2283
2284                 absolute = g_build_filename (assem->basedir, assem->image->module_name, NULL);
2285                 codebase = g_filename_to_uri (absolute, NULL, NULL);
2286                 aname->codebase = mono_string_new (domain, codebase);
2287                 g_free (codebase);
2288                 g_free (absolute);
2289                 mono_array_set (result, gpointer, i, aname);
2290         }
2291         return result;
2292 }
2293
2294 typedef struct {
2295         MonoArray *res;
2296         int idx;
2297 } NameSpaceInfo;
2298
2299 static void
2300 foreach_namespace (const char* key, gconstpointer val, NameSpaceInfo *info)
2301 {
2302         MonoString *name = mono_string_new (mono_object_domain (info->res), key);
2303
2304         mono_array_set (info->res, gpointer, info->idx, name);
2305         info->idx++;
2306 }
2307
2308 static MonoArray*
2309 ves_icall_System_Reflection_Assembly_GetNamespaces (MonoReflectionAssembly *assembly) 
2310 {
2311         MonoImage *img = assembly->assembly->image;
2312         int n;
2313         MonoArray *res;
2314         NameSpaceInfo info;
2315         
2316         MONO_ARCH_SAVE_REGS;
2317
2318         n = g_hash_table_size (img->name_cache);
2319         res = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n);
2320         info.res = res;
2321         info.idx = 0;
2322         g_hash_table_foreach (img->name_cache, (GHFunc)foreach_namespace, &info);
2323         return res;
2324 }
2325
2326 /* move this in some file in mono/util/ */
2327 static char *
2328 g_concat_dir_and_file (const char *dir, const char *file)
2329 {
2330         g_return_val_if_fail (dir != NULL, NULL);
2331         g_return_val_if_fail (file != NULL, NULL);
2332
2333         /*
2334          * If the directory name doesn't have a / on the end, we need
2335          * to add one so we get a proper path to the file
2336          */
2337         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
2338                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
2339         else
2340                 return g_strconcat (dir, file, NULL);
2341 }
2342
2343 static MonoObject*
2344 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name) 
2345 {
2346         char *n = mono_string_to_utf8 (name);
2347         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2348         guint32 i;
2349         guint32 cols [MONO_MANIFEST_SIZE];
2350         const char *val;
2351         MonoObject *result;
2352
2353         MONO_ARCH_SAVE_REGS;
2354
2355         for (i = 0; i < table->rows; ++i) {
2356                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2357                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2358                 if (strcmp (val, n) == 0)
2359                         break;
2360         }
2361         g_free (n);
2362         if (i == table->rows)
2363                 return NULL;
2364         /* FIXME */
2365         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
2366                 guint32 size;
2367                 MonoArray *data;
2368                 val = mono_image_get_resource (assembly->assembly->image, cols [MONO_MANIFEST_OFFSET], &size);
2369                 if (!val)
2370                         return NULL;
2371                 data = mono_array_new (mono_object_domain (assembly), mono_defaults.byte_class, size);
2372                 memcpy (mono_array_addr (data, char, 0), val, size);
2373                 return (MonoObject*)data;
2374         }
2375         switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
2376         case IMPLEMENTATION_FILE:
2377                 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2378                 table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2379                 i = mono_metadata_decode_row_col (table, i - 1, MONO_FILE_NAME);
2380                 val = mono_metadata_string_heap (assembly->assembly->image, i);
2381                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2382                 result = (MonoObject*)mono_string_new (mono_object_domain (assembly), n);
2383                 /* check hash if needed */
2384                 g_free (n);
2385                 return result;
2386
2387         case IMPLEMENTATION_ASSEMBLYREF:
2388                 break;
2389
2390         case IMPLEMENTATION_EXP_TYPE:
2391                 g_assert_not_reached ();
2392                 break;
2393         }
2394         return NULL;
2395 }
2396
2397 static gboolean
2398 ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflectionAssembly *assembly, MonoString *name, MonoManifestResourceInfo *info)
2399 {
2400         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2401         int i;
2402         guint32 cols [MONO_MANIFEST_SIZE];
2403         guint32 file_cols [MONO_FILE_SIZE];
2404         const char *val;
2405         char *n;
2406
2407         MONO_ARCH_SAVE_REGS;
2408
2409         n = mono_string_to_utf8 (name);
2410         for (i = 0; i < table->rows; ++i) {
2411                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2412                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2413                 if (strcmp (val, n) == 0)
2414                         break;
2415         }
2416         g_free (n);
2417         if (i == table->rows)
2418                 return FALSE;
2419
2420         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
2421                 info->location = RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST;
2422         }
2423         else {
2424                 switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
2425                 case IMPLEMENTATION_FILE:
2426                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2427                         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2428                         mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
2429                         val = mono_metadata_string_heap (assembly->assembly->image, file_cols [MONO_FILE_NAME]);
2430                         info->filename = mono_string_new (mono_object_domain (assembly), val);
2431                         if (file_cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA)
2432                                 info->location = 0;
2433                         else
2434                                 info->location = RESOURCE_LOCATION_EMBEDDED;
2435                         break;
2436
2437                 case IMPLEMENTATION_ASSEMBLYREF:
2438                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2439                         info->assembly = mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]);
2440
2441                         // Obtain info recursively
2442                         ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (info->assembly, name, info);
2443                         info->location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
2444                         break;
2445
2446                 case IMPLEMENTATION_EXP_TYPE:
2447                         g_assert_not_reached ();
2448                         break;
2449                 }
2450         }
2451
2452         return TRUE;
2453 }
2454
2455 static MonoObject*
2456 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) 
2457 {
2458         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2459         MonoArray *result = NULL;
2460         int i;
2461         const char *val;
2462         char *n;
2463
2464         MONO_ARCH_SAVE_REGS;
2465
2466         /* check hash if needed */
2467         if (name) {
2468                 n = mono_string_to_utf8 (name);
2469                 for (i = 0; i < table->rows; ++i) {
2470                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2471                         if (strcmp (val, n) == 0) {
2472                                 MonoString *fn;
2473                                 g_free (n);
2474                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2475                                 fn = mono_string_new (mono_object_domain (assembly), n);
2476                                 g_free (n);
2477                                 return (MonoObject*)fn;
2478                         }
2479                 }
2480                 g_free (n);
2481                 return NULL;
2482         }
2483
2484         for (i = 0; i < table->rows; ++i) {
2485                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2486                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2487                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2488                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
2489                 g_free (n);
2490         }
2491         return (MonoObject*)result;
2492 }
2493
2494 static MonoArray*
2495 ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly *assembly)
2496 {
2497         MonoDomain *domain = mono_domain_get();
2498         MonoArray *res;
2499         MonoClass *klass;
2500         int i, module_count = 0, file_count = 0;
2501         MonoImage **modules = assembly->assembly->image->modules;
2502         MonoTableInfo *table;
2503
2504         if (modules) {
2505                 while (modules[module_count])
2506                         ++module_count;
2507         }
2508
2509         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2510         file_count = table->rows;
2511
2512         g_assert( assembly->assembly->image != NULL);
2513         ++module_count;
2514
2515         klass = mono_class_from_name ( mono_defaults.corlib, "System.Reflection", "Module");
2516         res = mono_array_new (domain, klass, module_count + file_count);
2517
2518         mono_array_set (res, gpointer, 0, mono_module_get_object (domain, assembly->assembly->image));
2519         for ( i = 1; i < module_count; ++i )
2520                 mono_array_set (res, gpointer, i, mono_module_get_object (domain, modules[i]));
2521
2522         for (i = 0; i < table->rows; ++i)
2523                 mono_array_set (res, gpointer, module_count + i, mono_module_file_get_object (domain, assembly->assembly->image, i));
2524
2525         return res;
2526 }
2527
2528 static MonoReflectionMethod*
2529 ves_icall_GetCurrentMethod (void) 
2530 {
2531         MonoMethod *m = mono_method_get_last_managed ();
2532
2533         MONO_ARCH_SAVE_REGS;
2534
2535         return mono_method_get_object (mono_domain_get (), m, NULL);
2536 }
2537
2538 static MonoReflectionAssembly*
2539 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
2540 {
2541         MonoMethod *m = mono_method_get_last_managed ();
2542
2543         MONO_ARCH_SAVE_REGS;
2544
2545         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
2546 }
2547
2548
2549 static gboolean
2550 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
2551 {
2552         MonoMethod **dest = data;
2553
2554         /* skip unmanaged frames */
2555         if (!managed)
2556                 return FALSE;
2557
2558         if (m == *dest) {
2559                 *dest = NULL;
2560                 return FALSE;
2561         }
2562         if (!(*dest)) {
2563                 *dest = m;
2564                 return TRUE;
2565         }
2566         return FALSE;
2567 }
2568
2569 static MonoReflectionAssembly*
2570 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
2571 {
2572         MonoDomain* domain = mono_domain_get ();
2573
2574         MONO_ARCH_SAVE_REGS;
2575
2576         if (!domain->entry_assembly)
2577                 domain = mono_root_domain;
2578
2579         return mono_assembly_get_object (domain, domain->entry_assembly);
2580 }
2581
2582
2583 static MonoReflectionAssembly*
2584 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
2585 {
2586         MonoMethod *m = mono_method_get_last_managed ();
2587         MonoMethod *dest = m;
2588
2589         MONO_ARCH_SAVE_REGS;
2590
2591         mono_stack_walk (get_caller, &dest);
2592         if (!dest)
2593                 dest = m;
2594         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
2595 }
2596
2597 static MonoString *
2598 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
2599 {
2600         MonoDomain *domain = mono_object_domain (object); 
2601         MonoString *res;
2602         gchar *name;
2603
2604         MONO_ARCH_SAVE_REGS;
2605
2606         name = mono_type_get_name (object->type);
2607         res = mono_string_new (domain, name);
2608         g_free (name);
2609
2610         return res;
2611 }
2612
2613 static void
2614 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
2615 {
2616         MonoAssemblyName *name = &assembly->assembly->aname;
2617
2618         MONO_ARCH_SAVE_REGS;
2619
2620         if (strcmp (name->name, "corlib") == 0)
2621                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
2622         else
2623                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
2624
2625         aname->major = name->major;
2626         aname->minor = name->minor;
2627         aname->build = name->build;
2628         aname->revision = name->revision;
2629 }
2630
2631 static MonoArray*
2632 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
2633 {
2634         MonoDomain *domain = mono_object_domain (assembly); 
2635         MonoArray *res;
2636         MonoClass *klass;
2637         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
2638         int i, count;
2639         guint32 attrs, visibility;
2640
2641         MONO_ARCH_SAVE_REGS;
2642
2643         /* we start the count from 1 because we skip the special type <Module> */
2644         if (exportedOnly) {
2645                 count = 0;
2646                 for (i = 1; i < tdef->rows; ++i) {
2647                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2648                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2649                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
2650                                 count++;
2651                 }
2652         } else {
2653                 count = tdef->rows - 1;
2654         }
2655         res = mono_array_new (domain, mono_defaults.monotype_class, count);
2656         count = 0;
2657         for (i = 1; i < tdef->rows; ++i) {
2658                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2659                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2660                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
2661                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
2662                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
2663                         count++;
2664                 }
2665         }
2666         
2667         return res;
2668 }
2669
2670 static MonoReflectionType*
2671 ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
2672 {
2673         MonoDomain *domain = mono_object_domain (module); 
2674         MonoClass *klass;
2675
2676         MONO_ARCH_SAVE_REGS;
2677
2678         g_assert (module->image);
2679         klass = mono_class_get (module->image, 1 | MONO_TOKEN_TYPE_DEF);
2680         return mono_type_get_object (domain, &klass->byval_arg);
2681 }
2682
2683 static MonoString*
2684 ves_icall_System_Reflection_Module_GetGuidInternal (MonoReflectionModule *module)
2685 {
2686         MonoDomain *domain = mono_object_domain (module); 
2687
2688         MONO_ARCH_SAVE_REGS;
2689
2690         g_assert (module->image);
2691         return mono_string_new (domain, module->image->guid);
2692 }
2693
2694 static MonoReflectionType*
2695 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
2696 {
2697         MonoClass *klass;
2698         int isbyref = 0, rank;
2699         char *str = mono_string_to_utf8 (smodifiers);
2700         char *p;
2701
2702         MONO_ARCH_SAVE_REGS;
2703
2704         klass = mono_class_from_mono_type (tb->type.type);
2705         p = str;
2706         /* logic taken from mono_reflection_parse_type(): keep in sync */
2707         while (*p) {
2708                 switch (*p) {
2709                 case '&':
2710                         if (isbyref) { /* only one level allowed by the spec */
2711                                 g_free (str);
2712                                 return NULL;
2713                         }
2714                         isbyref = 1;
2715                         p++;
2716                         g_free (str);
2717                         return mono_type_get_object (mono_object_domain (tb), &klass->this_arg);
2718                         break;
2719                 case '*':
2720                         klass = mono_ptr_class_get (&klass->byval_arg);
2721                         mono_class_init (klass);
2722                         p++;
2723                         break;
2724                 case '[':
2725                         rank = 1;
2726                         p++;
2727                         while (*p) {
2728                                 if (*p == ']')
2729                                         break;
2730                                 if (*p == ',')
2731                                         rank++;
2732                                 else if (*p != '*') { /* '*' means unknown lower bound */
2733                                         g_free (str);
2734                                         return NULL;
2735                                 }
2736                                 ++p;
2737                         }
2738                         if (*p != ']') {
2739                                 g_free (str);
2740                                 return NULL;
2741                         }
2742                         p++;
2743                         klass = mono_array_class_get (&klass->byval_arg, rank);
2744                         mono_class_init (klass);
2745                         break;
2746                 default:
2747                         break;
2748                 }
2749         }
2750         g_free (str);
2751         return mono_type_get_object (mono_object_domain (tb), &klass->byval_arg);
2752 }
2753
2754 static MonoObject *
2755 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
2756                                                    MonoReflectionMethod *info)
2757 {
2758         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
2759         MonoObject *delegate;
2760         gpointer func;
2761
2762         MONO_ARCH_SAVE_REGS;
2763
2764         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
2765
2766         delegate = mono_object_new (mono_object_domain (type), delegate_class);
2767
2768         func = mono_compile_method (info->method);
2769
2770         mono_delegate_ctor (delegate, target, func);
2771
2772         return delegate;
2773 }
2774
2775 /*
2776  * Magic number to convert a time which is relative to
2777  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
2778  */
2779 #define EPOCH_ADJUST    ((gint64)62135596800L)
2780
2781 static gint64
2782 ves_icall_System_DateTime_GetNow (void)
2783 {
2784 #ifdef PLATFORM_WIN32
2785         SYSTEMTIME st;
2786         FILETIME ft;
2787         
2788         GetLocalTime (&st);
2789         SystemTimeToFileTime (&st, &ft);
2790         return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
2791 #else
2792         /* FIXME: put this in io-layer and call it GetLocalTime */
2793         struct timeval tv;
2794         gint64 res;
2795
2796         MONO_ARCH_SAVE_REGS;
2797
2798         if (gettimeofday (&tv, NULL) == 0) {
2799                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
2800                 return res;
2801         }
2802         /* fixme: raise exception */
2803         return 0;
2804 #endif
2805 }
2806
2807 /*
2808  * This is heavily based on zdump.c from glibc 2.2.
2809  *
2810  *  * data[0]:  start of daylight saving time (in DateTime ticks).
2811  *  * data[1]:  end of daylight saving time (in DateTime ticks).
2812  *  * data[2]:  utcoffset (in TimeSpan ticks).
2813  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
2814  *  * name[0]:  name of this timezone when not daylight saving.
2815  *  * name[1]:  name of this timezone when daylight saving.
2816  *
2817  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
2818  *         the class library allows years between 1 and 9999.
2819  *
2820  *  Returns true on success and zero on failure.
2821  */
2822 static guint32
2823 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
2824 {
2825 #ifndef PLATFORM_WIN32
2826         MonoDomain *domain = mono_domain_get ();
2827         struct tm start, tt;
2828         time_t t;
2829
2830         long int gmtoff;
2831         int is_daylight = 0, day;
2832         char tzone [64];
2833
2834         MONO_ARCH_SAVE_REGS;
2835
2836         MONO_CHECK_ARG_NULL (data);
2837         MONO_CHECK_ARG_NULL (names);
2838
2839         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2840         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2841
2842         /* 
2843          * no info is better than crashing: we'll need our own tz data to make 
2844          * this work properly, anyway.
2845          */
2846         if ((year < 1900) || (year > 2100)) {
2847                 t = time (NULL);
2848                 tt = *localtime (&t);
2849                 strftime (tzone, sizeof (tzone), "%Z", &tt);
2850                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2851                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2852                 return 1;
2853         }
2854
2855         memset (&start, 0, sizeof (start));
2856
2857         start.tm_mday = 1;
2858         start.tm_year = year-1900;
2859
2860         t = mktime (&start);
2861 #if defined (HAVE_TIMEZONE)
2862 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
2863 #elif defined (HAVE_TM_GMTOFF)
2864 #define gmt_offset(x) x.tm_gmtoff
2865 #else
2866 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
2867 #endif
2868         
2869         gmtoff = gmt_offset (start);
2870
2871         /* For each day of the year, calculate the tm_gmtoff. */
2872         for (day = 0; day < 365; day++) {
2873
2874                 t += 3600*24;
2875                 tt = *localtime (&t);
2876
2877                 /* Daylight saving starts or ends here. */
2878                 if (gmt_offset (tt) != gmtoff) {
2879                         struct tm tt1;
2880                         time_t t1;
2881
2882                         /* Try to find the exact hour when daylight saving starts/ends. */
2883                         t1 = t;
2884                         do {
2885                                 t1 -= 3600;
2886                                 tt1 = *localtime (&t1);
2887                         } while (gmt_offset (tt1) != gmtoff);
2888
2889                         /* Try to find the exact minute when daylight saving starts/ends. */
2890                         do {
2891                                 t1 += 60;
2892                                 tt1 = *localtime (&t1);
2893                         } while (gmt_offset (tt1) == gmtoff);
2894                         
2895                         strftime (tzone, sizeof (tzone), "%Z", &tt);
2896                         
2897                         /* Write data, if we're already in daylight saving, we're done. */
2898                         if (is_daylight) {
2899                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2900                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2901                                 return 1;
2902                         } else {
2903                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2904                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2905                                 is_daylight = 1;
2906                         }
2907
2908                         /* This is only set once when we enter daylight saving. */
2909                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
2910                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
2911
2912                         gmtoff = gmt_offset (tt);
2913                 }
2914
2915                 gmtoff = gmt_offset (tt);
2916         }
2917
2918         if (!is_daylight) {
2919                 strftime (tzone, sizeof (tzone), "%Z", &tt);
2920                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2921                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2922                 mono_array_set ((*data), gint64, 0, 0);
2923                 mono_array_set ((*data), gint64, 1, 0);
2924                 mono_array_set ((*data), gint64, 2, (gint64) gmtoff * 10000000L);
2925                 mono_array_set ((*data), gint64, 3, 0);
2926         }
2927
2928         return 1;
2929 #else
2930         MonoDomain *domain = mono_domain_get ();
2931         TIME_ZONE_INFORMATION tz_info;
2932         FILETIME ft;
2933         int i;
2934
2935         GetTimeZoneInformation (&tz_info);
2936
2937         MONO_CHECK_ARG_NULL (data);
2938         MONO_CHECK_ARG_NULL (names);
2939
2940         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2941         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2942
2943         for (i = 0; i < 32; ++i)
2944                 if (!tz_info.DaylightName [i])
2945                         break;
2946         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
2947         for (i = 0; i < 32; ++i)
2948                 if (!tz_info.StandardName [i])
2949                         break;
2950         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
2951
2952         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
2953         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2954         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
2955         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2956         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
2957         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
2958
2959         return 1;
2960 #endif
2961 }
2962
2963 static gpointer
2964 ves_icall_System_Object_obj_address (MonoObject *this) 
2965 {
2966         MONO_ARCH_SAVE_REGS;
2967
2968         return this;
2969 }
2970
2971 /* System.Buffer */
2972
2973 static gint32 
2974 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) 
2975 {
2976         MonoClass *klass;
2977         MonoTypeEnum etype;
2978         int length, esize;
2979         int i;
2980
2981         MONO_ARCH_SAVE_REGS;
2982
2983         klass = array->obj.vtable->klass;
2984         etype = klass->element_class->byval_arg.type;
2985         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
2986                 return -1;
2987
2988         if (array->bounds == NULL)
2989                 length = array->max_length;
2990         else {
2991                 length = 1;
2992                 for (i = 0; i < klass->rank; ++ i)
2993                         length *= array->bounds [i].length;
2994         }
2995
2996         esize = mono_array_element_size (klass);
2997         return length * esize;
2998 }
2999
3000 static gint8 
3001 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) 
3002 {
3003         MONO_ARCH_SAVE_REGS;
3004
3005         return mono_array_get (array, gint8, idx);
3006 }
3007
3008 static void 
3009 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) 
3010 {
3011         MONO_ARCH_SAVE_REGS;
3012
3013         mono_array_set (array, gint8, idx, value);
3014 }
3015
3016 static void 
3017 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) 
3018 {
3019         char *src_buf, *dest_buf;
3020
3021         MONO_ARCH_SAVE_REGS;
3022
3023         src_buf = (gint8 *)src->vector + src_offset;
3024         dest_buf = (gint8 *)dest->vector + dest_offset;
3025
3026         memcpy (dest_buf, src_buf, count);
3027 }
3028
3029 static MonoObject *
3030 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
3031 {
3032         MonoDomain *domain = mono_object_domain (this); 
3033         MonoObject *res;
3034         MonoRealProxy *rp = ((MonoRealProxy *)this);
3035         MonoType *type;
3036         MonoClass *klass;
3037
3038         MONO_ARCH_SAVE_REGS;
3039
3040         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
3041         
3042         ((MonoTransparentProxy *)res)->rp = rp;
3043         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
3044         klass = mono_class_from_mono_type (type);
3045
3046         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3047                 ((MonoTransparentProxy *)res)->klass = mono_defaults.marshalbyrefobject_class;
3048         else
3049                 ((MonoTransparentProxy *)res)->klass = klass;
3050
3051         res->vtable = mono_class_proxy_vtable (domain, klass);
3052
3053         return res;
3054 }
3055
3056 /* System.Environment */
3057
3058 static MonoString *
3059 ves_icall_System_Environment_get_MachineName (void)
3060 {
3061 #if defined (PLATFORM_WIN32)
3062         gunichar2 *buf;
3063         guint32 len;
3064         MonoString *result;
3065
3066         len = MAX_COMPUTERNAME_LENGTH + 1;
3067         buf = g_new (gunichar2, len);
3068
3069         result = NULL;
3070         if (GetComputerName (buf, (PDWORD) &len))
3071                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
3072
3073         g_free (buf);
3074         return result;
3075 #else
3076         gchar *buf;
3077         int len;
3078         MonoString *result;
3079
3080         MONO_ARCH_SAVE_REGS;
3081
3082         len = 256;
3083         buf = g_new (gchar, len);
3084
3085         result = NULL;
3086         if (gethostname (buf, len) == 0)
3087                 result = mono_string_new (mono_domain_get (), buf);
3088         
3089         g_free (buf);
3090         return result;
3091 #endif
3092 }
3093
3094 static int
3095 ves_icall_System_Environment_get_Platform (void)
3096 {
3097         MONO_ARCH_SAVE_REGS;
3098
3099 #if defined (PLATFORM_WIN32)
3100         /* Win32NT */
3101         return 2;
3102 #else
3103         /* Unix */
3104         return 128;
3105 #endif
3106 }
3107
3108 static MonoString *
3109 ves_icall_System_Environment_get_NewLine (void)
3110 {
3111         MONO_ARCH_SAVE_REGS;
3112
3113 #if defined (PLATFORM_WIN32)
3114         return mono_string_new (mono_domain_get (), "\r\n");
3115 #else
3116         return mono_string_new (mono_domain_get (), "\n");
3117 #endif
3118 }
3119
3120 static MonoString *
3121 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
3122 {
3123         const gchar *value;
3124         gchar *utf8_name;
3125
3126         MONO_ARCH_SAVE_REGS;
3127
3128         if (name == NULL)
3129                 return NULL;
3130
3131         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
3132         value = g_getenv (utf8_name);
3133         g_free (utf8_name);
3134
3135         if (value == 0)
3136                 return NULL;
3137         
3138         return mono_string_new (mono_domain_get (), value);
3139 }
3140
3141 /*
3142  * There is no standard way to get at environ.
3143  */
3144 #ifndef _MSC_VER
3145 extern
3146 #endif
3147 char **environ;
3148
3149 static MonoArray *
3150 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
3151 {
3152         MonoArray *names;
3153         MonoDomain *domain;
3154         MonoString *str;
3155         gchar **e, **parts;
3156         int n;
3157
3158         MONO_ARCH_SAVE_REGS;
3159
3160         n = 0;
3161         for (e = environ; *e != 0; ++ e)
3162                 ++ n;
3163
3164         domain = mono_domain_get ();
3165         names = mono_array_new (domain, mono_defaults.string_class, n);
3166
3167         n = 0;
3168         for (e = environ; *e != 0; ++ e) {
3169                 parts = g_strsplit (*e, "=", 2);
3170                 if (*parts != 0) {
3171                         str = mono_string_new (domain, *parts);
3172                         mono_array_set (names, MonoString *, n, str);
3173                 }
3174
3175                 g_strfreev (parts);
3176
3177                 ++ n;
3178         }
3179
3180         return names;
3181 }
3182
3183 /*
3184  * Returns the number of milliseconds elapsed since the system started.
3185  */
3186 static gint32
3187 ves_icall_System_Environment_get_TickCount (void)
3188 {
3189 #if defined (PLATFORM_WIN32)
3190         return GetTickCount();
3191 #else
3192         struct timeval tv;
3193         struct timezone tz;
3194         gint32 res;
3195
3196         MONO_ARCH_SAVE_REGS;
3197
3198         res = (gint32) gettimeofday (&tv, &tz);
3199
3200         if (res != -1)
3201                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
3202         return res;
3203 #endif
3204 }
3205
3206
3207 static void
3208 ves_icall_System_Environment_Exit (int result)
3209 {
3210         MONO_ARCH_SAVE_REGS;
3211
3212         mono_profiler_shutdown ();
3213
3214         /* we may need to do some cleanup here... */
3215         exit (result);
3216 }
3217
3218 static MonoString*
3219 ves_icall_System_Text_Encoding_InternalCodePage (void) 
3220 {
3221         const char *cset;
3222
3223         MONO_ARCH_SAVE_REGS;
3224
3225         g_get_charset (&cset);
3226         /* g_print ("charset: %s\n", cset); */
3227         /* handle some common aliases */
3228         switch (*cset) {
3229         case 'A':
3230                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
3231                         cset = "us-ascii";
3232                 break;
3233         }
3234         return mono_string_new (mono_domain_get (), cset);
3235 }
3236
3237 static void
3238 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
3239                                          MonoReflectionMethod *method,
3240                                          MonoArray *out_args)
3241 {
3242         MONO_ARCH_SAVE_REGS;
3243
3244         mono_message_init (mono_object_domain (this), this, method, out_args);
3245 }
3246
3247 static MonoBoolean
3248 ves_icall_IsTransparentProxy (MonoObject *proxy)
3249 {
3250         MONO_ARCH_SAVE_REGS;
3251
3252         if (!proxy)
3253                 return 0;
3254
3255         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
3256                 return 1;
3257
3258         return 0;
3259 }
3260
3261 static void
3262 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
3263 {
3264         MonoClass *klass;
3265         MonoVTable* vtable;
3266
3267         MONO_ARCH_SAVE_REGS;
3268
3269         klass = mono_class_from_mono_type (type->type);
3270         vtable = mono_class_vtable (mono_domain_get (), klass);
3271
3272         if (enable) vtable->remote = 1;
3273         else vtable->remote = 0;
3274 }
3275
3276 static MonoObject *
3277 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionType *type)
3278 {
3279         MonoClass *klass;
3280         MonoDomain *domain;
3281         
3282         MONO_ARCH_SAVE_REGS;
3283
3284         domain = mono_object_domain (type);
3285         klass = mono_class_from_mono_type (type->type);
3286
3287         // Bypass remoting object creation check
3288         return mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
3289 }
3290
3291 static MonoObject *
3292 ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal (MonoReflectionType *type)
3293 {
3294         MonoClass *klass;
3295         MonoObject *obj;
3296         MonoDomain *domain;
3297         
3298         MONO_ARCH_SAVE_REGS;
3299
3300         domain = mono_object_domain (type);
3301         klass = mono_class_from_mono_type (type->type);
3302
3303         if (klass->rank >= 1) {
3304                 g_assert (klass->rank == 1);
3305                 obj = (MonoObject *) mono_array_new (domain, klass->element_class, 0);
3306         } else {
3307                 obj = mono_object_new (domain, klass);
3308         }
3309
3310         return obj;
3311 }
3312
3313 static MonoString *
3314 ves_icall_System_IO_get_temp_path (void)
3315 {
3316         MONO_ARCH_SAVE_REGS;
3317
3318         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
3319 }
3320
3321 static gpointer
3322 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
3323 {
3324         MONO_ARCH_SAVE_REGS;
3325
3326         return mono_compile_method (method);
3327 }
3328
3329 char const * mono_cfg_dir = "";
3330
3331 void    
3332 mono_install_get_config_dir (void)
3333 {
3334 #ifdef PLATFORM_WIN32
3335   int i;
3336 #endif
3337
3338   mono_cfg_dir = getenv ("MONO_CFG_DIR");
3339
3340   if (!mono_cfg_dir) {
3341 #ifndef PLATFORM_WIN32
3342     mono_cfg_dir = MONO_CFG_DIR;
3343 #else
3344     mono_cfg_dir = g_strdup (MONO_CFG_DIR);
3345     for (i = strlen (mono_cfg_dir) - 1; i >= 0; i--) {
3346         if (mono_cfg_dir [i] == '/')
3347             ((char*) mono_cfg_dir) [i] = '\\';
3348     }
3349 #endif
3350   }
3351 }
3352
3353
3354 static MonoString *
3355 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
3356 {
3357         static MonoString *mcpath;
3358         gchar *path;
3359
3360         MONO_ARCH_SAVE_REGS;
3361
3362         if (mcpath != NULL)
3363                 return mcpath;
3364
3365         path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", "machine.config", NULL);
3366
3367 #if defined (PLATFORM_WIN32)
3368         /* Avoid mixing '/' and '\\' */
3369         {
3370                 gint i;
3371                 for (i = strlen (path) - 1; i >= 0; i--)
3372                         if (path [i] == '/')
3373                                 path [i] = '\\';
3374         }
3375 #endif
3376         mcpath = mono_string_new (mono_domain_get (), path);
3377         g_free (path);
3378
3379         return mcpath;
3380 }
3381
3382 static void
3383 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (MonoString *message)
3384 {
3385 #if defined (PLATFORM_WIN32)
3386         static void (*output_debug) (gchar *);
3387         static gboolean tried_loading = FALSE;
3388         gchar *str;
3389
3390         MONO_ARCH_SAVE_REGS;
3391
3392         if (!tried_loading && output_debug == NULL) {
3393                 GModule *k32;
3394
3395                 tried_loading = TRUE;
3396                 k32 = g_module_open ("kernel32", G_MODULE_BIND_LAZY);
3397                 if (!k32) {
3398                         gchar *error = g_strdup (g_module_error ());
3399                         g_warning ("Failed to load kernel32.dll: %s\n", error);
3400                         g_free (error);
3401                         return;
3402                 }
3403
3404                 g_module_symbol (k32, "OutputDebugStringW", (gpointer *) &output_debug);
3405                 if (!output_debug) {
3406                         gchar *error = g_strdup (g_module_error ());
3407                         g_warning ("Failed to load OutputDebugStringW: %s\n", error);
3408                         g_free (error);
3409                         return;
3410                 }
3411         }
3412
3413         if (output_debug == NULL)
3414                 return;
3415         
3416         str = mono_string_to_utf8 (message);
3417         output_debug (str);
3418         g_free (str);
3419 #else
3420         g_warning ("WriteWindowsDebugString called and PLATFORM_WIN32 not defined!\n");
3421 #endif
3422 }
3423
3424 /* Only used for value types */
3425 static MonoObject *
3426 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionType *type)
3427 {
3428         MonoClass *klass;
3429         MonoDomain *domain;
3430         
3431         MONO_ARCH_SAVE_REGS;
3432
3433         domain = mono_object_domain (type);
3434         klass = mono_class_from_mono_type (type->type);
3435
3436         return mono_object_new (domain, klass);
3437 }
3438
3439 static MonoReflectionMethod *
3440 ves_icall_MonoMethod_get_base_definition (MonoReflectionMethod *m)
3441 {
3442         MonoClass *klass;
3443         MonoMethod *method = m->method;
3444         MonoMethod *result = NULL;
3445
3446         MONO_ARCH_SAVE_REGS;
3447
3448         if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
3449              method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ||
3450              method->flags & METHOD_ATTRIBUTE_NEW_SLOT)
3451                 return m;
3452
3453         if (method->klass == NULL || (klass = method->klass->parent) == NULL)
3454                 return m;
3455
3456         if (klass->vtable_size > method->slot)
3457                 result = klass->vtable [method->slot];
3458
3459         if (result == NULL)
3460                 return m;
3461
3462         return mono_method_get_object (mono_domain_get (), result, NULL);
3463 }
3464
3465 static void
3466 mono_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
3467 {
3468         MONO_ARCH_SAVE_REGS;
3469
3470         iter->sig = *(MonoMethodSignature**)argsp;
3471         
3472         g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
3473         g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
3474
3475         iter->next_arg = 0;
3476         /* FIXME: it's not documented what start is exactly... */
3477         iter->args = start? start: argsp + sizeof (gpointer);
3478         iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
3479
3480         // g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos);
3481 }
3482
3483 static MonoTypedRef
3484 mono_ArgIterator_IntGetNextArg (MonoArgIterator *iter)
3485 {
3486         gint i, align, arg_size;
3487         MonoTypedRef res;
3488         MONO_ARCH_SAVE_REGS;
3489
3490         i = iter->sig->sentinelpos + iter->next_arg;
3491
3492         g_assert (i < iter->sig->param_count);
3493
3494         res.type = iter->sig->params [i];
3495         /* FIXME: endianess issue... */
3496         res.value = iter->args;
3497         arg_size = mono_type_stack_size (res.type, &align);
3498         iter->args = (char*)iter->args + arg_size;
3499         iter->next_arg++;
3500
3501         //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
3502
3503         return res;
3504 }
3505
3506 static MonoTypedRef
3507 mono_ArgIterator_IntGetNextArgT (MonoArgIterator *iter, MonoType *type)
3508 {
3509         gint i, align, arg_size;
3510         MonoTypedRef res;
3511         MONO_ARCH_SAVE_REGS;
3512
3513         i = iter->sig->sentinelpos + iter->next_arg;
3514
3515         g_assert (i < iter->sig->param_count);
3516
3517         while (i < iter->sig->param_count) {
3518                 if (!mono_metadata_type_equal (type, iter->sig->params [i]))
3519                         continue;
3520                 res.type = iter->sig->params [i];
3521                 /* FIXME: endianess issue... */
3522                 res.value = iter->args;
3523                 arg_size = mono_type_stack_size (res.type, &align);
3524                 iter->args = (char*)iter->args + arg_size;
3525                 iter->next_arg++;
3526                 //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
3527                 return res;
3528         }
3529         //g_print ("arg type 0x%02x not found\n", res.type->type);
3530
3531         res.type = NULL;
3532         res.value = NULL;
3533         return res;
3534 }
3535
3536 static MonoType*
3537 mono_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
3538 {
3539         gint i;
3540         MONO_ARCH_SAVE_REGS;
3541         
3542         i = iter->sig->sentinelpos + iter->next_arg;
3543
3544         g_assert (i < iter->sig->param_count);
3545
3546         return iter->sig->params [i];
3547 }
3548
3549 static MonoObject*
3550 mono_TypedReference_ToObject (MonoTypedRef tref)
3551 {
3552         MonoClass *klass;
3553         MONO_ARCH_SAVE_REGS;
3554
3555         if (MONO_TYPE_IS_REFERENCE (tref.type)) {
3556                 MonoObject** objp = tref.value;
3557                 return *objp;
3558         }
3559         klass = mono_class_from_mono_type (tref.type);
3560
3561         return mono_value_box (mono_domain_get (), klass, tref.value);
3562 }
3563
3564 /* icall map */
3565
3566 static gconstpointer icall_map [] = {
3567         /*
3568          * System.Array
3569          */
3570         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
3571         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
3572         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
3573         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
3574         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
3575         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
3576         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
3577         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
3578         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
3579         "System.Array::Clone",            mono_array_clone,
3580
3581         /*
3582          * System.ArgIterator
3583          */
3584         "System.ArgIterator::Setup",                            mono_ArgIterator_Setup,
3585         "System.ArgIterator::IntGetNextArg()",                  mono_ArgIterator_IntGetNextArg,
3586         "System.ArgIterator::IntGetNextArg(System.RuntimeTypeHandle)", mono_ArgIterator_IntGetNextArgT,
3587         "System.ArgIterator::IntGetNextArgType",                mono_ArgIterator_IntGetNextArgType,
3588
3589         /*
3590          * System.TypedReference
3591          */
3592         "System.TypedReference::ToObject",                      mono_TypedReference_ToObject,
3593
3594         /*
3595          * System.Object
3596          */
3597         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
3598         "System.Object::GetType", ves_icall_System_Object_GetType,
3599         "System.Object::InternalGetHashCode", ves_icall_System_Object_GetHashCode,
3600         "System.Object::obj_address", ves_icall_System_Object_obj_address,
3601
3602         /*
3603          * System.ValueType
3604          */
3605         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
3606         "System.ValueType::InternalEquals", ves_icall_System_ValueType_Equals,
3607
3608         /*
3609          * System.String
3610          */
3611         
3612         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
3613         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
3614         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
3615         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
3616         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
3617         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
3618         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
3619         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
3620         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
3621         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
3622         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
3623         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
3624         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
3625         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
3626         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
3627         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
3628         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
3629         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
3630         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
3631         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
3632         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
3633         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
3634         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
3635         "System.String::InternalPad", ves_icall_System_String_InternalPad,
3636         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
3637         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
3638         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
3639         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
3640         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
3641         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
3642         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
3643         "System.String::InternalCompare(string,int,string,int,int,int)", ves_icall_System_String_InternalCompareStr_N,
3644         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
3645         "System.String::get_Chars", ves_icall_System_String_get_Chars,
3646
3647         /*
3648          * System.AppDomain
3649          */
3650         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
3651         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
3652         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
3653         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
3654         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
3655         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
3656         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
3657         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
3658         "System.AppDomain::InternalUnload", ves_icall_System_AppDomain_InternalUnload,
3659         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
3660         "System.AppDomain::InternalSetDomain", ves_icall_System_AppDomain_InternalSetDomain,
3661         "System.AppDomain::InternalSetDomainByID", ves_icall_System_AppDomain_InternalSetDomainByID,
3662         "System.AppDomain::InternalSetContext", ves_icall_System_AppDomain_InternalSetContext,
3663         "System.AppDomain::InternalGetContext", ves_icall_System_AppDomain_InternalGetContext,
3664         "System.AppDomain::InternalGetDefaultContext", ves_icall_System_AppDomain_InternalGetDefaultContext,
3665         "System.AppDomain::InternalGetProcessGuid", ves_icall_System_AppDomain_InternalGetProcessGuid,
3666
3667         /*
3668          * System.AppDomainSetup
3669          */
3670         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
3671
3672         /*
3673          * System.Double
3674          */
3675         "System.Double::ParseImpl",    mono_double_ParseImpl,
3676
3677         /*
3678          * System.Decimal
3679          */
3680         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
3681         "System.Decimal::decimal2Int64", mono_decimal2Int64,
3682         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
3683         "System.Decimal::decimalIncr", mono_decimalIncr,
3684         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
3685         "System.Decimal::decimal2double", mono_decimal2double,
3686         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
3687         "System.Decimal::decimalRound", mono_decimalRound,
3688         "System.Decimal::decimalMult", mono_decimalMult,
3689         "System.Decimal::decimalDiv", mono_decimalDiv,
3690         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
3691         "System.Decimal::decimalCompare", mono_decimalCompare,
3692         "System.Decimal::string2decimal", mono_string2decimal,
3693         "System.Decimal::decimal2string", mono_decimal2string,
3694
3695         /*
3696          * ModuleBuilder
3697          */
3698         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
3699         "System.Reflection.Emit.ModuleBuilder::basic_init", mono_image_module_basic_init,
3700         
3701         /*
3702          * AssemblyBuilder
3703          */
3704         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
3705         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
3706         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
3707         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
3708         "System.Reflection.Emit.AssemblyBuilder::build_metadata", ves_icall_AssemblyBuilder_build_metadata,
3709
3710         /*
3711          * Reflection stuff.
3712          */
3713         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
3714         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
3715         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
3716         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
3717         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
3718         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
3719         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
3720         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
3721         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
3722         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
3723         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
3724         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
3725         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
3726         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
3727
3728         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
3729         "System.Reflection.MonoMethod::get_base_definition", ves_icall_MonoMethod_get_base_definition,
3730         
3731         /* System.Enum */
3732
3733         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
3734         "System.Enum::get_value", ves_icall_System_Enum_get_value,
3735         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
3736
3737         /*
3738          * TypeBuilder
3739          */
3740         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
3741         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
3742         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
3743         
3744         /*
3745          * MethodBuilder
3746          */
3747         
3748         /*
3749          * System.Type
3750          */
3751         "System.Type::internal_from_name", ves_icall_type_from_name,
3752         "System.Type::internal_from_handle", ves_icall_type_from_handle,
3753         "System.MonoType::get_attributes", ves_icall_get_attributes,
3754         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
3755         "System.Type::Equals", ves_icall_type_Equals,
3756         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
3757         "System.Type::GetInterfaceMapData", ves_icall_Type_GetInterfaceMapData,
3758
3759         /*
3760          * System.Reflection.FieldInfo
3761          */
3762         "System.Reflection.FieldInfo::internal_from_handle", ves_icall_System_Reflection_FieldInfo_internal_from_handle,
3763
3764         /*
3765          * System.Runtime.CompilerServices.RuntimeHelpers
3766          */
3767         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
3768         "System.Runtime.CompilerServices.RuntimeHelpers::GetOffsetToStringData", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData,
3769         "System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue,
3770         "System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor,
3771         
3772         /*
3773          * System.Threading
3774          */
3775         "System.Threading.Thread::Abort_internal(object)", ves_icall_System_Threading_Thread_Abort,
3776         "System.Threading.Thread::ResetAbort_internal()", ves_icall_System_Threading_Thread_ResetAbort,
3777         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
3778         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
3779         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
3780         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
3781         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
3782         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
3783         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
3784         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
3785         "System.Threading.Thread::GetDomainID", ves_icall_System_Threading_Thread_GetDomainID,
3786         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
3787         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
3788         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
3789         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
3790         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
3791         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
3792         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
3793         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
3794         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
3795         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
3796         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
3797         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
3798         "System.Threading.NativeEventCalls::CloseEvent_internal", ves_icall_System_Threading_Events_CloseEvent_internal,
3799
3800         /*
3801          * System.Threading.WaitHandle
3802          */
3803         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
3804         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
3805         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
3806
3807         /*
3808          * System.Runtime.InteropServices.Marshal
3809          */
3810         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
3811         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
3812         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
3813         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
3814         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
3815         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
3816         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
3817         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
3818         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
3819         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
3820
3821         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
3822         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
3823         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
3824         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
3825         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
3826         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
3827         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
3828
3829         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
3830         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
3831         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
3832         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
3833         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
3834         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
3835         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
3836         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
3837         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
3838         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
3839         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
3840         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
3841         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
3842         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
3843         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
3844
3845
3846         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
3847         "System.Reflection.Assembly::InternalGetType", ves_icall_System_Reflection_Assembly_InternalGetType,
3848         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
3849         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
3850         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
3851         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
3852         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
3853         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
3854         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
3855         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
3856         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
3857         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
3858         "System.Reflection.Assembly::GetManifestResourceInfoInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal,
3859         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
3860         "System.Reflection.Assembly::GetReferencedAssemblies", ves_icall_System_Reflection_Assembly_GetReferencedAssemblies,
3861         "System.Reflection.Assembly::GetNamespaces", ves_icall_System_Reflection_Assembly_GetNamespaces,
3862         "System.Reflection.Assembly::GetModulesInternal", ves_icall_System_Reflection_Assembly_GetModulesInternal,
3863
3864         /*
3865          * System.Reflection.Module
3866          */
3867         "System.Reflection.Module::GetGlobalType", ves_icall_System_Reflection_Module_GetGlobalType,
3868         "System.Reflection.Module::GetGuidInternal", ves_icall_System_Reflection_Module_GetGuidInternal,
3869
3870         /*
3871          * System.MonoType.
3872          */
3873         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
3874         "System.MonoType::type_from_obj", mono_type_type_from_obj,
3875         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
3876         "System.MonoType::get_type_info", ves_icall_get_type_info,
3877         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
3878         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
3879         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
3880         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
3881         "System.MonoType::GetField", ves_icall_Type_GetField,
3882         "System.MonoType::GetFields", ves_icall_Type_GetFields,
3883         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
3884         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
3885         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
3886         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
3887         "System.MonoType::InternalGetEvent", ves_icall_MonoType_GetEvent,
3888         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
3889         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
3890         "System.MonoType::GetNestedType", ves_icall_Type_GetNestedType,
3891
3892         /*
3893          * System.Net.Sockets I/O Services
3894          */
3895         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
3896         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
3897         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
3898         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
3899         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
3900         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
3901         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
3902         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
3903         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
3904         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
3905         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
3906         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
3907         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
3908         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
3909         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
3910         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
3911         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
3912         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
3913         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
3914         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
3915         "System.Net.Dns::GetHostByName_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByName_internal,
3916         "System.Net.Dns::GetHostByAddr_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByAddr_internal,
3917         "System.Net.Dns::GetHostName_internal(string&)", ves_icall_System_Net_Dns_GetHostName_internal,
3918
3919         /*
3920          * System.Char
3921          */
3922         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
3923         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
3924         "System.Char::IsControl", ves_icall_System_Char_IsControl,
3925         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
3926         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
3927         "System.Char::IsLower", ves_icall_System_Char_IsLower,
3928         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
3929         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
3930         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
3931         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
3932         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
3933         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
3934         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
3935         "System.Char::ToLower", ves_icall_System_Char_ToLower,
3936         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
3937
3938         /*
3939          * System.Text.Encoding
3940          */
3941         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
3942
3943         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
3944         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
3945
3946         /*
3947          * System.GC
3948          */
3949         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
3950         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
3951         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
3952         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
3953         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
3954         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
3955         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
3956         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
3957         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
3958         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
3959
3960         /*
3961          * System.Security.Cryptography calls
3962          */
3963
3964          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes,
3965          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes,
3966         
3967         /*
3968          * System.Buffer
3969          */
3970         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
3971         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
3972         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
3973         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
3974
3975         /*
3976          * System.IO.MonoIO
3977          */
3978         "System.IO.MonoIO::CreateDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CreateDirectory,
3979         "System.IO.MonoIO::RemoveDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_RemoveDirectory,
3980         "System.IO.MonoIO::FindFirstFile(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindFirstFile,
3981         "System.IO.MonoIO::FindNextFile(intptr,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindNextFile,
3982         "System.IO.MonoIO::FindClose(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindClose,
3983         "System.IO.MonoIO::GetCurrentDirectory(System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
3984         "System.IO.MonoIO::SetCurrentDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
3985         "System.IO.MonoIO::MoveFile(string,string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_MoveFile,
3986         "System.IO.MonoIO::CopyFile(string,string,bool,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CopyFile,
3987         "System.IO.MonoIO::DeleteFile(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_DeleteFile,
3988         "System.IO.MonoIO::GetFileAttributes(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileAttributes,
3989         "System.IO.MonoIO::SetFileAttributes(string,System.IO.FileAttributes,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileAttributes,
3990         "System.IO.MonoIO::GetFileType(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileType,
3991         "System.IO.MonoIO::GetFileStat(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileStat,
3992         "System.IO.MonoIO::Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open,
3993         "System.IO.MonoIO::Close(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Close,
3994         "System.IO.MonoIO::Read(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Read,
3995         "System.IO.MonoIO::Write(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Write,
3996         "System.IO.MonoIO::Seek(intptr,long,System.IO.SeekOrigin,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Seek,
3997         "System.IO.MonoIO::GetLength(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetLength,
3998         "System.IO.MonoIO::SetLength(intptr,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetLength,
3999         "System.IO.MonoIO::SetFileTime(intptr,long,long,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileTime,
4000         "System.IO.MonoIO::Flush(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Flush,
4001         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
4002         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
4003         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
4004         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
4005         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
4006         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
4007         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
4008         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
4009         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
4010         "System.IO.MonoIO::GetTempPath(string&)", ves_icall_System_IO_MonoIO_GetTempPath,
4011
4012         /*
4013          * System.Math
4014          */
4015         "System.Math::Floor", ves_icall_System_Math_Floor,
4016         "System.Math::Round", ves_icall_System_Math_Round,
4017         "System.Math::Round2", ves_icall_System_Math_Round2,
4018         "System.Math::Sin", ves_icall_System_Math_Sin,
4019         "System.Math::Cos", ves_icall_System_Math_Cos,
4020         "System.Math::Tan", ves_icall_System_Math_Tan,
4021         "System.Math::Sinh", ves_icall_System_Math_Sinh,
4022         "System.Math::Cosh", ves_icall_System_Math_Cosh,
4023         "System.Math::Tanh", ves_icall_System_Math_Tanh,
4024         "System.Math::Acos", ves_icall_System_Math_Acos,
4025         "System.Math::Asin", ves_icall_System_Math_Asin,
4026         "System.Math::Atan", ves_icall_System_Math_Atan,
4027         "System.Math::Atan2", ves_icall_System_Math_Atan2,
4028         "System.Math::Exp", ves_icall_System_Math_Exp,
4029         "System.Math::Log", ves_icall_System_Math_Log,
4030         "System.Math::Log10", ves_icall_System_Math_Log10,
4031         "System.Math::Pow", ves_icall_System_Math_Pow,
4032         "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
4033
4034         /*
4035          * System.Environment
4036          */
4037         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
4038         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
4039         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
4040         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
4041         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
4042         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
4043         "System.Environment::Exit", ves_icall_System_Environment_Exit,
4044         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
4045         "System.Environment::get_ExitCode", mono_environment_exitcode_get,
4046         "System.Environment::set_ExitCode", mono_environment_exitcode_set,
4047
4048         /*
4049          * System.Runtime.Remoting
4050          */     
4051         "System.Runtime.Remoting.RemotingServices::InternalExecute",
4052         ves_icall_InternalExecute,
4053         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
4054         ves_icall_IsTransparentProxy,
4055
4056         /*
4057          * System.Runtime.Remoting.Activation
4058          */     
4059         "System.Runtime.Remoting.Activation.ActivationServices::AllocateUninitializedClassInstance",
4060         ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance,
4061         "System.Runtime.Remoting.Activation.ActivationServices::EnableProxyActivation",
4062         ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation,
4063
4064         /*
4065          * System.Runtime.Remoting.Messaging
4066          */     
4067         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
4068         ves_icall_MonoMethodMessage_InitMessage,
4069         
4070         /*
4071          * System.Runtime.Remoting.Proxies
4072          */     
4073         "System.Runtime.Remoting.Proxies.RealProxy::InternalGetTransparentProxy", 
4074         ves_icall_Remoting_RealProxy_GetTransparentProxy,
4075
4076         /*
4077          * System.Threading.Interlocked
4078          */
4079         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
4080         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
4081         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
4082         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
4083         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
4084         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
4085         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
4086         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
4087         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
4088         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
4089
4090         /*
4091          * System.Diagnostics.Process
4092          */
4093         "System.Diagnostics.Process::GetProcess_internal(int)", ves_icall_System_Diagnostics_Process_GetProcess_internal,
4094         "System.Diagnostics.Process::GetProcesses_internal()", ves_icall_System_Diagnostics_Process_GetProcesses_internal,
4095         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
4096         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
4097         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
4098         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
4099         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
4100         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
4101         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
4102         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
4103         "System.Diagnostics.Process::ProcessName_internal(intptr)", ves_icall_System_Diagnostics_Process_ProcessName_internal,
4104         "System.Diagnostics.Process::GetWorkingSet_internal(intptr,int&,int&)", ves_icall_System_Diagnostics_Process_GetWorkingSet_internal,
4105         "System.Diagnostics.Process::SetWorkingSet_internal(intptr,int,int,bool)", ves_icall_System_Diagnostics_Process_SetWorkingSet_internal,
4106         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
4107
4108         /* 
4109          * System.Delegate
4110          */
4111         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
4112
4113         /* 
4114          * System.Runtime.Serialization
4115          */
4116         "System.Runtime.Serialization.FormatterServices::GetUninitializedObjectInternal",
4117         ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal,
4118
4119         /*
4120          * System.IO.Path
4121          */
4122         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
4123
4124         /*
4125          * Private icalls for the Mono Debugger
4126          */
4127         "System.Reflection.Assembly::MonoDebugger_GetMethod",
4128         ves_icall_MonoDebugger_GetMethod,
4129
4130         "System.Reflection.Assembly::MonoDebugger_GetMethodToken",
4131         ves_icall_MonoDebugger_GetMethodToken,
4132
4133         "System.Reflection.Assembly::MonoDebugger_GetLocalTypeFromSignature",
4134         ves_icall_MonoDebugger_GetLocalTypeFromSignature,
4135
4136         "System.Reflection.Assembly::MonoDebugger_GetType",
4137         ves_icall_MonoDebugger_GetType,
4138
4139         /*
4140          * System.Configuration
4141          */
4142         "System.Configuration.DefaultConfig::get_machine_config_path",
4143         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
4144
4145         /*
4146          * System.Diagnostics.DefaultTraceListener
4147          */
4148         "System.Diagnostics.DefaultTraceListener::WriteWindowsDebugString",
4149         ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString,
4150         /*
4151          * System.Activator
4152          */
4153         "System.Activator::CreateInstanceInternal",
4154         ves_icall_System_Activator_CreateInstanceInternal,
4155         /*
4156          * add other internal calls here
4157          */
4158         NULL, NULL
4159 };
4160
4161 void
4162 mono_init_icall (void)
4163 {
4164         const char *name;
4165         int i = 0;
4166
4167         while ((name = icall_map [i])) {
4168                 mono_add_internal_call (name, icall_map [i+1]);
4169                 i += 2;
4170         }
4171        
4172 }
4173
4174