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