Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 ATOMICDIR=$(dirname $0)
0005 
0006 . ${ATOMICDIR}/atomic-tbl.sh
0007 
0008 #gen_template_fallback(template, meta, pfx, name, sfx, order, atomic, int, args...)
0009 gen_template_fallback()
0010 {
0011         local template="$1"; shift
0012         local meta="$1"; shift
0013         local pfx="$1"; shift
0014         local name="$1"; shift
0015         local sfx="$1"; shift
0016         local order="$1"; shift
0017         local atomic="$1"; shift
0018         local int="$1"; shift
0019 
0020         local atomicname="arch_${atomic}_${pfx}${name}${sfx}${order}"
0021 
0022         local ret="$(gen_ret_type "${meta}" "${int}")"
0023         local retstmt="$(gen_ret_stmt "${meta}")"
0024         local params="$(gen_params "${int}" "${atomic}" "$@")"
0025         local args="$(gen_args "$@")"
0026 
0027         if [ ! -z "${template}" ]; then
0028                 printf "#ifndef ${atomicname}\n"
0029                 . ${template}
0030                 printf "#define ${atomicname} ${atomicname}\n"
0031                 printf "#endif\n\n"
0032         fi
0033 }
0034 
0035 #gen_proto_fallback(meta, pfx, name, sfx, order, atomic, int, args...)
0036 gen_proto_fallback()
0037 {
0038         local meta="$1"; shift
0039         local pfx="$1"; shift
0040         local name="$1"; shift
0041         local sfx="$1"; shift
0042         local order="$1"; shift
0043 
0044         local tmpl="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
0045         gen_template_fallback "${tmpl}" "${meta}" "${pfx}" "${name}" "${sfx}" "${order}" "$@"
0046 }
0047 
0048 #gen_basic_fallbacks(basename)
0049 gen_basic_fallbacks()
0050 {
0051         local basename="$1"; shift
0052 cat << EOF
0053 #define ${basename}_acquire ${basename}
0054 #define ${basename}_release ${basename}
0055 #define ${basename}_relaxed ${basename}
0056 EOF
0057 }
0058 
0059 gen_proto_order_variant()
0060 {
0061         local meta="$1"; shift
0062         local pfx="$1"; shift
0063         local name="$1"; shift
0064         local sfx="$1"; shift
0065         local order="$1"; shift
0066         local atomic="$1"
0067 
0068         local basename="arch_${atomic}_${pfx}${name}${sfx}"
0069 
0070         printf "#define ${basename}${order} ${basename}${order}\n"
0071 }
0072 
0073 #gen_proto_order_variants(meta, pfx, name, sfx, atomic, int, args...)
0074 gen_proto_order_variants()
0075 {
0076         local meta="$1"; shift
0077         local pfx="$1"; shift
0078         local name="$1"; shift
0079         local sfx="$1"; shift
0080         local atomic="$1"
0081 
0082         local basename="arch_${atomic}_${pfx}${name}${sfx}"
0083 
0084         local template="$(find_fallback_template "${pfx}" "${name}" "${sfx}" "${order}")"
0085 
0086         # If we don't have relaxed atomics, then we don't bother with ordering fallbacks
0087         # read_acquire and set_release need to be templated, though
0088         if ! meta_has_relaxed "${meta}"; then
0089                 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
0090 
0091                 if meta_has_acquire "${meta}"; then
0092                         gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
0093                 fi
0094 
0095                 if meta_has_release "${meta}"; then
0096                         gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
0097                 fi
0098 
0099                 return
0100         fi
0101 
0102         printf "#ifndef ${basename}_relaxed\n"
0103 
0104         if [ ! -z "${template}" ]; then
0105                 printf "#ifdef ${basename}\n"
0106         fi
0107 
0108         gen_basic_fallbacks "${basename}"
0109 
0110         if [ ! -z "${template}" ]; then
0111                 printf "#endif /* ${basename} */\n\n"
0112                 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
0113                 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
0114                 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
0115                 gen_proto_fallback "${meta}" "${pfx}" "${name}" "${sfx}" "_relaxed" "$@"
0116         fi
0117 
0118         printf "#else /* ${basename}_relaxed */\n\n"
0119 
0120         gen_template_fallback "${ATOMICDIR}/fallbacks/acquire"  "${meta}" "${pfx}" "${name}" "${sfx}" "_acquire" "$@"
0121         gen_template_fallback "${ATOMICDIR}/fallbacks/release"  "${meta}" "${pfx}" "${name}" "${sfx}" "_release" "$@"
0122         gen_template_fallback "${ATOMICDIR}/fallbacks/fence"  "${meta}" "${pfx}" "${name}" "${sfx}" "" "$@"
0123 
0124         printf "#endif /* ${basename}_relaxed */\n\n"
0125 }
0126 
0127 gen_order_fallbacks()
0128 {
0129         local xchg="$1"; shift
0130 
0131 cat <<EOF
0132 
0133 #ifndef ${xchg}_acquire
0134 #define ${xchg}_acquire(...) \\
0135         __atomic_op_acquire(${xchg}, __VA_ARGS__)
0136 #endif
0137 
0138 #ifndef ${xchg}_release
0139 #define ${xchg}_release(...) \\
0140         __atomic_op_release(${xchg}, __VA_ARGS__)
0141 #endif
0142 
0143 #ifndef ${xchg}
0144 #define ${xchg}(...) \\
0145         __atomic_op_fence(${xchg}, __VA_ARGS__)
0146 #endif
0147 
0148 EOF
0149 }
0150 
0151 gen_xchg_fallbacks()
0152 {
0153         local xchg="$1"; shift
0154         printf "#ifndef ${xchg}_relaxed\n"
0155 
0156         gen_basic_fallbacks ${xchg}
0157 
0158         printf "#else /* ${xchg}_relaxed */\n"
0159 
0160         gen_order_fallbacks ${xchg}
0161 
0162         printf "#endif /* ${xchg}_relaxed */\n\n"
0163 }
0164 
0165 gen_try_cmpxchg_fallback()
0166 {
0167         local cmpxchg="$1"; shift;
0168         local order="$1"; shift;
0169 
0170 cat <<EOF
0171 #ifndef arch_try_${cmpxchg}${order}
0172 #define arch_try_${cmpxchg}${order}(_ptr, _oldp, _new) \\
0173 ({ \\
0174         typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \\
0175         ___r = arch_${cmpxchg}${order}((_ptr), ___o, (_new)); \\
0176         if (unlikely(___r != ___o)) \\
0177                 *___op = ___r; \\
0178         likely(___r == ___o); \\
0179 })
0180 #endif /* arch_try_${cmpxchg}${order} */
0181 
0182 EOF
0183 }
0184 
0185 gen_try_cmpxchg_fallbacks()
0186 {
0187         local cmpxchg="$1"; shift;
0188 
0189         printf "#ifndef arch_try_${cmpxchg}_relaxed\n"
0190         printf "#ifdef arch_try_${cmpxchg}\n"
0191 
0192         gen_basic_fallbacks "arch_try_${cmpxchg}"
0193 
0194         printf "#endif /* arch_try_${cmpxchg} */\n\n"
0195 
0196         for order in "" "_acquire" "_release" "_relaxed"; do
0197                 gen_try_cmpxchg_fallback "${cmpxchg}" "${order}"
0198         done
0199 
0200         printf "#else /* arch_try_${cmpxchg}_relaxed */\n"
0201 
0202         gen_order_fallbacks "arch_try_${cmpxchg}"
0203 
0204         printf "#endif /* arch_try_${cmpxchg}_relaxed */\n\n"
0205 }
0206 
0207 cat << EOF
0208 // SPDX-License-Identifier: GPL-2.0
0209 
0210 // Generated by $0
0211 // DO NOT MODIFY THIS FILE DIRECTLY
0212 
0213 #ifndef _LINUX_ATOMIC_FALLBACK_H
0214 #define _LINUX_ATOMIC_FALLBACK_H
0215 
0216 #include <linux/compiler.h>
0217 
0218 EOF
0219 
0220 for xchg in "arch_xchg" "arch_cmpxchg" "arch_cmpxchg64"; do
0221         gen_xchg_fallbacks "${xchg}"
0222 done
0223 
0224 for cmpxchg in "cmpxchg" "cmpxchg64"; do
0225         gen_try_cmpxchg_fallbacks "${cmpxchg}"
0226 done
0227 
0228 grep '^[a-z]' "$1" | while read name meta args; do
0229         gen_proto "${meta}" "${name}" "atomic" "int" ${args}
0230 done
0231 
0232 cat <<EOF
0233 #ifdef CONFIG_GENERIC_ATOMIC64
0234 #include <asm-generic/atomic64.h>
0235 #endif
0236 
0237 EOF
0238 
0239 grep '^[a-z]' "$1" | while read name meta args; do
0240         gen_proto "${meta}" "${name}" "atomic64" "s64" ${args}
0241 done
0242 
0243 cat <<EOF
0244 #endif /* _LINUX_ATOMIC_FALLBACK_H */
0245 EOF