-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini.c
5544 lines (4722 loc) · 162 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_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
#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/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/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 "tasklets.h"
#include <string.h>
#include <ctype.h>
#include "trace.h"
#include "version.h"
#include "jit-icalls.h"
#include "debug-mini.h"
#include "mini-gc.h"
static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException **ex);
/* helper methods signature */
/* FIXME: Make these static again */
MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
MonoMethodSignature *helper_sig_domain_get = NULL;
MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline = 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;
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;
/* Statistics */
#ifdef ENABLE_LLVM
static int methods_with_llvm, methods_without_llvm;
#endif
#define mono_jit_lock() EnterCriticalSection (&jit_mutex)
#define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
static CRITICAL_SECTION jit_mutex;
static MonoCodeManager *global_codeman = NULL;
/* FIXME: Make this static again */
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;
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_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 (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_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);
}
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;
}
}
/**
* 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);
}
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);
}
#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)
{
MonoMethod *method = cfg->method;
MonoMethodHeader *header = mono_method_get_header (method);
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;
}
MonoInst *
mono_find_spvar_for_region (MonoCompile *cfg, int region)
{
if ((region & (0xf << 4)) == MONO_REGION_TRY) {
MonoMethodHeader *header = mono_method_get_header (cfg->method);
/*
* 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 g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
}
static MonoInst *
mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
{
return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
}
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;
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:
/* FIXME: all the arguments must be references for now,
* later look inside cfg and see if the arg num is
* really a reference
*/
g_assert (cfg->generic_sharing_context);
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;
switch (mono_type_get_underlying_type (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:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
return OP_LOAD_MEMBASE;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
return OP_LOADI8_MEMBASE;
case MONO_TYPE_R4:
return OP_LOADR4_MEMBASE;
case MONO_TYPE_R8:
return OP_LOADR8_MEMBASE;
case MONO_TYPE_VALUETYPE:
if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
return OP_LOADX_MEMBASE;
case MONO_TYPE_TYPEDBYREF:
return OP_LOADV_MEMBASE;
case MONO_TYPE_GENERICINST:
if (mono_type_generic_inst_is_valuetype (type))
return OP_LOADV_MEMBASE;
else
return OP_LOAD_MEMBASE;
break;
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
/* FIXME: all the arguments must be references for now,
* later look inside cfg and see if the arg num is
* really a reference
*/
g_assert (cfg->generic_sharing_context);
return OP_LOAD_MEMBASE;
default:
g_error ("unknown type 0x%02x in type_to_load_membase", type->type);
}
return -1;
}
static guint
mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
{
if (cfg->generic_sharing_context && !type->byref) {
/* FIXME: all the arguments must be references for now,
* later look inside cfg and see if the arg num is
* really a reference
*/
if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
return CEE_LDIND_REF;
}
return mono_type_to_ldind (type);
}
guint
mini_type_to_stind (MonoCompile* cfg, MonoType *type)
{
if (cfg->generic_sharing_context && !type->byref) {
/* FIXME: all the arguments must be references for now,
* later look inside cfg and see if the arg num is
* really a reference
*/
if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
return CEE_STIND_REF;
}
return mono_type_to_stind (type);
}
int
mono_op_imm_to_op (int opcode)
{
switch (opcode) {
case OP_ADD_IMM:
#if SIZEOF_REGISTER == 4
return OP_IADD;
#else
return OP_LADD;
#endif
case OP_IADD_IMM:
return OP_IADD;
case OP_LADD_IMM:
return OP_LADD;
case OP_ISUB_IMM:
return OP_ISUB;
case OP_LSUB_IMM:
return OP_LSUB;
case OP_IMUL_IMM:
return OP_IMUL;
case OP_AND_IMM:
#if SIZEOF_REGISTER == 4
return OP_IAND;
#else
return OP_LAND;
#endif
case OP_IAND_IMM:
return OP_IAND;
case OP_LAND_IMM:
return OP_LAND;
case OP_IOR_IMM:
return OP_IOR;
case OP_LOR_IMM:
return OP_LOR;
case OP_IXOR_IMM:
return OP_IXOR;
case OP_LXOR_IMM:
return OP_LXOR;
case OP_ISHL_IMM:
return OP_ISHL;
case OP_LSHL_IMM:
return OP_LSHL;
case OP_ISHR_IMM:
return OP_ISHR;
case OP_LSHR_IMM:
return OP_LSHR;
case OP_ISHR_UN_IMM:
return OP_ISHR_UN;
case OP_LSHR_UN_IMM:
return OP_LSHR_UN;
case OP_IDIV_IMM:
return OP_IDIV;
case OP_IDIV_UN_IMM:
return OP_IDIV_UN;
case OP_IREM_UN_IMM:
return OP_IREM_UN;
case OP_IREM_IMM:
return OP_IREM;
case OP_DIV_IMM:
#if SIZEOF_REGISTER == 4
return OP_IDIV;
#else
return OP_LDIV;
#endif
case OP_REM_IMM:
#if SIZEOF_REGISTER == 4
return OP_IREM;
#else
return OP_LREM;
#endif
case OP_ADDCC_IMM:
return OP_ADDCC;
case OP_ADC_IMM:
return OP_ADC;
case OP_SUBCC_IMM:
return OP_SUBCC;
case OP_SBB_IMM:
return OP_SBB;
case OP_IADC_IMM:
return OP_IADC;
case OP_ISBB_IMM:
return OP_ISBB;
case OP_COMPARE_IMM:
return OP_COMPARE;
case OP_ICOMPARE_IMM:
return OP_ICOMPARE;
case OP_LOCALLOC_IMM:
return OP_LOCALLOC;
default:
printf ("%s\n", mono_inst_name (opcode));
g_assert_not_reached ();
return -1;
}
}
/*
* mono_decompose_op_imm:
*
* Replace the OP_.._IMM INS with its non IMM variant.
*/
void
mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
{
MonoInst *temp;
MONO_INST_NEW (cfg, temp, OP_ICONST);
temp->inst_c0 = ins->inst_imm;
temp->dreg = mono_alloc_ireg (cfg);
mono_bblock_insert_before_ins (bb, ins, temp);
ins->opcode = mono_op_imm_to_op (ins->opcode);
if (ins->opcode == OP_LOCALLOC)
ins->sreg1 = temp->dreg;
else
ins->sreg2 = temp->dreg;
bb->max_vreg = MAX (bb->max_vreg, cfg->next_vreg);
}
static void
set_vreg_to_inst (MonoCompile *cfg, int vreg, MonoInst *inst)
{
if (vreg >= cfg->vreg_to_inst_len) {
MonoInst **tmp = cfg->vreg_to_inst;
int size = cfg->vreg_to_inst_len;
while (vreg >= cfg->vreg_to_inst_len)
cfg->vreg_to_inst_len = cfg->vreg_to_inst_len ? cfg->vreg_to_inst_len * 2 : 32;
cfg->vreg_to_inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * cfg->vreg_to_inst_len);
if (size)
memcpy (cfg->vreg_to_inst, tmp, size * sizeof (MonoInst*));
}
cfg->vreg_to_inst [vreg] = inst;
}
#define mono_type_is_long(type) (!(type)->byref && ((mono_type_get_underlying_type (type)->type == MONO_TYPE_I8) || (mono_type_get_underlying_type (type)->type == MONO_TYPE_U8)))
#define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
#ifdef DISABLE_JIT
MonoInst*
mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
{
return NULL;
}
#else
MonoInst*
mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode, int vreg)
{
MonoInst *inst;
int num = cfg->num_varinfo;
gboolean regpair;
if ((num + 1) >= cfg->varinfo_count) {
int orig_count = cfg->varinfo_count;
cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 64;
cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
cfg->vars = (MonoMethodVar *)g_realloc (cfg->vars, sizeof (MonoMethodVar) * cfg->varinfo_count);
memset (&cfg->vars [orig_count], 0, (cfg->varinfo_count - orig_count) * sizeof (MonoMethodVar));
}
mono_jit_stats.allocate_var++;
MONO_INST_NEW (cfg, inst, opcode);
inst->inst_c0 = num;
inst->inst_vtype = type;
inst->klass = mono_class_from_mono_type (type);
type_to_eval_stack_type (cfg, type, inst);
/* if set to 1 the variable is native */
inst->backend.is_pinvoke = 0;
inst->dreg = vreg;
cfg->varinfo [num] = inst;
MONO_INIT_VARINFO (&cfg->vars [num], num);
MONO_VARINFO (cfg, num)->vreg = vreg;
if (vreg != -1)
set_vreg_to_inst (cfg, vreg, inst);
#if SIZEOF_REGISTER == 4
#ifdef MONO_ARCH_SOFT_FLOAT
regpair = mono_type_is_long (type) || mono_type_is_float (type);
#else
regpair = mono_type_is_long (type);
#endif
#else
regpair = FALSE;
#endif
if (regpair) {
MonoInst *tree;
/*
* These two cannot be allocated using create_var_for_vreg since that would
* put it into the cfg->varinfo array, confusing many parts of the JIT.
*/
/*
* Set flags to VOLATILE so SSA skips it.
*/
if (cfg->verbose_level >= 4) {
printf (" Create LVAR R%d (R%d, R%d)\n", inst->dreg, inst->dreg + 1, inst->dreg + 2);
}
#ifdef MONO_ARCH_SOFT_FLOAT
if (cfg->opt & MONO_OPT_SSA) {
if (mono_type_is_float (type))
inst->flags = MONO_INST_VOLATILE;
}
#endif
/* Allocate a dummy MonoInst for the first vreg */
MONO_INST_NEW (cfg, tree, OP_LOCAL);
tree->dreg = inst->dreg + 1;
if (cfg->opt & MONO_OPT_SSA)
tree->flags = MONO_INST_VOLATILE;
tree->inst_c0 = num;
tree->type = STACK_I4;
tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
tree->klass = mono_class_from_mono_type (tree->inst_vtype);
set_vreg_to_inst (cfg, inst->dreg + 1, tree);
/* Allocate a dummy MonoInst for the second vreg */
MONO_INST_NEW (cfg, tree, OP_LOCAL);
tree->dreg = inst->dreg + 2;
if (cfg->opt & MONO_OPT_SSA)
tree->flags = MONO_INST_VOLATILE;
tree->inst_c0 = num;
tree->type = STACK_I4;
tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
tree->klass = mono_class_from_mono_type (tree->inst_vtype);
set_vreg_to_inst (cfg, inst->dreg + 2, tree);
}
cfg->num_varinfo++;
if (cfg->verbose_level > 2)
g_print ("created temp %d (R%d) of type %s\n", num, vreg, mono_type_get_name (type));
return inst;
}
MonoInst*
mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
{
int dreg;
if (mono_type_is_long (type))
dreg = mono_alloc_dreg (cfg, STACK_I8);
#ifdef MONO_ARCH_SOFT_FLOAT
else if (mono_type_is_float (type))
dreg = mono_alloc_dreg (cfg, STACK_R8);
#endif
else
/* All the others are unified */
dreg = mono_alloc_preg (cfg);
return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
}
/*
* Transform a MonoInst into a load from the variable of index var_index.
*/
void
mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
memset (dest, 0, sizeof (MonoInst));
dest->ssa_op = MONO_SSA_LOAD;
dest->inst_i0 = cfg->varinfo [var_index];
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;
}
#endif
static MonoType*
type_from_stack_type (MonoInst *ins) {
switch (ins->type) {
case STACK_I4: return &mono_defaults.int32_class->byval_arg;
case STACK_I8: return &mono_defaults.int64_class->byval_arg;
case STACK_PTR: return &mono_defaults.int_class->byval_arg;