diff 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
line wrap: on
line diff
--- a/CbC-scripts/make_headers.py2	Thu Oct 29 18:19:02 2009 +0900
+++ b/CbC-scripts/make_headers.py2	Tue Nov 10 16:34:29 2009 +0900
@@ -3,15 +3,19 @@
 
 import sys
 import re
+import getopt
 
 reserved_words = [ "if", "for", "switch", "return", "while", "else", ]
 
+PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^\{/;]*)\)\s*\{"
 #PATTERN = r"([a-zA-Z_]\w*)\s+([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{"
 #PATTERN = r"((?:[a-zA-Z_]\w*)\s+)+?([a-zA-Z_]\w*)\s*\(([^;]*)\)\s*\{"
-PATTERN = r"([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^/;]*)\)\s*\{"
 # TODO: 関数パラメータ内にコメントがあると正しく動かない!
 PROG = re.compile(PATTERN, re.S)
 
+omit_static=False
+add_extern=""
+
 def truncate_comments(data):
 	pass
 
@@ -33,6 +37,7 @@
 		truncate_comments(data)
 	except IOError:
 		print "cannot read file %s" % file
+		return None
 
 	# find all matched strings.
 	# moiter is iterator of MatchObject.
@@ -66,21 +71,56 @@
 			decl[key] = value.replace("\n"," ").replace("\t"," ")
 
 	print "/* defined in file %s at offset %d  */" % (file,decl["offset"])
-	print "%s %s (%s);" % (decl["type"],decl["name"],decl["parms"])
+	print "%s%s %s (%s);" % (add_extern, decl["type"],decl["name"],decl["parms"])
 	print ""
 
+def getoptions():
+	global omit_static, add_extern
+
+	try:
+		opts, args = getopt.getopt(sys.argv[1:], 'se', [ 'omit-static', 'add-extern' ])
+	except getopt.GetoptError:
+		print(err)
+		usage()
+		sys.exit(2)
+
+	for opt,a in opts:
+		if opt in ("-s", "--omit-static"):
+			omit_static=True
+		elif opt in ("-e", "--add-extern"):
+			add_extern="extern "
+		else:
+			print("unhandled option {0}".format(opt))
+			usage()
+	
+	return args
+
+def usage():
+	print( """\
+Usage: {0:s} OPION... [FILE]...
+OPTIONS:
+	-s, --omit-static	omit static functions
+	-e, --add-extern	add extern to all function declarations
+	""".format(sys.argv[0]))
+
 def main():
-	for file in sys.argv[1:]:
+	# option handling.
+	args = getoptions()
+
+	for file in args:
+		# read function declaration from each file.
 		decls = read_decls(file)
-
 		if decls==None or len(decls)==0:
+			# no function found.
 			print "%s have no function definition!" % file
 			continue
+
 		for decl in decls:
+			if omit_static and 0 <= decl["type"].find("static"):
+				# static function is ignored.
+				continue
 			#debug_print(decl)
 			format_print(decl, file)
-		#print decls[0]
-
 
 main()