BitcodeWriter: Expose less implementation details -- make BackpatchWord private

and remove getBuffer().

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151748 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar
2012-02-29 20:31:01 +00:00
parent fdc8f785cd
commit 02a248afe3
2 changed files with 57 additions and 55 deletions

View File

@@ -59,6 +59,15 @@ class BitstreamWriter {
}; };
std::vector<BlockInfo> BlockInfoRecords; std::vector<BlockInfo> BlockInfoRecords;
// BackpatchWord - Backpatch a 32-bit word in the output with the specified
// value.
void BackpatchWord(unsigned ByteNo, unsigned NewWord) {
Out[ByteNo++] = (unsigned char)(NewWord >> 0);
Out[ByteNo++] = (unsigned char)(NewWord >> 8);
Out[ByteNo++] = (unsigned char)(NewWord >> 16);
Out[ByteNo ] = (unsigned char)(NewWord >> 24);
}
public: public:
explicit BitstreamWriter(std::vector<unsigned char> &O) explicit BitstreamWriter(std::vector<unsigned char> &O)
: Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {} : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
@@ -78,8 +87,6 @@ public:
} }
} }
std::vector<unsigned char> &getBuffer() { return Out; }
/// \brief Retrieve the current position in the stream, in bits. /// \brief Retrieve the current position in the stream, in bits.
uint64_t GetCurrentBitNo() const { return Out.size() * 8 + CurBit; } uint64_t GetCurrentBitNo() const { return Out.size() * 8 + CurBit; }
@@ -164,15 +171,6 @@ public:
Emit(Val, CurCodeSize); Emit(Val, CurCodeSize);
} }
// BackpatchWord - Backpatch a 32-bit word in the output with the specified
// value.
void BackpatchWord(unsigned ByteNo, unsigned NewWord) {
Out[ByteNo++] = (unsigned char)(NewWord >> 0);
Out[ByteNo++] = (unsigned char)(NewWord >> 8);
Out[ByteNo++] = (unsigned char)(NewWord >> 16);
Out[ByteNo ] = (unsigned char)(NewWord >> 24);
}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Block Manipulation // Block Manipulation
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@@ -1774,7 +1774,18 @@ enum {
DarwinBCHeaderSize = 5*4 DarwinBCHeaderSize = 5*4
}; };
static void EmitDarwinBCHeader(BitstreamWriter &Stream, const Triple &TT) { static void WriteInt32ToBuffer(uint32_t Value,
std::vector<unsigned char> &Buffer,
uint32_t &Position) {
Buffer[Position + 0] = (unsigned char) (Value >> 0);
Buffer[Position + 1] = (unsigned char) (Value >> 8);
Buffer[Position + 2] = (unsigned char) (Value >> 16);
Buffer[Position + 3] = (unsigned char) (Value >> 24);
Position += 4;
}
static void EmitDarwinBCHeaderAndTrailer(std::vector<unsigned char> &Buffer,
const Triple &TT) {
unsigned CPUType = ~0U; unsigned CPUType = ~0U;
// Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
@@ -1801,50 +1812,39 @@ static void EmitDarwinBCHeader(BitstreamWriter &Stream, const Triple &TT) {
CPUType = DARWIN_CPU_TYPE_ARM; CPUType = DARWIN_CPU_TYPE_ARM;
// Traditional Bitcode starts after header. // Traditional Bitcode starts after header.
assert(Buffer.size() >= DarwinBCHeaderSize &&
"Expected header size to be reserved");
unsigned BCOffset = DarwinBCHeaderSize; unsigned BCOffset = DarwinBCHeaderSize;
unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
Stream.Emit(0x0B17C0DE, 32); // Write the magic and version.
Stream.Emit(0 , 32); // Version. unsigned Position = 0;
Stream.Emit(BCOffset , 32); WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
Stream.Emit(0 , 32); // Filled in later. WriteInt32ToBuffer(0 , Buffer, Position); // Version.
Stream.Emit(CPUType , 32); WriteInt32ToBuffer(BCOffset , Buffer, Position);
} WriteInt32ToBuffer(BCSize , Buffer, Position);
WriteInt32ToBuffer(CPUType , Buffer, Position);
/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
/// finalize the header.
static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
// Update the size field in the header.
Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
// If the file is not a multiple of 16 bytes, insert dummy padding. // If the file is not a multiple of 16 bytes, insert dummy padding.
while (BufferSize & 15) { while (Buffer.size() & 15)
Stream.Emit(0, 8); Buffer.push_back(0);
++BufferSize;
}
} }
static void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
/// WriteBitcodeToFile - Write the specified module to the specified output /// WriteBitcodeToFile - Write the specified module to the specified output
/// stream. /// stream.
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
std::vector<unsigned char> Buffer; std::vector<unsigned char> Buffer;
BitstreamWriter Stream(Buffer);
Buffer.reserve(256*1024); Buffer.reserve(256*1024);
WriteBitcodeToStream( M, Stream ); // If this is darwin or another generic macho target, reserve space for the
// header.
// Write the generated bitstream to "Out".
Out.write((char*)&Buffer.front(), Buffer.size());
}
static void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
// If this is darwin or another generic macho target, emit a file header and
// trailer if needed.
Triple TT(M->getTargetTriple()); Triple TT(M->getTargetTriple());
if (TT.isOSDarwin()) if (TT.isOSDarwin())
EmitDarwinBCHeader(Stream, TT); Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
// Emit the module into the buffer.
{
BitstreamWriter Stream(Buffer);
// Emit the file header. // Emit the file header.
Stream.Emit((unsigned)'B', 8); Stream.Emit((unsigned)'B', 8);
@@ -1856,7 +1856,11 @@ static void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
// Emit the module. // Emit the module.
WriteModule(M, Stream); WriteModule(M, Stream);
}
if (TT.isOSDarwin()) if (TT.isOSDarwin())
EmitDarwinBCTrailer(Stream, Stream.getBuffer().size()); EmitDarwinBCHeaderAndTrailer(Buffer, TT);
// Write the generated bitstream to "Out".
Out.write((char*)&Buffer.front(), Buffer.size());
} }