1 | # Copyright (c) 2001, Stanford University
|
---|
2 | # All rights reserved.
|
---|
3 | #
|
---|
4 | # See the file LICENSE.txt for information on redistributing this software.
|
---|
5 |
|
---|
6 | import sys
|
---|
7 |
|
---|
8 | import apiutil
|
---|
9 |
|
---|
10 |
|
---|
11 | apiutil.CopyrightC()
|
---|
12 |
|
---|
13 | print """/* DO NOT EDIT! THIS CODE IS AUTOGENERATED BY unpack.py */
|
---|
14 |
|
---|
15 | #include "unpacker.h"
|
---|
16 | #include "cr_opcodes.h"
|
---|
17 | #include "cr_error.h"
|
---|
18 | #include "cr_mem.h"
|
---|
19 | #include "cr_spu.h"
|
---|
20 | #include "unpack_extend.h"
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <memory.h>
|
---|
23 |
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 |
|
---|
26 | DECLEXPORT(const unsigned char *) cr_unpackData = NULL;
|
---|
27 | SPUDispatchTable cr_unpackDispatch;
|
---|
28 |
|
---|
29 | static void crUnpackExtend(void);
|
---|
30 | """
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | #
|
---|
35 | # Useful functions
|
---|
36 | #
|
---|
37 |
|
---|
38 | def ReadData( offset, arg_type ):
|
---|
39 | """Emit a READ_DOUBLE or READ_DATA call for pulling a GL function
|
---|
40 | argument out of the buffer's operand area."""
|
---|
41 | if arg_type == "GLdouble" or arg_type == "GLclampd":
|
---|
42 | retval = "READ_DOUBLE( %d )" % offset
|
---|
43 | else:
|
---|
44 | retval = "READ_DATA( %d, %s )" % (offset, arg_type)
|
---|
45 | return retval
|
---|
46 |
|
---|
47 |
|
---|
48 | def FindReturnPointer( return_type, params ):
|
---|
49 | """For GL functions that return values (either as the return value or
|
---|
50 | through a pointer parameter) emit a SET_RETURN_PTR call."""
|
---|
51 | arg_len = apiutil.PacketLength( params )
|
---|
52 | if (return_type != 'void'):
|
---|
53 | print '\tSET_RETURN_PTR( %d );' % (arg_len + 8) # extended opcode plus packet length
|
---|
54 | else:
|
---|
55 | paramList = [ ('foo', 'void *', 0) ]
|
---|
56 | print '\tSET_RETURN_PTR( %d );' % (arg_len + 8 - apiutil.PacketLength(paramList))
|
---|
57 |
|
---|
58 |
|
---|
59 | def FindWritebackPointer( return_type, params ):
|
---|
60 | """Emit a SET_WRITEBACK_PTR call."""
|
---|
61 | arg_len = apiutil.PacketLength( params )
|
---|
62 | if return_type != 'void':
|
---|
63 | paramList = [ ('foo', 'void *', 0) ]
|
---|
64 | arg_len += apiutil.PacketLength( paramList )
|
---|
65 |
|
---|
66 | print '\tSET_WRITEBACK_PTR( %d );' % (arg_len + 8) # extended opcode plus packet length
|
---|
67 |
|
---|
68 |
|
---|
69 | def MakeNormalCall( return_type, func_name, params, counter_init = 0 ):
|
---|
70 | counter = counter_init
|
---|
71 | copy_of_params = params[:]
|
---|
72 |
|
---|
73 | for i in range( 0, len(params) ):
|
---|
74 | (name, type, vecSize) = params[i]
|
---|
75 | if apiutil.IsPointer(copy_of_params[i][1]):
|
---|
76 | params[i] = ('NULL', type, vecSize)
|
---|
77 | copy_of_params[i] = (copy_of_params[i][0], 'void', 0)
|
---|
78 | if not "get" in apiutil.Properties(func_name):
|
---|
79 | print '\tcrError( "%s needs to be special cased!" );' % func_name
|
---|
80 | else:
|
---|
81 | print "\t%s %s = %s;" % ( copy_of_params[i][1], name, ReadData( counter, copy_of_params[i][1] ) )
|
---|
82 | counter += apiutil.sizeof(copy_of_params[i][1])
|
---|
83 |
|
---|
84 | if ("get" in apiutil.Properties(func_name)):
|
---|
85 | FindReturnPointer( return_type, params )
|
---|
86 | FindWritebackPointer( return_type, params )
|
---|
87 |
|
---|
88 | if return_type != "void":
|
---|
89 | print "\t(void)",
|
---|
90 | else:
|
---|
91 | print "\t",
|
---|
92 | print "cr_unpackDispatch.%s( %s );" % (func_name, apiutil.MakeCallString(params))
|
---|
93 |
|
---|
94 |
|
---|
95 | def MakeVectorCall( return_type, func_name, arg_type ):
|
---|
96 | """Convert a call like glVertex3f to glVertex3fv."""
|
---|
97 | vec_func = apiutil.VectorFunction(func_name)
|
---|
98 | params = apiutil.Parameters(vec_func)
|
---|
99 | assert len(params) == 1
|
---|
100 | (arg_name, vecType, vecSize) = params[0]
|
---|
101 |
|
---|
102 | if arg_type == "GLdouble" or arg_type == "GLclampd":
|
---|
103 | print "#ifdef CR_UNALIGNED_ACCESS_OKAY"
|
---|
104 | print "\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType)
|
---|
105 | print "#else"
|
---|
106 | for index in range(0, vecSize):
|
---|
107 | print "\tGLdouble v" + `index` + " = READ_DOUBLE(", `index * 8`, ");"
|
---|
108 | if return_type != "void":
|
---|
109 | print "\t(void) cr_unpackDispatch.%s(" % func_name,
|
---|
110 | else:
|
---|
111 | print "\tcr_unpackDispatch.%s(" % func_name,
|
---|
112 | for index in range(0, vecSize):
|
---|
113 | print "v" + `index`,
|
---|
114 | if index != vecSize - 1:
|
---|
115 | print ",",
|
---|
116 | print ");"
|
---|
117 | print "#endif"
|
---|
118 | else:
|
---|
119 | print "\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType)
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 | keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")
|
---|
124 |
|
---|
125 |
|
---|
126 | #
|
---|
127 | # Generate unpack functions for all the simple functions.
|
---|
128 | #
|
---|
129 | for func_name in keys:
|
---|
130 | if (not "pack" in apiutil.ChromiumProps(func_name) or
|
---|
131 | apiutil.FindSpecial( "unpacker", func_name )):
|
---|
132 | continue
|
---|
133 |
|
---|
134 | params = apiutil.Parameters(func_name)
|
---|
135 | return_type = apiutil.ReturnType(func_name)
|
---|
136 |
|
---|
137 | print "static void crUnpack%s(void)" % func_name
|
---|
138 | print "{"
|
---|
139 |
|
---|
140 | vector_func = apiutil.VectorFunction(func_name)
|
---|
141 | if (vector_func and len(apiutil.Parameters(vector_func)) == 1):
|
---|
142 | MakeVectorCall( return_type, func_name, params[0][1] )
|
---|
143 | else:
|
---|
144 | MakeNormalCall( return_type, func_name, params )
|
---|
145 | packet_length = apiutil.PacketLength( params )
|
---|
146 | if packet_length == 0:
|
---|
147 | print "\tINCR_DATA_PTR_NO_ARGS( );"
|
---|
148 | else:
|
---|
149 | print "\tINCR_DATA_PTR( %d );" % packet_length
|
---|
150 | print "}\n"
|
---|
151 |
|
---|
152 |
|
---|
153 | #
|
---|
154 | # Emit some code
|
---|
155 | #
|
---|
156 | print """
|
---|
157 | typedef struct __dispatchNode {
|
---|
158 | const unsigned char *unpackData;
|
---|
159 | struct __dispatchNode *next;
|
---|
160 | } DispatchNode;
|
---|
161 |
|
---|
162 | static DispatchNode *unpackStack = NULL;
|
---|
163 |
|
---|
164 | static SPUDispatchTable *cr_lastDispatch = NULL;
|
---|
165 |
|
---|
166 | void crUnpackPush(void)
|
---|
167 | {
|
---|
168 | DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
|
---|
169 | node->next = unpackStack;
|
---|
170 | unpackStack = node;
|
---|
171 | node->unpackData = cr_unpackData;
|
---|
172 | }
|
---|
173 |
|
---|
174 | void crUnpackPop(void)
|
---|
175 | {
|
---|
176 | DispatchNode *node = unpackStack;
|
---|
177 |
|
---|
178 | if (!node)
|
---|
179 | {
|
---|
180 | crError( "crUnpackPop called with an empty stack!" );
|
---|
181 | }
|
---|
182 | unpackStack = node->next;
|
---|
183 | cr_unpackData = node->unpackData;
|
---|
184 | crFree( node );
|
---|
185 | }
|
---|
186 |
|
---|
187 | void crUnpack( const void *data, const void *opcodes,
|
---|
188 | unsigned int num_opcodes, SPUDispatchTable *table )
|
---|
189 | {
|
---|
190 | unsigned int i;
|
---|
191 | const unsigned char *unpack_opcodes;
|
---|
192 | if (table != cr_lastDispatch)
|
---|
193 | {
|
---|
194 | crSPUCopyDispatchTable( &cr_unpackDispatch, table );
|
---|
195 | cr_lastDispatch = table;
|
---|
196 | }
|
---|
197 |
|
---|
198 | unpack_opcodes = (const unsigned char *)opcodes;
|
---|
199 | cr_unpackData = (const unsigned char *)data;
|
---|
200 |
|
---|
201 | for (i = 0 ; i < num_opcodes ; i++)
|
---|
202 | {
|
---|
203 | /*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
|
---|
204 | switch( *unpack_opcodes )
|
---|
205 | {"""
|
---|
206 |
|
---|
207 | #
|
---|
208 | # Emit switch cases for all unextended opcodes
|
---|
209 | #
|
---|
210 | for func_name in keys:
|
---|
211 | if "pack" in apiutil.ChromiumProps(func_name):
|
---|
212 | print '\t\t\tcase %s:' % apiutil.OpcodeName( func_name )
|
---|
213 | # print '\t\t\t\tcrDebug("Unpack: %s");' % apiutil.OpcodeName( func_name )
|
---|
214 | print '\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name
|
---|
215 |
|
---|
216 | print """
|
---|
217 | case CR_EXTEND_OPCODE: crUnpackExtend(); break;
|
---|
218 | default:
|
---|
219 | crError( "Unknown opcode: %d", *unpack_opcodes );
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | unpack_opcodes--;
|
---|
223 | }
|
---|
224 | }"""
|
---|
225 |
|
---|
226 |
|
---|
227 | #
|
---|
228 | # Emit unpack functions for extended opcodes, non-special functions only.
|
---|
229 | #
|
---|
230 | for func_name in keys:
|
---|
231 | if ("extpack" in apiutil.ChromiumProps(func_name)
|
---|
232 | and not apiutil.FindSpecial("unpacker", func_name)):
|
---|
233 | return_type = apiutil.ReturnType(func_name)
|
---|
234 | params = apiutil.Parameters(func_name)
|
---|
235 | print 'static void crUnpackExtend%s(void)' % func_name
|
---|
236 | print '{'
|
---|
237 | MakeNormalCall( return_type, func_name, params, 8 )
|
---|
238 | print '}\n'
|
---|
239 |
|
---|
240 | print 'static void crUnpackExtend(void)'
|
---|
241 | print '{'
|
---|
242 | print '\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' );
|
---|
243 | print ''
|
---|
244 | print '\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/'
|
---|
245 | print '\tswitch( extend_opcode )'
|
---|
246 | print '\t{'
|
---|
247 |
|
---|
248 |
|
---|
249 | #
|
---|
250 | # Emit switch statement for extended opcodes
|
---|
251 | #
|
---|
252 | for func_name in keys:
|
---|
253 | if "extpack" in apiutil.ChromiumProps(func_name):
|
---|
254 | print '\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name )
|
---|
255 | # print '\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )
|
---|
256 | print '\t\t\tcrUnpackExtend%s( );' % func_name
|
---|
257 | print '\t\t\tbreak;'
|
---|
258 |
|
---|
259 | print """ default:
|
---|
260 | crError( "Unknown extended opcode: %d", (int) extend_opcode );
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | INCR_VAR_PTR();
|
---|
264 | }"""
|
---|