Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LoongArch64PassStructInRegister.cs « JitInterface « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0a164d23ebee15a9501c28419a7d0489d95ccb48 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using ILCompiler;
using Internal.TypeSystem;

namespace Internal.JitInterface
{

    internal static class LoongArch64PassStructInRegister
    {
        public static uint GetLoongArch64PassStructInRegisterFlags(TypeDesc typeDesc)
        {
            FieldDesc firstField = null;
            uint floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
            int numIntroducedFields = 0;
            foreach (FieldDesc field in typeDesc.GetFields())
            {
                if (!field.IsStatic)
                {
                    if (firstField == null)
                    {
                        firstField = field;
                    }
                    numIntroducedFields++;
                }
            }

            if ((numIntroducedFields == 0) || (numIntroducedFields > 2) || (typeDesc.GetElementSize().AsInt > 16))
            {
                return (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
            }

            //// The SIMD Intrinsic types are meant to be handled specially and should not be passed as struct registers
            if (typeDesc.IsIntrinsic)
            {
                throw new NotImplementedException("For LoongArch64, SIMD would be implemented later");
            }

            MetadataType mdType = typeDesc as MetadataType;
            Debug.Assert(mdType != null);

            TypeDesc firstFieldElementType = firstField.FieldType;
            int firstFieldSize = firstFieldElementType.GetElementSize().AsInt;

            // A fixed buffer type is always a value type that has exactly one value type field at offset 0
            // and who's size is an exact multiple of the size of the field.
            // It is possible that we catch a false positive with this check, but that chance is extremely slim
            // and the user can always change their structure to something more descriptive of what they want
            // instead of adding additional padding at the end of a one-field structure.
            // We do this check here to save looking up the FixedBufferAttribute when loading the field
            // from metadata.
            bool isFixedBuffer = numIntroducedFields == 1
                                    && firstFieldElementType.IsValueType
                                    && firstField.Offset.AsInt == 0
                                    && mdType.HasLayout()
                                    && ((typeDesc.GetElementSize().AsInt % firstFieldSize) == 0);

            if (isFixedBuffer)
            {
                numIntroducedFields = typeDesc.GetElementSize().AsInt / firstFieldSize;
                if (numIntroducedFields > 2)
                {
                    return (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                }
            }

            int fieldIndex = 0;
            foreach (FieldDesc field in typeDesc.GetFields())
            {
                if (fieldIndex > 1)
                {
                    return (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                }
                else if (field.IsStatic)
                {
                    continue;
                }

                Debug.Assert(fieldIndex < numIntroducedFields);

                switch (field.FieldType.Category)
                {
                    case TypeFlags.Double:
                    {
                        if (numIntroducedFields == 1)
                        {
                            floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_ONLY_ONE;
                        }
                        else if (fieldIndex == 0)
                        {
                            floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FIRST_FIELD_DOUBLE;
                        }
                        else if ((floatFieldFlags & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST) != 0)
                        {
                            floatFieldFlags = floatFieldFlags ^ (uint)StructFloatFieldInfoFlags.STRUCT_MERGE_FIRST_SECOND_8;
                        }
                        else
                        {
                            floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_SECOND_FIELD_DOUBLE;
                        }
                    }
                    break;

                    case  TypeFlags.Single:
                    {
                        if (numIntroducedFields == 1)
                        {
                            floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_ONLY_ONE;
                        }
                        else if (fieldIndex == 0)
                        {
                            floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST;
                        }
                        else if ((floatFieldFlags & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST) != 0)
                        {
                            floatFieldFlags ^= (uint)StructFloatFieldInfoFlags.STRUCT_MERGE_FIRST_SECOND;
                        }
                        else
                        {
                            floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_SECOND;
                        }
                    }
                    break;

                    case TypeFlags.ValueType:
                    //case TypeFlags.Class:
                    //case TypeFlags.Array:
                    //case TypeFlags.SzArray:
                    {
                        uint floatFieldFlags2 = GetLoongArch64PassStructInRegisterFlags(field.FieldType);
                        if (numIntroducedFields == 1)
                        {
                            floatFieldFlags = floatFieldFlags2;
                        }
                        else if (field.FieldType.GetElementSize().AsInt > 8)
                        {
                            return (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                        }
                        else if (fieldIndex == 0)
                        {
                            if ((floatFieldFlags2 & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_ONLY_ONE) != 0)
                            {
                                floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST;
                            }
                            if (field.FieldType.GetElementSize().AsInt == 8)
                            {
                                floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_FIRST_FIELD_SIZE_IS8;
                            }
                        }
                        else
                        {
                            Debug.Assert(fieldIndex == 1);
                            if ((floatFieldFlags2 & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_ONLY_ONE) != 0)
                            {
                                floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_MERGE_FIRST_SECOND;
                            }
                            if (field.FieldType.GetElementSize().AsInt == 8)
                            {
                                floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_SECOND_FIELD_SIZE_IS8;
                            }

                            floatFieldFlags2 = floatFieldFlags & ((uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST | (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_SECOND);
                            if (floatFieldFlags2 == 0)
                            {
                                floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                            }
                            else if (floatFieldFlags2 == ((uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST | (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_SECOND))
                            {
                                floatFieldFlags ^= ((uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_ONLY_TWO | (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST | (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_SECOND);
                            }
                        }
                    }
                    break;

                    default:
                    {
                        if (field.FieldType.GetElementSize().AsInt == 8)
                        {
                            if (numIntroducedFields > 1)
                            {
                                if (fieldIndex == 0)
                                {
                                    floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_FIRST_FIELD_SIZE_IS8;
                                }
                                else if ((floatFieldFlags & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST) != 0)
                                {
                                    floatFieldFlags |= (uint)StructFloatFieldInfoFlags.STRUCT_SECOND_FIELD_SIZE_IS8;
                                }
                                else
                                {
                                    floatFieldFlags = (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                                }
                            }
                        }
                        else if (fieldIndex == 1)
                        {
                            floatFieldFlags = (floatFieldFlags & (uint)StructFloatFieldInfoFlags.STRUCT_FLOAT_FIELD_FIRST) > 0 ? floatFieldFlags : (uint)StructFloatFieldInfoFlags.STRUCT_NO_FLOAT_FIELD;
                        }
                        break;
                    }
                }

                fieldIndex++;
            }

            return floatFieldFlags;
        }
    }
}