2002-09-20 Martin Baulig <martin@gnome.org>
[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/reflection.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/file-io.h>
29 #include <mono/metadata/socket-io.h>
30 #include <mono/metadata/mono-endian.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/unicode.h>
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/marshal.h>
35 #include <mono/metadata/gc.h>
36 #include <mono/metadata/rand.h>
37 #include <mono/metadata/sysmath.h>
38 #include <mono/metadata/string-icalls.h>
39 #include <mono/metadata/debug-mono-symfile.h>
40 #include <mono/metadata/process.h>
41 #include <mono/io-layer/io-layer.h>
42 #include <mono/utils/strtod.h>
43
44 #if defined (PLATFORM_WIN32)
45 #include <windows.h>
46 #endif
47 #include "decimal.h"
48
49 static MonoString *
50 mono_double_ToStringImpl (double value)
51 {
52         /* FIXME: Handle formats, etc. */
53         MonoString *s;
54         gchar *retVal;
55         retVal = g_strdup_printf ("%.15g", value);
56         s = mono_string_new (mono_domain_get (), retVal);
57         g_free (retVal);
58         return s;
59 }
60
61 /*
62  * We expect a pointer to a char, not a string
63  */
64 static double
65 mono_double_ParseImpl (char *ptr)
66 {
67         return bsd_strtod (ptr, NULL);
68 }
69
70 static MonoString *
71 mono_float_ToStringImpl (float value)
72 {
73         return mono_double_ToStringImpl (value);
74 }
75
76 static MonoObject *
77 ves_icall_System_Array_GetValueImpl (MonoObject *this, guint32 pos)
78 {
79         MonoClass *ac;
80         MonoArray *ao;
81         gint32 esize;
82         gpointer *ea;
83
84         ao = (MonoArray *)this;
85         ac = (MonoClass *)ao->obj.vtable->klass;
86
87         esize = mono_array_element_size (ac);
88         ea = (gpointer*)((char*)ao->vector + (pos * esize));
89
90         if (ac->element_class->valuetype)
91                 return mono_value_box (this->vtable->domain, ac->element_class, ea);
92         else
93                 return *ea;
94 }
95
96 static MonoObject *
97 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
98 {
99         MonoClass *ac, *ic;
100         MonoArray *ao, *io;
101         gint32 i, pos, *ind;
102
103         MONO_CHECK_ARG_NULL (idxs);
104
105         io = (MonoArray *)idxs;
106         ic = (MonoClass *)io->obj.vtable->klass;
107         
108         ao = (MonoArray *)this;
109         ac = (MonoClass *)ao->obj.vtable->klass;
110
111         g_assert (ic->rank == 1);
112         if (io->bounds != NULL || io->max_length !=  ac->rank)
113                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
114
115         ind = (guint32 *)io->vector;
116
117         if (ao->bounds == NULL) {
118                 if (*ind < 0 || *ind >= ao->max_length)
119                         mono_raise_exception (mono_get_exception_index_out_of_range ());
120
121                 return ves_icall_System_Array_GetValueImpl (this, *ind);
122         }
123         
124         for (i = 0; i < ac->rank; i++)
125                 if ((ind [i] < ao->bounds [i].lower_bound) ||
126                     (ind [i] >= ao->bounds [i].length + ao->bounds [i].lower_bound))
127                         mono_raise_exception (mono_get_exception_index_out_of_range ());
128
129         pos = ind [0] - ao->bounds [0].lower_bound;
130         for (i = 1; i < ac->rank; i++)
131                 pos = pos*ao->bounds [i].length + ind [i] - 
132                         ao->bounds [i].lower_bound;
133
134         return ves_icall_System_Array_GetValueImpl (this, pos);
135 }
136
137 static void
138 ves_icall_System_Array_SetValueImpl (MonoArray *this, MonoObject *value, guint32 pos)
139 {
140         MonoClass *ac, *vc, *ec;
141         gint32 esize, vsize;
142         gpointer *ea, *va;
143
144         guint64 u64;
145         gint64 i64;
146         gdouble r64;
147
148         if (value)
149                 vc = value->vtable->klass;
150         else
151                 vc = NULL;
152
153         ac = this->obj.vtable->klass;
154         ec = ac->element_class;
155
156         esize = mono_array_element_size (ac);
157         ea = (gpointer*)((char*)this->vector + (pos * esize));
158         va = (gpointer*)((char*)value + sizeof (MonoObject));
159
160         if (!value) {
161                 memset (ea, 0,  esize);
162                 return;
163         }
164
165 #define NO_WIDENING_CONVERSION G_STMT_START{\
166         mono_raise_exception (mono_get_exception_argument ( \
167                 "value", "not a widening conversion")); \
168 }G_STMT_END
169
170 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{\
171         if (esize < vsize + (extra)) \
172                 mono_raise_exception (mono_get_exception_argument ( \
173                         "value", "not a widening conversion")); \
174 }G_STMT_END
175
176 #define INVALID_CAST G_STMT_START{\
177         mono_raise_exception (mono_get_exception_invalid_cast ()); \
178 }G_STMT_END
179
180         /* Check element (destination) type. */
181         switch (ec->byval_arg.type) {
182         case MONO_TYPE_STRING:
183                 switch (vc->byval_arg.type) {
184                 case MONO_TYPE_STRING:
185                         break;
186                 default:
187                         INVALID_CAST;
188                 }
189                 break;
190         case MONO_TYPE_BOOLEAN:
191                 switch (vc->byval_arg.type) {
192                 case MONO_TYPE_BOOLEAN:
193                         break;
194                 case MONO_TYPE_CHAR:
195                 case MONO_TYPE_U1:
196                 case MONO_TYPE_U2:
197                 case MONO_TYPE_U4:
198                 case MONO_TYPE_U8:
199                 case MONO_TYPE_I1:
200                 case MONO_TYPE_I2:
201                 case MONO_TYPE_I4:
202                 case MONO_TYPE_I8:
203                 case MONO_TYPE_R4:
204                 case MONO_TYPE_R8:
205                         NO_WIDENING_CONVERSION;
206                 default:
207                         INVALID_CAST;
208                 }
209                 break;
210         }
211
212         if (!ec->valuetype) {
213                 *ea = (gpointer)value;
214                 return;
215         }
216
217         if (mono_object_isinst (value, ec)) {
218                 memcpy (ea, (char *)value + sizeof (MonoObject), esize);
219                 return;
220         }
221
222         if (!vc->valuetype)
223                 INVALID_CAST;
224
225         vsize = mono_class_instance_size (vc) - sizeof (MonoObject);
226
227 #if 0
228         g_message (G_STRLOC ": %d (%d) <= %d (%d)",
229                    ec->byval_arg.type, esize,
230                    vc->byval_arg.type, vsize);
231 #endif
232
233 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
234         switch (vc->byval_arg.type) { \
235         case MONO_TYPE_U1: \
236         case MONO_TYPE_U2: \
237         case MONO_TYPE_U4: \
238         case MONO_TYPE_U8: \
239         case MONO_TYPE_CHAR: \
240                 CHECK_WIDENING_CONVERSION(0); \
241                 *(etype *) ea = (etype) u64; \
242                 return; \
243         /* You can't assign a signed value to an unsigned array. */ \
244         case MONO_TYPE_I1: \
245         case MONO_TYPE_I2: \
246         case MONO_TYPE_I4: \
247         case MONO_TYPE_I8: \
248         /* You can't assign a floating point number to an integer array. */ \
249         case MONO_TYPE_R4: \
250         case MONO_TYPE_R8: \
251                 NO_WIDENING_CONVERSION; \
252         } \
253 }G_STMT_END
254
255 #define ASSIGN_SIGNED(etype) G_STMT_START{\
256         switch (vc->byval_arg.type) { \
257         case MONO_TYPE_I1: \
258         case MONO_TYPE_I2: \
259         case MONO_TYPE_I4: \
260         case MONO_TYPE_I8: \
261                 CHECK_WIDENING_CONVERSION(0); \
262                 *(etype *) ea = (etype) i64; \
263                 return; \
264         /* You can assign an unsigned value to a signed array if the array's */ \
265         /* element size is larger than the value size. */ \
266         case MONO_TYPE_U1: \
267         case MONO_TYPE_U2: \
268         case MONO_TYPE_U4: \
269         case MONO_TYPE_U8: \
270         case MONO_TYPE_CHAR: \
271                 CHECK_WIDENING_CONVERSION(1); \
272                 *(etype *) ea = (etype) u64; \
273                 return; \
274         /* You can't assign a floating point number to an integer array. */ \
275         case MONO_TYPE_R4: \
276         case MONO_TYPE_R8: \
277                 NO_WIDENING_CONVERSION; \
278         } \
279 }G_STMT_END
280
281 #define ASSIGN_REAL(etype) G_STMT_START{\
282         switch (vc->byval_arg.type) { \
283         case MONO_TYPE_R4: \
284         case MONO_TYPE_R8: \
285                 CHECK_WIDENING_CONVERSION(0); \
286                 *(etype *) ea = (etype) r64; \
287                 return; \
288         /* All integer values fit into a floating point array, so we don't */ \
289         /* need to CHECK_WIDENING_CONVERSION here. */ \
290         case MONO_TYPE_I1: \
291         case MONO_TYPE_I2: \
292         case MONO_TYPE_I4: \
293         case MONO_TYPE_I8: \
294                 *(etype *) ea = (etype) i64; \
295                 return; \
296         case MONO_TYPE_U1: \
297         case MONO_TYPE_U2: \
298         case MONO_TYPE_U4: \
299         case MONO_TYPE_U8: \
300         case MONO_TYPE_CHAR: \
301                 *(etype *) ea = (etype) u64; \
302                 return; \
303         } \
304 }G_STMT_END
305
306         switch (vc->byval_arg.type) {
307         case MONO_TYPE_U1:
308                 u64 = *(guint8 *) va;
309                 break;
310         case MONO_TYPE_U2:
311                 u64 = *(guint16 *) va;
312                 break;
313         case MONO_TYPE_U4:
314                 u64 = *(guint32 *) va;
315                 break;
316         case MONO_TYPE_U8:
317                 u64 = *(guint64 *) va;
318                 break;
319         case MONO_TYPE_I1:
320                 i64 = *(gint8 *) va;
321                 break;
322         case MONO_TYPE_I2:
323                 i64 = *(gint16 *) va;
324                 break;
325         case MONO_TYPE_I4:
326                 i64 = *(gint32 *) va;
327                 break;
328         case MONO_TYPE_I8:
329                 i64 = *(gint64 *) va;
330                 break;
331         case MONO_TYPE_R4:
332                 r64 = *(gfloat *) va;
333                 break;
334         case MONO_TYPE_R8:
335                 r64 = *(gdouble *) va;
336                 break;
337         case MONO_TYPE_CHAR:
338                 u64 = *(guint16 *) va;
339                 break;
340         case MONO_TYPE_BOOLEAN:
341                 /* Boolean is only compatible with itself. */
342                 switch (ec->byval_arg.type) {
343                 case MONO_TYPE_CHAR:
344                 case MONO_TYPE_U1:
345                 case MONO_TYPE_U2:
346                 case MONO_TYPE_U4:
347                 case MONO_TYPE_U8:
348                 case MONO_TYPE_I1:
349                 case MONO_TYPE_I2:
350                 case MONO_TYPE_I4:
351                 case MONO_TYPE_I8:
352                 case MONO_TYPE_R4:
353                 case MONO_TYPE_R8:
354                         NO_WIDENING_CONVERSION;
355                 default:
356                         INVALID_CAST;
357                 }
358                 break;
359         }
360
361         /* If we can't do a direct copy, let's try a widening conversion. */
362         switch (ec->byval_arg.type) {
363         case MONO_TYPE_CHAR:
364                 ASSIGN_UNSIGNED (guint16);
365         case MONO_TYPE_U1:
366                 ASSIGN_UNSIGNED (guint8);
367         case MONO_TYPE_U2:
368                 ASSIGN_UNSIGNED (guint16);
369         case MONO_TYPE_U4:
370                 ASSIGN_UNSIGNED (guint32);
371         case MONO_TYPE_U8:
372                 ASSIGN_UNSIGNED (guint64);
373         case MONO_TYPE_I1:
374                 ASSIGN_SIGNED (gint8);
375         case MONO_TYPE_I2:
376                 ASSIGN_SIGNED (gint16);
377         case MONO_TYPE_I4:
378                 ASSIGN_SIGNED (gint32);
379         case MONO_TYPE_I8:
380                 ASSIGN_SIGNED (gint64);
381         case MONO_TYPE_R4:
382                 ASSIGN_REAL (gfloat);
383         case MONO_TYPE_R8:
384                 ASSIGN_REAL (gdouble);
385         }
386
387         INVALID_CAST;
388         /* Not reached, INVALID_CAST does not return. Just to avoid a compiler warning ... */
389         return;
390
391 #undef INVALID_CAST
392 #undef NO_WIDENING_CONVERSION
393 #undef CHECK_WIDENING_CONVERSION
394 #undef ASSIGN_UNSIGNED
395 #undef ASSIGN_SIGNED
396 #undef ASSIGN_REAL
397 }
398
399 static void 
400 ves_icall_System_Array_SetValue (MonoArray *this, MonoObject *value,
401                                  MonoArray *idxs)
402 {
403         MonoClass *ac, *ic;
404         gint32 i, pos, *ind;
405
406         MONO_CHECK_ARG_NULL (idxs);
407
408         ic = idxs->obj.vtable->klass;
409         ac = this->obj.vtable->klass;
410
411         g_assert (ic->rank == 1);
412         if (idxs->bounds != NULL || idxs->max_length != ac->rank)
413                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
414
415         ind = (guint32 *)idxs->vector;
416
417         if (this->bounds == NULL) {
418                 if (*ind < 0 || *ind >= this->max_length)
419                         mono_raise_exception (mono_get_exception_index_out_of_range ());
420
421                 ves_icall_System_Array_SetValueImpl (this, value, *ind);
422                 return;
423         }
424         
425         for (i = 0; i < ac->rank; i++)
426                 if ((ind [i] < this->bounds [i].lower_bound) ||
427                     (ind [i] >= this->bounds [i].length + this->bounds [i].lower_bound))
428                         mono_raise_exception (mono_get_exception_index_out_of_range ());
429
430         pos = ind [0] - this->bounds [0].lower_bound;
431         for (i = 1; i < ac->rank; i++)
432                 pos = pos * this->bounds [i].length + ind [i] - 
433                         this->bounds [i].lower_bound;
434
435         ves_icall_System_Array_SetValueImpl (this, value, pos);
436 }
437
438 static MonoArray *
439 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *lengths, MonoArray *bounds)
440 {
441         MonoClass *aklass;
442         MonoArray *array;
443         gint32 *sizes, i;
444
445         MONO_CHECK_ARG_NULL (type);
446         MONO_CHECK_ARG_NULL (lengths);
447
448         MONO_CHECK_ARG (lengths, mono_array_length (lengths) > 0);
449         if (bounds)
450                 MONO_CHECK_ARG (bounds, mono_array_length (lengths) == mono_array_length (bounds));
451
452         for (i = 0; i < mono_array_length (lengths); i++)
453                 if (mono_array_get (lengths, gint32, i) < 0)
454                         mono_raise_exception (mono_get_exception_argument_out_of_range (NULL));
455
456         aklass = mono_array_class_get (type->type, mono_array_length (lengths));
457
458         sizes = alloca (aklass->rank * sizeof(guint32) * 2);
459         for (i = 0; i < aklass->rank; ++i) {
460                 sizes [i] = mono_array_get (lengths, gint32, i);
461                 if (bounds)
462                         sizes [i + aklass->rank] = mono_array_get (bounds, gint32, i);
463                 else
464                         sizes [i + aklass->rank] = 0;
465         }
466
467         array = mono_array_new_full (mono_domain_get (), aklass, sizes, sizes + aklass->rank);
468
469         return array;
470 }
471
472 static gint32 
473 ves_icall_System_Array_GetRank (MonoObject *this)
474 {
475         return this->vtable->klass->rank;
476 }
477
478 static gint32
479 ves_icall_System_Array_GetLength (MonoArray *this, gint32 dimension)
480 {
481         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
482         if ((dimension < 0) || (dimension >= rank))
483                 mono_raise_exception (mono_get_exception_index_out_of_range ());
484         
485         if (this->bounds == NULL)
486                 return this->max_length;
487         
488         return this->bounds [dimension].length;
489 }
490
491 static gint32
492 ves_icall_System_Array_GetLowerBound (MonoArray *this, gint32 dimension)
493 {
494         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
495         if ((dimension < 0) || (dimension >= rank))
496                 mono_raise_exception (mono_get_exception_index_out_of_range ());
497         
498         if (this->bounds == NULL)
499                 return 0;
500         
501         return this->bounds [dimension].lower_bound;
502 }
503
504 static void
505 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
506 {
507         int element_size = mono_array_element_size (source->obj.vtable->klass);
508         void * dest_addr = mono_array_addr_with_size (dest, element_size, dest_idx);
509         void * source_addr = mono_array_addr_with_size (source, element_size, source_idx);
510
511         g_assert (dest_idx + length <= mono_array_length (dest));
512         g_assert (source_idx + length <= mono_array_length (source));
513         memmove (dest_addr, source_addr, element_size * length);
514 }
515
516 static void
517 ves_icall_InitializeArray (MonoArray *array, MonoClassField *field_handle)
518 {
519         MonoClass *klass = array->obj.vtable->klass;
520         guint32 size = mono_array_element_size (klass);
521         int i;
522
523         if (array->bounds == NULL)
524                 size *= array->max_length;
525         else
526                 for (i = 0; i < klass->rank; ++i) 
527                         size *= array->bounds [i].length;
528
529         memcpy (mono_array_addr (array, char, 0), field_handle->data, size);
530
531 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
532 #define SWAP(n) {\
533         gint i; \
534         guint ## n tmp; \
535         guint ## n *data = (guint ## n *) mono_array_addr (array, char, 0); \
536 \
537         for (i = 0; i < size; i += n/8, data++) { \
538                 tmp = read ## n (data); \
539                 *data = tmp; \
540         } \
541 }
542
543         /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
544
545         switch (klass->element_class->byval_arg.type) {
546         case MONO_TYPE_CHAR:
547         case MONO_TYPE_I2:
548         case MONO_TYPE_U2:
549                 SWAP (16);
550                 break;
551         case MONO_TYPE_I4:
552         case MONO_TYPE_U4:
553                 SWAP (32);
554                 break;
555         case MONO_TYPE_I8:
556         case MONO_TYPE_U8:
557                 SWAP (64);
558                 break;
559         }
560                  
561 #endif
562 }
563
564 static MonoObject *
565 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
566 {
567         return mono_object_clone (this);
568 }
569
570 #if HAVE_BOEHM_GC
571 #define MONO_OBJECT_ALIGNMENT_SHIFT     3
572 #else
573 #define MONO_OBJECT_ALIGNMENT_SHIFT     2
574 #endif
575
576 /*
577  * Return hashcode based on object address. This function will need to be
578  * smarter in the presence of a moving garbage collector, which will cache
579  * the address hash before relocating the object.
580  *
581  * Wang's address-based hash function:
582  *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
583  */
584 static gint32
585 ves_icall_System_Object_GetHashCode (MonoObject *this)
586 {
587         register guint32 key;
588         key = (GPOINTER_TO_UINT (this) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
589
590         return key & 0x7fffffff;
591 }
592
593 /*
594  * A hash function for value types. I have no idea if this is a good hash 
595  * function (its similar to g_str_hash).
596  */
597 static gint32
598 ves_icall_System_ValueType_GetHashCode (MonoObject *this)
599 {
600         gint32 i, size;
601         const char *p;
602         guint h = 0;
603
604         MONO_CHECK_ARG_NULL (this);
605
606         size = this->vtable->klass->instance_size - sizeof (MonoObject);
607
608         p = (const char *)this + sizeof (MonoObject);
609
610         for (i = 0; i < size; i++) {
611                 h = (h << 5) - h + *p;
612                 p++;
613         }
614
615         return h;
616 }
617
618 static MonoBoolean
619 ves_icall_System_ValueType_Equals (MonoObject *this, MonoObject *that)
620 {
621         gint32 size;
622         const char *p, *s;
623
624         MONO_CHECK_ARG_NULL (that);
625
626         if (this->vtable != that->vtable)
627                 return FALSE;
628
629         size = this->vtable->klass->instance_size - sizeof (MonoObject);
630
631         p = (const char *)this + sizeof (MonoObject);
632         s = (const char *)that + sizeof (MonoObject);
633
634         return memcmp (p, s, size)? FALSE: TRUE;
635 }
636
637 static MonoReflectionType *
638 ves_icall_System_Object_GetType (MonoObject *obj)
639 {
640         return mono_type_get_object (mono_domain_get (), &obj->vtable->klass->byval_arg);
641 }
642
643 static void
644 mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
645 {
646         mtype->type = &obj->vtable->klass->byval_arg;
647         g_assert (mtype->type->type);
648 }
649
650 static gint32
651 ves_icall_AssemblyBuilder_getToken (MonoReflectionAssemblyBuilder *assb, MonoObject *obj)
652 {
653         return mono_image_create_token (assb->dynamic_assembly, obj);
654 }
655
656 static gint32
657 ves_icall_AssemblyBuilder_getDataChunk (MonoReflectionAssemblyBuilder *assb, MonoArray *buf, gint32 offset)
658 {
659         int count;
660         MonoDynamicAssembly *ass = assb->dynamic_assembly;
661         char *p = mono_array_addr (buf, char, 0);
662
663         mono_image_create_pefile (assb);
664
665         if (offset >= ass->pefile.index)
666                 return 0;
667         count = mono_array_length (buf);
668         count = MIN (count, ass->pefile.index - offset);
669         
670         memcpy (p, ass->pefile.data + offset, count);
671
672         return count;
673 }
674
675 static gboolean
676 get_get_type_caller (MonoMethod *m, gint32 no, gint32 ilo, gpointer data) {
677         MonoImage **dest = data;
678
679         /* skip icalls and Type::GetType () */
680         if (!m || m->wrapper_type || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
681                         (strcmp (m->name, "GetType") == 0 && m->klass == mono_defaults.monotype_class->parent))
682                 return FALSE;
683         *dest = m->klass->image;
684         return TRUE;
685 }
686
687 static MonoReflectionType*
688 ves_icall_type_from_name (MonoString *name)
689 {
690         MonoImage *image = NULL;
691         MonoType *type;
692         gchar *str;
693
694         mono_stack_walk (get_get_type_caller, &image);
695         str = mono_string_to_utf8 (name);
696         /*g_print ("requested type %s\n", str);*/
697         type = mono_reflection_type_from_name (str, image);
698         g_free (str);
699         if (!type)
700                 return NULL;
701         /*g_print ("got it\n");*/
702         return mono_type_get_object (mono_object_domain (name), type);
703 }
704
705 static MonoReflectionType*
706 ves_icall_type_from_handle (MonoType *handle)
707 {
708         MonoDomain *domain = mono_domain_get (); 
709         MonoClass *klass = mono_class_from_mono_type (handle);
710
711         mono_class_init (klass);
712         return mono_type_get_object (domain, handle);
713 }
714
715 static guint32
716 ves_icall_type_Equals (MonoReflectionType *type, MonoReflectionType *c)
717 {
718         if (type->type && c->type)
719                 return mono_metadata_type_equal (type->type, c->type);
720         g_print ("type equals\n");
721         return 0;
722 }
723
724 /* System.TypeCode */
725 typedef enum {
726         TYPECODE_EMPTY,
727         TYPECODE_OBJECT,
728         TYPECODE_DBNULL,
729         TYPECODE_BOOLEAN,
730         TYPECODE_CHAR,
731         TYPECODE_SBYTE,
732         TYPECODE_BYTE,
733         TYPECODE_INT16,
734         TYPECODE_UINT16,
735         TYPECODE_INT32,
736         TYPECODE_UINT32,
737         TYPECODE_INT64,
738         TYPECODE_UINT64,
739         TYPECODE_SINGLE,
740         TYPECODE_DOUBLE,
741         TYPECODE_DECIMAL,
742         TYPECODE_DATETIME,
743         TYPECODE_STRING = 18
744 } TypeCode;
745
746 static guint32
747 ves_icall_type_GetTypeCode (MonoReflectionType *type)
748 {
749         int t = type->type->type;
750 handle_enum:
751         switch (t) {
752         case MONO_TYPE_VOID:
753                 return TYPECODE_OBJECT;
754         case MONO_TYPE_BOOLEAN:
755                 return TYPECODE_BOOLEAN;
756         case MONO_TYPE_U1:
757                 return TYPECODE_BYTE;
758         case MONO_TYPE_I1:
759                 return TYPECODE_SBYTE;
760         case MONO_TYPE_U2:
761                 return TYPECODE_UINT16;
762         case MONO_TYPE_I2:
763                 return TYPECODE_INT16;
764         case MONO_TYPE_CHAR:
765                 return TYPECODE_CHAR;
766         case MONO_TYPE_PTR:
767         case MONO_TYPE_U:
768         case MONO_TYPE_I:
769                 return TYPECODE_OBJECT;
770         case MONO_TYPE_U4:
771                 return TYPECODE_UINT32;
772         case MONO_TYPE_I4:
773                 return TYPECODE_INT32;
774         case MONO_TYPE_U8:
775                 return TYPECODE_UINT64;
776         case MONO_TYPE_I8:
777                 return TYPECODE_INT64;
778         case MONO_TYPE_R4:
779                 return TYPECODE_SINGLE;
780         case MONO_TYPE_R8:
781                 return TYPECODE_DOUBLE;
782         case MONO_TYPE_VALUETYPE:
783                 if (type->type->data.klass->enumtype) {
784                         t = type->type->data.klass->enum_basetype->type;
785                         goto handle_enum;
786                 } else {
787                         MonoClass *k =  type->type->data.klass;
788                         if (strcmp (k->name_space, "System") == 0) {
789                                 if (strcmp (k->name, "Decimal") == 0)
790                                         return TYPECODE_DECIMAL;
791                                 else if (strcmp (k->name, "DateTime") == 0)
792                                         return TYPECODE_DATETIME;
793                                 else if (strcmp (k->name, "DBNull") == 0)
794                                         return TYPECODE_DBNULL;
795                         }
796                 }
797                 /* handle datetime, dbnull.. */
798                 return TYPECODE_OBJECT;
799         case MONO_TYPE_STRING:
800                 return TYPECODE_STRING;
801         case MONO_TYPE_SZARRAY:
802         case MONO_TYPE_ARRAY:
803         case MONO_TYPE_OBJECT:
804                 return TYPECODE_OBJECT;
805         case MONO_TYPE_CLASS:
806                 return TYPECODE_OBJECT;
807         default:
808                 g_error ("type 0x%02x not handled in GetTypeCode()", t);
809         }
810         return 0;
811 }
812
813 static guint32
814 ves_icall_type_is_subtype_of (MonoReflectionType *type, MonoReflectionType *c, MonoBoolean check_interfaces)
815 {
816         MonoDomain *domain; 
817         MonoClass *klass;
818         MonoClass *klassc;
819
820         g_assert (type != NULL);
821         
822         domain = ((MonoObject *)type)->vtable->domain;
823
824         if (!c) /* FIXME: dont know what do do here */
825                 return 0;
826
827         klass = mono_class_from_mono_type (type->type);
828         klassc = mono_class_from_mono_type (c->type);
829
830         /* cut&paste from mono_object_isinst (): keep in sync */
831         if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
832                 MonoVTable *klass_vt = mono_class_vtable (domain, klass);
833                 if ((klassc->interface_id <= klass->max_interface_id) &&
834                     klass_vt->interface_offsets [klassc->interface_id])
835                         return 1;
836         } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
837                 int i;
838
839                 for (i = 0; i < klass->interface_count; i ++) {
840                         MonoClass *ic =  klass->interfaces [i];
841                         if (ic == klassc)
842                                 return 1;
843                 }
844         } else {
845                 /*
846                  * klass->baseval is 0 for interfaces 
847                  */
848                 if (klass->baseval && ((klass->baseval - klassc->baseval) <= klassc->diffval))
849                         return 1;
850         }
851         return 0;
852 }
853
854 static guint32
855 ves_icall_get_attributes (MonoReflectionType *type)
856 {
857         MonoClass *klass = mono_class_from_mono_type (type->type);
858
859         return klass->flags;
860 }
861
862 static void
863 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
864 {
865         MonoDomain *domain = mono_domain_get ();
866
867         info->parent = mono_type_get_object (domain, &method->klass->byval_arg);
868         info->ret = mono_type_get_object (domain, method->signature->ret);
869         info->attrs = method->flags;
870         info->implattrs = method->iflags;
871 }
872
873 static MonoArray*
874 ves_icall_get_parameter_info (MonoMethod *method)
875 {
876         MonoDomain *domain = mono_domain_get (); 
877         MonoArray *res;
878         static MonoClass *System_Reflection_ParameterInfo;
879         MonoReflectionParameter** args;
880         int i;
881
882         args = mono_param_get_objects (domain, method);
883         if (!System_Reflection_ParameterInfo)
884                 System_Reflection_ParameterInfo = mono_class_from_name (
885                         mono_defaults.corlib, "System.Reflection", "ParameterInfo");
886         res = mono_array_new (domain, System_Reflection_ParameterInfo, method->signature->param_count);
887         for (i = 0; i < method->signature->param_count; ++i) {
888                 mono_array_set (res, gpointer, i, args [i]);
889         }
890         return res;
891 }
892
893 static void
894 ves_icall_get_field_info (MonoReflectionField *field, MonoFieldInfo *info)
895 {
896         MonoDomain *domain = mono_domain_get (); 
897
898         info->parent = mono_type_get_object (domain, &field->klass->byval_arg);
899         info->type = mono_type_get_object (domain, field->field->type);
900         info->name = mono_string_new (domain, field->field->name);
901         info->attrs = field->field->type->attrs;
902 }
903
904 static MonoObject *
905 ves_icall_MonoField_GetValueInternal (MonoReflectionField *field, MonoObject *obj)
906 {       
907         MonoObject *o;
908         MonoClassField *cf = field->field;
909         MonoClass *klass;
910         MonoVTable *vtable;
911         MonoDomain *domain = mono_domain_get ();
912         gchar *v;
913         gboolean is_static = FALSE;
914         gboolean is_ref = FALSE;
915
916         mono_class_init (field->klass);
917
918         switch (cf->type->type) {
919         case MONO_TYPE_STRING:
920         case MONO_TYPE_OBJECT:
921         case MONO_TYPE_CLASS:
922         case MONO_TYPE_ARRAY:
923         case MONO_TYPE_SZARRAY:
924                 is_ref = TRUE;
925                 break;
926         case MONO_TYPE_U1:
927         case MONO_TYPE_I1:
928         case MONO_TYPE_BOOLEAN:
929         case MONO_TYPE_U2:
930         case MONO_TYPE_I2:
931         case MONO_TYPE_CHAR:
932         case MONO_TYPE_U:
933         case MONO_TYPE_I:
934         case MONO_TYPE_U4:
935         case MONO_TYPE_I4:
936         case MONO_TYPE_R4:
937         case MONO_TYPE_U8:
938         case MONO_TYPE_I8:
939         case MONO_TYPE_R8:
940         case MONO_TYPE_VALUETYPE:
941                 is_ref = cf->type->byref;
942                 break;
943         default:
944                 g_error ("type 0x%x not handled in "
945                          "ves_icall_Monofield_GetValue", cf->type->type);
946                 return NULL;
947         }
948
949         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
950                 is_static = TRUE;
951                 vtable = mono_class_vtable (domain, field->klass);
952         }
953         
954         if (is_ref) {
955                 if (is_static) {
956                         mono_field_static_get_value (vtable, cf, &o);
957                 } else {
958                         mono_field_get_value (obj, cf, &o);
959                 }
960                 return o;
961         }
962
963         /* boxed value type */
964         klass = mono_class_from_mono_type (cf->type);
965         o = mono_object_new (domain, klass);
966         v = ((gchar *) o) + sizeof (MonoObject);
967         if (is_static) {
968                 mono_field_static_get_value (vtable, cf, v);
969         } else {
970                 mono_field_get_value (obj, cf, v);
971         }
972
973         return o;
974 }
975
976 static void
977 ves_icall_FieldInfo_SetValueInternal (MonoReflectionField *field, MonoObject *obj, MonoObject *value)
978 {
979         MonoClassField *cf = field->field;
980         gchar *v;
981
982         v = (gchar *) value;
983         if (!cf->type->byref) {
984                 switch (cf->type->type) {
985                 case MONO_TYPE_U1:
986                 case MONO_TYPE_I1:
987                 case MONO_TYPE_BOOLEAN:
988                 case MONO_TYPE_U2:
989                 case MONO_TYPE_I2:
990                 case MONO_TYPE_CHAR:
991                 case MONO_TYPE_U:
992                 case MONO_TYPE_I:
993                 case MONO_TYPE_U4:
994                 case MONO_TYPE_I4:
995                 case MONO_TYPE_R4:
996                 case MONO_TYPE_U8:
997                 case MONO_TYPE_I8:
998                 case MONO_TYPE_R8:
999                 case MONO_TYPE_VALUETYPE:
1000                         v += sizeof (MonoObject);
1001                         break;
1002                 case MONO_TYPE_STRING:
1003                 case MONO_TYPE_OBJECT:
1004                 case MONO_TYPE_CLASS:
1005                 case MONO_TYPE_ARRAY:
1006                 case MONO_TYPE_SZARRAY:
1007                         /* Do nothing */
1008                         break;
1009                 default:
1010                         g_error ("type 0x%x not handled in "
1011                                  "ves_icall_FieldInfo_SetValueInternal", cf->type->type);
1012                         return;
1013                 }
1014         }
1015
1016         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1017                 MonoVTable *vtable = mono_class_vtable (mono_domain_get (), field->klass);
1018                 mono_field_static_set_value (vtable, cf, v);
1019         } else {
1020                 mono_field_set_value (obj, cf, v);
1021         }
1022 }
1023
1024 static void
1025 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
1026 {
1027         MonoDomain *domain = mono_domain_get (); 
1028
1029         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
1030         info->name = mono_string_new (domain, property->property->name);
1031         info->attrs = property->property->attrs;
1032         info->get = property->property->get ? mono_method_get_object (domain, property->property->get, NULL): NULL;
1033         info->set = property->property->set ? mono_method_get_object (domain, property->property->set, NULL): NULL;
1034         /* 
1035          * There may be other methods defined for properties, though, it seems they are not exposed 
1036          * in the reflection API 
1037          */
1038 }
1039
1040 static void
1041 ves_icall_get_event_info (MonoReflectionEvent *event, MonoEventInfo *info)
1042 {
1043         MonoDomain *domain = mono_domain_get (); 
1044
1045         info->parent = mono_type_get_object (domain, &event->klass->byval_arg);
1046         info->name = mono_string_new (domain, event->event->name);
1047         info->attrs = event->event->attrs;
1048         info->add_method = event->event->add ? mono_method_get_object (domain, event->event->add, NULL): NULL;
1049         info->remove_method = event->event->remove ? mono_method_get_object (domain, event->event->remove, NULL): NULL;
1050         info->raise_method = event->event->raise ? mono_method_get_object (domain, event->event->raise, NULL): NULL;
1051 }
1052
1053 static MonoArray*
1054 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
1055 {
1056         MonoDomain *domain = mono_domain_get (); 
1057         MonoArray *intf;
1058         int ninterf, i;
1059         MonoClass *class = mono_class_from_mono_type (type->type);
1060         MonoClass *parent;
1061
1062         ninterf = 0;
1063         for (parent = class; parent; parent = parent->parent) {
1064                 ninterf += parent->interface_count;
1065         }
1066         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
1067         ninterf = 0;
1068         for (parent = class; parent; parent = parent->parent) {
1069                 for (i = 0; i < parent->interface_count; ++i) {
1070                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
1071                         ++ninterf;
1072                 }
1073         }
1074         return intf;
1075 }
1076
1077 static MonoReflectionType*
1078 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
1079 {
1080         MonoClass *class = mono_class_from_mono_type (type->type);
1081         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1082                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
1083         else if (class->element_class)
1084                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
1085         else
1086                 return NULL;
1087 }
1088
1089 static MonoReflectionType*
1090 ves_icall_get_type_parent (MonoReflectionType *type)
1091 {
1092         MonoClass *class = mono_class_from_mono_type (type->type);
1093         return class->parent ? mono_type_get_object (mono_object_domain (type), &class->parent->byval_arg): NULL;
1094 }
1095
1096 static MonoBoolean
1097 ves_icall_type_ispointer (MonoReflectionType *type)
1098 {
1099         return type->type->type == MONO_TYPE_PTR;
1100 }
1101
1102 static MonoBoolean
1103 ves_icall_type_isbyref (MonoReflectionType *type)
1104 {
1105         return type->type->byref;
1106 }
1107
1108 static MonoReflectionModule*
1109 ves_icall_MonoType_get_Module (MonoReflectionType *type)
1110 {
1111         MonoClass *class = mono_class_from_mono_type (type->type);
1112         return mono_module_get_object (mono_object_domain (type), class->image);
1113 }
1114
1115 static void
1116 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
1117 {
1118         MonoDomain *domain = mono_domain_get (); 
1119         MonoClass *class = mono_class_from_mono_type (type);
1120
1121         info->nested_in = class->nested_in ? mono_type_get_object (domain, &class->nested_in->byval_arg): NULL;
1122         info->name = mono_string_new (domain, class->name);
1123         info->name_space = mono_string_new (domain, class->name_space);
1124         info->rank = class->rank;
1125         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
1126         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1127                 info->etype = mono_type_get_object (domain, class->enum_basetype);
1128         else if (class->element_class)
1129                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
1130         else
1131                 info->etype = NULL;
1132
1133         info->isprimitive = (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8);
1134 }
1135
1136 static MonoObject *
1137 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
1138 {
1139         return mono_runtime_invoke_array (method->method, this, params, NULL);
1140 }
1141
1142 static MonoObject *
1143 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
1144 {
1145         MonoDomain *domain = mono_domain_get (); 
1146         MonoMethod *m = method->method;
1147         MonoMethodSignature *sig = m->signature;
1148         MonoArray *out_args;
1149         MonoObject *result;
1150         int i, j, outarg_count = 0;
1151
1152         if (m->klass == mono_defaults.object_class) {
1153
1154                 if (!strcmp (m->name, "FieldGetter")) {
1155                         MonoClass *k = this->vtable->klass;
1156                         MonoString *name = mono_array_get (params, MonoString *, 1);
1157                         char *str;
1158
1159                         str = mono_string_to_utf8 (name);
1160                 
1161                         for (i = 0; i < k->field.count; i++) {
1162                                 if (!strcmp (k->fields [i].name, str)) {
1163                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1164                                         if (field_klass->valuetype)
1165                                                 result = mono_value_box (domain, field_klass,
1166                                                                          (char *)this + k->fields [i].offset);
1167                                         else 
1168                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
1169                                 
1170                                         g_assert (result);
1171                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
1172                                         *outArgs = out_args;
1173                                         mono_array_set (out_args, gpointer, 0, result);
1174                                         g_free (str);
1175                                         return NULL;
1176                                 }
1177                         }
1178
1179                         g_free (str);
1180                         g_assert_not_reached ();
1181
1182                 } else if (!strcmp (m->name, "FieldSetter")) {
1183                         MonoClass *k = this->vtable->klass;
1184                         MonoString *name = mono_array_get (params, MonoString *, 1);
1185                         int size, align;
1186                         char *str;
1187
1188                         str = mono_string_to_utf8 (name);
1189                 
1190                         for (i = 0; i < k->field.count; i++) {
1191                                 if (!strcmp (k->fields [i].name, str)) {
1192                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1193                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1194
1195                                         if (field_klass->valuetype) {
1196                                                 size = mono_type_size (k->fields [i].type, &align);
1197                                                 memcpy ((char *)this + k->fields [i].offset, 
1198                                                         ((char *)val) + sizeof (MonoObject), size);
1199                                         } else 
1200                                                 *((gpointer *)this + k->fields [i].offset) = val;
1201                                 
1202                                         g_assert (result);
1203                                         g_free (str);
1204                                         return NULL;
1205                                 }
1206                         }
1207
1208                         g_free (str);
1209                         g_assert_not_reached ();
1210
1211                 }
1212         }
1213
1214         for (i = 0; i < mono_array_length (params); i++) {
1215                 if (sig->params [i]->byref) 
1216                         outarg_count++;
1217         }
1218
1219         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1220         
1221         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1222                 if (sig->params [i]->byref) {
1223                         gpointer arg;
1224                         arg = mono_array_get (params, gpointer, i);
1225                         mono_array_set (out_args, gpointer, j, arg);
1226                         j++;
1227                 }
1228         }
1229
1230         /* fixme: handle constructors? */
1231         if (!strcmp (method->method->name, ".ctor"))
1232                 g_assert_not_reached ();
1233
1234         result = mono_runtime_invoke_array (method->method, this, params, NULL);
1235
1236         *outArgs = out_args;
1237
1238         return result;
1239 }
1240
1241 static MonoObject *
1242 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1243 {
1244         MonoDomain *domain = mono_domain_get (); 
1245         MonoClass *enumc, *objc;
1246         gint32 s1, s2;
1247         MonoObject *res;
1248         
1249         MONO_CHECK_ARG_NULL (type);
1250         MONO_CHECK_ARG_NULL (obj);
1251
1252         enumc = mono_class_from_mono_type (type->type);
1253         objc = obj->vtable->klass;
1254
1255         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1256         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1257                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1258         
1259         s1 = mono_class_value_size (enumc, NULL);
1260         s2 = mono_class_value_size (objc, NULL);
1261
1262         res = mono_object_new (domain, enumc);
1263
1264 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1265         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1266 #else
1267         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1268                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1269                 MIN (s1, s2));
1270 #endif
1271         return res;
1272 }
1273
1274 static MonoObject *
1275 ves_icall_System_Enum_get_value (MonoObject *this)
1276 {
1277         MonoDomain *domain = mono_domain_get (); 
1278         MonoObject *res;
1279         MonoClass *enumc;
1280         gpointer dst;
1281         gpointer src;
1282         int size;
1283
1284         if (!this)
1285                 return NULL;
1286
1287         g_assert (this->vtable->klass->enumtype);
1288         
1289         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1290         res = mono_object_new (domain, enumc);
1291         dst = (char *)res + sizeof (MonoObject);
1292         src = (char *)this + sizeof (MonoObject);
1293         size = mono_class_value_size (enumc, NULL);
1294
1295         memcpy (dst, src, size);
1296
1297         return res;
1298 }
1299
1300 static void
1301 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1302 {
1303         MonoDomain *domain = mono_domain_get (); 
1304         MonoClass *enumc = mono_class_from_mono_type (type->type);
1305         guint i, j, nvalues, crow;
1306         MonoClassField *field;
1307         
1308         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1309         nvalues = enumc->field.count - 1;
1310         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1311         info->values = mono_array_new (domain, enumc, nvalues);
1312         
1313         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1314                 field = &enumc->fields [i];
1315                 if (strcmp ("value__", field->name) == 0)
1316                         continue;
1317                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1318                 if (!field->data) {
1319                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1320                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1321                         /* 1 is the length of the blob */
1322                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1323                 }
1324                 switch (enumc->enum_basetype->type) {
1325                 case MONO_TYPE_U1:
1326                 case MONO_TYPE_I1:
1327                         mono_array_set (info->values, gchar, j, *field->data);
1328                         break;
1329                 case MONO_TYPE_CHAR:
1330                 case MONO_TYPE_U2:
1331                 case MONO_TYPE_I2:
1332                         mono_array_set (info->values, gint16, j, read16 (field->data));
1333                         break;
1334                 case MONO_TYPE_U4:
1335                 case MONO_TYPE_I4:
1336                         mono_array_set (info->values, gint32, j, read32 (field->data));
1337                         break;
1338                 case MONO_TYPE_U8:
1339                 case MONO_TYPE_I8:
1340                         mono_array_set (info->values, gint64, j, read64 (field->data));
1341                         break;
1342                 default:
1343                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1344                 }
1345                 ++j;
1346         }
1347 }
1348
1349 static MonoProperty*
1350 search_property (MonoClass *klass, char* name, MonoArray *args) {
1351         int i;
1352         MonoProperty *p;
1353
1354         /* FIXME: handle args */
1355         for (i = 0; i < klass->property.count; ++i) {
1356                 p = &klass->properties [i];
1357                 if (strcmp (p->name, name) == 0)
1358                         return p;
1359         }
1360         return NULL;
1361 }
1362
1363 static MonoReflectionProperty*
1364 ves_icall_get_property (MonoReflectionType *type, MonoString *name, MonoArray *args)
1365 {
1366         MonoDomain *domain = mono_domain_get (); 
1367         MonoProperty *p;
1368         MonoClass *class = mono_class_from_mono_type (type->type);
1369         char *n = mono_string_to_utf8 (name);
1370
1371         p = search_property (class, n, args);
1372         g_free (n);
1373         if (p)
1374                 return mono_property_get_object (domain, class, p);
1375         return NULL;
1376 }
1377
1378 enum {
1379         BFLAGS_IgnoreCase = 1,
1380         BFLAGS_DeclaredOnly = 2,
1381         BFLAGS_Instance = 4,
1382         BFLAGS_Static = 8,
1383         BFLAGS_Public = 0x10,
1384         BFLAGS_NonPublic = 0x20,
1385         BFLAGS_InvokeMethod = 0x100,
1386         BFLAGS_CreateInstance = 0x200,
1387         BFLAGS_GetField = 0x400,
1388         BFLAGS_SetField = 0x800,
1389         BFLAGS_GetProperty = 0x1000,
1390         BFLAGS_SetProperty = 0x2000,
1391         BFLAGS_ExactBinding = 0x10000,
1392         BFLAGS_SuppressChangeType = 0x20000,
1393         BFLAGS_OptionalParamBinding = 0x40000
1394 };
1395
1396 static MonoFieldInfo *
1397 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
1398 {
1399         MonoDomain *domain; 
1400         MonoClass *startklass, *klass;
1401         int i, match;
1402         MonoClassField *field;
1403         char *utf8_name;
1404         domain = ((MonoObject *)type)->vtable->domain;
1405         klass = startklass = mono_class_from_mono_type (type->type);
1406
1407         if (!name)
1408                 return NULL;
1409
1410 handle_parent:  
1411         for (i = 0; i < klass->field.count; ++i) {
1412                 match = 0;
1413                 field = &klass->fields [i];
1414                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1415                         if (bflags & BFLAGS_Public)
1416                                 match++;
1417                 } else {
1418                         if (bflags & BFLAGS_NonPublic)
1419                                 match++;
1420                 }
1421                 if (!match)
1422                         continue;
1423                 match = 0;
1424                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1425                         if (bflags & BFLAGS_Static)
1426                                 match++;
1427                 } else {
1428                         if (bflags & BFLAGS_Instance)
1429                                 match++;
1430                 }
1431
1432                 if (!match)
1433                         continue;
1434                 
1435                 utf8_name = mono_string_to_utf8 (name);
1436
1437                 if (strcmp (field->name, utf8_name)) {
1438                         g_free (utf8_name);
1439                         continue;
1440                 }
1441                 g_free (utf8_name);
1442                 
1443                 return (MonoFieldInfo *)mono_field_get_object (domain, klass, field);
1444         }
1445         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1446                 goto handle_parent;
1447
1448         return NULL;
1449 }
1450
1451 static MonoArray*
1452 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1453 {
1454         MonoDomain *domain; 
1455         GSList *l = NULL, *tmp;
1456         MonoClass *startklass, *klass;
1457         MonoArray *res;
1458         MonoObject *member;
1459         int i, len, match;
1460         MonoClassField *field;
1461
1462         domain = ((MonoObject *)type)->vtable->domain;
1463         klass = startklass = mono_class_from_mono_type (type->type);
1464
1465 handle_parent:  
1466         for (i = 0; i < klass->field.count; ++i) {
1467                 match = 0;
1468                 field = &klass->fields [i];
1469                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1470                         if (bflags & BFLAGS_Public)
1471                                 match++;
1472                 } else {
1473                         if (bflags & BFLAGS_NonPublic)
1474                                 match++;
1475                 }
1476                 if (!match)
1477                         continue;
1478                 match = 0;
1479                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1480                         if (bflags & BFLAGS_Static)
1481                                 match++;
1482                 } else {
1483                         if (bflags & BFLAGS_Instance)
1484                                 match++;
1485                 }
1486
1487                 if (!match)
1488                         continue;
1489                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1490                 l = g_slist_prepend (l, member);
1491         }
1492         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1493                 goto handle_parent;
1494         len = g_slist_length (l);
1495         res = mono_array_new (domain, mono_defaults.field_info_class, len);
1496         i = 0;
1497         tmp = g_slist_reverse (l);
1498         for (; tmp; tmp = tmp->next, ++i)
1499                 mono_array_set (res, gpointer, i, tmp->data);
1500         g_slist_free (l);
1501         return res;
1502 }
1503
1504 static MonoArray*
1505 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1506 {
1507         MonoDomain *domain; 
1508         GSList *l = NULL, *tmp;
1509         static MonoClass *System_Reflection_MethodInfo;
1510         MonoClass *startklass, *klass;
1511         MonoArray *res;
1512         MonoMethod *method;
1513         MonoObject *member;
1514         int i, len, match;
1515         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1516                 
1517         domain = ((MonoObject *)type)->vtable->domain;
1518         klass = startklass = mono_class_from_mono_type (type->type);
1519
1520 handle_parent:
1521         for (i = 0; i < klass->method.count; ++i) {
1522                 match = 0;
1523                 method = klass->methods [i];
1524                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1525                         continue;
1526                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1527                         if (bflags & BFLAGS_Public)
1528                                 match++;
1529                 } else {
1530                         if (bflags & BFLAGS_NonPublic)
1531                                 match++;
1532                 }
1533                 if (!match)
1534                         continue;
1535                 match = 0;
1536                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1537                         if (bflags & BFLAGS_Static)
1538                                 match++;
1539                 } else {
1540                         if (bflags & BFLAGS_Instance)
1541                                 match++;
1542                 }
1543
1544                 if (!match)
1545                         continue;
1546                 match = 0;
1547                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1548                         continue;
1549                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
1550                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1551                 
1552                 l = g_slist_prepend (l, member);
1553         }
1554         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1555                 goto handle_parent;
1556         len = g_slist_length (l);
1557         if (!System_Reflection_MethodInfo)
1558                 System_Reflection_MethodInfo = mono_class_from_name (
1559                         mono_defaults.corlib, "System.Reflection", "MethodInfo");
1560         res = mono_array_new (domain, System_Reflection_MethodInfo, len);
1561         i = 0;
1562         tmp = l;
1563         for (; tmp; tmp = tmp->next, ++i)
1564                 mono_array_set (res, gpointer, i, tmp->data);
1565         g_slist_free (l);
1566         g_hash_table_destroy (method_slots);
1567         return res;
1568 }
1569
1570 static MonoArray*
1571 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1572 {
1573         MonoDomain *domain; 
1574         GSList *l = NULL, *tmp;
1575         static MonoClass *System_Reflection_ConstructorInfo;
1576         MonoClass *startklass, *klass;
1577         MonoArray *res;
1578         MonoMethod *method;
1579         MonoObject *member;
1580         int i, len, match;
1581
1582         domain = ((MonoObject *)type)->vtable->domain;
1583         klass = startklass = mono_class_from_mono_type (type->type);
1584
1585 handle_parent:  
1586         for (i = 0; i < klass->method.count; ++i) {
1587                 match = 0;
1588                 method = klass->methods [i];
1589                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1590                         continue;
1591                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1592                         if (bflags & BFLAGS_Public)
1593                                 match++;
1594                 } else {
1595                         if (bflags & BFLAGS_NonPublic)
1596                                 match++;
1597                 }
1598                 if (!match)
1599                         continue;
1600                 match = 0;
1601                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1602                         if (bflags & BFLAGS_Static)
1603                                 match++;
1604                 } else {
1605                         if (bflags & BFLAGS_Instance)
1606                                 match++;
1607                 }
1608
1609                 if (!match)
1610                         continue;
1611                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1612                         
1613                 l = g_slist_prepend (l, member);
1614         }
1615         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1616                 goto handle_parent;
1617         len = g_slist_length (l);
1618         if (!System_Reflection_ConstructorInfo)
1619                 System_Reflection_ConstructorInfo = mono_class_from_name (
1620                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1621         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1622         i = 0;
1623         tmp = g_slist_reverse (l);
1624         for (; tmp; tmp = tmp->next, ++i)
1625                 mono_array_set (res, gpointer, i, tmp->data);
1626         g_slist_free (l);
1627         return res;
1628 }
1629
1630 static MonoArray*
1631 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1632 {
1633         MonoDomain *domain; 
1634         GSList *l = NULL, *tmp;
1635         static MonoClass *System_Reflection_PropertyInfo;
1636         MonoClass *startklass, *klass;
1637         MonoArray *res;
1638         MonoMethod *method;
1639         MonoProperty *prop;
1640         int i, len, match;
1641
1642         domain = ((MonoObject *)type)->vtable->domain;
1643         klass = startklass = mono_class_from_mono_type (type->type);
1644
1645 handle_parent:
1646         for (i = 0; i < klass->property.count; ++i) {
1647                 prop = &klass->properties [i];
1648                 match = 0;
1649                 method = prop->get;
1650                 if (!method)
1651                         method = prop->set;
1652                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1653                         if (bflags & BFLAGS_Public)
1654                                 match++;
1655                 } else {
1656                         if (bflags & BFLAGS_NonPublic)
1657                                 match++;
1658                 }
1659                 if (!match)
1660                         continue;
1661                 match = 0;
1662                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1663                         if (bflags & BFLAGS_Static)
1664                                 match++;
1665                 } else {
1666                         if (bflags & BFLAGS_Instance)
1667                                 match++;
1668                 }
1669
1670                 if (!match)
1671                         continue;
1672                 match = 0;
1673                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1674         }
1675         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1676                 goto handle_parent;
1677         len = g_slist_length (l);
1678         if (!System_Reflection_PropertyInfo)
1679                 System_Reflection_PropertyInfo = mono_class_from_name (
1680                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1681         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1682         i = 0;
1683         tmp = l;
1684         for (; tmp; tmp = tmp->next, ++i)
1685                 mono_array_set (res, gpointer, i, tmp->data);
1686         g_slist_free (l);
1687         return res;
1688 }
1689
1690 static MonoArray*
1691 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
1692 {
1693         MonoDomain *domain; 
1694         GSList *l = NULL, *tmp;
1695         static MonoClass *System_Reflection_EventInfo;
1696         MonoClass *startklass, *klass;
1697         MonoArray *res;
1698         MonoMethod *method;
1699         MonoEvent *event;
1700         int i, len, match;
1701
1702         domain = ((MonoObject *)type)->vtable->domain;
1703         klass = startklass = mono_class_from_mono_type (type->type);
1704
1705 handle_parent:  
1706         for (i = 0; i < klass->event.count; ++i) {
1707                 event = &klass->events [i];
1708                 match = 0;
1709                 method = event->add;
1710                 if (!method)
1711                         method = event->remove;
1712                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1713                         if (bflags & BFLAGS_Public)
1714                                 match++;
1715                 } else {
1716                         if (bflags & BFLAGS_NonPublic)
1717                                 match++;
1718                 }
1719                 if (!match)
1720                         continue;
1721                 match = 0;
1722                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1723                         if (bflags & BFLAGS_Static)
1724                                 match++;
1725                 } else {
1726                         if (bflags & BFLAGS_Instance)
1727                                 match++;
1728                 }
1729
1730                 if (!match)
1731                         continue;
1732                 match = 0;
1733                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
1734         }
1735         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1736                 goto handle_parent;
1737         len = g_slist_length (l);
1738         if (!System_Reflection_EventInfo)
1739                 System_Reflection_EventInfo = mono_class_from_name (
1740                         mono_defaults.corlib, "System.Reflection", "EventInfo");
1741         res = mono_array_new (domain, System_Reflection_EventInfo, len);
1742         i = 0;
1743         tmp = l;
1744         for (; tmp; tmp = tmp->next, ++i)
1745                 mono_array_set (res, gpointer, i, tmp->data);
1746         g_slist_free (l);
1747         return res;
1748 }
1749
1750 static MonoArray*
1751 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
1752 {
1753         MonoDomain *domain; 
1754         GSList *l = NULL, *tmp;
1755         GList *tmpn;
1756         MonoClass *startklass, *klass;
1757         MonoArray *res;
1758         MonoObject *member;
1759         int i, len, match;
1760         MonoClass *nested;
1761
1762         domain = ((MonoObject *)type)->vtable->domain;
1763         klass = startklass = mono_class_from_mono_type (type->type);
1764
1765         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
1766                 match = 0;
1767                 nested = tmpn->data;
1768                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
1769                         if (bflags & BFLAGS_Public)
1770                                 match++;
1771                 } else {
1772                         if (bflags & BFLAGS_NonPublic)
1773                                 match++;
1774                 }
1775                 if (!match)
1776                         continue;
1777                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
1778                 l = g_slist_prepend (l, member);
1779         }
1780         len = g_slist_length (l);
1781         res = mono_array_new (domain, mono_defaults.monotype_class, len);
1782         i = 0;
1783         tmp = g_slist_reverse (l);
1784         for (; tmp; tmp = tmp->next, ++i)
1785                 mono_array_set (res, gpointer, i, tmp->data);
1786         g_slist_free (l);
1787         return res;
1788 }
1789
1790 static MonoReflectionType*
1791 ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
1792 {
1793         MonoDomain *domain = mono_domain_get (); 
1794         gchar *str;
1795         MonoType *type;
1796         MonoTypeNameParse info;
1797
1798         str = mono_string_to_utf8 (name);
1799         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
1800         if (!mono_reflection_parse_type (str, &info)) {
1801                 g_free (str);
1802                 g_list_free (info.modifiers);
1803                 g_list_free (info.nested);
1804                 if (throwOnError) /* uhm: this is a parse error, though... */
1805                         mono_raise_exception (mono_get_exception_type_load ());
1806                 /*g_print ("failed parse\n");*/
1807                 return NULL;
1808         }
1809
1810         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
1811         g_free (str);
1812         g_list_free (info.modifiers);
1813         g_list_free (info.nested);
1814         if (!type) {
1815                 if (throwOnError)
1816                         mono_raise_exception (mono_get_exception_type_load ());
1817                 /* g_print ("failed find\n"); */
1818                 return NULL;
1819         }
1820         /* g_print ("got it\n"); */
1821         return mono_type_get_object (domain, type);
1822
1823 }
1824
1825 static MonoString *
1826 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
1827 {
1828         MonoDomain *domain = mono_domain_get (); 
1829         MonoString *res;
1830         char *name = g_strconcat (
1831                 "file://", assembly->assembly->image->name, NULL);
1832         
1833         res = mono_string_new (domain, name);
1834         g_free (name);
1835         return res;
1836 }
1837
1838 static MonoString *
1839 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
1840 {
1841         MonoDomain *domain = mono_domain_get (); 
1842         MonoString *res;
1843         char *name = g_strconcat (
1844                 assembly->assembly->basedir, G_DIR_SEPARATOR_S,
1845                 assembly->assembly->image->module_name, NULL);
1846
1847         res = mono_string_new (domain, name);
1848         g_free (name);
1849         return res;
1850 }
1851
1852 static MonoReflectionMethod*
1853 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) {
1854         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
1855         if (!token)
1856                 return NULL;
1857         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
1858 }
1859
1860 static MonoArray*
1861 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) {
1862         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
1863         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
1864         int i;
1865         const char *val;
1866
1867         for (i = 0; i < table->rows; ++i) {
1868                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
1869                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
1870         }
1871         return result;
1872 }
1873
1874 /* move this in some file in mono/util/ */
1875 static char *
1876 g_concat_dir_and_file (const char *dir, const char *file)
1877 {
1878         g_return_val_if_fail (dir != NULL, NULL);
1879         g_return_val_if_fail (file != NULL, NULL);
1880
1881         /*
1882          * If the directory name doesn't have a / on the end, we need
1883          * to add one so we get a proper path to the file
1884          */
1885         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
1886                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
1887         else
1888                 return g_strconcat (dir, file, NULL);
1889 }
1890
1891 static MonoObject*
1892 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name) {
1893         char *n = mono_string_to_utf8 (name);
1894         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
1895         guint32 i;
1896         guint32 cols [MONO_MANIFEST_SIZE];
1897         const char *val;
1898         MonoObject *result;
1899
1900         for (i = 0; i < table->rows; ++i) {
1901                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
1902                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
1903                 if (strcmp (val, n) == 0)
1904                         break;
1905         }
1906         g_free (n);
1907         if (i == table->rows)
1908                 return NULL;
1909         /* FIXME */
1910         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
1911                 guint32 size;
1912                 MonoArray *data;
1913                 val = mono_image_get_resource (assembly->assembly->image, cols [MONO_MANIFEST_OFFSET], &size);
1914                 if (!val)
1915                         return NULL;
1916                 data = mono_array_new (mono_object_domain (assembly), mono_defaults.byte_class, size);
1917                 memcpy (mono_array_addr (data, char, 0), val, size);
1918                 return (MonoObject*)data;
1919         }
1920         switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
1921         case IMPLEMENTATION_FILE:
1922                 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
1923                 table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
1924                 i = mono_metadata_decode_row_col (table, i - 1, MONO_FILE_NAME);
1925                 val = mono_metadata_string_heap (assembly->assembly->image, i);
1926                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
1927                 result = (MonoObject*)mono_string_new (mono_object_domain (assembly), n);
1928                 /* check hash if needed */
1929                 g_free (n);
1930                 return result;
1931         case IMPLEMENTATION_ASSEMBLYREF:
1932         case IMPLEMENTATION_EXP_TYPE:
1933                 /* FIXME */
1934                 break;
1935         }
1936         return NULL;
1937 }
1938
1939 static MonoObject*
1940 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) {
1941         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
1942         MonoArray *result;
1943         int i;
1944         const char *val;
1945         char *n;
1946
1947         /* check hash if needed */
1948         if (name) {
1949                 n = mono_string_to_utf8 (name);
1950                 for (i = 0; i < table->rows; ++i) {
1951                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
1952                         if (strcmp (val, n) == 0) {
1953                                 MonoString *fn;
1954                                 g_free (n);
1955                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
1956                                 fn = mono_string_new (mono_object_domain (assembly), n);
1957                                 g_free (n);
1958                                 return (MonoObject*)fn;
1959                         }
1960                 }
1961                 g_free (n);
1962                 return NULL;
1963         }
1964
1965         for (i = 0; i < table->rows; ++i) {
1966                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
1967                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
1968                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
1969                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
1970                 g_free (n);
1971         }
1972         return (MonoObject*)result;
1973 }
1974
1975 static MonoReflectionMethod*
1976 ves_icall_GetCurrentMethod (void) {
1977         MonoMethod *m = mono_method_get_last_managed ();
1978         return mono_method_get_object (mono_domain_get (), m, NULL);
1979 }
1980
1981 static MonoReflectionAssembly*
1982 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
1983 {
1984         MonoMethod *m = mono_method_get_last_managed ();
1985         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
1986 }
1987
1988
1989 static gboolean
1990 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gpointer data)
1991 {
1992         MonoMethod **dest = data;
1993         if (m == *dest) {
1994                 *dest = NULL;
1995                 return FALSE;
1996         }
1997         if (!(*dest)) {
1998                 *dest = m;
1999                 return TRUE;
2000         }
2001         return FALSE;
2002 }
2003
2004 static MonoReflectionAssembly*
2005 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
2006 {
2007         MonoDomain* domain = mono_domain_get ();
2008         g_assert (domain->entry_assembly);
2009         return mono_assembly_get_object (domain, domain->entry_assembly);
2010 }
2011
2012
2013 static MonoReflectionAssembly*
2014 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
2015 {
2016         MonoMethod *m = mono_method_get_last_managed ();
2017         MonoMethod *dest = m;
2018         mono_stack_walk (get_caller, &dest);
2019         if (!dest)
2020                 dest = m;
2021         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
2022 }
2023
2024 static MonoString *
2025 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
2026 {
2027         MonoDomain *domain = mono_domain_get (); 
2028         MonoString *res;
2029         gchar *name;
2030
2031         name = mono_type_get_name (object->type);
2032         res = mono_string_new (domain, name);
2033         g_free (name);
2034
2035         return res;
2036 }
2037
2038 static void
2039 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
2040 {
2041         MonoAssemblyName *name = &assembly->assembly->aname;
2042
2043         if (strcmp (name->name, "corlib") == 0)
2044                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
2045         else
2046                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
2047         aname->major = name->major;
2048 }
2049
2050 static MonoArray*
2051 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
2052 {
2053         MonoDomain *domain = mono_domain_get (); 
2054         MonoArray *res;
2055         MonoClass *klass;
2056         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
2057         int i, count;
2058         guint32 attrs, visibility;
2059
2060         /* we start the count from 1 because we skip the special type <Module> */
2061         if (exportedOnly) {
2062                 count = 0;
2063                 for (i = 1; i < tdef->rows; ++i) {
2064                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2065                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2066                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
2067                                 count++;
2068                 }
2069         } else {
2070                 count = tdef->rows - 1;
2071         }
2072         res = mono_array_new (domain, mono_defaults.monotype_class, count);
2073         count = 0;
2074         for (i = 1; i < tdef->rows; ++i) {
2075                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2076                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2077                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
2078                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
2079                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
2080                         count++;
2081                 }
2082         }
2083         
2084         return res;
2085 }
2086
2087 static MonoReflectionType*
2088 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
2089 {
2090         MonoClass *klass;
2091         int isbyref = 0, rank;
2092         char *str = mono_string_to_utf8 (smodifiers);
2093         char *p;
2094
2095         klass = mono_class_from_mono_type (tb->type.type);
2096         p = str;
2097         /* logic taken from mono_reflection_parse_type(): keep in sync */
2098         while (*p) {
2099                 switch (*p) {
2100                 case '&':
2101                         if (isbyref) { /* only one level allowed by the spec */
2102                                 g_free (str);
2103                                 return NULL;
2104                         }
2105                         isbyref = 1;
2106                         p++;
2107                         g_free (str);
2108                         return mono_type_get_object (mono_domain_get (), &klass->this_arg);
2109                         break;
2110                 case '*':
2111                         klass = mono_ptr_class_get (&klass->byval_arg);
2112                         mono_class_init (klass);
2113                         p++;
2114                         break;
2115                 case '[':
2116                         rank = 1;
2117                         p++;
2118                         while (*p) {
2119                                 if (*p == ']')
2120                                         break;
2121                                 if (*p == ',')
2122                                         rank++;
2123                                 else if (*p != '*') { /* '*' means unknown lower bound */
2124                                         g_free (str);
2125                                         return NULL;
2126                                 }
2127                                 ++p;
2128                         }
2129                         if (*p != ']') {
2130                                 g_free (str);
2131                                 return NULL;
2132                         }
2133                         p++;
2134                         klass = mono_array_class_get (&klass->byval_arg, rank);
2135                         mono_class_init (klass);
2136                         break;
2137                 default:
2138                         break;
2139                 }
2140         }
2141         g_free (str);
2142         return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
2143 }
2144
2145 static MonoObject *
2146 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
2147                                                    MonoReflectionMethod *info)
2148 {
2149         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
2150         MonoObject *delegate;
2151         gpointer func;
2152
2153         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
2154
2155         delegate = mono_object_new (mono_object_domain (type), delegate_class);
2156
2157         func = mono_compile_method (info->method);
2158
2159         mono_delegate_ctor (delegate, target, func);
2160
2161         return delegate;
2162 }
2163
2164 /*
2165  * Magic number to convert a time which is relative to
2166  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
2167  */
2168 #define EPOCH_ADJUST    ((gint64)62135596800L)
2169
2170 static gint64
2171 ves_icall_System_DateTime_GetNow (void)
2172 {
2173 #ifdef PLATFORM_WIN32
2174         SYSTEMTIME st;
2175         FILETIME ft;
2176         
2177         GetLocalTime (&st);
2178         SystemTimeToFileTime (&st, &ft);
2179         return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
2180 #else
2181         /* FIXME: put this in io-layer and call it GetLocalTime */
2182         struct timeval tv;
2183         gint64 res;
2184
2185         if (gettimeofday (&tv, NULL) == 0) {
2186                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
2187                 return res;
2188         }
2189         /* fixme: raise exception */
2190         return 0;
2191 #endif
2192 }
2193
2194 /*
2195  * This is heavily based on zdump.c from glibc 2.2.
2196  *
2197  *  * data[0]:  start of daylight saving time (in DateTime ticks).
2198  *  * data[1]:  end of daylight saving time (in DateTime ticks).
2199  *  * data[2]:  utcoffset (in TimeSpan ticks).
2200  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
2201  *  * name[0]:  name of this timezone when not daylight saving.
2202  *  * name[1]:  name of this timezone when daylight saving.
2203  *
2204  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
2205  *         the class library allows years between 1 and 9999.
2206  *
2207  *  Returns true on success and zero on failure.
2208  */
2209 static guint32
2210 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
2211 {
2212 #ifndef PLATFORM_WIN32
2213         MonoDomain *domain = mono_domain_get ();
2214         struct tm start, tt;
2215         time_t t;
2216
2217         long int gmtoff;
2218         int is_daylight = 0, day;
2219
2220         memset (&start, 0, sizeof (start));
2221
2222         start.tm_mday = 1;
2223         start.tm_year = year-1900;
2224
2225         t = mktime (&start);
2226 #if defined (HAVE_TIMEZONE)
2227 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
2228 #elif defined (HAVE_TM_GMTOFF)
2229 #define gmt_offset(x) x.tm_gmtoff
2230 #else
2231 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
2232 #endif
2233         
2234         gmtoff = gmt_offset (start);
2235         
2236         MONO_CHECK_ARG_NULL (data);
2237         MONO_CHECK_ARG_NULL (names);
2238
2239         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2240         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2241
2242         /* For each day of the year, calculate the tm_gmtoff. */
2243         for (day = 0; day < 365; day++) {
2244
2245                 t += 3600*24;
2246                 tt = *localtime (&t);
2247
2248                 /* Daylight saving starts or ends here. */
2249                 if (gmt_offset (tt) != gmtoff) {
2250                         char tzone[10];
2251                         struct tm tt1;
2252                         time_t t1;
2253
2254                         /* Try to find the exact hour when daylight saving starts/ends. */
2255                         t1 = t;
2256                         do {
2257                                 t1 -= 3600;
2258                                 tt1 = *localtime (&t1);
2259                         } while (gmt_offset (tt1) != gmtoff);
2260
2261                         /* Try to find the exact minute when daylight saving starts/ends. */
2262                         do {
2263                                 t1 += 60;
2264                                 tt1 = *localtime (&t1);
2265                         } while (gmt_offset (tt1) == gmtoff);
2266                         
2267                         strftime (tzone, 10, "%Z", &tt);
2268                         
2269                         /* Write data, if we're already in daylight saving, we're done. */
2270                         if (is_daylight) {
2271                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2272                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2273                                 return 1;
2274                         } else {
2275                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2276                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2277                                 is_daylight = 1;
2278                         }
2279
2280                         /* This is only set once when we enter daylight saving. */
2281                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
2282                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
2283
2284                         gmtoff = gmt_offset (tt);
2285                 }
2286
2287                 gmtoff = gmt_offset (tt);
2288         }
2289         return 1;
2290 #else
2291         MonoDomain *domain = mono_domain_get ();
2292         TIME_ZONE_INFORMATION tz_info;
2293         FILETIME ft;
2294         int i;
2295
2296         GetTimeZoneInformation (&tz_info);
2297
2298         MONO_CHECK_ARG_NULL (data);
2299         MONO_CHECK_ARG_NULL (names);
2300
2301         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2302         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2303
2304         for (i = 0; i < 32; ++i)
2305                 if (!tz_info.DaylightName [i])
2306                         break;
2307         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
2308         for (i = 0; i < 32; ++i)
2309                 if (!tz_info.StandardName [i])
2310                         break;
2311         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
2312
2313         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
2314         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2315         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
2316         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2317         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
2318         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
2319
2320         return 1;
2321 #endif
2322 }
2323
2324 static gpointer
2325 ves_icall_System_Object_obj_address (MonoObject *this) {
2326         return this;
2327 }
2328
2329 /* System.Buffer */
2330
2331 static gint32 
2332 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) {
2333         MonoClass *klass;
2334         MonoTypeEnum etype;
2335         int length, esize;
2336         int i;
2337
2338         klass = array->obj.vtable->klass;
2339         etype = klass->element_class->byval_arg.type;
2340         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
2341                 return -1;
2342
2343         if (array->bounds == NULL)
2344                 length = array->max_length;
2345         else {
2346                 length = 0;
2347                 for (i = 0; i < klass->rank; ++ i)
2348                         length += array->bounds [i].length;
2349         }
2350
2351         esize = mono_array_element_size (klass);
2352         return length * esize;
2353 }
2354
2355 static gint8 
2356 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) {
2357         return mono_array_get (array, gint8, idx);
2358 }
2359
2360 static void 
2361 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) {
2362         mono_array_set (array, gint8, idx, value);
2363 }
2364
2365 static void 
2366 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) {
2367         char *src_buf, *dest_buf;
2368
2369         src_buf = (gint8 *)src->vector + src_offset;
2370         dest_buf = (gint8 *)dest->vector + dest_offset;
2371
2372         memcpy (dest_buf, src_buf, count);
2373 }
2374
2375 static MonoObject *
2376 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
2377 {
2378         MonoDomain *domain = mono_domain_get (); 
2379         MonoObject *res;
2380         MonoRealProxy *rp = ((MonoRealProxy *)this);
2381         MonoType *type;
2382         MonoClass *klass;
2383
2384         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
2385         
2386         ((MonoTransparentProxy *)res)->rp = rp;
2387         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
2388         klass = mono_class_from_mono_type (type);
2389
2390         ((MonoTransparentProxy *)res)->klass = klass;
2391
2392         res->vtable = mono_class_proxy_vtable (domain, klass);
2393
2394         return res;
2395 }
2396
2397 /* System.Environment */
2398
2399 static MonoString *
2400 ves_icall_System_Environment_get_MachineName (void)
2401 {
2402 #if defined (PLATFORM_WIN32)
2403         gunichar2 *buf;
2404         guint32 len;
2405         MonoString *result;
2406
2407         len = MAX_COMPUTERNAME_LENGTH + 1;
2408         buf = g_new (gunichar2, len);
2409
2410         result = NULL;
2411         if (GetComputerName (buf, (PDWORD) &len))
2412                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
2413
2414         g_free (buf);
2415         return result;
2416 #else
2417         gchar *buf;
2418         int len;
2419         MonoString *result;
2420
2421         len = 256;
2422         buf = g_new (gchar, len);
2423
2424         result = NULL;
2425         if (gethostname (buf, len) != 0)
2426                 result = mono_string_new (mono_domain_get (), buf);
2427         
2428         g_free (buf);
2429         return result;
2430 #endif
2431 }
2432
2433 static int
2434 ves_icall_System_Environment_get_Platform (void)
2435 {
2436 #if defined (PLATFORM_WIN32)
2437         /* Win32NT */
2438         return 2;
2439 #else
2440         /* Unix */
2441         return 128;
2442 #endif
2443 }
2444
2445 static MonoString *
2446 ves_icall_System_Environment_get_NewLine (void)
2447 {
2448 #if defined (PLATFORM_WIN32)
2449         return mono_string_new (mono_domain_get (), "\r\n");
2450 #else
2451         return mono_string_new (mono_domain_get (), "\n");
2452 #endif
2453 }
2454
2455 static MonoString *
2456 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
2457 {
2458         const gchar *value;
2459         gchar *utf8_name;
2460
2461         if (name == NULL)
2462                 return NULL;
2463
2464         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
2465         value = g_getenv (utf8_name);
2466         g_free (utf8_name);
2467
2468         if (value == 0)
2469                 return NULL;
2470         
2471         return mono_string_new (mono_domain_get (), value);
2472 }
2473
2474 /*
2475  * There is no standard way to get at environ.
2476  */
2477 extern char **environ;
2478
2479 static MonoArray *
2480 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
2481 {
2482         MonoArray *names;
2483         MonoDomain *domain;
2484         MonoString *str;
2485         gchar **e, **parts;
2486         int n;
2487
2488         n = 0;
2489         for (e = environ; *e != 0; ++ e)
2490                 ++ n;
2491
2492         domain = mono_domain_get ();
2493         names = mono_array_new (domain, mono_defaults.string_class, n);
2494
2495         n = 0;
2496         for (e = environ; *e != 0; ++ e) {
2497                 parts = g_strsplit (*e, "=", 2);
2498                 if (*parts != 0) {
2499                         str = mono_string_new (domain, *parts);
2500                         mono_array_set (names, MonoString *, n, str);
2501                 }
2502
2503                 g_strfreev (parts);
2504
2505                 ++ n;
2506         }
2507
2508         return names;
2509 }
2510
2511 /*
2512  * Returns the number of milliseconds elapsed since the system started.
2513  */
2514 static gint32
2515 ves_icall_System_Environment_get_TickCount (void)
2516 {
2517 #if defined (PLATFORM_WIN32)
2518         return GetTickCount();
2519 #else
2520         struct timeval tv;
2521         struct timezone tz;
2522         gint32 res;
2523
2524         res = (gint32) gettimeofday (&tv, &tz);
2525
2526         if (res != -1)
2527                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
2528         return res;
2529 #endif
2530 }
2531
2532
2533 static void
2534 ves_icall_System_Environment_Exit (int result)
2535 {
2536         /* we may need to do some cleanup here... */
2537         exit (result);
2538 }
2539
2540 static MonoString*
2541 ves_icall_System_Text_Encoding_InternalCodePage (void) {
2542         const char *cset;
2543         g_get_charset (&cset);
2544         /* g_print ("charset: %s\n", cset); */
2545         /* handle some common aliases */
2546         switch (*cset) {
2547         case 'A':
2548                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
2549                         cset = "us-ascii";
2550                 break;
2551         }
2552         return mono_string_new (mono_domain_get (), cset);
2553 }
2554
2555 static void
2556 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
2557                                          MonoReflectionMethod *method,
2558                                          MonoArray *out_args)
2559 {
2560         MonoDomain *domain = mono_domain_get ();
2561         
2562         mono_message_init (domain, this, method, out_args);
2563 }
2564
2565 static MonoBoolean
2566 ves_icall_IsTransparentProxy (MonoObject *proxy)
2567 {
2568         if (!proxy)
2569                 return 0;
2570
2571         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
2572                 return 1;
2573
2574         return 0;
2575 }
2576
2577 static MonoObject *
2578 ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal (MonoReflectionType *type)
2579 {
2580         MonoClass *klass;
2581         MonoObject *obj;
2582         MonoDomain *domain;
2583         
2584         domain = mono_object_domain (type);
2585         klass = mono_class_from_mono_type (type->type);
2586
2587         if (klass->rank >= 1) {
2588                 g_assert (klass->rank == 1);
2589                 obj = (MonoObject *) mono_array_new (domain, klass->element_class, 0);
2590         } else {
2591                 obj = mono_object_new (domain, klass);
2592         }
2593
2594         return obj;
2595 }
2596
2597 static MonoString *
2598 ves_icall_System_IO_get_temp_path (void)
2599 {
2600         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
2601 }
2602
2603 static gpointer
2604 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
2605 {
2606         return mono_compile_method (method);
2607 }
2608
2609 /* icall map */
2610
2611 static gconstpointer icall_map [] = {
2612         /*
2613          * System.Array
2614          */
2615         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
2616         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
2617         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
2618         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
2619         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
2620         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
2621         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
2622         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
2623         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
2624         "System.Array::Clone",            mono_array_clone,
2625
2626         /*
2627          * System.Object
2628          */
2629         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
2630         "System.Object::GetType", ves_icall_System_Object_GetType,
2631         "System.Object::GetHashCode", ves_icall_System_Object_GetHashCode,
2632         "System.Object::obj_address", ves_icall_System_Object_obj_address,
2633
2634         /*
2635          * System.ValueType
2636          */
2637         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
2638         "System.ValueType::Equals", ves_icall_System_ValueType_Equals,
2639
2640         /*
2641          * System.String
2642          */
2643         
2644         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
2645         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
2646         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
2647         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
2648         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
2649         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
2650         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
2651         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
2652         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
2653         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
2654         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
2655         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
2656         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
2657         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
2658         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
2659         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
2660         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
2661         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
2662         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
2663         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
2664         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
2665         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
2666         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
2667         "System.String::InternalPad", ves_icall_System_String_InternalPad,
2668         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
2669         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
2670         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
2671         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
2672         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
2673         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
2674         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
2675         "System.String::InternalCompare(string,int,string,int,int,bool)", ves_icall_System_String_InternalCompareStr_N,
2676         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
2677         "System.String::get_Chars", ves_icall_System_String_get_Chars,
2678
2679         /*
2680          * System.AppDomain
2681          */
2682         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
2683         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
2684         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
2685         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
2686         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
2687         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
2688         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
2689         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
2690         "System.AppDomain::Unload", ves_icall_System_AppDomain_Unload,
2691         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
2692
2693         /*
2694          * System.AppDomainSetup
2695          */
2696         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
2697
2698         /*
2699          * System.Double
2700          */
2701         "System.Double::ToStringImpl", mono_double_ToStringImpl,
2702         "System.Double::ParseImpl",    mono_double_ParseImpl,
2703
2704         /*
2705          * System.Single
2706          */
2707         "System.Single::ToStringImpl", mono_float_ToStringImpl,
2708
2709         /*
2710          * System.Decimal
2711          */
2712         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
2713         "System.Decimal::decimal2Int64", mono_decimal2Int64,
2714         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
2715         "System.Decimal::decimalIncr", mono_decimalIncr,
2716         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
2717         "System.Decimal::decimal2double", mono_decimal2double,
2718         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
2719         "System.Decimal::decimalRound", mono_decimalRound,
2720         "System.Decimal::decimalMult", mono_decimalMult,
2721         "System.Decimal::decimalDiv", mono_decimalDiv,
2722         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
2723         "System.Decimal::decimalCompare", mono_decimalCompare,
2724         "System.Decimal::string2decimal", mono_string2decimal,
2725         "System.Decimal::decimal2string", mono_decimal2string,
2726
2727         /*
2728          * ModuleBuilder
2729          */
2730         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
2731         
2732         /*
2733          * AssemblyBuilder
2734          */
2735         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
2736         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
2737         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
2738         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
2739
2740         /*
2741          * Reflection stuff.
2742          */
2743         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
2744         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
2745         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
2746         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
2747         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
2748         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
2749         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
2750         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
2751         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
2752         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
2753         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
2754         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
2755         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
2756         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
2757
2758         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
2759         
2760         /* System.Enum */
2761
2762         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
2763         "System.Enum::get_value", ves_icall_System_Enum_get_value,
2764         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
2765
2766         /*
2767          * TypeBuilder
2768          */
2769         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
2770         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
2771         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
2772         
2773         /*
2774          * MethodBuilder
2775          */
2776         
2777         /*
2778          * System.Type
2779          */
2780         "System.Type::internal_from_name", ves_icall_type_from_name,
2781         "System.Type::internal_from_handle", ves_icall_type_from_handle,
2782         "System.Type::get_property", ves_icall_get_property,
2783         "System.MonoType::get_attributes", ves_icall_get_attributes,
2784         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
2785         "System.Type::Equals", ves_icall_type_Equals,
2786         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
2787
2788         /*
2789          * System.Runtime.CompilerServices.RuntimeHelpers
2790          */
2791         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_InitializeArray,
2792         
2793         /*
2794          * System.Threading
2795          */
2796         "System.Threading.Thread::Abort(object)", ves_icall_System_Threading_Thread_Abort,
2797         "System.Threading.Thread::ResetAbort", ves_icall_System_Threading_Thread_ResetAbort,
2798         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
2799         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
2800         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
2801         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
2802         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
2803         "System.Threading.Thread::CurrentThreadDomain_internal", ves_icall_System_Threading_Thread_CurrentThreadDomain_internal,
2804         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
2805         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
2806         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
2807         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
2808         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
2809         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
2810         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
2811         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
2812         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
2813         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
2814         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
2815         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
2816         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
2817         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
2818         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
2819
2820         /*
2821          * System.Threading.WaitHandle
2822          */
2823         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
2824         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
2825         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
2826
2827         /*
2828          * System.Runtime.InteropServices.Marshal
2829          */
2830         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
2831         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
2832         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
2833         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
2834         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
2835         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
2836         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
2837         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
2838         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
2839         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
2840
2841         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
2842         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
2843         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
2844         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
2845         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
2846         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
2847         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
2848
2849         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
2850         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
2851         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
2852         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
2853         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
2854         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
2855         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
2856         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
2857         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
2858         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
2859         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
2860         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
2861         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
2862         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
2863         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
2864
2865
2866         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
2867         "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
2868         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
2869         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
2870         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
2871         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
2872         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
2873         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
2874         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
2875         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
2876         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
2877         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
2878         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
2879
2880         /*
2881          * System.MonoType.
2882          */
2883         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
2884         "System.MonoType::type_from_obj", mono_type_type_from_obj,
2885         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
2886         "System.MonoType::get_type_info", ves_icall_get_type_info,
2887         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
2888         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
2889         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
2890         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
2891         "System.MonoType::GetField", ves_icall_Type_GetField,
2892         "System.MonoType::GetFields", ves_icall_Type_GetFields,
2893         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
2894         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
2895         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
2896         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
2897         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
2898         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
2899
2900         /*
2901          * System.Net.Sockets I/O Services
2902          */
2903         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
2904         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
2905         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
2906         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
2907         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
2908         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
2909         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
2910         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
2911         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
2912         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
2913         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
2914         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
2915         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
2916         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
2917         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
2918         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
2919         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
2920         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
2921         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
2922         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
2923         "System.Net.Dns::GetHostByName_internal", ves_icall_System_Net_Dns_GetHostByName_internal,
2924         "System.Net.Dns::GetHostByAddr_internal", ves_icall_System_Net_Dns_GetHostByAddr_internal,
2925
2926         /*
2927          * System.Char
2928          */
2929         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
2930         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
2931         "System.Char::IsControl", ves_icall_System_Char_IsControl,
2932         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
2933         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
2934         "System.Char::IsLower", ves_icall_System_Char_IsLower,
2935         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
2936         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
2937         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
2938         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
2939         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
2940         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
2941         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
2942         "System.Char::ToLower", ves_icall_System_Char_ToLower,
2943         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
2944
2945         /*
2946          * System.Text.Encoding
2947          */
2948         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
2949
2950         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
2951         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
2952
2953         /*
2954          * System.GC
2955          */
2956         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
2957         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
2958         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
2959         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
2960         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
2961         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
2962         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
2963         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
2964         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
2965         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
2966
2967         /*
2968          * System.Security.Cryptography calls
2969          */
2970
2971          "System.Security.Cryptography.RNGCryptoServiceProvider::GetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetBytes,
2972          "System.Security.Cryptography.RNGCryptoServiceProvider::GetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetNonZeroBytes,
2973         
2974         /*
2975          * System.Buffer
2976          */
2977         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
2978         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
2979         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
2980         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
2981
2982         /*
2983          * System.IO.MonoIO
2984          */
2985         "System.IO.MonoIO::GetLastError", ves_icall_System_IO_MonoIO_GetLastError,
2986         "System.IO.MonoIO::CreateDirectory", ves_icall_System_IO_MonoIO_CreateDirectory,
2987         "System.IO.MonoIO::RemoveDirectory", ves_icall_System_IO_MonoIO_RemoveDirectory,
2988         "System.IO.MonoIO::FindFirstFile", ves_icall_System_IO_MonoIO_FindFirstFile,
2989         "System.IO.MonoIO::FindNextFile", ves_icall_System_IO_MonoIO_FindNextFile,
2990         "System.IO.MonoIO::FindClose", ves_icall_System_IO_MonoIO_FindClose,
2991         "System.IO.MonoIO::GetCurrentDirectory", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
2992         "System.IO.MonoIO::SetCurrentDirectory", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
2993         "System.IO.MonoIO::MoveFile", ves_icall_System_IO_MonoIO_MoveFile,
2994         "System.IO.MonoIO::CopyFile", ves_icall_System_IO_MonoIO_CopyFile,
2995         "System.IO.MonoIO::DeleteFile", ves_icall_System_IO_MonoIO_DeleteFile,
2996         "System.IO.MonoIO::GetFileAttributes", ves_icall_System_IO_MonoIO_GetFileAttributes,
2997         "System.IO.MonoIO::SetFileAttributes", ves_icall_System_IO_MonoIO_SetFileAttributes,
2998         "System.IO.MonoIO::GetFileStat", ves_icall_System_IO_MonoIO_GetFileStat,
2999         "System.IO.MonoIO::Open", ves_icall_System_IO_MonoIO_Open,
3000         "System.IO.MonoIO::Close", ves_icall_System_IO_MonoIO_Close,
3001         "System.IO.MonoIO::Read", ves_icall_System_IO_MonoIO_Read,
3002         "System.IO.MonoIO::Write", ves_icall_System_IO_MonoIO_Write,
3003         "System.IO.MonoIO::Seek", ves_icall_System_IO_MonoIO_Seek,
3004         "System.IO.MonoIO::GetLength", ves_icall_System_IO_MonoIO_GetLength,
3005         "System.IO.MonoIO::SetLength", ves_icall_System_IO_MonoIO_SetLength,
3006         "System.IO.MonoIO::SetFileTime", ves_icall_System_IO_MonoIO_SetFileTime,
3007         "System.IO.MonoIO::Flush", ves_icall_System_IO_MonoIO_Flush,
3008         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
3009         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
3010         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
3011         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
3012         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
3013         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
3014         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
3015         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
3016         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
3017
3018         /*
3019          * System.Math
3020          */
3021         "System.Math::Sin", ves_icall_System_Math_Sin,
3022     "System.Math::Cos", ves_icall_System_Math_Cos,
3023     "System.Math::Tan", ves_icall_System_Math_Tan,
3024     "System.Math::Sinh", ves_icall_System_Math_Sinh,
3025     "System.Math::Cosh", ves_icall_System_Math_Cosh,
3026     "System.Math::Tanh", ves_icall_System_Math_Tanh,
3027     "System.Math::Acos", ves_icall_System_Math_Acos,
3028     "System.Math::Asin", ves_icall_System_Math_Asin,
3029     "System.Math::Atan", ves_icall_System_Math_Atan,
3030     "System.Math::Atan2", ves_icall_System_Math_Atan2,
3031     "System.Math::Exp", ves_icall_System_Math_Exp,
3032     "System.Math::Log", ves_icall_System_Math_Log,
3033     "System.Math::Log10", ves_icall_System_Math_Log10,
3034     "System.Math::PowImpl", ves_icall_System_Math_Pow,
3035     "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
3036
3037         /*
3038          * System.Environment
3039          */
3040         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
3041         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
3042         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
3043         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
3044         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
3045         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
3046         "System.Environment::Exit", ves_icall_System_Environment_Exit,
3047         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
3048
3049         /*
3050          * System.Runtime.Remoting
3051          */     
3052         "System.Runtime.Remoting.RemotingServices::InternalExecute",
3053         ves_icall_InternalExecute,
3054         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
3055         ves_icall_IsTransparentProxy,
3056
3057         /*
3058          * System.Runtime.Remoting.Messaging
3059          */     
3060         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
3061         ves_icall_MonoMethodMessage_InitMessage,
3062         
3063         /*
3064          * System.Runtime.Remoting.Proxies
3065          */     
3066         "System.Runtime.Remoting.Proxies.RealProxy::GetTransparentProxy", 
3067         ves_icall_Remoting_RealProxy_GetTransparentProxy,
3068
3069         /*
3070          * System.Threading.Interlocked
3071          */
3072         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
3073         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
3074         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
3075         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
3076         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
3077         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
3078         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
3079         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
3080         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
3081         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
3082
3083         /*
3084          * System.Diagnostics.Process
3085          */
3086         "System.Diagnostics.Process::GetCurrentProcess_internal()", ves_icall_System_Diagnostics_Process_GetCurrentProcess_internal,
3087         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
3088         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
3089         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
3090         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
3091         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
3092         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
3093         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
3094         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
3095         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
3096
3097         /* 
3098          * System.Delegate
3099          */
3100         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
3101
3102         /* 
3103          * System.Runtime.Serialization
3104          */
3105         "System.Runtime.Serialization.FormatterServices::GetUninitializedObjectInternal",
3106         ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal,
3107
3108         /*
3109          * System.IO.Path
3110          */
3111         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
3112
3113         /*
3114          * Private icalls for the Mono Debugger
3115          */
3116         "System.Reflection.Assembly::MonoDebugger_GetMethod",
3117         ves_icall_MonoDebugger_GetMethod,
3118
3119         /*
3120          * add other internal calls here
3121          */
3122         NULL, NULL
3123 };
3124
3125 void
3126 mono_init_icall (void)
3127 {
3128         const char *name;
3129         int i = 0;
3130
3131         while ((name = icall_map [i])) {
3132                 mono_add_internal_call (name, icall_map [i+1]);
3133                 i += 2;
3134         }
3135        
3136 }
3137
3138