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