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