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(stdapi.Rebuilder):
34 def visit_const(self, const):
37 def visit_opaque(self, opaque):
39 if expr.startswith('const '):
41 return stdapi.Opaque(expr)
44 class ValueExtractor(stdapi.Visitor):
46 def visit_literal(self, literal, lvalue, rvalue):
47 if literal.format == 'Bool':
48 print ' %s = static_cast<bool>(%s);' % (lvalue, rvalue)
50 print ' %s = %s;' % (lvalue, rvalue)
52 def visit_const(self, const, lvalue, rvalue):
53 self.visit(const.type, lvalue, rvalue)
55 def visit_alias(self, alias, lvalue, rvalue):
56 self.visit(alias.type, lvalue, rvalue)
58 def visit_enum(self, enum, lvalue, rvalue):
59 print ' %s = %s;' % (lvalue, rvalue)
61 def visit_bitmask(self, bitmask, lvalue, rvalue):
62 self.visit(bitmask.type, lvalue, rvalue)
64 def visit_array(self, array, lvalue, rvalue):
65 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
66 print ' if (__a%s) {' % (array.id)
67 length = '__a%s->values.size()' % array.id
68 print ' %s = new %s[%s];' % (lvalue, array.type, length)
69 index = '__j' + array.id
70 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
72 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
76 print ' %s = NULL;' % lvalue
79 def visit_pointer(self, pointer, lvalue, rvalue):
80 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
81 print ' if (__a%s) {' % (pointer.id)
82 print ' %s = new %s;' % (lvalue, pointer.type)
84 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
87 print ' %s = NULL;' % lvalue
90 def visit_handle(self, handle, lvalue, rvalue):
91 self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
92 print ' if (verbosity >= 2)'
93 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
95 def visit_blob(self, blob, lvalue, rvalue):
96 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
98 def visit_string(self, string, lvalue, rvalue):
99 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
103 class ValueWrapper(stdapi.Visitor):
105 def visit_literal(self, literal, lvalue, rvalue):
108 def visit_alias(self, alias, lvalue, rvalue):
109 self.visit(alias.type, lvalue, rvalue)
111 def visit_enum(self, enum, lvalue, rvalue):
114 def visit_bitmask(self, bitmask, lvalue, rvalue):
117 def visit_array(self, array, lvalue, rvalue):
118 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
119 print ' if (__a%s) {' % (array.id)
120 length = '__a%s->values.size()' % array.id
121 index = '__j' + array.id
122 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
124 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
129 def visit_pointer(self, pointer, lvalue, rvalue):
130 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
131 print ' if (__a%s) {' % (pointer.id)
133 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
138 def visit_handle(self, handle, lvalue, rvalue):
139 print " __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
140 print ' if (verbosity >= 2)'
141 print ' std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
143 def visit_blob(self, blob, lvalue, rvalue):
146 def visit_string(self, string, lvalue, rvalue):
151 def retrace_function(function):
152 print 'static void retrace_%s(Trace::Call &call) {' % function.name
154 for arg in function.args:
155 arg_type = ConstRemover().visit(arg.type)
156 #print ' // %s -> %s' % (arg.type, arg_type)
157 print ' %s %s;' % (arg_type, arg.name)
158 rvalue = 'call.arg(%u)' % (arg.index,)
161 ValueExtractor().visit(arg_type, lvalue, rvalue)
162 except NotImplementedError:
164 print ' %s = 0; // FIXME' % arg.name
166 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
168 arg_names = ", ".join([arg.name for arg in function.args])
169 if function.type is not stdapi.Void:
170 print ' %s __result;' % (function.type)
171 print ' __result = %s(%s);' % (function.name, arg_names)
173 print ' %s(%s);' % (function.name, arg_names)
174 for arg in function.args:
176 arg_type = ConstRemover().visit(arg.type)
177 rvalue = 'call.arg(%u)' % (arg.index,)
180 ValueWrapper().visit(arg_type, lvalue, rvalue)
181 except NotImplementedError:
182 print ' // FIXME: %s' % arg.name
183 if function.type is not stdapi.Void:
187 ValueWrapper().visit(function.type, lvalue, rvalue)
188 except NotImplementedError:
189 print ' // FIXME: result'
194 def retrace_functions(functions):
195 for function in functions:
196 if function.sideeffects:
197 retrace_function(function)
199 print 'static bool retrace_call(Trace::Call &call) {'
200 for function in functions:
201 if not function.sideeffects:
202 print ' if (call.name == "%s") {' % function.name
203 print ' return true;'
206 print ' if (verbosity >=1 ) {'
207 print ' std::cout << call;'
208 print ' std::cout.flush();'
211 for function in functions:
212 if function.sideeffects:
213 print ' if (call.name == "%s") {' % function.name
214 print ' retrace_%s(call);' % function.name
215 print ' return true;'
217 print ' std::cerr << "warning: unknown call " << call.name << "\\n";'
218 print ' return false;'
223 def retrace_api(api):
224 types = api.all_types()
226 handles = [type for type in types if isinstance(type, stdapi.Handle)]
227 for handle in handles:
228 print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
231 retrace_functions(api.functions)
234 if __name__ == '__main__':
236 print '#include <stdlib.h>'
237 print '#include <string.h>'
240 print '#include <windows.h>'
243 print '#include <GL/glew.h>'
244 print '#include <GL/glut.h>'
246 print '#include "trace_parser.hpp"'
248 print 'unsigned verbosity = 0;'
250 retrace_api(glapi.glapi)
253 Trace::Parser parser;
255 static bool insideGlBeginEnd;
257 static void display(void) {
260 while ((call = parser.parse_call())) {
261 if (call->name == "glFlush" ||
262 call->name == "glXSwapBuffers" ||
263 call->name == "wglSwapBuffers") {
270 if (call->name == "glBegin") {
271 insideGlBeginEnd = true;
274 if (call->name == "glEnd") {
275 insideGlBeginEnd = false;
278 if (!insideGlBeginEnd) {
279 GLenum error = glGetError();
280 if (error != GL_NO_ERROR) {
281 std::cerr << "warning: glGetError() = ";
283 case GL_INVALID_ENUM:
284 std::cerr << "GL_INVALID_ENUM";
286 case GL_INVALID_VALUE:
287 std::cerr << "GL_INVALID_VALUE";
289 case GL_INVALID_OPERATION:
290 std::cerr << "GL_INVALID_OPERATION";
292 case GL_STACK_OVERFLOW:
293 std::cerr << "GL_STACK_OVERFLOW";
295 case GL_STACK_UNDERFLOW:
296 std::cerr << "GL_STACK_UNDERFLOW";
298 case GL_OUT_OF_MEMORY:
299 std::cerr << "GL_OUT_OF_MEMORY";
301 case GL_INVALID_FRAMEBUFFER_OPERATION:
302 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
304 case GL_TABLE_TOO_LARGE:
305 std::cerr << "GL_TABLE_TOO_LARGE";
320 static void idle(void) {
324 int main(int argc, char **argv)
326 glutInit(&argc, argv);
327 glutInitWindowPosition(0, 0);
328 glutInitWindowSize(800, 600);
329 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
330 glutCreateWindow(argv[0]);
333 glutDisplayFunc(&display);
337 for (i = 1; i < argc; ++i) {
338 const char *arg = argv[i];
344 if (!strcmp(arg, "--")) {
347 else if (!strcmp(arg, "-v")) {
350 std::cerr << "error: unknown option " << arg << "\\n";
355 for ( ; i < argc; ++i) {
356 if (parser.open(argv[i])) {