José Fonseca | 7e32902 | 2010-11-19 17:05:18 +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 base |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 28 | import gl |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 29 | |
| 30 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 31 | |
| 32 | class ConstRemover(base.Rebuilder): |
| 33 | |
| 34 | def visit_const(self, const): |
| 35 | return const.type |
| 36 | |
José Fonseca | f6592d7 | 2010-11-21 12:44:41 +0000 | [diff] [blame] | 37 | def visit_opaque(self, opaque): |
| 38 | expr = opaque.expr |
| 39 | if expr.startswith('const '): |
| 40 | expr = expr[6:] |
| 41 | return base.Opaque(expr) |
| 42 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 43 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 44 | class ValueExtractor(base.Visitor): |
| 45 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 46 | def visit_literal(self, literal, lvalue, rvalue): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 47 | print ' %s = %s;' % (lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 48 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 49 | def visit_alias(self, alias, lvalue, rvalue): |
| 50 | self.visit(alias.type, lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 51 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 52 | def visit_enum(self, enum, lvalue, rvalue): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 53 | print ' %s = %s;' % (lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 54 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 55 | def visit_bitmask(self, bitmask, lvalue, rvalue): |
| 56 | self.visit(bitmask.type, lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 57 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 58 | def visit_array(self, array, lvalue, rvalue): |
José Fonseca | 8badad0 | 2010-11-21 02:34:13 +0000 | [diff] [blame] | 59 | print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue) |
| 60 | print ' if (__a%s) {' % (array.id) |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 61 | length = '__a%s->values.size()' % array.id |
| 62 | print ' %s = new %s[%s];' % (lvalue, array.type, length) |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 63 | index = '__i' + array.id |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 64 | print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length) |
| 65 | try: |
| 66 | self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index)) |
| 67 | finally: |
| 68 | print ' }' |
| 69 | print ' } else {' |
| 70 | print ' %s = NULL;' % lvalue |
| 71 | print ' }' |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 72 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 73 | def visit_blob(self, blob, lvalue, rvalue): |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 74 | print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue) |
| 75 | |
| 76 | def visit_string(self, string, lvalue, rvalue): |
| 77 | print ' %s = (%s).string();' % (lvalue, rvalue) |
José Fonseca | 885f265 | 2010-11-20 11:22:25 +0000 | [diff] [blame] | 78 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def retrace_function(function): |
| 82 | print 'static void retrace_%s(Trace::Call &call) {' % function.name |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame^] | 83 | success = True |
| 84 | for arg in function.args: |
| 85 | arg.type = ConstRemover().visit(arg.type) |
| 86 | print ' %s %s;' % (arg.type, arg.name) |
| 87 | rvalue = 'call.arg("%s")' % (arg.name,) |
| 88 | lvalue = arg.name |
| 89 | try: |
| 90 | ValueExtractor().visit(arg.type, lvalue, rvalue) |
| 91 | except NotImplementedError: |
| 92 | success = False |
| 93 | print ' %s = 0; // FIXME' % arg.name |
| 94 | if not success: |
| 95 | print ' std::cerr << "warning: unsupported call %s\\n";' % function.name |
| 96 | print ' return;' |
| 97 | arg_names = ", ".join([arg.name for arg in function.args]) |
| 98 | print ' %s(%s);' % (function.name, arg_names) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 99 | print '}' |
| 100 | print |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 101 | |
| 102 | |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 103 | def retrace_functions(functions): |
| 104 | for function in functions: |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame^] | 105 | if function.sideeffects: |
| 106 | retrace_function(function) |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 107 | |
| 108 | print 'static bool retrace_call(Trace::Call &call) {' |
| 109 | for function in functions: |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame^] | 110 | if not function.sideeffects: |
| 111 | print ' if (call.name == "%s") {' % function.name |
| 112 | print ' return true;' |
| 113 | print ' }' |
| 114 | print |
| 115 | print ' std::cout << call;' |
| 116 | print ' std::cout.flush();' |
| 117 | print |
| 118 | for function in functions: |
| 119 | if function.sideeffects: |
| 120 | print ' if (call.name == "%s") {' % function.name |
| 121 | print ' retrace_%s(call);' % function.name |
| 122 | print ' return true;' |
| 123 | print ' }' |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 124 | print ' std::cerr << "warning: unsupported call " << call.name << "\\n";' |
| 125 | print ' return false;' |
| 126 | print '}' |
| 127 | print |
| 128 | |
| 129 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 130 | if __name__ == '__main__': |
| 131 | print |
| 132 | print '#include <stdlib.h>' |
| 133 | print '#include <string.h>' |
| 134 | print '#include <GL/glew.h>' |
| 135 | print '#include <GL/glut.h>' |
| 136 | print |
| 137 | print '#include "trace_parser.hpp"' |
| 138 | print |
| 139 | |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 140 | functions = gl.basic_functions(base.Function) + gl.extended_functions(base.Function) |
| 141 | retrace_functions(functions) |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 142 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 143 | print ''' |
| 144 | |
| 145 | class Retracer : public Trace::Parser |
| 146 | { |
| 147 | void handle_call(Trace::Call &call) { |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 148 | retrace_call(call); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | int main(int argc, char **argv) |
| 153 | { |
| 154 | glutInit(&argc, argv); |
| 155 | glutInitWindowPosition( 0, 0 ); |
| 156 | glutInitWindowSize( 800, 600 ); |
| 157 | glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE ); |
| 158 | glutCreateWindow(argv[0]); |
| 159 | glewInit(); |
| 160 | for (int i = 1; i < argc; ++i) { |
| 161 | Retracer p; |
| 162 | p.parse(argv[i]); |
| 163 | glutMainLoop(); |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | ''' |