rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/src/platform_abi.cpp -rw-r--r-- 10.3 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 7 days ago
                                                                                
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
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "platform_abi.hpp"
#include "types.hpp"
#include "utils.hpp"
#include "dyn_cast.inc"
#include "styled_stream.hpp"

#include <assert.h>

#pragma GCC diagnostic ignored "-Wvla-extension"

namespace scopes {

//------------------------------------------------------------------------------
// PLATFORM ABI
//------------------------------------------------------------------------------

// life is unfair, which is why we need to implement the remaining platform ABI
// support in the front-end, particularly whether an argument is passed by
// value or not.

const char *abi_class_to_string(ABIClass class_) {
    switch(class_) {
    #define T(X) case ABI_CLASS_ ## X: return #X;
    DEF_ABI_CLASS_NAMES
    #undef T
    default: return "?";
    }
}

#undef DEF_ABI_CLASS_NAMES

#ifdef SCOPES_WIN32

ABIClass merge_abi_classes(ABIClass class1, ABIClass class2) {
    if (class1 == class2)
        return class1;

    if (class1 == ABI_CLASS_NO_CLASS)
        return class2;
    if (class2 == ABI_CLASS_NO_CLASS)
        return class1;

    if (class1 == ABI_CLASS_MEMORY || class2 == ABI_CLASS_MEMORY)
        return ABI_CLASS_MEMORY;

    if ((class1 == ABI_CLASS_INTEGERSI && class2 == ABI_CLASS_SSESF)
        || (class2 == ABI_CLASS_INTEGERSI && class1 == ABI_CLASS_SSESF))
        return ABI_CLASS_INTEGERSI;
    if (class1 == ABI_CLASS_INTEGER || class1 == ABI_CLASS_INTEGERSI
        || class2 == ABI_CLASS_INTEGER || class2 == ABI_CLASS_INTEGERSI)
        return ABI_CLASS_INTEGER;

    if (class1 == ABI_CLASS_X87
        || class1 == ABI_CLASS_X87UP
        || class1 == ABI_CLASS_COMPLEX_X87
        || class2 == ABI_CLASS_X87
        || class2 == ABI_CLASS_X87UP
        || class2 == ABI_CLASS_COMPLEX_X87)
        return ABI_CLASS_MEMORY;

    return ABI_CLASS_SSE;
}

#else
// x86-64 PS ABI based on https://www.uclibc.org/docs/psABI-x86_64.pdf

ABIClass merge_abi_classes(ABIClass class1, ABIClass class2) {
    if (class1 == class2)
        return class1;

    if (class1 == ABI_CLASS_NO_CLASS)
        return class2;
    if (class2 == ABI_CLASS_NO_CLASS)
        return class1;

    if (class1 == ABI_CLASS_MEMORY || class2 == ABI_CLASS_MEMORY)
        return ABI_CLASS_MEMORY;

    if ((class1 == ABI_CLASS_INTEGERSI && class2 == ABI_CLASS_SSESF)
        || (class2 == ABI_CLASS_INTEGERSI && class1 == ABI_CLASS_SSESF))
        return ABI_CLASS_INTEGERSI;
    if (class1 == ABI_CLASS_INTEGER || class1 == ABI_CLASS_INTEGERSI
        || class2 == ABI_CLASS_INTEGER || class2 == ABI_CLASS_INTEGERSI)
        return ABI_CLASS_INTEGER;

    if (class1 == ABI_CLASS_X87
        || class1 == ABI_CLASS_X87UP
        || class1 == ABI_CLASS_COMPLEX_X87
        || class2 == ABI_CLASS_X87
        || class2 == ABI_CLASS_X87UP
        || class2 == ABI_CLASS_COMPLEX_X87)
        return ABI_CLASS_MEMORY;

    return ABI_CLASS_SSE;
}

static size_t classify(const Type *T, ABIClass *classes, size_t offset);

static size_t classify_tuple_like(size_t size,
    const Type **fields, size_t count, bool packed,
    ABIClass *classes, size_t offset) {
    const size_t UNITS_PER_WORD = 8;
    size_t words = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
    if (size > 32)
        return 0;
    for (size_t i = 0; i < MAX_ABI_CLASSES; i++)
        classes[i] = ABI_CLASS_NO_CLASS;
    if (!words) {
        classes[0] = ABI_CLASS_NO_CLASS;
        return 1;
    }
    ABIClass subclasses[MAX_ABI_CLASSES];
    for (size_t i = 0; i < count; ++i) {
        auto ET = strip_qualifiers(fields[i]);
        if (!packed)
            offset = align(offset, align_of(ET).assert_ok());
        size_t num = classify (ET, subclasses, offset % 8);
        if (!num) return 0;
        for (size_t k = 0; k < num; ++k) {
            size_t pos = offset / 8;
            classes[k + pos] =
                merge_abi_classes (subclasses[k], classes[k + pos]);
        }
        offset += size_of(ET).assert_ok();
    }
    if (words > 2) {
        if (classes[0] != ABI_CLASS_SSE)
            return 0;
        for (size_t i = 1; i < words; ++i) {
            if (classes[i] != ABI_CLASS_SSEUP)
                return 0;
        }
    }
    for (size_t i = 0; i < words; i++) {
        if (classes[i] == ABI_CLASS_MEMORY)
            return 0;

        if (classes[i] == ABI_CLASS_SSEUP) {
            assert(i > 0);
            if (classes[i - 1] != ABI_CLASS_SSE
                && classes[i - 1] != ABI_CLASS_SSEUP) {
                classes[i] = ABI_CLASS_SSE;
            }
        }

        if (classes[i] == ABI_CLASS_X87UP) {
            assert(i > 0);
            if(classes[i - 1] != ABI_CLASS_X87) {
                return 0;
            }
        }
    }
    return words;

}

static size_t classify_array_like(size_t size,
    const Type *element_type, size_t count,
    ABIClass *classes, size_t offset) {
    const size_t UNITS_PER_WORD = 8;
    size_t words = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
    if (size > 32)
        return 0;
    for (size_t i = 0; i < MAX_ABI_CLASSES; i++)
        classes[i] = ABI_CLASS_NO_CLASS;
    if (!words) {
        classes[0] = ABI_CLASS_NO_CLASS;
        return 1;
    }
    auto ET = element_type;
    ABIClass subclasses[MAX_ABI_CLASSES];
    size_t alignment = align_of(ET).assert_ok();
    size_t esize = size_of(ET).assert_ok();
    for (size_t i = 0; i < count; ++i) {
        offset = align(offset, alignment);
        size_t num = classify(ET, subclasses, offset % 8);
        if (!num) return 0;
        for (size_t k = 0; k < num; ++k) {
            size_t pos = offset / 8;
            classes[k + pos] =
                merge_abi_classes (subclasses[k], classes[k + pos]);
        }
        offset += esize;
    }
    if (words > 2) {
        if (classes[0] != ABI_CLASS_SSE)
            return 0;
        for (size_t i = 1; i < words; ++i) {
            if (classes[i] != ABI_CLASS_SSEUP)
                return 0;
        }
    }
    for (size_t i = 0; i < words; i++) {
        if (classes[i] == ABI_CLASS_MEMORY)
            return 0;

        if (classes[i] == ABI_CLASS_SSEUP) {
            assert(i > 0);
            if (classes[i - 1] != ABI_CLASS_SSE
                && classes[i - 1] != ABI_CLASS_SSEUP) {
                classes[i] = ABI_CLASS_SSE;
            }
        }

        if (classes[i] == ABI_CLASS_X87UP) {
            assert(i > 0);
            if(classes[i - 1] != ABI_CLASS_X87) {
                return 0;
            }
        }
    }
    return words;
}

static size_t classify(const Type *T, ABIClass *classes, size_t offset) {
    switch(T->kind()) {
    case TK_Integer:
    case TK_Pointer: {
        size_t size = size_of(T).assert_ok() + offset;
        if (size <= 4) {
            classes[0] = ABI_CLASS_INTEGERSI;
            return 1;
        } else if (size <= 8) {
            classes[0] = ABI_CLASS_INTEGER;
            return 1;
        } else if (size <= 12) {
            classes[0] = ABI_CLASS_INTEGER;
            classes[1] = ABI_CLASS_INTEGERSI;
            return 2;
        } else if (size <= 16) {
            classes[0] = ABI_CLASS_INTEGER;
            classes[1] = ABI_CLASS_INTEGER;
            return 2;
        } else {
            return 0;
        }
    } break;
    case TK_Real: {
        size_t size = size_of(T).assert_ok();
        if (size == 4) {
            if (!(offset % 8))
                classes[0] = ABI_CLASS_SSESF;
            else
                classes[0] = ABI_CLASS_SSE;
            return 1;
        } else if (size == 8) {
            classes[0] = ABI_CLASS_SSEDF;
            return 1;
        } else {
            assert(false && "illegal type");
        }
    } break;
    case TK_Typename: {
        if (is_opaque(T)) {
            classes[0] = ABI_CLASS_NO_CLASS;
            return 1;
        } else {
            return classify(storage_type(T).assert_ok(), classes, offset);
        }
    } break;
    case TK_Vector: {
        auto tt = cast<VectorType>(T);
        return classify_array_like(size_of(T).assert_ok(),
            storage_type(tt->element_type).assert_ok(), tt->count, classes, offset);
    } break;
    case TK_Array: {
        auto tt = cast<ArrayType>(T);
        return classify_array_like(size_of(T).assert_ok(),
            storage_type(tt->element_type).assert_ok(), tt->count, classes, offset);
    } break;
    case TK_Tuple: {
        auto tt = cast<TupleType>(T);
        size_t count = tt->values.size();
        const Type *fields[count];
        for (size_t i = 0; i < tt->values.size(); ++i) {
            fields[i] = storage_type(tt->values[i]).assert_ok();
        }
        return classify_tuple_like(size_of(T).assert_ok(),
            fields, count, tt->packed, classes, offset);
    } break;
    default: {
        StyledStream ss;
        ss << "internal error: type " << T << " unsupported in ABI" << std::endl;
        assert(false && "not supported in ABI");
        return 0;
    } break;
    }
    return 0;
}
#endif // SCOPES_WIN32

size_t abi_classify(const Type *T, ABIClass *classes) {
    T = strip_qualifiers(T);
    if (T->kind() == TK_Arguments) {
        if (T == empty_arguments_type()) {
            classes[0] = ABI_CLASS_NO_CLASS;
            return 1;
        }
        T = cast<ArgumentsType>(T)->to_tuple_type();
    }
#ifdef SCOPES_WIN32
    classes[0] = ABI_CLASS_NO_CLASS;
    if (is_opaque(T))
        return 1;
    T = storage_type(T).assert_ok();
    size_t sz = size_of(T).assert_ok();
    if (sz > 8)
        return 0;
    switch(T->kind()) {
    case TK_Array:
    case TK_Tuple:
        if (sz <= 1)
            classes[0] = ABI_CLASS_INTEGERSI8;
        else if (sz <= 2)
            classes[0] = ABI_CLASS_INTEGERSI16;
        else if (sz <= 4)
            classes[0] = ABI_CLASS_INTEGERSI;
        else
            classes[0] = ABI_CLASS_INTEGER;
        return 1;
    case TK_Integer:
    case TK_Pointer:
    case TK_Real:
    case TK_Typename:
    case TK_Vector:
    default:
        return 1;
    }
#else
    size_t sz = classify(T, classes, 0);
#if 0
    if (sz) {
        StyledStream ss(std::cout);
        ss << T << " -> " << sz;
        for (int i = 0; i < sz; ++i) {
            ss << " " << abi_class_to_string(classes[i]);
        }
        ss << std::endl;
    }
#endif
    return sz;
#endif
}

bool is_memory_class(const Type *T) {
    ABIClass classes[MAX_ABI_CLASSES];
    return !abi_classify(T, classes);
}


} // namespace scopes