Debian Codegraph @

gcc/config/loongarch/genopts/gen-evolution.awk

raw
1 #!/usr/bin/awk -f
2 #
3 # A simple script that generates loongarch-evolution.h
4 # from genopts/isa-evolution.in
5 #
6 # Copyright (C) 2021-2025 Free Software Foundation, Inc.
7 #
8 # This file is part of GCC.
9 #
10 # GCC is free software; you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free
12 # Software Foundation; either version 3, or (at your option) any later
13 # version.
14 #
15 # GCC is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 # License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with GCC; see the file COPYING3. If not see
22 # <http://www.gnu.org/licenses/>.
23
24 BEGIN {
25 # isa_version_major[]
26 # isa_version_minor[]
27 # cpucfg_word[]
28 # cpucfg_bit_in_word[]
29 # name_capitalized[]
30 # comment[]
31 }
32
33 {
34 cpucfg_word[NR] = $1
35 cpucfg_bit_in_word[NR] = $2
36 name[NR] = $3
37 gsub("-", "_", name[NR])
38 name_capitalized[NR] = toupper(name[NR])
39 split($4, isa_ver, "\\.")
40 isa_version_major[NR] = isa_ver[1]
41 isa_version_minor[NR] = isa_ver[2]
42
43 $1 = $2 = $3 = $4 = ""
44 sub (/^\s*/, "")
45 comment[NR] = $0
46 }
47
48 function copyright_header(from_year,to_year)
49 {
50 print " Copyright (C) " from_year "-" to_year \
51 " Free Software Foundation, Inc."
52 print ""
53 print "This file is part of GCC."
54 print ""
55 print "GCC is free software; you can redistribute it and/or modify"
56 print "it under the terms of the GNU General Public License as published by"
57 print "the Free Software Foundation; either version 3, or (at your option)"
58 print "any later version."
59 print ""
60 print "GCC is distributed in the hope that it will be useful,"
61 print "but WITHOUT ANY WARRANTY; without even the implied warranty of"
62 print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
63 print "GNU General Public License for more details."
64 print ""
65 print "You should have received a copy of the GNU General Public License"
66 print "along with GCC; see the file COPYING3. If not see"
67 print "<http://www.gnu.org/licenses/>."
68 }
69
70 function gen_cpucfg_map()
71 {
72 print "static constexpr struct {"
73 print " int cpucfg_word;"
74 print " unsigned int cpucfg_bit;"
75 print " HOST_WIDE_INT isa_evolution_bit;"
76 print "} cpucfg_map[] = {"
77
78 for (i = 1; i <= NR; i++)
79 printf (" { %d, 1u << %d, OPTION_MASK_ISA_%s },\n",
80 cpucfg_word[i], cpucfg_bit_in_word[i], name_capitalized[i])
81
82 print "};"
83 }
84
85 function gen_cpucfg_useful_idx()
86 {
87 split("0 1 2 16 17 18 19", init_useful_idx)
88
89 delete idx_bucket
90
91 for (i in init_useful_idx)
92 idx_bucket[init_useful_idx[i]] = 1
93 delete init_useful_idx
94
95 for (i in cpucfg_word)
96 idx_bucket[cpucfg_word[i]] = 1
97
98 delete idx_list
99 j = 1
100 for (i in idx_bucket)
101 idx_list[j++] = i+0
102 delete idx_bucket
103
104 for (i = 1; i < j; i++) {
105 t = i
106 for (k = i + 1; k < j; k++)
107 t = idx_list[k] < idx_list[t] ? k : t
108
109 k = idx_list[t]
110 idx_list[t] = idx_list[i]
111 idx_list[i] = k
112 }
113
114 print "static constexpr int cpucfg_useful_idx[] = {"
115 for (i = 1; i < j; i++)
116 printf(" %d,\n", idx_list[i])
117 print "};"
118
119 print ""
120
121 printf ("static constexpr int N_CPUCFG_WORDS = %d;\n",
122 idx_list[j - 1] + 1)
123
124 delete idx_list
125 }
126
127 function gen_evolution_decl()
128 {
129 print "/* ISA evolution features */"
130 print "enum {"
131
132 for (i = 1; i <= NR; i++)
133 print " EVO_" name_capitalized[i] " = " i - 1 ","
134
135 print " N_EVO_FEATURES = " NR
136 print "};"
137 print ""
138
139 print "/* Condition macros */"
140 for (i = 1; i <= NR; i++)
141 printf ("#define ISA_HAS_%s \\\n" \
142 " (la_target.isa.evolution & OPTION_MASK_ISA_%s)\n",
143 name_capitalized[i], name_capitalized[i])
144 print ""
145
146 print "/* Bitmasks on la_target.isa.evolution. */"
147 print "extern int la_evo_feature_masks[N_EVO_FEATURES];"
148 print ""
149 print "/* Builtin macro names for the evolution features. */"
150 print "extern const char* la_evo_macro_name[N_EVO_FEATURES];"
151 print ""
152 print "/* The ISA version where a specific feature is introduced. */"
153 print "extern int la_evo_version_major[N_EVO_FEATURES];"
154 print "extern int la_evo_version_minor[N_EVO_FEATURES];"
155 }
156
157 function gen_full_header()
158 {
159 print "/* Generated automatically by \"genstr\" from \"isa-evolution.in\"."
160 print " Please do not edit this file directly."
161 print ""
162
163 copyright_header(2023, 2025)
164
165 print "*/"
166 print ""
167
168 print "#ifndef LOONGARCH_EVOLUTION_H"
169 print "#define LOONGARCH_EVOLUTION_H"
170 print ""
171 print "#if !defined(IN_LIBGCC2) && !defined(IN_TARGET_LIBS) && !defined(IN_RTS)"
172 print ""
173 print "#include \"options.h\""
174 print ""
175
176 gen_cpucfg_map()
177
178 print ""
179
180 gen_cpucfg_useful_idx()
181
182 print ""
183
184 gen_evolution_decl()
185
186 print ""
187 print "#endif"
188 print ""
189 print "#endif /* LOONGARCH_EVOLUTION_H */"
190 }
191
192
193 function gen_full_source()
194 {
195 print "/* Generated automatically by \"genstr\" from \"isa-evolution.in\"."
196 print " Please do not edit this file directly."
197 print ""
198
199 copyright_header(2023, 2025)
200
201 print "*/"
202 print ""
203 print "#include \"config.h\""
204 print "#include \"system.h\""
205 print "#include \"coretypes.h\""
206 print "#include \"options.h\""
207 print ""
208 print "#include \"loongarch-evolution.h\""
209 print ""
210
211 print "int la_evo_feature_masks[] = {";
212 for (i = 1; i <= NR; i++)
213 print " OPTION_MASK_ISA_" name_capitalized[i] ","
214 print "};"
215 print ""
216
217 print "const char* la_evo_macro_name[] = {";
218 for (i = 1; i <= NR; i++)
219 print " \"__loongarch_" name[i] "\","
220 print "};"
221 print ""
222
223
224 print "int la_evo_version_major[] = {"
225 for (i = 1; i <= NR; i++)
226 print " " isa_version_major[i] ", /* " name_capitalized[i] " */"
227 print "};"
228 print ""
229
230 print "int la_evo_version_minor[] = {"
231 for (i = 1; i <= NR; i++)
232 print " " isa_version_minor[i] ", /* " name_capitalized[i] " */"
233 print "};"
234 }
235
236 END {
237 if (header_p)
238 gen_full_header()
239 else
240 gen_full_source()
241 }