blob: 4b7ad7acdaa2205fc2afae474ed056b2f64cd66b [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
José Fonseca8fbdd3a2010-11-23 20:55:07 +000028import glapi
José Fonseca7e329022010-11-19 17:05:18 +000029
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
José Fonsecaf6592d72010-11-21 12:44:41 +000037 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é Fonsecac9edb832010-11-20 09:03:10 +000043
José Fonseca501f2862010-11-19 20:41:18 +000044class ValueExtractor(base.Visitor):
45
José Fonsecab11188f2010-11-21 01:53:58 +000046 def visit_literal(self, literal, 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_alias(self, alias, lvalue, rvalue):
50 self.visit(alias.type, lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000051
José Fonsecab11188f2010-11-21 01:53:58 +000052 def visit_enum(self, enum, lvalue, rvalue):
José Fonsecac9edb832010-11-20 09:03:10 +000053 print ' %s = %s;' % (lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000054
José Fonsecab11188f2010-11-21 01:53:58 +000055 def visit_bitmask(self, bitmask, lvalue, rvalue):
56 self.visit(bitmask.type, lvalue, rvalue)
José Fonseca501f2862010-11-19 20:41:18 +000057
José Fonsecac9edb832010-11-20 09:03:10 +000058 def visit_array(self, array, lvalue, rvalue):
José Fonseca8badad02010-11-21 02:34:13 +000059 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
60 print ' if (__a%s) {' % (array.id)
José Fonseca2defc982010-11-22 16:59:10 +000061 length = '__a%s->values.size()' % array.id
62 print ' %s = new %s[%s];' % (lvalue, array.type, length)
José Fonsecac9edb832010-11-20 09:03:10 +000063 index = '__i' + array.id
José Fonseca2defc982010-11-22 16:59:10 +000064 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é Fonseca50d78d82010-11-23 22:13:14 +000072
73 def visit_pointer(self, pointer, lvalue, rvalue):
74 # FIXME
75 raise NotImplementedError
José Fonsecac9edb832010-11-20 09:03:10 +000076
José Fonseca50d78d82010-11-23 22:13:14 +000077 def visit_handle(self, handle, lvalue, rvalue):
78 self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
José Fonseca094cb2d2010-11-24 15:55:03 +000079 print ' if (verbosity >= 2)'
80 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
José Fonseca50d78d82010-11-23 22:13:14 +000081
José Fonsecab11188f2010-11-21 01:53:58 +000082 def visit_blob(self, blob, lvalue, rvalue):
José Fonseca2defc982010-11-22 16:59:10 +000083 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
84
85 def visit_string(self, string, lvalue, rvalue):
José Fonsecae6a50bd2010-11-24 10:12:22 +000086 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
José Fonseca885f2652010-11-20 11:22:25 +000087
José Fonseca501f2862010-11-19 20:41:18 +000088
89
José Fonseca50d78d82010-11-23 22:13:14 +000090class ValueWrapper(base.Visitor):
91
92 def visit_literal(self, literal, lvalue, rvalue):
93 pass
94
95 def visit_alias(self, alias, lvalue, rvalue):
96 self.visit(alias.type, lvalue, rvalue)
97
98 def visit_enum(self, enum, lvalue, rvalue):
99 pass
100
101 def visit_bitmask(self, bitmask, lvalue, rvalue):
102 pass
103
104 def visit_array(self, array, lvalue, rvalue):
105 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
106 print ' if (__a%s) {' % (array.id)
107 length = '__a%s->values.size()' % array.id
108 index = '__i' + array.id
109 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
110 try:
111 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
112 finally:
113 print ' }'
114 print ' }'
115
116 def visit_pointer(self, pointer, lvalue, rvalue):
117 # FIXME
118 raise NotImplementedError
119
120 def visit_handle(self, handle, lvalue, rvalue):
121 print " __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
José Fonseca094cb2d2010-11-24 15:55:03 +0000122 print ' if (verbosity >= 2)'
123 print ' std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
José Fonseca50d78d82010-11-23 22:13:14 +0000124
125 def visit_blob(self, blob, lvalue, rvalue):
126 pass
127
128 def visit_string(self, string, lvalue, rvalue):
129 pass
130
131
132
José Fonseca501f2862010-11-19 20:41:18 +0000133def retrace_function(function):
134 print 'static void retrace_%s(Trace::Call &call) {' % function.name
José Fonsecaee855d92010-11-22 17:14:47 +0000135 success = True
136 for arg in function.args:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000137 arg_type = ConstRemover().visit(arg.type)
138 print ' // %s -> %s' % (arg.type, arg_type)
139 print ' %s %s;' % (arg_type, arg.name)
José Fonsecaee855d92010-11-22 17:14:47 +0000140 rvalue = 'call.arg("%s")' % (arg.name,)
141 lvalue = arg.name
142 try:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000143 ValueExtractor().visit(arg_type, lvalue, rvalue)
José Fonsecaee855d92010-11-22 17:14:47 +0000144 except NotImplementedError:
145 success = False
146 print ' %s = 0; // FIXME' % arg.name
147 if not success:
148 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
149 print ' return;'
150 arg_names = ", ".join([arg.name for arg in function.args])
José Fonsecae6a50bd2010-11-24 10:12:22 +0000151 if function.type is not base.Void:
152 print ' %s __result;' % (function.type)
153 print ' __result = %s(%s);' % (function.name, arg_names)
154 else:
155 print ' %s(%s);' % (function.name, arg_names)
José Fonseca50d78d82010-11-23 22:13:14 +0000156 for arg in function.args:
157 if arg.output:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000158 arg_type = ConstRemover().visit(arg.type)
José Fonseca50d78d82010-11-23 22:13:14 +0000159 rvalue = 'call.arg("%s")' % (arg.name,)
160 lvalue = arg.name
161 try:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000162 ValueWrapper().visit(arg_type, lvalue, rvalue)
José Fonseca50d78d82010-11-23 22:13:14 +0000163 except NotImplementedError:
164 print ' // FIXME: %s' % arg.name
José Fonsecae6a50bd2010-11-24 10:12:22 +0000165 if function.type is not base.Void:
166 rvalue = '*call.ret'
167 lvalue = '__result'
168 try:
169 ValueWrapper().visit(function.type, lvalue, rvalue)
170 except NotImplementedError:
171 print ' // FIXME: result'
José Fonseca501f2862010-11-19 20:41:18 +0000172 print '}'
173 print
José Fonseca7e329022010-11-19 17:05:18 +0000174
175
José Fonseca2defc982010-11-22 16:59:10 +0000176def retrace_functions(functions):
177 for function in functions:
José Fonsecaee855d92010-11-22 17:14:47 +0000178 if function.sideeffects:
179 retrace_function(function)
José Fonseca2defc982010-11-22 16:59:10 +0000180
181 print 'static bool retrace_call(Trace::Call &call) {'
182 for function in functions:
José Fonsecaee855d92010-11-22 17:14:47 +0000183 if not function.sideeffects:
184 print ' if (call.name == "%s") {' % function.name
185 print ' return true;'
186 print ' }'
187 print
José Fonseca094cb2d2010-11-24 15:55:03 +0000188 print ' if (verbosity >=1 ) {'
189 print ' std::cout << call;'
190 print ' std::cout.flush();'
191 print ' };'
José Fonsecaee855d92010-11-22 17:14:47 +0000192 print
193 for function in functions:
194 if function.sideeffects:
195 print ' if (call.name == "%s") {' % function.name
196 print ' retrace_%s(call);' % function.name
197 print ' return true;'
198 print ' }'
José Fonseca094cb2d2010-11-24 15:55:03 +0000199 print ' std::cerr << "warning: unknown call " << call.name << "\\n";'
José Fonseca2defc982010-11-22 16:59:10 +0000200 print ' return false;'
201 print '}'
202 print
203
204
José Fonsecae6a50bd2010-11-24 10:12:22 +0000205def retrace_api(api):
206 types = api.all_types()
207
208 handles = [type for type in types if isinstance(type, base.Handle)]
209 for handle in handles:
210 print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
211 print
212
213 retrace_functions(api.functions)
214
215
José Fonseca7e329022010-11-19 17:05:18 +0000216if __name__ == '__main__':
217 print
218 print '#include <stdlib.h>'
219 print '#include <string.h>'
José Fonseca094cb2d2010-11-24 15:55:03 +0000220 print
221 print '#ifdef WIN32'
222 print '#include <windows.h>'
223 print '#endif'
224 print
José Fonseca7e329022010-11-19 17:05:18 +0000225 print '#include <GL/glew.h>'
226 print '#include <GL/glut.h>'
227 print
228 print '#include "trace_parser.hpp"'
229 print
José Fonseca094cb2d2010-11-24 15:55:03 +0000230 print 'unsigned verbosity = 0;'
231 print
José Fonsecae6a50bd2010-11-24 10:12:22 +0000232 retrace_api(glapi.glapi)
José Fonseca7e329022010-11-19 17:05:18 +0000233 print '''
234
José Fonseca6f51d3b2010-11-22 19:56:19 +0000235Trace::Parser parser;
236
José Fonseca082051b2010-11-23 12:00:31 +0000237static bool insideGlBeginEnd;
238
José Fonseca6f51d3b2010-11-22 19:56:19 +0000239static void display(void) {
240 Trace::Call *call;
241
242 while ((call = parser.parse_call())) {
José Fonseca082051b2010-11-23 12:00:31 +0000243 if (call->name == "glFlush" ||
José Fonseca6f51d3b2010-11-22 19:56:19 +0000244 call->name == "glXSwapBuffers" ||
245 call->name == "wglSwapBuffers") {
246 glFlush();
247 return;
248 }
249
250 retrace_call(*call);
251
José Fonseca082051b2010-11-23 12:00:31 +0000252 if (call->name == "glBegin") {
253 insideGlBeginEnd = true;
254 }
255
256 if (call->name == "glEnd") {
257 insideGlBeginEnd = false;
258 }
259
260 if (!insideGlBeginEnd) {
261 GLenum error = glGetError();
262 if (error != GL_NO_ERROR) {
263 std::cerr << "warning: glGetError() = ";
264 switch (error) {
265 case GL_INVALID_ENUM:
266 std::cerr << "GL_INVALID_ENUM";
267 break;
268 case GL_INVALID_VALUE:
269 std::cerr << "GL_INVALID_VALUE";
270 break;
271 case GL_INVALID_OPERATION:
272 std::cerr << "GL_INVALID_OPERATION";
273 break;
274 case GL_STACK_OVERFLOW:
275 std::cerr << "GL_STACK_OVERFLOW";
276 break;
277 case GL_STACK_UNDERFLOW:
278 std::cerr << "GL_STACK_UNDERFLOW";
279 break;
280 case GL_OUT_OF_MEMORY:
281 std::cerr << "GL_OUT_OF_MEMORY";
282 break;
José Fonsecab2e03d82010-11-23 21:35:41 +0000283 case GL_INVALID_FRAMEBUFFER_OPERATION:
284 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
285 break;
José Fonseca082051b2010-11-23 12:00:31 +0000286 case GL_TABLE_TOO_LARGE:
287 std::cerr << "GL_TABLE_TOO_LARGE";
288 break;
289 default:
290 std::cerr << error;
291 break;
292 }
293 std::cerr << "\\n";
José Fonseca6f51d3b2010-11-22 19:56:19 +0000294 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000295 }
296 }
297
298 glFlush();
299 glutIdleFunc(NULL);
300}
301
302static void idle(void) {
303 glutPostRedisplay();
304}
José Fonseca7e329022010-11-19 17:05:18 +0000305
306int main(int argc, char **argv)
307{
308 glutInit(&argc, argv);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000309 glutInitWindowPosition(0, 0);
310 glutInitWindowSize(800, 600);
311 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
José Fonseca7e329022010-11-19 17:05:18 +0000312 glutCreateWindow(argv[0]);
313 glewInit();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000314
315 glutDisplayFunc(&display);
316 glutIdleFunc(&idle);
317
José Fonseca094cb2d2010-11-24 15:55:03 +0000318 int i;
319 for (i = 1; i < argc; ++i) {
320 const char *arg = argv[i];
321
322 if (arg[0] != '-') {
323 break;
324 }
325
326 if (!strcmp(arg, "--")) {
327 break;
328 }
329 else if (!strcmp(arg, "-v")) {
330 ++verbosity;
331 } else {
332 std::cerr << "error: unknown option " << arg << "\\n";
333 return 1;
334 }
335 }
336
337 for ( ; i < argc; ++i) {
José Fonseca6f51d3b2010-11-22 19:56:19 +0000338 if (parser.open(argv[i])) {
339 glutMainLoop();
340 parser.close();
341 }
José Fonseca7e329022010-11-19 17:05:18 +0000342 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000343
José Fonseca7e329022010-11-19 17:05:18 +0000344 return 0;
345}
346
347'''