2 # SPDX-License-Identifier: GPL-2.0
3 # helpers for dealing with atomics.tbl
18 meta_in "$1" "bBiIfFlR"
21 #meta_has_acquire(meta)
27 #meta_has_release(meta)
33 #meta_has_relaxed(meta)
39 #meta_is_implicitly_relaxed(meta)
40 meta_is_implicitly_relaxed()
45 #find_template(tmpltype, pfx, name, sfx, order)
48 local tmpltype="$1"; shift
50 local name="$1"; shift
52 local order="$1"; shift
57 # We may have fallbacks for a specific case (e.g. read_acquire()), or
58 # an entire class, e.g. *inc*().
60 # Start at the most specific, and fall back to the most general. Once
61 # we find a specific fallback, don't bother looking for more.
62 for base in "${pfx}${name}${sfx}${order}" "${pfx}${name}${sfx}" "${name}"; do
63 file="${ATOMICDIR}/${tmpltype}/${base}"
65 if [ -f "${file}" ]; then
72 #find_fallback_template(pfx, name, sfx, order)
73 find_fallback_template()
75 find_template "fallbacks" "$@"
78 #find_kerneldoc_template(pfx, name, sfx, order)
79 find_kerneldoc_template()
81 find_template "kerneldoc" "$@"
84 #gen_ret_type(meta, int)
86 local meta="$1"; shift
92 [aiIfFlR]) printf "${int}";;
99 if meta_has_ret "${meta}"; then
104 # gen_param_name(arg)
107 # strip off the leading 'c' for 'cv'
112 # gen_param_type(arg, int, atomic)
115 local type="${1%%:*}"; shift
116 local int="$1"; shift
117 local atomic="$1"; shift
122 v) type="${atomic}_t *";;
123 cv) type="const ${atomic}_t *";;
129 #gen_param(arg, int, atomic)
132 local arg="$1"; shift
133 local int="$1"; shift
134 local atomic="$1"; shift
135 local name="$(gen_param_name "${arg}")"
136 local type="$(gen_param_type "${arg}" "${int}" "${atomic}")"
138 printf "${type}${name}"
141 #gen_params(int, atomic, arg...)
144 local int="$1"; shift
145 local atomic="$1"; shift
147 while [ "$#" -gt 0 ]; do
148 gen_param "$1" "${int}" "${atomic}"
149 [ "$#" -gt 1 ] && printf ", "
157 while [ "$#" -gt 0 ]; do
158 printf "$(gen_param_name "$1")"
159 [ "$#" -gt 1 ] && printf ", "
164 #gen_desc_return(meta)
167 local meta="$1"; shift
171 printf "Return: Nothing."
174 printf "Return: The original value of @v."
177 printf "Return: The updated value of @v."
180 printf "Return: The value of @v."
185 #gen_template_kerneldoc(template, class, meta, pfx, name, sfx, order, atomic, int, args...)
186 gen_template_kerneldoc()
188 local template="$1"; shift
189 local class="$1"; shift
190 local meta="$1"; shift
191 local pfx="$1"; shift
192 local name="$1"; shift
193 local sfx="$1"; shift
194 local order="$1"; shift
195 local atomic="$1"; shift
196 local int="$1"; shift
198 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
200 local ret="$(gen_ret_type "${meta}" "${int}")"
201 local retstmt="$(gen_ret_stmt "${meta}")"
202 local params="$(gen_params "${int}" "${atomic}" "$@")"
203 local args="$(gen_args "$@")"
205 local desc_instrumentation=""
208 if [ ! -z "${order}" ]; then
209 desc_order="${order##_}"
210 elif meta_is_implicitly_relaxed "${meta}"; then
216 if [ -z "${class}" ]; then
217 desc_noinstr="Unsafe to use in noinstr code; use raw_${atomicname}() there."
219 desc_noinstr="Safe to use in noinstr code; prefer ${atomicname}() elsewhere."
222 desc_return="$(gen_desc_return "${meta}")"
227 #gen_kerneldoc(class, meta, pfx, name, sfx, order, atomic, int, args...)
230 local class="$1"; shift
231 local meta="$1"; shift
232 local pfx="$1"; shift
233 local name="$1"; shift
234 local sfx="$1"; shift
235 local order="$1"; shift
237 local atomicname="${atomic}_${pfx}${name}${sfx}${order}"
239 local tmpl="$(find_kerneldoc_template "${pfx}" "${name}" "${sfx}" "${order}")"
240 if [ -z "${tmpl}" ]; then
242 printf " * No kerneldoc available for ${class}${atomicname}\n"
245 gen_template_kerneldoc "${tmpl}" "${class}" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "$@"
249 #gen_proto_order_variants(meta, pfx, name, sfx, ...)
250 gen_proto_order_variants()
252 local meta="$1"; shift
253 local pfx="$1"; shift
254 local name="$1"; shift
255 local sfx="$1"; shift
257 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
259 if meta_has_acquire "${meta}"; then
260 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
262 if meta_has_release "${meta}"; then
263 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
265 if meta_has_relaxed "${meta}"; then
266 gen_proto_order_variant "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
270 #gen_proto_variants(meta, name, ...)
273 local meta="$1"; shift
274 local name="$1"; shift
278 meta_in "${meta}" "fF" && pfx="fetch_"
279 meta_in "${meta}" "R" && sfx="_return"
281 gen_proto_order_variants "${meta}" "${pfx}" "${name}" "${sfx}" "$@"
284 #gen_proto(meta, ...)
286 local meta="$1"; shift
287 for m in $(echo "${meta}" | grep -o .); do
288 gen_proto_variants "${m}" "$@"