llvm.org GIT mirror llvm / 62fe47a
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 David Greene 11 years ago
7 changed file(s) with 245 addition(s) and 215 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/CodeGen/AsmFormatter.h - Formatted asm framework ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains various I/O manipulators to pretty-print asm.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/FormattedStream.h"
14 #include "llvm/Target/TargetAsmInfo.h"
15
16 namespace llvm
17 {
18 /// AsmComment - An I/O manipulator to output an end-of-line comment
19 ///
20 class AsmComment : public Column {
21 private:
22 /// CommentColumn - The column at which to output a comment
23 ///
24 static const int CommentColumn = 60;
25 /// Text - The comment to output
26 ///
27 std::string Text;
28 /// TAI - Target information from the code generator
29 ///
30 const TargetAsmInfo &TAI;
31
32 public:
33 AsmComment(const TargetAsmInfo &T)
34 : Column(CommentColumn), Text(""), TAI(T) {}
35
36 AsmComment(const std::string &Cmnt,
37 const TargetAsmInfo &T)
38 : Column(CommentColumn), Text(Cmnt), TAI(T) {}
39
40 /// operator() - Store a comments tring for later processing.
41 ///
42 AsmComment &operator()(const std::string &Cmnt) {
43 Text = Cmnt;
44 return *this;
45 }
46
47 /// operator() - Make Comment a functor invoktable by a stream
48 /// output operator. This intentially hides Column's operator().
49 ///
50 formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const {
51 Column::operator()(Out);
52 Out << TAI.getCommentString() << " " << Text;
53 return(Out);
54 }
55 };
56
57 /// operator<< - Support comment formatting in formatted streams.
58 ///
59 inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out,
60 const AsmComment &Func)
61 {
62 return(Func(Out));
63 }
64 }
+0
-179
include/llvm/CodeGen/AsmStream.h less more
None //===-- llvm/CodeGen/AsmStream.h - AsmStream Framework --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains raw_ostream implementations for ASM printers to
10 // do things like pretty-print comments.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_ASMSTREAM_H
15 #define LLVM_CODEGEN_ASMSTREAM_H
16
17 #include "llvm/Target/TargetAsmInfo.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 namespace llvm
21 {
22 /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
23 /// asm-specific constructs
24 ///
25 class raw_asm_fd_ostream : public raw_fd_ostream {
26 private:
27 bool formatted;
28 int column;
29
30 protected:
31 void ComputeColumn(void) {
32 if (formatted) {
33 // Keep track of the current column by scanning the string for
34 // special characters
35
36 // Find the last newline. This is our column start. If there
37 // is no newline, start with the current column.
38 char *nlpos = NULL;
39 for (char *pos = CurBufPtr(), *epos = StartBufPtr(); pos > epos; --pos) {
40 if (*(pos-1) == '\n') {
41 nlpos = pos-1;
42 // The newline will be counted, setting this to zero. We
43 // need to do it this way in case nlpos is CurBufPtr().
44 column = -1;
45 break;
46 }
47 }
48
49 if (nlpos == NULL) {
50 nlpos = StartBufPtr();
51 }
52
53 // Walk through looking for tabs and advance column as appropriate
54 for (char *pos = nlpos, *epos = CurBufPtr(); pos != epos; ++pos) {
55 ++column;
56 if (*pos == '\t') {
57 // Advance to next tab stop (every eight characters)
58 column += ((8 - (column & 0x7)) & 0x7);
59 assert(!(column & 0x3) && "Column out of alignment");
60 }
61 }
62 }
63 }
64
65 virtual void AboutToFlush(void) {
66 ComputeColumn();
67 }
68
69 public:
70 /// raw_asm_fd_ostream - Open the specified file for writing. If
71 /// an error occurs, information about the error is put into
72 /// ErrorInfo, and the stream should be immediately destroyed; the
73 /// string will be empty if no error occurred.
74 ///
75 /// \param Filename - The file to open. If this is "-" then the
76 /// stream will use stdout instead.
77 /// \param Binary - The file should be opened in binary mode on
78 /// platforms that support this distinction.
79 raw_asm_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo)
80 : raw_fd_ostream(Filename, Binary, ErrorInfo),
81 formatted(!Binary), column(0) {}
82
83 /// raw_asm_fd_ostream ctor - FD is the file descriptor that this
84 /// writes to. If ShouldClose is true, this closes the file when
85 /// the stream is destroyed.
86 raw_asm_fd_ostream(int fd, bool shouldClose,
87 bool unbuffered=false)
88 : raw_fd_ostream(fd, shouldClose, unbuffered),
89 formatted(true), column(0) {
90 if (unbuffered) {
91 assert(0 && "asm stream must be buffered");
92 // Force buffering
93 SetBufferSize();
94 }
95 }
96
97
98 /// SetColumn - Align the output to some column number
99 ///
100 void setColumn(int newcol, int minpad = 0) {
101 if (formatted) {
102 flush();
103
104 // Output spaces until we reach the desired column
105 int num = newcol - column;
106 if (num <= 0) {
107 num = minpad;
108 }
109
110 // TODO: Write a whole string at a time
111 while (num-- > 0) {
112 write(' ');
113 }
114 }
115 };
116 };
117
118 /// Column - An I/O manipulator to advance the output to a certain column
119 ///
120 class Column {
121 private:
122 int column;
123
124 public:
125 explicit Column(int c)
126 : column(c) {}
127
128 raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const {
129 // Make at least one space before the comment
130 out.setColumn(column, 1);
131 return(out);
132 }
133 };
134
135 inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out, const Column &column)
136 {
137 return(column(out));
138 }
139
140 /// Comment - An I/O manipulator to output an end-of-line comment
141 ///
142 class Comment
143 : public Column {
144 private:
145 static const int CommentColumn = 60;
146 std::string text;
147 const TargetAsmInfo &TAI;
148
149 public:
150 Comment(const std::string &comment,
151 const TargetAsmInfo &tai)
152 : Column(CommentColumn), text(comment), TAI(tai) {}
153
154 raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const {
155 Column::operator()(out);
156 out << TAI.getCommentString() << " " << text;
157 return(out);
158 }
159 };
160
161 inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out,
162 const Comment &comment)
163 {
164 return(comment(out));
165 }
166
167 /// Asm stream equivalent for std streams
168 ///
169
170 /// WARNING: Do NOT use these streams in the constructors of global
171 /// objects. There is no mechanism to ensure they are initialized
172 /// before other global objects.
173 ///
174 extern raw_asm_fd_ostream asmouts;
175 extern raw_asm_fd_ostream asmerrs;
176 }
177
178 #endif
0 //===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains raw_ostream implementations for streams to do
10 // things like pretty-print comments.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_ASMSTREAM_H
15 #define LLVM_CODEGEN_ASMSTREAM_H
16
17 #include "llvm/Support/raw_ostream.h"
18
19 namespace llvm
20 {
21 /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
22 /// asm-specific constructs
23 ///
24 class formatted_raw_ostream : public raw_ostream {
25 private:
26 /// TheStream - The real stream we output to
27 ///
28 raw_ostream &TheStream;
29
30 /// Column - The current output column of the stream
31 ///
32 unsigned Column;
33
34 virtual void write_impl(const char *Ptr, unsigned Size) {
35 ComputeColumn(Ptr, Size);
36 TheStream.write(Ptr, Size);
37 }
38
39 /// current_pos - Return the current position within the stream,
40 /// not counting the bytes currently in the buffer.
41 virtual uint64_t current_pos() {
42 // This has the same effect as calling TheStream.current_pos(),
43 // but that interface is private.
44 return TheStream.tell() - TheStream.GetNumBytesInBuffer();
45 }
46
47 /// ComputeColumn - Examine the current output and figure out
48 /// which column we end up in after output.
49 ///
50 void ComputeColumn(const char *Ptr, unsigned Size);
51
52 public:
53 /// formatted_raw_ostream - Open the specified file for
54 /// writing. If an error occurs, information about the error is
55 /// put into ErrorInfo, and the stream should be immediately
56 /// destroyed; the string will be empty if no error occurred.
57 ///
58 /// \param Filename - The file to open. If this is "-" then the
59 /// stream will use stdout instead.
60 /// \param Binary - The file should be opened in binary mode on
61 /// platforms that support this distinction.
62 formatted_raw_ostream(raw_ostream &Stream)
63 : raw_ostream(), TheStream(Stream), Column(0) {}
64
65 /// PadToColumn - Align the output to some column number
66 ///
67 /// \param NewCol - The column to move to
68 /// \param MinPad - The minimum space to give after the most
69 /// recent I/O, even if the current column + minpad > newcol
70 ///
71 void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
72 };
73
74 /// Column - An I/O manipulator to advance the output to a certain column
75 ///
76 class Column {
77 private:
78 /// Col - The column to move to
79 ///
80 unsigned int Col;
81
82 public:
83 explicit Column(unsigned int c)
84 : Col(c) {}
85
86 /// operator() - Make Column a functor invokable by a stream
87 /// output operator.
88 ///
89 formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const {
90 // Make at least one space before the next output
91 Out.PadToColumn(Col, 1);
92 return(Out);
93 }
94 };
95
96 /// operator<< - Support coulmn-setting in formatted streams.
97 ///
98 inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out,
99 const Column &Func)
100 {
101 return(Func(Out));
102 }
103 }
104
105 #endif
4343 char *OutBufStart, *OutBufEnd, *OutBufCur;
4444 bool Unbuffered;
4545
46 protected:
47 /// CurBufPtr - Get a pointer to the current location in the buffer.
48 ///
49 char *CurBufPtr(void) { return OutBufCur; }
50 /// StartBufPtr - Get a pointer to the start of the buffer
51 ///
52 char *StartBufPtr(void) { return OutBufStart; }
53 /// EndBufPtr - Get a pointer to the end of the buffer
54 ///
55 char *EndBufPtr(void) { return OutBufEnd; }
56
57 /// AboutToFlush- Called when the buffer is about to be flushed,
58 /// allowing derived classes to take some action.
59 ///
60 virtual void AboutToFlush(void) {};
61
6246 public:
6347 // color order matches ANSI escape sequence, don't change
6448 enum Colors {
+0
-19
lib/CodeGen/AsmStream.cpp less more
None //===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains instantiations of "standard" AsmOStreams.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/CodeGen/AsmStream.h"
14
15 namespace llvm {
16 raw_asm_fd_ostream asmouts(STDOUT_FILENO, false);
17 raw_asm_fd_ostream asmerrs(STDERR_FILENO, false);
18 }
0 //===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains instantiations of "standard" AsmOStreams.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/FormattedStream.h"
14
15 namespace llvm {
16 /// ComputeColumn - Examine the current output and figure out which
17 /// column we end up in after output.
18 ///
19 void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
20 {
21 // Keep track of the current column by scanning the string for
22 // special characters
23
24 // Find the last newline. This is our column start. If there
25 // is no newline, start with the current column.
26 const char *nlpos = NULL;
27 for (const char *pos = Ptr + Size, *epos = Ptr; pos > epos; --pos) {
28 if (*(pos-1) == '\n') {
29 nlpos = pos-1;
30 // The newline will be counted, setting this to zero. We
31 // need to do it this way in case nlpos is Ptr.
32 Column = -1;
33 break;
34 }
35 }
36
37 if (nlpos == NULL) {
38 nlpos = Ptr;
39 }
40
41 // Walk through looking for tabs and advance column as appropriate
42 for (const char *pos = nlpos, *epos = Ptr + Size; pos != epos; ++pos) {
43 ++Column;
44 if (*pos == '\t') {
45 // Advance to next tab stop (every eight characters)
46 Column += ((8 - (Column & 0x7)) & 0x7);
47 assert(!(Column & 0x3) && "Column out of alignment");
48 }
49 }
50 }
51
52 /// PadToColumn - Align the output to some column number
53 ///
54 /// \param NewCol - The column to move to
55 /// \param MinPad - The minimum space to give after the most recent
56 /// I/O, even if the current column + minpad > newcol
57 ///
58 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad)
59 {
60 flush();
61
62 // Output spaces until we reach the desired column
63 unsigned num = NewCol - Column;
64 if (NewCol < Column || num < MinPad) {
65 num = MinPad;
66 }
67
68 // TODO: Write a whole string at a time
69 while (num-- > 0) {
70 write(' ');
71 }
72 }
73 }
119119
120120 void raw_ostream::flush_nonempty() {
121121 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
122 AboutToFlush();
123122 write_impl(OutBufStart, OutBufCur - OutBufStart);
124123 OutBufCur = OutBufStart;
125124 }