]> git.cworth.org Git - apitrace/blob - trace_model.cpp
First stab at binary trace and retracing.
[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 "trace_model.hpp"
28
29
30 namespace Trace {
31
32
33 void Void::visit(Visitor &visitor) {
34    visitor.visit(this);
35 }
36
37 void Bool::visit(Visitor &visitor) {
38    visitor.visit(this);
39 }
40
41 void SInt::visit(Visitor &visitor) {
42    visitor.visit(this);
43 }
44
45 void UInt::visit(Visitor &visitor) {
46    visitor.visit(this);
47 }
48
49 void Float::visit(Visitor &visitor) {
50    visitor.visit(this);
51 }
52
53 void String::visit(Visitor &visitor) {
54    visitor.visit(this);
55 }
56
57 void Const::visit(Visitor &visitor) {
58    visitor.visit(this);
59 }
60
61 void Array::visit(Visitor &visitor) {
62    visitor.visit(this);
63 }
64
65
66 class Dumper : public Visitor
67 {
68 public:
69    std::ostream &os;
70
71    Dumper() : os(std::cout) {}
72
73    Dumper(std::ostream &_os) : os(_os) {}
74
75    void visit(Void *node) {
76    }
77
78    void visit(Bool *node) {
79       os << (node->value ? "true" : "false");
80    }
81
82    void visit(SInt *node) {
83       os << node->value;
84    }
85
86    void visit(UInt *node) {
87       os << node->value;
88    }
89
90    void visit(Float *node) {
91       os << node->value;
92    }
93
94    void visit(String *node) {
95       os << '"' << node->value << '"';
96    }
97
98    void visit(Const *node) {
99       os << node->name;
100    }
101
102    void visit(Array *node) {
103       const char *sep = "";
104       os << "{";
105       for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
106          os << sep;
107          (*it)->visit(*this);
108          sep = ", ";
109       }
110       os << "}";
111    }
112 };
113
114
115 std::ostream & operator <<(std::ostream &os, Value *value) {
116    Dumper d(os);
117    if (value) {
118       value->visit(d);
119    }
120    return os;
121 }
122
123
124 static const Value *unwrap(const Value *node) {
125    const Const *c = dynamic_cast<const Const *>(node);
126    if (c)
127       return c->value;
128    return node;
129 }
130
131 signed long long asSInt(const Value *node) {
132    node = unwrap(node);
133    const SInt *sint = dynamic_cast<const SInt *>(node);
134    if (sint)
135       return sint->value;
136    const UInt *uint = dynamic_cast<const UInt *>(node);
137    if (uint)
138       return uint->value;
139    assert(0);
140    return 0;
141 }
142
143 unsigned long long asUInt(const Value *node) {
144    node = unwrap(node);
145    const UInt *uint = dynamic_cast<const UInt *>(node);
146    if (uint)
147       return uint->value;
148    assert(0);
149    return 0;
150 }
151
152
153 double asFloat(const Value *node) {
154    node = unwrap(node);
155    const Float *fl = dynamic_cast<const Float *>(node);
156    assert(fl);
157    return fl->value;
158 }
159
160
161 std::ostream & operator <<(std::ostream &os, Call &call) {
162    const char *sep = "";
163    os << call.name << "(";
164    for (std::list<Arg>::iterator it = call.args.begin(); it != call.args.end(); ++it) {
165       os << sep << it->first << " = " << it->second;
166       sep = ", ";
167    }
168    os << ")";
169    if (call.ret) {
170       os << " = " << call.ret;
171    }
172    os << "\n";
173    return os;
174 }
175
176
177 } /* namespace Trace */