summaryrefslogtreecommitdiff
path: root/src/xlat/gen.sh
blob: f7f32c604d1ab1e20503e0831509180883edb39c (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/sh -efu
#
# Copyright (c) 2014-2015 Mike Frysinger <vapier@gentoo.org>
# Copyright (c) 2014-2015 Dmitry V. Levin <ldv@strace.io>
# Copyright (c) 2014-2021 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-or-later

export LC_ALL=C

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
}

print_m4_record()
{
	local val output
	val="$1"; shift
	output="$1"; shift
	[ -n "$output" ] || return 0

	if [ "$first_enum" = 1 ]; then
		printf '%s' "$val"
	else
		printf ',\n%s' "$val"
	fi >> "$output"

	first_enum=0
}

cond_def()
{
	local line
	line="$1"; shift
	local xlat_type
	xlat_type="$1"; shift

	local val
	val="${line%%[!A-Za-z0-9_]*}"

	local t def=
	t="${line#*[	 ]}"
	if [ "$line" != "$t" ]; then
		while [ "$def" != "$t" ]; do
			def="$t"
			t="${t##[	 ]}"
		done
	fi

	if [ -n "$def" ]; then
		[ -n "$unconditional" ] ||
			printf '%s\n' \
				"#if defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val)"
		printf '%s\n' \
			"DIAG_PUSH_IGNORE_TAUTOLOGICAL_COMPARE" \
			"static_assert(($val) == ($def), \"$val != $def\");" \
			"DIAG_POP_IGNORE_TAUTOLOGICAL_COMPARE"
		[ -n "$unconditional" ] ||
			printf '%s\n' \
				"#else" \
				"# define $val $def" \
				"#endif"
	fi

	if [ XT_SORTED = "$xlat_type" ]; then
		[ -n "$unconditional" ] ||
			printf '%s\n' \
				"#if defined($val) || (defined(HAVE_DECL_$val) && HAVE_DECL_$val)"
		check_sort_order "$val"
		[ -n "$unconditional" ] ||
			printf '%s\n' "#endif"
	fi

}

check_sort_order()
{
	local val
	val="$1"; shift

	cat <<-EOF
		#if defined XLAT_PREV_VAL
		static_assert((unsigned long long) ($val)
			      > (unsigned long long) (XLAT_PREV_VAL),
			      "Incorrect order in #sorted xlat: $val"
			      " is not larger than the previous value");
		#endif
		#undef XLAT_PREV_VAL
		#define XLAT_PREV_VAL ($val)
	EOF
}

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

	echo " #define XLAT_VAL_$xlat_flag_cnt ((${val_type:-unsigned}) (${val}))"
	echo " #define XLAT_STR_$xlat_flag_cnt STRINGIFY(${val})"
	xlat_flag_cnt=$((xlat_flag_cnt + 1))

	[ -z "$enum" ] || print_m4_record "$val" "$output_m4"
}

print_xlat_pair()
{
	local val str macro
	val="$1"; shift
	str="$1"; shift
	macro="$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

	echo " #define XLAT_VAL_$xlat_flag_cnt ((${val_type:-unsigned}) (${val}))"
	echo " #define XLAT_STR_$xlat_flag_cnt \"${str}\""
	xlat_flag_cnt=$((xlat_flag_cnt + 1))

	[ -z "$enum" ] || print_m4_record "$macro" "$output_m4"
}

