/
transform.c
6232 lines (5739 loc) · 188 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
/**
* \file
* transform CIL into different opcodes for more
* efficient interpretation
*
* Written by Bernie Solomon (bernard@ugsolutions.com)
* Copyright (c) 2004.
*/
#include "config.h"
#include <string.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/exception-internals.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/metadata/seq-points-data.h>
#include <mono/metadata/mono-basic-block.h>
#include <mono/metadata/abi-details.h>
#include <mono/metadata/reflection-internals.h>
#include <mono/utils/unlocked.h>
#include <mono/mini/mini.h>
#include <mono/mini/mini-runtime.h>
#include "mintops.h"
#include "interp-internals.h"
#include "interp.h"
#define INTERP_INST_FLAG_SEQ_POINT_NONEMPTY_STACK 1
#define INTERP_INST_FLAG_SEQ_POINT_METHOD_ENTRY 2
#define INTERP_INST_FLAG_SEQ_POINT_METHOD_EXIT 4
#define INTERP_INST_FLAG_SEQ_POINT_NESTED_CALL 8
MonoInterpStats mono_interp_stats;
#define DEBUG 0
typedef struct InterpInst InterpInst;
typedef struct
{
MonoClass *klass;
unsigned char type;
unsigned char flags;
} StackInfo;
struct InterpInst {
guint16 opcode;
InterpInst *next, *prev;
// If this is -1, this instruction is not logically associated with an IL offset, it is
// part of the IL instruction associated with the previous interp instruction.
int il_offset;
guint32 flags;
guint16 data [MONO_ZERO_LEN_ARRAY];
};
typedef struct {
guint8 *ip;
GSList *preds;
GSList *seq_points;
SeqPoint *last_seq_point;
// This will hold a list of last sequence points of incoming basic blocks
SeqPoint **pred_seq_points;
guint num_pred_seq_points;
} InterpBasicBlock;
typedef enum {
RELOC_SHORT_BRANCH,
RELOC_LONG_BRANCH,
RELOC_SWITCH
} RelocType;
typedef struct {
RelocType type;
/* In the interpreter IR */
int offset;
/* In the IL code */
int target;
} Reloc;
typedef struct
{
MonoMethod *method;
MonoMethod *inlined_method;
MonoMethodHeader *header;
InterpMethod *rtm;
const unsigned char *il_code;
const unsigned char *ip;
const unsigned char *in_start;
InterpInst *last_ins, *first_ins;
int code_size;
int *in_offsets;
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 int max_code_size;
StackInfo *stack;
StackInfo *sp;
unsigned int max_stack_height;
unsigned int stack_capacity;
unsigned int vt_sp;
unsigned int max_vt_sp;
unsigned int total_locals_size;
int n_data_items;
int max_data_items;
void **data_items;
GHashTable *data_hash;
int *clause_indexes;
gboolean gen_sdb_seq_points;
GPtrArray *seq_points;
InterpBasicBlock **offset_to_bb;
InterpBasicBlock *entry_bb;
MonoMemPool *mempool;
GList *basic_blocks;
GPtrArray *relocs;
gboolean verbose_level;
GArray *line_numbers;
} TransformData;
#define STACK_TYPE_I4 0
#define STACK_TYPE_I8 1
#define STACK_TYPE_R4 2
#define STACK_TYPE_R8 3
#define STACK_TYPE_O 4
#define STACK_TYPE_VT 5
#define STACK_TYPE_MP 6
#define STACK_TYPE_F 7
static const char *stack_type_string [] = { "I4", "I8", "R4", "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_R4, /*R4*/
STACK_TYPE_R8, /*R8*/
STACK_TYPE_O, /*O*/
STACK_TYPE_MP, /*P*/
STACK_TYPE_VT
};
#if SIZEOF_VOID_P == 8
#define MINT_NEG_P MINT_NEG_I8
#define MINT_NOT_P MINT_NOT_I8
#define MINT_NEG_FP MINT_NEG_R8
#define MINT_ADD_P MINT_ADD_I8
#define MINT_SUB_P MINT_SUB_I8
#define MINT_MUL_P MINT_MUL_I8
#define MINT_DIV_P MINT_DIV_I8
#define MINT_DIV_UN_P MINT_DIV_UN_I8
#define MINT_REM_P MINT_REM_I8
#define MINT_REM_UN_P MINT_REM_UN_I8
#define MINT_AND_P MINT_AND_I8
#define MINT_OR_P MINT_OR_I8
#define MINT_XOR_P MINT_XOR_I8
#define MINT_SHL_P MINT_SHL_I8
#define MINT_SHR_P MINT_SHR_I8
#define MINT_SHR_UN_P MINT_SHR_UN_I8
#define MINT_CEQ_P MINT_CEQ_I8
#define MINT_CNE_P MINT_CNE_I8
#define MINT_CLT_P MINT_CLT_I8
#define MINT_CLT_UN_P MINT_CLT_UN_I8
#define MINT_CGT_P MINT_CGT_I8
#define MINT_CGT_UN_P MINT_CGT_UN_I8
#define MINT_CLE_P MINT_CLE_I8
#define MINT_CLE_UN_P MINT_CLE_UN_I8
#define MINT_CGE_P MINT_CGE_I8
#define MINT_CGE_UN_P MINT_CGE_UN_I8
#define MINT_ADD_FP MINT_ADD_R8
#define MINT_SUB_FP MINT_SUB_R8
#define MINT_MUL_FP MINT_MUL_R8
#define MINT_DIV_FP MINT_DIV_R8
#define MINT_REM_FP MINT_REM_R8
#define MINT_CNE_FP MINT_CNE_R8
#define MINT_CEQ_FP MINT_CEQ_R8
#define MINT_CGT_FP MINT_CGT_R8
#define MINT_CGE_FP MINT_CGE_R8
#define MINT_CLT_FP MINT_CLT_R8
#define MINT_CLE_FP MINT_CLE_R8
#define MINT_CONV_OVF_U4_P MINT_CONV_OVF_U4_I8
#else
#define MINT_NEG_P MINT_NEG_I4
#define MINT_NOT_P MINT_NOT_I4
#define MINT_NEG_FP MINT_NEG_R4
#define MINT_ADD_P MINT_ADD_I4
#define MINT_SUB_P MINT_SUB_I4
#define MINT_MUL_P MINT_MUL_I4
#define MINT_DIV_P MINT_DIV_I4
#define MINT_DIV_UN_P MINT_DIV_UN_I4
#define MINT_REM_P MINT_REM_I4
#define MINT_REM_UN_P MINT_REM_UN_I4
#define MINT_AND_P MINT_AND_I4
#define MINT_OR_P MINT_OR_I4
#define MINT_XOR_P MINT_XOR_I4
#define MINT_SHL_P MINT_SHL_I4
#define MINT_SHR_P MINT_SHR_I4
#define MINT_SHR_UN_P MINT_SHR_UN_I4
#define MINT_CEQ_P MINT_CEQ_I4
#define MINT_CNE_P MINT_CNE_I4
#define MINT_CLT_P MINT_CLT_I4
#define MINT_CLT_UN_P MINT_CLT_UN_I4
#define MINT_CGT_P MINT_CGT_I4
#define MINT_CGT_UN_P MINT_CGT_UN_I4
#define MINT_CLE_P MINT_CLE_I4
#define MINT_CLE_UN_P MINT_CLE_UN_I4
#define MINT_CGE_P MINT_CGE_I4
#define MINT_CGE_UN_P MINT_CGE_UN_I4
#define MINT_ADD_FP MINT_ADD_R4
#define MINT_SUB_FP MINT_SUB_R4
#define MINT_MUL_FP MINT_MUL_R4
#define MINT_DIV_FP MINT_DIV_R4
#define MINT_REM_FP MINT_REM_R4
#define MINT_CNE_FP MINT_CNE_R4
#define MINT_CEQ_FP MINT_CEQ_R4
#define MINT_CGT_FP MINT_CGT_R4
#define MINT_CGE_FP MINT_CGE_R4
#define MINT_CLT_FP MINT_CLT_R4
#define MINT_CLE_FP MINT_CLE_R4
#define MINT_CONV_OVF_U4_P MINT_CONV_OVF_U4_I4
#endif
typedef struct {
const gchar *op_name;
guint16 insn [3];
} MagicIntrinsic;
// static const MagicIntrinsic int_binop[] = {
static const MagicIntrinsic int_unnop[] = {
{ "op_UnaryPlus", {MINT_NOP, MINT_NOP, MINT_NOP}},
{ "op_UnaryNegation", {MINT_NEG_P, MINT_NEG_P, MINT_NEG_FP}},
{ "op_OnesComplement", {MINT_NOT_P, MINT_NOT_P, MINT_NIY}}
};
static const MagicIntrinsic int_binop[] = {
{ "op_Addition", {MINT_ADD_P, MINT_ADD_P, MINT_ADD_FP}},
{ "op_Subtraction", {MINT_SUB_P, MINT_SUB_P, MINT_SUB_FP}},
{ "op_Multiply", {MINT_MUL_P, MINT_MUL_P, MINT_MUL_FP}},
{ "op_Division", {MINT_DIV_P, MINT_DIV_UN_P, MINT_DIV_FP}},
{ "op_Modulus", {MINT_REM_P, MINT_REM_UN_P, MINT_REM_FP}},
{ "op_BitwiseAnd", {MINT_AND_P, MINT_AND_P, MINT_NIY}},
{ "op_BitwiseOr", {MINT_OR_P, MINT_OR_P, MINT_NIY}},
{ "op_ExclusiveOr", {MINT_XOR_P, MINT_XOR_P, MINT_NIY}},
{ "op_LeftShift", {MINT_SHL_P, MINT_SHL_P, MINT_NIY}},
{ "op_RightShift", {MINT_SHR_P, MINT_SHR_UN_P, MINT_NIY}},
};
static const MagicIntrinsic int_cmpop[] = {
{ "op_Inequality", {MINT_CNE_P, MINT_CNE_P, MINT_CNE_FP}},
{ "op_Equality", {MINT_CEQ_P, MINT_CEQ_P, MINT_CEQ_FP}},
{ "op_GreaterThan", {MINT_CGT_P, MINT_CGT_UN_P, MINT_CGT_FP}},
{ "op_GreaterThanOrEqual", {MINT_CGE_P, MINT_CGE_UN_P, MINT_CGE_FP}},
{ "op_LessThan", {MINT_CLT_P, MINT_CLT_UN_P, MINT_CLT_FP}},
{ "op_LessThanOrEqual", {MINT_CLE_P, MINT_CLE_UN_P, MINT_CLE_FP}}
};
static gboolean generate_code (TransformData *td, MonoMethod *method, MonoMethodHeader *header, MonoGenericContext *generic_context, MonoError *error);
static InterpInst*
interp_new_ins (TransformData *td, guint16 opcode, int len)
{
InterpInst *new_inst;
g_assert (len);
// Size of data region of instruction is length of instruction minus 1 (the opcode slot)
new_inst = mono_mempool_alloc0 (td->mempool, sizeof (InterpInst) + sizeof (guint16) * (len - 1));
new_inst->opcode = opcode;
// opcodes from inlined methods don't have il offset associated with them since the offset
// stored here is relevant only for the original method
if (!td->inlined_method)
new_inst->il_offset = td->in_start - td->il_code;
else
new_inst->il_offset = -1;
return new_inst;
}
// This version need to be used with switch opcode, which doesn't have constant length
static InterpInst*
interp_add_ins_explicit (TransformData *td, guint16 opcode, int len)
{
InterpInst *new_inst = interp_new_ins (td, opcode, len);
new_inst->prev = td->last_ins;
if (td->last_ins)
td->last_ins->next = new_inst;
else
td->first_ins = new_inst;
td->last_ins = new_inst;
return new_inst;
}
static InterpInst*
interp_add_ins (TransformData *td, guint16 opcode)
{
return interp_add_ins_explicit (td, opcode, mono_interp_oplen [opcode]);
}
// Inserts a new instruction inside the instruction stream
static InterpInst*
interp_insert_ins (TransformData *td, InterpInst *prev_ins, guint16 opcode)
{
InterpInst *new_inst = interp_new_ins (td, opcode, mono_interp_oplen [opcode]);
g_assert (prev_ins && prev_ins->next);
new_inst->prev = prev_ins;
new_inst->next = prev_ins->next;
prev_ins->next = new_inst;
new_inst->next->prev = new_inst;
return new_inst;
}
static void
interp_remove_ins (TransformData *td, InterpInst *ins)
{
if (ins->prev) {
ins->prev->next = ins->next;
} else {
td->first_ins = ins->next;
}
if (ins->next) {
ins->next->prev = ins->prev;
} else {
td->last_ins = ins->prev;
}
}
#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", \
m_class_get_name ((td)->method->klass), (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) \
interp_add_ins (td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
} while (0)
#define CHECK_TYPELOAD(klass) \
do { \
if (!(klass) || mono_class_has_failure (klass)) { \
mono_error_set_for_class_failure (error, klass); \
goto exit; \
} \
} while (0)
#if NO_UNALIGNED_ACCESS
#define WRITE32(ip, v) \
do { \
* (ip) = * (guint16 *)(v); \
* ((ip) + 1) = * ((guint16 *)(v) + 1); \
(ip) += 2; \
} while (0)
#define WRITE32_INS(ins, index, v) \
do { \
(ins)->data [index] = * (guint16 *)(v); \
(ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
} while (0)
#define WRITE64_INS(ins, index, v) \
do { \
(ins)->data [index] = * (guint16 *)(v); \
(ins)->data [index + 1] = * ((guint16 *)(v) + 1); \
(ins)->data [index + 2] = * ((guint16 *)(v) + 2); \
(ins)->data [index + 3] = * ((guint16 *)(v) + 3); \
} while (0)
#else
#define WRITE32(ip, v) \
do { \
* (guint32*)(ip) = * (guint32 *)(v); \
(ip) += 2; \
} while (0)
#define WRITE32_INS(ins, index, v) \
do { \
* (guint32 *)(&(ins)->data [index]) = * (guint32 *)(v); \
} while (0)
#define WRITE64_INS(ins, index, v) \
do { \
* (guint64 *)(&(ins)->data [index]) = * (guint64 *)(v); \
} while (0)
#endif
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 ();
/* Add exception checkpoint or safepoint for backward branches */
if (offset < 0) {
if (mono_threads_are_safepoints_enabled ())
interp_add_ins (td, MINT_SAFEPOINT);
else
interp_add_ins (td, MINT_CHECKPOINT);
}
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] = (StackInfo*)g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
td->vt_stack_size [target] = td->vt_sp;
}
if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
shorten_branch = 1;
if (shorten_branch) {
interp_add_ins (td, short_op);
td->last_ins->data [0] = (guint16) target;
} else {
interp_add_ins (td, long_op);
WRITE32_INS (td->last_ins, 0, &target);
}
}
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) {
interp_add_ins (td, MINT_CONV_I8_I4);
// The il instruction starts with the actual branch, and not with the conversion opcodes
td->last_ins->il_offset = -1;
} else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
interp_add_ins (td, MINT_CONV_I8_I4_SP);
td->last_ins->il_offset = -1;
} else if (type1 == STACK_TYPE_R4 && type2 == STACK_TYPE_R8) {
interp_add_ins (td, MINT_CONV_R8_R4);
td->last_ins->il_offset = -1;
} else if (type1 == STACK_TYPE_R8 && type2 == STACK_TYPE_R4) {
interp_add_ins (td, MINT_CONV_R8_R4_SP);
td->last_ins->il_offset = -1;
} else if (type1 != type2) {
g_warning("%s.%s: branch type mismatch %d %d",
m_class_get_name (td->method->klass), 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);
interp_add_ins (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) {
interp_add_ins (td, MINT_CONV_I8_I4);
type2 = STACK_TYPE_I8;
}
if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
interp_add_ins (td, MINT_CONV_I8_I4_SP);
type1 = STACK_TYPE_I8;
td->sp [-2].type = STACK_TYPE_I8;
}
#endif
if (type1 == STACK_TYPE_R8 && type2 == STACK_TYPE_R4) {
interp_add_ins (td, MINT_CONV_R8_R4);
type2 = STACK_TYPE_R8;
}
if (type1 == STACK_TYPE_R4 && type2 == STACK_TYPE_R8) {
interp_add_ins (td, MINT_CONV_R8_R4_SP);
type1 = STACK_TYPE_R8;
td->sp [-2].type = STACK_TYPE_R8;
}
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",
m_class_get_name (td->method->klass), 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);
interp_add_ins (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",
m_class_get_name (td->method->klass), td->method->name,
td->sp [-2].type);
}
interp_add_ins (td, op);
--td->sp;
}
static int
can_store (int st_value, int vt_value)
{
if (st_value == STACK_TYPE_O || st_value == STACK_TYPE_MP)
st_value = STACK_TYPE_I;
if (vt_value == STACK_TYPE_O || vt_value == STACK_TYPE_MP)
vt_value = STACK_TYPE_I;
return st_value == vt_value;
}
#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 REALLOC_STACK(td, sppos) \
do { \
(td)->stack_capacity *= 2; \
(td)->stack = (StackInfo*)realloc ((td)->stack, (td)->stack_capacity * sizeof (td->stack [0])); \
(td)->sp = (td)->stack + sppos; \
} 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; \
if (sp_height > (td)->stack_capacity) \
REALLOC_STACK(td, 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; \
if (sp_height > (td)->stack_capacity) \
REALLOC_STACK(td, sp_height); \
SET_TYPE((td)->sp - 1, ty, k); \
} while (0)
#define PUSH_VT(td, size) \
do { \
(td)->vt_sp += ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
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 -= ALIGN_TO ((size), MINT_VT_ALIGNMENT); \
} while (0)
static MonoType*
get_arg_type_exact (TransformData *td, int n, int *mt)
{
MonoType *type;
gboolean hasthis = mono_method_signature_internal (td->method)->hasthis;
if (hasthis && n == 0)
type = m_class_get_byval_arg (td->method->klass);
else
type = mono_method_signature_internal (td->method)->params [n - !!hasthis];
if (mt)
*mt = mint_type (type);
return type;
}
static void
load_arg(TransformData *td, int n)
{
int mt;
MonoClass *klass = NULL;
MonoType *type;
gboolean hasthis = mono_method_signature_internal (td->method)->hasthis;
type = get_arg_type_exact (td, n, &mt);
if (mt == MINT_TYPE_VT) {
gint32 size;
klass = mono_class_from_mono_type_internal (type);
if (mono_method_signature_internal (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;
interp_add_ins (td, MINT_LDARG_P);
td->last_ins->data [0] = 0;
klass = NULL;
} else {
PUSH_VT (td, size);
interp_add_ins (td, MINT_LDARG_VT);
td->last_ins->data [0] = n;
WRITE32_INS (td->last_ins, 1, &size);
}
} else {
if (hasthis && n == 0) {
mt = MINT_TYPE_P;
interp_add_ins (td, MINT_LDARG_P);
td->last_ins->data [0] = n;
klass = NULL;
} else {
interp_add_ins (td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
td->last_ins->data [0] = n;
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type_internal (type);
}
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
store_arg(TransformData *td, int n)
{
int mt;
CHECK_STACK (td, 1);
MonoType *type;
type = get_arg_type_exact (td, n, &mt);
if (mt == MINT_TYPE_VT) {
gint32 size;
MonoClass *klass = mono_class_from_mono_type_internal (type);
if (mono_method_signature_internal (td->method)->pinvoke)
size = mono_class_native_size (klass, NULL);
else
size = mono_class_value_size (klass, NULL);
interp_add_ins (td, MINT_STARG_VT);
td->last_ins->data [0] = n;
WRITE32_INS (td->last_ins, 1, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
interp_add_ins (td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
td->last_ins->data [0] = n;
}
--td->sp;
}
static void
load_local_general (TransformData *td, int offset, MonoType *type)
{
int mt = mint_type (type);
MonoClass *klass = NULL;
if (mt == MINT_TYPE_VT) {
klass = mono_class_from_mono_type_internal (type);
gint32 size = mono_class_value_size (klass, NULL);
PUSH_VT(td, size);
interp_add_ins (td, MINT_LDLOC_VT);
td->last_ins->data [0] = offset;
WRITE32_INS (td->last_ins, 1, &size);
} else {
g_assert (mt < MINT_TYPE_VT);
if (!td->gen_sdb_seq_points &&
mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_ins != NULL &&
td->last_ins->opcode == MINT_STLOC_I4 && td->last_ins->data [0] == offset) {
td->last_ins->opcode = MINT_STLOC_NP_I4;
} else if (!td->gen_sdb_seq_points &&
mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_ins != NULL &&
td->last_ins->opcode == MINT_STLOC_O && td->last_ins->data [0] == offset) {
td->last_ins->opcode = MINT_STLOC_NP_O;
} else {
interp_add_ins (td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
td->last_ins->data [0] = offset; /*FIX for large offset */
}
if (mt == MINT_TYPE_O)
klass = mono_class_from_mono_type_internal (type);
}
PUSH_TYPE(td, stack_type[mt], klass);
}
static void
load_local (TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int offset = td->rtm->local_offsets [n];
load_local_general (td, offset, type);
}
static void
store_local_general (TransformData *td, int offset, MonoType *type)
{
int mt = mint_type (type);
CHECK_STACK (td, 1);
#if SIZEOF_VOID_P == 8
if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
interp_add_ins (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",
m_class_get_name (td->method->klass), td->method->name,
stack_type [mt], td->sp [-1].type);
}
if (mt == MINT_TYPE_VT) {
MonoClass *klass = mono_class_from_mono_type_internal (type);
gint32 size = mono_class_value_size (klass, NULL);
interp_add_ins (td, MINT_STLOC_VT);
td->last_ins->data [0] = offset; /*FIX for large offset */
WRITE32_INS (td->last_ins, 1, &size);
if (td->sp [-1].type == STACK_TYPE_VT)
POP_VT(td, size);
} else {
g_assert (mt < MINT_TYPE_VT);
interp_add_ins (td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
td->last_ins->data [0] = offset; /*FIX for large offset */
}
--td->sp;
}
static void
store_local (TransformData *td, int n)
{
MonoType *type = td->header->locals [n];
int offset = td->rtm->local_offsets [n];
store_local_general (td, offset, type);
}
#define SIMPLE_OP(td, op) \
do { \
interp_add_ins (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 = (gpointer*)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 gboolean
jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
{
GSList *l;
if (sig->param_count > 6)
return FALSE;
if (sig->pinvoke)
return FALSE;
if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
return FALSE;
if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
return FALSE;
if (method->is_inflated)
return FALSE;
if (method->string_ctor)
return FALSE;
if (mono_aot_only && m_class_get_image (method->klass)->aot_module && !(method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
ERROR_DECL (error);
gpointer addr = mono_jit_compile_method_jit_only (method, error);
if (addr && mono_error_ok (error))
return TRUE;
}
for (l = mono_interp_jit_classes; l; l = l->next) {
const char *class_name = (const char*)l->data;
// FIXME: Namespaces
if (!strcmp (m_class_get_name (method->klass), class_name))
return TRUE;
}
//return TRUE;
return FALSE;
}
static int mono_class_get_magic_index (MonoClass *k)
{
if (mono_class_is_magic_int (k))
return !strcmp ("nint", m_class_get_name (k)) ? 0 : 1;
if (mono_class_is_magic_float (k))
return 2;
return -1;
}
static void
interp_generate_mae_throw (TransformData *td, MonoMethod *method, MonoMethod *target_method)
{
MonoJitICallInfo *info = &mono_get_jit_icall_info ()->mono_throw_method_access;
/* Inject code throwing MethodAccessException */
interp_add_ins (td, MINT_MONO_LDPTR);
td->last_ins->data [0] = get_data_item_index (td, method);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
interp_add_ins (td, MINT_MONO_LDPTR);
td->last_ins->data [0] = get_data_item_index (td, target_method);
PUSH_SIMPLE_TYPE (td, STACK_TYPE_I);
interp_add_ins (td, MINT_ICALL_PP_V);
td->last_ins->data [0] = get_data_item_index (td, (gpointer)info->func);
td->sp -= 2;
}
static void
interp_generate_bie_throw (TransformData *td)
{
MonoJitICallInfo *info = &mono_get_jit_icall_info ()->mono_throw_bad_image;
interp_add_ins (td, MINT_ICALL_PP_V);
td->last_ins->data [0] = get_data_item_index (td, (gpointer)info->func);
}
/*
* These are additional locals that can be allocated as we transform the code.
* They are allocated past the method locals so they are accessed in the same
* way, with an offset relative to the frame->locals.
*/
static int
create_interp_local (TransformData *td, MonoType *type)
{
int align, size;
int offset = td->total_locals_size;
size = mono_type_size (type, &align);
offset = ALIGN_TO (offset, align);
td->total_locals_size = offset + size;
return offset;
}
static void
dump_mint_code (const guint16 *start, const guint16* end)
{
const guint16 *p = start;
while (p < end) {
char *ins = mono_interp_dis_mintop (start, p);
g_print ("%s\n", ins);
g_free (ins);
p = mono_interp_dis_mintop_len (p);
}
}
/* For debug use */
void
mono_interp_print_code (InterpMethod *imethod)
{
MonoJitInfo *jinfo = imethod->jinfo;
const guint16 *start;
if (!jinfo)
return;
char *name = mono_method_full_name (imethod->method, 1);
g_print ("Method : %s\n", name);
g_free (name);
start = (guint16*) jinfo->code_start;
dump_mint_code (start, start + jinfo->code_size);
}
static MonoMethodHeader*
interp_method_get_header (MonoMethod* method, MonoError *error)
{
/* An explanation: mono_method_get_header_internal returns an error if
* called on a method with no body (e.g. an abstract method, or an
* icall). We don't want that.
*/
if (mono_method_has_no_body (method))
return NULL;
else
return mono_method_get_header_internal (method, error);
}
/* stores top of stack as local and pushes address of it on stack */
static void
emit_store_value_as_local (TransformData *td, MonoType *src)
{
int size = mini_magic_type_size (NULL, src);
int local_offset = create_interp_local (td, mini_native_type_replace_type (src));
store_local_general (td, local_offset, src);
size = ALIGN_TO (size, MINT_VT_ALIGNMENT);
interp_add_ins (td, MINT_LDLOC_VT);
td->last_ins->data [0] = local_offset;
WRITE32_INS (td->last_ins, 1, &size);
PUSH_VT (td, size);
PUSH_TYPE (td, STACK_TYPE_VT, NULL);
}
// Returns whether we can optimize away the instructions starting at start.
// If any instructions are part of a new basic block, we can't remove them.
static gboolean
interp_is_bb_start (TransformData *td, InterpInst *start, InterpInst *end)
{
InterpInst *ins = start;
while (ins != end) {
if (ins->il_offset != -1) {
if (td->is_bb_start [ins->il_offset])
return TRUE;
}
ins = ins->next;
}
return FALSE;
}
static gboolean
interp_ins_is_ldc (InterpInst *ins)
{
return ins->opcode >= MINT_LDC_I4_M1 && ins->opcode <= MINT_LDC_I8;
}
static gint32
interp_ldc_i4_get_const (InterpInst *ins)
{
switch (ins->opcode) {