-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathmini-amd64.c
9214 lines (8275 loc) · 273 KB
/
mini-amd64.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
/**
* \file
* AMD64 backend for the Mono code generator
*
* Based on mini-x86.c.
*
* Authors:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
* Patrik Torstensson
* Zoltan Varga (vargaz@gmail.com)
* Johan Lorensson (lateralusx.github@gmail.com)
*
* (C) 2003 Ximian, Inc.
* Copyright 2003-2011 Novell, Inc (http://www.novell.com)
* Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include "mini.h"
#include <string.h>
#include <math.h>
#include <assert.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <mono/metadata/abi-details.h>
#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-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/utils/mono-math.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-memory-model.h>
#include <mono/utils/mono-tls.h>
#include <mono/utils/mono-hwcap.h>
#include <mono/utils/mono-threads.h>
#include <mono/utils/unlocked.h>
#include "interp/interp.h"
#include "ir-emit.h"
#include "mini-amd64.h"
#include "cpu-amd64.h"
#include "mini-gc.h"
#include "mini-runtime.h"
#include "aot-runtime.h"
MONO_DISABLE_WARNING(4127) /* conditional expression is constant */
static GENERATE_TRY_GET_CLASS_WITH_CACHE (math, "System", "Math")
#define IS_IMM32(val) ((((guint64)val) >> 32) == 0)
#define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
/* The single step trampoline */
static gpointer ss_trampoline;
/* The breakpoint trampoline */
static gpointer bp_trampoline;
/* Offset between fp and the first argument in the callee */
#define ARGS_OFFSET 16
#define GP_SCRATCH_REG AMD64_R11
/* Max number of bblocks before we bail from using more advanced branch placement code */
#define MAX_BBLOCKS_FOR_BRANCH_OPTS 800
/*
* AMD64 register usage:
* - callee saved registers are used for global register allocation
* - %r11 is used for materializing 64 bit constants in opcodes
* - the rest is used for local allocation
*/
/*
* Floating point comparison results:
* ZF PF CF
* A > B 0 0 0
* A < B 0 0 1
* A = B 1 0 0
* A > B 0 0 0
* UNORDERED 1 1 1
*/
const char*
mono_arch_regname (int reg)
{
switch (reg) {
case AMD64_RAX: return "%rax";
case AMD64_RBX: return "%rbx";
case AMD64_RCX: return "%rcx";
case AMD64_RDX: return "%rdx";
case AMD64_RSP: return "%rsp";
case AMD64_RBP: return "%rbp";
case AMD64_RDI: return "%rdi";
case AMD64_RSI: return "%rsi";
case AMD64_R8: return "%r8";
case AMD64_R9: return "%r9";
case AMD64_R10: return "%r10";
case AMD64_R11: return "%r11";
case AMD64_R12: return "%r12";
case AMD64_R13: return "%r13";
case AMD64_R14: return "%r14";
case AMD64_R15: return "%r15";
}
return "unknown";
}
static const char * const packed_xmmregs [] = {
"p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
"p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
};
static const char * const single_xmmregs [] = {
"s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
"s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
};
const char*
mono_arch_fregname (int reg)
{
if (reg < AMD64_XMM_NREG)
return single_xmmregs [reg];
else
return "unknown";
}
const char *
mono_arch_xregname (int reg)
{
if (reg < AMD64_XMM_NREG)
return packed_xmmregs [reg];
else
return "unknown";
}
static gboolean
debug_omit_fp (void)
{
#if 0
return mono_debug_count ();
#else
return TRUE;
#endif
}
static gboolean
amd64_is_near_call (guint8 *code)
{
/* Skip REX */
if ((code [0] >= 0x40) && (code [0] <= 0x4f))
code += 1;
return code [0] == 0xe8;
}
static gboolean
amd64_use_imm32 (gint64 val)
{
if (mini_debug_options.single_imm_size)
return FALSE;
return amd64_is_imm32 (val);
}
void
mono_x86_patch (unsigned char* code, gpointer target)
{
mono_x86_patch_inline (code, target);
}
static void
amd64_patch (unsigned char* code, gpointer target)
{
// NOTE: Sometimes code has just been generated, is not running yet,
// and has no alignment requirements. Sometimes it could be running while we patch it,
// and there are alignment requirements.
// FIXME Assert alignment.
guint8 rex = 0;
/* Skip REX */
if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
rex = code [0];
code += 1;
}
if ((code [0] & 0xf8) == 0xb8) {
/* amd64_set_reg_template */
*(guint64*)(code + 1) = (guint64)target;
}
else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
/* mov 0(%rip), %dreg */
g_assert (!1); // Historical code was incorrect.
ptrdiff_t const offset = (guchar*)target - (code + 6);
g_assert (offset == (gint32)offset);
*(gint32*)(code + 2) = (gint32)offset;
}
else if (code [0] == 0xff && (code [1] == 0x15 || code [1] == 0x25)) {
/* call or jmp *<OFFSET>(%rip) */
// Patch the data, not the code.
g_assert (!2); // For possible use later.
*(void**)(code + 6 + *(gint32*)(code + 2)) = target;
}
else
x86_patch (code, target);
}
void
mono_amd64_patch (unsigned char* code, gpointer target)
{
amd64_patch (code, target);
}
#define DEBUG(a) if (cfg->verbose_level > 1) a
static void inline
add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
{
ainfo->offset = GUINT32_TO_INT16 (*stack_size);
if (*gr >= PARAM_REGS) {
ainfo->storage = ArgOnStack;
ainfo->arg_size = sizeof (target_mgreg_t);
/* Since the same stack slot size is used for all arg */
/* types, it needs to be big enough to hold them all */
(*stack_size) += sizeof (target_mgreg_t);
} else {
ainfo->storage = ArgInIReg;
ainfo->reg = GINT32_TO_UINT8 (param_regs [*gr]);
(*gr) ++;
}
}
static void inline
add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
{
ainfo->offset = GUINT32_TO_INT16 (*stack_size);
if (*gr >= FLOAT_PARAM_REGS) {
ainfo->storage = ArgOnStack;
ainfo->arg_size = sizeof (target_mgreg_t);
/* Since the same stack slot size is used for both float */
/* types, it needs to be big enough to hold them both */
(*stack_size) += sizeof (target_mgreg_t);
} else {
/* A double register */
if (is_double)
ainfo->storage = ArgInDoubleSSEReg;
else
ainfo->storage = ArgInFloatSSEReg;
ainfo->reg = GUINT32_TO_UINT8 (*gr);
(*gr) += 1;
}
}
typedef enum ArgumentClass {
ARG_CLASS_NO_CLASS,
ARG_CLASS_MEMORY,
ARG_CLASS_INTEGER,
ARG_CLASS_SSE
} ArgumentClass;
static ArgumentClass
merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
{
ArgumentClass class2 = ARG_CLASS_NO_CLASS;
MonoType *ptype;
ptype = mini_get_underlying_type (type);
switch (ptype->type) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_OBJECT:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
class2 = ARG_CLASS_INTEGER;
break;
case MONO_TYPE_R4:
case MONO_TYPE_R8:
#ifdef TARGET_WIN32
class2 = ARG_CLASS_INTEGER;
#else
class2 = ARG_CLASS_SSE;
#endif
break;
case MONO_TYPE_TYPEDBYREF:
g_assert_not_reached ();
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (ptype)) {
class2 = ARG_CLASS_INTEGER;
break;
}
/* fall through */
case MONO_TYPE_VALUETYPE: {
MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
for (guint32 i = 0; i < info->num_fields; ++i) {
class2 = class1;
class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
}
break;
}
default:
g_assert_not_reached ();
}
/* Merge */
if (class1 == class2)
;
else if (class1 == ARG_CLASS_NO_CLASS)
class1 = class2;
else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
class1 = ARG_CLASS_MEMORY;
else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
class1 = ARG_CLASS_INTEGER;
else
class1 = ARG_CLASS_SSE;
return class1;
}
typedef struct {
MonoType *type;
guint32 size, offset;
} StructFieldInfo;
/*
* collect_field_info_nested:
*
* Collect field info from KLASS recursively into FIELDS.
*/
static void
collect_field_info_nested (MonoClass *klass, GArray *fields_array, int offset, gboolean pinvoke, gboolean unicode)
{
MonoMarshalType *info;
if (pinvoke) {
info = mono_marshal_load_type_info (klass);
g_assert(info);
for (guint32 i = 0; i < info->num_fields; ++i) {
if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
collect_field_info_nested (mono_class_from_mono_type_internal (info->fields [i].field->type), fields_array, info->fields [i].offset, pinvoke, unicode);
} else {
guint32 align;
StructFieldInfo f;
f.type = info->fields [i].field->type;
f.size = mono_marshal_type_size (info->fields [i].field->type,
info->fields [i].mspec,
&align, TRUE, unicode);
f.offset = offset + info->fields [i].offset;
if (i == info->num_fields - 1 && f.size + f.offset < info->native_size) {
/* This can happen with .pack directives eg. 'fixed' arrays */
if (MONO_TYPE_IS_PRIMITIVE (f.type)) {
/* Replicate the last field to fill out the remaining place, since the code in add_valuetype () needs type information */
g_array_append_val (fields_array, f);
while (f.size + f.offset < info->native_size) {
f.offset += f.size;
g_array_append_val (fields_array, f);
}
} else {
f.size = info->native_size - f.offset;
g_array_append_val (fields_array, f);
}
} else {
g_array_append_val (fields_array, f);
}
}
}
} else {
gpointer iter;
MonoClassField *field;
iter = NULL;
while ((field = mono_class_get_fields_internal (klass, &iter))) {
if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
continue;
g_assert (!m_field_is_from_update (field));
if (MONO_TYPE_ISSTRUCT (field->type)) {
collect_field_info_nested (mono_class_from_mono_type_internal (field->type), fields_array, m_field_get_offset (field) - MONO_ABI_SIZEOF (MonoObject), pinvoke, unicode);
} else {
int align;
StructFieldInfo f;
f.type = field->type;
f.size = mono_type_size (field->type, &align);
f.offset = m_field_get_offset (field) - MONO_ABI_SIZEOF (MonoObject) + offset;
g_array_append_val (fields_array, f);
}
}
}
}
#ifdef TARGET_WIN32
/* Windows x64 ABI can pass/return value types in register of size 1,2,4,8 bytes. */
#define MONO_WIN64_VALUE_TYPE_FITS_REG(arg_size) (arg_size <= SIZEOF_REGISTER && (arg_size == 1 || arg_size == 2 || arg_size == 4 || arg_size == 8))
static gboolean
allocate_register_for_valuetype_win64 (ArgInfo *arg_info, ArgumentClass arg_class, guint32 arg_size, const AMD64_Reg_No int_regs [], guint32 int_reg_count, const AMD64_XMM_Reg_No float_regs [], guint32 float_reg_count, guint32 *current_int_reg, guint32 *current_float_reg)
{
gboolean result = FALSE;
assert (arg_info != NULL && int_regs != NULL && float_regs != NULL && current_int_reg != NULL && current_float_reg != NULL);
assert (arg_info->storage == ArgValuetypeInReg || arg_info->storage == ArgValuetypeAddrInIReg);
arg_info->pair_storage [0] = arg_info->pair_storage [1] = ArgNone;
arg_info->pair_regs [0] = arg_info->pair_regs [1] = ArgNone;
arg_info->pair_size [0] = 0;
arg_info->pair_size [1] = 0;
arg_info->nregs = 0;
if (arg_class == ARG_CLASS_INTEGER && *current_int_reg < int_reg_count) {
/* Pass parameter in integer register. */
arg_info->pair_storage [0] = ArgInIReg;
arg_info->pair_regs [0] = int_regs [*current_int_reg];
(*current_int_reg) ++;
result = TRUE;
} else if (arg_class == ARG_CLASS_SSE && *current_float_reg < float_reg_count) {
/* Pass parameter in float register. */
arg_info->pair_storage [0] = (arg_size <= sizeof (gfloat)) ? ArgInFloatSSEReg : ArgInDoubleSSEReg;
arg_info->pair_regs [0] = float_regs [*current_float_reg];
(*current_float_reg) ++;
result = TRUE;
}
if (result == TRUE) {
arg_info->pair_size [0] = arg_size;
arg_info->nregs = 1;
}
return result;
}
static gboolean
allocate_parameter_register_for_valuetype_win64 (ArgInfo *arg_info, ArgumentClass arg_class, guint32 arg_size, guint32 *current_int_reg, guint32 *current_float_reg)
{
return allocate_register_for_valuetype_win64 (arg_info, arg_class, arg_size, param_regs, PARAM_REGS, float_param_regs, FLOAT_PARAM_REGS, current_int_reg, current_float_reg);
}
static gboolean
allocate_return_register_for_valuetype_win64 (ArgInfo *arg_info, ArgumentClass arg_class, guint32 arg_size, guint32 *current_int_reg, guint32 *current_float_reg)
{
return allocate_register_for_valuetype_win64 (arg_info, arg_class, arg_size, return_regs, RETURN_REGS, float_return_regs, FLOAT_RETURN_REGS, current_int_reg, current_float_reg);
}
static void
allocate_storage_for_valuetype_win64 (ArgInfo *arg_info, MonoType *type, gboolean is_return, ArgumentClass arg_class,
guint32 arg_size, guint32 *current_int_reg, guint32 *current_float_reg, guint32 *stack_size)
{
/* Windows x64 value type ABI.
*
* Parameters: https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx
*
* Integer/Float types smaller than or equals to 8 bytes or porperly sized struct/union (1,2,4,8)
* Try pass in register using ArgValuetypeInReg/(ArgInIReg|ArgInFloatSSEReg|ArgInDoubleSSEReg) as storage and size of parameter(1,2,4,8), if no more registers, pass on stack using ArgOnStack as storage and size of parameter(1,2,4,8).
* Integer/Float types bigger than 8 bytes or struct/unions larger than 8 bytes or (3,5,6,7).
* Try to pass pointer in register using ArgValuetypeAddrInIReg, if no more registers, pass pointer on stack using ArgValuetypeAddrOnStack as storage and parameter size of register (8 bytes).
*
* Return values: https://msdn.microsoft.com/en-us/library/7572ztz4.aspx.
*
* Integers/Float types smaller than or equal to 8 bytes
* Return in corresponding register RAX/XMM0 using ArgValuetypeInReg/(ArgInIReg|ArgInFloatSSEReg|ArgInDoubleSSEReg) as storage and size of parameter(1,2,4,8).
* Properly sized struct/unions (1,2,4,8)
* Return in register RAX using ArgValuetypeInReg as storage and size of parameter(1,2,4,8).
* Types bigger than 8 bytes or struct/unions larger than 8 bytes or (3,5,6,7).
* Return pointer to allocated stack space (allocated by caller) using ArgValuetypeAddrInIReg as storage and parameter size.
*/
assert (arg_info != NULL && type != NULL && current_int_reg != NULL && current_float_reg != NULL && stack_size != NULL);
if (!is_return) {
/* Parameter cases. */
if (arg_class != ARG_CLASS_MEMORY && MONO_WIN64_VALUE_TYPE_FITS_REG (arg_size)) {
assert (arg_size == 1 || arg_size == 2 || arg_size == 4 || arg_size == 8);
/* First, try to use registers for parameter. If type is struct it can only be passed by value in integer register. */
arg_info->storage = ArgValuetypeInReg;
if (!allocate_parameter_register_for_valuetype_win64 (arg_info, !MONO_TYPE_ISSTRUCT (type) ? arg_class : ARG_CLASS_INTEGER, arg_size, current_int_reg, current_float_reg)) {
/* No more registers, fallback passing parameter on stack as value. */
assert (arg_info->pair_storage [0] == ArgNone && arg_info->pair_storage [1] == ArgNone && arg_info->pair_size [0] == 0 && arg_info->pair_size [1] == 0 && arg_info->nregs == 0);
/* Passing value directly on stack, so use size of value. */
arg_info->storage = ArgOnStack;
arg_size = ALIGN_TO (arg_size, sizeof (target_mgreg_t));
arg_info->offset = GUINT32_TO_INT16 (*stack_size);
arg_info->arg_size = arg_size;
*stack_size += arg_size;
}
} else {
/* Fallback to stack, try to pass address to parameter in register. Always use integer register to represent stack address. */
arg_info->storage = ArgValuetypeAddrInIReg;
if (!allocate_parameter_register_for_valuetype_win64 (arg_info, ARG_CLASS_INTEGER, arg_size, current_int_reg, current_float_reg)) {
/* No more registers, fallback passing address to parameter on stack. */
assert (arg_info->pair_storage [0] == ArgNone && arg_info->pair_storage [1] == ArgNone && arg_info->pair_size [0] == 0 && arg_info->pair_size [1] == 0 && arg_info->nregs == 0);
/* Passing an address to value on stack, so use size of register as argument size. */
arg_info->storage = ArgValuetypeAddrOnStack;
arg_size = sizeof (target_mgreg_t);
arg_info->offset = GUINT32_TO_INT16 (*stack_size);
arg_info->arg_size = arg_size;
*stack_size += arg_size;
}
}
} else {
/* Return value cases. */
if (arg_class != ARG_CLASS_MEMORY && MONO_WIN64_VALUE_TYPE_FITS_REG (arg_size)) {
assert (arg_size == 1 || arg_size == 2 || arg_size == 4 || arg_size == 8);
/* Return value fits into return registers. If type is struct it can only be returned by value in integer register. */
arg_info->storage = ArgValuetypeInReg;
allocate_return_register_for_valuetype_win64 (arg_info, !MONO_TYPE_ISSTRUCT (type) ? arg_class : ARG_CLASS_INTEGER, arg_size, current_int_reg, current_float_reg);
/* Only RAX/XMM0 should be used to return valuetype. */
assert ((arg_info->pair_regs[0] == AMD64_RAX && arg_info->pair_regs[1] == ArgNone) || (arg_info->pair_regs[0] == AMD64_XMM0 && arg_info->pair_regs[1] == ArgNone));
} else {
/* Return value doesn't fit into return register, return address to allocated stack space (allocated by caller and passed as input). */
arg_info->storage = ArgValuetypeAddrInIReg;
allocate_return_register_for_valuetype_win64 (arg_info, ARG_CLASS_INTEGER, arg_size, current_int_reg, current_float_reg);
/* Only RAX should be used to return valuetype address. */
assert (arg_info->pair_regs[0] == AMD64_RAX && arg_info->pair_regs[1] == ArgNone);
arg_size = ALIGN_TO (arg_size, sizeof (target_mgreg_t));
arg_info->offset = GUINT32_TO_INT16 (*stack_size);
*stack_size += arg_size;
}
}
}
static void
get_valuetype_size_win64 (MonoClass *klass, gboolean pinvoke, ArgInfo *arg_info, MonoType *type, ArgumentClass *arg_class, guint32 *arg_size)
{
*arg_size = 0;
*arg_class = ARG_CLASS_NO_CLASS;
assert (klass != NULL && arg_info != NULL && type != NULL && arg_class != NULL && arg_size != NULL);
if (pinvoke) {
/* Calculate argument class type and size of marshalled type. */
MonoMarshalType *info = mono_marshal_load_type_info (klass);
*arg_size = info->native_size;
} else {
/* Calculate argument class type and size of managed type. */
*arg_size = mono_class_value_size (klass, NULL);
}
/* Windows ABI only handle value types on stack or passed in integer register (if it fits register size). */
*arg_class = MONO_WIN64_VALUE_TYPE_FITS_REG (*arg_size) ? ARG_CLASS_INTEGER : ARG_CLASS_MEMORY;
if (*arg_class == ARG_CLASS_MEMORY) {
/* Value type has a size that doesn't seem to fit register according to ABI. Try to used full stack size of type. */
*arg_size = mini_type_stack_size_full (m_class_get_byval_arg (klass), NULL, pinvoke);
}
/*
* Standard C and C++ doesn't allow empty structs, empty structs will always have a size of 1 byte.
* GCC have an extension to allow empty structs, https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html.
* This cause a little dilemma since runtime build using none GCC compiler will not be compatible with
* GCC build C libraries and the other way around. On platforms where empty structs has size of 1 byte
* it must be represented in call and cannot be dropped.
*/
if (*arg_size == 0 && MONO_TYPE_ISSTRUCT (type)) {
arg_info->pass_empty_struct = TRUE;
*arg_size = SIZEOF_REGISTER;
*arg_class = ARG_CLASS_INTEGER;
}
assert (*arg_class != ARG_CLASS_NO_CLASS);
}
static void
add_valuetype_win64 (MonoMethodSignature *signature, ArgInfo *arg_info, MonoType *type,
gboolean is_return, guint32 *current_int_reg, guint32 *current_float_reg, guint32 *stack_size)
{
guint32 arg_size = SIZEOF_REGISTER;
MonoClass *klass = NULL;
ArgumentClass arg_class;
assert (signature != NULL && arg_info != NULL && type != NULL && current_int_reg != NULL && current_float_reg != NULL && stack_size != NULL);
klass = mono_class_from_mono_type_internal (type);
get_valuetype_size_win64 (klass, signature->pinvoke && !signature->marshalling_disabled, arg_info, type, &arg_class, &arg_size);
/* Only drop value type if its not an empty struct as input that must be represented in call */
if ((arg_size == 0 && !arg_info->pass_empty_struct) || (arg_info->pass_empty_struct && is_return)) {
arg_info->storage = ArgValuetypeInReg;
arg_info->pair_storage [0] = arg_info->pair_storage [1] = ArgNone;
} else {
/* Alocate storage for value type. */
allocate_storage_for_valuetype_win64 (arg_info, type, is_return, arg_class, arg_size, current_int_reg, current_float_reg, stack_size);
}
}
#endif /* TARGET_WIN32 */
static void
add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
gboolean is_return,
guint32 *gr, guint32 *fr, guint32 *stack_size)
{
#ifdef TARGET_WIN32
add_valuetype_win64 (sig, ainfo, type, is_return, gr, fr, stack_size);
#else
guint32 size, quad, nquads, i, nfields;
/* Keep track of the size used in each quad so we can */
/* use the right size when copying args/return vars. */
guint32 quadsize [2] = {8, 8};
ArgumentClass args [2];
StructFieldInfo *fields = NULL;
GArray *fields_array;
MonoClass *klass;
gboolean pass_on_stack = FALSE;
int struct_size;
klass = mono_class_from_mono_type_internal (type);
size = mini_type_stack_size_full (m_class_get_byval_arg (klass), NULL, sig->pinvoke && !sig->marshalling_disabled);
if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
/* We pass and return vtypes of size 8 in a register */
} else if (!sig->pinvoke || (size == 0) || (size > 16)) {
pass_on_stack = TRUE;
}
/* If this struct can't be split up naturally into 8-byte */
/* chunks (registers), pass it on the stack. */
if (sig->pinvoke && !sig->marshalling_disabled) {
MonoMarshalType *info = mono_marshal_load_type_info (klass);
g_assert (info);
struct_size = info->native_size;
} else {
struct_size = mono_class_value_size (klass, NULL);
}
/*
* Collect field information recursively to be able to
* handle nested structures.
*/
fields_array = g_array_new (FALSE, TRUE, sizeof (StructFieldInfo));
collect_field_info_nested (klass, fields_array, 0, sig->pinvoke && !sig->marshalling_disabled, m_class_is_unicode (klass));
fields = (StructFieldInfo*)fields_array->data;
nfields = fields_array->len;
for (i = 0; i < nfields; ++i) {
if ((fields [i].offset < 8) && (fields [i].offset + fields [i].size) > 8) {
pass_on_stack = TRUE;
break;
}
}
if (size == 0) {
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
return;
}
if (pass_on_stack) {
/* Always pass in memory */
ainfo->offset = GINT32_TO_INT16 (*stack_size);
*stack_size += ALIGN_TO (size, 8);
ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
if (!is_return)
ainfo->arg_size = ALIGN_TO (size, 8);
g_array_free (fields_array, TRUE);
return;
}
if (size > 8)
nquads = 2;
else
nquads = 1;
if (!sig->pinvoke) {
int n = mono_class_value_size (klass, NULL);
quadsize [0] = n >= 8 ? 8 : n;
quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
/* Always pass in 1 or 2 integer registers */
args [0] = ARG_CLASS_INTEGER;
args [1] = ARG_CLASS_INTEGER;
/* Only the simplest cases are supported */
if (is_return && nquads != 1) {
args [0] = ARG_CLASS_MEMORY;
args [1] = ARG_CLASS_MEMORY;
}
} else {
/*
* Implement the algorithm from section 3.2.3 of the X86_64 ABI.
* The X87 and SSEUP stuff is left out since there are no such types in
* the CLR.
*/
if (!nfields) {
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
return;
}
if (struct_size > 16) {
ainfo->offset = GINT32_TO_INT16 (*stack_size);
*stack_size += ALIGN_TO (struct_size, 8);
ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
if (!is_return)
ainfo->arg_size = ALIGN_TO (struct_size, 8);
g_array_free (fields_array, TRUE);
return;
}
args [0] = ARG_CLASS_NO_CLASS;
args [1] = ARG_CLASS_NO_CLASS;
for (quad = 0; quad < nquads; ++quad) {
ArgumentClass class1;
if (nfields == 0)
class1 = ARG_CLASS_MEMORY;
else
class1 = ARG_CLASS_NO_CLASS;
for (i = 0; i < nfields; ++i) {
if ((fields [i].offset < 8) && (fields [i].offset + fields [i].size) > 8) {
/* Unaligned field */
NOT_IMPLEMENTED;
}
/* Skip fields in other quad */
if ((quad == 0) && (fields [i].offset >= 8))
continue;
if ((quad == 1) && (fields [i].offset < 8))
continue;
/* How far into this quad this data extends.*/
/* (8 is size of quad) */
quadsize [quad] = fields [i].offset + fields [i].size - (quad * 8);
class1 = merge_argument_class_from_type (fields [i].type, class1);
}
/* Empty structs have a nonzero size, causing this assert to be hit */
if (sig->pinvoke)
g_assert (class1 != ARG_CLASS_NO_CLASS);
args [quad] = class1;
}
}
g_array_free (fields_array, TRUE);
/* Post merger cleanup */
if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
args [0] = args [1] = ARG_CLASS_MEMORY;
/* Allocate registers */
{
int orig_gr = *gr;
int orig_fr = *fr;
while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
quadsize [0] ++;
while (quadsize [1] != 0 && quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
quadsize [1] ++;
ainfo->storage = ArgValuetypeInReg;
ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
g_assert (quadsize [0] <= 8);
g_assert (quadsize [1] <= 8);
ainfo->pair_size [0] = quadsize [0];
ainfo->pair_size [1] = quadsize [1];
ainfo->nregs = nquads;
for (quad = 0; quad < nquads; ++quad) {
switch (args [quad]) {
case ARG_CLASS_INTEGER:
if (*gr >= PARAM_REGS)
args [quad] = ARG_CLASS_MEMORY;
else {
ainfo->pair_storage [quad] = ArgInIReg;
if (is_return)
ainfo->pair_regs [quad] = GINT32_TO_UINT8 (return_regs [*gr]);
else
ainfo->pair_regs [quad] = GINT32_TO_UINT8 (param_regs [*gr]);
(*gr) ++;
}
break;
case ARG_CLASS_SSE:
if (*fr >= FLOAT_PARAM_REGS)
args [quad] = ARG_CLASS_MEMORY;
else {
if (quadsize[quad] <= 4)
ainfo->pair_storage [quad] = ArgInFloatSSEReg;
else
ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
ainfo->pair_regs [quad] = GINT32_TO_UINT8 (*fr);
(*fr) ++;
}
break;
case ARG_CLASS_MEMORY:
break;
case ARG_CLASS_NO_CLASS:
break;
default:
g_assert_not_reached ();
}
}
if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
int arg_size;
/* Revert possible register assignments */
*gr = orig_gr;
*fr = orig_fr;
ainfo->offset = GINT32_TO_UINT16 (*stack_size);
if (sig->pinvoke)
arg_size = ALIGN_TO (struct_size, 8);
else
arg_size = nquads * sizeof (target_mgreg_t);
*stack_size += arg_size;
ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
if (!is_return)
ainfo->arg_size = arg_size;
}
}
#endif /* !TARGET_WIN32 */
}
/*
* get_call_info:
*
* Obtain information about a call according to the calling convention.
* For AMD64 System V, see the "System V ABI, x86-64 Architecture Processor Supplement
* Draft Version 0.23" document for more information.
* For AMD64 Windows, see "Overview of x64 Calling Conventions",
* https://msdn.microsoft.com/en-us/library/ms235286.aspx
*/
static CallInfo*
get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
{
guint32 i, gr, fr, pstart;
MonoType *ret_type;
int n = sig->hasthis + sig->param_count;
guint32 stack_size = 0;
CallInfo *cinfo;
gboolean is_pinvoke = sig->pinvoke;
if (mp)
cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
else
cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
cinfo->nargs = n;
cinfo->gsharedvt = mini_is_gsharedvt_variable_signature (sig);
gr = 0;
fr = 0;
#ifdef TARGET_WIN32
/* Reserve space where the callee can save the argument registers */
stack_size = 4 * sizeof (target_mgreg_t);
#endif
/* return value */
ret_type = mini_get_underlying_type (sig->ret);
switch (ret_type->type) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
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_OBJECT:
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = AMD64_RAX;
break;
case MONO_TYPE_U8:
case MONO_TYPE_I8:
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = AMD64_RAX;
break;
case MONO_TYPE_R4:
cinfo->ret.storage = ArgInFloatSSEReg;
cinfo->ret.reg = AMD64_XMM0;
break;
case MONO_TYPE_R8:
cinfo->ret.storage = ArgInDoubleSSEReg;
cinfo->ret.reg = AMD64_XMM0;
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (ret_type)) {
cinfo->ret.storage = ArgInIReg;
cinfo->ret.reg = AMD64_RAX;
break;
}
if (mini_is_gsharedvt_type (ret_type)) {
cinfo->ret.storage = ArgGsharedvtVariableInReg;
break;
}
/* fall through */
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_TYPEDBYREF: {
guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
g_assert (cinfo->ret.storage != ArgInIReg);
break;
}
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
g_assert (mini_is_gsharedvt_type (ret_type));
cinfo->ret.storage = ArgGsharedvtVariableInReg;
break;
case MONO_TYPE_VOID:
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.
*/
ArgStorage ret_storage = cinfo->ret.storage;
if ((ret_storage == ArgValuetypeAddrInIReg || ret_storage == ArgGsharedvtVariableInReg) && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
if (sig->hasthis) {
add_general (&gr, &stack_size, cinfo->args + 0);
} else {
add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
pstart = 1;
}
add_general (&gr, &stack_size, &cinfo->ret);
cinfo->ret.storage = ret_storage;
cinfo->vret_arg_index = 1;
} else {
/* this */
if (sig->hasthis)
add_general (&gr, &stack_size, cinfo->args + 0);
if (ret_storage == ArgValuetypeAddrInIReg || ret_storage == ArgGsharedvtVariableInReg) {
add_general (&gr, &stack_size, &cinfo->ret);
cinfo->ret.storage = ret_storage;
}
}
if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
gr = PARAM_REGS;
fr = FLOAT_PARAM_REGS;
/* Emit the signature cookie just before the implicit arguments */
add_general (&gr, &stack_size, &cinfo->sig_cookie);
}
for (i = pstart; i < sig->param_count; ++i) {
ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
MonoType *ptype;
#ifdef TARGET_WIN32
/* The float param registers and other param registers must be the same index on Windows x64.*/
if (gr > fr)
fr = gr;
else if (fr > gr)
gr = fr;
#endif
if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
/* We always pass the sig cookie on the stack for simplicity */
/*
* Prevent implicit arguments + the sig cookie from being passed
* in registers.
*/
gr = PARAM_REGS;
fr = FLOAT_PARAM_REGS;
/* Emit the signature cookie just before the implicit arguments */
add_general (&gr, &stack_size, &cinfo->sig_cookie);
}
ptype = mini_get_underlying_type (sig->params [i]);
switch (ptype->type) {
case MONO_TYPE_I1: