huangcm
2025-08-25 f350412dc55c15118d0a7925d1071877498e5e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/sh -eu
#
# Copyright (c) 2014-2015 Mike Frysinger <vapier@gentoo.org>
# Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
# Copyright (c) 2014-2018 The strace developers.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
usage()
{
   cat <<EOF
Usage: $0 <input> <output>
 
Generate xlat header files from <input> (a file or dir of files) and write
the generated headers to <output>.
EOF
   exit 1
}
 
cond_def()
{
   local line
   line="$1"; shift
 
   local val
   val="$(printf %s "$line" |
       LC_ALL=C sed -r -n 's/^([[:alpha:]_][[:alnum:]_]*).*$/\1/p')"
 
   local def
   def="$(printf %s "${line}" |
       sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
 
   if [ -n "$def" ]; then
       cat <<-EOF
       #if defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val)
       DIAG_PUSH_IGNORE_TAUTOLOGICAL_COMPARE
       static_assert(($val) == ($def), "$val != $def");
       DIAG_POP_IGNORE_TAUTOLOGICAL_COMPARE
       #else
       # define $val $def
       #endif
       EOF
   fi
}
 
print_xlat()
{
   local val
   val="$1"; shift
 
   [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
   if [ -z "${val_type-}" ]; then
       echo " XLAT(${val}),"
   else
       echo " XLAT_TYPE(${val_type}, ${val}),"
   fi
}
 
print_xlat_pair()
{
   local val str
   val="$1"; shift
   str="$1"; shift
 
   [ 1 = "$value_indexed" ] && printf " [%s] =" "${val}"
   if [ -z "${val_type-}" ]; then
       echo " XLAT_PAIR(${val}, \"${str}\"),"
   else
       echo " XLAT_TYPE_PAIR(${val_type}, ${val}, \"${str}\"),"
   fi
}
 
cond_xlat()
{
   local line val m def xlat
   line="$1"; shift
 
   val="$(printf %s "${line}" | sed -r -n 's/^([^[:space:]]+).*$/\1/p')"
   m="${val%%|*}"
   def="$(printf %s "${line}" |
          sed -r -n 's/^[^[:space:]]+[[:space:]]+([^[:space:]].*)$/\1/p')"
 
   if [ "${m}" = "${m#1<<}" ]; then
       xlat="$(print_xlat "${val}")"
   else
       xlat="$(print_xlat_pair "1ULL<<${val#1<<}" "${val}")"
       m="${m#1<<}"
   fi
 
   if [ -z "${def}" ]; then
       cat <<-EOF
       #if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})
        ${xlat}
       #endif
       EOF
   else
       echo "$xlat"
   fi
}
 
gen_header()
{
   local input="$1" output="$2" name="$3"
   echo "generating ${output}"
   (
   local defs="${0%/*}/../defs.h"
   local mpers="${0%/*}/../mpers_xlat.h"
   local decl="extern const struct xlat ${name}[];"
   local in_defs= in_mpers=
 
   value_indexed=0
 
   if grep -F -x "$decl" "$defs" > /dev/null; then
       in_defs=1
   elif grep -F -x "$decl" "$mpers" > /dev/null; then
       in_mpers=1
   fi
 
   cat <<-EOF
   /* Generated by $0 from $1; do not edit. */
 
   #include "gcc_compat.h"
   #include "static_assert.h"
 
   EOF
 
   local unconditional= line
   # 1st pass: output directives.
   while read line; do
       LC_COLLATE=C
       line=$(printf "%s" "$line" | \
           sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
 
       case $line in
       '#stop')
           exit 0
           ;;
       '#conditional')
           unconditional=
           ;;
       '#unconditional')
           unconditional=1
           ;;
       '#val_type '*)
           # to be processed during 2nd pass
           ;;
       '#value_indexed')
           value_indexed=1
           ;;
       '#'*)
           echo "${line}"
           ;;
       [A-Z_]*)
           [ -n "$unconditional" ] ||
               cond_def "$line"
           ;;
       esac
   done < "$input"
 
   cat <<-EOF
 
       #ifndef XLAT_MACROS_ONLY
 
   EOF
 
   if [ -n "$in_defs" ]; then
       cat <<-EOF
           # ifndef IN_MPERS
 
       EOF
   elif [ -n "$in_mpers" ]; then
       cat <<-EOF
           # ifdef IN_MPERS
 
           ${decl}
 
           # else
 
           #  if !(defined HAVE_M32_MPERS || defined HAVE_MX32_MPERS)
           static
           #  endif
       EOF
   else
       cat <<-EOF
           # ifdef IN_MPERS
 
           #  error static const struct xlat ${name} in mpers mode
 
           # else
 
           static
       EOF
   fi
 
   echo "const struct xlat ${name}[] = {"
 
   unconditional= val_type=
   # 2nd pass: output everything.
   while read line; do
       LC_COLLATE=C
       line=$(printf "%s" "$line" | \
           sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
 
       case ${line} in
       '#conditional')
           unconditional=
           ;;
       '#unconditional')
           unconditional=1
           ;;
       '#value_indexed')
           ;;
       '#val_type '*)
           val_type="${line#\#val_type }"
           ;;
       [A-Z_]*)    # symbolic constants
           if [ -n "${unconditional}" ]; then
               print_xlat "${line}"
           else
               cond_xlat "${line}"
           fi
           ;;
       '1<<'[A-Z_]*)    # symbolic constants with shift
           if [ -n "${unconditional}" ]; then
               print_xlat_pair "1ULL<<${line#1<<}" "${line}"
           else
               cond_xlat "${line}"
           fi
           ;;
       [0-9]*)    # numeric constants
           print_xlat "${line}"
           ;;
       *)    # verbatim lines
           echo "${line}"
           ;;
       esac
   done < "${input}"
   echo ' XLAT_END'
 
   cat <<-EOF
       };
 
       # endif /* !IN_MPERS */
 
       #endif /* !XLAT_MACROS_ONLY */
   EOF
   ) >"${output}"
}
 
