Debian Codegraph @

cc/ccom/init.c

raw symbols SCIP
1 /* $Id: init.c,v 1.110 2021/09/12 10:49:49 gmcgarry Exp $ */
2
3 /*
4 * Copyright (c) 2004, 2007 Anders Magnusson ([email protected]).
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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 *
37 * Redistributions of source code and documentation must retain the above
38 * copyright notice, this list of conditions and the following disclaimer.
39 * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed or owned by Caldera
45 * International, Inc.
46 * Neither the name of Caldera International, Inc. nor the names of other
47 * contributors may be used to endorse or promote products derived from
48 * this software without specific prior written permission.
49 *
50 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
51 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
55 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
60 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #include "pass1.h"
65 #include "unicode.h"
66 #include <string.h>
67
68 #define NODE P1ND
69 #define tfree p1tfree
70 #define nfree p1nfree
71 #define fwalk p1fwalk
72
73 /*
74 * The following machine-dependent routines may be called during
75 * initialization:
76 *
77 * zbits(OFFSZ, int) - sets int bits of zero at position OFFSZ.
78 * infld(CONSZ off, int fsz, CONSZ val)
79 * - sets the bitfield val starting at off and size fsz.
80 * ninval(CONSZ off, int fsz, NODE *)
81 * - prints an integer constant which may have
82 * a label associated with it, located at off and
83 * size fsz.
84 *
85 * Initialization may be of different kind:
86 * - Initialization at compile-time, all values are constants and laid
87 * out in memory. Static or extern variables outside functions.
88 * - Initialization at run-time, written to their values as code.
89 *
90 * Currently run-time-initialized variables are only initialized by using
91 * move instructions. An optimization might be to detect that it is
92 * initialized with constants and therefore copied from readonly memory.
93 */
94
95 /*
96 * The base element(s) of an initialized variable is kept in a linked
97 * list, allocated while initialized.
98 *
99 * When a scalar is found, entries are popped of the instk until it's
100 * possible to find an entry for a new scalar; then onstk() is called
101 * to get the correct type and size of that scalar.
102 *
103 * If a right brace is found, pop the stack until a matching left brace
104 * were found while filling the elements with zeros. This left brace is
105 * also marking where the current level is for designated initializations.
106 *
107 * Position entries are increased when traversing back down into the stack.
108 */
109
110 /*
111 * Good-to-know entries from symtab:
112 * soffset - # of bits from beginning of this structure.
113 */
114
115 /*
116 * TO FIX:
117 * - Alignment of structs on like i386 char members.
118 */
119
120 /*
121 * Struct used in array initialisation.
122 */
123 static struct instk {
124 struct instk *in_prev; /* linked list */
125 struct symtab *in_lnk; /* member in structure initializations */
126 struct symtab *in_sym; /* symtab index */
127 union dimfun *in_df; /* dimenston of array */
128 TWORD in_t; /* type for this level */
129 int in_n; /* number of arrays seen so far */
130 int in_fl; /* flag which says if this level is controlled by {} */
131 } *pstk, pbase;
132
133 int doing_init, statinit;
134 static struct symtab *csym;
135
136 #ifdef PCC_DEBUG
137 static void prtstk(struct instk *in);
138 #define ID(x) if (idebug) printf x
139 #else
140 #define ID(x)
141 #endif
142
143 /*
144 * Linked lists for initializations.
145 */
146 struct ilist {
147 struct ilist *next;
148 CONSZ off; /* bit offset of this entry */
149 int fsz; /* bit size of this entry */
150 NODE *n; /* node containing this data info */
151 };
152
153 struct llist {
154 SLIST_ENTRY(llist) next;
155 CONSZ begsz; /* bit offset of this entry */
156 struct ilist *il;
157 };
158 static SLIST_HEAD(llh, llist) lpole;
159 static CONSZ basesz;
160 static int numents; /* # of array entries allocated */
161
162 static struct initctx {
163 struct initctx *prev;
164 struct instk *pstk;
165 struct symtab *psym;
166 struct llh lpole;
167 CONSZ basesz;
168 int numents;
169 } *inilnk;
170
171 static struct ilist *
172 getil(struct ilist *next, CONSZ b, int sz, NODE *n)
173 {
174 struct ilist *il = tmpalloc(sizeof(struct ilist));
175
176 il->off = b;
177 il->fsz = sz;
178 il->n = n;
179 il->next = next;
180 return il;
181 }
182
183 /*
184 * Allocate a new struct defining a block of initializers appended to the
185 * end of the llist. Return that entry.
186 */
187 static struct llist *
188 getll(void)
189 {
190 struct llist *ll;
191
192 ll = tmpalloc(sizeof(struct llist));
193 ll->begsz = numents * basesz;
194 ll->il = NULL;
195 SLIST_INSERT_LAST(&lpole, ll, next);
196 numents++;
197 return ll;
198 }
199
200 /*
201 * Return structure containing off bitnumber.
202 * Allocate more entries, if needed.
203 */
204 static struct llist *
205 setll(OFFSZ off)
206 {
207 struct llist *ll = NULL;
208
209 /* Ensure that we have enough entries */
210 while (off >= basesz * numents)
211 ll = getll();
212
213 if (ll != NULL && ll->begsz <= off && ll->begsz + basesz > off)
214 return ll;
215
216 SLIST_FOREACH(ll, &lpole, next)
217 if (ll->begsz <= off && ll->begsz + basesz > off)
218 break;
219 return ll; /* ``cannot fail'' */
220 }
221 char *astypnames[] = { 0, 0, "\t.byte", "\t.byte", "\t.short", "\t.short",
222 "\t.word", "\t.word", "\t.long", "\t.long", "\t.quad", "\t.quad",
223 "ERR", "ERR", "ERR",
224 };
225
226 void
227 inval(CONSZ off, int fsz, NODE *p)
228 {
229 struct symtab *sp;
230 CONSZ val;
231 TWORD t;
232
233 #ifndef NO_COMPLEX
234 if (ANYCX(p) && p->n_left->n_right->n_right->n_op == FCON &&
235 p->n_left->n_left->n_right->n_op == FCON) {
236 NODE *r = p->n_left->n_right->n_right;
237 int sz = (int)tsize(r->n_type, r->n_df, r->n_ap);
238 ninval(off, sz, p->n_left->n_left->n_right);
239 ninval(off, sz, r);
240 tfree(p);
241 return;
242 }
243 #endif
244
245 if (p->n_op != ICON && p->n_op != FCON) {
246 uerror("constant required");
247 return;
248 }
249 if (p->n_type == BOOL) {
250 if ((U_CONSZ)glval(p) > 1)
251 slval(p, 1);
252 p->n_type = BOOL_TYPE;
253 }
254 if (ninval(off, fsz, p))
255 return; /* dealt with in local.c */
256 t = p->n_type;
257 if (t > BTMASK)
258 t = INTPTR;
259
260 val = (CONSZ)(glval(p) & SZMASK(sztable[t]));
261 if (t <= ULONGLONG) {
262 sp = p->n_sp;
263 printf(PRTPREF "%s ",astypnames[t]);
264 if (val || sp == NULL)
265 printf(CONFMT, val);
266 if (val && sp != NULL)
267 printf("+");
268 if (sp != NULL) {
269 if ((sp->sclass == STATIC && sp->slevel > 0)) {
270 /* fix problem with &&label not defined yet */
271 int o = sp->soffset;
272 printf(LABFMT, o < 0 ? -o : o);
273 if ((sp->sflags & SMASK) == SSTRING)
274 sp->sflags |= SASG;
275 } else
276 printf("%s", getexname(sp));
277 }
278 printf("\n");
279 } else if (t == FLOAT || t == DOUBLE || t == LDOUBLE) {
280 uint32_t *ufp;
281 int i, nbits;
282
283 ufp = soft_toush(p->n_scon, t, &nbits);
284 for (i = 0; i < sztable[t]; i += SZINT) {
285 printf(PRTPREF "%s %u\n", astypnames[INT],
286 (i < nbits ? ufp[i/SZINT] : 0));
287 }
288 } else
289 cerror("inval: unhandled type %d", (int)t);
290 }
291
292 #ifndef MYBFINIT
293
294 static int inbits;
295 static CONSZ xinval;
296 /*
297 * Initialize a bitfield.
298 * XXX - use U_CONSZ?
299 */
300 void
301 infld(CONSZ off, int fsz, CONSZ val)
302 {
303 #ifdef PCC_DEBUG
304 if (idebug)
305 printf("infld off " CONFMT ", fsz %d, val " CONFMT " inbits %d\n",
306 off, fsz, val, inbits);
307 #endif
308 val &= SZMASK(fsz);
309 #if TARGET_ENDIAN == TARGET_BE
310 while (fsz + inbits >= SZCHAR) {
311 int shsz = SZCHAR-inbits;
312 xinval = (xinval << shsz) | (val >> (fsz - shsz));
313 printf(PRTPREF "%s " CONFMT "\n",
314 astypnames[CHAR], (CONSZ)(xinval & SZMASK(SZCHAR)));
315 fsz -= shsz;
316 val &= SZMASK(fsz);
317 xinval = inbits = 0;
318 }
319 if (fsz) {
320 xinval = (xinval << fsz) | val;
321 inbits += fsz;
322 }
323 #else
324 while (fsz + inbits >= SZCHAR) {
325 int shsz = SZCHAR-inbits;
326 xinval |= (val << inbits);
327 printf(PRTPREF "%s " CONFMT "\n",
328 astypnames[CHAR], (CONSZ)(xinval & SZMASK(SZCHAR)));
329 fsz -= shsz;
330 val >>= shsz;
331 xinval = inbits = 0;
332 }
333 if (fsz) {
334 xinval |= (val << inbits);
335 inbits += fsz;
336 }
337 #endif
338 }
339
340 char *asspace = "\t.space";
341
342 /*
343 * set fsz bits in sequence to zero.
344 */
345 void
346 zbits(OFFSZ off, int fsz)
347 {
348 int m;
349
350 #ifdef PCC_DEBUG
351 if (idebug)
352 printf("zbits off " CONFMT ", fsz %d inbits %d\n", off, fsz, inbits);
353 #endif
354 #if TARGET_ENDIAN == TARGET_BE
355 if ((m = (inbits % SZCHAR))) {
356 m = SZCHAR - m;
357 if (fsz < m) {
358 inbits += fsz;
359 xinval <<= fsz;
360 return;
361 } else {
362 fsz -= m;
363 xinval <<= m;
364 printf(PRTPREF "%s " CONFMT "\n",
365 astypnames[CHAR], (CONSZ)(xinval & SZMASK(SZCHAR)));
366 xinval = inbits = 0;
367 }
368 }
369 #else
370 if ((m = (inbits % SZCHAR))) {
371 m = SZCHAR - m;
372 if (fsz < m) {
373 inbits += fsz;
374 return;
375 } else {
376 fsz -= m;
377 printf(PRTPREF "%s " CONFMT "\n",
378 astypnames[CHAR], (CONSZ)(xinval & SZMASK(SZCHAR)));
379 xinval = inbits = 0;
380 }
381 }
382 #endif
383 if (fsz >= SZCHAR) {
384 printf(PRTPREF "%s " CONFMT "\n", asspace, (CONSZ)(fsz/SZCHAR));
385 fsz -= (fsz/SZCHAR) * SZCHAR;
386 }
387 if (fsz) {
388 xinval = 0;
389 inbits = fsz;
390 }
391 }
392 #endif
393
394 /*
395 * beginning of initialization; allocate space to store initialized data.
396 * remember storage class for writeout in endinit().
397 * p is the newly declarated type.
398 */
399 void
400 beginit(struct symtab *sp)
401 {
402 struct initctx *ict;
403 struct instk *is = &pbase;
404
405 ID(("beginit(%p), sclass %s\n", sp, scnames(sp->sclass)));
406
407 if (pstk) {
408 ID(("beginit: saving ctx pstk %p\n", pstk));
409
410 /* save old context */
411 ict = tmpalloc(sizeof(struct initctx));
412 ict->prev = inilnk;
413 inilnk = ict;
414 ict->pstk = pstk;
415 ict->psym = csym;
416 ict->lpole = lpole;
417 ict->basesz = basesz;
418 ict->numents = numents;
419 is = tmpalloc(sizeof(struct instk));
420 }
421 csym = sp;
422
423 numents = 0; /* no entries in array list */
424 if (ISARY(sp->stype)) {
425 basesz = tsize(DECREF(sp->stype), sp->sdf+1, sp->sap);
426 if (basesz == 0) {
427 uerror("array has incomplete type");
428 basesz = SZINT;
429 }
430 } else
431 basesz = tsize(sp->stype, sp->sdf, sp->sap);
432 SLIST_INIT(&lpole);
433
434 /* first element */
435 if (ISSOU(sp->stype)) {
436 is->in_lnk = strmemb(sp->sap);
437 } else
438 is->in_lnk = NULL;
439 is->in_n = 0;
440 is->in_t = sp->stype;
441 is->in_sym = sp;
442 is->in_df = sp->sdf;
443 is->in_fl = 0;
444 is->in_prev = NULL;
445 pstk = is;
446 doing_init++;
447 if (sp->sclass == STATIC || sp->sclass == EXTDEF)
448 statinit++;
449 ID(("beginit(%p) end\n", sp));
450 }
451
452 /*
453 * Push a new entry on the initializer stack.
454 * The new entry will be "decremented" to the new sub-type of the previous
455 * entry when called.
456 * Popping of entries is done elsewhere.
457 */
458 static void
459 stkpush(void)
460 {
461 struct instk *is;
462 struct symtab *sq, *sp;
463 TWORD t;
464
465 if (pstk == NULL) {
466 sp = csym;
467 t = 0;
468 } else {
469 t = pstk->in_t;
470 sp = pstk->in_sym;
471 }
472
473 #ifdef PCC_DEBUG
474 if (idebug) {
475 printf("stkpush: '%s' %s ", sp->sname, scnames(sp->sclass));
476 tprint(t, 0);
477 }
478 #endif
479
480 /*
481 * Figure out what the next initializer will be, and push it on
482 * the stack. If this is an array, just decrement type, if it
483 * is a struct or union, extract the next element.
484 */
485 is = tmpalloc(sizeof(struct instk));
486 is->in_fl = 0;
487 is->in_n = 0;
488 if (pstk == NULL) {
489 /* stack empty */
490 is->in_lnk = ISSOU(sp->stype) ? strmemb(sp->sap) : NULL;
491 is->in_t = sp->stype;
492 is->in_sym = sp;
493 is->in_df = sp->sdf;
494 } else if (ISSOU(t)) {
495 sq = pstk->in_lnk;
496 if (sq == NULL) {
497 uerror("excess of initializing elements");
498 } else {
499 is->in_lnk = ISSOU(sq->stype) ? strmemb(sq->sap) : NULL;
500 is->in_t = sq->stype;
501 is->in_sym = sq;
502 is->in_df = sq->sdf;
503 }
504 } else if (ISARY(t)) {
505 is->in_lnk = ISSOU(DECREF(t)) ? strmemb(pstk->in_sym->sap) : 0;
506 is->in_t = DECREF(t);
507 is->in_sym = sp;
508 if (pstk->in_df->ddim != NOOFFSET && pstk->in_df->ddim &&
509 pstk->in_n >= pstk->in_df->ddim) {
510 werror("excess of initializing elements");
511 pstk->in_n--;
512 }
513 is->in_df = pstk->in_df+1;
514 } else
515 uerror("too many left braces");
516 is->in_prev = pstk;
517 pstk = is;
518
519 #ifdef PCC_DEBUG
520 if (idebug) {
521 printf(" newtype ");
522 tprint(is->in_t, 0);
523 printf("\n");
524 }
525 #endif
526 }
527
528 /*
529 * pop down to either next level that can handle a new initializer or
530 * to the next braced level.
531 */
532 static void
533 stkpop(void)
534 {
535 #ifdef PCC_DEBUG
536 if (idebug)
537 printf("stkpop\n");
538 #endif
539 for (; pstk; pstk = pstk->in_prev) {
540 if (pstk->in_t == STRTY && pstk->in_lnk != NULL) {
541 pstk->in_lnk = pstk->in_lnk->snext;
542 if (pstk->in_lnk != NULL)
543 break;
544 }
545 if (ISSOU(pstk->in_t) && pstk->in_fl)
546 break; /* need } */
547 if (ISARY(pstk->in_t)) {
548 pstk->in_n++;
549 if (pstk->in_fl)
550 break;
551 if (pstk->in_df->ddim == NOOFFSET ||
552 pstk->in_n < pstk->in_df->ddim)
553 break; /* ger more elements */
554 }
555 }
556 #ifdef PCC_DEBUG
557 if (idebug > 1)
558 prtstk(pstk);
559 #endif
560 }
561
562 /*
563 * Count how many elements an array may consist of.
564 */
565 static int
566 acalc(struct instk *is, int n)
567 {
568 if (is == NULL || !ISARY(is->in_t))
569 return 0;
570 return acalc(is->in_prev, n * is->in_df->ddim) + n * is->in_n;
571 }
572
573 /*
574 * Find current bit offset of the top element on the stack from
575 * the beginning of the aggregate.
576 */
577 static CONSZ
578 findoff(void)
579 {
580 struct instk *is;
581 OFFSZ off;
582
583 #ifdef PCC_DEBUG
584 if (ISARY(pstk->in_t))
585 cerror("findoff on bad type %x", pstk->in_t);
586 #endif
587
588 /*
589 * Offset calculations. If:
590 * - previous type is STRTY, soffset has in-struct offset.
591 * - this type is ARY, offset is ninit*stsize.
592 */
593 for (off = 0, is = pstk; is; is = is->in_prev) {
594 if (is->in_prev && is->in_prev->in_t == STRTY)
595 off += is->in_sym->soffset;
596 if (ISARY(is->in_t)) {
597 /* suesize is the basic type, so adjust */
598 TWORD t = is->in_t;
599 OFFSZ o;
600 while (ISARY(t))
601 t = DECREF(t);
602 if (ISPTR(t)) {
603 o = SZPOINT(t); /* XXX use tsize() */
604 } else {
605 o = tsize(t, is->in_sym->sdf, is->in_sym->sap);
606 }
607 off += o * acalc(is, 1);
608 while (is->in_prev && ISARY(is->in_prev->in_t)) {
609 if (is->in_prev->in_prev &&
610 is->in_prev->in_prev->in_t == STRTY)
611 off += is->in_sym->soffset;
612 is = is->in_prev;
613 }
614 }
615 }
616 #ifdef PCC_DEBUG
617 if (idebug>1) {
618 printf("findoff: off " CONFMT "\n", off);
619 prtstk(pstk);
620 }
621 #endif
622 return off;
623 }
624
625 /*
626 * Insert the node p with size fsz at position off.
627 * Bit fields are already dealt with, so a node of correct type
628 * with correct alignment and correct bit offset is given.
629 */
630 static void
631 nsetval(CONSZ off, int fsz, NODE *p)
632 {
633 struct llist *ll;
634 struct ilist *il;
635
636 if (idebug>1)
637 printf("setval: off " CONFMT " fsz %d p %p\n", off, fsz, p);
638
639 if (fsz == 0)
640 return;
641
642 ll = setll(off);
643 off -= ll->begsz;
644 if (ll->il == NULL) {
645 ll->il = getil(NULL, off, fsz, p);
646 } else {
647 il = ll->il;
648 if (il->off > off) {
649 ll->il = getil(ll->il, off, fsz, p);
650 } else {
651 for (il = ll->il; il->next; il = il->next)
652 if (il->off <= off && il->next->off > off)
653 break;
654 if (il->off == off) {
655 /* replace */
656 nfree(il->n);
657 il->n = p;
658 } else
659 il->next = getil(il->next, off, fsz, p);
660 }
661 }
662 }
663
664 /*
665 * take care of generating a value for the initializer p
666 * inoff has the current offset (last bit written)
667 * in the current word being generated
668 * Returns the offset.
669 */
670 CONSZ
671 scalinit(NODE *p)
672 {
673 CONSZ woff;
674 NODE *q;
675 int fsz;
676
677 ID(("scalinit(%p)\n", p));
678 #ifdef PCC_DEBUG
679 if (idebug > 2) {
680 fwalk(p, eprint, 0);
681 prtstk(pstk);
682 }
683 #endif
684
685 if (nerrors)
686 return 0;
687
688 p = optim(p);
689
690 #ifdef notdef /* leave to the target to decide if useable */
691 if (csym->sclass != AUTO && p->n_op != ICON &&
692 p->n_op != FCON && p->n_op != NAME)
693 cerror("scalinit not leaf");
694 #endif
695
696 /* Out of elements? */
697 if (pstk == NULL) {
698 uerror("excess of initializing elements");
699 return 0;
700 }
701
702 /*
703 * Get to the simple type if needed.
704 */
705 while (ISSOU(pstk->in_t) || ISARY(pstk->in_t)) {
706 stkpush();
707 /* If we are doing auto struct init */
708 if (ISSOU(pstk->in_t) && ISSOU(p->n_type) &&
709 suemeq(pstk->in_sym->sap, p->n_ap)) {
710 pstk->in_lnk = NULL; /* this elem is initialized */
711 break;
712 }
713 }
714
715 if (ISSOU(pstk->in_t) == 0) {
716 /* let buildtree do typechecking (and casting) */
717 q = block(NAME, NIL,NIL, pstk->in_t, pstk->in_df,
718 pstk->in_sym->sap);
719 p = buildtree(ASSIGN, q, p);
720 nfree(p->n_left);
721 q = p->n_right;
722 nfree(p);
723 } else
724 q = p;
725
726 q = optloop(q);
727
728 woff = findoff();
729
730 /* bitfield sizes are special */
731 if (pstk->in_sym->sclass & FIELD)
732 fsz = -(pstk->in_sym->sclass & FLDSIZ);
733 else
734 fsz = (int)tsize(pstk->in_t, pstk->in_sym->sdf,
735 pstk->in_sym->sap);
736
737 nsetval(woff, fsz, q);
738 if (q->n_op == ICON && q->n_sp &&
739 ((q->n_sp->sflags & SMASK) == SSTRING))
740 q->n_sp->sflags |= SASG;
741
742 stkpop();
743 ID(("scalinit end(%p)\n", q));
744 return woff;
745 }
746
747 /*
748 * Generate code to insert a value into a bitfield.
749 */
750 static void
751 insbf(OFFSZ off, int fsz, int val)
752 {
753 struct symtab sym;
754 NODE *p, *r;
755 TWORD typ;
756
757 #ifdef PCC_DEBUG
758 if (idebug > 1)
759 printf("insbf: off " CONFMT " fsz %d val %d\n", off, fsz, val);
760 #endif
761
762 if (fsz == 0)
763 return;
764
765 /* small opt: do char instead of bf asg */
766 if ((off & (ALCHAR-1)) == 0 && fsz == SZCHAR)
767 typ = CHAR;
768 else
769 typ = INT;
770 /* Fake a struct reference */
771 p = buildtree(ADDROF, nametree(csym), NIL);
772 sym.stype = typ;
773 sym.squal = 0;
774 sym.sdf = 0;
775 sym.sap = NULL;
776 sym.soffset = (int)off;
777 sym.sclass = (char)(typ == INT ? FIELD | fsz : MOU);
778 r = xbcon(0, &sym, typ);
779 p = block(STREF, p, r, INT, 0, 0);
780 ecomp(buildtree(ASSIGN, stref(p), bcon(val)));
781 }
782
783 /*
784 * Clear a bitfield, starting at off and size fsz.
785 */
786 static void
787 clearbf(OFFSZ off, OFFSZ fsz)
788 {
789 /* Pad up to the next even initializer */
790 if ((off & (ALCHAR-1)) || (fsz < SZCHAR)) {
791 int ba = (int)(((off + (SZCHAR-1)) & ~(SZCHAR-1)) - off);
792 if (ba > fsz)
793 ba = (int)fsz;
794 insbf(off, ba, 0);
795 off += ba;
796 fsz -= ba;
797 }
798 while (fsz >= SZCHAR) {
799 insbf(off, SZCHAR, 0);
800 off += SZCHAR;
801 fsz -= SZCHAR;
802 }
803 if (fsz)
804 insbf(off, fsz, 0);
805 }
806
807 /*
808 * final step of initialization.
809 * print out init nodes and generate copy code (if needed).
810 */
811 void
812 endinit(int seg)
813 {
814 struct llist *ll;
815 struct ilist *il;
816 int fsz;
817 OFFSZ lastoff, tbit;
818
819 ID(("endinit()\n"));
820
821 /* Calculate total block size */
822 if (ISARY(csym->stype) && csym->sdf->ddim == NOOFFSET) {
823 tbit = numents*basesz; /* open-ended arrays */
824 csym->sdf->ddim = numents;
825 if (csym->sclass == AUTO) { /* Get stack space */
826 csym->soffset = NOOFFSET;
827 oalloc(csym, &autooff);
828 }
829 } else
830 tbit = tsize(csym->stype, csym->sdf, csym->sap);
831
832 /* Setup symbols */
833 if (csym->sclass != AUTO) {
834 locctr(seg ? UDATA : DATA, csym);
835 defloc(csym);
836 }
837
838 /* Traverse all entries and print'em out */
839 lastoff = 0;
840 SLIST_FOREACH(ll, &lpole, next) {
841 for (il = ll->il; il; il = il->next) {
842 #ifdef PCC_DEBUG
843 if (idebug > 1) {
844 printf("off " CONFMT " size %d val " CONFMT " type ",
845 ll->begsz+il->off, il->fsz, glval(il->n));
846 tprint(il->n->n_type, 0);
847 printf("\n");
848 }
849 #endif
850 fsz = il->fsz;
851 if (csym->sclass == AUTO) {
852 struct symtab sym;
853 NODE *p, *r, *n;
854
855 if (ll->begsz + il->off > lastoff)
856 clearbf(lastoff,
857 (ll->begsz + il->off) - lastoff);
858
859 /* Fake a struct reference */
860 p = buildtree(ADDROF, nametree(csym), NIL);
861 n = il->n;
862 sym.stype = n->n_type;
863 sym.squal = n->n_qual;
864 sym.sdf = n->n_df;
865 sym.sap = n->n_ap;
866 sym.soffset = (int)(ll->begsz + il->off);
867 sym.sclass = (char)(fsz < 0 ? FIELD | -fsz : 0);
868 r = xbcon(0, &sym, INT);
869 p = block(STREF, p, r, INT, 0, 0);
870 ecomp(buildtree(ASSIGN, stref(p), il->n));
871 if (fsz < 0)
872 fsz = -fsz;
873
874 } else {
875 if (ll->begsz + il->off > lastoff)
876 zbits(lastoff,
877 (ll->begsz + il->off) - lastoff);
878 if (fsz < 0) {
879 fsz = -fsz;
880 infld(il->off, fsz, glval(il->n));
881 } else
882 inval(il->off, fsz, il->n);
883 tfree(il->n);
884 }
885 lastoff = ll->begsz + il->off + fsz;
886 }
887 }
888 if (csym->sclass == AUTO) {
889 clearbf(lastoff, tbit-lastoff);
890 } else
891 zbits(lastoff, tbit-lastoff);
892
893 doing_init--;
894 if (csym->sclass == STATIC || csym->sclass == EXTDEF)
895 statinit--;
896 endictx();
897 ID(("endinit() end\n"));
898 }
899
900 void
901 endictx(void)
902 {
903 struct initctx *ict = inilnk;
904
905 if (ict == NULL)
906 return;
907
908 pstk = ict->pstk;
909 csym = ict->psym;
910 lpole = ict->lpole;
911 basesz = ict->basesz;
912 numents = ict->numents;
913 inilnk = inilnk->prev;
914 #ifdef PCC_DEBUG
915 if (idebug)
916 printf("endinit: restoring ctx pstk %p\n", pstk);
917 #endif
918 }
919
920 /*
921 * process an initializer's left brace
922 */
923 void
924 ilbrace(void)
925 {
926 #ifdef PCC_DEBUG
927 if (idebug)
928 printf("ilbrace()\n");
929 #endif
930
931 if (pstk == NULL)
932 return;
933
934 stkpush();
935 pstk->in_fl = 1; /* mark lbrace */
936 #ifdef PCC_DEBUG
937 if (idebug > 1)
938 prtstk(pstk);
939 #endif
940 }
941
942 /*
943 * called when a '}' is seen
944 */
945 void
946 irbrace(void)
947 {
948 #ifdef PCC_DEBUG
949 if (idebug)
950 printf("irbrace()\n");
951 if (idebug > 2)
952 prtstk(pstk);
953 #endif
954
955 if (pstk == NULL)
956 return;
957
958 /* Got right brace, search for corresponding in the stack */
959 for (; pstk->in_prev != NULL; pstk = pstk->in_prev) {
960 if(!pstk->in_fl)
961 continue;
962
963 /* we have one now */
964
965 pstk->in_fl = 0; /* cancel { */
966 if (ISARY(pstk->in_t))
967 pstk->in_n = pstk->in_df->ddim;
968 else if (pstk->in_t == STRTY) {
969 while (pstk->in_lnk != NULL &&
970 pstk->in_lnk->snext != NULL)
971 pstk->in_lnk = pstk->in_lnk->snext;
972 }
973 stkpop();
974 return;
975 }
976 }
977
978 /*
979 * Search for next element given. If anon structs, must return each
980 * anon struct to make offset calculation happy.
981 */
982 static struct symtab *
983 felem(struct symtab *sp, char *n)
984 {
985 struct symtab *rs;
986
987 for (; sp; sp = sp->snext) {
988 if (sp->sname[0] == '*') {
989 if ((rs = felem(strattr(sp->sap)->amlist, n)) != NULL)
990 return sp;
991 } else if (sp->sname == n)
992 return sp;
993 }
994 return sp;
995 }
996
997 /*
998 * Create a new init stack based on given elements.
999 */
1000 static void
1001 mkstack(NODE *p)
1002 {
1003
1004 ID(("mkstack: %p\n", p));
1005 #ifdef PCC_DEBUG
1006 if (idebug > 1 && p)
1007 fwalk(p, eprint, 0);
1008 #endif
1009
1010 if (p == NULL)
1011 return;
1012 mkstack(p->n_left);
1013
1014 switch (p->n_op) {
1015 case LB: /* Array index */
1016 if (p->n_right->n_op != ICON)
1017 cerror("mkstack");
1018 if (!ISARY(pstk->in_t))
1019 uerror("array indexing non-array");
1020 pstk->in_n = (int)glval(p->n_right);
1021 nfree(p->n_right);
1022 break;
1023
1024 case NAME:
1025 ID(("mkstack name %s\n", (char *)p->n_sp));
1026 if (pstk->in_lnk) {
1027 pstk->in_lnk = felem(pstk->in_lnk, (char *)p->n_sp);
1028 if (pstk->in_lnk == NULL)
1029 uerror("member missing");
1030 } else {
1031 uerror("not a struct/union");
1032 }
1033 break;
1034 default:
1035 cerror("mkstack2");
1036 }
1037 nfree(p);
1038 stkpush();
1039 ID(("mkstack end: %p\n", p));
1040 }
1041
1042 /*
1043 * Initialize a specific element, as per C99.
1044 */
1045 void
1046 desinit(NODE *p)
1047 {
1048 int op = p->n_op;
1049
1050 ID(("desinit beg %p\n", p));
1051 if (pstk == NULL)
1052 stkpush(); /* passed end of array */
1053 while (pstk->in_prev && pstk->in_fl == 0)
1054 pstk = pstk->in_prev; /* Empty stack */
1055
1056 if (ISSOU(pstk->in_t))
1057 pstk->in_lnk = strmemb(pstk->in_sym->sap);
1058
1059 mkstack(p); /* Setup for assignment */
1060
1061 /* pop one step if SOU, ilbrace will push */
1062 if (op == NAME || op == LB)
1063 pstk = pstk->in_prev;
1064
1065 ID(("desinit end %p\n", p));
1066 #ifdef PCC_DEBUG
1067 if (idebug > 1)
1068 prtstk(pstk);
1069 #endif
1070 }
1071
1072 /*
1073 * Convert a string to an array of char/wchar for asginit.
1074 */
1075 static void
1076 strcvt(NODE *p)
1077 {
1078 NODE *q = p;
1079 char *s;
1080 int i;
1081
1082 #ifdef mach_arm
1083 /* XXX */
1084 if (p->n_op == UMUL && p->n_left->n_op == ADDROF)
1085 p = p->n_left->n_left;
1086 #endif
1087
1088 for (s = p->n_sp->sname; *s != 0; ) {
1089 if (p->n_type == ARY+WCHAR_TYPE)
1090 i = (int)u82cp(&s);
1091 else if (*s == '\\')
1092 i = esccon(&s);
1093 else
1094 i = (unsigned char)*s++;
1095 asginit(bcon(i));
1096 }
1097 tfree(q);
1098 }
1099
1100 /*
1101 * Do an assignment to a struct element.
1102 */
1103 void
1104 asginit(NODE *p)
1105 {
1106 int g;
1107
1108 #ifdef PCC_DEBUG
1109 if (idebug)
1110 printf("asginit %p\n", p);
1111 if (idebug > 1 && p)
1112 fwalk(p, eprint, 0);
1113 #endif
1114
1115 /* convert string to array of char/wchar */
1116 if (p && (DEUNSIGN(p->n_type) == ARY+CHAR ||
1117 p->n_type == ARY+WCHAR_TYPE)) {
1118 struct instk *is;
1119 TWORD t;
1120
1121 t = p->n_type == ARY+WCHAR_TYPE ? ARY+WCHAR_TYPE : ARY+CHAR;
1122 /*
1123 * ...but only if next element is ARY+CHAR, otherwise
1124 * just fall through.
1125 */
1126
1127 /* HACKHACKHACK */
1128 is = pstk;
1129
1130 if (pstk == NULL)
1131 stkpush();
1132 while (ISSOU(pstk->in_t) || ISARY(pstk->in_t))
1133 stkpush();
1134 if (pstk->in_prev &&
1135 (DEUNSIGN(pstk->in_prev->in_t) == t ||
1136 pstk->in_prev->in_t == t)) {
1137 pstk = pstk->in_prev;
1138 if ((g = pstk->in_fl) == 0)
1139 pstk->in_fl = 1; /* simulate ilbrace */
1140
1141 strcvt(p);
1142 if (pstk->in_df->ddim == NOOFFSET)
1143 asginit(bcon(0));
1144 if (g == 0)
1145 irbrace(); /* will fill with zeroes */
1146 return;
1147 } else
1148 pstk = is; /* no array of char */
1149 /* END HACKHACKHACK */
1150 }
1151
1152 if (p == NULL) { /* only end of compound stmt */
1153 irbrace();
1154 } else /* assign next element */
1155 scalinit(p);
1156 }
1157
1158 #ifdef PCC_DEBUG
1159 void
1160 prtstk(struct instk *in)
1161 {
1162 int i, o = 0;
1163
1164 printf("init stack:\n");
1165 for (; in != NULL; in = in->in_prev) {
1166 for (i = 0; i < o; i++)
1167 printf(" ");
1168 printf("%p) '%s' ", in, in->in_sym->sname);
1169 tprint(in->in_t, 0);
1170 printf(" %s ", scnames(in->in_sym->sclass));
1171 if (in->in_df /* && in->in_df->ddim */)
1172 printf("arydim=%d ", in->in_df->ddim);
1173 printf("ninit=%d ", in->in_n);
1174 if (BTYPE(in->in_t) == STRTY || ISARY(in->in_t))
1175 printf("stsize=%d ",
1176 (int)tsize(in->in_t, in->in_df, in->in_sym->sap));
1177 if (in->in_fl) printf("{ ");
1178 printf("soff=%d ", in->in_sym->soffset);
1179 if (in->in_t == STRTY) {
1180 if (in->in_lnk)
1181 printf("curel %s ", in->in_lnk->sname);
1182 else
1183 printf("END struct");
1184 }
1185 printf("\n");
1186 o++;
1187 }
1188 }
1189 #endif
1190
1191 /*
1192 * Do a simple initialization.
1193 * At block 0, just print out the value, at higher levels generate
1194 * appropriate code.
1195 */
1196 void
1197 simpleinit(struct symtab *sp, NODE *p)
1198 {
1199 NODE *q, *r, *nt;
1200 TWORD t;
1201 int sz;
1202
1203 /* May be an initialization of an array of char by a string */
1204 if ((DEUNSIGN(p->n_type) == ARY+CHAR &&
1205 DEUNSIGN(sp->stype) == ARY+CHAR) ||
1206 (DEUNSIGN(p->n_type) == DEUNSIGN(ARY+WCHAR_TYPE) &&
1207 DEUNSIGN(sp->stype) == DEUNSIGN(ARY+WCHAR_TYPE))) {
1208 /* Handle "aaa" as { 'a', 'a', 'a' } */
1209 beginit(sp);
1210 strcvt(p);
1211 if (csym->sdf->ddim == NOOFFSET)
1212 scalinit(bcon(0)); /* Null-term arrays */
1213 endinit(0);
1214 return;
1215 }
1216
1217 nt = nametree(sp);
1218 switch (sp->sclass) {
1219 case STATIC:
1220 case EXTDEF:
1221 q = nt;
1222 locctr(DATA, sp);
1223 defloc(sp);
1224 #ifndef NO_COMPLEX
1225 if (ANYCX(q) || ANYCX(p)) {
1226 r = cxop(ASSIGN, q, p);
1227 /* XXX must unwind the code generated here */
1228 /* We can rely on correct code generated */
1229 p = r->n_left->n_right->n_left;
1230 r->n_left->n_right->n_left = bcon(0);
1231 tfree(r);
1232 r = p->n_left->n_right;
1233 sz = (int)tsize(r->n_type, r->n_df, r->n_ap);
1234 inval(0, sz, r);
1235 inval(0, sz, p->n_right->n_right);
1236 tfree(p);
1237 break;
1238 } else if (ISITY(p->n_type) || ISITY(q->n_type)) {
1239 /* XXX merge this with code from imop() */
1240 int li = 0, ri = 0;
1241 if (ISITY(p->n_type))
1242 li = 1, p->n_type = p->n_type - (FIMAG-FLOAT);
1243 if (ISITY(q->n_type))
1244 ri = 1, q->n_type = q->n_type - (FIMAG-FLOAT);
1245 if (!(li && ri)) {
1246 tfree(p);
1247 p = bcon(0);
1248 }
1249 /* continue below */
1250 }
1251 #endif
1252 #ifdef TARGET_TIMODE
1253 struct attr *ap;
1254 if ((ap = attr_find(sp->sap, GCC_ATYP_MODE)) &&
1255 strcmp(ap->aa[0].sarg, "TI") == 0) {
1256 if (p->n_op != ICON)
1257 uerror("need to handle TImode initializer ");
1258 sz = (int)tsize(sp->stype, sp->sdf, sp->sap);
1259 p->n_type = ctype(LONGLONG);
1260 inval(0, sz/2, p);
1261 slval(p, 0); /* XXX fix signed types */
1262 inval(0, sz/2, p);
1263 tfree(p);
1264 tfree(q);
1265 break;
1266 }
1267 #endif
1268 if (p->n_op == NAME && p->n_sp &&
1269 (p->n_sp->sflags & SMASK) == SSTRING)
1270 p->n_sp->sflags |= SASG;
1271 p = optloop(buildtree(ASSIGN, nt, p));
1272 q = p->n_right;
1273 t = q->n_type;
1274 sz = (int)tsize(t, q->n_df, q->n_ap);
1275 inval(0, sz, q);
1276 tfree(p);
1277 break;
1278
1279 case AUTO:
1280 case REGISTER:
1281 if (ISARY(sp->stype))
1282 cerror("no array init");
1283 q = nt;
1284 #ifdef TARGET_TIMODE
1285 if ((r = gcc_eval_timode(ASSIGN, q, p)) != NULL)
1286 ;
1287 else
1288 #endif
1289 #ifndef NO_COMPLEX
1290
1291 if (ANYCX(q) || ANYCX(p))
1292 r = cxop(ASSIGN, q, p);
1293 else if (ISITY(p->n_type) || ISITY(q->n_type))
1294 r = imop(ASSIGN, q, p);
1295 else
1296 #endif
1297 r = buildtree(ASSIGN, q, p);
1298 ecomp(r);
1299 break;
1300
1301 default:
1302 uerror("illegal initialization");
1303 }
1304 }