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