diff options
author | Owen Anderson <resistor@mac.com> | 2008-08-21 20:58:52 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2008-08-21 20:58:52 +0000 |
commit | d2850538db5acce4b14e1c173e9c76722129d602 (patch) | |
tree | 9c007df3c9596597f4957ef9906393c07bd9186c /llvm/lib/Support/raw_ostream.cpp | |
parent | 0d191fd8d11a09ddde43ea2532f55fbd752c0eca (diff) | |
download | bcm5719-llvm-d2850538db5acce4b14e1c173e9c76722129d602.tar.gz bcm5719-llvm-d2850538db5acce4b14e1c173e9c76722129d602.zip |
Move non-trivial methods out of line to avoid code-size bloat.
llvm-svn: 55138
Diffstat (limited to 'llvm/lib/Support/raw_ostream.cpp')
-rw-r--r-- | llvm/lib/Support/raw_ostream.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index 39686cbcc6a..b1209a2da73 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -33,6 +33,98 @@ using namespace llvm; // An out of line virtual method to provide a home for the class vtable. void raw_ostream::handle() {} +raw_ostream &raw_ostream::operator<<(unsigned long N) { + // Zero is a special case. + if (N == 0) + return *this << '0'; + + char NumberBuffer[20]; + char *EndPtr = NumberBuffer+sizeof(NumberBuffer); + char *CurPtr = EndPtr; + + while (N) { + *--CurPtr = '0' + char(N % 10); + N /= 10; + } + return write(CurPtr, EndPtr-CurPtr); +} + +raw_ostream &raw_ostream::operator<<(long N) { + if (N < 0) { + if (OutBufCur >= OutBufEnd) + flush_impl(); + *OutBufCur++ = '-'; + + N = -N; + } + + return this->operator<<(static_cast<unsigned long>(N)); +} + +raw_ostream &raw_ostream::operator<<(unsigned long long N) { + // Zero is a special case. + if (N == 0) + return *this << '0'; + + char NumberBuffer[20]; + char *EndPtr = NumberBuffer+sizeof(NumberBuffer); + char *CurPtr = EndPtr; + + while (N) { + *--CurPtr = '0' + char(N % 10); + N /= 10; + } + return write(CurPtr, EndPtr-CurPtr); +} + +raw_ostream &raw_ostream::operator<<(long long N) { + if (N < 0) { + if (OutBufCur >= OutBufEnd) + flush_impl(); + *OutBufCur++ = '-'; + + N = -N; + } + + return this->operator<<(static_cast<unsigned long long>(N)); +} + +raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { + if (OutBufCur+Size > OutBufEnd) + flush_impl(); + + // Handle short strings specially, memcpy isn't very good at very short + // strings. + switch (Size) { + case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH + case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH + case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH + case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH + case 0: break; + default: + // Normally the string to emit is shorter than the buffer. + if (Size <= unsigned(OutBufEnd-OutBufStart)) { + memcpy(OutBufCur, Ptr, Size); + break; + } + + // If emitting a string larger than our buffer, emit in chunks. In this + // case we know that we just flushed the buffer. + while (Size) { + unsigned NumToEmit = OutBufEnd-OutBufStart; + if (Size < NumToEmit) NumToEmit = Size; + assert(OutBufCur == OutBufStart); + memcpy(OutBufStart, Ptr, NumToEmit); + Ptr += NumToEmit; + OutBufCur = OutBufStart + NumToEmit; + flush_impl(); + } + break; + } + OutBufCur += Size; + return *this; +} + //===----------------------------------------------------------------------===// // raw_fd_ostream //===----------------------------------------------------------------------===// |