/
transform.c
3283 lines (3153 loc) · 90.8 KB
/
transform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* transform CIL into different opcodes for more
* efficient interpretation
*
* Written by Bernie Solomon (bernard@ugsolutions.com)
* Copyright (c) 2004.
*/
#include <string.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/tabledefs.h>
#include <mono/mini/mini.h>
#include "mintops.h"
#include "interp.h"
// TODO: export from marshal.c
MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
#define DEBUG 0
typedef struct
{
MonoClass *klass;
unsigned char type;
unsigned char flags;
} StackInfo;
typedef struct
{
MonoMethod *method;
MonoMethodHeader *header;
RuntimeMethod *rtm;
const unsigned char *il_code;
const unsigned char *ip;
const unsigned char *last_ip;
const unsigned char *in_start;
int code_size;
int *in_offsets;
int *forward_refs;
StackInfo **stack_state;
int *stack_height;
int *vt_stack_size;
unsigned char *is_bb_start;
unsigned short *new_code;
unsigned short *new_code_end;
unsigned short *new_ip;
unsigned short *last_new_ip;
unsigned int max_code_size;
StackInfo *stack;
StackInfo *sp;
unsigned int max_stack_height;
unsigned int vt_sp;
unsigned int max_vt_sp;
int n_data_items;
int max_data_items;
void **data_items;
GHashTable *data_hash;
} TransformData;
#define MINT_TYPE_I1 0
#define MINT_TYPE_U1 1
#define MINT_TYPE_I2 2
#define MINT_TYPE_U2 3
#define MINT_TYPE_I4 4
#define MINT_TYPE_I8 5
#define MINT_TYPE_R4 6
#define MINT_TYPE_R8 7
#define MINT_TYPE_O 8
#define MINT_TYPE_P 9
#define MINT_TYPE_VT 10
#define STACK_TYPE_I4 0
#define STACK_TYPE_I8 1
#define STACK_TYPE_R8 2
#define STACK_TYPE_O 3
#define STACK_TYPE_VT 4
#define STACK_TYPE_MP 5
#define STACK_TYPE_F 6
static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
#if SIZEOF_VOID_P == 8
#define STACK_TYPE_I STACK_TYPE_I8
#else
#define STACK_TYPE_I STACK_TYPE_I4
#endif
static int stack_type [] = {
STACK_TYPE_I4, /*I1*/
STACK_TYPE_I4, /*U1*/
STACK_TYPE_I4, /*I2*/
STACK_TYPE_I4, /*U2*/
STACK_TYPE_I4, /*I4*/
STACK_TYPE_I8, /*I8*/
STACK_TYPE_R8, /*R4*/
STACK_TYPE_R8, /*R8*/
STACK_TYPE_O, /*O*/
STACK_TYPE_MP, /*P*/
STACK_TYPE_VT
};
static void
grow_code (TransformData *td)
{
unsigned int old_ip_offset = td->new_ip - td->new_code;
unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
g_assert (old_ip_offset <= td->max_code_size);
td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
td->new_code_end = td->new_code + td->max_code_size;
td->new_ip = td->new_code + old_ip_offset;
td->last_new_ip = td->new_code + old_last_ip_offset;
}
#define ENSURE_CODE(td, n) \
do { \
if ((td)->new_ip + (n) > (td)->new_code_end) \
grow_code (td); \
} while (0)
#define ADD_CODE(td, n) \
do { \
if ((td)->new_ip == (td)->new_code_end) \
grow_code (td); \
*(td)->new_ip++ = (n); \
} while (0)
#define CHECK_STACK(td, n) \
do { \
int stack_size = (td)->sp - (td)->stack; \
if (stack_size < (n)) \
g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
(td)->method->klass->name, (td)->method->name, \
stack_size, n, (td)->ip - (td)->il_code); \
} while (0)
#define ENSURE_I4(td, sp_off) \
do { \
if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
} while (0)
static void
handle_branch(TransformData *td, int short_op, int long_op, int offset)
{
int shorten_branch = 0;
int target = td->ip + offset - td->il_code;
if (target < 0 || target >= td->code_size)
g_assert_not_reached ();
if (offset > 0 && td->stack_height [target] < 0) {
td->stack_height [target] = td->sp - td->stack;
if (td->stack_height [target] > 0)
td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
td->vt_stack_size [target] = td->vt_sp;
}
if (offset < 0) {
offset = td->in_offsets [target] - (td->new_ip - td->new_code);
if (offset >= -32768) {
shorten_branch = 1;
}
} else {
int prev = td->forward_refs [target];
td->forward_refs [td->ip - td->il_code] = prev;
td->forward_refs [target] = td->ip - td->il_code;
offset = 0;
if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
shorten_branch = 1;
}
if (shorten_branch) {
ADD_CODE(td, short_op);
ADD_CODE(td, offset);
} else {
ADD_CODE(td, long_op);
ADD_CODE(td, * (unsigned short *)(&offset));
ADD_CODE(td, * ((unsigned short *)&offset + 1));
}
}
static void
one_arg_branch(TransformData *td, int mint_op, int offset)
{
int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
int long_op = mint_op + type - STACK_TYPE_I4;
int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
CHECK_STACK(td, 1);
--td->sp;
handle_branch (td, short_op, long_op, offset);
}
static void
two_arg_branch(TransformData *td, int mint_op, int offset)
{
int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
int long_op = mint_op + type1 - STACK_TYPE_I4;
int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
CHECK_STACK(td, 2);
if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
ADD_CODE(td, MINT_CONV_I8_I4);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
ADD_CODE(td, MINT_CONV_I8_I4_SP);
td->in_offsets [td->ip - td->il_code]++;
} else if (type1 != type2) {
g_warning("%s.%s: branch type mismatch %d %d",
td->method->klass->name, td->method->name,
td->sp [-1].type, td->sp [-2].type);
}
td->sp -= 2;
handle_branch (td, short_op, long_op, offset);
}
static void
unary_arith_op(TransformData *td, int mint_op)
{
int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
CHECK_STACK(td, 1);
ADD_CODE(td, op);
}
static void
binary_arith_op(TransformData *td, int mint_op)
{
int type1 = td->sp [-2].type;
int type2 = td->sp [-1].type;
int op;
#if SIZEOF_VOID_P == 8
if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
ADD_CODE(td, MINT_CONV_I8_I4);
type2 = STACK_TYPE_I8;
}
if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
ADD_CODE(td, MINT_CONV_I8_I4_SP);
type1 = STACK_TYPE_I8;
td->sp [-2].type = STACK_TYPE_I8;
}
#endif
if (type1 == STACK_TYPE_MP)
type1 = STACK_TYPE_I;
if (type2 == STACK_TYPE_MP)
type2 = STACK_TYPE_I;
if (type1 != type2) {
g_warning("%s.%s: %04x arith type mismatch %s %d %d",
td->method->klass->name, td->method->name,
td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
}
op = mint_op + type1 - STACK_TYPE_I4;
CHECK_STACK(td, 2);
ADD_CODE(td, op);
--td->sp;
}
static void
binary_int_op(TransformData *td, int mint_op)
{
int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
CHECK_STACK(td, 2);
if (td->sp [-1].type != td->sp [-2].type)
g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
ADD_CODE(td, op);
--td->sp;
}
static void
shift_op(TransformData *td, int mint_op)
{
int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
CHECK_STACK(td, 2);
if (td->sp [-1].type != STACK_TYPE_I4) {
g_warning("%s.%s: shift type mismatch %d",
td->method->klass->name, td->method->name,
td->sp [-2].type);
}
ADD_CODE(td, op);
--td->sp;
}
static int
mint_type(MonoType *type)
{
if (type->byref)
return MINT_TYPE_P;
enum_type:
switch (type->type) {
case MONO_TYPE_I1:
return MINT_TYPE_I1;
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
return MINT_TYPE_U1;
case MONO_TYPE_I2:
return MINT_TYPE_I2;
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
return MINT_TYPE_U2;
case MONO_TYPE_I4:
case MONO_TYPE_U4:
return MINT_TYPE_I4;
case MONO_TYPE_I:
case MONO_TYPE_U:
#if SIZEOF_VOID_P == 4
return MINT_TYPE_I4;
#else
return MINT_TYPE_I8;
#endif
case MONO_TYPE_PTR:
return MINT_TYPE_P;
case MONO_TYPE_R4:
return MINT_TYPE_R4;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
return MINT_TYPE_I8;
case MONO_TYPE_R8:
return MINT_TYPE_R8;
case MONO_TYPE_STRING:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_CLASS:
case MONO_TYPE_OBJECT:
case MONO_TYPE_ARRAY:
return MINT_TYPE_O;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
type = mono_class_enum_basetype (type->data.klass);
goto enum_type;
} else
return MINT_TYPE_VT;
case MONO_TYPE_GENERICINST:
type = &type->data.generic_class->container_class->byval_arg;
goto enum_type;
default:
g_warning ("got type 0x%02x", type->type);
g_assert_not_reached ();
}
return -1;
}
static int
can_store (int stack_type, int var_type)
{
if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
stack_type = STACK_TYPE_I;
if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
var_type = STACK_TYPE_I;
return stack_type == var_type;
}
#define SET_SIMPLE_TYPE(s, ty) \
do { \
(s)->type = (ty); \
(s)->flags = 0; \
(s)->klass = NULL; \
} while (0)
#define SET_TYPE(s, ty, k) \
do { \
(s)->type = (ty); \
(s)->flags = 0; \
(s)->klass = k; \
} while (0)
#define PUSH_SIMPLE_TYPE(td, ty) \
do { \
int sp_height; \
(td)->sp++; \
sp_height = (td)->sp - (td)->stack; \
if (sp_height > (td)->max_stack_height) \
(td)->max_stack_height = sp_height; \
SET_SIMPLE_TYPE((td)->sp - 1, ty); \
} while (0)
#define PUSH_TYPE(td, ty, k) \
do { \
int sp_height; \
(td)->sp++; \
sp_height = (td)->sp - (td)->stack; \
if (sp_height > (td)->max_stack_height) \
(td)->max_stack_height = sp_height; \
SET_TYPE((td)->sp - 1, ty, k); \
} while (0)
#define PUSH_VT(td, size) \
do { \
(td)->vt_sp += ((size) + 7) & ~7; \
if ((td)->vt_sp > (td)->max_vt_sp) \
(td)->max_vt_sp = (td)->vt_sp; \
} while (0)
#define POP_VT(td, size) \
do { \
(td)->vt_sp -= ((size) + 7) & ~7; \
} while (0)
#if NO_UNALIGNED_ACCESS
#define WRITE32(td, v) \
do { \
ENSURE_CODE(td, 2); \
* (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
* ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
(td)->new_ip += 2; \
} while (0)
#define WRITE64(td, v) \
do { \
ENSURE_CODE(td, 4); \
* (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
* ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
* ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
* ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
(td)->new_ip += 4; \
} while (0)
#else
#define WRITE32(td, v) \
do { \
ENSURE_CODE(td, 2); \
* (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
(td)->new_ip += 2; \
} while (0)
#define WRITE64(td, v) \
do { \
ENSURE_CODE(td, 4); \
* (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
(td)->new_ip += 4; \
} while (0)
#endif
static void
load_arg(TransformData *td, int n)
{
int mt;
MonoClass *klass = NULL;
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = &td->method->klass->byval_arg;
else
type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
mt = mint_type (type);
if (mt == MINT_TYPE_VT) {
gint32 size;
klass = mono_class_from_mono_type (type);
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
if (hasthis && n == 0) {
mt = MINT_TYPE_P;
ADD_CODE (td, MINT_LDARG_P);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
klass = NULL;
} else {
PUSH_VT (td, size);
ADD_CODE (td, MINT_LDARG_VT);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
WRITE32 (td, &size);
}
} else {
if (hasthis && n == 0) {
mt = MINT_TYPE_P;
ADD_CODE (td, MINT_LDARG_P);
ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
klass = NULL;
} else {
ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type (type);
}
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
store_arg(TransformData *td, int n)
{
int mt;
CHECK_STACK (td, 1);
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = &td->method->klass->byval_arg;
else
type = mono_method_signature (td->method)->params [n - !!hasthis];
mt = mint_type (type);
if (mt == MINT_TYPE_VT) {
gint32 size;
g_error ("data.klass");
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (type->data.klass, NULL);
else
size = mono_class_value_size (type->data.klass, NULL);
ADD_CODE(td, MINT_STARG_VT);
ADD_CODE(td, n);
WRITE32(td, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, td->rtm->arg_offsets [n]);
}
--td->sp;
}
static void
store_inarg(TransformData *td, int n)
{
MonoType *type;
gboolean hasthis = mono_method_signature (td->method)->hasthis;
if (hasthis && n == 0)
type = &td->method->klass->byval_arg;
else
type = mono_method_signature (td->method)->params [n - !!hasthis];
int mt = mint_type (type);
if (hasthis && n == 0) {
ADD_CODE (td, MINT_STINARG_P);
ADD_CODE (td, n);
return;
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (type);
gint32 size;
if (mono_method_signature (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
ADD_CODE(td, MINT_STINARG_VT);
ADD_CODE(td, n);
WRITE32(td, &size);
} else {
ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, n);
}
}
static void
load_local(TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int mt = mint_type (type);
int offset = td->rtm->local_offsets [n];
MonoClass *klass = NULL;
if (mt == MINT_TYPE_VT) {
klass = mono_class_from_mono_type (type);
gint32 size = mono_class_value_size (klass, NULL);
PUSH_VT(td, size);
ADD_CODE(td, MINT_LDLOC_VT);
ADD_CODE(td, offset); /*FIX for large offset */
WRITE32(td, &size);
} else {
g_assert (mt < MINT_TYPE_VT);
if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
td->last_new_ip [0] = MINT_STLOC_NP_I4;
} else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
td->last_new_ip [0] = MINT_STLOC_NP_O;
} else {
ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, offset); /*FIX for large offset */
}
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type (type);
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
store_local(TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int mt = mint_type (type);
int offset = td->rtm->local_offsets [n];
CHECK_STACK (td, 1);
#if SIZEOF_VOID_P == 8
if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
ADD_CODE(td, MINT_CONV_I8_I4);
td->sp [-1].type = STACK_TYPE_I8;
}
#endif
if (!can_store(td->sp [-1].type, stack_type [mt])) {
g_warning("%s.%s: Store local stack type mismatch %d %d",
td->method->klass->name, td->method->name,
stack_type [mt], td->sp [-1].type);
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type (type);
gint32 size = mono_class_value_size (klass, NULL);
ADD_CODE(td, MINT_STLOC_VT);
ADD_CODE(td, offset); /*FIX for large offset */
WRITE32(td, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
g_assert (mt < MINT_TYPE_VT);
ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
ADD_CODE(td, offset); /*FIX for large offset */
}
--td->sp;
}
#define SIMPLE_OP(td, op) \
do { \
ADD_CODE(&td, op); \
++td.ip; \
} while (0)
static guint16
get_data_item_index (TransformData *td, void *ptr)
{
gpointer p = g_hash_table_lookup (td->data_hash, ptr);
guint index;
if (p != NULL)
return GPOINTER_TO_UINT (p) - 1;
if (td->max_data_items == td->n_data_items) {
td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
}
index = td->n_data_items;
td->data_items [index] = ptr;
++td->n_data_items;
g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
return index;
}
static void
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)
{
MonoImage *image = method->klass->image;
MonoMethodSignature *csignature;
MonoError error;
int virtual = *td->ip == CEE_CALLVIRT;
int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
int i;
guint32 vt_stack_used = 0;
guint32 vt_res_size = 0;
int op = -1;
int native = 0;
int is_void = 0;
guint32 token = read32 (td->ip + 1);
if (target_method == NULL) {
if (calli) {
CHECK_STACK(td, 1);
native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
--td->sp;
if (method->wrapper_type != MONO_WRAPPER_NONE)
csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
else
csignature = mono_metadata_parse_signature (image, token);
target_method = NULL;
} else {
if (method->wrapper_type == MONO_WRAPPER_NONE)
target_method = mono_get_method_full (image, token, NULL, generic_context);
else
target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
csignature = mono_method_signature (target_method);
if (target_method->klass == mono_defaults.string_class) {
if (target_method->name [0] == 'g') {
if (strcmp (target_method->name, "get_Chars") == 0)
op = MINT_GETCHR;
else if (strcmp (target_method->name, "get_Length") == 0)
op = MINT_STRLEN;
}
} else if (target_method->klass == mono_defaults.array_class) {
if (strcmp (target_method->name, "get_Rank") == 0)
op = MINT_ARRAY_RANK;
else if (strcmp (target_method->name, "get_Length") == 0)
op = MINT_LDLEN;
}
}
} else {
csignature = mono_method_signature (target_method);
}
if (constrained_class) {
if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
/* Use the corresponding method from the base type to avoid boxing */
MonoType *base_type = mono_class_enum_basetype (constrained_class);
g_assert (base_type);
constrained_class = mono_class_from_mono_type (base_type);
target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
g_assert (target_method);
}
}
if (constrained_class) {
mono_class_setup_vtable (constrained_class);
#if DEBUG_INTERP
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);
#endif
target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
#if DEBUG_INTERP
g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
#endif
mono_error_cleanup (&error); /* FIXME: don't swallow the error */
mono_class_setup_vtable (target_method->klass);
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)) {
ADD_CODE (td, MINT_BOX);
ADD_CODE (td, get_data_item_index (td, constrained_class));
ADD_CODE (td, csignature->param_count);
} else if (!constrained_class->valuetype) {
/* managed pointer on the stack, we need to deref that puppy */
ADD_CODE (td, MINT_LDIND_I);
ADD_CODE (td, csignature->param_count);
} else {
g_assert (target_method->klass->valuetype);
virtual = FALSE;
}
}
if (target_method)
mono_class_init (target_method->klass);
CHECK_STACK (td, csignature->param_count + csignature->hasthis);
if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
(target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
(target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
MonoMethodHeader *mheader = mono_method_get_header (target_method);
if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
int offset;
if (mono_interp_traceopt)
g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
for (i = csignature->param_count - 1; i >= 0; --i)
store_arg (td, i + csignature->hasthis);
if (csignature->hasthis) {
g_error ("STTHIS removal");
// ADD_CODE(td, MINT_STTHIS);
--td->sp;
}
ADD_CODE(td, MINT_BR_S);
offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
ADD_CODE(td, offset);
if (!is_bb_start [td->ip + 5 - td->il_code])
++td->ip; /* gobble the CEE_RET if it isn't branched to */
td->ip += 5;
return;
} else {
/* mheader might not exist if this is a delegate invoc, etc */
if (mheader && *mheader->code == CEE_RET && called_inited) {
if (mono_interp_traceopt)
g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
for (i = 0; i < csignature->param_count; i++)
ADD_CODE(td, MINT_POP); /*FIX: vt */
if (csignature->hasthis) {
if (virtual)
ADD_CODE(td, MINT_CKNULL);
ADD_CODE(td, MINT_POP);
}
td->sp -= csignature->param_count + csignature->hasthis;
td->ip += 5;
return;
}
}
}
if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
target_method = mono_marshal_get_synchronized_wrapper (target_method);
}
g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
td->sp -= csignature->param_count + !!csignature->hasthis;
for (i = 0; i < csignature->param_count; ++i) {
if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
gint32 size;
MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
size = (size + 7) & ~7;
vt_stack_used += size;
}
}
/* need to handle typedbyref ... */
if (csignature->ret->type != MONO_TYPE_VOID) {
int mt = mint_type(csignature->ret);
MonoClass *klass = mono_class_from_mono_type (csignature->ret);
if (mt == MINT_TYPE_VT) {
if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
vt_res_size = mono_class_native_size (klass, NULL);
else
vt_res_size = mono_class_value_size (klass, NULL);
PUSH_VT(td, vt_res_size);
}
PUSH_TYPE(td, stack_type[mt], klass);
} else
is_void = TRUE;
if (op >= 0) {
ADD_CODE(td, op);
#if SIZEOF_VOID_P == 8
if (op == MINT_LDLEN)
ADD_CODE(td, MINT_CONV_I4_I8);
#endif
} else {
if (calli)
ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
else if (virtual)
ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
else
ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
if (calli) {
ADD_CODE(td, get_data_item_index (td, (void *)csignature));
} else {
ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
mono_error_cleanup (&error); /* FIXME: don't swallow the error */
}
}
td->ip += 5;
if (vt_stack_used != 0 || vt_res_size != 0) {
ADD_CODE(td, MINT_VTRESULT);
ADD_CODE(td, vt_res_size);
WRITE32(td, &vt_stack_used);
td->vt_sp -= vt_stack_used;
}
}
static void
generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start)
{
MonoMethodHeader *header = mono_method_get_header (method);
MonoMethodSignature *signature = mono_method_signature (method);
MonoImage *image = method->klass->image;
MonoDomain *domain = mono_domain_get ();
MonoGenericContext *generic_context = NULL;
MonoClass *constrained_class = NULL;
MonoError error;
int offset, mt;
int i;
int i32;
MonoClass *klass;
MonoClassField *field;
const unsigned char *end;
int new_in_start_offset;
int body_start_offset;
int target;
guint32 token;
TransformData td;
int generating_code = 1;
if (mono_method_signature (method)->is_inflated)
generic_context = &((MonoMethodInflated *) method)->context;
memset(&td, 0, sizeof(td));
td.method = method;
td.rtm = rtm;
td.is_bb_start = is_bb_start;
td.il_code = header->code;
td.code_size = header->code_size;
td.header = header;
td.max_code_size = td.code_size;
td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
td.new_code_end = td.new_code + td.max_code_size;
td.in_offsets = g_malloc0(header->code_size * sizeof(int));
td.forward_refs = g_malloc(header->code_size * sizeof(int));
td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
td.stack_height = g_malloc(header->code_size * sizeof(int));
td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
td.n_data_items = 0;
td.max_data_items = 0;
td.data_items = NULL;
td.data_hash = g_hash_table_new (NULL, NULL);
rtm->data_items = td.data_items;
for (i = 0; i < header->code_size; i++) {
td.forward_refs [i] = -1;
td.stack_height [i] = -1;
}
td.new_ip = td.new_code;
td.last_new_ip = NULL;
td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
td.sp = td.stack;
td.max_stack_height = 0;
for (i = 0; i < header->num_clauses; i++) {
MonoExceptionClause *c = header->clauses + i;
td.stack_height [c->handler_offset] = 0;
td.vt_stack_size [c->handler_offset] = 0;
td.is_bb_start [c->handler_offset] = 1;
if (c->flags == MONO_EXCEPTION_CLAUSE_NONE) {
td.stack_height [c->handler_offset] = 1;
td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
}
}
td.ip = header->code;
end = td.ip + header->code_size;
if (mono_interp_traceopt) {
char *tmp = mono_disasm_code (NULL, method, td.ip, end);
char *name = mono_method_full_name (method, TRUE);
g_print ("Method %s, original code:\n", name);
g_print ("%s\n", tmp);
g_free (tmp);
g_free (name);
}
if (signature->hasthis)
store_inarg (&td, 0);
for (i = 0; i < signature->param_count; i++)
store_inarg (&td, i + !!signature->hasthis);
body_start_offset = td.new_ip - td.new_code;
for (i = 0; i < header->num_locals; i++) {
int mt = mint_type(header->locals [i]);
if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O) {
ADD_CODE(&td, MINT_INITLOCALS);
break;
}
}
while (td.ip < end) {
int in_offset;
g_assert (td.sp >= td.stack);
g_assert (td.vt_sp < 0x10000000);
in_offset = td.ip - header->code;
td.in_offsets [in_offset] = td.new_ip - td.new_code;
new_in_start_offset = td.new_ip - td.new_code;
td.in_start = td.ip;
while (td.forward_refs [in_offset] >= 0) {
int j = td.forward_refs [in_offset];
int slot;
td.forward_refs [in_offset] = td.forward_refs [j];
if (td.in_offsets [j] < 0) {
int old_switch_offset = -td.in_offsets [j];
int new_switch_offset = td.in_offsets [old_switch_offset];
int switch_case = (j - old_switch_offset - 5) / 4;
int n_cases = read32 (header->code + old_switch_offset + 1);
offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
slot = new_switch_offset + 3 + 2 * switch_case;
td.new_code [slot] = * (unsigned short *)(&offset);
td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
} else {
int op = td.new_code [td.in_offsets [j]];
if (mono_interp_opargtype [op] == MintOpShortBranch) {
offset = (td.new_ip - td.new_code) - td.in_offsets [j];
g_assert (offset <= 32767);
slot = td.in_offsets [j] + 1;
td.new_code [slot] = offset;
} else {
offset = (td.new_ip - td.new_code) - td.in_offsets [j];
slot = td.in_offsets [j] + 1;
td.new_code [slot] = * (unsigned short *)(&offset);
td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
}
}
}
if (td.stack_height [in_offset] >= 0) {
g_assert (is_bb_start [in_offset]);
if (td.stack_height [in_offset] > 0)
memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
td.sp = td.stack + td.stack_height [in_offset];
td.vt_sp = td.vt_stack_size [in_offset];
}
if (is_bb_start [in_offset]) {
generating_code = 1;
}
if (!generating_code) {
while (td.ip < end && !is_bb_start [td.ip - td.il_code])
++td.ip;
continue;
}
if (mono_interp_traceopt > 1) {
printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
td.ip - td.il_code,
td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
(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) : "",
td.vt_sp, td.max_vt_sp);
}
switch (*td.ip) {
case CEE_NOP:
/* lose it */