777c0a959955a0fe10a3b22f067ecda5fad3900c
[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/debug-mono-symfile.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_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         }
1133         
1134         if (is_ref) {
1135                 if (is_static) {
1136                         mono_field_static_get_value (vtable, cf, &o);
1137                 } else {
1138                         mono_field_get_value (obj, cf, &o);
1139                 }
1140                 return o;
1141         }
1142
1143         /* boxed value type */
1144         klass = mono_class_from_mono_type (cf->type);
1145         o = mono_object_new (domain, klass);
1146         v = ((gchar *) o) + sizeof (MonoObject);
1147         if (is_static) {
1148                 mono_field_static_get_value (vtable, cf, v);
1149         } else {
1150                 mono_field_get_value (obj, cf, v);
1151         }
1152
1153         return o;
1154 }
1155
1156 static void
1157 ves_icall_FieldInfo_SetValueInternal (MonoReflectionField *field, MonoObject *obj, MonoObject *value)
1158 {
1159         MonoClassField *cf = field->field;
1160         gchar *v;
1161
1162         MONO_ARCH_SAVE_REGS;
1163
1164         v = (gchar *) value;
1165         if (!cf->type->byref) {
1166                 switch (cf->type->type) {
1167                 case MONO_TYPE_U1:
1168                 case MONO_TYPE_I1:
1169                 case MONO_TYPE_BOOLEAN:
1170                 case MONO_TYPE_U2:
1171                 case MONO_TYPE_I2:
1172                 case MONO_TYPE_CHAR:
1173                 case MONO_TYPE_U:
1174                 case MONO_TYPE_I:
1175                 case MONO_TYPE_U4:
1176                 case MONO_TYPE_I4:
1177                 case MONO_TYPE_R4:
1178                 case MONO_TYPE_U8:
1179                 case MONO_TYPE_I8:
1180                 case MONO_TYPE_R8:
1181                 case MONO_TYPE_VALUETYPE:
1182                         v += sizeof (MonoObject);
1183                         break;
1184                 case MONO_TYPE_STRING:
1185                 case MONO_TYPE_OBJECT:
1186                 case MONO_TYPE_CLASS:
1187                 case MONO_TYPE_ARRAY:
1188                 case MONO_TYPE_SZARRAY:
1189                         /* Do nothing */
1190                         break;
1191                 default:
1192                         g_error ("type 0x%x not handled in "
1193                                  "ves_icall_FieldInfo_SetValueInternal", cf->type->type);
1194                         return;
1195                 }
1196         }
1197
1198         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1199                 MonoVTable *vtable = mono_class_vtable (mono_object_domain (field), field->klass);
1200                 mono_field_static_set_value (vtable, cf, v);
1201         } else {
1202                 mono_field_set_value (obj, cf, v);
1203         }
1204 }
1205
1206 static void
1207 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
1208 {
1209         MonoDomain *domain = mono_object_domain (property); 
1210
1211         MONO_ARCH_SAVE_REGS;
1212
1213         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
1214         info->name = mono_string_new (domain, property->property->name);
1215         info->attrs = property->property->attrs;
1216         info->get = property->property->get ? mono_method_get_object (domain, property->property->get, NULL): NULL;
1217         info->set = property->property->set ? mono_method_get_object (domain, property->property->set, NULL): NULL;
1218         /* 
1219          * There may be other methods defined for properties, though, it seems they are not exposed 
1220          * in the reflection API 
1221          */
1222 }
1223
1224 static void
1225 ves_icall_get_event_info (MonoReflectionEvent *event, MonoEventInfo *info)
1226 {
1227         MonoDomain *domain = mono_object_domain (event); 
1228
1229         MONO_ARCH_SAVE_REGS;
1230
1231         info->parent = mono_type_get_object (domain, &event->klass->byval_arg);
1232         info->name = mono_string_new (domain, event->event->name);
1233         info->attrs = event->event->attrs;
1234         info->add_method = event->event->add ? mono_method_get_object (domain, event->event->add, NULL): NULL;
1235         info->remove_method = event->event->remove ? mono_method_get_object (domain, event->event->remove, NULL): NULL;
1236         info->raise_method = event->event->raise ? mono_method_get_object (domain, event->event->raise, NULL): NULL;
1237 }
1238
1239 static MonoArray*
1240 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
1241 {
1242         MonoDomain *domain = mono_object_domain (type); 
1243         MonoArray *intf;
1244         int ninterf, i;
1245         MonoClass *class = mono_class_from_mono_type (type->type);
1246         MonoClass *parent;
1247
1248         MONO_ARCH_SAVE_REGS;
1249
1250         ninterf = 0;
1251         for (parent = class; parent; parent = parent->parent) {
1252                 ninterf += parent->interface_count;
1253         }
1254         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
1255         ninterf = 0;
1256         for (parent = class; parent; parent = parent->parent) {
1257                 for (i = 0; i < parent->interface_count; ++i) {
1258                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
1259                         ++ninterf;
1260                 }
1261         }
1262         return intf;
1263 }
1264
1265 static void
1266 ves_icall_Type_GetInterfaceMapData (MonoReflectionType *type, MonoReflectionType *iface, MonoArray **targets, MonoArray **methods)
1267 {
1268         MonoClass *class = mono_class_from_mono_type (type->type);
1269         MonoClass *iclass = mono_class_from_mono_type (iface->type);
1270         MonoReflectionMethod *member;
1271         int i, len, ioffset;
1272         MonoDomain *domain;
1273
1274         MONO_ARCH_SAVE_REGS;
1275
1276         /* type doesn't implement iface: the exception is thrown in managed code */
1277         if ((iclass->interface_id > class->max_interface_id) || !class->interface_offsets [iclass->interface_id])
1278                         return;
1279
1280         len = iclass->method.count;
1281         ioffset = class->interface_offsets [iclass->interface_id];
1282         domain = mono_object_domain (type);
1283         *targets = mono_array_new (domain, mono_defaults.method_info_class, len);
1284         *methods = mono_array_new (domain, mono_defaults.method_info_class, len);
1285         for (i = 0; i < len; ++i) {
1286                 member = mono_method_get_object (domain, iclass->methods [i], iclass);
1287                 mono_array_set (*methods, gpointer, i, member);
1288                 member = mono_method_get_object (domain, class->vtable [i + ioffset], class);
1289                 mono_array_set (*targets, gpointer, i, member);
1290         }
1291 }
1292
1293 static MonoReflectionType*
1294 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
1295 {
1296         MonoClass *class = mono_class_from_mono_type (type->type);
1297
1298         MONO_ARCH_SAVE_REGS;
1299
1300         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1301                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
1302         else if (class->element_class)
1303                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
1304         else
1305                 return NULL;
1306 }
1307
1308 static MonoReflectionType*
1309 ves_icall_get_type_parent (MonoReflectionType *type)
1310 {
1311         MonoClass *class = mono_class_from_mono_type (type->type);
1312
1313         MONO_ARCH_SAVE_REGS;
1314
1315         return class->parent ? mono_type_get_object (mono_object_domain (type), &class->parent->byval_arg): NULL;
1316 }
1317
1318 static MonoBoolean
1319 ves_icall_type_ispointer (MonoReflectionType *type)
1320 {
1321         MONO_ARCH_SAVE_REGS;
1322
1323         return type->type->type == MONO_TYPE_PTR;
1324 }
1325
1326 static MonoBoolean
1327 ves_icall_type_isbyref (MonoReflectionType *type)
1328 {
1329         MONO_ARCH_SAVE_REGS;
1330
1331         return type->type->byref;
1332 }
1333
1334 static MonoReflectionModule*
1335 ves_icall_MonoType_get_Module (MonoReflectionType *type)
1336 {
1337         MonoClass *class = mono_class_from_mono_type (type->type);
1338
1339         MONO_ARCH_SAVE_REGS;
1340
1341         return mono_module_get_object (mono_object_domain (type), class->image);
1342 }
1343
1344 static void
1345 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
1346 {
1347         MonoDomain *domain = mono_domain_get (); 
1348         MonoClass *class = mono_class_from_mono_type (type);
1349
1350         MONO_ARCH_SAVE_REGS;
1351
1352         info->nested_in = class->nested_in ? mono_type_get_object (domain, &class->nested_in->byval_arg): NULL;
1353         info->name = mono_string_new (domain, class->name);
1354         info->name_space = mono_string_new (domain, class->name_space);
1355         info->rank = class->rank;
1356         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
1357         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1358                 info->etype = mono_type_get_object (domain, class->enum_basetype);
1359         else if (class->element_class)
1360                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
1361         else
1362                 info->etype = NULL;
1363
1364         info->isprimitive = (!type->byref && (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8));
1365 }
1366
1367 static MonoObject *
1368 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
1369 {
1370         /* 
1371          * Invoke from reflection is supposed to always be a virtual call (the API
1372          * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
1373          * greater flexibility.
1374          */
1375         MonoMethod *m = method->method;
1376         int pcount;
1377
1378         MONO_ARCH_SAVE_REGS;
1379
1380         if (this) {
1381                 if (!mono_object_isinst (this, m->klass))
1382                         mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1383                 m = mono_object_get_virtual_method (this, m);
1384         } else if (!(m->flags & METHOD_ATTRIBUTE_STATIC) && strcmp (m->name, ".ctor"))
1385                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1386
1387         pcount = params? mono_array_length (params): 0;
1388         if (pcount != m->signature->param_count)
1389                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException"));
1390
1391         return mono_runtime_invoke_array (m, this, params, NULL);
1392 }
1393
1394 static MonoObject *
1395 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
1396 {
1397         MonoDomain *domain = mono_object_domain (method); 
1398         MonoMethod *m = method->method;
1399         MonoMethodSignature *sig = m->signature;
1400         MonoArray *out_args;
1401         MonoObject *result;
1402         int i, j, outarg_count = 0;
1403
1404         MONO_ARCH_SAVE_REGS;
1405
1406         if (m->klass == mono_defaults.object_class) {
1407
1408                 if (!strcmp (m->name, "FieldGetter")) {
1409                         MonoClass *k = this->vtable->klass;
1410                         MonoString *name = mono_array_get (params, MonoString *, 1);
1411                         char *str;
1412
1413                         str = mono_string_to_utf8 (name);
1414                 
1415                         for (i = 0; i < k->field.count; i++) {
1416                                 if (!strcmp (k->fields [i].name, str)) {
1417                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1418                                         if (field_klass->valuetype)
1419                                                 result = mono_value_box (domain, field_klass,
1420                                                                          (char *)this + k->fields [i].offset);
1421                                         else 
1422                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
1423                                 
1424                                         g_assert (result);
1425                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
1426                                         *outArgs = out_args;
1427                                         mono_array_set (out_args, gpointer, 0, result);
1428                                         g_free (str);
1429                                         return NULL;
1430                                 }
1431                         }
1432
1433                         g_free (str);
1434                         g_assert_not_reached ();
1435
1436                 } else if (!strcmp (m->name, "FieldSetter")) {
1437                         MonoClass *k = this->vtable->klass;
1438                         MonoString *name = mono_array_get (params, MonoString *, 1);
1439                         int size, align;
1440                         char *str;
1441
1442                         str = mono_string_to_utf8 (name);
1443                 
1444                         for (i = 0; i < k->field.count; i++) {
1445                                 if (!strcmp (k->fields [i].name, str)) {
1446                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1447                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1448
1449                                         if (field_klass->valuetype) {
1450                                                 size = mono_type_size (k->fields [i].type, &align);
1451                                                 memcpy ((char *)this + k->fields [i].offset, 
1452                                                         ((char *)val) + sizeof (MonoObject), size);
1453                                         } else 
1454                                                 *(MonoObject**)((char *)this + k->fields [i].offset) = val;
1455                                 
1456                                         out_args = mono_array_new (domain, mono_defaults.object_class, 0);
1457                                         *outArgs = out_args;
1458
1459                                         g_free (str);
1460                                         return NULL;
1461                                 }
1462                         }
1463
1464                         g_free (str);
1465                         g_assert_not_reached ();
1466
1467                 }
1468         }
1469
1470         for (i = 0; i < mono_array_length (params); i++) {
1471                 if (sig->params [i]->byref) 
1472                         outarg_count++;
1473         }
1474
1475         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1476         
1477         /* fixme: handle constructors? */
1478         if (!strcmp (method->method->name, ".ctor"))
1479                 g_assert_not_reached ();
1480
1481         result = mono_runtime_invoke_array (method->method, this, params, NULL);
1482
1483         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1484                 if (sig->params [i]->byref) {
1485                         gpointer arg;
1486                         arg = mono_array_get (params, gpointer, i);
1487                         mono_array_set (out_args, gpointer, j, arg);
1488                         j++;
1489                 }
1490         }
1491
1492         *outArgs = out_args;
1493
1494         return result;
1495 }
1496
1497 static MonoObject *
1498 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1499 {
1500         MonoDomain *domain; 
1501         MonoClass *enumc, *objc;
1502         gint32 s1, s2;
1503         MonoObject *res;
1504         
1505         MONO_ARCH_SAVE_REGS;
1506
1507         MONO_CHECK_ARG_NULL (type);
1508         MONO_CHECK_ARG_NULL (obj);
1509
1510         domain = mono_object_domain (type); 
1511         enumc = mono_class_from_mono_type (type->type);
1512         objc = obj->vtable->klass;
1513
1514         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1515         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1516                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1517         
1518         s1 = mono_class_value_size (enumc, NULL);
1519         s2 = mono_class_value_size (objc, NULL);
1520
1521         res = mono_object_new (domain, enumc);
1522
1523 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1524         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1525 #else
1526         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1527                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1528                 MIN (s1, s2));
1529 #endif
1530         return res;
1531 }
1532
1533 static MonoObject *
1534 ves_icall_System_Enum_get_value (MonoObject *this)
1535 {
1536         MonoObject *res;
1537         MonoClass *enumc;
1538         gpointer dst;
1539         gpointer src;
1540         int size;
1541
1542         MONO_ARCH_SAVE_REGS;
1543
1544         if (!this)
1545                 return NULL;
1546
1547         g_assert (this->vtable->klass->enumtype);
1548         
1549         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1550         res = mono_object_new (mono_object_domain (this), enumc);
1551         dst = (char *)res + sizeof (MonoObject);
1552         src = (char *)this + sizeof (MonoObject);
1553         size = mono_class_value_size (enumc, NULL);
1554
1555         memcpy (dst, src, size);
1556
1557         return res;
1558 }
1559
1560 static void
1561 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1562 {
1563         MonoDomain *domain = mono_object_domain (type); 
1564         MonoClass *enumc = mono_class_from_mono_type (type->type);
1565         guint i, j, nvalues, crow;
1566         MonoClassField *field;
1567         
1568         MONO_ARCH_SAVE_REGS;
1569
1570         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1571         nvalues = enumc->field.count - 1;
1572         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1573         info->values = mono_array_new (domain, enumc, nvalues);
1574         
1575         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1576                 field = &enumc->fields [i];
1577                 if (strcmp ("value__", field->name) == 0)
1578                         continue;
1579                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1580                 if (!field->data) {
1581                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1582                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1583                         /* 1 is the length of the blob */
1584                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1585                 }
1586                 switch (enumc->enum_basetype->type) {
1587                 case MONO_TYPE_U1:
1588                 case MONO_TYPE_I1:
1589                         mono_array_set (info->values, gchar, j, *field->data);
1590                         break;
1591                 case MONO_TYPE_CHAR:
1592                 case MONO_TYPE_U2:
1593                 case MONO_TYPE_I2:
1594                         mono_array_set (info->values, gint16, j, read16 (field->data));
1595                         break;
1596                 case MONO_TYPE_U4:
1597                 case MONO_TYPE_I4:
1598                         mono_array_set (info->values, gint32, j, read32 (field->data));
1599                         break;
1600                 case MONO_TYPE_U8:
1601                 case MONO_TYPE_I8:
1602                         mono_array_set (info->values, gint64, j, read64 (field->data));
1603                         break;
1604                 default:
1605                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1606                 }
1607                 ++j;
1608         }
1609 }
1610
1611 enum {
1612         BFLAGS_IgnoreCase = 1,
1613         BFLAGS_DeclaredOnly = 2,
1614         BFLAGS_Instance = 4,
1615         BFLAGS_Static = 8,
1616         BFLAGS_Public = 0x10,
1617         BFLAGS_NonPublic = 0x20,
1618         BFLAGS_InvokeMethod = 0x100,
1619         BFLAGS_CreateInstance = 0x200,
1620         BFLAGS_GetField = 0x400,
1621         BFLAGS_SetField = 0x800,
1622         BFLAGS_GetProperty = 0x1000,
1623         BFLAGS_SetProperty = 0x2000,
1624         BFLAGS_ExactBinding = 0x10000,
1625         BFLAGS_SuppressChangeType = 0x20000,
1626         BFLAGS_OptionalParamBinding = 0x40000
1627 };
1628
1629 static MonoFieldInfo *
1630 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
1631 {
1632         MonoDomain *domain; 
1633         MonoClass *startklass, *klass;
1634         int i, match;
1635         MonoClassField *field;
1636         char *utf8_name;
1637         domain = ((MonoObject *)type)->vtable->domain;
1638         klass = startklass = mono_class_from_mono_type (type->type);
1639
1640         MONO_ARCH_SAVE_REGS;
1641
1642         if (!name)
1643                 return NULL;
1644
1645 handle_parent:  
1646         for (i = 0; i < klass->field.count; ++i) {
1647                 match = 0;
1648                 field = &klass->fields [i];
1649                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1650                         if (bflags & BFLAGS_Public)
1651                                 match++;
1652                 } else {
1653                         if (bflags & BFLAGS_NonPublic)
1654                                 match++;
1655                 }
1656                 if (!match)
1657                         continue;
1658                 match = 0;
1659                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1660                         if (bflags & BFLAGS_Static)
1661                                 match++;
1662                 } else {
1663                         if (bflags & BFLAGS_Instance)
1664                                 match++;
1665                 }
1666
1667                 if (!match)
1668                         continue;
1669                 
1670                 utf8_name = mono_string_to_utf8 (name);
1671
1672                 if (strcmp (field->name, utf8_name)) {
1673                         g_free (utf8_name);
1674                         continue;
1675                 }
1676                 g_free (utf8_name);
1677                 
1678                 return (MonoFieldInfo *)mono_field_get_object (domain, klass, field);
1679         }
1680         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1681                 goto handle_parent;
1682
1683         return NULL;
1684 }
1685
1686 static MonoArray*
1687 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1688 {
1689         MonoDomain *domain; 
1690         GSList *l = NULL, *tmp;
1691         MonoClass *startklass, *klass;
1692         MonoArray *res;
1693         MonoObject *member;
1694         int i, len, match;
1695         MonoClassField *field;
1696
1697         MONO_ARCH_SAVE_REGS;
1698
1699         domain = ((MonoObject *)type)->vtable->domain;
1700         klass = startklass = mono_class_from_mono_type (type->type);
1701
1702 handle_parent:  
1703         for (i = 0; i < klass->field.count; ++i) {
1704                 match = 0;
1705                 field = &klass->fields [i];
1706                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1707                         if (bflags & BFLAGS_Public)
1708                                 match++;
1709                 } else {
1710                         if (bflags & BFLAGS_NonPublic)
1711                                 match++;
1712                 }
1713                 if (!match)
1714                         continue;
1715                 match = 0;
1716                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1717                         if (bflags & BFLAGS_Static)
1718                                 match++;
1719                 } else {
1720                         if (bflags & BFLAGS_Instance)
1721                                 match++;
1722                 }
1723
1724                 if (!match)
1725                         continue;
1726                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1727                 l = g_slist_prepend (l, member);
1728         }
1729         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1730                 goto handle_parent;
1731         len = g_slist_length (l);
1732         res = mono_array_new (domain, mono_defaults.field_info_class, len);
1733         i = 0;
1734         tmp = g_slist_reverse (l);
1735         for (; tmp; tmp = tmp->next, ++i)
1736                 mono_array_set (res, gpointer, i, tmp->data);
1737         g_slist_free (l);
1738         return res;
1739 }
1740
1741 static MonoArray*
1742 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1743 {
1744         MonoDomain *domain; 
1745         GSList *l = NULL, *tmp;
1746         MonoClass *startklass, *klass;
1747         MonoArray *res;
1748         MonoMethod *method;
1749         MonoObject *member;
1750         int i, len, match;
1751         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1752                 
1753         MONO_ARCH_SAVE_REGS;
1754
1755         domain = ((MonoObject *)type)->vtable->domain;
1756         klass = startklass = mono_class_from_mono_type (type->type);
1757         len = 0;
1758
1759 handle_parent:
1760         for (i = 0; i < klass->method.count; ++i) {
1761                 match = 0;
1762                 method = klass->methods [i];
1763                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1764                         continue;
1765                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1766                         if (bflags & BFLAGS_Public)
1767                                 match++;
1768                 } else {
1769                         if (bflags & BFLAGS_NonPublic)
1770                                 match++;
1771                 }
1772                 if (!match)
1773                         continue;
1774                 match = 0;
1775                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1776                         if (bflags & BFLAGS_Static)
1777                                 match++;
1778                 } else {
1779                         if (bflags & BFLAGS_Instance)
1780                                 match++;
1781                 }
1782
1783                 if (!match)
1784                         continue;
1785                 match = 0;
1786                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1787                         continue;
1788                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
1789                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1790                 
1791                 l = g_slist_prepend (l, member);
1792                 len++;
1793         }
1794         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1795                 goto handle_parent;
1796         res = mono_array_new (domain, mono_defaults.method_info_class, len);
1797         i = 0;
1798         tmp = l;
1799         for (; tmp; tmp = tmp->next, ++i)
1800                 mono_array_set (res, gpointer, i, tmp->data);
1801         g_slist_free (l);
1802         g_hash_table_destroy (method_slots);
1803         return res;
1804 }
1805
1806 static MonoArray*
1807 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1808 {
1809         MonoDomain *domain; 
1810         GSList *l = NULL, *tmp;
1811         static MonoClass *System_Reflection_ConstructorInfo;
1812         MonoClass *startklass, *klass;
1813         MonoArray *res;
1814         MonoMethod *method;
1815         MonoObject *member;
1816         int i, len, match;
1817
1818         MONO_ARCH_SAVE_REGS;
1819
1820         domain = ((MonoObject *)type)->vtable->domain;
1821         klass = startklass = mono_class_from_mono_type (type->type);
1822
1823         for (i = 0; i < klass->method.count; ++i) {
1824                 match = 0;
1825                 method = klass->methods [i];
1826                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1827                         continue;
1828                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1829                         if (bflags & BFLAGS_Public)
1830                                 match++;
1831                 } else {
1832                         if (bflags & BFLAGS_NonPublic)
1833                                 match++;
1834                 }
1835                 if (!match)
1836                         continue;
1837                 match = 0;
1838                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1839                         if (bflags & BFLAGS_Static)
1840                                 match++;
1841                 } else {
1842                         if (bflags & BFLAGS_Instance)
1843                                 match++;
1844                 }
1845
1846                 if (!match)
1847                         continue;
1848                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1849                         
1850                 l = g_slist_prepend (l, member);
1851         }
1852         len = g_slist_length (l);
1853         if (!System_Reflection_ConstructorInfo)
1854                 System_Reflection_ConstructorInfo = mono_class_from_name (
1855                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1856         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1857         i = 0;
1858         tmp = g_slist_reverse (l);
1859         for (; tmp; tmp = tmp->next, ++i)
1860                 mono_array_set (res, gpointer, i, tmp->data);
1861         g_slist_free (l);
1862         return res;
1863 }
1864
1865 static MonoArray*
1866 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1867 {
1868         MonoDomain *domain; 
1869         GSList *l = NULL, *tmp;
1870         static MonoClass *System_Reflection_PropertyInfo;
1871         MonoClass *startklass, *klass;
1872         MonoArray *res;
1873         MonoMethod *method;
1874         MonoProperty *prop;
1875         int i, match;
1876         int len = 0;
1877         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1878
1879         MONO_ARCH_SAVE_REGS;
1880
1881         domain = ((MonoObject *)type)->vtable->domain;
1882         klass = startklass = mono_class_from_mono_type (type->type);
1883
1884 handle_parent:
1885         for (i = 0; i < klass->property.count; ++i) {
1886                 prop = &klass->properties [i];
1887                 match = 0;
1888                 method = prop->get;
1889                 if (!method)
1890                         method = prop->set;
1891                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1892                         if (bflags & BFLAGS_Public)
1893                                 match++;
1894                 } else {
1895                         if (bflags & BFLAGS_NonPublic)
1896                                 match++;
1897                 }
1898                 if (!match)
1899                         continue;
1900                 match = 0;
1901                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1902                         if (bflags & BFLAGS_Static)
1903                                 match++;
1904                 } else {
1905                         if (bflags & BFLAGS_Instance)
1906                                 match++;
1907                 }
1908
1909                 if (!match)
1910                         continue;
1911                 match = 0;
1912
1913                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1914                         continue;
1915                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), prop);
1916
1917                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1918                 len++;
1919         }
1920         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1921                 goto handle_parent;
1922         if (!System_Reflection_PropertyInfo)
1923                 System_Reflection_PropertyInfo = mono_class_from_name (
1924                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1925         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1926         i = 0;
1927         tmp = l;
1928         for (; tmp; tmp = tmp->next, ++i)
1929                 mono_array_set (res, gpointer, i, tmp->data);
1930         g_slist_free (l);
1931         g_hash_table_destroy (method_slots);
1932         return res;
1933 }
1934
1935 static MonoReflectionEvent *
1936 ves_icall_MonoType_GetEvent (MonoReflectionType *type, MonoString *name, guint32 bflags)
1937 {
1938         MonoDomain *domain;
1939         MonoClass *klass;
1940         gint i;
1941         MonoEvent *event;
1942         MonoMethod *method;
1943         gchar *event_name;
1944
1945         MONO_ARCH_SAVE_REGS;
1946
1947         event_name = mono_string_to_utf8 (name);
1948         klass = mono_class_from_mono_type (type->type);
1949         domain = mono_object_domain (type);
1950
1951 handle_parent:  
1952         for (i = 0; i < klass->event.count; i++) {
1953                 event = &klass->events [i];
1954                 if (strcmp (event->name, event_name))
1955                         continue;
1956
1957                 method = event->add;
1958                 if (!method)
1959                         method = event->remove;
1960
1961                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1962                         if (!(bflags & BFLAGS_Public))
1963                                 continue;
1964                 } else {
1965                         if (!(bflags & BFLAGS_NonPublic))
1966                                 continue;
1967                 }
1968
1969                 g_free (event_name);
1970                 return mono_event_get_object (domain, klass, event);
1971         }
1972
1973         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1974                 goto handle_parent;
1975
1976         g_free (event_name);
1977         return NULL;
1978 }
1979
1980 static MonoArray*
1981 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
1982 {
1983         MonoDomain *domain; 
1984         GSList *l = NULL, *tmp;
1985         static MonoClass *System_Reflection_EventInfo;
1986         MonoClass *startklass, *klass;
1987         MonoArray *res;
1988         MonoMethod *method;
1989         MonoEvent *event;
1990         int i, len, match;
1991
1992         MONO_ARCH_SAVE_REGS;
1993
1994         domain = ((MonoObject *)type)->vtable->domain;
1995         klass = startklass = mono_class_from_mono_type (type->type);
1996
1997 handle_parent:  
1998         for (i = 0; i < klass->event.count; ++i) {
1999                 event = &klass->events [i];
2000                 match = 0;
2001                 method = event->add;
2002                 if (!method)
2003                         method = event->remove;
2004                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2005                         if (bflags & BFLAGS_Public)
2006                                 match++;
2007                 } else {
2008                         if (bflags & BFLAGS_NonPublic)
2009                                 match++;
2010                 }
2011                 if (!match)
2012                         continue;
2013                 match = 0;
2014                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2015                         if (bflags & BFLAGS_Static)
2016                                 match++;
2017                 } else {
2018                         if (bflags & BFLAGS_Instance)
2019                                 match++;
2020                 }
2021
2022                 if (!match)
2023                         continue;
2024                 match = 0;
2025                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
2026         }
2027         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2028                 goto handle_parent;
2029         len = g_slist_length (l);
2030         if (!System_Reflection_EventInfo)
2031                 System_Reflection_EventInfo = mono_class_from_name (
2032                         mono_defaults.corlib, "System.Reflection", "EventInfo");
2033         res = mono_array_new (domain, System_Reflection_EventInfo, len);
2034         i = 0;
2035         tmp = l;
2036         for (; tmp; tmp = tmp->next, ++i)
2037                 mono_array_set (res, gpointer, i, tmp->data);
2038         g_slist_free (l);
2039         return res;
2040 }
2041
2042 static MonoType *
2043 ves_icall_Type_GetNestedType (MonoReflectionType *type, MonoString *name, guint32 bflags)
2044 {
2045         MonoDomain *domain; 
2046         MonoClass *startklass, *klass;
2047         MonoType *res;
2048         MonoClass *nested;
2049         GList *tmpn;
2050         char *str;
2051         
2052         MONO_ARCH_SAVE_REGS;
2053
2054         domain = ((MonoObject *)type)->vtable->domain;
2055         klass = startklass = mono_class_from_mono_type (type->type);
2056         str = mono_string_to_utf8 (name);
2057
2058  handle_parent:
2059         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2060                 int match = 0;
2061                 nested = tmpn->data;
2062                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2063                         if (bflags & BFLAGS_Public)
2064                                 match++;
2065                 } else {
2066                         if (bflags & BFLAGS_NonPublic)
2067                                 match++;
2068                 }
2069                 if (!match)
2070                         continue;
2071                 if (strcmp (nested->name, str) == 0){
2072                         g_free (str);
2073                         return mono_type_get_object (domain, &nested->byval_arg);
2074                 }
2075         }
2076         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2077                 goto handle_parent;
2078         g_free (str);
2079         return NULL;
2080 }
2081
2082 static MonoArray*
2083 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
2084 {
2085         MonoDomain *domain; 
2086         GSList *l = NULL, *tmp;
2087         GList *tmpn;
2088         MonoClass *startklass, *klass;
2089         MonoArray *res;
2090         MonoObject *member;
2091         int i, len, match;
2092         MonoClass *nested;
2093
2094         MONO_ARCH_SAVE_REGS;
2095
2096         domain = ((MonoObject *)type)->vtable->domain;
2097         klass = startklass = mono_class_from_mono_type (type->type);
2098
2099         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2100                 match = 0;
2101                 nested = tmpn->data;
2102                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2103                         if (bflags & BFLAGS_Public)
2104                                 match++;
2105                 } else {
2106                         if (bflags & BFLAGS_NonPublic)
2107                                 match++;
2108                 }
2109                 if (!match)
2110                         continue;
2111                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
2112                 l = g_slist_prepend (l, member);
2113         }
2114         len = g_slist_length (l);
2115         res = mono_array_new (domain, mono_defaults.monotype_class, len);
2116         i = 0;
2117         tmp = g_slist_reverse (l);
2118         for (; tmp; tmp = tmp->next, ++i)
2119                 mono_array_set (res, gpointer, i, tmp->data);
2120         g_slist_free (l);
2121         return res;
2122 }
2123
2124 static MonoReflectionType*
2125 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
2126 {
2127         gchar *str;
2128         MonoType *type;
2129         MonoTypeNameParse info;
2130
2131         MONO_ARCH_SAVE_REGS;
2132
2133         str = mono_string_to_utf8 (name);
2134         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
2135         if (!mono_reflection_parse_type (str, &info)) {
2136                 g_free (str);
2137                 g_list_free (info.modifiers);
2138                 g_list_free (info.nested);
2139                 if (throwOnError) /* uhm: this is a parse error, though... */
2140                         mono_raise_exception (mono_get_exception_type_load ());
2141                 /*g_print ("failed parse\n");*/
2142                 return NULL;
2143         }
2144
2145         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
2146         g_free (str);
2147         g_list_free (info.modifiers);
2148         g_list_free (info.nested);
2149         if (!type) {
2150                 if (throwOnError)
2151                         mono_raise_exception (mono_get_exception_type_load ());
2152                 /* g_print ("failed find\n"); */
2153                 return NULL;
2154         }
2155         /* g_print ("got it\n"); */
2156         return mono_type_get_object (mono_object_domain (assembly), type);
2157
2158 }
2159
2160 static MonoString *
2161 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
2162 {
2163         MonoDomain *domain = mono_object_domain (assembly); 
2164         MonoAssembly *mass = assembly->assembly;
2165         MonoString *res;
2166         gchar *uri;
2167         gchar *absolute;
2168         
2169         MONO_ARCH_SAVE_REGS;
2170
2171         absolute = g_build_filename (mass->basedir, mass->image->module_name, NULL);
2172         uri = g_filename_to_uri (absolute, NULL, NULL);
2173         res = mono_string_new (domain, uri);
2174         g_free (uri);
2175         g_free (absolute);
2176         return res;
2177 }
2178
2179 static MonoString *
2180 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
2181 {
2182         MonoDomain *domain = mono_object_domain (assembly); 
2183         MonoString *res;
2184         char *name = g_build_filename (
2185                 assembly->assembly->basedir,
2186                 assembly->assembly->image->module_name, NULL);
2187
2188         MONO_ARCH_SAVE_REGS;
2189
2190         res = mono_string_new (domain, name);
2191         g_free (name);
2192         return res;
2193 }
2194
2195 static MonoReflectionMethod*
2196 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) 
2197 {
2198         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
2199
2200         MONO_ARCH_SAVE_REGS;
2201
2202         if (!token)
2203                 return NULL;
2204         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
2205 }
2206
2207 static MonoArray*
2208 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) 
2209 {
2210         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2211         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2212         int i;
2213         const char *val;
2214
2215         MONO_ARCH_SAVE_REGS;
2216
2217         for (i = 0; i < table->rows; ++i) {
2218                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
2219                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
2220         }
2221         return result;
2222 }
2223
2224 static MonoArray*
2225 ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAssembly *assembly) 
2226 {
2227         static MonoClass *System_Reflection_AssemblyName;
2228         MonoArray *result;
2229         MonoAssembly **ptr;
2230         MonoDomain *domain = mono_object_domain (assembly);
2231         int i, count = 0;
2232
2233         MONO_ARCH_SAVE_REGS;
2234
2235         if (!System_Reflection_AssemblyName)
2236                 System_Reflection_AssemblyName = mono_class_from_name (
2237                         mono_defaults.corlib, "System.Reflection", "AssemblyName");
2238
2239         for (ptr = assembly->assembly->image->references; ptr && *ptr; ptr++)
2240                 count++;
2241
2242         result = mono_array_new (mono_object_domain (assembly), System_Reflection_AssemblyName, count);
2243
2244         for (i = 0; i < count; i++) {
2245                 MonoAssembly *assem = assembly->assembly->image->references [i];
2246                 MonoReflectionAssemblyName *aname;
2247                 char *codebase;
2248
2249                 aname = (MonoReflectionAssemblyName *) mono_object_new (
2250                         domain, System_Reflection_AssemblyName);
2251
2252                 if (strcmp (assem->aname.name, "corlib") == 0)
2253                         aname->name = mono_string_new (domain, "mscorlib");
2254                 else
2255                         aname->name = mono_string_new (domain, assem->aname.name);
2256                 aname->major = assem->aname.major;
2257
2258                 codebase = g_filename_to_uri (assembly->assembly->image->references [i]->image->name, NULL, NULL);
2259                 aname->codebase = mono_string_new (domain, codebase);
2260                 g_free (codebase);
2261                 mono_array_set (result, gpointer, i, aname);
2262         }
2263         return result;
2264 }
2265
2266 typedef struct {
2267         MonoArray *res;
2268         int idx;
2269 } NameSpaceInfo;
2270
2271 static void
2272 foreach_namespace (const char* key, gconstpointer val, NameSpaceInfo *info)
2273 {
2274         MonoString *name = mono_string_new (mono_object_domain (info->res), key);
2275
2276         mono_array_set (info->res, gpointer, info->idx, name);
2277         info->idx++;
2278 }
2279
2280 static MonoArray*
2281 ves_icall_System_Reflection_Assembly_GetNamespaces (MonoReflectionAssembly *assembly) 
2282 {
2283         MonoImage *img = assembly->assembly->image;
2284         int n;
2285         MonoArray *res;
2286         NameSpaceInfo info;
2287         
2288         MONO_ARCH_SAVE_REGS;
2289
2290         n = g_hash_table_size (img->name_cache);
2291         res = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n);
2292         info.res = res;
2293         info.idx = 0;
2294         g_hash_table_foreach (img->name_cache, (GHFunc)foreach_namespace, &info);
2295         return res;
2296 }
2297
2298 /* move this in some file in mono/util/ */
2299 static char *
2300 g_concat_dir_and_file (const char *dir, const char *file)
2301 {
2302         g_return_val_if_fail (dir != NULL, NULL);
2303         g_return_val_if_fail (file != NULL, NULL);
2304
2305         /*
2306          * If the directory name doesn't have a / on the end, we need
2307          * to add one so we get a proper path to the file
2308          */
2309         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
2310                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
2311         else
2312                 return g_strconcat (dir, file, NULL);
2313 }
2314
2315 static MonoObject*
2316 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name) 
2317 {
2318         char *n = mono_string_to_utf8 (name);
2319         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2320         guint32 i;
2321         guint32 cols [MONO_MANIFEST_SIZE];
2322         const char *val;
2323         MonoObject *result;
2324
2325         MONO_ARCH_SAVE_REGS;
2326
2327         for (i = 0; i < table->rows; ++i) {
2328                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2329                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2330                 if (strcmp (val, n) == 0)
2331                         break;
2332         }
2333         g_free (n);
2334         if (i == table->rows)
2335                 return NULL;
2336         /* FIXME */
2337         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
2338                 guint32 size;
2339                 MonoArray *data;
2340                 val = mono_image_get_resource (assembly->assembly->image, cols [MONO_MANIFEST_OFFSET], &size);
2341                 if (!val)
2342                         return NULL;
2343                 data = mono_array_new (mono_object_domain (assembly), mono_defaults.byte_class, size);
2344                 memcpy (mono_array_addr (data, char, 0), val, size);
2345                 return (MonoObject*)data;
2346         }
2347         switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
2348         case IMPLEMENTATION_FILE:
2349                 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2350                 table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2351                 i = mono_metadata_decode_row_col (table, i - 1, MONO_FILE_NAME);
2352                 val = mono_metadata_string_heap (assembly->assembly->image, i);
2353                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2354                 result = (MonoObject*)mono_string_new (mono_object_domain (assembly), n);
2355                 /* check hash if needed */
2356                 g_free (n);
2357                 return result;
2358         case IMPLEMENTATION_ASSEMBLYREF:
2359         case IMPLEMENTATION_EXP_TYPE:
2360                 /* FIXME */
2361                 break;
2362         }
2363         return NULL;
2364 }
2365
2366 static MonoObject*
2367 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) 
2368 {
2369         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2370         MonoArray *result = NULL;
2371         int i;
2372         const char *val;
2373         char *n;
2374
2375         MONO_ARCH_SAVE_REGS;
2376
2377         /* check hash if needed */
2378         if (name) {
2379                 n = mono_string_to_utf8 (name);
2380                 for (i = 0; i < table->rows; ++i) {
2381                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2382                         if (strcmp (val, n) == 0) {
2383                                 MonoString *fn;
2384                                 g_free (n);
2385                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2386                                 fn = mono_string_new (mono_object_domain (assembly), n);
2387                                 g_free (n);
2388                                 return (MonoObject*)fn;
2389                         }
2390                 }
2391                 g_free (n);
2392                 return NULL;
2393         }
2394
2395         for (i = 0; i < table->rows; ++i) {
2396                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2397                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2398                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2399                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
2400                 g_free (n);
2401         }
2402         return (MonoObject*)result;
2403 }
2404
2405 static MonoReflectionMethod*
2406 ves_icall_GetCurrentMethod (void) 
2407 {
2408         MonoMethod *m = mono_method_get_last_managed ();
2409
2410         MONO_ARCH_SAVE_REGS;
2411
2412         return mono_method_get_object (mono_domain_get (), m, NULL);
2413 }
2414
2415 static MonoReflectionAssembly*
2416 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
2417 {
2418         MonoMethod *m = mono_method_get_last_managed ();
2419
2420         MONO_ARCH_SAVE_REGS;
2421
2422         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
2423 }
2424
2425
2426 static gboolean
2427 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
2428 {
2429         MonoMethod **dest = data;
2430
2431         /* skip unmanaged frames */
2432         if (!managed)
2433                 return FALSE;
2434
2435         if (m == *dest) {
2436                 *dest = NULL;
2437                 return FALSE;
2438         }
2439         if (!(*dest)) {
2440                 *dest = m;
2441                 return TRUE;
2442         }
2443         return FALSE;
2444 }
2445
2446 static MonoReflectionAssembly*
2447 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
2448 {
2449         MonoDomain* domain = mono_domain_get ();
2450
2451         MONO_ARCH_SAVE_REGS;
2452
2453         if (!domain->entry_assembly)
2454                 domain = mono_root_domain;
2455
2456         return mono_assembly_get_object (domain, domain->entry_assembly);
2457 }
2458
2459
2460 static MonoReflectionAssembly*
2461 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
2462 {
2463         MonoMethod *m = mono_method_get_last_managed ();
2464         MonoMethod *dest = m;
2465
2466         MONO_ARCH_SAVE_REGS;
2467
2468         mono_stack_walk (get_caller, &dest);
2469         if (!dest)
2470                 dest = m;
2471         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
2472 }
2473
2474 static MonoString *
2475 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
2476 {
2477         MonoDomain *domain = mono_object_domain (object); 
2478         MonoString *res;
2479         gchar *name;
2480
2481         MONO_ARCH_SAVE_REGS;
2482
2483         name = mono_type_get_name (object->type);
2484         res = mono_string_new (domain, name);
2485         g_free (name);
2486
2487         return res;
2488 }
2489
2490 static void
2491 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
2492 {
2493         MonoAssemblyName *name = &assembly->assembly->aname;
2494
2495         MONO_ARCH_SAVE_REGS;
2496
2497         if (strcmp (name->name, "corlib") == 0)
2498                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
2499         else
2500                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
2501
2502         aname->major = name->major;
2503         aname->minor = name->minor;
2504         aname->build = name->build;
2505         aname->revision = name->revision;
2506 }
2507
2508 static MonoArray*
2509 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
2510 {
2511         MonoDomain *domain = mono_object_domain (assembly); 
2512         MonoArray *res;
2513         MonoClass *klass;
2514         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
2515         int i, count;
2516         guint32 attrs, visibility;
2517
2518         MONO_ARCH_SAVE_REGS;
2519
2520         /* we start the count from 1 because we skip the special type <Module> */
2521         if (exportedOnly) {
2522                 count = 0;
2523                 for (i = 1; i < tdef->rows; ++i) {
2524                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2525                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2526                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
2527                                 count++;
2528                 }
2529         } else {
2530                 count = tdef->rows - 1;
2531         }
2532         res = mono_array_new (domain, mono_defaults.monotype_class, count);
2533         count = 0;
2534         for (i = 1; i < tdef->rows; ++i) {
2535                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2536                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2537                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
2538                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
2539                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
2540                         count++;
2541                 }
2542         }
2543         
2544         return res;
2545 }
2546
2547 static MonoReflectionType*
2548 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
2549 {
2550         MonoClass *klass;
2551         int isbyref = 0, rank;
2552         char *str = mono_string_to_utf8 (smodifiers);
2553         char *p;
2554
2555         MONO_ARCH_SAVE_REGS;
2556
2557         klass = mono_class_from_mono_type (tb->type.type);
2558         p = str;
2559         /* logic taken from mono_reflection_parse_type(): keep in sync */
2560         while (*p) {
2561                 switch (*p) {
2562                 case '&':
2563                         if (isbyref) { /* only one level allowed by the spec */
2564                                 g_free (str);
2565                                 return NULL;
2566                         }
2567                         isbyref = 1;
2568                         p++;
2569                         g_free (str);
2570                         return mono_type_get_object (mono_object_domain (tb), &klass->this_arg);
2571                         break;
2572                 case '*':
2573                         klass = mono_ptr_class_get (&klass->byval_arg);
2574                         mono_class_init (klass);
2575                         p++;
2576                         break;
2577                 case '[':
2578                         rank = 1;
2579                         p++;
2580                         while (*p) {
2581                                 if (*p == ']')
2582                                         break;
2583                                 if (*p == ',')
2584                                         rank++;
2585                                 else if (*p != '*') { /* '*' means unknown lower bound */
2586                                         g_free (str);
2587                                         return NULL;
2588                                 }
2589                                 ++p;
2590                         }
2591                         if (*p != ']') {
2592                                 g_free (str);
2593                                 return NULL;
2594                         }
2595                         p++;
2596                         klass = mono_array_class_get (&klass->byval_arg, rank);
2597                         mono_class_init (klass);
2598                         break;
2599                 default:
2600                         break;
2601                 }
2602         }
2603         g_free (str);
2604         return mono_type_get_object (mono_object_domain (tb), &klass->byval_arg);
2605 }
2606
2607 static MonoObject *
2608 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
2609                                                    MonoReflectionMethod *info)
2610 {
2611         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
2612         MonoObject *delegate;
2613         gpointer func;
2614
2615         MONO_ARCH_SAVE_REGS;
2616
2617         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
2618
2619         delegate = mono_object_new (mono_object_domain (type), delegate_class);
2620
2621         func = mono_compile_method (info->method);
2622
2623         mono_delegate_ctor (delegate, target, func);
2624
2625         return delegate;
2626 }
2627
2628 /*
2629  * Magic number to convert a time which is relative to
2630  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
2631  */
2632 #define EPOCH_ADJUST    ((gint64)62135596800L)
2633
2634 static gint64
2635 ves_icall_System_DateTime_GetNow (void)
2636 {
2637 #ifdef PLATFORM_WIN32
2638         SYSTEMTIME st;
2639         FILETIME ft;
2640         
2641         GetLocalTime (&st);
2642         SystemTimeToFileTime (&st, &ft);
2643         return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
2644 #else
2645         /* FIXME: put this in io-layer and call it GetLocalTime */
2646         struct timeval tv;
2647         gint64 res;
2648
2649         MONO_ARCH_SAVE_REGS;
2650
2651         if (gettimeofday (&tv, NULL) == 0) {
2652                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
2653                 return res;
2654         }
2655         /* fixme: raise exception */
2656         return 0;
2657 #endif
2658 }
2659
2660 /*
2661  * This is heavily based on zdump.c from glibc 2.2.
2662  *
2663  *  * data[0]:  start of daylight saving time (in DateTime ticks).
2664  *  * data[1]:  end of daylight saving time (in DateTime ticks).
2665  *  * data[2]:  utcoffset (in TimeSpan ticks).
2666  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
2667  *  * name[0]:  name of this timezone when not daylight saving.
2668  *  * name[1]:  name of this timezone when daylight saving.
2669  *
2670  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
2671  *         the class library allows years between 1 and 9999.
2672  *
2673  *  Returns true on success and zero on failure.
2674  */
2675 static guint32
2676 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
2677 {
2678 #ifndef PLATFORM_WIN32
2679         MonoDomain *domain = mono_domain_get ();
2680         struct tm start, tt;
2681         time_t t;
2682
2683         long int gmtoff;
2684         int is_daylight = 0, day;
2685         char tzone[10];
2686
2687         MONO_ARCH_SAVE_REGS;
2688
2689         if ((year < 1900) || (year > 2100))
2690                 mono_raise_exception (mono_get_exception_not_implemented ());
2691
2692         memset (&start, 0, sizeof (start));
2693
2694         start.tm_mday = 1;
2695         start.tm_year = year-1900;
2696
2697         t = mktime (&start);
2698 #if defined (HAVE_TIMEZONE)
2699 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
2700 #elif defined (HAVE_TM_GMTOFF)
2701 #define gmt_offset(x) x.tm_gmtoff
2702 #else
2703 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
2704 #endif
2705         
2706         gmtoff = gmt_offset (start);
2707
2708         MONO_CHECK_ARG_NULL (data);
2709         MONO_CHECK_ARG_NULL (names);
2710
2711         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2712         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2713
2714         /* For each day of the year, calculate the tm_gmtoff. */
2715         for (day = 0; day < 365; day++) {
2716
2717                 t += 3600*24;
2718                 tt = *localtime (&t);
2719
2720                 /* Daylight saving starts or ends here. */
2721                 if (gmt_offset (tt) != gmtoff) {
2722                         struct tm tt1;
2723                         time_t t1;
2724
2725                         /* Try to find the exact hour when daylight saving starts/ends. */
2726                         t1 = t;
2727                         do {
2728                                 t1 -= 3600;
2729                                 tt1 = *localtime (&t1);
2730                         } while (gmt_offset (tt1) != gmtoff);
2731
2732                         /* Try to find the exact minute when daylight saving starts/ends. */
2733                         do {
2734                                 t1 += 60;
2735                                 tt1 = *localtime (&t1);
2736                         } while (gmt_offset (tt1) == gmtoff);
2737                         
2738                         strftime (tzone, 10, "%Z", &tt);
2739                         
2740                         /* Write data, if we're already in daylight saving, we're done. */
2741                         if (is_daylight) {
2742                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2743                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2744                                 return 1;
2745                         } else {
2746                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2747                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2748                                 is_daylight = 1;
2749                         }
2750
2751                         /* This is only set once when we enter daylight saving. */
2752                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
2753                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
2754
2755                         gmtoff = gmt_offset (tt);
2756                 }
2757
2758                 gmtoff = gmt_offset (tt);
2759         }
2760
2761         if (!is_daylight) {
2762                 strftime (tzone, 10, "%Z", &tt);
2763                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2764                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2765                 mono_array_set ((*data), gint64, 0, 0);
2766                 mono_array_set ((*data), gint64, 1, 0);
2767                 mono_array_set ((*data), gint64, 2, (gint64) gmtoff * 10000000L);
2768                 mono_array_set ((*data), gint64, 3, 0);
2769         }
2770
2771         return 1;
2772 #else
2773         MonoDomain *domain = mono_domain_get ();
2774         TIME_ZONE_INFORMATION tz_info;
2775         FILETIME ft;
2776         int i;
2777
2778         GetTimeZoneInformation (&tz_info);
2779
2780         MONO_CHECK_ARG_NULL (data);
2781         MONO_CHECK_ARG_NULL (names);
2782
2783         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2784         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2785
2786         for (i = 0; i < 32; ++i)
2787                 if (!tz_info.DaylightName [i])
2788                         break;
2789         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
2790         for (i = 0; i < 32; ++i)
2791                 if (!tz_info.StandardName [i])
2792                         break;
2793         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
2794
2795         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
2796         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2797         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
2798         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2799         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
2800         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
2801
2802         return 1;
2803 #endif
2804 }
2805
2806 static gpointer
2807 ves_icall_System_Object_obj_address (MonoObject *this) 
2808 {
2809         MONO_ARCH_SAVE_REGS;
2810
2811         return this;
2812 }
2813
2814 /* System.Buffer */
2815
2816 static gint32 
2817 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) 
2818 {
2819         MonoClass *klass;
2820         MonoTypeEnum etype;
2821         int length, esize;
2822         int i;
2823
2824         MONO_ARCH_SAVE_REGS;
2825
2826         klass = array->obj.vtable->klass;
2827         etype = klass->element_class->byval_arg.type;
2828         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
2829                 return -1;
2830
2831         if (array->bounds == NULL)
2832                 length = array->max_length;
2833         else {
2834                 length = 1;
2835                 for (i = 0; i < klass->rank; ++ i)
2836                         length *= array->bounds [i].length;
2837         }
2838
2839         esize = mono_array_element_size (klass);
2840         return length * esize;
2841 }
2842
2843 static gint8 
2844 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) 
2845 {
2846         MONO_ARCH_SAVE_REGS;
2847
2848         return mono_array_get (array, gint8, idx);
2849 }
2850
2851 static void 
2852 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) 
2853 {
2854         MONO_ARCH_SAVE_REGS;
2855
2856         mono_array_set (array, gint8, idx, value);
2857 }
2858
2859 static void 
2860 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) 
2861 {
2862         char *src_buf, *dest_buf;
2863
2864         MONO_ARCH_SAVE_REGS;
2865
2866         src_buf = (gint8 *)src->vector + src_offset;
2867         dest_buf = (gint8 *)dest->vector + dest_offset;
2868
2869         memcpy (dest_buf, src_buf, count);
2870 }
2871
2872 static MonoObject *
2873 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
2874 {
2875         MonoDomain *domain = mono_object_domain (this); 
2876         MonoObject *res;
2877         MonoRealProxy *rp = ((MonoRealProxy *)this);
2878         MonoType *type;
2879         MonoClass *klass;
2880
2881         MONO_ARCH_SAVE_REGS;
2882
2883         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
2884         
2885         ((MonoTransparentProxy *)res)->rp = rp;
2886         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
2887         klass = mono_class_from_mono_type (type);
2888
2889         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2890                 ((MonoTransparentProxy *)res)->klass = mono_defaults.marshalbyrefobject_class;
2891         else
2892                 ((MonoTransparentProxy *)res)->klass = klass;
2893
2894         res->vtable = mono_class_proxy_vtable (domain, klass);
2895
2896         return res;
2897 }
2898
2899 /* System.Environment */
2900
2901 static MonoString *
2902 ves_icall_System_Environment_get_MachineName (void)
2903 {
2904 #if defined (PLATFORM_WIN32)
2905         gunichar2 *buf;
2906         guint32 len;
2907         MonoString *result;
2908
2909         len = MAX_COMPUTERNAME_LENGTH + 1;
2910         buf = g_new (gunichar2, len);
2911
2912         result = NULL;
2913         if (GetComputerName (buf, (PDWORD) &len))
2914                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
2915
2916         g_free (buf);
2917         return result;
2918 #else
2919         gchar *buf;
2920         int len;
2921         MonoString *result;
2922
2923         MONO_ARCH_SAVE_REGS;
2924
2925         len = 256;
2926         buf = g_new (gchar, len);
2927
2928         result = NULL;
2929         if (gethostname (buf, len) == 0)
2930                 result = mono_string_new (mono_domain_get (), buf);
2931         
2932         g_free (buf);
2933         return result;
2934 #endif
2935 }
2936
2937 static int
2938 ves_icall_System_Environment_get_Platform (void)
2939 {
2940         MONO_ARCH_SAVE_REGS;
2941
2942 #if defined (PLATFORM_WIN32)
2943         /* Win32NT */
2944         return 2;
2945 #else
2946         /* Unix */
2947         return 128;
2948 #endif
2949 }
2950
2951 static MonoString *
2952 ves_icall_System_Environment_get_NewLine (void)
2953 {
2954         MONO_ARCH_SAVE_REGS;
2955
2956 #if defined (PLATFORM_WIN32)
2957         return mono_string_new (mono_domain_get (), "\r\n");
2958 #else
2959         return mono_string_new (mono_domain_get (), "\n");
2960 #endif
2961 }
2962
2963 static MonoString *
2964 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
2965 {
2966         const gchar *value;
2967         gchar *utf8_name;
2968
2969         MONO_ARCH_SAVE_REGS;
2970
2971         if (name == NULL)
2972                 return NULL;
2973
2974         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
2975         value = g_getenv (utf8_name);
2976         g_free (utf8_name);
2977
2978         if (value == 0)
2979                 return NULL;
2980         
2981         return mono_string_new (mono_domain_get (), value);
2982 }
2983
2984 /*
2985  * There is no standard way to get at environ.
2986  */
2987 extern char **environ;
2988
2989 static MonoArray *
2990 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
2991 {
2992         MonoArray *names;
2993         MonoDomain *domain;
2994         MonoString *str;
2995         gchar **e, **parts;
2996         int n;
2997
2998         MONO_ARCH_SAVE_REGS;
2999
3000         n = 0;
3001         for (e = environ; *e != 0; ++ e)
3002                 ++ n;
3003
3004         domain = mono_domain_get ();
3005         names = mono_array_new (domain, mono_defaults.string_class, n);
3006
3007         n = 0;
3008         for (e = environ; *e != 0; ++ e) {
3009                 parts = g_strsplit (*e, "=", 2);
3010                 if (*parts != 0) {
3011                         str = mono_string_new (domain, *parts);
3012                         mono_array_set (names, MonoString *, n, str);
3013                 }
3014
3015                 g_strfreev (parts);
3016
3017                 ++ n;
3018         }
3019
3020         return names;
3021 }
3022
3023 /*
3024  * Returns the number of milliseconds elapsed since the system started.
3025  */
3026 static gint32
3027 ves_icall_System_Environment_get_TickCount (void)
3028 {
3029 #if defined (PLATFORM_WIN32)
3030         return GetTickCount();
3031 #else
3032         struct timeval tv;
3033         struct timezone tz;
3034         gint32 res;
3035
3036         MONO_ARCH_SAVE_REGS;
3037
3038         res = (gint32) gettimeofday (&tv, &tz);
3039
3040         if (res != -1)
3041                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
3042         return res;
3043 #endif
3044 }
3045
3046
3047 static void
3048 ves_icall_System_Environment_Exit (int result)
3049 {
3050         MONO_ARCH_SAVE_REGS;
3051
3052         mono_profiler_shutdown ();
3053
3054         /* we may need to do some cleanup here... */
3055         exit (result);
3056 }
3057
3058 static MonoString*
3059 ves_icall_System_Text_Encoding_InternalCodePage (void) 
3060 {
3061         const char *cset;
3062
3063         MONO_ARCH_SAVE_REGS;
3064
3065         g_get_charset (&cset);
3066         /* g_print ("charset: %s\n", cset); */
3067         /* handle some common aliases */
3068         switch (*cset) {
3069         case 'A':
3070                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
3071                         cset = "us-ascii";
3072                 break;
3073         }
3074         return mono_string_new (mono_domain_get (), cset);
3075 }
3076
3077 static void
3078 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
3079                                          MonoReflectionMethod *method,
3080                                          MonoArray *out_args)
3081 {
3082         MONO_ARCH_SAVE_REGS;
3083
3084         mono_message_init (mono_object_domain (this), this, method, out_args);
3085 }
3086
3087 static MonoBoolean
3088 ves_icall_IsTransparentProxy (MonoObject *proxy)
3089 {
3090         MONO_ARCH_SAVE_REGS;
3091
3092         if (!proxy)
3093                 return 0;
3094
3095         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
3096                 return 1;
3097
3098         return 0;
3099 }
3100
3101 static void
3102 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
3103 {
3104         MonoClass *klass;
3105         MonoVTable* vtable;
3106
3107         MONO_ARCH_SAVE_REGS;
3108
3109         klass = mono_class_from_mono_type (type->type);
3110         vtable = mono_class_vtable (mono_domain_get (), klass);
3111
3112         if (enable) vtable->remote = 1;
3113         else vtable->remote = 0;
3114 }
3115
3116 static MonoObject *
3117 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionType *type)
3118 {
3119         MonoClass *klass;
3120         MonoDomain *domain;
3121         
3122         MONO_ARCH_SAVE_REGS;
3123
3124         domain = mono_object_domain (type);
3125         klass = mono_class_from_mono_type (type->type);
3126
3127         // Bypass remoting object creation check
3128         return mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
3129 }
3130
3131 static MonoObject *
3132 ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal (MonoReflectionType *type)
3133 {
3134         MonoClass *klass;
3135         MonoObject *obj;
3136         MonoDomain *domain;
3137         
3138         MONO_ARCH_SAVE_REGS;
3139
3140         domain = mono_object_domain (type);
3141         klass = mono_class_from_mono_type (type->type);
3142
3143         if (klass->rank >= 1) {
3144                 g_assert (klass->rank == 1);
3145                 obj = (MonoObject *) mono_array_new (domain, klass->element_class, 0);
3146         } else {
3147                 obj = mono_object_new (domain, klass);
3148         }
3149
3150         return obj;
3151 }
3152
3153 static MonoString *
3154 ves_icall_System_IO_get_temp_path (void)
3155 {
3156         MONO_ARCH_SAVE_REGS;
3157
3158         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
3159 }
3160
3161 static gpointer
3162 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
3163 {
3164         MONO_ARCH_SAVE_REGS;
3165
3166         return mono_compile_method (method);
3167 }
3168
3169 char const * mono_cfg_dir = "";
3170
3171 void    
3172 mono_install_get_config_dir (void)
3173 {
3174 #ifdef PLATFORM_WIN32
3175   int i;
3176 #endif
3177
3178   mono_cfg_dir = getenv ("MONO_CFG_DIR");
3179
3180   if (!mono_cfg_dir) {
3181 #ifndef PLATFORM_WIN32
3182     mono_cfg_dir = MONO_CFG_DIR;
3183 #else
3184     mono_cfg_dir = g_strdup (MONO_CFG_DIR);
3185     for (i = strlen (mono_cfg_dir) - 1; i >= 0; i--) {
3186         if (mono_cfg_dir [i] == '/')
3187             mono_cfg_dir [i] = '\\';
3188     }
3189 #endif
3190   }
3191 }
3192
3193
3194 static MonoString *
3195 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
3196 {
3197         static MonoString *mcpath;
3198         gchar *path;
3199
3200         MONO_ARCH_SAVE_REGS;
3201
3202         if (mcpath != NULL)
3203                 return mcpath;
3204
3205         path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", "machine.config", NULL);
3206
3207 #if defined (PLATFORM_WIN32)
3208         /* Avoid mixing '/' and '\\' */
3209         {
3210                 gint i;
3211                 for (i = strlen (path) - 1; i >= 0; i--)
3212                         if (path [i] == '/')
3213                                 path [i] = '\\';
3214         }
3215 #endif
3216         mcpath = mono_string_new (mono_domain_get (), path);
3217         g_free (path);
3218
3219         return mcpath;
3220 }
3221
3222 static void
3223 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (MonoString *message)
3224 {
3225 #if defined (PLATFORM_WIN32)
3226         static void (*output_debug) (gchar *);
3227         static gboolean tried_loading = FALSE;
3228         gchar *str;
3229
3230         MONO_ARCH_SAVE_REGS;
3231
3232         if (!tried_loading && output_debug == NULL) {
3233                 GModule *k32;
3234
3235                 tried_loading = TRUE;
3236                 k32 = g_module_open ("kernel32", G_MODULE_BIND_LAZY);
3237                 if (!k32) {
3238                         gchar *error = g_strdup (g_module_error ());
3239                         g_warning ("Failed to load kernel32.dll: %s\n", error);
3240                         g_free (error);
3241                         return;
3242                 }
3243
3244                 g_module_symbol (k32, "OutputDebugStringW", (gpointer *) &output_debug);
3245                 if (!output_debug) {
3246                         gchar *error = g_strdup (g_module_error ());
3247                         g_warning ("Failed to load OutputDebugStringW: %s\n", error);
3248                         g_free (error);
3249                         return;
3250                 }
3251         }
3252
3253         if (output_debug == NULL)
3254                 return;
3255         
3256         str = mono_string_to_utf8 (message);
3257         output_debug (str);
3258         g_free (str);
3259 #else
3260         g_warning ("WriteWindowsDebugString called and PLATFORM_WIN32 not defined!\n");
3261 #endif
3262 }
3263
3264 /* Only used for value types */
3265 static MonoObject *
3266 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionType *type)
3267 {
3268         MonoClass *klass;
3269         MonoDomain *domain;
3270         
3271         MONO_ARCH_SAVE_REGS;
3272
3273         domain = mono_object_domain (type);
3274         klass = mono_class_from_mono_type (type->type);
3275
3276         return mono_object_new (domain, klass);
3277 }
3278
3279 static MonoReflectionMethod *
3280 ves_icall_MonoMethod_get_base_definition (MonoReflectionMethod *m)
3281 {
3282         MonoClass *klass;
3283         MonoMethod *method = m->method;
3284         MonoMethod *result = NULL;
3285
3286         MONO_ARCH_SAVE_REGS;
3287
3288         if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
3289              method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ||
3290              method->flags & METHOD_ATTRIBUTE_NEW_SLOT)
3291                 return m;
3292
3293         if (method->klass == NULL || (klass = method->klass->parent) == NULL)
3294                 return m;
3295
3296         if (klass->vtable_size > method->slot)
3297                 result = klass->vtable [method->slot];
3298
3299         if (result == NULL)
3300                 return m;
3301
3302         return mono_method_get_object (mono_domain_get (), result, NULL);
3303 }
3304
3305 /* icall map */
3306
3307 static gconstpointer icall_map [] = {
3308         /*
3309          * System.Array
3310          */
3311         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
3312         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
3313         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
3314         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
3315         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
3316         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
3317         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
3318         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
3319         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
3320         "System.Array::Clone",            mono_array_clone,
3321
3322         /*
3323          * System.Object
3324          */
3325         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
3326         "System.Object::GetType", ves_icall_System_Object_GetType,
3327         "System.Object::InternalGetHashCode", ves_icall_System_Object_GetHashCode,
3328         "System.Object::obj_address", ves_icall_System_Object_obj_address,
3329
3330         /*
3331          * System.ValueType
3332          */
3333         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
3334         "System.ValueType::InternalEquals", ves_icall_System_ValueType_Equals,
3335
3336         /*
3337          * System.String
3338          */
3339         
3340         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
3341         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
3342         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
3343         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
3344         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
3345         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
3346         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
3347         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
3348         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
3349         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
3350         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
3351         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
3352         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
3353         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
3354         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
3355         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
3356         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
3357         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
3358         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
3359         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
3360         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
3361         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
3362         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
3363         "System.String::InternalPad", ves_icall_System_String_InternalPad,
3364         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
3365         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
3366         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
3367         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
3368         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
3369         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
3370         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
3371         "System.String::InternalCompare(string,int,string,int,int,int)", ves_icall_System_String_InternalCompareStr_N,
3372         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
3373         "System.String::get_Chars", ves_icall_System_String_get_Chars,
3374
3375         /*
3376          * System.AppDomain
3377          */
3378         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
3379         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
3380         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
3381         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
3382         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
3383         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
3384         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
3385         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
3386         "System.AppDomain::InternalUnload", ves_icall_System_AppDomain_InternalUnload,
3387         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
3388         "System.AppDomain::InternalSetDomain", ves_icall_System_AppDomain_InternalSetDomain,
3389         "System.AppDomain::InternalSetDomainByID", ves_icall_System_AppDomain_InternalSetDomainByID,
3390         "System.AppDomain::InternalSetContext", ves_icall_System_AppDomain_InternalSetContext,
3391         "System.AppDomain::InternalGetContext", ves_icall_System_AppDomain_InternalGetContext,
3392         "System.AppDomain::InternalGetDefaultContext", ves_icall_System_AppDomain_InternalGetDefaultContext,
3393         "System.AppDomain::InternalGetProcessGuid", ves_icall_System_AppDomain_InternalGetProcessGuid,
3394
3395         /*
3396          * System.AppDomainSetup
3397          */
3398         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
3399
3400         /*
3401          * System.Double
3402          */
3403         "System.Double::ParseImpl",    mono_double_ParseImpl,
3404
3405         /*
3406          * System.Decimal
3407          */
3408         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
3409         "System.Decimal::decimal2Int64", mono_decimal2Int64,
3410         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
3411         "System.Decimal::decimalIncr", mono_decimalIncr,
3412         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
3413         "System.Decimal::decimal2double", mono_decimal2double,
3414         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
3415         "System.Decimal::decimalRound", mono_decimalRound,
3416         "System.Decimal::decimalMult", mono_decimalMult,
3417         "System.Decimal::decimalDiv", mono_decimalDiv,
3418         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
3419         "System.Decimal::decimalCompare", mono_decimalCompare,
3420         "System.Decimal::string2decimal", mono_string2decimal,
3421         "System.Decimal::decimal2string", mono_decimal2string,
3422
3423         /*
3424          * ModuleBuilder
3425          */
3426         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
3427         "System.Reflection.Emit.ModuleBuilder::basic_init", mono_image_module_basic_init,
3428         
3429         /*
3430          * AssemblyBuilder
3431          */
3432         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
3433         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
3434         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
3435         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
3436         "System.Reflection.Emit.AssemblyBuilder::build_metadata", ves_icall_AssemblyBuilder_build_metadata,
3437
3438         /*
3439          * Reflection stuff.
3440          */
3441         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
3442         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
3443         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
3444         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
3445         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
3446         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
3447         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
3448         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
3449         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
3450         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
3451         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
3452         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
3453         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
3454         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
3455
3456         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
3457         "System.Reflection.MonoMethod::get_base_definition", ves_icall_MonoMethod_get_base_definition,
3458         
3459         /* System.Enum */
3460
3461         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
3462         "System.Enum::get_value", ves_icall_System_Enum_get_value,
3463         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
3464
3465         /*
3466          * TypeBuilder
3467          */
3468         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
3469         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
3470         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
3471         
3472         /*
3473          * MethodBuilder
3474          */
3475         
3476         /*
3477          * System.Type
3478          */
3479         "System.Type::internal_from_name", ves_icall_type_from_name,
3480         "System.Type::internal_from_handle", ves_icall_type_from_handle,
3481         "System.MonoType::get_attributes", ves_icall_get_attributes,
3482         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
3483         "System.Type::Equals", ves_icall_type_Equals,
3484         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
3485         "System.Type::GetInterfaceMapData", ves_icall_Type_GetInterfaceMapData,
3486
3487         /*
3488          * System.Reflection.FieldInfo
3489          */
3490         "System.Reflection.FieldInfo::internal_from_handle", ves_icall_System_Reflection_FieldInfo_internal_from_handle,
3491
3492         /*
3493          * System.Runtime.CompilerServices.RuntimeHelpers
3494          */
3495         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
3496         "System.Runtime.CompilerServices.RuntimeHelpers::GetOffsetToStringData", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData,
3497         "System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue,
3498         "System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor,
3499         
3500         /*
3501          * System.Threading
3502          */
3503         "System.Threading.Thread::Abort_internal(object)", ves_icall_System_Threading_Thread_Abort,
3504         "System.Threading.Thread::ResetAbort_internal()", ves_icall_System_Threading_Thread_ResetAbort,
3505         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
3506         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
3507         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
3508         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
3509         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
3510         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
3511         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
3512         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
3513         "System.Threading.Thread::GetDomainID", ves_icall_System_Threading_Thread_GetDomainID,
3514         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
3515         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
3516         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
3517         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
3518         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
3519         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
3520         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
3521         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
3522         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
3523         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
3524         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
3525         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
3526
3527         /*
3528          * System.Threading.WaitHandle
3529          */
3530         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
3531         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
3532         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
3533
3534         /*
3535          * System.Runtime.InteropServices.Marshal
3536          */
3537         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
3538         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
3539         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
3540         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
3541         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
3542         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
3543         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
3544         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
3545         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
3546         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
3547
3548         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
3549         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
3550         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
3551         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
3552         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
3553         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
3554         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
3555
3556         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
3557         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
3558         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
3559         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
3560         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
3561         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
3562         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
3563         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
3564         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
3565         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
3566         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
3567         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
3568         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
3569         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
3570         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
3571
3572
3573         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
3574         "System.Reflection.Assembly::InternalGetType", ves_icall_System_Reflection_Assembly_InternalGetType,
3575         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
3576         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
3577         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
3578         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
3579         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
3580         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
3581         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
3582         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
3583         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
3584         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
3585         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
3586         "System.Reflection.Assembly::GetReferencedAssemblies", ves_icall_System_Reflection_Assembly_GetReferencedAssemblies,
3587         "System.Reflection.Assembly::GetNamespaces", ves_icall_System_Reflection_Assembly_GetNamespaces,
3588
3589         /*
3590          * System.MonoType.
3591          */
3592         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
3593         "System.MonoType::type_from_obj", mono_type_type_from_obj,
3594         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
3595         "System.MonoType::get_type_info", ves_icall_get_type_info,
3596         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
3597         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
3598         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
3599         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
3600         "System.MonoType::GetField", ves_icall_Type_GetField,
3601         "System.MonoType::GetFields", ves_icall_Type_GetFields,
3602         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
3603         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
3604         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
3605         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
3606         "System.MonoType::InternalGetEvent", ves_icall_MonoType_GetEvent,
3607         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
3608         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
3609         "System.MonoType::GetNestedType", ves_icall_Type_GetNestedType,
3610
3611         /*
3612          * System.Net.Sockets I/O Services
3613          */
3614         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
3615         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
3616         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
3617         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
3618         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
3619         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
3620         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
3621         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
3622         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
3623         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
3624         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
3625         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
3626         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
3627         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
3628         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
3629         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
3630         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
3631         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
3632         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
3633         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
3634         "System.Net.Dns::GetHostByName_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByName_internal,
3635         "System.Net.Dns::GetHostByAddr_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByAddr_internal,
3636         "System.Net.Dns::GetHostName_internal(string&)", ves_icall_System_Net_Dns_GetHostName_internal,
3637
3638         /*
3639          * System.Char
3640          */
3641         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
3642         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
3643         "System.Char::IsControl", ves_icall_System_Char_IsControl,
3644         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
3645         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
3646         "System.Char::IsLower", ves_icall_System_Char_IsLower,
3647         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
3648         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
3649         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
3650         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
3651         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
3652         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
3653         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
3654         "System.Char::ToLower", ves_icall_System_Char_ToLower,
3655         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
3656
3657         /*
3658          * System.Text.Encoding
3659          */
3660         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
3661
3662         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
3663         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
3664
3665         /*
3666          * System.GC
3667          */
3668         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
3669         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
3670         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
3671         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
3672         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
3673         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
3674         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
3675         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
3676         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
3677         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
3678
3679         /*
3680          * System.Security.Cryptography calls
3681          */
3682
3683          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes,
3684          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes,
3685         
3686         /*
3687          * System.Buffer
3688          */
3689         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
3690         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
3691         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
3692         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
3693
3694         /*
3695          * System.IO.MonoIO
3696          */
3697         "System.IO.MonoIO::CreateDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CreateDirectory,
3698         "System.IO.MonoIO::RemoveDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_RemoveDirectory,
3699         "System.IO.MonoIO::FindFirstFile(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindFirstFile,
3700         "System.IO.MonoIO::FindNextFile(intptr,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindNextFile,
3701         "System.IO.MonoIO::FindClose(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindClose,
3702         "System.IO.MonoIO::GetCurrentDirectory(System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
3703         "System.IO.MonoIO::SetCurrentDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
3704         "System.IO.MonoIO::MoveFile(string,string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_MoveFile,
3705         "System.IO.MonoIO::CopyFile(string,string,bool,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CopyFile,
3706         "System.IO.MonoIO::DeleteFile(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_DeleteFile,
3707         "System.IO.MonoIO::GetFileAttributes(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileAttributes,
3708         "System.IO.MonoIO::SetFileAttributes(string,System.IO.FileAttributes,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileAttributes,
3709         "System.IO.MonoIO::GetFileType(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileType,
3710         "System.IO.MonoIO::GetFileStat(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileStat,
3711         "System.IO.MonoIO::Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open,
3712         "System.IO.MonoIO::Close(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Close,
3713         "System.IO.MonoIO::Read(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Read,
3714         "System.IO.MonoIO::Write(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Write,
3715         "System.IO.MonoIO::Seek(intptr,long,System.IO.SeekOrigin,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Seek,
3716         "System.IO.MonoIO::GetLength(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetLength,
3717         "System.IO.MonoIO::SetLength(intptr,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetLength,
3718         "System.IO.MonoIO::SetFileTime(intptr,long,long,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileTime,
3719         "System.IO.MonoIO::Flush(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Flush,
3720         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
3721         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
3722         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
3723         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
3724         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
3725         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
3726         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
3727         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
3728         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
3729
3730         /*
3731          * System.Math
3732          */
3733         "System.Math::Floor", ves_icall_System_Math_Floor,
3734         "System.Math::Round", ves_icall_System_Math_Round,
3735         "System.Math::Round2", ves_icall_System_Math_Round2,
3736         "System.Math::Sin", ves_icall_System_Math_Sin,
3737         "System.Math::Cos", ves_icall_System_Math_Cos,
3738         "System.Math::Tan", ves_icall_System_Math_Tan,
3739         "System.Math::Sinh", ves_icall_System_Math_Sinh,
3740         "System.Math::Cosh", ves_icall_System_Math_Cosh,
3741         "System.Math::Tanh", ves_icall_System_Math_Tanh,
3742         "System.Math::Acos", ves_icall_System_Math_Acos,
3743         "System.Math::Asin", ves_icall_System_Math_Asin,
3744         "System.Math::Atan", ves_icall_System_Math_Atan,
3745         "System.Math::Atan2", ves_icall_System_Math_Atan2,
3746         "System.Math::Exp", ves_icall_System_Math_Exp,
3747         "System.Math::Log", ves_icall_System_Math_Log,
3748         "System.Math::Log10", ves_icall_System_Math_Log10,
3749         "System.Math::Pow", ves_icall_System_Math_Pow,
3750         "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
3751
3752         /*
3753          * System.Environment
3754          */
3755         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
3756         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
3757         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
3758         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
3759         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
3760         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
3761         "System.Environment::Exit", ves_icall_System_Environment_Exit,
3762         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
3763         "System.Environment::get_ExitCode", mono_environment_exitcode_get,
3764         "System.Environment::set_ExitCode", mono_environment_exitcode_set,
3765
3766         /*
3767          * System.Runtime.Remoting
3768          */     
3769         "System.Runtime.Remoting.RemotingServices::InternalExecute",
3770         ves_icall_InternalExecute,
3771         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
3772         ves_icall_IsTransparentProxy,
3773
3774         /*
3775          * System.Runtime.Remoting.Activation
3776          */     
3777         "System.Runtime.Remoting.Activation.ActivationServices::AllocateUninitializedClassInstance",
3778         ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance,
3779         "System.Runtime.Remoting.Activation.ActivationServices::EnableProxyActivation",
3780         ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation,
3781
3782         /*
3783          * System.Runtime.Remoting.Messaging
3784          */     
3785         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
3786         ves_icall_MonoMethodMessage_InitMessage,
3787         
3788         /*
3789          * System.Runtime.Remoting.Proxies
3790          */     
3791         "System.Runtime.Remoting.Proxies.RealProxy::InternalGetTransparentProxy", 
3792         ves_icall_Remoting_RealProxy_GetTransparentProxy,
3793
3794         /*
3795          * System.Threading.Interlocked
3796          */
3797         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
3798         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
3799         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
3800         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
3801         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
3802         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
3803         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
3804         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
3805         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
3806         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
3807
3808         /*
3809          * System.Diagnostics.Process
3810          */
3811         "System.Diagnostics.Process::GetProcess_internal(int)", ves_icall_System_Diagnostics_Process_GetProcess_internal,
3812         "System.Diagnostics.Process::GetProcesses_internal()", ves_icall_System_Diagnostics_Process_GetProcesses_internal,
3813         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
3814         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
3815         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
3816         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
3817         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
3818         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
3819         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
3820         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
3821         "System.Diagnostics.Process::ProcessName_internal(intptr)", ves_icall_System_Diagnostics_Process_ProcessName_internal,
3822         "System.Diagnostics.Process::GetWorkingSet_internal(intptr,int&,int&)", ves_icall_System_Diagnostics_Process_GetWorkingSet_internal,
3823         "System.Diagnostics.Process::SetWorkingSet_internal(intptr,int,int,bool)", ves_icall_System_Diagnostics_Process_SetWorkingSet_internal,
3824         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
3825
3826         /* 
3827          * System.Delegate
3828          */
3829         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
3830
3831         /* 
3832          * System.Runtime.Serialization
3833          */
3834         "System.Runtime.Serialization.FormatterServices::GetUninitializedObjectInternal",
3835         ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal,
3836
3837         /*
3838          * System.IO.Path
3839          */
3840         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
3841
3842         /*
3843          * Private icalls for the Mono Debugger
3844          */
3845         "System.Reflection.Assembly::MonoDebugger_GetMethod",
3846         ves_icall_MonoDebugger_GetMethod,
3847
3848         "System.Reflection.Assembly::MonoDebugger_GetMethodToken",
3849         ves_icall_MonoDebugger_GetMethodToken,
3850
3851         "System.Reflection.Assembly::MonoDebugger_GetLocalTypeFromSignature",
3852         ves_icall_MonoDebugger_GetLocalTypeFromSignature,
3853
3854         "System.Reflection.Assembly::MonoDebugger_GetType",
3855         ves_icall_MonoDebugger_GetType,
3856
3857         /*
3858          * System.Configuration
3859          */
3860         "System.Configuration.DefaultConfig::get_machine_config_path",
3861         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
3862
3863         /*
3864          * System.Diagnostics.DefaultTraceListener
3865          */
3866         "System.Diagnostics.DefaultTraceListener::WriteWindowsDebugString",
3867         ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString,
3868         /*
3869          * System.Activator
3870          */
3871         "System.Activator::CreateInstanceInternal",
3872         ves_icall_System_Activator_CreateInstanceInternal,
3873
3874         /*
3875          * add other internal calls here
3876          */
3877         NULL, NULL
3878 };
3879
3880 void
3881 mono_init_icall (void)
3882 {
3883         const char *name;
3884         int i = 0;
3885
3886         while ((name = icall_map [i])) {
3887                 mono_add_internal_call (name, icall_map [i+1]);
3888                 i += 2;
3889         }
3890        
3891 }
3892
3893