cond_xlat()
{
	echo "$1" | {
		local val def m xlat

		read val def

		m="${val%%|*}"

		if [ "${m}" = "${m#1<<}" ]; then
			xlat="$(print_xlat "${val}")"
		else
			m="${m#1<<}"
			xlat="$(print_xlat_pair "1ULL<<${val#1<<}" "${val}" "$m")"
		fi

		if [ -z "${def}${unconditional}" ]; then
			printf '%s\n' \
				"#if defined(${m}) || (defined(HAVE_DECL_${m}) && HAVE_DECL_${m})" \
				" ${xlat}" \
				"#endif"
		else
			printf '%s\n' "$xlat"
		fi
	}

	# Since we're calling print_xlat/print_xlat_pair in subprocess
	xlat_flag_cnt=$((xlat_flag_cnt + 1))
	first_enum=0
}

gen_header()
{
	local input="$1" output="$2" name="$3" output_m4="$4"

	exec 3>&1

	echo "generating ${output}" >&2
	(
	local defs="${0%/*}/../defs.h"
	local mpers="${0%/*}/../mpers_xlat.h"
	local decl="extern const struct xlat ${name}[];"
	local in_defs= in_mpers=
	local xlat_type="XT_NORMAL"
	local includes=""
	local enum=""

	first_enum=1
	value_indexed=0
	xlat_flag_cnt=0

	if grep -F -q -x "$decl" "$defs"; then
		in_defs=1
	elif grep -F -q -x "$decl" "$mpers"; 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 -r line; do
		case "$line" in
			*/\**)
			line=$(printf "%s" "$line" |
				sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
			;;
		esac

		case $line in
		'#conditional')
			unconditional=
			;;
		'#unconditional')
			unconditional=1
			;;
		'#val_type '*)
			# to be processed during 2nd pass
			;;
		'#sorted'|'#sorted '*)
			xlat_type="XT_SORTED"
			;;
		'#value_indexed')
			value_indexed=1
			xlat_type="XT_INDEXED"
			;;
		'#enum')
			[ -z "$output_m4" ] || enum=1
			;;
		'#include '*)
			includes="${includes} ${line###include }"
			;;
		'#'*)
			echo "${line}"
			;;
		[A-Z_]*)
			cond_def "$line" "$xlat_type"
			;;
		'1<<'[A-Z_]*)	# symbolic constants with shift
			[ XT_SORTED != "$xlat_type" ] ||
				check_sort_order "1ULL<<${line#1<<}"
			;;
		[0-9]*)	# numeric constants
			[ XT_SORTED != "$xlat_type" ] ||
				check_sort_order "${line}"
			;;
		esac
	done < "$input"

	cat <<-EOF
		#undef XLAT_PREV_VAL

		#ifndef XLAT_MACROS_ONLY

	EOF

	[ -z "$unconditional" -o -z "$enum" ] || {
		echo "ignoring #enum due to #unconditional for ${input}" >&2
		enum=""
	}

	[ 1 != "$enum" ] || (
		echo "generating ${output_m4}" >&2
		printf 'dnl Generated by %s from %s; do not edit.\n' \
			"$0" "$input"
		printf 'AC_DEFUN([st_CHECK_ENUMS_%s],[\n' "$name"
		printf 'AC_CHECK_DECLS(m4_normalize([\n'
	) > "${output_m4}"

	if [ -n "$in_defs" ]; then
		cat <<-EOF
			# ifndef IN_MPERS

		EOF
	elif [ -n "$in_mpers" ]; then
		cat <<-EOF
			# ifdef IN_MPERS

			${decl}

			# else

		EOF
	else
		cat <<-EOF
			# ifdef IN_MPERS

			#  error static const struct xlat ${name} in mpers mode

			# else

		EOF
	fi

	echo "DIAG_PUSH_IGNORE_TAUTOLOGICAL_CONSTANT_COMPARE"
	echo "static const struct xlat_data ${name}_xdata[] = {"

	unconditional= val_type=
	# 2nd pass: output everything.
	while read -r line; do
		case "$line" in
			*/\**)
			line=$(printf "%s" "$line" |
				sed "s|[[:space:]]*/\*.*\*/[[:space:]]*||")
			;;
		esac

		case ${line} in
		'#conditional')
			unconditional=
			;;
		'#unconditional')
			unconditional=1
			;;
		'#sorted'|'#sorted '*)
			;;
		'#value_indexed')
			;;
		'#enum')
			;;
		'#include '*)
			;;
		'#val_type '*)
			val_type="${line#\#val_type }"
			;;
		[A-Z_!]*)	# symbolic constants
			cond_xlat "${line}"
			;;
		'1<<'[A-Z_]*)	# symbolic constants with shift
			cond_xlat "${line}"
			;;
		[0-9]*)	# numeric constants
			print_xlat "${line}"
			;;
		*)	# verbatim lines
			echo "${line}"
			;;
		esac
	done < "${input}"
	echo '};'

	[ 1 != "$enum" ] || (
		printf '\n]),,, [\n'
		[ -z "$includes" ] || printf '#include %s\n' $includes
		printf '])])])\n'

		# Providing macro name to main
		echo "st_CHECK_ENUMS_${name}" >&3
	) >> "${output_m4}"

	if [ -n "$in_defs" ]; then
		:
	elif [ -n "$in_mpers" ]; then
		cat <<-EOF
			#  if !(defined HAVE_M32_MPERS || defined HAVE_MX32_MPERS)
			static
			#  endif
		EOF
	else
		cat <<-EOF
			static
		EOF
	fi

	cat <<-EOF
		const struct xlat ${name}[1] = { {
			 .data = ${name}_xdata,
			 .size = ARRAY_SIZE(${name}_xdata),
			 .type = ${xlat_type},
	EOF

	echo " .flags_mask = 0"
	for i in $(seq 0 "$((xlat_flag_cnt - 1))"); do
		cat <<-EOF
			#  ifdef XLAT_VAL_${i}
			  | XLAT_VAL_${i}
			#  endif
		EOF
	done
	echo "  ,"

	echo " .flags_strsz = 0"
	for i in $(seq 0 "$((xlat_flag_cnt - 1))"); do
		cat <<-EOF
			#  ifdef XLAT_STR_${i}
			  + sizeof(XLAT_STR_${i})
			#  endif
		EOF
	done
	echo "  ,"

	cat <<-EOF
		} };
		DIAG_POP_IGNORE_TAUTOLOGICAL_CONSTANT_COMPARE

	EOF

	for i in $(seq 0 "$((xlat_flag_cnt - 1))"); do
		cat <<-EOF
			#  undef XLAT_STR_${i}
			#  undef XLAT_VAL_${i}
		EOF
	done

	cat <<-EOF
		# endif /* !IN_MPERS */

		#endif /* !XLAT_MACROS_ONLY */
	EOF
	) >"${output}"

	exec 3>&-
}

