Mercurial > hg > CbC > CbC_gcc
annotate CbC-scripts/make_headers.py2 @ 158:494b0b89df80 default tip
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 18:13:55 +0900 |
parents | f9b1a53df341 |
children |
rev | line source |
---|---|
26 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import sys | |
5 import re | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
6 import getopt |
26 | 7 |
8 reserved_words = [ "if", "for", "switch", "return", "while", "else", ] | |
9 | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
10 PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^\{/;]*)\)\s*\{" |
26 | 11 #PATTERN = r"([a-zA-Z_]\w*)\s+([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{" |
12 #PATTERN = r"((?:[a-zA-Z_]\w*)\s+)+?([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{" | |
13 # TODO: 関数パラメータ内にコメントがあると正しく動かない! | |
14 PROG = re.compile(PATTERN, re.S) | |
15 | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
16 omit_static=False |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
17 add_extern="" |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
18 |
26 | 19 def truncate_comments(data): |
20 pass | |
21 | |
22 def check_reserved_word(decl): | |
23 """ return true if decl's type and name is not reserved word. """ | |
24 | |
25 if decl["name"] in reserved_words or decl["type"] in reserved_words: | |
26 return False | |
27 return True | |
28 | |
29 def read_decls(file): | |
30 declarators = [] | |
31 | |
32 # open the file and read all lines into a string. | |
33 try: | |
34 fo = open(file, 'r') | |
35 lines = fo.readlines() | |
36 data = "".join(lines) | |
37 truncate_comments(data) | |
38 except IOError: | |
39 print "cannot read file %s" % file | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
40 return None |
26 | 41 |
42 # find all matched strings. | |
43 # moiter is iterator of MatchObject. | |
44 moiter = PROG.finditer(data) | |
45 for mo in moiter: | |
46 tmp = { "type": mo.group(1), | |
47 "name": mo.group(2), | |
48 "parms": mo.group(3), | |
49 "offset": mo.start() } | |
50 if check_reserved_word(tmp): | |
51 declarators.append(tmp) | |
52 | |
53 return declarators | |
54 | |
55 def debug_print(decl): | |
56 for (key,value) in decl.items(): | |
57 if isinstance(value, str): | |
58 decl[key] = value.replace("\n"," ").replace("\t"," ") | |
59 | |
60 print "Type:\t%s" % decl["type"] | |
61 print "Name:\t%s" % decl["name"] | |
62 print "Params:\t%s" % decl["parms"] | |
63 print "offset:\t%d" % decl["offset"] | |
64 print "" | |
65 #s = "%s %s ( %s );" % (decl["type"], decl["name"], decl["parms"]) | |
66 #print s, "/* offset: %d */" % decl["offset"] | |
67 | |
68 def format_print(decl, file): | |
69 for (key,value) in decl.items(): | |
70 if isinstance(value, str): | |
71 decl[key] = value.replace("\n"," ").replace("\t"," ") | |
72 | |
73 print "/* defined in file %s at offset %d */" % (file,decl["offset"]) | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
74 print "%s%s %s (%s);" % (add_extern, decl["type"],decl["name"],decl["parms"]) |
26 | 75 print "" |
76 | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
77 def getoptions(): |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
78 global omit_static, add_extern |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
79 |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
80 try: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
81 opts, args = getopt.getopt(sys.argv[1:], 'se', [ 'omit-static', 'add-extern' ]) |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
82 except getopt.GetoptError: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
83 print(err) |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
84 usage() |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
85 sys.exit(2) |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
86 |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
87 for opt,a in opts: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
88 if opt in ("-s", "--omit-static"): |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
89 omit_static=True |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
90 elif opt in ("-e", "--add-extern"): |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
91 add_extern="extern " |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
92 else: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
93 print("unhandled option {0}".format(opt)) |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
94 usage() |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
95 |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
96 return args |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
97 |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
98 def usage(): |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
99 print( """\ |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
100 Usage: {0:s} OPION... [FILE]... |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
101 OPTIONS: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
102 -s, --omit-static omit static functions |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
103 -e, --add-extern add extern to all function declarations |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
104 """.format(sys.argv[0])) |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
105 |
26 | 106 def main(): |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
107 # option handling. |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
108 args = getoptions() |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
109 |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
110 for file in args: |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
111 # read function declaration from each file. |
26 | 112 decls = read_decls(file) |
113 if decls==None or len(decls)==0: | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
114 # no function found. |
26 | 115 print "%s have no function definition!" % file |
116 continue | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
117 |
26 | 118 for decl in decls: |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
119 if omit_static and 0 <= decl["type"].find("static"): |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
120 # static function is ignored. |
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
121 continue |
26 | 122 #debug_print(decl) |
123 format_print(decl, file) | |
124 | |
125 main() | |
126 |