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