gen_make()
{
	local output="$1"
	local name
	shift
	echo "generating ${output}" >&2
	(
		printf "XLAT_INPUT_FILES = "
		printf 'xlat/%s.in ' "$@"
		echo
		printf "XLAT_HEADER_FILES = "
		printf 'xlat/%s.h ' "$@"
		echo
		for name; do
			printf '$(top_srcdir)/src/xlat/%s.h: $(top_srcdir)/src/xlat/%s.in $(top_srcdir)/src/xlat/gen.sh\n' \
				"${name}" "${name}"
			echo '	$(AM_V_GEN)$(top_srcdir)/src/xlat/gen.sh $< $@'
		done
	) >"${output}"
}

gen_git()
{
	local output="$1"
	shift
	echo "generating ${output}" >&2
	(
		printf '/%s\n' .gitignore Makemodule.am st_check_enums.m4
		printf '/%s.h\n' "$@"
		printf '/%s.m4\n' "$@"
	) >"${output}"
}

gen_m4_entry()
{
	local output
	output="$1"; shift

	echo "generating $output" >&2
	{
		printf 'AC_DEFUN([st_CHECK_ENUMS],[\n'
		while read fun; do
			printf '\t%s\n' "$fun"
		done
		printf '])\n'
	} >"$output"
}

main()
{
	case $# in
	0) set -- "${0%/*}" "${0%/*}" ;;
	2) ;;
	3) ;;
	*) usage ;;
	esac

	local input="$1"
	local output="$2"
	local output_m4="${3:-}"
	local name
	local jobs=0
	local ncpus="$(getconf _NPROCESSORS_ONLN)"
	local pids=
	[ "${ncpus}" -ge 1 ] ||
		ncpus=1

	if [ -d "${input}" ]; then
		(
		local f names=
		set +f
		set -- "${input}"/*.in
		set -f
		for f; do
			[ -f "${f}" ] || continue
			name=${f##*/}
			name=${name%.in}
			gen_header "${f}" "${output}/${name}.h" "${name}" \
				"${output}/${name}.m4" &
			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
		) | sort | gen_m4_entry "${output}/st_check_enums.m4"
	else
		name=${input##*/}
		name=${name%.in}
		gen_header "${input}" "${output}" "${name}" "${output_m4}" \
			> /dev/null
	fi
}

main "$@"