xref: /onnv-gate/usr/src/tools/ctf/cvt/st_parse.c (revision 12177:800b7f847f1e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51882Sjohnlev  * Common Development and Distribution License (the "License").
61882Sjohnlev  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12177SSurya.Prakki@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * This file is a sewer.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <stdarg.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate #include <setjmp.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <uts/common/sys/ctf.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "ctftools.h"
390Sstevel@tonic-gate #include "memory.h"
400Sstevel@tonic-gate #include "list.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	HASH(NUM)	((int)(NUM & (BUCKETS - 1)))
430Sstevel@tonic-gate #define	BUCKETS		128
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	TYPEPAIRMULT	10000
460Sstevel@tonic-gate #define	MAKETYPEID(file, num)	((file) * TYPEPAIRMULT + num)
470Sstevel@tonic-gate #define	TYPEFILE(tid)		((tid) / TYPEPAIRMULT)
480Sstevel@tonic-gate #define	TYPENUM(tid)		((tid) % TYPEPAIRMULT)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	expected(a, b, c) _expected(a, b, c, __LINE__)
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static int faketypenumber = 100000000;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static tdesc_t *hash_table[BUCKETS];
550Sstevel@tonic-gate static tdesc_t *name_table[BUCKETS];
560Sstevel@tonic-gate 
570Sstevel@tonic-gate list_t *typedbitfldmems;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static void reset(void);
600Sstevel@tonic-gate static jmp_buf	resetbuf;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static char *soudef(char *cp, stabtype_t type, tdesc_t **rtdp);
630Sstevel@tonic-gate static void enumdef(char *cp, tdesc_t **rtdp);
642722Sjohnlev static int compute_sum(const char *w);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static char *number(char *cp, int *n);
670Sstevel@tonic-gate static char *name(char *cp, char **w);
680Sstevel@tonic-gate static char *id(char *cp, int *h);
690Sstevel@tonic-gate static char *whitesp(char *cp);
700Sstevel@tonic-gate static void addhash(tdesc_t *tdp, int num);
710Sstevel@tonic-gate static int tagadd(char *w, int h, tdesc_t *tdp);
720Sstevel@tonic-gate static char *tdefdecl(char *cp, int h, tdesc_t **rtdp);
730Sstevel@tonic-gate static char *intrinsic(char *cp, tdesc_t **rtdp);
740Sstevel@tonic-gate static char *arraydef(char *cp, tdesc_t **rtdp);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate extern int debug_level;
770Sstevel@tonic-gate int debug_parse = DEBUG_PARSE;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*PRINTFLIKE3*/
800Sstevel@tonic-gate static void
parse_debug(int level,char * cp,char * fmt,...)810Sstevel@tonic-gate parse_debug(int level, char *cp, char *fmt, ...)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	va_list ap;
840Sstevel@tonic-gate 	char buf[1024];
850Sstevel@tonic-gate 	char tmp[32];
860Sstevel@tonic-gate 	int i;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (level > debug_level || !debug_parse)
890Sstevel@tonic-gate 		return;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (cp != NULL) {
920Sstevel@tonic-gate 		for (i = 0; i < 30; i++) {
930Sstevel@tonic-gate 			if (cp[i] == '\0')
940Sstevel@tonic-gate 				break;
950Sstevel@tonic-gate 			if (!iscntrl(cp[i]))
960Sstevel@tonic-gate 				tmp[i] = cp[i];
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		tmp[i] = '\0';
990Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
1000Sstevel@tonic-gate 	} else {
1010Sstevel@tonic-gate 		strcpy(buf, fmt);
1020Sstevel@tonic-gate 		strcat(buf, "\n");
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	va_start(ap, fmt);
1060Sstevel@tonic-gate 	vadebug(level, buf, ap);
1070Sstevel@tonic-gate 	va_end(ap);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /* Report unexpected syntax in stabs. */
1110Sstevel@tonic-gate static void
_expected(char * who,char * what,char * where,int line)1120Sstevel@tonic-gate _expected(
1130Sstevel@tonic-gate 	char *who,	/* what function, or part thereof, is reporting */
1140Sstevel@tonic-gate 	char *what,	/* what was expected */
1150Sstevel@tonic-gate 	char *where,	/* where we were in the line of input */
1160Sstevel@tonic-gate 	int line)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
1190Sstevel@tonic-gate 	fprintf(stderr, "code line: %d, file %s\n", line,
1200Sstevel@tonic-gate 	    (curhdr ? curhdr : "NO FILE"));
1210Sstevel@tonic-gate 	reset();
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*ARGSUSED*/
1250Sstevel@tonic-gate void
parse_init(tdata_t * td)1260Sstevel@tonic-gate parse_init(tdata_t *td)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	int i;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++) {
1310Sstevel@tonic-gate 		hash_table[i] = NULL;
1320Sstevel@tonic-gate 		name_table[i] = NULL;
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (typedbitfldmems != NULL) {
1360Sstevel@tonic-gate 		list_free(typedbitfldmems, NULL, NULL);
1370Sstevel@tonic-gate 		typedbitfldmems = NULL;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate void
parse_finish(tdata_t * td)1420Sstevel@tonic-gate parse_finish(tdata_t *td)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	td->td_nextid = ++faketypenumber;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static tdesc_t *
unres_new(int tid)1480Sstevel@tonic-gate unres_new(int tid)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	tdesc_t *tdp;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	tdp = xcalloc(sizeof (*tdp));
1530Sstevel@tonic-gate 	tdp->t_type = TYPEDEF_UNRES;
1540Sstevel@tonic-gate 	tdp->t_id = tid;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (tdp);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate char *
read_tid(char * cp,tdesc_t ** tdpp)1600Sstevel@tonic-gate read_tid(char *cp, tdesc_t **tdpp)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	tdesc_t *tdp;
1630Sstevel@tonic-gate 	int tid;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	cp = id(cp, &tid);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	assert(tid != 0);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (*cp == '=') {
1700Sstevel@tonic-gate 		if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
1710Sstevel@tonic-gate 			return (NULL);
1720Sstevel@tonic-gate 		if (tdp->t_id && tdp->t_id != tid) {
1730Sstevel@tonic-gate 			tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 			ntdp->t_type = TYPEDEF;
1760Sstevel@tonic-gate 			ntdp->t_tdesc = tdp;
1770Sstevel@tonic-gate 			tdp = ntdp;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		addhash(tdp, tid);
1800Sstevel@tonic-gate 	} else if ((tdp = lookup(tid)) == NULL)
1810Sstevel@tonic-gate 		tdp = unres_new(tid);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	*tdpp = tdp;
1840Sstevel@tonic-gate 	return (cp);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static iitype_t
parse_fun(char * cp,iidesc_t * ii)1880Sstevel@tonic-gate parse_fun(char *cp, iidesc_t *ii)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	iitype_t iitype;
1910Sstevel@tonic-gate 	tdesc_t *tdp;
1920Sstevel@tonic-gate 	tdesc_t **args = NULL;
1930Sstevel@tonic-gate 	int nargs = 0;
1940Sstevel@tonic-gate 	int va = 0;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * name:P		prototype
1980Sstevel@tonic-gate 	 * name:F		global function
1990Sstevel@tonic-gate 	 * name:f		static function
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	switch (*cp++) {
2020Sstevel@tonic-gate 	case 'P':
2030Sstevel@tonic-gate 		iitype = II_NOT; /* not interesting */
2040Sstevel@tonic-gate 		break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	case 'F':
2070Sstevel@tonic-gate 		iitype = II_GFUN;
2080Sstevel@tonic-gate 		break;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	case 'f':
2110Sstevel@tonic-gate 		iitype = II_SFUN;
2120Sstevel@tonic-gate 		break;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	default:
2150Sstevel@tonic-gate 		expected("parse_nfun", "[PfF]", cp - 1);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (!(cp = read_tid(cp, &tdp)))
2190Sstevel@tonic-gate 		return (-1);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (*cp)
2220Sstevel@tonic-gate 		args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	while (*cp && *++cp) {
2250Sstevel@tonic-gate 		if (*cp == '0') {
2260Sstevel@tonic-gate 			va = 1;
2270Sstevel@tonic-gate 			continue;
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		nargs++;
2310Sstevel@tonic-gate 		if (nargs > FUNCARG_DEF)
2320Sstevel@tonic-gate 			args = xrealloc(args, sizeof (tdesc_t *) * nargs);
2330Sstevel@tonic-gate 		if (!(cp = read_tid(cp, &args[nargs - 1])))
2340Sstevel@tonic-gate 			return (-1);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	ii->ii_type = iitype;
2380Sstevel@tonic-gate 	ii->ii_dtype = tdp;
2390Sstevel@tonic-gate 	ii->ii_nargs = nargs;
2400Sstevel@tonic-gate 	ii->ii_args = args;
2410Sstevel@tonic-gate 	ii->ii_vargs = va;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (iitype);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static iitype_t
parse_sym(char * cp,iidesc_t * ii)2470Sstevel@tonic-gate parse_sym(char *cp, iidesc_t *ii)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	tdesc_t *tdp;
2500Sstevel@tonic-gate 	iitype_t iitype;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * name:G		global variable
2540Sstevel@tonic-gate 	 * name:S		static variable
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	switch (*cp++) {
2570Sstevel@tonic-gate 	case 'G':
2580Sstevel@tonic-gate 		iitype = II_GVAR;
2590Sstevel@tonic-gate 		break;
2600Sstevel@tonic-gate 	case 'S':
2610Sstevel@tonic-gate 		iitype = II_SVAR;
2620Sstevel@tonic-gate 		break;
2630Sstevel@tonic-gate 	case 'p':
2640Sstevel@tonic-gate 		iitype = II_PSYM;
2650Sstevel@tonic-gate 		break;
2660Sstevel@tonic-gate 	case '(':
2670Sstevel@tonic-gate 		cp--;
2680Sstevel@tonic-gate 		/*FALLTHROUGH*/
2690Sstevel@tonic-gate 	case 'r':
2700Sstevel@tonic-gate 	case 'V':
2710Sstevel@tonic-gate 		iitype = II_NOT; /* not interesting */
2720Sstevel@tonic-gate 		break;
2730Sstevel@tonic-gate 	default:
2740Sstevel@tonic-gate 		expected("parse_sym", "[GprSV(]", cp - 1);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (!(cp = read_tid(cp, &tdp)))
2780Sstevel@tonic-gate 		return (-1);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	ii->ii_type = iitype;
2810Sstevel@tonic-gate 	ii->ii_dtype = tdp;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (iitype);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static iitype_t
parse_type(char * cp,iidesc_t * ii)2870Sstevel@tonic-gate parse_type(char *cp, iidesc_t *ii)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	tdesc_t *tdp, *ntdp;
2900Sstevel@tonic-gate 	int tid;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (*cp++ != 't')
2930Sstevel@tonic-gate 		expected("parse_type", "t (type)", cp - 1);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	cp = id(cp, &tid);
2960Sstevel@tonic-gate 	if ((tdp = lookup(tid)) == NULL) {
2970Sstevel@tonic-gate 		if (*cp++ != '=')
2980Sstevel@tonic-gate 			expected("parse_type", "= (definition)", cp - 1);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		(void) tdefdecl(cp, tid, &tdp);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		if (tdp->t_id == tid) {
3030Sstevel@tonic-gate 			assert(tdp->t_type != TYPEDEF);
3040Sstevel@tonic-gate 			assert(!lookup(tdp->t_id));
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 			if (!streq(tdp->t_name, ii->ii_name)) {
3070Sstevel@tonic-gate 				ntdp = xcalloc(sizeof (*ntdp));
3080Sstevel@tonic-gate 				ntdp->t_name = xstrdup(ii->ii_name);
3090Sstevel@tonic-gate 				ntdp->t_type = TYPEDEF;
3100Sstevel@tonic-gate 				ntdp->t_tdesc = tdp;
3110Sstevel@tonic-gate 				tdp->t_id = faketypenumber++;
3120Sstevel@tonic-gate 				tdp = ntdp;
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 		} else if (tdp->t_id == 0) {
3150Sstevel@tonic-gate 			assert(tdp->t_type == FORWARD ||
3160Sstevel@tonic-gate 			    tdp->t_type == INTRINSIC);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
3190Sstevel@tonic-gate 				ntdp = xcalloc(sizeof (*ntdp));
3200Sstevel@tonic-gate 				ntdp->t_name = xstrdup(ii->ii_name);
3210Sstevel@tonic-gate 				ntdp->t_type = TYPEDEF;
3220Sstevel@tonic-gate 				ntdp->t_tdesc = tdp;
3230Sstevel@tonic-gate 				tdp->t_id = faketypenumber++;
3240Sstevel@tonic-gate 				tdp = ntdp;
3250Sstevel@tonic-gate 			}
3260Sstevel@tonic-gate 		} else if (tdp->t_id != tid) {
3270Sstevel@tonic-gate 			ntdp = xcalloc(sizeof (*ntdp));
3280Sstevel@tonic-gate 			ntdp->t_name = xstrdup(ii->ii_name);
3290Sstevel@tonic-gate 			ntdp->t_type = TYPEDEF;
3300Sstevel@tonic-gate 			ntdp->t_tdesc = tdp;
3310Sstevel@tonic-gate 			tdp = ntdp;
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		if (tagadd(ii->ii_name, tid, tdp) < 0)
3350Sstevel@tonic-gate 			return (-1);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	ii->ii_type = II_TYPE;
3390Sstevel@tonic-gate 	ii->ii_dtype = tdp;
3400Sstevel@tonic-gate 	return (II_TYPE);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static iitype_t
parse_sou(char * cp,iidesc_t * idp)3440Sstevel@tonic-gate parse_sou(char *cp, iidesc_t *idp)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	tdesc_t *rtdp;
3470Sstevel@tonic-gate 	int tid;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (*cp++ != 'T')
3500Sstevel@tonic-gate 		expected("parse_sou", "T (sou)", cp - 1);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	cp = id(cp, &tid);
3530Sstevel@tonic-gate 	if (*cp++ != '=')
3540Sstevel@tonic-gate 		expected("parse_sou", "= (definition)", cp - 1);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
3570Sstevel@tonic-gate 	    idp->ii_name : "(anon)");
3580Sstevel@tonic-gate 	if ((rtdp = lookup(tid)) != NULL) {
3590Sstevel@tonic-gate 		if (idp->ii_name != NULL) {
3600Sstevel@tonic-gate 			if (rtdp->t_name != NULL &&
3610Sstevel@tonic-gate 			    strcmp(rtdp->t_name, idp->ii_name) != 0) {
3620Sstevel@tonic-gate 				tdesc_t *tdp;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 				tdp = xcalloc(sizeof (*tdp));
3650Sstevel@tonic-gate 				tdp->t_name = xstrdup(idp->ii_name);
3660Sstevel@tonic-gate 				tdp->t_type = TYPEDEF;
3670Sstevel@tonic-gate 				tdp->t_tdesc = rtdp;
3680Sstevel@tonic-gate 				addhash(tdp, tid); /* for *(x,y) types */
3690Sstevel@tonic-gate 				parse_debug(3, NULL, "    %s defined as %s(%d)",
3701882Sjohnlev 				    idp->ii_name, tdesc_name(rtdp), tid);
3710Sstevel@tonic-gate 			} else if (rtdp->t_name == NULL) {
3720Sstevel@tonic-gate 				rtdp->t_name = xstrdup(idp->ii_name);
3730Sstevel@tonic-gate 				addhash(rtdp, tid);
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 	} else {
3770Sstevel@tonic-gate 		rtdp = xcalloc(sizeof (*rtdp));
3780Sstevel@tonic-gate 		rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
3790Sstevel@tonic-gate 		addhash(rtdp, tid);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	switch (*cp++) {
3830Sstevel@tonic-gate 	case 's':
3840Sstevel@tonic-gate 		(void) soudef(cp, STRUCT, &rtdp);
3850Sstevel@tonic-gate 		break;
3860Sstevel@tonic-gate 	case 'u':
3870Sstevel@tonic-gate 		(void) soudef(cp, UNION, &rtdp);
3880Sstevel@tonic-gate 		break;
3890Sstevel@tonic-gate 	case 'e':
3900Sstevel@tonic-gate 		enumdef(cp, &rtdp);
3910Sstevel@tonic-gate 		break;
3920Sstevel@tonic-gate 	default:
3930Sstevel@tonic-gate 		expected("parse_sou", "<tag type s/u/e>", cp - 1);
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	idp->ii_type = II_SOU;
3980Sstevel@tonic-gate 	idp->ii_dtype = rtdp;
3990Sstevel@tonic-gate 	return (II_SOU);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate int
parse_stab(stab_t * stab,char * cp,iidesc_t ** iidescp)4030Sstevel@tonic-gate parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	iidesc_t *ii = NULL;
4060Sstevel@tonic-gate 	iitype_t (*parse)(char *, iidesc_t *);
4070Sstevel@tonic-gate 	int rc;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * set up for reset()
4110Sstevel@tonic-gate 	 */
4120Sstevel@tonic-gate 	if (setjmp(resetbuf))
4130Sstevel@tonic-gate 		return (-1);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	cp = whitesp(cp);
4160Sstevel@tonic-gate 	ii = iidesc_new(NULL);
4170Sstevel@tonic-gate 	cp = name(cp, &ii->ii_name);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	switch (stab->n_type) {
4200Sstevel@tonic-gate 	case N_FUN:
4210Sstevel@tonic-gate 		parse = parse_fun;
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	case N_LSYM:
4250Sstevel@tonic-gate 		if (*cp == 't')
4260Sstevel@tonic-gate 			parse = parse_type;
4270Sstevel@tonic-gate 		else if (*cp == 'T')
4280Sstevel@tonic-gate 			parse = parse_sou;
4290Sstevel@tonic-gate 		else
4300Sstevel@tonic-gate 			parse = parse_sym;
4310Sstevel@tonic-gate 		break;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	case N_GSYM:
4340Sstevel@tonic-gate 	case N_LCSYM:
4350Sstevel@tonic-gate 	case N_PSYM:
4360Sstevel@tonic-gate 	case N_ROSYM:
4370Sstevel@tonic-gate 	case N_RSYM:
4380Sstevel@tonic-gate 	case N_STSYM:
4390Sstevel@tonic-gate 		parse = parse_sym;
4400Sstevel@tonic-gate 		break;
4410Sstevel@tonic-gate 	default:
4420Sstevel@tonic-gate 		parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
4430Sstevel@tonic-gate 		bzero(&resetbuf, sizeof (resetbuf));
4440Sstevel@tonic-gate 		return (-1);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	rc = parse(cp, ii);
4480Sstevel@tonic-gate 	bzero(&resetbuf, sizeof (resetbuf));
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (rc < 0 || ii->ii_type == II_NOT) {
4510Sstevel@tonic-gate 		iidesc_free(ii, NULL);
4520Sstevel@tonic-gate 		return (rc);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	*iidescp = ii;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	return (1);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Check if we have this node in the hash table already
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate tdesc_t *
lookup(int h)4640Sstevel@tonic-gate lookup(int h)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	int bucket = HASH(h);
4670Sstevel@tonic-gate 	tdesc_t *tdp = hash_table[bucket];
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	while (tdp != NULL) {
4700Sstevel@tonic-gate 		if (tdp->t_id == h)
4710Sstevel@tonic-gate 			return (tdp);
4720Sstevel@tonic-gate 		tdp = tdp->t_hash;
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	return (NULL);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static char *
whitesp(char * cp)4780Sstevel@tonic-gate whitesp(char *cp)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	char c;
4810Sstevel@tonic-gate 
4827230Ssn199410 	for (c = *cp++; isspace(c); c = *cp++)
4837230Ssn199410 		;
4840Sstevel@tonic-gate 	--cp;
4850Sstevel@tonic-gate 	return (cp);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate static char *
name(char * cp,char ** w)4890Sstevel@tonic-gate name(char *cp, char **w)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	char *new, *orig, c;
4920Sstevel@tonic-gate 	int len;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	orig = cp;
4950Sstevel@tonic-gate 	c = *cp++;
4960Sstevel@tonic-gate 	if (c == ':')
4970Sstevel@tonic-gate 		*w = NULL;
4987230Ssn199410 	else if (isalpha(c) || strchr("_.$#", c)) {
4997230Ssn199410 		for (c = *cp++; isalnum(c) || strchr(" _.$#", c); c = *cp++)
5000Sstevel@tonic-gate 			;
5010Sstevel@tonic-gate 		if (c != ':')
5020Sstevel@tonic-gate 			reset();
5030Sstevel@tonic-gate 		len = cp - orig;
5040Sstevel@tonic-gate 		new = xmalloc(len);
5050Sstevel@tonic-gate 		while (orig < cp - 1)
5060Sstevel@tonic-gate 			*new++ = *orig++;
5070Sstevel@tonic-gate 		*new = '\0';
5080Sstevel@tonic-gate 		*w = new - (len - 1);
5090Sstevel@tonic-gate 	} else
5100Sstevel@tonic-gate 		reset();
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	return (cp);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate static char *
number(char * cp,int * n)5160Sstevel@tonic-gate number(char *cp, int *n)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	char *next;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	*n = (int)strtol(cp, &next, 10);
5210Sstevel@tonic-gate 	if (next == cp)
5220Sstevel@tonic-gate 		expected("number", "<number>", cp);
5230Sstevel@tonic-gate 	return (next);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static char *
id(char * cp,int * h)5270Sstevel@tonic-gate id(char *cp, int *h)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	int n1, n2;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (*cp == '(') {	/* SunPro style */
5320Sstevel@tonic-gate 		cp++;
5330Sstevel@tonic-gate 		cp = number(cp, &n1);
5340Sstevel@tonic-gate 		if (*cp++ != ',')
5350Sstevel@tonic-gate 			expected("id", ",", cp - 1);
5360Sstevel@tonic-gate 		cp = number(cp, &n2);
5370Sstevel@tonic-gate 		if (*cp++ != ')')
5380Sstevel@tonic-gate 			expected("id", ")", cp - 1);
5390Sstevel@tonic-gate 		*h = MAKETYPEID(n1, n2);
5400Sstevel@tonic-gate 	} else if (isdigit(*cp)) { /* gcc style */
5410Sstevel@tonic-gate 		cp = number(cp, &n1);
5420Sstevel@tonic-gate 		*h = n1;
5430Sstevel@tonic-gate 	} else {
5440Sstevel@tonic-gate 		expected("id", "(/0-9", cp);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	return (cp);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate static int
tagadd(char * w,int h,tdesc_t * tdp)5500Sstevel@tonic-gate tagadd(char *w, int h, tdesc_t *tdp)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	tdesc_t *otdp;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	tdp->t_name = w;
5550Sstevel@tonic-gate 	if (!(otdp = lookup(h)))
5560Sstevel@tonic-gate 		addhash(tdp, h);
5570Sstevel@tonic-gate 	else if (otdp != tdp) {
5580Sstevel@tonic-gate 		warning("duplicate entry\n");
5591882Sjohnlev 		warning("  old: %s %d (%d,%d)\n", tdesc_name(otdp),
5600Sstevel@tonic-gate 		    otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
5611882Sjohnlev 		warning("  new: %s %d (%d,%d)\n", tdesc_name(tdp),
5620Sstevel@tonic-gate 		    tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
5630Sstevel@tonic-gate 		return (-1);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	return (0);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate static char *
tdefdecl(char * cp,int h,tdesc_t ** rtdp)5700Sstevel@tonic-gate tdefdecl(char *cp, int h, tdesc_t **rtdp)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	tdesc_t *ntdp;
5730Sstevel@tonic-gate 	char *w;
5740Sstevel@tonic-gate 	int c, h2;
5750Sstevel@tonic-gate 	char type;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	parse_debug(3, cp, "tdefdecl h=%d", h);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/* Type codes */
5800Sstevel@tonic-gate 	switch (type = *cp) {
5810Sstevel@tonic-gate 	case 'b': /* integer */
5820Sstevel@tonic-gate 	case 'R': /* fp */
5830Sstevel@tonic-gate 		cp = intrinsic(cp, rtdp);
5840Sstevel@tonic-gate 		break;
5850Sstevel@tonic-gate 	case '(': /* equiv to another type */
5860Sstevel@tonic-gate 		cp = id(cp, &h2);
5870Sstevel@tonic-gate 		ntdp = lookup(h2);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		if (ntdp != NULL && *cp == '=') {
5900Sstevel@tonic-gate 			if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
5910Sstevel@tonic-gate 				/*
5920Sstevel@tonic-gate 				 * The 6.2 compiler, and possibly others, will
5930Sstevel@tonic-gate 				 * sometimes emit the same stab for a forward
5940Sstevel@tonic-gate 				 * declaration twice.  That is, "(1,2)=xsfoo:"
5950Sstevel@tonic-gate 				 * will sometimes show up in two different
5960Sstevel@tonic-gate 				 * places.  This is, of course, quite fun.  We
5970Sstevel@tonic-gate 				 * want CTF to work in spite of the compiler,
5980Sstevel@tonic-gate 				 * so we'll let this one through.
5990Sstevel@tonic-gate 				 */
6000Sstevel@tonic-gate 				char *c2 = cp + 2;
6010Sstevel@tonic-gate 				char *nm;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 				if (!strchr("sue", *c2++)) {
6040Sstevel@tonic-gate 					expected("tdefdecl/x-redefine", "[sue]",
6050Sstevel@tonic-gate 					    c2 - 1);
6060Sstevel@tonic-gate 				}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 				c2 = name(c2, &nm);
6090Sstevel@tonic-gate 				if (strcmp(nm, ntdp->t_name) != 0) {
6100Sstevel@tonic-gate 					terminate("Stabs error: Attempt to "
6110Sstevel@tonic-gate 					    "redefine type (%d,%d) as "
6120Sstevel@tonic-gate 					    "something else: %s\n",
6130Sstevel@tonic-gate 					    TYPEFILE(h2), TYPENUM(h2),
6140Sstevel@tonic-gate 					    c2 - 1);
6150Sstevel@tonic-gate 				}
6160Sstevel@tonic-gate 				free(nm);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 				h2 = faketypenumber++;
6190Sstevel@tonic-gate 				ntdp = NULL;
6200Sstevel@tonic-gate 			} else {
6210Sstevel@tonic-gate 				terminate("Stabs error: Attempting to "
6220Sstevel@tonic-gate 				    "redefine type (%d,%d)\n", TYPEFILE(h2),
6230Sstevel@tonic-gate 				    TYPENUM(h2));
6240Sstevel@tonic-gate 			}
6250Sstevel@tonic-gate 		}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
6280Sstevel@tonic-gate 			if (*cp != '=') {
6290Sstevel@tonic-gate 				/* record it as unresolved */
6300Sstevel@tonic-gate 				parse_debug(3, NULL, "tdefdecl unres type %d",
6310Sstevel@tonic-gate 				    h2);
6320Sstevel@tonic-gate 				*rtdp = calloc(sizeof (**rtdp), 1);
6330Sstevel@tonic-gate 				(*rtdp)->t_type = TYPEDEF_UNRES;
6340Sstevel@tonic-gate 				(*rtdp)->t_id = h2;
6350Sstevel@tonic-gate 				break;
6360Sstevel@tonic-gate 			} else
6370Sstevel@tonic-gate 				cp++;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 			/* define a new type */
6400Sstevel@tonic-gate 			cp = tdefdecl(cp, h2, rtdp);
6410Sstevel@tonic-gate 			if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
6420Sstevel@tonic-gate 				ntdp = calloc(sizeof (*ntdp), 1);
6430Sstevel@tonic-gate 				ntdp->t_type = TYPEDEF;
6440Sstevel@tonic-gate 				ntdp->t_tdesc = *rtdp;
6450Sstevel@tonic-gate 				*rtdp = ntdp;
6460Sstevel@tonic-gate 			}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 			addhash(*rtdp, h2);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 		} else { /* that type is already defined */
6510Sstevel@tonic-gate 			if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
6520Sstevel@tonic-gate 				*rtdp = ntdp;
6530Sstevel@tonic-gate 			} else {
6540Sstevel@tonic-gate 				parse_debug(3, NULL,
6550Sstevel@tonic-gate 				    "No duplicate typedef anon for ref");
6560Sstevel@tonic-gate 				*rtdp = ntdp;
6570Sstevel@tonic-gate 			}
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 		break;
6600Sstevel@tonic-gate 	case '*':
6610Sstevel@tonic-gate 		ntdp = NULL;
6620Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
6630Sstevel@tonic-gate 		if (ntdp == NULL)
6640Sstevel@tonic-gate 			expected("tdefdecl/*", "id", cp);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 		if (!ntdp->t_id)
6670Sstevel@tonic-gate 			ntdp->t_id = faketypenumber++;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
6700Sstevel@tonic-gate 		(*rtdp)->t_type = POINTER;
6710Sstevel@tonic-gate 		(*rtdp)->t_size = 0;
6720Sstevel@tonic-gate 		(*rtdp)->t_id = h;
6730Sstevel@tonic-gate 		(*rtdp)->t_tdesc = ntdp;
6740Sstevel@tonic-gate 		break;
6750Sstevel@tonic-gate 	case 'f':
6760Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
6770Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
6780Sstevel@tonic-gate 		(*rtdp)->t_type = FUNCTION;
6790Sstevel@tonic-gate 		(*rtdp)->t_size = 0;
6800Sstevel@tonic-gate 		(*rtdp)->t_id = h;
6810Sstevel@tonic-gate 		(*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * The 6.1 compiler will sometimes generate incorrect stabs for
6840Sstevel@tonic-gate 		 * function pointers (it'll get the return type wrong).  This
6850Sstevel@tonic-gate 		 * causes merges to fail.  We therefore treat function pointers
6860Sstevel@tonic-gate 		 * as if they all point to functions that return int.  When
6870Sstevel@tonic-gate 		 * 4432549 is fixed, the lookupname() call below should be
6880Sstevel@tonic-gate 		 * replaced with `ntdp'.
6890Sstevel@tonic-gate 		 */
6900Sstevel@tonic-gate 		(*rtdp)->t_fndef->fn_ret = lookupname("int");
6910Sstevel@tonic-gate 		break;
6920Sstevel@tonic-gate 	case 'a':
6930Sstevel@tonic-gate 	case 'z':
6940Sstevel@tonic-gate 		cp++;
6950Sstevel@tonic-gate 		if (*cp++ != 'r')
6960Sstevel@tonic-gate 			expected("tdefdecl/[az]", "r", cp - 1);
6970Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
6980Sstevel@tonic-gate 		(*rtdp)->t_type = ARRAY;
6990Sstevel@tonic-gate 		(*rtdp)->t_id = h;
7000Sstevel@tonic-gate 		cp = arraydef(cp, rtdp);
7010Sstevel@tonic-gate 		break;
7020Sstevel@tonic-gate 	case 'x':
7030Sstevel@tonic-gate 		c = *++cp;
7040Sstevel@tonic-gate 		if (c != 's' && c != 'u' && c != 'e')
7050Sstevel@tonic-gate 			expected("tdefdecl/x", "[sue]", cp - 1);
7060Sstevel@tonic-gate 		cp = name(cp + 1, &w);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		ntdp = xcalloc(sizeof (*ntdp));
7090Sstevel@tonic-gate 		ntdp->t_type = FORWARD;
7100Sstevel@tonic-gate 		ntdp->t_name = w;
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * We explicitly don't set t_id here - the caller will do it.
7130Sstevel@tonic-gate 		 * The caller may want to use a real type ID, or they may
7140Sstevel@tonic-gate 		 * choose to make one up.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		*rtdp = ntdp;
7180Sstevel@tonic-gate 		break;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	case 'B': /* volatile */
7210Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (!ntdp->t_id)
7240Sstevel@tonic-gate 			ntdp->t_id = faketypenumber++;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
7270Sstevel@tonic-gate 		(*rtdp)->t_type = VOLATILE;
7280Sstevel@tonic-gate 		(*rtdp)->t_size = 0;
7290Sstevel@tonic-gate 		(*rtdp)->t_tdesc = ntdp;
7300Sstevel@tonic-gate 		(*rtdp)->t_id = h;
7310Sstevel@tonic-gate 		break;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	case 'k': /* const */
7340Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		if (!ntdp->t_id)
7370Sstevel@tonic-gate 			ntdp->t_id = faketypenumber++;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
7400Sstevel@tonic-gate 		(*rtdp)->t_type = CONST;
7410Sstevel@tonic-gate 		(*rtdp)->t_size = 0;
7420Sstevel@tonic-gate 		(*rtdp)->t_tdesc = ntdp;
7430Sstevel@tonic-gate 		(*rtdp)->t_id = h;
7440Sstevel@tonic-gate 		break;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	case 'K': /* restricted */
7470Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		if (!ntdp->t_id)
7500Sstevel@tonic-gate 			ntdp->t_id = faketypenumber++;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
7530Sstevel@tonic-gate 		(*rtdp)->t_type = RESTRICT;
7540Sstevel@tonic-gate 		(*rtdp)->t_size = 0;
7550Sstevel@tonic-gate 		(*rtdp)->t_tdesc = ntdp;
7560Sstevel@tonic-gate 		(*rtdp)->t_id = h;
7570Sstevel@tonic-gate 		break;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	case 'u':
7600Sstevel@tonic-gate 	case 's':
7610Sstevel@tonic-gate 		cp++;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		*rtdp = xcalloc(sizeof (**rtdp));
7640Sstevel@tonic-gate 		(*rtdp)->t_name = NULL;
7650Sstevel@tonic-gate 		cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
7660Sstevel@tonic-gate 		break;
7670Sstevel@tonic-gate 	default:
7680Sstevel@tonic-gate 		expected("tdefdecl", "<type code>", cp);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	return (cp);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate static char *
intrinsic(char * cp,tdesc_t ** rtdp)7740Sstevel@tonic-gate intrinsic(char *cp, tdesc_t **rtdp)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	intr_t *intr = xcalloc(sizeof (intr_t));
7770Sstevel@tonic-gate 	tdesc_t *tdp;
7780Sstevel@tonic-gate 	int width, fmt, i;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	switch (*cp++) {
7810Sstevel@tonic-gate 	case 'b':
7820Sstevel@tonic-gate 		intr->intr_type = INTR_INT;
7830Sstevel@tonic-gate 		if (*cp == 's')
7840Sstevel@tonic-gate 			intr->intr_signed = 1;
7850Sstevel@tonic-gate 		else if (*cp != 'u')
7860Sstevel@tonic-gate 			expected("intrinsic/b", "[su]", cp);
7870Sstevel@tonic-gate 		cp++;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 		if (strchr("cbv", *cp))
7900Sstevel@tonic-gate 			intr->intr_iformat = *cp++;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		cp = number(cp, &width);
7930Sstevel@tonic-gate 		if (*cp++ != ';')
7940Sstevel@tonic-gate 			expected("intrinsic/b", "; (post-width)", cp - 1);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		cp = number(cp, &intr->intr_offset);
7970Sstevel@tonic-gate 		if (*cp++ != ';')
7980Sstevel@tonic-gate 			expected("intrinsic/b", "; (post-offset)", cp - 1);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 		cp = number(cp, &intr->intr_nbits);
8010Sstevel@tonic-gate 		break;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	case 'R':
8040Sstevel@tonic-gate 		intr->intr_type = INTR_REAL;
8050Sstevel@tonic-gate 		for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
8060Sstevel@tonic-gate 			fmt = fmt * 10 + (*(cp + i) - '0');
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		if (fmt < 1 || fmt > CTF_FP_MAX)
8090Sstevel@tonic-gate 			expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 		intr->intr_fformat = fmt;
8120Sstevel@tonic-gate 		cp += i;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		if (*cp++ != ';')
8150Sstevel@tonic-gate 			expected("intrinsic/R", ";", cp - 1);
8160Sstevel@tonic-gate 		cp = number(cp, &width);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		intr->intr_nbits = width * 8;
8190Sstevel@tonic-gate 		break;
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	tdp = xcalloc(sizeof (*tdp));
8230Sstevel@tonic-gate 	tdp->t_type = INTRINSIC;
8240Sstevel@tonic-gate 	tdp->t_size = width;
8250Sstevel@tonic-gate 	tdp->t_name = NULL;
8260Sstevel@tonic-gate 	tdp->t_intr = intr;
8270Sstevel@tonic-gate 	parse_debug(3, NULL, "intrinsic: size=%d", width);
8280Sstevel@tonic-gate 	*rtdp = tdp;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	return (cp);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static tdesc_t *
bitintrinsic(tdesc_t * template,int nbits)8340Sstevel@tonic-gate bitintrinsic(tdesc_t *template, int nbits)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	newtdp->t_name = xstrdup(template->t_name);
8390Sstevel@tonic-gate 	newtdp->t_id = faketypenumber++;
8400Sstevel@tonic-gate 	newtdp->t_type = INTRINSIC;
8410Sstevel@tonic-gate 	newtdp->t_size = template->t_size;
8420Sstevel@tonic-gate 	newtdp->t_intr = xmalloc(sizeof (intr_t));
8430Sstevel@tonic-gate 	bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
8440Sstevel@tonic-gate 	newtdp->t_intr->intr_nbits = nbits;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	return (newtdp);
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate static char *
offsize(char * cp,mlist_t * mlp)8500Sstevel@tonic-gate offsize(char *cp, mlist_t *mlp)
8510Sstevel@tonic-gate {
8520Sstevel@tonic-gate 	int offset, size;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if (*cp == ',')
8550Sstevel@tonic-gate 		cp++;
8560Sstevel@tonic-gate 	cp = number(cp, &offset);
8570Sstevel@tonic-gate 	if (*cp++ != ',')
8580Sstevel@tonic-gate 		expected("offsize/2", ",", cp - 1);
8590Sstevel@tonic-gate 	cp = number(cp, &size);
8600Sstevel@tonic-gate 	if (*cp++ != ';')
8610Sstevel@tonic-gate 		expected("offsize/3", ";", cp - 1);
8620Sstevel@tonic-gate 	mlp->ml_offset = offset;
8630Sstevel@tonic-gate 	mlp->ml_size = size;
8640Sstevel@tonic-gate 	return (cp);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate static tdesc_t *
find_intrinsic(tdesc_t * tdp)8680Sstevel@tonic-gate find_intrinsic(tdesc_t *tdp)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	for (;;) {
8710Sstevel@tonic-gate 		switch (tdp->t_type) {
8720Sstevel@tonic-gate 		case TYPEDEF:
8730Sstevel@tonic-gate 		case VOLATILE:
8740Sstevel@tonic-gate 		case CONST:
8750Sstevel@tonic-gate 		case RESTRICT:
8760Sstevel@tonic-gate 			tdp = tdp->t_tdesc;
8770Sstevel@tonic-gate 			break;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		default:
8800Sstevel@tonic-gate 			return (tdp);
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate static char *
soudef(char * cp,stabtype_t type,tdesc_t ** rtdp)8860Sstevel@tonic-gate soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	mlist_t *mlp, **prev;
8890Sstevel@tonic-gate 	char *w;
8900Sstevel@tonic-gate 	int h;
8910Sstevel@tonic-gate 	int size;
8920Sstevel@tonic-gate 	tdesc_t *tdp, *itdp;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	cp = number(cp, &size);
8950Sstevel@tonic-gate 	(*rtdp)->t_size = size;
8960Sstevel@tonic-gate 	(*rtdp)->t_type = type; /* s or u */
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8990Sstevel@tonic-gate 	 * An '@' here indicates a bitmask follows.   This is so the
9000Sstevel@tonic-gate 	 * compiler can pass information to debuggers about how structures
9010Sstevel@tonic-gate 	 * are passed in the v9 world.  We don't need this information
9020Sstevel@tonic-gate 	 * so we skip over it.
9030Sstevel@tonic-gate 	 */
9040Sstevel@tonic-gate 	if (cp[0] == '@') {
9050Sstevel@tonic-gate 		cp += 3;
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9081882Sjohnlev 	parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
9090Sstevel@tonic-gate 	    (*rtdp)->t_size);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	prev = &((*rtdp)->t_members);
9120Sstevel@tonic-gate 	/* now fill up the fields */
9130Sstevel@tonic-gate 	while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
9140Sstevel@tonic-gate 		mlp = xcalloc(sizeof (*mlp));
9150Sstevel@tonic-gate 		*prev = mlp;
9160Sstevel@tonic-gate 		cp = name(cp, &w);
9170Sstevel@tonic-gate 		mlp->ml_name = w;
9180Sstevel@tonic-gate 		cp = id(cp, &h);
9190Sstevel@tonic-gate 		/*
9200Sstevel@tonic-gate 		 * find the tdesc struct in the hash table for this type
9210Sstevel@tonic-gate 		 * and stick a ptr in here
9220Sstevel@tonic-gate 		 */
9230Sstevel@tonic-gate 		tdp = lookup(h);
9240Sstevel@tonic-gate 		if (tdp == NULL) { /* not in hash list */
9250Sstevel@tonic-gate 			parse_debug(3, NULL, "      defines %s (%d)", w, h);
9260Sstevel@tonic-gate 			if (*cp++ != '=') {
9270Sstevel@tonic-gate 				tdp = unres_new(h);
9280Sstevel@tonic-gate 				parse_debug(3, NULL,
9290Sstevel@tonic-gate 				    "      refers to %s (unresolved %d)",
9300Sstevel@tonic-gate 				    (w ? w : "anon"), h);
9310Sstevel@tonic-gate 			} else {
9320Sstevel@tonic-gate 				cp = tdefdecl(cp, h, &tdp);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 				if (tdp->t_id && tdp->t_id != h) {
9350Sstevel@tonic-gate 					tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 					ntdp->t_type = TYPEDEF;
9380Sstevel@tonic-gate 					ntdp->t_tdesc = tdp;
9390Sstevel@tonic-gate 					tdp = ntdp;
9400Sstevel@tonic-gate 				}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 				addhash(tdp, h);
9430Sstevel@tonic-gate 				parse_debug(4, cp,
9440Sstevel@tonic-gate 				    "     soudef now looking at    ");
9450Sstevel@tonic-gate 				cp++;
9460Sstevel@tonic-gate 			}
9470Sstevel@tonic-gate 		} else {
9480Sstevel@tonic-gate 			parse_debug(3, NULL, "      refers to %s (%d, %s)",
9491882Sjohnlev 			    w ? w : "anon", h, tdesc_name(tdp));
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		cp = offsize(cp, mlp);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		itdp = find_intrinsic(tdp);
9550Sstevel@tonic-gate 		if (itdp->t_type == INTRINSIC) {
9560Sstevel@tonic-gate 			if (mlp->ml_size != itdp->t_intr->intr_nbits) {
9570Sstevel@tonic-gate 				parse_debug(4, cp, "making %d bit intrinsic "
9581882Sjohnlev 				    "from %s", mlp->ml_size, tdesc_name(itdp));
9590Sstevel@tonic-gate 				mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
9600Sstevel@tonic-gate 			} else
9610Sstevel@tonic-gate 				mlp->ml_type = tdp;
9620Sstevel@tonic-gate 		} else if (itdp->t_type == TYPEDEF_UNRES) {
9630Sstevel@tonic-gate 			list_add(&typedbitfldmems, mlp);
9640Sstevel@tonic-gate 			mlp->ml_type = tdp;
9650Sstevel@tonic-gate 		} else {
9660Sstevel@tonic-gate 			mlp->ml_type = tdp;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		/* cp is now pointing to next field */
9700Sstevel@tonic-gate 		prev = &mlp->ml_next;
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 	return (cp);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate static char *
arraydef(char * cp,tdesc_t ** rtdp)9760Sstevel@tonic-gate arraydef(char *cp, tdesc_t **rtdp)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	int start, end, h;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	cp = id(cp, &h);
9810Sstevel@tonic-gate 	if (*cp++ != ';')
9820Sstevel@tonic-gate 		expected("arraydef/1", ";", cp - 1);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	(*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
9850Sstevel@tonic-gate 	(*rtdp)->t_ardef->ad_idxtype = lookup(h);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	cp = number(cp, &start); /* lower */
9880Sstevel@tonic-gate 	if (*cp++ != ';')
9890Sstevel@tonic-gate 		expected("arraydef/2", ";", cp - 1);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	if (*cp == 'S') {
992*12177SSurya.Prakki@Sun.COM 		/*
993*12177SSurya.Prakki@Sun.COM 		 * variable length array - treat as null dimensioned
994*12177SSurya.Prakki@Sun.COM 		 *
995*12177SSurya.Prakki@Sun.COM 		 * For VLA variables on sparc, SS12 generated stab entry
996*12177SSurya.Prakki@Sun.COM 		 * looks as follows:
997*12177SSurya.Prakki@Sun.COM 		 * .stabs "buf:(0,28)=zr(0,4);0;S-12;(0,1)", 0x80, 0, 0, -16
998*12177SSurya.Prakki@Sun.COM 		 * Whereas SS12u1 generated stab entry looks like this:
999*12177SSurya.Prakki@Sun.COM 		 * .stabs "buf:(0,28)=zr(0,4);0;S0;(0,1)", 0x80, 0, 0, 0
1000*12177SSurya.Prakki@Sun.COM 		 * On x86, both versions generate the first type of entry.
1001*12177SSurya.Prakki@Sun.COM 		 * We should be able to parse both.
1002*12177SSurya.Prakki@Sun.COM 		 */
10030Sstevel@tonic-gate 		cp++;
1004*12177SSurya.Prakki@Sun.COM 		if (*cp == '-')
1005*12177SSurya.Prakki@Sun.COM 			cp++;
10060Sstevel@tonic-gate 		cp = number(cp, &end);
10070Sstevel@tonic-gate 		end = start;
10080Sstevel@tonic-gate 	} else {
1009*12177SSurya.Prakki@Sun.COM 		/*
1010*12177SSurya.Prakki@Sun.COM 		 * normal fixed-dimension array
1011*12177SSurya.Prakki@Sun.COM 		 * Stab entry for this looks as follows :
1012*12177SSurya.Prakki@Sun.COM 		 * .stabs "x:(0,28)=ar(0,4);0;9;(0,3)", 0x80, 0, 40, 0
1013*12177SSurya.Prakki@Sun.COM 		 */
10140Sstevel@tonic-gate 		cp = number(cp, &end);  /* upper */
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	if (*cp++ != ';')
10180Sstevel@tonic-gate 		expected("arraydef/3", ";", cp - 1);
10190Sstevel@tonic-gate 	(*rtdp)->t_ardef->ad_nelems = end - start + 1;
10200Sstevel@tonic-gate 	cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	parse_debug(3, cp, "defined array idx type %d %d-%d next ",
10230Sstevel@tonic-gate 	    h, start, end);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	return (cp);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate static void
enumdef(char * cp,tdesc_t ** rtdp)10290Sstevel@tonic-gate enumdef(char *cp, tdesc_t **rtdp)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	elist_t *elp, **prev;
10320Sstevel@tonic-gate 	char *w;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	(*rtdp)->t_type = ENUM;
10350Sstevel@tonic-gate 	(*rtdp)->t_emem = NULL;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	prev = &((*rtdp)->t_emem);
10380Sstevel@tonic-gate 	while (*cp != ';') {
10390Sstevel@tonic-gate 		elp = xcalloc(sizeof (*elp));
10400Sstevel@tonic-gate 		elp->el_next = NULL;
10410Sstevel@tonic-gate 		*prev = elp;
10420Sstevel@tonic-gate 		cp = name(cp, &w);
10430Sstevel@tonic-gate 		elp->el_name = w;
10440Sstevel@tonic-gate 		cp = number(cp, &elp->el_number);
10451882Sjohnlev 		parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
10460Sstevel@tonic-gate 		    elp->el_name, elp->el_number);
10470Sstevel@tonic-gate 		prev = &elp->el_next;
10480Sstevel@tonic-gate 		if (*cp++ != ',')
10490Sstevel@tonic-gate 			expected("enumdef", ",", cp - 1);
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate tdesc_t *
lookup_name(tdesc_t ** hash,const char * name)10542722Sjohnlev lookup_name(tdesc_t **hash, const char *name)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	int bucket = compute_sum(name);
10570Sstevel@tonic-gate 	tdesc_t *tdp, *ttdp = NULL;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
10600Sstevel@tonic-gate 		if (tdp->t_name != NULL && strcmp(tdp->t_name, name) == 0) {
10610Sstevel@tonic-gate 			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
10620Sstevel@tonic-gate 			    tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
10630Sstevel@tonic-gate 				return (tdp);
10640Sstevel@tonic-gate 			if (tdp->t_type == TYPEDEF)
10650Sstevel@tonic-gate 				ttdp = tdp;
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 	return (ttdp);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate tdesc_t *
lookupname(const char * name)10722722Sjohnlev lookupname(const char *name)
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	return (lookup_name(name_table, name));
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate  * Add a node to the hash queues.
10790Sstevel@tonic-gate  */
10800Sstevel@tonic-gate static void
addhash(tdesc_t * tdp,int num)10810Sstevel@tonic-gate addhash(tdesc_t *tdp, int num)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	int hash = HASH(num);
10840Sstevel@tonic-gate 	tdesc_t *ttdp;
10850Sstevel@tonic-gate 	char added_num = 0, added_name = 0;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	/*
10880Sstevel@tonic-gate 	 * If it already exists in the hash table don't add it again
10890Sstevel@tonic-gate 	 * (but still check to see if the name should be hashed).
10900Sstevel@tonic-gate 	 */
10910Sstevel@tonic-gate 	ttdp = lookup(num);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (ttdp == NULL) {
10940Sstevel@tonic-gate 		tdp->t_id = num;
10950Sstevel@tonic-gate 		tdp->t_hash = hash_table[hash];
10960Sstevel@tonic-gate 		hash_table[hash] = tdp;
10970Sstevel@tonic-gate 		added_num = 1;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (tdp->t_name != NULL) {
11010Sstevel@tonic-gate 		ttdp = lookupname(tdp->t_name);
11020Sstevel@tonic-gate 		if (ttdp == NULL) {
11030Sstevel@tonic-gate 			hash = compute_sum(tdp->t_name);
11040Sstevel@tonic-gate 			tdp->t_next = name_table[hash];
11050Sstevel@tonic-gate 			name_table[hash] = tdp;
11060Sstevel@tonic-gate 			added_name = 1;
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	if (!added_num && !added_name) {
11100Sstevel@tonic-gate 		terminate("stabs: broken hash\n");
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate static int
compute_sum(const char * w)11152722Sjohnlev compute_sum(const char *w)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	char c;
11180Sstevel@tonic-gate 	int sum;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	for (sum = 0; (c = *w) != '\0'; sum += c, w++)
11210Sstevel@tonic-gate 		;
11220Sstevel@tonic-gate 	return (HASH(sum));
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate static void
reset(void)11260Sstevel@tonic-gate reset(void)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate 	longjmp(resetbuf, 1);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate void
check_hash(void)11320Sstevel@tonic-gate check_hash(void)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	tdesc_t *tdp;
11350Sstevel@tonic-gate 	int i;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	printf("checking hash\n");
11380Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++) {
11390Sstevel@tonic-gate 		if (hash_table[i]) {
11400Sstevel@tonic-gate 			for (tdp = hash_table[i]->t_hash;
11410Sstevel@tonic-gate 			    tdp && tdp != hash_table[i];
11420Sstevel@tonic-gate 			    tdp = tdp->t_hash)
11430Sstevel@tonic-gate 				continue;
11440Sstevel@tonic-gate 			if (tdp) {
11450Sstevel@tonic-gate 				terminate("cycle in hash bucket %d\n", i);
11460Sstevel@tonic-gate 				return;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		if (name_table[i]) {
11510Sstevel@tonic-gate 			for (tdp = name_table[i]->t_next;
11520Sstevel@tonic-gate 			    tdp && tdp != name_table[i];
11530Sstevel@tonic-gate 			    tdp = tdp->t_next)
11540Sstevel@tonic-gate 				continue;
11550Sstevel@tonic-gate 			if (tdp) {
11560Sstevel@tonic-gate 				terminate("cycle in name bucket %d\n", i);
11570Sstevel@tonic-gate 				return;
11580Sstevel@tonic-gate 			}
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 	printf("done\n");
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /*ARGSUSED1*/
11650Sstevel@tonic-gate static int
resolve_typed_bitfields_cb(mlist_t * ml,void * private)11660Sstevel@tonic-gate resolve_typed_bitfields_cb(mlist_t *ml, void *private)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	tdesc_t *tdp = ml->ml_type;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	debug(3, "Resolving typed bitfields (member %s)\n",
11710Sstevel@tonic-gate 	    (ml->ml_name ? ml->ml_name : "(anon)"));
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	while (tdp) {
11740Sstevel@tonic-gate 		switch (tdp->t_type) {
11750Sstevel@tonic-gate 		case INTRINSIC:
11760Sstevel@tonic-gate 			if (ml->ml_size != tdp->t_intr->intr_nbits) {
11770Sstevel@tonic-gate 				debug(3, "making %d bit intrinsic from %s",
11781882Sjohnlev 				    ml->ml_size, tdesc_name(tdp));
11790Sstevel@tonic-gate 				ml->ml_type = bitintrinsic(tdp, ml->ml_size);
11800Sstevel@tonic-gate 			} else {
11810Sstevel@tonic-gate 				debug(3, "using existing %d bit %s intrinsic",
11821882Sjohnlev 				    ml->ml_size, tdesc_name(tdp));
11830Sstevel@tonic-gate 				ml->ml_type = tdp;
11840Sstevel@tonic-gate 			}
11850Sstevel@tonic-gate 			return (1);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		case POINTER:
11880Sstevel@tonic-gate 		case TYPEDEF:
11890Sstevel@tonic-gate 		case VOLATILE:
11900Sstevel@tonic-gate 		case CONST:
11910Sstevel@tonic-gate 		case RESTRICT:
11920Sstevel@tonic-gate 			tdp = tdp->t_tdesc;
11930Sstevel@tonic-gate 			break;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		default:
11960Sstevel@tonic-gate 			return (1);
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	terminate("type chain for bitfield member %s has a NULL", ml->ml_name);
12010Sstevel@tonic-gate 	/*NOTREACHED*/
12020Sstevel@tonic-gate 	return (0);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate void
resolve_typed_bitfields(void)12060Sstevel@tonic-gate resolve_typed_bitfields(void)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	(void) list_iter(typedbitfldmems,
12090Sstevel@tonic-gate 	    (int (*)())resolve_typed_bitfields_cb, NULL);
12100Sstevel@tonic-gate }
1211