struct.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* ADMINISTRATION OF STRUCT AND UNION DECLARATIONS */
  7. #include "nobitfield.h"
  8. #include "debug.h"
  9. #include "botch_free.h"
  10. #include <alloc.h>
  11. #include "arith.h"
  12. #include "stack.h"
  13. #include "idf.h"
  14. #include "def.h"
  15. #include "type.h"
  16. #include "struct.h"
  17. #include "field.h"
  18. #include "LLlex.h"
  19. #include "Lpars.h"
  20. #include "align.h"
  21. #include "level.h"
  22. #include "assert.h"
  23. #include "sizes.h"
  24. #include "noRoption.h"
  25. /* Type of previous selector declared with a field width specified,
  26. if any. If a selector is declared with no field with it is set to 0.
  27. */
  28. static field_busy = 0;
  29. extern char options[];
  30. int lcm();
  31. /* The semantics of the identification of structure/union tags is
  32. obscure. Some highly regarded compilers are found out to accept,
  33. e.g.:
  34. f(xp) struct aap *xp; {
  35. struct aap {char *za;};
  36. xp->za;
  37. }
  38. Equally highly regarded software uses this feature, so we shall
  39. humbly oblige.
  40. The rules we use are:
  41. 1. A structure definition applies at the level where it is
  42. found, unless there is a structure declaration without a
  43. definition on an outer level, in which case the definition
  44. is applied at that level.
  45. 2. A selector is applied on the same level as on which its
  46. structure is being defined.
  47. If below struct is mentioned, union is implied (and sometimes enum
  48. as well).
  49. */
  50. add_sel(stp, tp, idf, sdefpp, szp, fd) /* this is horrible */
  51. register struct type *stp; /* type of the structure */
  52. struct type *tp; /* type of the selector */
  53. register struct idf *idf; /* idf of the selector */
  54. struct sdef ***sdefpp; /* address of hook to selector definition */
  55. arith *szp; /* pointer to struct size upto here */
  56. struct field *fd;
  57. {
  58. /* The selector idf with type tp is added to two chains: the
  59. selector identification chain starting at idf->id_sdef,
  60. and to the end of the member list starting at stp->tp_sdef.
  61. The address of the hook in the latest member (sdef) is
  62. given in sdefpp; the hook itself must still be empty.
  63. */
  64. arith offset;
  65. #ifndef NOBITFIELD
  66. extern arith add_field();
  67. #endif /* NOBITFIELD */
  68. struct tag *tg = stp->tp_idf->id_struct; /* or union */
  69. struct sdef *sdef = idf->id_sdef;
  70. register struct sdef *newsdef;
  71. int lvl = tg->tg_level;
  72. #ifndef NOROPTION
  73. if (options['R'] && !is_anon_idf(idf)) {
  74. /* a K & R test */
  75. if (idf->id_struct && idf->id_struct->tg_level == level)
  76. warning("%s is also a struct/union tag", idf->id_text);
  77. }
  78. #endif
  79. if (stp->tp_fund == STRUCT) {
  80. #ifndef NOBITFIELD
  81. if (fd == 0) { /* no field width specified */
  82. offset = align(*szp, tp->tp_align);
  83. field_busy = 0;
  84. }
  85. else {
  86. /* if something is wrong, the type of the
  87. specified selector remains unchanged; its
  88. bitfield specifier, however, is thrown away.
  89. */
  90. offset = add_field(szp, fd, &tp, idf, stp);
  91. }
  92. #else /* NOBITFIELD */
  93. offset = align(*szp, tp->tp_align);
  94. field_busy = 0;
  95. #endif /* NOBITFIELD */
  96. }
  97. else { /* (stp->tp_fund == UNION) */
  98. if (fd) {
  99. error("fields not allowed in unions");
  100. free_field(fd);
  101. fd = 0;
  102. }
  103. offset = (arith)0;
  104. }
  105. check_selector(idf, stp);
  106. #ifndef NOROPTION
  107. if (options['R']) {
  108. if ( sdef && sdef->sd_level == lvl &&
  109. ( sdef->sd_offset != offset || sdef->sd_type != tp )
  110. ) /* RM 8.5 */
  111. warning("selector %s redeclared", idf->id_text);
  112. }
  113. #endif
  114. newsdef = new_sdef();
  115. newsdef->sd_sdef = (struct sdef *) 0;
  116. /* link into selector descriptor list of this id
  117. */
  118. newsdef->next = sdef;
  119. idf->id_sdef = newsdef;
  120. newsdef->sd_level = lvl;
  121. newsdef->sd_idf = idf;
  122. newsdef->sd_stype = stp;
  123. newsdef->sd_type = tp;
  124. newsdef->sd_offset = offset;
  125. #ifndef NOBITFIELD
  126. if (tp->tp_fund == FIELD)
  127. tp->tp_field->fd_sdef = newsdef;
  128. #endif /* NOBITFIELD */
  129. stack_idf(idf, stack_level_of(lvl));
  130. /* link into selector definition list of the struct/union
  131. */
  132. **sdefpp = newsdef;
  133. *sdefpp = &newsdef->sd_sdef;
  134. /* update the size of the struct/union upward */
  135. if (stp->tp_fund == STRUCT && fd == 0) {
  136. /* Note: the case that a bitfield is declared is
  137. handled by add_field() !
  138. */
  139. *szp = offset + size_of_type(tp, "member");
  140. stp->tp_align = lcm(stp->tp_align, tp->tp_align);
  141. }
  142. else
  143. if (stp->tp_fund == UNION) {
  144. arith sel_size = size_of_type(tp, "member");
  145. if (*szp < sel_size)
  146. *szp = sel_size;
  147. stp->tp_align = lcm(stp->tp_align, tp->tp_align);
  148. }
  149. }
  150. check_selector(idf, stp)
  151. register struct idf *idf;
  152. struct type *stp; /* the type of the struct */
  153. {
  154. /* checks if idf occurs already as a selector in
  155. struct or union *stp.
  156. */
  157. register struct sdef *sdef = stp->tp_sdef;
  158. while (sdef) {
  159. if (sdef->sd_idf == idf)
  160. error("multiple selector %s", idf->id_text);
  161. sdef = sdef->sd_sdef;
  162. }
  163. }
  164. char *symbol2str();
  165. declare_struct(fund, idf, tpp)
  166. register struct idf *idf;
  167. struct type **tpp;
  168. {
  169. /* A struct, union or enum (depending on fund) with tag (!)
  170. idf is declared, and its type (incomplete as it may be) is
  171. returned in *tpp.
  172. The idf may be missing (i.e. idf == 0), in which case an
  173. anonymous struct etc. is defined.
  174. */
  175. register struct tag **tgp;
  176. register struct tag *tg;
  177. if (!idf)
  178. idf = gen_idf();
  179. tgp = (fund == ENUM ? &idf->id_enum : &idf->id_struct);
  180. #ifndef NOROPTION
  181. if (options['R'] && !is_anon_idf(idf)) {
  182. /* a K & R test */
  183. if ( fund != ENUM &&
  184. idf->id_sdef && idf->id_sdef->sd_level == level
  185. ) {
  186. warning("%s is also a selector", idf->id_text);
  187. }
  188. if ( fund == ENUM &&
  189. idf->id_def && idf->id_def->df_level == level
  190. ) {
  191. warning("%s is also a variable", idf->id_text);
  192. }
  193. }
  194. #endif
  195. tg = *tgp;
  196. if (tg && tg->tg_type->tp_size < 0 && tg->tg_type->tp_fund == fund) {
  197. /* An unfinished declaration has preceded it, possibly on
  198. an earlier level. We just fill in the answer.
  199. */
  200. if (tg->tg_busy) {
  201. error("recursive declaration of struct/union %s",
  202. idf->id_text);
  203. declare_struct(fund, gen_idf(), tpp);
  204. }
  205. else {
  206. #ifndef NOROPTION
  207. if (options['R'] && tg->tg_level != level)
  208. warning("%s declares %s in different range",
  209. idf->id_text, symbol2str(fund));
  210. #endif
  211. *tpp = tg->tg_type;
  212. }
  213. }
  214. else
  215. if (tg && tg->tg_level == level) {
  216. /* There is an already defined struct/union of this name
  217. on our level!
  218. */
  219. error("redeclaration of struct/union %s", idf->id_text);
  220. declare_struct(fund, gen_idf(), tpp);
  221. /* to allow a second struct_declaration_pack */
  222. }
  223. else {
  224. /* The struct is new. */
  225. /* Hook in a new struct tag */
  226. tg = new_tag();
  227. tg->next = *tgp;
  228. *tgp = tg;
  229. tg->tg_level = level;
  230. /* and supply room for a type */
  231. tg->tg_type = create_type(fund);
  232. tg->tg_type->tp_align =
  233. fund == ENUM ? int_align :
  234. fund == STRUCT ? struct_align :
  235. /* fund == UNION */ union_align;
  236. tg->tg_type->tp_idf = idf;
  237. *tpp = tg->tg_type;
  238. stack_idf(idf, local_level);
  239. }
  240. }
  241. apply_struct(fund, idf, tpp)
  242. register struct idf *idf;
  243. struct type **tpp;
  244. {
  245. /* The occurrence of a struct, union or enum (depending on
  246. fund) with tag idf is noted. It may or may not have been
  247. declared before. Its type (complete or incomplete) is
  248. returned in *tpp.
  249. */
  250. register struct tag **tgp;
  251. tgp = (is_struct_or_union(fund) ? &idf->id_struct : &idf->id_enum);
  252. if (*tgp)
  253. *tpp = (*tgp)->tg_type;
  254. else
  255. declare_struct(fund, idf, tpp);
  256. }
  257. struct sdef *
  258. idf2sdef(idf, tp)
  259. register struct idf *idf;
  260. struct type *tp;
  261. {
  262. /* The identifier idf is identified as a selector, preferably
  263. in the struct tp, but we will settle for any unique
  264. identification.
  265. If the attempt fails, a selector of type error_type is
  266. created.
  267. */
  268. register struct sdef **sdefp = &idf->id_sdef, *sdef;
  269. /* Follow chain from idf, to meet tp. */
  270. while ((sdef = *sdefp)) {
  271. if (sdef->sd_stype == tp)
  272. return sdef;
  273. sdefp = &(*sdefp)->next;
  274. }
  275. /* Tp not met; any unique identification will do. */
  276. if (sdef = idf->id_sdef) {
  277. /* There is an identification */
  278. if (uniq_selector(sdef)) {
  279. /* and it is unique, so we accept */
  280. warning("selector %s applied to alien type",
  281. idf->id_text);
  282. }
  283. else {
  284. /* it is ambiguous */
  285. error("ambiguous use of selector %s", idf->id_text);
  286. }
  287. return sdef;
  288. }
  289. /* No luck; create an error entry. */
  290. if (!is_anon_idf(idf))
  291. error("unknown selector %s", idf->id_text);
  292. *sdefp = sdef = new_sdef();
  293. sdef->sd_idf = idf;
  294. sdef->sd_stype = sdef->sd_type = error_type;
  295. return sdef;
  296. }
  297. int
  298. uniq_selector(idf_sdef)
  299. register struct sdef *idf_sdef;
  300. {
  301. /* Returns true if idf_sdef (which is guaranteed to exist)
  302. is unique for this level, i.e there is no other selector
  303. on this level with the same name or the other selectors
  304. with the same name have the same offset.
  305. See /usr/src/cmd/sed/sed.h for an example of this absurd
  306. case!
  307. */
  308. register struct sdef *sdef = idf_sdef->next;
  309. while (sdef && sdef->sd_level == idf_sdef->sd_level) {
  310. if ( sdef->sd_type != idf_sdef->sd_type
  311. || sdef->sd_offset != idf_sdef->sd_offset
  312. ) {
  313. return 0; /* ambiguity found */
  314. }
  315. sdef = sdef->next;
  316. }
  317. return 1;
  318. }
  319. #ifndef NOBITFIELD
  320. arith
  321. add_field(szp, fd, fdtpp, idf, stp)
  322. arith *szp; /* size of struct upto here */
  323. register struct field *fd; /* bitfield, containing width */
  324. register struct type **fdtpp; /* type of selector */
  325. struct idf *idf; /* name of selector */
  326. register struct type *stp; /* current struct descriptor */
  327. {
  328. /* The address where this selector is put is returned. If the
  329. selector with specified width does not fit in the word, or
  330. an explicit alignment is given, a new address is needed.
  331. Note that the fields are packed into machine words (according
  332. to the RM.)
  333. */
  334. int bits_in_type = (int) (*fdtpp)->tp_size * 8;
  335. static int field_offset = (arith)0;
  336. static struct type *current_struct = 0;
  337. static int bits_declared; /* nr of bits used in *field_offset */
  338. if (current_struct != stp) {
  339. /* This struct differs from the last one
  340. */
  341. field_busy = 0;
  342. current_struct = stp;
  343. }
  344. if ( fd->fd_width < 0 ||
  345. (fd->fd_width == 0 && !is_anon_idf(idf)) ||
  346. fd->fd_width > bits_in_type
  347. ) {
  348. error("illegal field-width specified");
  349. *fdtpp = error_type;
  350. return field_offset;
  351. }
  352. switch ((*fdtpp)->tp_fund) {
  353. case CHAR:
  354. case SHORT:
  355. case INT:
  356. case ENUM:
  357. case LONG:
  358. /* right type; size OK? */
  359. if ((int) (*fdtpp)->tp_size > (int) word_size) {
  360. error("bit field type %s does not fit in a word",
  361. symbol2str((*fdtpp)->tp_fund));
  362. *fdtpp = error_type;
  363. return field_offset;
  364. }
  365. break;
  366. default:
  367. /* wrong type altogether */
  368. error("field type cannot be %s",
  369. symbol2str((*fdtpp)->tp_fund));
  370. *fdtpp = error_type;
  371. return field_offset;
  372. }
  373. if (field_busy == 0) {
  374. /* align this selector on the next boundary :
  375. the previous selector wasn't a bitfield.
  376. */
  377. field_offset = align(*szp, int_align);
  378. *szp = field_offset + int_size;
  379. stp->tp_align = lcm(stp->tp_align, int_align);
  380. bits_declared = 0;
  381. field_busy = 1;
  382. }
  383. if (fd->fd_width > bits_in_type - bits_declared) {
  384. /* field overflow: fetch next memory unit
  385. */
  386. field_offset = align(*szp, int_align);
  387. *szp = field_offset + int_size;
  388. stp->tp_align = lcm(stp->tp_align, int_align);
  389. bits_declared = fd->fd_width;
  390. }
  391. else
  392. if (fd->fd_width == 0)
  393. /* next field should be aligned on the next boundary.
  394. This will take care that no field will fit in the
  395. space allocated upto here.
  396. */
  397. bits_declared = bits_in_type + 1;
  398. else /* the bitfield fits in the current field */
  399. bits_declared += fd->fd_width;
  400. /* Arrived here, the place where the selector is stored in the
  401. struct is computed.
  402. Now we need a mask to use its value in expressions.
  403. */
  404. *fdtpp = construct_type(FIELD, *fdtpp, (arith)0);
  405. (*fdtpp)->tp_field = fd;
  406. /* Set the mask right shifted. This solution avoids the
  407. problem of having sign extension when using the mask for
  408. extracting the value from the field-int.
  409. Sign extension could occur on some machines when shifting
  410. the mask to the left.
  411. */
  412. if (fd->fd_width >= 8*sizeof(arith)) fd->fd_mask = -1;
  413. else fd->fd_mask = (1L << fd->fd_width) - 1;
  414. if (options['r']) /* adjust the field at the right */
  415. fd->fd_shift = bits_declared - fd->fd_width;
  416. else /* adjust the field at the left */
  417. fd->fd_shift = bits_in_type - bits_declared;
  418. return field_offset;
  419. }
  420. #endif /* NOBITFIELD */
  421. /* some utilities */
  422. int
  423. is_struct_or_union(fund)
  424. register int fund;
  425. {
  426. return fund == STRUCT || fund == UNION;
  427. }
  428. /* Greatest Common Divisor
  429. */
  430. int
  431. gcd(m, n)
  432. register int m, n;
  433. {
  434. register int r;
  435. while (n) {
  436. r = m % n;
  437. m = n;
  438. n = r;
  439. }
  440. return m;
  441. }
  442. /* Least Common Multiple
  443. */
  444. int
  445. lcm(m, n)
  446. register int m, n;
  447. {
  448. return m * (n / gcd(m, n));
  449. }