2003-12-01 Dick Porter <dick@ximian.com>
[mono.git] / mono / metadata / icall.c
1 /*
2  * icall.c:
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *       Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 #if defined (PLATFORM_WIN32)
19 #include <stdlib.h>
20 #endif
21
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/monitor.h>
25 #include <mono/metadata/reflection.h>
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/file-io.h>
30 #include <mono/metadata/socket-io.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/unicode.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/marshal.h>
36 #include <mono/metadata/gc-internal.h>
37 #include <mono/metadata/rand.h>
38 #include <mono/metadata/sysmath.h>
39 #include <mono/metadata/string-icalls.h>
40 #include <mono/metadata/mono-debug-debugger.h>
41 #include <mono/metadata/process.h>
42 #include <mono/metadata/environment.h>
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/locales.h>
45 #include <mono/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                 if (assembly->assembly->dynamic) {
2536                         /* Enumerate all modules */
2537                         MonoReflectionAssemblyBuilder *abuilder = (MonoReflectionAssemblyBuilder*)assembly;
2538                         int i;
2539
2540                         if (!abuilder->modules)
2541                                 type = NULL;
2542                         else {
2543                                 for (i = 0; i < mono_array_length (abuilder->modules); ++i) {
2544                                         MonoReflectionModuleBuilder *mb = mono_array_get (abuilder->modules, MonoReflectionModuleBuilder*, i);
2545                                         type = mono_reflection_get_type (&mb->dynamic_image->image, &info, ignoreCase);
2546                                         if (type)
2547                                                 break;
2548                                 }
2549                         }
2550                 }
2551                 else
2552                         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
2553         g_free (str);
2554         g_list_free (info.modifiers);
2555         g_list_free (info.nested);
2556         if (!type) {
2557                 if (throwOnError)
2558                         mono_raise_exception (mono_get_exception_type_load (name));
2559                 /* g_print ("failed find\n"); */
2560                 return NULL;
2561         }
2562         /* g_print ("got it\n"); */
2563         return mono_type_get_object (mono_object_domain (assembly), type);
2564
2565 }
2566
2567 static MonoString *
2568 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
2569 {
2570         MonoDomain *domain = mono_object_domain (assembly); 
2571         MonoAssembly *mass = assembly->assembly;
2572         MonoString *res;
2573         gchar *uri;
2574         gchar *absolute;
2575         
2576         MONO_ARCH_SAVE_REGS;
2577
2578         absolute = g_build_filename (mass->basedir, mass->image->module_name, NULL);
2579         uri = g_filename_to_uri (absolute, NULL, NULL);
2580         res = mono_string_new (domain, uri);
2581         g_free (uri);
2582         g_free (absolute);
2583         return res;
2584 }
2585
2586 static MonoString *
2587 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
2588 {
2589         MonoDomain *domain = mono_object_domain (assembly); 
2590         MonoString *res;
2591         char *name = g_build_filename (
2592                 assembly->assembly->basedir,
2593                 assembly->assembly->image->module_name, NULL);
2594
2595         MONO_ARCH_SAVE_REGS;
2596
2597         res = mono_string_new (domain, name);
2598         g_free (name);
2599         return res;
2600 }
2601
2602 static MonoString *
2603 ves_icall_System_Reflection_Assembly_InternalImageRuntimeVersion (MonoReflectionAssembly *assembly)
2604 {
2605         MonoDomain *domain = mono_object_domain (assembly); 
2606
2607         MONO_ARCH_SAVE_REGS;
2608
2609         return mono_string_new (domain, assembly->assembly->image->version);
2610 }
2611
2612 static MonoReflectionMethod*
2613 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) 
2614 {
2615         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
2616
2617         MONO_ARCH_SAVE_REGS;
2618
2619         if (!token)
2620                 return NULL;
2621         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
2622 }
2623
2624 static MonoArray*
2625 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) 
2626 {
2627         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2628         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2629         int i;
2630         const char *val;
2631
2632         MONO_ARCH_SAVE_REGS;
2633
2634         for (i = 0; i < table->rows; ++i) {
2635                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
2636                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
2637         }
2638         return result;
2639 }
2640
2641 static MonoArray*
2642 ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflectionAssembly *assembly) 
2643 {
2644         static MonoClass *System_Reflection_AssemblyName;
2645         MonoArray *result;
2646         MonoAssembly **ptr;
2647         MonoDomain *domain = mono_object_domain (assembly);
2648         int i, count = 0;
2649
2650         MONO_ARCH_SAVE_REGS;
2651
2652         if (!System_Reflection_AssemblyName)
2653                 System_Reflection_AssemblyName = mono_class_from_name (
2654                         mono_defaults.corlib, "System.Reflection", "AssemblyName");
2655
2656         for (ptr = assembly->assembly->image->references; ptr && *ptr; ptr++)
2657                 count++;
2658
2659         result = mono_array_new (mono_object_domain (assembly), System_Reflection_AssemblyName, count);
2660
2661         for (i = 0; i < count; i++) {
2662                 MonoAssembly *assem = assembly->assembly->image->references [i];
2663                 MonoReflectionAssemblyName *aname;
2664                 char *codebase, *absolute;
2665
2666                 aname = (MonoReflectionAssemblyName *) mono_object_new (
2667                         domain, System_Reflection_AssemblyName);
2668
2669                 if (strcmp (assem->aname.name, "corlib") == 0)
2670                         aname->name = mono_string_new (domain, "mscorlib");
2671                 else
2672                         aname->name = mono_string_new (domain, assem->aname.name);
2673                 aname->major = assem->aname.major;
2674
2675                 absolute = g_build_filename (assem->basedir, assem->image->module_name, NULL);
2676                 codebase = g_filename_to_uri (absolute, NULL, NULL);
2677                 aname->codebase = mono_string_new (domain, codebase);
2678                 g_free (codebase);
2679                 g_free (absolute);
2680                 mono_array_set (result, gpointer, i, aname);
2681         }
2682         return result;
2683 }
2684
2685 typedef struct {
2686         MonoArray *res;
2687         int idx;
2688 } NameSpaceInfo;
2689
2690 static void
2691 foreach_namespace (const char* key, gconstpointer val, NameSpaceInfo *info)
2692 {
2693         MonoString *name = mono_string_new (mono_object_domain (info->res), key);
2694
2695         mono_array_set (info->res, gpointer, info->idx, name);
2696         info->idx++;
2697 }
2698
2699 static MonoArray*
2700 ves_icall_System_Reflection_Assembly_GetNamespaces (MonoReflectionAssembly *assembly) 
2701 {
2702         MonoImage *img = assembly->assembly->image;
2703         int n;
2704         MonoArray *res;
2705         NameSpaceInfo info;
2706         MonoTableInfo  *t = &img->tables [MONO_TABLE_EXPORTEDTYPE];
2707         int i;
2708
2709         MONO_ARCH_SAVE_REGS;
2710
2711         n = g_hash_table_size (img->name_cache);
2712         res = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n);
2713         info.res = res;
2714         info.idx = 0;
2715         g_hash_table_foreach (img->name_cache, (GHFunc)foreach_namespace, &info);
2716
2717         /* Add namespaces from the EXPORTEDTYPES table as well */
2718         if (t->rows) {
2719                 MonoArray *res2;
2720                 GPtrArray *nspaces = g_ptr_array_new ();
2721                 for (i = 0; i < t->rows; ++i) {
2722                         const char *nspace = mono_metadata_string_heap (img, mono_metadata_decode_row_col (t, i, MONO_EXP_TYPE_NAMESPACE));
2723                         if (!g_hash_table_lookup (img->name_cache, nspace)) {
2724                                 g_ptr_array_add (nspaces, (char*)nspace);
2725                         }
2726                 }
2727                 if (nspaces->len > 0) {
2728                         res2 = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, n + nspaces->len);
2729                         memcpy (mono_array_addr (res2, MonoString*, 0),
2730                                         mono_array_addr (res, MonoString*, 0),
2731                                         n * sizeof (MonoString*));
2732                         for (i = 0; i < nspaces->len; ++i)
2733                                 mono_array_set (res2, MonoString*, n + i, 
2734                                                                 mono_string_new (mono_object_domain (assembly),
2735                                                                                                  g_ptr_array_index (nspaces, i)));
2736                         res = res2;
2737                 }
2738                 g_ptr_array_free (nspaces, TRUE);
2739         }
2740
2741         return res;
2742 }
2743
2744 /* move this in some file in mono/util/ */
2745 static char *
2746 g_concat_dir_and_file (const char *dir, const char *file)
2747 {
2748         g_return_val_if_fail (dir != NULL, NULL);
2749         g_return_val_if_fail (file != NULL, NULL);
2750
2751         /*
2752          * If the directory name doesn't have a / on the end, we need
2753          * to add one so we get a proper path to the file
2754          */
2755         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
2756                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
2757         else
2758                 return g_strconcat (dir, file, NULL);
2759 }
2760
2761 static void *
2762 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name, gint32 *size) 
2763 {
2764         char *n = mono_string_to_utf8 (name);
2765         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2766         guint32 i;
2767         guint32 cols [MONO_MANIFEST_SIZE];
2768         const char *val;
2769
2770         MONO_ARCH_SAVE_REGS;
2771
2772         for (i = 0; i < table->rows; ++i) {
2773                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2774                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2775                 if (strcmp (val, n) == 0)
2776                         break;
2777         }
2778         g_free (n);
2779         if (i == table->rows)
2780                 return NULL;
2781         /* FIXME */
2782         if (cols [MONO_MANIFEST_IMPLEMENTATION]) {
2783                 /*
2784                  * this code should only be called after obtaining the 
2785                  * ResourceInfo and handling the other cases.
2786                  */
2787                 g_assert_not_reached ();
2788                 return NULL;
2789         }
2790
2791         return (void*)mono_image_get_resource (assembly->assembly->image, cols [MONO_MANIFEST_OFFSET], size);
2792 }
2793
2794 static gboolean
2795 ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (MonoReflectionAssembly *assembly, MonoString *name, MonoManifestResourceInfo *info)
2796 {
2797         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
2798         int i;
2799         guint32 cols [MONO_MANIFEST_SIZE];
2800         guint32 file_cols [MONO_FILE_SIZE];
2801         const char *val;
2802         char *n;
2803
2804         MONO_ARCH_SAVE_REGS;
2805
2806         n = mono_string_to_utf8 (name);
2807         for (i = 0; i < table->rows; ++i) {
2808                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
2809                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
2810                 if (strcmp (val, n) == 0)
2811                         break;
2812         }
2813         g_free (n);
2814         if (i == table->rows)
2815                 return FALSE;
2816
2817         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
2818                 info->location = RESOURCE_LOCATION_EMBEDDED | RESOURCE_LOCATION_IN_MANIFEST;
2819         }
2820         else {
2821                 switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
2822                 case IMPLEMENTATION_FILE:
2823                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2824                         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2825                         mono_metadata_decode_row (table, i - 1, file_cols, MONO_FILE_SIZE);
2826                         val = mono_metadata_string_heap (assembly->assembly->image, file_cols [MONO_FILE_NAME]);
2827                         info->filename = mono_string_new (mono_object_domain (assembly), val);
2828                         if (file_cols [MONO_FILE_FLAGS] && FILE_CONTAINS_NO_METADATA)
2829                                 info->location = 0;
2830                         else
2831                                 info->location = RESOURCE_LOCATION_EMBEDDED;
2832                         break;
2833
2834                 case IMPLEMENTATION_ASSEMBLYREF:
2835                         i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
2836                         info->assembly = mono_assembly_get_object (mono_domain_get (), assembly->assembly->image->references [i - 1]);
2837
2838                         // Obtain info recursively
2839                         ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal (info->assembly, name, info);
2840                         info->location |= RESOURCE_LOCATION_ANOTHER_ASSEMBLY;
2841                         break;
2842
2843                 case IMPLEMENTATION_EXP_TYPE:
2844                         g_assert_not_reached ();
2845                         break;
2846                 }
2847         }
2848
2849         return TRUE;
2850 }
2851
2852 static MonoObject*
2853 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) 
2854 {
2855         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2856         MonoArray *result = NULL;
2857         int i;
2858         const char *val;
2859         char *n;
2860
2861         MONO_ARCH_SAVE_REGS;
2862
2863         /* check hash if needed */
2864         if (name) {
2865                 n = mono_string_to_utf8 (name);
2866                 for (i = 0; i < table->rows; ++i) {
2867                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2868                         if (strcmp (val, n) == 0) {
2869                                 MonoString *fn;
2870                                 g_free (n);
2871                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2872                                 fn = mono_string_new (mono_object_domain (assembly), n);
2873                                 g_free (n);
2874                                 return (MonoObject*)fn;
2875                         }
2876                 }
2877                 g_free (n);
2878                 return NULL;
2879         }
2880
2881         for (i = 0; i < table->rows; ++i) {
2882                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2883                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2884                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2885                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
2886                 g_free (n);
2887         }
2888         return (MonoObject*)result;
2889 }
2890
2891 static MonoArray*
2892 ves_icall_System_Reflection_Assembly_GetModulesInternal (MonoReflectionAssembly *assembly)
2893 {
2894         MonoDomain *domain = mono_domain_get();
2895         MonoArray *res;
2896         MonoClass *klass;
2897         int i, module_count = 0, file_count = 0;
2898         MonoImage **modules = assembly->assembly->image->modules;
2899         MonoTableInfo *table;
2900
2901         if (modules) {
2902                 while (modules[module_count])
2903                         ++module_count;
2904         }
2905
2906         table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
2907         file_count = table->rows;
2908
2909         g_assert( assembly->assembly->image != NULL);
2910         ++module_count;
2911
2912         klass = mono_class_from_name ( mono_defaults.corlib, "System.Reflection", "Module");
2913         res = mono_array_new (domain, klass, module_count + file_count);
2914
2915         mono_array_set (res, gpointer, 0, mono_module_get_object (domain, assembly->assembly->image));
2916         for ( i = 1; i < module_count; ++i )
2917                 mono_array_set (res, gpointer, i, mono_module_get_object (domain, modules[i]));
2918
2919         for (i = 0; i < table->rows; ++i)
2920                 mono_array_set (res, gpointer, module_count + i, mono_module_file_get_object (domain, assembly->assembly->image, i));
2921
2922         return res;
2923 }
2924
2925 static MonoReflectionMethod*
2926 ves_icall_GetCurrentMethod (void) 
2927 {
2928         MonoMethod *m = mono_method_get_last_managed ();
2929
2930         MONO_ARCH_SAVE_REGS;
2931
2932         return mono_method_get_object (mono_domain_get (), m, NULL);
2933 }
2934
2935 static MonoReflectionAssembly*
2936 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
2937 {
2938         MonoMethod *m = mono_method_get_last_managed ();
2939
2940         MONO_ARCH_SAVE_REGS;
2941
2942         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
2943 }
2944
2945
2946 static gboolean
2947 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gboolean managed, gpointer data)
2948 {
2949         MonoMethod **dest = data;
2950
2951         /* skip unmanaged frames */
2952         if (!managed)
2953                 return FALSE;
2954
2955         if (m == *dest) {
2956                 *dest = NULL;
2957                 return FALSE;
2958         }
2959         if (!(*dest)) {
2960                 *dest = m;
2961                 return TRUE;
2962         }
2963         return FALSE;
2964 }
2965
2966 static MonoReflectionAssembly*
2967 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
2968 {
2969         MonoDomain* domain = mono_domain_get ();
2970
2971         MONO_ARCH_SAVE_REGS;
2972
2973         if (!domain->entry_assembly)
2974                 domain = mono_root_domain;
2975
2976         return mono_assembly_get_object (domain, domain->entry_assembly);
2977 }
2978
2979
2980 static MonoReflectionAssembly*
2981 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
2982 {
2983         MonoMethod *m = mono_method_get_last_managed ();
2984         MonoMethod *dest = m;
2985
2986         MONO_ARCH_SAVE_REGS;
2987
2988         mono_stack_walk (get_caller, &dest);
2989         if (!dest)
2990                 dest = m;
2991         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
2992 }
2993
2994 static MonoString *
2995 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
2996 {
2997         MonoDomain *domain = mono_object_domain (object); 
2998         MonoString *res;
2999         gchar *name;
3000
3001         MONO_ARCH_SAVE_REGS;
3002
3003         name = mono_type_get_name (object->type);
3004         res = mono_string_new (domain, name);
3005         g_free (name);
3006
3007         return res;
3008 }
3009
3010 static void
3011 fill_reflection_assembly_name (MonoDomain *domain, MonoReflectionAssemblyName *aname, MonoAssemblyName *name, const char *absolute)
3012 {
3013         static MonoMethod *create_culture = NULL;
3014     gpointer args [1];
3015         guint32 pkey_len;
3016         const char *pkey_ptr;
3017         gchar *codebase;
3018
3019         MONO_ARCH_SAVE_REGS;
3020
3021         if (strcmp (name->name, "corlib") == 0)
3022                 aname->name = mono_string_new (domain, "mscorlib");
3023         else
3024                 aname->name = mono_string_new (domain, name->name);
3025
3026         aname->major = name->major;
3027         aname->minor = name->minor;
3028         aname->build = name->build;
3029         aname->revision = name->revision;
3030         aname->hashalg = name->hash_alg;
3031
3032         codebase = g_filename_to_uri (absolute, NULL, NULL);
3033         if (codebase) {
3034                 aname->codebase = mono_string_new (domain, codebase);
3035                 g_free (codebase);
3036         }
3037
3038         if (!create_culture) {
3039                 MonoMethodDesc *desc = mono_method_desc_new ("System.Globalization.CultureInfo:CreateSpecificCulture(string)", TRUE);
3040                 create_culture = mono_method_desc_search_in_image (desc, mono_defaults.corlib);
3041                 g_assert (create_culture);
3042                 mono_method_desc_free (desc);
3043         }
3044
3045         args [0] = mono_string_new (domain, name->culture);
3046         aname->cultureInfo = 
3047                 mono_runtime_invoke (create_culture, NULL, args, NULL);
3048
3049         if (name->public_key) {
3050                 pkey_ptr = name->public_key;
3051                 pkey_len = mono_metadata_decode_blob_size (pkey_ptr, &pkey_ptr);
3052
3053                 aname->publicKey = mono_array_new (domain, mono_defaults.byte_class, pkey_len);
3054                 memcpy (mono_array_addr (aname->publicKey, guint8, 0), pkey_ptr, pkey_len);
3055         }
3056 }
3057
3058 static void
3059 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
3060 {
3061         gchar *absolute;
3062
3063         MONO_ARCH_SAVE_REGS;
3064
3065         absolute = g_build_filename (assembly->assembly->basedir, assembly->assembly->image->module_name, NULL);
3066
3067         fill_reflection_assembly_name (mono_object_domain (assembly), aname, 
3068                                                                    &assembly->assembly->aname, absolute);
3069
3070         g_free (absolute);
3071 }
3072
3073 static void
3074 ves_icall_System_Reflection_Assembly_InternalGetAssemblyName (MonoString *fname, MonoReflectionAssemblyName *aname)
3075 {
3076         char *filename;
3077         MonoImageOpenStatus status = MONO_IMAGE_OK;
3078         gboolean res;
3079         MonoImage *image;
3080         MonoAssemblyName name;
3081
3082         MONO_ARCH_SAVE_REGS;
3083
3084         filename = mono_string_to_utf8 (fname);
3085
3086         image = mono_image_open (filename, &status);
3087         
3088         if (!image){
3089                 MonoException *exc;
3090
3091                 g_free (filename);
3092                 exc = mono_get_exception_file_not_found (fname);
3093                 mono_raise_exception (exc);
3094         }
3095
3096         res = mono_assembly_fill_assembly_name (image, &name);
3097         if (!res) {
3098                 mono_image_close (image);
3099                 g_free (filename);
3100                 mono_raise_exception (mono_get_exception_argument ("assemblyFile", "The file does not contain a manifest"));
3101         }
3102
3103         fill_reflection_assembly_name (mono_domain_get (), aname, &name, filename);
3104
3105         g_free (filename);
3106         mono_image_close (image);
3107 }
3108
3109 static MonoArray*
3110 mono_module_get_types (MonoDomain *domain, MonoImage *image, 
3111                                            MonoBoolean exportedOnly)
3112 {
3113         MonoArray *res;
3114         MonoClass *klass;
3115         MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
3116         int i, count;
3117         guint32 attrs, visibility;
3118
3119         /* we start the count from 1 because we skip the special type <Module> */
3120         if (exportedOnly) {
3121                 count = 0;
3122                 for (i = 1; i < tdef->rows; ++i) {
3123                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
3124                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3125                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
3126                                 count++;
3127                 }
3128         } else {
3129                 count = tdef->rows - 1;
3130         }
3131         res = mono_array_new (domain, mono_defaults.monotype_class, count);
3132         count = 0;
3133         for (i = 1; i < tdef->rows; ++i) {
3134                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
3135                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3136                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
3137                         klass = mono_class_get (image, (i + 1) | MONO_TOKEN_TYPE_DEF);
3138                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
3139                         count++;
3140                 }
3141         }
3142         
3143         return res;
3144 }
3145
3146 static MonoArray*
3147 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
3148 {
3149         MonoArray *res;
3150         MonoImage *image = assembly->assembly->image;
3151         MonoTableInfo *table = &image->tables [MONO_TABLE_FILE];
3152         MonoDomain *domain;
3153         int i;
3154
3155         MONO_ARCH_SAVE_REGS;
3156
3157         domain = mono_object_domain (assembly);
3158         res = mono_module_get_types (domain, image, exportedOnly);
3159
3160         /* Append data from all modules in the assembly */
3161         for (i = 0; i < table->rows; ++i) {
3162                 if (!(mono_metadata_decode_row_col (table, i, MONO_FILE_FLAGS) & FILE_CONTAINS_NO_METADATA)) {
3163                         MonoImage *loaded_image = mono_assembly_load_module (image->assembly, i + 1);
3164                         if (loaded_image) {
3165                                 MonoArray *res2 = mono_module_get_types (domain, loaded_image, exportedOnly);
3166                                 /* Append the new types to the end of the array */
3167                                 if (mono_array_length (res2) > 0) {
3168                                         guint32 len1, len2;
3169                                         MonoArray *res3;
3170
3171                                         len1 = mono_array_length (res);
3172                                         len2 = mono_array_length (res2);
3173                                         res3 = mono_array_new (domain, mono_defaults.monotype_class, len1 + len2);
3174                                         memcpy (mono_array_addr (res3, MonoReflectionType*, 0),
3175                                                         mono_array_addr (res, MonoReflectionType*, 0),
3176                                                         len1 * sizeof (MonoReflectionType*));
3177                                         memcpy (mono_array_addr (res3, MonoReflectionType*, len1),
3178                                                         mono_array_addr (res2, MonoReflectionType*, 0),
3179                                                         len2 * sizeof (MonoReflectionType*));
3180                                         res = res3;
3181                                 }
3182                         }
3183                 }
3184         }               
3185
3186         return res;
3187 }
3188
3189 static MonoReflectionType*
3190 ves_icall_System_Reflection_Module_GetGlobalType (MonoReflectionModule *module)
3191 {
3192         MonoDomain *domain = mono_object_domain (module); 
3193         MonoClass *klass;
3194
3195         MONO_ARCH_SAVE_REGS;
3196
3197         g_assert (module->image);
3198         klass = mono_class_get (module->image, 1 | MONO_TOKEN_TYPE_DEF);
3199         return mono_type_get_object (domain, &klass->byval_arg);
3200 }
3201
3202 static MonoString*
3203 ves_icall_System_Reflection_Module_GetGuidInternal (MonoReflectionModule *module)
3204 {
3205         MonoDomain *domain = mono_object_domain (module); 
3206
3207         MONO_ARCH_SAVE_REGS;
3208
3209         g_assert (module->image);
3210         return mono_string_new (domain, module->image->guid);
3211 }
3212
3213 static MonoArray*
3214 ves_icall_System_Reflection_Module_InternalGetTypes (MonoReflectionModule *module)
3215 {
3216         MONO_ARCH_SAVE_REGS;
3217
3218         if (!module->image)
3219                 return mono_array_new (mono_object_domain (module), mono_defaults.monotype_class, 0);
3220         else
3221                 return mono_module_get_types (mono_object_domain (module), module->image, FALSE);
3222 }
3223
3224 static MonoReflectionType*
3225 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
3226 {
3227         MonoClass *klass;
3228         int isbyref = 0, rank;
3229         char *str = mono_string_to_utf8 (smodifiers);
3230         char *p;
3231
3232         MONO_ARCH_SAVE_REGS;
3233
3234         klass = mono_class_from_mono_type (tb->type.type);
3235         p = str;
3236         /* logic taken from mono_reflection_parse_type(): keep in sync */
3237         while (*p) {
3238                 switch (*p) {
3239                 case '&':
3240                         if (isbyref) { /* only one level allowed by the spec */
3241                                 g_free (str);
3242                                 return NULL;
3243                         }
3244                         isbyref = 1;
3245                         p++;
3246                         g_free (str);
3247                         return mono_type_get_object (mono_object_domain (tb), &klass->this_arg);
3248                         break;
3249                 case '*':
3250                         klass = mono_ptr_class_get (&klass->byval_arg);
3251                         mono_class_init (klass);
3252                         p++;
3253                         break;
3254                 case '[':
3255                         rank = 1;
3256                         p++;
3257                         while (*p) {
3258                                 if (*p == ']')
3259                                         break;
3260                                 if (*p == ',')
3261                                         rank++;
3262                                 else if (*p != '*') { /* '*' means unknown lower bound */
3263                                         g_free (str);
3264                                         return NULL;
3265                                 }
3266                                 ++p;
3267                         }
3268                         if (*p != ']') {
3269                                 g_free (str);
3270                                 return NULL;
3271                         }
3272                         p++;
3273                         klass = mono_array_class_get (klass, rank);
3274                         mono_class_init (klass);
3275                         break;
3276                 default:
3277                         break;
3278                 }
3279         }
3280         g_free (str);
3281         return mono_type_get_object (mono_object_domain (tb), &klass->byval_arg);
3282 }
3283
3284 static MonoBoolean
3285 ves_icall_Type_IsArrayImpl (MonoReflectionType *t)
3286 {
3287         MonoType *type;
3288         MonoBoolean res;
3289
3290         MONO_ARCH_SAVE_REGS;
3291
3292         type = t->type;
3293         res = !type->byref && (type->type == MONO_TYPE_ARRAY || type->type == MONO_TYPE_SZARRAY);
3294
3295         return res;
3296 }
3297
3298 static MonoObject *
3299 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
3300                                                    MonoReflectionMethod *info)
3301 {
3302         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
3303         MonoObject *delegate;
3304         gpointer func;
3305
3306         MONO_ARCH_SAVE_REGS;
3307
3308         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
3309
3310         delegate = mono_object_new (mono_object_domain (type), delegate_class);
3311
3312         func = mono_compile_method (info->method);
3313
3314         mono_delegate_ctor (delegate, target, func);
3315
3316         return delegate;
3317 }
3318
3319 /*
3320  * Magic number to convert a time which is relative to
3321  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
3322  */
3323 #define EPOCH_ADJUST    ((guint64)62135596800L)
3324
3325 /*
3326  * Magic number to convert FILETIME base Jan 1, 1601 to DateTime - base Jan, 1, 0001
3327  */
3328 #define FILETIME_ADJUST ((guint64)504911232000000000LL)
3329
3330 /*
3331  * This returns Now in UTC
3332  */
3333 static gint64
3334 ves_icall_System_DateTime_GetNow (void)
3335 {
3336 #ifdef PLATFORM_WIN32
3337         SYSTEMTIME st;
3338         FILETIME ft;
3339         
3340         GetLocalTime (&st);
3341         SystemTimeToFileTime (&st, &ft);
3342         return (gint64) FILETIME_ADJUST + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
3343 #else
3344         /* FIXME: put this in io-layer and call it GetLocalTime */
3345         struct timeval tv;
3346         gint64 res;
3347
3348         MONO_ARCH_SAVE_REGS;
3349
3350         if (gettimeofday (&tv, NULL) == 0) {
3351                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
3352                 return res;
3353         }
3354         /* fixme: raise exception */
3355         return 0;
3356 #endif
3357 }
3358
3359 #ifdef PLATFORM_WIN32
3360 /* convert a SYSTEMTIME which is of the form "last thursday in october" to a real date */
3361 static void
3362 convert_to_absolute_date(SYSTEMTIME *date)
3363 {
3364 #define IS_LEAP(y) ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0))
3365         static int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3366         static int leap_days_in_month[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3367         /* from the calendar FAQ */
3368         int a = (14 - date->wMonth) / 12;
3369         int y = date->wYear - a;
3370         int m = date->wMonth + 12 * a - 2;
3371         int d = (1 + y + y/4 - y/100 + y/400 + (31*m)/12) % 7;
3372
3373         /* d is now the day of the week for the first of the month (0 == Sunday) */
3374
3375         int day_of_week = date->wDayOfWeek;
3376
3377         /* set day_in_month to the first day in the month which falls on day_of_week */    
3378         int day_in_month = 1 + (day_of_week - d);
3379         if (day_in_month <= 0)
3380                 day_in_month += 7;
3381
3382         /* wDay is 1 for first weekday in month, 2 for 2nd ... 5 means last - so work that out allowing for days in the month */
3383         date->wDay = day_in_month + (date->wDay - 1) * 7;
3384         if (date->wDay > (IS_LEAP(date->wYear) ? leap_days_in_month[date->wMonth - 1] : days_in_month[date->wMonth - 1]))
3385                 date->wDay -= 7;
3386 }
3387 #endif
3388
3389 #ifndef PLATFORM_WIN32
3390 /*
3391  * Return's the offset from GMT of a local time.
3392  * 
3393  *  tm is a local time
3394  *  t  is the same local time as seconds.
3395  */
3396 static int 
3397 gmt_offset(struct tm *tm, time_t t)
3398 {
3399 #if defined (HAVE_TM_GMTOFF)
3400         return tm->tm_gmtoff;
3401 #else
3402         struct tm g;
3403         time_t t2;
3404         g = *gmtime(&t);
3405         g.tm_isdst = tm->tm_isdst;
3406         t2 = mktime(&g);
3407         return (int)difftime(t, t2);
3408 #endif
3409 }
3410 #endif
3411 /*
3412  * This is heavily based on zdump.c from glibc 2.2.
3413  *
3414  *  * data[0]:  start of daylight saving time (in DateTime ticks).
3415  *  * data[1]:  end of daylight saving time (in DateTime ticks).
3416  *  * data[2]:  utcoffset (in TimeSpan ticks).
3417  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
3418  *  * name[0]:  name of this timezone when not daylight saving.
3419  *  * name[1]:  name of this timezone when daylight saving.
3420  *
3421  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
3422  *         the class library allows years between 1 and 9999.
3423  *
3424  *  Returns true on success and zero on failure.
3425  */
3426 static guint32
3427 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
3428 {
3429 #ifndef PLATFORM_WIN32
3430         MonoDomain *domain = mono_domain_get ();
3431         struct tm start, tt;
3432         time_t t;
3433
3434         long int gmtoff;
3435         int is_daylight = 0, day;
3436         char tzone [64];
3437
3438         MONO_ARCH_SAVE_REGS;
3439
3440         MONO_CHECK_ARG_NULL (data);
3441         MONO_CHECK_ARG_NULL (names);
3442
3443         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
3444         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
3445
3446         /* 
3447          * no info is better than crashing: we'll need our own tz data to make 
3448          * this work properly, anyway. The range is reduced to 1970 .. 2037 because
3449          * that is what mktime is guaranteed to support (we get into an infinite loop 
3450          * otherwise).
3451          */
3452         if ((year < 1970) || (year > 2037)) {
3453                 t = time (NULL);
3454                 tt = *localtime (&t);
3455                 strftime (tzone, sizeof (tzone), "%Z", &tt);
3456                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
3457                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
3458                 return 1;
3459         }
3460
3461         memset (&start, 0, sizeof (start));
3462
3463         start.tm_mday = 1;
3464         start.tm_year = year-1900;
3465
3466         t = mktime (&start);
3467         gmtoff = gmt_offset (&start, t);
3468
3469         /* For each day of the year, calculate the tm_gmtoff. */
3470         for (day = 0; day < 365; day++) {
3471
3472                 t += 3600*24;
3473                 tt = *localtime (&t);
3474
3475                 /* Daylight saving starts or ends here. */
3476                 if (gmt_offset (&tt, t) != gmtoff) {
3477                         struct tm tt1;
3478                         time_t t1;
3479
3480                         /* Try to find the exact hour when daylight saving starts/ends. */
3481                         t1 = t;
3482                         do {
3483                                 t1 -= 3600;
3484                                 tt1 = *localtime (&t1);
3485                         } while (gmt_offset (&tt1, t1) != gmtoff);
3486
3487                         /* Try to find the exact minute when daylight saving starts/ends. */
3488                         do {
3489                                 t1 += 60;
3490                                 tt1 = *localtime (&t1);
3491                         } while (gmt_offset (&tt1, t1) == gmtoff);
3492                         
3493                         strftime (tzone, sizeof (tzone), "%Z", &tt);
3494                         
3495                         /* Write data, if we're already in daylight saving, we're done. */
3496                         if (is_daylight) {
3497                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
3498                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
3499                                 return 1;
3500                         } else {
3501                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
3502                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
3503                                 is_daylight = 1;
3504                         }
3505
3506                         /* This is only set once when we enter daylight saving. */
3507                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
3508                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (&tt, t) - gmtoff) * 10000000L);
3509
3510                         gmtoff = gmt_offset (&tt, t);
3511                 }
3512         }
3513
3514         if (!is_daylight) {
3515                 strftime (tzone, sizeof (tzone), "%Z", &tt);
3516                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
3517                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
3518                 mono_array_set ((*data), gint64, 0, 0);
3519                 mono_array_set ((*data), gint64, 1, 0);
3520                 mono_array_set ((*data), gint64, 2, (gint64) gmtoff * 10000000L);
3521                 mono_array_set ((*data), gint64, 3, 0);
3522         }
3523
3524         return 1;
3525 #else
3526         MonoDomain *domain = mono_domain_get ();
3527         TIME_ZONE_INFORMATION tz_info;
3528         FILETIME ft;
3529         int i;
3530         int err, tz_id;
3531
3532         tz_id = GetTimeZoneInformation (&tz_info);
3533         if (tz_id == TIME_ZONE_ID_INVALID)
3534                 return 0;
3535
3536         MONO_CHECK_ARG_NULL (data);
3537         MONO_CHECK_ARG_NULL (names);
3538
3539         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
3540         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
3541
3542         for (i = 0; i < 32; ++i)
3543                 if (!tz_info.DaylightName [i])
3544                         break;
3545         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
3546         for (i = 0; i < 32; ++i)
3547                 if (!tz_info.StandardName [i])
3548                         break;
3549         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
3550
3551         if ((year <= 1601) || (year > 30827)) {
3552                 /*
3553                  * According to MSDN, the MS time functions can't handle dates outside
3554                  * this interval.
3555                  */
3556                 return 1;
3557         }
3558
3559         /* even if the timezone has no daylight savings it may have Bias (e.g. GMT+13 it seems) */
3560         if (tz_id != TIME_ZONE_ID_UNKNOWN) {
3561                 tz_info.StandardDate.wYear = year;
3562                 convert_to_absolute_date(&tz_info.StandardDate);
3563                 err = SystemTimeToFileTime (&tz_info.StandardDate, &ft);
3564                 g_assert(err);
3565                 mono_array_set ((*data), gint64, 1, FILETIME_ADJUST + (((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime));
3566                 tz_info.DaylightDate.wYear = year;
3567                 convert_to_absolute_date(&tz_info.DaylightDate);
3568                 err = SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
3569                 g_assert(err);
3570                 mono_array_set ((*data), gint64, 0, FILETIME_ADJUST + (((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime));
3571         }
3572         mono_array_set ((*data), gint64, 2, (tz_info.Bias + tz_info.StandardBias) * -600000000LL);
3573         mono_array_set ((*data), gint64, 3, (tz_info.DaylightBias - tz_info.StandardBias) * -600000000LL);
3574
3575         return 1;
3576 #endif
3577 }
3578
3579 static gpointer
3580 ves_icall_System_Object_obj_address (MonoObject *this) 
3581 {
3582         MONO_ARCH_SAVE_REGS;
3583
3584         return this;
3585 }
3586
3587 /* System.Buffer */
3588
3589 static gint32 
3590 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) 
3591 {
3592         MonoClass *klass;
3593         MonoTypeEnum etype;
3594         int length, esize;
3595         int i;
3596
3597         MONO_ARCH_SAVE_REGS;
3598
3599         klass = array->obj.vtable->klass;
3600         etype = klass->element_class->byval_arg.type;
3601         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
3602                 return -1;
3603
3604         if (array->bounds == NULL)
3605                 length = array->max_length;
3606         else {
3607                 length = 1;
3608                 for (i = 0; i < klass->rank; ++ i)
3609                         length *= array->bounds [i].length;
3610         }
3611
3612         esize = mono_array_element_size (klass);
3613         return length * esize;
3614 }
3615
3616 static gint8 
3617 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) 
3618 {
3619         MONO_ARCH_SAVE_REGS;
3620
3621         return mono_array_get (array, gint8, idx);
3622 }
3623
3624 static void 
3625 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) 
3626 {
3627         MONO_ARCH_SAVE_REGS;
3628
3629         mono_array_set (array, gint8, idx, value);
3630 }
3631
3632 static void 
3633 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) 
3634 {
3635         char *src_buf, *dest_buf;
3636
3637         MONO_ARCH_SAVE_REGS;
3638
3639         src_buf = (gint8 *)src->vector + src_offset;
3640         dest_buf = (gint8 *)dest->vector + dest_offset;
3641
3642         memcpy (dest_buf, src_buf, count);
3643 }
3644
3645 static MonoObject *
3646 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
3647 {
3648         MonoDomain *domain = mono_object_domain (this); 
3649         MonoObject *res;
3650         MonoRealProxy *rp = ((MonoRealProxy *)this);
3651         MonoType *type;
3652         MonoClass *klass;
3653
3654         MONO_ARCH_SAVE_REGS;
3655
3656         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
3657         
3658         ((MonoTransparentProxy *)res)->rp = rp;
3659         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
3660         klass = mono_class_from_mono_type (type);
3661
3662         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3663                 ((MonoTransparentProxy *)res)->klass = mono_defaults.marshalbyrefobject_class;
3664         else
3665                 ((MonoTransparentProxy *)res)->klass = klass;
3666
3667         res->vtable = mono_class_proxy_vtable (domain, klass);
3668
3669         return res;
3670 }
3671
3672 /* System.Environment */
3673
3674 static MonoString *
3675 ves_icall_System_Environment_get_MachineName (void)
3676 {
3677 #if defined (PLATFORM_WIN32)
3678         gunichar2 *buf;
3679         guint32 len;
3680         MonoString *result;
3681
3682         len = MAX_COMPUTERNAME_LENGTH + 1;
3683         buf = g_new (gunichar2, len);
3684
3685         result = NULL;
3686         if (GetComputerName (buf, (PDWORD) &len))
3687                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
3688
3689         g_free (buf);
3690         return result;
3691 #else
3692         gchar *buf;
3693         int len;
3694         MonoString *result;
3695
3696         MONO_ARCH_SAVE_REGS;
3697
3698         len = 256;
3699         buf = g_new (gchar, len);
3700
3701         result = NULL;
3702         if (gethostname (buf, len) == 0)
3703                 result = mono_string_new (mono_domain_get (), buf);
3704         
3705         g_free (buf);
3706         return result;
3707 #endif
3708 }
3709
3710 static int
3711 ves_icall_System_Environment_get_Platform (void)
3712 {
3713         MONO_ARCH_SAVE_REGS;
3714
3715 #if defined (PLATFORM_WIN32)
3716         /* Win32NT */
3717         return 2;
3718 #else
3719         /* Unix */
3720         return 128;
3721 #endif
3722 }
3723
3724 static MonoString *
3725 ves_icall_System_Environment_get_NewLine (void)
3726 {
3727         MONO_ARCH_SAVE_REGS;
3728
3729 #if defined (PLATFORM_WIN32)
3730         return mono_string_new (mono_domain_get (), "\r\n");
3731 #else
3732         return mono_string_new (mono_domain_get (), "\n");
3733 #endif
3734 }
3735
3736 static MonoString *
3737 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
3738 {
3739         const gchar *value;
3740         gchar *utf8_name;
3741
3742         MONO_ARCH_SAVE_REGS;
3743
3744         if (name == NULL)
3745                 return NULL;
3746
3747         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
3748         value = g_getenv (utf8_name);
3749         g_free (utf8_name);
3750
3751         if (value == 0)
3752                 return NULL;
3753         
3754         return mono_string_new (mono_domain_get (), value);
3755 }
3756
3757 /*
3758  * There is no standard way to get at environ.
3759  */
3760 #ifndef _MSC_VER
3761 extern
3762 #endif
3763 char **environ;
3764
3765 static MonoArray *
3766 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
3767 {
3768         MonoArray *names;
3769         MonoDomain *domain;
3770         MonoString *str;
3771         gchar **e, **parts;
3772         int n;
3773
3774         MONO_ARCH_SAVE_REGS;
3775
3776         n = 0;
3777         for (e = environ; *e != 0; ++ e)
3778                 ++ n;
3779
3780         domain = mono_domain_get ();
3781         names = mono_array_new (domain, mono_defaults.string_class, n);
3782
3783         n = 0;
3784         for (e = environ; *e != 0; ++ e) {
3785                 parts = g_strsplit (*e, "=", 2);
3786                 if (*parts != 0) {
3787                         str = mono_string_new (domain, *parts);
3788                         mono_array_set (names, MonoString *, n, str);
3789                 }
3790
3791                 g_strfreev (parts);
3792
3793                 ++ n;
3794         }
3795
3796         return names;
3797 }
3798
3799 /*
3800  * Returns the number of milliseconds elapsed since the system started.
3801  */
3802 static gint32
3803 ves_icall_System_Environment_get_TickCount (void)
3804 {
3805 #if defined (PLATFORM_WIN32)
3806         return GetTickCount();
3807 #else
3808         struct timeval tv;
3809         struct timezone tz;
3810         gint32 res;
3811
3812         MONO_ARCH_SAVE_REGS;
3813
3814         res = (gint32) gettimeofday (&tv, &tz);
3815
3816         if (res != -1)
3817                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
3818         return res;
3819 #endif
3820 }
3821
3822
3823 static void
3824 ves_icall_System_Environment_Exit (int result)
3825 {
3826         MONO_ARCH_SAVE_REGS;
3827
3828         mono_runtime_quit ();
3829
3830         /* we may need to do some cleanup here... */
3831         exit (result);
3832 }
3833
3834 static MonoString*
3835 ves_icall_System_Text_Encoding_InternalCodePage (void) 
3836 {
3837         const char *cset;
3838
3839         MONO_ARCH_SAVE_REGS;
3840
3841         g_get_charset (&cset);
3842         /* g_print ("charset: %s\n", cset); */
3843         /* handle some common aliases */
3844         switch (*cset) {
3845         case 'A':
3846                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
3847                         cset = "us-ascii";
3848                 break;
3849         }
3850         return mono_string_new (mono_domain_get (), cset);
3851 }
3852
3853 static void
3854 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
3855                                          MonoReflectionMethod *method,
3856                                          MonoArray *out_args)
3857 {
3858         MONO_ARCH_SAVE_REGS;
3859
3860         mono_message_init (mono_object_domain (this), this, method, out_args);
3861 }
3862
3863 static MonoBoolean
3864 ves_icall_IsTransparentProxy (MonoObject *proxy)
3865 {
3866         MONO_ARCH_SAVE_REGS;
3867
3868         if (!proxy)
3869                 return 0;
3870
3871         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
3872                 return 1;
3873
3874         return 0;
3875 }
3876
3877 static void
3878 ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
3879 {
3880         MonoClass *klass;
3881         MonoVTable* vtable;
3882
3883         MONO_ARCH_SAVE_REGS;
3884
3885         klass = mono_class_from_mono_type (type->type);
3886         vtable = mono_class_vtable (mono_domain_get (), klass);
3887
3888         if (enable) vtable->remote = 1;
3889         else vtable->remote = 0;
3890 }
3891
3892 static MonoObject *
3893 ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionType *type)
3894 {
3895         MonoClass *klass;
3896         MonoDomain *domain;
3897         
3898         MONO_ARCH_SAVE_REGS;
3899
3900         domain = mono_object_domain (type);
3901         klass = mono_class_from_mono_type (type->type);
3902
3903         if (klass->rank >= 1) {
3904                 g_assert (klass->rank == 1);
3905                 return (MonoObject *) mono_array_new (domain, klass->element_class, 0);
3906         } else {
3907                 // Bypass remoting object creation check
3908                 return mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
3909         }
3910 }
3911
3912 static MonoString *
3913 ves_icall_System_IO_get_temp_path (void)
3914 {
3915         MONO_ARCH_SAVE_REGS;
3916
3917         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
3918 }
3919
3920 static gpointer
3921 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
3922 {
3923         MONO_ARCH_SAVE_REGS;
3924
3925         return mono_compile_method (method);
3926 }
3927
3928 char const * mono_cfg_dir = "";
3929
3930 void    
3931 mono_install_get_config_dir (void)
3932 {
3933 #ifdef PLATFORM_WIN32
3934   int i;
3935 #endif
3936
3937   mono_cfg_dir = getenv ("MONO_CFG_DIR");
3938
3939   if (!mono_cfg_dir) {
3940 #ifndef PLATFORM_WIN32
3941     mono_cfg_dir = MONO_CFG_DIR;
3942 #else
3943     mono_cfg_dir = g_strdup (MONO_CFG_DIR);
3944     for (i = strlen (mono_cfg_dir) - 1; i >= 0; i--) {
3945         if (mono_cfg_dir [i] == '/')
3946             ((char*) mono_cfg_dir) [i] = '\\';
3947     }
3948 #endif
3949   }
3950 }
3951
3952
3953 static MonoString *
3954 ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
3955 {
3956         MonoString *mcpath;
3957         gchar *path;
3958
3959         MONO_ARCH_SAVE_REGS;
3960
3961         path = g_build_path (G_DIR_SEPARATOR_S, mono_cfg_dir, "mono", "machine.config", NULL);
3962
3963 #if defined (PLATFORM_WIN32)
3964         /* Avoid mixing '/' and '\\' */
3965         {
3966                 gint i;
3967                 for (i = strlen (path) - 1; i >= 0; i--)
3968                         if (path [i] == '/')
3969                                 path [i] = '\\';
3970         }
3971 #endif
3972         mcpath = mono_string_new (mono_domain_get (), path);
3973         g_free (path);
3974
3975         return mcpath;
3976 }
3977
3978 static MonoString *
3979 ves_icall_System_Web_Util_ICalls_get_machine_install_dir (void)
3980 {
3981         MonoString *ipath;
3982         gchar *path;
3983
3984         MONO_ARCH_SAVE_REGS;
3985
3986         path = g_path_get_dirname (mono_cfg_dir);
3987
3988 #if defined (PLATFORM_WIN32)
3989         /* Avoid mixing '/' and '\\' */
3990         {
3991                 gint i;
3992                 for (i = strlen (path) - 1; i >= 0; i--)
3993                         if (path [i] == '/')
3994                                 path [i] = '\\';
3995         }
3996 #endif
3997         ipath = mono_string_new (mono_domain_get (), path);
3998         g_free (path);
3999
4000         return ipath;
4001 }
4002
4003 static void
4004 ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString (MonoString *message)
4005 {
4006 #if defined (PLATFORM_WIN32)
4007         static void (*output_debug) (gchar *);
4008         static gboolean tried_loading = FALSE;
4009
4010         MONO_ARCH_SAVE_REGS;
4011
4012         if (!tried_loading && output_debug == NULL) {
4013                 GModule *k32;
4014
4015                 tried_loading = TRUE;
4016                 k32 = g_module_open ("kernel32", G_MODULE_BIND_LAZY);
4017                 if (!k32) {
4018                         gchar *error = g_strdup (g_module_error ());
4019                         g_warning ("Failed to load kernel32.dll: %s\n", error);
4020                         g_free (error);
4021                         return;
4022                 }
4023
4024                 g_module_symbol (k32, "OutputDebugStringW", (gpointer *) &output_debug);
4025                 if (!output_debug) {
4026                         gchar *error = g_strdup (g_module_error ());
4027                         g_warning ("Failed to load OutputDebugStringW: %s\n", error);
4028                         g_free (error);
4029                         return;
4030                 }
4031         }
4032
4033         if (output_debug == NULL)
4034                 return;
4035         
4036         output_debug (mono_string_chars (message));
4037 #else
4038         g_warning ("WriteWindowsDebugString called and PLATFORM_WIN32 not defined!\n");
4039 #endif
4040 }
4041
4042 /* Only used for value types */
4043 static MonoObject *
4044 ves_icall_System_Activator_CreateInstanceInternal (MonoReflectionType *type)
4045 {
4046         MonoClass *klass;
4047         MonoDomain *domain;
4048         
4049         MONO_ARCH_SAVE_REGS;
4050
4051         domain = mono_object_domain (type);
4052         klass = mono_class_from_mono_type (type->type);
4053
4054         return mono_object_new (domain, klass);
4055 }
4056
4057 static MonoReflectionMethod *
4058 ves_icall_MonoMethod_get_base_definition (MonoReflectionMethod *m)
4059 {
4060         MonoClass *klass;
4061         MonoMethod *method = m->method;
4062         MonoMethod *result = NULL;
4063
4064         MONO_ARCH_SAVE_REGS;
4065
4066         if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
4067              method->klass->flags & TYPE_ATTRIBUTE_INTERFACE ||
4068              method->flags & METHOD_ATTRIBUTE_NEW_SLOT)
4069                 return m;
4070
4071         if (method->klass == NULL || (klass = method->klass->parent) == NULL)
4072                 return m;
4073
4074         while (result == NULL && klass != NULL && (klass->vtable_size > method->slot))
4075         {
4076                 result = klass->vtable [method->slot];
4077                 if (result == NULL) {
4078                         /* It is an abstract method */
4079                         int i;
4080                         for (i=0; i<klass->method.count; i++) {
4081                                 if (klass->methods [i]->slot == method->slot) {
4082                                         result = klass->methods [i];
4083                                         break;
4084                                 }
4085                         }
4086                 }
4087                 klass = klass->parent;
4088         }
4089
4090         if (result == NULL)
4091                 return m;
4092
4093         return mono_method_get_object (mono_domain_get (), result, NULL);
4094 }
4095
4096 static void
4097 mono_ArgIterator_Setup (MonoArgIterator *iter, char* argsp, char* start)
4098 {
4099         MONO_ARCH_SAVE_REGS;
4100
4101         iter->sig = *(MonoMethodSignature**)argsp;
4102         
4103         g_assert (iter->sig->sentinelpos <= iter->sig->param_count);
4104         g_assert (iter->sig->call_convention == MONO_CALL_VARARG);
4105
4106         iter->next_arg = 0;
4107         /* FIXME: it's not documented what start is exactly... */
4108         iter->args = start? start: argsp + sizeof (gpointer);
4109         iter->num_args = iter->sig->param_count - iter->sig->sentinelpos;
4110
4111         // g_print ("sig %p, param_count: %d, sent: %d\n", iter->sig, iter->sig->param_count, iter->sig->sentinelpos);
4112 }
4113
4114 static MonoTypedRef
4115 mono_ArgIterator_IntGetNextArg (MonoArgIterator *iter)
4116 {
4117         gint i, align, arg_size;
4118         MonoTypedRef res;
4119         MONO_ARCH_SAVE_REGS;
4120
4121         i = iter->sig->sentinelpos + iter->next_arg;
4122
4123         g_assert (i < iter->sig->param_count);
4124
4125         res.type = iter->sig->params [i];
4126         res.klass = mono_class_from_mono_type (res.type);
4127         /* FIXME: endianess issue... */
4128         res.value = iter->args;
4129         arg_size = mono_type_stack_size (res.type, &align);
4130         iter->args = (char*)iter->args + arg_size;
4131         iter->next_arg++;
4132
4133         //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
4134
4135         return res;
4136 }
4137
4138 static MonoTypedRef
4139 mono_ArgIterator_IntGetNextArgT (MonoArgIterator *iter, MonoType *type)
4140 {
4141         gint i, align, arg_size;
4142         MonoTypedRef res;
4143         MONO_ARCH_SAVE_REGS;
4144
4145         i = iter->sig->sentinelpos + iter->next_arg;
4146
4147         g_assert (i < iter->sig->param_count);
4148
4149         while (i < iter->sig->param_count) {
4150                 if (!mono_metadata_type_equal (type, iter->sig->params [i]))
4151                         continue;
4152                 res.type = iter->sig->params [i];
4153                 res.klass = mono_class_from_mono_type (res.type);
4154                 /* FIXME: endianess issue... */
4155                 res.value = iter->args;
4156                 arg_size = mono_type_stack_size (res.type, &align);
4157                 iter->args = (char*)iter->args + arg_size;
4158                 iter->next_arg++;
4159                 //g_print ("returning arg %d, type 0x%02x of size %d at %p\n", i, res.type->type, arg_size, res.value);
4160                 return res;
4161         }
4162         //g_print ("arg type 0x%02x not found\n", res.type->type);
4163
4164         res.type = NULL;
4165         res.value = NULL;
4166         res.klass = NULL;
4167         return res;
4168 }
4169
4170 static MonoType*
4171 mono_ArgIterator_IntGetNextArgType (MonoArgIterator *iter)
4172 {
4173         gint i;
4174         MONO_ARCH_SAVE_REGS;
4175         
4176         i = iter->sig->sentinelpos + iter->next_arg;
4177
4178         g_assert (i < iter->sig->param_count);
4179
4180         return iter->sig->params [i];
4181 }
4182
4183 static MonoObject*
4184 mono_TypedReference_ToObject (MonoTypedRef tref)
4185 {
4186         MONO_ARCH_SAVE_REGS;
4187
4188         if (MONO_TYPE_IS_REFERENCE (tref.type)) {
4189                 MonoObject** objp = tref.value;
4190                 return *objp;
4191         }
4192
4193         return mono_value_box (mono_domain_get (), tref.klass, tref.value);
4194 }
4195
4196 static void
4197 prelink_method (MonoMethod *method)
4198 {
4199         if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
4200                 return;
4201         mono_lookup_pinvoke_call (method);
4202         /* create the wrapper, too? */
4203 }
4204
4205 static void
4206 ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethod *method)
4207 {
4208         MONO_ARCH_SAVE_REGS;
4209         prelink_method (method->method);
4210 }
4211
4212 static void
4213 ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionType *type)
4214 {
4215         MonoClass *klass = mono_class_from_mono_type (type->type);
4216         int i;
4217         MONO_ARCH_SAVE_REGS;
4218
4219         mono_class_init (klass);
4220         for (i = 0; i < klass->method.count; ++i)
4221                 prelink_method (klass->methods [i]);
4222 }
4223
4224 /* icall map */
4225
4226 static gconstpointer icall_map [] = {
4227         /*
4228          * System.Array
4229          */
4230         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
4231         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
4232         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
4233         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
4234         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
4235         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
4236         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
4237         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
4238         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
4239         "System.Array::Clone",            mono_array_clone,
4240
4241         /*
4242          * System.ArgIterator
4243          */
4244         "System.ArgIterator::Setup",                            mono_ArgIterator_Setup,
4245         "System.ArgIterator::IntGetNextArg()",                  mono_ArgIterator_IntGetNextArg,
4246         "System.ArgIterator::IntGetNextArg(intptr)", mono_ArgIterator_IntGetNextArgT,
4247         "System.ArgIterator::IntGetNextArgType",                mono_ArgIterator_IntGetNextArgType,
4248
4249         /*
4250          * System.TypedReference
4251          */
4252         "System.TypedReference::ToObject",                      mono_TypedReference_ToObject,
4253
4254         /*
4255          * System.Object
4256          */
4257         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
4258         "System.Object::GetType", ves_icall_System_Object_GetType,
4259         "System.Object::InternalGetHashCode", ves_icall_System_Object_GetHashCode,
4260         "System.Object::obj_address", ves_icall_System_Object_obj_address,
4261
4262         /*
4263          * System.ValueType
4264          */
4265         "System.ValueType::InternalGetHashCode", ves_icall_System_ValueType_InternalGetHashCode,
4266         "System.ValueType::InternalEquals", ves_icall_System_ValueType_Equals,
4267
4268         /*
4269          * System.String
4270          */
4271         
4272         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
4273         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
4274         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
4275         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
4276         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
4277         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
4278         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
4279         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
4280         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
4281         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
4282         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
4283         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
4284         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
4285         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
4286         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
4287         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
4288         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
4289         "System.String::InternalPad", ves_icall_System_String_InternalPad,
4290         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
4291         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
4292         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
4293         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
4294         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
4295         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
4296         "System.String::get_Chars", ves_icall_System_String_get_Chars,
4297
4298         /*
4299          * System.AppDomain
4300          */
4301         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
4302         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
4303         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
4304         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
4305         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
4306         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
4307         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
4308         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
4309         "System.AppDomain::LoadAssemblyRaw", ves_icall_System_AppDomain_LoadAssemblyRaw,
4310         "System.AppDomain::InternalIsFinalizingForUnload", ves_icall_System_AppDomain_InternalIsFinalizingForUnload,
4311         "System.AppDomain::InternalUnload", ves_icall_System_AppDomain_InternalUnload,
4312         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
4313         "System.AppDomain::InternalSetDomain", ves_icall_System_AppDomain_InternalSetDomain,
4314         "System.AppDomain::InternalSetDomainByID", ves_icall_System_AppDomain_InternalSetDomainByID,
4315         "System.AppDomain::InternalPushDomainRef", ves_icall_System_AppDomain_InternalPushDomainRef,
4316         "System.AppDomain::InternalPushDomainRefByID", ves_icall_System_AppDomain_InternalPushDomainRefByID,
4317         "System.AppDomain::InternalPopDomainRef", ves_icall_System_AppDomain_InternalPopDomainRef,
4318         "System.AppDomain::InternalSetContext", ves_icall_System_AppDomain_InternalSetContext,
4319         "System.AppDomain::InternalGetContext", ves_icall_System_AppDomain_InternalGetContext,
4320         "System.AppDomain::InternalGetDefaultContext", ves_icall_System_AppDomain_InternalGetDefaultContext,
4321         "System.AppDomain::InternalGetProcessGuid", ves_icall_System_AppDomain_InternalGetProcessGuid,
4322
4323         /*
4324          * System.AppDomainSetup
4325          */
4326         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
4327
4328         /*
4329          * System.Double
4330          */
4331         "System.Double::ParseImpl",    mono_double_ParseImpl,
4332         "System.Double::AssertEndianity", ves_icall_System_Double_AssertEndianity,
4333
4334         /*
4335          * System.Decimal
4336          */
4337         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
4338         "System.Decimal::decimal2Int64", mono_decimal2Int64,
4339         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
4340         "System.Decimal::decimalIncr", mono_decimalIncr,
4341         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
4342         "System.Decimal::decimal2double", mono_decimal2double,
4343         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
4344         "System.Decimal::decimalRound", mono_decimalRound,
4345         "System.Decimal::decimalMult", mono_decimalMult,
4346         "System.Decimal::decimalDiv", mono_decimalDiv,
4347         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
4348         "System.Decimal::decimalCompare", mono_decimalCompare,
4349         "System.Decimal::string2decimal", mono_string2decimal,
4350         "System.Decimal::decimal2string", mono_decimal2string,
4351
4352         /*
4353          * ModuleBuilder
4354          */
4355         "System.Reflection.Emit.ModuleBuilder::getUSIndex", mono_image_insert_string,
4356         "System.Reflection.Emit.ModuleBuilder::getToken", ves_icall_ModuleBuilder_getToken,
4357         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
4358         "System.Reflection.Emit.ModuleBuilder::basic_init", mono_image_module_basic_init,
4359         "System.Reflection.Emit.ModuleBuilder::build_metadata", ves_icall_ModuleBuilder_build_metadata,
4360         "System.Reflection.Emit.ModuleBuilder::getDataChunk", ves_icall_ModuleBuilder_getDataChunk,
4361         
4362         /*
4363          * AssemblyBuilder
4364          */
4365         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
4366
4367         /*
4368          * Reflection stuff.
4369          */
4370         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
4371         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
4372         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
4373         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
4374         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
4375         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
4376         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
4377         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
4378         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
4379         "System.Reflection.MonoField::GetParentType", ves_icall_MonoField_GetParentType,
4380         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
4381         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
4382         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
4383         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
4384
4385         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
4386         "System.Reflection.MonoMethod::get_base_definition", ves_icall_MonoMethod_get_base_definition,
4387         
4388         /* System.Enum */
4389
4390         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
4391         "System.Enum::get_value", ves_icall_System_Enum_get_value,
4392         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
4393
4394         /*
4395          * TypeBuilder
4396          */
4397         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
4398         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
4399         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
4400         "System.Reflection.Emit.TypeBuilder::setup_generic_class", mono_reflection_setup_generic_class,
4401
4402         /*
4403          * DynamicMethod
4404          */
4405         "System.Reflection.Emit.DynamicMethod::create_dynamic_method", mono_reflection_create_dynamic_method,
4406
4407         /*
4408          * TypeBuilder generics icalls.
4409          */
4410         "System.Reflection.Emit.TypeBuilder::get_IsGenericParameter", ves_icall_TypeBuilder_get_IsGenericParameter,
4411         "System.Reflection.Emit.TypeBuilder::define_generic_parameter", ves_icall_TypeBuilder_define_generic_parameter,
4412         
4413         /*
4414          * MethodBuilder generic icalls.
4415          */
4416         "System.Reflection.Emit.MethodBuilder::define_generic_parameter", ves_icall_MethodBuilder_define_generic_parameter,
4417
4418         /*
4419          * MonoGenericInst generic icalls.
4420          */
4421         "System.Reflection.MonoGenericInst::inflate_method", mono_reflection_inflate_method_or_ctor,
4422         "System.Reflection.MonoGenericInst::inflate_ctor", mono_reflection_inflate_method_or_ctor,
4423         "System.Reflection.MonoGenericInst::inflate_field", mono_reflection_inflate_field,
4424         
4425         /*
4426          * System.Type
4427          */
4428         "System.Type::internal_from_name", ves_icall_type_from_name,
4429         "System.Type::internal_from_handle", ves_icall_type_from_handle,
4430         "System.MonoType::get_attributes", ves_icall_get_attributes,
4431         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
4432         "System.Type::type_is_assignable_from", ves_icall_type_is_assignable_from,
4433         "System.Type::Equals", ves_icall_type_Equals,
4434         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
4435         "System.Type::GetInterfaceMapData", ves_icall_Type_GetInterfaceMapData,
4436         "System.Type::IsArrayImpl", ves_icall_Type_IsArrayImpl,
4437
4438         /* Type generics icalls */
4439         "System.Type::GetGenericArguments", ves_icall_Type_GetGenericArguments,
4440         "System.Type::GetGenericParameterPosition", ves_icall_Type_GetGenericParameterPosition,
4441         "System.Type::get_IsGenericTypeDefinition", ves_icall_Type_get_IsGenericTypeDefinition,
4442         "System.Type::GetGenericTypeDefinition_impl", ves_icall_Type_GetGenericTypeDefinition_impl,
4443         "System.Type::BindGenericParameters", ves_icall_Type_BindGenericParameters,
4444         "System.Type::get_IsGenericInstance", ves_icall_Type_get_IsGenericInstance,
4445         
4446         "System.MonoType::get_HasGenericArguments", ves_icall_MonoType_get_HasGenericArguments,
4447         "System.MonoType::get_IsGenericParameter", ves_icall_MonoType_get_IsGenericParameter,
4448         "System.MonoType::get_DeclaringMethod", ves_icall_MonoType_get_DeclaringMethod,
4449
4450         /* Method generics icalls */
4451         "System.Reflection.MethodInfo::get_IsGenericMethodDefinition", ves_icall_MethodInfo_get_IsGenericMethodDefinition,
4452         "System.Reflection.MethodInfo::BindGenericParameters", mono_reflection_bind_generic_method_parameters,
4453         "System.Reflection.MonoMethod::GetGenericArguments", ves_icall_MonoMethod_GetGenericArguments,
4454
4455
4456         /*
4457          * System.Reflection.FieldInfo
4458          */
4459         "System.Reflection.FieldInfo::internal_from_handle", ves_icall_System_Reflection_FieldInfo_internal_from_handle,
4460
4461         /*
4462          * System.Runtime.CompilerServices.RuntimeHelpers
4463          */
4464         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
4465         "System.Runtime.CompilerServices.RuntimeHelpers::GetOffsetToStringData", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData,
4466         "System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue,
4467         "System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor,
4468         
4469         /*
4470          * System.Threading
4471          */
4472         "System.Threading.Thread::Abort_internal(object)", ves_icall_System_Threading_Thread_Abort,
4473         "System.Threading.Thread::ResetAbort_internal()", ves_icall_System_Threading_Thread_ResetAbort,
4474         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
4475         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
4476         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
4477         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
4478         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
4479         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
4480         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
4481         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
4482         "System.Threading.Thread::GetDomainID", ves_icall_System_Threading_Thread_GetDomainID,
4483         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
4484         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
4485         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
4486         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
4487         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
4488         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
4489         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
4490         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
4491         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
4492         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
4493         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
4494         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
4495         "System.Threading.NativeEventCalls::CloseEvent_internal", ves_icall_System_Threading_Events_CloseEvent_internal,
4496         "System.Threading.ThreadPool::GetAvailableThreads", ves_icall_System_Threading_ThreadPool_GetAvailableThreads,
4497         "System.Threading.ThreadPool::GetMaxThreads", ves_icall_System_Threading_ThreadPool_GetMaxThreads,
4498         "System.Threading.Thread::VolatileRead(byte&)", ves_icall_System_Threading_Thread_VolatileRead1,
4499         "System.Threading.Thread::VolatileRead(double&)", ves_icall_System_Threading_Thread_VolatileRead8,
4500         "System.Threading.Thread::VolatileRead(short&)", ves_icall_System_Threading_Thread_VolatileRead2,
4501         "System.Threading.Thread::VolatileRead(int&)", ves_icall_System_Threading_Thread_VolatileRead4,
4502         "System.Threading.Thread::VolatileRead(long&)", ves_icall_System_Threading_Thread_VolatileRead8,
4503         "System.Threading.Thread::VolatileRead(IntPtr&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4504         "System.Threading.Thread::VolatileRead(object&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4505         "System.Threading.Thread::VolatileRead(sbyte&)", ves_icall_System_Threading_Thread_VolatileRead1,
4506         "System.Threading.Thread::VolatileRead(float&)", ves_icall_System_Threading_Thread_VolatileRead4,
4507         "System.Threading.Thread::VolatileRead(ushort&)", ves_icall_System_Threading_Thread_VolatileRead2,
4508         "System.Threading.Thread::VolatileRead(uint&)", ves_icall_System_Threading_Thread_VolatileRead2,
4509         "System.Threading.Thread::VolatileRead(ulong&)", ves_icall_System_Threading_Thread_VolatileRead8,
4510         "System.Threading.Thread::VolatileRead(UIntPtr&)", ves_icall_System_Threading_Thread_VolatileReadIntPtr,
4511         "System.Threading.Thread::VolatileWrite(byte&,byte)", ves_icall_System_Threading_Thread_VolatileWrite1,
4512         "System.Threading.Thread::VolatileWrite(double&,double)", ves_icall_System_Threading_Thread_VolatileWrite8,
4513         "System.Threading.Thread::VolatileWrite(short&,short)", ves_icall_System_Threading_Thread_VolatileWrite2,
4514         "System.Threading.Thread::VolatileWrite(int&,int)", ves_icall_System_Threading_Thread_VolatileWrite4,
4515         "System.Threading.Thread::VolatileWrite(long&,long)", ves_icall_System_Threading_Thread_VolatileWrite8,
4516         "System.Threading.Thread::VolatileWrite(IntPtr&,IntPtr)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4517         "System.Threading.Thread::VolatileWrite(object&,object)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4518         "System.Threading.Thread::VolatileWrite(sbyte&,sbyte)", ves_icall_System_Threading_Thread_VolatileWrite1,
4519         "System.Threading.Thread::VolatileWrite(float&,float)", ves_icall_System_Threading_Thread_VolatileWrite4,
4520         "System.Threading.Thread::VolatileWrite(ushort&,ushort)", ves_icall_System_Threading_Thread_VolatileWrite2,
4521         "System.Threading.Thread::VolatileWrite(uint&,uint)", ves_icall_System_Threading_Thread_VolatileWrite2,
4522         "System.Threading.Thread::VolatileWrite(ulong&,ulong)", ves_icall_System_Threading_Thread_VolatileWrite8,
4523         "System.Threading.Thread::VolatileWrite(UIntPtr&,UIntPtr)", ves_icall_System_Threading_Thread_VolatileWriteIntPtr,
4524
4525         /*
4526          * System.Threading.WaitHandle
4527          */
4528         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
4529         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
4530         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
4531
4532         /*
4533          * System.Runtime.InteropServices.Marshal
4534          */
4535         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
4536         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
4537         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
4538         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
4539         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
4540         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
4541         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
4542         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
4543         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
4544         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
4545
4546         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
4547         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
4548         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
4549         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
4550         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
4551         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
4552         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
4553
4554         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
4555         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
4556         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
4557         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
4558         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
4559         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
4560         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
4561         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
4562         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
4563         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
4564         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
4565         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
4566         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
4567         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
4568         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
4569         "System.Runtime.InteropServices.Marshal::Prelink", ves_icall_System_Runtime_InteropServices_Marshal_Prelink,
4570         "System.Runtime.InteropServices.Marshal::PrelinkAll", ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll,
4571
4572
4573         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
4574         "System.Reflection.Assembly::InternalGetType", ves_icall_System_Reflection_Assembly_InternalGetType,
4575         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
4576         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
4577         "System.Reflection.Assembly::InternalGetAssemblyName", ves_icall_System_Reflection_Assembly_InternalGetAssemblyName,
4578         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
4579         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
4580         "System.Reflection.Assembly::InternalImageRuntimeVersion", ves_icall_System_Reflection_Assembly_InternalImageRuntimeVersion,
4581         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
4582         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
4583         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
4584         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
4585         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
4586         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
4587         "System.Reflection.Assembly::GetManifestResourceInfoInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInfoInternal,
4588         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
4589         "System.Reflection.Assembly::GetReferencedAssemblies", ves_icall_System_Reflection_Assembly_GetReferencedAssemblies,
4590         "System.Reflection.Assembly::GetNamespaces", ves_icall_System_Reflection_Assembly_GetNamespaces,
4591         "System.Reflection.Assembly::GetModulesInternal", ves_icall_System_Reflection_Assembly_GetModulesInternal,
4592
4593         /*
4594          * System.Reflection.Module
4595          */
4596         "System.Reflection.Module::GetGlobalType", ves_icall_System_Reflection_Module_GetGlobalType,
4597         "System.Reflection.Module::GetGuidInternal", ves_icall_System_Reflection_Module_GetGuidInternal,
4598         "System.Reflection.Module::InternalGetTypes", ves_icall_System_Reflection_Module_InternalGetTypes,
4599
4600         /*
4601          * System.MonoType.
4602          */
4603         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
4604         "System.MonoType::type_from_obj", mono_type_type_from_obj,
4605         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
4606         "System.MonoType::get_type_info", ves_icall_get_type_info,
4607         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
4608         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
4609         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
4610         "System.MonoType::IsPrimitiveImpl", ves_icall_type_isprimitive,
4611         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
4612         "System.MonoType::GetField", ves_icall_Type_GetField,
4613         "System.MonoType::GetFields", ves_icall_Type_GetFields,
4614         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
4615         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
4616         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
4617         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
4618         "System.MonoType::InternalGetEvent", ves_icall_MonoType_GetEvent,
4619         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
4620         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
4621         "System.MonoType::GetNestedType", ves_icall_Type_GetNestedType,
4622
4623         /*
4624          * System.Net.Sockets I/O Services
4625          */
4626         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
4627         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
4628         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
4629         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
4630         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
4631         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
4632         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
4633         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
4634         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
4635         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
4636         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
4637         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
4638         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
4639         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
4640         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
4641         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
4642         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
4643         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
4644         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
4645         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
4646         "System.Net.Dns::GetHostByName_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByName_internal,
4647         "System.Net.Dns::GetHostByAddr_internal(string,string&,string[]&,string[]&)", ves_icall_System_Net_Dns_GetHostByAddr_internal,
4648         "System.Net.Dns::GetHostName_internal(string&)", ves_icall_System_Net_Dns_GetHostName_internal,
4649
4650         /*
4651          * System.Char
4652          */
4653         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
4654         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
4655         "System.Char::IsControl", ves_icall_System_Char_IsControl,
4656         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
4657         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
4658         "System.Char::IsLower", ves_icall_System_Char_IsLower,
4659         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
4660         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
4661         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
4662         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
4663         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
4664         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
4665         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
4666         "System.Char::ToLower", ves_icall_System_Char_ToLower,
4667         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
4668
4669         /*
4670          * System.Text.Encoding
4671          */
4672         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
4673
4674         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
4675         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
4676
4677         /*
4678          * System.GC
4679          */
4680         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
4681         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
4682         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
4683         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
4684         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
4685         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
4686         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
4687         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
4688         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
4689         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
4690
4691         /*
4692          * System.Security.Cryptography calls
4693          */
4694
4695          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes,
4696          "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes,
4697         
4698         /*
4699          * System.Buffer
4700          */
4701         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
4702         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
4703         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
4704         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
4705         
4706         /*
4707          * System.IO.MonoIO
4708          */
4709         "System.IO.MonoIO::CreateDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CreateDirectory,
4710         "System.IO.MonoIO::RemoveDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_RemoveDirectory,
4711         "System.IO.MonoIO::FindFirstFile(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindFirstFile,
4712         "System.IO.MonoIO::FindNextFile(intptr,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindNextFile,
4713         "System.IO.MonoIO::FindClose(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_FindClose,
4714         "System.IO.MonoIO::GetCurrentDirectory(System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
4715         "System.IO.MonoIO::SetCurrentDirectory(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
4716         "System.IO.MonoIO::MoveFile(string,string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_MoveFile,
4717         "System.IO.MonoIO::CopyFile(string,string,bool,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_CopyFile,
4718         "System.IO.MonoIO::DeleteFile(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_DeleteFile,
4719         "System.IO.MonoIO::GetFileAttributes(string,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileAttributes,
4720         "System.IO.MonoIO::SetFileAttributes(string,System.IO.FileAttributes,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileAttributes,
4721         "System.IO.MonoIO::GetFileType(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileType,
4722         "System.IO.MonoIO::GetFileStat(string,System.IO.MonoIOStat&,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetFileStat,
4723         "System.IO.MonoIO::Open(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Open,
4724         "System.IO.MonoIO::Close(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Close,
4725         "System.IO.MonoIO::Read(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Read,
4726         "System.IO.MonoIO::Write(intptr,byte[],int,int,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Write,
4727         "System.IO.MonoIO::Seek(intptr,long,System.IO.SeekOrigin,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Seek,
4728         "System.IO.MonoIO::GetLength(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_GetLength,
4729         "System.IO.MonoIO::SetLength(intptr,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetLength,
4730         "System.IO.MonoIO::SetFileTime(intptr,long,long,long,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_SetFileTime,
4731         "System.IO.MonoIO::Flush(intptr,System.IO.MonoIOError&)", ves_icall_System_IO_MonoIO_Flush,
4732         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
4733         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
4734         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
4735         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
4736         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
4737         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
4738         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
4739         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
4740         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
4741         "System.IO.MonoIO::GetTempPath(string&)", ves_icall_System_IO_MonoIO_GetTempPath,
4742
4743         /*
4744          * System.Math
4745          */
4746         "System.Math::Floor", ves_icall_System_Math_Floor,
4747         "System.Math::Round", ves_icall_System_Math_Round,
4748         "System.Math::Round2", ves_icall_System_Math_Round2,
4749         "System.Math::Sin", ves_icall_System_Math_Sin,
4750         "System.Math::Cos", ves_icall_System_Math_Cos,
4751         "System.Math::Tan", ves_icall_System_Math_Tan,
4752         "System.Math::Sinh", ves_icall_System_Math_Sinh,
4753         "System.Math::Cosh", ves_icall_System_Math_Cosh,
4754         "System.Math::Tanh", ves_icall_System_Math_Tanh,
4755         "System.Math::Acos", ves_icall_System_Math_Acos,
4756         "System.Math::Asin", ves_icall_System_Math_Asin,
4757         "System.Math::Atan", ves_icall_System_Math_Atan,
4758         "System.Math::Atan2", ves_icall_System_Math_Atan2,
4759         "System.Math::Exp", ves_icall_System_Math_Exp,
4760         "System.Math::Log", ves_icall_System_Math_Log,
4761         "System.Math::Log10", ves_icall_System_Math_Log10,
4762         "System.Math::Pow", ves_icall_System_Math_Pow,
4763         "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
4764
4765         /*
4766          * System.Environment
4767          */
4768         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
4769         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
4770         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
4771         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
4772         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
4773         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
4774         "System.Environment::Exit", ves_icall_System_Environment_Exit,
4775         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
4776         "System.Environment::get_ExitCode", mono_environment_exitcode_get,
4777         "System.Environment::set_ExitCode", mono_environment_exitcode_set,
4778         "System.Environment::GetMachineConfigPath",     ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
4779
4780         /*
4781          * System.Runtime.Remoting
4782          */     
4783         "System.Runtime.Remoting.RemotingServices::InternalExecute",
4784         ves_icall_InternalExecute,
4785         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
4786         ves_icall_IsTransparentProxy,
4787
4788         /*
4789          * System.Runtime.Remoting.Activation
4790          */     
4791         "System.Runtime.Remoting.Activation.ActivationServices::AllocateUninitializedClassInstance",
4792         ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance,
4793         "System.Runtime.Remoting.Activation.ActivationServices::EnableProxyActivation",
4794         ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation,
4795
4796         /*
4797          * System.Runtime.Remoting.Messaging
4798          */     
4799         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
4800         ves_icall_MonoMethodMessage_InitMessage,
4801         
4802         /*
4803          * System.Runtime.Remoting.Proxies
4804          */     
4805         "System.Runtime.Remoting.Proxies.RealProxy::InternalGetTransparentProxy", 
4806         ves_icall_Remoting_RealProxy_GetTransparentProxy,
4807
4808         /*
4809          * System.Threading.Interlocked
4810          */
4811         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
4812         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
4813         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
4814         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
4815         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
4816         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
4817         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
4818         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
4819         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
4820         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
4821         "System.Threading.Thread::current_lcid()", ves_icall_System_Threading_Thread_current_lcid,
4822
4823         /*
4824          * System.Diagnostics.Process
4825          */
4826         "System.Diagnostics.Process::GetProcess_internal(int)", ves_icall_System_Diagnostics_Process_GetProcess_internal,
4827         "System.Diagnostics.Process::GetProcesses_internal()", ves_icall_System_Diagnostics_Process_GetProcesses_internal,
4828         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
4829         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
4830         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
4831         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,System.Diagnostics.Process/ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
4832         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
4833         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
4834         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
4835         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
4836         "System.Diagnostics.Process::ProcessName_internal(intptr)", ves_icall_System_Diagnostics_Process_ProcessName_internal,
4837         "System.Diagnostics.Process::GetWorkingSet_internal(intptr,int&,int&)", ves_icall_System_Diagnostics_Process_GetWorkingSet_internal,
4838         "System.Diagnostics.Process::SetWorkingSet_internal(intptr,int,int,bool)", ves_icall_System_Diagnostics_Process_SetWorkingSet_internal,
4839         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
4840
4841         /* 
4842          * System.Delegate
4843          */
4844         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
4845
4846         /*
4847          * System.IO.Path
4848          */
4849         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
4850
4851         /*
4852          * Private icalls for the Mono Debugger
4853          */
4854         "System.Reflection.Assembly::MonoDebugger_GetMethod",
4855         ves_icall_MonoDebugger_GetMethod,
4856
4857         "System.Reflection.Assembly::MonoDebugger_GetMethodToken",
4858         ves_icall_MonoDebugger_GetMethodToken,
4859
4860         "System.Reflection.Assembly::MonoDebugger_GetLocalTypeFromSignature",
4861         ves_icall_MonoDebugger_GetLocalTypeFromSignature,
4862
4863         "System.Reflection.Assembly::MonoDebugger_GetType",
4864         ves_icall_MonoDebugger_GetType,
4865
4866         /*
4867          * System.Configuration
4868          */
4869         "System.Configuration.DefaultConfig::get_machine_config_path",
4870         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
4871
4872         /*
4873          * System.Diagnostics.DefaultTraceListener
4874          */
4875         "System.Diagnostics.DefaultTraceListener::WriteWindowsDebugString",
4876         ves_icall_System_Diagnostics_DefaultTraceListener_WriteWindowsDebugString,
4877         /*
4878          * System.Activator
4879          */
4880         "System.Activator::CreateInstanceInternal",
4881         ves_icall_System_Activator_CreateInstanceInternal,
4882
4883         /* 
4884          * System.Web
4885          */
4886         "System.Web.Util.ICalls::GetMachineConfigPath",
4887         ves_icall_System_Configuration_DefaultConfig_get_machine_config_path,
4888
4889         "System.Web.Util.ICalls::GetMachineInstallDirectory",
4890         ves_icall_System_Web_Util_ICalls_get_machine_install_dir,
4891
4892         /*
4893          * System.Globalization
4894          */
4895         "System.Globalization.CultureInfo::construct_internal_locale(string)", ves_icall_System_Globalization_CultureInfo_construct_internal_locale,
4896         "System.Globalization.CultureInfo::construct_compareinfo(object,string)", ves_icall_System_Globalization_CultureInfo_construct_compareinfo,
4897         "System.Globalization.CompareInfo::internal_compare(string,string,System.Globalization.CompareOptions)", ves_icall_System_Globalization_CompareInfo_internal_compare,
4898         "System.Globalization.CompareInfo::free_internal_collator()", ves_icall_System_Globalization_CompareInfo_free_internal_collator,
4899         "System.Globalization.CompareInfo::assign_sortkey(object,string,System.Globalization.CompareOptions)", ves_icall_System_Globalization_CompareInfo_assign_sortkey,
4900         "System.Globalization.CompareInfo::internal_index(string,int,int,string,System.Globalization.CompareOptions,bool)", ves_icall_System_Globalization_CompareInfo_internal_index,
4901         "System.String::InternalReplace(string,string,System.Globalization.CompareInfo)", ves_icall_System_String_InternalReplace_Str_Comp,
4902         "System.String::InternalToLower(System.Globalization.CultureInfo)", ves_icall_System_String_InternalToLower_Comp,
4903         "System.String::InternalToUpper(System.Globalization.CultureInfo)", ves_icall_System_String_InternalToUpper_Comp,
4904
4905         /*
4906          * add other internal calls here
4907          */
4908
4909         /* These will be deleted after the next release */
4910         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
4911         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
4912         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
4913         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
4914         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
4915         "System.String::InternalCompare(string,int,string,int,int,int)", ves_icall_System_String_InternalCompareStr_N,
4916         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
4917         "System.String::InternalToLower()", ves_icall_System_String_InternalToLower,
4918         "System.String::InternalToUpper()", ves_icall_System_String_InternalToUpper,
4919
4920         NULL, NULL
4921 };
4922
4923 void
4924 mono_init_icall (void)
4925 {
4926         const char *name;
4927         int i = 0;
4928
4929         while ((name = icall_map [i])) {
4930                 mono_add_internal_call (name, icall_map [i+1]);
4931                 i += 2;
4932         }
4933        
4934 }
4935
4936