|
6 | 6 |
|
7 | 7 | #include "rust_internal.h"
|
8 | 8 |
|
9 |
| -#ifdef _MSC_VER |
10 |
| -#define ALIGNOF __alignof |
11 |
| -#else |
12 |
| -#define ALIGNOF __alignof__ |
13 |
| -#endif |
14 |
| - |
15 | 9 | #define ARENA_SIZE 256
|
16 | 10 |
|
17 | 11 | #define DPRINT(fmt,...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
@@ -86,6 +80,30 @@ class arena {
|
86 | 80 | };
|
87 | 81 |
|
88 | 82 |
|
| 83 | +// Alignment inquiries |
| 84 | +// |
| 85 | +// We can't directly use __alignof__ everywhere because that returns the |
| 86 | +// preferred alignment of the type, which is different from the ABI-mandated |
| 87 | +// alignment of the type in some cases (e.g. doubles on x86). The latter is |
| 88 | +// what actually gets used for struct elements. |
| 89 | + |
| 90 | +template<typename T> |
| 91 | +inline size_t |
| 92 | +alignof() { |
| 93 | +#ifdef _MSC_VER |
| 94 | + return __alignof(T); |
| 95 | +#else |
| 96 | + return __alignof__(T); |
| 97 | +#endif |
| 98 | +} |
| 99 | + |
| 100 | +template<> |
| 101 | +inline size_t |
| 102 | +alignof<double>() { |
| 103 | + return 4; |
| 104 | +} |
| 105 | + |
| 106 | + |
89 | 107 | // Utility classes
|
90 | 108 |
|
91 | 109 | struct size_align {
|
@@ -546,7 +564,7 @@ class size_of : public ctxt<size_of> {
|
546 | 564 | }
|
547 | 565 |
|
548 | 566 | template<typename T>
|
549 |
| - void walk_number(bool align) { sa.set(sizeof(T), ALIGNOF(T)); } |
| 567 | + void walk_number(bool align) { sa.set(sizeof(T), alignof<T>()); } |
550 | 568 |
|
551 | 569 | void compute_tag_size(tag_info &tinfo);
|
552 | 570 |
|
@@ -709,7 +727,7 @@ namespace shape {
|
709 | 727 | // for methods that actually manipulate the data involved.
|
710 | 728 |
|
711 | 729 | #define DATA_SIMPLE(ty, call) \
|
712 |
| - if (align) dp = align_to(dp, sizeof(ty)); \ |
| 730 | + if (align) dp = align_to(dp, alignof<ty>()); \ |
713 | 731 | U end_dp = dp + sizeof(ty); \
|
714 | 732 | static_cast<T *>(this)->call; \
|
715 | 733 | dp = end_dp;
|
@@ -847,7 +865,7 @@ data<T,U>::walk_tag(bool align, tag_info &tinfo) {
|
847 | 865 | size_of::compute_tag_size(*this, tinfo);
|
848 | 866 |
|
849 | 867 | if (tinfo.variant_count > 1 && align)
|
850 |
| - dp = align_to(dp, ALIGNOF(uint32_t)); |
| 868 | + dp = align_to(dp, alignof<uint32_t>()); |
851 | 869 |
|
852 | 870 | U end_dp = dp + tinfo.tag_sa.size;
|
853 | 871 |
|
|
0 commit comments