/
method-to-ir.c
11884 lines (10397 loc) · 355 KB
/
method-to-ir.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
/*
* method-to-ir.c: Convert CIL to the JIT internal representation
*
* 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>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#include <mono/utils/memcheck.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/attrdefs.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/marshal.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/security-manager.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/security-core-clr.h>
#include <mono/metadata/monitor.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/profiler.h>
#include <mono/utils/mono-compiler.h>
#include <mono/metadata/mono-basic-block.h>
#include "mini.h"
#include "trace.h"
#include "ir-emit.h"
#include "jit-icalls.h"
#include "jit.h"
#include "debugger-agent.h"
#define BRANCH_COST 10
#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); \
mono_cfg_set_exception (cfg, 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); \
mono_cfg_set_exception (cfg, 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 > 2) \
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__); \
mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
goto exception_exit; \
} \
} while (0)
#define OUT_OF_MEMORY_FAILURE do { \
mono_cfg_set_exception (cfg, MONO_EXCEPTION_OUT_OF_MEMORY); \
goto exception_exit; \
} while (0)
/* Determine whenever 'ins' represents a load of the 'this' argument */
#define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
static int ldind_to_load_membase (int opcode);
static int stind_to_store_membase (int opcode);
int mono_op_to_op_imm (int opcode);
int mono_op_to_op_imm_noemul (int opcode);
MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
void mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native);
void mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass);
/* helper methods signatures */
static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
static MonoMethodSignature *helper_sig_domain_get = NULL;
static MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
static MonoMethodSignature *helper_sig_generic_class_init_trampoline_llvm = NULL;
static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
static MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline = NULL;
static MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline_llvm = NULL;
/*
* Instruction metadata
*/
#ifdef MINI_OP
#undef MINI_OP
#endif
#ifdef MINI_OP3
#undef MINI_OP3
#endif
#define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
#define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
#define NONE ' '
#define IREG 'i'
#define FREG 'f'
#define VREG 'v'
#define XREG 'x'
#if SIZEOF_REGISTER == 8
#define LREG IREG
#else
#define LREG 'l'
#endif
/* keep in sync with the enum in mini.h */
const char
ins_info[] = {
#include "mini-ops.h"
};
#undef MINI_OP
#undef MINI_OP3
#define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
#define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
/*
* This should contain the index of the last sreg + 1. This is not the same
* as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
*/
const gint8 ins_sreg_counts[] = {
#include "mini-ops.h"
};
#undef MINI_OP
#undef MINI_OP3
#define MONO_INIT_VARINFO(vi,id) do { \
(vi)->range.first_use.pos.bid = 0xffff; \
(vi)->reg = -1; \
(vi)->idx = (id); \
} while (0)
void
mono_inst_set_src_registers (MonoInst *ins, int *regs)
{
ins->sreg1 = regs [0];
ins->sreg2 = regs [1];
ins->sreg3 = regs [2];
}
guint32
mono_alloc_ireg (MonoCompile *cfg)
{
return alloc_ireg (cfg);
}
guint32
mono_alloc_freg (MonoCompile *cfg)
{
return alloc_freg (cfg);
}
guint32
mono_alloc_preg (MonoCompile *cfg)
{
return alloc_preg (cfg);
}
guint32
mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
{
return alloc_dreg (cfg, stack_type);
}
/*
* mono_alloc_ireg_ref:
*
* Allocate an IREG, and mark it as holding a GC ref.
*/
guint32
mono_alloc_ireg_ref (MonoCompile *cfg)
{
return alloc_ireg_ref (cfg);
}
/*
* mono_alloc_ireg_mp:
*
* Allocate an IREG, and mark it as holding a managed pointer.
*/
guint32
mono_alloc_ireg_mp (MonoCompile *cfg)
{
return alloc_ireg_mp (cfg);
}
/*
* mono_alloc_ireg_copy:
*
* Allocate an IREG with the same GC type as VREG.
*/
guint32
mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
{
if (vreg_is_ref (cfg, vreg))
return alloc_ireg_ref (cfg);
else if (vreg_is_mp (cfg, vreg))
return alloc_ireg_mp (cfg);
else
return alloc_ireg (cfg);
}
guint
mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
{
if (type->byref)
return OP_MOVE;
handle_enum:
switch (type->type) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
return OP_MOVE;
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
return OP_MOVE;
case MONO_TYPE_I4:
case MONO_TYPE_U4:
return OP_MOVE;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
return OP_MOVE;
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
return OP_MOVE;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
#if SIZEOF_REGISTER == 8
return OP_MOVE;
#else
return OP_LMOVE;
#endif
case MONO_TYPE_R4:
return OP_FMOVE;
case MONO_TYPE_R8:
return OP_FMOVE;
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_XMOVE;
return OP_VMOVE;
case MONO_TYPE_TYPEDBYREF:
return OP_VMOVE;
case MONO_TYPE_GENERICINST:
type = &type->data.generic_class->container_class->byval_arg;
goto handle_enum;
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
g_assert (cfg->generic_sharing_context);
return OP_MOVE;
default:
g_error ("unknown type 0x%02x in type_to_regstore", type->type);
}
return -1;
}
void
mono_print_bb (MonoBasicBlock *bb, const char *msg)
{
int i;
MonoInst *tree;
printf ("\n%s %d: [IN: ", msg, bb->block_num);
for (i = 0; i < bb->in_count; ++i)
printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
printf (", OUT: ");
for (i = 0; i < bb->out_count; ++i)
printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
printf (" ]\n");
for (tree = bb->code; tree; tree = tree->next)
mono_print_ins_index (-1, tree);
}
void
mono_create_helper_signatures (void)
{
helper_sig_domain_get = mono_create_icall_signature ("ptr");
helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
helper_sig_generic_class_init_trampoline_llvm = mono_create_icall_signature ("void ptr");
helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
helper_sig_monitor_enter_exit_trampoline = mono_create_icall_signature ("void");
helper_sig_monitor_enter_exit_trampoline_llvm = mono_create_icall_signature ("void object");
}
/*
* Can't put this at the beginning, since other files reference stuff from this
* file.
*/
#ifndef DISABLE_JIT
#define UNVERIFIED do { if (mini_get_debug_options ()->break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
#define LOAD_ERROR do { if (mini_get_debug_options ()->break_on_unverified) G_BREAKPOINT (); else goto load_error; } 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)
#if defined(TARGET_X86) || defined(TARGET_AMD64)
#define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
(dest)->dreg = alloc_ireg_mp ((cfg)); \
(dest)->sreg1 = (sr1); \
(dest)->sreg2 = (sr2); \
(dest)->inst_imm = (imm); \
(dest)->backend.shift_amount = (shift); \
MONO_ADD_INS ((cfg)->cbb, (dest)); \
} while (0)
#endif
#if SIZEOF_REGISTER == 8
#define ADD_WIDEN_OP(ins, arg1, arg2) do { \
/* FIXME: Need to add many more cases */ \
if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) { \
MonoInst *widen; \
int dr = alloc_preg (cfg); \
EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg); \
(ins)->sreg2 = widen->dreg; \
} \
} while (0)
#else
#define ADD_WIDEN_OP(ins, arg1, arg2)
#endif
#define ADD_BINOP(op) do { \
MONO_INST_NEW (cfg, ins, (op)); \
sp -= 2; \
ins->sreg1 = sp [0]->dreg; \
ins->sreg2 = sp [1]->dreg; \
type_from_op (ins, sp [0], sp [1]); \
CHECK_TYPE (ins); \
/* Have to insert a widening op */ \
ADD_WIDEN_OP (ins, sp [0], sp [1]); \
ins->dreg = alloc_dreg ((cfg), (ins)->type); \
MONO_ADD_INS ((cfg)->cbb, (ins)); \
*sp++ = mono_decompose_opcode ((cfg), (ins)); \
} while (0)
#define ADD_UNOP(op) do { \
MONO_INST_NEW (cfg, ins, (op)); \
sp--; \
ins->sreg1 = sp [0]->dreg; \
type_from_op (ins, sp [0], NULL); \
CHECK_TYPE (ins); \
(ins)->dreg = alloc_dreg ((cfg), (ins)->type); \
MONO_ADD_INS ((cfg)->cbb, (ins)); \
*sp++ = mono_decompose_opcode (cfg, ins); \
} while (0)
#define ADD_BINCOND(next_block) do { \
MonoInst *cmp; \
sp -= 2; \
MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
cmp->sreg1 = sp [0]->dreg; \
cmp->sreg2 = sp [1]->dreg; \
type_from_op (cmp, sp [0], sp [1]); \
CHECK_TYPE (cmp); \
type_from_op (ins, sp [0], sp [1]); \
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; \
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; \
} \
if (sp != stack_start) { \
handle_stack_args (cfg, stack_start, sp - stack_start); \
CHECK_UNVERIFIABLE (cfg); \
} \
MONO_ADD_INS (bblock, cmp); \
MONO_ADD_INS (bblock, ins); \
} while (0)
/* *
* 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)
printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
else
printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
} else {
if (to->cil_code)
printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
else
printf ("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;
}
}
void
mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
{
link_bblock (cfg, from, to);
}
/**
* 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)
{
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;
}
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)
{
MonoMethodHeader *header = cfg->header;
MonoExceptionClause *clause;
int i;
GList *res = NULL;
for (i = 0; i < header->num_clauses; ++i) {
clause = &header->clauses [i];
if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
(!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
if (clause->flags == type)
res = g_list_append (res, clause);
}
}
return res;
}
static void
mono_create_spvar_for_region (MonoCompile *cfg, int region)
{
MonoInst *var;
var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
if (var)
return;
var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
/* prevent it from being register allocated */
var->flags |= MONO_INST_INDIRECT;
g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
}
MonoInst *
mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
{
return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
}
static MonoInst*
mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
{
MonoInst *var;
var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
if (var)
return var;
var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
/* prevent it from being register allocated */
var->flags |= MONO_INST_INDIRECT;
g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
return var;
}
/*
* Returns the type used in the eval stack when @type is loaded.
* FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
*/
void
type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
{
MonoClass *klass;
inst->klass = klass = mono_class_from_mono_type (type);
if (type->byref) {
inst->type = STACK_MP;
return;
}
handle_enum:
switch (type->type) {
case MONO_TYPE_VOID:
inst->type = STACK_INV;
return;
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
inst->type = STACK_I4;
return;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
inst->type = STACK_PTR;
return;
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
inst->type = STACK_OBJ;
return;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
inst->type = STACK_I8;
return;
case MONO_TYPE_R4:
case MONO_TYPE_R8:
inst->type = STACK_R8;
return;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
type = mono_class_enum_basetype (type->data.klass);
goto handle_enum;
} else {
inst->klass = klass;
inst->type = STACK_VTYPE;
return;
}
case MONO_TYPE_TYPEDBYREF:
inst->klass = mono_defaults.typed_reference_class;
inst->type = STACK_VTYPE;
return;
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);
inst->type = STACK_OBJ;
return;
default:
g_error ("unknown type 0x%02x in eval stack type", type->type);
}
}
/*
* The following tables are used to quickly validate the IL code in type_from_op ().
*/
static const char
bin_num_table [STACK_MAX] [STACK_MAX] = {
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
};
static const char
neg_table [] = {
STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
};
/* reduce the size of this table */
static const char
bin_int_table [STACK_MAX] [STACK_MAX] = {
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
};
static const char
bin_comp_table [STACK_MAX] [STACK_MAX] = {
/* Inv i L p F & O vt */
{0},
{0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
{0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
{0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
{0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
{0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
{0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
{0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
};
/* reduce the size of this table */
static const char
shift_table [STACK_MAX] [STACK_MAX] = {
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
{STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
};
/*
* Tables to map from the non-specific opcode to the matching
* type-specific opcode.
*/
/* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
static const guint16
binops_op_map [STACK_MAX] = {
0, OP_IADD-CEE_ADD, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
};
/* handles from CEE_NEG to CEE_CONV_U8 */
static const guint16
unops_op_map [STACK_MAX] = {
0, OP_INEG-CEE_NEG, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
};
/* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
static const guint16
ovfops_op_map [STACK_MAX] = {
0, OP_ICONV_TO_U2-CEE_CONV_U2, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2
};
/* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
static const guint16
ovf2ops_op_map [STACK_MAX] = {
0, OP_ICONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
};
/* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
static const guint16
ovf3ops_op_map [STACK_MAX] = {
0, OP_ICONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1
};
/* handles from CEE_BEQ to CEE_BLT_UN */
static const guint16
beqops_op_map [STACK_MAX] = {
0, OP_IBEQ-CEE_BEQ, OP_LBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_FBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ
};
/* handles from CEE_CEQ to CEE_CLT_UN */
static const guint16
ceqops_op_map [STACK_MAX] = {
0, OP_ICEQ-OP_CEQ, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_PCEQ-OP_CEQ
};
/*
* Sets ins->type (the type on the eval stack) according to the
* type of the opcode and the arguments to it.
* Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
*
* FIXME: this function sets ins->type unconditionally in some cases, but
* it should set it to invalid for some types (a conv.x on an object)
*/
static void
type_from_op (MonoInst *ins, MonoInst *src1, MonoInst *src2) {
switch (ins->opcode) {
/* binops */
case CEE_ADD:
case CEE_SUB:
case CEE_MUL:
case CEE_DIV:
case CEE_REM:
/* FIXME: check unverifiable args for STACK_MP */
ins->type = bin_num_table [src1->type] [src2->type];
ins->opcode += binops_op_map [ins->type];
break;
case CEE_DIV_UN:
case CEE_REM_UN:
case CEE_AND:
case CEE_OR:
case CEE_XOR:
ins->type = bin_int_table [src1->type] [src2->type];
ins->opcode += binops_op_map [ins->type];
break;
case CEE_SHL:
case CEE_SHR:
case CEE_SHR_UN:
ins->type = shift_table [src1->type] [src2->type];
ins->opcode += binops_op_map [ins->type];
break;
case OP_COMPARE:
case OP_LCOMPARE:
case OP_ICOMPARE:
ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
ins->opcode = OP_LCOMPARE;
else if (src1->type == STACK_R8)
ins->opcode = OP_FCOMPARE;
else
ins->opcode = OP_ICOMPARE;
break;
case OP_ICOMPARE_IMM:
ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
ins->opcode = OP_LCOMPARE_IMM;
break;
case CEE_BEQ:
case CEE_BGE:
case CEE_BGT:
case CEE_BLE:
case CEE_BLT:
case CEE_BNE_UN:
case CEE_BGE_UN:
case CEE_BGT_UN:
case CEE_BLE_UN:
case CEE_BLT_UN:
ins->opcode += beqops_op_map [src1->type];
break;
case OP_CEQ:
ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
ins->opcode += ceqops_op_map [src1->type];
break;
case OP_CGT:
case OP_CGT_UN:
case OP_CLT:
case OP_CLT_UN:
ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
ins->opcode += ceqops_op_map [src1->type];
break;
/* unops */
case CEE_NEG:
ins->type = neg_table [src1->type];
ins->opcode += unops_op_map [ins->type];
break;
case CEE_NOT:
if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
ins->type = src1->type;
else
ins->type = STACK_INV;
ins->opcode += unops_op_map [ins->type];
break;
case CEE_CONV_I1:
case CEE_CONV_I2:
case CEE_CONV_I4:
case CEE_CONV_U4:
ins->type = STACK_I4;
ins->opcode += unops_op_map [src1->type];
break;
case CEE_CONV_R_UN:
ins->type = STACK_R8;
switch (src1->type) {
case STACK_I4:
case STACK_PTR:
ins->opcode = OP_ICONV_TO_R_UN;
break;
case STACK_I8:
ins->opcode = OP_LCONV_TO_R_UN;
break;
}
break;
case CEE_CONV_OVF_I1:
case CEE_CONV_OVF_U1:
case CEE_CONV_OVF_I2:
case CEE_CONV_OVF_U2:
case CEE_CONV_OVF_I4:
case CEE_CONV_OVF_U4:
ins->type = STACK_I4;
ins->opcode += ovf3ops_op_map [src1->type];
break;
case CEE_CONV_OVF_I_UN:
case CEE_CONV_OVF_U_UN:
ins->type = STACK_PTR;
ins->opcode += ovf2ops_op_map [src1->type];
break;
case CEE_CONV_OVF_I1_UN:
case CEE_CONV_OVF_I2_UN:
case CEE_CONV_OVF_I4_UN:
case CEE_CONV_OVF_U1_UN:
case CEE_CONV_OVF_U2_UN:
case CEE_CONV_OVF_U4_UN:
ins->type = STACK_I4;
ins->opcode += ovf2ops_op_map [src1->type];
break;
case CEE_CONV_U:
ins->type = STACK_PTR;
switch (src1->type) {
case STACK_I4:
ins->opcode = OP_ICONV_TO_U;
break;
case STACK_PTR:
case STACK_MP:
#if SIZEOF_VOID_P == 8
ins->opcode = OP_LCONV_TO_U;
#else
ins->opcode = OP_MOVE;
#endif
break;
case STACK_I8:
ins->opcode = OP_LCONV_TO_U;
break;
case STACK_R8:
ins->opcode = OP_FCONV_TO_U;
break;
}
break;
case CEE_CONV_I8:
case CEE_CONV_U8:
ins->type = STACK_I8;
ins->opcode += unops_op_map [src1->type];
break;
case CEE_CONV_OVF_I8:
case CEE_CONV_OVF_U8:
ins->type = STACK_I8;
ins->opcode += ovf3ops_op_map [src1->type];
break;
case CEE_CONV_OVF_U8_UN:
case CEE_CONV_OVF_I8_UN:
ins->type = STACK_I8;
ins->opcode += ovf2ops_op_map [src1->type];
break;
case CEE_CONV_R4:
case CEE_CONV_R8:
ins->type = STACK_R8;
ins->opcode += unops_op_map [src1->type];
break;
case OP_CKFINITE:
ins->type = STACK_R8;
break;
case CEE_CONV_U2:
case CEE_CONV_U1:
ins->type = STACK_I4;
ins->opcode += ovfops_op_map [src1->type];
break;
case CEE_CONV_I:
case CEE_CONV_OVF_I:
case CEE_CONV_OVF_U:
ins->type = STACK_PTR;
ins->opcode += ovfops_op_map [src1->type];
break;
case CEE_ADD_OVF:
case CEE_ADD_OVF_UN:
case CEE_MUL_OVF:
case CEE_MUL_OVF_UN:
case CEE_SUB_OVF:
case CEE_SUB_OVF_UN:
ins->type = bin_num_table [src1->type] [src2->type];
ins->opcode += ovfops_op_map [src1->type];
if (ins->type == STACK_R8)
ins->type = STACK_INV;
break;
case OP_LOAD_MEMBASE:
ins->type = STACK_PTR;
break;
case OP_LOADI1_MEMBASE:
case OP_LOADU1_MEMBASE:
case OP_LOADI2_MEMBASE:
case OP_LOADU2_MEMBASE:
case OP_LOADI4_MEMBASE:
case OP_LOADU4_MEMBASE:
ins->type = STACK_PTR;