llvm.org GIT mirror llvm / 21a01d1
Make the host endianness check an integer constant expression. I will remove the isBigEndianHost function once I update clang. The ifdef logic is designed to * not use configure/cmake to avoid breaking -arch i686 -arch ppc. * default to little endian * be as small as possible It looks like sys/endian.h is the preferred header on most modern BSD systems, but it is better to change this in a followup patch as machine/endian.h is available on FreeBSD, OpenBSD, NetBSD and OS X. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179527 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 7 years ago
8 changed file(s) with 30 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
150150 inline uint64_t fetch64(const char *p) {
151151 uint64_t result;
152152 memcpy(&result, p, sizeof(result));
153 if (sys::isBigEndianHost())
153 if (sys::IsBigEndianHost)
154154 return sys::SwapByteOrder(result);
155155 return result;
156156 }
158158 inline uint32_t fetch32(const char *p) {
159159 uint32_t result;
160160 memcpy(&result, p, sizeof(result));
161 if (sys::isBigEndianHost())
161 if (sys::IsBigEndianHost)
162162 return sys::SwapByteOrder(result);
163163 return result;
164164 }
3636 namespace endian {
3737 template
3838 inline value_type byte_swap(value_type value) {
39 if (endian != native && sys::isBigEndianHost() != (endian == big))
39 if (endian != native && sys::IsBigEndianHost != (endian == big))
4040 return sys::SwapByteOrder(value);
4141 return value;
4242 }
1414 #define LLVM_SUPPORT_HOST_H
1515
1616 #include "llvm/ADT/StringMap.h"
17
18 #if defined(__linux__)
19 #include
20 #else
21 #ifndef _MSC_VER
22 #include
23 #endif
24 #endif
25
1726 #include
1827
1928 namespace llvm {
2029 namespace sys {
2130
22 inline bool isLittleEndianHost() {
23 union {
24 int i;
25 char c;
26 };
27 i = 1;
28 return c;
29 }
31 #if BYTE_ORDER == BIG_ENDIAN
32 static const bool IsBigEndianHost = true;
33 #else
34 static const bool IsBigEndianHost = false;
35 #endif
36
37 static const bool IsLittleEndianHost = !IsBigEndianHost;
3038
3139 inline bool isBigEndianHost() {
32 return !isLittleEndianHost();
40 return IsBigEndianHost;
3341 }
3442
3543 /// getDefaultTargetTriple() - Return the default target triple the compiler
947947 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
948948 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
949949
950 if (sys::isLittleEndianHost()) {
950 if (sys::IsLittleEndianHost) {
951951 // Little-endian host - the source is ordered from LSB to MSB. Order the
952952 // destination from LSB to MSB: Do a straight copy.
953953 memcpy(Dst, Src, StoreBytes);
10081008 break;
10091009 }
10101010
1011 if (sys::isLittleEndianHost() != getDataLayout()->isLittleEndian())
1011 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
10121012 // Host and target are different endian - reverse the stored bytes.
10131013 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
10141014 }
10201020 uint8_t *Dst = reinterpret_cast(
10211021 const_cast(IntVal.getRawData()));
10221022
1023 if (sys::isLittleEndianHost())
1023 if (sys::IsLittleEndianHost)
10241024 // Little-endian host - the destination must be ordered from LSB to MSB.
10251025 // The source is ordered from LSB to MSB: Do a straight copy.
10261026 memcpy(Dst, Src, LoadBytes);
201201
202202
203203 void writeInt16BE(uint8_t *Addr, uint16_t Value) {
204 if (sys::isLittleEndianHost())
204 if (sys::IsLittleEndianHost)
205205 Value = sys::SwapByteOrder(Value);
206206 *Addr = (Value >> 8) & 0xFF;
207207 *(Addr+1) = Value & 0xFF;
208208 }
209209
210210 void writeInt32BE(uint8_t *Addr, uint32_t Value) {
211 if (sys::isLittleEndianHost())
211 if (sys::IsLittleEndianHost)
212212 Value = sys::SwapByteOrder(Value);
213213 *Addr = (Value >> 24) & 0xFF;
214214 *(Addr+1) = (Value >> 16) & 0xFF;
217217 }
218218
219219 void writeInt64BE(uint8_t *Addr, uint64_t Value) {
220 if (sys::isLittleEndianHost())
220 if (sys::IsLittleEndianHost)
221221 Value = sys::SwapByteOrder(Value);
222222 *Addr = (Value >> 56) & 0xFF;
223223 *(Addr+1) = (Value >> 48) & 0xFF;
6060 MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
6161 bool Is64Bit_)
6262 : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
63 IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
63 IsSwappedEndian(IsLittleEndian != sys::IsLittleEndianHost),
6464 HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
6565 // Load the common header.
6666 memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header));
1919 uint32_t offset = *offset_ptr;
2020 if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
2121 std::memcpy(&val, &Data[offset], sizeof(val));
22 if (sys::isLittleEndianHost() != isLittleEndian)
22 if (sys::IsLittleEndianHost != isLittleEndian)
2323 val = sys::SwapByteOrder(val);
2424
2525 // Advance the offset
100100 // Otherwise do it the hard way.
101101 // To be compatible with above bulk transfer, we need to take endianness
102102 // into account.
103 if (sys::isBigEndianHost()) {
103 if (sys::IsBigEndianHost) {
104104 for (Pos += 4; Pos <= Size; Pos += 4) {
105105 unsigned V = ((unsigned char)String[Pos - 4] << 24) |
106106 ((unsigned char)String[Pos - 3] << 16) |
109109 Bits.push_back(V);
110110 }
111111 } else {
112 assert(sys::isLittleEndianHost() && "Unexpected host endianness");
112 assert(sys::IsLittleEndianHost && "Unexpected host endianness");
113113 for (Pos += 4; Pos <= Size; Pos += 4) {
114114 unsigned V = ((unsigned char)String[Pos - 1] << 24) |
115115 ((unsigned char)String[Pos - 2] << 16) |