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