]> git.cworth.org Git - apitrace/blob - trace_model.cpp
More efficient bitmask representation.
[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 void Null::visit(Visitor &visitor) {
35    visitor.visit(this);
36 }
37
38 void Bool::visit(Visitor &visitor) {
39    visitor.visit(this);
40 }
41
42 void SInt::visit(Visitor &visitor) {
43    visitor.visit(this);
44 }
45
46 void UInt::visit(Visitor &visitor) {
47    visitor.visit(this);
48 }
49
50 void Float::visit(Visitor &visitor) {
51    visitor.visit(this);
52 }
53
54 void String::visit(Visitor &visitor) {
55    visitor.visit(this);
56 }
57
58 void Const::visit(Visitor &visitor) {
59    visitor.visit(this);
60 }
61
62 void Bitmask::visit(Visitor &visitor) {
63    visitor.visit(this);
64 }
65
66 void Array::visit(Visitor &visitor) {
67    visitor.visit(this);
68 }
69
70 void Blob::visit(Visitor &visitor) {
71    visitor.visit(this);
72 }
73
74
75 class Dumper : public Visitor
76 {
77 protected:
78    std::ostream &os;
79    Formatter::Formatter *formatter;
80    Formatter::Attribute *normal;
81    Formatter::Attribute *bold;
82    Formatter::Attribute *italic;
83    Formatter::Attribute *red;
84    Formatter::Attribute *pointer;
85    Formatter::Attribute *literal;
86
87 public:
88    Dumper(std::ostream &_os) : os(_os) {
89       formatter = Formatter::defaultFormatter();
90       normal = formatter->normal();
91       bold = formatter->bold();
92       italic = formatter->italic();
93       red = formatter->color(Formatter::RED);
94       pointer = formatter->color(Formatter::GREEN);
95       literal = formatter->color(Formatter::BLUE);
96    }
97
98    ~Dumper() {
99       delete normal;
100       delete bold;
101       delete italic;
102       delete red;
103       delete pointer;
104       delete literal;
105       delete formatter;
106    }
107
108    void visit(Null *) {
109       os << "NULL";
110    }
111
112    void visit(Bool *node) {
113       os << literal << (node->value ? "true" : "false") << normal;
114    }
115
116    void visit(SInt *node) {
117       os << literal << node->value << normal;
118    }
119
120    void visit(UInt *node) {
121       os << literal << node->value << normal;
122    }
123
124    void visit(Float *node) {
125       os << literal << node->value << normal;
126    }
127
128    void visit(String *node) {
129       os << literal << '"' << node->value << '"' << normal;
130    }
131
132    void visit(Const *node) {
133       os << literal << node->name << normal;
134    }
135
136    void visit(Bitmask *bitmask) {
137       unsigned long long value = bitmask->value;
138       const Bitmask::Signature *sig = bitmask->sig;
139       bool first = true;
140       for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
141           assert(it->second);
142           if ((value & it->second) == it->second) {
143               if (!first) {
144                   os << " | ";
145               }
146               os << literal << it->first << normal;
147               value &= ~it->second;
148               first = false;
149           }
150       }
151       if (value || first) {
152           if (!first) {
153               os << " | ";
154           }
155           os << literal << std::hex << value << std::dec << normal;
156       }
157    }
158
159    void visit(Array *array) {
160       if (array->values.size() == 1) {
161          os << "&";
162          _visit(array->values[0]);
163       }
164       else {
165          const char *sep = "";
166          os << "{";
167          for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
168             os << sep;
169             _visit(*it);
170             sep = ", ";
171          }
172          os << "}";
173       }
174    }
175    
176    void visit(Blob *blob) {
177       os << pointer << "blob(" << blob->size << ")" << normal;
178    }
179
180    void visit(Call *call) {
181       const char *sep = "";
182       os << bold << call->name << normal << "(";
183       for (std::vector<Arg>::iterator it = call->args.begin(); it != call->args.end(); ++it) {
184          os << sep << italic << it->first << normal << " = ";
185          _visit(it->second);
186          sep = ", ";
187       }
188       os << ")";
189       if (call->ret) {
190          os << " = ";
191          _visit(call->ret);
192       }
193       os << "\n";
194    }
195 };
196
197
198 std::ostream & operator <<(std::ostream &os, Value *value) {
199    Dumper d(os);
200    if (value) {
201       value->visit(d);
202    }
203    return os;
204 }
205
206
207 static inline const Value *unwrap(const Value *node) {
208    const Const *c = dynamic_cast<const Const *>(node);
209    if (c)
210       return c->value;
211    return node;
212 }
213
214
215 Value::operator bool(void) const {
216    const Bool *b = dynamic_cast<const Bool *>(unwrap(this));
217    if (b)
218       return b->value;
219    assert(0);
220    return false;
221 }
222
223 Value::operator signed long long(void) const {
224    const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
225    if (sint)
226       return sint->value;
227    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
228    if (uint)
229       return uint->value;
230    assert(0);
231    return 0;
232 }
233
234 Value::operator unsigned long long(void) const {
235    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
236    if (uint)
237       return uint->value;
238    assert(0);
239    return 0;
240 }
241
242
243 Value::operator double(void) const {
244    const Float *fl = dynamic_cast<const Float *>(unwrap(this));
245    assert(fl);
246    return fl->value;
247 }
248
249 static Null null;
250
251 const Value & Value::operator[](size_t index) const {
252     const Array *array = dynamic_cast<const Array *>(unwrap(this));
253     if (array) {
254         if (index < array->values.size()) {
255             return *array->values[index];
256         }
257     }
258     return null;
259 }
260
261 void * Value::blob(void) const {
262    const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
263    if (blob)
264        return blob->buf;
265    const Null *null = dynamic_cast<const Null *>(unwrap(this));
266    if (null)
267        return NULL;
268    assert(0);
269    return NULL;
270 }
271
272 const char * Value::string(void) const {
273    const String *string = dynamic_cast<const String *>(unwrap(this));
274    if (string)
275        return string->value.c_str();
276    const Null *null = dynamic_cast<const Null *>(unwrap(this));
277    if (null)
278        return NULL;
279    assert(0);
280    return NULL;
281 }
282
283 std::ostream & operator <<(std::ostream &os, Call &call) {
284    Dumper d(os);
285    d.visit(&call);
286    return os;
287 }
288
289
290 } /* namespace Trace */