1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Generate system call table for perf. Derived from
5# powerpc script.
6#
7# Author(s):  Ming Wang <wangming01@loongson.cn>
8# Author(s):  Huacai Chen <chenhuacai@loongson.cn>
9# Copyright (C) 2020-2023 Loongson Technology Corporation Limited
10
11gcc=$1
12hostcc=$2
13incpath=$3
14input=$4
15
16if ! test -r $input; then
17	echo "Could not read input file" >&2
18	exit 1
19fi
20
21create_table_from_c()
22{
23	local sc nr last_sc
24
25	create_table_exe=`mktemp ${TMPDIR:-/tmp}/create-table-XXXXXX`
26
27	{
28
29	cat <<-_EoHEADER
30		#include <stdio.h>
31		#include "$input"
32		int main(int argc, char *argv[])
33		{
34	_EoHEADER
35
36	while read sc nr; do
37		printf "%s\n" "	printf(\"\\t[%d] = \\\"$sc\\\",\\n\", $nr);"
38		last_sc=$nr
39	done
40
41	printf "%s\n" "	printf(\"#define SYSCALLTBL_LOONGARCH_MAX_ID %d\\n\", $last_sc);"
42	printf "}\n"
43
44	} | $hostcc -I $incpath/include/uapi -o $create_table_exe -x c -
45
46	$create_table_exe
47
48	rm -f $create_table_exe
49}
50
51create_table()
52{
53	echo "static const char *syscalltbl_loongarch[] = {"
54	create_table_from_c
55	echo "};"
56}
57
58$gcc -E -dM -x c  -I $incpath/include/uapi $input	       \
59	|sed -ne 's/^#define __NR_//p' \
60	|sort -t' ' -k2 -n \
61	|create_table
62