[llvm] r179527 - Make the host endianness check an integer constant expression.
Rafael Espindola
rafael.espindola at gmail.com
Mon Apr 15 07:44:24 PDT 2013
Author: rafael
Date: Mon Apr 15 09:44:24 2013
New Revision: 179527
URL: http://llvm.org/viewvc/llvm-project?rev=179527&view=rev
Log:
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.
Modified:
llvm/trunk/include/llvm/ADT/Hashing.h
llvm/trunk/include/llvm/Support/Endian.h
llvm/trunk/include/llvm/Support/Host.h
llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
llvm/trunk/lib/Object/MachOObject.cpp
llvm/trunk/lib/Support/DataExtractor.cpp
llvm/trunk/lib/Support/FoldingSet.cpp
Modified: llvm/trunk/include/llvm/ADT/Hashing.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Hashing.h?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Hashing.h (original)
+++ llvm/trunk/include/llvm/ADT/Hashing.h Mon Apr 15 09:44:24 2013
@@ -151,7 +151,7 @@ namespace detail {
inline uint64_t fetch64(const char *p) {
uint64_t result;
memcpy(&result, p, sizeof(result));
- if (sys::isBigEndianHost())
+ if (sys::IsBigEndianHost)
return sys::SwapByteOrder(result);
return result;
}
@@ -159,7 +159,7 @@ inline uint64_t fetch64(const char *p) {
inline uint32_t fetch32(const char *p) {
uint32_t result;
memcpy(&result, p, sizeof(result));
- if (sys::isBigEndianHost())
+ if (sys::IsBigEndianHost)
return sys::SwapByteOrder(result);
return result;
}
Modified: llvm/trunk/include/llvm/Support/Endian.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Endian.h?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Endian.h (original)
+++ llvm/trunk/include/llvm/Support/Endian.h Mon Apr 15 09:44:24 2013
@@ -37,7 +37,7 @@ namespace detail {
namespace endian {
template<typename value_type, endianness endian>
inline value_type byte_swap(value_type value) {
- if (endian != native && sys::isBigEndianHost() != (endian == big))
+ if (endian != native && sys::IsBigEndianHost != (endian == big))
return sys::SwapByteOrder(value);
return value;
}
Modified: llvm/trunk/include/llvm/Support/Host.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Host.h?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Host.h (original)
+++ llvm/trunk/include/llvm/Support/Host.h Mon Apr 15 09:44:24 2013
@@ -15,22 +15,30 @@
#define LLVM_SUPPORT_HOST_H
#include "llvm/ADT/StringMap.h"
+
+#if defined(__linux__)
+#include <endian.h>
+#else
+#ifndef _MSC_VER
+#include <machine/endian.h>
+#endif
+#endif
+
#include <string>
namespace llvm {
namespace sys {
- inline bool isLittleEndianHost() {
- union {
- int i;
- char c;
- };
- i = 1;
- return c;
- }
+#if BYTE_ORDER == BIG_ENDIAN
+ static const bool IsBigEndianHost = true;
+#else
+ static const bool IsBigEndianHost = false;
+#endif
+
+ static const bool IsLittleEndianHost = !IsBigEndianHost;
inline bool isBigEndianHost() {
- return !isLittleEndianHost();
+ return IsBigEndianHost;
}
/// getDefaultTargetTriple() - Return the default target triple the compiler
Modified: llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp Mon Apr 15 09:44:24 2013
@@ -948,7 +948,7 @@ static void StoreIntToMemory(const APInt
assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
- if (sys::isLittleEndianHost()) {
+ if (sys::IsLittleEndianHost) {
// Little-endian host - the source is ordered from LSB to MSB. Order the
// destination from LSB to MSB: Do a straight copy.
memcpy(Dst, Src, StoreBytes);
@@ -1009,7 +1009,7 @@ void ExecutionEngine::StoreValueToMemory
break;
}
- if (sys::isLittleEndianHost() != getDataLayout()->isLittleEndian())
+ if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
// Host and target are different endian - reverse the stored bytes.
std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
}
@@ -1021,7 +1021,7 @@ static void LoadIntFromMemory(APInt &Int
uint8_t *Dst = reinterpret_cast<uint8_t *>(
const_cast<uint64_t *>(IntVal.getRawData()));
- if (sys::isLittleEndianHost())
+ if (sys::IsLittleEndianHost)
// Little-endian host - the destination must be ordered from LSB to MSB.
// The source is ordered from LSB to MSB: Do a straight copy.
memcpy(Dst, Src, LoadBytes);
Modified: llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h (original)
+++ llvm/trunk/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h Mon Apr 15 09:44:24 2013
@@ -202,14 +202,14 @@ protected:
void writeInt16BE(uint8_t *Addr, uint16_t Value) {
- if (sys::isLittleEndianHost())
+ if (sys::IsLittleEndianHost)
Value = sys::SwapByteOrder(Value);
*Addr = (Value >> 8) & 0xFF;
*(Addr+1) = Value & 0xFF;
}
void writeInt32BE(uint8_t *Addr, uint32_t Value) {
- if (sys::isLittleEndianHost())
+ if (sys::IsLittleEndianHost)
Value = sys::SwapByteOrder(Value);
*Addr = (Value >> 24) & 0xFF;
*(Addr+1) = (Value >> 16) & 0xFF;
@@ -218,7 +218,7 @@ protected:
}
void writeInt64BE(uint8_t *Addr, uint64_t Value) {
- if (sys::isLittleEndianHost())
+ if (sys::IsLittleEndianHost)
Value = sys::SwapByteOrder(Value);
*Addr = (Value >> 56) & 0xFF;
*(Addr+1) = (Value >> 48) & 0xFF;
Modified: llvm/trunk/lib/Object/MachOObject.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObject.cpp?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObject.cpp (original)
+++ llvm/trunk/lib/Object/MachOObject.cpp Mon Apr 15 09:44:24 2013
@@ -61,7 +61,7 @@ static void ReadInMemoryStruct(const Mac
MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
bool Is64Bit_)
: Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
- IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
+ IsSwappedEndian(IsLittleEndian != sys::IsLittleEndianHost),
HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
// Load the common header.
memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header));
Modified: llvm/trunk/lib/Support/DataExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DataExtractor.cpp?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DataExtractor.cpp (original)
+++ llvm/trunk/lib/Support/DataExtractor.cpp Mon Apr 15 09:44:24 2013
@@ -20,7 +20,7 @@ static T getU(uint32_t *offset_ptr, cons
uint32_t offset = *offset_ptr;
if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
std::memcpy(&val, &Data[offset], sizeof(val));
- if (sys::isLittleEndianHost() != isLittleEndian)
+ if (sys::IsLittleEndianHost != isLittleEndian)
val = sys::SwapByteOrder(val);
// Advance the offset
Modified: llvm/trunk/lib/Support/FoldingSet.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FoldingSet.cpp?rev=179527&r1=179526&r2=179527&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FoldingSet.cpp (original)
+++ llvm/trunk/lib/Support/FoldingSet.cpp Mon Apr 15 09:44:24 2013
@@ -101,7 +101,7 @@ void FoldingSetNodeID::AddString(StringR
// Otherwise do it the hard way.
// To be compatible with above bulk transfer, we need to take endianness
// into account.
- if (sys::isBigEndianHost()) {
+ if (sys::IsBigEndianHost) {
for (Pos += 4; Pos <= Size; Pos += 4) {
unsigned V = ((unsigned char)String[Pos - 4] << 24) |
((unsigned char)String[Pos - 3] << 16) |
@@ -110,7 +110,7 @@ void FoldingSetNodeID::AddString(StringR
Bits.push_back(V);
}
} else {
- assert(sys::isLittleEndianHost() && "Unexpected host endianness");
+ assert(sys::IsLittleEndianHost && "Unexpected host endianness");
for (Pos += 4; Pos <= Size; Pos += 4) {
unsigned V = ((unsigned char)String[Pos - 1] << 24) |
((unsigned char)String[Pos - 2] << 16) |
More information about the llvm-commits
mailing list