Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Modules / cgen.py
blobc0329628e6d8b8904f94434a0a01cf8b365b7fe5
1 ########################################################################
2 # Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 # The Netherlands.
5 # All Rights Reserved
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation for any purpose and without fee is hereby granted,
9 # provided that the above copyright notice appear in all copies and that
10 # both that copyright notice and this permission notice appear in
11 # supporting documentation, and that the names of Stichting Mathematisch
12 # Centrum or CWI or Corporation for National Research Initiatives or
13 # CNRI not be used in advertising or publicity pertaining to
14 # distribution of the software without specific, written prior
15 # permission.
17 # While CWI is the initial source for this software, a modified version
18 # is made available by the Corporation for National Research Initiatives
19 # (CNRI) at the Internet address ftp://ftp.python.org.
21 # STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 # REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 # MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 # CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 # PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 # PERFORMANCE OF THIS SOFTWARE.
29 ########################################################################
31 # Python script to parse cstubs file for gl and generate C stubs.
32 # usage: python cgen.py <cstubs >glmodule.c
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.
38 # XXX BUG return arrays generate wrong code
39 # XXX need to change error returns into gotos to free mallocked arrays
42 import string
43 import sys
46 # Function to print to stderr
48 def err(*args):
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
59 # The set of digits that form a number
61 digits = '0123456789'
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.
68 def getnum(s):
69 n = ''
70 while s and s[0] in digits:
71 n = n + s[0]
72 s = s[1:]
73 return n, s
76 # Function to check if a string is a number
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
85 # Allowed function return types
87 return_types = ['void', 'short', 'long']
90 # Allowed function argument types
92 arg_types = ['char', 'string', 'short', 'u_short', 'float', 'long', 'double']
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
102 # Array dimensions can be specified as follows
103 # constant
104 # argN
105 # constant * argN
106 # retval
107 # constant * retval
109 # The dimensions given as constants * something are really
110 # arrays of points where points are 2- 3- or 4-tuples
112 # We have to consider three lists:
113 # python input arguments
114 # C stub arguments (in & out)
115 # python output arguments (really return values)
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
122 # Exception raised by checkarg() and generate()
124 arg_error = 'bad arg'
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.
132 def checkarg(type, arg):
134 # Turn "char *x" into "string x".
136 if type == 'char' and arg[0] == '*':
137 type = 'string'
138 arg = arg[1:]
140 # Check that the type is supported.
142 if type not in arg_types:
143 raise arg_error, ('bad type', type)
144 if type[:2] == 'u_':
145 type = 'unsigned ' + type[2:]
147 # Split it in the mode (first character) and the rest.
149 mode, rest = arg[:1], arg[1:]
151 # The mode must be 's' for send (= input) or 'r' for return argument.
153 if mode not in ('r', 's'):
154 raise arg_error, ('bad arg mode', mode)
156 # Is it a simple argument: if so, we are done.
158 if not rest:
159 return type, mode, '', ''
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):
166 # argN
167 # retval
168 # N*argN
169 # N*retval
171 if rest[:1] <> '[' or rest[-1:] <> ']':
172 raise arg_error, ('subscript expected', rest)
173 sub = rest[1:-1]
175 # Is there a leading number?
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, ''
183 if sub[:1] == '*':
184 # There is a factor prefix
185 sub = sub[1:]
186 else:
187 raise arg_error, ('\'*\' expected', sub)
188 if sub == 'retval':
189 # size is retval -- must be a reply argument
190 if mode <> 'r':
191 raise arg_error, ('non-r mode with [retval]', mode)
192 elif not isnum(sub) and (sub[:3] <> 'arg' or not isnum(sub[3:])):
193 raise arg_error, ('bad subscript', sub)
195 return type, mode, num, sub
198 # List of functions for which we have generated stubs
200 functions = []
203 # Generate the stub for the given function, using the database of argument
204 # information build by successive calls to checkarg()
206 def generate(type, func, database):
208 # Check that we can handle this case:
209 # no variable size reply arrays yet
211 n_in_args = 0
212 n_out_args = 0
214 for a_type, a_mode, a_factor, a_sub in database:
215 if a_mode == 's':
216 n_in_args = n_in_args + 1
217 elif a_mode == 'r':
218 n_out_args = n_out_args + 1
219 else:
220 # Can't happen
221 raise arg_error, ('bad a_mode', a_mode)
222 if (a_mode == 'r' and a_sub) or a_sub == 'retval':
223 err('Function', func, 'too complicated:',
224 a_type, a_mode, a_factor, a_sub)
225 print '/* XXX Too complicated to generate code for */'
226 return
228 functions.append(func)
230 # Stub header
232 print
233 print 'static PyObject *'
234 print 'gl_' + func + '(self, args)'
235 print '\tPyObject *self;'
236 print '\tPyObject *args;'
237 print '{'
239 # Declare return value if any
241 if type <> 'void':
242 print '\t' + type, 'retval;'
244 # Declare arguments
246 for i in range(len(database)):
247 a_type, a_mode, a_factor, a_sub = database[i]
248 print '\t' + a_type,
249 brac = ket = ''
250 if a_sub and not isnum(a_sub):
251 if a_factor:
252 brac = '('
253 ket = ')'
254 print brac + '*',
255 print 'arg' + `i+1` + ket,
256 if a_sub and isnum(a_sub):
257 print '[', a_sub, ']',
258 if a_factor:
259 print '[', a_factor, ']',
260 print ';'
262 # Find input arguments derived from array sizes
264 for i in range(len(database)):
265 a_type, a_mode, a_factor, a_sub = database[i]
266 if a_mode == 's' and a_sub[:3] == 'arg' and isnum(a_sub[3:]):
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]
271 if b_mode == 's':
272 database[n-1] = b_type, 'i', a_factor, `i`
273 n_in_args = n_in_args - 1
275 # Assign argument positions in the Python argument list
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]
281 if a_mode == 's':
282 in_pos.append(i_in)
283 i_in = i_in + 1
284 else:
285 in_pos.append(-1)
287 # Get input arguments
289 for i in range(len(database)):
290 a_type, a_mode, a_factor, a_sub = database[i]
291 if a_type[:9] == 'unsigned ':
292 xtype = a_type[9:]
293 else:
294 xtype = a_type
295 if a_mode == 'i':
297 # Implicit argument;
298 # a_factor is divisor if present,
299 # a_sub indicates which arg (`database index`)
301 j = eval(a_sub)
302 print '\tif',
303 print '(!geti' + xtype + 'arraysize(args,',
304 print `n_in_args` + ',',
305 print `in_pos[j]` + ',',
306 if xtype <> a_type:
307 print '('+xtype+' *)',
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 + ';'
314 elif a_mode == 's':
315 if a_sub and not isnum(a_sub):
316 # Allocate memory for varsize array
317 print '\tif ((arg' + `i+1`, '=',
318 if a_factor:
319 print '('+a_type+'(*)['+a_factor+'])',
320 print 'PyMem_NEW(' + a_type, ',',
321 if a_factor:
322 print a_factor, '*',
323 print a_sub, ')) == NULL)'
324 print '\t\treturn PyErr_NoMemory();'
325 print '\tif',
326 if a_factor or a_sub: # Get a fixed-size array array
327 print '(!geti' + xtype + 'array(args,',
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,
333 print ',',
334 if (a_sub and a_factor) or xtype <> a_type:
335 print '('+xtype+' *)',
336 print 'arg' + `i+1` + '))'
337 else: # Get a simple variable
338 print '(!geti' + xtype + 'arg(args,',
339 print `n_in_args` + ',',
340 print `in_pos[i]` + ',',
341 if xtype <> a_type:
342 print '('+xtype+' *)',
343 print '&arg' + `i+1` + '))'
344 print '\t\treturn NULL;'
346 # Begin of function call
348 if type <> 'void':
349 print '\tretval =', func + '(',
350 else:
351 print '\t' + func + '(',
353 # Argument list
355 for i in range(len(database)):
356 if i > 0: print ',',
357 a_type, a_mode, a_factor, a_sub = database[i]
358 if a_mode == 'r' and not a_factor:
359 print '&',
360 print 'arg' + `i+1`,
362 # End of function call
364 print ');'
366 # Free varsize arrays
368 for i in range(len(database)):
369 a_type, a_mode, a_factor, a_sub = database[i]
370 if a_mode == 's' and a_sub and not isnum(a_sub):
371 print '\tPyMem_DEL(arg' + `i+1` + ');'
373 # Return
375 if n_out_args:
377 # Multiple return values -- construct a tuple
379 if type <> 'void':
380 n_out_args = n_out_args + 1
381 if n_out_args == 1:
382 for i in range(len(database)):
383 a_type, a_mode, a_factor, a_sub = database[i]
384 if a_mode == 'r':
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{ PyObject *v = PyTuple_New(',
392 print n_out_args, ');'
393 print '\t if (v == NULL) return NULL;'
394 i_out = 0
395 if type <> 'void':
396 print '\t PyTuple_SetItem(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]
402 if a_mode == 'r':
403 print '\t PyTuple_SetItem(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:
412 # Simple function return
413 # Return None or return value
415 if type == 'void':
416 print '\tPy_INCREF(Py_None);'
417 print '\treturn Py_None;'
418 else:
419 print '\treturn', mkobject(type, 'retval') + ';'
421 # Stub body closing brace
423 print '}'
426 # Subroutine to return a function call to mknew<type>object(<arg>)
428 def mkobject(type, arg):
429 if type[:9] == 'unsigned ':
430 type = type[9:]
431 return 'mknew' + type + 'object((' + type + ') ' + arg + ')'
432 return 'mknew' + type + 'object(' + arg + ')'
435 defined_archs = []
437 # usage: cgen [ -Dmach ... ] [ file ]
438 for arg in sys.argv[1:]:
439 if arg[:2] == '-D':
440 defined_archs.append(arg[2:])
441 else:
442 # Open optional file argument
443 sys.stdin = open(arg, 'r')
446 # Input line number
447 lno = 0
450 # Input is divided in two parts, separated by a line containing '%%'.
451 # <part1> -- literally copied to stdout
452 # <part2> -- stub definitions
454 # Variable indicating the current input part.
456 part = 1
458 # Main loop over the input
460 while 1:
461 try:
462 line = raw_input()
463 except EOFError:
464 break
466 lno = lno+1
467 words = string.split(line)
469 if part == 1:
471 # In part 1, copy everything literally
472 # except look for a line of just '%%'
474 if words == ['%%']:
475 part = part + 1
476 else:
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_'.
483 if words and words[0][0] == '%':
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
491 continue
492 if not words:
493 continue # skip empty line
494 elif words[0] == 'if':
495 # if XXX rest
496 # if !XXX rest
497 if words[1][0] == '!':
498 if words[1][1:] in defined_archs:
499 continue
500 elif words[1] not in defined_archs:
501 continue
502 words = words[2:]
503 if words[0] == '#include':
504 print line
505 elif words[0][:1] == '#':
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)
529 print
530 print 'static struct PyMethodDef 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 'void'
537 print 'initgl()'
538 print '{'
539 print '\t(void) Py_InitModule("gl", gl_methods);'
540 print '}'