[interp] throw overflow exception on long to ulong cast where long is negative
[mono-project.git] / mono / mini / interp / transform.c
blob515c7da81ba93d9e956d4d6eedf6908107b3ab2d
1 /*
2 * transform CIL into different opcodes for more
3 * efficient interpretation
5 * Written by Bernie Solomon (bernard@ugsolutions.com)
6 * Copyright (c) 2004.
7 */
9 #include <string.h>
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/exception.h>
13 #include <mono/metadata/mono-endian.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/profiler-private.h>
16 #include <mono/metadata/tabledefs.h>
18 #include <mono/mini/mini.h>
20 #include "mintops.h"
21 #include "interp-internals.h"
22 #include "interp.h"
24 // TODO: export from marshal.c
25 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
27 #define DEBUG 0
29 typedef struct
31 MonoClass *klass;
32 unsigned char type;
33 unsigned char flags;
34 } StackInfo;
36 typedef struct
38 MonoMethod *method;
39 MonoMethodHeader *header;
40 RuntimeMethod *rtm;
41 const unsigned char *il_code;
42 const unsigned char *ip;
43 const unsigned char *last_ip;
44 const unsigned char *in_start;
45 int code_size;
46 int *in_offsets;
47 int *forward_refs;
48 StackInfo **stack_state;
49 int *stack_height;
50 int *vt_stack_size;
51 unsigned char *is_bb_start;
52 unsigned short *new_code;
53 unsigned short *new_code_end;
54 unsigned short *new_ip;
55 unsigned short *last_new_ip;
56 unsigned int max_code_size;
57 StackInfo *stack;
58 StackInfo *sp;
59 unsigned int max_stack_height;
60 unsigned int vt_sp;
61 unsigned int max_vt_sp;
62 int n_data_items;
63 int max_data_items;
64 void **data_items;
65 GHashTable *data_hash;
66 } TransformData;
68 #define MINT_TYPE_I1 0
69 #define MINT_TYPE_U1 1
70 #define MINT_TYPE_I2 2
71 #define MINT_TYPE_U2 3
72 #define MINT_TYPE_I4 4
73 #define MINT_TYPE_I8 5
74 #define MINT_TYPE_R4 6
75 #define MINT_TYPE_R8 7
76 #define MINT_TYPE_O 8
77 #define MINT_TYPE_P 9
78 #define MINT_TYPE_VT 10
80 #define STACK_TYPE_I4 0
81 #define STACK_TYPE_I8 1
82 #define STACK_TYPE_R8 2
83 #define STACK_TYPE_O 3
84 #define STACK_TYPE_VT 4
85 #define STACK_TYPE_MP 5
86 #define STACK_TYPE_F 6
88 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
90 #if SIZEOF_VOID_P == 8
91 #define STACK_TYPE_I STACK_TYPE_I8
92 #else
93 #define STACK_TYPE_I STACK_TYPE_I4
94 #endif
96 static int stack_type [] = {
97 STACK_TYPE_I4, /*I1*/
98 STACK_TYPE_I4, /*U1*/
99 STACK_TYPE_I4, /*I2*/
100 STACK_TYPE_I4, /*U2*/
101 STACK_TYPE_I4, /*I4*/
102 STACK_TYPE_I8, /*I8*/
103 STACK_TYPE_R8, /*R4*/
104 STACK_TYPE_R8, /*R8*/
105 STACK_TYPE_O, /*O*/
106 STACK_TYPE_MP, /*P*/
107 STACK_TYPE_VT
110 static void
111 grow_code (TransformData *td)
113 unsigned int old_ip_offset = td->new_ip - td->new_code;
114 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
115 g_assert (old_ip_offset <= td->max_code_size);
116 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
117 td->new_code_end = td->new_code + td->max_code_size;
118 td->new_ip = td->new_code + old_ip_offset;
119 td->last_new_ip = td->new_code + old_last_ip_offset;
122 #define ENSURE_CODE(td, n) \
123 do { \
124 if ((td)->new_ip + (n) > (td)->new_code_end) \
125 grow_code (td); \
126 } while (0)
128 #define ADD_CODE(td, n) \
129 do { \
130 if ((td)->new_ip == (td)->new_code_end) \
131 grow_code (td); \
132 *(td)->new_ip++ = (n); \
133 } while (0)
135 #define CHECK_STACK(td, n) \
136 do { \
137 int stack_size = (td)->sp - (td)->stack; \
138 if (stack_size < (n)) \
139 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
140 (td)->method->klass->name, (td)->method->name, \
141 stack_size, n, (td)->ip - (td)->il_code); \
142 } while (0)
144 #define ENSURE_I4(td, sp_off) \
145 do { \
146 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
147 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
148 } while (0)
150 static void
151 handle_branch(TransformData *td, int short_op, int long_op, int offset)
153 int shorten_branch = 0;
154 int target = td->ip + offset - td->il_code;
155 if (target < 0 || target >= td->code_size)
156 g_assert_not_reached ();
157 if (offset > 0 && td->stack_height [target] < 0) {
158 td->stack_height [target] = td->sp - td->stack;
159 if (td->stack_height [target] > 0)
160 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
161 td->vt_stack_size [target] = td->vt_sp;
163 if (offset < 0) {
164 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
165 if (offset >= -32768) {
166 shorten_branch = 1;
168 } else {
169 int prev = td->forward_refs [target];
170 td->forward_refs [td->ip - td->il_code] = prev;
171 td->forward_refs [target] = td->ip - td->il_code;
172 offset = 0;
173 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
174 shorten_branch = 1;
176 if (shorten_branch) {
177 ADD_CODE(td, short_op);
178 ADD_CODE(td, offset);
179 } else {
180 ADD_CODE(td, long_op);
181 ADD_CODE(td, * (unsigned short *)(&offset));
182 ADD_CODE(td, * ((unsigned short *)&offset + 1));
186 static void
187 one_arg_branch(TransformData *td, int mint_op, int offset)
189 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
190 int long_op = mint_op + type - STACK_TYPE_I4;
191 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
192 CHECK_STACK(td, 1);
193 --td->sp;
194 handle_branch (td, short_op, long_op, offset);
197 static void
198 two_arg_branch(TransformData *td, int mint_op, int offset)
200 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
201 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
202 int long_op = mint_op + type1 - STACK_TYPE_I4;
203 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
204 CHECK_STACK(td, 2);
205 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
206 ADD_CODE(td, MINT_CONV_I8_I4);
207 td->in_offsets [td->ip - td->il_code]++;
208 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
209 ADD_CODE(td, MINT_CONV_I8_I4_SP);
210 td->in_offsets [td->ip - td->il_code]++;
211 } else if (type1 != type2) {
212 g_warning("%s.%s: branch type mismatch %d %d",
213 td->method->klass->name, td->method->name,
214 td->sp [-1].type, td->sp [-2].type);
216 td->sp -= 2;
217 handle_branch (td, short_op, long_op, offset);
220 static void
221 unary_arith_op(TransformData *td, int mint_op)
223 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
224 CHECK_STACK(td, 1);
225 ADD_CODE(td, op);
228 static void
229 binary_arith_op(TransformData *td, int mint_op)
231 int type1 = td->sp [-2].type;
232 int type2 = td->sp [-1].type;
233 int op;
234 #if SIZEOF_VOID_P == 8
235 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
236 ADD_CODE(td, MINT_CONV_I8_I4);
237 type2 = STACK_TYPE_I8;
239 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
240 ADD_CODE(td, MINT_CONV_I8_I4_SP);
241 type1 = STACK_TYPE_I8;
242 td->sp [-2].type = STACK_TYPE_I8;
244 #endif
245 if (type1 == STACK_TYPE_MP)
246 type1 = STACK_TYPE_I;
247 if (type2 == STACK_TYPE_MP)
248 type2 = STACK_TYPE_I;
249 if (type1 != type2) {
250 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
251 td->method->klass->name, td->method->name,
252 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
254 op = mint_op + type1 - STACK_TYPE_I4;
255 CHECK_STACK(td, 2);
256 ADD_CODE(td, op);
257 --td->sp;
260 static void
261 binary_int_op(TransformData *td, int mint_op)
263 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
264 CHECK_STACK(td, 2);
265 if (td->sp [-1].type != td->sp [-2].type)
266 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
267 ADD_CODE(td, op);
268 --td->sp;
271 static void
272 shift_op(TransformData *td, int mint_op)
274 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
275 CHECK_STACK(td, 2);
276 if (td->sp [-1].type != STACK_TYPE_I4) {
277 g_warning("%s.%s: shift type mismatch %d",
278 td->method->klass->name, td->method->name,
279 td->sp [-2].type);
281 ADD_CODE(td, op);
282 --td->sp;
285 static int
286 mint_type(MonoType *type)
288 if (type->byref)
289 return MINT_TYPE_P;
290 enum_type:
291 switch (type->type) {
292 case MONO_TYPE_I1:
293 return MINT_TYPE_I1;
294 case MONO_TYPE_U1:
295 case MONO_TYPE_BOOLEAN:
296 return MINT_TYPE_U1;
297 case MONO_TYPE_I2:
298 return MINT_TYPE_I2;
299 case MONO_TYPE_U2:
300 case MONO_TYPE_CHAR:
301 return MINT_TYPE_U2;
302 case MONO_TYPE_I4:
303 case MONO_TYPE_U4:
304 return MINT_TYPE_I4;
305 case MONO_TYPE_I:
306 case MONO_TYPE_U:
307 #if SIZEOF_VOID_P == 4
308 return MINT_TYPE_I4;
309 #else
310 return MINT_TYPE_I8;
311 #endif
312 case MONO_TYPE_PTR:
313 return MINT_TYPE_P;
314 case MONO_TYPE_R4:
315 return MINT_TYPE_R4;
316 case MONO_TYPE_I8:
317 case MONO_TYPE_U8:
318 return MINT_TYPE_I8;
319 case MONO_TYPE_R8:
320 return MINT_TYPE_R8;
321 case MONO_TYPE_STRING:
322 case MONO_TYPE_SZARRAY:
323 case MONO_TYPE_CLASS:
324 case MONO_TYPE_OBJECT:
325 case MONO_TYPE_ARRAY:
326 return MINT_TYPE_O;
327 case MONO_TYPE_VALUETYPE:
328 if (type->data.klass->enumtype) {
329 type = mono_class_enum_basetype (type->data.klass);
330 goto enum_type;
331 } else
332 return MINT_TYPE_VT;
333 case MONO_TYPE_GENERICINST:
334 type = &type->data.generic_class->container_class->byval_arg;
335 goto enum_type;
336 default:
337 g_warning ("got type 0x%02x", type->type);
338 g_assert_not_reached ();
340 return -1;
343 static int
344 can_store (int stack_type, int var_type)
346 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
347 stack_type = STACK_TYPE_I;
348 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
349 var_type = STACK_TYPE_I;
350 return stack_type == var_type;
353 #define SET_SIMPLE_TYPE(s, ty) \
354 do { \
355 (s)->type = (ty); \
356 (s)->flags = 0; \
357 (s)->klass = NULL; \
358 } while (0)
360 #define SET_TYPE(s, ty, k) \
361 do { \
362 (s)->type = (ty); \
363 (s)->flags = 0; \
364 (s)->klass = k; \
365 } while (0)
367 #define PUSH_SIMPLE_TYPE(td, ty) \
368 do { \
369 int sp_height; \
370 (td)->sp++; \
371 sp_height = (td)->sp - (td)->stack; \
372 if (sp_height > (td)->max_stack_height) \
373 (td)->max_stack_height = sp_height; \
374 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
375 } while (0)
377 #define PUSH_TYPE(td, ty, k) \
378 do { \
379 int sp_height; \
380 (td)->sp++; \
381 sp_height = (td)->sp - (td)->stack; \
382 if (sp_height > (td)->max_stack_height) \
383 (td)->max_stack_height = sp_height; \
384 SET_TYPE((td)->sp - 1, ty, k); \
385 } while (0)
387 #define PUSH_VT(td, size) \
388 do { \
389 (td)->vt_sp += ((size) + 7) & ~7; \
390 if ((td)->vt_sp > (td)->max_vt_sp) \
391 (td)->max_vt_sp = (td)->vt_sp; \
392 } while (0)
394 #define POP_VT(td, size) \
395 do { \
396 (td)->vt_sp -= ((size) + 7) & ~7; \
397 } while (0)
399 #if NO_UNALIGNED_ACCESS
400 #define WRITE32(td, v) \
401 do { \
402 ENSURE_CODE(td, 2); \
403 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
404 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
405 (td)->new_ip += 2; \
406 } while (0)
408 #define WRITE64(td, v) \
409 do { \
410 ENSURE_CODE(td, 4); \
411 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
412 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
413 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
414 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
415 (td)->new_ip += 4; \
416 } while (0)
417 #else
418 #define WRITE32(td, v) \
419 do { \
420 ENSURE_CODE(td, 2); \
421 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
422 (td)->new_ip += 2; \
423 } while (0)
425 #define WRITE64(td, v) \
426 do { \
427 ENSURE_CODE(td, 4); \
428 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
429 (td)->new_ip += 4; \
430 } while (0)
432 #endif
434 static void
435 load_arg(TransformData *td, int n)
437 int mt;
438 MonoClass *klass = NULL;
439 MonoType *type;
441 gboolean hasthis = mono_method_signature (td->method)->hasthis;
442 if (hasthis && n == 0)
443 type = &td->method->klass->byval_arg;
444 else
445 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
447 mt = mint_type (type);
448 if (mt == MINT_TYPE_VT) {
449 gint32 size;
450 klass = mono_class_from_mono_type (type);
451 if (mono_method_signature (td->method)->pinvoke)
452 size = mono_class_native_size (klass, NULL);
453 else
454 size = mono_class_value_size (klass, NULL);
456 if (hasthis && n == 0) {
457 mt = MINT_TYPE_P;
458 ADD_CODE (td, MINT_LDARG_P);
459 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
460 klass = NULL;
461 } else {
462 PUSH_VT (td, size);
463 ADD_CODE (td, MINT_LDARG_VT);
464 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
465 WRITE32 (td, &size);
467 } else {
468 if (hasthis && n == 0) {
469 mt = MINT_TYPE_P;
470 ADD_CODE (td, MINT_LDARG_P);
471 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
472 klass = NULL;
473 } else {
474 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
475 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
476 if (mt == MINT_TYPE_O)
477 klass = mono_class_from_mono_type (type);
480 PUSH_TYPE(td, stack_type[mt], klass);
483 static void
484 store_arg(TransformData *td, int n)
486 int mt;
487 CHECK_STACK (td, 1);
488 MonoType *type;
490 gboolean hasthis = mono_method_signature (td->method)->hasthis;
491 if (hasthis && n == 0)
492 type = &td->method->klass->byval_arg;
493 else
494 type = mono_method_signature (td->method)->params [n - !!hasthis];
496 mt = mint_type (type);
497 if (mt == MINT_TYPE_VT) {
498 gint32 size;
499 g_error ("data.klass");
500 if (mono_method_signature (td->method)->pinvoke)
501 size = mono_class_native_size (type->data.klass, NULL);
502 else
503 size = mono_class_value_size (type->data.klass, NULL);
504 ADD_CODE(td, MINT_STARG_VT);
505 ADD_CODE(td, n);
506 WRITE32(td, &size);
507 if (td->sp [-1].type == STACK_TYPE_VT)
508 POP_VT(td, size);
509 } else {
510 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
511 ADD_CODE(td, td->rtm->arg_offsets [n]);
513 --td->sp;
516 static void
517 store_inarg(TransformData *td, int n)
519 MonoType *type;
520 gboolean hasthis = mono_method_signature (td->method)->hasthis;
521 if (hasthis && n == 0)
522 type = &td->method->klass->byval_arg;
523 else
524 type = mono_method_signature (td->method)->params [n - !!hasthis];
526 int mt = mint_type (type);
527 if (hasthis && n == 0) {
528 ADD_CODE (td, MINT_STINARG_P);
529 ADD_CODE (td, n);
530 return;
532 if (mt == MINT_TYPE_VT) {
533 MonoClass *klass = mono_class_from_mono_type (type);
534 gint32 size;
535 if (mono_method_signature (td->method)->pinvoke)
536 size = mono_class_native_size (klass, NULL);
537 else
538 size = mono_class_value_size (klass, NULL);
539 ADD_CODE(td, MINT_STINARG_VT);
540 ADD_CODE(td, n);
541 WRITE32(td, &size);
542 } else {
543 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
544 ADD_CODE(td, n);
548 static void
549 load_local(TransformData *td, int n)
551 MonoType *type = td->header->locals [n];
552 int mt = mint_type (type);
553 int offset = td->rtm->local_offsets [n];
554 MonoClass *klass = NULL;
555 if (mt == MINT_TYPE_VT) {
556 klass = mono_class_from_mono_type (type);
557 gint32 size = mono_class_value_size (klass, NULL);
558 PUSH_VT(td, size);
559 ADD_CODE(td, MINT_LDLOC_VT);
560 ADD_CODE(td, offset); /*FIX for large offset */
561 WRITE32(td, &size);
562 } else {
563 g_assert (mt < MINT_TYPE_VT);
564 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
565 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
566 td->last_new_ip [0] = MINT_STLOC_NP_I4;
567 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
568 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
569 td->last_new_ip [0] = MINT_STLOC_NP_O;
570 } else {
571 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
572 ADD_CODE(td, offset); /*FIX for large offset */
574 if (mt == MINT_TYPE_O)
575 klass = mono_class_from_mono_type (type);
577 PUSH_TYPE(td, stack_type[mt], klass);
580 static void
581 store_local(TransformData *td, int n)
583 MonoType *type = td->header->locals [n];
584 int mt = mint_type (type);
585 int offset = td->rtm->local_offsets [n];
586 CHECK_STACK (td, 1);
587 #if SIZEOF_VOID_P == 8
588 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
589 ADD_CODE(td, MINT_CONV_I8_I4);
590 td->sp [-1].type = STACK_TYPE_I8;
592 #endif
593 if (!can_store(td->sp [-1].type, stack_type [mt])) {
594 g_warning("%s.%s: Store local stack type mismatch %d %d",
595 td->method->klass->name, td->method->name,
596 stack_type [mt], td->sp [-1].type);
598 if (mt == MINT_TYPE_VT) {
599 MonoClass *klass = mono_class_from_mono_type (type);
600 gint32 size = mono_class_value_size (klass, NULL);
601 ADD_CODE(td, MINT_STLOC_VT);
602 ADD_CODE(td, offset); /*FIX for large offset */
603 WRITE32(td, &size);
604 if (td->sp [-1].type == STACK_TYPE_VT)
605 POP_VT(td, size);
606 } else {
607 g_assert (mt < MINT_TYPE_VT);
608 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
609 ADD_CODE(td, offset); /*FIX for large offset */
611 --td->sp;
614 #define SIMPLE_OP(td, op) \
615 do { \
616 ADD_CODE(&td, op); \
617 ++td.ip; \
618 } while (0)
620 static guint16
621 get_data_item_index (TransformData *td, void *ptr)
623 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
624 guint index;
625 if (p != NULL)
626 return GPOINTER_TO_UINT (p) - 1;
627 if (td->max_data_items == td->n_data_items) {
628 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
629 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
631 index = td->n_data_items;
632 td->data_items [index] = ptr;
633 ++td->n_data_items;
634 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
635 return index;
638 static void
639 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
641 MonoImage *image = method->klass->image;
642 MonoMethodSignature *csignature;
643 MonoError error;
644 int virtual = *td->ip == CEE_CALLVIRT;
645 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
646 int i;
647 guint32 vt_stack_used = 0;
648 guint32 vt_res_size = 0;
649 int op = -1;
650 int native = 0;
651 int is_void = 0;
653 guint32 token = read32 (td->ip + 1);
655 if (target_method == NULL) {
656 if (calli) {
657 CHECK_STACK(td, 1);
658 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
659 --td->sp;
660 if (method->wrapper_type != MONO_WRAPPER_NONE)
661 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
662 else
663 csignature = mono_metadata_parse_signature (image, token);
665 if (generic_context) {
666 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
667 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
670 target_method = NULL;
671 } else {
672 if (method->wrapper_type == MONO_WRAPPER_NONE)
673 target_method = mono_get_method_full (image, token, NULL, generic_context);
674 else
675 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
676 csignature = mono_method_signature (target_method);
677 if (target_method->klass == mono_defaults.string_class) {
678 if (target_method->name [0] == 'g') {
679 if (strcmp (target_method->name, "get_Chars") == 0)
680 op = MINT_GETCHR;
681 else if (strcmp (target_method->name, "get_Length") == 0)
682 op = MINT_STRLEN;
684 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
685 if (!strcmp (target_method->name, "get_Rank")) {
686 op = MINT_ARRAY_RANK;
687 } else if (!strcmp (target_method->name, "get_Length")) {
688 op = MINT_LDLEN;
689 } else if (!strcmp (target_method->name, "Address")) {
690 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
692 } else if (target_method && generic_context) {
693 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
694 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
695 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
696 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
699 } else {
700 csignature = mono_method_signature (target_method);
703 if (constrained_class) {
704 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
705 /* Use the corresponding method from the base type to avoid boxing */
706 MonoType *base_type = mono_class_enum_basetype (constrained_class);
707 g_assert (base_type);
708 constrained_class = mono_class_from_mono_type (base_type);
709 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
710 g_assert (target_method);
714 if (constrained_class) {
715 mono_class_setup_vtable (constrained_class);
716 #if DEBUG_INTERP
717 g_print ("CONSTRAINED.CALLVIRT: %s::%s. %s (%p) ->\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
718 #endif
719 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
720 #if DEBUG_INTERP
721 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
722 #endif
723 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
724 mono_class_setup_vtable (target_method->klass);
726 if (constrained_class->valuetype && (target_method->klass == mono_defaults.object_class || target_method->klass == mono_defaults.enum_class->parent || target_method->klass == mono_defaults.enum_class)) {
727 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
728 /* managed pointer on the stack, we need to deref that puppy */
729 ADD_CODE (td, MINT_LDIND_I);
730 ADD_CODE (td, csignature->param_count);
732 ADD_CODE (td, MINT_BOX);
733 ADD_CODE (td, get_data_item_index (td, constrained_class));
734 ADD_CODE (td, csignature->param_count);
735 } else if (!constrained_class->valuetype) {
736 /* managed pointer on the stack, we need to deref that puppy */
737 ADD_CODE (td, MINT_LDIND_I);
738 ADD_CODE (td, csignature->param_count);
739 } else {
740 if (target_method->klass->valuetype) {
741 /* Own method */
742 } else {
743 /* Interface method */
744 int ioffset, slot;
746 mono_class_setup_vtable (constrained_class);
747 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
748 if (ioffset == -1)
749 g_error ("type load error: constrained_class");
750 slot = mono_method_get_vtable_slot (target_method);
751 if (slot == -1)
752 g_error ("type load error: target_method->klass");
753 target_method = constrained_class->vtable [ioffset + slot];
755 if (target_method->klass == mono_defaults.enum_class) {
756 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
757 /* managed pointer on the stack, we need to deref that puppy */
758 ADD_CODE (td, MINT_LDIND_I);
759 ADD_CODE (td, csignature->param_count);
761 ADD_CODE (td, MINT_BOX);
762 ADD_CODE (td, get_data_item_index (td, constrained_class));
763 ADD_CODE (td, csignature->param_count);
766 virtual = FALSE;
770 if (target_method)
771 mono_class_init (target_method->klass);
773 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
774 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
775 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
776 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
777 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
778 MonoMethodHeader *mheader = mono_method_get_header (target_method);
780 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
781 int offset;
782 if (mono_interp_traceopt)
783 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
784 for (i = csignature->param_count - 1; i >= 0; --i)
785 store_arg (td, i + csignature->hasthis);
787 ADD_CODE(td, MINT_BR_S);
788 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
789 ADD_CODE(td, offset);
790 if (!is_bb_start [td->ip + 5 - td->il_code])
791 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
792 td->ip += 5;
793 return;
794 } else {
795 /* mheader might not exist if this is a delegate invoc, etc */
796 if (mheader && *mheader->code == CEE_RET && called_inited) {
797 if (mono_interp_traceopt)
798 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
799 for (i = 0; i < csignature->param_count; i++)
800 ADD_CODE (td, MINT_POP); /*FIX: vt */
801 ADD_CODE (td, 0);
802 if (csignature->hasthis) {
803 if (virtual)
804 ADD_CODE(td, MINT_CKNULL);
805 ADD_CODE (td, MINT_POP);
806 ADD_CODE (td, 0);
808 td->sp -= csignature->param_count + csignature->hasthis;
809 td->ip += 5;
810 return;
814 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
815 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
816 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
817 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
818 target_method = mono_marshal_get_synchronized_wrapper (target_method);
820 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
821 td->sp -= csignature->param_count + !!csignature->hasthis;
822 for (i = 0; i < csignature->param_count; ++i) {
823 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
824 gint32 size;
825 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
826 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
827 size = mono_class_native_size (klass, NULL);
828 else
829 size = mono_class_value_size (klass, NULL);
830 size = (size + 7) & ~7;
831 vt_stack_used += size;
835 /* need to handle typedbyref ... */
836 if (csignature->ret->type != MONO_TYPE_VOID) {
837 int mt = mint_type(csignature->ret);
838 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
839 if (mt == MINT_TYPE_VT) {
840 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
841 vt_res_size = mono_class_native_size (klass, NULL);
842 else
843 vt_res_size = mono_class_value_size (klass, NULL);
844 PUSH_VT(td, vt_res_size);
846 PUSH_TYPE(td, stack_type[mt], klass);
847 } else
848 is_void = TRUE;
850 if (op >= 0) {
851 ADD_CODE (td, op);
852 #if SIZEOF_VOID_P == 8
853 if (op == MINT_LDLEN)
854 ADD_CODE (td, MINT_CONV_I4_I8);
855 #endif
856 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
857 ADD_CODE (td, get_data_item_index (td, target_method->klass));
858 ADD_CODE (td, 1 + target_method->klass->rank);
860 } else {
861 if (calli)
862 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
863 else if (virtual)
864 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
865 else
866 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
868 if (calli) {
869 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
870 } else {
871 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
872 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
875 td->ip += 5;
876 if (vt_stack_used != 0 || vt_res_size != 0) {
877 ADD_CODE(td, MINT_VTRESULT);
878 ADD_CODE(td, vt_res_size);
879 WRITE32(td, &vt_stack_used);
880 td->vt_sp -= vt_stack_used;
884 static void
885 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
887 MonoMethodHeader *header = mono_method_get_header (method);
888 MonoMethodSignature *signature = mono_method_signature (method);
889 MonoImage *image = method->klass->image;
890 MonoDomain *domain = mono_domain_get ();
891 MonoClass *constrained_class = NULL;
892 MonoError error;
893 int offset, mt, i, i32;
894 gboolean readonly = FALSE;
895 MonoClass *klass;
896 MonoClassField *field;
897 const unsigned char *end;
898 int new_in_start_offset;
899 int body_start_offset;
900 int target;
901 guint32 token;
902 TransformData td;
903 int generating_code = 1;
905 memset(&td, 0, sizeof(td));
906 td.method = method;
907 td.rtm = rtm;
908 td.is_bb_start = is_bb_start;
909 td.il_code = header->code;
910 td.code_size = header->code_size;
911 td.header = header;
912 td.max_code_size = td.code_size;
913 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
914 td.new_code_end = td.new_code + td.max_code_size;
915 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
916 td.forward_refs = g_malloc(header->code_size * sizeof(int));
917 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
918 td.stack_height = g_malloc(header->code_size * sizeof(int));
919 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
920 td.n_data_items = 0;
921 td.max_data_items = 0;
922 td.data_items = NULL;
923 td.data_hash = g_hash_table_new (NULL, NULL);
924 rtm->data_items = td.data_items;
925 for (i = 0; i < header->code_size; i++) {
926 td.forward_refs [i] = -1;
927 td.stack_height [i] = -1;
929 td.new_ip = td.new_code;
930 td.last_new_ip = NULL;
932 td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
933 td.sp = td.stack;
934 td.max_stack_height = 0;
936 for (i = 0; i < header->num_clauses; i++) {
937 MonoExceptionClause *c = header->clauses + i;
938 td.stack_height [c->handler_offset] = 0;
939 td.vt_stack_size [c->handler_offset] = 0;
940 td.is_bb_start [c->handler_offset] = 1;
942 td.stack_height [c->handler_offset] = 1;
943 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
944 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
945 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
948 td.ip = header->code;
949 end = td.ip + header->code_size;
951 if (mono_interp_traceopt) {
952 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
953 char *name = mono_method_full_name (method, TRUE);
954 g_print ("Method %s, original code:\n", name);
955 g_print ("%s\n", tmp);
956 g_free (tmp);
957 g_free (name);
960 if (signature->hasthis)
961 store_inarg (&td, 0);
962 for (i = 0; i < signature->param_count; i++)
963 store_inarg (&td, i + !!signature->hasthis);
965 body_start_offset = td.new_ip - td.new_code;
967 for (i = 0; i < header->num_locals; i++) {
968 int mt = mint_type(header->locals [i]);
969 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
970 ADD_CODE(&td, MINT_INITLOCALS);
971 break;
975 while (td.ip < end) {
976 int in_offset;
978 g_assert (td.sp >= td.stack);
979 g_assert (td.vt_sp < 0x10000000);
980 in_offset = td.ip - header->code;
981 td.in_offsets [in_offset] = td.new_ip - td.new_code;
982 new_in_start_offset = td.new_ip - td.new_code;
983 td.in_start = td.ip;
984 while (td.forward_refs [in_offset] >= 0) {
985 int j = td.forward_refs [in_offset];
986 int slot;
987 td.forward_refs [in_offset] = td.forward_refs [j];
988 if (td.in_offsets [j] < 0) {
989 int old_switch_offset = -td.in_offsets [j];
990 int new_switch_offset = td.in_offsets [old_switch_offset];
991 int switch_case = (j - old_switch_offset - 5) / 4;
992 int n_cases = read32 (header->code + old_switch_offset + 1);
993 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
994 slot = new_switch_offset + 3 + 2 * switch_case;
995 td.new_code [slot] = * (unsigned short *)(&offset);
996 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
997 } else {
998 int op = td.new_code [td.in_offsets [j]];
999 if (mono_interp_opargtype [op] == MintOpShortBranch) {
1000 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1001 g_assert (offset <= 32767);
1002 slot = td.in_offsets [j] + 1;
1003 td.new_code [slot] = offset;
1004 } else {
1005 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1006 slot = td.in_offsets [j] + 1;
1007 td.new_code [slot] = * (unsigned short *)(&offset);
1008 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1012 if (td.stack_height [in_offset] >= 0) {
1013 g_assert (is_bb_start [in_offset]);
1014 if (td.stack_height [in_offset] > 0)
1015 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1016 td.sp = td.stack + td.stack_height [in_offset];
1017 td.vt_sp = td.vt_stack_size [in_offset];
1019 if (is_bb_start [in_offset]) {
1020 generating_code = 1;
1022 if (!generating_code) {
1023 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1024 ++td.ip;
1025 continue;
1027 if (mono_interp_traceopt > 1) {
1028 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1029 td.ip - td.il_code,
1030 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1031 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1032 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1033 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1034 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1035 (td.sp > td.stack && (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_VT)) ? (td.sp [-1].klass == NULL ? "?" : td.sp [-1].klass->name) : "",
1036 td.vt_sp, td.max_vt_sp);
1038 switch (*td.ip) {
1039 case CEE_NOP:
1040 /* lose it */
1041 ++td.ip;
1042 break;
1043 case CEE_BREAK:
1044 SIMPLE_OP(td, MINT_BREAK);
1045 break;
1046 case CEE_LDARG_0:
1047 case CEE_LDARG_1:
1048 case CEE_LDARG_2:
1049 case CEE_LDARG_3:
1050 load_arg (&td, *td.ip - CEE_LDARG_0);
1051 ++td.ip;
1052 break;
1053 case CEE_LDLOC_0:
1054 case CEE_LDLOC_1:
1055 case CEE_LDLOC_2:
1056 case CEE_LDLOC_3:
1057 load_local (&td, *td.ip - CEE_LDLOC_0);
1058 ++td.ip;
1059 break;
1060 case CEE_STLOC_0:
1061 case CEE_STLOC_1:
1062 case CEE_STLOC_2:
1063 case CEE_STLOC_3:
1064 store_local (&td, *td.ip - CEE_STLOC_0);
1065 ++td.ip;
1066 break;
1067 case CEE_LDARG_S:
1068 load_arg (&td, ((guint8 *)td.ip)[1]);
1069 td.ip += 2;
1070 break;
1071 case CEE_LDARGA_S: {
1072 /* NOTE: n includes this */
1073 int n = ((guint8 *)td.ip)[1];
1074 if (n == 0 && signature->hasthis) {
1075 g_error ("LDTHISA: NOPE");
1076 ADD_CODE(&td, MINT_LDTHISA);
1078 else {
1079 ADD_CODE(&td, MINT_LDARGA);
1080 ADD_CODE(&td, td.rtm->arg_offsets [n]);
1082 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1083 td.ip += 2;
1084 break;
1086 case CEE_STARG_S:
1087 store_arg (&td, ((guint8 *)td.ip)[1]);
1088 td.ip += 2;
1089 break;
1090 case CEE_LDLOC_S:
1091 load_local (&td, ((guint8 *)td.ip)[1]);
1092 td.ip += 2;
1093 break;
1094 case CEE_LDLOCA_S:
1095 ADD_CODE(&td, MINT_LDLOCA_S);
1096 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1097 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1098 td.ip += 2;
1099 break;
1100 case CEE_STLOC_S:
1101 store_local (&td, ((guint8 *)td.ip)[1]);
1102 td.ip += 2;
1103 break;
1104 case CEE_LDNULL:
1105 SIMPLE_OP(td, MINT_LDNULL);
1106 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1107 break;
1108 case CEE_LDC_I4_M1:
1109 SIMPLE_OP(td, MINT_LDC_I4_M1);
1110 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1111 break;
1112 case CEE_LDC_I4_0:
1113 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1114 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1115 SIMPLE_OP(td, MINT_CEQ0_I4);
1116 td.ip += 2;
1117 } else {
1118 SIMPLE_OP(td, MINT_LDC_I4_0);
1119 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1121 break;
1122 case CEE_LDC_I4_1:
1123 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1124 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1125 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1126 td.ip += 2;
1127 } else {
1128 SIMPLE_OP(td, MINT_LDC_I4_1);
1129 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1131 break;
1132 case CEE_LDC_I4_2:
1133 case CEE_LDC_I4_3:
1134 case CEE_LDC_I4_4:
1135 case CEE_LDC_I4_5:
1136 case CEE_LDC_I4_6:
1137 case CEE_LDC_I4_7:
1138 case CEE_LDC_I4_8:
1139 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1140 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1141 break;
1142 case CEE_LDC_I4_S:
1143 ADD_CODE(&td, MINT_LDC_I4_S);
1144 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1145 td.ip += 2;
1146 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1147 break;
1148 case CEE_LDC_I4:
1149 i32 = read32 (td.ip + 1);
1150 ADD_CODE(&td, MINT_LDC_I4);
1151 WRITE32(&td, &i32);
1152 td.ip += 5;
1153 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1154 break;
1155 case CEE_LDC_I8: {
1156 gint64 val = read64 (td.ip + 1);
1157 ADD_CODE(&td, MINT_LDC_I8);
1158 WRITE64(&td, &val);
1159 td.ip += 9;
1160 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1161 break;
1163 case CEE_LDC_R4: {
1164 float val;
1165 readr4 (td.ip + 1, &val);
1166 ADD_CODE(&td, MINT_LDC_R4);
1167 WRITE32(&td, &val);
1168 td.ip += 5;
1169 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1170 break;
1172 case CEE_LDC_R8: {
1173 double val;
1174 readr8 (td.ip + 1, &val);
1175 ADD_CODE(&td, MINT_LDC_R8);
1176 WRITE64(&td, &val);
1177 td.ip += 9;
1178 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1179 break;
1181 case CEE_DUP: {
1182 int type = td.sp [-1].type;
1183 MonoClass *klass = td.sp [-1].klass;
1184 if (td.sp [-1].type == STACK_TYPE_VT) {
1185 gint32 size = mono_class_value_size (klass, NULL);
1186 PUSH_VT(&td, size);
1187 ADD_CODE(&td, MINT_DUP_VT);
1188 WRITE32(&td, &size);
1189 td.ip ++;
1190 } else
1191 SIMPLE_OP(td, MINT_DUP);
1192 PUSH_TYPE(&td, type, klass);
1193 break;
1195 case CEE_POP:
1196 CHECK_STACK(&td, 1);
1197 SIMPLE_OP(td, MINT_POP);
1198 ADD_CODE (&td, 0);
1199 if (td.sp [-1].type == STACK_TYPE_VT) {
1200 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1201 size = (size + 7) & ~7;
1202 ADD_CODE(&td, MINT_VTRESULT);
1203 ADD_CODE(&td, 0);
1204 WRITE32(&td, &size);
1205 td.vt_sp -= size;
1207 --td.sp;
1208 break;
1209 case CEE_JMP: {
1210 MonoMethod *m;
1211 if (td.sp > td.stack)
1212 g_warning ("CEE_JMP: stack must be empty");
1213 token = read32 (td.ip + 1);
1214 m = mono_get_method_full (image, token, NULL, generic_context);
1215 ADD_CODE (&td, MINT_JMP);
1216 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1217 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1218 td.ip += 5;
1219 break;
1221 case CEE_CALLVIRT: /* Fall through */
1222 case CEE_CALLI: /* Fall through */
1223 case CEE_CALL: {
1224 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1225 constrained_class = NULL;
1226 readonly = FALSE;
1227 break;
1229 case CEE_RET: {
1230 int vt_size = 0;
1231 if (signature->ret->type != MONO_TYPE_VOID) {
1232 --td.sp;
1233 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1234 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1235 vt_size = mono_class_value_size (klass, NULL);
1236 vt_size = (vt_size + 7) & ~7;
1239 if (td.sp > td.stack)
1240 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1241 if (td.vt_sp != vt_size)
1242 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1243 if (vt_size == 0)
1244 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1245 else {
1246 ADD_CODE(&td, MINT_RET_VT);
1247 WRITE32(&td, &vt_size);
1248 ++td.ip;
1250 generating_code = 0;
1251 break;
1253 case CEE_BR:
1254 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1255 td.ip += 5;
1256 generating_code = 0;
1257 break;
1258 case CEE_BR_S:
1259 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1260 td.ip += 2;
1261 generating_code = 0;
1262 break;
1263 case CEE_BRFALSE:
1264 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1265 td.ip += 5;
1266 break;
1267 case CEE_BRFALSE_S:
1268 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1269 td.ip += 2;
1270 break;
1271 case CEE_BRTRUE:
1272 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1273 td.ip += 5;
1274 break;
1275 case CEE_BRTRUE_S:
1276 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1277 td.ip += 2;
1278 break;
1279 case CEE_BEQ:
1280 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1281 td.ip += 5;
1282 break;
1283 case CEE_BEQ_S:
1284 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1285 td.ip += 2;
1286 break;
1287 case CEE_BGE:
1288 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1289 td.ip += 5;
1290 break;
1291 case CEE_BGE_S:
1292 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1293 td.ip += 2;
1294 break;
1295 case CEE_BGT:
1296 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1297 td.ip += 5;
1298 break;
1299 case CEE_BGT_S:
1300 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1301 td.ip += 2;
1302 break;
1303 case CEE_BLT:
1304 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1305 td.ip += 5;
1306 break;
1307 case CEE_BLT_S:
1308 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1309 td.ip += 2;
1310 break;
1311 case CEE_BLE:
1312 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1313 td.ip += 5;
1314 break;
1315 case CEE_BLE_S:
1316 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1317 td.ip += 2;
1318 break;
1319 case CEE_BNE_UN:
1320 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1321 td.ip += 5;
1322 break;
1323 case CEE_BNE_UN_S:
1324 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1325 td.ip += 2;
1326 break;
1327 case CEE_BGE_UN:
1328 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1329 td.ip += 5;
1330 break;
1331 case CEE_BGE_UN_S:
1332 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1333 td.ip += 2;
1334 break;
1335 case CEE_BGT_UN:
1336 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1337 td.ip += 5;
1338 break;
1339 case CEE_BGT_UN_S:
1340 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1341 td.ip += 2;
1342 break;
1343 case CEE_BLE_UN:
1344 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1345 td.ip += 5;
1346 break;
1347 case CEE_BLE_UN_S:
1348 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1349 td.ip += 2;
1350 break;
1351 case CEE_BLT_UN:
1352 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1353 td.ip += 5;
1354 break;
1355 case CEE_BLT_UN_S:
1356 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1357 td.ip += 2;
1358 break;
1359 case CEE_SWITCH: {
1360 guint32 n;
1361 const unsigned char *next_ip;
1362 const unsigned char *base_ip = td.ip;
1363 unsigned short *next_new_ip;
1364 ++td.ip;
1365 n = read32 (td.ip);
1366 ADD_CODE(&td, MINT_SWITCH);
1367 ADD_CODE(&td, * (unsigned short *)(&n));
1368 ADD_CODE(&td, * ((unsigned short *)&n + 1));
1369 td.ip += 4;
1370 next_ip = td.ip + n * 4;
1371 next_new_ip = td.new_ip + n * 2;
1372 for (i = 0; i < n; i++) {
1373 offset = read32 (td.ip);
1374 target = next_ip - td.il_code + offset;
1375 if (offset < 0)
1376 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1377 else {
1378 int prev = td.forward_refs [target];
1379 td.forward_refs [td.ip - td.il_code] = prev;
1380 td.forward_refs [target] = td.ip - td.il_code;
1381 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1383 ADD_CODE(&td, * (unsigned short *)(&target));
1384 ADD_CODE(&td, * ((unsigned short *)&target + 1));
1385 td.ip += 4;
1387 --td.sp;
1388 break;
1390 case CEE_LDIND_I1:
1391 CHECK_STACK (&td, 1);
1392 SIMPLE_OP (td, MINT_LDIND_I1);
1393 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1394 break;
1395 case CEE_LDIND_U1:
1396 CHECK_STACK (&td, 1);
1397 SIMPLE_OP (td, MINT_LDIND_U1);
1398 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1399 break;
1400 case CEE_LDIND_I2:
1401 CHECK_STACK (&td, 1);
1402 SIMPLE_OP (td, MINT_LDIND_I2);
1403 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1404 break;
1405 case CEE_LDIND_U2:
1406 CHECK_STACK (&td, 1);
1407 SIMPLE_OP (td, MINT_LDIND_U2);
1408 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1409 break;
1410 case CEE_LDIND_I4:
1411 CHECK_STACK (&td, 1);
1412 SIMPLE_OP (td, MINT_LDIND_I4);
1413 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1414 break;
1415 case CEE_LDIND_U4:
1416 CHECK_STACK (&td, 1);
1417 SIMPLE_OP (td, MINT_LDIND_U4);
1418 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1419 break;
1420 case CEE_LDIND_I8:
1421 CHECK_STACK (&td, 1);
1422 SIMPLE_OP (td, MINT_LDIND_I8);
1423 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1424 break;
1425 case CEE_LDIND_I:
1426 CHECK_STACK (&td, 1);
1427 SIMPLE_OP (td, MINT_LDIND_I);
1428 ADD_CODE (&td, 0);
1429 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1430 break;
1431 case CEE_LDIND_R4:
1432 CHECK_STACK (&td, 1);
1433 SIMPLE_OP (td, MINT_LDIND_R4);
1434 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1435 break;
1436 case CEE_LDIND_R8:
1437 CHECK_STACK (&td, 1);
1438 SIMPLE_OP (td, MINT_LDIND_R8);
1439 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1440 break;
1441 case CEE_LDIND_REF:
1442 CHECK_STACK (&td, 1);
1443 SIMPLE_OP (td, MINT_LDIND_REF);
1444 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1445 break;
1446 case CEE_STIND_REF:
1447 CHECK_STACK (&td, 2);
1448 SIMPLE_OP (td, MINT_STIND_REF);
1449 td.sp -= 2;
1450 break;
1451 case CEE_STIND_I1:
1452 CHECK_STACK (&td, 2);
1453 SIMPLE_OP (td, MINT_STIND_I1);
1454 td.sp -= 2;
1455 break;
1456 case CEE_STIND_I2:
1457 CHECK_STACK (&td, 2);
1458 SIMPLE_OP (td, MINT_STIND_I2);
1459 td.sp -= 2;
1460 break;
1461 case CEE_STIND_I4:
1462 CHECK_STACK (&td, 2);
1463 SIMPLE_OP (td, MINT_STIND_I4);
1464 td.sp -= 2;
1465 break;
1466 case CEE_STIND_I:
1467 CHECK_STACK (&td, 2);
1468 SIMPLE_OP (td, MINT_STIND_I);
1469 td.sp -= 2;
1470 break;
1471 case CEE_STIND_I8:
1472 CHECK_STACK (&td, 2);
1473 SIMPLE_OP (td, MINT_STIND_I8);
1474 td.sp -= 2;
1475 break;
1476 case CEE_STIND_R4:
1477 CHECK_STACK (&td, 2);
1478 SIMPLE_OP (td, MINT_STIND_R4);
1479 td.sp -= 2;
1480 break;
1481 case CEE_STIND_R8:
1482 CHECK_STACK (&td, 2);
1483 SIMPLE_OP (td, MINT_STIND_R8);
1484 td.sp -= 2;
1485 break;
1486 case CEE_ADD:
1487 binary_arith_op(&td, MINT_ADD_I4);
1488 ++td.ip;
1489 break;
1490 case CEE_SUB:
1491 binary_arith_op(&td, MINT_SUB_I4);
1492 ++td.ip;
1493 break;
1494 case CEE_MUL:
1495 binary_arith_op(&td, MINT_MUL_I4);
1496 ++td.ip;
1497 break;
1498 case CEE_DIV:
1499 binary_arith_op(&td, MINT_DIV_I4);
1500 ++td.ip;
1501 break;
1502 case CEE_DIV_UN:
1503 binary_arith_op(&td, MINT_DIV_UN_I4);
1504 ++td.ip;
1505 break;
1506 case CEE_REM:
1507 binary_int_op (&td, MINT_REM_I4);
1508 ++td.ip;
1509 break;
1510 case CEE_REM_UN:
1511 binary_int_op (&td, MINT_REM_UN_I4);
1512 ++td.ip;
1513 break;
1514 case CEE_AND:
1515 binary_int_op (&td, MINT_AND_I4);
1516 ++td.ip;
1517 break;
1518 case CEE_OR:
1519 binary_int_op (&td, MINT_OR_I4);
1520 ++td.ip;
1521 break;
1522 case CEE_XOR:
1523 binary_int_op (&td, MINT_XOR_I4);
1524 ++td.ip;
1525 break;
1526 case CEE_SHL:
1527 shift_op (&td, MINT_SHL_I4);
1528 ++td.ip;
1529 break;
1530 case CEE_SHR:
1531 shift_op (&td, MINT_SHR_I4);
1532 ++td.ip;
1533 break;
1534 case CEE_SHR_UN:
1535 shift_op (&td, MINT_SHR_UN_I4);
1536 ++td.ip;
1537 break;
1538 case CEE_NEG:
1539 unary_arith_op (&td, MINT_NEG_I4);
1540 ++td.ip;
1541 break;
1542 case CEE_NOT:
1543 unary_arith_op (&td, MINT_NOT_I4);
1544 ++td.ip;
1545 break;
1546 case CEE_CONV_U1:
1547 CHECK_STACK (&td, 1);
1548 switch (td.sp [-1].type) {
1549 case STACK_TYPE_R8:
1550 ADD_CODE(&td, MINT_CONV_U1_R8);
1551 break;
1552 case STACK_TYPE_I4:
1553 ADD_CODE(&td, MINT_CONV_U1_I4);
1554 break;
1555 case STACK_TYPE_I8:
1556 ADD_CODE(&td, MINT_CONV_U1_I8);
1557 break;
1558 default:
1559 g_assert_not_reached ();
1561 ++td.ip;
1562 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1563 break;
1564 case CEE_CONV_I1:
1565 CHECK_STACK (&td, 1);
1566 switch (td.sp [-1].type) {
1567 case STACK_TYPE_R8:
1568 ADD_CODE(&td, MINT_CONV_I1_R8);
1569 break;
1570 case STACK_TYPE_I4:
1571 ADD_CODE(&td, MINT_CONV_I1_I4);
1572 break;
1573 case STACK_TYPE_I8:
1574 ADD_CODE(&td, MINT_CONV_I1_I8);
1575 break;
1576 default:
1577 g_assert_not_reached ();
1579 ++td.ip;
1580 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1581 break;
1582 case CEE_CONV_U2:
1583 CHECK_STACK (&td, 1);
1584 switch (td.sp [-1].type) {
1585 case STACK_TYPE_R8:
1586 ADD_CODE(&td, MINT_CONV_U2_R8);
1587 break;
1588 case STACK_TYPE_I4:
1589 ADD_CODE(&td, MINT_CONV_U2_I4);
1590 break;
1591 case STACK_TYPE_I8:
1592 ADD_CODE(&td, MINT_CONV_U2_I8);
1593 break;
1594 default:
1595 g_assert_not_reached ();
1597 ++td.ip;
1598 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1599 break;
1600 case CEE_CONV_I2:
1601 CHECK_STACK (&td, 1);
1602 switch (td.sp [-1].type) {
1603 case STACK_TYPE_R8:
1604 ADD_CODE(&td, MINT_CONV_I2_R8);
1605 break;
1606 case STACK_TYPE_I4:
1607 ADD_CODE(&td, MINT_CONV_I2_I4);
1608 break;
1609 case STACK_TYPE_I8:
1610 ADD_CODE(&td, MINT_CONV_I2_I8);
1611 break;
1612 default:
1613 g_assert_not_reached ();
1615 ++td.ip;
1616 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1617 break;
1618 case CEE_CONV_U:
1619 CHECK_STACK (&td, 1);
1620 switch (td.sp [-1].type) {
1621 case STACK_TYPE_R8:
1622 #if SIZEOF_VOID_P == 4
1623 ADD_CODE(&td, MINT_CONV_U4_R8);
1624 #else
1625 ADD_CODE(&td, MINT_CONV_U8_R8);
1626 #endif
1627 break;
1628 case STACK_TYPE_I4:
1629 #if SIZEOF_VOID_P == 8
1630 ADD_CODE(&td, MINT_CONV_U8_I4);
1631 #endif
1632 break;
1633 case STACK_TYPE_I8:
1634 #if SIZEOF_VOID_P == 4
1635 ADD_CODE(&td, MINT_CONV_U4_I8);
1636 #endif
1637 break;
1638 case STACK_TYPE_MP:
1639 break;
1640 default:
1641 g_assert_not_reached ();
1643 ++td.ip;
1644 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1645 break;
1646 case CEE_CONV_I:
1647 CHECK_STACK (&td, 1);
1648 switch (td.sp [-1].type) {
1649 case STACK_TYPE_R8:
1650 #if SIZEOF_VOID_P == 8
1651 ADD_CODE(&td, MINT_CONV_I8_R8);
1652 #else
1653 ADD_CODE(&td, MINT_CONV_I4_R8);
1654 #endif
1655 break;
1656 case STACK_TYPE_I4:
1657 #if SIZEOF_VOID_P == 8
1658 ADD_CODE(&td, MINT_CONV_I8_I4);
1659 #endif
1660 break;
1661 case STACK_TYPE_O:
1662 break;
1663 case STACK_TYPE_MP:
1664 break;
1665 case STACK_TYPE_I8:
1666 #if SIZEOF_VOID_P == 4
1667 ADD_CODE(&td, MINT_CONV_I4_I8);
1668 #endif
1669 break;
1670 default:
1671 g_assert_not_reached ();
1673 ++td.ip;
1674 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1675 break;
1676 case CEE_CONV_U4:
1677 CHECK_STACK (&td, 1);
1678 switch (td.sp [-1].type) {
1679 case STACK_TYPE_R8:
1680 ADD_CODE(&td, MINT_CONV_U4_R8);
1681 break;
1682 case STACK_TYPE_I4:
1683 break;
1684 case STACK_TYPE_I8:
1685 ADD_CODE(&td, MINT_CONV_U4_I8);
1686 break;
1687 case STACK_TYPE_MP:
1688 #if SIZEOF_VOID_P == 8
1689 ADD_CODE(&td, MINT_CONV_U4_I8);
1690 #endif
1691 break;
1692 default:
1693 g_assert_not_reached ();
1695 ++td.ip;
1696 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1697 break;
1698 case CEE_CONV_I4:
1699 CHECK_STACK (&td, 1);
1700 switch (td.sp [-1].type) {
1701 case STACK_TYPE_R8:
1702 ADD_CODE(&td, MINT_CONV_I4_R8);
1703 break;
1704 case STACK_TYPE_I4:
1705 break;
1706 case STACK_TYPE_I8:
1707 ADD_CODE(&td, MINT_CONV_I4_I8);
1708 break;
1709 case STACK_TYPE_MP:
1710 #if SIZEOF_VOID_P == 8
1711 ADD_CODE(&td, MINT_CONV_I4_I8);
1712 #endif
1713 break;
1714 default:
1715 g_assert_not_reached ();
1717 ++td.ip;
1718 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1719 break;
1720 case CEE_CONV_I8:
1721 CHECK_STACK (&td, 1);
1722 switch (td.sp [-1].type) {
1723 case STACK_TYPE_R8:
1724 ADD_CODE(&td, MINT_CONV_I8_R8);
1725 break;
1726 case STACK_TYPE_I4:
1727 ADD_CODE(&td, MINT_CONV_I8_I4);
1728 break;
1729 case STACK_TYPE_I8:
1730 break;
1731 case STACK_TYPE_MP:
1732 #if SIZEOF_VOID_P == 4
1733 ADD_CODE(&td, MINT_CONV_I8_I4);
1734 #endif
1735 break;
1736 default:
1737 g_assert_not_reached ();
1739 ++td.ip;
1740 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1741 break;
1742 case CEE_CONV_R4:
1743 CHECK_STACK (&td, 1);
1744 switch (td.sp [-1].type) {
1745 case STACK_TYPE_R8:
1746 ADD_CODE(&td, MINT_CONV_R4_R8);
1747 break;
1748 case STACK_TYPE_I8:
1749 ADD_CODE(&td, MINT_CONV_R4_I8);
1750 break;
1751 case STACK_TYPE_I4:
1752 ADD_CODE(&td, MINT_CONV_R4_I4);
1753 break;
1754 default:
1755 g_assert_not_reached ();
1757 ++td.ip;
1758 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1759 break;
1760 case CEE_CONV_R8:
1761 CHECK_STACK (&td, 1);
1762 switch (td.sp [-1].type) {
1763 case STACK_TYPE_I4:
1764 ADD_CODE(&td, MINT_CONV_R8_I4);
1765 break;
1766 case STACK_TYPE_I8:
1767 ADD_CODE(&td, MINT_CONV_R8_I8);
1768 break;
1769 case STACK_TYPE_R8:
1770 break;
1771 default:
1772 g_assert_not_reached ();
1774 ++td.ip;
1775 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1776 break;
1777 case CEE_CONV_U8:
1778 CHECK_STACK (&td, 1);
1779 switch (td.sp [-1].type) {
1780 case STACK_TYPE_I4:
1781 ADD_CODE(&td, MINT_CONV_U8_I4);
1782 break;
1783 case STACK_TYPE_I8:
1784 break;
1785 case STACK_TYPE_R8:
1786 ADD_CODE(&td, MINT_CONV_U8_R8);
1787 break;
1788 case STACK_TYPE_MP:
1789 #if SIZEOF_VOID_P == 4
1790 ADD_CODE(&td, MINT_CONV_U8_I4);
1791 #endif
1792 break;
1793 default:
1794 g_assert_not_reached ();
1796 ++td.ip;
1797 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1798 break;
1799 case CEE_CPOBJ: {
1800 CHECK_STACK (&td, 2);
1802 token = read32 (td.ip + 1);
1803 klass = mono_class_get_full (image, token, generic_context);
1805 if (klass->valuetype) {
1806 ADD_CODE (&td, MINT_CPOBJ);
1807 ADD_CODE (&td, get_data_item_index(&td, klass));
1808 } else {
1809 ADD_CODE (&td, MINT_LDIND_REF);
1810 ADD_CODE (&td, MINT_STIND_REF);
1812 td.ip += 5;
1813 td.sp -= 2;
1814 break;
1816 case CEE_LDOBJ: {
1817 int size;
1818 CHECK_STACK (&td, 1);
1820 token = read32 (td.ip + 1);
1822 if (method->wrapper_type != MONO_WRAPPER_NONE)
1823 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1824 else
1825 klass = mono_class_get_full (image, token, generic_context);
1827 ADD_CODE(&td, MINT_LDOBJ);
1828 ADD_CODE(&td, get_data_item_index(&td, klass));
1829 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1830 size = mono_class_value_size (klass, NULL);
1831 PUSH_VT(&td, size);
1833 td.ip += 5;
1834 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1835 break;
1837 case CEE_LDSTR: {
1838 MonoString *s;
1839 token = mono_metadata_token_index (read32 (td.ip + 1));
1840 td.ip += 5;
1841 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1842 s = mono_string_new_wrapper(
1843 mono_method_get_wrapper_data (method, token));
1845 else
1846 s = mono_ldstr (domain, image, token);
1847 ADD_CODE(&td, MINT_LDSTR);
1848 ADD_CODE(&td, get_data_item_index (&td, s));
1849 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1850 break;
1852 case CEE_NEWOBJ: {
1853 MonoMethod *m;
1854 MonoMethodSignature *csignature;
1855 guint32 vt_stack_used = 0;
1856 guint32 vt_res_size = 0;
1858 td.ip++;
1859 token = read32 (td.ip);
1860 td.ip += 4;
1862 if (method->wrapper_type != MONO_WRAPPER_NONE)
1863 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1864 else
1865 m = mono_get_method_full (image, token, NULL, generic_context);
1867 csignature = mono_method_signature (m);
1868 klass = m->klass;
1869 td.sp -= csignature->param_count;
1870 ADD_CODE(&td, MINT_NEWOBJ);
1871 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1872 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1874 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1875 vt_res_size = mono_class_value_size (klass, NULL);
1876 PUSH_VT (&td, vt_res_size);
1878 for (i = 0; i < csignature->param_count; ++i) {
1879 int mt = mint_type(csignature->params [i]);
1880 if (mt == MINT_TYPE_VT) {
1881 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1882 gint32 size = mono_class_value_size (k, NULL);
1883 size = (size + 7) & ~7;
1884 vt_stack_used += size;
1887 if (vt_stack_used != 0 || vt_res_size != 0) {
1888 ADD_CODE(&td, MINT_VTRESULT);
1889 ADD_CODE(&td, vt_res_size);
1890 WRITE32(&td, &vt_stack_used);
1891 td.vt_sp -= vt_stack_used;
1893 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1894 break;
1896 case CEE_CASTCLASS:
1897 CHECK_STACK (&td, 1);
1898 token = read32 (td.ip + 1);
1899 klass = mono_class_get_full (image, token, generic_context);
1900 ADD_CODE(&td, MINT_CASTCLASS);
1901 ADD_CODE(&td, get_data_item_index (&td, klass));
1902 td.sp [-1].klass = klass;
1903 td.ip += 5;
1904 break;
1905 case CEE_ISINST:
1906 CHECK_STACK (&td, 1);
1907 token = read32 (td.ip + 1);
1908 klass = mono_class_get_full (image, token, generic_context);
1909 ADD_CODE(&td, MINT_ISINST);
1910 ADD_CODE(&td, get_data_item_index (&td, klass));
1911 td.ip += 5;
1912 break;
1913 case CEE_CONV_R_UN:
1914 switch (td.sp [-1].type) {
1915 case STACK_TYPE_R8:
1916 break;
1917 case STACK_TYPE_I8:
1918 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1919 break;
1920 case STACK_TYPE_I4:
1921 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1922 break;
1923 default:
1924 g_assert_not_reached ();
1926 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1927 ++td.ip;
1928 break;
1929 case CEE_UNBOX:
1930 CHECK_STACK (&td, 1);
1931 token = read32 (td.ip + 1);
1933 if (method->wrapper_type != MONO_WRAPPER_NONE)
1934 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1935 else
1936 klass = mono_class_get_full (image, token, generic_context);
1938 if (mono_class_is_nullable (klass)) {
1939 g_error ("cee_unbox: implement Nullable");
1942 ADD_CODE(&td, MINT_UNBOX);
1943 ADD_CODE(&td, get_data_item_index (&td, klass));
1944 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1945 td.ip += 5;
1946 break;
1947 case CEE_UNBOX_ANY:
1948 CHECK_STACK (&td, 1);
1949 token = read32 (td.ip + 1);
1951 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1952 klass = mono_class_get_full (image, token, generic_context);
1954 if (mini_type_is_reference (&klass->byval_arg)) {
1955 ADD_CODE (&td, MINT_CASTCLASS);
1956 ADD_CODE (&td, get_data_item_index (&td, klass));
1957 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1958 td.ip += 5;
1959 } else if (mono_class_is_nullable (klass)) {
1960 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
1961 /* td.ip is incremented by interp_transform_call */
1962 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
1963 } else {
1964 int mt = mint_type (&klass->byval_arg);
1965 ADD_CODE (&td, MINT_UNBOX);
1966 ADD_CODE (&td, get_data_item_index (&td, klass));
1968 ADD_CODE (&td, MINT_LDOBJ);
1969 ADD_CODE (&td, get_data_item_index(&td, klass));
1970 SET_TYPE (td.sp - 1, stack_type [mt], klass);
1972 if (mt == MINT_TYPE_VT) {
1973 int size = mono_class_value_size (klass, NULL);
1974 PUSH_VT (&td, size);
1976 td.ip += 5;
1979 break;
1980 case CEE_THROW:
1981 CHECK_STACK (&td, 1);
1982 SIMPLE_OP (td, MINT_THROW);
1983 --td.sp;
1984 generating_code = 0;
1985 break;
1986 case CEE_LDFLDA:
1987 CHECK_STACK (&td, 1);
1988 token = read32 (td.ip + 1);
1989 field = mono_field_from_token (image, token, &klass, generic_context);
1990 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
1991 mono_class_init (klass);
1992 if (is_static) {
1993 ADD_CODE (&td, MINT_POP);
1994 ADD_CODE (&td, 0);
1995 ADD_CODE (&td, MINT_LDSFLDA);
1996 ADD_CODE (&td, get_data_item_index (&td, field));
1997 } else {
1998 ADD_CODE (&td, MINT_LDFLDA);
1999 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2001 td.ip += 5;
2002 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2003 break;
2004 case CEE_LDFLD: {
2005 CHECK_STACK (&td, 1);
2006 token = read32 (td.ip + 1);
2007 field = mono_field_from_token (image, token, &klass, generic_context);
2008 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2009 mono_class_init (klass);
2011 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2012 mt = mint_type (&field_klass->byval_arg);
2013 if (klass->marshalbyref) {
2014 g_assert (!is_static);
2015 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2016 ADD_CODE(&td, get_data_item_index (&td, field));
2017 } else {
2018 if (is_static) {
2019 ADD_CODE (&td, MINT_POP);
2020 ADD_CODE (&td, 0);
2021 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2022 ADD_CODE (&td, get_data_item_index (&td, field));
2023 } else {
2024 ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2025 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2028 if (mt == MINT_TYPE_VT) {
2029 int size = mono_class_value_size (field_klass, NULL);
2030 PUSH_VT(&td, size);
2031 WRITE32(&td, &size);
2033 if (td.sp [-1].type == STACK_TYPE_VT) {
2034 int size = mono_class_value_size (klass, NULL);
2035 size = (size + 7) & ~7;
2036 td.vt_sp -= size;
2037 ADD_CODE (&td, MINT_VTRESULT);
2038 ADD_CODE (&td, 0);
2039 WRITE32 (&td, &size);
2041 td.ip += 5;
2042 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2043 break;
2045 case CEE_STFLD: {
2046 CHECK_STACK (&td, 2);
2047 token = read32 (td.ip + 1);
2048 field = mono_field_from_token (image, token, &klass, generic_context);
2049 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2050 mono_class_init (klass);
2051 mt = mint_type(field->type);
2053 if (klass->marshalbyref) {
2054 g_assert (!is_static);
2055 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2056 ADD_CODE(&td, get_data_item_index (&td, field));
2057 } else {
2058 if (is_static) {
2059 ADD_CODE (&td, MINT_POP);
2060 ADD_CODE (&td, 1);
2061 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2062 ADD_CODE (&td, get_data_item_index (&td, field));
2063 } else {
2064 ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2065 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2068 if (mt == MINT_TYPE_VT) {
2069 MonoClass *klass = mono_class_from_mono_type (field->type);
2070 int size = mono_class_value_size (klass, NULL);
2071 POP_VT(&td, size);
2072 WRITE32(&td, &size);
2074 td.ip += 5;
2075 td.sp -= 2;
2076 break;
2078 case CEE_LDSFLDA:
2079 token = read32 (td.ip + 1);
2080 field = mono_field_from_token (image, token, &klass, generic_context);
2081 ADD_CODE(&td, MINT_LDSFLDA);
2082 ADD_CODE(&td, get_data_item_index (&td, field));
2083 td.ip += 5;
2084 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2085 break;
2086 case CEE_LDSFLD:
2087 token = read32 (td.ip + 1);
2088 field = mono_field_from_token (image, token, &klass, generic_context);
2089 mt = mint_type(field->type);
2090 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2091 ADD_CODE(&td, get_data_item_index (&td, field));
2092 klass = NULL;
2093 if (mt == MINT_TYPE_VT) {
2094 MonoClass *klass = mono_class_from_mono_type (field->type);
2095 int size = mono_class_value_size (klass, NULL);
2096 PUSH_VT(&td, size);
2097 WRITE32(&td, &size);
2098 klass = field->type->data.klass;
2099 } else {
2100 if (mt == MINT_TYPE_O)
2101 klass = mono_class_from_mono_type (field->type);
2103 td.ip += 5;
2104 PUSH_TYPE(&td, stack_type [mt], klass);
2105 break;
2106 case CEE_STSFLD:
2107 CHECK_STACK (&td, 1);
2108 token = read32 (td.ip + 1);
2109 field = mono_field_from_token (image, token, &klass, generic_context);
2110 mt = mint_type(field->type);
2111 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2112 ADD_CODE(&td, get_data_item_index (&td, field));
2113 if (mt == MINT_TYPE_VT) {
2114 MonoClass *klass = mono_class_from_mono_type (field->type);
2115 int size = mono_class_value_size (klass, NULL);
2116 POP_VT (&td, size);
2117 WRITE32 (&td, &size);
2119 td.ip += 5;
2120 --td.sp;
2121 break;
2122 case CEE_STOBJ: {
2123 int size;
2124 token = read32 (td.ip + 1);
2126 if (method->wrapper_type != MONO_WRAPPER_NONE)
2127 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2128 else
2129 klass = mono_class_get_full (image, token, generic_context);
2131 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2132 ADD_CODE(&td, get_data_item_index (&td, klass));
2133 if (td.sp [-1].type == STACK_TYPE_VT) {
2134 size = mono_class_value_size (klass, NULL);
2135 size = (size + 7) & ~7;
2136 td.vt_sp -= size;
2138 td.ip += 5;
2139 td.sp -= 2;
2140 break;
2142 case CEE_CONV_OVF_I_UN:
2143 case CEE_CONV_OVF_U_UN:
2144 CHECK_STACK (&td, 1);
2145 switch (td.sp [-1].type) {
2146 case STACK_TYPE_R8:
2147 #if SIZEOF_VOID_P == 8
2148 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2149 #else
2150 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2151 #endif
2152 break;
2153 case STACK_TYPE_I8:
2154 #if SIZEOF_VOID_P == 4
2155 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2156 #endif
2157 break;
2158 case STACK_TYPE_I4:
2159 #if SIZEOF_VOID_P == 8
2160 ADD_CODE(&td, MINT_CONV_I8_U4);
2161 #endif
2162 break;
2163 default:
2164 g_assert_not_reached ();
2165 break;
2167 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2168 ++td.ip;
2169 break;
2170 case CEE_CONV_OVF_I8_UN:
2171 case CEE_CONV_OVF_U8_UN:
2172 CHECK_STACK (&td, 1);
2173 switch (td.sp [-1].type) {
2174 case STACK_TYPE_R8:
2175 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2176 break;
2177 case STACK_TYPE_I8:
2178 if (*td.ip == CEE_CONV_OVF_I8_UN)
2179 ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
2180 break;
2181 case STACK_TYPE_I4:
2182 ADD_CODE(&td, MINT_CONV_I8_U4);
2183 break;
2184 default:
2185 g_assert_not_reached ();
2186 break;
2188 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2189 ++td.ip;
2190 break;
2191 case CEE_BOX: {
2192 int size;
2193 CHECK_STACK (&td, 1);
2194 token = read32 (td.ip + 1);
2195 if (method->wrapper_type != MONO_WRAPPER_NONE)
2196 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2197 else
2198 klass = mono_class_get_full (image, token, generic_context);
2200 if (mono_class_is_nullable (klass)) {
2201 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2202 /* td.ip is incremented by interp_transform_call */
2203 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2204 } else if (!klass->valuetype) {
2205 /* already boxed, do nothing. */
2206 td.ip += 5;
2207 } else {
2208 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2209 size = mono_class_value_size (klass, NULL);
2210 size = (size + 7) & ~7;
2211 td.vt_sp -= size;
2213 ADD_CODE(&td, MINT_BOX);
2214 ADD_CODE(&td, get_data_item_index (&td, klass));
2215 ADD_CODE (&td, 0);
2216 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2217 td.ip += 5;
2220 break;
2222 case CEE_NEWARR: {
2223 CHECK_STACK (&td, 1);
2224 token = read32 (td.ip + 1);
2226 if (method->wrapper_type != MONO_WRAPPER_NONE)
2227 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2228 else
2229 klass = mono_class_get_full (image, token, generic_context);
2231 unsigned char lentype = (td.sp - 1)->type;
2232 if (lentype == STACK_TYPE_I8) {
2233 /* mimic mini behaviour */
2234 ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
2235 } else {
2236 g_assert (lentype == STACK_TYPE_I4);
2237 ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
2239 SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
2240 ADD_CODE (&td, MINT_NEWARR);
2241 ADD_CODE (&td, get_data_item_index (&td, klass));
2242 SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
2243 td.ip += 5;
2244 break;
2246 case CEE_LDLEN:
2247 CHECK_STACK (&td, 1);
2248 SIMPLE_OP (td, MINT_LDLEN);
2249 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2250 break;
2251 case CEE_LDELEMA:
2252 CHECK_STACK (&td, 2);
2253 ENSURE_I4 (&td, 1);
2254 token = read32 (td.ip + 1);
2256 if (method->wrapper_type != MONO_WRAPPER_NONE)
2257 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2258 else
2259 klass = mono_class_get_full (image, token, generic_context);
2261 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2262 ADD_CODE (&td, MINT_LDELEMA_TC);
2263 } else {
2264 ADD_CODE (&td, MINT_LDELEMA);
2266 ADD_CODE (&td, get_data_item_index (&td, klass));
2267 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2268 ADD_CODE (&td, 2);
2269 readonly = FALSE;
2271 td.ip += 5;
2272 --td.sp;
2273 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2274 break;
2275 case CEE_LDELEM_I1:
2276 CHECK_STACK (&td, 2);
2277 ENSURE_I4 (&td, 1);
2278 SIMPLE_OP (td, MINT_LDELEM_I1);
2279 --td.sp;
2280 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2281 break;
2282 case CEE_LDELEM_U1:
2283 CHECK_STACK (&td, 2);
2284 ENSURE_I4 (&td, 1);
2285 SIMPLE_OP (td, MINT_LDELEM_U1);
2286 --td.sp;
2287 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2288 break;
2289 case CEE_LDELEM_I2:
2290 CHECK_STACK (&td, 2);
2291 ENSURE_I4 (&td, 1);
2292 SIMPLE_OP (td, MINT_LDELEM_I2);
2293 --td.sp;
2294 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2295 break;
2296 case CEE_LDELEM_U2:
2297 CHECK_STACK (&td, 2);
2298 ENSURE_I4 (&td, 1);
2299 SIMPLE_OP (td, MINT_LDELEM_U2);
2300 --td.sp;
2301 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2302 break;
2303 case CEE_LDELEM_I4:
2304 CHECK_STACK (&td, 2);
2305 ENSURE_I4 (&td, 1);
2306 SIMPLE_OP (td, MINT_LDELEM_I4);
2307 --td.sp;
2308 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2309 break;
2310 case CEE_LDELEM_U4:
2311 CHECK_STACK (&td, 2);
2312 ENSURE_I4 (&td, 1);
2313 SIMPLE_OP (td, MINT_LDELEM_U4);
2314 --td.sp;
2315 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2316 break;
2317 case CEE_LDELEM_I8:
2318 CHECK_STACK (&td, 2);
2319 ENSURE_I4 (&td, 1);
2320 SIMPLE_OP (td, MINT_LDELEM_I8);
2321 --td.sp;
2322 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2323 break;
2324 case CEE_LDELEM_I:
2325 CHECK_STACK (&td, 2);
2326 ENSURE_I4 (&td, 1);
2327 SIMPLE_OP (td, MINT_LDELEM_I);
2328 --td.sp;
2329 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2330 break;
2331 case CEE_LDELEM_R4:
2332 CHECK_STACK (&td, 2);
2333 ENSURE_I4 (&td, 1);
2334 SIMPLE_OP (td, MINT_LDELEM_R4);
2335 --td.sp;
2336 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2337 break;
2338 case CEE_LDELEM_R8:
2339 CHECK_STACK (&td, 2);
2340 ENSURE_I4 (&td, 1);
2341 SIMPLE_OP (td, MINT_LDELEM_R8);
2342 --td.sp;
2343 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2344 break;
2345 case CEE_LDELEM_REF:
2346 CHECK_STACK (&td, 2);
2347 ENSURE_I4 (&td, 1);
2348 SIMPLE_OP (td, MINT_LDELEM_REF);
2349 --td.sp;
2350 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2351 break;
2352 case CEE_LDELEM:
2353 CHECK_STACK (&td, 2);
2354 token = read32 (td.ip + 1);
2355 klass = mono_class_get_full (image, token, generic_context);
2356 switch (mint_type (&klass->byval_arg)) {
2357 case MINT_TYPE_I1:
2358 ENSURE_I4 (&td, 1);
2359 SIMPLE_OP (td, MINT_LDELEM_I1);
2360 --td.sp;
2361 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2362 break;
2363 case MINT_TYPE_U1:
2364 ENSURE_I4 (&td, 1);
2365 SIMPLE_OP (td, MINT_LDELEM_U1);
2366 --td.sp;
2367 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2368 break;
2369 case MINT_TYPE_U2:
2370 ENSURE_I4 (&td, 1);
2371 SIMPLE_OP (td, MINT_LDELEM_U2);
2372 --td.sp;
2373 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2374 break;
2375 case MINT_TYPE_I2:
2376 ENSURE_I4 (&td, 1);
2377 SIMPLE_OP (td, MINT_LDELEM_I2);
2378 --td.sp;
2379 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2380 break;
2381 case MINT_TYPE_I4:
2382 ENSURE_I4 (&td, 1);
2383 SIMPLE_OP (td, MINT_LDELEM_I4);
2384 --td.sp;
2385 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2386 break;
2387 case MINT_TYPE_I8:
2388 ENSURE_I4 (&td, 1);
2389 SIMPLE_OP (td, MINT_LDELEM_I8);
2390 --td.sp;
2391 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2392 break;
2393 case MINT_TYPE_R4:
2394 ENSURE_I4 (&td, 1);
2395 SIMPLE_OP (td, MINT_LDELEM_R4);
2396 --td.sp;
2397 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2398 break;
2399 case MINT_TYPE_R8:
2400 ENSURE_I4 (&td, 1);
2401 SIMPLE_OP (td, MINT_LDELEM_R8);
2402 --td.sp;
2403 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2404 break;
2405 case MINT_TYPE_O:
2406 ENSURE_I4 (&td, 1);
2407 SIMPLE_OP (td, MINT_LDELEM_REF);
2408 --td.sp;
2409 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2410 break;
2411 case MINT_TYPE_VT: {
2412 int size = mono_class_value_size (klass, NULL);
2413 ENSURE_I4 (&td, 1);
2414 SIMPLE_OP (td, MINT_LDELEM_VT);
2415 ADD_CODE (&td, get_data_item_index (&td, klass));
2416 WRITE32 (&td, &size);
2417 --td.sp;
2418 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2419 PUSH_VT (&td, size);
2420 break;
2422 default: {
2423 GString *res = g_string_new ("");
2424 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2425 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2426 g_string_free (res, TRUE);
2427 g_assert (0);
2428 break;
2431 td.ip += 4;
2432 break;
2433 case CEE_STELEM_I:
2434 CHECK_STACK (&td, 3);
2435 ENSURE_I4 (&td, 2);
2436 SIMPLE_OP (td, MINT_STELEM_I);
2437 td.sp -= 3;
2438 break;
2439 case CEE_STELEM_I1:
2440 CHECK_STACK (&td, 3);
2441 ENSURE_I4 (&td, 2);
2442 SIMPLE_OP (td, MINT_STELEM_I1);
2443 td.sp -= 3;
2444 break;
2445 case CEE_STELEM_I2:
2446 CHECK_STACK (&td, 3);
2447 ENSURE_I4 (&td, 2);
2448 SIMPLE_OP (td, MINT_STELEM_I2);
2449 td.sp -= 3;
2450 break;
2451 case CEE_STELEM_I4:
2452 CHECK_STACK (&td, 3);
2453 ENSURE_I4 (&td, 2);
2454 SIMPLE_OP (td, MINT_STELEM_I4);
2455 td.sp -= 3;
2456 break;
2457 case CEE_STELEM_I8:
2458 CHECK_STACK (&td, 3);
2459 ENSURE_I4 (&td, 2);
2460 SIMPLE_OP (td, MINT_STELEM_I8);
2461 td.sp -= 3;
2462 break;
2463 case CEE_STELEM_R4:
2464 CHECK_STACK (&td, 3);
2465 ENSURE_I4 (&td, 2);
2466 SIMPLE_OP (td, MINT_STELEM_R4);
2467 td.sp -= 3;
2468 break;
2469 case CEE_STELEM_R8:
2470 CHECK_STACK (&td, 3);
2471 ENSURE_I4 (&td, 2);
2472 SIMPLE_OP (td, MINT_STELEM_R8);
2473 td.sp -= 3;
2474 break;
2475 case CEE_STELEM_REF:
2476 CHECK_STACK (&td, 3);
2477 ENSURE_I4 (&td, 2);
2478 SIMPLE_OP (td, MINT_STELEM_REF);
2479 td.sp -= 3;
2480 break;
2481 case CEE_STELEM:
2482 CHECK_STACK (&td, 3);
2483 ENSURE_I4 (&td, 2);
2484 token = read32 (td.ip + 1);
2485 klass = mono_class_get_full (image, token, generic_context);
2486 switch (mint_type (&klass->byval_arg)) {
2487 case MINT_TYPE_U1:
2488 SIMPLE_OP (td, MINT_STELEM_U1);
2489 break;
2490 case MINT_TYPE_I4:
2491 SIMPLE_OP (td, MINT_STELEM_I4);
2492 break;
2493 case MINT_TYPE_O:
2494 SIMPLE_OP (td, MINT_STELEM_REF);
2495 break;
2496 case MINT_TYPE_VT: {
2497 int size = mono_class_value_size (klass, NULL);
2498 SIMPLE_OP (td, MINT_STELEM_VT);
2499 ADD_CODE (&td, get_data_item_index (&td, klass));
2500 WRITE32 (&td, &size);
2501 POP_VT (&td, size);
2502 break;
2504 default: {
2505 GString *res = g_string_new ("");
2506 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2507 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2508 g_string_free (res, TRUE);
2509 g_assert (0);
2510 break;
2513 td.ip += 4;
2514 td.sp -= 3;
2515 break;
2516 #if 0
2517 case CEE_CONV_OVF_U1:
2519 case CEE_CONV_OVF_I8:
2521 #if SIZEOF_VOID_P == 8
2522 case CEE_CONV_OVF_U:
2523 #endif
2524 case CEE_REFANYVAL: ves_abort(); break;
2525 #endif
2526 case CEE_CKFINITE:
2527 CHECK_STACK (&td, 1);
2528 SIMPLE_OP (td, MINT_CKFINITE);
2529 break;
2530 case CEE_CONV_OVF_I1:
2531 case CEE_CONV_OVF_I1_UN:
2532 CHECK_STACK (&td, 1);
2533 switch (td.sp [-1].type) {
2534 case STACK_TYPE_R8:
2535 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2536 break;
2537 case STACK_TYPE_I4:
2538 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2539 break;
2540 case STACK_TYPE_I8:
2541 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2542 break;
2543 default:
2544 g_assert_not_reached ();
2546 ++td.ip;
2547 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2548 break;
2549 case CEE_CONV_OVF_U1:
2550 case CEE_CONV_OVF_U1_UN:
2551 CHECK_STACK (&td, 1);
2552 switch (td.sp [-1].type) {
2553 case STACK_TYPE_R8:
2554 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2555 break;
2556 case STACK_TYPE_I4:
2557 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2558 break;
2559 case STACK_TYPE_I8:
2560 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2561 break;
2562 default:
2563 g_assert_not_reached ();
2565 ++td.ip;
2566 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2567 break;
2568 case CEE_CONV_OVF_I2:
2569 case CEE_CONV_OVF_I2_UN:
2570 CHECK_STACK (&td, 1);
2571 switch (td.sp [-1].type) {
2572 case STACK_TYPE_R8:
2573 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2574 break;
2575 case STACK_TYPE_I4:
2576 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2577 break;
2578 case STACK_TYPE_I8:
2579 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2580 break;
2581 default:
2582 g_assert_not_reached ();
2584 ++td.ip;
2585 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2586 break;
2587 case CEE_CONV_OVF_U2_UN:
2588 case CEE_CONV_OVF_U2:
2589 CHECK_STACK (&td, 1);
2590 switch (td.sp [-1].type) {
2591 case STACK_TYPE_R8:
2592 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2593 break;
2594 case STACK_TYPE_I4:
2595 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2596 break;
2597 case STACK_TYPE_I8:
2598 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2599 break;
2600 default:
2601 g_assert_not_reached ();
2603 ++td.ip;
2604 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2605 break;
2606 #if SIZEOF_VOID_P == 4
2607 case CEE_CONV_OVF_I:
2608 #endif
2609 case CEE_CONV_OVF_I4:
2610 case CEE_CONV_OVF_I4_UN:
2611 CHECK_STACK (&td, 1);
2612 switch (td.sp [-1].type) {
2613 case STACK_TYPE_R8:
2614 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2615 break;
2616 case STACK_TYPE_I4:
2617 if (*td.ip == CEE_CONV_OVF_I4_UN)
2618 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2619 break;
2620 case STACK_TYPE_I8:
2621 if (*td.ip == CEE_CONV_OVF_I4_UN)
2622 ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
2623 else
2624 ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
2625 break;
2626 default:
2627 g_assert_not_reached ();
2629 ++td.ip;
2630 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2631 break;
2632 #if SIZEOF_VOID_P == 4
2633 case CEE_CONV_OVF_U:
2634 #endif
2635 case CEE_CONV_OVF_U4:
2636 case CEE_CONV_OVF_U4_UN:
2637 CHECK_STACK (&td, 1);
2638 switch (td.sp [-1].type) {
2639 case STACK_TYPE_R8:
2640 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2641 break;
2642 case STACK_TYPE_I4:
2643 if (*td.ip != CEE_CONV_OVF_U4_UN)
2644 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2645 break;
2646 case STACK_TYPE_I8:
2647 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2648 break;
2649 default:
2650 g_assert_not_reached ();
2652 ++td.ip;
2653 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2654 break;
2655 #if SIZEOF_VOID_P == 8
2656 case CEE_CONV_OVF_I:
2657 #endif
2658 case CEE_CONV_OVF_I8:
2659 CHECK_STACK (&td, 1);
2660 switch (td.sp [-1].type) {
2661 case STACK_TYPE_R8:
2662 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2663 break;
2664 case STACK_TYPE_I4:
2665 ADD_CODE(&td, MINT_CONV_I8_I4);
2666 break;
2667 case STACK_TYPE_I8:
2668 break;
2669 default:
2670 g_assert_not_reached ();
2672 ++td.ip;
2673 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2674 break;
2675 #if SIZEOF_VOID_P == 8
2676 case CEE_CONV_OVF_U:
2677 #endif
2678 case CEE_CONV_OVF_U8:
2679 CHECK_STACK (&td, 1);
2680 switch (td.sp [-1].type) {
2681 case STACK_TYPE_R8:
2682 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2683 break;
2684 case STACK_TYPE_I4:
2685 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2686 break;
2687 case STACK_TYPE_I8:
2688 ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
2689 break;
2690 default:
2691 g_assert_not_reached ();
2693 ++td.ip;
2694 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2695 break;
2696 case CEE_LDTOKEN: {
2697 int size;
2698 gpointer handle;
2699 token = read32 (td.ip + 1);
2700 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2701 handle = mono_method_get_wrapper_data (method, token);
2702 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2703 if (klass == mono_defaults.typehandle_class)
2704 handle = &((MonoClass *) handle)->byval_arg;
2705 } else {
2706 handle = mono_ldtoken (image, token, &klass, generic_context);
2708 mt = mint_type (&klass->byval_arg);
2709 g_assert (mt == MINT_TYPE_VT);
2710 size = mono_class_value_size (klass, NULL);
2711 g_assert (size == sizeof(gpointer));
2712 PUSH_VT (&td, sizeof(gpointer));
2713 ADD_CODE (&td, MINT_LDTOKEN);
2714 ADD_CODE (&td, get_data_item_index (&td, handle));
2716 SET_TYPE (td.sp, stack_type [mt], klass);
2717 td.sp++;
2718 td.ip += 5;
2719 break;
2721 case CEE_ADD_OVF:
2722 binary_arith_op(&td, MINT_ADD_OVF_I4);
2723 ++td.ip;
2724 break;
2725 case CEE_ADD_OVF_UN:
2726 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2727 ++td.ip;
2728 break;
2729 case CEE_MUL_OVF:
2730 binary_arith_op(&td, MINT_MUL_OVF_I4);
2731 ++td.ip;
2732 break;
2733 case CEE_MUL_OVF_UN:
2734 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2735 ++td.ip;
2736 break;
2737 case CEE_SUB_OVF:
2738 binary_arith_op(&td, MINT_SUB_OVF_I4);
2739 ++td.ip;
2740 break;
2741 case CEE_SUB_OVF_UN:
2742 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2743 ++td.ip;
2744 break;
2745 case CEE_ENDFINALLY:
2746 SIMPLE_OP (td, MINT_ENDFINALLY);
2747 generating_code = 0;
2748 break;
2749 case CEE_LEAVE:
2750 td.sp = td.stack;
2751 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2752 td.ip += 5;
2753 generating_code = 0;
2754 break;
2755 case CEE_LEAVE_S:
2756 td.sp = td.stack;
2757 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2758 td.ip += 2;
2759 generating_code = 0;
2760 break;
2761 case CEE_UNUSED41:
2762 ++td.ip;
2763 switch (*td.ip) {
2764 case CEE_MONO_CALLI_EXTRA_ARG:
2765 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2766 ADD_CODE (&td, MINT_POP);
2767 ADD_CODE (&td, 1);
2768 --td.sp;
2769 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2770 break;
2771 case CEE_MONO_JIT_ICALL_ADDR: {
2772 guint32 token;
2773 gpointer func;
2774 MonoJitICallInfo *info;
2776 token = read32 (td.ip + 1);
2777 td.ip += 5;
2778 func = mono_method_get_wrapper_data (method, token);
2779 info = mono_find_jit_icall_by_addr (func);
2781 ADD_CODE (&td, MINT_LDFTN);
2782 ADD_CODE (&td, get_data_item_index (&td, func));
2783 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2784 break;
2786 case CEE_MONO_ICALL: {
2787 guint32 token;
2788 gpointer func;
2789 MonoJitICallInfo *info;
2791 token = read32 (td.ip + 1);
2792 td.ip += 5;
2793 func = mono_method_get_wrapper_data (method, token);
2794 info = mono_find_jit_icall_by_addr (func);
2795 g_assert (info);
2797 CHECK_STACK (&td, info->sig->param_count);
2798 switch (info->sig->param_count) {
2799 case 0:
2800 if (MONO_TYPE_IS_VOID (info->sig->ret))
2801 ADD_CODE (&td,MINT_ICALL_V_V);
2802 else
2803 ADD_CODE (&td, MINT_ICALL_V_P);
2804 break;
2805 case 1:
2806 if (MONO_TYPE_IS_VOID (info->sig->ret))
2807 ADD_CODE (&td,MINT_ICALL_P_V);
2808 else
2809 ADD_CODE (&td,MINT_ICALL_P_P);
2810 break;
2811 case 2:
2812 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2813 if (info->sig->params [1]->type == MONO_TYPE_I4)
2814 ADD_CODE (&td,MINT_ICALL_PI_V);
2815 else
2816 ADD_CODE (&td,MINT_ICALL_PP_V);
2817 } else {
2818 if (info->sig->params [1]->type == MONO_TYPE_I4)
2819 ADD_CODE (&td,MINT_ICALL_PI_P);
2820 else
2821 ADD_CODE (&td,MINT_ICALL_PP_P);
2823 break;
2824 case 3:
2825 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2826 if (info->sig->params [2]->type == MONO_TYPE_I4)
2827 ADD_CODE (&td,MINT_ICALL_PPI_V);
2828 else
2829 ADD_CODE (&td,MINT_ICALL_PPP_V);
2830 break;
2831 default:
2832 g_assert_not_reached ();
2835 if (func == mono_ftnptr_to_delegate) {
2836 g_error ("TODO: ?");
2838 ADD_CODE(&td, get_data_item_index (&td, func));
2839 td.sp -= info->sig->param_count;
2841 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2842 td.sp ++;
2843 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2845 break;
2847 case CEE_MONO_VTADDR: {
2848 int size;
2849 CHECK_STACK (&td, 1);
2850 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2851 size = mono_class_native_size(td.sp [-1].klass, NULL);
2852 else
2853 size = mono_class_value_size(td.sp [-1].klass, NULL);
2854 size = (size + 7) & ~7;
2855 ADD_CODE(&td, MINT_VTRESULT);
2856 ADD_CODE(&td, 0);
2857 WRITE32(&td, &size);
2858 td.vt_sp -= size;
2859 ++td.ip;
2860 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2861 break;
2863 case CEE_MONO_LDPTR:
2864 case CEE_MONO_CLASSCONST:
2865 token = read32 (td.ip + 1);
2866 td.ip += 5;
2867 ADD_CODE(&td, MINT_MONO_LDPTR);
2868 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2869 td.sp [0].type = STACK_TYPE_I;
2870 ++td.sp;
2871 break;
2872 case CEE_MONO_OBJADDR:
2873 CHECK_STACK (&td, 1);
2874 ++td.ip;
2875 td.sp[-1].type = STACK_TYPE_MP;
2876 /* do nothing? */
2877 break;
2878 case CEE_MONO_NEWOBJ:
2879 token = read32 (td.ip + 1);
2880 td.ip += 5;
2881 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2882 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2883 td.sp [0].type = STACK_TYPE_O;
2884 ++td.sp;
2885 break;
2886 case CEE_MONO_RETOBJ:
2887 CHECK_STACK (&td, 1);
2888 token = read32 (td.ip + 1);
2889 td.ip += 5;
2890 ADD_CODE(&td, MINT_MONO_RETOBJ);
2891 td.sp--;
2893 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2895 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2897 if (td.sp > td.stack)
2898 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2899 break;
2900 case CEE_MONO_LDNATIVEOBJ:
2901 token = read32 (td.ip + 1);
2902 td.ip += 5;
2903 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2904 g_assert(klass->valuetype);
2905 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2906 break;
2907 case CEE_MONO_SAVE_LMF:
2908 case CEE_MONO_RESTORE_LMF:
2909 case CEE_MONO_NOT_TAKEN:
2910 ++td.ip;
2911 break;
2912 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2913 ADD_CODE (&td, MINT_MONO_LDPTR);
2914 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2915 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2916 ++td.ip;
2917 break;
2918 default:
2919 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2921 break;
2922 #if 0
2923 case CEE_PREFIX7:
2924 case CEE_PREFIX6:
2925 case CEE_PREFIX5:
2926 case CEE_PREFIX4:
2927 case CEE_PREFIX3:
2928 case CEE_PREFIX2:
2929 case CEE_PREFIXREF: ves_abort(); break;
2930 #endif
2932 * Note: Exceptions thrown when executing a prefixed opcode need
2933 * to take into account the number of prefix bytes (usually the
2934 * throw point is just (ip - n_prefix_bytes).
2936 case CEE_PREFIX1:
2937 ++td.ip;
2938 switch (*td.ip) {
2939 #if 0
2940 case CEE_ARGLIST: ves_abort(); break;
2941 #endif
2942 case CEE_CEQ:
2943 CHECK_STACK(&td, 2);
2944 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2945 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2946 else
2947 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
2948 --td.sp;
2949 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2950 ++td.ip;
2951 break;
2952 case CEE_CGT:
2953 CHECK_STACK(&td, 2);
2954 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2955 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2956 else
2957 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2958 --td.sp;
2959 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2960 ++td.ip;
2961 break;
2962 case CEE_CGT_UN:
2963 CHECK_STACK(&td, 2);
2964 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2965 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2966 else
2967 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2968 --td.sp;
2969 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2970 ++td.ip;
2971 break;
2972 case CEE_CLT:
2973 CHECK_STACK(&td, 2);
2974 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2975 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2976 else
2977 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
2978 --td.sp;
2979 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2980 ++td.ip;
2981 break;
2982 case CEE_CLT_UN:
2983 CHECK_STACK(&td, 2);
2984 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
2985 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
2986 else
2987 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
2988 --td.sp;
2989 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2990 ++td.ip;
2991 break;
2992 case CEE_LDVIRTFTN: /* fallthrough */
2993 case CEE_LDFTN: {
2994 MonoMethod *m;
2995 if (*td.ip == CEE_LDVIRTFTN) {
2996 CHECK_STACK (&td, 1);
2997 --td.sp;
2999 token = read32 (td.ip + 1);
3000 if (method->wrapper_type != MONO_WRAPPER_NONE)
3001 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3002 else
3003 m = mono_get_method_full (image, token, NULL, generic_context);
3005 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3006 m = mono_marshal_get_synchronized_wrapper (m);
3008 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3009 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
3010 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3011 td.ip += 5;
3012 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
3013 break;
3015 case CEE_LDARG:
3016 load_arg (&td, read16 (td.ip + 1));
3017 td.ip += 3;
3018 break;
3019 case CEE_LDARGA: {
3020 int n = read16 (td.ip + 1);
3021 if (n == 0 && signature->hasthis) {
3022 g_error ("LDTHISA: NOPE");
3023 ADD_CODE(&td, MINT_LDTHISA);
3025 else {
3026 ADD_CODE(&td, MINT_LDARGA);
3027 ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
3029 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3030 td.ip += 3;
3031 break;
3033 case CEE_STARG:
3034 store_arg (&td, read16 (td.ip + 1));
3035 td.ip += 3;
3036 break;
3037 case CEE_LDLOC:
3038 load_local (&td, read16 (td.ip + 1));
3039 td.ip += 3;
3040 break;
3041 case CEE_LDLOCA:
3042 ADD_CODE(&td, MINT_LDLOCA_S);
3043 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
3044 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3045 td.ip += 3;
3046 break;
3047 case CEE_STLOC:
3048 store_local (&td, read16 (td.ip + 1));
3049 td.ip += 3;
3050 break;
3051 case CEE_LOCALLOC:
3052 CHECK_STACK (&td, 1);
3053 #if SIZEOF_VOID_P == 8
3054 if (td.sp [-1].type == STACK_TYPE_I8)
3055 ADD_CODE(&td, MINT_CONV_I4_I8);
3056 #endif
3057 ADD_CODE(&td, MINT_LOCALLOC);
3058 if (td.sp != td.stack + 1)
3059 g_warning("CEE_LOCALLOC: stack not empty");
3060 ++td.ip;
3061 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3062 break;
3063 #if 0
3064 case CEE_UNUSED57: ves_abort(); break;
3065 case CEE_ENDFILTER: ves_abort(); break;
3066 #endif
3067 case CEE_UNALIGNED_:
3068 ++td.ip;
3069 /* FIX: should do something? */;
3070 break;
3071 case CEE_VOLATILE_:
3072 ++td.ip;
3073 /* FIX: should do something? */;
3074 break;
3075 case CEE_TAIL_:
3076 ++td.ip;
3077 /* FIX: should do something? */;
3078 break;
3079 case CEE_INITOBJ:
3080 CHECK_STACK(&td, 1);
3081 token = read32 (td.ip + 1);
3082 klass = mono_class_get_full (image, token, generic_context);
3083 if (klass->valuetype) {
3084 ADD_CODE (&td, MINT_INITOBJ);
3085 i32 = mono_class_value_size (klass, NULL);
3086 WRITE32 (&td, &i32);
3087 } else {
3088 ADD_CODE (&td, MINT_LDNULL);
3089 ADD_CODE (&td, MINT_STIND_REF);
3091 td.ip += 5;
3092 --td.sp;
3093 break;
3094 case CEE_CPBLK:
3095 CHECK_STACK(&td, 3);
3096 /* FIX? convert length to I8? */
3097 ADD_CODE(&td, MINT_CPBLK);
3098 td.sp -= 3;
3099 ++td.ip;
3100 break;
3101 case CEE_READONLY_:
3102 readonly = TRUE;
3103 td.ip += 1;
3104 break;
3105 case CEE_CONSTRAINED_:
3106 token = read32 (td.ip + 1);
3107 constrained_class = mono_class_get_full (image, token, generic_context);
3108 mono_class_init (constrained_class);
3109 td.ip += 5;
3110 break;
3111 case CEE_INITBLK:
3112 CHECK_STACK(&td, 3);
3113 ADD_CODE(&td, MINT_INITBLK);
3114 td.sp -= 3;
3115 break;
3116 #if 0
3117 case CEE_NO_:
3118 /* FIXME: implement */
3119 ip += 2;
3120 break;
3121 #endif
3122 case CEE_RETHROW:
3123 SIMPLE_OP (td, MINT_RETHROW);
3124 generating_code = 0;
3125 break;
3126 case CEE_SIZEOF: {
3127 gint32 size;
3128 token = read32 (td.ip + 1);
3129 td.ip += 5;
3130 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3131 int align;
3132 MonoType *type = mono_type_create_from_typespec (image, token);
3133 size = mono_type_size (type, &align);
3134 } else {
3135 int align;
3136 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
3137 mono_class_init (szclass);
3138 #if 0
3139 if (!szclass->valuetype)
3140 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3141 #endif
3142 size = mono_type_size (&szclass->byval_arg, &align);
3144 ADD_CODE(&td, MINT_LDC_I4);
3145 WRITE32(&td, &size);
3146 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3147 break;
3149 #if 0
3150 case CEE_REFANYTYPE: ves_abort(); break;
3151 #endif
3152 default:
3153 g_error ("transform.c: Unimplemented opcode: 0xFE %02x (%s) at 0x%x\n", *td.ip, mono_opcode_name (256 + *td.ip), td.ip-header->code);
3155 break;
3156 default:
3157 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3160 if (td.new_ip - td.new_code != new_in_start_offset)
3161 td.last_new_ip = td.new_code + new_in_start_offset;
3162 else if (td.is_bb_start [td.in_start - td.il_code])
3163 td.is_bb_start [td.ip - td.il_code] = 1;
3165 td.last_ip = td.in_start;
3168 if (mono_interp_traceopt) {
3169 const guint16 *p = td.new_code;
3170 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3171 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3172 while (p < td.new_ip) {
3173 p = mono_interp_dis_mintop(td.new_code, p);
3174 printf("\n");
3177 g_assert (td.max_stack_height <= (header->max_stack + 1));
3179 rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
3180 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3181 rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
3182 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3183 g_free (td.new_code);
3184 rtm->new_body_start = rtm->code + body_start_offset;
3185 rtm->num_clauses = header->num_clauses;
3186 for (i = 0; i < header->num_clauses; i++) {
3187 MonoExceptionClause *c = rtm->clauses + i;
3188 int end_off = c->try_offset + c->try_len;
3189 c->try_offset = td.in_offsets [c->try_offset];
3190 c->try_len = td.in_offsets [end_off] - c->try_offset;
3191 end_off = c->handler_offset + c->handler_len;
3192 c->handler_offset = td.in_offsets [c->handler_offset];
3193 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3195 rtm->vt_stack_size = td.max_vt_sp;
3196 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3197 rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
3198 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3199 g_free (td.in_offsets);
3200 g_free (td.forward_refs);
3201 for (i = 0; i < header->code_size; ++i)
3202 g_free (td.stack_state [i]);
3203 g_free (td.stack_state);
3204 g_free (td.stack_height);
3205 g_free (td.vt_stack_size);
3206 g_free (td.data_items);
3207 g_free (td.stack);
3208 g_hash_table_destroy (td.data_hash);
3211 static mono_mutex_t calc_section;
3213 void
3214 mono_interp_transform_init (void)
3216 mono_os_mutex_init_recursive(&calc_section);
3219 MonoException *
3220 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3222 int i, align, size, offset;
3223 MonoMethod *method = runtime_method->method;
3224 MonoImage *image = method->klass->image;
3225 MonoMethodHeader *header = mono_method_get_header (method);
3226 MonoMethodSignature *signature = mono_method_signature (method);
3227 register const unsigned char *ip, *end;
3228 const MonoOpcode *opcode;
3229 MonoMethod *m;
3230 MonoClass *class;
3231 MonoDomain *domain = mono_domain_get ();
3232 unsigned char *is_bb_start;
3233 int in;
3234 MonoVTable *method_class_vt;
3235 int backwards;
3236 MonoGenericContext *generic_context = NULL;
3238 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3239 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3240 if (!method_class_vt->initialized) {
3241 jmp_buf env;
3242 MonoInvocation *last_env_frame = context->env_frame;
3243 jmp_buf *old_env = context->current_env;
3245 if (setjmp(env)) {
3246 MonoException *failed = context->env_frame->ex;
3247 context->env_frame->ex = NULL;
3248 context->env_frame = last_env_frame;
3249 context->current_env = old_env;
3250 return failed;
3252 context->env_frame = context->current_frame;
3253 context->current_env = &env;
3254 mono_runtime_class_init (method_class_vt);
3255 context->env_frame = last_env_frame;
3256 context->current_env = old_env;
3259 mono_profiler_method_jit (method); /* sort of... */
3261 if (mono_method_signature (method)->is_inflated)
3262 generic_context = mono_method_get_context (method);
3263 else {
3264 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3265 if (generic_container)
3266 generic_context = &generic_container->context;
3269 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3270 MonoMethod *nm = NULL;
3271 mono_os_mutex_lock(&calc_section);
3272 if (runtime_method->transformed) {
3273 mono_os_mutex_unlock(&calc_section);
3274 g_error ("FIXME: no jit info?");
3275 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3276 return NULL;
3279 /* assumes all internal calls with an array this are built in... */
3280 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3281 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3282 signature = mono_method_signature (nm);
3283 } else {
3284 const char *name = method->name;
3285 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3286 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3287 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3288 g_assert (mi);
3289 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3290 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3291 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3292 nm = mono_marshal_get_delegate_invoke (method, NULL);
3293 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3294 nm = mono_marshal_get_delegate_begin_invoke (method);
3295 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3296 nm = mono_marshal_get_delegate_end_invoke (method);
3299 if (nm == NULL) {
3300 runtime_method->code = g_malloc(sizeof(short));
3301 runtime_method->code[0] = MINT_CALLRUN;
3304 if (nm == NULL) {
3305 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3306 runtime_method->alloca_size = runtime_method->stack_size;
3307 runtime_method->transformed = TRUE;
3308 mono_os_mutex_unlock(&calc_section);
3309 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3310 return NULL;
3312 method = nm;
3313 header = mono_method_get_header (nm);
3314 mono_os_mutex_unlock(&calc_section);
3316 g_assert ((signature->param_count + signature->hasthis) < 1000);
3317 g_assert (header->max_stack < 10000);
3318 /* intern the strings in the method. */
3319 ip = header->code;
3320 end = ip + header->code_size;
3322 is_bb_start = g_malloc0(header->code_size);
3323 is_bb_start [0] = 1;
3324 while (ip < end) {
3325 in = *ip;
3326 if (in == 0xfe) {
3327 ip++;
3328 in = *ip + 256;
3330 else if (in == 0xf0) {
3331 ip++;
3332 in = *ip + MONO_CEE_MONO_ICALL;
3334 opcode = &mono_opcodes [in];
3335 switch (opcode->argument) {
3336 case MonoInlineNone:
3337 ++ip;
3338 break;
3339 case MonoInlineString:
3340 if (method->wrapper_type == MONO_WRAPPER_NONE)
3341 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3342 ip += 5;
3343 break;
3344 case MonoInlineType:
3345 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3346 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3347 mono_class_init (class);
3348 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3349 #if 0
3350 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3351 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3352 mono_class_vtable (domain, class);
3353 #endif
3355 ip += 5;
3356 break;
3357 case MonoInlineMethod:
3358 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3359 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3360 if (m == NULL) {
3361 g_free (is_bb_start);
3362 g_error ("FIXME: where to get method and class string?");
3363 return NULL;
3364 // return mono_get_exception_missing_method ();
3366 mono_class_init (m->klass);
3367 if (!mono_class_is_interface (m->klass))
3368 mono_class_vtable (domain, m->klass);
3370 ip += 5;
3371 break;
3372 case MonoInlineField:
3373 case MonoInlineSig:
3374 case MonoInlineI:
3375 case MonoInlineTok:
3376 case MonoShortInlineR:
3377 ip += 5;
3378 break;
3379 case MonoInlineBrTarget:
3380 offset = read32 (ip + 1);
3381 ip += 5;
3382 backwards = offset < 0;
3383 offset += ip - header->code;
3384 g_assert (offset >= 0 && offset < header->code_size);
3385 is_bb_start [offset] |= backwards ? 2 : 1;
3386 break;
3387 case MonoShortInlineBrTarget:
3388 offset = ((gint8 *)ip) [1];
3389 ip += 2;
3390 backwards = offset < 0;
3391 offset += ip - header->code;
3392 g_assert (offset >= 0 && offset < header->code_size);
3393 is_bb_start [offset] |= backwards ? 2 : 1;
3394 break;
3395 case MonoInlineVar:
3396 ip += 3;
3397 break;
3398 case MonoShortInlineVar:
3399 case MonoShortInlineI:
3400 ip += 2;
3401 break;
3402 case MonoInlineSwitch: {
3403 guint32 n;
3404 const unsigned char *next_ip;
3405 ++ip;
3406 n = read32 (ip);
3407 ip += 4;
3408 next_ip = ip + 4 * n;
3409 for (i = 0; i < n; i++) {
3410 offset = read32 (ip);
3411 backwards = offset < 0;
3412 offset += next_ip - header->code;
3413 g_assert (offset >= 0 && offset < header->code_size);
3414 is_bb_start [offset] |= backwards ? 2 : 1;
3415 ip += 4;
3417 break;
3419 case MonoInlineR:
3420 case MonoInlineI8:
3421 ip += 9;
3422 break;
3423 default:
3424 g_assert_not_reached ();
3427 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3429 /* the rest needs to be locked so it is only done once */
3430 mono_os_mutex_lock(&calc_section);
3431 if (runtime_method->transformed) {
3432 mono_os_mutex_unlock(&calc_section);
3433 g_free (is_bb_start);
3434 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3435 return NULL;
3438 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3439 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3440 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3441 offset = 0;
3442 for (i = 0; i < header->num_locals; ++i) {
3443 size = mono_type_size (header->locals [i], &align);
3444 offset += align - 1;
3445 offset &= ~(align - 1);
3446 runtime_method->local_offsets [i] = offset;
3447 offset += size;
3449 offset = (offset + 7) & ~7;
3450 runtime_method->locals_size = offset;
3451 g_assert (runtime_method->locals_size < 65536);
3452 offset = 0;
3453 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3455 if (signature->hasthis) {
3456 g_assert (!signature->pinvoke);
3457 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3458 offset += align - 1;
3459 offset &= ~(align - 1);
3460 runtime_method->arg_offsets [0] = offset;
3461 offset += size;
3464 for (i = 0; i < signature->param_count; ++i) {
3465 if (signature->pinvoke) {
3466 guint32 dummy;
3467 size = mono_type_native_stack_size (signature->params [i], &dummy);
3468 align = 8;
3470 else
3471 size = mono_type_stack_size (signature->params [i], &align);
3472 offset += align - 1;
3473 offset &= ~(align - 1);
3474 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3475 offset += size;
3477 offset = (offset + 7) & ~7;
3478 runtime_method->args_size = offset;
3479 g_assert (runtime_method->args_size < 10000);
3481 generate (method, runtime_method, is_bb_start, generic_context);
3483 g_free (is_bb_start);
3485 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3486 runtime_method->transformed = TRUE;
3487 mono_os_mutex_unlock(&calc_section);
3489 return NULL;