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