2007-01-17 23:17:24 +01:00
|
|
|
//=== OutputBuffer.h - Output Buffer ----------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-17 23:17:24 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Methods to output values to a data buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_OUTPUTBUFFER_H
|
|
|
|
#define LLVM_SUPPORT_OUTPUTBUFFER_H
|
|
|
|
|
2009-11-05 14:30:28 +01:00
|
|
|
#include <cassert>
|
2007-01-18 02:23:11 +01:00
|
|
|
#include <string>
|
2007-01-17 23:17:24 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-01-17 23:17:24 +01:00
|
|
|
class OutputBuffer {
|
|
|
|
/// Output buffer.
|
|
|
|
std::vector<unsigned char> &Output;
|
|
|
|
|
|
|
|
/// is64Bit/isLittleEndian - This information is inferred from the target
|
|
|
|
/// machine directly, indicating what header values and flags to set.
|
|
|
|
bool is64Bit, isLittleEndian;
|
|
|
|
public:
|
2007-01-18 02:23:11 +01:00
|
|
|
OutputBuffer(std::vector<unsigned char> &Out,
|
|
|
|
bool is64bit, bool le)
|
|
|
|
: Output(Out), is64Bit(is64bit), isLittleEndian(le) {}
|
2007-01-17 23:17:24 +01:00
|
|
|
|
|
|
|
// align - Emit padding into the file until the current output position is
|
|
|
|
// aligned to the specified power of two boundary.
|
|
|
|
void align(unsigned Boundary) {
|
|
|
|
assert(Boundary && (Boundary & (Boundary - 1)) == 0 &&
|
2007-04-20 05:27:36 +02:00
|
|
|
"Must align to 2^k boundary");
|
2007-01-17 23:17:24 +01:00
|
|
|
size_t Size = Output.size();
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-01-17 23:17:24 +01:00
|
|
|
if (Size & (Boundary - 1)) {
|
|
|
|
// Add padding to get alignment to the correct place.
|
|
|
|
size_t Pad = Boundary - (Size & (Boundary - 1));
|
|
|
|
Output.resize(Size + Pad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Out Functions - Output the specified value to the data buffer.
|
|
|
|
|
|
|
|
void outbyte(unsigned char X) {
|
|
|
|
Output.push_back(X);
|
|
|
|
}
|
|
|
|
void outhalf(unsigned short X) {
|
|
|
|
if (isLittleEndian) {
|
|
|
|
Output.push_back(X & 255);
|
|
|
|
Output.push_back(X >> 8);
|
|
|
|
} else {
|
|
|
|
Output.push_back(X >> 8);
|
|
|
|
Output.push_back(X & 255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void outword(unsigned X) {
|
|
|
|
if (isLittleEndian) {
|
|
|
|
Output.push_back((X >> 0) & 255);
|
|
|
|
Output.push_back((X >> 8) & 255);
|
|
|
|
Output.push_back((X >> 16) & 255);
|
|
|
|
Output.push_back((X >> 24) & 255);
|
|
|
|
} else {
|
|
|
|
Output.push_back((X >> 24) & 255);
|
|
|
|
Output.push_back((X >> 16) & 255);
|
|
|
|
Output.push_back((X >> 8) & 255);
|
|
|
|
Output.push_back((X >> 0) & 255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void outxword(uint64_t X) {
|
|
|
|
if (isLittleEndian) {
|
|
|
|
Output.push_back(unsigned(X >> 0) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 8) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 16) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 24) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 32) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 40) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 48) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 56) & 255);
|
|
|
|
} else {
|
|
|
|
Output.push_back(unsigned(X >> 56) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 48) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 40) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 32) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 24) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 16) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 8) & 255);
|
|
|
|
Output.push_back(unsigned(X >> 0) & 255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void outaddr32(unsigned X) {
|
|
|
|
outword(X);
|
|
|
|
}
|
|
|
|
void outaddr64(uint64_t X) {
|
|
|
|
outxword(X);
|
|
|
|
}
|
|
|
|
void outaddr(uint64_t X) {
|
|
|
|
if (!is64Bit)
|
|
|
|
outword((unsigned)X);
|
|
|
|
else
|
|
|
|
outxword(X);
|
|
|
|
}
|
2007-01-18 02:23:11 +01:00
|
|
|
void outstring(const std::string &S, unsigned Length) {
|
2008-05-05 20:30:58 +02:00
|
|
|
unsigned len_to_copy = static_cast<unsigned>(S.length()) < Length
|
|
|
|
? static_cast<unsigned>(S.length()) : Length;
|
|
|
|
unsigned len_to_fill = static_cast<unsigned>(S.length()) < Length
|
|
|
|
? Length - static_cast<unsigned>(S.length()) : 0;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-01-17 23:17:24 +01:00
|
|
|
for (unsigned i = 0; i < len_to_copy; ++i)
|
|
|
|
outbyte(S[i]);
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-01-17 23:17:24 +01:00
|
|
|
for (unsigned i = 0; i < len_to_fill; ++i)
|
|
|
|
outbyte(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Fix Functions - Replace an existing entry at an offset.
|
|
|
|
|
|
|
|
void fixhalf(unsigned short X, unsigned Offset) {
|
|
|
|
unsigned char *P = &Output[Offset];
|
|
|
|
P[0] = (X >> (isLittleEndian ? 0 : 8)) & 255;
|
|
|
|
P[1] = (X >> (isLittleEndian ? 8 : 0)) & 255;
|
|
|
|
}
|
|
|
|
void fixword(unsigned X, unsigned Offset) {
|
|
|
|
unsigned char *P = &Output[Offset];
|
|
|
|
P[0] = (X >> (isLittleEndian ? 0 : 24)) & 255;
|
|
|
|
P[1] = (X >> (isLittleEndian ? 8 : 16)) & 255;
|
|
|
|
P[2] = (X >> (isLittleEndian ? 16 : 8)) & 255;
|
|
|
|
P[3] = (X >> (isLittleEndian ? 24 : 0)) & 255;
|
|
|
|
}
|
2009-06-07 23:22:38 +02:00
|
|
|
void fixxword(uint64_t X, unsigned Offset) {
|
|
|
|
unsigned char *P = &Output[Offset];
|
|
|
|
P[0] = (X >> (isLittleEndian ? 0 : 56)) & 255;
|
|
|
|
P[1] = (X >> (isLittleEndian ? 8 : 48)) & 255;
|
|
|
|
P[2] = (X >> (isLittleEndian ? 16 : 40)) & 255;
|
|
|
|
P[3] = (X >> (isLittleEndian ? 24 : 32)) & 255;
|
|
|
|
P[4] = (X >> (isLittleEndian ? 32 : 24)) & 255;
|
|
|
|
P[5] = (X >> (isLittleEndian ? 40 : 16)) & 255;
|
|
|
|
P[6] = (X >> (isLittleEndian ? 48 : 8)) & 255;
|
|
|
|
P[7] = (X >> (isLittleEndian ? 56 : 0)) & 255;
|
|
|
|
}
|
2007-01-17 23:17:24 +01:00
|
|
|
void fixaddr(uint64_t X, unsigned Offset) {
|
|
|
|
if (!is64Bit)
|
|
|
|
fixword((unsigned)X, Offset);
|
|
|
|
else
|
2009-06-07 23:22:38 +02:00
|
|
|
fixxword(X, Offset);
|
2007-01-17 23:17:24 +01:00
|
|
|
}
|
2007-02-03 03:38:15 +01:00
|
|
|
|
2007-02-05 03:37:07 +01:00
|
|
|
unsigned char &operator[](unsigned Index) {
|
2007-02-03 03:38:15 +01:00
|
|
|
return Output[Index];
|
|
|
|
}
|
2007-02-05 03:37:07 +01:00
|
|
|
const unsigned char &operator[](unsigned Index) const {
|
2007-02-03 03:38:15 +01:00
|
|
|
return Output[Index];
|
|
|
|
}
|
2007-01-17 23:17:24 +01:00
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-01-17 23:17:24 +01:00
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
#endif // LLVM_SUPPORT_OUTPUTBUFFER_H
|