]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - cddl/contrib/opensolaris/tools/ctf/cvt/st_parse.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / cddl / contrib / opensolaris / tools / ctf / cvt / st_parse.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 /*
26  * This file is a sewer.
27  */
28
29 #include <limits.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <strings.h>
34 #include <setjmp.h>
35 #include <ctype.h>
36 #include <uts/common/sys/ctf.h>
37
38 #include "ctftools.h"
39 #include "memory.h"
40 #include "list.h"
41
42 #define HASH(NUM)       ((int)(NUM & (BUCKETS - 1)))
43 #define BUCKETS         128
44
45 #define TYPEPAIRMULT    10000
46 #define MAKETYPEID(file, num)   ((file) * TYPEPAIRMULT + num)
47 #define TYPEFILE(tid)           ((tid) / TYPEPAIRMULT)
48 #define TYPENUM(tid)            ((tid) % TYPEPAIRMULT)
49
50 #define expected(a, b, c) _expected(a, b, c, __LINE__)
51
52 static int faketypenumber = 100000000;
53
54 static tdesc_t *hash_table[BUCKETS];
55 static tdesc_t *name_table[BUCKETS];
56
57 list_t *typedbitfldmems;
58
59 static void reset(void);
60 static jmp_buf  resetbuf;
61
62 static char *soudef(char *cp, stabtype_t type, tdesc_t **rtdp);
63 static void enumdef(char *cp, tdesc_t **rtdp);
64 static int compute_sum(const char *w);
65
66 static char *number(char *cp, int *n);
67 static char *name(char *cp, char **w);
68 static char *id(char *cp, int *h);
69 static char *whitesp(char *cp);
70 static void addhash(tdesc_t *tdp, int num);
71 static int tagadd(char *w, int h, tdesc_t *tdp);
72 static char *tdefdecl(char *cp, int h, tdesc_t **rtdp);
73 static char *intrinsic(char *cp, tdesc_t **rtdp);
74 static char *arraydef(char *cp, tdesc_t **rtdp);
75
76 int debug_parse = DEBUG_PARSE;
77
78 /*PRINTFLIKE3*/
79 static void
80 parse_debug(int level, char *cp, const char *fmt, ...)
81 {
82         va_list ap;
83         char buf[1024];
84         char tmp[32];
85         int i;
86
87         if (level > debug_level || !debug_parse)
88                 return;
89
90         if (cp != NULL) {
91                 for (i = 0; i < 30; i++) {
92                         if (cp[i] == '\0')
93                                 break;
94                         if (!iscntrl(cp[i]))
95                                 tmp[i] = cp[i];
96                 }
97                 tmp[i] = '\0';
98                 (void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
99         } else {
100                 strcpy(buf, fmt);
101                 strcat(buf, "\n");
102         }
103
104         va_start(ap, fmt);
105         vadebug(level, buf, ap);
106         va_end(ap);
107 }
108
109 /* Report unexpected syntax in stabs. */
110 static void
111 _expected(
112         const char *who,        /* what function, or part thereof, is reporting */
113         const char *what,       /* what was expected */
114         const char *where,      /* where we were in the line of input */
115         int line)
116 {
117         fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
118         fprintf(stderr, "code line: %d, file %s\n", line,
119             (curhdr ? curhdr : "NO FILE"));
120         reset();
121 }
122
123 /*ARGSUSED*/
124 void
125 parse_init(tdata_t *td __unused)
126 {
127         int i;
128
129         for (i = 0; i < BUCKETS; i++) {
130                 hash_table[i] = NULL;
131                 name_table[i] = NULL;
132         }
133
134         if (typedbitfldmems != NULL) {
135                 list_free(typedbitfldmems, NULL, NULL);
136                 typedbitfldmems = NULL;
137         }
138 }
139
140 void
141 parse_finish(tdata_t *td)
142 {
143         td->td_nextid = ++faketypenumber;
144 }
145
146 static tdesc_t *
147 unres_new(int tid)
148 {
149         tdesc_t *tdp;
150
151         tdp = xcalloc(sizeof (*tdp));
152         tdp->t_type = TYPEDEF_UNRES;
153         tdp->t_id = tid;
154
155         return (tdp);
156 }
157
158 static char *
159 read_tid(char *cp, tdesc_t **tdpp)
160 {
161         tdesc_t *tdp;
162         int tid;
163
164         cp = id(cp, &tid);
165
166         assert(tid != 0);
167
168         if (*cp == '=') {
169                 if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
170                         return (NULL);
171                 if (tdp->t_id && tdp->t_id != tid) {
172                         tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
173
174                         ntdp->t_type = TYPEDEF;
175                         ntdp->t_tdesc = tdp;
176                         tdp = ntdp;
177                 }
178                 addhash(tdp, tid);
179         } else if ((tdp = lookup(tid)) == NULL)
180                 tdp = unres_new(tid);
181
182         *tdpp = tdp;
183         return (cp);
184 }
185
186 static iitype_t
187 parse_fun(char *cp, iidesc_t *ii)
188 {
189         iitype_t iitype = 0;
190         tdesc_t *tdp;
191         tdesc_t **args = NULL;
192         int nargs = 0;
193         int va = 0;
194
195         /*
196          * name:P               prototype
197          * name:F               global function
198          * name:f               static function
199          */
200         switch (*cp++) {
201         case 'P':
202                 iitype = II_NOT; /* not interesting */
203                 break;
204
205         case 'F':
206                 iitype = II_GFUN;
207                 break;
208
209         case 'f':
210                 iitype = II_SFUN;
211                 break;
212
213         default:
214                 expected("parse_nfun", "[PfF]", cp - 1);
215         }
216
217         if (!(cp = read_tid(cp, &tdp)))
218                 return (-1);
219
220         if (*cp)
221                 args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
222
223         while (*cp && *++cp) {
224                 if (*cp == '0') {
225                         va = 1;
226                         continue;
227                 }
228
229                 nargs++;
230                 if (nargs > FUNCARG_DEF)
231                         args = xrealloc(args, sizeof (tdesc_t *) * nargs);
232                 if (!(cp = read_tid(cp, &args[nargs - 1])))
233                         return (-1);
234         }
235
236         ii->ii_type = iitype;
237         ii->ii_dtype = tdp;
238         ii->ii_nargs = nargs;
239         ii->ii_args = args;
240         ii->ii_vargs = va;
241
242         return (iitype);
243 }
244
245 static iitype_t
246 parse_sym(char *cp, iidesc_t *ii)
247 {
248         tdesc_t *tdp;
249         iitype_t iitype = 0;
250
251         /*
252          * name:G               global variable
253          * name:S               static variable
254          */
255         switch (*cp++) {
256         case 'G':
257                 iitype = II_GVAR;
258                 break;
259         case 'S':
260                 iitype = II_SVAR;
261                 break;
262         case 'p':
263                 iitype = II_PSYM;
264                 break;
265         case '(':
266                 cp--;
267                 /*FALLTHROUGH*/
268         case 'r':
269         case 'V':
270                 iitype = II_NOT; /* not interesting */
271                 break;
272         default:
273                 expected("parse_sym", "[GprSV(]", cp - 1);
274         }
275
276         if (!(cp = read_tid(cp, &tdp)))
277                 return (-1);
278
279         ii->ii_type = iitype;
280         ii->ii_dtype = tdp;
281
282         return (iitype);
283 }
284
285 static iitype_t
286 parse_type(char *cp, iidesc_t *ii)
287 {
288         tdesc_t *tdp, *ntdp;
289         int tid;
290
291         if (*cp++ != 't')
292                 expected("parse_type", "t (type)", cp - 1);
293
294         cp = id(cp, &tid);
295         if ((tdp = lookup(tid)) == NULL) {
296                 if (*cp++ != '=')
297                         expected("parse_type", "= (definition)", cp - 1);
298
299                 (void) tdefdecl(cp, tid, &tdp);
300
301                 if (tdp->t_id == tid) {
302                         assert(tdp->t_type != TYPEDEF);
303                         assert(!lookup(tdp->t_id));
304
305                         if (!streq(tdp->t_name, ii->ii_name)) {
306                                 ntdp = xcalloc(sizeof (*ntdp));
307                                 ntdp->t_name = xstrdup(ii->ii_name);
308                                 ntdp->t_type = TYPEDEF;
309                                 ntdp->t_tdesc = tdp;
310                                 tdp->t_id = faketypenumber++;
311                                 tdp = ntdp;
312                         }
313                 } else if (tdp->t_id == 0) {
314                         assert(tdp->t_type == FORWARD ||
315                             tdp->t_type == INTRINSIC);
316
317                         if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
318                                 ntdp = xcalloc(sizeof (*ntdp));
319                                 ntdp->t_name = xstrdup(ii->ii_name);
320                                 ntdp->t_type = TYPEDEF;
321                                 ntdp->t_tdesc = tdp;
322                                 tdp->t_id = faketypenumber++;
323                                 tdp = ntdp;
324                         }
325                 } else if (tdp->t_id != tid) {
326                         ntdp = xcalloc(sizeof (*ntdp));
327                         ntdp->t_name = xstrdup(ii->ii_name);
328                         ntdp->t_type = TYPEDEF;
329                         ntdp->t_tdesc = tdp;
330                         tdp = ntdp;
331                 }
332
333                 if (tagadd(ii->ii_name, tid, tdp) < 0)
334                         return (-1);
335         }
336
337         ii->ii_type = II_TYPE;
338         ii->ii_dtype = tdp;
339         return (II_TYPE);
340 }
341
342 static iitype_t
343 parse_sou(char *cp, iidesc_t *idp)
344 {
345         tdesc_t *rtdp;
346         int tid;
347
348         if (*cp++ != 'T')
349                 expected("parse_sou", "T (sou)", cp - 1);
350
351         cp = id(cp, &tid);
352         if (*cp++ != '=')
353                 expected("parse_sou", "= (definition)", cp - 1);
354
355         parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
356             idp->ii_name : "(anon)");
357         if ((rtdp = lookup(tid)) != NULL) {
358                 if (idp->ii_name != NULL) {
359                         if (rtdp->t_name != NULL &&
360                             strcmp(rtdp->t_name, idp->ii_name) != 0) {
361                                 tdesc_t *tdp;
362
363                                 tdp = xcalloc(sizeof (*tdp));
364                                 tdp->t_name = xstrdup(idp->ii_name);
365                                 tdp->t_type = TYPEDEF;
366                                 tdp->t_tdesc = rtdp;
367                                 addhash(tdp, tid); /* for *(x,y) types */
368                                 parse_debug(3, NULL, "    %s defined as %s(%d)",
369                                     idp->ii_name, tdesc_name(rtdp), tid);
370                         } else if (rtdp->t_name == NULL) {
371                                 rtdp->t_name = xstrdup(idp->ii_name);
372                                 addhash(rtdp, tid);
373                         }
374                 }
375         } else {
376                 rtdp = xcalloc(sizeof (*rtdp));
377                 rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
378                 addhash(rtdp, tid);
379         }
380
381         switch (*cp++) {
382         case 's':
383                 (void) soudef(cp, STRUCT, &rtdp);
384                 break;
385         case 'u':
386                 (void) soudef(cp, UNION, &rtdp);
387                 break;
388         case 'e':
389                 enumdef(cp, &rtdp);
390                 break;
391         default:
392                 expected("parse_sou", "<tag type s/u/e>", cp - 1);
393                 break;
394         }
395
396         idp->ii_type = II_SOU;
397         idp->ii_dtype = rtdp;
398         return (II_SOU);
399 }
400
401 int
402 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
403 {
404         iidesc_t *ii = NULL;
405         iitype_t (*parse)(char *, iidesc_t *);
406         int rc;
407
408         /*
409          * set up for reset()
410          */
411         if (setjmp(resetbuf))
412                 return (-1);
413
414         cp = whitesp(cp);
415         ii = iidesc_new(NULL);
416         cp = name(cp, &ii->ii_name);
417
418         switch (stab->n_type) {
419         case N_FUN:
420                 parse = parse_fun;
421                 break;
422
423         case N_LSYM:
424                 if (*cp == 't')
425                         parse = parse_type;
426                 else if (*cp == 'T')
427                         parse = parse_sou;
428                 else
429                         parse = parse_sym;
430                 break;
431
432         case N_GSYM:
433         case N_LCSYM:
434         case N_PSYM:
435         case N_ROSYM:
436         case N_RSYM:
437         case N_STSYM:
438                 parse = parse_sym;
439                 break;
440         default:
441                 parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
442                 bzero(&resetbuf, sizeof (resetbuf));
443                 return (-1);
444         }
445
446         rc = parse(cp, ii);
447         bzero(&resetbuf, sizeof (resetbuf));
448
449         if (rc < 0 || ii->ii_type == II_NOT) {
450                 iidesc_free(ii, NULL);
451                 return (rc);
452         }
453
454         *iidescp = ii;
455
456         return (1);
457 }
458
459 /*
460  * Check if we have this node in the hash table already
461  */
462 tdesc_t *
463 lookup(int h)
464 {
465         int bucket = HASH(h);
466         tdesc_t *tdp = hash_table[bucket];
467
468         while (tdp != NULL) {
469                 if (tdp->t_id == h)
470                         return (tdp);
471                 tdp = tdp->t_hash;
472         }
473         return (NULL);
474 }
475
476 static char *
477 whitesp(char *cp)
478 {
479         char c;
480
481         for (c = *cp++; isspace(c); c = *cp++)
482                 ;
483         --cp;
484         return (cp);
485 }
486
487 static char *
488 name(char *cp, char **w)
489 {
490         char *new, *orig, c;
491         int len;
492
493         orig = cp;
494         c = *cp++;
495         if (c == ':')
496                 *w = NULL;
497         else if (isalpha(c) || strchr("_.$#", c)) {
498                 for (c = *cp++; isalnum(c) || strchr(" _.$#", c); c = *cp++)
499                         ;
500                 if (c != ':')
501                         reset();
502                 len = cp - orig;
503                 new = xmalloc(len);
504                 while (orig < cp - 1)
505                         *new++ = *orig++;
506                 *new = '\0';
507                 *w = new - (len - 1);
508         } else
509                 reset();
510
511         return (cp);
512 }
513
514 static char *
515 number(char *cp, int *n)
516 {
517         char *next;
518
519         *n = (int)strtol(cp, &next, 10);
520         if (next == cp)
521                 expected("number", "<number>", cp);
522         return (next);
523 }
524
525 static char *
526 id(char *cp, int *h)
527 {
528         int n1, n2;
529
530         if (*cp == '(') {       /* SunPro style */
531                 cp++;
532                 cp = number(cp, &n1);
533                 if (*cp++ != ',')
534                         expected("id", ",", cp - 1);
535                 cp = number(cp, &n2);
536                 if (*cp++ != ')')
537                         expected("id", ")", cp - 1);
538                 *h = MAKETYPEID(n1, n2);
539         } else if (isdigit(*cp)) { /* gcc style */
540                 cp = number(cp, &n1);
541                 *h = n1;
542         } else {
543                 expected("id", "(/0-9", cp);
544         }
545         return (cp);
546 }
547
548 static int
549 tagadd(char *w, int h, tdesc_t *tdp)
550 {
551         tdesc_t *otdp;
552
553         tdp->t_name = w;
554         if (!(otdp = lookup(h)))
555                 addhash(tdp, h);
556         else if (otdp != tdp) {
557                 warning("duplicate entry\n");
558                 warning("  old: %s %d (%d,%d)\n", tdesc_name(otdp),
559                     otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
560                 warning("  new: %s %d (%d,%d)\n", tdesc_name(tdp),
561                     tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
562                 return (-1);
563         }
564
565         return (0);
566 }
567
568 static char *
569 tdefdecl(char *cp, int h, tdesc_t **rtdp)
570 {
571         tdesc_t *ntdp;
572         char *w;
573         int c, h2;
574         char type;
575
576         parse_debug(3, cp, "tdefdecl h=%d", h);
577
578         /* Type codes */
579         switch (type = *cp) {
580         case 'b': /* integer */
581         case 'R': /* fp */
582                 cp = intrinsic(cp, rtdp);
583                 break;
584         case '(': /* equiv to another type */
585                 cp = id(cp, &h2);
586                 ntdp = lookup(h2);
587
588                 if (ntdp != NULL && *cp == '=') {
589                         if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
590                                 /*
591                                  * The 6.2 compiler, and possibly others, will
592                                  * sometimes emit the same stab for a forward
593                                  * declaration twice.  That is, "(1,2)=xsfoo:"
594                                  * will sometimes show up in two different
595                                  * places.  This is, of course, quite fun.  We
596                                  * want CTF to work in spite of the compiler,
597                                  * so we'll let this one through.
598                                  */
599                                 char *c2 = cp + 2;
600                                 char *nm;
601
602                                 if (!strchr("sue", *c2++)) {
603                                         expected("tdefdecl/x-redefine", "[sue]",
604                                             c2 - 1);
605                                 }
606
607                                 c2 = name(c2, &nm);
608                                 if (strcmp(nm, ntdp->t_name) != 0) {
609                                         terminate("Stabs error: Attempt to "
610                                             "redefine type (%d,%d) as "
611                                             "something else: %s\n",
612                                             TYPEFILE(h2), TYPENUM(h2),
613                                             c2 - 1);
614                                 }
615                                 free(nm);
616
617                                 h2 = faketypenumber++;
618                                 ntdp = NULL;
619                         } else {
620                                 terminate("Stabs error: Attempting to "
621                                     "redefine type (%d,%d)\n", TYPEFILE(h2),
622                                     TYPENUM(h2));
623                         }
624                 }
625
626                 if (ntdp == NULL) {  /* if that type isn't defined yet */
627                         if (*cp != '=') {
628                                 /* record it as unresolved */
629                                 parse_debug(3, NULL, "tdefdecl unres type %d",
630                                     h2);
631                                 *rtdp = calloc(sizeof (**rtdp), 1);
632                                 (*rtdp)->t_type = TYPEDEF_UNRES;
633                                 (*rtdp)->t_id = h2;
634                                 break;
635                         } else
636                                 cp++;
637
638                         /* define a new type */
639                         cp = tdefdecl(cp, h2, rtdp);
640                         if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
641                                 ntdp = calloc(sizeof (*ntdp), 1);
642                                 ntdp->t_type = TYPEDEF;
643                                 ntdp->t_tdesc = *rtdp;
644                                 *rtdp = ntdp;
645                         }
646
647                         addhash(*rtdp, h2);
648
649                 } else { /* that type is already defined */
650                         if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
651                                 *rtdp = ntdp;
652                         } else {
653                                 parse_debug(3, NULL,
654                                     "No duplicate typedef anon for ref");
655                                 *rtdp = ntdp;
656                         }
657                 }
658                 break;
659         case '*':
660                 ntdp = NULL;
661                 cp = tdefdecl(cp + 1, h, &ntdp);
662                 if (ntdp == NULL)
663                         expected("tdefdecl/*", "id", cp);
664
665                 if (!ntdp->t_id)
666                         ntdp->t_id = faketypenumber++;
667
668                 *rtdp = xcalloc(sizeof (**rtdp));
669                 (*rtdp)->t_type = POINTER;
670                 (*rtdp)->t_size = 0;
671                 (*rtdp)->t_id = h;
672                 (*rtdp)->t_tdesc = ntdp;
673                 break;
674         case 'f':
675                 cp = tdefdecl(cp + 1, h, &ntdp);
676                 *rtdp = xcalloc(sizeof (**rtdp));
677                 (*rtdp)->t_type = FUNCTION;
678                 (*rtdp)->t_size = 0;
679                 (*rtdp)->t_id = h;
680                 (*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
681                 /*
682                  * The 6.1 compiler will sometimes generate incorrect stabs for
683                  * function pointers (it'll get the return type wrong).  This
684                  * causes merges to fail.  We therefore treat function pointers
685                  * as if they all point to functions that return int.  When
686                  * 4432549 is fixed, the lookupname() call below should be
687                  * replaced with `ntdp'.
688                  */
689                 (*rtdp)->t_fndef->fn_ret = lookupname("int");
690                 break;
691         case 'a':
692         case 'z':
693                 cp++;
694                 if (*cp++ != 'r')
695                         expected("tdefdecl/[az]", "r", cp - 1);
696                 *rtdp = xcalloc(sizeof (**rtdp));
697                 (*rtdp)->t_type = ARRAY;
698                 (*rtdp)->t_id = h;
699                 cp = arraydef(cp, rtdp);
700                 break;
701         case 'x':
702                 c = *++cp;
703                 if (c != 's' && c != 'u' && c != 'e')
704                         expected("tdefdecl/x", "[sue]", cp - 1);
705                 cp = name(cp + 1, &w);
706
707                 ntdp = xcalloc(sizeof (*ntdp));
708                 ntdp->t_type = FORWARD;
709                 ntdp->t_name = w;
710                 /*
711                  * We explicitly don't set t_id here - the caller will do it.
712                  * The caller may want to use a real type ID, or they may
713                  * choose to make one up.
714                  */
715
716                 *rtdp = ntdp;
717                 break;
718
719         case 'B': /* volatile */
720                 cp = tdefdecl(cp + 1, h, &ntdp);
721
722                 if (!ntdp->t_id)
723                         ntdp->t_id = faketypenumber++;
724
725                 *rtdp = xcalloc(sizeof (**rtdp));
726                 (*rtdp)->t_type = VOLATILE;
727                 (*rtdp)->t_size = 0;
728                 (*rtdp)->t_tdesc = ntdp;
729                 (*rtdp)->t_id = h;
730                 break;
731
732         case 'k': /* const */
733                 cp = tdefdecl(cp + 1, h, &ntdp);
734
735                 if (!ntdp->t_id)
736                         ntdp->t_id = faketypenumber++;
737
738                 *rtdp = xcalloc(sizeof (**rtdp));
739                 (*rtdp)->t_type = CONST;
740                 (*rtdp)->t_size = 0;
741                 (*rtdp)->t_tdesc = ntdp;
742                 (*rtdp)->t_id = h;
743                 break;
744
745         case 'K': /* restricted */
746                 cp = tdefdecl(cp + 1, h, &ntdp);
747
748                 if (!ntdp->t_id)
749                         ntdp->t_id = faketypenumber++;
750
751                 *rtdp = xcalloc(sizeof (**rtdp));
752                 (*rtdp)->t_type = RESTRICT;
753                 (*rtdp)->t_size = 0;
754                 (*rtdp)->t_tdesc = ntdp;
755                 (*rtdp)->t_id = h;
756                 break;
757
758         case 'u':
759         case 's':
760                 cp++;
761
762                 *rtdp = xcalloc(sizeof (**rtdp));
763                 (*rtdp)->t_name = NULL;
764                 cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
765                 break;
766         default:
767                 expected("tdefdecl", "<type code>", cp);
768         }
769         return (cp);
770 }
771
772 static char *
773 intrinsic(char *cp, tdesc_t **rtdp)
774 {
775         intr_t *intr = xcalloc(sizeof (intr_t));
776         tdesc_t *tdp;
777         int width, fmt, i;
778
779         switch (*cp++) {
780         case 'b':
781                 intr->intr_type = INTR_INT;
782                 if (*cp == 's')
783                         intr->intr_signed = 1;
784                 else if (*cp != 'u')
785                         expected("intrinsic/b", "[su]", cp);
786                 cp++;
787
788                 if (strchr("cbv", *cp))
789                         intr->intr_iformat = *cp++;
790
791                 cp = number(cp, &width);
792                 if (*cp++ != ';')
793                         expected("intrinsic/b", "; (post-width)", cp - 1);
794
795                 cp = number(cp, &intr->intr_offset);
796                 if (*cp++ != ';')
797                         expected("intrinsic/b", "; (post-offset)", cp - 1);
798
799                 cp = number(cp, &intr->intr_nbits);
800                 break;
801
802         case 'R':
803                 intr->intr_type = INTR_REAL;
804                 for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
805                         fmt = fmt * 10 + (*(cp + i) - '0');
806
807                 if (fmt < 1 || fmt > CTF_FP_MAX)
808                         expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
809
810                 intr->intr_fformat = fmt;
811                 cp += i;
812
813                 if (*cp++ != ';')
814                         expected("intrinsic/R", ";", cp - 1);
815                 cp = number(cp, &width);
816
817                 intr->intr_nbits = width * 8;
818                 break;
819         }
820
821         tdp = xcalloc(sizeof (*tdp));
822         tdp->t_type = INTRINSIC;
823         tdp->t_size = width;
824         tdp->t_name = NULL;
825         tdp->t_intr = intr;
826         parse_debug(3, NULL, "intrinsic: size=%d", width);
827         *rtdp = tdp;
828
829         return (cp);
830 }
831
832 static tdesc_t *
833 bitintrinsic(tdesc_t *template, int nbits)
834 {
835         tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
836
837         newtdp->t_name = xstrdup(template->t_name);
838         newtdp->t_id = faketypenumber++;
839         newtdp->t_type = INTRINSIC;
840         newtdp->t_size = template->t_size;
841         newtdp->t_intr = xmalloc(sizeof (intr_t));
842         bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
843         newtdp->t_intr->intr_nbits = nbits;
844
845         return (newtdp);
846 }
847
848 static char *
849 offsize(char *cp, mlist_t *mlp)
850 {
851         int offset, size;
852
853         if (*cp == ',')
854                 cp++;
855         cp = number(cp, &offset);
856         if (*cp++ != ',')
857                 expected("offsize/2", ",", cp - 1);
858         cp = number(cp, &size);
859         if (*cp++ != ';')
860                 expected("offsize/3", ";", cp - 1);
861         mlp->ml_offset = offset;
862         mlp->ml_size = size;
863         return (cp);
864 }
865
866 static tdesc_t *
867 find_intrinsic(tdesc_t *tdp)
868 {
869         for (;;) {
870                 switch (tdp->t_type) {
871                 case TYPEDEF:
872                 case VOLATILE:
873                 case CONST:
874                 case RESTRICT:
875                         tdp = tdp->t_tdesc;
876                         break;
877
878                 default:
879                         return (tdp);
880                 }
881         }
882 }
883
884 static char *
885 soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
886 {
887         mlist_t *mlp, **prev;
888         char *w;
889         int h;
890         int size;
891         tdesc_t *tdp, *itdp;
892
893         cp = number(cp, &size);
894         (*rtdp)->t_size = size;
895         (*rtdp)->t_type = type; /* s or u */
896
897         /*
898          * An '@' here indicates a bitmask follows.   This is so the
899          * compiler can pass information to debuggers about how structures
900          * are passed in the v9 world.  We don't need this information
901          * so we skip over it.
902          */
903         if (cp[0] == '@') {
904                 cp += 3;
905         }
906
907         parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
908             (*rtdp)->t_size);
909
910         prev = &((*rtdp)->t_members);
911         /* now fill up the fields */
912         while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
913                 mlp = xcalloc(sizeof (*mlp));
914                 *prev = mlp;
915                 cp = name(cp, &w);
916                 mlp->ml_name = w;
917                 cp = id(cp, &h);
918                 /*
919                  * find the tdesc struct in the hash table for this type
920                  * and stick a ptr in here
921                  */
922                 tdp = lookup(h);
923                 if (tdp == NULL) { /* not in hash list */
924                         parse_debug(3, NULL, "      defines %s (%d)", w, h);
925                         if (*cp++ != '=') {
926                                 tdp = unres_new(h);
927                                 parse_debug(3, NULL,
928                                     "      refers to %s (unresolved %d)",
929                                     (w ? w : "anon"), h);
930                         } else {
931                                 cp = tdefdecl(cp, h, &tdp);
932
933                                 if (tdp->t_id && tdp->t_id != h) {
934                                         tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
935
936                                         ntdp->t_type = TYPEDEF;
937                                         ntdp->t_tdesc = tdp;
938                                         tdp = ntdp;
939                                 }
940
941                                 addhash(tdp, h);
942                                 parse_debug(4, cp,
943                                     "     soudef now looking at    ");
944                                 cp++;
945                         }
946                 } else {
947                         parse_debug(3, NULL, "      refers to %s (%d, %s)",
948                             w ? w : "anon", h, tdesc_name(tdp));
949                 }
950
951                 cp = offsize(cp, mlp);
952
953                 itdp = find_intrinsic(tdp);
954                 if (itdp->t_type == INTRINSIC) {
955                         if (mlp->ml_size != itdp->t_intr->intr_nbits) {
956                                 parse_debug(4, cp, "making %d bit intrinsic "
957                                     "from %s", mlp->ml_size, tdesc_name(itdp));
958                                 mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
959                         } else
960                                 mlp->ml_type = tdp;
961                 } else if (itdp->t_type == TYPEDEF_UNRES) {
962                         list_add(&typedbitfldmems, mlp);
963                         mlp->ml_type = tdp;
964                 } else {
965                         mlp->ml_type = tdp;
966                 }
967
968                 /* cp is now pointing to next field */
969                 prev = &mlp->ml_next;
970         }
971         return (cp);
972 }
973
974 static char *
975 arraydef(char *cp, tdesc_t **rtdp)
976 {
977         int start, end, h;
978
979         cp = id(cp, &h);
980         if (*cp++ != ';')
981                 expected("arraydef/1", ";", cp - 1);
982
983         (*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
984         (*rtdp)->t_ardef->ad_idxtype = lookup(h);
985
986         cp = number(cp, &start); /* lower */
987         if (*cp++ != ';')
988                 expected("arraydef/2", ";", cp - 1);
989
990         if (*cp == 'S') {
991                 /*
992                  * variable length array - treat as null dimensioned
993                  *
994                  * For VLA variables on sparc, SS12 generated stab entry
995                  * looks as follows:
996                  * .stabs "buf:(0,28)=zr(0,4);0;S-12;(0,1)", 0x80, 0, 0, -16
997                  * Whereas SS12u1 generated stab entry looks like this:
998                  * .stabs "buf:(0,28)=zr(0,4);0;S0;(0,1)", 0x80, 0, 0, 0
999                  * On x86, both versions generate the first type of entry.
1000                  * We should be able to parse both.
1001                  */
1002                 cp++;
1003                 if (*cp == '-')
1004                         cp++;
1005                 cp = number(cp, &end);
1006                 end = start;
1007         } else {
1008                 /*
1009                  * normal fixed-dimension array
1010                  * Stab entry for this looks as follows :
1011                  * .stabs "x:(0,28)=ar(0,4);0;9;(0,3)", 0x80, 0, 40, 0
1012                  */
1013                 cp = number(cp, &end);  /* upper */
1014         }
1015
1016         if (*cp++ != ';')
1017                 expected("arraydef/3", ";", cp - 1);
1018         (*rtdp)->t_ardef->ad_nelems = end - start + 1;
1019         cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
1020
1021         parse_debug(3, cp, "defined array idx type %d %d-%d next ",
1022             h, start, end);
1023
1024         return (cp);
1025 }
1026
1027 static void
1028 enumdef(char *cp, tdesc_t **rtdp)
1029 {
1030         elist_t *elp, **prev;
1031         char *w;
1032
1033         (*rtdp)->t_type = ENUM;
1034         (*rtdp)->t_emem = NULL;
1035
1036         prev = &((*rtdp)->t_emem);
1037         while (*cp != ';') {
1038                 elp = xcalloc(sizeof (*elp));
1039                 elp->el_next = NULL;
1040                 *prev = elp;
1041                 cp = name(cp, &w);
1042                 elp->el_name = w;
1043                 cp = number(cp, &elp->el_number);
1044                 parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
1045                     elp->el_name, elp->el_number);
1046                 prev = &elp->el_next;
1047                 if (*cp++ != ',')
1048                         expected("enumdef", ",", cp - 1);
1049         }
1050 }
1051
1052 static tdesc_t *
1053 lookup_name(tdesc_t **hash, const char *name1)
1054 {
1055         int bucket = compute_sum(name1);
1056         tdesc_t *tdp, *ttdp = NULL;
1057
1058         for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
1059                 if (tdp->t_name != NULL && strcmp(tdp->t_name, name1) == 0) {
1060                         if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1061                             tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
1062                                 return (tdp);
1063                         if (tdp->t_type == TYPEDEF)
1064                                 ttdp = tdp;
1065                 }
1066         }
1067         return (ttdp);
1068 }
1069
1070 tdesc_t *
1071 lookupname(const char *name1)
1072 {
1073         return (lookup_name(name_table, name1));
1074 }
1075
1076 /*
1077  * Add a node to the hash queues.
1078  */
1079 static void
1080 addhash(tdesc_t *tdp, int num)
1081 {
1082         int hash = HASH(num);
1083         tdesc_t *ttdp;
1084         char added_num = 0, added_name = 0;
1085
1086         /*
1087          * If it already exists in the hash table don't add it again
1088          * (but still check to see if the name should be hashed).
1089          */
1090         ttdp = lookup(num);
1091
1092         if (ttdp == NULL) {
1093                 tdp->t_id = num;
1094                 tdp->t_hash = hash_table[hash];
1095                 hash_table[hash] = tdp;
1096                 added_num = 1;
1097         }
1098
1099         if (tdp->t_name != NULL) {
1100                 ttdp = lookupname(tdp->t_name);
1101                 if (ttdp == NULL) {
1102                         hash = compute_sum(tdp->t_name);
1103                         tdp->t_next = name_table[hash];
1104                         name_table[hash] = tdp;
1105                         added_name = 1;
1106                 }
1107         }
1108         if (!added_num && !added_name) {
1109                 terminate("stabs: broken hash\n");
1110         }
1111 }
1112
1113 static int
1114 compute_sum(const char *w)
1115 {
1116         char c;
1117         int sum;
1118
1119         for (sum = 0; (c = *w) != '\0'; sum += c, w++)
1120                 ;
1121         return (HASH(sum));
1122 }
1123
1124 static void
1125 reset(void)
1126 {
1127         longjmp(resetbuf, 1);
1128 }
1129
1130 void
1131 check_hash(void)
1132 {
1133         tdesc_t *tdp;
1134         int i;
1135
1136         printf("checking hash\n");
1137         for (i = 0; i < BUCKETS; i++) {
1138                 if (hash_table[i]) {
1139                         for (tdp = hash_table[i]->t_hash;
1140                             tdp && tdp != hash_table[i];
1141                             tdp = tdp->t_hash)
1142                                 continue;
1143                         if (tdp) {
1144                                 terminate("cycle in hash bucket %d\n", i);
1145                                 return;
1146                         }
1147                 }
1148
1149                 if (name_table[i]) {
1150                         for (tdp = name_table[i]->t_next;
1151                             tdp && tdp != name_table[i];
1152                             tdp = tdp->t_next)
1153                                 continue;
1154                         if (tdp) {
1155                                 terminate("cycle in name bucket %d\n", i);
1156                                 return;
1157                         }
1158                 }
1159         }
1160         printf("done\n");
1161 }
1162
1163 /*ARGSUSED1*/
1164 static int
1165 resolve_typed_bitfields_cb(void *arg, void *private __unused)
1166 {
1167         mlist_t *ml = arg;
1168         tdesc_t *tdp = ml->ml_type;
1169
1170         debug(3, "Resolving typed bitfields (member %s)\n",
1171             (ml->ml_name ? ml->ml_name : "(anon)"));
1172
1173         while (tdp) {
1174                 switch (tdp->t_type) {
1175                 case INTRINSIC:
1176                         if (ml->ml_size != tdp->t_intr->intr_nbits) {
1177                                 debug(3, "making %d bit intrinsic from %s",
1178                                     ml->ml_size, tdesc_name(tdp));
1179                                 ml->ml_type = bitintrinsic(tdp, ml->ml_size);
1180                         } else {
1181                                 debug(3, "using existing %d bit %s intrinsic",
1182                                     ml->ml_size, tdesc_name(tdp));
1183                                 ml->ml_type = tdp;
1184                         }
1185                         return (1);
1186
1187                 case POINTER:
1188                 case TYPEDEF:
1189                 case VOLATILE:
1190                 case CONST:
1191                 case RESTRICT:
1192                         tdp = tdp->t_tdesc;
1193                         break;
1194
1195                 default:
1196                         return (1);
1197                 }
1198         }
1199
1200         terminate("type chain for bitfield member %s has a NULL", ml->ml_name);
1201         /*NOTREACHED*/
1202         return (0);
1203 }
1204
1205 void
1206 resolve_typed_bitfields(void)
1207 {
1208         (void) list_iter(typedbitfldmems,
1209             resolve_typed_bitfields_cb, NULL);
1210 }