Debian Codegraph @

cc/cxxcom/gcc_compat.c

raw symbols SCIP
1 /* $Id: gcc_compat.c,v 1.11 2016/10/11 13:48:24 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 static struct kw {
40 char *name, *ptr;
41 int rv;
42 } kw[] = {
43 /*
44 * Do NOT change the order of these entries unless you know
45 * what you're doing!
46 */
47 /* 0 */ { "__asm", NULL, C_ASM },
48 /* 1 */ { "__signed", NULL, 0 },
49 /* 2 */ { "__inline", NULL, C_FUNSPEC },
50 /* 3 */ { "__const", NULL, 0 },
51 /* 4 */ { "__asm__", NULL, C_ASM },
52 /* 5 */ { "__inline__", NULL, C_FUNSPEC },
53 /* 6 */ { "__thread", NULL, 0 },
54 /* 7 */ { "__FUNCTION__", NULL, 0 },
55 /* 8 */ { "__volatile", NULL, 0 },
56 /* 9 */ { "__volatile__", NULL, 0 },
57 /* 10 */{ "__restrict", NULL, -1 },
58 /* 11 */{ "__typeof__", NULL, C_TYPEOF },
59 /* 12 */{ "typeof", NULL, C_TYPEOF },
60 /* 13 */{ "__extension__", NULL, -1 },
61 /* 14 */{ "__signed__", NULL, 0 },
62 /* 15 */{ "__attribute__", NULL, 0 },
63 /* 16 */{ "__attribute", NULL, 0 },
64 /* 17 */{ "__real__", NULL, 0 },
65 /* 18 */{ "__imag__", NULL, 0 },
66 /* 19 */{ "__builtin_offsetof", NULL, PCC_OFFSETOF },
67 /* 20 */{ "__PRETTY_FUNCTION__", NULL, 0 },
68 /* 21 */{ "__alignof__", NULL, C_ALIGNOF },
69 /* 22 */{ "__typeof", NULL, C_TYPEOF },
70 /* 23 */{ "__alignof", NULL, C_ALIGNOF },
71 /* 24 */{ "__restrict__", NULL, -1 },
72 { NULL, NULL, 0 },
73 };
74
75 /* g77 stuff */
76 #if SZFLOAT == SZLONG
77 #define G77_INTEGER LONG
78 #define G77_UINTEGER ULONG
79 #elif SZFLOAT == SZINT
80 #define G77_INTEGER INT
81 #define G77_UINTEGER UNSIGNED
82 #else
83 #error fix g77 stuff
84 #endif
85 #if SZFLOAT*2 == SZLONG
86 #define G77_LONGINT LONG
87 #define G77_ULONGINT ULONG
88 #elif SZFLOAT*2 == SZLONGLONG
89 #define G77_LONGINT LONGLONG
90 #define G77_ULONGINT ULONGLONG
91 #else
92 #error fix g77 long stuff
93 #endif
94
95 static TWORD g77t[] = { G77_INTEGER, G77_UINTEGER, G77_LONGINT, G77_ULONGINT };
96 static char *g77n[] = { "__g77_integer", "__g77_uinteger",
97 "__g77_longint", "__g77_ulongint" };
98
99 void
100 gcc_init(void)
101 {
102 struct kw *kwp;
103 NODE *p;
104 TWORD t;
105 int i;
106
107 for (kwp = kw; kwp->name; kwp++)
108 kwp->ptr = addname(kwp->name);
109
110 for (i = 0; i < 4; i++) {
111 struct symtab *sp;
112 t = ctype(g77t[i]);
113 p = block(NAME, NIL, NIL, t, NULL, 0);
114 sp = lookup(addname(g77n[i]), 0);
115 p->n_sp = sp;
116 defid(p, TYPEDEF);
117 nfree(p);
118 }
119 }
120
121 #define TS "\n#pragma tls\n# %d\n"
122 #define TLLEN sizeof(TS)+10
123 /*
124 * See if a string matches a gcc keyword.
125 */
126 int
127 gcc_keyword(char *str, NODE **n)
128 {
129 extern int inattr, parlvl, parbal;
130 YYSTYPE *yyl = (YYSTYPE *)n; /* XXX should pass yylval */
131 char tlbuf[TLLEN], *tw;
132 struct kw *kwp;
133 int i;
134
135 /* XXX hack, should pass everything in expressions */
136 if (str == kw[21].ptr)
137 return kw[21].rv;
138
139 if (inattr)
140 return 0;
141
142 for (i = 0, kwp = kw; kwp->name; kwp++, i++)
143 if (str == kwp->ptr)
144 break;
145 if (kwp->name == NULL)
146 return 0;
147 if (kwp->rv)
148 return kwp->rv;
149 switch (i) {
150 case 1: /* __signed */
151 case 14: /* __signed__ */
152 *n = mkty((TWORD)SIGNED, 0, 0);
153 return C_TYPE;
154 case 3: /* __const */
155 *n = block(QUALIFIER, NIL, NIL, CON, 0, 0);
156 (*n)->n_qual = CON;
157 return C_QUALIFIER;
158 case 6: /* __thread */
159 snprintf(tlbuf, TLLEN, TS, lineno);
160 tw = &tlbuf[strlen(tlbuf)];
161 while (tw > tlbuf)
162 cunput(*--tw);
163 return -1;
164 case 7: /* __FUNCTION__ */
165 case 20: /* __PRETTY_FUNCTION__ */
166 if (cftnsp == NULL) {
167 uerror("%s outside function", kwp->name);
168 yylval.strp = "";
169 } else
170 yylval.strp = cftnsp->sname; /* XXX - not C99 */
171 return C_STRING;
172 case 8: /* __volatile */
173 case 9: /* __volatile__ */
174 *n = block(QUALIFIER, NIL, NIL, VOL, 0, 0);
175 (*n)->n_qual = VOL;
176 return C_QUALIFIER;
177 case 15: /* __attribute__ */
178 case 16: /* __attribute */
179 inattr = 1;
180 parlvl = parbal;
181 return C_ATTRIBUTE;
182 case 17: /* __real__ */
183 yyl->intval = XREAL;
184 return C_UNOP;
185 case 18: /* __imag__ */
186 yyl->intval = XIMAG;
187 return C_UNOP;
188 }
189 cerror("gcc_keyword");
190 return 0;
191 }
192
193 #ifndef TARGET_ATTR
194 #define TARGET_ATTR(p, sue) 0
195 #endif
196 #ifndef ALMAX
197 #define ALMAX (ALLDOUBLE > ALLONGLONG ? ALLDOUBLE : ALLONGLONG)
198 #endif
199
200 /* allowed number of args */
201 #define A_0ARG 0x01
202 #define A_1ARG 0x02
203 #define A_2ARG 0x04
204 #define A_3ARG 0x08
205 /* arg # is a name */
206 #define A1_NAME 0x10
207 #define A2_NAME 0x20
208 #define A3_NAME 0x40
209 #define A_MANY 0x80
210 /* arg # is "string" */
211 #define A1_STR 0x100
212 #define A2_STR 0x200
213 #define A3_STR 0x400
214
215 #ifdef __MSC__
216 #define CS(x)
217 #else
218 #define CS(x) [x] =
219 #endif
220
221 struct atax {
222 int typ;
223 char *name;
224 } atax[GCC_ATYP_MAX] = {
225 CS(ATTR_NONE) { 0, NULL },
226 CS(ATTR_COMPLEX) { 0, NULL },
227 CS(xxxATTR_BASETYP) { 0, NULL },
228 CS(ATTR_QUALTYP) { 0, NULL },
229 CS(ATTR_STRUCT) { 0, NULL },
230 CS(ATTR_ALIGNED) { A_0ARG|A_1ARG, "aligned" },
231 CS(GCC_ATYP_PACKED) { A_0ARG|A_1ARG, "packed" },
232 CS(GCC_ATYP_SECTION) { A_1ARG|A1_STR, "section" },
233 CS(GCC_ATYP_TRANSP_UNION) { A_0ARG, "transparent_union" },
234 CS(GCC_ATYP_UNUSED) { A_0ARG, "unused" },
235 CS(GCC_ATYP_DEPRECATED) { A_0ARG, "deprecated" },
236 CS(GCC_ATYP_MAYALIAS) { A_0ARG, "may_alias" },
237 CS(GCC_ATYP_MODE) { A_1ARG|A1_NAME, "mode" },
238 CS(GCC_ATYP_NORETURN) { A_0ARG, "noreturn" },
239 CS(GCC_ATYP_FORMAT) { A_3ARG|A1_NAME, "format" },
240 CS(GCC_ATYP_NONNULL) { A_MANY, "nonnull" },
241 CS(GCC_ATYP_SENTINEL) { A_0ARG|A_1ARG, "sentinel" },
242 CS(GCC_ATYP_WEAK) { A_0ARG, "weak" },
243 CS(GCC_ATYP_FORMATARG) { A_1ARG, "format_arg" },
244 CS(GCC_ATYP_GNU_INLINE) { A_0ARG, "gnu_inline" },
245 CS(GCC_ATYP_MALLOC) { A_0ARG, "malloc" },
246 CS(GCC_ATYP_NOTHROW) { A_0ARG, "nothrow" },
247 CS(GCC_ATYP_CONST) { A_0ARG, "const" },
248 CS(GCC_ATYP_PURE) { A_0ARG, "pure" },
249 CS(GCC_ATYP_CONSTRUCTOR) { A_0ARG, "constructor" },
250 CS(GCC_ATYP_DESTRUCTOR) { A_0ARG, "destructor" },
251 CS(GCC_ATYP_VISIBILITY) { A_1ARG|A1_STR, "visibility" },
252 CS(GCC_ATYP_STDCALL) { A_0ARG, "stdcall" },
253 CS(GCC_ATYP_CDECL) { A_0ARG, "cdecl" },
254 CS(GCC_ATYP_WARN_UNUSED_RESULT) { A_0ARG, "warn_unused_result" },
255 CS(GCC_ATYP_USED) { A_0ARG, "used" },
256 CS(GCC_ATYP_NO_INSTR_FUN) { A_0ARG, "no_instrument_function" },
257 CS(GCC_ATYP_NOINLINE) { A_0ARG, "noinline" },
258 CS(GCC_ATYP_ALIAS) { A_1ARG|A1_STR, "alias" },
259 CS(GCC_ATYP_WEAKREF) { A_0ARG|A_1ARG|A1_STR, "weakref" },
260 CS(GCC_ATYP_ALLOCSZ) { A_1ARG|A_2ARG, "alloc_size" },
261 CS(GCC_ATYP_ALW_INL) { A_0ARG, "always_inline" },
262 CS(GCC_ATYP_TLSMODEL) { A_1ARG|A1_STR, "tls_model" },
263 CS(GCC_ATYP_ALIASWEAK) { A_1ARG|A1_STR, "aliasweak" },
264 CS(GCC_ATYP_REGPARM) { A_1ARG, "regparm" },
265 CS(GCC_ATYP_FASTCALL) { A_0ARG, "fastcall" },
266
267 CS(GCC_ATYP_BOUNDED) { A_3ARG|A_MANY|A1_NAME, "bounded" },
268 };
269
270 #if SZPOINT(CHAR) == SZLONGLONG
271 #define GPT LONGLONG
272 #else
273 #define GPT INT
274 #endif
275
276 struct atax mods[] = {
277 { 0, NULL },
278 { INT, "SI" },
279 { INT, "word" },
280 { GPT, "pointer" },
281 { CHAR, "byte" },
282 { CHAR, "QI" },
283 { SHORT, "HI" },
284 { LONGLONG, "DI" },
285 { FLOAT, "SF" },
286 { DOUBLE, "DF" },
287 { LDOUBLE, "XF" },
288 { FCOMPLEX, "SC" },
289 { COMPLEX, "DC" },
290 { LCOMPLEX, "XC" },
291 #ifdef TARGET_MODS
292 TARGET_MODS
293 #endif
294 };
295 #define ATSZ (sizeof(mods)/sizeof(mods[0]))
296
297 static int
298 amatch(char *s, struct atax *at, int mx)
299 {
300 int i, len;
301
302 if (s[0] == '_' && s[1] == '_')
303 s += 2;
304 len = (int)strlen(s);
305 if (len > 2 && s[len-1] == '_' && s[len-2] == '_')
306 len -= 2;
307 for (i = 0; i < mx; i++) {
308 char *t = at[i].name;
309 if (t != NULL && strncmp(s, t, len) == 0 && t[len] == 0)
310 return i;
311 }
312 return 0;
313 }
314
315 static void
316 setaarg(int str, union aarg *aa, NODE *p)
317 {
318 if (str) {
319 if (((str & (A1_STR|A2_STR|A3_STR)) && p->n_op != STRING) ||
320 ((str & (A1_NAME|A2_NAME|A3_NAME)) && p->n_op != NAME))
321 uerror("bad arg to attribute");
322 if (p->n_op == STRING) {
323 aa->sarg = newstring(p->n_name, strlen(p->n_name));
324 } else
325 aa->sarg = (char *)p->n_sp;
326 nfree(p);
327 } else
328 aa->iarg = (int)icons(eve(p));
329 }
330
331 /*
332 * Parse attributes from an argument list.
333 */
334 static struct attr *
335 gcc_attribs(NODE *p)
336 {
337 NODE *q, *r;
338 struct attr *ap;
339 char *name = NULL, *c;
340 int cw, attr, narg, i;
341
342 if (p->n_op == NAME) {
343 name = (char *)p->n_sp;
344 } else if (p->n_op == CALL || p->n_op == UCALL) {
345 name = (char *)p->n_left->n_sp;
346 } else if (p->n_op == ICON && p->n_type == STRTY) {
347 return NULL;
348 } else
349 cerror("bad variable attribute");
350
351 if ((attr = amatch(name, atax, GCC_ATYP_MAX)) == 0) {
352 warner(Wattributes, name);
353 ap = NULL;
354 goto out;
355 }
356 narg = 0;
357 if (p->n_op == CALL)
358 for (narg = 1, q = p->n_right; q->n_op == CM; q = q->n_left)
359 narg++;
360
361 cw = atax[attr].typ;
362 if (!(cw & A_MANY) && ((narg > 3) || ((cw & (1 << narg)) == 0))) {
363 uerror("wrong attribute arg count");
364 return NULL;
365 }
366 ap = attr_new(attr, 3); /* XXX should be narg */
367 q = p->n_right;
368
369 switch (narg) {
370 default:
371 /* XXX */
372 while (narg-- > 3) {
373 r = q;
374 q = q->n_left;
375 tfree(r->n_right);
376 nfree(r);
377 }
378 /* FALLTHROUGH */
379 case 3:
380 setaarg(cw & (A3_NAME|A3_STR), &ap->aa[2], q->n_right);
381 r = q;
382 q = q->n_left;
383 nfree(r);
384 /* FALLTHROUGH */
385 case 2:
386 setaarg(cw & (A2_NAME|A2_STR), &ap->aa[1], q->n_right);
387 r = q;
388 q = q->n_left;
389 nfree(r);
390 /* FALLTHROUGH */
391 case 1:
392 setaarg(cw & (A1_NAME|A1_STR), &ap->aa[0], q);
393 p->n_op = UCALL;
394 /* FALLTHROUGH */
395 case 0:
396 break;
397 }
398
399 /* some attributes must be massaged special */
400 switch (attr) {
401 case ATTR_ALIGNED:
402 if (narg == 0)
403 ap->aa[0].iarg = ALMAX;
404 else
405 ap->aa[0].iarg *= SZCHAR;
406 break;
407 case GCC_ATYP_PACKED:
408 if (narg == 0)
409 ap->aa[0].iarg = 1; /* bitwise align */
410 else
411 ap->aa[0].iarg *= SZCHAR;
412 break;
413
414 case GCC_ATYP_MODE:
415 if ((i = amatch(ap->aa[0].sarg, mods, ATSZ)) == 0)
416 werror("unknown mode arg %s", ap->aa[0].sarg);
417 ap->aa[0].iarg = ctype(mods[i].typ);
418 break;
419
420 case GCC_ATYP_VISIBILITY:
421 c = ap->aa[0].sarg;
422 if (strcmp(c, "default") && strcmp(c, "hidden") &&
423 strcmp(c, "internal") && strcmp(c, "protected"))
424 werror("unknown visibility %s", c);
425 break;
426
427 case GCC_ATYP_TLSMODEL:
428 c = ap->aa[0].sarg;
429 if (strcmp(c, "global-dynamic") && strcmp(c, "local-dynamic") &&
430 strcmp(c, "initial-exec") && strcmp(c, "local-exec"))
431 werror("unknown tls model %s", c);
432 break;
433
434 default:
435 break;
436 }
437 out:
438 return ap;
439 }
440
441 /*
442 * Extract attributes from a node tree and return attribute entries
443 * based on its contents.
444 */
445 struct attr *
446 gcc_attr_parse(NODE *p)
447 {
448 struct attr *b, *c;
449
450 if (p == NIL)
451 return NULL;
452
453 if (p->n_op != CM) {
454 b = gcc_attribs(p);
455 tfree(p);
456 } else {
457 b = gcc_attr_parse(p->n_left);
458 c = gcc_attr_parse(p->n_right);
459 nfree(p);
460 b = b ? attr_add(b, c) : c;
461 }
462 return b;
463 }
464
465 /*
466 * Fixup struct/unions depending on attributes.
467 */
468 void
469 gcc_tcattrfix(NODE *p)
470 {
471 struct symtab *sp;
472 struct attr *ap;
473 int sz, coff, csz, al, oal, mxal;
474
475 if ((ap = attr_find(p->n_ap, GCC_ATYP_PACKED)) == NULL)
476 return; /* nothing to fix */
477
478 al = ap->iarg(0);
479 mxal = 0;
480
481 /* Must repack struct */
482 coff = csz = 0;
483 for (sp = strmemb(ap); sp; sp = sp->snext) {
484 oal = talign(sp->stype, sp->sap);
485 if (oal > al)
486 oal = al;
487 if (mxal < oal)
488 mxal = oal;
489 if (sp->sclass & FIELD)
490 sz = sp->sclass&FLDSIZ;
491 else
492 sz = (int)tsize(sp->stype, sp->sdf, sp->sap);
493 sp->soffset = upoff(sz, oal, &coff);
494 if (coff > csz)
495 csz = coff;
496 if (p->n_type == UNIONTY)
497 coff = 0;
498 }
499 if (mxal < ALCHAR)
500 mxal = ALCHAR; /* for bitfields */
501 SETOFF(csz, mxal); /* Roundup to whatever */
502
503 ap = attr_find(p->n_ap, ATTR_STRUCT);
504 ap->amsize = csz;
505 ap = attr_find(p->n_ap, ATTR_ALIGNED);
506 ap->iarg(0) = mxal;
507
508 }
509
510 /*
511 * gcc-specific pragmas.
512 */
513 int
514 pragmas_gcc(char *t)
515 {
516 char u;
517 extern char *pragstore;
518
519 if (strcmp((t = pragtok(NULL)), "diagnostic") == 0) {
520 int warn, err;
521
522 if (strcmp((t = pragtok(NULL)), "ignored") == 0)
523 warn = 0, err = 0;
524 else if (strcmp(t, "warning") == 0)
525 warn = 1, err = 0;
526 else if (strcmp(t, "error") == 0)
527 warn = 1, err = 1;
528 else
529 return 1;
530
531 if (eat('\"') || eat('-'))
532 return 1;
533
534 for (t = pragstore; *t && *t != '\"'; t++)
535 ;
536
537 u = *t;
538 *t = 0;
539 Wset(pragstore + 1, warn, err);
540 *t = u;
541 } else if (strcmp(t, "poison") == 0) {
542 /* currently ignore */;
543 } else if (strcmp(t, "visibility") == 0) {
544 /* currently ignore */;
545 } else if (strcmp(t, "system_header") == 0) {
546 /* currently ignore */;
547 } else
548 werror("gcc pragma unsupported");
549 return 0;
550 }
551
552 #ifdef PCC_DEBUG
553 void
554 dump_attr(struct attr *ap)
555 {
556 printf("attributes; ");
557 for (; ap; ap = ap->next) {
558 if (ap->atype >= GCC_ATYP_MAX) {
559 printf("bad type %d, ", ap->atype);
560 } else if (atax[ap->atype].name == 0) {
561 char *c = ap->atype == ATTR_COMPLEX ? "complex" :
562 ap->atype == ATTR_STRUCT ? "struct" : "badtype";
563 printf("%s, ", c);
564 } else {
565 printf("%s: ", atax[ap->atype].name);
566 printf("%d %d %d, ", ap->iarg(0),
567 ap->iarg(1), ap->iarg(2));
568 }
569 }
570 printf("\n");
571 }
572 #endif
573 #endif