llvm.org GIT mirror llvm / 191cf28
Make some more changes suggested by Chris. Manipulators go away. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75472 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 11 years ago
3 changed file(s) with 49 addition(s) and 160 deletion(s). Raw diff Collapse all Expand all
+0
-65
include/llvm/CodeGen/AsmFormatter.h less more
None //===-- 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 }
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #ifndef LLVM_CODEGEN_ASMSTREAM_H
15 #define LLVM_CODEGEN_ASMSTREAM_H
14 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
15 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
1616
1717 #include "llvm/Support/raw_ostream.h"
1818
1919 namespace llvm
2020 {
21 /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
22 /// asm-specific constructs
21 /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
22 /// asm-specific constructs.
2323 ///
2424 class formatted_raw_ostream : public raw_ostream {
2525 private:
26 /// TheStream - The real stream we output to
26 /// TheStream - The real stream we output to.
2727 ///
2828 raw_ostream &TheStream;
2929
30 /// Column - The current output column of the stream
30 /// Column - The current output column of the stream. The column
31 /// scheme is zero-based.
3132 ///
3233 unsigned Column;
3334
6263 formatted_raw_ostream(raw_ostream &Stream)
6364 : raw_ostream(), TheStream(Stream), Column(0) {}
6465
65 /// PadToColumn - Align the output to some column number
66 /// PadToColumn - Align the output to some column number.
6667 ///
67 /// \param NewCol - The column to move to
68 /// \param NewCol - The column to move to.
6869 /// \param MinPad - The minimum space to give after the most
69 /// recent I/O, even if the current column + minpad > newcol
70 /// recent I/O, even if the current column + minpad > newcol.
7071 ///
7172 void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
7273 };
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 }
10374 }
10475
10576 #endif
None //===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
0 //===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
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.
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file contains instantiations of "standard" AsmOStreams.
9 // This file contains the implementation of formatted_raw_ostream and
10 // friends.
1011 //
1112 //===----------------------------------------------------------------------===//
1213
1314 #include "llvm/Support/FormattedStream.h"
1415
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
16 using namespace llvm;
2317
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 }
18 /// ComputeColumn - Examine the current output and figure out which
19 /// column we end up in after output.
20 ///
21 void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
22 {
23 // Keep track of the current column by scanning the string for
24 // special characters
3625
37 if (nlpos == NULL) {
38 nlpos = Ptr;
39 }
26 for (const char *epos = Ptr + Size; Ptr != epos; ++Ptr) {
27 ++Column;
28 if (*Ptr == '\n' || *Ptr == '\r')
29 Column = 0;
30 else if (*Ptr == '\t')
31 Column += (8 - (Column & 0x7)) & 0x7;
32 }
33 }
4034
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 }
35 /// PadToColumn - Align the output to some column number.
36 ///
37 /// \param NewCol - The column to move to.
38 /// \param MinPad - The minimum space to give after the most recent
39 /// I/O, even if the current column + minpad > newcol.
40 ///
41 void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad)
42 {
43 flush();
44
45 // Output spaces until we reach the desired column.
46 unsigned num = NewCol - Column;
47 if (NewCol < Column || num < MinPad) {
48 num = MinPad;
5049 }
5150
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 }
51 // TODO: Write a whole string at a time.
52 while (num-- > 0) {
53 write(' ');
7254 }
7355 }
56