Debian Codegraph @

cc/ccom/gcc_compat.c

raw symbols SCIP
1 /* $Id: gcc_compat.c,v 1.122 2018/03/08 12:23:00 ragge Exp $ */
2 /*
3 * Copyright (c) 2004 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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Routines to support some of the gcc extensions to C.
31 */
32 #ifdef GCC_COMPAT
33
34 #include "pass1.h"
35 #include "cgram.h"
36
37 #include <string.h>
38
39 #define NODE P1ND
40 #define nfree p1nfree
41 #define tfree p1tfree
42
43 static struct kw {
44 char *name, *ptr;
45 int rv;
46 } kw[] = {
47 /*
48 * Do NOT change the order of these entries unless you know
49 * what you're doing!
50 */
51 /* 0 */ { "__asm", NULL, C_ASM },
52 /* 1 */ { "__signed", NULL, 0 },
53 /* 2 */ { "__inline", NULL, 0 },
54 /* 3 */ { "__const", NULL, 0 },
55 /* 4 */ { "__asm__", NULL, C_ASM },
56 /* 5 */ { "__inline__", NULL, 0 },
57 /* 6 */ { "__thread", NULL, 0 },
58 /* 7 */ { "__FUNCTION__", NULL, 0 },
59 /* 8 */ { "__volatile", NULL, 0 },
60 /* 9 */ { "__volatile__", NULL, 0 },
61 /* 10 */{ "__restrict", NULL, -1 },
62 /* 11 */{ "__typeof__", NULL, C_TYPEOF },
63 /* 12 */{ "typeof", NULL, C_TYPEOF },
64 /* 13 */{ "__extension__", NULL, -1 },
65 /* 14 */{ "__signed__", NULL, 0 },
66 /* 15 */{ "__attribute__", NULL, 0 },
67 /* 16 */{ "__attribute", NULL, 0 },
68 /* 17 */{ "__real__", NULL, 0 },
69 /* 18 */{ "__imag__", NULL, 0 },
70 /* 19 */{ "__builtin_offsetof", NULL, PCC_OFFSETOF },
71 /* 20 */{ "__PRETTY_FUNCTION__", NULL, 0 },
72 /* 21 */{ "__alignof__", NULL, C_ALIGNOF },
73 /* 22 */{ "__typeof", NULL, C_TYPEOF },
74 /* 23 */{ "__alignof", NULL, C_ALIGNOF },
75 /* 24 */{ "__restrict__", NULL, -1 },
76 { NULL, NULL, 0 },
77 };
78
79 /* g77 stuff */
80 #if SZFLOAT == SZLONG
81 #define G77_INTEGER LONG
82 #define G77_UINTEGER ULONG
83 #elif SZFLOAT == SZINT
84 #define G77_INTEGER INT
85 #define G77_UINTEGER UNSIGNED
86 #else
87 #error fix g77 stuff
88 #endif
89 #if SZFLOAT*2 == SZLONG
90 #define G77_LONGINT LONG
91 #define G77_ULONGINT ULONG
92 #elif SZFLOAT*2 == SZLONGLONG
93 #define G77_LONGINT LONGLONG
94 #define G77_ULONGINT ULONGLONG
95 #else
96 #error fix g77 long stuff
97 #endif
98
99 static TWORD g77t[] = { G77_INTEGER, G77_UINTEGER, G77_LONGINT, G77_ULONGINT };
100 static char *g77n[] = { "__g77_integer", "__g77_uinteger",
101 "__g77_longint", "__g77_ulongint" };
102
103 #ifdef TARGET_TIMODE
104 static char *loti, *hiti, *TISTR;
105 static char *lotf, *hitf, *TFSTR;
106 static char *loctf, *hictf, *loitf, *hiitf, *TCSTR;
107 static struct symtab *tisp, *tfsp, *tcsp, *ucmpti2sp, *cmpti2sp, *subvti3sp,
108 *addvti3sp, *mulvti3sp, *divti3sp, *udivti3sp, *modti3sp, *umodti3sp,
109 *ashlti3sp, *ashrti3sp, *lshrti3sp, *floatuntixfsp;
110
111 #define TYPE_TI 800
112 #define TYPE_TF 864
113 #define TYPE_TC 928
114
115 struct tifuns {
116 struct symtab **sp;
117 char *fun;
118 TWORD type;
119 int apt;
120 } tifuns[] = {
121 { &cmpti2sp, "__cmpti2", INT, 0 },
122 { &ucmpti2sp, "__ucmpti2", INT, 0 },
123
124 { &addvti3sp, "__addvti3", STRTY, 1 },
125 { &subvti3sp, "__subvti3", STRTY, 1 },
126 { &mulvti3sp, "__mulvti3", STRTY, 1 },
127 { &divti3sp, "__divti3", STRTY, 1 },
128 { &modti3sp, "__modti3", STRTY, 1 },
129
130 { &udivti3sp, "__udivti3", STRTY, 2 },
131 { &umodti3sp, "__umodti3", STRTY, 2 },
132 { &ashlti3sp, "__ashlti3", STRTY, 2 },
133 { &ashrti3sp, "__ashrti3", STRTY, 2 },
134 { &lshrti3sp, "__lshrti3", STRTY, 2 },
135
136 { &floatuntixfsp, "__floatuntixf", LDOUBLE, 0 },
137
138 };
139
140 static struct symtab *
141 addftn(char *n, TWORD t)
142 {
143 NODE *p = block(TYPE, 0, 0, 0, 0, 0);
144 struct symtab *sp;
145
146 sp = lookup(addname(n), 0);
147 p->n_type = INCREF(t) + (FTN-PTR);
148 p->n_sp = sp;
149 p->n_df = memset(permalloc(sizeof(union dimfun)), 0,
150 sizeof(union dimfun));
151 defid(p, EXTERN);
152 nfree(p);
153 return sp;
154 }
155
156 static struct symtab *
157 addstr(char *n, char *typ)
158 {
159 NODE *p = block(NAME, NIL, NIL, FLOAT, 0, 0);
160 struct symtab *sp;
161 NODE *q;
162 struct attr *ap;
163 struct rstack *rp;
164 extern struct rstack *rpole;
165
166 p->n_type = ctype(ULONGLONG);
167 rpole = rp = bstruct(NULL, STNAME, NULL);
168 if (typ[1] == 'I') {
169 soumemb(p, loti, 0);
170 soumemb(p, hiti, 0);
171 } else if (typ[1] == 'F') {
172 soumemb(p, lotf, 0);
173 soumemb(p, hitf, 0);
174 } else {
175 soumemb(p, loctf, 0);
176 soumemb(p, hictf, 0);
177 soumemb(p, loitf, 0);
178 soumemb(p, hiitf, 0);
179 }
180 q = dclstruct(rp);
181 sp = q->n_sp = lookup(addname(n), 0);
182 defid(q, TYPEDEF);
183 ap = attr_new(GCC_ATYP_MODE, 3);
184 ap->sarg(0) = addname(typ);
185 ap->iarg(1) = 0;
186 sp->sap = attr_add(sp->sap, ap);
187 nfree(q);
188 nfree(p);
189
190 return sp;
191 }
192 #endif
193
194 void
195 gcc_init(void)
196 {
197 struct kw *kwp;
198 NODE *p;
199 TWORD t;
200 int i, d_debug;
201
202 d_debug = ddebug;
203 ddebug = 0;
204 for (kwp = kw; kwp->name; kwp++)
205 kwp->ptr = addname(kwp->name);
206
207 for (i = 0; i < 4; i++) {
208 struct symtab *sp;
209 t = ctype(g77t[i]);
210 p = block(NAME, NIL, NIL, t, NULL, 0);
211 sp = lookup(addname(g77n[i]), 0);
212 p->n_sp = sp;
213 defid(p, TYPEDEF);
214 nfree(p);
215 }
216 ddebug = d_debug;
217 #ifdef TARGET_TIMODE
218 {
219 struct attr *ap2;
220
221 loti = addname("__loti");
222 hiti = addname("__hiti");
223 TISTR = addname("TI");
224 tisp = addstr("0ti", "TI");
225
226 lotf = addname("__lotf");
227 hitf = addname("__hitf");
228 TFSTR = addname("TF");
229 tfsp = addstr("0tf", "TF");
230
231 loctf = addname("__loctf");
232 hictf = addname("__hictf");
233 loitf = addname("__loitf");
234 hiitf = addname("__hiitf");
235 TCSTR = addname("TC");
236 tcsp = addstr("0tc", "TC");
237
238 ap2 = attr_new(GCC_ATYP_MODE, 3);
239 ap2->sarg(0) = TISTR;
240 ap2->iarg(1) = 1;
241 ap2 = attr_add(tisp->sap, ap2);
242
243 for (i = 0; i < (int)(sizeof(tifuns)/sizeof(tifuns[0])); i++) {
244 *tifuns[i].sp = addftn(tifuns[i].fun, tifuns[i].type);
245 switch (tifuns[i].apt) {
246 case 0: /* nothing */
247 break;
248 case 1: /* just tisp */
249 (*tifuns[i].sp)->sap = tisp->sap;
250 break;
251 case 2: /* mode string */
252 (*tifuns[i].sp)->sap = ap2;
253 }
254 }
255
256
257 }
258 #endif
259 }
260
261 #define TS "\n#pragma tls\n# %d\n"
262 #define TLLEN sizeof(TS)+10
263 /*
264 * See if a string matches a gcc keyword.
265 */
266 int
267 gcc_keyword(char *str)
268 {
269 extern int inattr, parlvl, parbal;
270 char tlbuf[TLLEN], *tw;
271 struct kw *kwp;
272 int i;
273
274 /* XXX hack, should pass everything in expressions */
275 if (str == kw[21].ptr)
276 return kw[21].rv;
277
278 if (inattr)
279 return 0;
280
281 for (i = 0, kwp = kw; kwp->name; kwp++, i++)
282 if (str == kwp->ptr)
283 break;
284 if (kwp->name == NULL)
285 return 0;
286 if (kwp->rv)
287 return kwp->rv;
288 switch (i) {
289 case 1: /* __signed */
290 case 14: /* __signed__ */
291 yylval.type = SIGNED;
292 return C_TYPE;
293 case 2: /* __inline */
294 case 5: /* __inline__ */
295 yylval.type = INLINE;
296 return C_FUNSPEC;
297 case 3: /* __const */
298 yylval.type = CON;
299 return C_QUALIFIER;
300 case 6: /* __thread */
301 snprintf(tlbuf, TLLEN, TS, lineno);
302 tw = &tlbuf[strlen(tlbuf)];
303 while (tw > tlbuf)
304 cunput(*--tw);
305 return -1;
306 case 7: /* __FUNCTION__ */
307 case 20: /* __PRETTY_FUNCTION__ */
308 if (cftnsp == NULL) {
309 uerror("%s outside function", kwp->name);
310 yylval.strp = "";
311 } else
312 yylval.strp = cftnsp->sname; /* XXX - not C99 */
313 return C_STRING;
314 case 8: /* __volatile */
315 case 9: /* __volatile__ */
316 yylval.type = VOL;
317 return C_QUALIFIER;
318 case 15: /* __attribute__ */
319 case 16: /* __attribute */
320 inattr = 1;
321 parlvl = parbal;
322 return C_ATTRIBUTE;
323 case 17: /* __real__ */
324 yylval.intval = XREAL;
325 return C_UNOP;
326 case 18: /* __imag__ */
327 yylval.intval = XIMAG;
328 return C_UNOP;
329 }
330 cerror("gcc_keyword");
331 return 0;
332 }
333
334 #ifndef TARGET_ATTR
335 #define TARGET_ATTR(p, sue) 0
336 #endif
337 #ifndef ALMAX
338 #define ALMAX (ALLDOUBLE > ALLONGLONG ? ALLDOUBLE : ALLONGLONG)
339 #endif
340
341 /* allowed number of args */
342 #define A_0ARG 0x01
343 #define A_1ARG 0x02
344 #define A_2ARG 0x04
345 #define A_3ARG 0x08
346 /* arg # is a name */
347 #define A1_NAME 0x10
348 #define A2_NAME 0x20
349 #define A3_NAME 0x40
350 #define A_MANY 0x80
351 /* arg # is "string" */
352 #define A1_STR 0x100
353 #define A2_STR 0x200
354 #define A3_STR 0x400
355
356 #ifdef __MSC__
357 #define CS(x)
358 #else
359 #define CS(x) [x] =
360 #endif
361
362 struct atax {
363 int typ;
364 char *name;
365 } atax[GCC_ATYP_MAX] = {
366 CS(ATTR_NONE) { 0, NULL },
367 CS(ATTR_COMPLEX) { 0, NULL },
368 CS(xxxATTR_BASETYP) { 0, NULL },
369 CS(ATTR_QUALTYP) { 0, NULL },
370 CS(ATTR_STRUCT) { 0, NULL },
371 CS(ATTR_ALIGNED) { A_0ARG|A_1ARG, "aligned" },
372 CS(ATTR_NORETURN) { A_0ARG, "noreturn" },
373 CS(ATTR_P1LABELS) { A_0ARG, "p1labels" },
374 CS(ATTR_SONAME) { A_1ARG|A1_STR, "soname" },
375 CS(GCC_ATYP_PACKED) { A_0ARG|A_1ARG, "packed" },
376 CS(GCC_ATYP_SECTION) { A_1ARG|A1_STR, "section" },
377 CS(GCC_ATYP_TRANSP_UNION) { A_0ARG, "transparent_union" },
378 CS(GCC_ATYP_UNUSED) { A_0ARG, "unused" },
379 CS(GCC_ATYP_DEPRECATED) { A_0ARG, "deprecated" },
380 CS(GCC_ATYP_MAYALIAS) { A_0ARG, "may_alias" },
381 CS(GCC_ATYP_MODE) { A_1ARG|A1_NAME, "mode" },
382 CS(GCC_ATYP_FORMAT) { A_3ARG|A1_NAME, "format" },
383 CS(GCC_ATYP_NONNULL) { A_MANY, "nonnull" },
384 CS(GCC_ATYP_SENTINEL) { A_0ARG|A_1ARG, "sentinel" },
385 CS(GCC_ATYP_WEAK) { A_0ARG, "weak" },
386 CS(GCC_ATYP_FORMATARG) { A_1ARG, "format_arg" },
387 CS(GCC_ATYP_GNU_INLINE) { A_0ARG, "gnu_inline" },
388 CS(GCC_ATYP_MALLOC) { A_0ARG, "malloc" },
389 CS(GCC_ATYP_NOTHROW) { A_0ARG, "nothrow" },
390 CS(GCC_ATYP_CONST) { A_0ARG, "const" },
391 CS(GCC_ATYP_PURE) { A_0ARG, "pure" },
392 CS(GCC_ATYP_CONSTRUCTOR) { A_0ARG, "constructor" },
393 CS(GCC_ATYP_DESTRUCTOR) { A_0ARG, "destructor" },
394 CS(GCC_ATYP_VISIBILITY) { A_1ARG|A1_STR, "visibility" },
395 CS(GCC_ATYP_STDCALL) { A_0ARG, "stdcall" },
396 CS(GCC_ATYP_CDECL) { A_0ARG, "cdecl" },
397 CS(GCC_ATYP_WARN_UNUSED_RESULT) { A_0ARG, "warn_unused_result" },
398 CS(GCC_ATYP_USED) { A_0ARG, "used" },
399 CS(GCC_ATYP_NO_INSTR_FUN) { A_0ARG, "no_instrument_function" },
400 CS(GCC_ATYP_NOINLINE) { A_0ARG, "noinline" },
401 CS(GCC_ATYP_ALIAS) { A_1ARG|A1_STR, "alias" },
402 CS(GCC_ATYP_WEAKREF) { A_0ARG|A_1ARG|A1_STR, "weakref" },
403 CS(GCC_ATYP_ALLOCSZ) { A_1ARG|A_2ARG, "alloc_size" },
404 CS(GCC_ATYP_ALW_INL) { A_0ARG, "always_inline" },
405 CS(GCC_ATYP_TLSMODEL) { A_1ARG|A1_STR, "tls_model" },
406 CS(GCC_ATYP_ALIASWEAK) { A_1ARG|A1_STR, "aliasweak" },
407 CS(GCC_ATYP_RETURNS_TWICE) { A_0ARG, "returns_twice" },
408 CS(GCC_ATYP_WARNING) { A_1ARG|A1_STR, "warning" },
409 CS(GCC_ATYP_NOCLONE) { A_0ARG, "noclone" },
410 CS(GCC_ATYP_REGPARM) { A_1ARG, "regparm" },
411 CS(GCC_ATYP_FASTCALL) { A_0ARG, "fastcall" },
412
413 CS(GCC_ATYP_BOUNDED) { A_3ARG|A_MANY|A1_NAME, "bounded" },
414
415 CS(GCC_ATYP_WEAKIMPORT) { A_0ARG, "weak_import" },
416 };
417
418 #if SZPOINT(CHAR) == SZLONGLONG
419 #define GPT LONGLONG
420 #else
421 #define GPT INT
422 #endif
423
424 struct atax mods[] = {
425 { 0, NULL },
426 { INT, "SI" },
427 { INT, "word" },
428 { GPT, "pointer" },
429 { CHAR, "byte" },
430 { CHAR, "QI" },
431 { SHORT, "HI" },
432 { LONGLONG, "DI" },
433 { FLOAT, "SF" },
434 { DOUBLE, "DF" },
435 { LDOUBLE, "XF" },
436 { FCOMPLEX, "SC" },
437 { COMPLEX, "DC" },
438 { LCOMPLEX, "XC" },
439 { INT, "libgcc_cmp_return" },
440 { INT, "libgcc_shift_count" },
441 { LONG, "unwind_word" },
442 #ifdef TARGET_TIMODE
443 { 800, "TI" },
444 { 864, "TF" },
445 { 928, "TC" },
446 #endif
447 #ifdef TARGET_MODS
448 TARGET_MODS
449 #endif
450 };
451 #define ATSZ (sizeof(mods)/sizeof(mods[0]))
452
453 static int
454 amatch(char *s, struct atax *at, int mx)
455 {
456 int i;
457 size_t len;
458
459 if (s[0] == '_' && s[1] == '_')
460 s += 2;
461 len = strlen(s);
462 if (len > 2 && s[len-1] == '_' && s[len-2] == '_')
463 len -= 2;
464 for (i = 0; i < mx; i++) {
465 char *t = at[i].name;
466 if (t != NULL && strncmp(s, t, len) == 0 && t[len] == 0)
467 return i;
468 }
469 return 0;
470 }
471
472 static void
473 setaarg(int str, union aarg *aa, NODE *p)
474 {
475 if (str) {
476 if (((str & (A1_STR|A2_STR|A3_STR)) && p->n_op != STRING) ||
477 ((str & (A1_NAME|A2_NAME|A3_NAME)) && p->n_op != NAME))
478 uerror("bad arg to attribute");
479 if (p->n_op == STRING) {
480 aa->sarg = p->n_name; /* saved in cgram.y */
481 } else
482 aa->sarg = (char *)p->n_sp;
483 nfree(p);
484 } else
485 aa->iarg = (int)icons(eve(p));
486 }
487
488 /*
489 * Parse attributes from an argument list.
490 */
491 static struct attr *
492 gcc_attribs(NODE *p)
493 {
494 NODE *q, *r;
495 struct attr *ap;
496 char *name = NULL, *c;
497 int cw, attr, narg;
498
499 if (p->n_op == NAME) {
500 name = (char *)p->n_sp;
501 } else if (p->n_op == CALL || p->n_op == UCALL) {
502 name = (char *)p->n_left->n_sp;
503 } else if (p->n_op == ICON && p->n_type == STRTY) {
504 return NULL;
505 } else
506 cerror("bad variable attribute");
507
508 if ((attr = amatch(name, atax, GCC_ATYP_MAX)) == 0) {
509 warner(Wattributes, name);
510 ap = NULL;
511 goto out;
512 }
513 narg = 0;
514 if (p->n_op == CALL)
515 for (narg = 1, q = p->n_right; q->n_op == CM; q = q->n_left)
516 narg++;
517
518 cw = atax[attr].typ;
519 if (!(cw & A_MANY) && ((narg > 3) || ((cw & (1 << narg)) == 0))) {
520 uerror("wrong attribute arg count");
521 return NULL;
522 }
523 ap = attr_new(attr, 3); /* XXX should be narg */
524 q = p->n_right;
525
526 switch (narg) {
527 default:
528 /* XXX */
529 while (narg-- > 3) {
530 r = q;
531 q = q->n_left;
532 tfree(r->n_right);
533 nfree(r);
534 }
535 /* FALLTHROUGH */
536 case 3:
537 setaarg(cw & (A3_NAME|A3_STR), &ap->aa[2], q->n_right);
538 r = q;
539 q = q->n_left;
540 nfree(r);
541 /* FALLTHROUGH */
542 case 2:
543 setaarg(cw & (A2_NAME|A2_STR), &ap->aa[1], q->n_right);
544 r = q;
545 q = q->n_left;
546 nfree(r);
547 /* FALLTHROUGH */
548 case 1:
549 setaarg(cw & (A1_NAME|A1_STR), &ap->aa[0], q);
550 p->n_op = UCALL;
551 /* FALLTHROUGH */
552 case 0:
553 break;
554 }
555
556 /* some attributes must be massaged special */
557 switch (attr) {
558 case ATTR_ALIGNED:
559 if (narg == 0)
560 ap->aa[0].iarg = ALMAX;
561 else
562 ap->aa[0].iarg *= SZCHAR;
563 break;
564 case GCC_ATYP_PACKED:
565 if (narg == 0)
566 ap->aa[0].iarg = 1; /* bitwise align */
567 else
568 ap->aa[0].iarg *= SZCHAR;
569 break;
570
571 case GCC_ATYP_VISIBILITY:
572 c = ap->aa[0].sarg;
573 if (strcmp(c, "default") && strcmp(c, "hidden") &&
574 strcmp(c, "internal") && strcmp(c, "protected"))
575 werror("unknown visibility %s", c);
576 break;
577
578 case GCC_ATYP_TLSMODEL:
579 c = ap->aa[0].sarg;
580 if (strcmp(c, "global-dynamic") && strcmp(c, "local-dynamic") &&
581 strcmp(c, "initial-exec") && strcmp(c, "local-exec"))
582 werror("unknown tls model %s", c);
583 break;
584
585 default:
586 break;
587 }
588 out:
589 return ap;
590 }
591
592 /*
593 * Extract attributes from a node tree and return attribute entries
594 * based on its contents.
595 */
596 struct attr *
597 gcc_attr_parse(NODE *p)
598 {
599 struct attr *b, *c;
600
601 if (p == NIL)
602 return NULL;
603
604 if (p->n_op != CM) {
605 b = gcc_attribs(p);
606 tfree(p);
607 } else {
608 b = gcc_attr_parse(p->n_left);
609 c = gcc_attr_parse(p->n_right);
610 nfree(p);
611 b = b ? attr_add(b, c) : c;
612 }
613 return b;
614 }
615
616 /*
617 * Fixup struct/unions depending on attributes.
618 */
619 void
620 gcc_tcattrfix(NODE *p)
621 {
622 struct symtab *sp;
623 struct attr *ap;
624 int sz, coff, csz, al, oal, mxal;
625
626 if (!ISSOU(p->n_type)) /* only for structs or unions */
627 return;
628 if ((ap = attr_find(p->n_ap, GCC_ATYP_PACKED)) == NULL)
629 return; /* nothing to fix */
630
631 al = ap->iarg(0);
632 mxal = 0;
633
634 /* Must repack struct */
635 coff = csz = 0;
636 for (sp = strmemb(ap); sp; sp = sp->snext) {
637 oal = talign(sp->stype, sp->sap);
638 if (oal > al)
639 oal = al;
640 if (mxal < oal)
641 mxal = oal;
642 if (sp->sclass & FIELD)
643 sz = sp->sclass&FLDSIZ;
644 else
645 sz = (int)tsize(sp->stype, sp->sdf, sp->sap);
646 sp->soffset = upoff(sz, oal, &coff);
647 if (coff > csz)
648 csz = coff;
649 if (p->n_type == UNIONTY)
650 coff = 0;
651 }
652 if (mxal < ALCHAR)
653 mxal = ALCHAR; /* for bitfields */
654 SETOFF(csz, mxal); /* Roundup to whatever */
655
656 ap = attr_find(p->n_ap, ATTR_STRUCT);
657 ap->amsize = csz;
658 ap = attr_find(p->n_ap, ATTR_ALIGNED);
659 ap->iarg(0) = mxal;
660
661 }
662
663 /*
664 * gcc-specific pragmas.
665 */
666 int
667 pragmas_gcc(char *t)
668 {
669 char u;
670 extern char *pragstore;
671
672 if (strcmp((t = pragtok(NULL)), "diagnostic") == 0) {
673 int warn, err;
674
675 if (strcmp((t = pragtok(NULL)), "ignored") == 0)
676 warn = 0, err = 0;
677 else if (strcmp(t, "warning") == 0)
678 warn = 1, err = 0;
679 else if (strcmp(t, "error") == 0)
680 warn = 1, err = 1;
681 else
682 return 1;
683
684 if (eat('\"') || eat('-'))
685 return 1;
686
687 for (t = pragstore; *t && *t != '\"'; t++)
688 ;
689
690 u = *t;
691 *t = 0;
692 Wset(pragstore + 1, warn, err);
693 *t = u;
694 } else if (strcmp(t, "poison") == 0) {
695 /* currently ignore */;
696 } else if (strcmp(t, "visibility") == 0) {
697 /* currently ignore */;
698 } else if (strcmp(t, "system_header") == 0) {
699 /* currently ignore */;
700 } else
701 werror("gcc pragma unsupported");
702 return 0;
703 }
704
705 /*
706 * Fixup types when modes given in defid().
707 */
708 void
709 gcc_modefix(NODE *p)
710 {
711 struct attr *ap;
712 #ifdef TARGET_TIMODE
713 struct attr *a2;
714 #endif
715 struct symtab *sp;
716 char *s;
717 int i, u;
718
719 if ((ap = attr_find(p->n_ap, GCC_ATYP_MODE)) == NULL)
720 return;
721
722 u = ISUNSIGNED(BTYPE(p->n_type));
723 if ((i = amatch(ap->aa[0].sarg, mods, ATSZ)) == 0) {
724 werror("unknown mode arg %s", ap->aa[0].sarg);
725 return;
726 }
727 i = mods[i].typ;
728 if (i >= 1 && i <= MAXTYPES) {
729 MODTYPE(p->n_type, ctype(i));
730 if (u)
731 p->n_type = ENUNSIGN(p->n_type);
732 } else switch (i) {
733 #ifdef TARGET_TIMODE
734 case 864:
735 case 928:
736 case 800:
737 if (BTYPE(p->n_type) == STRTY)
738 break;
739 MODTYPE(p->n_type, tisp->stype);
740 p->n_df = tisp->sdf;
741 p->n_ap = tisp->sap;
742 if (ap->iarg(1) == u)
743 break;
744 /* must add a new mode struct to avoid overwriting */
745 a2 = attr_new(GCC_ATYP_MODE, 3);
746 a2->sarg(0) = ap->sarg(0);
747 a2->iarg(1) = u;
748 p->n_ap = attr_add(p->n_ap, a2);
749 break;
750 #endif
751 case FCOMPLEX:
752 case COMPLEX:
753 case LCOMPLEX:
754 /* Destination should have been converted to a struct already */
755 if (BTYPE(p->n_type) != STRTY)
756 uerror("gcc_modefix: complex not STRTY");
757 i -= (FCOMPLEX-FLOAT);
758 ap = strattr(p->n_ap);
759 sp = ap->amlist;
760 if (sp->stype == (unsigned)i)
761 return; /* Already correct type */
762 /* we must change to another struct */
763 s = i == FLOAT ? "0f" :
764 i == DOUBLE ? "0d" :
765 i == LDOUBLE ? "0l" : 0;
766 sp = lookup(addname(s), 0);
767 for (ap = sp->sap; ap != NULL; ap = ap->next)
768 p->n_ap = attr_add(p->n_ap, attr_dup(ap));
769 break;
770
771 default:
772 cerror("gcc_modefix");
773 }
774 }
775
776 #ifdef TARGET_TIMODE
777
778 /*
779 * Return ap if this node is a TI node, else NULL.
780 */
781 struct attr *
782 isti(NODE *p)
783 {
784 struct attr *ap;
785
786 if (p->n_type != STRTY)
787 return NULL;
788 if ((ap = attr_find(p->n_ap, GCC_ATYP_MODE)) == NULL)
789 return NULL;
790 if (strcmp(ap->sarg(0), TISTR) && strcmp(ap->sarg(0), TFSTR) &&
791 strcmp(ap->sarg(0), TCSTR))
792 return NULL;
793 return ap;
794 }
795
796 static char *
797 tistack(void)
798 {
799 struct symtab *sp, *sp2;
800 char buf[12];
801 NODE *q;
802 char *n;
803
804 /* allocate space on stack */
805 snprintf(buf, 12, "%d", getlab());
806 n = addname(buf);
807 sp = lookup(n, 0);
808 sp2 = tisp;
809 q = block(TYPE, NIL, NIL, sp2->stype, sp2->sdf, sp2->sap);
810 q->n_sp = sp;
811 nidcl2(q, AUTO, 0);
812 nfree(q);
813 return n;
814 }
815
816 #define biop(x,y,z) block(x, y, z, INT, 0, 0)
817 /*
818 * Create a ti node from something not a ti node.
819 * This usually means: allocate space on stack, store val, give stack address.
820 */
821 static NODE *
822 ticast(NODE *p, int u)
823 {
824 CONSZ val;
825 NODE *q;
826 char *n;
827 int u2;
828
829 n = tistack();
830
831 /* store val */
832 switch (p->n_op) {
833 case ICON:
834 val = 0;
835 if (u == 0 && glval(p) < 0)
836 val = -1;
837 q = eve(biop(DOT, bdty(NAME, n), bdty(NAME, loti)));
838 q = buildtree(ASSIGN, q, p);
839 p = biop(DOT, bdty(NAME, n), bdty(NAME, hiti));
840 p = eve(biop(ASSIGN, p, bcon(val)));
841 q = buildtree(COMOP, q, p);
842 p = buildtree(COMOP, q, eve(bdty(NAME, n)));
843 break;
844
845 default:
846 u2 = ISUNSIGNED(p->n_type);
847 q = eve(biop(DOT, bdty(NAME, n), bdty(NAME, loti)));
848 q = buildtree(ASSIGN, q, p);
849 p = biop(DOT, bdty(NAME, n), bdty(NAME, hiti));
850 if (u2) {
851 p = eve(biop(ASSIGN, p, bcon(0)));
852 } else {
853 q = buildtree(ASSIGN, eve(p1tcopy(p)), q);
854 p = buildtree(RSEQ, eve(p), bcon(SZLONG-1));
855 }
856 q = buildtree(COMOP, q, p);
857 p = buildtree(COMOP, q, eve(bdty(NAME, n)));
858 break;
859 }
860 return p;
861 }
862
863 /*
864 * Check if we may have to do a cast to/from TI.
865 */
866 NODE *
867 gcc_eval_ticast(int op, NODE *p1, NODE *p2)
868 {
869 struct attr *a1, *a2;
870 int t;
871
872 a2 = NULL; /* XXX flow analysis */
873 if ((a1 = isti(p1)) == NULL && (a2 = isti(p2)) == NULL)
874 return NIL;
875
876 if (op == RETURN)
877 p1 = p1tcopy(p1);
878 if (a1 == NULL) {
879 if (a2 == NULL)
880 cerror("gcc_eval_ticast error");
881 switch (p1->n_type) {
882 case LDOUBLE:
883 p2 = doacall(floatuntixfsp,
884 nametree(floatuntixfsp), p2);
885 tfree(p1);
886 break;
887 case ULONG:
888 case LONG:
889 p2 = cast(structref(p2, DOT, loti), p1->n_type, 0);
890 tfree(p1);
891 break;
892 case VOID:
893 return NIL;
894 default:
895 uerror("gcc_eval_ticast: %d", p1->n_type);
896 }
897 return p2;
898 }
899 /* p2 can be anything, but we must cast it to p1 */
900 t = a1->iarg(1);
901
902 if (p2->n_type == STRTY &&
903 (a2 = attr_find(p2->n_ap, GCC_ATYP_MODE)) &&
904 strcmp(a2->sarg(0), TISTR) == 0) {
905 /* Already TI, just add extra mode bits */
906 a2 = attr_new(GCC_ATYP_MODE, 3);
907 a2->sarg(0) = TISTR;
908 a2->iarg(1) = t;
909 p2->n_ap = attr_add(p2->n_ap, a2);
910 } else {
911 p2 = ticast(p2, t);
912 }
913 tfree(p1);
914 return p2;
915 }
916
917 /*
918 * Apply a unary op on a TI value.
919 */
920 NODE *
921 gcc_eval_tiuni(int op, NODE *p1)
922 {
923 struct attr *a1;
924 NODE *p;
925
926 if ((a1 = isti(p1)) == NULL)
927 return NULL;
928
929 switch (op) {
930 case UMINUS:
931 p = ticast(bcon(0), 0);
932 p = buildtree(CM, p, p1);
933 p = doacall(subvti3sp, nametree(subvti3sp), p);
934 break;
935
936 case UMUL:
937 p = NULL;
938 default:
939 uerror("unsupported unary TI mode op %d", op);
940 p = NULL;
941 }
942 return p;
943 }
944
945 /*
946 * Evaluate AND/OR/ER. p1 and p2 are pointers to ti struct.
947 */
948 static NODE *
949 gcc_andorer(int op, NODE *p1, NODE *p2)
950 {
951 char *n = tistack();
952 NODE *p, *t1, *t2, *p3;
953
954 t1 = tempnode(0, p1->n_type, p1->n_df, p1->n_ap);
955 t2 = tempnode(0, p2->n_type, p2->n_df, p2->n_ap);
956
957 p1 = buildtree(ASSIGN, p1tcopy(t1), p1);
958 p2 = buildtree(ASSIGN, p1tcopy(t2), p2);
959 p = buildtree(COMOP, p1, p2);
960
961 p3 = buildtree(ADDROF, eve(bdty(NAME, n)), NIL);
962 p1 = buildtree(ASSIGN, structref(p1tcopy(p3), STREF, hiti),
963 buildtree(op, structref(p1tcopy(t1), STREF, hiti),
964 structref(p1tcopy(t2), STREF, hiti)));
965 p = buildtree(COMOP, p, p1);
966 p1 = buildtree(ASSIGN, structref(p1tcopy(p3), STREF, loti),
967 buildtree(op, structref(t1, STREF, loti),
968 structref(t2, STREF, loti)));
969 p = buildtree(COMOP, p, p1);
970 p = buildtree(COMOP, p, buildtree(UMUL, p3, NIL));
971 return p;
972 }
973
974 /*
975 * Ensure that a 128-bit assign succeeds.
976 * If left is not TI, make right not TI,
977 * else if left _is_ TI, make right TI,
978 * else do nothing.
979 */
980 static NODE *
981 timodeassign(NODE *p1, NODE *p2)
982 {
983 struct attr *a1, *a2;
984
985 a1 = isti(p1);
986 a2 = isti(p2);
987 if (a1 && a2 == NULL) {
988 p2 = ticast(p2, a1->iarg(1));
989 } else if (a1 == NULL && a2) {
990 if (ISFTY(p1->n_type))
991 cerror("cannot TI float convert");
992 p2 = structref(p2, DOT, loti);
993 }
994 return buildtree(ASSIGN, p1, p2);
995 }
996
997 /*
998 * Evaluate 128-bit operands.
999 */
1000 NODE *
1001 gcc_eval_timode(int op, NODE *p1, NODE *p2)
1002 {
1003 struct attr *a1, *a2;
1004 struct symtab *sp;
1005 NODE *p;
1006 int isu = 0, gotti, isaop;
1007
1008 if (op == CM)
1009 return buildtree(op, p1, p2);
1010
1011 a1 = isti(p1);
1012 a2 = isti(p2);
1013
1014 if (a1 == NULL && a2 == NULL)
1015 return NULL;
1016
1017 if (op == ASSIGN)
1018 return timodeassign(p1, p2);
1019
1020 gotti = (a1 != NULL);
1021 gotti += (a2 != NULL);
1022
1023 if (gotti == 0)
1024 return NULL;
1025
1026 if (a1 != NULL)
1027 isu = a1->iarg(1);
1028 if (a2 != NULL && !isu)
1029 isu = a2->iarg(1);
1030
1031 if (a1 == NULL) {
1032 p1 = ticast(p1, isu);
1033 a1 = attr_find(p1->n_ap, GCC_ATYP_MODE);
1034 }
1035 if (a2 == NULL && (cdope(op) & SHFFLG) == 0) {
1036 p2 = ticast(p2, isu);
1037 a2 = attr_find(p2->n_ap, GCC_ATYP_MODE);
1038 }
1039
1040 switch (op) {
1041 case GT:
1042 case GE:
1043 case LT:
1044 case LE:
1045 case EQ:
1046 case NE:
1047 /* change to call */
1048 sp = isu ? ucmpti2sp : cmpti2sp;
1049 p = doacall(sp, nametree(sp), buildtree(CM, p1, p2));
1050 p = buildtree(op, p, bcon(1));
1051 break;
1052
1053 case AND:
1054 case ER:
1055 case OR:
1056 if (!ISPTR(p1->n_type))
1057 p1 = buildtree(ADDROF, p1, NIL);
1058 if (!ISPTR(p2->n_type))
1059 p2 = buildtree(ADDROF, p2, NIL);
1060 p = gcc_andorer(op, p1, p2);
1061 break;
1062
1063 case LSEQ:
1064 case RSEQ:
1065 case LS:
1066 case RS:
1067 sp = op == LS || op == LSEQ ? ashlti3sp :
1068 isu ? lshrti3sp : ashrti3sp;
1069 p2 = cast(p2, INT, 0);
1070 /* XXX p1 p1tcopy may have side effects */
1071 p = doacall(sp, nametree(sp), buildtree(CM, p1tcopy(p1), p2));
1072 if (op == LSEQ || op == RSEQ) {
1073 p = buildtree(ASSIGN, p1, p);
1074 } else
1075 tfree(p1);
1076 break;
1077
1078 case PLUSEQ:
1079 case MINUSEQ:
1080 case MULEQ:
1081 case DIVEQ:
1082 case MODEQ:
1083 case PLUS:
1084 case MINUS:
1085 case MUL:
1086 case DIV:
1087 case MOD:
1088 isaop = (cdope(op)&ASGOPFLG);
1089 if (isaop)
1090 op = UNASG op;
1091 sp = op == PLUS ? addvti3sp :
1092 op == MINUS ? subvti3sp :
1093 op == MUL ? mulvti3sp :
1094 op == DIV ? (isu ? udivti3sp : divti3sp) :
1095 op == MOD ? (isu ? umodti3sp : modti3sp) : 0;
1096 /* XXX p1 p1tcopy may have side effects */
1097 p = doacall(sp, nametree(sp), buildtree(CM, p1tcopy(p1), p2));
1098 if (isaop)
1099 p = buildtree(ASSIGN, p1, p);
1100 else
1101 tfree(p1);
1102 break;
1103
1104 default:
1105 uerror("unsupported TImode op %d", op);
1106 p = bcon(0);
1107 }
1108 return p;
1109 }
1110 #endif
1111
1112 #ifdef PCC_DEBUG
1113 void
1114 dump_attr(struct attr *ap)
1115 {
1116 printf("attributes; ");
1117 for (; ap; ap = ap->next) {
1118 if (ap->atype >= GCC_ATYP_MAX) {
1119 printf("bad type %d, ", ap->atype);
1120 } else if (atax[ap->atype].name == 0) {
1121 char *c = ap->atype == ATTR_COMPLEX ? "complex" :
1122 ap->atype == ATTR_STRUCT ? "struct" : "badtype";
1123 printf("%s, ", c);
1124 } else {
1125 printf("%s: ", atax[ap->atype].name);
1126 if (atax[ap->atype].typ & A1_STR)
1127 printf("%s ", ap->sarg(0));
1128 else
1129 printf("%d %d %d, ", ap->iarg(0),
1130 ap->iarg(1), ap->iarg(2));
1131 }
1132 }
1133 printf("\n");
1134 }
1135 #endif
1136 #endif