scripts/carray.sh: Allow referencing a struct member

It can be useful to embed the objects referenced by a carray inside
another struct. To avoid type punning, the generated carray code must
use the correct type for the enclosing struct and member access to
compute the desired object address.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Samuel Holland
2024-11-11 14:02:48 -08:00
committed by Anup Patel
parent bb008e5d27
commit 10df2d6fb5

View File

@@ -61,6 +61,13 @@ if [ -z "${ARRAY_NAME}" ]; then
usage
fi
MEMBER_NAME=$(awk '{ if ($1 == "MEMBER-NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}")
MEMBER_TYPE=$(awk '{ if ($1 == "MEMBER-TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}")
if [ -n "${MEMBER_NAME}" ] && [ -z "${MEMBER_TYPE}" ]; then
echo "Must specify MEMBER-TYPE: when using MEMBER-NAME:"
usage
fi
printf "// Generated with $(basename $0) from $(basename ${CONFIG_FILE})\n"
printf "#include <%s>\n\n" "${TYPE_HEADER}"
@@ -69,9 +76,9 @@ for VAR in ${VAR_LIST}; do
done
printf "\n"
printf "%s *const %s[] = {\n" "${TYPE_NAME}" "${ARRAY_NAME}"
printf "%s *const %s[] = {\n" "${MEMBER_TYPE:-${TYPE_NAME}}" "${ARRAY_NAME}"
for VAR in ${VAR_LIST}; do
printf "\t&%s,\n" "${VAR}"
printf "\t&%s,\n" "${VAR}${MEMBER_NAME:+.}${MEMBER_NAME}"
done
printf "\tNULL\n"
printf "};\n"