X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=retrace.py;h=392519b02739e6abcf1d6b3364e1940c2c3b032b;hb=447f4a55a01e82b0265f44212b8d439fa83750d7;hp=53cc6a028af4962f57431c2cc9737a09f9019fc6;hpb=2250a0e42a0fcad757f38121859ce7de10384df3;p=apitrace diff --git a/retrace.py b/retrace.py index 53cc6a0..392519b 100644 --- a/retrace.py +++ b/retrace.py @@ -24,9 +24,11 @@ ##########################################################################/ +"""Generic retracing code generator.""" + import stdapi import glapi - +from codegen import * class ConstRemover(stdapi.Rebuilder): @@ -35,19 +37,22 @@ class ConstRemover(stdapi.Rebuilder): return const.type def visit_opaque(self, opaque): - expr = opaque.expr - if expr.startswith('const '): - expr = expr[6:] - return stdapi.Opaque(expr) + return opaque + + +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): - if literal.format == 'Bool': - print ' %s = static_cast(%s);' % (lvalue, rvalue) - else: - print ' %s = %s;' % (lvalue, rvalue) + #if literal.format in ('Bool', 'UInt'): + print ' %s = (%s).to%s();' % (lvalue, rvalue, literal.format) def visit_const(self, const, lvalue, rvalue): self.visit(const.type, lvalue, rvalue) @@ -56,7 +61,7 @@ class ValueExtractor(stdapi.Visitor): self.visit(alias.type, lvalue, rvalue) def visit_enum(self, enum, lvalue, rvalue): - print ' %s = %s;' % (lvalue, rvalue) + print ' %s = (%s).toSInt();' % (lvalue, rvalue) def visit_bitmask(self, bitmask, lvalue, rvalue): self.visit(bitmask.type, lvalue, rvalue) @@ -67,7 +72,7 @@ class ValueExtractor(stdapi.Visitor): length = '__a%s->values.size()' % array.id print ' %s = new %s[%s];' % (lvalue, array.type, length) index = '__j' + array.id - print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) + print ' for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) try: self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) finally: @@ -88,16 +93,28 @@ class ValueExtractor(stdapi.Visitor): print ' }' def visit_handle(self, handle, lvalue, rvalue): - self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue)); - print ' if (verbosity >= 2)' - print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue) + OpaqueValueExtractor().visit(handle.type, lvalue, rvalue); + new_lvalue = handle_entry(handle, lvalue) + print ' if (retrace::verbosity >= 2) {' + print ' std::cout << "%s " << size_t(%s) << " <- " << size_t(%s) << "\\n";' % (handle.name, lvalue, new_lvalue) + print ' }' + print ' %s = %s;' % (lvalue, new_lvalue) def visit_blob(self, blob, lvalue, rvalue): - print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue) + print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, blob, rvalue) def visit_string(self, string, lvalue, rvalue): - print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue) + print ' %s = (%s)((%s).toString());' % (lvalue, string.expr, rvalue) + + +class OpaqueValueExtractor(ValueExtractor): + '''Value extractor that also understands opaque values. + + Normally opaque values can't be retraced, unless they are being extracted + in the context of handles.''' + def visit_opaque(self, opaque, lvalue, rvalue): + print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, opaque, rvalue) class ValueWrapper(stdapi.Visitor): @@ -119,7 +136,7 @@ class ValueWrapper(stdapi.Visitor): print ' if (__a%s) {' % (array.id) length = '__a%s->values.size()' % array.id index = '__j' + array.id - print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) + print ' for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) try: self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) finally: @@ -134,18 +151,26 @@ class ValueWrapper(stdapi.Visitor): finally: print ' }' - def visit_handle(self, handle, lvalue, rvalue): + print ' %s __orig_result;' % handle.type + OpaqueValueExtractor().visit(handle.type, '__orig_result', rvalue); if handle.range is None: - print " __{handle.name}_map[static_cast<{handle.type}>({rvalue})] = {lvalue};".format(**locals()) - print ' if (verbosity >= 2)' - print ' std::cout << "{handle.name} " << static_cast<{handle.type}>({rvalue}) << " -> " << {lvalue} << "\\n";'.format(**locals()) + rvalue = "__orig_result" + entry = handle_entry(handle, rvalue) + print " %s = %s;" % (entry, lvalue) + print ' if (retrace::verbosity >= 2) {' + print ' std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals()) + print ' }' else: i = '__h' + handle.id - 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 ' if (verbosity >= 2)' - print ' std::cout << "{handle.name} " << (static_cast<{handle.type}>({rvalue}) + {i}) << " -> " << ({lvalue} + {i}) << "\\n";'.format(**locals()) + lvalue = "%s + %s" % (lvalue, i) + rvalue = "__orig_result + %s" % (i,) + entry = handle_entry(handle, rvalue) + print ' for ({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals()) + print ' {entry} = {lvalue};'.format(**locals()) + print ' if (retrace::verbosity >= 2) {' + print ' std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals()) + print ' }' print ' }' def visit_blob(self, blob, lvalue, rvalue): @@ -155,11 +180,19 @@ 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): + if not function.sideeffects: + print ' (void)call;' + return + success = True for arg in function.args: arg_type = ConstRemover().visit(arg.type) @@ -173,7 +206,9 @@ class Retracer: success = False print ' %s = 0; // FIXME' % arg.name if not success: + print ' if (1) {' self.fail_function(function) + print ' }' self.call_function(function) for arg in function.args: if arg.output: @@ -191,21 +226,24 @@ 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 (retrace::verbosity >= 0)' + print ' std::cerr << "warning: unsupported call %s\\n";' % function.name print ' return;' def extract_arg(self, function, arg, arg_type, lvalue, rvalue): ValueExtractor().visit(arg_type, lvalue, rvalue) + + def extract_opaque_arg(self, function, arg, arg_type, lvalue, rvalue): + OpaqueValueExtractor().visit(arg_type, lvalue, rvalue) def call_function(self, function): arg_names = ", ".join([arg.name for arg in function.args]) if function.type is not stdapi.Void: print ' %s __result;' % (function.type) print ' __result = %s(%s);' % (function.name, arg_names) + print ' (void)__result;' else: print ' %s(%s);' % (function.name, arg_names) @@ -216,29 +254,22 @@ class Retracer: functions = filter(self.filter_function, functions) for function in functions: - if function.sideeffects: - self.retrace_function(function) + self.retrace_function(function) - print 'static bool retrace_call(Trace::Call &call) {' - for function in functions: - if not function.sideeffects: - print ' if (call.name() == "%s") {' % function.name - print ' return true;' - print ' }' + print 'void retrace::retrace_call(Trace::Call &call) {' + print ' const char *name = call.name();' print - print ' if (verbosity >=1 ) {' - print ' std::cout << call;' - print ' std::cout.flush();' - print ' };' - print - for function in functions: - if function.sideeffects: - print ' if (call.name() == "%s") {' % function.name - print ' retrace_%s(call);' % function.name - print ' return true;' - print ' }' - print ' std::cerr << "warning: unknown call " << call.name() << "\\n";' - print ' return false;' + + func_dict = dict([(function.name, function) for function in functions]) + + def handle_case(function_name): + function = func_dict[function_name] + print ' retrace_%s(call);' % function.name + print ' return;' + + string_switch('name', func_dict.keys(), handle_case) + + print ' retrace_unknown(call);' print '}' print @@ -246,6 +277,7 @@ class Retracer: def retrace_api(self, api): print '#include "trace_parser.hpp"' + print '#include "retrace.hpp"' print types = api.all_types() @@ -253,141 +285,13 @@ 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 retrace::map<%s> __%s_map;' % (handle.type, handle.name) + else: + key_name, key_type = handle.key + print 'static std::map<%s, retrace::map<%s> > __%s_map;' % (key_type, handle.type, handle.name) handle_names.add(handle.name) print - print 'unsigned 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; -} - -'''