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