2002-07-06 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mono / metadata / icall.c
1 /*
2  * icall.c:
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *       Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 #if defined (PLATFORM_WIN32)
19 #include <stdlib.h>
20 #endif
21
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/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         MonoDomain *domain = mono_domain_get (); 
679         MonoType *type;
680         MonoImage *image;
681         MonoTypeNameParse info;
682         gchar *str;
683         
684         str = mono_string_to_utf8 (name);
685         /*g_print ("requested type %s\n", str);*/
686         if (!mono_reflection_parse_type (str, &info)) {
687                 g_free (str);
688                 g_list_free (info.modifiers);
689                 g_list_free (info.nested);
690                 return NULL;
691         }
692
693         if (info.assembly) {
694                 image = mono_image_loaded (info.assembly);
695                 /* do we need to load if it's not already loaded? */
696                 if (!image) {
697                         g_free (str);
698                         g_list_free (info.modifiers);
699                         g_list_free (info.nested);
700                         return NULL;
701                 }
702         } else
703                 image = mono_defaults.corlib;
704
705         type = mono_reflection_get_type (image, &info, FALSE);
706         g_free (str);
707         g_list_free (info.modifiers);
708         g_list_free (info.nested);
709         if (!type)
710                 return NULL;
711         /*g_print ("got it\n");*/
712         return mono_type_get_object (domain, type);
713 }
714
715 static MonoReflectionType*
716 ves_icall_type_from_handle (MonoType *handle)
717 {
718         MonoDomain *domain = mono_domain_get (); 
719         MonoClass *klass = mono_class_from_mono_type (handle);
720
721         mono_class_init (klass);
722         return mono_type_get_object (domain, handle);
723 }
724
725 static guint32
726 ves_icall_type_Equals (MonoReflectionType *type, MonoReflectionType *c)
727 {
728         if (type->type && c->type)
729                 return mono_metadata_type_equal (type->type, c->type);
730         g_print ("type equals\n");
731         return 0;
732 }
733
734 static guint32
735 ves_icall_type_is_subtype_of (MonoReflectionType *type, MonoReflectionType *c, MonoBoolean check_interfaces)
736 {
737         MonoDomain *domain; 
738         MonoClass *klass;
739         MonoClass *klassc;
740
741         g_assert (type != NULL);
742         
743         domain = ((MonoObject *)type)->vtable->domain;
744
745         if (!c) /* FIXME: dont know what do do here */
746                 return 0;
747
748         klass = mono_class_from_mono_type (type->type);
749         klassc = mono_class_from_mono_type (c->type);
750
751         /* cut&paste from mono_object_isinst (): keep in sync */
752         if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
753                 MonoVTable *klass_vt = mono_class_vtable (domain, klass);
754                 if ((klassc->interface_id <= klass->max_interface_id) &&
755                     klass_vt->interface_offsets [klassc->interface_id])
756                         return 1;
757         } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
758                 int i;
759
760                 for (i = 0; i < klass->interface_count; i ++) {
761                         MonoClass *ic =  klass->interfaces [i];
762                         if (ic == klassc)
763                                 return 1;
764                 }
765         } else {
766                 /*
767                  * klass->baseval is 0 for interfaces 
768                  */
769                 if (klass->baseval && ((klass->baseval - klassc->baseval) <= klassc->diffval))
770                         return 1;
771         }
772         return 0;
773 }
774
775 static guint32
776 ves_icall_get_attributes (MonoReflectionType *type)
777 {
778         MonoClass *klass = mono_class_from_mono_type (type->type);
779
780         return klass->flags;
781 }
782
783 static void
784 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
785 {
786         MonoDomain *domain = mono_domain_get (); 
787
788         info->parent = mono_type_get_object (domain, &method->klass->byval_arg);
789         info->ret = mono_type_get_object (domain, method->signature->ret);
790         info->attrs = method->flags;
791         info->implattrs = method->iflags;
792 }
793
794 static MonoArray*
795 ves_icall_get_parameter_info (MonoMethod *method)
796 {
797         MonoDomain *domain = mono_domain_get (); 
798         MonoArray *res;
799         static MonoClass *System_Reflection_ParameterInfo;
800         MonoReflectionParameter** args;
801         int i;
802
803         args = mono_param_get_objects (domain, method);
804         if (!System_Reflection_ParameterInfo)
805                 System_Reflection_ParameterInfo = mono_class_from_name (
806                         mono_defaults.corlib, "System.Reflection", "ParameterInfo");
807         res = mono_array_new (domain, System_Reflection_ParameterInfo, method->signature->param_count);
808         for (i = 0; i < method->signature->param_count; ++i) {
809                 mono_array_set (res, gpointer, i, args [i]);
810         }
811         return res;
812 }
813
814 static void
815 ves_icall_get_field_info (MonoReflectionField *field, MonoFieldInfo *info)
816 {
817         MonoDomain *domain = mono_domain_get (); 
818
819         info->parent = mono_type_get_object (domain, &field->klass->byval_arg);
820         info->type = mono_type_get_object (domain, field->field->type);
821         info->name = mono_string_new (domain, field->field->name);
822         info->attrs = field->field->type->attrs;
823 }
824
825 static MonoObject *
826 ves_icall_MonoField_GetValue (MonoReflectionField *field, MonoObject *obj) {
827         MonoObject *res;
828         MonoClass *klass;
829         MonoType *ftype = field->field->type;
830         int type = ftype->type;
831         char *p, *r;
832         guint32 align;
833
834         mono_class_init (field->klass);
835         if (ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
836                 MonoVTable *vtable;
837                 vtable = mono_class_vtable (mono_domain_get (), field->klass);
838                 p = (char*)(vtable->data) + field->field->offset;
839         } else {
840                 p = (char*)obj + field->field->offset;
841         }
842
843         switch (type) {
844         case MONO_TYPE_OBJECT:
845         case MONO_TYPE_STRING:
846         case MONO_TYPE_SZARRAY:
847         case MONO_TYPE_ARRAY:
848                 return *(MonoObject**)p;
849         }
850         klass = mono_class_from_mono_type (ftype);
851         res = mono_object_new (mono_domain_get (), klass);
852         r = (char*)res + sizeof (MonoObject);
853         memcpy (r, p, mono_class_value_size (klass, &align));
854
855         return res;
856 }
857
858 static void
859 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
860 {
861         MonoDomain *domain = mono_domain_get (); 
862
863         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
864         info->name = mono_string_new (domain, property->property->name);
865         info->attrs = property->property->attrs;
866         info->get = property->property->get ? mono_method_get_object (domain, property->property->get): NULL;
867         info->set = property->property->set ? mono_method_get_object (domain, property->property->set): NULL;
868         /* 
869          * There may be other methods defined for properties, though, it seems they are not exposed 
870          * in the reflection API 
871          */
872 }
873
874 static void
875 ves_icall_get_event_info (MonoReflectionEvent *event, MonoEventInfo *info)
876 {
877         MonoDomain *domain = mono_domain_get (); 
878
879         info->parent = mono_type_get_object (domain, &event->klass->byval_arg);
880         info->name = mono_string_new (domain, event->event->name);
881         info->attrs = event->event->attrs;
882         info->add_method = event->event->add ? mono_method_get_object (domain, event->event->add): NULL;
883         info->remove_method = event->event->remove ? mono_method_get_object (domain, event->event->remove): NULL;
884         info->raise_method = event->event->raise ? mono_method_get_object (domain, event->event->raise): NULL;
885 }
886
887 static MonoArray*
888 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
889 {
890         MonoDomain *domain = mono_domain_get (); 
891         MonoArray *intf;
892         int ninterf, i;
893         MonoClass *class = mono_class_from_mono_type (type->type);
894         MonoClass *parent;
895
896         ninterf = 0;
897         for (parent = class; parent; parent = parent->parent) {
898                 ninterf += parent->interface_count;
899         }
900         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
901         ninterf = 0;
902         for (parent = class; parent; parent = parent->parent) {
903                 for (i = 0; i < parent->interface_count; ++i) {
904                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
905                         ++ninterf;
906                 }
907         }
908         return intf;
909 }
910
911 static MonoReflectionType*
912 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
913 {
914         MonoClass *class = mono_class_from_mono_type (type->type);
915         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
916                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
917         else if (class->element_class)
918                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
919         else
920                 return NULL;
921 }
922
923 static void
924 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
925 {
926         MonoDomain *domain = mono_domain_get (); 
927         MonoClass *class = mono_class_from_mono_type (type);
928
929         info->parent = class->parent ? mono_type_get_object (domain, &class->parent->byval_arg): NULL;
930         info->name = mono_string_new (domain, class->name);
931         info->name_space = mono_string_new (domain, class->name_space);
932         info->attrs = class->flags;
933         info->rank = class->rank;
934         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
935         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
936                 info->etype = mono_type_get_object (domain, class->enum_basetype);
937         else if (class->element_class)
938                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
939         else
940                 info->etype = NULL;
941
942         info->isbyref = type->byref;
943         info->ispointer = type->type == MONO_TYPE_PTR;
944         info->isprimitive = (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8);
945 }
946
947 static MonoObject *
948 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
949 {
950         return mono_runtime_invoke_array (method->method, this, params, NULL);
951 }
952
953 static MonoObject *
954 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
955 {
956         MonoDomain *domain = mono_domain_get (); 
957         MonoMethod *m = method->method;
958         MonoMethodSignature *sig = m->signature;
959         MonoArray *out_args;
960         MonoObject *result;
961         int i, j, outarg_count = 0;
962
963         if (m->klass == mono_defaults.object_class) {
964
965                 if (!strcmp (m->name, "FieldGetter")) {
966                         MonoClass *k = this->vtable->klass;
967                         MonoString *name = mono_array_get (params, MonoString *, 1);
968                         char *str;
969
970                         str = mono_string_to_utf8 (name);
971                 
972                         for (i = 0; i < k->field.count; i++) {
973                                 if (!strcmp (k->fields [i].name, str)) {
974                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
975                                         if (field_klass->valuetype)
976                                                 result = mono_value_box (domain, field_klass,
977                                                                          (char *)this + k->fields [i].offset);
978                                         else 
979                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
980                                 
981                                         g_assert (result);
982                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
983                                         *outArgs = out_args;
984                                         mono_array_set (out_args, gpointer, 0, result);
985                                         g_free (str);
986                                         return NULL;
987                                 }
988                         }
989
990                         g_free (str);
991                         g_assert_not_reached ();
992
993                 } else if (!strcmp (m->name, "FieldSetter")) {
994                         MonoClass *k = this->vtable->klass;
995                         MonoString *name = mono_array_get (params, MonoString *, 1);
996                         int size, align;
997                         char *str;
998
999                         str = mono_string_to_utf8 (name);
1000                 
1001                         for (i = 0; i < k->field.count; i++) {
1002                                 if (!strcmp (k->fields [i].name, str)) {
1003                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1004                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1005
1006                                         if (field_klass->valuetype) {
1007                                                 size = mono_type_size (k->fields [i].type, &align);
1008                                                 memcpy ((char *)this + k->fields [i].offset, 
1009                                                         ((char *)val) + sizeof (MonoObject), size);
1010                                         } else 
1011                                                 *((gpointer *)this + k->fields [i].offset) = val;
1012                                 
1013                                         g_assert (result);
1014                                         g_free (str);
1015                                         return NULL;
1016                                 }
1017                         }
1018
1019                         g_free (str);
1020                         g_assert_not_reached ();
1021
1022                 }
1023         }
1024
1025         for (i = 0; i < mono_array_length (params); i++) {
1026                 if (sig->params [i]->byref) 
1027                         outarg_count++;
1028         }
1029
1030         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1031         
1032         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1033                 if (sig->params [i]->byref) {
1034                         gpointer arg;
1035                         arg = mono_array_get (params, gpointer, i);
1036                         mono_array_set (out_args, gpointer, j, arg);
1037                         j++;
1038                 }
1039         }
1040
1041         /* fixme: handle constructors? */
1042         if (!strcmp (method->method->name, ".ctor"))
1043                 g_assert_not_reached ();
1044
1045         result = mono_runtime_invoke_array (method->method, this, params, NULL);
1046
1047         *outArgs = out_args;
1048
1049         return result;
1050 }
1051
1052 static MonoObject *
1053 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1054 {
1055         MonoDomain *domain = mono_domain_get (); 
1056         MonoClass *enumc, *objc;
1057         gint32 s1, s2;
1058         MonoObject *res;
1059         
1060         MONO_CHECK_ARG_NULL (type);
1061         MONO_CHECK_ARG_NULL (obj);
1062
1063         enumc = mono_class_from_mono_type (type->type);
1064         objc = obj->vtable->klass;
1065
1066         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1067         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1068                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1069         
1070         s1 = mono_class_value_size (enumc, NULL);
1071         s2 = mono_class_value_size (objc, NULL);
1072
1073         res = mono_object_new (domain, enumc);
1074
1075 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1076         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1077 #else
1078         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1079                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1080                 MIN (s1, s2));
1081 #endif
1082         return res;
1083 }
1084
1085 static MonoObject *
1086 ves_icall_System_Enum_get_value (MonoObject *this)
1087 {
1088         MonoDomain *domain = mono_domain_get (); 
1089         MonoObject *res;
1090         MonoClass *enumc;
1091         gpointer dst;
1092         gpointer src;
1093         int size;
1094
1095         if (!this)
1096                 return NULL;
1097
1098         g_assert (this->vtable->klass->enumtype);
1099         
1100         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1101         res = mono_object_new (domain, enumc);
1102         dst = (char *)res + sizeof (MonoObject);
1103         src = (char *)this + sizeof (MonoObject);
1104         size = mono_class_value_size (enumc, NULL);
1105
1106         memcpy (dst, src, size);
1107
1108         return res;
1109 }
1110
1111 static void
1112 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1113 {
1114         MonoDomain *domain = mono_domain_get (); 
1115         MonoClass *enumc = mono_class_from_mono_type (type->type);
1116         guint i, j, nvalues, crow;
1117         MonoClassField *field;
1118         
1119         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1120         nvalues = enumc->field.count - 1;
1121         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1122         info->values = mono_array_new (domain, enumc, nvalues);
1123         
1124         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1125                 field = &enumc->fields [i];
1126                 if (strcmp ("value__", field->name) == 0)
1127                         continue;
1128                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1129                 if (!field->data) {
1130                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1131                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1132                         /* 1 is the length of the blob */
1133                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1134                 }
1135                 switch (enumc->enum_basetype->type) {
1136                 case MONO_TYPE_U1:
1137                 case MONO_TYPE_I1:
1138                         mono_array_set (info->values, gchar, j, *field->data);
1139                         break;
1140                 case MONO_TYPE_CHAR:
1141                 case MONO_TYPE_U2:
1142                 case MONO_TYPE_I2:
1143                         mono_array_set (info->values, gint16, j, read16 (field->data));
1144                         break;
1145                 case MONO_TYPE_U4:
1146                 case MONO_TYPE_I4:
1147                         mono_array_set (info->values, gint32, j, read32 (field->data));
1148                         break;
1149                 case MONO_TYPE_U8:
1150                 case MONO_TYPE_I8:
1151                         mono_array_set (info->values, gint64, j, read64 (field->data));
1152                         break;
1153                 default:
1154                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1155                 }
1156                 ++j;
1157         }
1158 }
1159
1160 static MonoMethod*
1161 search_method (MonoReflectionType *type, const char *name, guint32 flags, MonoArray *args)
1162 {
1163         MonoClass *klass, *start_class;
1164         MonoMethod *m;
1165         MonoReflectionType *paramt;
1166         int i, j;
1167
1168         start_class = klass = mono_class_from_mono_type (type->type);
1169         while (klass) {
1170                 for (i = 0; i < klass->method.count; ++i) {
1171                         m = klass->methods [i];
1172                         if (!((m->flags & flags) == flags))
1173                                 continue;
1174                         if (strcmp(m->name, name))
1175                                 continue;
1176                         if (m->signature->param_count != mono_array_length (args))
1177                                 continue;
1178                         for (j = 0; j < m->signature->param_count; ++j) {
1179                                 paramt = mono_array_get (args, MonoReflectionType*, j);
1180                                 if (!mono_metadata_type_equal (paramt->type, m->signature->params [j]))
1181                                         break;
1182                         }
1183                         if (j == m->signature->param_count)
1184                                 return m;
1185                 }
1186                 klass = klass->parent;
1187         }
1188         g_print ("Method %s.%s::%s (%d) not found\n", start_class->name_space, start_class->name, name, mono_array_length (args));
1189         return NULL;
1190 }
1191
1192 static MonoReflectionMethod*
1193 ves_icall_get_constructor (MonoReflectionType *type, MonoArray *args)
1194 {
1195         MonoDomain *domain = mono_domain_get (); 
1196         MonoMethod *m;
1197
1198         m = search_method (type, ".ctor", METHOD_ATTRIBUTE_RT_SPECIAL_NAME, args);
1199         if (m)
1200                 return mono_method_get_object (domain, m);
1201         return NULL;
1202 }
1203
1204 static MonoReflectionMethod*
1205 ves_icall_get_method (MonoReflectionType *type, MonoString *name, MonoArray *args)
1206 {
1207         MonoDomain *domain = mono_domain_get (); 
1208         MonoMethod *m;
1209         char *n = mono_string_to_utf8 (name);
1210
1211         m = search_method (type, n, 0, args);
1212         g_free (n);
1213         if (m)
1214                 return mono_method_get_object (domain, m);
1215         return NULL;
1216 }
1217
1218 static MonoProperty*
1219 search_property (MonoClass *klass, char* name, MonoArray *args) {
1220         int i;
1221         MonoProperty *p;
1222
1223         /* FIXME: handle args */
1224         for (i = 0; i < klass->property.count; ++i) {
1225                 p = &klass->properties [i];
1226                 if (strcmp (p->name, name) == 0)
1227                         return p;
1228         }
1229         return NULL;
1230 }
1231
1232 static MonoReflectionProperty*
1233 ves_icall_get_property (MonoReflectionType *type, MonoString *name, MonoArray *args)
1234 {
1235         MonoDomain *domain = mono_domain_get (); 
1236         MonoProperty *p;
1237         MonoClass *class = mono_class_from_mono_type (type->type);
1238         char *n = mono_string_to_utf8 (name);
1239
1240         p = search_property (class, n, args);
1241         g_free (n);
1242         if (p)
1243                 return mono_property_get_object (domain, class, p);
1244         return NULL;
1245 }
1246
1247 enum {
1248         BFLAGS_IgnoreCase = 1,
1249         BFLAGS_DeclaredOnly = 2,
1250         BFLAGS_Instance = 4,
1251         BFLAGS_Static = 8,
1252         BFLAGS_Public = 0x10,
1253         BFLAGS_NonPublic = 0x20,
1254         BFLAGS_InvokeMethod = 0x100,
1255         BFLAGS_CreateInstance = 0x200,
1256         BFLAGS_GetField = 0x400,
1257         BFLAGS_SetField = 0x800,
1258         BFLAGS_GetProperty = 0x1000,
1259         BFLAGS_SetProperty = 0x2000,
1260         BFLAGS_ExactBinding = 0x10000,
1261         BFLAGS_SuppressChangeType = 0x20000,
1262         BFLAGS_OptionalParamBinding = 0x40000
1263 };
1264
1265 static MonoFieldInfo *
1266 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
1267 {
1268         MonoDomain *domain; 
1269         MonoClass *startklass, *klass;
1270         int i, match;
1271         MonoClassField *field;
1272         char *utf8_name;
1273         domain = ((MonoObject *)type)->vtable->domain;
1274         klass = startklass = mono_class_from_mono_type (type->type);
1275
1276         if (!name)
1277                 return NULL;
1278
1279 handle_parent:  
1280         for (i = 0; i < klass->field.count; ++i) {
1281                 match = 0;
1282                 field = &klass->fields [i];
1283                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1284                         if (bflags & BFLAGS_Public)
1285                                 match++;
1286                 } else {
1287                         if (bflags & BFLAGS_NonPublic)
1288                                 match++;
1289                 }
1290                 if (!match)
1291                         continue;
1292                 match = 0;
1293                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1294                         if (bflags & BFLAGS_Static)
1295                                 match++;
1296                 } else {
1297                         if (bflags & BFLAGS_Instance)
1298                                 match++;
1299                 }
1300
1301                 if (!match)
1302                         continue;
1303                 
1304                 utf8_name = mono_string_to_utf8 (name);
1305
1306                 if (strcmp (field->name, utf8_name)) {
1307                         g_free (utf8_name);
1308                         continue;
1309                 }
1310                 g_free (utf8_name);
1311                 
1312                 return mono_field_get_object (domain, klass, field);
1313         }
1314         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1315                 goto handle_parent;
1316
1317         return NULL;
1318 }
1319
1320 static MonoArray*
1321 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1322 {
1323         MonoDomain *domain; 
1324         GSList *l = NULL, *tmp;
1325         MonoClass *startklass, *klass;
1326         MonoArray *res;
1327         MonoObject *member;
1328         int i, len, match;
1329         MonoClassField *field;
1330
1331         domain = ((MonoObject *)type)->vtable->domain;
1332         klass = startklass = mono_class_from_mono_type (type->type);
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                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1359                 l = g_slist_prepend (l, member);
1360         }
1361         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1362                 goto handle_parent;
1363         len = g_slist_length (l);
1364         res = mono_array_new (domain, mono_defaults.field_info_class, len);
1365         i = 0;
1366         tmp = g_slist_reverse (l);
1367         for (; tmp; tmp = tmp->next, ++i)
1368                 mono_array_set (res, gpointer, i, tmp->data);
1369         g_slist_free (l);
1370         return res;
1371 }
1372
1373 static MonoArray*
1374 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1375 {
1376         MonoDomain *domain; 
1377         GSList *l = NULL, *tmp;
1378         static MonoClass *System_Reflection_MethodInfo;
1379         MonoClass *startklass, *klass;
1380         MonoArray *res;
1381         MonoMethod *method;
1382         MonoObject *member;
1383         int i, len, match;
1384                 
1385         domain = ((MonoObject *)type)->vtable->domain;
1386         klass = startklass = mono_class_from_mono_type (type->type);
1387
1388 handle_parent:
1389         for (i = 0; i < klass->method.count; ++i) {
1390                 match = 0;
1391                 method = klass->methods [i];
1392                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1393                         continue;
1394                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1395                         if (bflags & BFLAGS_Public)
1396                                 match++;
1397                 } else {
1398                         if (bflags & BFLAGS_NonPublic)
1399                                 match++;
1400                 }
1401                 if (!match)
1402                         continue;
1403                 match = 0;
1404                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1405                         if (bflags & BFLAGS_Static)
1406                                 match++;
1407                 } else {
1408                         if (bflags & BFLAGS_Instance)
1409                                 match++;
1410                 }
1411
1412                 if (!match)
1413                         continue;
1414                 match = 0;
1415                 member = (MonoObject*)mono_method_get_object (domain, method);
1416                         
1417                 l = g_slist_prepend (l, member);
1418         }
1419         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1420                 goto handle_parent;
1421         len = g_slist_length (l);
1422         if (!System_Reflection_MethodInfo)
1423                 System_Reflection_MethodInfo = mono_class_from_name (
1424                         mono_defaults.corlib, "System.Reflection", "MethodInfo");
1425         res = mono_array_new (domain, System_Reflection_MethodInfo, len);
1426         i = 0;
1427         tmp = l;
1428         for (; tmp; tmp = tmp->next, ++i)
1429                 mono_array_set (res, gpointer, i, tmp->data);
1430         g_slist_free (l);
1431
1432         return res;
1433 }
1434
1435 static MonoArray*
1436 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1437 {
1438         MonoDomain *domain; 
1439         GSList *l = NULL, *tmp;
1440         static MonoClass *System_Reflection_ConstructorInfo;
1441         MonoClass *startklass, *klass;
1442         MonoArray *res;
1443         MonoMethod *method;
1444         MonoObject *member;
1445         int i, len, match;
1446
1447         domain = ((MonoObject *)type)->vtable->domain;
1448         klass = startklass = mono_class_from_mono_type (type->type);
1449
1450 handle_parent:  
1451         for (i = 0; i < klass->method.count; ++i) {
1452                 match = 0;
1453                 method = klass->methods [i];
1454                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1455                         continue;
1456                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1457                         if (bflags & BFLAGS_Public)
1458                                 match++;
1459                 } else {
1460                         if (bflags & BFLAGS_NonPublic)
1461                                 match++;
1462                 }
1463                 if (!match)
1464                         continue;
1465                 match = 0;
1466                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1467                         if (bflags & BFLAGS_Static)
1468                                 match++;
1469                 } else {
1470                         if (bflags & BFLAGS_Instance)
1471                                 match++;
1472                 }
1473
1474                 if (!match)
1475                         continue;
1476                 member = (MonoObject*)mono_method_get_object (domain, method);
1477                         
1478                 l = g_slist_prepend (l, member);
1479         }
1480         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1481                 goto handle_parent;
1482         len = g_slist_length (l);
1483         if (!System_Reflection_ConstructorInfo)
1484                 System_Reflection_ConstructorInfo = mono_class_from_name (
1485                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1486         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1487         i = 0;
1488         tmp = g_slist_reverse (l);
1489         for (; tmp; tmp = tmp->next, ++i)
1490                 mono_array_set (res, gpointer, i, tmp->data);
1491         g_slist_free (l);
1492         return res;
1493 }
1494
1495 static MonoArray*
1496 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1497 {
1498         MonoDomain *domain; 
1499         GSList *l = NULL, *tmp;
1500         static MonoClass *System_Reflection_PropertyInfo;
1501         MonoClass *startklass, *klass;
1502         MonoArray *res;
1503         MonoMethod *method;
1504         MonoProperty *prop;
1505         int i, len, match;
1506
1507         domain = ((MonoObject *)type)->vtable->domain;
1508         klass = startklass = mono_class_from_mono_type (type->type);
1509
1510 handle_parent:
1511         for (i = 0; i < klass->property.count; ++i) {
1512                 prop = &klass->properties [i];
1513                 match = 0;
1514                 method = prop->get;
1515                 if (!method)
1516                         method = prop->set;
1517                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1518                         if (bflags & BFLAGS_Public)
1519                                 match++;
1520                 } else {
1521                         if (bflags & BFLAGS_NonPublic)
1522                                 match++;
1523                 }
1524                 if (!match)
1525                         continue;
1526                 match = 0;
1527                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1528                         if (bflags & BFLAGS_Static)
1529                                 match++;
1530                 } else {
1531                         if (bflags & BFLAGS_Instance)
1532                                 match++;
1533                 }
1534
1535                 if (!match)
1536                         continue;
1537                 match = 0;
1538                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1539         }
1540         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1541                 goto handle_parent;
1542         len = g_slist_length (l);
1543         if (!System_Reflection_PropertyInfo)
1544                 System_Reflection_PropertyInfo = mono_class_from_name (
1545                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1546         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1547         i = 0;
1548         tmp = l;
1549         for (; tmp; tmp = tmp->next, ++i)
1550                 mono_array_set (res, gpointer, i, tmp->data);
1551         g_slist_free (l);
1552         return res;
1553 }
1554
1555 static MonoArray*
1556 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
1557 {
1558         MonoDomain *domain; 
1559         GSList *l = NULL, *tmp;
1560         static MonoClass *System_Reflection_EventInfo;
1561         MonoClass *startklass, *klass;
1562         MonoArray *res;
1563         MonoMethod *method;
1564         MonoEvent *event;
1565         int i, len, match;
1566
1567         domain = ((MonoObject *)type)->vtable->domain;
1568         klass = startklass = mono_class_from_mono_type (type->type);
1569
1570 handle_parent:  
1571         for (i = 0; i < klass->event.count; ++i) {
1572                 event = &klass->events [i];
1573                 match = 0;
1574                 method = event->add;
1575                 if (!method)
1576                         method = event->remove;
1577                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1578                         if (bflags & BFLAGS_Public)
1579                                 match++;
1580                 } else {
1581                         if (bflags & BFLAGS_NonPublic)
1582                                 match++;
1583                 }
1584                 if (!match)
1585                         continue;
1586                 match = 0;
1587                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1588                         if (bflags & BFLAGS_Static)
1589                                 match++;
1590                 } else {
1591                         if (bflags & BFLAGS_Instance)
1592                                 match++;
1593                 }
1594
1595                 if (!match)
1596                         continue;
1597                 match = 0;
1598                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
1599         }
1600         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1601                 goto handle_parent;
1602         len = g_slist_length (l);
1603         if (!System_Reflection_EventInfo)
1604                 System_Reflection_EventInfo = mono_class_from_name (
1605                         mono_defaults.corlib, "System.Reflection", "EventInfo");
1606         res = mono_array_new (domain, System_Reflection_EventInfo, len);
1607         i = 0;
1608         tmp = l;
1609         for (; tmp; tmp = tmp->next, ++i)
1610                 mono_array_set (res, gpointer, i, tmp->data);
1611         g_slist_free (l);
1612         return res;
1613 }
1614
1615 static MonoArray*
1616 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
1617 {
1618         MonoDomain *domain; 
1619         GSList *l = NULL, *tmp;
1620         GList *tmpn;
1621         MonoClass *startklass, *klass;
1622         MonoArray *res;
1623         MonoObject *member;
1624         int i, len, match;
1625         MonoClass *nested;
1626
1627         domain = ((MonoObject *)type)->vtable->domain;
1628         klass = startklass = mono_class_from_mono_type (type->type);
1629
1630         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
1631                 match = 0;
1632                 nested = tmpn->data;
1633                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
1634                         if (bflags & BFLAGS_Public)
1635                                 match++;
1636                 } else {
1637                         if (bflags & BFLAGS_NonPublic)
1638                                 match++;
1639                 }
1640                 if (!match)
1641                         continue;
1642                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
1643                 l = g_slist_prepend (l, member);
1644         }
1645         len = g_slist_length (l);
1646         res = mono_array_new (domain, mono_defaults.monotype_class, len);
1647         i = 0;
1648         tmp = g_slist_reverse (l);
1649         for (; tmp; tmp = tmp->next, ++i)
1650                 mono_array_set (res, gpointer, i, tmp->data);
1651         g_slist_free (l);
1652         return res;
1653 }
1654
1655 static void
1656 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *src, gint32 start_index,
1657                                                                     gpointer dest, gint32 length)
1658 {
1659         int element_size;
1660         void *source_addr;
1661
1662         MONO_CHECK_ARG_NULL (src);
1663         MONO_CHECK_ARG_NULL (dest);
1664
1665         g_assert (src->obj.vtable->klass->rank == 1);
1666         g_assert (start_index >= 0 && start_index < mono_array_length (src));
1667         g_assert (start_index + length <= mono_array_length (src));
1668
1669         element_size = mono_array_element_size (src->obj.vtable->klass);
1670           
1671         source_addr = mono_array_addr_with_size (src, element_size, start_index);
1672
1673         memcpy (dest, source_addr, length * element_size);
1674 }
1675
1676 static void
1677 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src, gint32 start_index,
1678                                                                       MonoArray *dest, gint32 length)
1679 {
1680         int element_size;
1681         void *dest_addr;
1682
1683         MONO_CHECK_ARG_NULL (src);
1684         MONO_CHECK_ARG_NULL (dest);
1685
1686         g_assert (dest->obj.vtable->klass->rank == 1);
1687         g_assert (start_index >= 0 && start_index < mono_array_length (dest));
1688         g_assert (start_index + length <= mono_array_length (dest));
1689
1690         element_size = mono_array_element_size (dest->obj.vtable->klass);
1691           
1692         dest_addr = mono_array_addr_with_size (dest, element_size, start_index);
1693
1694         memcpy (dest_addr, src, length * element_size);
1695 }
1696
1697 static gpointer
1698 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr, gint32 offset)
1699 {
1700         char *p = ptr;
1701         return *(gpointer*)(p + offset);
1702 }
1703
1704 static unsigned char
1705 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr, gint32 offset)
1706 {
1707         char *p = ptr;
1708         return *(unsigned char*)(p + offset);
1709 }
1710
1711 static gint16
1712 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr, gint32 offset)
1713 {
1714         char *p = ptr;
1715         return *(gint16*)(p + offset);
1716 }
1717
1718 static gint32
1719 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr, gint32 offset)
1720 {
1721         char *p = ptr;
1722         return *(gint32*)(p + offset);
1723 }
1724
1725 static gint64
1726 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr, gint32 offset)
1727 {
1728         char *p = ptr;
1729         return *(gint64*)(p + offset);
1730 }
1731
1732 static void
1733 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr, gint32 offset, unsigned char val)
1734 {
1735         char *p = ptr;
1736         *(unsigned char*)(p + offset) = val;
1737 }
1738
1739 static void
1740 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr, gint32 offset, gpointer val)
1741 {
1742         char *p = ptr;
1743         *(gpointer*)(p + offset) = val;
1744 }
1745
1746 static void
1747 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr, gint32 offset, gint16 val)
1748 {
1749         char *p = ptr;
1750         *(gint16*)(p + offset) = val;
1751 }
1752
1753 static void
1754 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr, gint32 offset, gint32 val)
1755 {
1756         char *p = ptr;
1757         *(gint32*)(p + offset) = val;
1758 }
1759
1760 static void
1761 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr, gint32 offset, gint64 val)
1762 {
1763         char *p = ptr;
1764         *(gint64*)(p + offset) = val;
1765 }
1766
1767 static MonoString*
1768 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto (gpointer ptr)
1769 {
1770         MonoDomain *domain = mono_domain_get (); 
1771
1772         return mono_string_new (domain, (char *)ptr);
1773 }
1774
1775 static guint32 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error(void)
1776 {
1777         return(GetLastError());
1778 }
1779
1780 static MonoReflectionType*
1781 ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
1782 {
1783         MonoDomain *domain = mono_domain_get (); 
1784         gchar *str;
1785         MonoType *type;
1786         MonoTypeNameParse info;
1787
1788         str = mono_string_to_utf8 (name);
1789         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
1790         if (!mono_reflection_parse_type (str, &info)) {
1791                 g_free (str);
1792                 g_list_free (info.modifiers);
1793                 g_list_free (info.nested);
1794                 if (throwOnError) /* uhm: this is a parse error, though... */
1795                         mono_raise_exception (mono_get_exception_type_load ());
1796                 /*g_print ("failed parse\n");*/
1797                 return NULL;
1798         }
1799
1800         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
1801         g_free (str);
1802         g_list_free (info.modifiers);
1803         g_list_free (info.nested);
1804         if (!type) {
1805                 if (throwOnError)
1806                         mono_raise_exception (mono_get_exception_type_load ());
1807                 /* g_print ("failed find\n"); */
1808                 return NULL;
1809         }
1810         /* g_print ("got it\n"); */
1811         return mono_type_get_object (domain, type);
1812
1813 }
1814
1815 static MonoString *
1816 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
1817 {
1818         MonoDomain *domain = mono_domain_get (); 
1819         MonoString *res;
1820         char *name = g_strconcat (
1821                 "file://", assembly->assembly->image->name, NULL);
1822         
1823         res = mono_string_new (domain, name);
1824         g_free (name);
1825         return res;
1826 }
1827
1828 static MonoString *
1829 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
1830 {
1831         MonoDomain *domain = mono_domain_get (); 
1832         MonoString *res;
1833         gchar *name;
1834
1835         name = mono_type_get_name (object->type);
1836         res = mono_string_new (domain, name);
1837         g_free (name);
1838
1839         return res;
1840 }
1841
1842 static void
1843 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
1844 {
1845         MonoAssemblyName *name = &assembly->assembly->aname;
1846
1847         if (strcmp (name->name, "corlib") == 0)
1848                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
1849         else
1850                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
1851         aname->major = name->major;
1852 }
1853
1854 static MonoArray*
1855 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
1856 {
1857         MonoDomain *domain = mono_domain_get (); 
1858         MonoArray *res;
1859         MonoClass *klass;
1860         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
1861         int i, count;
1862         guint32 attrs, visibility;
1863
1864         /* we start the count from 1 because we skip the special type <Module> */
1865         if (exportedOnly) {
1866                 count = 0;
1867                 for (i = 1; i < tdef->rows; ++i) {
1868                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
1869                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1870                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
1871                                 count++;
1872                 }
1873         } else {
1874                 count = tdef->rows - 1;
1875         }
1876         res = mono_array_new (domain, mono_defaults.monotype_class, count);
1877         count = 0;
1878         for (i = 1; i < tdef->rows; ++i) {
1879                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
1880                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1881                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
1882                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
1883                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
1884                         count++;
1885                 }
1886         }
1887         
1888         return res;
1889 }
1890
1891 static MonoReflectionType*
1892 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
1893 {
1894         MonoClass *klass;
1895         int isbyref = 0, rank;
1896         char *str = mono_string_to_utf8 (smodifiers);
1897         char *p;
1898
1899         klass = mono_class_from_mono_type (tb->type.type);
1900         p = str;
1901         /* logic taken from mono_reflection_parse_type(): keep in sync */
1902         while (*p) {
1903                 switch (*p) {
1904                 case '&':
1905                         if (isbyref) { /* only one level allowed by the spec */
1906                                 g_free (str);
1907                                 return NULL;
1908                         }
1909                         isbyref = 1;
1910                         p++;
1911                         g_free (str);
1912                         return mono_type_get_object (mono_domain_get (), &klass->this_arg);
1913                         break;
1914                 case '*':
1915                         klass = mono_ptr_class_get (&klass->byval_arg);
1916                         mono_class_init (klass);
1917                         p++;
1918                         break;
1919                 case '[':
1920                         rank = 1;
1921                         p++;
1922                         while (*p) {
1923                                 if (*p == ']')
1924                                         break;
1925                                 if (*p == ',')
1926                                         rank++;
1927                                 else if (*p != '*') { /* '*' means unknown lower bound */
1928                                         g_free (str);
1929                                         return NULL;
1930                                 }
1931                                 ++p;
1932                         }
1933                         if (*p != ']') {
1934                                 g_free (str);
1935                                 return NULL;
1936                         }
1937                         p++;
1938                         klass = mono_array_class_get (&klass->byval_arg, rank);
1939                         mono_class_init (klass);
1940                         break;
1941                 default:
1942                         break;
1943                 }
1944         }
1945         g_free (str);
1946         return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
1947 }
1948
1949 /*
1950  * Magic number to convert a time which is relative to
1951  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
1952  */
1953 #define EPOCH_ADJUST    ((gint64)62135596800L)
1954
1955 static gint64
1956 ves_icall_System_DateTime_GetNow (void)
1957 {
1958 #ifdef PLATFORM_WIN32
1959         SYSTEMTIME st;
1960         FILETIME ft;
1961         
1962         GetLocalTime (&st);
1963         SystemTimeToFileTime (&st, &ft);
1964         return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
1965 #else
1966         /* FIXME: put this in io-layer and call it GetLocalTime */
1967         struct timeval tv;
1968         gint64 res;
1969
1970         if (gettimeofday (&tv, NULL) == 0) {
1971                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
1972                 return res;
1973         }
1974         /* fixme: raise exception */
1975         return 0;
1976 #endif
1977 }
1978
1979 /*
1980  * This is heavily based on zdump.c from glibc 2.2.
1981  *
1982  *  * data[0]:  start of daylight saving time (in DateTime ticks).
1983  *  * data[1]:  end of daylight saving time (in DateTime ticks).
1984  *  * data[2]:  utcoffset (in TimeSpan ticks).
1985  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
1986  *  * name[0]:  name of this timezone when not daylight saving.
1987  *  * name[1]:  name of this timezone when daylight saving.
1988  *
1989  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
1990  *         the class library allows years between 1 and 9999.
1991  *
1992  *  Returns true on success and zero on failure.
1993  */
1994 static guint32
1995 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
1996 {
1997 #ifndef PLATFORM_WIN32
1998         MonoDomain *domain = mono_domain_get ();
1999         struct tm start, tt;
2000         time_t t;
2001
2002         long int gmtoff;
2003         int is_daylight = 0, day;
2004
2005         memset (&start, 0, sizeof (start));
2006
2007         start.tm_mday = 1;
2008         start.tm_year = year-1900;
2009
2010         t = mktime (&start);
2011 #if defined (HAVE_TIMEZONE)
2012 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
2013 #elif defined (HAVE_TM_GMTOFF)
2014 #define gmt_offset(x) x.tm_gmtoff
2015 #else
2016 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
2017 #endif
2018         
2019         gmtoff = gmt_offset (start);
2020         
2021         MONO_CHECK_ARG_NULL (data);
2022         MONO_CHECK_ARG_NULL (names);
2023
2024         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2025         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2026
2027         /* For each day of the year, calculate the tm_gmtoff. */
2028         for (day = 0; day < 365; day++) {
2029
2030                 t += 3600*24;
2031                 tt = *localtime (&t);
2032
2033                 /* Daylight saving starts or ends here. */
2034                 if (gmt_offset (tt) != gmtoff) {
2035                         char tzone[10];
2036                         struct tm tt1;
2037                         time_t t1;
2038
2039                         /* Try to find the exact hour when daylight saving starts/ends. */
2040                         t1 = t;
2041                         do {
2042                                 t1 -= 3600;
2043                                 tt1 = *localtime (&t1);
2044                         } while (gmt_offset (tt1) != gmtoff);
2045
2046                         /* Try to find the exact minute when daylight saving starts/ends. */
2047                         do {
2048                                 t1 += 60;
2049                                 tt1 = *localtime (&t1);
2050                         } while (gmt_offset (tt1) == gmtoff);
2051                         
2052                         strftime (tzone, 10, "%Z", &tt);
2053                         
2054                         /* Write data, if we're already in daylight saving, we're done. */
2055                         if (is_daylight) {
2056                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2057                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2058                                 return 1;
2059                         } else {
2060                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2061                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2062                                 is_daylight = 1;
2063                         }
2064
2065                         /* This is only set once when we enter daylight saving. */
2066                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
2067                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
2068
2069                         gmtoff = gmt_offset (tt);
2070                 }
2071
2072                 gmtoff = gmt_offset (tt);
2073         }
2074         return 1;
2075 #else
2076         MonoDomain *domain = mono_domain_get ();
2077         TIME_ZONE_INFORMATION tz_info;
2078         FILETIME ft;
2079         int i;
2080
2081         GetTimeZoneInformation (&tz_info);
2082
2083         MONO_CHECK_ARG_NULL (data);
2084         MONO_CHECK_ARG_NULL (names);
2085
2086         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2087         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2088
2089         for (i = 0; i < 32; ++i)
2090                 if (!tz_info.DaylightName [i])
2091                         break;
2092         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
2093         for (i = 0; i < 32; ++i)
2094                 if (!tz_info.StandardName [i])
2095                         break;
2096         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
2097
2098         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
2099         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2100         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
2101         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2102         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
2103         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
2104
2105         return 1;
2106 #endif
2107 }
2108
2109 static gpointer
2110 ves_icall_System_Object_obj_address (MonoObject *this) {
2111         return this;
2112 }
2113
2114 /* System.Buffer */
2115
2116 static gint32 
2117 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) {
2118         MonoClass *klass;
2119         MonoTypeEnum etype;
2120         int length, esize;
2121         int i;
2122
2123         klass = array->obj.vtable->klass;
2124         etype = klass->element_class->byval_arg.type;
2125         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
2126                 return -1;
2127
2128         if (array->bounds == NULL)
2129                 length = array->max_length;
2130         else {
2131                 length = 0;
2132                 for (i = 0; i < klass->rank; ++ i)
2133                         length += array->bounds [i].length;
2134         }
2135
2136         esize = mono_array_element_size (klass);
2137         return length * esize;
2138 }
2139
2140 static gint8 
2141 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) {
2142         return mono_array_get (array, gint8, idx);
2143 }
2144
2145 static void 
2146 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) {
2147         mono_array_set (array, gint8, idx, value);
2148 }
2149
2150 static void 
2151 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) {
2152         char *src_buf, *dest_buf;
2153
2154         src_buf = (gint8 *)src->vector + src_offset;
2155         dest_buf = (gint8 *)dest->vector + dest_offset;
2156
2157         memcpy (dest_buf, src_buf, count);
2158 }
2159
2160 static MonoObject *
2161 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
2162 {
2163         MonoDomain *domain = mono_domain_get (); 
2164         MonoObject *res;
2165         MonoRealProxy *rp = ((MonoRealProxy *)this);
2166         MonoType *type;
2167         MonoClass *klass;
2168
2169         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
2170         
2171         ((MonoTransparentProxy *)res)->rp = rp;
2172         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
2173         klass = mono_class_from_mono_type (type);
2174
2175         ((MonoTransparentProxy *)res)->klass = klass;
2176
2177         res->vtable = mono_class_proxy_vtable (domain, klass);
2178
2179         return res;
2180 }
2181
2182 /* System.Environment */
2183
2184 static MonoString *
2185 ves_icall_System_Environment_get_MachineName (void)
2186 {
2187 #if defined (PLATFORM_WIN32)
2188         gunichar2 *buf;
2189         guint32 len;
2190         MonoString *result;
2191
2192         len = MAX_COMPUTERNAME_LENGTH + 1;
2193         buf = g_new (gunichar2, len);
2194
2195         result = NULL;
2196         if (GetComputerName (buf, &len))
2197                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
2198
2199         g_free (buf);
2200         return result;
2201 #else
2202         gchar *buf;
2203         int len;
2204         MonoString *result;
2205
2206         len = 256;
2207         buf = g_new (gchar, len);
2208
2209         result = NULL;
2210         if (gethostname (buf, len) != 0)
2211                 result = mono_string_new (mono_domain_get (), buf);
2212         
2213         g_free (buf);
2214         return result;
2215 #endif
2216 }
2217
2218 static MonoString *
2219 ves_icall_System_Environment_get_NewLine (void)
2220 {
2221 #if defined (PLATFORM_WIN32)
2222         return mono_string_new (mono_domain_get (), "\r\n");
2223 #else
2224         return mono_string_new (mono_domain_get (), "\n");
2225 #endif
2226 }
2227
2228 static MonoString *
2229 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
2230 {
2231         const gchar *value;
2232         gchar *utf8_name;
2233
2234         if (name == NULL)
2235                 return NULL;
2236
2237         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
2238         value = g_getenv (utf8_name);
2239         g_free (utf8_name);
2240
2241         if (value == 0)
2242                 return NULL;
2243         
2244         return mono_string_new (mono_domain_get (), value);
2245 }
2246
2247 /*
2248  * There is no standard way to get at environ.
2249  */
2250 extern char **environ;
2251
2252 static MonoArray *
2253 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
2254 {
2255         MonoArray *names;
2256         MonoDomain *domain;
2257         MonoString *str;
2258         gchar **e, **parts;
2259         int n;
2260
2261         n = 0;
2262         for (e = environ; *e != 0; ++ e)
2263                 ++ n;
2264
2265         domain = mono_domain_get ();
2266         names = mono_array_new (domain, mono_defaults.string_class, n);
2267
2268         n = 0;
2269         for (e = environ; *e != 0; ++ e) {
2270                 parts = g_strsplit (*e, "=", 2);
2271                 if (*parts != 0) {
2272                         str = mono_string_new (domain, *parts);
2273                         mono_array_set (names, MonoString *, n, str);
2274                 }
2275
2276                 g_strfreev (parts);
2277
2278                 ++ n;
2279         }
2280
2281         return names;
2282 }
2283
2284 /*
2285  * Returns the number of milliseconds elapsed since the system started.
2286  */
2287 static gint32
2288 ves_icall_System_Environment_get_TickCount (void)
2289 {
2290 #if defined (PLATFORM_WIN32)
2291         return GetTickCount();
2292 #else
2293         struct timeval tv;
2294         struct timezone tz;
2295         gint32 res;
2296
2297         res = (gint32) gettimeofday (&tv, &tz);
2298
2299         if (res != -1)
2300                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
2301         return res;
2302 #endif
2303 }
2304
2305
2306 static void
2307 ves_icall_System_Environment_Exit (int result)
2308 {
2309         /* we may need to do some cleanup here... */
2310         exit (result);
2311 }
2312
2313 static void
2314 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
2315                                          MonoReflectionMethod *method,
2316                                          MonoArray *out_args)
2317 {
2318         MonoDomain *domain = mono_domain_get ();
2319         
2320         mono_message_init (domain, this, method, out_args);
2321 }
2322
2323 static MonoBoolean
2324 ves_icall_IsTransparentProxy (MonoObject *proxy)
2325 {
2326         if (!proxy)
2327                 return 0;
2328
2329         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
2330                 return 1;
2331
2332         return 0;
2333 }
2334
2335
2336 /* icall map */
2337
2338 static gconstpointer icall_map [] = {
2339         /*
2340          * System.Array
2341          */
2342         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
2343         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
2344         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
2345         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
2346         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
2347         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
2348         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
2349         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
2350         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
2351         "System.Array::Clone",            mono_array_clone,
2352
2353         /*
2354          * System.Object
2355          */
2356         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
2357         "System.Object::GetType", ves_icall_System_Object_GetType,
2358         "System.Object::GetHashCode", ves_icall_System_Object_GetHashCode,
2359         "System.Object::obj_address", ves_icall_System_Object_obj_address,
2360
2361         /*
2362          * System.ValueType
2363          */
2364         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
2365         "System.ValueType::Equals", ves_icall_System_ValueType_Equals,
2366
2367         /*
2368          * System.String
2369          */
2370         
2371         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
2372         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
2373         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
2374         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
2375         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
2376         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
2377         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
2378         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
2379         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
2380         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
2381         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
2382         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
2383         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
2384         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
2385         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
2386         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
2387         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
2388         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
2389         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
2390         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
2391         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
2392         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
2393         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
2394         "System.String::InternalPad", ves_icall_System_String_InternalPad,
2395         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
2396         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
2397         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
2398         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
2399         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
2400         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
2401         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
2402         "System.String::InternalCompare(string,int,string,int,int,bool)", ves_icall_System_String_InternalCompareStr_N,
2403         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
2404         "System.String::get_Chars", ves_icall_System_String_get_Chars,
2405
2406         /*
2407          * System.AppDomain
2408          */
2409         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
2410         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
2411         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
2412         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
2413         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
2414         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
2415         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
2416         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
2417         "System.AppDomain::Unload", ves_icall_System_AppDomain_Unload,
2418         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
2419
2420         /*
2421          * System.AppDomainSetup
2422          */
2423         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
2424
2425         /*
2426          * System.Double
2427          */
2428         "System.Double::ToStringImpl", mono_double_ToStringImpl,
2429         "System.Double::ParseImpl",    mono_double_ParseImpl,
2430
2431         /*
2432          * System.Single
2433          */
2434         "System.Single::ToStringImpl", mono_float_ToStringImpl,
2435
2436         /*
2437          * System.Decimal
2438          */
2439         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
2440         "System.Decimal::decimal2Int64", mono_decimal2Int64,
2441         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
2442         "System.Decimal::decimalIncr", mono_decimalIncr,
2443         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
2444         "System.Decimal::decimal2double", mono_decimal2double,
2445         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
2446         "System.Decimal::decimalRound", mono_decimalRound,
2447         "System.Decimal::decimalMult", mono_decimalMult,
2448         "System.Decimal::decimalDiv", mono_decimalDiv,
2449         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
2450         "System.Decimal::decimalCompare", mono_decimalCompare,
2451         "System.Decimal::string2decimal", mono_string2decimal,
2452         "System.Decimal::decimal2string", mono_decimal2string,
2453
2454         /*
2455          * ModuleBuilder
2456          */
2457         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
2458         
2459         /*
2460          * AssemblyBuilder
2461          */
2462         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
2463         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
2464         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
2465         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
2466
2467         /*
2468          * Reflection stuff.
2469          */
2470         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
2471         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
2472         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
2473         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
2474         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
2475         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
2476         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
2477         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
2478         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
2479         "System.Reflection.MonoField::GetValue", ves_icall_MonoField_GetValue,
2480         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
2481         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
2482
2483         
2484         /* System.Enum */
2485
2486         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
2487         "System.Enum::get_value", ves_icall_System_Enum_get_value,
2488         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
2489
2490         /*
2491          * TypeBuilder
2492          */
2493         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
2494         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
2495
2496         
2497         /*
2498          * MethodBuilder
2499          */
2500         
2501         /*
2502          * System.Type
2503          */
2504         "System.Type::internal_from_name", ves_icall_type_from_name,
2505         "System.Type::internal_from_handle", ves_icall_type_from_handle,
2506         "System.Type::get_constructor", ves_icall_get_constructor,
2507         "System.Type::get_property", ves_icall_get_property,
2508         "System.Type::get_method", ves_icall_get_method,
2509         "System.MonoType::get_attributes", ves_icall_get_attributes,
2510         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
2511         "System.Type::Equals", ves_icall_type_Equals,
2512
2513         /*
2514          * System.Runtime.CompilerServices.RuntimeHelpers
2515          */
2516         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_InitializeArray,
2517         
2518         /*
2519          * System.Threading
2520          */
2521         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
2522         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
2523         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
2524         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
2525         "System.Threading.Thread::CurrentThread_internal", ves_icall_System_Threading_Thread_CurrentThread_internal,
2526         "System.Threading.Thread::CurrentThreadDomain_internal", ves_icall_System_Threading_Thread_CurrentThreadDomain_internal,
2527         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
2528         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
2529         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
2530         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
2531         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
2532         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
2533         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
2534         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
2535         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
2536         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
2537         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
2538         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
2539         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
2540         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
2541         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
2542
2543         /*
2544          * System.Threading.WaitHandle
2545          */
2546         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
2547         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
2548         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
2549
2550         /*
2551          * System.Runtime.InteropServices.Marshal
2552          */
2553         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
2554         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
2555         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
2556         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
2557         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
2558         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
2559         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
2560         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
2561         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
2562         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
2563         "System.Runtime.InteropServices.Marshal::PtrToStringAuto", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto,
2564         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
2565         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
2566         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
2567         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
2568         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
2569         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
2570
2571
2572         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
2573         "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
2574         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
2575         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
2576         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
2577
2578         /*
2579          * System.MonoType.
2580          */
2581         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
2582         "System.MonoType::type_from_obj", mono_type_type_from_obj,
2583         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
2584         "System.MonoType::get_type_info", ves_icall_get_type_info,
2585         "System.MonoType::GetField", ves_icall_Type_GetField,
2586         "System.MonoType::GetFields", ves_icall_Type_GetFields,
2587         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
2588         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
2589         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
2590         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
2591         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
2592         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
2593
2594         /*
2595          * System.Net.Sockets I/O Services
2596          */
2597         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
2598         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
2599         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
2600         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
2601         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
2602         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
2603         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
2604         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
2605         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
2606         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
2607         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
2608         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
2609         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
2610         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
2611         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
2612         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
2613         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
2614         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
2615         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
2616         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
2617         "System.Net.Dns::GetHostByName_internal", ves_icall_System_Net_Dns_GetHostByName_internal,
2618         "System.Net.Dns::GetHostByAddr_internal", ves_icall_System_Net_Dns_GetHostByAddr_internal,
2619
2620         /*
2621          * System.Char
2622          */
2623         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
2624         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
2625         "System.Char::IsControl", ves_icall_System_Char_IsControl,
2626         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
2627         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
2628         "System.Char::IsLower", ves_icall_System_Char_IsLower,
2629         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
2630         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
2631         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
2632         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
2633         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
2634         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
2635         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
2636         "System.Char::ToLower", ves_icall_System_Char_ToLower,
2637         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
2638
2639         "System.Text.Encoding::IConvNewEncoder", ves_icall_iconv_new_encoder,
2640         "System.Text.Encoding::IConvNewDecoder", ves_icall_iconv_new_decoder,
2641         "System.Text.Encoding::IConvReset", ves_icall_iconv_reset,
2642         "System.Text.Encoding::IConvGetByteCount", ves_icall_iconv_get_byte_count,
2643         "System.Text.Encoding::IConvGetBytes", ves_icall_iconv_get_bytes,
2644         "System.Text.Encoding::IConvGetCharCount", ves_icall_iconv_get_char_count,
2645         "System.Text.Encoding::IConvGetChars", ves_icall_iconv_get_chars,
2646
2647         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
2648         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
2649
2650         /*
2651          * System.GC
2652          */
2653         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
2654         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
2655         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
2656         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
2657         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
2658         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
2659         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
2660         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
2661         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
2662         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
2663
2664         /*
2665          * System.Security.Cryptography calls
2666          */
2667
2668          "System.Security.Cryptography.RNGCryptoServiceProvider::GetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetBytes,
2669          "System.Security.Cryptography.RNGCryptoServiceProvider::GetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetNonZeroBytes,
2670         
2671         /*
2672          * System.Buffer
2673          */
2674         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
2675         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
2676         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
2677         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
2678
2679         /*
2680          * System.IO.MonoIO
2681          */
2682         "System.IO.MonoIO::GetLastError", ves_icall_System_IO_MonoIO_GetLastError,
2683         "System.IO.MonoIO::CreateDirectory", ves_icall_System_IO_MonoIO_CreateDirectory,
2684         "System.IO.MonoIO::RemoveDirectory", ves_icall_System_IO_MonoIO_RemoveDirectory,
2685         "System.IO.MonoIO::FindFirstFile", ves_icall_System_IO_MonoIO_FindFirstFile,
2686         "System.IO.MonoIO::FindNextFile", ves_icall_System_IO_MonoIO_FindNextFile,
2687         "System.IO.MonoIO::FindClose", ves_icall_System_IO_MonoIO_FindClose,
2688         "System.IO.MonoIO::GetCurrentDirectory", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
2689         "System.IO.MonoIO::SetCurrentDirectory", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
2690         "System.IO.MonoIO::MoveFile", ves_icall_System_IO_MonoIO_MoveFile,
2691         "System.IO.MonoIO::CopyFile", ves_icall_System_IO_MonoIO_CopyFile,
2692         "System.IO.MonoIO::DeleteFile", ves_icall_System_IO_MonoIO_DeleteFile,
2693         "System.IO.MonoIO::GetFileAttributes", ves_icall_System_IO_MonoIO_GetFileAttributes,
2694         "System.IO.MonoIO::SetFileAttributes", ves_icall_System_IO_MonoIO_SetFileAttributes,
2695         "System.IO.MonoIO::GetFileStat", ves_icall_System_IO_MonoIO_GetFileStat,
2696         "System.IO.MonoIO::Open", ves_icall_System_IO_MonoIO_Open,
2697         "System.IO.MonoIO::Close", ves_icall_System_IO_MonoIO_Close,
2698         "System.IO.MonoIO::Read", ves_icall_System_IO_MonoIO_Read,
2699         "System.IO.MonoIO::Write", ves_icall_System_IO_MonoIO_Write,
2700         "System.IO.MonoIO::Seek", ves_icall_System_IO_MonoIO_Seek,
2701         "System.IO.MonoIO::GetLength", ves_icall_System_IO_MonoIO_GetLength,
2702         "System.IO.MonoIO::SetLength", ves_icall_System_IO_MonoIO_SetLength,
2703         "System.IO.MonoIO::SetFileTime", ves_icall_System_IO_MonoIO_SetFileTime,
2704         "System.IO.MonoIO::Flush", ves_icall_System_IO_MonoIO_Flush,
2705         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
2706         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
2707         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
2708         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
2709         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
2710         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
2711         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
2712         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
2713
2714         /*
2715          * System.Math
2716          */
2717         "System.Math::Sin", ves_icall_System_Math_Sin,
2718     "System.Math::Cos", ves_icall_System_Math_Cos,
2719     "System.Math::Tan", ves_icall_System_Math_Tan,
2720     "System.Math::Sinh", ves_icall_System_Math_Sinh,
2721     "System.Math::Cosh", ves_icall_System_Math_Cosh,
2722     "System.Math::Tanh", ves_icall_System_Math_Tanh,
2723     "System.Math::Acos", ves_icall_System_Math_Acos,
2724     "System.Math::Asin", ves_icall_System_Math_Asin,
2725     "System.Math::Atan", ves_icall_System_Math_Atan,
2726     "System.Math::Atan2", ves_icall_System_Math_Atan2,
2727     "System.Math::Exp", ves_icall_System_Math_Exp,
2728     "System.Math::Log", ves_icall_System_Math_Log,
2729     "System.Math::Log10", ves_icall_System_Math_Log10,
2730     "System.Math::PowImpl", ves_icall_System_Math_Pow,
2731     "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
2732
2733         /*
2734          * System.Environment
2735          */
2736         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
2737         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
2738         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
2739         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
2740         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
2741         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
2742         "System.Environment::Exit", ves_icall_System_Environment_Exit,
2743
2744         /*
2745          * System.Runtime.Remoting
2746          */     
2747         "System.Runtime.Remoting.RemotingServices::InternalExecute",
2748         ves_icall_InternalExecute,
2749         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
2750         ves_icall_IsTransparentProxy,
2751
2752         /*
2753          * System.Runtime.Remoting.Messaging
2754          */     
2755         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
2756         ves_icall_MonoMethodMessage_InitMessage,
2757         
2758         /*
2759          * System.Runtime.Remoting.Proxies
2760          */     
2761         "System.Runtime.Remoting.Proxies.RealProxy::GetTransparentProxy", 
2762         ves_icall_Remoting_RealProxy_GetTransparentProxy,
2763
2764         /*
2765          * System.Threading.Interlocked
2766          */
2767         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
2768         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
2769         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
2770         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
2771         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
2772         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
2773         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
2774         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
2775         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
2776         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
2777
2778         /*
2779          * System.Diagnostics.Process
2780          */
2781         "System.Diagnostics.Process::GetCurrentProcess_internal()", ves_icall_System_Diagnostics_Process_GetCurrentProcess_internal,
2782         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
2783         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
2784         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
2785         "System.Diagnostics.Process::Start_internal(string,string,ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
2786         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
2787         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
2788         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
2789         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
2790         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
2791
2792         /*
2793          * add other internal calls here
2794          */
2795         NULL, NULL
2796 };
2797
2798 void
2799 mono_init_icall (void)
2800 {
2801         const char *name;
2802         int i = 0;
2803
2804         while ((name = icall_map [i])) {
2805                 mono_add_internal_call (name, icall_map [i+1]);
2806                 i += 2;
2807         }
2808        
2809 }
2810
2811