#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Generate C file mapping errno codes to errno names.
#
# Copyright IBM Corp. 2018
# Author(s):  Hendrik Brueckner <brueckner@linux.vnet.ibm.com>

gcc="$1"
toolsdir="$2"
include_path="-I$toolsdir/include/uapi"

arch_string()
{
	echo "$1" |sed -e 'y/- /__/' |tr '[[:upper:]]' '[[:lower:]]'
}

asm_errno_file()
{
	arch="$1"

	header="$toolsdir/arch/$arch/include/uapi/asm/errno.h"
	if test -r "$header"; then
		echo "$header"
	else
		echo "$toolsdir/include/uapi/asm-generic/errno.h"
	fi
}

create_errno_lookup_func()
{
	arch=$(arch_string "$1")

	printf "static const char *errno_to_name__%s(int err)\n{\n\tswitch (err) {\n" $arch

	while read name nr; do
		printf '\tcase %d: return "%s";\n' $nr $name
	done

	printf '\tdefault: return "(unknown)";\n\t}\n}\n'
}

process_arch()
{
	arch="$1"
	asm_errno=$(asm_errno_file "$arch")

	$gcc $CFLAGS $include_path -E -dM -x c $asm_errno \
		|grep -hE '^#define[[:blank:]]+(E[A-Z0-9]+)[[:blank:]]+([[:digit:]]+).*' \
		|awk '{ print $2","$3; }' \
		|sort -t, -k2 -nu \
		|IFS=, create_errno_lookup_func "$arch"
}

arch_to_e_machine()
{
	case "$1" in
	alpha)      printf '\tcase EM_ALPHA:\n' ;;
	arc)        printf '\tcase EM_ARC:\n' ;;
	arm)        printf '\tcase EM_ARM:\n' ;;
	arm64)      printf '\tcase EM_AARCH64:\n' ;;
	csky)       printf '\tcase EM_CSKY:\n' ;;
	hexagon)    printf '\tcase EM_HEXAGON:\n' ;;
	loongarch)  printf '\tcase EM_LOONGARCH:\n' ;;
	microblaze) printf '\tcase EM_MICROBLAZE:\n' ;;
	mips)       printf '\tcase EM_MIPS:\n' ;;
	parisc)     printf '\tcase EM_PARISC:\n' ;;
	powerpc)    printf '\tcase EM_PPC:\n\tcase EM_PPC64:\n' ;;
	riscv)      printf '\tcase EM_RISCV:\n' ;;
	s390)       printf '\tcase EM_S390:\n' ;;
	sh)         printf '\tcase EM_SH:\n' ;;
	sparc)      printf '\tcase EM_SPARC:\n\tcase EM_SPARC32PLUS:\n\tcase EM_SPARCV9:\n' ;;
	x86)        printf '\tcase EM_386:\n\tcase EM_X86_64:\n' ;;
	xtensa)     printf '\tcase EM_XTENSA:\n' ;;
	esac
}

create_arch_errno_table_func()
{
	archlist="$1"
	default="$2"

	printf 'const char *arch_syscalls__strerrno(uint16_t e_machine, int err);\n\n'
	printf '__attribute__((unused)) const char *\n'
	printf 'arch_syscalls__strerrno(uint16_t e_machine, int err)\n'
	printf '{\n'
	printf '\tswitch (e_machine) {\n'
	for arch in $archlist; do
		arch_str=$(arch_string "$arch")
		ems=$(arch_to_e_machine "$arch_str")
		if [ -n "$ems" ]; then
			printf '%s\n' "$ems"
			printf '\t\treturn errno_to_name__%s(err);\n' "$arch_str"
		fi
	done
	arch_str=$(arch_string "$default")
	printf '\tdefault:\n\t\treturn errno_to_name__%s(err);\n' "$arch_str"
	printf '\t}\n'
	printf '}\n'
}

cat <<EoHEADER
/* SPDX-License-Identifier: GPL-2.0 */

#include <string.h>
#include <stdint.h>
#include <elf.h>

#ifndef EM_AARCH64
#define EM_AARCH64	183
#endif

#ifndef EM_CSKY
#define EM_CSKY		252
#endif

#ifndef EM_LOONGARCH
#define EM_LOONGARCH	258
#endif

EoHEADER

# Create list of architectures that have a specific errno.h.
archlist=""
for f in $toolsdir/arch/*/include/uapi/asm/errno.h; do
	d=${f%/include/uapi/asm/errno.h}
	arch="${d##*/}"
	test -f $toolsdir/arch/$arch/include/uapi/asm/errno.h && archlist="$archlist $arch"
done

for arch in generic $archlist; do
	process_arch "$arch"
done
create_arch_errno_table_func "$archlist" "generic"