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