diff options
author | David Greene <greened@obbligato.org> | 2009-07-10 21:14:44 +0000 |
---|---|---|
committer | David Greene <greened@obbligato.org> | 2009-07-10 21:14:44 +0000 |
commit | 62fe47a33755719ab9c6e8c239e0dd01fc87e6f9 (patch) | |
tree | 3d0c98ada5d7952dc0723c6710bf0eeb54db2ef9 /include | |
parent | a93e77073faf8547f8405919339f44cebdc74d11 (diff) | |
download | external_llvm-62fe47a33755719ab9c6e8c239e0dd01fc87e6f9.zip external_llvm-62fe47a33755719ab9c6e8c239e0dd01fc87e6f9.tar.gz external_llvm-62fe47a33755719ab9c6e8c239e0dd01fc87e6f9.tar.bz2 |
Make changes suggested by Chris and eliminate newly-added raw_ostream
hooks as they're no longer needed.
The major change with this patch is to make formatted_raw_ostream usable
by any client of raw_ostream.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75283 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/CodeGen/AsmFormatter.h | 65 | ||||
-rw-r--r-- | include/llvm/CodeGen/AsmStream.h | 179 | ||||
-rw-r--r-- | include/llvm/Support/FormattedStream.h | 106 | ||||
-rw-r--r-- | include/llvm/Support/raw_ostream.h | 16 |
4 files changed, 171 insertions, 195 deletions
diff --git a/include/llvm/CodeGen/AsmFormatter.h b/include/llvm/CodeGen/AsmFormatter.h new file mode 100644 index 0000000..7d446bc --- /dev/null +++ b/include/llvm/CodeGen/AsmFormatter.h @@ -0,0 +1,65 @@ +//===-- llvm/CodeGen/AsmFormatter.h - Formatted asm framework ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains various I/O manipulators to pretty-print asm. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/FormattedStream.h" +#include "llvm/Target/TargetAsmInfo.h" + +namespace llvm +{ + /// AsmComment - An I/O manipulator to output an end-of-line comment + /// + class AsmComment : public Column { + private: + /// CommentColumn - The column at which to output a comment + /// + static const int CommentColumn = 60; + /// Text - The comment to output + /// + std::string Text; + /// TAI - Target information from the code generator + /// + const TargetAsmInfo &TAI; + + public: + AsmComment(const TargetAsmInfo &T) + : Column(CommentColumn), Text(""), TAI(T) {} + + AsmComment(const std::string &Cmnt, + const TargetAsmInfo &T) + : Column(CommentColumn), Text(Cmnt), TAI(T) {} + + /// operator() - Store a comments tring for later processing. + /// + AsmComment &operator()(const std::string &Cmnt) { + Text = Cmnt; + return *this; + } + + /// operator() - Make Comment a functor invoktable by a stream + /// output operator. This intentially hides Column's operator(). + /// + formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const { + Column::operator()(Out); + Out << TAI.getCommentString() << " " << Text; + return(Out); + } + }; + + /// operator<< - Support comment formatting in formatted streams. + /// + inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out, + const AsmComment &Func) + { + return(Func(Out)); + } +} diff --git a/include/llvm/CodeGen/AsmStream.h b/include/llvm/CodeGen/AsmStream.h deleted file mode 100644 index 6b17c73..0000000 --- a/include/llvm/CodeGen/AsmStream.h +++ /dev/null @@ -1,179 +0,0 @@ -//===-- llvm/CodeGen/AsmStream.h - AsmStream Framework --------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains raw_ostream implementations for ASM printers to -// do things like pretty-print comments. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CODEGEN_ASMSTREAM_H -#define LLVM_CODEGEN_ASMSTREAM_H - -#include "llvm/Target/TargetAsmInfo.h" -#include "llvm/Support/raw_ostream.h" - -namespace llvm -{ - /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle - /// asm-specific constructs - /// - class raw_asm_fd_ostream : public raw_fd_ostream { - private: - bool formatted; - int column; - - protected: - void ComputeColumn(void) { - if (formatted) { - // Keep track of the current column by scanning the string for - // special characters - - // Find the last newline. This is our column start. If there - // is no newline, start with the current column. - char *nlpos = NULL; - for (char *pos = CurBufPtr(), *epos = StartBufPtr(); pos > epos; --pos) { - if (*(pos-1) == '\n') { - nlpos = pos-1; - // The newline will be counted, setting this to zero. We - // need to do it this way in case nlpos is CurBufPtr(). - column = -1; - break; - } - } - - if (nlpos == NULL) { - nlpos = StartBufPtr(); - } - - // Walk through looking for tabs and advance column as appropriate - for (char *pos = nlpos, *epos = CurBufPtr(); pos != epos; ++pos) { - ++column; - if (*pos == '\t') { - // Advance to next tab stop (every eight characters) - column += ((8 - (column & 0x7)) & 0x7); - assert(!(column & 0x3) && "Column out of alignment"); - } - } - } - } - - virtual void AboutToFlush(void) { - ComputeColumn(); - } - - public: - /// raw_asm_fd_ostream - Open the specified file for writing. If - /// an error occurs, information about the error is put into - /// ErrorInfo, and the stream should be immediately destroyed; the - /// string will be empty if no error occurred. - /// - /// \param Filename - The file to open. If this is "-" then the - /// stream will use stdout instead. - /// \param Binary - The file should be opened in binary mode on - /// platforms that support this distinction. - raw_asm_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo) - : raw_fd_ostream(Filename, Binary, ErrorInfo), - formatted(!Binary), column(0) {} - - /// raw_asm_fd_ostream ctor - FD is the file descriptor that this - /// writes to. If ShouldClose is true, this closes the file when - /// the stream is destroyed. - raw_asm_fd_ostream(int fd, bool shouldClose, - bool unbuffered=false) - : raw_fd_ostream(fd, shouldClose, unbuffered), - formatted(true), column(0) { - if (unbuffered) { - assert(0 && "asm stream must be buffered"); - // Force buffering - SetBufferSize(); - } - } - - - /// SetColumn - Align the output to some column number - /// - void setColumn(int newcol, int minpad = 0) { - if (formatted) { - flush(); - - // Output spaces until we reach the desired column - int num = newcol - column; - if (num <= 0) { - num = minpad; - } - - // TODO: Write a whole string at a time - while (num-- > 0) { - write(' '); - } - } - }; - }; - - /// Column - An I/O manipulator to advance the output to a certain column - /// - class Column { - private: - int column; - - public: - explicit Column(int c) - : column(c) {} - - raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const { - // Make at least one space before the comment - out.setColumn(column, 1); - return(out); - } - }; - - inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out, const Column &column) - { - return(column(out)); - } - - /// Comment - An I/O manipulator to output an end-of-line comment - /// - class Comment - : public Column { - private: - static const int CommentColumn = 60; - std::string text; - const TargetAsmInfo &TAI; - - public: - Comment(const std::string &comment, - const TargetAsmInfo &tai) - : Column(CommentColumn), text(comment), TAI(tai) {} - - raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const { - Column::operator()(out); - out << TAI.getCommentString() << " " << text; - return(out); - } - }; - - inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out, - const Comment &comment) - { - return(comment(out)); - } - - /// Asm stream equivalent for std streams - /// - - /// WARNING: Do NOT use these streams in the constructors of global - /// objects. There is no mechanism to ensure they are initialized - /// before other global objects. - /// - extern raw_asm_fd_ostream asmouts; - extern raw_asm_fd_ostream asmerrs; -} - -#endif diff --git a/include/llvm/Support/FormattedStream.h b/include/llvm/Support/FormattedStream.h new file mode 100644 index 0000000..fb8aa35 --- /dev/null +++ b/include/llvm/Support/FormattedStream.h @@ -0,0 +1,106 @@ +//===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains raw_ostream implementations for streams to do +// things like pretty-print comments. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_ASMSTREAM_H +#define LLVM_CODEGEN_ASMSTREAM_H + +#include "llvm/Support/raw_ostream.h" + +namespace llvm +{ + /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle + /// asm-specific constructs + /// + class formatted_raw_ostream : public raw_ostream { + private: + /// TheStream - The real stream we output to + /// + raw_ostream &TheStream; + + /// Column - The current output column of the stream + /// + unsigned Column; + + virtual void write_impl(const char *Ptr, unsigned Size) { + ComputeColumn(Ptr, Size); + TheStream.write(Ptr, Size); + } + + /// current_pos - Return the current position within the stream, + /// not counting the bytes currently in the buffer. + virtual uint64_t current_pos() { + // This has the same effect as calling TheStream.current_pos(), + // but that interface is private. + return TheStream.tell() - TheStream.GetNumBytesInBuffer(); + } + + /// ComputeColumn - Examine the current output and figure out + /// which column we end up in after output. + /// + void ComputeColumn(const char *Ptr, unsigned Size); + + public: + /// formatted_raw_ostream - Open the specified file for + /// writing. If an error occurs, information about the error is + /// put into ErrorInfo, and the stream should be immediately + /// destroyed; the string will be empty if no error occurred. + /// + /// \param Filename - The file to open. If this is "-" then the + /// stream will use stdout instead. + /// \param Binary - The file should be opened in binary mode on + /// platforms that support this distinction. + formatted_raw_ostream(raw_ostream &Stream) + : raw_ostream(), TheStream(Stream), Column(0) {} + + /// PadToColumn - Align the output to some column number + /// + /// \param NewCol - The column to move to + /// \param MinPad - The minimum space to give after the most + /// recent I/O, even if the current column + minpad > newcol + /// + void PadToColumn(unsigned NewCol, unsigned MinPad = 0); + }; + + /// Column - An I/O manipulator to advance the output to a certain column + /// + class Column { + private: + /// Col - The column to move to + /// + unsigned int Col; + + public: + explicit Column(unsigned int c) + : Col(c) {} + + /// operator() - Make Column a functor invokable by a stream + /// output operator. + /// + formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const { + // Make at least one space before the next output + Out.PadToColumn(Col, 1); + return(Out); + } + }; + + /// operator<< - Support coulmn-setting in formatted streams. + /// + inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out, + const Column &Func) + { + return(Func(Out)); + } +} + +#endif diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index 22fb384..8242f04 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -44,22 +44,6 @@ private: char *OutBufStart, *OutBufEnd, *OutBufCur; bool Unbuffered; -protected: - /// CurBufPtr - Get a pointer to the current location in the buffer. - /// - char *CurBufPtr(void) { return OutBufCur; } - /// StartBufPtr - Get a pointer to the start of the buffer - /// - char *StartBufPtr(void) { return OutBufStart; } - /// EndBufPtr - Get a pointer to the end of the buffer - /// - char *EndBufPtr(void) { return OutBufEnd; } - - /// AboutToFlush- Called when the buffer is about to be flushed, - /// allowing derived classes to take some action. - /// - virtual void AboutToFlush(void) {}; - public: // color order matches ANSI escape sequence, don't change enum Colors { |