blob: d8a38610c40348f9e54e6ca083f0ffa32804e897 [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
40 def visit_literal(self, type, lvalue, rvalue):
José Fonsecac9edb832010-11-20 09:03:10 +000041 print ' %s = %s;' % (lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000042
43 def visit_alias(self, type, lvalue, rvalue):
44 self.visit(type.type, lvalue, rvalue)
45
46 def visit_enum(self, type, lvalue, rvalue):
José Fonsecac9edb832010-11-20 09:03:10 +000047 print ' %s = %s;' % (lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000048
49 def visit_bitmask(self, type, lvalue, rvalue):
50 self.visit(type.type, lvalue, rvalue)
51
José Fonsecac9edb832010-11-20 09:03:10 +000052 def visit_array(self, array, lvalue, rvalue):
53 print ' %s = new %s[%s];' % (lvalue, array.type, array.length)
54 index = '__i' + array.id
55 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = array.length)
56 self.visit(array.type, '%s[%s]' % (lvalue, index), '%s[%s]' % (rvalue, index))
57 print ' }'
58
José Fonseca885f2652010-11-20 11:22:25 +000059 def visit_blob(self, type, lvalue, rvalue):
60 print ' %s = %s;' % (lvalue, rvalue)
61
José Fonseca501f2862010-11-19 20:41:18 +000062
63
64def retrace_function(function):
65 print 'static void retrace_%s(Trace::Call &call) {' % function.name
66 if not function.name.startswith('glX'):
67 success = True
68 for arg_type, arg_name in function.args:
José Fonsecac9edb832010-11-20 09:03:10 +000069 arg_type = ConstRemover().visit(arg_type)
José Fonseca501f2862010-11-19 20:41:18 +000070 print ' %s %s;' % (arg_type, arg_name)
José Fonseca501f2862010-11-19 20:41:18 +000071 rvalue = 'call.arg("%s")' % (arg_name,)
72 lvalue = arg_name
73 try:
74 ValueExtractor().visit(arg_type, lvalue, rvalue)
75 except NotImplementedError:
76 success = False
77 print ' %s = 0; // FIXME' % arg_name
78 if not success:
79 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
80 print ' return;'
81 arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
82 print ' %s(%s);' % (function.name, arg_names)
83 print '}'
84 print
José Fonseca7e329022010-11-19 17:05:18 +000085
86
87if __name__ == '__main__':
88 print
89 print '#include <stdlib.h>'
90 print '#include <string.h>'
91 print '#include <GL/glew.h>'
92 print '#include <GL/glut.h>'
93 print
94 print '#include "trace_parser.hpp"'
95 print
96
José Fonseca501f2862010-11-19 20:41:18 +000097 for function in libgl.functions:
98 retrace_function(function)
José Fonseca7e329022010-11-19 17:05:18 +000099
100 print 'static bool retrace_call(Trace::Call &call) {'
José Fonseca501f2862010-11-19 20:41:18 +0000101 for function in libgl.functions:
José Fonseca7e329022010-11-19 17:05:18 +0000102 print ' if (call.name == "%s") {' % function.name
103 print ' retrace_%s(call);' % function.name
104 print ' return true;'
105 print ' }'
José Fonseca501f2862010-11-19 20:41:18 +0000106 print ' std::cerr << "warning: unsupported call " << call.name << "\\n";'
José Fonseca7e329022010-11-19 17:05:18 +0000107 print ' return false;'
108 print '}'
109 print '''
110
111class Retracer : public Trace::Parser
112{
113 void handle_call(Trace::Call &call) {
114 std::cout << call;
115 std::cout.flush();
116 retrace_call(call);
117 }
118};
119
120int main(int argc, char **argv)
121{
122 glutInit(&argc, argv);
123 glutInitWindowPosition( 0, 0 );
124 glutInitWindowSize( 800, 600 );
125 glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
126 glutCreateWindow(argv[0]);
127 glewInit();
128 for (int i = 1; i < argc; ++i) {
129 Retracer p;
130 p.parse(argv[i]);
131 glutMainLoop();
132 }
133 return 0;
134}
135
136'''