blob: e5675af2e743df8043ebbd215f3f7427df5234ec [file] [log] [blame]
José Fonseca7e329022010-11-19 17:05:18 +00001##########################################################################
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
27import base
28from glx import libgl
29
30
José Fonsecac9edb832010-11-20 09:03:10 +000031
32class ConstRemover(base.Rebuilder):
33
34 def visit_const(self, const):
35 return const.type
36
37
José Fonseca501f2862010-11-19 20:41:18 +000038class ValueExtractor(base.Visitor):
39
José Fonsecab11188f2010-11-21 01:53:58 +000040 def visit_literal(self, literal, lvalue, rvalue):
José Fonsecac9edb832010-11-20 09:03:10 +000041 print ' %s = %s;' % (lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000042
José Fonsecab11188f2010-11-21 01:53:58 +000043 def visit_alias(self, alias, lvalue, rvalue):
44 self.visit(alias.type, lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000045
José Fonsecab11188f2010-11-21 01:53:58 +000046 def visit_enum(self, enum, lvalue, rvalue):
José Fonsecac9edb832010-11-20 09:03:10 +000047 print ' %s = %s;' % (lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000048
José Fonsecab11188f2010-11-21 01:53:58 +000049 def visit_bitmask(self, bitmask, lvalue, rvalue):
50 self.visit(bitmask.type, lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000051
José Fonsecac9edb832010-11-20 09:03:10 +000052 def visit_array(self, array, lvalue, rvalue):
José Fonseca8badad02010-11-21 02:34:13 +000053 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
54 print ' if (__a%s) {' % (array.id)
55 print ' %s = new %s[%s];' % (lvalue, array.type, array.length)
José Fonsecac9edb832010-11-20 09:03:10 +000056 index = '__i' + array.id
José Fonseca8badad02010-11-21 02:34:13 +000057 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = array.length)
58 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
59 print ' }'
60 print ' } else {'
61 print ' %s = NULL;' % lvalue
José Fonsecac9edb832010-11-20 09:03:10 +000062 print ' }'
63
José Fonsecab11188f2010-11-21 01:53:58 +000064 def visit_blob(self, blob, lvalue, rvalue):
65 print ' %s = (%s)(void *)%s;' % (lvalue, blob, rvalue)
José Fonseca885f2652010-11-20 11:22:25 +000066
José Fonseca501f2862010-11-19 20:41:18 +000067
68
69def retrace_function(function):
70 print 'static void retrace_%s(Trace::Call &call) {' % function.name
71 if not function.name.startswith('glX'):
72 success = True
73 for arg_type, arg_name in function.args:
José Fonsecac9edb832010-11-20 09:03:10 +000074 arg_type = ConstRemover().visit(arg_type)
José Fonseca501f2862010-11-19 20:41:18 +000075 print ' %s %s;' % (arg_type, arg_name)
José Fonseca501f2862010-11-19 20:41:18 +000076 rvalue = 'call.arg("%s")' % (arg_name,)
77 lvalue = arg_name
78 try:
79 ValueExtractor().visit(arg_type, lvalue, rvalue)
80 except NotImplementedError:
81 success = False
82 print ' %s = 0; // FIXME' % arg_name
83 if not success:
84 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
85 print ' return;'
86 arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
87 print ' %s(%s);' % (function.name, arg_names)
88 print '}'
89 print
José Fonseca7e329022010-11-19 17:05:18 +000090
91
92if __name__ == '__main__':
93 print
94 print '#include <stdlib.h>'
95 print '#include <string.h>'
96 print '#include <GL/glew.h>'
97 print '#include <GL/glut.h>'
98 print
99 print '#include "trace_parser.hpp"'
100 print
101
José Fonseca501f2862010-11-19 20:41:18 +0000102 for function in libgl.functions:
103 retrace_function(function)
José Fonseca7e329022010-11-19 17:05:18 +0000104
105 print 'static bool retrace_call(Trace::Call &call) {'
José Fonseca501f2862010-11-19 20:41:18 +0000106 for function in libgl.functions:
José Fonseca7e329022010-11-19 17:05:18 +0000107 print ' if (call.name == "%s") {' % function.name
108 print ' retrace_%s(call);' % function.name
109 print ' return true;'
110 print ' }'
José Fonseca501f2862010-11-19 20:41:18 +0000111 print ' std::cerr << "warning: unsupported call " << call.name << "\\n";'
José Fonseca7e329022010-11-19 17:05:18 +0000112 print ' return false;'
113 print '}'
114 print '''
115
116class Retracer : public Trace::Parser
117{
118 void handle_call(Trace::Call &call) {
119 std::cout << call;
120 std::cout.flush();
121 retrace_call(call);
122 }
123};
124
125int main(int argc, char **argv)
126{
127 glutInit(&argc, argv);
128 glutInitWindowPosition( 0, 0 );
129 glutInitWindowSize( 800, 600 );
130 glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
131 glutCreateWindow(argv[0]);
132 glewInit();
133 for (int i = 1; i < argc; ++i) {
134 Retracer p;
135 p.parse(argv[i]);
136 glutMainLoop();
137 }
138 return 0;
139}
140
141'''