Debian Codegraph @

cc/cxxcom/pftn.c

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