comparison CbC-scripts/make_headers.py2 @ 27:f9b1a53df341

implemented indirect sibcall for ppc.
author kent@teto.cr.ie.u-ryukyu.ac.jp
date Tue, 10 Nov 2009 16:34:29 +0900
parents b388631e4738
children
comparison
equal deleted inserted replaced
26:b388631e4738 27:f9b1a53df341
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 import sys 4 import sys
5 import re 5 import re
6 import getopt
6 7
7 reserved_words = [ "if", "for", "switch", "return", "while", "else", ] 8 reserved_words = [ "if", "for", "switch", "return", "while", "else", ]
8 9
10 PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^\{/;]*)\)\s*\{"
9 #PATTERN = r"([a-zA-Z_]\w*)\s+([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{" 11 #PATTERN = r"([a-zA-Z_]\w*)\s+([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{"
10 #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*\{"
11 PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^/;]*)\)\s*\{"
12 # TODO: 関数パラメータ内にコメントがあると正しく動かない! 13 # TODO: 関数パラメータ内にコメントがあると正しく動かない!
13 PROG = re.compile(PATTERN, re.S) 14 PROG = re.compile(PATTERN, re.S)
15
16 omit_static=False
17 add_extern=""
14 18
15 def truncate_comments(data): 19 def truncate_comments(data):
16 pass 20 pass
17 21
18 def check_reserved_word(decl): 22 def check_reserved_word(decl):
31 lines = fo.readlines() 35 lines = fo.readlines()
32 data = "".join(lines) 36 data = "".join(lines)
33 truncate_comments(data) 37 truncate_comments(data)
34 except IOError: 38 except IOError:
35 print "cannot read file %s" % file 39 print "cannot read file %s" % file
40 return None
36 41
37 # find all matched strings. 42 # find all matched strings.
38 # moiter is iterator of MatchObject. 43 # moiter is iterator of MatchObject.
39 moiter = PROG.finditer(data) 44 moiter = PROG.finditer(data)
40 for mo in moiter: 45 for mo in moiter:
64 for (key,value) in decl.items(): 69 for (key,value) in decl.items():
65 if isinstance(value, str): 70 if isinstance(value, str):
66 decl[key] = value.replace("\n"," ").replace("\t"," ") 71 decl[key] = value.replace("\n"," ").replace("\t"," ")
67 72
68 print "/* defined in file %s at offset %d */" % (file,decl["offset"]) 73 print "/* defined in file %s at offset %d */" % (file,decl["offset"])
69 print "%s %s (%s);" % (decl["type"],decl["name"],decl["parms"]) 74 print "%s%s %s (%s);" % (add_extern, decl["type"],decl["name"],decl["parms"])
70 print "" 75 print ""
71 76
77 def getoptions():
78 global omit_static, add_extern
79
80 try:
81 opts, args = getopt.getopt(sys.argv[1:], 'se', [ 'omit-static', 'add-extern' ])
82 except getopt.GetoptError:
83 print(err)
84 usage()
85 sys.exit(2)
86
87 for opt,a in opts:
88 if opt in ("-s", "--omit-static"):
89 omit_static=True
90 elif opt in ("-e", "--add-extern"):
91 add_extern="extern "
92 else:
93 print("unhandled option {0}".format(opt))
94 usage()
95
96 return args
97
98 def usage():
99 print( """\
100 Usage: {0:s} OPION... [FILE]...
101 OPTIONS:
102 -s, --omit-static omit static functions
103 -e, --add-extern add extern to all function declarations
104 """.format(sys.argv[0]))
105
72 def main(): 106 def main():
73 for file in sys.argv[1:]: 107 # option handling.
108 args = getoptions()
109
110 for file in args:
111 # read function declaration from each file.
74 decls = read_decls(file) 112 decls = read_decls(file)
75
76 if decls==None or len(decls)==0: 113 if decls==None or len(decls)==0:
114 # no function found.
77 print "%s have no function definition!" % file 115 print "%s have no function definition!" % file
78 continue 116 continue
117
79 for decl in decls: 118 for decl in decls:
119 if omit_static and 0 <= decl["type"].find("static"):
120 # static function is ignored.
121 continue
80 #debug_print(decl) 122 #debug_print(decl)
81 format_print(decl, file) 123 format_print(decl, file)
82 #print decls[0]
83
84 124
85 main() 125 main()
86 126