debuggers.hg

annotate xen/tools/get-fields.sh @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents f964ec304363
children
rev   line source
ack@13293 1 test -n "$1" -a -n "$2" -a -n "$3"
ack@13293 2 set -ef
ack@13293 3
ack@13362 4 SED=sed
keir@17818 5 if test -x /usr/xpg4/bin/sed; then
keir@17818 6 SED=/usr/xpg4/bin/sed
keir@17818 7 fi
keir@19954 8 if test -z ${PYTHON}; then
keir@19954 9 PYTHON=`/usr/bin/env python`
keir@19954 10 fi
keir@19954 11 if test -z ${PYTHON}; then
keir@19954 12 echo "Python not found"
keir@19954 13 exit 1
keir@19954 14 fi
ack@13362 15
keir@16180 16 get_fields ()
keir@16180 17 {
ack@13293 18 local level=1 aggr=0 name= fields=
ack@13293 19 for token in $2
ack@13293 20 do
ack@13293 21 case "$token" in
ack@13293 22 struct|union)
ack@13293 23 test $level != 1 || aggr=1 fields= name=
ack@13293 24 ;;
ack@13293 25 "{")
ack@13293 26 level=$(expr $level + 1)
ack@13293 27 ;;
ack@13293 28 "}")
ack@13293 29 level=$(expr $level - 1)
ack@13293 30 if [ $level = 1 -a $name = $1 ]
ack@13293 31 then
ack@13293 32 echo "$fields }"
ack@13293 33 return 0
ack@13293 34 fi
ack@13293 35 ;;
keir@16180 36 [a-zA-Z_]*)
ack@13293 37 test $aggr = 0 -o -n "$name" || name="$token"
ack@13293 38 ;;
ack@13293 39 esac
ack@13293 40 test $aggr = 0 || fields="$fields $token"
ack@13293 41 done
ack@13293 42 }
ack@13293 43
keir@19809 44 get_typedefs ()
keir@19809 45 {
keir@19809 46 local level=1 state=
keir@19809 47 for token in $1
keir@19809 48 do
keir@19809 49 case "$token" in
keir@19809 50 typedef)
keir@19809 51 test $level != 1 || state=1
keir@19809 52 ;;
keir@19809 53 COMPAT_HANDLE\(*\))
keir@19809 54 test $level != 1 -o "$state" != 1 || state=2
keir@19809 55 ;;
keir@19809 56 [\{\[])
keir@19809 57 level=$(expr $level + 1)
keir@19809 58 ;;
keir@19809 59 [\}\]])
keir@19809 60 level=$(expr $level - 1)
keir@19809 61 ;;
keir@19809 62 ";")
keir@19809 63 test $level != 1 || state=
keir@19809 64 ;;
keir@19809 65 [a-zA-Z_]*)
keir@19809 66 test $level != 1 -o "$state" != 2 || echo "$token"
keir@19809 67 ;;
keir@19809 68 esac
keir@19809 69 done
keir@19809 70 }
keir@19809 71
keir@16180 72 build_enums ()
keir@16180 73 {
ack@13293 74 local level=1 kind= fields= members= named= id= token
ack@13293 75 for token in $2
ack@13293 76 do
ack@13293 77 case "$token" in
ack@13293 78 struct|union)
ack@13293 79 test $level != 2 || fields=" "
ack@13293 80 kind="$token;$kind"
ack@13293 81 ;;
ack@13293 82 "{")
ack@13293 83 level=$(expr $level + 1)
ack@13293 84 ;;
ack@13293 85 "}")
ack@13293 86 level=$(expr $level - 1)
ack@13293 87 if [ $level = 1 ]
ack@13293 88 then
ack@13293 89 if [ "${kind%%;*}" = union ]
ack@13293 90 then
ack@13293 91 echo
ack@13293 92 echo "enum XLAT_$1 {"
ack@13293 93 for m in $members
ack@13293 94 do
ack@13293 95 echo " XLAT_${1}_$m,"
ack@13293 96 done
ack@13293 97 echo "};"
ack@13293 98 fi
ack@13293 99 return 0
ack@13293 100 elif [ $level = 2 ]
ack@13293 101 then
ack@13293 102 named='?'
ack@13293 103 fi
ack@13293 104 ;;
keir@16180 105 [a-zA-Z]*)
ack@13293 106 id=$token
ack@13293 107 if [ -n "$named" -a -n "${kind#*;}" ]
ack@13293 108 then
ack@13293 109 build_enums ${1}_$token "$fields"
ack@13293 110 named='!'
ack@13293 111 fi
ack@13293 112 ;;
ack@13293 113 ",")
ack@13293 114 test $level != 2 || members="$members $id"
ack@13293 115 ;;
ack@13293 116 ";")
ack@13293 117 test $level != 2 || members="$members $id"
ack@13293 118 test -z "$named" || kind=${kind#*;}
ack@13293 119 named=
ack@13293 120 ;;
ack@13293 121 esac
ack@13293 122 test -z "$fields" || fields="$fields $token"
ack@13293 123 done
ack@13293 124 }
ack@13293 125
keir@16180 126 handle_field ()
keir@16180 127 {
ack@13293 128 if [ -z "$5" ]
ack@13293 129 then
ack@13293 130 echo " \\"
ack@13293 131 if [ -z "$4" ]
ack@13293 132 then
ack@13293 133 echo -n "$1(_d_)->$3 = (_s_)->$3;"
ack@13293 134 else
ack@13362 135 echo -n "$1XLAT_${2}_HNDL_$(echo $3 | $SED 's,\.,_,g')(_d_, _s_);"
ack@13293 136 fi
ack@13362 137 elif [ -z "$(echo "$5" | $SED 's,[^{}],,g')" ]
ack@13293 138 then
keir@19954 139 local tag=$(echo "$5" | ${PYTHON} -c '
ack@13362 140 import re,sys
ack@13362 141 for line in sys.stdin.readlines():
ack@13362 142 print re.subn(r"\s*(struct|union)\s+(compat_)?(\w+)\s.*", r"\3", line)[0].rstrip()
ack@13362 143 ')
ack@13293 144 echo " \\"
ack@13293 145 echo -n "${1}XLAT_$tag(&(_d_)->$3, &(_s_)->$3);"
ack@13293 146 else
ack@13293 147 local level=1 kind= fields= id= array= arrlvl=1 array_type= type= token
ack@13293 148 for token in $5
ack@13293 149 do
ack@13293 150 case "$token" in
ack@13293 151 struct|union)
ack@13293 152 test $level != 2 || fields=" "
atse@13397 153 if [ $level = 1 ]
ack@13293 154 then
ack@13293 155 kind=$token
ack@13293 156 if [ $kind = union ]
ack@13293 157 then
ack@13293 158 echo " \\"
ack@13362 159 echo -n "${1}switch ($(echo $3 | $SED 's,\.,_,g')) {"
ack@13293 160 fi
ack@13293 161 fi
ack@13293 162 ;;
ack@13293 163 "{")
ack@13293 164 level=$(expr $level + 1) id=
ack@13293 165 ;;
ack@13293 166 "}")
ack@13293 167 level=$(expr $level - 1) id=
atse@13397 168 if [ $level = 1 -a $kind = union ]
ack@13293 169 then
ack@13293 170 echo " \\"
ack@13293 171 echo -n "$1}"
ack@13293 172 fi
ack@13293 173 ;;
ack@13293 174 "[")
ack@13293 175 if [ $level != 2 -o $arrlvl != 1 ]
ack@13293 176 then
ack@13293 177 :
ack@13293 178 elif [ -z "$array" ]
ack@13293 179 then
ack@13293 180 array=" "
ack@13293 181 else
ack@13293 182 array="$array;"
ack@13293 183 fi
ack@13293 184 arrlvl=$(expr $arrlvl + 1)
ack@13293 185 ;;
ack@13293 186 "]")
ack@13293 187 arrlvl=$(expr $arrlvl - 1)
ack@13293 188 ;;
ack@13293 189 COMPAT_HANDLE\(*\))
atse@13397 190 if [ $level = 2 -a -z "$id" ]
ack@13293 191 then
ack@13293 192 type=${token#COMPAT_HANDLE?}
ack@13293 193 type=${type%?}
ack@13293 194 type=${type#compat_}
ack@13293 195 fi
ack@13293 196 ;;
ack@13293 197 compat_domain_handle_t)
atse@13397 198 if [ $level = 2 -a -z "$id" ]
ack@13293 199 then
ack@13293 200 array_type=$token
ack@13293 201 fi
ack@13293 202 ;;
keir@16180 203 [a-zA-Z]*)
keir@19809 204 if [ -z "$id" -a -z "$type" -a -z "$array_type" ]
keir@19809 205 then
keir@19809 206 for id in $typedefs
keir@19809 207 do
keir@19809 208 test $id != "$token" || type=$id
keir@19809 209 done
keir@19809 210 if [ -z "$type" ]
keir@19809 211 then
keir@19809 212 id=$token
keir@19809 213 else
keir@19809 214 id=
keir@19809 215 fi
keir@19809 216 else
keir@19809 217 id=$token
keir@19809 218 fi
ack@13293 219 ;;
ack@13293 220 [\,\;])
atse@13397 221 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
ack@13293 222 then
ack@13293 223 if [ $kind = union ]
ack@13293 224 then
ack@13293 225 echo " \\"
ack@13362 226 echo -n "${1}case XLAT_${2}_$(echo $3.$id | $SED 's,\.,_,g'):"
ack@13293 227 handle_field "$1 " $2 $3.$id "$type" "$fields"
ack@13293 228 elif [ -z "$array" -a -z "$array_type" ]
ack@13293 229 then
ack@13293 230 handle_field "$1" $2 $3.$id "$type" "$fields"
ack@13293 231 elif [ -z "$array" ]
ack@13293 232 then
ack@13293 233 copy_array " " $3.$id
ack@13293 234 else
ack@13293 235 handle_array "$1" $2 $3.$id "${array#*;}" "$type" "$fields"
ack@13293 236 fi
ack@13293 237 test "$token" != ";" || fields= id= type=
ack@13293 238 array=
ack@13293 239 if [ $kind = union ]
ack@13293 240 then
ack@13293 241 echo " \\"
ack@13293 242 echo -n "$1 break;"
ack@13293 243 fi
ack@13293 244 fi
ack@13293 245 ;;
ack@13293 246 *)
ack@13293 247 if [ -n "$array" ]
ack@13293 248 then
ack@13293 249 array="$array $token"
ack@13293 250 fi
ack@13293 251 ;;
ack@13293 252 esac
ack@13293 253 test -z "$fields" || fields="$fields $token"
ack@13293 254 done
ack@13293 255 fi
ack@13293 256 }
ack@13293 257
keir@16180 258 copy_array ()
keir@16180 259 {
ack@13293 260 echo " \\"
ack@13293 261 echo "${1}if ((_d_)->$2 != (_s_)->$2) \\"
ack@13293 262 echo -n "$1 memcpy((_d_)->$2, (_s_)->$2, sizeof((_d_)->$2));"
ack@13293 263 }
ack@13293 264
keir@16180 265 handle_array ()
keir@16180 266 {
ack@13362 267 local i="i$(echo $4 | $SED 's,[^;], ,g' | wc -w | $SED 's,[[:space:]]*,,g')"
ack@13293 268 echo " \\"
ack@13293 269 echo "$1{ \\"
ack@13293 270 echo "$1 unsigned int $i; \\"
ack@13293 271 echo -n "$1 for ($i = 0; $i < "${4%%;*}"; ++$i) {"
ack@13293 272 if [ "$4" = "${4#*;}" ]
ack@13293 273 then
ack@13293 274 handle_field "$1 " $2 $3[$i] "$5" "$6"
ack@13293 275 else
ack@13293 276 handle_array "$1 " $2 $3[$i] "${4#*;}" "$5" "$6"
ack@13293 277 fi
ack@13293 278 echo " \\"
ack@13293 279 echo "$1 } \\"
ack@13293 280 echo -n "$1}"
ack@13293 281 }
ack@13293 282
keir@16180 283 build_body ()
keir@16180 284 {
ack@13293 285 echo
Tim@13804 286 echo -n "#define XLAT_$1(_d_, _s_) do {"
ack@13293 287 local level=1 fields= id= array= arrlvl=1 array_type= type= token
ack@13293 288 for token in $2
ack@13293 289 do
ack@13293 290 case "$token" in
ack@13293 291 struct|union)
ack@13293 292 test $level != 2 || fields=" "
ack@13293 293 ;;
ack@13293 294 "{")
ack@13293 295 level=$(expr $level + 1) id=
ack@13293 296 ;;
ack@13293 297 "}")
ack@13293 298 level=$(expr $level - 1) id=
ack@13293 299 ;;
ack@13293 300 "[")
ack@13293 301 if [ $level != 2 -o $arrlvl != 1 ]
ack@13293 302 then
ack@13293 303 :
ack@13293 304 elif [ -z "$array" ]
ack@13293 305 then
ack@13293 306 array=" "
ack@13293 307 else
ack@13293 308 array="$array;"
ack@13293 309 fi
ack@13293 310 arrlvl=$(expr $arrlvl + 1)
ack@13293 311 ;;
ack@13293 312 "]")
ack@13293 313 arrlvl=$(expr $arrlvl - 1)
ack@13293 314 ;;
ack@13293 315 COMPAT_HANDLE\(*\))
atse@13397 316 if [ $level = 2 -a -z "$id" ]
ack@13293 317 then
ack@13293 318 type=${token#COMPAT_HANDLE?}
ack@13293 319 type=${type%?}
ack@13293 320 type=${type#compat_}
ack@13293 321 fi
ack@13293 322 ;;
ack@13293 323 compat_domain_handle_t)
atse@13397 324 if [ $level = 2 -a -z "$id" ]
ack@13293 325 then
ack@13293 326 array_type=$token
ack@13293 327 fi
ack@13293 328 ;;
keir@16180 329 [a-zA-Z_]*)
ack@13293 330 if [ -n "$array" ]
ack@13293 331 then
ack@13293 332 array="$array $token"
keir@19809 333 elif [ -z "$id" -a -z "$type" -a -z "$array_type" ]
keir@19809 334 then
keir@19809 335 for id in $typedefs
keir@19809 336 do
keir@19809 337 test $id != "$token" || type=$id
keir@19809 338 done
keir@19809 339 if [ -z "$type" ]
keir@19809 340 then
keir@19809 341 id=$token
keir@19809 342 else
keir@19809 343 id=
keir@19809 344 fi
ack@13293 345 else
ack@13293 346 id=$token
ack@13293 347 fi
ack@13293 348 ;;
ack@13293 349 [\,\;])
atse@13397 350 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
ack@13293 351 then
ack@13293 352 if [ -z "$array" -a -z "$array_type" ]
ack@13293 353 then
ack@13293 354 handle_field " " $1 $id "$type" "$fields"
ack@13293 355 elif [ -z "$array" ]
ack@13293 356 then
ack@13293 357 copy_array " " $id
ack@13293 358 else
ack@13293 359 handle_array " " $1 $id "${array#*;}" "$type" "$fields"
ack@13293 360 fi
ack@13293 361 test "$token" != ";" || fields= id= type=
ack@13293 362 array=
ack@13293 363 fi
ack@13293 364 ;;
ack@13293 365 *)
ack@13293 366 if [ -n "$array" ]
ack@13293 367 then
ack@13293 368 array="$array $token"
ack@13293 369 fi
ack@13293 370 ;;
ack@13293 371 esac
ack@13293 372 test -z "$fields" || fields="$fields $token"
ack@13293 373 done
Tim@13804 374 echo " \\"
Tim@13804 375 echo "} while (0)"
ack@13293 376 }
ack@13293 377
keir@16180 378 check_field ()
keir@16180 379 {
ack@13362 380 if [ -z "$(echo "$4" | $SED 's,[^{}],,g')" ]
ack@13293 381 then
ack@13293 382 echo "; \\"
ack@13362 383 local n=$(echo $3 | $SED 's,[^.], ,g' | wc -w | $SED 's,[[:space:]]*,,g')
ack@13293 384 if [ -n "$4" ]
ack@13293 385 then
ack@13293 386 for n in $4
ack@13293 387 do
ack@13293 388 case $n in
ack@13293 389 struct|union)
ack@13293 390 ;;
keir@16180 391 [a-zA-Z_]*)
keir@19502 392 echo -n " CHECK_${n#xen_}"
ack@13293 393 break
ack@13293 394 ;;
ack@13293 395 *)
ack@13293 396 echo "Malformed compound declaration: '$n'" >&2
ack@13293 397 exit 1
ack@13293 398 ;;
ack@13293 399 esac
ack@13293 400 done
ack@13293 401 elif [ $n = 0 ]
ack@13293 402 then
ack@13293 403 echo -n " CHECK_FIELD_($1, $2, $3)"
ack@13293 404 else
ack@13362 405 echo -n " CHECK_SUBFIELD_${n}_($1, $2, $(echo $3 | $SED 's!\.!, !g'))"
ack@13293 406 fi
ack@13293 407 else
ack@13293 408 local level=1 fields= id= token
ack@13293 409 for token in $4
ack@13293 410 do
ack@13293 411 case "$token" in
ack@13293 412 struct|union)
ack@13293 413 test $level != 2 || fields=" "
ack@13293 414 ;;
ack@13293 415 "{")
ack@13293 416 level=$(expr $level + 1) id=
ack@13293 417 ;;
ack@13293 418 "}")
ack@13293 419 level=$(expr $level - 1) id=
ack@13293 420 ;;
keir@16180 421 [a-zA-Z]*)
ack@13293 422 id=$token
ack@13293 423 ;;
ack@13293 424 [\,\;])
atse@13397 425 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
ack@13293 426 then
ack@13293 427 check_field $1 $2 $3.$id "$fields"
ack@13293 428 test "$token" != ";" || fields= id=
ack@13293 429 fi
ack@13293 430 ;;
ack@13293 431 esac
ack@13293 432 test -z "$fields" || fields="$fields $token"
ack@13293 433 done
ack@13293 434 fi
ack@13293 435 }
ack@13293 436
keir@16180 437 build_check ()
keir@16180 438 {
ack@13293 439 echo
ack@13293 440 echo "#define CHECK_$1 \\"
ack@13293 441 local level=1 fields= kind= id= arrlvl=1 token
ack@13293 442 for token in $2
ack@13293 443 do
ack@13293 444 case "$token" in
ack@13293 445 struct|union)
atse@13397 446 if [ $level = 1 ]
ack@13293 447 then
ack@13293 448 kind=$token
ack@13293 449 echo -n " CHECK_SIZE_($kind, $1)"
atse@13397 450 elif [ $level = 2 ]
ack@13293 451 then
ack@13293 452 fields=" "
ack@13293 453 fi
ack@13293 454 ;;
ack@13293 455 "{")
ack@13293 456 level=$(expr $level + 1) id=
ack@13293 457 ;;
ack@13293 458 "}")
ack@13293 459 level=$(expr $level - 1) id=
ack@13293 460 ;;
ack@13293 461 "[")
ack@13293 462 arrlvl=$(expr $arrlvl + 1)
ack@13293 463 ;;
ack@13293 464 "]")
ack@13293 465 arrlvl=$(expr $arrlvl - 1)
ack@13293 466 ;;
keir@16180 467 [a-zA-Z_]*)
ack@13293 468 test $level != 2 -o $arrlvl != 1 || id=$token
ack@13293 469 ;;
ack@13293 470 [\,\;])
atse@13397 471 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
ack@13293 472 then
ack@13293 473 check_field $kind $1 $id "$fields"
ack@13293 474 test "$token" != ";" || fields= id=
ack@13293 475 fi
ack@13293 476 ;;
ack@13293 477 esac
ack@13293 478 test -z "$fields" || fields="$fields $token"
ack@13293 479 done
ack@13293 480 echo ""
ack@13293 481 }
ack@13293 482
keir@19809 483 list="$($SED -e 's,^[[:space:]]#.*,,' -e 's!\([]\[,;:{}]\)! \1 !g' $3)"
keir@19809 484 fields="$(get_fields $(echo $2 | $SED 's,^compat_xen,compat_,') "$list")"
ack@13293 485 if [ -z "$fields" ]
ack@13293 486 then
ack@13293 487 echo "Fields of '$2' not found in '$3'" >&2
ack@13293 488 exit 1
ack@13293 489 fi
ack@13293 490 name=${2#compat_}
ack@13293 491 name=${name#xen}
ack@13293 492 case "$1" in
ack@13293 493 "!")
keir@19809 494 typedefs="$(get_typedefs "$list")"
ack@13293 495 build_enums $name "$fields"
ack@13293 496 build_body $name "$fields"
ack@13293 497 ;;
ack@13293 498 "?")
ack@13293 499 build_check $name "$fields"
ack@13293 500 ;;
ack@13293 501 *)
ack@13293 502 echo "Invalid translation indicator: '$1'" >&2
ack@13293 503 exit 1
ack@13293 504 ;;
ack@13293 505 esac