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