genassym.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # $OpenBSD: genassym.sh,v 1.12 2014/07/05 07:18:33 jsg Exp $
  2. # $NetBSD: genassym.sh,v 1.9 1998/04/25 19:48:27 matthias Exp $
  3. #
  4. # Copyright (c) 1997 Matthias Pfaller.
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #
  27. # If first argument is -c, create a temporary C file,
  28. # compile it and execute the result.
  29. awk=${AWK:-awk}
  30. if [ "x$1" = "x-c" ] ; then
  31. shift
  32. ccode=1
  33. else
  34. ccode=0
  35. fi
  36. TMPC=`mktemp /tmp/genassym_c.XXXXXXXXXX` || exit 1
  37. TMP=`mktemp /tmp/genassym.XXXXXXXXXX` || {
  38. rm -f ${TMPC}
  39. exit 1
  40. }
  41. trap "rm -f $TMPC $TMP" 0 1 2 3 15
  42. $awk '
  43. BEGIN {
  44. printf("#ifndef _KERNEL\n#define _KERNEL\n#endif\n");
  45. printf("#define offsetof(type, member) ((size_t)(&((type *)0)->member))\n");
  46. defining = 0;
  47. type = "long";
  48. asmtype = "n";
  49. asmprint = "";
  50. }
  51. $0 ~ /^[ \t]*#.*/ || $0 ~ /^[ \t]*$/ {
  52. # Just ignore comments and empty lines
  53. next;
  54. }
  55. $0 ~ /^config[ \t]/ {
  56. type = $2;
  57. asmtype = $3;
  58. asmprint = $4;
  59. next;
  60. }
  61. /^include[ \t]/ {
  62. if (defining != 0) {
  63. defining = 0;
  64. printf("}\n");
  65. }
  66. if (includes[$2] == 0) {
  67. printf("#%s\n", $0);
  68. includes[$2] = 1;
  69. }
  70. next;
  71. }
  72. $0 ~ /^if[ \t]/ ||
  73. $0 ~ /^ifdef[ \t]/ ||
  74. $0 ~ /^ifndef[ \t]/ ||
  75. $0 ~ /^else/ ||
  76. $0 ~ /^elif[ \t]/ ||
  77. $0 ~ /^endif/ {
  78. printf("#%s\n", $0);
  79. next;
  80. }
  81. /^union[ \t]/ {
  82. structname = $2;
  83. prefixname = toupper($3);
  84. structtype = "union"
  85. if (union[structname] == 1)
  86. next;
  87. else {
  88. union[structname] = 1;
  89. $0 = "define " toupper(structname) "_SIZEOF sizeof(union " structname ")";
  90. }
  91. # fall through
  92. }
  93. /^struct[ \t]/ {
  94. structname = $2;
  95. prefixname = toupper($3);
  96. structtype = "struct"
  97. if (struct[structname] == 1)
  98. next;
  99. else {
  100. struct[structname] = 1;
  101. $0 = "define " toupper(structname) "_SIZEOF sizeof(struct " structname ")";
  102. }
  103. # fall through
  104. }
  105. /^member[ \t]/ {
  106. if (NF > 2)
  107. $0 = "define " prefixname toupper($2) " offsetof(" structtype " " structname ", " $3 ")";
  108. else
  109. $0 = "define " prefixname toupper($2) " offsetof(" structtype " " structname ", " $2 ")";
  110. # fall through
  111. }
  112. /^export[ \t]/ {
  113. $0 = "define " $2 " " $2;
  114. # fall through
  115. }
  116. /^define[ \t]/ {
  117. if (defining == 0) {
  118. defining = 1;
  119. printf("void f" FNR "(void);\n");
  120. printf("void f" FNR "() {\n");
  121. if (ccode)
  122. call[FNR] = "f" FNR;
  123. defining = 1;
  124. }
  125. value = $0
  126. gsub("^define[ \t]+[A-Za-z_][A-Za-z_0-9]*[ \t]+", "", value)
  127. if (ccode)
  128. printf("printf(\"#define " $2 " %%ld\\n\", (%s)" value ");\n", type);
  129. else
  130. printf("__asm(\"XYZZY %s %%%s0\" : : \"%s\" (%s));\n", $2, asmprint, asmtype, value);
  131. next;
  132. }
  133. /^quote[ \t]/ {
  134. gsub("^quote[ \t]+", "");
  135. print;
  136. next;
  137. }
  138. {
  139. printf("syntax error in line %d\n", FNR) >"/dev/stderr";
  140. exit(1);
  141. }
  142. END {
  143. if (defining != 0) {
  144. defining = 0;
  145. printf("}\n");
  146. }
  147. if (ccode) {
  148. printf("int main(int argc, char **argv) {");
  149. for (i in call)
  150. printf(call[i] "();");
  151. printf("return(0); }\n");
  152. }
  153. }
  154. ' ccode=$ccode > $TMPC || exit 1
  155. if [ $ccode = 1 ] ; then
  156. "$@" -x c $TMPC -o $TMP && $TMP
  157. else
  158. # Kill all of the "#" and "$" modifiers; locore.s already
  159. # prepends the correct "constant" modifier.
  160. "$@" -x c -S ${TMPC} -o ${TMP} || exit 1
  161. sed -e 's/#//g' -e 's/\$//g' ${TMP} | sed -n 's/.*XYZZY/#define/gp'
  162. fi