struct.c 12 KB

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