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