/
mini.c
8065 lines (6890 loc) · 237 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.
*
* Authors:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
*
* Copyright 2002-2003 Ximian, Inc.
* Copyright 2003-2010 Novell, Inc.
* Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
*/
#define MONO_LLVM_IN_MINI 1
#include <config.h>
#include <signal.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <math.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <mono/utils/memcheck.h>
#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/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/threads.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/gc-internal.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/verify.h>
#include <mono/metadata/verify-internals.h>
#include <mono/metadata/mempool-internals.h>
#include <mono/metadata/attach.h>
#include <mono/metadata/runtime.h>
#include <mono/metadata/mono-debug-debugger.h>
#include <mono/utils/mono-math.h>
#include <mono/utils/mono-compiler.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-logger-internal.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-path.h>
#include <mono/utils/mono-tls.h>
#include <mono/utils/mono-hwcap.h>
#include <mono/utils/dtrace.h>
#include <mono/utils/mono-signal-handler.h>
#include <mono/utils/mono-threads.h>
#include "mini.h"
#include "mini-llvm.h"
#include "tasklets.h"
#include <string.h>
#include <ctype.h>
#include "trace.h"
#include "version.h"
#include "ir-emit.h"
#include "jit-icalls.h"
#include "mini-gc.h"
#include "debugger-agent.h"
static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex);
static guint32 default_opt = 0;
static gboolean default_opt_set = FALSE;
MonoNativeTlsKey mono_jit_tls_id;
#ifdef MONO_HAVE_FAST_TLS
MONO_FAST_TLS_DECLARE(mono_jit_tls);
#endif
MonoTraceSpec *mono_jit_trace_calls = NULL;
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 */
gboolean mono_use_imt = ARCH_HAVE_IMT;
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;
gboolean mono_do_x86_stack_align = TRUE;
const char *mono_build_date;
gboolean mono_do_signal_chaining;
static gboolean mono_using_xdebug;
static int mini_verbose = 0;
/*
* This flag controls whenever the runtime uses LLVM for JIT compilation, and whenever
* it can load AOT code compiled by LLVM.
*/
gboolean mono_use_llvm = FALSE;
#define mono_jit_lock() EnterCriticalSection (&jit_mutex)
#define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
static CRITICAL_SECTION jit_mutex;
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;
/* Whenever to disable passing/returning small valuetypes in registers for managed methods */
gboolean disable_vtypes_in_regs = FALSE;
gboolean mono_dont_free_global_codeman;
static GSList *tramp_infos;
gpointer
mono_realloc_native_code (MonoCompile *cfg)
{
#if defined(__default_codegen__)
return g_realloc (cfg->native_code, cfg->code_size);
#elif defined(__native_client_codegen__)
guint old_padding;
gpointer native_code;
guint alignment_check;
/* Save the old alignment offset so we can re-align after the realloc. */
old_padding = (guint)(cfg->native_code - cfg->native_code_alloc);
cfg->code_size = NACL_BUNDLE_ALIGN_UP (cfg->code_size);
cfg->native_code_alloc = g_realloc ( cfg->native_code_alloc,
cfg->code_size + kNaClAlignment );
/* Align native_code to next nearest kNaClAlignment byte. */
native_code = (guint)cfg->native_code_alloc + kNaClAlignment;
native_code = (guint)native_code & ~kNaClAlignmentMask;
/* Shift the data to be 32-byte aligned again. */
memmove (native_code, cfg->native_code_alloc + old_padding, cfg->code_size);
alignment_check = (guint)native_code & kNaClAlignmentMask;
g_assert (alignment_check == 0);
return native_code;
#else
g_assert_not_reached ();
return cfg->native_code;
#endif
}
#ifdef __native_client_codegen__
/* Prevent instructions from straddling a 32-byte alignment boundary. */
/* Instructions longer than 32 bytes must be aligned internally. */
/* IN: pcode, instlen */
/* OUT: pcode */
void mono_nacl_align_inst(guint8 **pcode, int instlen) {
int space_in_block;
space_in_block = kNaClAlignment - ((uintptr_t)(*pcode) & kNaClAlignmentMask);
if (G_UNLIKELY (instlen >= kNaClAlignment)) {
g_assert_not_reached();
} else if (instlen > space_in_block) {
*pcode = mono_arch_nacl_pad(*pcode, space_in_block);
}
}
/* Move emitted call sequence to the end of a kNaClAlignment-byte block. */
/* IN: start pointer to start of call sequence */
/* IN: pcode pointer to end of call sequence (current "IP") */
/* OUT: start pointer to the start of the call sequence after padding */
/* OUT: pcode pointer to the end of the call sequence after padding */
void mono_nacl_align_call(guint8 **start, guint8 **pcode) {
const size_t MAX_NACL_CALL_LENGTH = kNaClAlignment;
guint8 copy_of_call[MAX_NACL_CALL_LENGTH];
guint8 *temp;
const size_t length = (size_t)((*pcode)-(*start));
g_assert(length < MAX_NACL_CALL_LENGTH);
memcpy(copy_of_call, *start, length);
temp = mono_nacl_pad_call(*start, (guint8)length);
memcpy(temp, copy_of_call, length);
(*start) = temp;
(*pcode) = temp + length;
}
/* mono_nacl_pad_call(): Insert padding for Native Client call instructions */
/* code pointer to buffer for emitting code */
/* ilength length of call instruction */
guint8 *mono_nacl_pad_call(guint8 *code, guint8 ilength) {
int freeSpaceInBlock = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
int padding = freeSpaceInBlock - ilength;
if (padding < 0) {
/* There isn't enough space in this block for the instruction. */
/* Fill this block and start a new one. */
code = mono_arch_nacl_pad(code, freeSpaceInBlock);
freeSpaceInBlock = kNaClAlignment;
padding = freeSpaceInBlock - ilength;
}
g_assert(ilength > 0);
g_assert(padding >= 0);
g_assert(padding < kNaClAlignment);
if (0 == padding) return code;
return mono_arch_nacl_pad(code, padding);
}
guint8 *mono_nacl_align(guint8 *code) {
int padding = kNaClAlignment - ((uintptr_t)code & kNaClAlignmentMask);
if (padding != kNaClAlignment) code = mono_arch_nacl_pad(code, padding);
return code;
}
void mono_nacl_fix_patches(const guint8 *code, MonoJumpInfo *ji)
{
#ifndef USE_JUMP_TABLES
MonoJumpInfo *patch_info;
for (patch_info = ji; patch_info; patch_info = patch_info->next) {
unsigned char *ip = patch_info->ip.i + code;
ip = mono_arch_nacl_skip_nops(ip);
patch_info->ip.i = ip - code;
}
#endif
}
#endif /* __native_client_codegen__ */
gboolean
mono_running_on_valgrind (void)
{
if (RUNNING_ON_VALGRIND){
#ifdef VALGRIND_JIT_REGISTER_MAP
valgrind_register = TRUE;
#endif
return TRUE;
} else
return FALSE;
}
typedef struct {
MonoExceptionClause *clause;
MonoBasicBlock *basic_block;
int start_offset;
} TryBlockHole;
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;
if (!domain)
domain = mono_get_root_domain ();
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_info (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 (jinfo_get_method (ji), TRUE);
/* FIXME: unused ? */
location = mono_debug_lookup_source_location (jinfo_get_method (ji), (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.
*/
#ifdef __GNUC__
/* Prevent the linker from optimizing this away in embedding setups to help debugging */
__attribute__((used))
#endif
void
mono_print_method_from_ip (void *ip)
{
MonoJitInfo *ji;
char *method;
MonoDebugSourceLocation *source;
MonoDomain *domain = mono_domain_get ();
MonoDomain *target_domain = mono_domain_get ();
FindTrampUserData user_data;
MonoGenericSharingContext*gsctx;
const char *shared_type;
GSList *l;
ji = mini_jit_info_table_find (domain, ip, &target_domain);
if (!ji) {
user_data.ip = ip;
user_data.method = NULL;
mono_domain_lock (domain);
g_hash_table_foreach (domain_jit_info (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);
return;
}
for (l = tramp_infos; l; l = l->next) {
MonoTrampInfo *tinfo = l->data;
if ((guint8*)ip >= tinfo->code && (guint8*)ip <= tinfo->code + tinfo->code_size) {
printf ("IP %p is at offset 0x%x of trampoline '%s'.\n", ip, (int)((guint8*)ip - tinfo->code), tinfo->name);
return;
}
}
g_print ("No method at %p\n", ip);
fflush (stdout);
return;
}
method = mono_method_full_name (jinfo_get_method (ji), TRUE);
source = mono_debug_lookup_source_location (jinfo_get_method (ji), (guint32)((guint8*)ip - (guint8*)ji->code_start), target_domain);
gsctx = mono_jit_info_get_generic_sharing_context (ji);
shared_type = "";
if (gsctx) {
if (gsctx->var_is_vt || gsctx->mvar_is_vt)
shared_type = "gsharedvt ";
else
shared_type = "gshared ";
}
g_print ("IP %p at offset 0x%x of %smethod %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), shared_type, method, ji->code_start, (char*)ji->code_start + ji->code_size, target_domain, target_domain->friendly_name);
if (source)
g_print ("%s:%d\n", source->source_file, source->row);
fflush (stdout);
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)
{
MonoMethod *cmethod;
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;
cmethod = jinfo_get_method (caller);
if ((cmethod->klass == mono_defaults.appdomain_class) &&
(strstr (cmethod->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;
}
}
#if defined(__native_client_codegen__) && defined(__native_client__)
void
mono_nacl_gc()
{
#ifdef __native_client_gc__
__nacl_suspend_thread_if_needed();
#endif
}
/* Given the temporary buffer (allocated by mono_global_codeman_reserve) into
* which we are generating code, return a pointer to the destination in the
* dynamic code segment into which the code will be copied when
* mono_global_codeman_commit is called.
* LOCKING: Acquires the jit lock.
*/
void*
nacl_global_codeman_get_dest (void *data)
{
void *dest;
mono_jit_lock ();
dest = nacl_code_manager_get_code_dest (global_codeman, data);
mono_jit_unlock ();
return dest;
}
void
mono_global_codeman_commit (void *data, int size, int newsize)
{
mono_jit_lock ();
mono_code_manager_commit (global_codeman, data, size, newsize);
mono_jit_unlock ();
}
/*
* Convenience function which calls mono_global_codeman_commit to validate and
* copy the code. The caller sets *buf_base and *buf_size to the start and size
* of the buffer (allocated by mono_global_codeman_reserve), and *code_end to
* the byte after the last instruction byte. On return, *buf_base will point to
* the start of the copied in the code segment, and *code_end will point after
* the end of the copied code.
*/
void
nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
{
guint8 *tmp = nacl_global_codeman_get_dest (*buf_base);
mono_global_codeman_commit (*buf_base, buf_size, *code_end - *buf_base);
*code_end = tmp + (*code_end - *buf_base);
*buf_base = tmp;
}
#else
/* no-op versions of Native Client functions */
void*
nacl_global_codeman_get_dest (void *data)
{
return data;
}
void
mono_global_codeman_commit (void *data, int size, int newsize)
{
}
void
nacl_global_codeman_validate (guint8 **buf_base, int buf_size, guint8 **code_end)
{
}
#endif /* __native_client__ */
/**
* mono_create_unwind_op:
*
* Create an unwind op with the given parameters.
*/
MonoUnwindOp*
mono_create_unwind_op (int when, int tag, int reg, int val)
{
MonoUnwindOp *op = g_new0 (MonoUnwindOp, 1);
op->op = tag;
op->reg = reg;
op->val = val;
op->when = when;
return op;
}
/**
* mono_emit_unwind_op:
*
* Add an unwind op with the given parameters for the list of unwind ops stored in
* cfg->unwind_ops.
*/
void
mono_emit_unwind_op (MonoCompile *cfg, int when, int tag, int reg, int val)
{
MonoUnwindOp *op = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoUnwindOp));
op->op = tag;
op->reg = reg;
op->val = val;
op->when = when;
cfg->unwind_ops = g_slist_append_mempool (cfg->mempool, cfg->unwind_ops, op);
if (cfg->verbose_level > 1) {
switch (tag) {
case DW_CFA_def_cfa:
printf ("CFA: [%x] def_cfa: %s+0x%x\n", when, mono_arch_regname (reg), val);
break;
case DW_CFA_def_cfa_register:
printf ("CFA: [%x] def_cfa_reg: %s\n", when, mono_arch_regname (reg));
break;
case DW_CFA_def_cfa_offset:
printf ("CFA: [%x] def_cfa_offset: 0x%x\n", when, val);
break;
case DW_CFA_offset:
printf ("CFA: [%x] offset: %s at cfa-0x%x\n", when, mono_arch_regname (reg), -val);
break;
}
}
}
MonoJumpInfoToken *
mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context)
{
MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
res->image = image;
res->token = token;
res->has_context = context != NULL;
if (context)
memcpy (&res->context, context, sizeof (MonoGenericContext));
return res;
}
MonoJumpInfoToken *
mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
{
return mono_jump_info_token_new2 (mp, image, token, NULL);
}
/*
* mono_tramp_info_create:
*
* Create a MonoTrampInfo structure from the arguments. This function assumes ownership
* of JI, and UNWIND_OPS.
*/
MonoTrampInfo*
mono_tramp_info_create (const char *name, guint8 *code, guint32 code_size, MonoJumpInfo *ji, GSList *unwind_ops)
{
MonoTrampInfo *info = g_new0 (MonoTrampInfo, 1);
info->name = g_strdup ((char*)name);
info->code = code;
info->code_size = code_size;
info->ji = ji;
info->unwind_ops = unwind_ops;
return info;
}
void
mono_tramp_info_free (MonoTrampInfo *info)
{
GSList *l;
g_free (info->name);
// FIXME: ji
for (l = info->unwind_ops; l; l = l->next)
g_free (l->data);
g_slist_free (info->unwind_ops);
g_free (info);
}
/*
* mono_tramp_info_register:
*
* Remember INFO for use by xdebug, mono_print_method_from_ip (), jit maps, etc.
* INFO can be NULL.
* Frees INFO.
*/
void
mono_tramp_info_register (MonoTrampInfo *info)
{
MonoTrampInfo *copy;
if (!info)
return;
copy = g_new0 (MonoTrampInfo, 1);
copy->code = info->code;
copy->code_size = info->code_size;
copy->name = g_strdup (info->name);
mono_jit_lock ();
tramp_infos = g_slist_prepend (tramp_infos, copy);
mono_jit_unlock ();
mono_save_trampoline_xdebug_info (info);
if (mono_jit_map_is_enabled ())
mono_emit_jit_tramp (info->code, info->code_size, info->name);
mono_tramp_info_free (info);
}
static void
mono_tramp_info_cleanup (void)
{
GSList *l;
for (l = tramp_infos; l; l = l->next) {
MonoTrampInfo *info = l->data;
mono_tramp_info_free (info);
}
g_slist_free (tramp_infos);
}
G_GNUC_UNUSED static void
break_count (void)
{
}
/*
* Runtime debugging tool, use if (debug_count ()) <x> else <y> to do <x> the first COUNT times, then do <y> afterwards.
* Set a breakpoint in break_count () to break the last time <x> is done.
*/
G_GNUC_UNUSED gboolean
mono_debug_count (void)
{
static int count = 0;
count ++;
if (!g_getenv ("COUNT"))
return TRUE;
if (count == atoi (g_getenv ("COUNT"))) {
break_count ();
}
if (count > atoi (g_getenv ("COUNT"))) {
return FALSE;
}
return TRUE;
}
#define MONO_INIT_VARINFO(vi,id) do { \
(vi)->range.first_use.pos.bid = 0xffff; \
(vi)->reg = -1; \
(vi)->idx = (id); \
} while (0)
/**
* mono_unlink_bblock:
*
* Unlink two basic blocks.
*/
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_bblocks_linked:
*
* Return whenever BB1 and BB2 are linked in the CFG.
*/
gboolean
mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
{
int i;
for (i = 0; i < bb1->out_count; ++i) {
if (bb1->out_bb [i] == bb2)
return TRUE;
}
return FALSE;
}
static int
mono_find_block_region_notry (MonoCompile *cfg, int offset)
{
MonoMethodHeader *header = cfg->header;
MonoExceptionClause *clause;
int i;
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
return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
}
}
return -1;
}
/*
* mono_get_block_region_notry:
*
* Return the region corresponding to REGION, ignoring try clauses nested inside
* finally clauses.
*/
int
mono_get_block_region_notry (MonoCompile *cfg, int region)
{
if ((region & (0xf << 4)) == MONO_REGION_TRY) {
MonoMethodHeader *header = cfg->header;
/*
* This can happen if a try clause is nested inside a finally clause.
*/
int clause_index = (region >> 8) - 1;
g_assert (clause_index >= 0 && clause_index < header->num_clauses);
region = mono_find_block_region_notry (cfg, header->clauses [clause_index].try_offset);
}
return region;
}
MonoInst *
mono_find_spvar_for_region (MonoCompile *cfg, int region)
{
region = mono_get_block_region_notry (cfg, region);
return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
}
static void
df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
{
int i;
array [*dfn] = start;
/* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
for (i = 0; i < start->out_count; ++i) {
if (start->out_bb [i]->dfn)
continue;
(*dfn)++;
start->out_bb [i]->dfn = *dfn;
start->out_bb [i]->df_parent = start;
array [*dfn] = start->out_bb [i];
df_visit (start->out_bb [i], dfn, array);
}
}
guint32
mono_reverse_branch_op (guint32 opcode)
{
static const int reverse_map [] = {
CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
};
static const int reverse_fmap [] = {
OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
};
static const int reverse_lmap [] = {
OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
};
static const int reverse_imap [] = {
OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
};
if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
opcode = reverse_map [opcode - CEE_BEQ];
} else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
opcode = reverse_fmap [opcode - OP_FBEQ];
} else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
opcode = reverse_lmap [opcode - OP_LBEQ];
} else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
opcode = reverse_imap [opcode - OP_IBEQ];
} else
g_assert_not_reached ();
return opcode;
}
guint
mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
{
if (type->byref)
return OP_STORE_MEMBASE_REG;
type = mini_replace_type (type);
handle_enum:
switch (type->type) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
return OP_STOREI1_MEMBASE_REG;
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
return OP_STOREI2_MEMBASE_REG;
case MONO_TYPE_I4:
case MONO_TYPE_U4:
return OP_STOREI4_MEMBASE_REG;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
return OP_STORE_MEMBASE_REG;
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
return OP_STORE_MEMBASE_REG;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
return OP_STOREI8_MEMBASE_REG;
case MONO_TYPE_R4:
return OP_STORER4_MEMBASE_REG;
case MONO_TYPE_R8:
return OP_STORER8_MEMBASE_REG;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
type = mono_class_enum_basetype (type->data.klass);
goto handle_enum;
}
if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
return OP_STOREX_MEMBASE;
return OP_STOREV_MEMBASE;
case MONO_TYPE_TYPEDBYREF:
return OP_STOREV_MEMBASE;
case MONO_TYPE_GENERICINST:
type = &type->data.generic_class->container_class->byval_arg;
goto handle_enum;
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
if (mini_type_var_is_vt (cfg, type))
return OP_STOREV_MEMBASE;
else
return OP_STORE_MEMBASE_REG;
default:
g_error ("unknown type 0x%02x in type_to_store_membase", type->type);
}
return -1;
}
guint
mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
{
if (type->byref)
return OP_LOAD_MEMBASE;
type = mini_replace_type (type);
switch (type->type) {
case MONO_TYPE_I1:
return OP_LOADI1_MEMBASE;
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
return OP_LOADU1_MEMBASE;
case MONO_TYPE_I2:
return OP_LOADI2_MEMBASE;
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
return OP_LOADU2_MEMBASE;
case MONO_TYPE_I4:
return OP_LOADI4_MEMBASE;
case MONO_TYPE_U4:
return OP_LOADU4_MEMBASE;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
return OP_LOAD_MEMBASE;
case MONO_TYPE_CLASS: