/
mini.c
14500 lines (12749 loc) · 427 KB
/
mini.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
/*
* mini.c: The new Mono code generator.
*
* Author:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
*
* (C) 2002 Ximian, Inc.
*/
#include <config.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <math.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef PLATFORM_MACOSX
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/exception.h>
#include <mach/task.h>
#include <pthread.h>
#endif
#ifdef HAVE_VALGRIND_MEMCHECK_H
#include <valgrind/memcheck.h>
#endif
#include <mono/metadata/assembly.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/class.h>
#include <mono/metadata/object.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/opcodes.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/socket-io.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/io-layer/io-layer.h>
#include "mono/metadata/profiler.h"
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/monitor.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/security-manager.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/rawbuffer.h>
#include <mono/metadata/security-core-clr.h>
#include <mono/metadata/verify.h>
#include <mono/metadata/verify-internals.h>
#include <mono/utils/mono-math.h>
#include <mono/utils/mono-compiler.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-logger.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/dtrace.h>
#include "mini.h"
#include <string.h>
#include <ctype.h>
#include "inssel.h"
#include "trace.h"
#include "jit-icalls.h"
#include "aliasing.h"
#include "debug-mini.h"
#define BRANCH_COST 100
#define INLINE_LENGTH_LIMIT 20
#define INLINE_FAILURE do {\
if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
goto inline_failure;\
} while (0)
#define CHECK_CFG_EXCEPTION do {\
if (cfg->exception_type != MONO_EXCEPTION_NONE)\
goto exception_exit;\
} while (0)
#define METHOD_ACCESS_FAILURE do { \
char *method_fname = mono_method_full_name (method, TRUE); \
char *cil_method_fname = mono_method_full_name (cil_method, TRUE); \
cfg->exception_type = MONO_EXCEPTION_METHOD_ACCESS; \
cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname); \
g_free (method_fname); \
g_free (cil_method_fname); \
goto exception_exit; \
} while (0)
#define FIELD_ACCESS_FAILURE do { \
char *method_fname = mono_method_full_name (method, TRUE); \
char *field_fname = mono_field_full_name (field); \
cfg->exception_type = MONO_EXCEPTION_FIELD_ACCESS; \
cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname); \
g_free (method_fname); \
g_free (field_fname); \
goto exception_exit; \
} while (0)
#define GENERIC_SHARING_FAILURE(opcode) do { \
if (cfg->generic_sharing_context) { \
if (cfg->verbose_level > 1) \
printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", method->klass->name_space, method->klass->name, method->name, method->signature->param_count, mono_opcode_name ((opcode)), __LINE__); \
cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED; \
goto exception_exit; \
} \
} while (0)
#define GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(opcode) do { \
if (method->klass->valuetype) \
GENERIC_SHARING_FAILURE ((opcode)); \
} while (0)
#define GET_RGCTX(rgctx, context_used) do { \
MonoInst *this = NULL; \
g_assert (context_used); \
GENERIC_SHARING_FAILURE_IF_VALUETYPE_METHOD(*ip); \
if (!(method->flags & METHOD_ATTRIBUTE_STATIC) && \
!((context_used) & MONO_GENERIC_CONTEXT_USED_METHOD)) \
NEW_ARGLOAD (cfg, this, 0); \
(rgctx) = get_runtime_generic_context (cfg, method, (context_used), this, ip); \
} while (0)
#define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
static void setup_stat_profiler (void);
gboolean mono_arch_print_tree(MonoInst *tree, int arity);
static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
static gpointer mono_jit_compile_method (MonoMethod *method);
inline static int mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip);
static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
guint inline_offset, gboolean is_virtual_call);
#ifdef MONO_ARCH_SOFT_FLOAT
static void
handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip);
#endif
/* helper methods signature */
static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
static MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
static MonoMethodSignature *helper_sig_domain_get = NULL;
static guint32 default_opt = 0;
static gboolean default_opt_set = FALSE;
guint32 mono_jit_tls_id = -1;
#ifdef HAVE_KW_THREAD
static __thread gpointer mono_jit_tls MONO_TLS_FAST;
#endif
MonoTraceSpec *mono_jit_trace_calls = NULL;
gboolean mono_break_on_exc = FALSE;
gboolean mono_compile_aot = FALSE;
/* If this is set, no code is generated dynamically, everything is taken from AOT files */
gboolean mono_aot_only = FALSE;
/* Whenever to use IMT */
#ifdef MONO_ARCH_HAVE_IMT
gboolean mono_use_imt = TRUE;
#else
gboolean mono_use_imt = FALSE;
#endif
MonoMethodDesc *mono_inject_async_exc_method = NULL;
int mono_inject_async_exc_pos;
MonoMethodDesc *mono_break_at_bb_method = NULL;
int mono_break_at_bb_bb_num;
static int mini_verbose = 0;
#define mono_jit_lock() EnterCriticalSection (&jit_mutex)
#define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
static CRITICAL_SECTION jit_mutex;
static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
static MonoCodeManager *global_codeman = NULL;
static GHashTable *jit_icall_name_hash = NULL;
static MonoDebugOptions debug_options;
#ifdef VALGRIND_JIT_REGISTER_MAP
static int valgrind_register = 0;
#endif
/*
* Table written to by the debugger with a 1-based index into the
* mono_breakpoint_info table, which contains changes made to
* the JIT instructions by the debugger.
*/
gssize
mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
/* Whenever to check for pending exceptions in managed-to-native wrappers */
gboolean check_for_pending_exc = TRUE;
gboolean
mono_running_on_valgrind (void)
{
#ifdef HAVE_VALGRIND_MEMCHECK_H
if (RUNNING_ON_VALGRIND){
#ifdef VALGRIND_JIT_REGISTER_MAP
valgrind_register = TRUE;
#endif
return TRUE;
} else
return FALSE;
#else
return FALSE;
#endif
}
typedef struct {
void *ip;
MonoMethod *method;
} FindTrampUserData;
static void
find_tramp (gpointer key, gpointer value, gpointer user_data)
{
FindTrampUserData *ud = (FindTrampUserData*)user_data;
if (value == ud->ip)
ud->method = (MonoMethod*)key;
}
/* debug function */
G_GNUC_UNUSED static char*
get_method_from_ip (void *ip)
{
MonoJitInfo *ji;
char *method;
char *res;
MonoDomain *domain = mono_domain_get ();
MonoDebugSourceLocation *location;
FindTrampUserData user_data;
ji = mono_jit_info_table_find (domain, ip);
if (!ji) {
user_data.ip = ip;
user_data.method = NULL;
mono_domain_lock (domain);
g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
mono_domain_unlock (domain);
if (user_data.method) {
char *mname = mono_method_full_name (user_data.method, TRUE);
res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
g_free (mname);
return res;
}
else
return NULL;
}
method = mono_method_full_name (ji->method, TRUE);
/* FIXME: unused ? */
location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
res = g_strdup_printf (" %s + 0x%x (%p %p) [%p - %s]", method, (int)((char*)ip - (char*)ji->code_start), ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
mono_debug_free_source_location (location);
g_free (method);
return res;
}
/**
* mono_pmip:
* @ip: an instruction pointer address
*
* This method is used from a debugger to get the name of the
* method at address @ip. This routine is typically invoked from
* a debugger like this:
*
* (gdb) print mono_pmip ($pc)
*
* Returns: the name of the method at address @ip.
*/
G_GNUC_UNUSED char *
mono_pmip (void *ip)
{
return get_method_from_ip (ip);
}
/**
* mono_print_method_from_ip
* @ip: an instruction pointer address
*
* This method is used from a debugger to get the name of the
* method at address @ip.
*
* This prints the name of the method at address @ip in the standard
* output. Unlike mono_pmip which returns a string, this routine
* prints the value on the standard output.
*/
void
mono_print_method_from_ip (void *ip)
{
MonoJitInfo *ji;
char *method;
MonoDebugSourceLocation *source;
MonoDomain *domain = mono_domain_get ();
FindTrampUserData user_data;
ji = mono_jit_info_table_find (domain, ip);
if (!ji) {
user_data.ip = ip;
user_data.method = NULL;
mono_domain_lock (domain);
g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
mono_domain_unlock (domain);
if (user_data.method) {
char *mname = mono_method_full_name (user_data.method, TRUE);
printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
g_free (mname);
}
else
g_print ("No method at %p\n", ip);
return;
}
method = mono_method_full_name (ji->method, TRUE);
source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
g_print ("IP %p at offset 0x%x of method %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), method, ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
if (source)
g_print ("%s:%d\n", source->source_file, source->row);
mono_debug_free_source_location (source);
g_free (method);
}
/*
* mono_method_same_domain:
*
* Determine whenever two compiled methods are in the same domain, thus
* the address of the callee can be embedded in the caller.
*/
gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
{
if (!caller || !callee)
return FALSE;
/*
* If the call was made from domain-neutral to domain-specific
* code, we can't patch the call site.
*/
if (caller->domain_neutral && !callee->domain_neutral)
return FALSE;
if ((caller->method->klass == mono_defaults.appdomain_class) &&
(strstr (caller->method->name, "InvokeInDomain"))) {
/* The InvokeInDomain methods change the current appdomain */
return FALSE;
}
return TRUE;
}
/*
* mono_global_codeman_reserve:
*
* Allocate code memory from the global code manager.
*/
void *mono_global_codeman_reserve (int size)
{
void *ptr;
if (mono_aot_only)
g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
if (!global_codeman) {
/* This can happen during startup */
global_codeman = mono_code_manager_new ();
return mono_code_manager_reserve (global_codeman, size);
}
else {
mono_jit_lock ();
ptr = mono_code_manager_reserve (global_codeman, size);
mono_jit_unlock ();
return ptr;
}
}
MonoJumpInfoToken *
mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
{
MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
res->image = image;
res->token = token;
return res;
}
#define MONO_INIT_VARINFO(vi,id) do { \
(vi)->range.first_use.pos.bid = 0xffff; \
(vi)->reg = -1; \
(vi)->idx = (id); \
} while (0)
//#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
#define UNVERIFIED do { if (debug_options.break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
/*
* Basic blocks have two numeric identifiers:
* dfn: Depth First Number
* block_num: unique ID assigned at bblock creation
*/
#define NEW_BBLOCK(cfg,new_bb) do { \
new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)); \
MONO_INST_LIST_INIT (&new_bb->ins_list); \
} while (0)
#define ADD_BBLOCK(cfg,b) do { \
cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b); \
(b)->block_num = cfg->num_bblocks++; \
(b)->real_offset = real_offset; \
} while (0)
#define GET_BBLOCK(cfg,tblock,ip) do { \
(tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
if (!(tblock)) { \
if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
NEW_BBLOCK (cfg, (tblock)); \
(tblock)->cil_code = (ip); \
ADD_BBLOCK (cfg, (tblock)); \
} \
} while (0)
#define CHECK_BBLOCK(target,ip,tblock) do { \
if ((target) < (ip) && \
MONO_INST_LIST_EMPTY (&(tblock)->ins_list)) { \
bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
if (cfg->verbose_level > 2) \
g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (int)((target) - header->code), (int)((ip) - header->code)); \
} \
} while (0)
#define NEW_ICONST(cfg,dest,val) do { \
(dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
(dest)->opcode = OP_ICONST; \
(dest)->inst_c0 = (val); \
(dest)->type = STACK_I4; \
} while (0)
#define NEW_PCONST(cfg,dest,val) do { \
MONO_INST_NEW ((cfg), (dest), OP_PCONST); \
(dest)->inst_p0 = (val); \
(dest)->type = STACK_PTR; \
} while (0)
#ifdef MONO_ARCH_NEED_GOT_VAR
#define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO); \
(dest)->inst_left = (gpointer)(el1); \
(dest)->inst_right = (gpointer)(el2); \
} while (0)
#define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
if (cfg->compile_aot) { \
MonoInst *group, *got_var, *got_loc; \
got_loc = mono_get_got_var (cfg); \
NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
(dest)->inst_p0 = got_var; \
(dest)->inst_p1 = group; \
} else { \
(dest)->inst_p0 = (cons); \
(dest)->inst_i1 = (gpointer)(patch_type); \
} \
(dest)->type = STACK_PTR; \
} while (0)
#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
MonoInst *group, *got_var, *got_loc; \
MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
got_loc = mono_get_got_var (cfg); \
NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
(dest)->inst_p0 = got_var; \
(dest)->inst_p1 = group; \
(dest)->type = (stack_type); \
(dest)->klass = (stack_class); \
} while (0)
#else
#define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
(dest)->inst_p0 = (cons); \
(dest)->inst_i1 = (gpointer)(patch_type); \
(dest)->type = STACK_PTR; \
} while (0)
#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
MONO_INST_NEW ((cfg), (dest), OP_AOTCONST); \
(dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
(dest)->inst_p1 = (gpointer)(patch_type); \
(dest)->type = (stack_type); \
(dest)->klass = (stack_class); \
} while (0)
#endif
#define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
#define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
#define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
#define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
#define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
#define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
#define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
#define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
#define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
#define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
if (cfg->compile_aot) { \
NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
} else { \
NEW_PCONST (cfg, args [0], (entry).blob); \
} \
} while (0)
#define NEW_DOMAINCONST(cfg,dest) do { \
if (cfg->opt & MONO_OPT_SHARED) { \
/* avoid depending on undefined C behavior in sequence points */ \
MonoInst* __domain_var = mono_get_domainvar (cfg); \
NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
} else { \
NEW_PCONST (cfg, dest, (cfg)->domain); \
} \
} while (0)
#define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
#define NEW_ARGLOAD(cfg,dest,num) do { \
if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_LOAD; \
(dest)->inst_i0 = arg_array [(num)]; \
(dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
type_to_eval_stack_type ((cfg), param_types [(num)], (dest)); \
(dest)->klass = (dest)->inst_i0->klass; \
}} while (0)
#define NEW_LOCLOAD(cfg,dest,num) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_LOAD; \
(dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
(dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
type_to_eval_stack_type ((cfg), header->locals [(num)], (dest)); \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define NEW_LOCLOADA(cfg,dest,num) do { \
MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
(dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
(dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
(dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
(dest)->type = STACK_MP; \
(dest)->klass = (dest)->inst_i0->klass; \
if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
(cfg)->disable_ssa = TRUE; \
} while (0)
#define NEW_RETLOADA(cfg,dest) do { \
if (cfg->vret_addr) { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_LOAD; \
(dest)->inst_i0 = cfg->vret_addr; \
(dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
(dest)->type = STACK_MP; \
(dest)->klass = (dest)->inst_i0->klass; \
} else { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
(dest)->inst_i0 = (cfg)->ret; \
(dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
(dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
(dest)->type = STACK_MP; \
(dest)->klass = (dest)->inst_i0->klass; \
(cfg)->disable_ssa = TRUE; \
} \
} while (0)
#define NEW_ARGLOADA(cfg,dest,num) do { \
if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
(dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
(dest)->inst_i0 = arg_array [(num)]; \
(dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
(dest)->type = STACK_MP; \
(dest)->klass = (dest)->inst_i0->klass; \
(cfg)->disable_ssa = TRUE; \
} while (0)
#define NEW_TEMPLOAD(cfg,dest,num) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_LOAD; \
(dest)->inst_i0 = (cfg)->varinfo [(num)]; \
(dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
type_to_eval_stack_type ((cfg), (dest)->inst_i0->inst_vtype, (dest)); \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define NEW_TEMPLOADA(cfg,dest,num) do { \
MONO_INST_NEW ((cfg), (dest), OP_LDADDR); \
(dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
(dest)->inst_i0 = (cfg)->varinfo [(num)]; \
(dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
(dest)->type = STACK_MP; \
(dest)->klass = (dest)->inst_i0->klass; \
if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
(cfg)->disable_ssa = TRUE; \
} while (0)
#define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->inst_left = addr; \
(dest)->opcode = mini_type_to_ldind ((cfg), vtype); \
type_to_eval_stack_type ((cfg), vtype, (dest)); \
/* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
} while (0)
#define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->inst_i0 = addr; \
(dest)->opcode = mini_type_to_stind ((cfg), vtype); \
(dest)->inst_i1 = (value); \
/* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
} while (0)
#define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->ssa_op = MONO_SSA_STORE; \
(dest)->inst_i0 = (cfg)->varinfo [(num)]; \
(dest)->opcode = mini_type_to_stind ((cfg), (dest)->inst_i0->inst_vtype); \
(dest)->inst_i1 = (inst); \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define NEW_LOCSTORE(cfg,dest,num,inst) do { \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->opcode = mini_type_to_stind ((cfg), header->locals [(num)]); \
(dest)->ssa_op = MONO_SSA_STORE; \
(dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
(dest)->inst_i1 = (inst); \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define NEW_ARGSTORE(cfg,dest,num,inst) do { \
if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
MONO_INST_NEW ((cfg), (dest), OP_NOP); \
(dest)->opcode = mini_type_to_stind ((cfg), param_types [(num)]); \
(dest)->ssa_op = MONO_SSA_STORE; \
(dest)->inst_i0 = arg_array [(num)]; \
(dest)->inst_i1 = (inst); \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define NEW_MEMCPY(cfg,dest,dst,src,memcpy_size,memcpy_align) do { \
MONO_INST_NEW (cfg, dest, OP_MEMCPY); \
(dest)->inst_left = (dst); \
(dest)->inst_right = (src); \
(dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
(dest)->backend.memcpy_args->size = (memcpy_size); \
(dest)->backend.memcpy_args->align = (memcpy_align); \
} while (0)
#define NEW_MEMSET(cfg,dest,dst,imm,memcpy_size,memcpy_align) do { \
MONO_INST_NEW (cfg, dest, OP_MEMSET); \
(dest)->inst_left = (dst); \
(dest)->inst_imm = (imm); \
(dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
(dest)->backend.memcpy_args->size = (memcpy_size); \
(dest)->backend.memcpy_args->align = (memcpy_align); \
} while (0)
#define NEW_DUMMY_USE(cfg,dest,load) do { \
MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE); \
(dest)->inst_left = (load); \
} while (0)
#define NEW_DUMMY_STORE(cfg,dest,num) do { \
MONO_INST_NEW ((cfg), (dest), OP_DUMMY_STORE); \
(dest)->inst_i0 = (cfg)->varinfo [(num)]; \
(dest)->klass = (dest)->inst_i0->klass; \
} while (0)
#define ADD_BINOP(op) do { \
MONO_INST_NEW (cfg, ins, (op)); \
sp -= 2; \
ins->inst_i0 = sp [0]; \
ins->inst_i1 = sp [1]; \
*sp++ = ins; \
type_from_op (ins); \
CHECK_TYPE (ins); \
/* Have to insert a widening op */ \
/* FIXME: Need to add many more cases */ \
if (ins->inst_i0->type == STACK_PTR && ins->inst_i1->type == STACK_I4) { \
MonoInst *widen; \
MONO_INST_NEW (cfg, widen, CEE_CONV_I); \
widen->inst_i0 = ins->inst_i1; \
ins->inst_i1 = widen; \
} \
} while (0)
#define ADD_UNOP(op) do { \
MONO_INST_NEW (cfg, ins, (op)); \
sp--; \
ins->inst_i0 = sp [0]; \
*sp++ = ins; \
type_from_op (ins); \
CHECK_TYPE (ins); \
} while (0)
#define ADD_BINCOND(next_block) do { \
MonoInst *cmp; \
sp -= 2; \
MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
cmp->inst_i0 = sp [0]; \
cmp->inst_i1 = sp [1]; \
cmp->cil_code = ins->cil_code; \
type_from_op (cmp); \
CHECK_TYPE (cmp); \
ins->inst_i0 = cmp; \
MONO_ADD_INS (bblock, ins); \
ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
GET_BBLOCK (cfg, tblock, target); \
link_bblock (cfg, bblock, tblock); \
ins->inst_true_bb = tblock; \
CHECK_BBLOCK (target, ip, tblock); \
if ((next_block)) { \
link_bblock (cfg, bblock, (next_block)); \
ins->inst_false_bb = (next_block); \
start_new_bblock = 1; \
} else { \
GET_BBLOCK (cfg, tblock, ip); \
link_bblock (cfg, bblock, tblock); \
ins->inst_false_bb = tblock; \
start_new_bblock = 2; \
} \
} while (0)
/* FIXME: handle float, long ... */
#define ADD_UNCOND(istrue) do { \
MonoInst *cmp; \
sp--; \
MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
cmp->inst_i0 = sp [0]; \
switch (cmp->inst_i0->type) { \
case STACK_I8: \
cmp->inst_i1 = zero_int64; break; \
case STACK_R8: \
cmp->inst_i1 = zero_r8; break; \
case STACK_PTR: \
case STACK_MP: \
cmp->inst_i1 = zero_ptr; break; \
case STACK_OBJ: \
cmp->inst_i1 = zero_obj; break; \
default: \
cmp->inst_i1 = zero_int32; \
} \
cmp->cil_code = ins->cil_code; \
type_from_op (cmp); \
CHECK_TYPE (cmp); \
ins->inst_i0 = cmp; \
ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
MONO_ADD_INS (bblock, ins); \
ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
GET_BBLOCK (cfg, tblock, target); \
link_bblock (cfg, bblock, tblock); \
ins->inst_true_bb = tblock; \
CHECK_BBLOCK (target, ip, tblock); \
GET_BBLOCK (cfg, tblock, ip); \
link_bblock (cfg, bblock, tblock); \
ins->inst_false_bb = tblock; \
start_new_bblock = 2; \
} while (0)
#define NEW_LDELEMA(cfg,dest,sp,k) do { \
MONO_INST_NEW ((cfg), (dest), CEE_LDELEMA); \
(dest)->inst_left = (sp) [0]; \
(dest)->inst_right = (sp) [1]; \
(dest)->type = STACK_MP; \
(dest)->klass = (k); \
(cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
} while (0)
#define NEW_GROUP(cfg,dest,el1,el2) do { \
MONO_INST_NEW ((cfg), (dest), OP_GROUP); \
(dest)->inst_left = (el1); \
(dest)->inst_right = (el2); \
} while (0)
#if 0
static gint
compare_bblock (gconstpointer a, gconstpointer b)
{
const MonoBasicBlock *b1 = a;
const MonoBasicBlock *b2 = b;
return b2->cil_code - b1->cil_code;
}
#endif
/* *
* link_bblock: Links two basic blocks
*
* links two basic blocks in the control flow graph, the 'from'
* argument is the starting block and the 'to' argument is the block
* the control flow ends to after 'from'.
*/
static void
link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
{
MonoBasicBlock **newa;
int i, found;
#if 0
if (from->cil_code) {
if (to->cil_code)
g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
else
g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
} else {
if (to->cil_code)
g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
else
g_print ("edge from entry to exit\n");
}
#endif
found = FALSE;
for (i = 0; i < from->out_count; ++i) {
if (to == from->out_bb [i]) {
found = TRUE;
break;
}
}
if (!found) {
newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
for (i = 0; i < from->out_count; ++i) {
newa [i] = from->out_bb [i];
}
newa [i] = to;
from->out_count++;
from->out_bb = newa;
}
found = FALSE;
for (i = 0; i < to->in_count; ++i) {
if (from == to->in_bb [i]) {
found = TRUE;
break;
}
}
if (!found) {
newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
for (i = 0; i < to->in_count; ++i) {
newa [i] = to->in_bb [i];
}
newa [i] = from;
to->in_count++;
to->in_bb = newa;
}
}
/**
* mono_unlink_bblock:
*
* Unlink two basic blocks.
*/
static void
mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
{
int i, pos;
gboolean found;
found = FALSE;
for (i = 0; i < from->out_count; ++i) {
if (to == from->out_bb [i]) {
found = TRUE;
break;
}
}
if (found) {
pos = 0;
for (i = 0; i < from->out_count; ++i) {
if (from->out_bb [i] != to)
from->out_bb [pos ++] = from->out_bb [i];
}
g_assert (pos == from->out_count - 1);
from->out_count--;
}
found = FALSE;
for (i = 0; i < to->in_count; ++i) {
if (from == to->in_bb [i]) {
found = TRUE;
break;
}
}
if (found) {
pos = 0;
for (i = 0; i < to->in_count; ++i) {
if (to->in_bb [i] != from)
to->in_bb [pos ++] = to->in_bb [i];
}
g_assert (pos == to->in_count - 1);
to->in_count--;
}
}
/**
* mono_find_block_region:
*
* We mark each basic block with a region ID. We use that to avoid BB
* optimizations when blocks are in different regions.
*
* Returns:
* A region token that encodes where this region is, and information
* about the clause owner for this block.
*
* The region encodes the try/catch/filter clause that owns this block
* as well as the type. -1 is a special value that represents a block
* that is in none of try/catch/filter.
*/
static int
mono_find_block_region (MonoCompile *cfg, int offset)
{
MonoMethod *method = cfg->method;
MonoMethodHeader *header = mono_method_get_header (method);
MonoExceptionClause *clause;
int i;
/* first search for handlers and filters */
for (i = 0; i < header->num_clauses; ++i) {
clause = &header->clauses [i];
if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
(offset < (clause->handler_offset)))
return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
}
}
/* search the try blocks */
for (i = 0; i < header->num_clauses; ++i) {
clause = &header->clauses [i];
if (MONO_OFFSET_IN_CLAUSE (clause, offset))
return ((i + 1) << 8) | clause->flags;
}
return -1;
}
static GList*
mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
{
MonoMethod *method = cfg->method;
MonoMethodHeader *header = mono_method_get_header (method);
MonoExceptionClause *clause;
MonoBasicBlock *handler;
int i;
GList *res = NULL;