xref: /openbmc/linux/scripts/atomic/atomic-tbl.sh (revision 55fd7e02)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# helpers for dealing with atomics.tbl
4
5#meta_in(meta, match)
6meta_in()
7{
8	case "$1" in
9	[$2]) return 0;;
10	esac
11
12	return 1
13}
14
15#meta_has_ret(meta)
16meta_has_ret()
17{
18	meta_in "$1" "bBiIfFlR"
19}
20
21#meta_has_acquire(meta)
22meta_has_acquire()
23{
24	meta_in "$1" "BFIlR"
25}
26
27#meta_has_release(meta)
28meta_has_release()
29{
30	meta_in "$1" "BFIRs"
31}
32
33#meta_has_relaxed(meta)
34meta_has_relaxed()
35{
36	meta_in "$1" "BFIR"
37}
38
39#find_fallback_template(pfx, name, sfx, order)
40find_fallback_template()
41{
42	local pfx="$1"; shift
43	local name="$1"; shift
44	local sfx="$1"; shift
45	local order="$1"; shift
46
47	local base=""
48	local file=""
49
50	# We may have fallbacks for a specific case (e.g. read_acquire()), or
51	# an entire class, e.g. *inc*().
52	#
53	# Start at the most specific, and fall back to the most general. Once
54	# we find a specific fallback, don't bother looking for more.
55	for base in "${pfx}${name}${sfx}${order}" "${name}"; do
56		file="${ATOMICDIR}/fallbacks/${base}"
57
58		if [ -f "${file}" ]; then
59			printf "${file}"
60			break
61		fi
62	done
63}
64
65#gen_ret_type(meta, int)
66gen_ret_type() {
67	local meta="$1"; shift
68	local int="$1"; shift
69
70	case "${meta}" in
71	[sv]) printf "void";;
72	[bB]) printf "bool";;
73	[aiIfFlR]) printf "${int}";;
74	esac
75}
76
77#gen_ret_stmt(meta)
78gen_ret_stmt()
79{
80	if meta_has_ret "${meta}"; then
81		printf "return ";
82	fi
83}
84
85# gen_param_name(arg)
86gen_param_name()
87{
88	# strip off the leading 'c' for 'cv'
89	local name="${1#c}"
90	printf "${name#*:}"
91}
92
93# gen_param_type(arg, int, atomic)
94gen_param_type()
95{
96	local type="${1%%:*}"; shift
97	local int="$1"; shift
98	local atomic="$1"; shift
99
100	case "${type}" in
101	i) type="${int} ";;
102	p) type="${int} *";;
103	v) type="${atomic}_t *";;
104	cv) type="const ${atomic}_t *";;
105	esac
106
107	printf "${type}"
108}
109
110#gen_param(arg, int, atomic)
111gen_param()
112{
113	local arg="$1"; shift
114	local int="$1"; shift
115	local atomic="$1"; shift
116	local name="$(gen_param_name "${arg}")"
117	local type="$(gen_param_type "${arg}" "${int}" "${atomic}")"
118
119	printf "${type}${name}"
120}
121
122#gen_params(int, atomic, arg...)
123gen_params()
124{
125	local int="$1"; shift
126	local atomic="$1"; shift
127
128	while [ "$#" -gt 0 ]; do
129		gen_param "$1" "${int}" "${atomic}"
130		[ "$#" -gt 1 ] && printf ", "
131		shift;
132	done
133}
134
135#gen_args(arg...)
136gen_args()
137{
138	while [ "$#" -gt 0 ]; do
139		printf "$(gen_param_name "$1")"
140		[ "$#" -gt 1 ] && printf ", "
141		shift;
142	done
143}
144
145#gen_proto_order_variants(meta, pfx, name, sfx, ...)
146gen_proto_order_variants()
147{
148	local meta="$1"; shift
149	local pfx="$1"; shift
150	local name="$1"; shift
151	local sfx="$1"; shift
152
153	gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
154
155	if meta_has_acquire "${meta}"; then
156		gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
157	fi
158	if meta_has_release "${meta}"; then
159		gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
160	fi
161	if meta_has_relaxed "${meta}"; then
162		gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
163	fi
164}
165
166#gen_proto_variants(meta, name, ...)
167gen_proto_variants()
168{
169	local meta="$1"; shift
170	local name="$1"; shift
171	local pfx=""
172	local sfx=""
173
174	meta_in "${meta}" "fF" && pfx="fetch_"
175	meta_in "${meta}" "R" && sfx="_return"
176
177	gen_proto_order_variants "${meta}" "${pfx}" "${name}" "${sfx}" "$@"
178}
179
180#gen_proto(meta, ...)
181gen_proto() {
182	local meta="$1"; shift
183	for m in $(echo "${meta}" | grep -o .); do
184		gen_proto_variants "${m}" "$@"
185	done
186}
187