Use ELFOSABI_NONE instead of ELFOSABI_LINUX.
[oota-llvm.git] / include / llvm / Support / Endian.h
index 8d5649dc1f91ec3ab130d168b1a1422d81daef02..fd59009e0d3a60f843c4573c255f5294b66b56eb 100644 (file)
 #ifndef LLVM_SUPPORT_ENDIAN_H
 #define LLVM_SUPPORT_ENDIAN_H
 
+#include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/SwapByteOrder.h"
-#include "llvm/Support/type_traits.h"
 
 namespace llvm {
 namespace support {
+enum endianness {big, little, native};
 
-enum endianness {big, little};
-enum alignment {unaligned, aligned};
+// These are named values for common alignments.
+enum {aligned = 0, unaligned = 1};
 
 namespace detail {
-
-template<typename value_type, alignment align>
-struct alignment_access_helper;
-
-template<typename value_type>
-struct alignment_access_helper<value_type, aligned>
-{
-  value_type val;
-};
-
-// Provides unaligned loads and stores.
-#pragma pack(push)
-#pragma pack(1)
-template<typename value_type>
-struct alignment_access_helper<value_type, unaligned>
-{
-  value_type val;
-};
-#pragma pack(pop)
-
+  /// \brief ::value is either alignment, or alignof(T) if alignment is 0.
+  template<class T, int alignment>
+  struct PickAlignment {
+    enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
+  };
 } // end namespace detail
 
 namespace endian {
-  template<typename value_type, alignment align>
-  inline value_type read_le(const void *memory) {
-    value_type t =
-      reinterpret_cast<const detail::alignment_access_helper
-        <value_type, align> *>(memory)->val;
-    if (sys::isBigEndianHost())
-      return sys::SwapByteOrder(t);
-    return t;
-  }
+/// Swap the bytes of value to match the given endianness.
+template<typename value_type, endianness endian>
+inline value_type byte_swap(value_type value) {
+  if (endian != native && sys::IsBigEndianHost != (endian == big))
+    sys::swapByteOrder(value);
+  return value;
+}
 
-  template<typename value_type, alignment align>
-  inline void write_le(void *memory, value_type value) {
-    if (sys::isBigEndianHost())
-      value = sys::SwapByteOrder(value);
-    reinterpret_cast<detail::alignment_access_helper<value_type, align> *>
-      (memory)->val = value;
-  }
+/// Read a value of a particular endianness from memory.
+template<typename value_type,
+         endianness endian,
+         std::size_t alignment>
+inline value_type read(const void *memory) {
+  value_type ret;
+
+  memcpy(&ret,
+         LLVM_ASSUME_ALIGNED(memory,
+           (detail::PickAlignment<value_type, alignment>::value)),
+         sizeof(value_type));
+  return byte_swap<value_type, endian>(ret);
+}
 
-  template<typename value_type, alignment align>
-  inline value_type read_be(const void *memory) {
-    value_type t =
-      reinterpret_cast<const detail::alignment_access_helper
-        <value_type, align> *>(memory)->val;
-    if (sys::isLittleEndianHost())
-      return sys::SwapByteOrder(t);
-    return t;
-  }
+/// Read a value of a particular endianness from a buffer, and increment the
+/// buffer past that value.
+template<typename value_type, endianness endian, std::size_t alignment,
+         typename CharT>
+inline value_type readNext(const CharT *&memory) {
+  value_type ret = read<value_type, endian, alignment>(memory);
+  memory += sizeof(value_type);
+  return ret;
+}
 
-  template<typename value_type, alignment align>
-  inline void write_be(void *memory, value_type value) {
-    if (sys::isLittleEndianHost())
-      value = sys::SwapByteOrder(value);
-    reinterpret_cast<detail::alignment_access_helper<value_type, align> *>
-      (memory)->val = value;
-  }
+/// Write a value to memory with a particular endianness.
+template<typename value_type,
+         endianness endian,
+         std::size_t alignment>
+inline void write(void *memory, value_type value) {
+  value = byte_swap<value_type, endian>(value);
+  memcpy(LLVM_ASSUME_ALIGNED(memory,
+           (detail::PickAlignment<value_type, alignment>::value)),
+         &value,
+         sizeof(value_type));
 }
+} // end namespace endian
 
 namespace detail {
-
 template<typename value_type,
          endianness endian,
-         alignment  align>
-class packed_endian_specific_integral;
-
-template<typename value_type>
-class packed_endian_specific_integral<value_type, little, unaligned> {
-public:
+         std::size_t alignment>
+struct packed_endian_specific_integral {
   operator value_type() const {
-    return endian::read_le<value_type, unaligned>(Value);
+    return endian::read<value_type, endian, alignment>(
+      (const void*)Value.buffer);
   }
+
   void operator=(value_type newValue) {
-    endian::write_le<value_type, unaligned>((void *)&Value, newValue);
+    endian::write<value_type, endian, alignment>(
+      (void*)Value.buffer, newValue);
   }
-private:
-  uint8_t Value[sizeof(value_type)];
-};
 
-template<typename value_type>
-class packed_endian_specific_integral<value_type, big, unaligned> {
-public:
-  operator value_type() const {
-    return endian::read_be<value_type, unaligned>(Value);
+  packed_endian_specific_integral &operator+=(value_type newValue) {
+    *this = *this + newValue;
+    return *this;
   }
-  void operator=(value_type newValue) {
-    endian::write_be<value_type, unaligned>((void *)&Value, newValue);
+
+  packed_endian_specific_integral &operator-=(value_type newValue) {
+    *this = *this - newValue;
+    return *this;
   }
-private:
-  uint8_t Value[sizeof(value_type)];
-};
 
-template<typename value_type>
-class packed_endian_specific_integral<value_type, little, aligned> {
-public:
-  operator value_type() const {
-    return endian::read_le<value_type, aligned>(&Value);
+  packed_endian_specific_integral &operator|=(value_type newValue) {
+    *this = *this | newValue;
+    return *this;
   }
-  void operator=(value_type newValue) {
-    endian::write_le<value_type, aligned>((void *)&Value, newValue);
+
+  packed_endian_specific_integral &operator&=(value_type newValue) {
+    *this = *this & newValue;
+    return *this;
   }
+
 private:
-  value_type Value;
-};
+  AlignedCharArray<PickAlignment<value_type, alignment>::value,
+                   sizeof(value_type)> Value;
 
-template<typename value_type>
-class packed_endian_specific_integral<value_type, big, aligned> {
 public:
-  operator value_type() const {
-    return endian::read_be<value_type, aligned>(&Value);
-  }
-  void operator=(value_type newValue) {
-    endian::write_be<value_type, aligned>((void *)&Value, newValue);
-  }
-private:
-  value_type Value;
+  struct ref {
+    explicit ref(void *Ptr) : Ptr(Ptr) {}
+
+    operator value_type() const {
+      return endian::read<value_type, endian, alignment>(Ptr);
+    }
+
+    void operator=(value_type NewValue) {
+      endian::write<value_type, endian, alignment>(Ptr, NewValue);
+    }
+
+  private:
+    void *Ptr;
+  };
 };
 
 } // end namespace detail
 
-typedef detail::packed_endian_specific_integral
-                  <uint8_t, little, unaligned>  ulittle8_t;
 typedef detail::packed_endian_specific_integral
                   <uint16_t, little, unaligned> ulittle16_t;
 typedef detail::packed_endian_specific_integral
@@ -155,8 +144,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                   <uint64_t, little, unaligned> ulittle64_t;
 
-typedef detail::packed_endian_specific_integral
-                   <int8_t, little, unaligned>  little8_t;
 typedef detail::packed_endian_specific_integral
                    <int16_t, little, unaligned> little16_t;
 typedef detail::packed_endian_specific_integral
@@ -164,8 +151,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                    <int64_t, little, unaligned> little64_t;
 
-typedef detail::packed_endian_specific_integral
-                    <uint8_t, little, aligned>  aligned_ulittle8_t;
 typedef detail::packed_endian_specific_integral
                     <uint16_t, little, aligned> aligned_ulittle16_t;
 typedef detail::packed_endian_specific_integral
@@ -173,8 +158,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                     <uint64_t, little, aligned> aligned_ulittle64_t;
 
-typedef detail::packed_endian_specific_integral
-                     <int8_t, little, aligned>  aligned_little8_t;
 typedef detail::packed_endian_specific_integral
                      <int16_t, little, aligned> aligned_little16_t;
 typedef detail::packed_endian_specific_integral
@@ -182,8 +165,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                      <int64_t, little, aligned> aligned_little64_t;
 
-typedef detail::packed_endian_specific_integral
-                  <uint8_t, big, unaligned>     ubig8_t;
 typedef detail::packed_endian_specific_integral
                   <uint16_t, big, unaligned>    ubig16_t;
 typedef detail::packed_endian_specific_integral
@@ -191,8 +172,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                   <uint64_t, big, unaligned>    ubig64_t;
 
-typedef detail::packed_endian_specific_integral
-                   <int8_t, big, unaligned>     big8_t;
 typedef detail::packed_endian_specific_integral
                    <int16_t, big, unaligned>    big16_t;
 typedef detail::packed_endian_specific_integral
@@ -200,8 +179,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                    <int64_t, big, unaligned>    big64_t;
 
-typedef detail::packed_endian_specific_integral
-                    <uint8_t, big, aligned>     aligned_ubig8_t;
 typedef detail::packed_endian_specific_integral
                     <uint16_t, big, aligned>    aligned_ubig16_t;
 typedef detail::packed_endian_specific_integral
@@ -209,8 +186,6 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                     <uint64_t, big, aligned>    aligned_ubig64_t;
 
-typedef detail::packed_endian_specific_integral
-                     <int8_t, big, aligned>     aligned_big8_t;
 typedef detail::packed_endian_specific_integral
                      <int16_t, big, aligned>    aligned_big16_t;
 typedef detail::packed_endian_specific_integral
@@ -218,7 +193,36 @@ typedef detail::packed_endian_specific_integral
 typedef detail::packed_endian_specific_integral
                      <int64_t, big, aligned>    aligned_big64_t;
 
-} // end namespace llvm
+typedef detail::packed_endian_specific_integral
+                  <uint16_t, native, unaligned> unaligned_uint16_t;
+typedef detail::packed_endian_specific_integral
+                  <uint32_t, native, unaligned> unaligned_uint32_t;
+typedef detail::packed_endian_specific_integral
+                  <uint64_t, native, unaligned> unaligned_uint64_t;
+
+typedef detail::packed_endian_specific_integral
+                   <int16_t, native, unaligned> unaligned_int16_t;
+typedef detail::packed_endian_specific_integral
+                   <int32_t, native, unaligned> unaligned_int32_t;
+typedef detail::packed_endian_specific_integral
+                   <int64_t, native, unaligned> unaligned_int64_t;
+
+namespace endian {
+inline uint16_t read16le(const void *p) { return *(const ulittle16_t *)p; }
+inline uint32_t read32le(const void *p) { return *(const ulittle32_t *)p; }
+inline uint64_t read64le(const void *p) { return *(const ulittle64_t *)p; }
+inline uint16_t read16be(const void *p) { return *(const ubig16_t *)p; }
+inline uint32_t read32be(const void *p) { return *(const ubig32_t *)p; }
+inline uint64_t read64be(const void *p) { return *(const ubig64_t *)p; }
+
+inline void write16le(void *p, uint16_t v) { *(ulittle16_t *)p = v; }
+inline void write32le(void *p, uint32_t v) { *(ulittle32_t *)p = v; }
+inline void write64le(void *p, uint64_t v) { *(ulittle64_t *)p = v; }
+inline void write16be(void *p, uint16_t v) { *(ubig16_t *)p = v; }
+inline void write32be(void *p, uint32_t v) { *(ubig32_t *)p = v; }
+inline void write64be(void *p, uint64_t v) { *(ubig64_t *)p = v; }
+} // end namespace endian
 } // end namespace support
+} // end namespace llvm
 
 #endif