- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathcgen.py
523 lines (485 loc) · 15 KB
/
cgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
########################################################################
# Copyright (c) 2000, BeOpen.com.
# Copyright (c) 1995-2000, Corporation for National Research Initiatives.
# Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
# All rights reserved.
#
# See the file "Misc/COPYRIGHT" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
########################################################################
# Python script to parse cstubs file for gl and generate C stubs.
# usage: python cgen.py <cstubs >glmodule.c
#
# NOTE: You must first make a python binary without the "GL" option
# before you can run this, when building Python for the first time.
# See comments in the Makefile.
#
# XXX BUG return arrays generate wrong code
# XXX need to change error returns into gotos to free mallocked arrays
fromwarningsimportwarnpy3k
warnpy3k("the cgen module has been removed in Python 3.0", stacklevel=2)
delwarnpy3k
importstring
importsys
# Function to print to stderr
#
deferr(*args):
savestdout=sys.stdout
try:
sys.stdout=sys.stderr
foriinargs:
printi,
print
finally:
sys.stdout=savestdout
# The set of digits that form a number
#
digits='0123456789'
# Function to extract a string of digits from the front of the string.
# Returns the leading string of digits and the remaining string.
# If no number is found, returns '' and the original string.
#
defgetnum(s):
n=''
whilesands[0] indigits:
n=n+s[0]
s=s[1:]
returnn, s
# Function to check if a string is a number
#
defisnum(s):
ifnots: returnFalse
forcins:
ifnotcindigits: returnFalse
returnTrue
# Allowed function return types
#
return_types= ['void', 'short', 'long']
# Allowed function argument types
#
arg_types= ['char', 'string', 'short', 'u_short', 'float', 'long', 'double']
# Need to classify arguments as follows
# simple input variable
# simple output variable
# input array
# output array
# input giving size of some array
#
# Array dimensions can be specified as follows
# constant
# argN
# constant * argN
# retval
# constant * retval
#
# The dimensions given as constants * something are really
# arrays of points where points are 2- 3- or 4-tuples
#
# We have to consider three lists:
# python input arguments
# C stub arguments (in & out)
# python output arguments (really return values)
#
# There is a mapping from python input arguments to the input arguments
# of the C stub, and a further mapping from C stub arguments to the
# python return values
# Exception raised by checkarg() and generate()
#
arg_error='bad arg'
# Function to check one argument.
# Arguments: the type and the arg "name" (really mode plus subscript).
# Raises arg_error if something's wrong.
# Return type, mode, factor, rest of subscript; factor and rest may be empty.
#
defcheckarg(type, arg):
#
# Turn "char *x" into "string x".
#
iftype=='char'andarg[0] =='*':
type='string'
arg=arg[1:]
#
# Check that the type is supported.
#
iftypenotinarg_types:
raisearg_error, ('bad type', type)
iftype[:2] =='u_':
type='unsigned '+type[2:]
#
# Split it in the mode (first character) and the rest.
#
mode, rest=arg[:1], arg[1:]
#
# The mode must be 's' for send (= input) or 'r' for return argument.
#
ifmodenotin ('r', 's'):
raisearg_error, ('bad arg mode', mode)
#
# Is it a simple argument: if so, we are done.
#
ifnotrest:
returntype, mode, '', ''
#
# Not a simple argument; must be an array.
# The 'rest' must be a subscript enclosed in [ and ].
# The subscript must be one of the following forms,
# otherwise we don't handle it (where N is a number):
# N
# argN
# retval
# N*argN
# N*retval
#
ifrest[:1] <>'['orrest[-1:] <>']':
raisearg_error, ('subscript expected', rest)
sub=rest[1:-1]
#
# Is there a leading number?
#
num, sub=getnum(sub)
ifnum:
# There is a leading number
ifnotsub:
# The subscript is just a number
returntype, mode, num, ''
ifsub[:1] =='*':
# There is a factor prefix
sub=sub[1:]
else:
raisearg_error, ('\'*\' expected', sub)
ifsub=='retval':
# size is retval -- must be a reply argument
ifmode<>'r':
raisearg_error, ('non-r mode with [retval]', mode)
elifnotisnum(sub) and (sub[:3] <>'arg'ornotisnum(sub[3:])):
raisearg_error, ('bad subscript', sub)
#
returntype, mode, num, sub
# List of functions for which we have generated stubs
#
functions= []
# Generate the stub for the given function, using the database of argument
# information build by successive calls to checkarg()
#
defgenerate(type, func, database):
#
# Check that we can handle this case:
# no variable size reply arrays yet
#
n_in_args=0
n_out_args=0
#
fora_type, a_mode, a_factor, a_subindatabase:
ifa_mode=='s':
n_in_args=n_in_args+1
elifa_mode=='r':
n_out_args=n_out_args+1
else:
# Can't happen
raisearg_error, ('bad a_mode', a_mode)
if (a_mode=='r'anda_sub) ora_sub=='retval':
err('Function', func, 'too complicated:',
a_type, a_mode, a_factor, a_sub)
print'/* XXX Too complicated to generate code for */'
return
#
functions.append(func)
#
# Stub header
#
print
print'static PyObject *'
print'gl_'+func+'(self, args)'
print'\tPyObject *self;'
print'\tPyObject *args;'
print'{'
#
# Declare return value if any
#
iftype<>'void':
print'\t'+type, 'retval;'
#
# Declare arguments
#
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
print'\t'+a_type,
brac=ket=''
ifa_subandnotisnum(a_sub):
ifa_factor:
brac='('
ket=')'
printbrac+'*',
print'arg'+repr(i+1) +ket,
ifa_subandisnum(a_sub):
print'[', a_sub, ']',
ifa_factor:
print'[', a_factor, ']',
print';'
#
# Find input arguments derived from array sizes
#
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='s'anda_sub[:3] =='arg'andisnum(a_sub[3:]):
# Sending a variable-length array
n=eval(a_sub[3:])
if1<=n<=len(database):
b_type, b_mode, b_factor, b_sub=database[n-1]
ifb_mode=='s':
database[n-1] =b_type, 'i', a_factor, repr(i)
n_in_args=n_in_args-1
#
# Assign argument positions in the Python argument list
#
in_pos= []
i_in=0
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='s':
in_pos.append(i_in)
i_in=i_in+1
else:
in_pos.append(-1)
#
# Get input arguments
#
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_type[:9] =='unsigned ':
xtype=a_type[9:]
else:
xtype=a_type
ifa_mode=='i':
#
# Implicit argument;
# a_factor is divisor if present,
# a_sub indicates which arg (`database index`)
#
j=eval(a_sub)
print'\tif',
print'(!geti'+xtype+'arraysize(args,',
printrepr(n_in_args) +',',
printrepr(in_pos[j]) +',',
ifxtype<>a_type:
print'('+xtype+' *)',
print'&arg'+repr(i+1) +'))'
print'\t\treturn NULL;'
ifa_factor:
print'\targ'+repr(i+1),
print'= arg'+repr(i+1),
print'/', a_factor+';'
elifa_mode=='s':
ifa_subandnotisnum(a_sub):
# Allocate memory for varsize array
print'\tif ((arg'+repr(i+1), '=',
ifa_factor:
print'('+a_type+'(*)['+a_factor+'])',
print'PyMem_NEW('+a_type, ',',
ifa_factor:
printa_factor, '*',
printa_sub, ')) == NULL)'
print'\t\treturn PyErr_NoMemory();'
print'\tif',
ifa_factorora_sub: # Get a fixed-size array array
print'(!geti'+xtype+'array(args,',
printrepr(n_in_args) +',',
printrepr(in_pos[i]) +',',
ifa_factor: printa_factor,
ifa_factoranda_sub: print'*',
ifa_sub: printa_sub,
print',',
if (a_subanda_factor) orxtype<>a_type:
print'('+xtype+' *)',
print'arg'+repr(i+1) +'))'
else: # Get a simple variable
print'(!geti'+xtype+'arg(args,',
printrepr(n_in_args) +',',
printrepr(in_pos[i]) +',',
ifxtype<>a_type:
print'('+xtype+' *)',
print'&arg'+repr(i+1) +'))'
print'\t\treturn NULL;'
#
# Begin of function call
#
iftype<>'void':
print'\tretval =', func+'(',
else:
print'\t'+func+'(',
#
# Argument list
#
foriinrange(len(database)):
ifi>0: print',',
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='r'andnota_factor:
print'&',
print'arg'+repr(i+1),
#
# End of function call
#
print');'
#
# Free varsize arrays
#
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='s'anda_subandnotisnum(a_sub):
print'\tPyMem_DEL(arg'+repr(i+1) +');'
#
# Return
#
ifn_out_args:
#
# Multiple return values -- construct a tuple
#
iftype<>'void':
n_out_args=n_out_args+1
ifn_out_args==1:
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='r':
break
else:
raisearg_error, 'expected r arg not found'
print'\treturn',
printmkobject(a_type, 'arg'+repr(i+1)) +';'
else:
print'\t{ PyObject *v = PyTuple_New(',
printn_out_args, ');'
print'\t if (v == NULL) return NULL;'
i_out=0
iftype<>'void':
print'\t PyTuple_SetItem(v,',
printrepr(i_out) +',',
printmkobject(type, 'retval') +');'
i_out=i_out+1
foriinrange(len(database)):
a_type, a_mode, a_factor, a_sub=database[i]
ifa_mode=='r':
print'\t PyTuple_SetItem(v,',
printrepr(i_out) +',',
s=mkobject(a_type, 'arg'+repr(i+1))
prints+');'
i_out=i_out+1
print'\t return v;'
print'\t}'
else:
#
# Simple function return
# Return None or return value
#
iftype=='void':
print'\tPy_INCREF(Py_None);'
print'\treturn Py_None;'
else:
print'\treturn', mkobject(type, 'retval') +';'
#
# Stub body closing brace
#
print'}'
# Subroutine to return a function call to mknew<type>object(<arg>)
#
defmkobject(type, arg):
iftype[:9] =='unsigned ':
type=type[9:]
return'mknew'+type+'object(('+type+') '+arg+')'
return'mknew'+type+'object('+arg+')'
defined_archs= []
# usage: cgen [ -Dmach ... ] [ file ]
forarginsys.argv[1:]:
ifarg[:2] =='-D':
defined_archs.append(arg[2:])
else:
# Open optional file argument
sys.stdin=open(arg, 'r')
# Input line number
lno=0
# Input is divided in two parts, separated by a line containing '%%'.
# <part1> -- literally copied to stdout
# <part2> -- stub definitions
# Variable indicating the current input part.
#
part=1
# Main loop over the input
#
while1:
try:
line=raw_input()
exceptEOFError:
break
#
lno=lno+1
words=string.split(line)
#
ifpart==1:
#
# In part 1, copy everything literally
# except look for a line of just '%%'
#
ifwords== ['%%']:
part=part+1
else:
#
# Look for names of manually written
# stubs: a single percent followed by the name
# of the function in Python.
# The stub name is derived by prefixing 'gl_'.
#
ifwordsandwords[0][0] =='%':
func=words[0][1:]
if (notfunc) andwords[1:]:
func=words[1]
iffunc:
functions.append(func)
else:
printline
continue
ifnotwords:
continue# skip empty line
elifwords[0] =='if':
# if XXX rest
# if !XXX rest
ifwords[1][0] =='!':
ifwords[1][1:] indefined_archs:
continue
elifwords[1] notindefined_archs:
continue
words=words[2:]
ifwords[0] =='#include':
printline
elifwords[0][:1] =='#':
pass# ignore comment
elifwords[0] notinreturn_types:
err('Line', lno, ': bad return type :', words[0])
eliflen(words) <2:
err('Line', lno, ': no funcname :', line)
else:
iflen(words) %2<>0:
err('Line', lno, ': odd argument list :', words[2:])
else:
database= []
try:
foriinrange(2, len(words), 2):
x=checkarg(words[i], words[i+1])
database.append(x)
print
print'/*',
forwinwords: printw,
print'*/'
generate(words[0], words[1], database)
exceptarg_error, msg:
err('Line', lno, ':', msg)
print
print'static struct PyMethodDef gl_methods[] = {'
forfuncinfunctions:
print'\t{"'+func+'", gl_'+func+'},'
print'\t{NULL, NULL} /* Sentinel */'
print'};'
print
print'void'
print'initgl()'
print'{'
print'\t(void) Py_InitModule("gl", gl_methods);'
print'}'