blob: fa71c23b73a95f3f5aea760a0a1d2a46d8292b7e [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001#! /usr/bin/env python
2"""Generate C code from an ASDL description."""
3
4# TO DO
5# handle fields that have a type but no name
6
7import os, sys, traceback
8
9import asdl
10
11TABSIZE = 8
12MAX_COL = 80
13
14def get_c_type(name):
15 """Return a string for the C name of the type.
16
17 This function special cases the default types provided by asdl:
18 identifier, string, int, bool.
19 """
20 # XXX ack! need to figure out where Id is useful and where string
21 if isinstance(name, asdl.Id):
22 name = name.value
23 if name in asdl.builtin_types:
24 return name
25 else:
26 return "%s_ty" % name
27
28def reflow_lines(s, depth):
29 """Reflow the line s indented depth tabs.
30
31 Return a sequence of lines where no line extends beyond MAX_COL
32 when properly indented. The first line is properly indented based
33 exclusively on depth * TABSIZE. All following lines -- these are
34 the reflowed lines generated by this function -- start at the same
35 column as the first character beyond the opening { in the first
36 line.
37 """
38 size = MAX_COL - depth * TABSIZE
39 if len(s) < size:
40 return [s]
41
42 lines = []
43 cur = s
44 padding = ""
45 while len(cur) > size:
46 i = cur.rfind(' ', 0, size)
47 # XXX this should be fixed for real
48 if i == -1 and 'GeneratorExp' in cur:
49 i = size + 3
50 assert i != -1, "Impossible line %d to reflow: %s" % (size, `s`)
51 lines.append(padding + cur[:i])
52 if len(lines) == 1:
53 # find new size based on brace
54 j = cur.find('{', 0, i)
55 if j >= 0:
56 j += 2 # account for the brace and the space after it
57 size -= j
58 padding = " " * j
59 else:
60 j = cur.find('(', 0, i)
61 if j >= 0:
62 j += 1 # account for the paren (no space after it)
63 size -= j
64 padding = " " * j
65 cur = cur[i+1:]
66 else:
67 lines.append(padding + cur)
68 return lines
69
70def is_simple(sum):
71 """Return True if a sum is a simple.
72
73 A sum is simple if its types have no fields, e.g.
74 unaryop = Invert | Not | UAdd | USub
75 """
76
77 for t in sum.types:
78 if t.fields:
79 return False
80 return True
81
82class EmitVisitor(asdl.VisitorBase):
83 """Visit that emits lines"""
84
85 def __init__(self, file):
86 self.file = file
87 super(EmitVisitor, self).__init__()
88
89 def emit(self, s, depth, reflow=1):
90 # XXX reflow long lines?
91 if reflow:
92 lines = reflow_lines(s, depth)
93 else:
94 lines = [s]
95 for line in lines:
96 line = (" " * TABSIZE * depth) + line + "\n"
97 self.file.write(line)
98
99class TypeDefVisitor(EmitVisitor):
100 def visitModule(self, mod):
101 for dfn in mod.dfns:
102 self.visit(dfn)
103
104 def visitType(self, type, depth=0):
105 self.visit(type.value, type.name, depth)
106
107 def visitSum(self, sum, name, depth):
108 if is_simple(sum):
109 self.simple_sum(sum, name, depth)
110 else:
111 self.sum_with_constructors(sum, name, depth)
112
113 def simple_sum(self, sum, name, depth):
114 enum = []
115 for i in range(len(sum.types)):
116 type = sum.types[i]
117 enum.append("%s=%d" % (type.name, i + 1))
118 enums = ", ".join(enum)
119 ctype = get_c_type(name)
120 s = "typedef enum _%s { %s } %s;" % (name, enums, ctype)
121 self.emit(s, depth)
122 self.emit("", depth)
123
124 def sum_with_constructors(self, sum, name, depth):
125 ctype = get_c_type(name)
126 s = "typedef struct _%(name)s *%(ctype)s;" % locals()
127 self.emit(s, depth)
128 self.emit("", depth)
129
130 def visitProduct(self, product, name, depth):
131 ctype = get_c_type(name)
132 s = "typedef struct _%(name)s *%(ctype)s;" % locals()
133 self.emit(s, depth)
134 self.emit("", depth)
135
136class StructVisitor(EmitVisitor):
137 """Visitor to generate typdefs for AST."""
138
139 def visitModule(self, mod):
140 for dfn in mod.dfns:
141 self.visit(dfn)
142
143 def visitType(self, type, depth=0):
144 self.visit(type.value, type.name, depth)
145
146 def visitSum(self, sum, name, depth):
147 if not is_simple(sum):
148 self.sum_with_constructors(sum, name, depth)
149
150 def sum_with_constructors(self, sum, name, depth):
151 def emit(s, depth=depth):
152 self.emit(s % sys._getframe(1).f_locals, depth)
153 enum = []
154 for i in range(len(sum.types)):
155 type = sum.types[i]
156 enum.append("%s_kind=%d" % (type.name, i + 1))
157
158 emit("struct _%(name)s {")
159 emit("enum { " + ", ".join(enum) + " } kind;", depth + 1)
160 emit("union {", depth + 1)
161 for t in sum.types:
162 self.visit(t, depth + 2)
163 emit("} v;", depth + 1)
164 for field in sum.attributes:
165 # rudimentary attribute handling
166 type = str(field.type)
167 assert type in asdl.builtin_types, type
168 emit("%s %s;" % (type, field.name), depth + 1);
169 emit("};")
170 emit("")
171
172 def visitConstructor(self, cons, depth):
173 if cons.fields:
174 self.emit("struct {", depth)
175 for f in cons.fields:
176 self.visit(f, depth + 1)
177 self.emit("} %s;" % cons.name, depth)
178 self.emit("", depth)
179 else:
180 # XXX not sure what I want here, nothing is probably fine
181 pass
182
183 def visitField(self, field, depth):
184 # XXX need to lookup field.type, because it might be something
185 # like a builtin...
186 ctype = get_c_type(field.type)
187 name = field.name
188 if field.seq:
189 self.emit("asdl_seq *%(name)s;" % locals(), depth)
190 else:
191 self.emit("%(ctype)s %(name)s;" % locals(), depth)
192
193 def visitProduct(self, product, name, depth):
194 self.emit("struct _%(name)s {" % locals(), depth)
195 for f in product.fields:
196 self.visit(f, depth + 1)
197 self.emit("};", depth)
198 self.emit("", depth)
199
200class PrototypeVisitor(EmitVisitor):
201 """Generate function prototypes for the .h file"""
202
203 def visitModule(self, mod):
204 for dfn in mod.dfns:
205 self.visit(dfn)
206
207 def visitType(self, type):
208 self.visit(type.value, type.name)
209
210 def visitSum(self, sum, name):
211 if is_simple(sum):
212 pass # XXX
213 else:
214 for t in sum.types:
215 self.visit(t, name, sum.attributes)
216
217 def get_args(self, fields):
218 """Return list of C argument into, one for each field.
219
220 Argument info is 3-tuple of a C type, variable name, and flag
221 that is true if type can be NULL.
222 """
223 args = []
224 unnamed = {}
225 for f in fields:
226 if f.name is None:
227 name = f.type
228 c = unnamed[name] = unnamed.get(name, 0) + 1
229 if c > 1:
230 name = "name%d" % (c - 1)
231 else:
232 name = f.name
233 # XXX should extend get_c_type() to handle this
234 if f.seq:
235 ctype = "asdl_seq *"
236 else:
237 ctype = get_c_type(f.type)
238 args.append((ctype, name, f.opt or f.seq))
239 return args
240
241 def visitConstructor(self, cons, type, attrs):
242 args = self.get_args(cons.fields)
243 attrs = self.get_args(attrs)
244 ctype = get_c_type(type)
245 self.emit_function(cons.name, ctype, args, attrs)
246
247 def emit_function(self, name, ctype, args, attrs, union=1):
248 args = args + attrs
249 if args:
250 argstr = ", ".join(["%s %s" % (atype, aname)
251 for atype, aname, opt in args])
252 else:
253 argstr = "void"
254 self.emit("%s %s(%s);" % (ctype, name, argstr), 0)
255
256 def visitProduct(self, prod, name):
257 self.emit_function(name, get_c_type(name),
258 self.get_args(prod.fields), [], union=0)
259
260class FunctionVisitor(PrototypeVisitor):
261 """Visitor to generate constructor functions for AST."""
262
263 def emit_function(self, name, ctype, args, attrs, union=1):
264 def emit(s, depth=0, reflow=1):
265 self.emit(s, depth, reflow)
266 argstr = ", ".join(["%s %s" % (atype, aname)
267 for atype, aname, opt in args + attrs])
268 self.emit("%s" % ctype, 0)
269 emit("%s(%s)" % (name, argstr))
270 emit("{")
271 emit("%s p;" % ctype, 1)
272 for argtype, argname, opt in args:
273 # XXX hack alert: false is allowed for a bool
274 if not opt and not argtype == "bool":
275 emit("if (!%s) {" % argname, 1)
276 emit("PyErr_SetString(PyExc_ValueError,", 2)
277 msg = "field %s is required for %s" % (argname, name)
278 emit(' "%s");' % msg,
279 2, reflow=0)
280 emit('return NULL;', 2)
281 emit('}', 1)
282
283 emit("p = (%s)malloc(sizeof(*p));" % ctype, 1)
284 emit("if (!p) {", 1)
Neal Norwitza34584b2005-10-23 18:59:17 +0000285 emit("PyErr_NoMemory();", 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286 emit("return NULL;", 2)
287 emit("}", 1)
288 if union:
289 self.emit_body_union(name, args, attrs)
290 else:
291 self.emit_body_struct(name, args, attrs)
292 emit("return p;", 1)
293 emit("}")
294 emit("")
295
296 def emit_body_union(self, name, args, attrs):
297 def emit(s, depth=0, reflow=1):
298 self.emit(s, depth, reflow)
299 emit("p->kind = %s_kind;" % name, 1)
300 for argtype, argname, opt in args:
301 emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
302 for argtype, argname, opt in attrs:
303 emit("p->%s = %s;" % (argname, argname), 1)
304
305 def emit_body_struct(self, name, args, attrs):
306 def emit(s, depth=0, reflow=1):
307 self.emit(s, depth, reflow)
308 for argtype, argname, opt in args:
309 emit("p->%s = %s;" % (argname, argname), 1)
310 assert not attrs
311
312class PickleVisitor(EmitVisitor):
313
314 def visitModule(self, mod):
315 for dfn in mod.dfns:
316 self.visit(dfn)
317
318 def visitType(self, type):
319 self.visit(type.value, type.name)
320
321 def visitSum(self, sum, name):
322 pass
323
324 def visitProduct(self, sum, name):
325 pass
326
327 def visitConstructor(self, cons, name):
328 pass
329
330 def visitField(self, sum):
331 pass
332
333class MarshalPrototypeVisitor(PickleVisitor):
334
335 def prototype(self, sum, name):
336 ctype = get_c_type(name)
Neal Norwitz6576bd82005-11-13 18:41:28 +0000337 self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338 % (name, ctype), 0)
339
340 visitProduct = visitSum = prototype
341
342class FreePrototypeVisitor(PickleVisitor):
343
344 def prototype(self, sum, name):
345 ctype = get_c_type(name)
346 self.emit("void free_%s(%s);" % (name, ctype), 0)
347
348 visitProduct = visitSum = prototype
349
350_SPECIALIZED_SEQUENCES = ('stmt', 'expr')
351
352def find_sequence(fields, doing_specialization):
353 """Return True if any field uses a sequence."""
354 for f in fields:
355 if f.seq:
356 if not doing_specialization:
357 return True
358 if str(f.type) not in _SPECIALIZED_SEQUENCES:
359 return True
360 return False
361
362def has_sequence(types, doing_specialization):
363 for t in types:
364 if find_sequence(t.fields, doing_specialization):
365 return True
366 return False
367
368
369class StaticVisitor(PickleVisitor):
370 '''Very simple, always emit this static code'''
371
372 CODE = '''static void
373free_seq_exprs(asdl_seq *seq)
374{
375 int i, n;
376 n = asdl_seq_LEN(seq);
377 for (i = 0; i < n; i++)
378 free_expr((expr_ty)asdl_seq_GET(seq, i));
379 asdl_seq_free(seq);
380}
381
382static void
383free_seq_stmts(asdl_seq *seq)
384{
385 int i, n;
386 n = asdl_seq_LEN(seq);
387 for (i = 0; i < n; i++)
388 free_stmt((stmt_ty)asdl_seq_GET(seq, i));
389 asdl_seq_free(seq);
390}
391'''
392
393 def visit(self, object):
394 self.emit(self.CODE, 0, reflow=False)
395
396
397class FreeVisitor(PickleVisitor):
398
399 def func_begin(self, name, has_seq):
400 ctype = get_c_type(name)
401 self.emit("void", 0)
402 self.emit("free_%s(%s o)" % (name, ctype), 0)
403 self.emit("{", 0)
404 if has_seq:
405 self.emit("int i, n;", 1)
406 self.emit("asdl_seq *seq;", 1)
407 self.emit('', 0)
408 self.emit('if (!o)', 1)
409 self.emit('return;', 2)
410 self.emit('', 0)
411
412 def func_end(self):
413 self.emit("}", 0)
414 self.emit("", 0)
415
416 def visitSum(self, sum, name):
417 has_seq = has_sequence(sum.types, True)
418 self.func_begin(name, has_seq)
419 if not is_simple(sum):
420 self.emit("switch (o->kind) {", 1)
421 for i in range(len(sum.types)):
422 t = sum.types[i]
423 self.visitConstructor(t, i + 1, name)
424 self.emit("}", 1)
425 self.emit("", 0)
426 self.emit("free(o);", 1)
427 self.func_end()
428
429 def visitProduct(self, prod, name):
430 self.func_begin(name, find_sequence(prod.fields, True))
431 for field in prod.fields:
432 self.visitField(field, name, 1, True)
433 self.emit("", 0)
434 self.emit("free(o);", 1)
435 self.func_end()
436
437 def visitConstructor(self, cons, enum, name):
438 self.emit("case %s_kind:" % cons.name, 1)
439 for f in cons.fields:
440 self.visitField(f, cons.name, 2, False)
441 self.emit("break;", 2)
442
443 def visitField(self, field, name, depth, product):
444 def emit(s, d):
445 self.emit(s, depth + d)
446 if product:
447 value = "o->%s" % field.name
448 else:
449 value = "o->v.%s.%s" % (name, field.name)
450 if field.seq:
451 self.emitSeq(field, value, depth, emit)
452
453 # XXX need to know the simple types in advance, so that we
454 # don't call free_TYPE() for them.
455
456 elif field.opt:
457 emit("if (%s) {" % value, 0)
458 self.free(field, value, depth + 1)
459 emit("}", 0)
460 else:
461 self.free(field, value, depth)
462
463 def emitSeq(self, field, value, depth, emit):
464 # specialize for freeing sequences of statements and expressions
465 if str(field.type) in _SPECIALIZED_SEQUENCES:
466 c_code = "free_seq_%ss(%s);" % (field.type, value)
467 emit(c_code, 0)
468 else:
469 emit("seq = %s;" % value, 0)
470 emit("n = asdl_seq_LEN(seq);", 0)
471 emit("for (i = 0; i < n; i++)", 0)
472 self.free(field, "asdl_seq_GET(seq, i)", depth + 1)
473 emit("asdl_seq_free(seq);", 0)
474
475 def free(self, field, value, depth):
476 if str(field.type) in ("identifier", "string", "object"):
477 ctype = get_c_type(field.type)
478 self.emit("Py_DECREF((%s)%s);" % (ctype, value), depth)
479 elif str(field.type) == "bool":
480 return
481 else:
482 ctype = get_c_type(field.type)
483 self.emit("free_%s((%s)%s);" % (field.type, ctype, value), depth)
484
485
486class MarshalFunctionVisitor(PickleVisitor):
487
488 def func_begin(self, name, has_seq):
489 ctype = get_c_type(name)
Neal Norwitz6576bd82005-11-13 18:41:28 +0000490 self.emit("static int", 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491 self.emit("marshal_write_%s(PyObject **buf, int *off, %s o)" %
492 (name, ctype), 0)
493 self.emit("{", 0)
Neal Norwitza34584b2005-10-23 18:59:17 +0000494 if has_seq:
495 self.emit("int i;", 1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
497 def func_end(self):
498 self.emit("return 1;", 1)
499 self.emit("}", 0)
500 self.emit("", 0)
501
502 def visitSum(self, sum, name):
Neal Norwitza34584b2005-10-23 18:59:17 +0000503 self.func_begin(name, has_sequence(sum.types, False))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 simple = is_simple(sum)
505 if simple:
506 self.emit("switch (o) {", 1)
507 else:
508 self.emit("switch (o->kind) {", 1)
509 for i in range(len(sum.types)):
510 t = sum.types[i]
511 self.visitConstructor(t, i + 1, name, simple)
512 self.emit("}", 1)
513 self.func_end()
514
515 def visitProduct(self, prod, name):
Neal Norwitza34584b2005-10-23 18:59:17 +0000516 self.func_begin(name, find_sequence(prod.fields, False))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 for field in prod.fields:
518 self.visitField(field, name, 1, 1)
519 self.func_end()
520
521 def visitConstructor(self, cons, enum, name, simple):
522 if simple:
523 self.emit("case %s:" % cons.name, 1)
524 self.emit("marshal_write_int(buf, off, %d);" % enum, 2);
525 self.emit("break;", 2)
526 else:
527 self.emit("case %s_kind:" % cons.name, 1)
528 self.emit("marshal_write_int(buf, off, %d);" % enum, 2)
529 for f in cons.fields:
530 self.visitField(f, cons.name, 2, 0)
531 self.emit("break;", 2)
532
533 def visitField(self, field, name, depth, product):
534 def emit(s, d):
535 self.emit(s, depth + d)
536 if product:
537 value = "o->%s" % field.name
538 else:
539 value = "o->v.%s.%s" % (name, field.name)
540 if field.seq:
541 emit("marshal_write_int(buf, off, asdl_seq_LEN(%s));" % value, 0)
542 emit("for (i = 0; i < asdl_seq_LEN(%s); i++) {" % value, 0)
543 emit("void *elt = asdl_seq_GET(%s, i);" % value, 1);
544 ctype = get_c_type(field.type);
545 emit("marshal_write_%s(buf, off, (%s)elt);" % (field.type,
546 ctype), 1)
547 emit("}", 0)
548 elif field.opt:
549 emit("if (%s) {" % value, 0)
550 emit("marshal_write_int(buf, off, 1);", 1)
551 emit("marshal_write_%s(buf, off, %s);" % (field.type, value), 1)
552 emit("}", 0)
553 emit("else {", 0)
554 emit("marshal_write_int(buf, off, 0);", 1)
555 emit("}", 0)
556 else:
557 emit("marshal_write_%s(buf, off, %s);" % (field.type, value), 0)
558
559class ChainOfVisitors:
560 def __init__(self, *visitors):
561 self.visitors = visitors
562
563 def visit(self, object):
564 for v in self.visitors:
565 v.visit(object)
566
567def main(srcfile):
568 auto_gen_msg = '/* File automatically generated by %s */\n' % sys.argv[0]
569 mod = asdl.parse(srcfile)
570 if not asdl.check(mod):
571 sys.exit(1)
572 if INC_DIR:
573 p = "%s/%s-ast.h" % (INC_DIR, mod.name)
574 else:
575 p = "%s-ast.h" % mod.name
576 f = open(p, "wb")
577 print >> f, auto_gen_msg
578 print >> f, '#include "asdl.h"\n'
579 c = ChainOfVisitors(TypeDefVisitor(f),
580 StructVisitor(f),
581 PrototypeVisitor(f),
582 FreePrototypeVisitor(f),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 )
584 c.visit(mod)
585 f.close()
586
587 if SRC_DIR:
588 p = "%s/%s-ast.c" % (SRC_DIR, mod.name)
589 else:
590 p = "%s-ast.c" % mod.name
591 f = open(p, "wb")
592 print >> f, auto_gen_msg
593 print >> f, '#include "Python.h"'
594 print >> f, '#include "%s-ast.h"' % mod.name
595 print >> f
Neal Norwitz6576bd82005-11-13 18:41:28 +0000596 v = ChainOfVisitors(MarshalPrototypeVisitor(f),
597 FunctionVisitor(f),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 StaticVisitor(f),
599 FreeVisitor(f),
600 MarshalFunctionVisitor(f),
601 )
602 v.visit(mod)
603 f.close()
604
605if __name__ == "__main__":
606 import sys
607 import getopt
608
609 INC_DIR = ''
610 SRC_DIR = ''
611 opts, args = getopt.getopt(sys.argv[1:], "h:c:")
612 for o, v in opts:
613 if o == '-h':
614 INC_DIR = v
615 if o == '-c':
616 SRC_DIR = v
617 if len(args) != 1:
618 print "Must specify single input file"
619 main(args[0])