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