1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 ##########################################################################/
32 class ConstRemover(base.Rebuilder):
34 def visit_const(self, const):
37 def visit_opaque(self, opaque):
39 if expr.startswith('const '):
41 return base.Opaque(expr)
44 class ValueExtractor(base.Visitor):
46 def visit_literal(self, literal, lvalue, rvalue):
47 print ' %s = %s;' % (lvalue, rvalue)
49 def visit_alias(self, alias, lvalue, rvalue):
50 self.visit(alias.type, lvalue, rvalue)
52 def visit_enum(self, enum, lvalue, rvalue):
53 print ' %s = %s;' % (lvalue, rvalue)
55 def visit_bitmask(self, bitmask, lvalue, rvalue):
56 self.visit(bitmask.type, lvalue, rvalue)
58 def visit_array(self, array, lvalue, rvalue):
59 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
60 print ' if (__a%s) {' % (array.id)
61 length = '__a%s->values.size()' % array.id
62 print ' %s = new %s[%s];' % (lvalue, array.type, length)
63 index = '__i' + array.id
64 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
66 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
70 print ' %s = NULL;' % lvalue
73 def visit_pointer(self, pointer, lvalue, rvalue):
75 raise NotImplementedError
77 def visit_handle(self, handle, lvalue, rvalue):
78 self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
79 print ' if (verbosity >= 2)'
80 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
82 def visit_blob(self, blob, lvalue, rvalue):
83 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
85 def visit_string(self, string, lvalue, rvalue):
86 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
90 class ValueWrapper(base.Visitor):
92 def visit_literal(self, literal, lvalue, rvalue):
95 def visit_alias(self, alias, lvalue, rvalue):
96 self.visit(alias.type, lvalue, rvalue)
98 def visit_enum(self, enum, lvalue, rvalue):
101 def visit_bitmask(self, bitmask, lvalue, rvalue):
104 def visit_array(self, array, lvalue, rvalue):
105 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
106 print ' if (__a%s) {' % (array.id)
107 length = '__a%s->values.size()' % array.id
108 index = '__i' + array.id
109 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
111 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
116 def visit_pointer(self, pointer, lvalue, rvalue):
118 raise NotImplementedError
120 def visit_handle(self, handle, lvalue, rvalue):
121 print " __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
122 print ' if (verbosity >= 2)'
123 print ' std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
125 def visit_blob(self, blob, lvalue, rvalue):
128 def visit_string(self, string, lvalue, rvalue):
133 def retrace_function(function):
134 print 'static void retrace_%s(Trace::Call &call) {' % function.name
136 for arg in function.args:
137 arg_type = ConstRemover().visit(arg.type)
138 print ' // %s -> %s' % (arg.type, arg_type)
139 print ' %s %s;' % (arg_type, arg.name)
140 rvalue = 'call.arg(%u)' % (arg.index,)
143 ValueExtractor().visit(arg_type, lvalue, rvalue)
144 except NotImplementedError:
146 print ' %s = 0; // FIXME' % arg.name
148 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
150 arg_names = ", ".join([arg.name for arg in function.args])
151 if function.type is not base.Void:
152 print ' %s __result;' % (function.type)
153 print ' __result = %s(%s);' % (function.name, arg_names)
155 print ' %s(%s);' % (function.name, arg_names)
156 for arg in function.args:
158 arg_type = ConstRemover().visit(arg.type)
159 rvalue = 'call.arg(%u)' % (arg.index,)
162 ValueWrapper().visit(arg_type, lvalue, rvalue)
163 except NotImplementedError:
164 print ' // FIXME: %s' % arg.name
165 if function.type is not base.Void:
169 ValueWrapper().visit(function.type, lvalue, rvalue)
170 except NotImplementedError:
171 print ' // FIXME: result'
176 def retrace_functions(functions):
177 for function in functions:
178 if function.sideeffects:
179 retrace_function(function)
181 print 'static bool retrace_call(Trace::Call &call) {'
182 for function in functions:
183 if not function.sideeffects:
184 print ' if (call.name == "%s") {' % function.name
185 print ' return true;'
188 print ' if (verbosity >=1 ) {'
189 print ' std::cout << call;'
190 print ' std::cout.flush();'
193 for function in functions:
194 if function.sideeffects:
195 print ' if (call.name == "%s") {' % function.name
196 print ' retrace_%s(call);' % function.name
197 print ' return true;'
199 print ' std::cerr << "warning: unknown call " << call.name << "\\n";'
200 print ' return false;'
205 def retrace_api(api):
206 types = api.all_types()
208 handles = [type for type in types if isinstance(type, base.Handle)]
209 for handle in handles:
210 print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
213 retrace_functions(api.functions)
216 if __name__ == '__main__':
218 print '#include <stdlib.h>'
219 print '#include <string.h>'
222 print '#include <windows.h>'
225 print '#include <GL/glew.h>'
226 print '#include <GL/glut.h>'
228 print '#include "trace_parser.hpp"'
230 print 'unsigned verbosity = 0;'
232 retrace_api(glapi.glapi)
235 Trace::Parser parser;
237 static bool insideGlBeginEnd;
239 static void display(void) {
242 while ((call = parser.parse_call())) {
243 if (call->name == "glFlush" ||
244 call->name == "glXSwapBuffers" ||
245 call->name == "wglSwapBuffers") {
252 if (call->name == "glBegin") {
253 insideGlBeginEnd = true;
256 if (call->name == "glEnd") {
257 insideGlBeginEnd = false;
260 if (!insideGlBeginEnd) {
261 GLenum error = glGetError();
262 if (error != GL_NO_ERROR) {
263 std::cerr << "warning: glGetError() = ";
265 case GL_INVALID_ENUM:
266 std::cerr << "GL_INVALID_ENUM";
268 case GL_INVALID_VALUE:
269 std::cerr << "GL_INVALID_VALUE";
271 case GL_INVALID_OPERATION:
272 std::cerr << "GL_INVALID_OPERATION";
274 case GL_STACK_OVERFLOW:
275 std::cerr << "GL_STACK_OVERFLOW";
277 case GL_STACK_UNDERFLOW:
278 std::cerr << "GL_STACK_UNDERFLOW";
280 case GL_OUT_OF_MEMORY:
281 std::cerr << "GL_OUT_OF_MEMORY";
283 case GL_INVALID_FRAMEBUFFER_OPERATION:
284 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
286 case GL_TABLE_TOO_LARGE:
287 std::cerr << "GL_TABLE_TOO_LARGE";
302 static void idle(void) {
306 int main(int argc, char **argv)
308 glutInit(&argc, argv);
309 glutInitWindowPosition(0, 0);
310 glutInitWindowSize(800, 600);
311 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
312 glutCreateWindow(argv[0]);
315 glutDisplayFunc(&display);
319 for (i = 1; i < argc; ++i) {
320 const char *arg = argv[i];
326 if (!strcmp(arg, "--")) {
329 else if (!strcmp(arg, "-v")) {
332 std::cerr << "error: unknown option " << arg << "\\n";
337 for ( ; i < argc; ++i) {
338 if (parser.open(argv[i])) {