gen_make()
{
   local output="$1"
   local name
   shift
   echo "generating ${output}"
   (
       printf "XLAT_INPUT_FILES = "
       printf 'xlat/%s.in ' "$@"
       echo
       printf "XLAT_HEADER_FILES = "
       printf 'xlat/%s.h ' "$@"
       echo
       for name; do
           printf '$(top_srcdir)/xlat/%s.h: $(top_srcdir)/xlat/%s.in $(top_srcdir)/xlat/gen.sh\n' \
               "${name}" "${name}"
           echo '    $(AM_V_GEN)$(top_srcdir)/xlat/gen.sh $< $@'
       done
   ) >"${output}"
}
 
gen_git()
{
   local output="$1"
   shift
   echo "generating ${output}"
   (
       printf '/%s\n' .gitignore Makemodule.am
       printf '/%s.h\n' "$@"
   ) >"${output}"
}
 
main()
{
   case $# in
   0) set -- "${0%/*}" "${0%/*}" ;;
   2) ;;
   *) usage ;;
   esac
 
   local input="$1"
   local output="$2"
   local name
   local jobs=0
   local ncpus="$(getconf _NPROCESSORS_ONLN)"
   local pids=
   [ "${ncpus}" -ge 1 ] ||
       ncpus=1
 
   if [ -d "${input}" ]; then
       local f names=
       for f in "${input}"/*.in; do
           [ -f "${f}" ] || continue
           name=${f##*/}
           name=${name%.in}
           gen_header "${f}" "${output}/${name}.h" "${name}" &
           pids="$pids $!"
           names="${names} ${name}"
           : $(( jobs += 1 ))
           if [ "${jobs}" -gt "$(( ncpus * 2 ))" ]; then
               read wait_pid rest
               pids="$rest"
               wait -n 2>/dev/null || wait "$wait_pid"
               : $(( jobs -= 1 ))
           fi <<- EOF
           $pids
           EOF
       done
       gen_git "${output}/.gitignore" ${names} &
       gen_make "${output}/Makemodule.am" ${names} &
       wait
   else
       name=${input##*/}
       name=${name%.in}
       gen_header "${input}" "${output}" "${name}"
   fi
}
 
main "$@"