[d5e5fd1] | 1 | #!/bin/sh
|
---|
| 2 |
|
---|
[5af6cf3d] | 3 | # There's a bunch of restriction. The script assumes that the structure definition begins with "typedef struct struct_name {",
|
---|
| 4 | # ends with "struct_name_t;", and that every word in between that ends with ";" is a struct member name with optional
|
---|
| 5 | # array subscript. Error handling is mostly omitted for simplicity, so any input that does not follow these rules will result
|
---|
| 6 | # in cryptic errors.
|
---|
[d5e5fd1] | 7 |
|
---|
[5af6cf3d] | 8 | input="$1"
|
---|
| 9 | output="$2"
|
---|
| 10 | toolsdir="$(readlink -f $(dirname "$0"))"
|
---|
| 11 | awkscript="$toolsdir/autogen2.awk"
|
---|
[d5e5fd1] | 12 |
|
---|
[5af6cf3d] | 13 | # Set default value for $CC.
|
---|
[d5e5fd1] | 14 | if [ -z "${CC}" ]; then
|
---|
| 15 | CC=cc
|
---|
| 16 | fi
|
---|
| 17 |
|
---|
[5af6cf3d] | 18 | # If $CC is clang, we need to make sure integrated assembler is not used.
|
---|
[d5e5fd1] | 19 | if ( ${CC} --version | grep clang > /dev/null ) ; then
|
---|
| 20 | CFLAGS="${CFLAGS} -no-integrated-as"
|
---|
| 21 | fi
|
---|
| 22 |
|
---|
[5af6cf3d] | 23 | # Tell the compiler to generate makefile dependencies.
|
---|
| 24 | CFLAGS="${CFLAGS} -MD -MP -MT $output -MF $output.d"
|
---|
[d5e5fd1] | 25 |
|
---|
[5af6cf3d] | 26 | # Generate defines
|
---|
| 27 | defs=`$awkscript -- $input || exit 1`
|
---|
[d5e5fd1] | 28 |
|
---|
[5af6cf3d] | 29 | # Generate C file.
|
---|
| 30 | cat > $output.c <<- EOF
|
---|
[d5e5fd1] | 31 |
|
---|
[5af6cf3d] | 32 | #include "`basename $input`"
|
---|
| 33 |
|
---|
| 34 | #define DEFINE_MEMBER(ls, us, lm, um) \
|
---|
| 35 | asm volatile ("/* #define "#us"_OFFSET_"#um" %0 */" :: "i" (__builtin_offsetof(struct ls, lm))); \
|
---|
| 36 | asm volatile ("/* #define "#us"_SIZE_"#um" %0 */" :: "i" (sizeof((struct ls){}.lm)));
|
---|
| 37 |
|
---|
| 38 | #define DEFINE_STRUCT(ls, us) \\
|
---|
| 39 | asm volatile ("/* #define "#us"_SIZE %0 */" :: "i" (sizeof(struct ls)));
|
---|
[d5e5fd1] | 40 |
|
---|
| 41 | void autogen()
|
---|
| 42 | {
|
---|
[5af6cf3d] | 43 | $defs
|
---|
[d5e5fd1] | 44 | }
|
---|
| 45 |
|
---|
| 46 | EOF
|
---|
[5af6cf3d] | 47 |
|
---|
| 48 | # Turn the C file into assembly.
|
---|
| 49 | ${CC} ${CFLAGS} -w -S -o $output.s $output.c || exit 1
|
---|
| 50 |
|
---|
| 51 | # Process the output.
|
---|
| 52 |
|
---|
| 53 | echo "/* Autogenerated file, do not modify. */" > $output
|
---|
| 54 | echo "#pragma once" >> $output
|
---|
| 55 | sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' < $output.s > $output || exit 1 |
---|