Updated Authors from bk prs info
[gem5.git] / src / kern / tru64 / printf.cc
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #include <sys/types.h>
32 #include <algorithm>
33
34 #include "base/cprintf.hh"
35 #include "base/trace.hh"
36 #include "sim/host.hh"
37 #include "arch/arguments.hh"
38 #include "arch/vtophys.hh"
39
40 using namespace std;
41
42 namespace tru64 {
43
44 void
45 Printf(AlphaISA::AlphaArguments args)
46 {
47 char *p = (char *)args++;
48
49 ios::fmtflags saved_flags = DebugOut().flags();
50 char old_fill = DebugOut().fill();
51 int old_precision = DebugOut().precision();
52
53 while (*p) {
54 switch (*p) {
55 case '%': {
56 bool more = true;
57 bool islong = false;
58 bool leftjustify = false;
59 bool format = false;
60 bool zero = false;
61 int width = 0;
62 while (more && *++p) {
63 switch (*p) {
64 case 'l':
65 case 'L':
66 islong = true;
67 break;
68 case '-':
69 leftjustify = true;
70 break;
71 case '#':
72 format = true;
73 break;
74 case '0':
75 if (width)
76 width *= 10;
77 else
78 zero = true;
79 break;
80 default:
81 if (*p >= '1' && *p <= '9')
82 width = 10 * width + *p - '0';
83 else
84 more = false;
85 break;
86 }
87 }
88
89 bool hexnum = false;
90 bool octal = false;
91 bool sign = false;
92 switch (*p) {
93 case 'X':
94 case 'x':
95 hexnum = true;
96 break;
97 case 'O':
98 case 'o':
99 octal = true;
100 break;
101 case 'D':
102 case 'd':
103 sign = true;
104 break;
105 case 'P':
106 format = true;
107 case 'p':
108 hexnum = true;
109 break;
110 }
111
112 switch (*p) {
113 case 'D':
114 case 'd':
115 case 'U':
116 case 'u':
117 case 'X':
118 case 'x':
119 case 'O':
120 case 'o':
121 case 'P':
122 case 'p': {
123 if (hexnum)
124 DebugOut() << hex;
125
126 if (octal)
127 DebugOut() << oct;
128
129 if (format) {
130 if (!zero)
131 DebugOut().setf(ios::showbase);
132 else {
133 if (hexnum) {
134 DebugOut() << "0x";
135 width -= 2;
136 } else if (octal) {
137 DebugOut() << "0";
138 width -= 1;
139 }
140 }
141 }
142
143 if (zero)
144 DebugOut().fill('0');
145
146 if (width > 0)
147 DebugOut().width(width);
148
149 if (leftjustify && !zero)
150 DebugOut().setf(ios::left);
151
152 if (sign) {
153 if (islong)
154 DebugOut() << (int64_t)args;
155 else
156 DebugOut() << (int32_t)args;
157 } else {
158 if (islong)
159 DebugOut() << (uint64_t)args;
160 else
161 DebugOut() << (uint32_t)args;
162 }
163
164 if (zero)
165 DebugOut().fill(' ');
166
167 if (width > 0)
168 DebugOut().width(0);
169
170 DebugOut() << dec;
171
172 ++args;
173 }
174 break;
175
176 case 's': {
177 char *s = (char *)args;
178 if (!s)
179 s = "<NULL>";
180
181 if (width > 0)
182 DebugOut().width(width);
183 if (leftjustify)
184 DebugOut().setf(ios::left);
185
186 DebugOut() << s;
187 ++args;
188 }
189 break;
190 case 'C':
191 case 'c': {
192 uint64_t mask = (*p == 'C') ? 0xffL : 0x7fL;
193 uint64_t num;
194 int width;
195
196 if (islong) {
197 num = (uint64_t)args;
198 width = sizeof(uint64_t);
199 } else {
200 num = (uint32_t)args;
201 width = sizeof(uint32_t);
202 }
203
204 while (width-- > 0) {
205 char c = (char)(num & mask);
206 if (c)
207 DebugOut() << c;
208 num >>= 8;
209 }
210
211 ++args;
212 }
213 break;
214 case 'b': {
215 uint64_t n = (uint64_t)args++;
216 char *s = (char *)args++;
217 DebugOut() << s << ": " << n;
218 }
219 break;
220 case 'n':
221 case 'N': {
222 args += 2;
223 #if 0
224 uint64_t n = (uint64_t)args++;
225 struct reg_values *rv = (struct reg_values *)args++;
226 #endif
227 }
228 break;
229 case 'r':
230 case 'R': {
231 args += 2;
232 #if 0
233 uint64_t n = (uint64_t)args++;
234 struct reg_desc *rd = (struct reg_desc *)args++;
235 #endif
236 }
237 break;
238 case '%':
239 DebugOut() << '%';
240 break;
241 }
242 ++p;
243 }
244 break;
245 case '\n':
246 DebugOut() << endl;
247 ++p;
248 break;
249 case '\r':
250 ++p;
251 if (*p != '\n')
252 DebugOut() << endl;
253 break;
254
255 default: {
256 size_t len = strcspn(p, "%\n\r\0");
257 DebugOut().write(p, len);
258 p += len;
259 }
260 }
261 }
262
263 DebugOut().flags(saved_flags);
264 DebugOut().fill(old_fill);
265 DebugOut().precision(old_precision);
266 }
267
268 } // namespace Tru64