]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Handle moe uniform types and sizes.
[apitrace] / trace_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
29
30
31 namespace Trace {
32
33
34 Call::~Call() {
35     for (unsigned i = 0; i < args.size(); ++i) {
36         delete args[i];
37     }
38
39     if (ret) {
40         delete ret;
41     }
42 }
43
44
45 Struct::~Struct() {
46     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
47         delete *it;
48     }
49 }
50
51
52 Array::~Array() {
53     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
54         delete *it;
55     }
56 }
57
58 Blob::~Blob() {
59     // TODO: Don't leak blobs.  Blobs are often bound and accessed during many
60     // calls, so we can't delete them here.
61     //delete [] buf;
62 }
63
64
65 // bool cast
66 bool Null   ::toBool(void) const { return false; }
67 bool Bool   ::toBool(void) const { return value; }
68 bool SInt   ::toBool(void) const { return value != 0; }
69 bool UInt   ::toBool(void) const { return value != 0; }
70 bool Float  ::toBool(void) const { return value != 0; }
71 bool String ::toBool(void) const { return true; }
72 bool Enum   ::toBool(void) const { return sig->value != 0; }
73 bool Struct ::toBool(void) const { return true; }
74 bool Array  ::toBool(void) const { return true; }
75 bool Blob   ::toBool(void) const { return true; }
76 bool Pointer::toBool(void) const { return value != 0; }
77
78
79 // signed integer cast
80 signed long long Value  ::toSInt(void) const { assert(0); return 0; }
81 signed long long Null   ::toSInt(void) const { return 0; }
82 signed long long Bool   ::toSInt(void) const { return static_cast<signed long long>(value); }
83 signed long long SInt   ::toSInt(void) const { return value; }
84 signed long long UInt   ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
85 signed long long Float  ::toSInt(void) const { return static_cast<signed long long>(value); }
86 signed long long Enum   ::toSInt(void) const { return sig->value; }
87
88
89 // unsigned integer cast
90 unsigned long long Value  ::toUInt(void) const { assert(0); return 0; }
91 unsigned long long Null   ::toUInt(void) const { return 0; }
92 unsigned long long Bool   ::toUInt(void) const { return static_cast<unsigned long long>(value); }
93 unsigned long long SInt   ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
94 unsigned long long UInt   ::toUInt(void) const { return value; }
95 unsigned long long Float  ::toUInt(void) const { return static_cast<unsigned long long>(value); }
96 unsigned long long Enum   ::toUInt(void) const { assert(sig->value >= 0); return sig->value; }
97
98
99 // floating point cast
100 float Value  ::toFloat(void) const { assert(0); return 0; }
101 float Null   ::toFloat(void) const { return 0; }
102 float Bool   ::toFloat(void) const { return static_cast<float>(value); }
103 float SInt   ::toFloat(void) const { return static_cast<float>(value); }
104 float UInt   ::toFloat(void) const { return static_cast<float>(value); }
105 float Float  ::toFloat(void) const { return value; }
106 float Enum   ::toFloat(void) const { return static_cast<float>(sig->value); }
107
108
109 // floating point cast
110 double Value  ::toDouble(void) const { assert(0); return 0; }
111 double Null   ::toDouble(void) const { return 0; }
112 double Bool   ::toDouble(void) const { return static_cast<double>(value); }
113 double SInt   ::toDouble(void) const { return static_cast<double>(value); }
114 double UInt   ::toDouble(void) const { return static_cast<double>(value); }
115 double Float  ::toDouble(void) const { return value; }
116 double Enum   ::toDouble(void) const { return static_cast<double>(sig->value); }
117
118
119 // pointer cast
120 void * Value  ::toPointer(void) const { assert(0); return NULL; }
121 void * Null   ::toPointer(void) const { return NULL; }
122 void * Blob   ::toPointer(void) const { return buf; }
123 void * Pointer::toPointer(void) const { return (void *)value; }
124
125
126 // pointer cast
127 unsigned long long Value  ::toUIntPtr(void) const { assert(0); return 0; }
128 unsigned long long Null   ::toUIntPtr(void) const { return 0; }
129 unsigned long long Pointer::toUIntPtr(void) const { return value; }
130
131
132 // string cast
133 const char * Value ::toString(void) const { assert(0); return NULL; }
134 const char * Null  ::toString(void) const { return NULL; }
135 const char * String::toString(void) const { return value; }
136
137
138 // virtual Value::visit()
139 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
140 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
141 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
142 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
143 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
144 void String ::visit(Visitor &visitor) { visitor.visit(this); }
145 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
146 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
147 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
148 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
149 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
150 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
151
152
153 void Visitor::visit(Null *) { assert(0); }
154 void Visitor::visit(Bool *) { assert(0); }
155 void Visitor::visit(SInt *) { assert(0); }
156 void Visitor::visit(UInt *) { assert(0); }
157 void Visitor::visit(Float *) { assert(0); }
158 void Visitor::visit(String *) { assert(0); }
159 void Visitor::visit(Enum *node) { assert(0); }
160 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
161 void Visitor::visit(Struct *) { assert(0); }
162 void Visitor::visit(Array *) { assert(0); }
163 void Visitor::visit(Blob *) { assert(0); }
164 void Visitor::visit(Pointer *) { assert(0); }
165
166
167 class Dumper : public Visitor
168 {
169 protected:
170     std::ostream &os;
171     Formatter::Formatter *formatter;
172     Formatter::Attribute *normal;
173     Formatter::Attribute *bold;
174     Formatter::Attribute *italic;
175     Formatter::Attribute *red;
176     Formatter::Attribute *pointer;
177     Formatter::Attribute *literal;
178
179 public:
180     Dumper(std::ostream &_os) : os(_os) {
181         formatter = Formatter::defaultFormatter();
182         normal = formatter->normal();
183         bold = formatter->bold();
184         italic = formatter->italic();
185         red = formatter->color(Formatter::RED);
186         pointer = formatter->color(Formatter::GREEN);
187         literal = formatter->color(Formatter::BLUE);
188     }
189
190     ~Dumper() {
191         delete normal;
192         delete bold;
193         delete italic;
194         delete red;
195         delete pointer;
196         delete literal;
197         delete formatter;
198     }
199
200     void visit(Null *) {
201         os << "NULL";
202     }
203
204     void visit(Bool *node) {
205         os << literal << (node->value ? "true" : "false") << normal;
206     }
207
208     void visit(SInt *node) {
209         os << literal << node->value << normal;
210     }
211
212     void visit(UInt *node) {
213         os << literal << node->value << normal;
214     }
215
216     void visit(Float *node) {
217         os << literal << node->value << normal;
218     }
219
220     void visit(String *node) {
221         os << literal << "\"";
222         for (const char *it = node->value; *it; ++it) {
223             unsigned char c = (unsigned char) *it;
224             if (c == '\"')
225                 os << "\\\"";
226             else if (c == '\\')
227                 os << "\\\\";
228             else if (c >= 0x20 && c <= 0x7e)
229                 os << c;
230             else if (c == '\t') {
231                 os << "\t";
232             } else if (c == '\r') {
233                 // Ignore carriage-return
234             } else if (c == '\n') {
235                 // Reset formatting so that it looks correct with 'less -R'
236                 os << normal << '\n' << literal;
237             } else {
238                 unsigned octal0 = c & 0x7;
239                 unsigned octal1 = (c >> 3) & 0x7;
240                 unsigned octal2 = (c >> 3) & 0x7;
241                 os << "\\";
242                 if (octal2)
243                     os << octal2;
244                 if (octal1)
245                     os << octal1;
246                 os << octal0;
247             }
248         }
249         os << "\"" << normal;
250     }
251
252     void visit(Enum *node) {
253         os << literal << node->sig->name << normal;
254     }
255
256     void visit(Bitmask *bitmask) {
257         unsigned long long value = bitmask->value;
258         const BitmaskSig *sig = bitmask->sig;
259         bool first = true;
260         for (const BitmaskFlag *it = sig->flags; value != 0 && it != sig->flags + sig->num_flags; ++it) {
261             if ((it->value && (value & it->value) == it->value) ||
262                 (!it->value && value == 0)) {
263                 if (!first) {
264                     os << " | ";
265                 }
266                 os << literal << it->name << normal;
267                 value &= ~it->value;
268                 first = false;
269             }
270         }
271         if (value || first) {
272             if (!first) {
273                 os << " | ";
274             }
275             os << literal << "0x" << std::hex << value << std::dec << normal;
276         }
277     }
278
279     void visit(Struct *s) {
280         const char *sep = "";
281         os << "{";
282         for (unsigned i = 0; i < s->members.size(); ++i) {
283             os << sep << italic << s->sig->member_names[i] << normal << " = ";
284             _visit(s->members[i]);
285             sep = ", ";
286         }
287         os << "}";
288     }
289
290     void visit(Array *array) {
291         if (array->values.size() == 1) {
292             os << "&";
293             _visit(array->values[0]);
294         }
295         else {
296             const char *sep = "";
297             os << "{";
298             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
299                 os << sep;
300                 _visit(*it);
301                 sep = ", ";
302             }
303             os << "}";
304         }
305     }
306
307     void visit(Blob *blob) {
308         os << pointer << "blob(" << blob->size << ")" << normal;
309     }
310
311     void visit(Pointer *p) {
312         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
313     }
314
315     void visit(Call *call) {
316         const char *sep = "";
317         os << bold << call->sig->name << normal << "(";
318         for (unsigned i = 0; i < call->args.size(); ++i) {
319             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
320             if (call->args[i]) {
321                 _visit(call->args[i]);
322             } else {
323                os << "?";
324             }
325             sep = ", ";
326         }
327         os << ")";
328         if (call->ret) {
329             os << " = ";
330             _visit(call->ret);
331         }
332         os << "\n";
333     }
334 };
335
336
337 std::ostream & operator <<(std::ostream &os, Value *value) {
338     Dumper d(os);
339     if (value) {
340         value->visit(d);
341     }
342     return os;
343 }
344
345
346 static Null null;
347
348 const Value & Value::operator[](size_t index) const {
349     const Array *array = dynamic_cast<const Array *>(this);
350     if (array) {
351         if (index < array->values.size()) {
352             return *array->values[index];
353         }
354     }
355     return null;
356 }
357
358 std::ostream & operator <<(std::ostream &os, Call &call) {
359     Dumper d(os);
360     os << call.no << " ";
361     d.visit(&call);
362     return os;
363 }
364
365
366 } /* namespace Trace */