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