1#  Copyright (C) 2003,2004,2005,2006 Free Software Foundation, Inc.
2#  Contributed by Kelley Cook, June 2004.
3#  Original code from Neil Booth, May 2003.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the
7# Free Software Foundation; either version 2, or (at your option) any
8# later version.
9# 
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14# 
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19# This Awk script reads in the option records generated from 
20# opt-gather.awk, combines the flags of duplicate options and generates a
21# C header file.
22#
23# This program uses functions from opt-functions.awk
24# Usage: awk -f opt-functions.awk -f opth-gen.awk < inputfile > options.h
25
26BEGIN {
27	n_opts = 0
28	n_langs = 0
29	n_extra_masks = 0
30	quote = "\042"
31	comma = ","
32	FS=SUBSEP
33}
34
35# Collect the text and flags of each option into an array
36	{
37		if ($1 == "Language") {
38			langs[n_langs] = $2
39			n_langs++;
40		}
41		else {
42			name = opt_args("Mask", $1)
43			if (name == "") {
44				opts[n_opts]  = $1
45				flags[n_opts] = $2
46				help[n_opts]  = $3
47				n_opts++;
48			}
49			else {
50				extra_masks[n_extra_masks++] = name
51			}
52		}
53	}
54
55# Dump out an enumeration into a .h file.
56# Combine the flags of duplicate options.
57END {
58print "/* This file is auto-generated by opts.sh.  */"
59print ""
60print "#ifndef OPTIONS_H"
61print "#define OPTIONS_H"
62print ""
63print "extern int target_flags;"
64print ""
65
66for (i = 0; i < n_opts; i++) {
67	name = var_name(flags[i]);
68	if (name == "")
69		continue;
70
71	print "extern " var_type(flags[i]) name ";"
72}
73print ""
74
75for (i = 0; i < n_opts; i++) {
76	name = opt_args("Mask", flags[i])
77	vname = var_name(flags[i])
78	mask = "MASK_"
79	if (vname != "") {
80		mask = "OPTION_MASK_"
81	}
82	if (name != "" && !flag_set_p("MaskExists", flags[i]))
83		print "#define " mask name " (1 << " masknum[vname]++ ")"
84}
85for (i = 0; i < n_extra_masks; i++) {
86	print "#define MASK_" extra_masks[i] " (1 << " masknum[""]++ ")"
87}
88
89for (var in masknum) {
90	if (masknum[var] > 32) {
91		if (var == "")
92			print "#error too many target masks"
93		else
94			print "#error too many masks for " var
95	}
96}
97print ""
98
99for (i = 0; i < n_opts; i++) {
100	name = opt_args("Mask", flags[i])
101	vname = var_name(flags[i])
102	macro = "OPTION_"
103	mask = "OPTION_MASK_"
104	if (vname == "") {
105		vname = "target_flags"
106		macro = "TARGET_"
107		mask = "MASK_"
108	}
109	if (name != "" && !flag_set_p("MaskExists", flags[i]))
110		print "#define " macro name \
111		      " ((" vname " & " mask name ") != 0)"
112}
113for (i = 0; i < n_extra_masks; i++) {
114	print "#define TARGET_" extra_masks[i] \
115	      " ((target_flags & MASK_" extra_masks[i] ") != 0)"
116}
117print ""
118
119for (i = 0; i < n_opts; i++) {
120	opt = opt_args("InverseMask", flags[i])
121	if (opt ~ ",") {
122		vname = var_name(flags[i])
123		macro = "OPTION_"
124		mask = "OPTION_MASK_"
125		if (vname == "") {
126			vname = "target_flags"
127			macro = "TARGET_"
128			mask = "MASK_"
129		}
130		print "#define " macro nth_arg(1, opt) \
131		      " ((" vname " & " mask nth_arg(0, opt) ") == 0)"
132	}
133}
134print ""
135
136for (i = 0; i < n_langs; i++) {
137	macros[i] = "CL_" langs[i]
138	gsub( "[^A-Za-z0-9_]", "X", macros[i] )
139	s = substr("            ", length (macros[i]))
140	print "#define " macros[i] s " (1 << " i ")"
141    }
142
143print ""
144print "enum opt_code"
145print "{"
146	
147for (i = 0; i < n_opts; i++)
148	back_chain[i] = "N_OPTS";
149
150for (i = 0; i < n_opts; i++) {
151	# Combine the flags of identical switches.  Switches
152	# appear many times if they are handled by many front
153	# ends, for example.
154	while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
155		flags[i + 1] = flags[i] " " flags[i + 1];
156		i++;
157	}
158
159	len = length (opts[i]);
160	enum = "OPT_" opts[i]
161	if (opts[i] == "finline-limit=")
162		enum = enum "eq"
163	gsub ("[^A-Za-z0-9]", "_", enum)
164
165	# If this switch takes joined arguments, back-chain all
166	# subsequent switches to it for which it is a prefix.  If
167	# a later switch S is a longer prefix of a switch T, T
168	# will be back-chained to S in a later iteration of this
169	# for() loop, which is what we want.
170	if (flag_set_p("Joined.*", flags[i])) {
171		for (j = i + 1; j < n_opts; j++) {
172			if (substr (opts[j], 1, len) != opts[i])
173				break;
174			back_chain[j] = enum;
175		}
176	}
177
178	s = substr("                                     ", length (opts[i]))
179	if (i + 1 == n_opts)
180		comma = ""
181
182	if (help[i] == "")
183		hlp = "0"
184	else
185		hlp = "N_(\"" help[i] "\")";
186
187	print "  " enum "," s "/* -" opts[i] " */"
188}
189
190print "  N_OPTS"
191print "};"
192print ""
193print "#endif /* OPTIONS_H */"
194}
195