Debian Codegraph @

cc/ccom/pftn.c

raw symbols SCIP
1 /* $Id: pftn.c,v 1.439 2022/03/27 20:11:30 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 /*
27 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * Redistributions of source code and documentation must retain the above
34 * copyright notice, this list of conditions and the following disclaimer.
35 * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditionsand the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed or owned by Caldera
41 * International, Inc.
42 * Neither the name of Caldera International, Inc. nor the names of other
43 * contributors may be used to endorse or promote products derived from
44 * this software without specific prior written permission.
45 *
46 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
47 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
51 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
56 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 */
59
60 /*
61 * Many changes from the 32V sources, among them:
62 * - New symbol table manager (moved to another file).
63 * - Prototype saving/checks.
64 */
65
66 # include "pass1.h"
67 #include "unicode.h"
68
69 #include <stddef.h>
70 #include <stdlib.h>
71
72 #include "cgram.h"
73
74 #define NODE P1ND
75 #define tfree p1tfree
76 #define nfree p1nfree
77 #define ccopy p1tcopy
78 #define flist p1flist
79 #define fwalk p1fwalk
80
81 struct symtab *cftnsp;
82 int arglistcnt, dimfuncnt; /* statistics */
83 int symtabcnt, suedefcnt; /* statistics */
84 int lcommsz, blkalloccnt;
85 int autooff, /* the next unused automatic offset */
86 maxautooff, /* highest used automatic offset in function */
87 argoff; /* the next unused argument offset */
88 int retlab = NOLAB; /* return label for subroutine */
89 int brklab;
90 int contlab;
91 int flostat;
92 int blevel;
93 int reached, prolab;
94
95 struct params;
96
97 #define MKTY(p, t, d, s) r = p1alloc(); *r = *p; \
98 r = argcast(r, t, d, s); *p = *r; nfree(r);
99
100 /*
101 * Linked list stack while reading in structs.
102 */
103 struct rstack {
104 struct rstack *rnext;
105 int rsou;
106 int rstr;
107 struct symtab *rsym;
108 struct symtab *rb;
109 struct attr *ap;
110 int flags;
111 #define LASTELM 1
112 } *rpole;
113
114 /*
115 * Array for parameter declarations.
116 */
117 #define ARGSZ 16
118 static int maxastore = ARGSZ;
119 static struct symtab *argstore[ARGSZ];
120 static struct symtab **argptr = argstore;
121
122 static int nparams;
123
124 /* defines used for getting things off of the initialization stack */
125
126 #define MAXDF 12 /* 5.2.4.1 */
127 NODE *arrstk[10];
128 static int intcompare;
129 NODE *parlink;
130
131 void fixtype(NODE *p, int class);
132 int fixclass(int class, TWORD type);
133 static void dynalloc(struct symtab *p, int *poff);
134 int isdyn(struct symtab *p);
135 void inforce(OFFSZ n);
136 void vfdalign(int n);
137 #ifdef PCC_DEBUG
138 static void alprint(union arglist *al, int in);
139 #endif
140 static void lcommadd(struct symtab *sp);
141 static NODE *mkcmplx(NODE *p, TWORD dt);
142 static void cxargfixup(NODE *arg, TWORD dt, struct attr *ap);
143 extern int fun_inline;
144
145 void
146 defid(NODE *q, int class)
147 {
148 defid2(q, class, 0);
149 }
150
151 static void
152 addsoname(struct symtab *sp, char *so)
153 {
154 struct attr *ap = attr_new(ATTR_SONAME, 1);
155 ap->sarg(0) = so;
156 sp->sap = attr_add(sp->sap, ap);
157 }
158
159 /*
160 * Declaration of an identifier. Handles redeclarations, hiding,
161 * incomplete types and forward declarations.
162 *
163 * q is a TYPE node setup after parsing with n_type, n_df and n_ap.
164 * n_sp is a pointer to the not-yet initalized symbol table entry
165 * unless it's a redeclaration or supposed to hide a variable.
166 */
167
168 void
169 defid2(NODE *q, int class, char *astr)
170 {
171 struct attr *ap;
172 struct symtab *p;
173 TWORD type, qual;
174 TWORD stp, stq;
175 int scl, i;
176 union dimfun *dsym, *ddef;
177 int slev, temp, changed;
178
179 if (q == NIL)
180 return; /* an error was detected */
181
182 #ifdef GCC_COMPAT
183 gcc_modefix(q);
184 #endif
185 p = q->n_sp;
186
187 if (p->sname == NULL)
188 cerror("defining null identifier");
189
190 #ifdef PCC_DEBUG
191 if (ddebug) {
192 printf("defid(%s '%s'(%p), ", p->sname, "soname" , p);
193 tprint(q->n_type, q->n_qual);
194 printf(", %s, (%p)), level %d\n\t", scnames(class),
195 q->n_df, blevel);
196 #ifdef GCC_COMPAT
197 dump_attr(q->n_ap);
198 #endif
199 }
200 #endif
201
202 fixtype(q, class);
203
204 type = q->n_type;
205 qual = q->n_qual;
206 class = fixclass(class, type);
207
208 stp = p->stype;
209 stq = p->squal;
210 slev = p->slevel;
211
212 #ifdef PCC_DEBUG
213 if (ddebug) {
214 printf(" modified to ");
215 tprint(type, qual);
216 printf(", %s\n", scnames(class));
217 printf(" previous def'n: ");
218 tprint(stp, stq);
219 printf(", %s, (%p,%p)), level %d\n",
220 scnames(p->sclass), p->sdf, p->sap, slev);
221 }
222 #endif
223
224 if (blevel == 1) {
225 switch (class) {
226 default:
227 if (!(class&FIELD) && !ISFTN(type))
228 uerror("declared argument %s missing",
229 p->sname );
230 break;
231 case MOS:
232 case MOU:
233 cerror("field5");
234 case TYPEDEF:
235 case PARAM:
236 break;
237 }
238 }
239
240 if (stp == UNDEF)
241 goto enter; /* New symbol */
242
243 if (type != stp)
244 goto mismatch;
245
246 if (blevel > slev && (class == AUTO || class == REGISTER))
247 /* new scope */
248 goto mismatch;
249
250 /*
251 * test (and possibly adjust) dimensions.
252 * also check that prototypes are correct.
253 */
254 dsym = p->sdf;
255 ddef = q->n_df;
256 changed = 0;
257 for (temp = type; temp & TMASK; temp = DECREF(temp)) {
258 if (ISARY(temp)) {
259 if (dsym->ddim == NOOFFSET) {
260 dsym->ddim = ddef->ddim;
261 changed = 1;
262 } else if (ddef->ddim != NOOFFSET &&
263 dsym->ddim!=ddef->ddim) {
264 goto mismatch;
265 }
266 ++dsym;
267 ++ddef;
268 } else if (ISFTN(temp)) {
269 /* add a late-defined prototype here */
270 if (!oldstyle && dsym->dfun == NULL)
271 dsym->dfun = ddef->dfun;
272 if (!oldstyle && ddef->dfun != NULL &&
273 chkftn(dsym->dfun, ddef->dfun))
274 uerror("declaration doesn't match prototype");
275 dsym++, ddef++;
276 }
277 }
278 #ifdef STABS
279 if (changed && gflag)
280 stabs_chgsym(p); /* symbol changed */
281 #endif
282
283 /* check that redeclarations are to the same structure */
284 if (temp == STRTY || temp == UNIONTY) {
285 if (strmemb(p->sap) != strmemb(q->n_ap))
286 goto mismatch;
287 }
288
289 scl = p->sclass;
290
291 #ifdef PCC_DEBUG
292 if (ddebug)
293 printf(" previous class: %s\n", scnames(scl));
294 #endif
295
296 /*
297 * Its allowed to add attributes to existing declarations.
298 * Be careful though not to trash existing attributes.
299 * XXX - code below is probably not correct.
300 */
301 if (p->sap && p->sap->atype <= ATTR_MAX) {
302 /* nothing special, just overwrite */
303 p->sap = q->n_ap;
304 } else {
305 if (p->slevel == blevel) {
306 for (ap = q->n_ap; ap; ap = ap->next) {
307 if (ap->atype > ATTR_MAX)
308 p->sap = attr_add(p->sap, attr_dup(ap));
309 }
310 } else
311 p->sap = q->n_ap;
312 }
313
314 if (class & FIELD)
315 cerror("field1");
316 switch(class) {
317
318 case EXTERN:
319 if (astr)
320 addsoname(p, astr);
321 switch( scl ){
322 case STATIC:
323 case USTATIC:
324 if( slev==0 )
325 goto done;
326 break;
327 case EXTDEF:
328 case EXTERN:
329 goto done;
330 case SNULL:
331 if (p->sflags & SINLINE) {
332 p->sclass = EXTDEF;
333 inline_ref(p);
334 goto done;
335 }
336 break;
337 }
338 break;
339
340 case STATIC:
341 if (astr)
342 addsoname(p, astr);
343 if (scl==USTATIC || (scl==EXTERN && blevel==0)) {
344 p->sclass = STATIC;
345 goto done;
346 }
347 if (changed || (scl == STATIC && blevel == slev))
348 goto done; /* identical redeclaration */
349 break;
350
351 case USTATIC:
352 if (scl==STATIC || scl==USTATIC)
353 goto done;
354 break;
355
356 case TYPEDEF:
357 if (scl == class)
358 goto done;
359 break;
360
361 case MOU:
362 case MOS:
363 cerror("field6");
364
365 case EXTDEF:
366 switch (scl) {
367 case EXTERN:
368 p->sclass = EXTDEF;
369 goto done;
370 case USTATIC:
371 p->sclass = STATIC;
372 goto done;
373 case SNULL:
374 #ifdef GCC_COMPAT
375 /*
376 * Handle redeclarations of inlined functions.
377 * This is allowed if the previous declaration is of
378 * type gnu_inline.
379 */
380 if (attr_find(p->sap, GCC_ATYP_GNU_INLINE))
381 goto done;
382 #endif
383 break;
384 }
385 break;
386
387 case AUTO:
388 case REGISTER:
389 break; /* mismatch.. */
390 case SNULL:
391 if (fun_inline && ISFTN(type)) {
392 if (scl == EXTERN) {
393 p->sclass = EXTDEF;
394 inline_ref(p);
395 }
396 goto done;
397 }
398 break;
399 }
400
401 mismatch:
402
403 /*
404 * Only allowed for automatic variables.
405 */
406 if ((blevel == 2 && slev == 1) || blevel <= slev || class == EXTERN) {
407 uerror("redeclaration of %s", p->sname);
408 return;
409 }
410 if ((ISFTN(p->stype) && ISFTN(type)) ||
411 (!ISFTN(p->stype) && !ISFTN(type)))
412 warner(Wshadow, p->sname, p->slevel ? "local" : "global");
413 q->n_sp = p = hide(p);
414
415 enter: /* make a new entry */
416
417 if (type == VOID && class != TYPEDEF)
418 uerror("void not allowed for variables");
419
420 #ifdef PCC_DEBUG
421 if(ddebug)
422 printf(" new entry made\n");
423 #endif
424 p->stype = type;
425 p->squal = qual;
426 p->sclass = (char)class;
427 p->slevel = (char)blevel;
428 p->soffset = NOOFFSET;
429 if (q->n_ap)
430 p->sap = attr_add(q->n_ap, p->sap);
431
432 /* copy dimensions */
433 p->sdf = q->n_df;
434 /* Do not save param info for old-style functions */
435 if (ISFTN(type) && oldstyle)
436 p->sdf->dfun = NULL;
437
438 /* allocate offsets */
439 if (class&FIELD) {
440 cerror("field2"); /* new entry */
441 } else switch (class) {
442
443 case REGISTER:
444 p->sclass = class = AUTO;
445 #ifndef PASS1
446 if (astr != NULL) {
447 #ifdef GCC_COMPAT
448 if (blevel == 0)
449 werror("no register assignment (yet)");
450 else if (astr != NULL) {
451 for (i = 0; i < MAXREGS; i++) {
452 extern char *rnames[]; /* XXX */
453 if (strcmp(rnames[i], astr) == 0) {
454 p->sflags |= SINREG;
455 p->soffset = i;
456 break;
457 }
458 }
459 if (i != MAXREGS)
460 break;
461 werror("reg '%s' invalid", astr);
462 }
463 #endif
464 }
465 #endif
466 /* FALLTHROUGH */
467 case AUTO:
468 if (isdyn(p)) {
469 p->sflags |= SDYNARRAY;
470 dynalloc(p, &autooff);
471 } else if ((i = tnodenr(p)) != 0) {
472 p->soffset = i;
473 p->sflags |= STNODE;
474 } else
475 oalloc(p, &autooff);
476 break;
477
478 case PARAM:
479 if (q->n_type != FARG)
480 oalloc(p, &argoff);
481 break;
482
483 case STATIC:
484 case EXTDEF:
485 case EXTERN:
486 p->soffset = getlab();
487 /* FALLTHROUGH */
488 case USTATIC:
489 if (astr)
490 addsoname(p, astr);
491 break;
492
493 case MOU:
494 case MOS:
495 cerror("field7");
496 case SNULL:
497 #ifdef notdef
498 if (fun_inline) {
499 p->slevel = 1;
500 p->soffset = getlab();
501 }
502 #endif
503 break;
504 }
505
506 #ifdef STABS
507 if (gflag && p->stype != FARG)
508 stabs_newsym(p);
509 #endif
510
511 done:
512 fixdef(p); /* Leave last word to target */
513 #ifndef HAVE_WEAKREF
514 {
515 struct attr *at;
516
517 /* Refer renamed function */
518 if ((at = attr_find(p->sap, GCC_ATYP_WEAKREF)))
519 addsoname(p, at->sarg(0));
520 }
521 #endif
522 #ifdef PCC_DEBUG
523 if (ddebug) {
524 printf( " sdf, offset: %p, %d\n\t",
525 p->sdf, p->soffset);
526 #ifdef GCC_COMPAT
527 dump_attr(p->sap);
528 #endif
529 }
530 #endif
531 }
532
533 /*
534 * end of function
535 */
536 void
537 ftnend(void)
538 {
539 #ifdef GCC_COMPAT
540 struct attr *gc, *gd;
541 #endif
542 extern int *mkclabs(void);
543 extern NODE *cftnod;
544 extern struct savbc *savbc;
545 extern struct swdef *swpole;
546 extern int tvaloff;
547 int stack_usage;
548 char *c;
549
550 if (retlab != NOLAB && nerrors == 0) { /* inside a real function */
551 plabel(retlab);
552 if (cftnod)
553 ecomp(buildtree(FORCE, p1tcopy(cftnod), NIL));
554 efcode(); /* struct return handled here */
555 c = getexname(cftnsp);
556 #ifndef STACK_TYPE
557 #define STACK_TYPE CHAR
558 #endif
559 SETOFF(maxautooff, talign(STACK_TYPE, NULL));
560 stack_usage = maxautooff / (int)tsize(STACK_TYPE, NULL, NULL);
561 send_passt(IP_EPILOG, stack_usage, c,
562 cftnsp->stype, cftnsp->sclass == EXTDEF,
563 retlab, tvaloff, mkclabs());
564 }
565
566 cftnod = NIL;
567 tcheck();
568 brklab = contlab = retlab = NOLAB;
569 flostat &= FP_CONTR_CBR;
570 if (nerrors == 0) {
571 if (savbc != NULL)
572 cerror("bcsave error");
573 if (swpole != NULL)
574 cerror("switch error");
575 }
576 #ifdef GCC_COMPAT
577 if (cftnsp) {
578 gc = attr_find(cftnsp->sap, GCC_ATYP_CONSTRUCTOR);
579 gd = attr_find(cftnsp->sap, GCC_ATYP_DESTRUCTOR);
580 if (gc || gd) {
581 struct symtab sts = *cftnsp;
582 NODE *p;
583 sts.stype = INCREF(sts.stype);
584 p = nametree(&sts);
585 p->n_op = ICON;
586 if (gc) {
587 locctr(CTORS, NULL);
588 inval(0, SZPOINT(0), p);
589 }
590 if (gd) {
591 locctr(DTORS, NULL);
592 inval(0, SZPOINT(0), p);
593 }
594 tfree(p);
595 }
596 }
597 #endif
598 savbc = NULL;
599 cftnsp = NULL;
600 maxautooff = autooff = AUTOINIT;
601 reached = 1;
602
603 if (isinlining)
604 inline_end();
605 inline_prtout();
606
607 tmpfree(); /* Release memory resources */
608 }
609
610 static struct symtab nulsym = {
611 NULL, 0, 0, 0, 0, "null", INT, 0, NULL, NULL
612 };
613
614 void
615 dclargs(void)
616 {
617 union dimfun *df;
618 union arglist *al;
619 struct symtab *p; /* XXX gcc */
620 int i;
621
622 /*
623 * Deal with fun(void) properly.
624 */
625 if (nparams == 1 && argptr[0]->stype == VOID)
626 goto done;
627
628 /*
629 * Generate a list for bfcode().
630 * Parameters were pushed in reverse order.
631 */
632
633 for (i = 0; i < nparams; i++) {
634 p = argptr[i];
635 if (p == NULL) {
636 uerror("parameter %d name missing", i);
637 p = &nulsym; /* empty symtab */
638 }
639 if (p->stype == FARG)
640 p->stype = INT;
641 if (ISARY(p->stype)) {
642 p->stype += (PTR-ARY);
643 p->sdf++;
644 } else if (p->stype == FLOAT && oldstyle) {
645 p->stype = DOUBLE;
646 } else if (ISFTN(p->stype)) {
647 werror("function declared as argument");
648 p->stype = INCREF(p->stype);
649 }
650 #ifdef STABS
651 if (gflag)
652 stabs_newsym(p);
653 #endif
654 }
655 if (oldstyle && (df = cftnsp->sdf) && (al = df->dfun)) {
656 /*
657 * Check against prototype of oldstyle function.
658 */
659 union arglist *al2, *alb;
660
661 alb = al2 = FUNALLO(sizeof(union arglist) * nparams * 3 + 1);
662 for (i = 0; i < nparams; i++) {
663 TWORD type = argptr[i]->stype;
664 (al2++)->type = type;
665 if (ISSOU(BTYPE(type)))
666 (al2++)->sap = argptr[i]->sap;
667 while (!ISFTN(type) && !ISARY(type) && type > BTMASK)
668 type = DECREF(type);
669 if (type > BTMASK)
670 (al2++)->df = argptr[i]->sdf;
671 }
672 al2->type = TNULL;
673 intcompare = 1;
674 if (chkftn(al, alb))
675 uerror("function doesn't match prototype");
676 FUNFREE(alb);
677 intcompare = 0;
678
679 }
680
681 if (oldstyle && nparams) {
682 /* Must recalculate offset for oldstyle args here */
683 argoff = ARGINIT;
684 for (i = 0; i < nparams; i++) {
685 argptr[i]->soffset = NOOFFSET;
686 oalloc(argptr[i], &argoff);
687 }
688 }
689
690 done: autooff = AUTOINIT;
691
692 plabel(prolab); /* after prolog, used in optimization */
693 retlab = getlab();
694 bfcode(argptr, nparams);
695 if (fun_inline && (xinline
696 #ifdef GCC_COMPAT
697 || attr_find(cftnsp->sap, GCC_ATYP_ALW_INL)
698 #endif
699 ))
700 inline_args(argptr, nparams);
701 plabel(getlab()); /* used when spilling */
702 if (parlink)
703 ecomp(parlink);
704 parlink = NIL;
705 nparams = 0;
706 symclear(1); /* In case of function pointer args */
707 }
708
709 /*
710 * basic attributes for structs and enums
711 */
712 static struct attr *
713 seattr(void)
714 {
715 return attr_add(attr_new(ATTR_ALIGNED, 4), attr_new(ATTR_STRUCT, 2));
716 }
717
718 /*
719 * Struct/union/enum symtab construction.
720 */
721 static void
722 defstr(struct symtab *sp, int class)
723 {
724 sp->sclass = (char)class;
725 if (class == STNAME)
726 sp->stype = STRTY;
727 else if (class == UNAME)
728 sp->stype = UNIONTY;
729 else if (class == ENAME)
730 sp->stype = ENUMTY;
731 }
732
733 /*
734 * Declare a struct/union/enum tag.
735 * If not found, create a new tag with UNDEF type.
736 */
737 static struct symtab *
738 deftag(char *name, int class)
739 {
740 struct symtab *sp;
741
742 if ((sp = lookup(name, STAGNAME))->sap == NULL) {
743 /* New tag */
744 defstr(sp, class);
745 } else if (sp->sclass != class)
746 uerror("tag %s redeclared", name);
747 return sp;
748 }
749
750 /*
751 * reference to a structure or union, with no definition
752 */
753 NODE *
754 rstruct(char *tag, int soru)
755 {
756 struct symtab *sp;
757
758 sp = deftag(tag, soru);
759 if (sp->sap == NULL)
760 sp->sap = seattr();
761 return mkty(sp->stype, 0, sp->sap);
762 }
763
764 static int enumlow, enumhigh;
765 int enummer;
766
767 /*
768 * Declare a member of enum.
769 */
770 void
771 moedef(char *name)
772 {
773 struct symtab *sp;
774
775 sp = lookup(name, SNORMAL);
776 if (sp->stype == UNDEF || (sp->slevel < blevel)) {
777 if (sp->stype != UNDEF)
778 sp = hide(sp);
779 sp->stype = INT; /* always */
780 sp->sclass = MOE;
781 sp->soffset = enummer;
782 } else
783 uerror("%s redeclared", name);
784 if (enummer < enumlow)
785 enumlow = enummer;
786 if (enummer > enumhigh)
787 enumhigh = enummer;
788 enummer++;
789 }
790
791 /*
792 * Declare an enum tag. Complain if already defined.
793 */
794 struct symtab *
795 enumhd(char *name)
796 {
797 struct attr *ap;
798 struct symtab *sp;
799
800 enummer = enumlow = enumhigh = 0;
801 if (name == NULL)
802 return NULL;
803
804 sp = deftag(name, ENAME);
805 if (sp->stype != ENUMTY) {
806 if (sp->slevel == blevel)
807 uerror("%s redeclared", name);
808 sp = hide(sp);
809 defstr(sp, ENAME);
810 }
811 if (sp->sap == NULL)
812 ap = sp->sap = attr_new(ATTR_STRUCT, 4);
813 else
814 ap = attr_find(sp->sap, ATTR_STRUCT);
815 ap->amlist = sp;
816 return sp;
817 }
818
819 /*
820 * finish declaration of an enum
821 */
822 NODE *
823 enumdcl(struct symtab *sp)
824 {
825 NODE *p;
826 TWORD t;
827
828 #ifdef ENUMSIZE
829 t = ENUMSIZE(enumhigh, enumlow);
830 #else
831 t = ctype(enumlow < 0 ? INT : UNSIGNED);
832 #ifdef notdef
833 if (enumhigh <= MAX_CHAR && enumlow >= MIN_CHAR)
834 t = ctype(CHAR);
835 else if (enumhigh <= MAX_SHORT && enumlow >= MIN_SHORT)
836 t = ctype(SHORT);
837 else
838 t = ctype(INT);
839 #endif
840 #endif
841
842 if (sp)
843 sp->stype = t;
844 p = mkty(t, 0, 0);
845 p->n_sp = sp;
846 return p;
847 }
848
849 /*
850 * Handle reference to an enum
851 */
852 NODE *
853 enumref(char *name)
854 {
855 struct symtab *sp;
856 NODE *p;
857
858 sp = lookup(name, STAGNAME);
859
860 #ifdef notdef
861 /*
862 * 6.7.2.3 Clause 2:
863 * "A type specifier of the form 'enum identifier' without an
864 * enumerator list shall only appear after the type it specifies
865 * is complete."
866 */
867 if (sp->sclass != ENAME)
868 uerror("enum %s undeclared", name);
869 #endif
870 if (sp->sclass == SNULL) {
871 /* declare existence of enum */
872 sp = enumhd(name);
873 sp->stype = ENUMTY;
874 }
875
876 p = mkty(sp->stype, 0, sp->sap);
877 p->n_sp = sp;
878 return p;
879 }
880
881 /*
882 * begining of structure or union declaration
883 * It's an error if this routine is called twice with the same struct.
884 */
885 struct rstack *
886 bstruct(char *name, int soru, NODE *gp)
887 {
888 struct rstack *r;
889 struct symtab *sp;
890 struct attr *ap, *gap;
891
892 #ifdef GCC_COMPAT
893 gap = gp ? gcc_attr_parse(gp) : NULL;
894 #else
895 gap = NULL;
896 #endif
897
898 if (name != NULL) {
899 sp = deftag(name, soru);
900 if (sp->sap == NULL)
901 sp->sap = seattr();
902 ap = attr_find(sp->sap, ATTR_ALIGNED);
903 if (ap->iarg(0) != 0) {
904 if (sp->slevel < blevel) {
905 sp = hide(sp);
906 defstr(sp, soru);
907 sp->sap = seattr();
908 } else
909 uerror("%s redeclared", name);
910 }
911 gap = sp->sap = attr_add(sp->sap, gap);
912 } else {
913 gap = attr_add(seattr(), gap);
914 sp = NULL;
915 }
916
917 r = tmpcalloc(sizeof(struct rstack));
918 r->rsou = soru;
919 r->rsym = sp;
920 r->rb = NULL;
921 r->ap = gap;
922 r->rnext = rpole;
923 rpole = r;
924
925 return r;
926 }
927
928 /*
929 * Called after a struct is declared to restore the environment.
930 * - If ALSTRUCT is defined, this will be the struct alignment and the
931 * struct size will be a multiple of ALSTRUCT, otherwise it will use
932 * the alignment of the largest struct member.
933 */
934 NODE *
935 dclstruct(struct rstack *r)
936 {
937 NODE *n;
938 struct attr *aps, *apb;
939 struct symtab *sp;
940 int al, sa, sz;
941
942 apb = attr_find(r->ap, ATTR_ALIGNED);
943 aps = attr_find(r->ap, ATTR_STRUCT);
944 aps->amlist = r->rb;
945
946 #ifdef ALSTRUCT
947 al = ALSTRUCT;
948 #else
949 al = ALCHAR;
950 #endif
951
952 /*
953 * extract size and alignment, calculate offsets
954 */
955 for (sp = r->rb; sp; sp = sp->snext) {
956 sa = talign(sp->stype, sp->sap);
957 if (sp->sclass & FIELD)
958 sz = sp->sclass&FLDSIZ;
959 else
960 sz = (int)tsize(sp->stype, sp->sdf, sp->sap);
961 if (sz > rpole->rstr)
962 rpole->rstr = sz; /* for use with unions */
963 /*
964 * set al, the alignment, to the lcm of the alignments
965 * of the members.
966 */
967 SETOFF(al, sa);
968 }
969
970 SETOFF(rpole->rstr, al);
971
972 aps->amsize = rpole->rstr;
973 apb->iarg(0) = al;
974
975 #ifdef PCC_DEBUG
976 if (ddebug) {
977 printf("dclstruct(%s): size=%d, align=%d\n",
978 r->rsym ? r->rsym->sname : "??",
979 aps->amsize, apb->iarg(0));
980 }
981 if (ddebug>1) {
982 printf("\tsize %d align %d link %p\n",
983 aps->amsize, apb->iarg(0), aps->amlist);
984 for (sp = aps->amlist; sp != NULL; sp = sp->snext) {
985 printf("\tmember %s(%p)\n", sp->sname, sp);
986 }
987 }
988 #endif
989
990 #ifdef STABS
991 if (gflag)
992 stabs_struct(r->rsym, r->ap);
993 #endif
994
995 rpole = r->rnext;
996 n = mkty(r->rsou == STNAME ? STRTY : UNIONTY, 0, r->ap);
997 n->n_sp = r->rsym;
998
999 n->n_qual |= 1; /* definition place XXX used by attributes */
1000 return n;
1001 }
1002
1003 /*
1004 * Add a new member to the current struct or union being declared.
1005 */
1006 void
1007 soumemb(NODE *n, char *name, int class)
1008 {
1009 struct symtab *sp, *lsp;
1010 int incomp, tsz, al;
1011 TWORD t;
1012
1013 if (rpole == NULL)
1014 cerror("soumemb");
1015
1016 /* check if tag name exists */
1017 lsp = NULL;
1018 for (sp = rpole->rb; sp != NULL; lsp = sp, sp = sp->snext)
1019 if (*name != '*' && sp->sname == name)
1020 uerror("redeclaration of %s", name);
1021
1022 sp = getsymtab(name, SMOSNAME);
1023 if (rpole->rb == NULL)
1024 rpole->rb = sp;
1025 else
1026 lsp->snext = sp;
1027
1028 n->n_sp = sp;
1029 sp->stype = n->n_type;
1030 sp->squal = n->n_qual;
1031 sp->slevel = blevel;
1032 sp->sap = n->n_ap;
1033 sp->sdf = n->n_df;
1034
1035 if (class & FIELD) {
1036 sp->sclass = (char)class;
1037 if (rpole->rsou == UNAME)
1038 rpole->rstr = 0;
1039 falloc(sp, class&FLDSIZ, NIL);
1040 } else if (rpole->rsou == STNAME || rpole->rsou == UNAME) {
1041 sp->sclass = rpole->rsou == STNAME ? MOS : MOU;
1042 if (sp->sclass == MOU)
1043 rpole->rstr = 0;
1044 al = talign(sp->stype, sp->sap);
1045 tsz = (int)tsize(sp->stype, sp->sdf, sp->sap);
1046 sp->soffset = upoff(tsz, al, &rpole->rstr);
1047 }
1048
1049 /*
1050 * 6.7.2.1 clause 16:
1051 * "...the last member of a structure with more than one
1052 * named member may have incomplete array type;"
1053 */
1054 if (ISARY(sp->stype) && sp->sdf->ddim == NOOFFSET)
1055 incomp = 1;
1056 else
1057 incomp = 0;
1058 if ((rpole->flags & LASTELM) || (rpole->rb == sp && incomp == 1))
1059 uerror("incomplete array in struct");
1060 if (incomp == 1)
1061 rpole->flags |= LASTELM;
1062
1063 /*
1064 * 6.7.2.1 clause 2:
1065 * "...such a structure shall not be a member of a structure
1066 * or an element of an array."
1067 */
1068 t = sp->stype;
1069 if (rpole->rsou != STNAME || BTYPE(t) != STRTY)
1070 return; /* not for unions */
1071 while (ISARY(t))
1072 t = DECREF(t);
1073 if (ISPTR(t))
1074 return;
1075
1076 if ((lsp = strmemb(sp->sap)) != NULL) {
1077 for (; lsp->snext; lsp = lsp->snext)
1078 ;
1079 if (ISARY(lsp->stype) && lsp->snext &&
1080 lsp->sdf->ddim == NOOFFSET)
1081 uerror("incomplete struct in struct");
1082 }
1083 }
1084
1085 /*
1086 * error printing routine in parser
1087 */
1088 void
1089 yyerror(char *s)
1090 {
1091 uerror(s);
1092 }
1093
1094 void yyaccpt(void);
1095 void
1096 yyaccpt(void)
1097 {
1098 ftnend();
1099 }
1100
1101
1102 /*
1103 * Save argument symtab entry pointers in an array for later use in dclargs.
1104 */
1105 void
1106 argsave(P1ND *p)
1107 {
1108 if (p->n_op == ELLIPSIS || p->n_type == VOID)
1109 return;
1110 if (nparams == maxastore) {
1111 argptr = memcpy(tmpalloc(maxastore*2 * sizeof(struct symtab *)),
1112 argptr, maxastore * sizeof(struct symtab *));
1113 maxastore*=2;
1114 }
1115 argptr[nparams++] = p->n_sp;
1116 }
1117
1118 /*
1119 * compute the alignment of an object with type ty, sizeoff index s
1120 */
1121 int
1122 talign(unsigned int ty, struct attr *apl)
1123 {
1124 struct attr *al;
1125 int a;
1126
1127 for (; ty > BTMASK; ty = DECREF(ty)) {
1128 switch (ty & TMASK) {
1129 case PTR:
1130 return(ALPOINT);
1131 case ARY:
1132 continue;
1133 case FTN:
1134 cerror("compiler takes alignment of function");
1135 }
1136 }
1137
1138 /* check for alignment attribute */
1139 if ((al = attr_find(apl, ATTR_ALIGNED))) {
1140 if ((a = al->iarg(0)) == 0) {
1141 uerror("no alignment");
1142 a = ALINT;
1143 }
1144 return a;
1145 }
1146
1147 #ifndef NO_COMPLEX
1148 if (ISITY(ty))
1149 ty -= (FIMAG-FLOAT);
1150 #endif
1151 ty = BTYPE(ty);
1152 if (ty >= CHAR && ty <= ULONGLONG && ISUNSIGNED(ty))
1153 ty = DEUNSIGN(ty);
1154
1155 switch (ty) {
1156 #ifdef GCC_COMPAT
1157 case VOID: a = ALCHAR; break; /* GCC */
1158 #endif
1159 case BOOL: a = ALBOOL; break;
1160 case CHAR: a = ALCHAR; break;
1161 case SHORT: a = ALSHORT; break;
1162 case INT: a = ALINT; break;
1163 case LONG: a = ALLONG; break;
1164 case LONGLONG: a = ALLONGLONG; break;
1165 case FLOAT: a = ALFLOAT; break;
1166 case DOUBLE: a = ALDOUBLE; break;
1167 case LDOUBLE: a = ALLDOUBLE; break;
1168 default:
1169 uerror("no alignment");
1170 a = ALINT;
1171 }
1172 return a;
1173 }
1174
1175 short sztable[] = { 0, SZBOOL, SZCHAR, SZCHAR, SZSHORT, SZSHORT, SZINT, SZINT,
1176 SZLONG, SZLONG, SZLONGLONG, SZLONGLONG, SZFLOAT, SZDOUBLE, SZLDOUBLE };
1177
1178 /* compute the size associated with type ty,
1179 * dimoff d, and sizoff s */
1180 /* BETTER NOT BE CALLED WHEN t, d, and s REFER TO A BIT FIELD... */
1181 OFFSZ
1182 tsize(TWORD ty, union dimfun *d, struct attr *apl)
1183 {
1184 struct attr *ap, *ap2;
1185 OFFSZ mult, sz;
1186
1187 mult = 1;
1188
1189 for (; ty > BTMASK; ty = DECREF(ty)) {
1190 switch (ty & TMASK) {
1191
1192 case FTN:
1193 #ifdef GCC_COMPAT
1194 return SZCHAR;
1195 #else
1196 uerror( "cannot take size of function");
1197 #endif
1198 case PTR:
1199 return( SZPOINT(ty) * mult );
1200 case ARY:
1201 if (d->ddim == NOOFFSET)
1202 return 0;
1203 if (d->ddim < 0)
1204 cerror("tsize: dynarray");
1205 mult *= d->ddim;
1206 d++;
1207 }
1208 }
1209
1210 #ifndef NO_COMPLEX
1211 if (ISITY(ty))
1212 ty -= (FIMAG-FLOAT);
1213 #endif
1214
1215 if (ty == VOID)
1216 ty = CHAR;
1217 if (ty <= LDOUBLE)
1218 sz = sztable[ty];
1219 else if (ISSOU(ty)) {
1220 if ((ap = strattr(apl)) == NULL ||
1221 (ap2 = attr_find(apl, ATTR_ALIGNED)) == NULL ||
1222 (ap2->iarg(0) == 0)) {
1223 uerror("unknown structure/union/enum");
1224 sz = SZINT;
1225 } else
1226 sz = ap->amsize;
1227 } else {
1228 uerror("unknown type");
1229 sz = SZINT;
1230 }
1231
1232 return((unsigned int)sz * mult);
1233 }
1234
1235 #ifndef MYINSTRING
1236 /*
1237 * Print out a string of characters.
1238 * Assume that the assembler understands C-style escape
1239 * sequences.
1240 */
1241 void
1242 instring(struct symtab *sp)
1243 {
1244 unsigned short sh[2];
1245 char *s, *str;
1246 TWORD t;
1247 NODE *p;
1248
1249 locctr(STRNG, sp);
1250 defloc(sp);
1251
1252 t = BTYPE(sp->stype);
1253 str = s = sp->sname;
1254 if (t == ctype(USHORT)) {
1255 /* convert to UTF-16 */
1256 p = xbcon(0, NULL, t);
1257 while (*s) {
1258 cp2u16(u82cp(&s), sh);
1259 if ((glval(p) = sh[0]))
1260 inval(0, SZSHORT, p);
1261 if ((glval(p) = sh[1]))
1262 inval(0, SZSHORT, p);
1263 }
1264 slval(p, 0);
1265 inval(0, SZSHORT, p);
1266 nfree(p);
1267 } else if (t == ctype(SZINT < 32 ? ULONG : UNSIGNED) ||
1268 t == ctype(SZINT < 32 ? LONG : INT)) {
1269 /* convert to UTF-32 */
1270 p = xbcon(0, NULL, t);
1271 while (*s) {
1272 slval(p, u82cp(&s));
1273 inval(0, SZINT < 32 ? SZLONG : SZINT, p);
1274 }
1275 slval(p, 0);
1276 inval(0, SZINT < 32 ? SZLONG : SZINT, p);
1277 nfree(p);
1278 } else if (t == CHAR || t == UCHAR) {
1279 printf(PRTPREF "\t.ascii \"");
1280 while (*s) {
1281 if (*s == '\\')
1282 (void)esccon(&s);
1283 else
1284 s++;
1285
1286 if (s - str > 60) {
1287 fwrite(str, 1, s - str, stdout);
1288 printf("\"\n" PRTPREF "\t.ascii \"");
1289 str = s;
1290 }
1291 }
1292
1293 fwrite(str, 1, s - str, stdout);
1294 printf("\\0\"\n");
1295 } else
1296 cerror("instring %ld", t);
1297 }
1298 #endif
1299
1300 /*
1301 * update the offset pointed to by poff; return the
1302 * offset of a value of size `size', alignment `alignment',
1303 * given that off is increasing
1304 */
1305 int
1306 upoff(int size, int alignment, int *poff)
1307 {
1308 int off;
1309
1310 off = *poff;
1311 SETOFF(off, alignment);
1312 if (off < 0)
1313 cerror("structure or stack overgrown"); /* wrapped */
1314 *poff = off+size;
1315 return (off);
1316 }
1317
1318 /*
1319 * allocate p with offset *poff, and update *poff.
1320 * This routine is used to create stack reference for arguments and automatics.
1321 * poff is always increasing even if the stack goes downwards, hence
1322 * the off must must be calculated before/after the increase/decrease
1323 * of the stack pointer.
1324 */
1325 int
1326 oalloc(struct symtab *p, int *poff )
1327 {
1328 int al, off, tsz;
1329 int noff;
1330
1331 al = talign(p->stype, p->sap);
1332 noff = *poff;
1333 off = 0;
1334 tsz = (int)tsize(p->stype, p->sdf, p->sap);
1335
1336 #ifdef STACK_DOWN
1337 if (p->sclass == AUTO) {
1338 /* negative part of stack */
1339 noff += tsz;
1340 SETOFF(noff, al);
1341 off = -noff;
1342 } else if (p->sclass == PARAM) {
1343 /* positive part of stack */
1344 if (p->stype < INT || p->stype == BOOL)
1345 tsz = SZINT, al = ALINT;
1346 off = upoff(tsz, al, &noff);
1347 } else
1348 cerror("bad oalloc class %d", p->sclass);
1349 #else
1350 if (p->sclass == AUTO) {
1351 /* positive part of stack */
1352 off = upoff(tsz, al, &noff);
1353 } else if (p->sclass == PARAM) {
1354 /* negative part of stack */
1355 noff += tsz;
1356 SETOFF(noff, al);
1357 off = -noff;
1358 } else
1359 cerror("bad oalloc class %d", p->sclass);
1360 #endif
1361
1362 if (p->soffset == NOOFFSET)
1363 p->soffset = off;
1364 else if(off != p->soffset)
1365 cerror("oalloc: reallocated");
1366
1367 *poff = noff;
1368 return(0);
1369 }
1370
1371 /*
1372 * Delay emission of code generated in argument headers.
1373 */
1374 static void
1375 edelay(NODE *p)
1376 {
1377 if (blevel == 1) {
1378 /* Delay until after declarations */
1379 if (parlink == NULL)
1380 parlink = p;
1381 else
1382 parlink = block(COMOP, parlink, p, 0, 0, 0);
1383 } else
1384 ecomp(p);
1385 }
1386
1387 /*
1388 * Return 1 if dynamic array, 0 otherwise.
1389 */
1390 int
1391 isdyn(struct symtab *sp)
1392 {
1393 union dimfun *df = sp->sdf;
1394 TWORD t;
1395
1396 for (t = sp->stype; t > BTMASK; t = DECREF(t)) {
1397 if (!ISARY(t))
1398 return 0;
1399 if (df->ddim < 0 && df->ddim != NOOFFSET)
1400 return 1;
1401 df++;
1402 }
1403 return 0;
1404 }
1405
1406 /*
1407 * Allocate space on the stack for dynamic arrays (or at least keep track
1408 * of the index).
1409 * Strategy is as follows:
1410 * - first entry is a pointer to the dynamic datatype.
1411 * - if it's a one-dimensional array this will be the only entry used.
1412 * - if it's a multi-dimensional array the following (numdim-1) integers
1413 * will contain the sizes to multiply the indexes with.
1414 * - code to write the dimension sizes this will be generated here.
1415 * - code to allocate space on the stack will be generated here.
1416 */
1417 static void
1418 dynalloc(struct symtab *p, int *poff)
1419 {
1420 union dimfun *df;
1421 NODE *n, *tn, *pol;
1422 TWORD t;
1423
1424 /*
1425 * The pointer to the array is not necessarily stored in a
1426 * TEMP node, but if it is, its number is in the soffset field;
1427 */
1428 t = p->stype;
1429 p->sflags |= STNODE;
1430 p->stype = INCREF(p->stype); /* Make this an indirect pointer */
1431 tn = tempnode(0, p->stype, p->sdf, p->sap);
1432 p->soffset = regno(tn);
1433
1434 df = p->sdf;
1435
1436 pol = bcon(1);
1437 for (; t > BTMASK; t = DECREF(t)) {
1438 if (!ISARY(t))
1439 break;
1440 if (df->ddim < 0)
1441 n = tempnode(-df->ddim, INT, 0, 0);
1442 else
1443 n = bcon(df->ddim);
1444
1445 pol = buildtree(MUL, pol, n);
1446 df++;
1447 }
1448 /* Create stack gap */
1449 spalloc(tn, pol, tsize(t, 0, p->sap));
1450 }
1451
1452 /*
1453 * allocate a field of width w
1454 * new is 0 if new entry, 1 if redefinition, -1 if alignment
1455 */
1456 int
1457 falloc(struct symtab *p, int w, NODE *pty)
1458 {
1459 TWORD otype, type;
1460 int al,sz;
1461
1462 otype = type = p ? p->stype : pty->n_type;
1463
1464 if (type == BOOL)
1465 type = BOOL_TYPE;
1466 if (!ISINTEGER(type)) {
1467 uerror("illegal field type");
1468 type = INT;
1469 }
1470
1471 al = talign(type, NULL);
1472 sz = (int)tsize(type, NULL, NULL);
1473
1474 if (w > sz) {
1475 uerror("field too big");
1476 w = sz;
1477 }
1478
1479 if (w == 0) { /* align only */
1480 SETOFF(rpole->rstr, al);
1481 if (p != NULL)
1482 uerror("zero size field");
1483 return(0);
1484 }
1485
1486 if (rpole->rstr%al + w > sz)
1487 SETOFF(rpole->rstr, al);
1488 if (p == NULL) {
1489 rpole->rstr += w; /* we know it will fit */
1490 return(0);
1491 }
1492
1493 /* establish the field */
1494
1495 p->soffset = rpole->rstr;
1496 rpole->rstr += w;
1497 p->stype = otype;
1498 fldty(p);
1499 return(0);
1500 }
1501
1502 /*
1503 * Check if this symbol should be a common or must be handled in data seg.
1504 */
1505 static void
1506 commchk(struct symtab *sp)
1507 {
1508 if ((sp->sflags & STLS)
1509 #ifdef GCC_COMPAT
1510 || attr_find(sp->sap, GCC_ATYP_SECTION)
1511 #endif
1512 ) {
1513 /* TLS handled in data segment */
1514 if (sp->sclass == EXTERN)
1515 sp->sclass = EXTDEF;
1516 beginit(sp);
1517 endinit(1);
1518 } else {
1519 symdirec(sp);
1520 defzero(sp);
1521 }
1522 }
1523
1524 void
1525 nidcl(NODE *p, int class)
1526 {
1527 nidcl2(p, class, 0);
1528 }
1529
1530 /*
1531 * handle unitialized declarations assumed to be not functions:
1532 * int a;
1533 * extern int a;
1534 * static int a;
1535 */
1536 void
1537 nidcl2(NODE *p, int class, char *astr)
1538 {
1539 struct symtab *sp;
1540 int commflag = 0;
1541
1542 /* compute class */
1543 if (class == SNULL) {
1544 if (blevel > 1)
1545 class = AUTO;
1546 else if (blevel != 0 || rpole)
1547 cerror( "nidcl error" );
1548 else /* blevel = 0 */
1549 commflag = 1, class = EXTERN;
1550 }
1551
1552 defid2(p, class, astr);
1553
1554 sp = p->n_sp;
1555 /* check if forward decl */
1556 if (ISARY(sp->stype) && sp->sdf->ddim == NOOFFSET)
1557 return;
1558
1559 if (sp->sflags & SASG)
1560 return; /* already initialized */
1561
1562 switch (class) {
1563 case EXTDEF:
1564 /* simulate initialization by 0 */
1565 simpleinit(p->n_sp, bcon(0));
1566 break;
1567 case EXTERN:
1568 if (commflag)
1569 lcommadd(p->n_sp);
1570 else
1571 extdec(p->n_sp);
1572 break;
1573 case STATIC:
1574 if (blevel == 0)
1575 lcommadd(p->n_sp);
1576 else
1577 commchk(p->n_sp);
1578 break;
1579 }
1580 }
1581
1582 struct lcd {
1583 SLIST_ENTRY(lcd) next;
1584 struct symtab *sp;
1585 };
1586
1587 static SLIST_HEAD(, lcd) lhead = { NULL, &lhead.q_forw};
1588
1589 /*
1590 * Add a local common statement to the printout list.
1591 */
1592 void
1593 lcommadd(struct symtab *sp)
1594 {
1595 struct lcd *lc, *lcp;
1596
1597 lcp = NULL;
1598 SLIST_FOREACH(lc, &lhead, next) {
1599 if (lc->sp == sp)
1600 return; /* already exists */
1601 if (lc->sp == NULL && lcp == NULL)
1602 lcp = lc;
1603 }
1604 if (lcp == NULL) {
1605 lc = permalloc(sizeof(struct lcd));
1606 lcommsz += sizeof(struct lcd);
1607 lc->sp = sp;
1608 SLIST_INSERT_LAST(&lhead, lc, next);
1609 } else
1610 lcp->sp = sp;
1611 }
1612
1613 /*
1614 * Delete a local common statement.
1615 */
1616 void
1617 lcommdel(struct symtab *sp)
1618 {
1619 struct lcd *lc;
1620
1621 SLIST_FOREACH(lc, &lhead, next) {
1622 if (lc->sp == sp) {
1623 lc->sp = NULL;
1624 return;
1625 }
1626 }
1627 }
1628
1629 /*
1630 * Print out the remaining common statements.
1631 */
1632 void
1633 lcommprint(void)
1634 {
1635 struct lcd *lc;
1636
1637 SLIST_FOREACH(lc, &lhead, next) {
1638 if (lc->sp != NULL)
1639 commchk(lc->sp);
1640 }
1641 }
1642
1643 /*
1644 * Merge given types to a single node.
1645 * Any type can end up here.
1646 * p is the old node, q is the old (if any).
1647 * CLASS is AUTO, EXTERN, REGISTER, STATIC or TYPEDEF.
1648 * QUALIFIER is VOL or CON
1649 * TYPE is CHAR, SHORT, INT, LONG, SIGNED, UNSIGNED, VOID, BOOL, FLOAT,
1650 * DOUBLE, STRTY, UNIONTY.
1651 */
1652 struct typctx {
1653 int class, qual, sig, uns, cmplx, imag, err, align;
1654 TWORD type;
1655 NODE *saved;
1656 struct attr *pre, *post;
1657 };
1658
1659 static void
1660 typwalk(NODE *p, void *arg)
1661 {
1662 struct typctx *tc = arg;
1663
1664 #define cmop(x,y) block(CM, x, y, INT, 0, 0)
1665 switch (p->n_op) {
1666 case ALIGN:
1667 if (tc->align < glval(p))
1668 tc->align = (int)glval(p);
1669 break;
1670 case ATTRIB:
1671 #ifdef GCC_COMPAT
1672 if (tc->saved && (tc->saved->n_qual & 1)) {
1673 tc->post = attr_add(tc->post,gcc_attr_parse(p->n_left));
1674 } else {
1675 tc->pre = attr_add(tc->pre, gcc_attr_parse(p->n_left));
1676 }
1677 p->n_left = bcon(0); /* For tfree() */
1678 #else
1679 uerror("gcc type attribute used");
1680 #endif
1681 break;
1682 case CLASS:
1683 if (p->n_type == 0)
1684 break; /* inline hack */
1685 if (tc->class)
1686 tc->err = 1; /* max 1 class */
1687 tc->class = p->n_type;
1688 break;
1689
1690 case FUNSPEC:
1691 if (p->n_type == INLINE) {
1692 fun_inline = 1;
1693 } else if (p->n_type == NORETURN) {
1694 tc->pre = attr_add(tc->pre, attr_new(ATTR_NORETURN, 3));
1695 } else
1696 tc->err = 1;
1697 break;
1698
1699 case QUALIFIER:
1700 if (p->n_qual == 0 &&
1701 ((tc->saved && !ISPTR(tc->saved->n_type)) ||
1702 (tc->saved == 0)))
1703 uerror("invalid use of 'restrict'");
1704 tc->qual |= p->n_qual >> TSHIFT;
1705 break;
1706
1707 case TYPE:
1708 if (p->n_sp != NULL || ISSOU(p->n_type)) {
1709 /* typedef, enum or struct/union */
1710 if (tc->saved || tc->type)
1711 tc->err = 1;
1712 #ifdef GCC_COMPAT
1713 if (ISSOU(p->n_type) && p->n_left) {
1714 if (tc->post)
1715 cerror("typwalk");
1716 tc->post = gcc_attr_parse(p->n_left);
1717 }
1718 #endif
1719 tc->saved = ccopy(p);
1720 break;
1721 }
1722
1723 switch (p->n_type) {
1724 case BOOL:
1725 case CHAR:
1726 case FLOAT:
1727 case VOID:
1728 if (tc->type)
1729 tc->err = 1;
1730 tc->type = p->n_type;
1731 break;
1732 case DOUBLE:
1733 if (tc->type == 0)
1734 tc->type = DOUBLE;
1735 else if (tc->type == LONG)
1736 tc->type = LDOUBLE;
1737 else
1738 tc->err = 1;
1739 break;
1740 case SHORT:
1741 if (tc->type == 0 || tc->type == INT)
1742 tc->type = SHORT;
1743 else
1744 tc->err = 1;
1745 break;
1746 case INT:
1747 if (tc->type == SHORT || tc->type == LONG ||
1748 tc->type == LONGLONG)
1749 break;
1750 else if (tc->type == 0)
1751 tc->type = INT;
1752 else
1753 tc->err = 1;
1754 break;
1755 case LONG:
1756 if (tc->type == 0)
1757 tc->type = LONG;
1758 else if (tc->type == INT)
1759 break;
1760 else if (tc->type == LONG)
1761 tc->type = LONGLONG;
1762 else if (tc->type == DOUBLE)
1763 tc->type = LDOUBLE;
1764 else
1765 tc->err = 1;
1766 break;
1767 case SIGNED:
1768 if (tc->sig || tc->uns)
1769 tc->err = 1;
1770 tc->sig = 1;
1771 break;
1772 case UNSIGNED:
1773 if (tc->sig || tc->uns)
1774 tc->err = 1;
1775 tc->uns = 1;
1776 break;
1777 case COMPLEX:
1778 tc->cmplx = 1;
1779 break;
1780 case IMAG:
1781 tc->imag = 1;
1782 break;
1783 default:
1784 cerror("typwalk");
1785 }
1786 }
1787
1788 }
1789
1790 NODE *
1791 typenode(NODE *p)
1792 {
1793 struct attr *ap;
1794 struct symtab *sp;
1795 struct typctx tc;
1796 NODE *q;
1797 char *c;
1798
1799 memset(&tc, 0, sizeof(struct typctx));
1800
1801 flist(p, typwalk, &tc);
1802 tfree(p);
1803
1804 if (tc.err)
1805 goto bad;
1806
1807 if (tc.cmplx || tc.imag) {
1808 if (tc.type == 0)
1809 tc.type = DOUBLE;
1810 if ((tc.cmplx && tc.imag) || tc.sig || tc.uns ||
1811 !ISFTY(tc.type))
1812 goto bad;
1813 if (tc.cmplx) {
1814 c = tc.type == DOUBLE ? "0d" :
1815 tc.type == FLOAT ? "0f" : "0l";
1816 sp = lookup(addname(c), 0);
1817 tc.type = STRTY;
1818 tc.saved = mkty(tc.type, sp->sdf, sp->sap);
1819 tc.saved->n_sp = sp;
1820 tc.type = 0;
1821 } else
1822 tc.type += (FIMAG-FLOAT);
1823 }
1824
1825 if (tc.saved && tc.type)
1826 goto bad;
1827 if (tc.sig || tc.uns) {
1828 if (tc.type == 0)
1829 tc.type = tc.sig ? INT : UNSIGNED;
1830 if (tc.type > ULONGLONG)
1831 goto bad;
1832 if (tc.uns)
1833 tc.type = ENUNSIGN(tc.type);
1834 }
1835
1836 if (xuchar && tc.type == CHAR && tc.sig == 0)
1837 tc.type = UCHAR;
1838
1839 #ifdef GCC_COMPAT
1840 if (pragma_packed) {
1841 q = bdty(CALL, bdty(NAME, "packed"), bcon(pragma_packed));
1842 tc.post = attr_add(tc.post, gcc_attr_parse(q));
1843 }
1844 if (pragma_aligned) {
1845 /* Deal with relevant pragmas */
1846 if (tc.align < pragma_aligned)
1847 tc.align = pragma_aligned;
1848 }
1849 pragma_aligned = pragma_packed = 0;
1850 #endif
1851 if ((q = tc.saved) == NULL) {
1852 TWORD t;
1853 if ((t = BTYPE(tc.type)) > LDOUBLE && t != VOID &&
1854 t != BOOL && !(t >= FIMAG && t <= LIMAG))
1855 cerror("typenode2 t %x", tc.type);
1856 if (t == UNDEF) {
1857 t = INT;
1858 MODTYPE(tc.type, INT);
1859 }
1860 q = mkty(tc.type, 0, 0);
1861 }
1862 q->n_ap = attr_add(q->n_ap, tc.post);
1863 q->n_qual = tc.qual;
1864 slval(q, tc.class);
1865 #ifdef GCC_COMPAT
1866 if (tc.post) {
1867 /* Can only occur for TYPEDEF, STRUCT or UNION */
1868 if (tc.saved == NULL)
1869 cerror("typenode");
1870 if (tc.saved->n_sp) /* trailer attributes for structs */
1871 tc.saved->n_sp->sap = q->n_ap;
1872 }
1873 if (tc.pre)
1874 q->n_ap = attr_add(q->n_ap, tc.pre);
1875 gcc_tcattrfix(q);
1876 #endif
1877
1878 if (tc.align && (ap = attr_find(q->n_ap, ATTR_ALIGNED)) &&
1879 ap->iarg(0) && tc.align > talign(q->n_type, q->n_ap)/SZCHAR) {
1880 q->n_ap = attr_add(q->n_ap, attr_new(ATTR_ALIGNED, 1));
1881 q->n_ap->aa[0].iarg = SZCHAR * tc.align;
1882 }
1883
1884 return q;
1885
1886 bad: uerror("illegal type combination");
1887 return mkty(INT, 0, 0);
1888 }
1889
1890 struct tylnk {
1891 struct tylnk *next;
1892 union dimfun df;
1893 };
1894
1895 /*
1896 * Retrieve all CM-separated argument types, sizes and dimensions and
1897 * put them in an array.
1898 * XXX - can only check first type level, side effects?
1899 */
1900 static union arglist *
1901 arglist(NODE *n)
1902 {
1903 union arglist *al;
1904 NODE *w = n, **ap;
1905 int num, cnt, i, j, k;
1906 TWORD ty;
1907
1908 #ifdef PCC_DEBUG
1909 if (pdebug) {
1910 printf("arglist %p\n", n);
1911 fwalk(n, eprint, 0);
1912 }
1913 #endif
1914 /* First: how much to allocate */
1915 for (num = cnt = 0, w = n; w->n_op == CM; w = w->n_left) {
1916 cnt++; /* Number of levels */
1917 num++; /* At least one per step */
1918 if (w->n_right->n_op == ELLIPSIS)
1919 continue;
1920 ty = w->n_right->n_type;
1921 if (ty == ENUMTY) {
1922 uerror("arg %d enum undeclared", cnt);
1923 ty = w->n_right->n_type = INT;
1924 }
1925 if (BTYPE(ty) == STRTY || BTYPE(ty) == UNIONTY)
1926 num++;
1927 while (!ISFTN(ty) && !ISARY(ty) && ty > BTMASK)
1928 ty = DECREF(ty);
1929 if (ty > BTMASK)
1930 num++;
1931 }
1932 cnt++;
1933 ty = w->n_type;
1934 if (BTYPE(ty) == ENUMTY) {
1935 struct attr *app = attr_find(w->n_ap, ATTR_STRUCT);
1936 struct symtab *sp;
1937
1938 if (app == NULL)
1939 uerror("arg %d enum undeclared", cnt);
1940 sp = app->amlist;
1941 if (sp->stype != ENUMTY)
1942 MODTYPE(ty, sp->stype);
1943 w->n_type = ty;
1944 }
1945 if (ty == ENUMTY) {
1946 uerror("arg %d enum undeclared", cnt);
1947 ty = w->n_type = INT;
1948 }
1949 if (BTYPE(ty) == STRTY || BTYPE(ty) == UNIONTY)
1950 num++;
1951 while (!ISFTN(ty) && !ISARY(ty) && ty > BTMASK)
1952 ty = DECREF(ty);
1953 if (ty > BTMASK)
1954 num++;
1955 num += 2; /* TEND + last arg type */
1956
1957 /* Second: Create list to work on */
1958 ap = FUNALLO(sizeof(NODE *) * cnt);
1959 al = permalloc(sizeof(union arglist) * num);
1960 arglistcnt += num;
1961
1962 for (w = n, i = 0; w->n_op == CM; w = w->n_left)
1963 ap[i++] = w->n_right;
1964 ap[i] = w;
1965
1966 /* Third: Create actual arg list */
1967 for (k = 0, j = i; j >= 0; j--) {
1968 if (ap[j]->n_op == ELLIPSIS) {
1969 al[k++].type = TELLIPSIS;
1970 ap[j]->n_op = ICON; /* for tfree() */
1971 continue;
1972 }
1973 /* Convert arrays to pointers */
1974 if (ISARY(ap[j]->n_type)) {
1975 ap[j]->n_type += (PTR-ARY);
1976 ap[j]->n_df++;
1977 }
1978 /* Convert (silently) functions to pointers */
1979 if (ISFTN(ap[j]->n_type))
1980 ap[j]->n_type = INCREF(ap[j]->n_type);
1981 ty = ap[j]->n_type;
1982 #ifdef GCC_COMPAT
1983 if (ty == UNIONTY &&
1984 attr_find(ap[j]->n_ap, GCC_ATYP_TRANSP_UNION)){
1985 /* transparent unions must have compatible types
1986 * shortcut here: if pointers, set void *,
1987 * otherwise btype.
1988 */
1989 struct symtab *sp = strmemb(ap[j]->n_ap);
1990 ty = ISPTR(sp->stype) ? PTR|VOID : sp->stype;
1991 }
1992 #endif
1993 al[k++].type = ty;
1994 if (BTYPE(ty) == STRTY || BTYPE(ty) == UNIONTY)
1995 al[k++].sap = ap[j]->n_ap;
1996 while (!ISFTN(ty) && !ISARY(ty) && ty > BTMASK)
1997 ty = DECREF(ty);
1998 if (ty > BTMASK)
1999 al[k++].df = ap[j]->n_df;
2000 }
2001 al[k++].type = TNULL;
2002 if (k > num)
2003 cerror("arglist: k%d > num%d", k, num);
2004 tfree(n);
2005 FUNFREE(ap);
2006 #ifdef PCC_DEBUG
2007 if (pdebug)
2008 alprint(al, 0);
2009 #endif
2010 return al;
2011 }
2012
2013 static int numdfs;
2014
2015 static void
2016 tylkadd(union dimfun dim, union dimfun *df)
2017 {
2018 df[numdfs++] = dim;
2019 if (numdfs == MAXDF)
2020 uerror("too many dimensions (C11 5.2.4.1)");
2021 }
2022
2023 /*
2024 * build a type, and stash away dimensions,
2025 * from a parse tree of the declaration
2026 * the type is build top down, the dimensions bottom up
2027 */
2028 static void
2029 tyreduce(NODE *p, union dimfun *df)
2030 {
2031 union dimfun dim;
2032 NODE *r = NULL;
2033 int o;
2034 TWORD t, q;
2035
2036 o = p->n_op;
2037 if (o == NAME) {
2038 p->n_qual = DECQAL(p->n_qual);
2039 return;
2040 }
2041
2042 t = INCREF(p->n_type);
2043 q = p->n_qual;
2044 switch (o) {
2045 case CALL:
2046 t += (FTN-PTR);
2047 dim.dfun = arglist(p->n_right);
2048 break;
2049 case UCALL:
2050 t += (FTN-PTR);
2051 dim.dfun = NULL;
2052 break;
2053 case LB:
2054 t += (ARY-PTR);
2055 if (p->n_right->n_op != ICON) {
2056 r = p->n_right;
2057 o = RB;
2058 } else {
2059 dim.ddim = (int)glval(p->n_right);
2060 nfree(p->n_right);
2061 #ifdef notdef
2062 /* XXX - check dimensions at usage time */
2063 if (dim.ddim == NOOFFSET && p->n_left->n_op == LB)
2064 uerror("null dimension");
2065 #endif
2066 }
2067 break;
2068 case UMUL:
2069 break;
2070 default:
2071 cerror("bad node %d\n", o);
2072 }
2073
2074 p->n_left->n_type = t;
2075 p->n_left->n_qual = INCQAL(q) | p->n_left->n_qual;
2076 tyreduce(p->n_left, df);
2077
2078 if (o == LB || o == UCALL || o == CALL)
2079 tylkadd(dim, df);
2080 if (o == RB) {
2081 P1ND *qq = tempnode(0, INT, 0, 0);
2082 dim.ddim = -regno(qq);
2083 tylkadd(dim, df);
2084 edelay(buildtree(ASSIGN, qq, r));
2085 }
2086 if (p->n_op == LB || p->n_op == CALL)
2087 p->n_op = RB;
2088
2089 p->n_sp = p->n_left->n_sp;
2090 p->n_type = p->n_left->n_type;
2091 p->n_qual = p->n_left->n_qual;
2092 }
2093
2094 /*
2095 * merge type typ with identifier idp.
2096 * idp is returned as a NAME node with correct types,
2097 * typ is untouched since multiple declarations uses it.
2098 * typ has type attributes, idp can never carry such attributes
2099 * so on return just a pointer to the typ attributes is returned.
2100 */
2101 NODE *
2102 tymerge(NODE *typ, NODE *idp)
2103 {
2104 TWORD t;
2105 union dimfun dfs[MAXDF];
2106 union dimfun *j;
2107
2108 #ifdef PCC_DEBUG
2109 if (ddebug > 2) {
2110 printf("tymerge(%p,%p)\n", typ, idp);
2111 fwalk(typ, eprint, 0);
2112 fwalk(idp, eprint, 0);
2113 }
2114 #endif
2115
2116 if (typ->n_op != TYPE)
2117 cerror("tymerge: arg 1");
2118
2119 idp->n_type = typ->n_type;
2120 idp->n_qual |= typ->n_qual;
2121
2122 numdfs = 0;
2123
2124 tyreduce(idp, dfs);
2125
2126 for (t = typ->n_type, j = typ->n_df; t&TMASK; t = DECREF(t))
2127 if (ISARY(t) || ISFTN(t))
2128 tylkadd(*j++, dfs);
2129
2130 dimfuncnt += numdfs;
2131 if (numdfs) {
2132 union dimfun *a = permalloc(sizeof(union dimfun) * numdfs);
2133 idp->n_df = memcpy(a, dfs, sizeof(union dimfun) * numdfs);
2134 } else
2135 idp->n_df = NULL;
2136
2137 /* now idp is a single node: fix up type */
2138 idp->n_type = ctype(idp->n_type);
2139
2140 if (idp->n_op != NAME)
2141 p1tfree(idp->n_left);
2142 idp->n_op = NAME;
2143
2144 /* carefully not destroy any type attributes */
2145 idp->n_ap = attr_add(typ->n_ap, idp->n_ap);
2146
2147 return(idp);
2148 }
2149
2150 static NODE *
2151 argcast(NODE *p, TWORD t, union dimfun *d, struct attr *ap)
2152 {
2153 NODE *u, *r = p1alloc();
2154
2155 r->n_op = NAME;
2156 r->n_type = t;
2157 r->n_qual = 0; /* XXX */
2158 r->n_df = d;
2159 r->n_ap = ap;
2160
2161 u = buildtree(CAST, r, p);
2162 nfree(u->n_left);
2163 r = u->n_right;
2164 nfree(u);
2165 return r;
2166 }
2167
2168 #ifdef PCC_DEBUG
2169 /*
2170 * Print a prototype.
2171 */
2172 static void
2173 alprint(union arglist *al, int in)
2174 {
2175 TWORD t;
2176 int i = 0, j;
2177
2178 for (; al->type != TNULL; al++) {
2179 for (j = in; j > 0; j--)
2180 printf(" ");
2181 printf("arg %d: ", i++);
2182 t = al->type;
2183 tprint(t, 0);
2184 while (t > BTMASK) {
2185 if (ISARY(t)) {
2186 al++;
2187 printf(" dim %d ", al->df->ddim);
2188 } else if (ISFTN(t)) {
2189 al++;
2190 if (al->df->dfun) {
2191 printf("\n");
2192 alprint(al->df->dfun, in+1);
2193 }
2194 }
2195 t = DECREF(t);
2196 }
2197 if (ISSOU(t)) {
2198 al++;
2199 printf(" (size %d align %d)", (int)tsize(t, 0, al->sap),
2200 (int)talign(t, al->sap));
2201 }
2202 printf("\n");
2203 }
2204 if (in == 0)
2205 printf("end arglist\n");
2206 }
2207 #endif
2208
2209 int
2210 suemeq(struct attr *s1, struct attr *s2)
2211 {
2212
2213 return (strmemb(s1) == strmemb(s2));
2214 }
2215
2216 /*
2217 * Sanity-check old-style args.
2218 */
2219 static NODE *
2220 oldarg(NODE *p)
2221 {
2222 if (p->n_op == TYPE)
2223 uerror("type is not an argument");
2224 if (p->n_type == FLOAT)
2225 return cast(p, DOUBLE, p->n_qual);
2226 return p;
2227 }
2228
2229 /*
2230 * Do prototype checking and add conversions before calling a function.
2231 * Argument f is function and a is a CM-separated list of arguments.
2232 * Returns a merged node (via buildtree() of function and arguments.
2233 */
2234 NODE *
2235 doacall(struct symtab *sp, NODE *f, NODE *a)
2236 {
2237 NODE *w, *r;
2238 union arglist *al;
2239 struct ap {
2240 struct ap *next;
2241 NODE *node;
2242 } *at, *apole = NULL, *apary = NULL;
2243 int i, argidx/* , hasarray = 0*/;
2244 TWORD type, arrt;
2245
2246 #ifdef PCC_DEBUG
2247 if (ddebug) {
2248 printf("doacall.\n");
2249 fwalk(f, eprint, 0);
2250 if (a)
2251 fwalk(a, eprint, 0);
2252 }
2253 #endif
2254 if (ISARY(f->n_type))
2255 goto build; /* something bad happened */
2256
2257 /* First let MD code do something */
2258 calldec(f, a);
2259 /* XXX XXX hack */
2260 if ((f->n_op == CALL) &&
2261 f->n_left->n_op == ADDROF &&
2262 f->n_left->n_left->n_op == NAME &&
2263 (f->n_left->n_left->n_type & 0x7e0) == 0x4c0)
2264 goto build;
2265 /* XXX XXX hack */
2266
2267 /* Check for undefined or late defined enums */
2268 if (BTYPE(f->n_type) == ENUMTY) {
2269 /* not-yet check if declared enum */
2270 struct symtab *sq = strmemb(f->n_ap);
2271 if (sq->stype != ENUMTY)
2272 MODTYPE(f->n_type, sq->stype);
2273 if (BTYPE(f->n_type) == ENUMTY)
2274 uerror("enum %s not declared", sq->sname);
2275 }
2276
2277 /*
2278 * Do some basic checks.
2279 */
2280 if (f->n_df == NULL || (al = f->n_df[0].dfun) == NULL) {
2281 /*
2282 * Handle non-prototype declarations.
2283 */
2284 if (f->n_op == NAME && f->n_sp != NULL) {
2285 if (strncmp(f->n_sp->sname, "__builtin", 9) != 0 &&
2286 (f->n_sp->sflags & SINSYS) == 0)
2287 warner(Wmissing_prototypes, f->n_sp->sname);
2288 } else
2289 warner(Wmissing_prototypes, "<pointer>");
2290
2291 /* floats must be cast to double */
2292 if (a == NULL)
2293 goto build;
2294 if (a->n_op != CM) {
2295 a = oldarg(a);
2296 } else {
2297 for (w = a; w->n_left->n_op == CM; w = w->n_left)
2298 w->n_right = oldarg(w->n_right);
2299 w->n_left = oldarg(w->n_left);
2300 w->n_right = oldarg(w->n_right);
2301 }
2302 goto build;
2303 }
2304 if (al->type == VOID) {
2305 if (a != NULL)
2306 uerror("function takes no arguments");
2307 goto build; /* void function */
2308 } else {
2309 if (a == NULL) {
2310 uerror("function needs arguments");
2311 goto build;
2312 }
2313 }
2314 #ifdef PCC_DEBUG
2315 if (pdebug) {
2316 printf("arglist for %s\n",
2317 f->n_sp != NULL ? f->n_sp->sname : "function pointer");
2318 alprint(al, 0);
2319 }
2320 #endif
2321
2322 /*
2323 * Create a list of pointers to the nodes given as arg.
2324 */
2325 for (w = a, i = 1; w->n_op == CM; w = w->n_left)
2326 i++;
2327 apary = FUNALLO(sizeof(struct ap) * i);
2328
2329 for (w = a, i = 0; w->n_op == CM; w = w->n_left) {
2330 at = &apary[i++];
2331 at->node = w->n_right;
2332 at->next = apole;
2333 apole = at;
2334 }
2335 at = &apary[i];
2336 at->node = w;
2337 at->next = apole;
2338 apole = at;
2339
2340 /*
2341 * Do the typechecking by walking up the list.
2342 */
2343 argidx = 1;
2344 while (al->type != TNULL) {
2345 if (al->type == TELLIPSIS) {
2346 /* convert the rest of float to double */
2347 for (; apole; apole = apole->next) {
2348 if (apole->node->n_type != FLOAT)
2349 continue;
2350 MKTY(apole->node, DOUBLE, 0, 0);
2351 }
2352 goto build;
2353 }
2354 if (apole == NULL) {
2355 uerror("too few arguments to function");
2356 goto build;
2357 }
2358 /* al = prototyp, apole = argument till ftn */
2359 /* type = argumentets typ, arrt = prototypens typ */
2360 type = apole->node->n_type;
2361 arrt = al->type;
2362 #if 0
2363 if ((hasarray = ISARY(arrt)))
2364 arrt += (PTR-ARY);
2365 #endif
2366 /* Taking addresses of arrays are meaningless in expressions */
2367 /* but people tend to do that and also use in prototypes */
2368 /* this is mostly a problem with typedefs */
2369 if (ISARY(type)) {
2370 if (ISPTR(arrt) && ISARY(DECREF(arrt)))
2371 type = INCREF(type);
2372 else
2373 type += (PTR-ARY);
2374 } else if (ISPTR(type) && !ISARY(DECREF(type)) &&
2375 ISPTR(arrt) && ISARY(DECREF(arrt))) {
2376 type += (ARY-PTR);
2377 type = INCREF(type);
2378 }
2379
2380 /* Check structs */
2381 if (type <= BTMASK && arrt <= BTMASK) {
2382 #ifndef NO_COMPLEX
2383 if ((type != arrt) && (ANYCX(apole->node) ||
2384 (arrt == STRTY &&
2385 attr_find(al[1].sap, ATTR_COMPLEX)))) {
2386 cxargfixup(apole->node, arrt, al[1].sap);
2387 } else
2388 #endif
2389 if (type != arrt) {
2390 if (ISSOU(BTYPE(type)) || ISSOU(BTYPE(arrt))) {
2391 incomp: uerror("incompatible types for arg %d",
2392 argidx);
2393 } else {
2394 MKTY(apole->node, arrt, 0, 0)
2395 }
2396 #ifndef NO_COMPLEX
2397 } else if (type == STRTY &&
2398 attr_find(apole->node->n_ap, ATTR_COMPLEX) &&
2399 attr_find(al[1].sap, ATTR_COMPLEX)) {
2400 /* Both are complex */
2401 if (strmemb(apole->node->n_ap)->stype !=
2402 strmemb(al[1].sap)->stype) {
2403 /* must convert to correct type */
2404 w = p1alloc();
2405 *w = *apole->node;
2406 w = mkcmplx(w,
2407 strmemb(al[1].sap)->stype);
2408 *apole->node = *w;
2409 nfree(w);
2410 }
2411 goto out;
2412 #endif
2413 } else if (ISSOU(BTYPE(type))) {
2414 if (!suemeq(apole->node->n_ap, al[1].sap))
2415 goto incomp;
2416 }
2417 goto out;
2418 }
2419
2420 /* XXX should (recusively) check return type and arg list of
2421 func ptr arg XXX */
2422 if (ISFTN(DECREF(arrt)) && ISFTN(type))
2423 type = INCREF(type);
2424
2425 /* Hereafter its only pointers (or arrays) left */
2426 /* Check for struct/union intermixing with other types */
2427 if (((type <= BTMASK) && ISSOU(BTYPE(type))) ||
2428 ((arrt <= BTMASK) && ISSOU(BTYPE(arrt))))
2429 goto incomp;
2430
2431 /* Check for struct/union compatibility */
2432 if (type == arrt) {
2433 if (ISSOU(BTYPE(type))) {
2434 if (suemeq(apole->node->n_ap, al[1].sap))
2435 goto out;
2436 } else
2437 goto out;
2438 }
2439 if (BTYPE(arrt) == VOID && type > BTMASK)
2440 goto skip; /* void *f = some pointer */
2441 if (arrt > BTMASK && BTYPE(type) == VOID)
2442 goto skip; /* some *f = void pointer */
2443 if (apole->node->n_op == ICON && glval(apole->node) == 0)
2444 goto skip; /* Anything assigned a zero */
2445
2446 if ((type & ~BTMASK) == (arrt & ~BTMASK)) {
2447 /* do not complain for pointers with signedness */
2448 if ((DEUNSIGN(BTYPE(type)) == DEUNSIGN(BTYPE(arrt))) &&
2449 (BTYPE(type) != BTYPE(arrt))) {
2450 warner(Wpointer_sign);
2451 goto skip;
2452 }
2453 }
2454
2455 werror("implicit conversion of argument %d due to prototype",
2456 argidx);
2457
2458 skip: if (ISSOU(BTYPE(arrt))) {
2459 MKTY(apole->node, arrt, 0, al[1].sap)
2460 } else {
2461 MKTY(apole->node, arrt, 0, 0)
2462 }
2463
2464 out: al++;
2465 if (ISSOU(BTYPE(arrt)))
2466 al++;
2467 #if 0
2468 while (arrt > BTMASK && !ISFTN(arrt))
2469 arrt = DECREF(arrt);
2470 if (ISFTN(arrt) || hasarray)
2471 al++;
2472 #else
2473 while (arrt > BTMASK) {
2474 if (ISARY(arrt) || ISFTN(arrt)) {
2475 al++;
2476 break;
2477 }
2478 arrt = DECREF(arrt);
2479 }
2480 #endif
2481 apole = apole->next;
2482 argidx++;
2483 }
2484 if (apole != NULL)
2485 uerror("too many arguments to function");
2486
2487 build: if (apary)
2488 FUNFREE(apary);
2489 if (sp != NULL && (sp->sflags & SINLINE) && (w = inlinetree(sp, f, a)))
2490 return w;
2491 return buildtree(a == NIL ? UCALL : CALL, f, a);
2492 }
2493
2494 static int
2495 chk2(TWORD type, union dimfun *dsym, union dimfun *ddef)
2496 {
2497 while (type > BTMASK) {
2498 switch (type & TMASK) {
2499 case ARY:
2500 /* may be declared without dimension */
2501 if (dsym->ddim == NOOFFSET)
2502 dsym->ddim = ddef->ddim;
2503 if (dsym->ddim < 0 && ddef->ddim < 0)
2504 ; /* dynamic arrays as arguments */
2505 else if (ddef->ddim > 0 && dsym->ddim != ddef->ddim)
2506 return 1;
2507 dsym++, ddef++;
2508 break;
2509 case FTN:
2510 /* old-style function headers with function pointers
2511 * will most likely not have a prototype.
2512 * This is not considered an error. */
2513 if (ddef->dfun == NULL) {
2514 #ifdef notyet
2515 werror("declaration not a prototype");
2516 #endif
2517 } else if (chkftn(dsym->dfun, ddef->dfun))
2518 return 1;
2519 dsym++, ddef++;
2520 break;
2521 }
2522 type = DECREF(type);
2523 }
2524 return 0;
2525 }
2526
2527 /*
2528 * Compare two function argument lists to see if they match.
2529 */
2530 int
2531 chkftn(union arglist *usym, union arglist *udef)
2532 {
2533 TWORD t2;
2534 int ty, tyn;
2535
2536 if (usym == NULL)
2537 return 0;
2538 if (cftnsp != NULL && udef == NULL && usym->type == VOID)
2539 return 0; /* foo() { function with foo(void); prototype */
2540 if (udef == NULL && usym->type != TNULL)
2541 return 1;
2542 while (usym->type != TNULL) {
2543 if (usym->type == udef->type)
2544 goto done;
2545 /*
2546 * If an old-style declaration, then all types smaller than
2547 * int are given as int parameters.
2548 */
2549 if (intcompare) {
2550 ty = BTYPE(usym->type);
2551 tyn = BTYPE(udef->type);
2552 if (ty == tyn || ty != INT)
2553 return 1;
2554 if (tyn == CHAR || tyn == UCHAR ||
2555 tyn == SHORT || tyn == USHORT)
2556 goto done;
2557 return 1;
2558 } else
2559 return 1;
2560
2561 done: ty = BTYPE(usym->type);
2562 t2 = usym->type;
2563 if (ISSOU(ty)) {
2564 usym++, udef++;
2565 if (suemeq(usym->sap, udef->sap) == 0)
2566 return 1;
2567 }
2568
2569 while (!ISFTN(t2) && !ISARY(t2) && t2 > BTMASK)
2570 t2 = DECREF(t2);
2571 if (t2 > BTMASK) {
2572 usym++, udef++;
2573 if (chk2(t2, usym->df, udef->df))
2574 return 1;
2575 }
2576 usym++, udef++;
2577 }
2578 if (usym->type != udef->type)
2579 return 1;
2580 return 0;
2581 }
2582
2583 void
2584 fixtype(NODE *p, int class)
2585 {
2586 unsigned int t, type;
2587 int mod1, mod2;
2588 /* fix up the types, and check for legality */
2589
2590 /* forward declared enums */
2591 if (BTYPE(p->n_sp->stype) == ENUMTY) {
2592 MODTYPE(p->n_sp->stype, strmemb(p->n_sp->sap)->stype);
2593 }
2594
2595 if( (type = p->n_type) == UNDEF ) return;
2596 if ((mod2 = (type&TMASK))) {
2597 t = DECREF(type);
2598 while( mod1=mod2, mod2 = (t&TMASK) ){
2599 if( mod1 == ARY && mod2 == FTN ){
2600 uerror( "array of functions is illegal" );
2601 type = 0;
2602 }
2603 else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){
2604 uerror( "function returns illegal type" );
2605 type = 0;
2606 }
2607 t = DECREF(t);
2608 }
2609 }
2610
2611 /* detect function arguments, watching out for structure declarations */
2612 if (rpole && ISFTN(type)) {
2613 uerror("function illegal in structure or union");
2614 type = INCREF(type);
2615 }
2616 p->n_type = type;
2617 }
2618
2619 /*
2620 * give undefined version of class
2621 */
2622 int
2623 uclass(int class)
2624 {
2625 if (class == SNULL)
2626 return(EXTERN);
2627 else if (class == STATIC)
2628 return(USTATIC);
2629 else
2630 return(class);
2631 }
2632
2633 int
2634 fixclass(int class, TWORD type)
2635 {
2636 extern int fun_inline;
2637
2638 /* first, fix null class */
2639 if (class == SNULL) {
2640 if (fun_inline && ISFTN(type))
2641 return SNULL;
2642 if (rpole)
2643 cerror("field8");
2644 else if (blevel == 0)
2645 class = EXTDEF;
2646 else
2647 class = AUTO;
2648 }
2649
2650 /* now, do general checking */
2651
2652 if( ISFTN( type ) ){
2653 switch( class ) {
2654 default:
2655 uerror( "function has illegal storage class" );
2656 case AUTO:
2657 class = EXTERN;
2658 case EXTERN:
2659 case EXTDEF:
2660 case TYPEDEF:
2661 case STATIC:
2662 case USTATIC:
2663 ;
2664 }
2665 }
2666
2667 if (class & FIELD) {
2668 cerror("field3");
2669 }
2670
2671 switch (class) {
2672
2673 case MOS:
2674 case MOU:
2675 cerror("field4");
2676
2677 case REGISTER:
2678 if (blevel == 0)
2679 uerror("illegal register declaration");
2680 if (blevel == 1)
2681 return(PARAM);
2682 else
2683 return(REGISTER);
2684
2685 case AUTO:
2686 if( blevel < 2 ) uerror( "illegal ULABEL class" );
2687 return( class );
2688
2689 case EXTERN:
2690 case STATIC:
2691 case EXTDEF:
2692 case TYPEDEF:
2693 case USTATIC:
2694 case PARAM:
2695 return( class );
2696
2697 default:
2698 cerror( "illegal class: %d", class );
2699 /* NOTREACHED */
2700
2701 }
2702 return 0; /* XXX */
2703 }
2704
2705 /*
2706 * Generates a goto statement; sets up label number etc.
2707 */
2708 void
2709 gotolabel(char *name)
2710 {
2711 struct symtab *s = lookup(name, SLBLNAME|STEMP);
2712
2713 if (s->soffset == 0) {
2714 s->soffset = -getlab();
2715 s->sclass = STATIC;
2716 }
2717 branch(s->soffset < 0 ? -s->soffset : s->soffset);
2718 }
2719
2720 /*
2721 * Sets a label for gotos.
2722 */
2723 void
2724 deflabel(char *name, NODE *p)
2725 {
2726 struct symtab *s = lookup(name, SLBLNAME|STEMP);
2727
2728 #ifdef GCC_COMPAT
2729 s->sap = gcc_attr_parse(p);
2730 #endif
2731 if (s->soffset > 0)
2732 uerror("label '%s' redefined", name);
2733 if (s->soffset == 0) {
2734 s->soffset = getlab();
2735 s->sclass = STATIC;
2736 }
2737 if (s->soffset < 0)
2738 s->soffset = -s->soffset;
2739 plabel( s->soffset);
2740 }
2741
2742 struct symtab *
2743 getsymtab(char *name, int flags)
2744 {
2745 struct symtab *s;
2746
2747 if (flags & SSTMT) {
2748 s = stmtalloc(sizeof(struct symtab));
2749 } else if (flags & SBLK) {
2750 s = blkalloc(sizeof(struct symtab));
2751 } else if (flags & STEMP) {
2752 s = tmpalloc(sizeof(struct symtab));
2753 } else {
2754 s = permalloc(sizeof(struct symtab));
2755 symtabcnt++;
2756 }
2757 s->sname = name;
2758 s->snext = NULL;
2759 s->stype = UNDEF;
2760 s->squal = 0;
2761 s->sclass = SNULL;
2762 s->sflags = (short)(flags & SMASK);
2763 s->soffset = 0;
2764 s->slevel = (char)blevel;
2765 s->sdf = NULL;
2766 s->sap = NULL;
2767 return s;
2768 }
2769
2770 int
2771 fldchk(int sz)
2772 {
2773 if (rpole->rsou != STNAME && rpole->rsou != UNAME)
2774 uerror("field outside of structure");
2775 if (sz < 0 || sz >= FIELD) {
2776 uerror("illegal field size");
2777 return 1;
2778 }
2779 return 0;
2780 }
2781
2782 #ifdef PCC_DEBUG
2783 static char *
2784 ccnames[] = { /* names of storage classes */
2785 "SNULL",
2786 "AUTO",
2787 "EXTERN",
2788 "STATIC",
2789 "REGISTER",
2790 "EXTDEF",
2791 "LABEL",
2792 "ULABEL",
2793 "MOS",
2794 "PARAM",
2795 "STNAME",
2796 "MOU",
2797 "UNAME",
2798 "TYPEDEF",
2799 "FORTRAN",
2800 "ENAME",
2801 "MOE",
2802 "UFORTRAN",
2803 "USTATIC",
2804 };
2805
2806 char *
2807 scnames(int c)
2808 {
2809 /* return the name for storage class c */
2810 static char buf[12];
2811 if( c&FIELD ){
2812 snprintf( buf, sizeof(buf), "FIELD[%d]", c&FLDSIZ );
2813 return( buf );
2814 }
2815 return( ccnames[c] );
2816 }
2817 #endif
2818
2819 #if 0
2820 static char *stack_chk_fail = "__stack_smash_handler";
2821 static char *stack_chk_guard = "__guard";
2822 #else
2823 static char *stack_chk_fail = "__stack_chk_fail";
2824 static char *stack_chk_guard = "__stack_chk_guard";
2825 #endif
2826 static char *stack_chk_canary = "__stack_chk_canary";
2827
2828 void
2829 sspinit(void)
2830 {
2831 NODE *p;
2832
2833 p = block(NAME, NIL, NIL, FTN+VOID, 0, 0);
2834 p->n_sp = lookup(stack_chk_fail, SNORMAL);
2835 defid(p, EXTERN);
2836 nfree(p);
2837
2838 p = block(NAME, NIL, NIL, INT, 0, 0);
2839 p->n_sp = lookup(stack_chk_guard, SNORMAL);
2840 defid(p, EXTERN);
2841 nfree(p);
2842 }
2843
2844 void
2845 sspstart(void)
2846 {
2847 NODE *p, *q;
2848
2849 q = block(NAME, NIL, NIL, INT, 0, 0);
2850 q->n_sp = lookup(stack_chk_guard, SNORMAL);
2851 q = clocal(q);
2852
2853 p = block(REG, NIL, NIL, INCREF(INT), 0, 0);
2854 slval(p, 0);
2855 p->n_rval = FPREG;
2856 p = cast(p, INT, 0);
2857 q = buildtree(ER, p, q);
2858
2859 p = block(NAME, NIL, NIL, INT, 0, 0);
2860 p->n_qual = VOL >> TSHIFT;
2861 p->n_sp = lookup(stack_chk_canary, SNORMAL);
2862 defid(p, AUTO);
2863 p = clocal(p);
2864 ecomp(buildtree(ASSIGN, p, q));
2865 }
2866
2867 void
2868 sspend(void)
2869 {
2870 NODE *p, *q;
2871 TWORD t;
2872 int lab;
2873
2874 if (retlab != NOLAB) {
2875 plabel(retlab);
2876 retlab = getlab();
2877 }
2878
2879 t = DECREF(cftnsp->stype);
2880 if (t == BOOL)
2881 t = BOOL_TYPE;
2882
2883 p = block(NAME, NIL, NIL, INT, 0, 0);
2884 p->n_sp = lookup(stack_chk_canary, SNORMAL);
2885 p = clocal(p);
2886
2887 q = block(REG, NIL, NIL, INCREF(INT), 0, 0);
2888 slval(q, 0);
2889 q->n_rval = FPREG;
2890 q = cast(q, INT, 0);
2891 q = buildtree(ER, p, q);
2892
2893 p = block(NAME, NIL, NIL, INT, 0, 0);
2894 p->n_sp = lookup(stack_chk_guard, SNORMAL);
2895 p = clocal(p);
2896
2897 lab = getlab();
2898 cbranch(buildtree(EQ, p, q), bcon(lab));
2899
2900 p = block(NAME, NIL, NIL, FTN+VOID, 0, 0);
2901 p->n_sp = lookup(stack_chk_fail, SNORMAL);
2902 p = clocal(p);
2903
2904 q = eve(bdty(STRING, cftnsp->sname, PTR|CHAR));
2905 ecomp(buildtree(CALL, p, q));
2906
2907 plabel(lab);
2908 }
2909
2910 /*
2911 * Fetch pointer to first member in a struct list.
2912 */
2913 struct symtab *
2914 strmemb(struct attr *ap)
2915 {
2916
2917 if ((ap = attr_find(ap, ATTR_STRUCT)) == NULL)
2918 cerror("strmemb");
2919 return ap->amlist;
2920 }
2921
2922 #ifndef NO_COMPLEX
2923
2924 static char *real, *imag;
2925 static struct symtab *cxsp[3], *cxmul[3], *cxdiv[3];
2926 static char *cxnmul[] = { "__mulsc3", "__muldc3", "__mulxc3" };
2927 static char *cxndiv[] = { "__divsc3", "__divdc3", "__divxc3" };
2928 /*
2929 * As complex numbers internally are handled as structs, create
2930 * these by hand-crafting them.
2931 */
2932 void
2933 complinit(void)
2934 {
2935 struct attr *ap;
2936 struct rstack *rp;
2937 NODE *p, *q;
2938 char *n[] = { "0f", "0d", "0l" };
2939 int i, d_debug;
2940
2941 d_debug = ddebug;
2942 ddebug = 0;
2943 real = addname("__real");
2944 imag = addname("__imag");
2945 p = block(NAME, NIL, NIL, FLOAT, 0, 0);
2946 for (i = 0; i < 3; i++) {
2947 p->n_type = FLOAT+i;
2948 rpole = rp = bstruct(NULL, STNAME, NULL);
2949 soumemb(p, real, 0);
2950 soumemb(p, imag, 0);
2951 q = dclstruct(rp);
2952 cxsp[i] = q->n_sp = lookup(addname(n[i]), 0);
2953 defid(q, TYPEDEF);
2954 ap = attr_new(ATTR_COMPLEX, 0);
2955 q->n_sp->sap = attr_add(q->n_sp->sap, ap);
2956 nfree(q);
2957 }
2958 /* create function declarations for external ops */
2959 for (i = 0; i < 3; i++) {
2960 cxnmul[i] = addname(cxnmul[i]);
2961 p->n_sp = cxmul[i] = lookup(cxnmul[i], 0);
2962 p->n_type = FTN|STRTY;
2963 p->n_ap = cxsp[i]->sap;
2964 p->n_df = cxsp[i]->sdf;
2965 defid2(p, EXTERN, 0);
2966 cxmul[i]->sdf = permalloc(sizeof(union dimfun));
2967 dimfuncnt++;
2968 cxmul[i]->sdf->dfun = NULL;
2969 cxndiv[i] = addname(cxndiv[i]);
2970 p->n_sp = cxdiv[i] = lookup(cxndiv[i], 0);
2971 p->n_type = FTN|STRTY;
2972 p->n_ap = cxsp[i]->sap;
2973 p->n_df = cxsp[i]->sdf;
2974 defid2(p, EXTERN, 0);
2975 cxdiv[i]->sdf = permalloc(sizeof(union dimfun));
2976 dimfuncnt++;
2977 cxdiv[i]->sdf->dfun = NULL;
2978 }
2979 nfree(p);
2980 ddebug = d_debug;
2981 }
2982
2983 static TWORD
2984 maxtt(NODE *p)
2985 {
2986 TWORD t;
2987
2988 t = ANYCX(p) ? strmemb(p->n_ap)->stype : p->n_type;
2989 t = BTYPE(t);
2990 if (t == VOID)
2991 t = CHAR; /* pointers */
2992 if (ISITY(t))
2993 t -= (FIMAG - FLOAT);
2994 return t;
2995 }
2996
2997 /*
2998 * Return the highest real floating point type.
2999 * Known that at least one type is complex or imaginary.
3000 */
3001 static TWORD
3002 maxtyp(NODE *l, NODE *r)
3003 {
3004 TWORD tl, tr, t;
3005
3006 tl = maxtt(l);
3007 tr = maxtt(r);
3008 t = tl > tr ? tl : tr;
3009 if (!ISFTY(t))
3010 cerror("maxtyp");
3011 return t;
3012 }
3013
3014 /*
3015 * Fetch space on stack for complex struct.
3016 */
3017 static NODE *
3018 cxstore(TWORD t)
3019 {
3020 struct symtab s;
3021
3022 s = *cxsp[t - FLOAT];
3023 s.sclass = AUTO;
3024 s.soffset = NOOFFSET;
3025 oalloc(&s, &autooff);
3026 return nametree(&s);
3027 }
3028
3029 #define comop(x,y) buildtree(COMOP, x, y)
3030
3031 /*
3032 * Convert node p to complex type dt.
3033 */
3034 static NODE *
3035 mkcmplx(NODE *p, TWORD dt)
3036 {
3037 NODE *q, *r, *i, *t;
3038
3039 if (!ANYCX(p)) {
3040 /* Not complex, convert to complex on stack */
3041 q = cxstore(dt);
3042 if (ISITY(p->n_type)) {
3043 p->n_type = p->n_type - FIMAG + FLOAT;
3044 r = bcon(0);
3045 i = p;
3046 } else {
3047 if (ISPTR(p->n_type))
3048 p = cast(p, INTPTR, 0);
3049 r = p;
3050 i = bcon(0);
3051 }
3052 p = buildtree(ASSIGN, structref(ccopy(q), DOT, real), r);
3053 p = comop(p, buildtree(ASSIGN, structref(ccopy(q), DOT, imag), i));
3054 p = comop(p, q);
3055 } else {
3056 if (strmemb(p->n_ap)->stype != dt) {
3057 q = cxstore(dt);
3058 p = buildtree(ADDROF, p, NIL);
3059 t = tempnode(0, p->n_type, p->n_df, p->n_ap);
3060 p = buildtree(ASSIGN, ccopy(t), p);
3061 p = comop(p, buildtree(ASSIGN,
3062 structref(ccopy(q), DOT, real),
3063 structref(ccopy(t), STREF, real)));
3064 p = comop(p, buildtree(ASSIGN,
3065 structref(ccopy(q), DOT, imag),
3066 structref(t, STREF, imag)));
3067 p = comop(p, q);
3068 }
3069 }
3070 return p;
3071 }
3072
3073 static NODE *
3074 cxasg(NODE *l, NODE *r)
3075 {
3076 TWORD tl, tr;
3077
3078 tl = strattr(l->n_ap) ? strmemb(l->n_ap)->stype : 0;
3079 tr = strattr(r->n_ap) ? strmemb(r->n_ap)->stype : 0;
3080
3081 if (ANYCX(l) && ANYCX(r) && tl != tr) {
3082 /* different types in structs */
3083 r = mkcmplx(r, tl);
3084 } else if (!ANYCX(l))
3085 r = structref(r, DOT, ISITY(l->n_type) ? imag : real);
3086 else if (!ANYCX(r))
3087 r = mkcmplx(r, tl);
3088 return buildtree(ASSIGN, l, r);
3089 }
3090
3091 /*
3092 * Fixup complex operations.
3093 * At least one operand is complex.
3094 */
3095 NODE *
3096 cxop(int op, NODE *l, NODE *r)
3097 {
3098 TWORD mxtyp;
3099 NODE *p, *q;
3100 NODE *ltemp, *rtemp;
3101 NODE *real_l, *imag_l;
3102 NODE *real_r, *imag_r;
3103 real_r = imag_r = NULL; /* bad uninit var warning */
3104
3105 if (op == ASSIGN)
3106 return cxasg(l, r);
3107
3108 mxtyp = maxtyp(l, r);
3109 l = mkcmplx(l, mxtyp);
3110 if (op != UMINUS)
3111 r = mkcmplx(r, mxtyp);
3112
3113 if (op == COLON)
3114 return buildtree(COLON, l, r);
3115
3116 /* put a pointer to left and right elements in a TEMP */
3117 l = buildtree(ADDROF, l, NIL);
3118 ltemp = tempnode(0, l->n_type, l->n_df, l->n_ap);
3119 l = buildtree(ASSIGN, ccopy(ltemp), l);
3120
3121 if (op != UMINUS) {
3122 r = buildtree(ADDROF, r, NIL);
3123 rtemp = tempnode(0, r->n_type, r->n_df, r->n_ap);
3124 r = buildtree(ASSIGN, ccopy(rtemp), r);
3125
3126 p = comop(l, r);
3127 } else
3128 p = l;
3129
3130 /* create the four trees needed for calculation */
3131 real_l = structref(ccopy(ltemp), STREF, real);
3132 imag_l = structref(ltemp, STREF, imag);
3133 if (op != UMINUS) {
3134 real_r = structref(ccopy(rtemp), STREF, real);
3135 imag_r = structref(rtemp, STREF, imag);
3136 }
3137
3138 /* get storage on stack for the result */
3139 q = cxstore(mxtyp);
3140
3141 switch (op) {
3142 case NE:
3143 case EQ:
3144 tfree(q);
3145 p = buildtree(op, comop(p, real_l), real_r);
3146 q = buildtree(op, imag_l, imag_r);
3147 p = buildtree(op == EQ ? ANDAND : OROR, p, q);
3148 return p;
3149
3150 case ANDAND:
3151 case OROR: /* go via EQ to get INT of it */
3152 tfree(q);
3153 p = buildtree(NE, comop(p, real_l), bcon(0)); /* gets INT */
3154 q = buildtree(NE, imag_l, bcon(0));
3155 p = buildtree(OR, p, q);
3156
3157 q = buildtree(NE, real_r, bcon(0));
3158 q = buildtree(OR, q, buildtree(NE, imag_r, bcon(0)));
3159
3160 p = buildtree(op, p, q);
3161 return p;
3162
3163 case UMINUS:
3164 p = comop(p, buildtree(ASSIGN, structref(ccopy(q), DOT, real),
3165 buildtree(op, real_l, NIL)));
3166 p = comop(p, buildtree(ASSIGN, structref(ccopy(q), DOT, imag),
3167 buildtree(op, imag_l, NIL)));
3168 break;
3169
3170 case PLUS:
3171 case MINUS:
3172 p = comop(p, buildtree(ASSIGN, structref(ccopy(q), DOT, real),
3173 buildtree(op, real_l, real_r)));
3174 p = comop(p, buildtree(ASSIGN, structref(ccopy(q), DOT, imag),
3175 buildtree(op, imag_l, imag_r)));
3176 break;
3177
3178 case MUL:
3179 case DIV:
3180 /* Complex mul is "complex" */
3181 /* (u+iv)*(x+iy)=((u*x)-(v*y))+i(v*x+y*u) */
3182 /* Complex div is even more "complex" */
3183 /* (u+iv)/(x+iy)=(u*x+v*y)/(x*x+y*y)+i((v*x-u*y)/(x*x+y*y)) */
3184 /* but we need to do it via a subroutine */
3185 tfree(q);
3186 p = buildtree(CM, comop(p, real_l), imag_l);
3187 p = buildtree(CM, p, real_r);
3188 p = buildtree(CM, p, imag_r);
3189 q = nametree(op == DIV ?
3190 cxdiv[mxtyp-FLOAT] : cxmul[mxtyp-FLOAT]);
3191 return buildtree(CALL, q, p);
3192 break;
3193 default:
3194 uerror("illegal operator %s", copst(op));
3195 }
3196 return comop(p, q);
3197 }
3198
3199 /*
3200 * Fixup imaginary operations.
3201 * At least one operand is imaginary, none is complex.
3202 */
3203 NODE *
3204 imop(int op, NODE *l, NODE *r)
3205 {
3206 NODE *p, *q;
3207 TWORD mxtyp;
3208 int li, ri;
3209
3210 li = ri = 0;
3211 if (ISITY(l->n_type))
3212 li = 1, l->n_type = l->n_type - (FIMAG-FLOAT);
3213 if (ISITY(r->n_type))
3214 ri = 1, r->n_type = r->n_type - (FIMAG-FLOAT);
3215
3216 mxtyp = maxtyp(l, r);
3217 switch (op) {
3218 case ASSIGN:
3219 /* if both are imag, store value, otherwise store 0.0 */
3220 if (!(li && ri)) {
3221 tfree(r);
3222 r = bcon(0);
3223 }
3224 p = buildtree(ASSIGN, l, r);
3225 p->n_type += (FIMAG-FLOAT);
3226 break;
3227
3228 case PLUS:
3229 if (li && ri) {
3230 p = buildtree(PLUS, l, r);
3231 p->n_type += (FIMAG-FLOAT);
3232 } else {
3233 /* If one is imaginary and one is real, make complex */
3234 if (li)
3235 q = l, l = r, r = q; /* switch */
3236 q = cxstore(mxtyp);
3237 p = buildtree(ASSIGN,
3238 structref(ccopy(q), DOT, real), l);
3239 p = comop(p, buildtree(ASSIGN,
3240 structref(ccopy(q), DOT, imag), r));
3241 p = comop(p, q);
3242 }
3243 break;
3244
3245 case MINUS:
3246 if (li && ri) {
3247 p = buildtree(MINUS, l, r);
3248 p->n_type += (FIMAG-FLOAT);
3249 } else if (li) {
3250 q = cxstore(mxtyp);
3251 p = buildtree(ASSIGN, structref(ccopy(q), DOT, real),
3252 buildtree(UMINUS, r, NIL));
3253 p = comop(p, buildtree(ASSIGN,
3254 structref(ccopy(q), DOT, imag), l));
3255 p = comop(p, q);
3256 } else /* if (ri) */ {
3257 q = cxstore(mxtyp);
3258 p = buildtree(ASSIGN,
3259 structref(ccopy(q), DOT, real), l);
3260 p = comop(p, buildtree(ASSIGN,
3261 structref(ccopy(q), DOT, imag),
3262 buildtree(UMINUS, r, NIL)));
3263 p = comop(p, q);
3264 }
3265 break;
3266
3267 case MUL:
3268 p = buildtree(MUL, l, r);
3269 if (li && ri)
3270 p = buildtree(UMINUS, p, NIL);
3271 if (li ^ ri)
3272 p->n_type += (FIMAG-FLOAT);
3273 break;
3274
3275 case DIV:
3276 p = buildtree(DIV, l, r);
3277 if (ri && !li)
3278 p = buildtree(UMINUS, p, NIL);
3279 if (li ^ ri)
3280 p->n_type += (FIMAG-FLOAT);
3281 break;
3282
3283 case EQ:
3284 case NE:
3285 case LT:
3286 case LE:
3287 case GT:
3288 case GE:
3289 if (li ^ ri) { /* always 0 */
3290 tfree(l);
3291 tfree(r);
3292 p = bcon(0);
3293 } else
3294 p = buildtree(op, l, r);
3295 break;
3296
3297 default:
3298 cerror("imop");
3299 p = NULL;
3300 }
3301 return p;
3302 }
3303
3304 NODE *
3305 cxelem(int op, NODE *p)
3306 {
3307
3308 if (ANYCX(p)) {
3309 p = structref(p, DOT, op == XREAL ? real : imag);
3310 } else if (op == XIMAG) {
3311 /* XXX sanitycheck? */
3312 tfree(p);
3313 p = bcon(0);
3314 }
3315 return p;
3316 }
3317
3318 NODE *
3319 cxconj(NODE *p)
3320 {
3321 NODE *q, *r;
3322
3323 /* XXX side effects? */
3324 q = cxstore(strmemb(p->n_ap)->stype);
3325 r = buildtree(ASSIGN, structref(ccopy(q), DOT, real),
3326 structref(ccopy(p), DOT, real));
3327 r = comop(r, buildtree(ASSIGN, structref(ccopy(q), DOT, imag),
3328 buildtree(UMINUS, structref(p, DOT, imag), NIL)));
3329 return comop(r, q);
3330 }
3331
3332 /*
3333 * Prepare for return.
3334 * There may be implicit casts to other types.
3335 */
3336 NODE *
3337 imret(NODE *p, NODE *q)
3338 {
3339 if (ISITY(q->n_type) && ISITY(p->n_type)) {
3340 if (p->n_type != q->n_type) {
3341 p->n_type -= (FIMAG-FLOAT);
3342 p = cast(p, q->n_type - (FIMAG-FLOAT), 0);
3343 p->n_type += (FIMAG-FLOAT);
3344 }
3345 } else {
3346 p1tfree(p);
3347 if (ISITY(q->n_type)) {
3348 p = block(FCON, 0, 0, q->n_type, 0, 0);
3349 p->n_scon = sfallo();
3350 FLOAT_INT2FP(p->n_scon, 0, INT);
3351 } else
3352 p = bcon(0);
3353 }
3354
3355 return p;
3356 }
3357
3358 /*
3359 * Prepare for return.
3360 * There may be implicit casts to other types.
3361 */
3362 NODE *
3363 cxret(NODE *p, NODE *q)
3364 {
3365 if (ANYCX(q)) { /* Return complex type */
3366 p = mkcmplx(p, strmemb(q->n_ap)->stype);
3367 } else if (q->n_type < STRTY || ISITY(q->n_type)) { /* real or imag */
3368 p = structref(p, DOT, ISITY(q->n_type) ? imag : real);
3369 if (p->n_type != q->n_type)
3370 p = cast(p, q->n_type, 0);
3371 } else
3372 cerror("cxred failing type");
3373 return p;
3374 }
3375
3376 /*
3377 * either p1 or p2 is complex, so fixup the remaining type accordingly.
3378 */
3379 NODE *
3380 cxcast(NODE *p1, NODE *p2)
3381 {
3382 if (ANYCX(p1) && ANYCX(p2)) {
3383 if (p1->n_type != p2->n_type)
3384 p2 = mkcmplx(p2, p1->n_type);
3385 } else if (ANYCX(p1)) {
3386 p2 = mkcmplx(p2, strmemb(p1->n_ap)->stype);
3387 } else /* if (ANYCX(p2)) */ {
3388 p2 = cast(structref(p2, DOT, real), p1->n_type, 0);
3389 }
3390 nfree(p1);
3391 return p2;
3392 }
3393
3394 static void
3395 cxargfixup(NODE *a, TWORD dt, struct attr *ap)
3396 {
3397 NODE *p;
3398 TWORD t;
3399
3400 p = p1alloc();
3401 *p = *a;
3402 if (dt == STRTY) {
3403 /* dest complex */
3404 t = strmemb(ap)->stype;
3405 p = mkcmplx(p, t);
3406 } else {
3407 /* src complex, not dest */
3408 p = structref(p, DOT, ISFTY(dt) ? real : imag);
3409 }
3410 *a = *p;
3411 nfree(p);
3412 }
3413 #endif
3414
3415 /*
3416 * Allocations:
3417 * permalloc() Never freed. in pass2.
3418 * tmpalloc() during a function lifetime, then freed. in pass2.
3419 * blkalloc() during a block lifetime. Variables etc. In pass1.
3420 * stmtalloc() during a statement lifetime. Expression trees. In pass1.
3421 */
3422
3423 /*
3424 * Short-time allocations during statements.
3425 */
3426 #define MEMCHUNKSZ 8192 /* 8k per allocation */
3427 struct balloc {
3428 char a1;
3429 union {
3430 long long l;
3431 long double d;
3432 } a2;
3433 };
3434 #define ALIGNMENT offsetof(struct balloc, a2)
3435 #define ROUNDUP(x) (((x) + ((ALIGNMENT)-1)) & ~((ALIGNMENT)-1))
3436
3437 #define MAXSZ MEMCHUNKSZ-sizeof(struct xalloc *)
3438 struct xalloc {
3439 struct xalloc *next;
3440 union {
3441 long long b; /* for initial alignment */
3442 long double d;
3443 char elm[MAXSZ];
3444 };
3445 } *sapole, *bkpole;
3446 int cstp, cbkp;
3447
3448 void *
3449 stmtalloc(size_t size)
3450 {
3451 struct xalloc *xp;
3452 void *rv;
3453
3454 size = ROUNDUP(size);
3455 if (size > MAXSZ)
3456 cerror("stmtalloc");
3457 if (sapole == 0 || (size + cstp) > MAXSZ) {
3458 xp = xmalloc(sizeof(struct xalloc));
3459 xp->next = sapole;
3460 sapole = xp;
3461 cstp = 0;
3462 }
3463 rv = &sapole->elm[cstp];
3464 cstp += (int)size;
3465 return rv;
3466 }
3467
3468 void
3469 stmtfree(void)
3470 {
3471 extern P1ND *frelink;
3472 extern int usdnodes;
3473 struct xalloc *x1;
3474
3475 if (usdnodes != 0)
3476 cerror("stmtfree: usdnodes %d", usdnodes);
3477 frelink = NULL;
3478
3479 while (sapole) {
3480 x1 = sapole->next;
3481 free(sapole);
3482 sapole = x1;
3483 }
3484 cstp = 0;
3485 }
3486
3487 void *
3488 blkalloc(size_t size)
3489 {
3490 struct xalloc *xp;
3491 void *rv;
3492
3493 if (blevel < 2)
3494 return permalloc(size);
3495
3496 size = ROUNDUP(size);
3497 if (size > MAXSZ)
3498 cerror("blkalloc");
3499 if (bkpole == 0 || (size + cbkp) > MAXSZ) {
3500 xp = xmalloc(sizeof(struct xalloc));
3501 xp->next = bkpole;
3502 bkpole = xp;
3503 cbkp = 0;
3504 }
3505 rv = &bkpole->elm[cbkp];
3506 cbkp += (int)size;
3507 return rv;
3508 }
3509
3510 void
3511 blkfree(void)
3512 {
3513 struct xalloc *x1;
3514
3515 while (bkpole) {
3516 x1 = bkpole->next;
3517 free(bkpole);
3518 bkpole = x1;
3519 }
3520 cbkp = 0;
3521 }
3522