X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=retrace.py;h=ad8c844bcab21c53b3762f0fb7a5f3e05e05e10b;hb=912301e348caf5f83d7c3c3646742b20879c038f;hp=9eb5d0d1cc8f8ab2b8ef8b00e93e9212849c35cb;hpb=522b7443666892d1d76b76f9b824989d5520ec1c;p=apitrace diff --git a/retrace.py b/retrace.py index 9eb5d0d..ad8c844 100644 --- a/retrace.py +++ b/retrace.py @@ -24,6 +24,8 @@ ##########################################################################/ +"""Generic retracing code generator.""" + import stdapi import glapi from codegen import * @@ -41,6 +43,14 @@ class ConstRemover(stdapi.Rebuilder): return stdapi.Opaque(expr) +def handle_entry(handle, value): + if handle.key is None: + return "__%s_map[%s]" % (handle.name, value) + else: + key_name, key_type = handle.key + return "__%s_map[%s][%s]" % (handle.name, key_name, value) + + class ValueExtractor(stdapi.Visitor): def visit_literal(self, literal, lvalue, rvalue): @@ -88,7 +98,7 @@ class ValueExtractor(stdapi.Visitor): print ' }' def visit_handle(self, handle, lvalue, rvalue): - self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue)); + self.visit(handle.type, lvalue, handle_entry(handle, rvalue)); print ' if (verbosity >= 2)' print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue) @@ -137,15 +147,20 @@ class ValueWrapper(stdapi.Visitor): def visit_handle(self, handle, lvalue, rvalue): if handle.range is None: - print " __{handle.name}_map[static_cast<{handle.type}>({rvalue})] = {lvalue};".format(**locals()) + rvalue = "static_cast<%s>(%s)" % (handle.type, rvalue) + entry = handle_entry(handle, rvalue) + print " %s = %s;" % (entry, lvalue) print ' if (verbosity >= 2)' - print ' std::cout << "{handle.name} " << static_cast<{handle.type}>({rvalue}) << " -> " << {lvalue} << "\\n";'.format(**locals()) + print ' std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals()) else: i = '__h' + handle.id + lvalue = "%s + %s" % (lvalue, i) + rvalue = "static_cast<%s>(%s) + %s" % (handle.type, rvalue, i) + entry = handle_entry(handle, rvalue) print ' for({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals()) - print ' __{handle.name}_map[static_cast<{handle.type}>({rvalue}) + {i}] = {lvalue} + {i};'.format(**locals()) + print ' {entry} = {lvalue};'.format(**locals()) print ' if (verbosity >= 2)' - print ' std::cout << "{handle.name} " << (static_cast<{handle.type}>({rvalue}) + {i}) << " -> " << ({lvalue} + {i}) << "\\n";'.format(**locals()) + print ' std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals()) print ' }' def visit_blob(self, blob, lvalue, rvalue): @@ -155,11 +170,15 @@ class ValueWrapper(stdapi.Visitor): pass - class Retracer: def retrace_function(self, function): print 'static void retrace_%s(Trace::Call &call) {' % function.name + self.retrace_function_body(function) + print '}' + print + + def retrace_function_body(self, function): success = True for arg in function.args: arg_type = ConstRemover().visit(arg.type) @@ -191,11 +210,10 @@ class Retracer: ValueWrapper().visit(function.type, lvalue, rvalue) except NotImplementedError: print ' // FIXME: result' - print '}' - print def fail_function(self, function): - print ' std::cerr << "warning: unsupported call %s\\n";' % function.name + print ' if (verbosity >= 0)' + print ' std::cerr << "warning: unsupported call %s\\n";' % function.name print ' return;' def extract_arg(self, function, arg, arg_type, lvalue, rvalue): @@ -222,7 +240,7 @@ class Retracer: print 'static bool retrace_call(Trace::Call &call) {' print ' const char *name = call.name().c_str();' print - print ' if (verbosity >=1 ) {' + print ' if (verbosity >= 1) {' print ' std::cout << call;' print ' std::cout.flush();' print ' };' @@ -238,7 +256,8 @@ class Retracer: string_switch('name', func_dict.keys(), handle_case) - print ' std::cerr << "warning: unknown call " << call.name() << "\\n";' + print ' if (verbosity >= 0)' + print ' std::cerr << "warning: unknown call " << call.name() << "\\n";' print ' return false;' print '}' print @@ -254,141 +273,16 @@ class Retracer: handle_names = set() for handle in handles: if handle.name not in handle_names: - print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name) + if handle.key is None: + print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name) + else: + key_name, key_type = handle.key + print 'static std::map<%s, std::map<%s, %s> > __%s_map;' % (key_type, handle.type, handle.type, handle.name) handle_names.add(handle.name) print - print 'unsigned verbosity = 0;' + print 'int verbosity = 0;' print self.retrace_functions(api.functions) - -if __name__ == '__main__': - print - print '#include ' - print '#include ' - print - print '#ifdef WIN32' - print '#include ' - print '#endif' - print - print '#include ' - print '#include ' - print - retrace_api(glapi.glapi) - print ''' - -Trace::Parser parser; - -static bool insideGlBeginEnd; - -static void display(void) { - Trace::Call *call; - - while ((call = parser.parse_call())) { - if (call->name() == "glFlush" || - call->name() == "glXSwapBuffers" || - call->name() == "wglSwapBuffers") { - glFlush(); - return; - } - - retrace_call(*call); - - if (call->name() == "glBegin") { - insideGlBeginEnd = true; - } - - if (call->name() == "glEnd") { - insideGlBeginEnd = false; - } - - if (!insideGlBeginEnd) { - GLenum error = glGetError(); - if (error != GL_NO_ERROR) { - std::cerr << "warning: glGetError() = "; - switch (error) { - case GL_INVALID_ENUM: - std::cerr << "GL_INVALID_ENUM"; - break; - case GL_INVALID_VALUE: - std::cerr << "GL_INVALID_VALUE"; - break; - case GL_INVALID_OPERATION: - std::cerr << "GL_INVALID_OPERATION"; - break; - case GL_STACK_OVERFLOW: - std::cerr << "GL_STACK_OVERFLOW"; - break; - case GL_STACK_UNDERFLOW: - std::cerr << "GL_STACK_UNDERFLOW"; - break; - case GL_OUT_OF_MEMORY: - std::cerr << "GL_OUT_OF_MEMORY"; - break; - case GL_INVALID_FRAMEBUFFER_OPERATION: - std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION"; - break; - case GL_TABLE_TOO_LARGE: - std::cerr << "GL_TABLE_TOO_LARGE"; - break; - default: - std::cerr << error; - break; - } - std::cerr << "\\n"; - } - } - } - - glFlush(); - glutIdleFunc(NULL); -} - -static void idle(void) { - glutPostRedisplay(); -} - -int main(int argc, char **argv) -{ - glutInit(&argc, argv); - glutInitWindowPosition(0, 0); - glutInitWindowSize(800, 600); - glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE); - glutCreateWindow(argv[0]); - glewInit(); - - glutDisplayFunc(&display); - glutIdleFunc(&idle); - - int i; - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - if (!strcmp(arg, "--")) { - break; - } - else if (!strcmp(arg, "-v")) { - ++verbosity; - } else { - std::cerr << "error: unknown option " << arg << "\\n"; - return 1; - } - } - - for ( ; i < argc; ++i) { - if (parser.open(argv[i])) { - glutMainLoop(); - parser.close(); - } - } - - return 0; -} - -'''