2004-02-08 Martin Baulig <martin@ximian.com>
[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 MonoReflectionType*
1666 ves_icall_Type_BindGenericParameters (MonoReflectionType *type, MonoArray *types)
1667 {
1668         MonoType *geninst;
1669
1670         MONO_ARCH_SAVE_REGS;
1671
1672         if (type->type->byref)
1673                 return NULL;
1674
1675         geninst = mono_reflection_bind_generic_parameters (type->type, types);
1676
1677         return mono_type_get_object (mono_object_domain (type), geninst);
1678 }
1679
1680 static gboolean
1681 ves_icall_Type_get_IsGenericInstance (MonoReflectionType *type)
1682 {
1683         MonoClass *klass;
1684         MONO_ARCH_SAVE_REGS;
1685
1686         if (type->type->byref)
1687                 return FALSE;
1688         klass = mono_class_from_mono_type (type->type);
1689         return klass->generic_inst != NULL;
1690 }
1691
1692 static gint32
1693 ves_icall_Type_GetGenericParameterPosition (MonoReflectionType *type)
1694 {
1695         MONO_ARCH_SAVE_REGS;
1696
1697         if (type->type->byref)
1698                 return -1;
1699         if (type->type->type == MONO_TYPE_VAR || type->type->type == MONO_TYPE_MVAR)
1700                 return type->type->data.generic_param->num;
1701         return -1;
1702 }
1703
1704 static MonoBoolean
1705 ves_icall_MonoType_get_HasGenericArguments (MonoReflectionType *type)
1706 {
1707         MonoClass *klass;
1708         MONO_ARCH_SAVE_REGS;
1709
1710         if (type->type->byref)
1711                 return FALSE;
1712         klass = mono_class_from_mono_type (type->type);
1713         if (klass->gen_params || klass->generic_inst)
1714                 return TRUE;
1715         return FALSE;
1716 }
1717
1718 static MonoBoolean
1719 ves_icall_MonoType_get_IsGenericParameter (MonoReflectionType *type)
1720 {
1721         MONO_ARCH_SAVE_REGS;
1722
1723         if (type->type->byref)
1724                 return FALSE;
1725         if (type->type->type == MONO_TYPE_VAR || type->type->type == MONO_TYPE_MVAR)
1726                 return TRUE;
1727         return FALSE;
1728 }
1729
1730 static MonoBoolean
1731 ves_icall_TypeBuilder_get_IsGenericParameter (MonoReflectionTypeBuilder *tb)
1732 {
1733         MONO_ARCH_SAVE_REGS;
1734
1735         if (tb->type.type->byref)
1736                 return FALSE;
1737         if (tb->type.type->type == MONO_TYPE_VAR || tb->type.type->type == MONO_TYPE_MVAR)
1738                 return TRUE;
1739         return FALSE;
1740 }
1741
1742 static MonoReflectionGenericParam*
1743 ves_icall_TypeBuilder_define_generic_parameter (MonoReflectionTypeBuilder *tb, MonoString *name, int index)
1744 {
1745         MONO_ARCH_SAVE_REGS;
1746
1747         return mono_reflection_define_generic_parameter (tb, NULL, name, index);
1748 }
1749
1750 static MonoReflectionGenericParam*
1751 ves_icall_MethodBuilder_define_generic_parameter (MonoReflectionMethodBuilder *mb, MonoString *name, int index)
1752 {
1753         MONO_ARCH_SAVE_REGS;
1754
1755         return mono_reflection_define_generic_parameter (NULL, mb, name, index);
1756 }
1757
1758 static MonoReflectionType*
1759 ves_icall_MonoGenericInst_GetParentType (MonoReflectionGenericInst *type)
1760 {
1761         MonoGenericInst *ginst;
1762         MonoClass *klass;
1763
1764         MONO_ARCH_SAVE_REGS;
1765
1766         ginst = type->type.type->data.generic_inst;
1767         if (!ginst || !ginst->parent || (ginst->parent->type != MONO_TYPE_GENERICINST))
1768                 return NULL;
1769
1770         klass = mono_class_from_mono_type (ginst->parent);
1771         if (!klass->generic_inst && !klass->gen_params)
1772                 return NULL;
1773
1774         return mono_type_get_object (mono_object_domain (type), ginst->parent);
1775 }
1776
1777 static MonoArray*
1778 ves_icall_MonoGenericInst_GetInterfaces (MonoReflectionGenericInst *type)
1779 {
1780         static MonoClass *System_Reflection_MonoGenericInst;
1781         MonoGenericInst *ginst;
1782         MonoDomain *domain;
1783         MonoClass *klass;
1784         MonoArray *res;
1785         int i;
1786
1787         MONO_ARCH_SAVE_REGS;
1788
1789         if (!System_Reflection_MonoGenericInst) {
1790                 System_Reflection_MonoGenericInst = mono_class_from_name (
1791                         mono_defaults.corlib, "System.Reflection", "MonoGenericInst");
1792                 g_assert (System_Reflection_MonoGenericInst);
1793         }
1794
1795         domain = mono_object_domain (type);
1796
1797         ginst = type->type.type->data.generic_inst;
1798         if (!ginst || !ginst->ifaces)
1799                 return mono_array_new (domain, System_Reflection_MonoGenericInst, 0);
1800
1801         klass = mono_class_from_mono_type (ginst->generic_type);
1802
1803         res = mono_array_new (domain, System_Reflection_MonoGenericInst, klass->interface_count);
1804
1805         for (i = 0; i < klass->interface_count; i++) {
1806                 MonoReflectionType *iface = mono_type_get_object (domain, ginst->ifaces [i]);
1807
1808                 mono_array_set (res, gpointer, i, iface);
1809         }
1810
1811         return res;
1812 }
1813
1814 static void
1815 ves_icall_MonoGenericParam_initialize (MonoReflectionGenericParam *gparam)
1816 {
1817         MONO_ARCH_SAVE_REGS;
1818         mono_reflection_initialize_generic_parameter (gparam);
1819 }
1820
1821 static MonoReflectionMethod *
1822 ves_icall_MonoType_get_DeclaringMethod (MonoReflectionType *type)
1823 {
1824         MonoMethod *method;
1825         MonoClass *klass;
1826
1827         MONO_ARCH_SAVE_REGS;
1828
1829         if (type->type->byref)
1830                 return FALSE;
1831
1832         method = type->type->data.generic_param->method;
1833         if (!method)
1834                 return NULL;
1835
1836         klass = mono_class_from_mono_type (type->type);
1837         return mono_method_get_object (mono_object_domain (type), method, klass);
1838 }
1839
1840 static gboolean
1841 ves_icall_MethodInfo_get_IsGenericMethodDefinition (MonoReflectionMethod *method)
1842 {
1843         MonoMethodNormal *mn;
1844         MONO_ARCH_SAVE_REGS;
1845
1846         if ((method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1847             (method->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1848                 return FALSE;
1849
1850         mn = (MonoMethodNormal *) method->method;
1851         return mn->header->gen_params != NULL;
1852 }
1853
1854 static MonoArray*
1855 ves_icall_MonoMethod_GetGenericArguments (MonoReflectionMethod *method)
1856 {
1857         MonoMethodNormal *mn;
1858         MonoArray *res;
1859         int count, i;
1860         MONO_ARCH_SAVE_REGS;
1861
1862         if ((method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1863             (method->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
1864                 return mono_array_new (mono_object_domain (method), mono_defaults.monotype_class, 0);
1865
1866         mn = (MonoMethodNormal *) method->method;
1867         count = method->method->signature->generic_param_count;
1868         res = mono_array_new (mono_object_domain (method), mono_defaults.monotype_class, count);
1869
1870         for (i = 0; i < count; i++) {
1871                 MonoGenericParam *param = &mn->header->gen_params [i];
1872                 MonoClass *pklass = mono_class_from_generic_parameter (param, method->method->klass->image, TRUE);
1873                 mono_array_set (res, gpointer, i, mono_type_get_object (mono_object_domain (method), &pklass->byval_arg));
1874         }
1875
1876         return res;
1877 }
1878
1879 static MonoObject *
1880 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
1881 {
1882         /* 
1883          * Invoke from reflection is supposed to always be a virtual call (the API
1884          * is stupid), mono_runtime_invoke_*() calls the provided method, allowing
1885          * greater flexibility.
1886          */
1887         MonoMethod *m = method->method;
1888         int pcount;
1889
1890         MONO_ARCH_SAVE_REGS;
1891
1892         if (this) {
1893                 if (!mono_object_isinst (this, m->klass))
1894                         mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1895                 m = mono_object_get_virtual_method (this, m);
1896         } else if (!(m->flags & METHOD_ATTRIBUTE_STATIC) && strcmp (m->name, ".ctor") && !m->wrapper_type)
1897                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetException"));
1898
1899         pcount = params? mono_array_length (params): 0;
1900         if (pcount != m->signature->param_count)
1901                 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib, "System.Reflection", "TargetParameterCountException"));
1902
1903         if (m->klass->rank && !strcmp (m->name, ".ctor")) {
1904                 int i;
1905                 guint32 *lengths;
1906                 guint32 *lower_bounds;
1907                 pcount = mono_array_length (params);
1908                 lengths = alloca (sizeof (guint32) * pcount);
1909                 for (i = 0; i < pcount; ++i)
1910                         lengths [i] = *(gint32*) ((char*)mono_array_get (params, gpointer, i) + sizeof (MonoObject));
1911
1912                 if (m->klass->rank == pcount) {
1913                         /* Only lengths provided. */
1914                         lower_bounds = NULL;
1915                 } else {
1916                         g_assert (pcount == (m->klass->rank * 2));
1917                         /* lower bounds are first. */
1918                         lower_bounds = lengths;
1919                         lengths += m->klass->rank;
1920                 }
1921
1922                 return (MonoObject*)mono_array_new_full (mono_object_domain (params), m->klass, lengths, lower_bounds);
1923         }
1924         return mono_runtime_invoke_array (m, this, params, NULL);
1925 }
1926
1927 static MonoObject *
1928 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
1929 {
1930         MonoDomain *domain = mono_object_domain (method); 
1931         MonoMethod *m = method->method;
1932         MonoMethodSignature *sig = m->signature;
1933         MonoArray *out_args;
1934         MonoObject *result;
1935         int i, j, outarg_count = 0;
1936
1937         MONO_ARCH_SAVE_REGS;
1938
1939         if (m->klass == mono_defaults.object_class) {
1940
1941                 if (!strcmp (m->name, "FieldGetter")) {
1942                         MonoClass *k = this->vtable->klass;
1943                         MonoString *name = mono_array_get (params, MonoString *, 1);
1944                         char *str;
1945
1946                         str = mono_string_to_utf8 (name);
1947                 
1948                         for (i = 0; i < k->field.count; i++) {
1949                                 if (!strcmp (k->fields [i].name, str)) {
1950                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1951                                         if (field_klass->valuetype)
1952                                                 result = mono_value_box (domain, field_klass,
1953                                                                          (char *)this + k->fields [i].offset);
1954                                         else 
1955                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
1956                                 
1957                                         g_assert (result);
1958                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
1959                                         *outArgs = out_args;
1960                                         mono_array_set (out_args, gpointer, 0, result);
1961                                         g_free (str);
1962                                         return NULL;
1963                                 }
1964                         }
1965
1966                         g_free (str);
1967                         g_assert_not_reached ();
1968
1969                 } else if (!strcmp (m->name, "FieldSetter")) {
1970                         MonoClass *k = this->vtable->klass;
1971                         MonoString *name = mono_array_get (params, MonoString *, 1);
1972                         int size, align;
1973                         char *str;
1974
1975                         str = mono_string_to_utf8 (name);
1976                 
1977                         for (i = 0; i < k->field.count; i++) {
1978                                 if (!strcmp (k->fields [i].name, str)) {
1979                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1980                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1981
1982                                         if (field_klass->valuetype) {
1983                                                 size = mono_type_size (k->fields [i].type, &align);
1984                                                 memcpy ((char *)this + k->fields [i].offset, 
1985                                                         ((char *)val) + sizeof (MonoObject), size);
1986                                         } else 
1987                                                 *(MonoObject**)((char *)this + k->fields [i].offset) = val;
1988                                 
1989                                         out_args = mono_array_new (domain, mono_defaults.object_class, 0);
1990                                         *outArgs = out_args;
1991
1992                                         g_free (str);
1993                                         return NULL;
1994                                 }
1995                         }
1996
1997                         g_free (str);
1998                         g_assert_not_reached ();
1999
2000                 }
2001         }
2002
2003         for (i = 0; i < mono_array_length (params); i++) {
2004                 if (sig->params [i]->byref) 
2005                         outarg_count++;
2006         }
2007
2008         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
2009         
2010         /* fixme: handle constructors? */
2011         if (!strcmp (method->method->name, ".ctor"))
2012                 g_assert_not_reached ();
2013
2014         result = mono_runtime_invoke_array (method->method, this, params, NULL);
2015
2016         for (i = 0, j = 0; i < mono_array_length (params); i++) {
2017                 if (sig->params [i]->byref) {
2018                         gpointer arg;
2019                         arg = mono_array_get (params, gpointer, i);
2020                         mono_array_set (out_args, gpointer, j, arg);
2021                         j++;
2022                 }
2023         }
2024
2025         *outArgs = out_args;
2026
2027         return result;
2028 }
2029
2030 static MonoObject *
2031 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
2032 {
2033         MonoDomain *domain; 
2034         MonoClass *enumc, *objc;
2035         gint32 s1, s2;
2036         MonoObject *res;
2037         
2038         MONO_ARCH_SAVE_REGS;
2039
2040         MONO_CHECK_ARG_NULL (type);
2041         MONO_CHECK_ARG_NULL (obj);
2042
2043         domain = mono_object_domain (type); 
2044         enumc = mono_class_from_mono_type (type->type);
2045         objc = obj->vtable->klass;
2046
2047         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
2048         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
2049                                                   objc->byval_arg.type <= MONO_TYPE_U8));
2050         
2051         s1 = mono_class_value_size (enumc, NULL);
2052         s2 = mono_class_value_size (objc, NULL);
2053
2054         res = mono_object_new (domain, enumc);
2055
2056 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2057         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
2058 #else
2059         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
2060                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
2061                 MIN (s1, s2));
2062 #endif
2063         return res;
2064 }
2065
2066 static MonoObject *
2067 ves_icall_System_Enum_get_value (MonoObject *this)
2068 {
2069         MonoObject *res;
2070         MonoClass *enumc;
2071         gpointer dst;
2072         gpointer src;
2073         int size;
2074
2075         MONO_ARCH_SAVE_REGS;
2076
2077         if (!this)
2078                 return NULL;
2079
2080         g_assert (this->vtable->klass->enumtype);
2081         
2082         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
2083         res = mono_object_new (mono_object_domain (this), enumc);
2084         dst = (char *)res + sizeof (MonoObject);
2085         src = (char *)this + sizeof (MonoObject);
2086         size = mono_class_value_size (enumc, NULL);
2087
2088         memcpy (dst, src, size);
2089
2090         return res;
2091 }
2092
2093 static void
2094 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
2095 {
2096         MonoDomain *domain = mono_object_domain (type); 
2097         MonoClass *enumc = mono_class_from_mono_type (type->type);
2098         guint i, j, nvalues, crow;
2099         MonoClassField *field;
2100
2101         MONO_ARCH_SAVE_REGS;
2102
2103         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
2104         nvalues = enumc->field.count - 1;
2105         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
2106         info->values = mono_array_new (domain, enumc, nvalues);
2107         
2108         crow = -1;
2109         for (i = 0, j = 0; i < enumc->field.count; ++i) {
2110                 const char *p;
2111                 int len;
2112
2113                 field = &enumc->fields [i];
2114                 if (strcmp ("value__", field->name) == 0)
2115                         continue;
2116                 if (mono_field_is_deleted (field))
2117                         continue;
2118                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
2119                 if (!field->def_value) {
2120                         field->def_value = g_new0 (MonoConstant, 1);
2121                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1), crow + 1);
2122                         field->def_value->type = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_TYPE);
2123                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
2124                         field->def_value->value = (gpointer)mono_metadata_blob_heap (enumc->image, crow);
2125                 }
2126
2127                 p = field->def_value->value;
2128                 len = mono_metadata_decode_blob_size (p, &p);
2129                 switch (enumc->enum_basetype->type) {
2130                 case MONO_TYPE_U1:
2131                 case MONO_TYPE_I1:
2132                         mono_array_set (info->values, gchar, j, *p);
2133                         break;
2134                 case MONO_TYPE_CHAR:
2135                 case MONO_TYPE_U2:
2136                 case MONO_TYPE_I2:
2137                         mono_array_set (info->values, gint16, j, read16 (p));
2138                         break;
2139                 case MONO_TYPE_U4:
2140                 case MONO_TYPE_I4:
2141                         mono_array_set (info->values, gint32, j, read32 (p));
2142                         break;
2143                 case MONO_TYPE_U8:
2144                 case MONO_TYPE_I8:
2145                         mono_array_set (info->values, gint64, j, read64 (p));
2146                         break;
2147                 default:
2148                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
2149                 }
2150                 ++j;
2151         }
2152 }
2153
2154 enum {
2155         BFLAGS_IgnoreCase = 1,
2156         BFLAGS_DeclaredOnly = 2,
2157         BFLAGS_Instance = 4,
2158         BFLAGS_Static = 8,
2159         BFLAGS_Public = 0x10,
2160         BFLAGS_NonPublic = 0x20,
2161         BFLAGS_FlattenHierarchy = 0x40,
2162         BFLAGS_InvokeMethod = 0x100,
2163         BFLAGS_CreateInstance = 0x200,
2164         BFLAGS_GetField = 0x400,
2165         BFLAGS_SetField = 0x800,
2166         BFLAGS_GetProperty = 0x1000,
2167         BFLAGS_SetProperty = 0x2000,
2168         BFLAGS_ExactBinding = 0x10000,
2169         BFLAGS_SuppressChangeType = 0x20000,
2170         BFLAGS_OptionalParamBinding = 0x40000
2171 };
2172
2173 static MonoReflectionField *
2174 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
2175 {
2176         MonoDomain *domain; 
2177         MonoClass *startklass, *klass;
2178         int i, match;
2179         MonoClassField *field;
2180         char *utf8_name;
2181         domain = ((MonoObject *)type)->vtable->domain;
2182         klass = startklass = mono_class_from_mono_type (type->type);
2183
2184         MONO_ARCH_SAVE_REGS;
2185
2186         if (!name)
2187                 mono_raise_exception (mono_get_exception_argument_null ("name"));
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                 
2217                 utf8_name = mono_string_to_utf8 (name);
2218
2219                 if (strcmp (field->name, utf8_name)) {
2220                         g_free (utf8_name);
2221                         continue;
2222                 }
2223                 g_free (utf8_name);
2224                 
2225                 return mono_field_get_object (domain, klass, field);
2226         }
2227         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2228                 goto handle_parent;
2229
2230         return NULL;
2231 }
2232
2233 static MonoArray*
2234 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
2235 {
2236         MonoDomain *domain; 
2237         GSList *l = NULL, *tmp;
2238         MonoClass *startklass, *klass;
2239         MonoArray *res;
2240         MonoObject *member;
2241         int i, len, match;
2242         MonoClassField *field;
2243
2244         MONO_ARCH_SAVE_REGS;
2245
2246         domain = ((MonoObject *)type)->vtable->domain;
2247         klass = startklass = mono_class_from_mono_type (type->type);
2248
2249 handle_parent:  
2250         for (i = 0; i < klass->field.count; ++i) {
2251                 match = 0;
2252                 field = &klass->fields [i];
2253                 if (mono_field_is_deleted (field))
2254                         continue;
2255                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
2256                         if (bflags & BFLAGS_Public)
2257                                 match++;
2258                 } else {
2259                         if (bflags & BFLAGS_NonPublic)
2260                                 match++;
2261                 }
2262                 if (!match)
2263                         continue;
2264                 match = 0;
2265                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
2266                         if (bflags & BFLAGS_Static)
2267                                 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
2268                                         match++;
2269                 } else {
2270                         if (bflags & BFLAGS_Instance)
2271                                 match++;
2272                 }
2273
2274                 if (!match)
2275                         continue;
2276                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
2277                 l = g_slist_prepend (l, member);
2278         }
2279         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2280                 goto handle_parent;
2281         len = g_slist_length (l);
2282         res = mono_array_new (domain, mono_defaults.field_info_class, len);
2283         i = 0;
2284         tmp = l = g_slist_reverse (l);
2285         for (; tmp; tmp = tmp->next, ++i)
2286                 mono_array_set (res, gpointer, i, tmp->data);
2287         g_slist_free (l);
2288         return res;
2289 }
2290
2291 static MonoArray*
2292 ves_icall_Type_GetMethodsByName (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoBoolean ignore_case)
2293 {
2294         MonoDomain *domain; 
2295         GSList *l = NULL, *tmp;
2296         MonoClass *startklass, *klass;
2297         MonoArray *res;
2298         MonoMethod *method;
2299         MonoObject *member;
2300         int i, len, match;
2301         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
2302         gchar *mname = NULL;
2303         int (*compare_func) (const char *s1, const char *s2) = NULL;
2304                 
2305         MONO_ARCH_SAVE_REGS;
2306
2307         domain = ((MonoObject *)type)->vtable->domain;
2308         klass = startklass = mono_class_from_mono_type (type->type);
2309         len = 0;
2310         if (name != NULL) {
2311                 mname = mono_string_to_utf8 (name);
2312                 compare_func = (ignore_case) ? g_strcasecmp : strcmp;
2313         }
2314
2315 handle_parent:
2316         for (i = 0; i < klass->method.count; ++i) {
2317                 match = 0;
2318                 method = klass->methods [i];
2319                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
2320                         continue;
2321                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2322                         if (bflags & BFLAGS_Public)
2323                                 match++;
2324                 } else {
2325                         if (bflags & BFLAGS_NonPublic)
2326                                 match++;
2327                 }
2328                 if (!match)
2329                         continue;
2330                 match = 0;
2331                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2332                         if (bflags & BFLAGS_Static)
2333                                 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
2334                                         match++;
2335                 } else {
2336                         if (bflags & BFLAGS_Instance)
2337                                 match++;
2338                 }
2339
2340                 if (!match)
2341                         continue;
2342
2343                 if (name != NULL) {
2344                         if (compare_func (mname, method->name))
2345                                 continue;
2346                 }
2347                 
2348                 match = 0;
2349                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
2350                         continue;
2351                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
2352                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
2353                 
2354                 l = g_slist_prepend (l, member);
2355                 len++;
2356         }
2357         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2358                 goto handle_parent;
2359
2360         g_free (mname);
2361         res = mono_array_new (domain, mono_defaults.method_info_class, len);
2362         i = 0;
2363
2364         tmp = l = g_slist_reverse (l);
2365
2366         for (; tmp; tmp = tmp->next, ++i)
2367                 mono_array_set (res, gpointer, i, tmp->data);
2368         g_slist_free (l);
2369         g_hash_table_destroy (method_slots);
2370         return res;
2371 }
2372
2373 static MonoArray*
2374 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
2375 {
2376         MonoDomain *domain; 
2377         GSList *l = NULL, *tmp;
2378         static MonoClass *System_Reflection_ConstructorInfo;
2379         MonoClass *startklass, *klass;
2380         MonoArray *res;
2381         MonoMethod *method;
2382         MonoObject *member;
2383         int i, len, match;
2384
2385         MONO_ARCH_SAVE_REGS;
2386
2387         domain = ((MonoObject *)type)->vtable->domain;
2388         klass = startklass = mono_class_from_mono_type (type->type);
2389
2390         for (i = 0; i < klass->method.count; ++i) {
2391                 match = 0;
2392                 method = klass->methods [i];
2393                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
2394                         continue;
2395                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2396                         if (bflags & BFLAGS_Public)
2397                                 match++;
2398                 } else {
2399                         if (bflags & BFLAGS_NonPublic)
2400                                 match++;
2401                 }
2402                 if (!match)
2403                         continue;
2404                 match = 0;
2405                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2406                         if (bflags & BFLAGS_Static)
2407                                 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
2408                                         match++;
2409                 } else {
2410                         if (bflags & BFLAGS_Instance)
2411                                 match++;
2412                 }
2413
2414                 if (!match)
2415                         continue;
2416                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
2417                         
2418                 l = g_slist_prepend (l, member);
2419         }
2420         len = g_slist_length (l);
2421         if (!System_Reflection_ConstructorInfo)
2422                 System_Reflection_ConstructorInfo = mono_class_from_name (
2423                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
2424         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
2425         i = 0;
2426         tmp = l = g_slist_reverse (l);
2427         for (; tmp; tmp = tmp->next, ++i)
2428                 mono_array_set (res, gpointer, i, tmp->data);
2429         g_slist_free (l);
2430         return res;
2431 }
2432
2433 static MonoArray*
2434 ves_icall_Type_GetPropertiesByName (MonoReflectionType *type, MonoString *name, guint32 bflags, MonoBoolean ignore_case)
2435 {
2436         MonoDomain *domain; 
2437         GSList *l = NULL, *tmp;
2438         static MonoClass *System_Reflection_PropertyInfo;
2439         MonoClass *startklass, *klass;
2440         MonoArray *res;
2441         MonoMethod *method;
2442         MonoProperty *prop;
2443         int i, match;
2444         int len = 0;
2445         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
2446         gchar *propname = NULL;
2447         int (*compare_func) (const char *s1, const char *s2) = NULL;
2448
2449         MONO_ARCH_SAVE_REGS;
2450
2451         domain = ((MonoObject *)type)->vtable->domain;
2452         klass = startklass = mono_class_from_mono_type (type->type);
2453         if (name != NULL) {
2454                 propname = mono_string_to_utf8 (name);
2455                 compare_func = (ignore_case) ? g_strcasecmp : strcmp;
2456         }
2457
2458 handle_parent:
2459         for (i = 0; i < klass->property.count; ++i) {
2460                 prop = &klass->properties [i];
2461                 match = 0;
2462                 method = prop->get;
2463                 if (!method)
2464                         method = prop->set;
2465                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2466                         if (bflags & BFLAGS_Public)
2467                                 match++;
2468                 } else {
2469                         if (bflags & BFLAGS_NonPublic)
2470                                 match++;
2471                 }
2472                 if (!match)
2473                         continue;
2474                 match = 0;
2475                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2476                         if (bflags & BFLAGS_Static)
2477                                 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
2478                                         match++;
2479                 } else {
2480                         if (bflags & BFLAGS_Instance)
2481                                 match++;
2482                 }
2483
2484                 if (!match)
2485                         continue;
2486                 match = 0;
2487
2488                 if (name != NULL) {
2489                         if (compare_func (propname, prop->name))
2490                                 continue;
2491                 }
2492                 
2493                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
2494                         continue;
2495                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), prop);
2496
2497                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
2498                 len++;
2499         }
2500         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
2501                 goto handle_parent;
2502
2503         g_free (propname);
2504         if (!System_Reflection_PropertyInfo)
2505                 System_Reflection_PropertyInfo = mono_class_from_name (
2506                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
2507         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
2508         i = 0;
2509
2510         tmp = l = g_slist_reverse (l);
2511
2512         for (; tmp; tmp = tmp->next, ++i)
2513                 mono_array_set (res, gpointer, i, tmp->data);
2514         g_slist_free (l);
2515         g_hash_table_destroy (method_slots);
2516         return res;
2517 }
2518
2519 static MonoReflectionEvent *
2520 ves_icall_MonoType_GetEvent (MonoReflectionType *type, MonoString *name, guint32 bflags)
2521 {
2522         MonoDomain *domain;
2523         MonoClass *klass;
2524         gint i;
2525         MonoEvent *event;
2526         MonoMethod *method;
2527         gchar *event_name;
2528
2529         MONO_ARCH_SAVE_REGS;
2530
2531         event_name = mono_string_to_utf8 (name);
2532         klass = mono_class_from_mono_type (type->type);
2533         domain = mono_object_domain (type);
2534
2535 handle_parent:  
2536         for (i = 0; i < klass->event.count; i++) {
2537                 event = &klass->events [i];
2538                 if (strcmp (event->name, event_name))
2539                         continue;
2540
2541                 method = event->add;
2542                 if (!method)
2543                         method = event->remove;
2544
2545                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2546                         if (!(bflags & BFLAGS_Public))
2547                                 continue;
2548                 } else {
2549                         if (!(bflags & BFLAGS_NonPublic))
2550                                 continue;
2551                 }
2552
2553                 g_free (event_name);
2554                 return mono_event_get_object (domain, klass, event);
2555         }
2556
2557         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2558                 goto handle_parent;
2559
2560         g_free (event_name);
2561         return NULL;
2562 }
2563
2564 static MonoArray*
2565 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
2566 {
2567         MonoDomain *domain; 
2568         GSList *l = NULL, *tmp;
2569         static MonoClass *System_Reflection_EventInfo;
2570         MonoClass *startklass, *klass;
2571         MonoArray *res;
2572         MonoMethod *method;
2573         MonoEvent *event;
2574         int i, len, match;
2575
2576         MONO_ARCH_SAVE_REGS;
2577
2578         domain = ((MonoObject *)type)->vtable->domain;
2579         klass = startklass = mono_class_from_mono_type (type->type);
2580
2581 handle_parent:  
2582         for (i = 0; i < klass->event.count; ++i) {
2583                 event = &klass->events [i];
2584                 match = 0;
2585                 method = event->add;
2586                 if (!method)
2587                         method = event->remove;
2588                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
2589                         if (bflags & BFLAGS_Public)
2590                                 match++;
2591                 } else {
2592                         if (bflags & BFLAGS_NonPublic)
2593                                 match++;
2594                 }
2595                 if (!match)
2596                         continue;
2597                 match = 0;
2598                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2599                         if (bflags & BFLAGS_Static)
2600                                 if ((bflags & BFLAGS_FlattenHierarchy) || (klass == startklass))
2601                                         match++;
2602                 } else {
2603                         if (bflags & BFLAGS_Instance)
2604                                 match++;
2605                 }
2606
2607                 if (!match)
2608                         continue;
2609                 match = 0;
2610                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
2611         }
2612         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2613                 goto handle_parent;
2614         len = g_slist_length (l);
2615         if (!System_Reflection_EventInfo)
2616                 System_Reflection_EventInfo = mono_class_from_name (
2617                         mono_defaults.corlib, "System.Reflection", "EventInfo");
2618         res = mono_array_new (domain, System_Reflection_EventInfo, len);
2619         i = 0;
2620
2621         tmp = l = g_slist_reverse (l);
2622
2623         for (; tmp; tmp = tmp->next, ++i)
2624                 mono_array_set (res, gpointer, i, tmp->data);
2625         g_slist_free (l);
2626         return res;
2627 }
2628
2629 static MonoReflectionType *
2630 ves_icall_Type_GetNestedType (MonoReflectionType *type, MonoString *name, guint32 bflags)
2631 {
2632         MonoDomain *domain; 
2633         MonoClass *startklass, *klass;
2634         MonoClass *nested;
2635         GList *tmpn;
2636         char *str;
2637         
2638         MONO_ARCH_SAVE_REGS;
2639
2640         domain = ((MonoObject *)type)->vtable->domain;
2641         klass = startklass = mono_class_from_mono_type (type->type);
2642         str = mono_string_to_utf8 (name);
2643
2644  handle_parent:
2645         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2646                 int match = 0;
2647                 nested = tmpn->data;
2648                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2649                         if (bflags & BFLAGS_Public)
2650                                 match++;
2651                 } else {
2652                         if (bflags & BFLAGS_NonPublic)
2653                                 match++;
2654                 }
2655                 if (!match)
2656                         continue;
2657                 if (strcmp (nested->name, str) == 0){
2658                         g_free (str);
2659                         return mono_type_get_object (domain, &nested->byval_arg);
2660                 }
2661         }
2662         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
2663                 goto handle_parent;
2664         g_free (str);
2665         return NULL;
2666 }
2667
2668 static MonoArray*
2669 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
2670 {
2671         MonoDomain *domain; 
2672         GSList *l = NULL, *tmp;
2673         GList *tmpn;
2674         MonoClass *startklass, *klass;
2675         MonoArray *res;
2676         MonoObject *member;
2677         int i, len, match;
2678         MonoClass *nested;
2679
2680         MONO_ARCH_SAVE_REGS;
2681
2682         domain = ((MonoObject *)type)->vtable->domain;
2683         klass = startklass = mono_class_from_mono_type (type->type);
2684
2685         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
2686                 match = 0;
2687                 nested = tmpn->data;
2688                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
2689                         if (bflags & BFLAGS_Public)
2690                                 match++;
2691                 } else {
2692                         if (bflags & BFLAGS_NonPublic)
2693                                 match++;
2694                 }
2695                 if (!match)
2696                         continue;
2697                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
2698                 l = g_slist_prepend (l, member);
2699         }
2700         len = g_slist_length (l);
2701         res = mono_array_new (domain, mono_defaults.monotype_class, len);
2702         i = 0;
2703         tmp = l = g_slist_reverse (l);
2704         for (; tmp; tmp = tmp->next, ++i)
2705                 mono_array_set (res, gpointer, i, tmp->data);
2706         g_slist_free (l);
2707         return res;
2708 }
2709
2710 static MonoReflectionType*
2711 ves_icall_System_Reflection_Assembly_InternalGetType (MonoReflectionAssembly *assembly, MonoReflectionModule *module, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
2712 {
2713         gchar *str;
2714         MonoType *type = NULL;
2715         MonoTypeNameParse info;
2716
2717         MONO_ARCH_SAVE_REGS;
2718
2719         str = mono_string_to_utf8 (name);
2720         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
2721         if (!mono_reflection_parse_type (str, &info)) {
2722                 g_free (str);
2723                 g_list_free (info.modifiers);
2724                 g_list_free (info.nested);
2725                 if (throwOnError) /* uhm: this is a parse error, though... */
2726                         mono_raise_exception (mono_get_exception_type_load (name));
2727                 /*g_print ("failed parse\n");*/
2728                 return NULL;
2729         }
2730
2731         if (module != NULL) {
2732                 if (module->image)
2733                         type = mono_reflection_get_type (module->image, &info, ignoreCase);
2734                 else
2735                         type = NULL;
2736         }
2737         else
2738                 if (assembly->assembly->dynamic) {
2739                         /* Enumerate all modules */
2740                         MonoReflectionAssemblyBuilder *abuilder = (MonoReflectionAssemblyBuilder*)assembly;
2741                         int i;
2742
2743                         type = NULL;
2744                         if (abuilder->modules) {
2745                                 for (i = 0; i < mono_array_length (abuilder->modules); ++i) {
2746                                         MonoReflectionModuleBuilder *mb = mono_array_get (abuilder->modules, MonoReflectionModuleBuilder*, i);
2747                                         type = mono_reflection_get_type (&mb->dynamic_image->image, &info, ignoreCase);
2748                                         if (type)
2749                                                 break;
2750                                 }
2751                         }
2752
2753                         if (!type && abuilder->loaded_modules) {
2754                                 for (i = 0; i < mono_array_length (abuilder->loaded_modules); ++i) {
2755                                         MonoReflectionModule *mod = mono_array_get (abuilder->loaded_modules, MonoReflectionModule*, i);
2756                                         type = mono_reflection_get_type (mod->image, &info, ignoreCase);
2757                                         if (type)
2758                                                 break;
2759                                 }
2760                         }
2761                 }
2762                 else
2763                         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
2764         g_free (str);
2765         g_list_free (info.modifiers);
2766         g_list_free (info.nested);
2767         if (!type) {
2768                 if (throwOnError)
2769                         mono_raise_exception (mono_get_exception_type_load (name));
2770                 /* g_print ("failed find\n"); */
2771                 return NULL;
2772         }
2773         /* g_print ("got it\n"); */
2774         return mono_type_get_object (mono_object_domain (assembly), type);
2775
2776 }
2777
2778 static MonoString *
2779 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
2780 {
2781         MonoDomain *domain = mono_object_domain (assembly); 
2782         MonoAssembly *mass = assembly->assembly;
2783         MonoString *res;
2784         gchar *uri;
2785         gchar *absolute;
2786         
2787         MONO_ARCH_SAVE_REGS;
2788
2789         absolute = g_build_filename (mass->basedir, mass->image->module_name, NULL);
2790         uri = g_filename_to_uri (absolute, NULL, NULL);
2791         res = mono_string_new (domain, uri);
2792         g_free (uri);
2793         g_free (absolute);
2794         return res;
2795 }
2796
2797 static MonoString *
2798 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
2799 {
2800         MonoDomain *domain = mono_object_domain (assembly); 
2801         MonoString *res;
2802         char *name = g_build_filename (
2803                 assembly->assembly->basedir,
2804                 assembly->assembly->image->module_name, NULL);
2805
2806         MONO_ARCH_SAVE_REGS;
2807
2808         res = mono_string_new (domain, name);
2809         g_free (name);
2810         return res;
2811 }
2812
2813 static MonoString *
2814 ves_icall_System_Reflection_Assembly_InternalImageRuntimeVersion (MonoReflectionAssembly *assembly)
2815 {
2816         MonoDomain *domain = mono_object_domain (assembly); 
2817
2818         MONO_ARCH_SAVE_REGS;
2819
2820         return mono_string_new (domain, assembly->assembly->image->version);
2821 }
2822
2823 static MonoReflectionMethod*
2824 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) 
2825 {
2826         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
2827
2828         MONO_ARCH_SAVE_REGS;
2829
2830         if (!token)
2831                 return NULL;
2832         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
2833 }
2834
2835 static MonoArray*
2836 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) 
2837 {
2838         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2839         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2840         int i;
2841         const char *val;
2842
2843         MONO_ARCH_SAVE_REGS;
2844
2845         for (i = 0; i < table->rows; ++i) {
2846                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
2847                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
2848         }
2849         return result;
2850 }
2851
2852 static MonoArray*
2853 ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAssembly *assembly) 
2854 {
2855         static MonoClass *System_Reflection_AssemblyName;
2856         MonoArray *result;
2857         MonoAssembly **ptr;
2858         MonoDomain *domain = mono_object_domain (assembly);
2859         int i, count = 0;
2860
2861         MONO_ARCH_SAVE_REGS;
2862
2863         if (!System_Reflection_AssemblyName)
2864                 System_Reflection_AssemblyName = mono_class_from_name (
2865                         mono_defaults.corlib, "System.Reflection", "AssemblyName");
2866
2867         for (ptr = assembly->assembly->image->references; ptr && *ptr; ptr++)
2868                 count++;
2869
2870         result = mono_array_new (mono_object_domain (assembly), System_Reflection_AssemblyName, count);
2871
2872         for (i = 0; i < count; i++) {
2873                 MonoAssembly *assem = assembly->assembly->image->references [i];
2874                 MonoReflectionAssemblyName *aname;
2875                 char *codebase, *absolute;
2876
2877                 aname = (MonoReflectionAssemblyName *) mono_object_new (
2878                         domain, System_Reflection_AssemblyName);
2879
2880                 if (strcmp (assem->aname.name, "corlib") == 0)
2881                         aname->name = mono_string_new (domain, "mscorlib");
2882                 else
2883                         aname->name = mono_string_new (domain, assem->aname.name);
2884                 aname->major = assem->aname.major;
2885
2886                 absolute = g_build_filename (assem->basedir, assem->image->module_name, NULL);
2887                 codebase = g_filename_to_uri (absolute, NULL, NULL);
2888                 aname->codebase = mono_string_new (domain, codebase);
2889                 g_free (codebase);
2890                 g_free (absolute);
2891                 mono_array_set (result, gpointer, i, aname);
2892         }
2893         return result;
2894 }
2895
2896 typedef struct {
2897         MonoArray *res;
2898         int idx;
2899 } NameSpaceInfo;
2900
2901 static void
2902 foreach_namespace (const char* key, gconstpointer val, NameSpaceInfo *info)
2903 {
2904         MonoString *name = mono_string_new (mono_object_domain (info->res), key);
2905
2906         mono_array_set (info->res, gpointer, info->idx, name);
2907         info->idx++;
2908 }
2909
2910 static MonoArray*
2911 ves_icall_System_Reflection_Assembly_GetNamespaces (MonoReflectionAssembly *assembly) 
2912 {
2913         MonoImage *img = assembly->assembly->image;
2914         int n;
2915         MonoArray *res;
2916         NameSpaceInfo info;
2917         MonoTableInfo  *t = &img->tables [MONO_TABLE_EXPORTEDTYPE];
2918         int i;
2919
2920         MONO_ARCH_SAVE_REGS;
2921
2922         n = g_hash_table_size (img->name_cache);
2923         res = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n);
2924         info.res = res;
2925         info.idx = 0;
2926         g_hash_table_foreach (img->name_cache, (GHFunc)foreach_namespace, &info);
2927
2928         /* Add namespaces from the EXPORTEDTYPES table as well */
2929         if (t->rows) {
2930                 MonoArray *res2;
2931                 GPtrArray *nspaces = g_ptr_array_new ();
2932                 for (i = 0; i < t->rows; ++i) {
2933                         const char *nspace = mono_metadata_string_heap (img, mono_metadata_decode_row_col (t, i, MONO_EXP_TYPE_NAMESPACE));
2934                         if (!g_hash_table_lookup (img->name_cache, nspace)) {
2935                                 g_ptr_array_add (nspaces, (char*)nspace);
2936                         }
2937                 }
2938                 if (nspaces->len > 0) {
2939                         res2 = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n + nspaces->len);
2940                         memcpy (mono_array_addr (res2, MonoString*, 0),
2941                                         mono_array_addr (res, MonoString*, 0),
2942                                         n * sizeof (MonoString*));
2943                         for (i = 0; i < nspaces->len; ++i)
2944                                 mono_array_set (res2, MonoString*, n + i, 
2945                                                                 mono_string_new (mono_object_domain (assembly),
2946                                                                                                  g_ptr_array_index (nspaces, i)));
2947                         res = res2;
2948                 }
2949                 g_ptr_array_free (nspaces, TRUE);
2950         }
2951
2952         return res;
2953 }
2954
2955 /* move this in some file in mono/util/ */
2956 static char *
2957 g_concat_dir_and_file (const char *dir, const char *file)
2958 {
2959         g_return_val_if_fail (dir != NULL, NULL);
2960         g_return_val_if_fail (file != NULL, NULL);
2961
2962         /*
2963          * If the directory name doesn't have a / on the end, we need
2964          * to add one so we get a proper path to the file
2965          */
2966         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
2967                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
2968         else
2969                 return g_strconcat (dir, file, NULL);
2970 }
2971
2972 static void *
2973 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name, gint32 *size, MonoReflectionModule **ref_module) 
2974 {
2975         char *n = mono_string_to_utf8 (name);
2976         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2977         guint32 i;
2978         guint32 cols [MONO_MANIFEST_SIZE];
2979         guint32 impl, file_idx;
2980         const char *val;
2981         MonoImage *module;
2982
2983         MONO_ARCH_SAVE_REGS;
2984
2985         for (i = 0; i < table->rows; ++i) {
2986                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2987                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2988                 if (strcmp (val, n) == 0)
2989                         break;
2990         }
2991         g_free (n);
2992         if (i == table->rows)
2993                 return NULL;
2994         /* FIXME */
2995         impl = cols [MONO_MANIFEST_IMPLEMENTATION];
2996         if (impl) {
2997                 /*
2998                  * this code should only be called after obtaining the 
2999                  * ResourceInfo and handling the other cases.
3000                  */
3001                 g_assert ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE);
3002                 file_idx = impl >> IMPLEMENTATION_BITS;
3003
3004                 module = mono_image_load_file_for_image (assembly->assembly->image, file_idx);
3005                 if (!module)
3006                         return NULL;
3007         }
3008         else
3009                 module = assembly->assembly->image;
3010
3011         *ref_module = mono_module_get_object (mono_domain_get (), module);
3012
3013         return (void*)mono_image_get_resource (module, cols [MONO_MANIFEST_OFFSET], size);
3014 }
3015
3016 static gboolean
3017 ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflectionAssembly *assembly, MonoString *name, MonoManifestResourceInfo *info)
3018 {
3019         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
3020         int i;
3021         guint32 cols [MONO_MANIFEST_SIZE];
3022         guint32 file_cols [MONO_FILE_SIZE];
3023         const char *val;
3024         char *n;
3025
3026         MONO_ARCH_SAVE_REGS;
3027
3028         n = mono_string_to_utf8 (name);
3029         for (i = 0; i < table->rows; ++i) {
3030                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
3031                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
3032                 if (strcmp (val, n) == 0)
3033                         break;
3034         }
3035         g_free (n);
3036         if (i == table->rows)
3037                 return FALSE;
3038
3039         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
3040                 info->location = RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST;
3041         }
3042         else {
3043                 switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
3044                 case IMPLEMENTATION_FILE:
3045                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
3046                         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
3047                         mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
3048                         val = mono_metadata_string_heap (assembly->assembly->image, file_cols [MONO_FILE_NAME]);
3049                         info->filename = mono_string_new (mono_object_domain (assembly), val);
3050                         if (file_cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA)
3051                                 info->location = 0;
3052                         else
3053                                 info->location = RESOURCE_LOCATION_EMBEDDED;
3054                         break;
3055
3056                 case IMPLEMENTATION_ASSEMBLYREF:
3057                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
3058                         info->assembly = mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]);
3059
3060                         // Obtain info recursively
3061                         ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (info->assembly, name, info);
3062                         info->location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
3063                         break;
3064
3065                 case IMPLEMENTATION_EXP_TYPE:
3066                         g_assert_not_reached ();
3067                         break;
3068                 }
3069         }
3070
3071         return TRUE;
3072 }
3073
3074 static MonoObject*
3075 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) 
3076 {
3077         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
3078         MonoArray *result = NULL;
3079         int i;
3080         const char *val;
3081         char *n;
3082
3083         MONO_ARCH_SAVE_REGS;
3084
3085         /* check hash if needed */
3086         if (name) {
3087                 n = mono_string_to_utf8 (name);
3088                 for (i = 0; i < table->rows; ++i) {
3089                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
3090                         if (strcmp (val, n) == 0) {
3091                                 MonoString *fn;
3092                                 g_free (n);
3093                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
3094                                 fn = mono_string_new (mono_object_domain (assembly), n);
3095                                 g_free (n);
3096                                 return (MonoObject*)fn;
3097                         }
3098                 }
3099                 g_free (n);
3100                 return NULL;
3101         }
3102
3103         for (i = 0; i < table->rows; ++i) {
3104                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
3105                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
3106                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
3107                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
3108                 g_free (n);
3109         }
3110         return (MonoObject*)result;
3111 }
3112
3113 static MonoArray*
3114 ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly *assembly)
3115 {
3116         MonoDomain *domain = mono_domain_get();
3117         MonoArray *res;
3118         MonoClass *klass;
3119         int i, module_count = 0, file_count = 0;
3120         MonoImage **modules = assembly->assembly->image->modules;
3121         MonoTableInfo *table;
3122
3123         if (modules) {
3124                 while (modules[module_count])
3125                         ++module_count;
3126         }
3127
3128         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
3129         file_count = table->rows;
3130
3131         g_assert( assembly->assembly->image != NULL);
3132         ++module_count;
3133
3134         klass = mono_class_from_name ( mono_defaults.corlib, "System.Reflection", "Module");
3135         res = mono_array_new (domain, klass, module_count + file_count);
3136
3137         mono_array_set (res, gpointer, 0, mono_module_get_object (domain, assembly->assembly->image));
3138         for ( i = 1; i < module_count; ++i )
3139                 mono_array_set (res, gpointer, i, mono_module_get_object (domain, modules[i]));
3140
3141         for (i = 0; i < table->rows; ++i)
3142                 mono_array_set (res, gpointer, module_count + i, mono_module_file_get_object (domain, assembly->assembly->image, i));
3143
3144         return res;
3145 }
3146
3147 static MonoReflectionMethod*
3148 ves_icall_GetCurrentMethod (void) 
3149 {
3150         MonoMethod *m = mono_method_get_last_managed ();
3151
3152         MONO_ARCH_SAVE_REGS;
3153
3154         return mono_method_get_object (mono_domain_get (), m, NULL);
3155 }
3156
3157 static MonoReflectionAssembly*
3158 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
3159 {
3160         MonoMethod *m = mono_method_get_last_managed ();
3161
3162         MONO_ARCH_SAVE_REGS;
3163
3164         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
3165 }
3166
3167
3168 static gboolean
3169 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
3170 {
3171         MonoMethod **dest = data;
3172
3173         /* skip unmanaged frames */
3174         if (!managed)
3175                 return FALSE;
3176
3177         if (m == *dest) {
3178                 *dest = NULL;
3179                 return FALSE;
3180         }
3181         if (!(*dest)) {
3182                 *dest = m;
3183                 return TRUE;
3184         }
3185         return FALSE;
3186 }
3187
3188 static MonoReflectionAssembly*
3189 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
3190 {
3191         MonoDomain* domain = mono_domain_get ();
3192
3193         MONO_ARCH_SAVE_REGS;
3194
3195         if (!domain->entry_assembly)
3196                 domain = mono_root_domain;
3197
3198         return mono_assembly_get_object (domain, domain->entry_assembly);
3199 }
3200
3201
3202 static MonoReflectionAssembly*
3203 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
3204 {
3205         MonoMethod *m = mono_method_get_last_managed ();
3206         MonoMethod *dest = m;
3207
3208         MONO_ARCH_SAVE_REGS;
3209
3210         mono_stack_walk (get_caller, &dest);
3211         if (!dest)
3212                 dest = m;
3213         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
3214 }
3215
3216 static MonoString *
3217 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
3218 {
3219         MonoDomain *domain = mono_object_domain (object); 
3220         MonoString *res;
3221         gchar *name;
3222
3223         MONO_ARCH_SAVE_REGS;
3224
3225         name = mono_type_get_name (object->type);
3226         res = mono_string_new (domain, name);
3227         g_free (name);
3228
3229         return res;
3230 }
3231
3232 static void
3233 fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *aname, MonoAssemblyName *name, const char *absolute)
3234 {
3235         static MonoMethod *create_culture = NULL;
3236     gpointer args [1];
3237         guint32 pkey_len;
3238         const char *pkey_ptr;
3239         gchar *codebase;
3240
3241         MONO_ARCH_SAVE_REGS;
3242
3243         if (strcmp (name->name, "corlib") == 0)
3244                 aname->name = mono_string_new (domain, "mscorlib");
3245         else
3246                 aname->name = mono_string_new (domain, name->name);
3247
3248         aname->major = name->major;
3249         aname->minor = name->minor;
3250         aname->build = name->build;
3251         aname->revision = name->revision;
3252         aname->hashalg = name->hash_alg;
3253
3254         codebase = g_filename_to_uri (absolute, NULL, NULL);
3255         if (codebase) {
3256                 aname->codebase = mono_string_new (domain, codebase);
3257                 g_free (codebase);
3258         }
3259
3260         if (!create_culture) {
3261                 MonoMethodDesc *desc = mono_method_desc_new ("System.Globalization.CultureInfo:CreateSpecificCulture(string)", TRUE);
3262                 create_culture = mono_method_desc_search_in_image (desc, mono_defaults.corlib);
3263                 g_assert (create_culture);
3264                 mono_method_desc_free (desc);
3265         }
3266
3267         args [0] = mono_string_new (domain, name->culture);
3268         aname->cultureInfo = 
3269                 mono_runtime_invoke (create_culture, NULL, args, NULL);
3270
3271         if (name->public_key) {
3272                 pkey_ptr = name->public_key;
3273                 pkey_len = mono_metadata_decode_blob_size (pkey_ptr, &pkey_ptr);
3274
3275                 aname->publicKey = mono_array_new (domain, mono_defaults.byte_class, pkey_len);
3276                 memcpy (mono_array_addr (aname->publicKey, guint8, 0), pkey_ptr, pkey_len);
3277         }
3278 }
3279
3280 static void
3281 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
3282 {
3283         gchar *absolute;
3284
3285         MONO_ARCH_SAVE_REGS;
3286
3287         absolute = g_build_filename (assembly->assembly->basedir, assembly->assembly->image->module_name, NULL);
3288
3289         fill_reflection_assembly_name (mono_object_domain (assembly), aname, 
3290                                                                    &assembly->assembly->aname, absolute);
3291
3292         g_free (absolute);
3293 }
3294
3295 static void
3296 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname, MonoReflectionAssemblyName *aname)
3297 {
3298         char *filename;
3299         MonoImageOpenStatus status = MONO_IMAGE_OK;
3300         gboolean res;
3301         MonoImage *image;
3302         MonoAssemblyName name;
3303
3304         MONO_ARCH_SAVE_REGS;
3305
3306         filename = mono_string_to_utf8 (fname);
3307
3308         image = mono_image_open (filename, &status);
3309         
3310         if (!image){
3311                 MonoException *exc;
3312
3313                 g_free (filename);
3314                 exc = mono_get_exception_file_not_found (fname);
3315                 mono_raise_exception (exc);
3316         }
3317
3318         res = mono_assembly_fill_assembly_name (image, &name);
3319         if (!res) {
3320                 mono_image_close (image);
3321                 g_free (filename);
3322                 mono_raise_exception (mono_get_exception_argument ("assemblyFile", "The file does not contain a manifest"));
3323         }
3324
3325         fill_reflection_assembly_name (mono_domain_get (), aname, &name, filename);
3326
3327         g_free (filename);
3328         mono_image_close (image);
3329 }
3330
3331 static MonoArray*
3332 mono_module_get_types (MonoDomain *domain, MonoImage *image, 
3333                                            MonoBoolean exportedOnly)
3334 {
3335         MonoArray *res;
3336         MonoClass *klass;
3337         MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
3338         int i, count;
3339         guint32 attrs, visibility;
3340
3341         /* we start the count from 1 because we skip the special type <Module> */
3342         if (exportedOnly) {
3343                 count = 0;
3344                 for (i = 1; i < tdef->rows; ++i) {
3345                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
3346                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3347                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
3348                                 count++;
3349                 }
3350         } else {
3351                 count = tdef->rows - 1;
3352         }
3353         res = mono_array_new (domain, mono_defaults.monotype_class, count);
3354         count = 0;
3355         for (i = 1; i < tdef->rows; ++i) {
3356                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
3357                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3358                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
3359                         klass = mono_class_get (image, (i + 1) | MONO_TOKEN_TYPE_DEF);
3360                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
3361                         count++;
3362                 }
3363         }
3364         
3365         return res;
3366 }
3367
3368 static MonoArray*
3369 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
3370 {
3371         MonoArray *res;
3372         MonoImage *image = assembly->assembly->image;
3373         MonoTableInfo *table = &image->tables [MONO_TABLE_FILE];
3374         MonoDomain *domain;
3375         int i;
3376
3377         MONO_ARCH_SAVE_REGS;
3378
3379         domain = mono_object_domain (assembly);
3380         res = mono_module_get_types (domain, image, exportedOnly);
3381
3382         /* Append data from all modules in the assembly */
3383         for (i = 0; i < table->rows; ++i) {
3384                 if (!(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
3385                         MonoImage *loaded_image = mono_assembly_load_module (image->assembly, i + 1);
3386                         if (loaded_image) {
3387                                 MonoArray *res2 = mono_module_get_types (domain, loaded_image, exportedOnly);
3388                                 /* Append the new types to the end of the array */
3389                                 if (mono_array_length (res2) > 0) {
3390                                         guint32 len1, len2;
3391                                         MonoArray *res3;
3392
3393                                         len1 = mono_array_length (res);
3394                                         len2 = mono_array_length (res2);
3395                                         res3 = mono_array_new (domain, mono_defaults.monotype_class, len1 + len2);
3396                                         memcpy (mono_array_addr (res3, MonoReflectionType*, 0),
3397                                                         mono_array_addr (res, MonoReflectionType*, 0),
3398                                                         len1 * sizeof (MonoReflectionType*));
3399                                         memcpy (mono_array_addr (res3, MonoReflectionType*, len1),
3400                                                         mono_array_addr (res2, MonoReflectionType*, 0),
3401                                                         len2 * sizeof (MonoReflectionType*));
3402                                         res = res3;
3403                                 }
3404                         }
3405                 }
3406         }               
3407
3408         return res;
3409 }
3410
3411 static MonoReflectionType*
3412 ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
3413 {
3414         MonoDomain *domain = mono_object_domain (module); 
3415         MonoClass *klass;
3416
3417         MONO_ARCH_SAVE_REGS;
3418
3419         g_assert (module->image);
3420         klass = mono_class_get (module->image, 1 | MONO_TOKEN_TYPE_DEF);
3421         return mono_type_get_object (domain, &klass->byval_arg);
3422 }
3423
3424 static void
3425 ves_icall_System_Reflection_Module_Close (MonoReflectionModule *module)
3426 {
3427         if (module->image)
3428                 mono_image_close (module->image);
3429 }
3430
3431 static MonoString*
3432 ves_icall_System_Reflection_Module_GetGuidInternal (MonoReflectionModule *module)
3433 {
3434         MonoDomain *domain = mono_object_domain (module); 
3435
3436         MONO_ARCH_SAVE_REGS;
3437
3438         g_assert (module->image);
3439         return mono_string_new (domain, module->image->guid);
3440 }
3441
3442 static MonoArray*
3443 ves_icall_System_Reflection_Module_InternalGetTypes (MonoReflectionModule *module)
3444 {
3445         MONO_ARCH_SAVE_REGS;
3446
3447         if (!module->image)
3448                 return mono_array_new (mono_object_domain (module), mono_defaults.monotype_class, 0);
3449         else
3450                 return mono_module_get_types (mono_object_domain (module), module->image, FALSE);
3451 }
3452
3453 static MonoReflectionType*
3454 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
3455 {
3456         MonoClass *klass;
3457         int isbyref = 0, rank;
3458         char *str = mono_string_to_utf8 (smodifiers);
3459         char *p;
3460
3461         MONO_ARCH_SAVE_REGS;
3462
3463         klass = mono_class_from_mono_type (tb->type.type);
3464         p = str;
3465         /* logic taken from mono_reflection_parse_type(): keep in sync */
3466         while (*p) {
3467                 switch (*p) {
3468                 case '&':
3469                         if (isbyref) { /* only one level allowed by the spec */
3470                                 g_free (str);
3471                                 return NULL;
3472                         }
3473                         isbyref = 1;
3474                         p++;
3475                         g_free (str);
3476                         return mono_type_get_object (mono_object_domain (tb), &klass->this_arg);
3477                         break;
3478                 case '*':
3479                         klass = mono_ptr_class_get (&klass->byval_arg);
3480                         mono_class_init (klass);
3481                         p++;
3482                         break;
3483                 case '[':
3484                         rank = 1;
3485                         p++;
3486                         while (*p) {
3487                                 if (*p == ']')
3488                                         break;
3489                                 if (*p == ',')
3490                                         rank++;
3491                                 else if (*p != '*') { /* '*' means unknown lower bound */
3492                                         g_free (str);
3493                                         return NULL;
3494                                 }
3495                                 ++p;
3496                         }
3497                         if (*p != ']') {
3498                                 g_free (str);
3499                                 return NULL;
3500                         }
3501                         p++;
3502                         klass = mono_array_class_get (klass, rank);
3503                         mono_class_init (klass);
3504                         break;
3505                 default:
3506                         break;
3507                 }
3508         }
3509         g_free (str);
3510         return mono_type_get_object (mono_object_domain (tb), &klass->byval_arg);
3511 }
3512
3513 static MonoBoolean
3514 ves_icall_Type_IsArrayImpl (MonoReflectionType *t)
3515 {
3516         MonoType *type;
3517         MonoBoolean res;
3518
3519         MONO_ARCH_SAVE_REGS;
3520
3521         type = t->type;
3522         res = !type->byref && (type->type == MONO_TYPE_ARRAY || type->type == MONO_TYPE_SZARRAY);
3523
3524         return res;
3525 }
3526
3527 static MonoReflectionType *
3528 ves_icall_Type_make_array_type (MonoReflectionType *type, int rank)
3529 {
3530         MonoClass *klass, *aklass;
3531
3532         MONO_ARCH_SAVE_REGS;
3533
3534         klass = mono_class_from_mono_type (type->type);
3535         aklass = mono_array_class_get (klass, rank);
3536
3537         return mono_type_get_object (mono_object_domain (type), &aklass->byval_arg);
3538 }
3539
3540 static MonoReflectionType *
3541 ves_icall_Type_make_byref_type (MonoReflectionType *type)
3542 {
3543         MonoClass *klass;
3544
3545         MONO_ARCH_SAVE_REGS;
3546
3547         klass = mono_class_from_mono_type (type->type);
3548
3549         return mono_type_get_object (mono_object_domain (type), &klass->this_arg);
3550 }
3551
3552 static MonoObject *
3553 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
3554                                                    MonoReflectionMethod *info)
3555 {
3556         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
3557         MonoObject *delegate;
3558         gpointer func;
3559
3560         MONO_ARCH_SAVE_REGS;
3561
3562         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
3563
3564         delegate = mono_object_new (mono_object_domain (type), delegate_class);
3565
3566         func = mono_compile_method (info->method);
3567
3568         mono_delegate_ctor (delegate, target, func);
3569
3570         return delegate;
3571 }
3572
3573 /*
3574  * Magic number to convert a time which is relative to
3575  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
3576  */
3577 #define EPOCH_ADJUST    ((guint64)62135596800LL)
3578
3579 /*
3580  * Magic number to convert FILETIME base Jan 1, 1601 to DateTime - base Jan, 1, 0001
3581  */
3582 #define FILETIME_ADJUST ((guint64)504911232000000000LL)
3583
3584 /*
3585  * This returns Now in UTC
3586  */
3587 static gint64
3588 ves_icall_System_DateTime_GetNow (void)
3589 {
3590 #ifdef PLATFORM_WIN32
3591         SYSTEMTIME st;
3592         FILETIME ft;
3593         
3594         GetLocalTime (&st);
3595         SystemTimeToFileTime (&st, &ft);
3596         return (gint64) FILETIME_ADJUST + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
3597 #else
3598         /* FIXME: put this in io-layer and call it GetLocalTime */
3599         struct timeval tv;
3600         gint64 res;
3601
3602         MONO_ARCH_SAVE_REGS;
3603
3604         if (gettimeofday (&tv, NULL) == 0) {
3605                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
3606                 return res;
3607         }
3608         /* fixme: raise exception */
3609         return 0;
3610 #endif
3611 }
3612
3613 #ifdef PLATFORM_WIN32
3614 /* convert a SYSTEMTIME which is of the form "last thursday in october" to a real date */
3615 static void
3616 convert_to_absolute_date(SYSTEMTIME *date)
3617 {
3618 #define IS_LEAP(y) ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0))
3619         static int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3620         static int leap_days_in_month[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3621         /* from the calendar FAQ */
3622         int a = (14 - date->wMonth) / 12;
3623         int y = date->wYear - a;
3624         int m = date->wMonth + 12 * a - 2;
3625         int d = (1 + y + y/4 - y/100 + y/400 + (31*m)/12) % 7;
3626
3627         /* d is now the day of the week for the first of the month (0 == Sunday) */
3628
3629         int day_of_week = date->wDayOfWeek;
3630
3631         /* set day_in_month to the first day in the month which falls on day_of_week */    
3632         int day_in_month = 1 + (day_of_week - d);
3633         if (day_in_month <= 0)
3634                 day_in_month += 7;
3635
3636         /* wDay is 1 for first weekday in month, 2 for 2nd ... 5 means last - so work that out allowing for days in the month */
3637         date->wDay = day_in_month + (date->wDay - 1) * 7;
3638         if (date->wDay > (IS_LEAP(date->wYear) ? leap_days_in_month[date->wMonth - 1] : days_in_month[date->wMonth - 1]))
3639                 date->wDay -= 7;
3640 }
3641 #endif
3642
3643 #ifndef PLATFORM_WIN32
3644 /*
3645  * Return's the offset from GMT of a local time.
3646  * 
3647  *  tm is a local time
3648  *  t  is the same local time as seconds.
3649  */
3650 static int 
3651 gmt_offset(struct tm *tm, time_t t)
3652 {
3653 #if defined (HAVE_TM_GMTOFF)
3654         return tm->tm_gmtoff;
3655 #else
3656         struct tm g;
3657         time_t t2;
3658         g = *gmtime(&t);
3659         g.tm_isdst = tm->tm_isdst;
3660         t2 = mktime(&g);
3661         return (int)difftime(t, t2);
3662 #endif
3663 }
3664 #endif
3665 /*
3666  * This is heavily based on zdump.c from glibc 2.2.
3667  *
3668  *  * data[0]:  start of daylight saving time (in DateTime ticks).
3669  *  * data[1]:  end of daylight saving time (in DateTime ticks).
3670  *  * data[2]:  utcoffset (in TimeSpan ticks).
3671  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
3672  *  * name[0]:  name of this timezone when not daylight saving.
3673  *  * name[1]:  name of this timezone when daylight saving.
3674  *
3675  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
3676  *         the class library allows years between 1 and 9999.
3677  *
3678  *  Returns true on success and zero on failure.
3679  */
3680 static guint32
3681 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
3682 {
3683 #ifndef PLATFORM_WIN32
3684         MonoDomain *domain = mono_domain_get ();
3685         struct tm start, tt;
3686         time_t t;
3687
3688         long int gmtoff;
3689         int is_daylight = 0, day;
3690         char tzone [64];
3691
3692         MONO_ARCH_SAVE_REGS;
3693
3694         MONO_CHECK_ARG_NULL (data);
3695         MONO_CHECK_ARG_NULL (names);
3696
3697         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
3698         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
3699
3700         /* 
3701          * no info is better than crashing: we'll need our own tz data to make 
3702          * this work properly, anyway. The range is reduced to 1970 .. 2037 because
3703          * that is what mktime is guaranteed to support (we get into an infinite loop 
3704          * otherwise).
3705          */
3706         if ((year < 1970) || (year > 2037)) {
3707                 t = time (NULL);
3708                 tt = *localtime (&t);
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                 return 1;
3713         }
3714
3715         memset (&start, 0, sizeof (start));
3716
3717         start.tm_mday = 1;
3718         start.tm_year = year-1900;
3719
3720         t = mktime (&start);
3721         gmtoff = gmt_offset (&start, t);
3722
3723         /* For each day of the year, calculate the tm_gmtoff. */
3724         for (day = 0; day < 365; day++) {
3725
3726                 t += 3600*24;
3727                 tt = *localtime (&t);
3728
3729                 /* Daylight saving starts or ends here. */
3730                 if (gmt_offset (&tt, t) != gmtoff) {
3731                         struct tm tt1;
3732                         time_t t1;
3733
3734                         /* Try to find the exact hour when daylight saving starts/ends. */
3735                         t1 = t;
3736                         do {
3737                                 t1 -= 3600;
3738                                 tt1 = *localtime (&t1);
3739                         } while (gmt_offset (&tt1, t1) != gmtoff);
3740
3741                         /* Try to find the exact minute when daylight saving starts/ends. */
3742                         do {
3743                                 t1 += 60;
3744                                 tt1 = *localtime (&t1);
3745                         } while (gmt_offset (&tt1, t1) == gmtoff);
3746                         
3747                         strftime (tzone, sizeof (tzone), "%Z", &tt);
3748                         
3749                         /* Write data, if we're already in daylight saving, we're done. */
3750                         if (is_daylight) {
3751                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
3752                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
3753                                 return 1;
3754                         } else {
3755                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
3756                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
3757                                 is_daylight = 1;
3758                         }
3759
3760                         /* This is only set once when we enter daylight saving. */
3761                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
3762                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (&tt, t) - gmtoff) * 10000000L);
3763
3764                         gmtoff = gmt_offset (&tt, t);
3765                 }
3766         }
3767
3768         if (!is_daylight) {
3769                 strftime (tzone, sizeof (tzone), "%Z", &tt);
3770                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
3771                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
3772                 mono_array_set ((*data), gint64, 0, 0);
3773                 mono_array_set ((*data), gint64, 1, 0);
3774                 mono_array_set ((*data), gint64, 2, (gint64) gmtoff * 10000000L);
3775                 mono_array_set ((*data), gint64, 3, 0);
3776         }
3777
3778         return 1;
3779 #else
3780         MonoDomain *domain = mono_domain_get ();
3781         TIME_ZONE_INFORMATION tz_info;
3782         FILETIME ft;
3783         int i;
3784         int err, tz_id;
3785
3786         tz_id = GetTimeZoneInformation (&tz_info);
3787         if (tz_id == TIME_ZONE_ID_INVALID)
3788                 return 0;
3789
3790         MONO_CHECK_ARG_NULL (data);
3791         MONO_CHECK_ARG_NULL (names);
3792
3793         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
3794         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
3795
3796         for (i = 0; i < 32; ++i)
3797                 if (!tz_info.DaylightName [i])
3798                         break;
3799         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
3800         for (i = 0; i < 32; ++i)
3801                 if (!tz_info.StandardName [i])
3802                         break;
3803         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
3804
3805         if ((year <= 1601) || (year > 30827)) {
3806                 /*
3807                  * According to MSDN, the MS time functions can't handle dates outside
3808                  * this interval.
3809                  */
3810                 return 1;
3811         }
3812
3813         /* even if the timezone has no daylight savings it may have Bias (e.g. GMT+13 it seems) */
3814         if (tz_id != TIME_ZONE_ID_UNKNOWN) {
3815                 tz_info.StandardDate.wYear = year;
3816                 convert_to_absolute_date(&tz_info.StandardDate);
3817                 err = SystemTimeToFileTime (&tz_info.StandardDate, &ft);
3818                 g_assert(err);
3819                 mono_array_set ((*data), gint64, 1, FILETIME_ADJUST + (((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime));
3820                 tz_info.DaylightDate.wYear = year;
3821                 convert_to_absolute_date(&tz_info.DaylightDate);
3822                 err = SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
3823                 g_assert(err);
3824                 mono_array_set ((*data), gint64, 0, FILETIME_ADJUST + (((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime));
3825         }
3826         mono_array_set ((*data), gint64, 2, (tz_info.Bias + tz_info.StandardBias) * -600000000LL);
3827         mono_array_set ((*data), gint64, 3, (tz_info.DaylightBias - tz_info.StandardBias) * -600000000LL);
3828
3829         return 1;
3830 #endif
3831 }
3832
3833 static gpointer
3834 ves_icall_System_Object_obj_address (MonoObject *this) 
3835 {
3836         MONO_ARCH_SAVE_REGS;
3837
3838         return this;
3839 }
3840
3841 /* System.Buffer */
3842
3843 static gint32 
3844 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) 
3845 {
3846         MonoClass *klass;
3847         MonoTypeEnum etype;
3848         int length, esize;
3849         int i;
3850
3851         MONO_ARCH_SAVE_REGS;
3852
3853         klass = array->obj.vtable->klass;
3854         etype = klass->element_class->byval_arg.type;
3855         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
3856                 return -1;
3857
3858         if (array->bounds == NULL)
3859                 length = array->max_length;
3860         else {
3861                 length = 1;
3862                 for (i = 0; i < klass->rank; ++ i)
3863                         length *= array->bounds [i].length;
3864         }
3865
3866         esize = mono_array_element_size (klass);
3867         return length * esize;
3868 }
3869
3870 static gint8 
3871 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) 
3872 {
3873         MONO_ARCH_SAVE_REGS;
3874
3875         return mono_array_get (array, gint8, idx);
3876 }
3877
3878 static void 
3879 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) 
3880 {
3881         MONO_ARCH_SAVE_REGS;
3882
3883         mono_array_set (array, gint8, idx, value);
3884 }
3885
3886 static void 
3887 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) 
3888 {
3889         char *src_buf, *dest_buf;
3890
3891         MONO_ARCH_SAVE_REGS;
3892
3893         src_buf = (gint8 *)src->vector + src_offset;
3894         dest_buf = (gint8 *)dest->vector + dest_offset;
3895
3896         memcpy (dest_buf, src_buf, count);
3897 }
3898
3899 static MonoObject *
3900 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
3901 {
3902         MonoDomain *domain = mono_object_domain (this); 
3903         MonoObject *res;
3904         MonoRealProxy *rp = ((MonoRealProxy *)this);
3905         MonoTransparentProxy *tp;
3906         MonoType *type;
3907         MonoClass *klass;
3908
3909         MONO_ARCH_SAVE_REGS;
3910
3911         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
3912         tp = (MonoTransparentProxy*) res;
3913         
3914         tp->rp = rp;
3915         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
3916         klass = mono_class_from_mono_type (type);
3917
3918         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3919                 tp->klass = mono_defaults.marshalbyrefobject_class;
3920         else
3921                 tp->klass = klass;
3922         
3923         tp->custom_type_info = (mono_object_isinst (this, mono_defaults.iremotingtypeinfo_class) != NULL);
3924
3925         res->vtable = mono_class_proxy_vtable (domain, klass);
3926
3927         return res;
3928 }
3929
3930 /* System.Environment */
3931
3932 static MonoString *
3933 ves_icall_System_Environment_get_MachineName (void)
3934 {
3935 #if defined (PLATFORM_WIN32)
3936         gunichar2 *buf;
3937         guint32 len;
3938         MonoString *result;
3939
3940         len = MAX_COMPUTERNAME_LENGTH + 1;
3941         buf = g_new (gunichar2, len);
3942
3943         result = NULL;
3944         if (GetComputerName (buf, (PDWORD) &len))
3945                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
3946
3947         g_free (buf);
3948         return result;
3949 #else
3950         gchar *buf;
3951         int len;
3952         MonoString *result;
3953
3954         MONO_ARCH_SAVE_REGS;
3955
3956         len = 256;
3957         buf = g_new (gchar, len);
3958
3959         result = NULL;
3960         if (gethostname (buf, len) == 0)
3961                 result = mono_string_new (mono_domain_get (), buf);
3962         
3963         g_free (buf);
3964         return result;
3965 #endif
3966 }
3967
3968 static int
3969 ves_icall_System_Environment_get_Platform (void)
3970 {
3971         MONO_ARCH_SAVE_REGS;
3972
3973 #if defined (PLATFORM_WIN32)
3974         /* Win32NT */
3975         return 2;
3976 #else
3977         /* Unix */
3978         return 128;
3979 #endif
3980 }
3981
3982 static MonoString *
3983 ves_icall_System_Environment_get_NewLine (void)
3984 {
3985         MONO_ARCH_SAVE_REGS;
3986
3987 #if defined (PLATFORM_WIN32)
3988         return mono_string_new (mono_domain_get (), "\r\n");
3989 #else
3990         return mono_string_new (mono_domain_get (), "\n");
3991 #endif
3992 }
3993
3994 static MonoString *
3995 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
3996 {
3997         const gchar *value;
3998         gchar *utf8_name;
3999
4000         MONO_ARCH_SAVE_REGS;
4001
4002         if (name == NULL)
4003                 return NULL;
4004
4005         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
4006         value = g_getenv (utf8_name);
4007         g_free (utf8_name);
4008
4009         if (value == 0)
4010                 return NULL;
4011         
4012         return mono_string_new (mono_domain_get (), value);
4013 }
4014
4015 /*
4016  * There is no standard way to get at environ.
4017  */
4018 #ifndef _MSC_VER
4019 extern
4020 #endif
4021 char **environ;
4022
4023 static MonoArray *
4024 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
4025 {
4026         MonoArray *names;
4027         MonoDomain *domain;
4028         MonoString *str;
4029         gchar **e, **parts;
4030         int n;
4031
4032         MONO_ARCH_SAVE_REGS;
4033
4034         n = 0;
4035         for (e = environ; *e != 0; ++ e)
4036                 ++ n;
4037
4038         domain = mono_domain_get ();
4039         names = mono_array_new (domain, mono_defaults.string_class, n);
4040
4041         n = 0;
4042         for (e = environ; *e != 0; ++ e) {
4043                 parts = g_strsplit (*e, "=", 2);
4044                 if (*parts != 0) {
4045                         str = mono_string_new (domain, *parts);
4046                         mono_array_set (names, MonoString *, n, str);
4047                 }
4048
4049                 g_strfreev (parts);
4050
4051                 ++ n;
4052         }
4053
4054         return names;
4055 }
4056
4057 /*
4058  * Returns the number of milliseconds elapsed since the system started.
4059  */
4060 static gint32
4061 ves_icall_System_Environment_get_TickCount (void)
4062 {
4063 #if defined (PLATFORM_WIN32)
4064         return GetTickCount();
4065 #else
4066         struct timeval tv;
4067         struct timezone tz;
4068         gint32 res;
4069
4070         MONO_ARCH_SAVE_REGS;
4071
4072         res = (gint32) gettimeofday (&tv, &tz);
4073
4074         if (res != -1)
4075                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
4076         return res;
4077 #endif
4078 }
4079
4080
4081 static void
4082 ves_icall_System_Environment_Exit (int result)
4083 {
4084         MONO_ARCH_SAVE_REGS;
4085
4086         mono_runtime_quit ();
4087
4088         /* we may need to do some cleanup here... */
4089         exit (result);
4090 }
4091
4092 static MonoString*
4093 ves_icall_System_Environment_GetGacPath (void)
4094 {
4095         return mono_string_new (mono_domain_get (), MONO_ASSEMBLIES);
4096 }
4097
4098 static MonoString*
4099 ves_icall_System_Text_Encoding_InternalCodePage (void) 
4100 {
4101         const char *cset;
4102
4103         MONO_ARCH_SAVE_REGS;
4104
4105         g_get_charset (&cset);
4106         /* g_print ("charset: %s\n", cset); */
4107         /* handle some common aliases */
4108         switch (*cset) {
4109         case 'A':
4110                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
4111                         cset = "us-ascii";
4112                 break;
4113         }
4114         return mono_string_new (mono_domain_get (), cset);
4115 }
4116
4117 static MonoBoolean
4118 ves_icall_System_Environment_get_HasShutdownStarted (void)
4119 {
4120         if (mono_runtime_is_shutting_down ())
4121                 return TRUE;
4122
4123         if (mono_domain_is_unloading (mono_domain_get ()))
4124                 return TRUE;
4125
4126         return FALSE;
4127 }
4128
4129 static void
4130 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
4131                                          MonoReflectionMethod *method,
4132                                          MonoArray *out_args)
4133 {
4134         MONO_ARCH_SAVE_REGS;
4135
4136         mono_message_init (mono_object_domain (this), this, method, out_args);
4137 }
4138
4139 static MonoBoolean
4140 ves_icall_IsTransparentProxy (MonoObject *proxy)
4141 {
4142         MONO_ARCH_SAVE_REGS;
4143
4144         if (!proxy)
4145                 return 0;
4146
4147         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
4148                 return 1;
4149
4150         return 0;
4151 }
4152
4153 static void
4154 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
4155 {
4156         MonoClass *klass;
4157         MonoVTable* vtable;
4158
4159         MONO_ARCH_SAVE_REGS;
4160
4161         klass = mono_class_from_mono_type (type->type);
4162         vtable = mono_class_vtable (mono_domain_get (), klass);
4163
4164         if (enable) vtable->remote = 1;
4165         else vtable->remote = 0;
4166 }
4167
4168 static MonoObject *
4169 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionType *type)
4170 {
4171         MonoClass *klass;
4172         MonoDomain *domain;
4173         
4174         MONO_ARCH_SAVE_REGS;
4175
4176         domain = mono_object_domain (type);
4177         klass = mono_class_from_mono_type (type->type);
4178
4179         if (klass->rank >= 1) {
4180                 g_assert (klass->rank == 1);
4181                 return (MonoObject *) mono_array_new (domain, klass->element_class, 0);
4182         } else {
4183                 // Bypass remoting object creation check
4184                 return mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
4185         }
4186 }
4187
4188 static MonoString *
4189 ves_icall_System_IO_get_temp_path (void)
4190 {
4191         MONO_ARCH_SAVE_REGS;
4192
4193         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
4194 }
4195
4196 static gpointer
4197 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
4198 {
4199         MONO_ARCH_SAVE_REGS;
4200
4201         return mono_compile_method (method);
4202 }
4203
4204 char const * mono_cfg_dir = "";
4205
4206 void    
4207 mono_install_get_config_dir (void)
4208 {
4209 #ifdef PLATFORM_WIN32
4210   int i;
4211 #endif
4212
4213   mono_cfg_dir = getenv ("MONO_CFG_DIR");
4214
4215   if (!mono_cfg_dir) {
4216 #ifndef PLATFORM_WIN32
4217     mono_cfg_dir = MONO_CFG_DIR;
4218 #else
4219     mono_cfg_dir = g_strdup (MONO_CFG_DIR);
4220     for (i = strlen (mono_cfg_dir) - 1; i >= 0; i--) {
4221         if (mono_cfg_dir [i] == '/')
4222             ((char*) mono_cfg_dir) [i] = '\\';
4223     }
4224 #endif
4225   }
4226 }
4227
4228
4229 static MonoString *
4230 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
4231 {
4232         MonoString *mcpath;
4233         gchar *path;
4234
4235         MONO_ARCH_SAVE_REGS;
4236
4237         path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", "machine.config", NULL);
4238
4239 #if defined (PLATFORM_WIN32)
4240         /* Avoid mixing '/' and '\\' */
4241         {
4242                 gint i;
4243                 for (i = strlen (path) - 1; i >= 0; i--)
4244                         if (path [i] == '/')
4245                                 path [i] = '\\';
4246         }
4247 #endif
4248         mcpath = mono_string_new (mono_domain_get (), path);
4249         g_free (path);
4250
4251         return mcpath;
4252 }
4253
4254 static MonoString *
4255 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (void)
4256 {
4257         MonoString *ipath;
4258         gchar *path;
4259
4260         MONO_ARCH_SAVE_REGS;
4261
4262         path = g_path_get_dirname (mono_cfg_dir);
4263
4264 #if defined (PLATFORM_WIN32)
4265         /* Avoid mixing '/' and '\\' */
4266         {
4267                 gint i;
4268                 for (i = strlen (path) - 1; i >= 0; i--)
4269                         if (path [i] == '/')
4270                                 path [i] = '\\';
4271         }
4272 #endif
4273         ipath = mono_string_new (mono_domain_get (), path);
4274         g_free (path);
4275
4276         return ipath;
4277 }
4278
4279 static void
4280 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (MonoString *message)
4281 {
4282 #if defined (PLATFORM_WIN32)
4283         static void (*output_debug) (gchar *);
4284         static gboolean tried_loading = FALSE;
4285
4286         MONO_ARCH_SAVE_REGS;
4287
4288         if (!tried_loading && output_debug == NULL) {
4289                 GModule *k32;
4290
4291                 tried_loading = TRUE;
4292                 k32 = g_module_open ("kernel32", G_MODULE_BIND_LAZY);
4293                 if (!k32) {
4294                         gchar *error = g_strdup (g_module_error ());
4295                         g_warning ("Failed to load kernel32.dll: %s\n", error);
4296                         g_free (error);
4297                         return;
4298                 }
4299
4300                 g_module_symbol (k32, "OutputDebugStringW", (gpointer *) &output_debug);
4301                 if (!output_debug) {
4302                         gchar *error = g_strdup (g_module_error ());
4303                         g_warning ("Failed to load OutputDebugStringW: %s\n", error);
4304                         g_free (error);
4305                         return;
4306                 }
4307         }
4308
4309         if (output_debug == NULL)
4310                 return;
4311         
4312         output_debug (mono_string_chars (message));
4313 #else
4314         g_warning ("WriteWindowsDebugString called and PLATFORM_WIN32 not defined!\n");
4315 #endif
4316 }
4317
4318 /* Only used for value types */
4319 static MonoObject *
4320 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionType *type)
4321 {
4322         MonoClass *klass;
4323         MonoDomain *domain;
4324         
4325         MONO_ARCH_SAVE_REGS;
4326
4327         domain = mono_object_domain (type);
4328         klass = mono_class_from_mono_type (type->type);
4329
4330         return mono_object_new (domain, klass);
4331 }
4332
4333 static MonoReflectionMethod *
4334 ves_icall_MonoMethod_get_base_definition (MonoReflectionMethod *m)
4335 {
4336         MonoClass *klass;
4337         MonoMethod *method = m->method;
4338         MonoMethod *result = NULL;
4339
4340         MONO_ARCH_SAVE_REGS;
4341
4342         if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
4343              method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ||
4344              method->flags & METHOD_ATTRIBUTE_NEW_SLOT)
4345                 return m;
4346
4347         if (method->klass == NULL || (klass = method->klass->parent) == NULL)
4348                 return m;
4349
4350         while (result == NULL && klass != NULL && (klass->vtable_size > method->slot))
4351         {
4352                 result = klass->vtable [method->slot];
4353                 if (result == NULL) {
4354                         /* It is an abstract method */
4355                         int i;
4356                         for (i=0; i<klass->method.count; i++) {
4357                                 if (klass->methods [i]->slot == method->slot) {
4358                                         result = klass->methods [i];
4359                                         break;
4360                                 }
4361                         }
4362                 }
4363                 klass = klass->parent;
4364         }
4365
4366         if (result == NULL)
4367                 return m;
4368
4369         return mono_method_get_object (mono_domain_get (), result, NULL);
4370 }
4371
4372 static void
4373 mono_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
4374 {
4375         MONO_ARCH_SAVE_REGS;
4376
4377         iter->sig = *(MonoMethodSignature**)argsp;
4378         
4379         g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
4380         g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
4381
4382         iter->next_arg = 0;
4383         /* FIXME: it's not documented what start is exactly... */
4384         iter->args = start? start: argsp + sizeof (gpointer);
4385         iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
4386
4387         // g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos);
4388 }
4389
4390 static MonoTypedRef
4391 mono_ArgIterator_IntGetNextArg (MonoArgIterator *iter)
4392 {
4393         gint i, align, arg_size;
4394         MonoTypedRef res;
4395         MONO_ARCH_SAVE_REGS;
4396
4397         i = iter->sig->sentinelpos + iter->next_arg;
4398
4399         g_assert (i < iter->sig->param_count);
4400
4401         res.type = iter->sig->params [i];
4402         res.klass = mono_class_from_mono_type (res.type);
4403         /* FIXME: endianess issue... */
4404         res.value = iter->args;
4405         arg_size = mono_type_stack_size (res.type, &align);
4406         iter->args = (char*)iter->args + arg_size;
4407         iter->next_arg++;
4408
4409         //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
4410
4411         return res;
4412 }
4413
4414 static MonoTypedRef
4415 mono_ArgIterator_IntGetNextArgT (MonoArgIterator *iter, MonoType *type)
4416 {
4417         gint i, align, arg_size;
4418         MonoTypedRef res;
4419         MONO_ARCH_SAVE_REGS;
4420
4421         i = iter->sig->sentinelpos + iter->next_arg;
4422
4423         g_assert (i < iter->sig->param_count);
4424
4425         while (i < iter->sig->param_count) {
4426                 if (!mono_metadata_type_equal (type, iter->sig->params [i]))
4427                         continue;
4428                 res.type = iter->sig->params [i];
4429                 res.klass = mono_class_from_mono_type (res.type);
4430                 /* FIXME: endianess issue... */
4431                 res.value = iter->args;
4432                 arg_size = mono_type_stack_size (res.type, &align);
4433                 iter->args = (char*)iter->args + arg_size;
4434                 iter->next_arg++;
4435                 //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
4436                 return res;
4437         }
4438         //g_print ("arg type 0x%02x not found\n", res.type->type);
4439
4440         res.type = NULL;
4441         res.value = NULL;
4442         res.klass = NULL;
4443         return res;
4444 }
4445
4446 static MonoType*
4447 mono_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
4448 {
4449         gint i;
4450         MONO_ARCH_SAVE_REGS;
4451         
4452         i = iter->sig->sentinelpos + iter->next_arg;
4453
4454         g_assert (i < iter->sig->param_count);
4455
4456         return iter->sig->params [i];
4457 }
4458
4459 static MonoObject*
4460 mono_TypedReference_ToObject (MonoTypedRef tref)
4461 {
4462         MONO_ARCH_SAVE_REGS;
4463
4464         if (MONO_TYPE_IS_REFERENCE (tref.type)) {
4465                 MonoObject** objp = tref.value;
4466                 return *objp;
4467         }
4468
4469         return mono_value_box (mono_domain_get (), tref.klass, tref.value);
4470 }
4471
4472 static void
4473 prelink_method (MonoMethod *method)
4474 {
4475         const char *exc_class, *exc_arg;
4476         if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
4477                 return;
4478         mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
4479         if (exc_class) {
4480                 mono_raise_exception( 
4481                         mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg ) );
4482         }
4483         /* create the wrapper, too? */
4484 }
4485
4486 static void
4487 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethod *method)
4488 {
4489         MONO_ARCH_SAVE_REGS;
4490         prelink_method (method->method);
4491 }
4492
4493 static void
4494 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionType *type)
4495 {
4496         MonoClass *klass = mono_class_from_mono_type (type->type);
4497         int i;
4498         MONO_ARCH_SAVE_REGS;
4499
4500         mono_class_init (klass);
4501         for (i = 0; i < klass->method.count; ++i)
4502                 prelink_method (klass->methods [i]);
4503 }
4504
4505 /* icall map */
4506
4507 static gconstpointer icall_map [] = {
4508         /*
4509          * System.Array
4510          */
4511         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
4512         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
4513         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
4514         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
4515         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
4516         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
4517         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
4518         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
4519         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
4520         "System.Array::Clone",            mono_array_clone,
4521
4522         /*
4523          * System.ArgIterator
4524          */
4525         "System.ArgIterator::Setup",                            mono_ArgIterator_Setup,
4526         "System.ArgIterator::IntGetNextArg()",                  mono_ArgIterator_IntGetNextArg,
4527         "System.ArgIterator::IntGetNextArg(intptr)", mono_ArgIterator_IntGetNextArgT,
4528         "System.ArgIterator::IntGetNextArgType",                mono_ArgIterator_IntGetNextArgType,
4529
4530         /*
4531          * System.TypedReference
4532          */
4533         "System.TypedReference::ToObject",                      mono_TypedReference_ToObject,
4534
4535         /*
4536          * System.Object
4537          */
4538         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
4539         "System.Object::GetType", ves_icall_System_Object_GetType,
4540         "System.Object::InternalGetHashCode", ves_icall_System_Object_GetHashCode,
4541         "System.Object::obj_address", ves_icall_System_Object_obj_address,
4542
4543         /*
4544          * System.ValueType
4545          */
4546         "System.ValueType::InternalGetHashCode", ves_icall_System_ValueType_InternalGetHashCode,
4547         "System.ValueType::InternalEquals", ves_icall_System_ValueType_Equals,
4548
4549         /*
4550          * System.String
4551          */
4552         
4553         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
4554         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
4555         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
4556         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
4557         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
4558         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
4559         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
4560         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
4561         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
4562         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
4563         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
4564         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
4565         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
4566         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
4567         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
4568         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
4569         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
4570         "System.String::InternalPad", ves_icall_System_String_InternalPad,
4571         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
4572         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
4573         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
4574         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
4575         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
4576         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
4577         "System.String::get_Chars", ves_icall_System_String_get_Chars,
4578
4579         /*
4580          * System.AppDomain
4581          */
4582         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
4583         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
4584         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
4585         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
4586         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
4587         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
4588         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
4589         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
4590         "System.AppDomain::LoadAssemblyRaw", ves_icall_System_AppDomain_LoadAssemblyRaw,
4591         "System.AppDomain::InternalIsFinalizingForUnload", ves_icall_System_AppDomain_InternalIsFinalizingForUnload,
4592         "System.AppDomain::InternalUnload", ves_icall_System_AppDomain_InternalUnload,
4593         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
4594         "System.AppDomain::InternalSetDomain", ves_icall_System_AppDomain_InternalSetDomain,
4595         "System.AppDomain::InternalSetDomainByID", ves_icall_System_AppDomain_InternalSetDomainByID,
4596         "System.AppDomain::InternalPushDomainRef", ves_icall_System_AppDomain_InternalPushDomainRef,
4597         "System.AppDomain::InternalPushDomainRefByID", ves_icall_System_AppDomain_InternalPushDomainRefByID,
4598         "System.AppDomain::InternalPopDomainRef", ves_icall_System_AppDomain_InternalPopDomainRef,
4599         "System.AppDomain::InternalSetContext", ves_icall_System_AppDomain_InternalSetContext,
4600         "System.AppDomain::InternalGetContext", ves_icall_System_AppDomain_InternalGetContext,
4601         "System.AppDomain::InternalGetDefaultContext", ves_icall_System_AppDomain_InternalGetDefaultContext,
4602         "System.AppDomain::InternalGetProcessGuid", ves_icall_System_AppDomain_InternalGetProcessGuid,
4603
4604         /*
4605          * System.AppDomainSetup
4606          */
4607         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
4608
4609         /*
4610          * System.Double
4611          */
4612         "System.Double::ParseImpl",    mono_double_ParseImpl,
4613         "System.Double::AssertEndianity", ves_icall_System_Double_AssertEndianity,
4614
4615         /*
4616          * System.Decimal
4617          */
4618         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
4619         "System.Decimal::decimal2Int64", mono_decimal2Int64,
4620         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
4621         "System.Decimal::decimalIncr", mono_decimalIncr,
4622         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
4623         "System.Decimal::decimal2double", mono_decimal2double,
4624         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
4625         "System.Decimal::decimalRound", mono_decimalRound,
4626         "System.Decimal::decimalMult", mono_decimalMult,
4627         "System.Decimal::decimalDiv", mono_decimalDiv,
4628         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
4629         "System.Decimal::decimalCompare", mono_decimalCompare,
4630         "System.Decimal::string2decimal", mono_string2decimal,
4631         "System.Decimal::decimal2string", mono_decimal2string,
4632
4633         /*
4634          * ModuleBuilder
4635          */
4636         "System.Reflection.Emit.ModuleBuilder::getUSIndex", mono_image_insert_string,
4637         "System.Reflection.Emit.ModuleBuilder::getToken", ves_icall_ModuleBuilder_getToken,
4638         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
4639         "System.Reflection.Emit.ModuleBuilder::basic_init", mono_image_module_basic_init,
4640         "System.Reflection.Emit.ModuleBuilder::build_metadata", ves_icall_ModuleBuilder_build_metadata,
4641         "System.Reflection.Emit.ModuleBuilder::getDataChunk", ves_icall_ModuleBuilder_getDataChunk,
4642         
4643         /*
4644          * AssemblyBuilder
4645          */
4646         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
4647         "System.Reflection.Emit.AssemblyBuilder::InternalAddModule", mono_image_load_module,
4648
4649         /*
4650          * Reflection stuff.
4651          */
4652         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
4653         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
4654         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
4655         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
4656         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
4657         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
4658         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
4659         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
4660         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
4661         "System.Reflection.MonoField::GetParentType", ves_icall_MonoField_GetParentType,
4662         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
4663         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
4664         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
4665         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
4666
4667         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
4668         "System.Reflection.MonoMethod::get_base_definition", ves_icall_MonoMethod_get_base_definition,
4669         
4670         /* System.Enum */
4671
4672         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
4673         "System.Enum::get_value", ves_icall_System_Enum_get_value,
4674         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
4675
4676         /*
4677          * TypeBuilder
4678          */
4679         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
4680         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
4681         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
4682         "System.Reflection.Emit.TypeBuilder::setup_generic_class", mono_reflection_setup_generic_class,
4683
4684         /*
4685          * DynamicMethod
4686          */
4687         "System.Reflection.Emit.DynamicMethod::create_dynamic_method", mono_reflection_create_dynamic_method,
4688
4689         /*
4690          * TypeBuilder generics icalls.
4691          */
4692         "System.Reflection.Emit.TypeBuilder::get_IsGenericParameter", ves_icall_TypeBuilder_get_IsGenericParameter,
4693         "System.Reflection.Emit.TypeBuilder::define_generic_parameter", ves_icall_TypeBuilder_define_generic_parameter,
4694         
4695         /*
4696          * MethodBuilder generic icalls.
4697          */
4698         "System.Reflection.Emit.MethodBuilder::define_generic_parameter", ves_icall_MethodBuilder_define_generic_parameter,
4699
4700         /*
4701          * MonoGenericInst generic icalls.
4702          */
4703         "System.Reflection.MonoGenericInst::GetParentType", ves_icall_MonoGenericInst_GetParentType,
4704         "System.Reflection.MonoGenericInst::GetInterfaces_internal", ves_icall_MonoGenericInst_GetInterfaces,
4705         "System.Reflection.MonoGenericInst::inflate_method", mono_reflection_inflate_method_or_ctor,
4706         "System.Reflection.MonoGenericInst::inflate_ctor", mono_reflection_inflate_method_or_ctor,
4707         "System.Reflection.MonoGenericInst::inflate_field", mono_reflection_inflate_field,
4708         "System.Reflection.MonoGenericParam::initialize", ves_icall_MonoGenericParam_initialize,
4709         
4710         /*
4711          * System.Type
4712          */
4713         "System.Type::internal_from_name", ves_icall_type_from_name,
4714         "System.Type::internal_from_handle", ves_icall_type_from_handle,
4715         "System.MonoType::get_attributes", ves_icall_get_attributes,
4716         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
4717         "System.Type::type_is_assignable_from", ves_icall_type_is_assignable_from,
4718         "System.Type::IsInstanceOfType", ves_icall_type_IsInstanceOfType,
4719         "System.Type::Equals", ves_icall_type_Equals,
4720         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
4721         "System.Type::GetInterfaceMapData", ves_icall_Type_GetInterfaceMapData,
4722         "System.Type::IsArrayImpl", ves_icall_Type_IsArrayImpl,
4723         "System.Type::make_array_type", ves_icall_Type_make_array_type,
4724         "System.Type::make_byref_type", ves_icall_Type_make_byref_type,
4725
4726         /* Type generics icalls */
4727         "System.Type::GetGenericArguments", ves_icall_Type_GetGenericArguments,
4728         "System.Type::GetGenericParameterPosition", ves_icall_Type_GetGenericParameterPosition,
4729         "System.Type::get_IsGenericTypeDefinition", ves_icall_Type_get_IsGenericTypeDefinition,
4730         "System.Type::GetGenericTypeDefinition_impl", ves_icall_Type_GetGenericTypeDefinition_impl,
4731         "System.Type::BindGenericParameters", ves_icall_Type_BindGenericParameters,
4732         "System.Type::get_IsGenericInstance", ves_icall_Type_get_IsGenericInstance,
4733         
4734         "System.MonoType::get_HasGenericArguments", ves_icall_MonoType_get_HasGenericArguments,
4735         "System.MonoType::get_IsGenericParameter", ves_icall_MonoType_get_IsGenericParameter,
4736         "System.MonoType::get_DeclaringMethod", ves_icall_MonoType_get_DeclaringMethod,
4737
4738         /* Method generics icalls */
4739         "System.Reflection.MethodInfo::get_IsGenericMethodDefinition", ves_icall_MethodInfo_get_IsGenericMethodDefinition,
4740         "System.Reflection.MethodInfo::BindGenericParameters", mono_reflection_bind_generic_method_parameters,
4741         "System.Reflection.MonoMethod::GetGenericArguments", ves_icall_MonoMethod_GetGenericArguments,
4742
4743
4744         /*
4745          * System.Reflection.FieldInfo
4746          */
4747         "System.Reflection.FieldInfo::internal_from_handle", ves_icall_System_Reflection_FieldInfo_internal_from_handle,
4748
4749         /*
4750          * System.Runtime.CompilerServices.RuntimeHelpers
4751          */
4752         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
4753         "System.Runtime.CompilerServices.RuntimeHelpers::GetOffsetToStringData", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData,
4754         "System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue,
4755         "System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor,
4756         
4757         /*
4758          * System.Threading
4759          */
4760         "System.Threading.Thread::Abort_internal(object)", ves_icall_System_Threading_Thread_Abort,
4761         "System.Threading.Thread::ResetAbort_internal()", ves_icall_System_Threading_Thread_ResetAbort,
4762         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
4763         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
4764         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
4765         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
4766         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
4767         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
4768         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
4769         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
4770         "System.Threading.Thread::GetDomainID", ves_icall_System_Threading_Thread_GetDomainID,
4771         "System.Threading.Thread::GetName_internal", ves_icall_System_Threading_Thread_GetName_internal,
4772         "System.Threading.Thread::SetName_internal", ves_icall_System_Threading_Thread_SetName_internal,
4773         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
4774         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
4775         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
4776         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
4777         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
4778         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
4779         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
4780         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
4781         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
4782         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
4783         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
4784         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
4785         "System.Threading.NativeEventCalls::CloseEvent_internal", ves_icall_System_Threading_Events_CloseEvent_internal,
4786         "System.Threading.ThreadPool::GetAvailableThreads", ves_icall_System_Threading_ThreadPool_GetAvailableThreads,
4787         "System.Threading.ThreadPool::GetMaxThreads", ves_icall_System_Threading_ThreadPool_GetMaxThreads,
4788         "System.Threading.Thread::VolatileRead(byte&)", ves_icall_System_Threading_Thread_VolatileRead1,
4789         "System.Threading.Thread::VolatileRead(double&)", ves_icall_System_Threading_Thread_VolatileRead8,
4790         "System.Threading.Thread::VolatileRead(short&)", ves_icall_System_Threading_Thread_VolatileRead2,
4791         "System.Threading.Thread::VolatileRead(int&)", ves_icall_System_Threading_Thread_VolatileRead4,
4792         "System.Threading.Thread::VolatileRead(long&)", ves_icall_System_Threading_Thread_VolatileRead8,
4793         "System.Threading.Thread::VolatileRead(IntPtr&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4794         "System.Threading.Thread::VolatileRead(object&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4795         "System.Threading.Thread::VolatileRead(sbyte&)", ves_icall_System_Threading_Thread_VolatileRead1,
4796         "System.Threading.Thread::VolatileRead(float&)", ves_icall_System_Threading_Thread_VolatileRead4,
4797         "System.Threading.Thread::VolatileRead(ushort&)", ves_icall_System_Threading_Thread_VolatileRead2,
4798         "System.Threading.Thread::VolatileRead(uint&)", ves_icall_System_Threading_Thread_VolatileRead2,
4799         "System.Threading.Thread::VolatileRead(ulong&)", ves_icall_System_Threading_Thread_VolatileRead8,
4800         "System.Threading.Thread::VolatileRead(UIntPtr&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4801         "System.Threading.Thread::VolatileWrite(byte&,byte)", ves_icall_System_Threading_Thread_VolatileWrite1,
4802         "System.Threading.Thread::VolatileWrite(double&,double)", ves_icall_System_Threading_Thread_VolatileWrite8,
4803         "System.Threading.Thread::VolatileWrite(short&,short)", ves_icall_System_Threading_Thread_VolatileWrite2,
4804         "System.Threading.Thread::VolatileWrite(int&,int)", ves_icall_System_Threading_Thread_VolatileWrite4,
4805         "System.Threading.Thread::VolatileWrite(long&,long)", ves_icall_System_Threading_Thread_VolatileWrite8,
4806         "System.Threading.Thread::VolatileWrite(IntPtr&,IntPtr)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4807         "System.Threading.Thread::VolatileWrite(object&,object)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4808         "System.Threading.Thread::VolatileWrite(sbyte&,sbyte)", ves_icall_System_Threading_Thread_VolatileWrite1,
4809         "System.Threading.Thread::VolatileWrite(float&,float)", ves_icall_System_Threading_Thread_VolatileWrite4,
4810         "System.Threading.Thread::VolatileWrite(ushort&,ushort)", ves_icall_System_Threading_Thread_VolatileWrite2,
4811         "System.Threading.Thread::VolatileWrite(uint&,uint)", ves_icall_System_Threading_Thread_VolatileWrite2,
4812         "System.Threading.Thread::VolatileWrite(ulong&,ulong)", ves_icall_System_Threading_Thread_VolatileWrite8,
4813         "System.Threading.Thread::VolatileWrite(UIntPtr&,UIntPtr)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4814
4815         /*
4816          * System.Threading.WaitHandle
4817          */
4818         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
4819         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
4820         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
4821
4822         /*
4823          * System.Runtime.InteropServices.Marshal
4824          */
4825         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
4826         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
4827         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
4828         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
4829         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
4830         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
4831         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
4832         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
4833         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
4834         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
4835
4836         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
4837         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
4838         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
4839         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
4840         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
4841         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
4842         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
4843
4844         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
4845         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
4846         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
4847         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
4848         "System.Runtime.InteropServices.Marshal::AllocCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem,
4849         "System.Runtime.InteropServices.Marshal::FreeCoTaskMem", ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem,
4850         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
4851         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
4852         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
4853         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
4854         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
4855         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
4856         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
4857         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
4858         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
4859         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
4860         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
4861         "System.Runtime.InteropServices.Marshal::Prelink", ves_icall_System_Runtime_InteropServices_Marshal_Prelink,
4862         "System.Runtime.InteropServices.Marshal::PrelinkAll", ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll,
4863
4864
4865         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
4866         "System.Reflection.Assembly::InternalGetType", ves_icall_System_Reflection_Assembly_InternalGetType,
4867         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
4868         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
4869         "System.Reflection.Assembly::InternalGetAssemblyName", ves_icall_System_Reflection_Assembly_InternalGetAssemblyName,
4870         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
4871         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
4872         "System.Reflection.Assembly::InternalImageRuntimeVersion", ves_icall_System_Reflection_Assembly_InternalImageRuntimeVersion,
4873         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
4874         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
4875         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
4876         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
4877         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
4878         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
4879         "System.Reflection.Assembly::GetManifestResourceInfoInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal,
4880         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
4881         "System.Reflection.Assembly::GetReferencedAssemblies", ves_icall_System_Reflection_Assembly_GetReferencedAssemblies,
4882         "System.Reflection.Assembly::GetNamespaces", ves_icall_System_Reflection_Assembly_GetNamespaces,
4883         "System.Reflection.Assembly::GetModulesInternal", ves_icall_System_Reflection_Assembly_GetModulesInternal,
4884
4885         /*
4886          * System.Reflection.Module
4887          */
4888         "System.Reflection.Module::GetGlobalType", ves_icall_System_Reflection_Module_GetGlobalType,
4889         "System.Reflection.Module::Close", ves_icall_System_Reflection_Module_Close,
4890         "System.Reflection.Module::GetGuidInternal", ves_icall_System_Reflection_Module_GetGuidInternal,
4891         "System.Reflection.Module::InternalGetTypes", ves_icall_System_Reflection_Module_InternalGetTypes,
4892
4893         /*
4894          * System.MonoType.
4895          */
4896         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
4897         "System.MonoType::type_from_obj", mono_type_type_from_obj,
4898         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
4899         "System.MonoType::GetArrayRank", ves_icall_MonoType_GetArrayRank,
4900         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
4901         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
4902         "System.MonoType::get_Assembly", ves_icall_MonoType_get_Assembly,
4903         "System.MonoType::get_DeclaringType", ves_icall_MonoType_get_DeclaringType,
4904         "System.MonoType::get_UnderlyingSystemType", ves_icall_MonoType_get_UnderlyingSystemType,
4905         "System.MonoType::get_Name", ves_icall_MonoType_get_Name,
4906         "System.MonoType::get_Namespace", ves_icall_MonoType_get_Namespace,
4907         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
4908         "System.MonoType::IsPrimitiveImpl", ves_icall_type_isprimitive,
4909         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
4910         "System.MonoType::GetField", ves_icall_Type_GetField,
4911         "System.MonoType::GetFields", ves_icall_Type_GetFields,
4912         "System.MonoType::GetMethodsByName", ves_icall_Type_GetMethodsByName,
4913         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
4914         "System.MonoType::GetPropertiesByName", ves_icall_Type_GetPropertiesByName,
4915         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
4916         "System.MonoType::InternalGetEvent", ves_icall_MonoType_GetEvent,
4917         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
4918         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
4919         "System.MonoType::GetNestedType", ves_icall_Type_GetNestedType,
4920
4921         /*
4922          * System.Net.Sockets I/O Services
4923          */
4924         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
4925         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
4926         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
4927         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
4928         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
4929         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
4930         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
4931         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
4932         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
4933         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
4934         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
4935         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
4936         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
4937         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
4938         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
4939         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
4940         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
4941         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
4942         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
4943         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
4944         "System.Net.Dns::GetHostByName_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByName_internal,
4945         "System.Net.Dns::GetHostByAddr_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByAddr_internal,
4946         "System.Net.Dns::GetHostName_internal(string&)", ves_icall_System_Net_Dns_GetHostName_internal,
4947
4948         /*
4949          * System.Char
4950          */
4951         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
4952         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
4953         "System.Char::IsControl", ves_icall_System_Char_IsControl,
4954         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
4955         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
4956         "System.Char::IsLower", ves_icall_System_Char_IsLower,
4957         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
4958         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
4959         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
4960         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
4961         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
4962         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
4963         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
4964         "System.Char::ToLower", ves_icall_System_Char_ToLower,
4965         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
4966
4967         /*
4968          * System.Text.Encoding
4969          */
4970         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
4971
4972         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
4973         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
4974
4975         /*
4976          * System.GC
4977          */
4978         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
4979         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
4980         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
4981         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
4982         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
4983         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
4984         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
4985         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
4986         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
4987         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
4988
4989         /*
4990          * System.Security.Cryptography calls
4991          */
4992
4993          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes,
4994         
4995         /*
4996          * System.Buffer
4997          */
4998         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
4999         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
5000         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
5001         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
5002         
5003         /*
5004          * System.IO.MonoIO
5005          */
5006         "System.IO.MonoIO::CreateDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CreateDirectory,
5007         "System.IO.MonoIO::RemoveDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_RemoveDirectory,
5008         "System.IO.MonoIO::FindFirstFile(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindFirstFile,
5009         "System.IO.MonoIO::FindNextFile(intptr,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindNextFile,
5010         "System.IO.MonoIO::FindClose(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindClose,
5011         "System.IO.MonoIO::GetCurrentDirectory(System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
5012         "System.IO.MonoIO::SetCurrentDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
5013         "System.IO.MonoIO::MoveFile(string,string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_MoveFile,
5014         "System.IO.MonoIO::CopyFile(string,string,bool,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CopyFile,
5015         "System.IO.MonoIO::DeleteFile(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_DeleteFile,
5016         "System.IO.MonoIO::GetFileAttributes(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileAttributes,
5017         "System.IO.MonoIO::SetFileAttributes(string,System.IO.FileAttributes,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileAttributes,
5018         "System.IO.MonoIO::GetFileType(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileType,
5019         "System.IO.MonoIO::GetFileStat(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileStat,
5020         "System.IO.MonoIO::Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open,
5021         "System.IO.MonoIO::Close(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Close,
5022         "System.IO.MonoIO::Read(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Read,
5023         "System.IO.MonoIO::Write(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Write,
5024         "System.IO.MonoIO::Seek(intptr,long,System.IO.SeekOrigin,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Seek,
5025         "System.IO.MonoIO::GetLength(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetLength,
5026         "System.IO.MonoIO::SetLength(intptr,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetLength,
5027         "System.IO.MonoIO::SetFileTime(intptr,long,long,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileTime,
5028         "System.IO.MonoIO::Flush(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Flush,
5029         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
5030         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
5031         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
5032         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
5033         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
5034         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
5035         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
5036         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
5037         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
5038         "System.IO.MonoIO::GetTempPath(string&)", ves_icall_System_IO_MonoIO_GetTempPath,
5039
5040         /*
5041          * System.Math
5042          */
5043         "System.Math::Floor", ves_icall_System_Math_Floor,
5044         "System.Math::Round", ves_icall_System_Math_Round,
5045         "System.Math::Round2", ves_icall_System_Math_Round2,
5046         "System.Math::Sin", ves_icall_System_Math_Sin,
5047         "System.Math::Cos", ves_icall_System_Math_Cos,
5048         "System.Math::Tan", ves_icall_System_Math_Tan,
5049         "System.Math::Sinh", ves_icall_System_Math_Sinh,
5050         "System.Math::Cosh", ves_icall_System_Math_Cosh,
5051         "System.Math::Tanh", ves_icall_System_Math_Tanh,
5052         "System.Math::Acos", ves_icall_System_Math_Acos,
5053         "System.Math::Asin", ves_icall_System_Math_Asin,
5054         "System.Math::Atan", ves_icall_System_Math_Atan,
5055         "System.Math::Atan2", ves_icall_System_Math_Atan2,
5056         "System.Math::Exp", ves_icall_System_Math_Exp,
5057         "System.Math::Log", ves_icall_System_Math_Log,
5058         "System.Math::Log10", ves_icall_System_Math_Log10,
5059         "System.Math::Pow", ves_icall_System_Math_Pow,
5060         "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
5061
5062         /*
5063          * System.Environment
5064          */
5065         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
5066         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
5067         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
5068         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
5069         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
5070         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
5071         "System.Environment::Exit", ves_icall_System_Environment_Exit,
5072         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
5073         "System.Environment::get_HasShutdownStarted", ves_icall_System_Environment_get_HasShutdownStarted,
5074         "System.Environment::get_ExitCode", mono_environment_exitcode_get,
5075         "System.Environment::set_ExitCode", mono_environment_exitcode_set,
5076         "System.Environment::GetMachineConfigPath",     ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
5077         "System.Environment::internalGetGacPath", ves_icall_System_Environment_GetGacPath,
5078
5079         /*
5080          * System.Runtime.Remoting
5081          */     
5082         "System.Runtime.Remoting.RemotingServices::InternalExecute",
5083         ves_icall_InternalExecute,
5084         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
5085         ves_icall_IsTransparentProxy,
5086
5087         /*
5088          * System.Runtime.Remoting.Activation
5089          */     
5090         "System.Runtime.Remoting.Activation.ActivationServices::AllocateUninitializedClassInstance",
5091         ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance,
5092         "System.Runtime.Remoting.Activation.ActivationServices::EnableProxyActivation",
5093         ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation,
5094
5095         /*
5096          * System.Runtime.Remoting.Messaging
5097          */     
5098         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
5099         ves_icall_MonoMethodMessage_InitMessage,
5100         
5101         /*
5102          * System.Runtime.Remoting.Proxies
5103          */     
5104         "System.Runtime.Remoting.Proxies.RealProxy::InternalGetTransparentProxy", 
5105         ves_icall_Remoting_RealProxy_GetTransparentProxy,
5106
5107         /*
5108          * System.Threading.Interlocked
5109          */
5110         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
5111         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
5112         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
5113         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
5114         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
5115         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
5116         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
5117         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
5118         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
5119         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
5120         "System.Threading.Thread::current_lcid()", ves_icall_System_Threading_Thread_current_lcid,
5121
5122         /*
5123          * System.Diagnostics.Process
5124          */
5125         "System.Diagnostics.Process::GetProcess_internal(int)", ves_icall_System_Diagnostics_Process_GetProcess_internal,
5126         "System.Diagnostics.Process::GetProcesses_internal()", ves_icall_System_Diagnostics_Process_GetProcesses_internal,
5127         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
5128         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
5129         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
5130         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,System.Diagnostics.Process/ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
5131         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
5132         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
5133         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
5134         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
5135         "System.Diagnostics.Process::ProcessName_internal(intptr)", ves_icall_System_Diagnostics_Process_ProcessName_internal,
5136         "System.Diagnostics.Process::GetWorkingSet_internal(intptr,int&,int&)", ves_icall_System_Diagnostics_Process_GetWorkingSet_internal,
5137         "System.Diagnostics.Process::SetWorkingSet_internal(intptr,int,int,bool)", ves_icall_System_Diagnostics_Process_SetWorkingSet_internal,
5138         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
5139
5140         /* 
5141          * System.Delegate
5142          */
5143         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
5144
5145         /*
5146          * System.IO.Path
5147          */
5148         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
5149
5150         /*
5151          * Private icalls for the Mono Debugger
5152          */
5153         "System.Reflection.Assembly::MonoDebugger_GetMethod",
5154         ves_icall_MonoDebugger_GetMethod,
5155
5156         "System.Reflection.Assembly::MonoDebugger_GetMethodToken",
5157         ves_icall_MonoDebugger_GetMethodToken,
5158
5159         "System.Reflection.Assembly::MonoDebugger_GetLocalTypeFromSignature",
5160         ves_icall_MonoDebugger_GetLocalTypeFromSignature,
5161
5162         "System.Reflection.Assembly::MonoDebugger_GetType",
5163         ves_icall_MonoDebugger_GetType,
5164
5165         /*
5166          * System.Configuration
5167          */
5168         "System.Configuration.DefaultConfig::get_machine_config_path",
5169         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
5170
5171         /*
5172          * System.Diagnostics.DefaultTraceListener
5173          */
5174         "System.Diagnostics.DefaultTraceListener::WriteWindowsDebugString",
5175         ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString,
5176         /*
5177          * System.Activator
5178          */
5179         "System.Activator::CreateInstanceInternal",
5180         ves_icall_System_Activator_CreateInstanceInternal,
5181
5182         /* 
5183          * System.Web
5184          */
5185         "System.Web.Util.ICalls::GetMachineConfigPath",
5186         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
5187
5188         "System.Web.Util.ICalls::GetMachineInstallDirectory",
5189         ves_icall_System_Web_Util_ICalls_get_machine_install_dir,
5190
5191         /*
5192          * System.Globalization
5193          */
5194         "System.Globalization.CultureInfo::construct_internal_locale(string)", ves_icall_System_Globalization_CultureInfo_construct_internal_locale,
5195         "System.Globalization.CompareInfo::construct_compareinfo(string)", ves_icall_System_Globalization_CompareInfo_construct_compareinfo,
5196         "System.Globalization.CompareInfo::internal_compare(string,int,int,string,int,int,System.Globalization.CompareOptions)", ves_icall_System_Globalization_CompareInfo_internal_compare,
5197         "System.Globalization.CompareInfo::free_internal_collator()", ves_icall_System_Globalization_CompareInfo_free_internal_collator,
5198         "System.Globalization.CompareInfo::assign_sortkey(object,string,System.Globalization.CompareOptions)", ves_icall_System_Globalization_CompareInfo_assign_sortkey,
5199         "System.Globalization.CompareInfo::internal_index(string,int,int,string,System.Globalization.CompareOptions,bool)", ves_icall_System_Globalization_CompareInfo_internal_index,
5200         "System.Globalization.CompareInfo::internal_index(string,int,int,char,System.Globalization.CompareOptions,bool)", ves_icall_System_Globalization_CompareInfo_internal_index_char,
5201         "System.String::InternalReplace(string,string,System.Globalization.CompareInfo)", ves_icall_System_String_InternalReplace_Str_Comp,
5202         "System.String::InternalToLower(System.Globalization.CultureInfo)", ves_icall_System_String_InternalToLower_Comp,
5203         "System.String::InternalToUpper(System.Globalization.CultureInfo)", ves_icall_System_String_InternalToUpper_Comp,
5204
5205         /*
5206          * System.IO.FileSystemWatcher
5207          */
5208         "System.IO.FileSystemWatcher::InternalSupportsFSW", ves_icall_System_IO_FSW_SupportsFSW,
5209         "System.IO.FileSystemWatcher::InternalOpenDirectory", ves_icall_System_IO_FSW_OpenDirectory,
5210         "System.IO.FileSystemWatcher::InternalCloseDirectory", ves_icall_System_IO_FSW_CloseDirectory,
5211         "System.IO.FileSystemWatcher::InternalReadDirectoryChanges", ves_icall_System_IO_FSW_ReadDirectoryChanges,
5212         /*
5213          * System.IO.FAMWatcher
5214          */
5215         "System.IO.FAMWatcher::InternalFAMNextEvent", ves_icall_System_IO_FAMW_InternalFAMNextEvent,
5216
5217         /*
5218          * add other internal calls here
5219          */
5220
5221         /* These will be deleted after the next release */
5222         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
5223         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
5224         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
5225         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
5226         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
5227         "System.String::InternalCompare(string,int,string,int,int,int)", ves_icall_System_String_InternalCompareStr_N,
5228         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
5229         "System.String::InternalToLower()", ves_icall_System_String_InternalToLower,
5230         "System.String::InternalToUpper()", ves_icall_System_String_InternalToUpper,
5231         "System.Globalization.CultureInfo::construct_compareinfo(object,string)", ves_icall_System_Globalization_CompareInfo_construct_compareinfo,
5232
5233         NULL, NULL
5234 };
5235
5236 void
5237 mono_init_icall (void)
5238 {
5239         const char *name;
5240         int i = 0;
5241
5242         while ((name = icall_map [i])) {
5243                 mono_add_internal_call (name, icall_map [i+1]);
5244                 i += 2;
5245         }
5246        
5247 }
5248
5249