José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 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 | import stdapi |
| 28 | import glapi |
| 29 | |
| 30 | |
| 31 | |
| 32 | class ConstRemover(stdapi.Rebuilder): |
| 33 | |
| 34 | def visit_const(self, const): |
| 35 | return const.type |
| 36 | |
| 37 | def visit_opaque(self, opaque): |
| 38 | expr = opaque.expr |
| 39 | if expr.startswith('const '): |
| 40 | expr = expr[6:] |
| 41 | return stdapi.Opaque(expr) |
| 42 | |
| 43 | |
| 44 | class ValueExtractor(stdapi.Visitor): |
| 45 | |
| 46 | def visit_literal(self, literal, lvalue, rvalue): |
| 47 | if literal.format == 'Bool': |
| 48 | print ' %s = static_cast<bool>(%s);' % (lvalue, rvalue) |
| 49 | else: |
| 50 | print ' %s = %s;' % (lvalue, rvalue) |
| 51 | |
| 52 | def visit_const(self, const, lvalue, rvalue): |
| 53 | self.visit(const.type, lvalue, rvalue) |
| 54 | |
| 55 | def visit_alias(self, alias, lvalue, rvalue): |
| 56 | self.visit(alias.type, lvalue, rvalue) |
| 57 | |
| 58 | def visit_enum(self, enum, lvalue, rvalue): |
| 59 | print ' %s = %s;' % (lvalue, rvalue) |
| 60 | |
| 61 | def visit_bitmask(self, bitmask, lvalue, rvalue): |
| 62 | self.visit(bitmask.type, lvalue, rvalue) |
| 63 | |
| 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) |
| 71 | try: |
| 72 | self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) |
| 73 | finally: |
| 74 | print ' }' |
| 75 | print ' } else {' |
| 76 | print ' %s = NULL;' % lvalue |
| 77 | print ' }' |
| 78 | |
| 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) |
| 83 | try: |
| 84 | self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,)) |
| 85 | finally: |
| 86 | print ' } else {' |
| 87 | print ' %s = NULL;' % lvalue |
| 88 | print ' }' |
| 89 | |
| 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) |
| 94 | |
| 95 | def visit_blob(self, blob, lvalue, rvalue): |
| 96 | print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue) |
| 97 | |
| 98 | def visit_string(self, string, lvalue, rvalue): |
| 99 | print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue) |
| 100 | |
| 101 | |
| 102 | |
| 103 | class ValueWrapper(stdapi.Visitor): |
| 104 | |
| 105 | def visit_literal(self, literal, lvalue, rvalue): |
| 106 | pass |
| 107 | |
| 108 | def visit_alias(self, alias, lvalue, rvalue): |
| 109 | self.visit(alias.type, lvalue, rvalue) |
| 110 | |
| 111 | def visit_enum(self, enum, lvalue, rvalue): |
| 112 | pass |
| 113 | |
| 114 | def visit_bitmask(self, bitmask, lvalue, rvalue): |
| 115 | pass |
| 116 | |
| 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) |
| 123 | try: |
| 124 | self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) |
| 125 | finally: |
| 126 | print ' }' |
| 127 | print ' }' |
| 128 | |
| 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) |
| 132 | try: |
| 133 | self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,)) |
| 134 | finally: |
| 135 | print ' }' |
| 136 | |
| 137 | |
| 138 | def visit_handle(self, handle, lvalue, rvalue): |
José Fonseca | d922e1d | 2010-11-25 17:14:02 +0000 | [diff] [blame] | 139 | if handle.range is None: |
| 140 | print " __{handle.name}_map[static_cast<{handle.type}>({rvalue})] = {lvalue};".format(**locals()) |
| 141 | print ' if (verbosity >= 2)' |
| 142 | print ' std::cout << "{handle.name} " << static_cast<{handle.type}>({rvalue}) << " -> " << {lvalue} << "\\n";'.format(**locals()) |
| 143 | else: |
| 144 | i = '__h' + handle.id |
| 145 | print ' for({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals()) |
| 146 | print ' __{handle.name}_map[static_cast<{handle.type}>({rvalue}) + {i}] = {lvalue} + {i};'.format(**locals()) |
| 147 | print ' if (verbosity >= 2)' |
| 148 | print ' std::cout << "{handle.name} " << (static_cast<{handle.type}>({rvalue}) + {i}) << " -> " << ({lvalue} + {i}) << "\\n";'.format(**locals()) |
| 149 | print ' }' |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 150 | |
| 151 | def visit_blob(self, blob, lvalue, rvalue): |
| 152 | pass |
| 153 | |
| 154 | def visit_string(self, string, lvalue, rvalue): |
| 155 | pass |
| 156 | |
| 157 | |
| 158 | |
| 159 | class Retracer: |
| 160 | |
| 161 | def retrace_function(self, function): |
| 162 | print 'static void retrace_%s(Trace::Call &call) {' % function.name |
| 163 | success = True |
| 164 | for arg in function.args: |
| 165 | arg_type = ConstRemover().visit(arg.type) |
| 166 | #print ' // %s -> %s' % (arg.type, arg_type) |
| 167 | print ' %s %s;' % (arg_type, arg.name) |
| 168 | rvalue = 'call.arg(%u)' % (arg.index,) |
| 169 | lvalue = arg.name |
| 170 | try: |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 171 | self.extract_arg(function, arg, arg_type, lvalue, rvalue) |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 172 | except NotImplementedError: |
| 173 | success = False |
| 174 | print ' %s = 0; // FIXME' % arg.name |
| 175 | if not success: |
José Fonseca | fa15d33 | 2010-11-25 20:22:39 +0000 | [diff] [blame] | 176 | self.fail_function(function) |
| 177 | self.call_function(function) |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 178 | for arg in function.args: |
| 179 | if arg.output: |
| 180 | arg_type = ConstRemover().visit(arg.type) |
| 181 | rvalue = 'call.arg(%u)' % (arg.index,) |
| 182 | lvalue = arg.name |
| 183 | try: |
| 184 | ValueWrapper().visit(arg_type, lvalue, rvalue) |
| 185 | except NotImplementedError: |
| 186 | print ' // FIXME: %s' % arg.name |
| 187 | if function.type is not stdapi.Void: |
| 188 | rvalue = '*call.ret' |
| 189 | lvalue = '__result' |
| 190 | try: |
| 191 | ValueWrapper().visit(function.type, lvalue, rvalue) |
| 192 | except NotImplementedError: |
| 193 | print ' // FIXME: result' |
| 194 | print '}' |
| 195 | print |
| 196 | |
José Fonseca | fa15d33 | 2010-11-25 20:22:39 +0000 | [diff] [blame] | 197 | def fail_function(self, function): |
| 198 | print ' std::cerr << "warning: unsupported call %s\\n";' % function.name |
| 199 | print ' return;' |
| 200 | |
José Fonseca | dacd8dd | 2010-11-25 17:50:26 +0000 | [diff] [blame] | 201 | def extract_arg(self, function, arg, arg_type, lvalue, rvalue): |
| 202 | ValueExtractor().visit(arg_type, lvalue, rvalue) |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 203 | |
José Fonseca | fa15d33 | 2010-11-25 20:22:39 +0000 | [diff] [blame] | 204 | def call_function(self, function): |
| 205 | arg_names = ", ".join([arg.name for arg in function.args]) |
| 206 | if function.type is not stdapi.Void: |
| 207 | print ' %s __result;' % (function.type) |
| 208 | print ' __result = %s(%s);' % (function.name, arg_names) |
| 209 | else: |
| 210 | print ' %s(%s);' % (function.name, arg_names) |
| 211 | |
José Fonseca | 4441baf | 2010-11-25 19:55:27 +0000 | [diff] [blame] | 212 | def filter_function(self, function): |
| 213 | return True |
| 214 | |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 215 | def retrace_functions(self, functions): |
José Fonseca | 4441baf | 2010-11-25 19:55:27 +0000 | [diff] [blame] | 216 | functions = filter(self.filter_function, functions) |
| 217 | |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 218 | for function in functions: |
| 219 | if function.sideeffects: |
| 220 | self.retrace_function(function) |
| 221 | |
| 222 | print 'static bool retrace_call(Trace::Call &call) {' |
| 223 | for function in functions: |
| 224 | if not function.sideeffects: |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 225 | print ' if (call.name() == "%s") {' % function.name |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 226 | print ' return true;' |
| 227 | print ' }' |
| 228 | print |
| 229 | print ' if (verbosity >=1 ) {' |
| 230 | print ' std::cout << call;' |
| 231 | print ' std::cout.flush();' |
| 232 | print ' };' |
| 233 | print |
| 234 | for function in functions: |
| 235 | if function.sideeffects: |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 236 | print ' if (call.name() == "%s") {' % function.name |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 237 | print ' retrace_%s(call);' % function.name |
| 238 | print ' return true;' |
| 239 | print ' }' |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 240 | print ' std::cerr << "warning: unknown call " << call.name() << "\\n";' |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 241 | print ' return false;' |
| 242 | print '}' |
| 243 | print |
| 244 | |
| 245 | |
| 246 | def retrace_api(self, api): |
| 247 | |
| 248 | print '#include "trace_parser.hpp"' |
| 249 | print |
| 250 | |
| 251 | types = api.all_types() |
| 252 | handles = [type for type in types if isinstance(type, stdapi.Handle)] |
José Fonseca | d922e1d | 2010-11-25 17:14:02 +0000 | [diff] [blame] | 253 | handle_names = set() |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 254 | for handle in handles: |
José Fonseca | d922e1d | 2010-11-25 17:14:02 +0000 | [diff] [blame] | 255 | if handle.name not in handle_names: |
| 256 | print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name) |
| 257 | handle_names.add(handle.name) |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 258 | print |
| 259 | |
| 260 | print 'unsigned verbosity = 0;' |
| 261 | print |
| 262 | |
| 263 | self.retrace_functions(api.functions) |
| 264 | |
| 265 | |
| 266 | if __name__ == '__main__': |
| 267 | print |
| 268 | print '#include <stdlib.h>' |
| 269 | print '#include <string.h>' |
| 270 | print |
| 271 | print '#ifdef WIN32' |
| 272 | print '#include <windows.h>' |
| 273 | print '#endif' |
| 274 | print |
| 275 | print '#include <GL/glew.h>' |
| 276 | print '#include <GL/glut.h>' |
| 277 | print |
| 278 | retrace_api(glapi.glapi) |
| 279 | print ''' |
| 280 | |
| 281 | Trace::Parser parser; |
| 282 | |
| 283 | static bool insideGlBeginEnd; |
| 284 | |
| 285 | static void display(void) { |
| 286 | Trace::Call *call; |
| 287 | |
| 288 | while ((call = parser.parse_call())) { |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 289 | if (call->name() == "glFlush" || |
| 290 | call->name() == "glXSwapBuffers" || |
| 291 | call->name() == "wglSwapBuffers") { |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 292 | glFlush(); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | retrace_call(*call); |
| 297 | |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 298 | if (call->name() == "glBegin") { |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 299 | insideGlBeginEnd = true; |
| 300 | } |
| 301 | |
José Fonseca | 2250a0e | 2010-11-26 15:01:29 +0000 | [diff] [blame^] | 302 | if (call->name() == "glEnd") { |
José Fonseca | e0e6140 | 2010-11-25 15:03:23 +0000 | [diff] [blame] | 303 | insideGlBeginEnd = false; |
| 304 | } |
| 305 | |
| 306 | if (!insideGlBeginEnd) { |
| 307 | GLenum error = glGetError(); |
| 308 | if (error != GL_NO_ERROR) { |
| 309 | std::cerr << "warning: glGetError() = "; |
| 310 | switch (error) { |
| 311 | case GL_INVALID_ENUM: |
| 312 | std::cerr << "GL_INVALID_ENUM"; |
| 313 | break; |
| 314 | case GL_INVALID_VALUE: |
| 315 | std::cerr << "GL_INVALID_VALUE"; |
| 316 | break; |
| 317 | case GL_INVALID_OPERATION: |
| 318 | std::cerr << "GL_INVALID_OPERATION"; |
| 319 | break; |
| 320 | case GL_STACK_OVERFLOW: |
| 321 | std::cerr << "GL_STACK_OVERFLOW"; |
| 322 | break; |
| 323 | case GL_STACK_UNDERFLOW: |
| 324 | std::cerr << "GL_STACK_UNDERFLOW"; |
| 325 | break; |
| 326 | case GL_OUT_OF_MEMORY: |
| 327 | std::cerr << "GL_OUT_OF_MEMORY"; |
| 328 | break; |
| 329 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 330 | std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION"; |
| 331 | break; |
| 332 | case GL_TABLE_TOO_LARGE: |
| 333 | std::cerr << "GL_TABLE_TOO_LARGE"; |
| 334 | break; |
| 335 | default: |
| 336 | std::cerr << error; |
| 337 | break; |
| 338 | } |
| 339 | std::cerr << "\\n"; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | glFlush(); |
| 345 | glutIdleFunc(NULL); |
| 346 | } |
| 347 | |
| 348 | static void idle(void) { |
| 349 | glutPostRedisplay(); |
| 350 | } |
| 351 | |
| 352 | int main(int argc, char **argv) |
| 353 | { |
| 354 | glutInit(&argc, argv); |
| 355 | glutInitWindowPosition(0, 0); |
| 356 | glutInitWindowSize(800, 600); |
| 357 | glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE); |
| 358 | glutCreateWindow(argv[0]); |
| 359 | glewInit(); |
| 360 | |
| 361 | glutDisplayFunc(&display); |
| 362 | glutIdleFunc(&idle); |
| 363 | |
| 364 | int i; |
| 365 | for (i = 1; i < argc; ++i) { |
| 366 | const char *arg = argv[i]; |
| 367 | |
| 368 | if (arg[0] != '-') { |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | if (!strcmp(arg, "--")) { |
| 373 | break; |
| 374 | } |
| 375 | else if (!strcmp(arg, "-v")) { |
| 376 | ++verbosity; |
| 377 | } else { |
| 378 | std::cerr << "error: unknown option " << arg << "\\n"; |
| 379 | return 1; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | for ( ; i < argc; ++i) { |
| 384 | if (parser.open(argv[i])) { |
| 385 | glutMainLoop(); |
| 386 | parser.close(); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | ''' |