blob: 8f65b81a825a70d5c5cb918c3d6c61c6dd801321 [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));
79 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
80
José Fonsecab11188f2010-11-21 01:53:58 +000081 def visit_blob(self, blob, lvalue, rvalue):
José Fonseca2defc982010-11-22 16:59:10 +000082 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
83
84 def visit_string(self, string, lvalue, rvalue):
José Fonsecae6a50bd2010-11-24 10:12:22 +000085 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
José Fonseca885f2652010-11-20 11:22:25 +000086
José Fonseca501f2862010-11-19 20:41:18 +000087
88
José Fonseca50d78d82010-11-23 22:13:14 +000089class ValueWrapper(base.Visitor):
90
91 def visit_literal(self, literal, lvalue, rvalue):
92 pass
93
94 def visit_alias(self, alias, lvalue, rvalue):
95 self.visit(alias.type, lvalue, rvalue)
96
97 def visit_enum(self, enum, lvalue, rvalue):
98 pass
99
100 def visit_bitmask(self, bitmask, lvalue, rvalue):
101 pass
102
103 def visit_array(self, array, lvalue, rvalue):
104 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
105 print ' if (__a%s) {' % (array.id)
106 length = '__a%s->values.size()' % array.id
107 index = '__i' + array.id
108 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
109 try:
110 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
111 finally:
112 print ' }'
113 print ' }'
114
115 def visit_pointer(self, pointer, lvalue, rvalue):
116 # FIXME
117 raise NotImplementedError
118
119 def visit_handle(self, handle, lvalue, rvalue):
120 print " __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
121 print ' std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
122
123 def visit_blob(self, blob, lvalue, rvalue):
124 pass
125
126 def visit_string(self, string, lvalue, rvalue):
127 pass
128
129
130
José Fonseca501f2862010-11-19 20:41:18 +0000131def retrace_function(function):
132 print 'static void retrace_%s(Trace::Call &call) {' % function.name
José Fonsecaee855d92010-11-22 17:14:47 +0000133 success = True
134 for arg in function.args:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000135 arg_type = ConstRemover().visit(arg.type)
136 print ' // %s -> %s' % (arg.type, arg_type)
137 print ' %s %s;' % (arg_type, arg.name)
José Fonsecaee855d92010-11-22 17:14:47 +0000138 rvalue = 'call.arg("%s")' % (arg.name,)
139 lvalue = arg.name
140 try:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000141 ValueExtractor().visit(arg_type, lvalue, rvalue)
José Fonsecaee855d92010-11-22 17:14:47 +0000142 except NotImplementedError:
143 success = False
144 print ' %s = 0; // FIXME' % arg.name
145 if not success:
146 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
147 print ' return;'
148 arg_names = ", ".join([arg.name for arg in function.args])
José Fonsecae6a50bd2010-11-24 10:12:22 +0000149 if function.type is not base.Void:
150 print ' %s __result;' % (function.type)
151 print ' __result = %s(%s);' % (function.name, arg_names)
152 else:
153 print ' %s(%s);' % (function.name, arg_names)
José Fonseca50d78d82010-11-23 22:13:14 +0000154 for arg in function.args:
155 if arg.output:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000156 arg_type = ConstRemover().visit(arg.type)
José Fonseca50d78d82010-11-23 22:13:14 +0000157 rvalue = 'call.arg("%s")' % (arg.name,)
158 lvalue = arg.name
159 try:
José Fonsecaf12c6bc2010-11-24 11:03:17 +0000160 ValueWrapper().visit(arg_type, lvalue, rvalue)
José Fonseca50d78d82010-11-23 22:13:14 +0000161 except NotImplementedError:
162 print ' // FIXME: %s' % arg.name
José Fonsecae6a50bd2010-11-24 10:12:22 +0000163 if function.type is not base.Void:
164 rvalue = '*call.ret'
165 lvalue = '__result'
166 try:
167 ValueWrapper().visit(function.type, lvalue, rvalue)
168 except NotImplementedError:
169 print ' // FIXME: result'
José Fonseca501f2862010-11-19 20:41:18 +0000170 print '}'
171 print
José Fonseca7e329022010-11-19 17:05:18 +0000172
173
José Fonseca2defc982010-11-22 16:59:10 +0000174def retrace_functions(functions):
175 for function in functions:
José Fonsecaee855d92010-11-22 17:14:47 +0000176 if function.sideeffects:
177 retrace_function(function)
José Fonseca2defc982010-11-22 16:59:10 +0000178
179 print 'static bool retrace_call(Trace::Call &call) {'
180 for function in functions:
José Fonsecaee855d92010-11-22 17:14:47 +0000181 if not function.sideeffects:
182 print ' if (call.name == "%s") {' % function.name
183 print ' return true;'
184 print ' }'
185 print
186 print ' std::cout << call;'
187 print ' std::cout.flush();'
188 print
189 for function in functions:
190 if function.sideeffects:
191 print ' if (call.name == "%s") {' % function.name
192 print ' retrace_%s(call);' % function.name
193 print ' return true;'
194 print ' }'
José Fonseca2defc982010-11-22 16:59:10 +0000195 print ' std::cerr << "warning: unsupported call " << call.name << "\\n";'
196 print ' return false;'
197 print '}'
198 print
199
200
José Fonsecae6a50bd2010-11-24 10:12:22 +0000201def retrace_api(api):
202 types = api.all_types()
203
204 handles = [type for type in types if isinstance(type, base.Handle)]
205 for handle in handles:
206 print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
207 print
208
209 retrace_functions(api.functions)
210
211
José Fonseca7e329022010-11-19 17:05:18 +0000212if __name__ == '__main__':
213 print
214 print '#include <stdlib.h>'
215 print '#include <string.h>'
216 print '#include <GL/glew.h>'
217 print '#include <GL/glut.h>'
218 print
219 print '#include "trace_parser.hpp"'
220 print
José Fonsecae6a50bd2010-11-24 10:12:22 +0000221 retrace_api(glapi.glapi)
José Fonseca7e329022010-11-19 17:05:18 +0000222 print '''
223
José Fonseca6f51d3b2010-11-22 19:56:19 +0000224Trace::Parser parser;
225
José Fonseca082051b2010-11-23 12:00:31 +0000226static bool insideGlBeginEnd;
227
José Fonseca6f51d3b2010-11-22 19:56:19 +0000228static void display(void) {
229 Trace::Call *call;
230
231 while ((call = parser.parse_call())) {
José Fonseca082051b2010-11-23 12:00:31 +0000232 if (call->name == "glFlush" ||
José Fonseca6f51d3b2010-11-22 19:56:19 +0000233 call->name == "glXSwapBuffers" ||
234 call->name == "wglSwapBuffers") {
235 glFlush();
236 return;
237 }
238
239 retrace_call(*call);
240
José Fonseca082051b2010-11-23 12:00:31 +0000241 if (call->name == "glBegin") {
242 insideGlBeginEnd = true;
243 }
244
245 if (call->name == "glEnd") {
246 insideGlBeginEnd = false;
247 }
248
249 if (!insideGlBeginEnd) {
250 GLenum error = glGetError();
251 if (error != GL_NO_ERROR) {
252 std::cerr << "warning: glGetError() = ";
253 switch (error) {
254 case GL_INVALID_ENUM:
255 std::cerr << "GL_INVALID_ENUM";
256 break;
257 case GL_INVALID_VALUE:
258 std::cerr << "GL_INVALID_VALUE";
259 break;
260 case GL_INVALID_OPERATION:
261 std::cerr << "GL_INVALID_OPERATION";
262 break;
263 case GL_STACK_OVERFLOW:
264 std::cerr << "GL_STACK_OVERFLOW";
265 break;
266 case GL_STACK_UNDERFLOW:
267 std::cerr << "GL_STACK_UNDERFLOW";
268 break;
269 case GL_OUT_OF_MEMORY:
270 std::cerr << "GL_OUT_OF_MEMORY";
271 break;
José Fonsecab2e03d82010-11-23 21:35:41 +0000272 case GL_INVALID_FRAMEBUFFER_OPERATION:
273 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
274 break;
José Fonseca082051b2010-11-23 12:00:31 +0000275 case GL_TABLE_TOO_LARGE:
276 std::cerr << "GL_TABLE_TOO_LARGE";
277 break;
278 default:
279 std::cerr << error;
280 break;
281 }
282 std::cerr << "\\n";
José Fonseca6f51d3b2010-11-22 19:56:19 +0000283 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000284 }
285 }
286
287 glFlush();
288 glutIdleFunc(NULL);
289}
290
291static void idle(void) {
292 glutPostRedisplay();
293}
José Fonseca7e329022010-11-19 17:05:18 +0000294
295int main(int argc, char **argv)
296{
297 glutInit(&argc, argv);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000298 glutInitWindowPosition(0, 0);
299 glutInitWindowSize(800, 600);
300 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
José Fonseca7e329022010-11-19 17:05:18 +0000301 glutCreateWindow(argv[0]);
302 glewInit();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000303
304 glutDisplayFunc(&display);
305 glutIdleFunc(&idle);
306
José Fonseca7e329022010-11-19 17:05:18 +0000307 for (int i = 1; i < argc; ++i) {
José Fonseca6f51d3b2010-11-22 19:56:19 +0000308 if (parser.open(argv[i])) {
309 glutMainLoop();
310 parser.close();
311 }
José Fonseca7e329022010-11-19 17:05:18 +0000312 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000313
José Fonseca7e329022010-11-19 17:05:18 +0000314 return 0;
315}
316
317'''