1 | #!/usr/bin/env python3
|
---|
2 |
|
---|
3 | from sys import stdout
|
---|
4 | from pickle import dump
|
---|
5 | from pycparser import c_ast, parse_file
|
---|
6 |
|
---|
7 | cross_gcc = "/opt/cross-m68k/bin/m68k-none-elf-gcc"
|
---|
8 |
|
---|
9 | class Visitor(c_ast.NodeVisitor):
|
---|
10 | def __init__(self):
|
---|
11 | self.path = None
|
---|
12 | self.void = True
|
---|
13 | self.proto = []
|
---|
14 |
|
---|
15 | def visit_Return(self, node):
|
---|
16 | if node.expr is not None:
|
---|
17 | self.void = False
|
---|
18 |
|
---|
19 | self.generic_visit(node)
|
---|
20 |
|
---|
21 | def visit_Decl(self, node):
|
---|
22 | node.storage = [x for x in node.storage if
|
---|
23 | x != "register" and
|
---|
24 | x != "static" and
|
---|
25 | x != "extern"]
|
---|
26 |
|
---|
27 | self.generic_visit(node)
|
---|
28 |
|
---|
29 | def visit_FuncDef(self, node):
|
---|
30 | if node.coord.file != self.path:
|
---|
31 | return
|
---|
32 |
|
---|
33 | self.generic_visit(node)
|
---|
34 |
|
---|
35 | # Convert K&R parameter declarations to ANSI.
|
---|
36 |
|
---|
37 | if node.param_decls is not None:
|
---|
38 | node.decl.type.args.params = node.param_decls
|
---|
39 | node.param_decls = None
|
---|
40 |
|
---|
41 | # Turn "foobar()" into "foobar(void)".
|
---|
42 |
|
---|
43 | if node.decl.type.args is None:
|
---|
44 | node.decl.type.args = c_ast.Typename(None, [],
|
---|
45 | c_ast.TypeDecl(None, [],
|
---|
46 | c_ast.ID("void")))
|
---|
47 |
|
---|
48 | # Override default int type, if necessary.
|
---|
49 |
|
---|
50 | if self.void:
|
---|
51 | node.decl.type.type.type = c_ast.ID("void")
|
---|
52 |
|
---|
53 | self.proto.append(node.decl)
|
---|
54 | self.void = True
|
---|
55 |
|
---|
56 | def visit_path(self, path, ast):
|
---|
57 | self.path = path
|
---|
58 | self.visit(ast)
|
---|
59 | self.path = None
|
---|
60 |
|
---|
61 | def store_proto(self, path):
|
---|
62 | with open(path, "wb") as f:
|
---|
63 | dump(self.proto, f)
|
---|
64 |
|
---|
65 | path_ast = []
|
---|
66 |
|
---|
67 | with open("misc/c-files.txt", "r") as f:
|
---|
68 | for path in f:
|
---|
69 | path = path.rstrip()
|
---|
70 |
|
---|
71 | if path == "ram/wdfield.c": # breaks pycparser
|
---|
72 | continue
|
---|
73 |
|
---|
74 | stdout.write("parsing {} \r".format(path))
|
---|
75 | stdout.flush()
|
---|
76 |
|
---|
77 | ast = parse_file(path, use_cpp = True, cpp_path = cross_gcc,
|
---|
78 | cpp_args = ["-E", "-I", "include", "-include", "predef.h"])
|
---|
79 | path_ast.append((path, ast))
|
---|
80 | # ast.show()
|
---|
81 |
|
---|
82 | print("")
|
---|
83 |
|
---|
84 | vis = Visitor()
|
---|
85 |
|
---|
86 | for (path, ast) in path_ast:
|
---|
87 | stdout.write("visiting {} \r".format(path))
|
---|
88 | stdout.flush()
|
---|
89 |
|
---|
90 | vis.visit_path(path, ast)
|
---|
91 |
|
---|
92 | print("")
|
---|
93 | vis.store_proto("proto.dat")
|
---|