/
mini-x86.c
6737 lines (6060 loc) · 194 KB
/
mini-x86.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-x86.c: x86 backend for the Mono code generator
*
* Authors:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
* Patrik Torstensson
*
* Copyright 2003 Ximian, Inc.
* Copyright 2003-2011 Novell Inc.
* Copyright 2011 Xamarin Inc.
*/
#include "mini.h"
#include <string.h>
#include <math.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/gc-internal.h>
#include <mono/utils/mono-math.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-memory-model.h>
#include <mono/utils/mono-hwcap-x86.h>
#include "trace.h"
#include "mini-x86.h"
#include "cpu-x86.h"
#include "ir-emit.h"
#include "mini-gc.h"
#ifndef TARGET_WIN32
#ifdef MONO_XEN_OPT
static gboolean optimize_for_xen = TRUE;
#else
#define optimize_for_xen 0
#endif
#endif
/* This mutex protects architecture specific caches */
#define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
#define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
static CRITICAL_SECTION mini_arch_mutex;
#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
#define ARGS_OFFSET 8
#ifdef TARGET_WIN32
/* Under windows, the default pinvoke calling convention is stdcall */
#define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
#else
#define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_THISCALL))
#endif
#define X86_IS_CALLEE_SAVED_REG(reg) (((reg) == X86_EBX) || ((reg) == X86_EDI) || ((reg) == X86_ESI))
MonoBreakpointInfo
mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
static guint8*
emit_load_aotconst (guint8 *start, guint8 *code, MonoCompile *cfg, MonoJumpInfo **ji, int dreg, int tramp_type, gconstpointer target);
#ifdef __native_client_codegen__
/* Default alignment for Native Client is 32-byte. */
gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
/* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
/* Check that alignment doesn't cross an alignment boundary. */
guint8 *
mono_arch_nacl_pad (guint8 *code, int pad)
{
const int kMaxPadding = 7; /* see x86-codegen.h: x86_padding() */
if (pad == 0) return code;
/* assertion: alignment cannot cross a block boundary */
g_assert(((uintptr_t)code & (~kNaClAlignmentMask)) ==
(((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
while (pad >= kMaxPadding) {
x86_padding (code, kMaxPadding);
pad -= kMaxPadding;
}
if (pad != 0) x86_padding (code, pad);
return code;
}
guint8 *
mono_arch_nacl_skip_nops (guint8 *code)
{
x86_skip_nops (code);
return code;
}
#endif /* __native_client_codegen__ */
/*
* The code generated for sequence points reads from this location, which is
* made read-only when single stepping is enabled.
*/
static gpointer ss_trigger_page;
/* Enabled breakpoints read from this trigger page */
static gpointer bp_trigger_page;
const char*
mono_arch_regname (int reg)
{
switch (reg) {
case X86_EAX: return "%eax";
case X86_EBX: return "%ebx";
case X86_ECX: return "%ecx";
case X86_EDX: return "%edx";
case X86_ESP: return "%esp";
case X86_EBP: return "%ebp";
case X86_EDI: return "%edi";
case X86_ESI: return "%esi";
}
return "unknown";
}
const char*
mono_arch_fregname (int reg)
{
switch (reg) {
case 0:
return "%fr0";
case 1:
return "%fr1";
case 2:
return "%fr2";
case 3:
return "%fr3";
case 4:
return "%fr4";
case 5:
return "%fr5";
case 6:
return "%fr6";
case 7:
return "%fr7";
default:
return "unknown";
}
}
const char *
mono_arch_xregname (int reg)
{
switch (reg) {
case 0:
return "%xmm0";
case 1:
return "%xmm1";
case 2:
return "%xmm2";
case 3:
return "%xmm3";
case 4:
return "%xmm4";
case 5:
return "%xmm5";
case 6:
return "%xmm6";
case 7:
return "%xmm7";
default:
return "unknown";
}
}
void
mono_x86_patch (unsigned char* code, gpointer target)
{
x86_patch (code, (unsigned char*)target);
}
typedef enum {
ArgInIReg,
ArgInFloatSSEReg,
ArgInDoubleSSEReg,
ArgOnStack,
ArgValuetypeInReg,
ArgOnFloatFpStack,
ArgOnDoubleFpStack,
/* gsharedvt argument passed by addr */
ArgGSharedVt,
ArgNone
} ArgStorage;
typedef struct {
gint16 offset;
gint8 reg;
ArgStorage storage;
int nslots;
gboolean is_pair;
/* Only if storage == ArgValuetypeInReg */
ArgStorage pair_storage [2];
gint8 pair_regs [2];
} ArgInfo;
typedef struct {
int nargs;
guint32 stack_usage;
guint32 reg_usage;
guint32 freg_usage;
gboolean need_stack_align;
guint32 stack_align_amount;
gboolean vtype_retaddr;
/* The index of the vret arg in the argument list */
int vret_arg_index;
int vret_arg_offset;
ArgInfo ret;
ArgInfo sig_cookie;
ArgInfo args [1];
} CallInfo;
#define FLOAT_PARAM_REGS 0
static const guint32 thiscall_param_regs [] = { X86_ECX, X86_NREG };
static const guint32 *callconv_param_regs(MonoMethodSignature *sig)
{
if (!sig->pinvoke)
return NULL;
switch (sig->call_convention) {
case MONO_CALL_THISCALL:
return thiscall_param_regs;
default:
return NULL;
}
}
#if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
#define SMALL_STRUCTS_IN_REGS
static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
#endif
static void inline
add_general (guint32 *gr, const guint32 *param_regs, guint32 *stack_size, ArgInfo *ainfo)
{
ainfo->offset = *stack_size;
if (!param_regs || param_regs [*gr] == X86_NREG) {
ainfo->storage = ArgOnStack;
ainfo->nslots = 1;
(*stack_size) += sizeof (gpointer);
}
else {
ainfo->storage = ArgInIReg;
ainfo->reg = param_regs [*gr];
(*gr) ++;
}
}
static void inline
add_general_pair (guint32 *gr, const guint32 *param_regs , guint32 *stack_size, ArgInfo *ainfo)
{
ainfo->offset = *stack_size;
g_assert(!param_regs || param_regs[*gr] == X86_NREG);
ainfo->storage = ArgOnStack;
(*stack_size) += sizeof (gpointer) * 2;
ainfo->nslots = 2;
}
static void inline
add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
{
ainfo->offset = *stack_size;
if (*gr >= FLOAT_PARAM_REGS) {
ainfo->storage = ArgOnStack;
(*stack_size) += is_double ? 8 : 4;
ainfo->nslots = is_double ? 2 : 1;
}
else {
/* A double register */
if (is_double)
ainfo->storage = ArgInDoubleSSEReg;
else
ainfo->storage = ArgInFloatSSEReg;
ainfo->reg = *gr;
(*gr) += 1;
}
}
static void
add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
gboolean is_return,
guint32 *gr, const guint32 *param_regs, guint32 *fr, guint32 *stack_size)
{
guint32 size;
MonoClass *klass;
klass = mono_class_from_mono_type (type);
size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
#ifdef SMALL_STRUCTS_IN_REGS
if (sig->pinvoke && is_return) {
MonoMarshalType *info;
/*
* the exact rules are not very well documented, the code below seems to work with the
* code generated by gcc 3.3.3 -mno-cygwin.
*/
info = mono_marshal_load_type_info (klass);
g_assert (info);
ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
/* Special case structs with only a float member */
if (info->num_fields == 1) {
int ftype = mini_replace_type (info->fields [0].field->type)->type;
if ((info->native_size == 8) && (ftype == MONO_TYPE_R8)) {
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ArgOnDoubleFpStack;
return;
}
if ((info->native_size == 4) && (ftype == MONO_TYPE_R4)) {
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ArgOnFloatFpStack;
return;
}
}
if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ArgInIReg;
ainfo->pair_regs [0] = return_regs [0];
if (info->native_size > 4) {
ainfo->pair_storage [1] = ArgInIReg;
ainfo->pair_regs [1] = return_regs [1];
}
return;
}
}
#endif
if (param_regs && param_regs [*gr] != X86_NREG && !is_return) {
g_assert (size <= 4);
ainfo->storage = ArgValuetypeInReg;
ainfo->reg = param_regs [*gr];
(*gr)++;
return;
}
ainfo->offset = *stack_size;
ainfo->storage = ArgOnStack;
*stack_size += ALIGN_TO (size, sizeof (gpointer));
ainfo->nslots = ALIGN_TO (size, sizeof (gpointer)) / sizeof (gpointer);
}
/*
* get_call_info:
*
* Obtain information about a call according to the calling convention.
* For x86 ELF, see the "System V Application Binary Interface Intel386
* Architecture Processor Supplment, Fourth Edition" document for more
* information.
* For x86 win32, see ???.
*/
static CallInfo*
get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig)
{
guint32 i, gr, fr, pstart;
const guint32 *param_regs;
MonoType *ret_type;
int n = sig->hasthis + sig->param_count;
guint32 stack_size = 0;
gboolean is_pinvoke = sig->pinvoke;
gr = 0;
fr = 0;
cinfo->nargs = n;
param_regs = callconv_param_regs(sig);
/* return value */
{
ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
switch (ret_type->type) {
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_CLASS:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
case MONO_TYPE_STRING:
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = X86_EAX;
break;
case MONO_TYPE_U8:
case MONO_TYPE_I8:
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = X86_EAX;
cinfo->ret.is_pair = TRUE;
break;
case MONO_TYPE_R4:
cinfo->ret.storage = ArgOnFloatFpStack;
break;
case MONO_TYPE_R8:
cinfo->ret.storage = ArgOnDoubleFpStack;
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (ret_type)) {
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = X86_EAX;
break;
}
if (mini_is_gsharedvt_type_gsctx (gsctx, ret_type)) {
cinfo->ret.storage = ArgOnStack;
cinfo->vtype_retaddr = TRUE;
break;
}
/* Fall through */
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_TYPEDBYREF: {
guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, NULL, &tmp_fr, &tmp_stacksize);
if (cinfo->ret.storage == ArgOnStack) {
cinfo->vtype_retaddr = TRUE;
/* The caller passes the address where the value is stored */
}
break;
}
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ret_type));
cinfo->ret.storage = ArgOnStack;
cinfo->vtype_retaddr = TRUE;
break;
case MONO_TYPE_VOID:
cinfo->ret.storage = ArgNone;
break;
default:
g_error ("Can't handle as return value 0x%x", ret_type->type);
}
}
pstart = 0;
/*
* To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
* the first argument, allowing 'this' to be always passed in the first arg reg.
* Also do this if the first argument is a reference type, since virtual calls
* are sometimes made using calli without sig->hasthis set, like in the delegate
* invoke wrappers.
*/
if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
if (sig->hasthis) {
add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
} else {
add_general (&gr, param_regs, &stack_size, &cinfo->args [sig->hasthis + 0]);
pstart = 1;
}
cinfo->vret_arg_offset = stack_size;
add_general (&gr, NULL, &stack_size, &cinfo->ret);
cinfo->vret_arg_index = 1;
} else {
/* this */
if (sig->hasthis)
add_general (&gr, param_regs, &stack_size, cinfo->args + 0);
if (cinfo->vtype_retaddr)
add_general (&gr, NULL, &stack_size, &cinfo->ret);
}
if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
fr = FLOAT_PARAM_REGS;
/* Emit the signature cookie just before the implicit arguments */
add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
}
for (i = pstart; i < sig->param_count; ++i) {
ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
MonoType *ptype;
if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
/* We allways pass the sig cookie on the stack for simplicity */
/*
* Prevent implicit arguments + the sig cookie from being passed
* in registers.
*/
fr = FLOAT_PARAM_REGS;
/* Emit the signature cookie just before the implicit arguments */
add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
}
if (sig->params [i]->byref) {
add_general (&gr, param_regs, &stack_size, ainfo);
continue;
}
ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
switch (ptype->type) {
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
add_general (&gr, param_regs, &stack_size, ainfo);
break;
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_CHAR:
add_general (&gr, param_regs, &stack_size, ainfo);
break;
case MONO_TYPE_I4:
case MONO_TYPE_U4:
add_general (&gr, param_regs, &stack_size, ainfo);
break;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_CLASS:
case MONO_TYPE_OBJECT:
case MONO_TYPE_STRING:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
add_general (&gr, param_regs, &stack_size, ainfo);
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (ptype)) {
add_general (&gr, param_regs, &stack_size, ainfo);
break;
}
if (mini_is_gsharedvt_type_gsctx (gsctx, ptype)) {
/* gsharedvt arguments are passed by ref */
add_general (&gr, param_regs, &stack_size, ainfo);
g_assert (ainfo->storage == ArgOnStack);
ainfo->storage = ArgGSharedVt;
break;
}
/* Fall through */
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_TYPEDBYREF:
add_valuetype (gsctx, sig, ainfo, ptype, FALSE, &gr, param_regs, &fr, &stack_size);
break;
case MONO_TYPE_U8:
case MONO_TYPE_I8:
add_general_pair (&gr, param_regs, &stack_size, ainfo);
break;
case MONO_TYPE_R4:
add_float (&fr, &stack_size, ainfo, FALSE);
break;
case MONO_TYPE_R8:
add_float (&fr, &stack_size, ainfo, TRUE);
break;
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
/* gsharedvt arguments are passed by ref */
g_assert (mini_is_gsharedvt_type_gsctx (gsctx, ptype));
add_general (&gr, param_regs, &stack_size, ainfo);
g_assert (ainfo->storage == ArgOnStack);
ainfo->storage = ArgGSharedVt;
break;
default:
g_error ("unexpected type 0x%x", ptype->type);
g_assert_not_reached ();
}
}
if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
fr = FLOAT_PARAM_REGS;
/* Emit the signature cookie just before the implicit arguments */
add_general (&gr, param_regs, &stack_size, &cinfo->sig_cookie);
}
if (mono_do_x86_stack_align && (stack_size % MONO_ARCH_FRAME_ALIGNMENT) != 0) {
cinfo->need_stack_align = TRUE;
cinfo->stack_align_amount = MONO_ARCH_FRAME_ALIGNMENT - (stack_size % MONO_ARCH_FRAME_ALIGNMENT);
stack_size += cinfo->stack_align_amount;
}
cinfo->stack_usage = stack_size;
cinfo->reg_usage = gr;
cinfo->freg_usage = fr;
return cinfo;
}
static CallInfo*
get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
{
int n = sig->hasthis + sig->param_count;
CallInfo *cinfo;
if (mp)
cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
else
cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
return get_call_info_internal (gsctx, cinfo, sig);
}
/*
* mono_arch_get_argument_info:
* @csig: a method signature
* @param_count: the number of parameters to consider
* @arg_info: an array to store the result infos
*
* Gathers information on parameters such as size, alignment and
* padding. arg_info should be large enought to hold param_count + 1 entries.
*
* Returns the size of the argument area on the stack.
* This should be signal safe, since it is called from
* mono_arch_find_jit_info ().
* FIXME: The metadata calls might not be signal safe.
*/
int
mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
{
int len, k, args_size = 0;
int size, pad;
guint32 align;
int offset = 8;
CallInfo *cinfo;
/* Avoid g_malloc as it is not signal safe */
len = sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1));
cinfo = (CallInfo*)g_newa (guint8*, len);
memset (cinfo, 0, len);
cinfo = get_call_info_internal (gsctx, cinfo, csig);
arg_info [0].offset = offset;
if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 0) {
args_size += sizeof (gpointer);
offset += 4;
}
if (csig->hasthis) {
args_size += sizeof (gpointer);
offset += 4;
}
if (cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && csig->hasthis) {
/* Emitted after this */
args_size += sizeof (gpointer);
offset += 4;
}
arg_info [0].size = args_size;
for (k = 0; k < param_count; k++) {
size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
/* ignore alignment for now */
align = 1;
args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
arg_info [k].pad = pad;
args_size += size;
arg_info [k + 1].pad = 0;
arg_info [k + 1].size = size;
offset += pad;
arg_info [k + 1].offset = offset;
offset += size;
if (k == 0 && cinfo->vtype_retaddr && cinfo->vret_arg_index == 1 && !csig->hasthis) {
/* Emitted after the first arg */
args_size += sizeof (gpointer);
offset += 4;
}
}
if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
align = MONO_ARCH_FRAME_ALIGNMENT;
else
align = 4;
args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
arg_info [k].pad = pad;
return args_size;
}
gboolean
mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
{
MonoType *callee_ret;
CallInfo *c1, *c2;
gboolean res;
if (cfg->compile_aot && !cfg->full_aot)
/* OP_TAILCALL doesn't work with AOT */
return FALSE;
c1 = get_call_info (NULL, NULL, caller_sig);
c2 = get_call_info (NULL, NULL, callee_sig);
/*
* Tail calls with more callee stack usage than the caller cannot be supported, since
* the extra stack space would be left on the stack after the tail call.
*/
res = c1->stack_usage >= c2->stack_usage;
callee_ret = mini_replace_type (callee_sig->ret);
if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
/* An address on the callee's stack is passed as the first argument */
res = FALSE;
g_free (c1);
g_free (c2);
return res;
}
/*
* Initialize the cpu to execute managed code.
*/
void
mono_arch_cpu_init (void)
{
/* spec compliance requires running with double precision */
#ifndef _MSC_VER
guint16 fpcw;
__asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
fpcw &= ~X86_FPCW_PRECC_MASK;
fpcw |= X86_FPCW_PREC_DOUBLE;
__asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
__asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
#else
_control87 (_PC_53, MCW_PC);
#endif
}
/*
* Initialize architecture specific code.
*/
void
mono_arch_init (void)
{
InitializeCriticalSection (&mini_arch_mutex);
ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
mono_aot_register_jit_icall ("mono_x86_throw_exception", mono_x86_throw_exception);
mono_aot_register_jit_icall ("mono_x86_throw_corlib_exception", mono_x86_throw_corlib_exception);
#if defined(ENABLE_GSHAREDVT)
mono_aot_register_jit_icall ("mono_x86_start_gsharedvt_call", mono_x86_start_gsharedvt_call);
#endif
}
/*
* Cleanup architecture specific code.
*/
void
mono_arch_cleanup (void)
{
if (ss_trigger_page)
mono_vfree (ss_trigger_page, mono_pagesize ());
if (bp_trigger_page)
mono_vfree (bp_trigger_page, mono_pagesize ());
DeleteCriticalSection (&mini_arch_mutex);
}
/*
* This function returns the optimizations supported on this cpu.
*/
guint32
mono_arch_cpu_optimizations (guint32 *exclude_mask)
{
#if !defined(__native_client__)
guint32 opts = 0;
*exclude_mask = 0;
if (mono_hwcap_x86_has_cmov) {
opts |= MONO_OPT_CMOV;
if (mono_hwcap_x86_has_fcmov)
opts |= MONO_OPT_FCMOV;
else
*exclude_mask |= MONO_OPT_FCMOV;
} else {
*exclude_mask |= MONO_OPT_CMOV;
}
if (mono_hwcap_x86_has_sse2)
opts |= MONO_OPT_SSE2;
else
*exclude_mask |= MONO_OPT_SSE2;
#ifdef MONO_ARCH_SIMD_INTRINSICS
/*SIMD intrinsics require at least SSE2.*/
if (!mono_hwcap_x86_has_sse2)
*exclude_mask |= MONO_OPT_SIMD;
#endif
return opts;
#else
return MONO_OPT_CMOV | MONO_OPT_FCMOV | MONO_OPT_SSE2;
#endif
}
/*
* This function test for all SSE functions supported.
*
* Returns a bitmask corresponding to all supported versions.
*
*/
guint32
mono_arch_cpu_enumerate_simd_versions (void)
{
guint32 sse_opts = 0;
if (mono_hwcap_x86_has_sse1)
sse_opts |= SIMD_VERSION_SSE1;
if (mono_hwcap_x86_has_sse2)
sse_opts |= SIMD_VERSION_SSE2;
if (mono_hwcap_x86_has_sse3)
sse_opts |= SIMD_VERSION_SSE3;
if (mono_hwcap_x86_has_ssse3)
sse_opts |= SIMD_VERSION_SSSE3;
if (mono_hwcap_x86_has_sse41)
sse_opts |= SIMD_VERSION_SSE41;
if (mono_hwcap_x86_has_sse42)
sse_opts |= SIMD_VERSION_SSE42;
if (mono_hwcap_x86_has_sse4a)
sse_opts |= SIMD_VERSION_SSE4a;
return sse_opts;
}
/*
* Determine whenever the trap whose info is in SIGINFO is caused by
* integer overflow.
*/
gboolean
mono_arch_is_int_overflow (void *sigctx, void *info)
{
MonoContext ctx;
guint8* ip;
mono_arch_sigctx_to_monoctx (sigctx, &ctx);
ip = (guint8*)ctx.eip;
if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
gint32 reg;
/* idiv REG */
switch (x86_modrm_rm (ip [1])) {
case X86_EAX:
reg = ctx.eax;
break;
case X86_ECX:
reg = ctx.ecx;
break;
case X86_EDX:
reg = ctx.edx;
break;
case X86_EBX:
reg = ctx.ebx;
break;
case X86_ESI:
reg = ctx.esi;
break;
case X86_EDI:
reg = ctx.edi;
break;
default:
g_assert_not_reached ();
reg = -1;
}
if (reg == -1)
return TRUE;
}
return FALSE;
}
GList *
mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
{
GList *vars = NULL;
int i;
for (i = 0; i < cfg->num_varinfo; i++) {
MonoInst *ins = cfg->varinfo [i];
MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
/* unused vars */
if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
continue;
if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
(ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
continue;
/* we dont allocate I1 to registers because there is no simply way to sign extend
* 8bit quantities in caller saved registers on x86 */
if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
g_assert (MONO_VARINFO (cfg, i)->reg == -1);
g_assert (i == vmv->idx);
vars = g_list_prepend (vars, vmv);
}
}
vars = mono_varlist_sort (cfg, vars, 0);
return vars;
}
GList *
mono_arch_get_global_int_regs (MonoCompile *cfg)
{
GList *regs = NULL;
/* we can use 3 registers for global allocation */
regs = g_list_prepend (regs, (gpointer)X86_EBX);
regs = g_list_prepend (regs, (gpointer)X86_ESI);
regs = g_list_prepend (regs, (gpointer)X86_EDI);
return regs;
}
/*
* mono_arch_regalloc_cost:
*
* Return the cost, in number of memory references, of the action of
* allocating the variable VMV into a register during global register
* allocation.
*/
guint32
mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
{
MonoInst *ins = cfg->varinfo [vmv->idx];
if (cfg->method->save_lmf)
/* The register is already saved */
return (ins->opcode == OP_ARG) ? 1 : 0;
else
/* push+pop+possible load if it is an argument */
return (ins->opcode == OP_ARG) ? 3 : 2;
}
static void
set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
{
static int inited = FALSE;
static int count = 0;
if (cfg->arch.need_stack_frame_inited) {
g_assert (cfg->arch.need_stack_frame == flag);
return;
}
cfg->arch.need_stack_frame = flag;
cfg->arch.need_stack_frame_inited = TRUE;
if (flag)
return;
if (!inited) {
mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
inited = TRUE;
}
++count;
//g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
}
static gboolean
needs_stack_frame (MonoCompile *cfg)
{
MonoMethodSignature *sig;
MonoMethodHeader *header;
gboolean result = FALSE;
#if defined(__APPLE__)