struct.c 12 KB

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