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