Mercurial > cpython-lru_cache-weakref
annotate Modules/cgen.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | 202602faefc7 |
| rev | line source |
|---|---|
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
1 ######################################################################## |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
2 # Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
3 # The Netherlands. |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
4 # |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
5 # All Rights Reserved |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
6 # |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
7 # Permission to use, copy, modify, and distribute this software and its |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
8 # documentation for any purpose and without fee is hereby granted, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
9 # provided that the above copyright notice appear in all copies and that |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
10 # both that copyright notice and this permission notice appear in |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
11 # supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
12 # Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
13 # CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
14 # distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
15 # permission. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
16 # |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
17 # While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
18 # is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
19 # (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
20 # |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
21 # STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
22 # REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
23 # MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
24 # CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
25 # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
26 # PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
27 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2807
diff
changeset
|
28 # PERFORMANCE OF THIS SOFTWARE. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
29 ######################################################################## |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1443
diff
changeset
|
30 |
| 6 | 31 # Python script to parse cstubs file for gl and generate C stubs. |
|
335
17a29760d062
Reflect changed file name in comments.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
32 # usage: python cgen.py <cstubs >glmodule.c |
| 6 | 33 # |
| 402 | 34 # NOTE: You must first make a python binary without the "GL" option |
| 35 # before you can run this, when building Python for the first time. | |
| 36 # See comments in the Makefile. | |
| 37 # | |
| 6 | 38 # XXX BUG return arrays generate wrong code |
| 39 # XXX need to change error returns into gotos to free mallocked arrays | |
| 40 | |
| 41 | |
| 42 import string | |
| 43 import sys | |
| 44 | |
| 45 | |
| 46 # Function to print to stderr | |
| 47 # | |
|
2807
5ca5d77afd28
err() should be varargs -- and fix one call
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
48 def err(*args): |
| 6 | 49 savestdout = sys.stdout |
| 50 try: | |
| 51 sys.stdout = sys.stderr | |
| 52 for i in args: | |
| 53 print i, | |
| 54 print | |
| 55 finally: | |
| 56 sys.stdout = savestdout | |
| 57 | |
| 58 | |
| 59 # The set of digits that form a number | |
| 60 # | |
| 61 digits = '0123456789' | |
| 62 | |
| 63 | |
| 64 # Function to extract a string of digits from the front of the string. | |
| 65 # Returns the leading string of digits and the remaining string. | |
| 66 # If no number is found, returns '' and the original string. | |
| 67 # | |
| 68 def getnum(s): | |
| 69 n = '' | |
| 86 | 70 while s and s[0] in digits: |
| 71 n = n + s[0] | |
| 6 | 72 s = s[1:] |
| 73 return n, s | |
| 74 | |
| 75 | |
| 76 # Function to check if a string is a number | |
| 77 # | |
| 78 def isnum(s): | |
| 79 if not s: return 0 | |
| 80 for c in s: | |
| 81 if not c in digits: return 0 | |
| 82 return 1 | |
| 83 | |
| 84 | |
| 85 # Allowed function return types | |
| 86 # | |
| 87 return_types = ['void', 'short', 'long'] | |
| 88 | |
| 89 | |
| 90 # Allowed function argument types | |
| 91 # | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
92 arg_types = ['char', 'string', 'short', 'u_short', 'float', 'long', 'double'] |
| 6 | 93 |
| 94 | |
| 95 # Need to classify arguments as follows | |
| 96 # simple input variable | |
| 97 # simple output variable | |
| 98 # input array | |
| 99 # output array | |
| 100 # input giving size of some array | |
| 101 # | |
| 102 # Array dimensions can be specified as follows | |
| 103 # constant | |
| 104 # argN | |
| 105 # constant * argN | |
| 106 # retval | |
| 107 # constant * retval | |
| 108 # | |
| 109 # The dimensions given as constants * something are really | |
| 110 # arrays of points where points are 2- 3- or 4-tuples | |
| 111 # | |
| 112 # We have to consider three lists: | |
| 113 # python input arguments | |
| 114 # C stub arguments (in & out) | |
| 115 # python output arguments (really return values) | |
| 116 # | |
| 117 # There is a mapping from python input arguments to the input arguments | |
| 118 # of the C stub, and a further mapping from C stub arguments to the | |
| 119 # python return values | |
| 120 | |
| 121 | |
| 122 # Exception raised by checkarg() and generate() | |
| 123 # | |
| 124 arg_error = 'bad arg' | |
| 125 | |
| 126 | |
| 127 # Function to check one argument. | |
| 128 # Arguments: the type and the arg "name" (really mode plus subscript). | |
| 129 # Raises arg_error if something's wrong. | |
| 130 # Return type, mode, factor, rest of subscript; factor and rest may be empty. | |
| 131 # | |
| 132 def checkarg(type, arg): | |
| 133 # | |
| 134 # Turn "char *x" into "string x". | |
| 135 # | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
136 if type == 'char' and arg[0] == '*': |
| 6 | 137 type = 'string' |
| 138 arg = arg[1:] | |
| 139 # | |
| 140 # Check that the type is supported. | |
| 141 # | |
| 142 if type not in arg_types: | |
| 143 raise arg_error, ('bad type', type) | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
144 if type[:2] == 'u_': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
145 type = 'unsigned ' + type[2:] |
| 6 | 146 # |
| 147 # Split it in the mode (first character) and the rest. | |
| 148 # | |
| 149 mode, rest = arg[:1], arg[1:] | |
| 150 # | |
| 151 # The mode must be 's' for send (= input) or 'r' for return argument. | |
| 152 # | |
| 153 if mode not in ('r', 's'): | |
| 154 raise arg_error, ('bad arg mode', mode) | |
| 155 # | |
| 156 # Is it a simple argument: if so, we are done. | |
| 157 # | |
| 158 if not rest: | |
| 159 return type, mode, '', '' | |
| 160 # | |
| 161 # Not a simple argument; must be an array. | |
| 162 # The 'rest' must be a subscript enclosed in [ and ]. | |
| 163 # The subscript must be one of the following forms, | |
| 164 # otherwise we don't handle it (where N is a number): | |
| 165 # N | |
| 166 # argN | |
| 167 # retval | |
| 168 # N*argN | |
| 169 # N*retval | |
| 170 # | |
| 171 if rest[:1] <> '[' or rest[-1:] <> ']': | |
| 172 raise arg_error, ('subscript expected', rest) | |
| 173 sub = rest[1:-1] | |
| 174 # | |
| 175 # Is there a leading number? | |
| 176 # | |
| 177 num, sub = getnum(sub) | |
| 178 if num: | |
| 179 # There is a leading number | |
| 180 if not sub: | |
| 181 # The subscript is just a number | |
| 182 return type, mode, num, '' | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
183 if sub[:1] == '*': |
| 6 | 184 # There is a factor prefix |
| 185 sub = sub[1:] | |
| 186 else: | |
| 187 raise arg_error, ('\'*\' expected', sub) | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
188 if sub == 'retval': |
| 6 | 189 # size is retval -- must be a reply argument |
| 190 if mode <> 'r': | |
| 191 raise arg_error, ('non-r mode with [retval]', mode) | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
192 elif not isnum(sub) and (sub[:3] <> 'arg' or not isnum(sub[3:])): |
| 6 | 193 raise arg_error, ('bad subscript', sub) |
| 194 # | |
| 195 return type, mode, num, sub | |
| 196 | |
| 197 | |
| 198 # List of functions for which we have generated stubs | |
| 199 # | |
| 200 functions = [] | |
| 201 | |
| 202 | |
| 203 # Generate the stub for the given function, using the database of argument | |
| 204 # information build by successive calls to checkarg() | |
| 205 # | |
| 206 def generate(type, func, database): | |
| 207 # | |
| 208 # Check that we can handle this case: | |
| 209 # no variable size reply arrays yet | |
| 210 # | |
| 211 n_in_args = 0 | |
| 212 n_out_args = 0 | |
| 213 # | |
| 214 for a_type, a_mode, a_factor, a_sub in database: | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
215 if a_mode == 's': |
| 6 | 216 n_in_args = n_in_args + 1 |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
217 elif a_mode == 'r': |
| 6 | 218 n_out_args = n_out_args + 1 |
| 219 else: | |
| 220 # Can't happen | |
| 221 raise arg_error, ('bad a_mode', a_mode) | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
222 if (a_mode == 'r' and a_sub) or a_sub == 'retval': |
|
2807
5ca5d77afd28
err() should be varargs -- and fix one call
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
223 err('Function', func, 'too complicated:', |
|
5ca5d77afd28
err() should be varargs -- and fix one call
Guido van Rossum <guido@python.org>
parents:
2061
diff
changeset
|
224 a_type, a_mode, a_factor, a_sub) |
| 6 | 225 print '/* XXX Too complicated to generate code for */' |
| 226 return | |
| 227 # | |
| 228 functions.append(func) | |
| 229 # | |
| 230 # Stub header | |
| 231 # | |
| 232 print | |
| 233 print 'static object *' | |
| 234 print 'gl_' + func + '(self, args)' | |
| 235 print '\tobject *self;' | |
| 236 print '\tobject *args;' | |
| 237 print '{' | |
| 238 # | |
| 239 # Declare return value if any | |
| 240 # | |
| 241 if type <> 'void': | |
| 242 print '\t' + type, 'retval;' | |
| 243 # | |
| 244 # Declare arguments | |
| 245 # | |
| 246 for i in range(len(database)): | |
| 247 a_type, a_mode, a_factor, a_sub = database[i] | |
| 248 print '\t' + a_type, | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
249 brac = ket = '' |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
250 if a_sub and not isnum(a_sub): |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
251 if a_factor: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
252 brac = '(' |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
253 ket = ')' |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
254 print brac + '*', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
255 print 'arg' + `i+1` + ket, |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
256 if a_sub and isnum(a_sub): |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
257 print '[', a_sub, ']', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
258 if a_factor: |
| 6 | 259 print '[', a_factor, ']', |
| 260 print ';' | |
| 261 # | |
| 262 # Find input arguments derived from array sizes | |
| 263 # | |
| 264 for i in range(len(database)): | |
| 265 a_type, a_mode, a_factor, a_sub = database[i] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
266 if a_mode == 's' and a_sub[:3] == 'arg' and isnum(a_sub[3:]): |
| 6 | 267 # Sending a variable-length array |
| 268 n = eval(a_sub[3:]) | |
| 269 if 1 <= n <= len(database): | |
| 270 b_type, b_mode, b_factor, b_sub = database[n-1] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
271 if b_mode == 's': |
| 6 | 272 database[n-1] = b_type, 'i', a_factor, `i` |
| 273 n_in_args = n_in_args - 1 | |
| 274 # | |
| 275 # Assign argument positions in the Python argument list | |
| 276 # | |
| 277 in_pos = [] | |
| 278 i_in = 0 | |
| 279 for i in range(len(database)): | |
| 280 a_type, a_mode, a_factor, a_sub = database[i] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
281 if a_mode == 's': |
| 6 | 282 in_pos.append(i_in) |
| 283 i_in = i_in + 1 | |
| 284 else: | |
| 285 in_pos.append(-1) | |
| 286 # | |
| 287 # Get input arguments | |
| 288 # | |
| 289 for i in range(len(database)): | |
| 290 a_type, a_mode, a_factor, a_sub = database[i] | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
291 if a_type[:9] == 'unsigned ': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
292 xtype = a_type[9:] |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
293 else: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
294 xtype = a_type |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
295 if a_mode == 'i': |
| 6 | 296 # |
| 297 # Implicit argument; | |
| 298 # a_factor is divisor if present, | |
| 299 # a_sub indicates which arg (`database index`) | |
| 300 # | |
| 301 j = eval(a_sub) | |
| 302 print '\tif', | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
303 print '(!geti' + xtype + 'arraysize(args,', |
| 6 | 304 print `n_in_args` + ',', |
| 305 print `in_pos[j]` + ',', | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
306 if xtype <> a_type: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
307 print '('+xtype+' *)', |
| 6 | 308 print '&arg' + `i+1` + '))' |
| 309 print '\t\treturn NULL;' | |
| 310 if a_factor: | |
| 311 print '\targ' + `i+1`, | |
| 312 print '= arg' + `i+1`, | |
| 313 print '/', a_factor + ';' | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
314 elif a_mode == 's': |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
315 if a_sub and not isnum(a_sub): |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
316 # Allocate memory for varsize array |
| 6 | 317 print '\tif ((arg' + `i+1`, '=', |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
318 if a_factor: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
319 print '('+a_type+'(*)['+a_factor+'])', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
320 print 'NEW(' + a_type, ',', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
321 if a_factor: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
322 print a_factor, '*', |
| 6 | 323 print a_sub, ')) == NULL)' |
| 324 print '\t\treturn err_nomem();' | |
| 325 print '\tif', | |
| 326 if a_factor or a_sub: # Get a fixed-size array array | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
327 print '(!geti' + xtype + 'array(args,', |
| 6 | 328 print `n_in_args` + ',', |
| 329 print `in_pos[i]` + ',', | |
| 330 if a_factor: print a_factor, | |
| 331 if a_factor and a_sub: print '*', | |
| 332 if a_sub: print a_sub, | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
333 print ',', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
334 if (a_sub and a_factor) or xtype <> a_type: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
335 print '('+xtype+' *)', |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
336 print 'arg' + `i+1` + '))' |
| 6 | 337 else: # Get a simple variable |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
338 print '(!geti' + xtype + 'arg(args,', |
| 6 | 339 print `n_in_args` + ',', |
| 340 print `in_pos[i]` + ',', | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
341 if xtype <> a_type: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
342 print '('+xtype+' *)', |
| 6 | 343 print '&arg' + `i+1` + '))' |
| 344 print '\t\treturn NULL;' | |
| 345 # | |
| 346 # Begin of function call | |
| 347 # | |
| 348 if type <> 'void': | |
| 349 print '\tretval =', func + '(', | |
| 350 else: | |
| 351 print '\t' + func + '(', | |
| 352 # | |
| 353 # Argument list | |
| 354 # | |
| 355 for i in range(len(database)): | |
| 356 if i > 0: print ',', | |
| 357 a_type, a_mode, a_factor, a_sub = database[i] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
358 if a_mode == 'r' and not a_factor: |
| 6 | 359 print '&', |
| 360 print 'arg' + `i+1`, | |
| 361 # | |
| 362 # End of function call | |
| 363 # | |
| 364 print ');' | |
| 365 # | |
| 366 # Free varsize arrays | |
| 367 # | |
| 368 for i in range(len(database)): | |
| 369 a_type, a_mode, a_factor, a_sub = database[i] | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
370 if a_mode == 's' and a_sub and not isnum(a_sub): |
| 6 | 371 print '\tDEL(arg' + `i+1` + ');' |
| 372 # | |
| 373 # Return | |
| 374 # | |
| 375 if n_out_args: | |
| 376 # | |
| 377 # Multiple return values -- construct a tuple | |
| 378 # | |
| 379 if type <> 'void': | |
| 380 n_out_args = n_out_args + 1 | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
381 if n_out_args == 1: |
| 6 | 382 for i in range(len(database)): |
| 383 a_type, a_mode, a_factor, a_sub = database[i] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
384 if a_mode == 'r': |
| 6 | 385 break |
| 386 else: | |
| 387 raise arg_error, 'expected r arg not found' | |
| 388 print '\treturn', | |
| 389 print mkobject(a_type, 'arg' + `i+1`) + ';' | |
| 390 else: | |
| 391 print '\t{ object *v = newtupleobject(', | |
| 392 print n_out_args, ');' | |
| 393 print '\t if (v == NULL) return NULL;' | |
| 394 i_out = 0 | |
| 395 if type <> 'void': | |
| 396 print '\t settupleitem(v,', | |
| 397 print `i_out` + ',', | |
| 398 print mkobject(type, 'retval') + ');' | |
| 399 i_out = i_out + 1 | |
| 400 for i in range(len(database)): | |
| 401 a_type, a_mode, a_factor, a_sub = database[i] | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
402 if a_mode == 'r': |
| 6 | 403 print '\t settupleitem(v,', |
| 404 print `i_out` + ',', | |
| 405 s = mkobject(a_type, 'arg' + `i+1`) | |
| 406 print s + ');' | |
| 407 i_out = i_out + 1 | |
| 408 print '\t return v;' | |
| 409 print '\t}' | |
| 410 else: | |
| 411 # | |
| 412 # Simple function return | |
| 413 # Return None or return value | |
| 414 # | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
415 if type == 'void': |
| 6 | 416 print '\tINCREF(None);' |
| 417 print '\treturn None;' | |
| 418 else: | |
| 419 print '\treturn', mkobject(type, 'retval') + ';' | |
| 420 # | |
| 421 # Stub body closing brace | |
| 422 # | |
| 423 print '}' | |
| 424 | |
| 425 | |
| 426 # Subroutine to return a function call to mknew<type>object(<arg>) | |
| 427 # | |
| 428 def mkobject(type, arg): | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
429 if type[:9] == 'unsigned ': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
430 type = type[9:] |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
431 return 'mknew' + type + 'object((' + type + ') ' + arg + ')' |
| 6 | 432 return 'mknew' + type + 'object(' + arg + ')' |
| 433 | |
| 434 | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
435 defined_archs = [] |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
436 |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
437 # usage: cgen [ -Dmach ... ] [ file ] |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
438 for arg in sys.argv[1:]: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
439 if arg[:2] == '-D': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
440 defined_archs.append(arg[2:]) |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
441 else: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
442 # Open optional file argument |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
443 sys.stdin = open(arg, 'r') |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
975
diff
changeset
|
444 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
975
diff
changeset
|
445 |
| 6 | 446 # Input line number |
| 447 lno = 0 | |
| 448 | |
| 449 | |
| 450 # Input is divided in two parts, separated by a line containing '%%'. | |
| 451 # <part1> -- literally copied to stdout | |
| 452 # <part2> -- stub definitions | |
| 453 | |
| 454 # Variable indicating the current input part. | |
| 455 # | |
| 456 part = 1 | |
| 457 | |
| 458 # Main loop over the input | |
| 459 # | |
| 460 while 1: | |
| 461 try: | |
| 462 line = raw_input() | |
| 463 except EOFError: | |
| 464 break | |
| 465 # | |
| 466 lno = lno+1 | |
| 467 words = string.split(line) | |
| 468 # | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
469 if part == 1: |
| 6 | 470 # |
| 471 # In part 1, copy everything literally | |
| 472 # except look for a line of just '%%' | |
| 473 # | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
474 if words == ['%%']: |
| 6 | 475 part = part + 1 |
| 476 else: | |
| 477 # | |
| 478 # Look for names of manually written | |
| 479 # stubs: a single percent followed by the name | |
| 480 # of the function in Python. | |
| 481 # The stub name is derived by prefixing 'gl_'. | |
| 482 # | |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
483 if words and words[0][0] == '%': |
| 6 | 484 func = words[0][1:] |
| 485 if (not func) and words[1:]: | |
| 486 func = words[1] | |
| 487 if func: | |
| 488 functions.append(func) | |
| 489 else: | |
| 490 print line | |
|
1443
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
491 continue |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
492 if not words: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
493 continue # skip empty line |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
494 elif words[0] == 'if': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
495 # if XXX rest |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
496 # if !XXX rest |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
497 if words[1][0] == '!': |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
498 if words[1][1:] in defined_archs: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
499 continue |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
500 elif words[1] not in defined_archs: |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
501 continue |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
502 words = words[2:] |
|
2ec08a9d0325
rgbimgmodule.c, Makefile, config.c: new module to read RGB image files.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1048
diff
changeset
|
503 if words[0] == '#include': |
| 6 | 504 print line |
|
569
6c36c273d57d
Converted to use == equality check syntax.
Guido van Rossum <guido@python.org>
parents:
402
diff
changeset
|
505 elif words[0][:1] == '#': |
| 6 | 506 pass # ignore comment |
| 507 elif words[0] not in return_types: | |
| 508 err('Line', lno, ': bad return type :', words[0]) | |
| 509 elif len(words) < 2: | |
| 510 err('Line', lno, ': no funcname :', line) | |
| 511 else: | |
| 512 if len(words) % 2 <> 0: | |
| 513 err('Line', lno, ': odd argument list :', words[2:]) | |
| 514 else: | |
| 515 database = [] | |
| 516 try: | |
| 517 for i in range(2, len(words), 2): | |
| 518 x = checkarg(words[i], words[i+1]) | |
| 519 database.append(x) | |
| 520 print | |
| 521 print '/*', | |
| 522 for w in words: print w, | |
| 523 print '*/' | |
| 524 generate(words[0], words[1], database) | |
| 525 except arg_error, msg: | |
| 526 err('Line', lno, ':', msg) | |
| 527 | |
| 528 | |
| 529 print | |
| 530 print 'static struct methodlist gl_methods[] = {' | |
| 531 for func in functions: | |
| 532 print '\t{"' + func + '", gl_' + func + '},' | |
| 533 print '\t{NULL, NULL} /* Sentinel */' | |
| 534 print '};' | |
| 535 print | |
| 536 print 'initgl()' | |
| 537 print '{' | |
| 538 print '\tinitmodule("gl", gl_methods);' | |
| 539 print '}' |
