diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index ae581e0a331..fb71907b105 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -16,6 +16,7 @@ #define LLVM_APINT_H #include "llvm/Support/DataTypes.h" +#include "llvm/Bitcode/SerializationFwd.h" #include #include @@ -203,6 +204,16 @@ public: /// @brief Destructor. ~APInt(); + + /// Default constructor that creates an uninitialized APInt. This is useful + /// for object deserialization (pair this with the static method Read). + explicit APInt() {} + + /// @brief Used by the Bitcode serializer to emit APInts to Bitcode. + void Emit(Serializer& S) const; + + /// @brief Used by the Bitcode deserializer to deserialize APInts. + void Read(Deserializer& D); /// @} /// @name Value Tests diff --git a/lib/Bitcode/Reader/DeserializeAPInt.cpp b/lib/Bitcode/Reader/DeserializeAPInt.cpp new file mode 100644 index 00000000000..af942fbb27e --- /dev/null +++ b/lib/Bitcode/Reader/DeserializeAPInt.cpp @@ -0,0 +1,33 @@ +//===-- DeserializeAPInt.cpp - Deserialization for APInts ------*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements deserialization of APInts. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/APInt.h" +#include "llvm/Bitcode/Deserialize.h" +#include + +using namespace llvm; + +void APInt::Read(Deserializer& D) { + BitWidth = D.ReadInt(); + + if (isSingleWord()) + VAL = D.ReadInt(); + else { + uint32_t NumWords = D.ReadInt(); + assert (NumWords > 1); + pVal = new uint64_t[NumWords]; + assert (pVal && "Allocation in deserialization of APInt failed."); + for (unsigned i = 0; i < NumWords; ++i) + pVal[i] = D.ReadInt(); + } +} diff --git a/lib/Bitcode/Writer/SerializeAPInt.cpp b/lib/Bitcode/Writer/SerializeAPInt.cpp new file mode 100644 index 00000000000..33cf7f4525e --- /dev/null +++ b/lib/Bitcode/Writer/SerializeAPInt.cpp @@ -0,0 +1,31 @@ +//===-- SerializeAPInt.cpp - Serialization for APInts ----------*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under the +// University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements serialization of APInts. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/APInt.h" +#include "llvm/Bitcode/Serialize.h" +#include + +using namespace llvm; + +void APInt::Emit(Serializer& S) const { + S.EmitInt(BitWidth); + + if (isSingleWord()) + S.EmitInt(VAL); + else { + uint32_t NumWords = getNumWords(); + S.EmitInt(NumWords); + for (unsigned i = 0; i < NumWords; ++i) + S.EmitInt(pVal[i]); + } +}