xref: /onnv-gate/usr/src/tools/stabs/stabs.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1996-2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <limits.h>
30*0Sstevel@tonic-gate #include <stdarg.h>
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "stabs.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate static struct tdesc *hash_table[BUCKETS];
36*0Sstevel@tonic-gate static struct tdesc *name_table[BUCKETS];
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static void reset(void);
39*0Sstevel@tonic-gate static jmp_buf	resetbuf;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static char *get_line(void);
42*0Sstevel@tonic-gate static void parseline(char *cp);
43*0Sstevel@tonic-gate static char *soudef(char *cp, enum type type, struct tdesc **rtdp);
44*0Sstevel@tonic-gate static void enumdef(char *cp, struct tdesc **rtdp);
45*0Sstevel@tonic-gate static int compute_sum(char *w);
46*0Sstevel@tonic-gate static struct tdesc *lookup(int h);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static char *number(char *cp, int *n);
49*0Sstevel@tonic-gate static char *name(char *cp, char **w);
50*0Sstevel@tonic-gate static char *id(char *cp, int *h);
51*0Sstevel@tonic-gate static char *offsize(char *cp, struct mlist *mlp);
52*0Sstevel@tonic-gate static char *whitesp(char *cp);
53*0Sstevel@tonic-gate static void addhash(struct tdesc *tdp, int num);
54*0Sstevel@tonic-gate static void tagadd(char *w, int h, struct tdesc *tdp);
55*0Sstevel@tonic-gate static void tagdecl(char *cp, struct tdesc **rtdp, int h, char *w);
56*0Sstevel@tonic-gate static char *tdefdecl(char *cp, int h, struct tdesc **rtdp);
57*0Sstevel@tonic-gate static char *intrinsic(char *cp, struct tdesc **rtdp);
58*0Sstevel@tonic-gate static char *arraydef(char *cp, struct tdesc **rtdp);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static int line_number = 0;
61*0Sstevel@tonic-gate static int debug_line  = 0;
62*0Sstevel@tonic-gate static char linebuf[MAXLINE];
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate extern int debug_level;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static void
debug(int level,char * cp,char * fmt,...)67*0Sstevel@tonic-gate debug(int level, char *cp, char *fmt, ...)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	va_list ap;
70*0Sstevel@tonic-gate 	char buf[1024];
71*0Sstevel@tonic-gate 	char tmp[32];
72*0Sstevel@tonic-gate 	int i;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (level > debug_level)
75*0Sstevel@tonic-gate 		return;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if (cp != NULL) {
78*0Sstevel@tonic-gate 		for (i = 0; i < 30; i++) {
79*0Sstevel@tonic-gate 			if (cp[i] == '\0')
80*0Sstevel@tonic-gate 				break;
81*0Sstevel@tonic-gate 			if (!iscntrl(cp[i]))
82*0Sstevel@tonic-gate 				tmp[i] = cp[i];
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 		tmp[i] = '\0';
85*0Sstevel@tonic-gate 		(void) sprintf(buf, "%s [cp='%s']\n", fmt, tmp);
86*0Sstevel@tonic-gate 	} else {
87*0Sstevel@tonic-gate 		strcpy(buf, fmt);
88*0Sstevel@tonic-gate 		strcat(buf, "\n");
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	va_start(ap, fmt);
92*0Sstevel@tonic-gate 	(void) vfprintf(stderr, buf, ap);
93*0Sstevel@tonic-gate 	va_end(ap);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /* Report unexpected syntax in stabs. */
98*0Sstevel@tonic-gate static void
expected(char * who,char * what,char * where)99*0Sstevel@tonic-gate expected(
100*0Sstevel@tonic-gate 	char *who,	/* what function, or part thereof, is reporting */
101*0Sstevel@tonic-gate 	char *what,	/* what was expected */
102*0Sstevel@tonic-gate 	char *where)	/* where we were in the line of input */
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	fprintf(stderr, "%s, input line %d: expecting \"%s\" at \"%s\"\n",
105*0Sstevel@tonic-gate 		who, line_number, what, where);
106*0Sstevel@tonic-gate 	exit(1);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /* Read a line from stdin into linebuf and increment line_number. */
110*0Sstevel@tonic-gate static char *
get_line(void)111*0Sstevel@tonic-gate get_line(void)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	char *cp = fgets(linebuf, MAXLINE, stdin);
114*0Sstevel@tonic-gate 	line_number++;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/* For debugging, you can set debug_line to a line to stop on. */
117*0Sstevel@tonic-gate 	if (line_number == debug_line) {
118*0Sstevel@tonic-gate 		fprintf(stderr, "Hit debug line number %d\n", line_number);
119*0Sstevel@tonic-gate 		for (;;)
120*0Sstevel@tonic-gate 			sleep(1);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	return (cp);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /* Get the continuation of the current input line. */
126*0Sstevel@tonic-gate static char *
get_continuation(void)127*0Sstevel@tonic-gate get_continuation(void)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	char *cp = get_line();
130*0Sstevel@tonic-gate 	if (!cp) {
131*0Sstevel@tonic-gate 		fprintf(stderr, "expecting continuation line, "
132*0Sstevel@tonic-gate 		    "got end of input\n");
133*0Sstevel@tonic-gate 		exit(1);
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	/* Skip to the quoted stuff. */
137*0Sstevel@tonic-gate 	while (*cp++ != '"')
138*0Sstevel@tonic-gate 		;
139*0Sstevel@tonic-gate 	return (cp);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate void
parse_input(void)143*0Sstevel@tonic-gate parse_input(void)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	char *cp;
146*0Sstevel@tonic-gate 	int i = 0;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++) {
149*0Sstevel@tonic-gate 		hash_table[i] = NULL;
150*0Sstevel@tonic-gate 		name_table[i] = NULL;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	/*
154*0Sstevel@tonic-gate 	 * get a line at a time from the .s stabs file and parse.
155*0Sstevel@tonic-gate 	 */
156*0Sstevel@tonic-gate 	while ((cp = get_line()) != NULL)
157*0Sstevel@tonic-gate 		parseline(cp);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * Parse each line of the .s file (stabs entry) gather meaningful information
162*0Sstevel@tonic-gate  * like name of type, size, offsets of fields etc.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate static void
parseline(char * cp)165*0Sstevel@tonic-gate parseline(char *cp)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	struct tdesc *tdp;
168*0Sstevel@tonic-gate 	char c, *w;
169*0Sstevel@tonic-gate 	int h, tagdef;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	/*
172*0Sstevel@tonic-gate 	 * setup for reset()
173*0Sstevel@tonic-gate 	 */
174*0Sstevel@tonic-gate 	if (setjmp(resetbuf))
175*0Sstevel@tonic-gate 		return;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/*
178*0Sstevel@tonic-gate 	 * Look for lines of the form
179*0Sstevel@tonic-gate 	 *	.stabs	"str",n,n,n,n
180*0Sstevel@tonic-gate 	 * The part in '"' is then parsed.
181*0Sstevel@tonic-gate 	 */
182*0Sstevel@tonic-gate 	cp = whitesp(cp);
183*0Sstevel@tonic-gate #define	STLEN	6
184*0Sstevel@tonic-gate 	debug(2, cp, "parseline");
185*0Sstevel@tonic-gate 	if (strncmp(cp, ".stabs", STLEN) != 0)
186*0Sstevel@tonic-gate 		reset();
187*0Sstevel@tonic-gate 	cp += STLEN;
188*0Sstevel@tonic-gate #undef STLEN
189*0Sstevel@tonic-gate 	cp = whitesp(cp);
190*0Sstevel@tonic-gate 	if (*cp++ != '"')
191*0Sstevel@tonic-gate 		reset();
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	/*
194*0Sstevel@tonic-gate 	 * name:type		variable (ignored)
195*0Sstevel@tonic-gate 	 * name:ttype		typedef
196*0Sstevel@tonic-gate 	 * name:Ttype		struct tag define
197*0Sstevel@tonic-gate 	 */
198*0Sstevel@tonic-gate 	cp = whitesp(cp);
199*0Sstevel@tonic-gate 	cp = name(cp, &w);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	tagdef = 0;
202*0Sstevel@tonic-gate 	switch (c = *cp++) {
203*0Sstevel@tonic-gate 	case 't': /* type */
204*0Sstevel@tonic-gate 		break;
205*0Sstevel@tonic-gate 	case 'T': /* struct, union, enum */
206*0Sstevel@tonic-gate 		tagdef = 1;
207*0Sstevel@tonic-gate 		break;
208*0Sstevel@tonic-gate 	default:
209*0Sstevel@tonic-gate 		reset();
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/*
213*0Sstevel@tonic-gate 	 * The type id and definition follow.
214*0Sstevel@tonic-gate 	 */
215*0Sstevel@tonic-gate 	cp = id(cp, &h);
216*0Sstevel@tonic-gate 	if (*cp == '"') {
217*0Sstevel@tonic-gate 		struct tdesc *ntdp;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		cp++;
220*0Sstevel@tonic-gate 		ntdp = lookup(h);
221*0Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
222*0Sstevel@tonic-gate 			if (*cp++ != '=')  /* better be defining it now */
223*0Sstevel@tonic-gate 				expected("parseline/'0-9'", "=", cp - 1);
224*0Sstevel@tonic-gate 			cp = tdefdecl(cp, h, &tdp);
225*0Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
226*0Sstevel@tonic-gate 		} else { /* that type is already defined */
227*0Sstevel@tonic-gate 			tdp = malloc(sizeof (*tdp));
228*0Sstevel@tonic-gate 			tdp->type = TYPEOF;
229*0Sstevel@tonic-gate 			tdp->name = (w != NULL) ? strdup(w) : NULL;
230*0Sstevel@tonic-gate 			tdp->data.tdesc = ntdp;
231*0Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
232*0Sstevel@tonic-gate 			debug(3, NULL, "    %s defined as %s(%d)", w,
233*0Sstevel@tonic-gate 			    (ntdp->name != NULL) ? ntdp->name : "anon", h);
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 		return;
236*0Sstevel@tonic-gate 	} else if (*cp++ != '=') {
237*0Sstevel@tonic-gate 		expected("parseline", "=", cp - 1);
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	if (tagdef) {
240*0Sstevel@tonic-gate 		tagdecl(cp, &tdp, h, w);
241*0Sstevel@tonic-gate 	} else {
242*0Sstevel@tonic-gate 		tdefdecl(cp, h, &tdp);
243*0Sstevel@tonic-gate 		tagadd(w, h, tdp);
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * Check if we have this node in the hash table already
249*0Sstevel@tonic-gate  */
250*0Sstevel@tonic-gate static struct tdesc *
lookup(int h)251*0Sstevel@tonic-gate lookup(int h)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	int hash = HASH(h);
254*0Sstevel@tonic-gate 	struct tdesc *tdp = hash_table[hash];
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	while (tdp != NULL) {
257*0Sstevel@tonic-gate 		if (tdp->id == h)
258*0Sstevel@tonic-gate 			return (tdp);
259*0Sstevel@tonic-gate 		tdp = tdp->hash;
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 	return (NULL);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate static char *
whitesp(char * cp)265*0Sstevel@tonic-gate whitesp(char *cp)
266*0Sstevel@tonic-gate {
267*0Sstevel@tonic-gate 	char *orig, c;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	orig = cp;
270*0Sstevel@tonic-gate 	for (c = *cp++; isspace(c); c = *cp++)
271*0Sstevel@tonic-gate 		;
272*0Sstevel@tonic-gate 	--cp;
273*0Sstevel@tonic-gate 	return (cp);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate static char *
name(char * cp,char ** w)277*0Sstevel@tonic-gate name(char *cp, char **w)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate 	char *new, *orig, c;
280*0Sstevel@tonic-gate 	int len;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	orig = cp;
283*0Sstevel@tonic-gate 	c = *cp++;
284*0Sstevel@tonic-gate 	if (c == ':')
285*0Sstevel@tonic-gate 		*w = NULL;
286*0Sstevel@tonic-gate 	else if (isalpha(c) || c == '_') {
287*0Sstevel@tonic-gate 		for (c = *cp++; isalnum(c) || c == ' ' || c == '_'; c = *cp++)
288*0Sstevel@tonic-gate 			;
289*0Sstevel@tonic-gate 		if (c != ':')
290*0Sstevel@tonic-gate 			reset();
291*0Sstevel@tonic-gate 		len = cp - orig;
292*0Sstevel@tonic-gate 		new = malloc(len);
293*0Sstevel@tonic-gate 		while (orig < cp - 1)
294*0Sstevel@tonic-gate 			*new++ = *orig++;
295*0Sstevel@tonic-gate 		*new = '\0';
296*0Sstevel@tonic-gate 		*w = new - (len - 1);
297*0Sstevel@tonic-gate 	} else
298*0Sstevel@tonic-gate 		reset();
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	return (cp);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static char *
number(char * cp,int * n)304*0Sstevel@tonic-gate number(char *cp, int *n)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	char *next;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	*n = (int)strtol(cp, &next, 10);
309*0Sstevel@tonic-gate 	if (next == cp)
310*0Sstevel@tonic-gate 		expected("number", "<number>", cp);
311*0Sstevel@tonic-gate 	return (next);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate static char *
id(char * cp,int * h)315*0Sstevel@tonic-gate id(char *cp, int *h)
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	int n1, n2;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if (*cp == '(') {	/* SunPro style */
320*0Sstevel@tonic-gate 		cp++;
321*0Sstevel@tonic-gate 		cp = number(cp, &n1);
322*0Sstevel@tonic-gate 		if (*cp++ != ',')
323*0Sstevel@tonic-gate 			expected("id", ",", cp - 1);
324*0Sstevel@tonic-gate 		cp = number(cp, &n2);
325*0Sstevel@tonic-gate 		if (*cp++ != ')')
326*0Sstevel@tonic-gate 			expected("id", ")", cp - 1);
327*0Sstevel@tonic-gate 		*h = n1 * 1000 + n2;
328*0Sstevel@tonic-gate 	} else if (isdigit(*cp)) { /* gcc style */
329*0Sstevel@tonic-gate 		cp = number(cp, &n1);
330*0Sstevel@tonic-gate 		*h = n1;
331*0Sstevel@tonic-gate 	} else {
332*0Sstevel@tonic-gate 		expected("id", "(/0-9", cp);
333*0Sstevel@tonic-gate 	}
334*0Sstevel@tonic-gate 	return (cp);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate static void
tagadd(char * w,int h,struct tdesc * tdp)338*0Sstevel@tonic-gate tagadd(char *w, int h, struct tdesc *tdp)
339*0Sstevel@tonic-gate {
340*0Sstevel@tonic-gate 	struct tdesc *otdp;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	tdp->name = w;
343*0Sstevel@tonic-gate 	if (!(otdp = lookup(h)))
344*0Sstevel@tonic-gate 		addhash(tdp, h);
345*0Sstevel@tonic-gate 	else if (otdp != tdp) {
346*0Sstevel@tonic-gate 		fprintf(stderr, "duplicate entry\n");
347*0Sstevel@tonic-gate 		fprintf(stderr, "old: %s %d %d %d\n",
348*0Sstevel@tonic-gate 		    otdp->name ? otdp->name : "NULL",
349*0Sstevel@tonic-gate 		    otdp->type, otdp->id / 1000, otdp->id % 1000);
350*0Sstevel@tonic-gate 		fprintf(stderr, "new: %s %d %d %d\n",
351*0Sstevel@tonic-gate 		    tdp->name ? tdp->name : "NULL",
352*0Sstevel@tonic-gate 		    tdp->type, tdp->id / 1000, tdp->id % 1000);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate static void
tagdecl(char * cp,struct tdesc ** rtdp,int h,char * w)357*0Sstevel@tonic-gate tagdecl(char *cp, struct tdesc **rtdp, int h, char *w)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	debug(1, NULL, "tagdecl: declaring '%s'", w ? w : "(anon)");
360*0Sstevel@tonic-gate 	if ((*rtdp = lookup(h)) != NULL) {
361*0Sstevel@tonic-gate 		if (w != NULL) {
362*0Sstevel@tonic-gate 			if ((*rtdp)->name != NULL &&
363*0Sstevel@tonic-gate 			    strcmp((*rtdp)->name, w) != 0) {
364*0Sstevel@tonic-gate 				struct tdesc *tdp;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 				tdp = malloc(sizeof (*tdp));
367*0Sstevel@tonic-gate 				tdp->name = strdup(w);
368*0Sstevel@tonic-gate 				tdp->type = TYPEOF;
369*0Sstevel@tonic-gate 				tdp->data.tdesc = *rtdp;
370*0Sstevel@tonic-gate 				addhash(tdp, h); /* for *(x,y) types */
371*0Sstevel@tonic-gate 				debug(3, NULL, "    %s defined as %s(%d)", w,
372*0Sstevel@tonic-gate 				    ((*rtdp)->name != NULL) ?
373*0Sstevel@tonic-gate 				    (*rtdp)->name : "anon", h);
374*0Sstevel@tonic-gate 			} else if ((*rtdp)->name == NULL) {
375*0Sstevel@tonic-gate 				(*rtdp)->name = w;
376*0Sstevel@tonic-gate 				addhash(*rtdp, h);
377*0Sstevel@tonic-gate 			}
378*0Sstevel@tonic-gate 		}
379*0Sstevel@tonic-gate 	} else {
380*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
381*0Sstevel@tonic-gate 		(*rtdp)->name = w;
382*0Sstevel@tonic-gate 		addhash(*rtdp, h);
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	switch (*cp++) {
386*0Sstevel@tonic-gate 	case 's':
387*0Sstevel@tonic-gate 		soudef(cp, STRUCT, rtdp);
388*0Sstevel@tonic-gate 		break;
389*0Sstevel@tonic-gate 	case 'u':
390*0Sstevel@tonic-gate 		soudef(cp, UNION, rtdp);
391*0Sstevel@tonic-gate 		break;
392*0Sstevel@tonic-gate 	case 'e':
393*0Sstevel@tonic-gate 		enumdef(cp, rtdp);
394*0Sstevel@tonic-gate 		break;
395*0Sstevel@tonic-gate 	default:
396*0Sstevel@tonic-gate 		expected("tagdecl", "<tag type s/u/e>", cp - 1);
397*0Sstevel@tonic-gate 		break;
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate static char *
tdefdecl(char * cp,int h,struct tdesc ** rtdp)402*0Sstevel@tonic-gate tdefdecl(char *cp, int h, struct tdesc **rtdp)
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate 	struct tdesc *ntdp;
405*0Sstevel@tonic-gate 	char *w;
406*0Sstevel@tonic-gate 	int c, h2;
407*0Sstevel@tonic-gate 	char type;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	debug(3, cp, "tdefdecl h=%d", h);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	/* Type codes */
412*0Sstevel@tonic-gate 	switch (type = *cp) {
413*0Sstevel@tonic-gate 	case 'b': /* integer */
414*0Sstevel@tonic-gate 		c = *++cp;
415*0Sstevel@tonic-gate 		if (c != 's' && c != 'u')
416*0Sstevel@tonic-gate 			expected("tdefdecl/b", "[su]", cp - 1);
417*0Sstevel@tonic-gate 		c = *++cp;
418*0Sstevel@tonic-gate 		if (c == 'c')
419*0Sstevel@tonic-gate 			cp++;
420*0Sstevel@tonic-gate 		cp = intrinsic(cp, rtdp);
421*0Sstevel@tonic-gate 		break;
422*0Sstevel@tonic-gate 	case 'R': /* fp */
423*0Sstevel@tonic-gate 		/* skip up to and past ';' */
424*0Sstevel@tonic-gate 		while (*cp++ != ';')
425*0Sstevel@tonic-gate 			/* NULL */;
426*0Sstevel@tonic-gate 		cp = intrinsic(cp, rtdp);
427*0Sstevel@tonic-gate 		break;
428*0Sstevel@tonic-gate 	case '(': /* equiv to another type */
429*0Sstevel@tonic-gate 		cp = id(cp, &h2);
430*0Sstevel@tonic-gate 		ntdp = lookup(h2);
431*0Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
432*0Sstevel@tonic-gate 			if (*cp++ != '=')  /* better be defining it now */
433*0Sstevel@tonic-gate 				expected("tdefdecl/'('", "=", cp - 1);
434*0Sstevel@tonic-gate 			cp = tdefdecl(cp, h2, rtdp);
435*0Sstevel@tonic-gate 			ntdp = malloc(sizeof (*ntdp));
436*0Sstevel@tonic-gate 			ntdp->type = TYPEOF;
437*0Sstevel@tonic-gate 			ntdp->data.tdesc = *rtdp;
438*0Sstevel@tonic-gate 			addhash(ntdp, h2);
439*0Sstevel@tonic-gate 		} else { /* that type is already defined */
440*0Sstevel@tonic-gate 			*rtdp = malloc(sizeof (**rtdp));
441*0Sstevel@tonic-gate 			(*rtdp)->type = TYPEOF;
442*0Sstevel@tonic-gate 			(*rtdp)->data.tdesc = ntdp;
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 		break;
445*0Sstevel@tonic-gate 	case '*':
446*0Sstevel@tonic-gate 		ntdp = NULL;
447*0Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
448*0Sstevel@tonic-gate 		if (ntdp == NULL)
449*0Sstevel@tonic-gate 			expected("tdefdecl/*", "id", cp);
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
452*0Sstevel@tonic-gate 		(*rtdp)->type = POINTER;
453*0Sstevel@tonic-gate 		(*rtdp)->size = model->pointersize;
454*0Sstevel@tonic-gate 		(*rtdp)->name = "pointer";
455*0Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
456*0Sstevel@tonic-gate 		break;
457*0Sstevel@tonic-gate 	case 'f':
458*0Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
459*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
460*0Sstevel@tonic-gate 		(*rtdp)->type = FUNCTION;
461*0Sstevel@tonic-gate 		(*rtdp)->size = model->pointersize;
462*0Sstevel@tonic-gate 		(*rtdp)->name = "function";
463*0Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
464*0Sstevel@tonic-gate 		break;
465*0Sstevel@tonic-gate 	case 'a':
466*0Sstevel@tonic-gate 		cp++;
467*0Sstevel@tonic-gate 		if (*cp++ != 'r')
468*0Sstevel@tonic-gate 			expected("tdefdecl/a", "r", cp - 1);
469*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
470*0Sstevel@tonic-gate 		(*rtdp)->type = ARRAY;
471*0Sstevel@tonic-gate 		(*rtdp)->name = "array";
472*0Sstevel@tonic-gate 		cp = arraydef(cp, rtdp);
473*0Sstevel@tonic-gate 		break;
474*0Sstevel@tonic-gate 	case 'x':
475*0Sstevel@tonic-gate 		c = *++cp;
476*0Sstevel@tonic-gate 		if (c != 's' && c != 'u' && c != 'e')
477*0Sstevel@tonic-gate 			expected("tdefdecl/x", "[sue]", cp - 1);
478*0Sstevel@tonic-gate 		cp = name(cp + 1, &w);
479*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
480*0Sstevel@tonic-gate 		(*rtdp)->type = FORWARD;
481*0Sstevel@tonic-gate 		(*rtdp)->name = w;
482*0Sstevel@tonic-gate 		break;
483*0Sstevel@tonic-gate 	case 'B': /* volatile */
484*0Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
485*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
486*0Sstevel@tonic-gate 		(*rtdp)->type = VOLATILE;
487*0Sstevel@tonic-gate 		(*rtdp)->size = 0;
488*0Sstevel@tonic-gate 		(*rtdp)->name = "volatile";
489*0Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
490*0Sstevel@tonic-gate 		break;
491*0Sstevel@tonic-gate 	case 'k': /* const */
492*0Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
493*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
494*0Sstevel@tonic-gate 		(*rtdp)->type = CONST;
495*0Sstevel@tonic-gate 		(*rtdp)->size = 0;
496*0Sstevel@tonic-gate 		(*rtdp)->name = "const";
497*0Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
498*0Sstevel@tonic-gate 		break;
499*0Sstevel@tonic-gate 	case '0': case '1': case '2': case '3':	case '4':
500*0Sstevel@tonic-gate 	case '5': case '6': case '7': case '8': case '9':
501*0Sstevel@tonic-gate 		/* gcc equiv to another type */
502*0Sstevel@tonic-gate 		cp = id(cp, &h2);
503*0Sstevel@tonic-gate 		ntdp = lookup(h2);
504*0Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
505*0Sstevel@tonic-gate 			/* better be defining it now */
506*0Sstevel@tonic-gate 			if (*cp++ != '=') {
507*0Sstevel@tonic-gate 				if (h != h2)
508*0Sstevel@tonic-gate 					expected("tdefdecl/'0-9'", "=", cp - 1);
509*0Sstevel@tonic-gate 				/* defined in terms of itself */
510*0Sstevel@tonic-gate 				*rtdp = malloc(sizeof (**rtdp));
511*0Sstevel@tonic-gate 				(*rtdp)->type = INTRINSIC;
512*0Sstevel@tonic-gate 				(*rtdp)->name = "void";
513*0Sstevel@tonic-gate 				(*rtdp)->size = 0;
514*0Sstevel@tonic-gate 			} else {
515*0Sstevel@tonic-gate 				cp = tdefdecl(cp, h2, rtdp);
516*0Sstevel@tonic-gate 				ntdp = malloc(sizeof (*ntdp));
517*0Sstevel@tonic-gate 				ntdp->type = TYPEOF;
518*0Sstevel@tonic-gate 				ntdp->data.tdesc = *rtdp;
519*0Sstevel@tonic-gate 				addhash(ntdp, h2);
520*0Sstevel@tonic-gate 			}
521*0Sstevel@tonic-gate 		} else { /* that type is already defined */
522*0Sstevel@tonic-gate 			*rtdp = malloc(sizeof (**rtdp));
523*0Sstevel@tonic-gate 			(*rtdp)->type = TYPEOF;
524*0Sstevel@tonic-gate 			(*rtdp)->data.tdesc = ntdp;
525*0Sstevel@tonic-gate 		}
526*0Sstevel@tonic-gate 		break;
527*0Sstevel@tonic-gate 	case 'u':
528*0Sstevel@tonic-gate 	case 's':
529*0Sstevel@tonic-gate 		cp++;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
532*0Sstevel@tonic-gate 		(*rtdp)->name = NULL;
533*0Sstevel@tonic-gate 		cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
534*0Sstevel@tonic-gate 		break;
535*0Sstevel@tonic-gate 	default:
536*0Sstevel@tonic-gate 		expected("tdefdecl", "<type code>", cp);
537*0Sstevel@tonic-gate 	}
538*0Sstevel@tonic-gate 	return (cp);
539*0Sstevel@tonic-gate }
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate static char *
intrinsic(char * cp,struct tdesc ** rtdp)542*0Sstevel@tonic-gate intrinsic(char *cp, struct tdesc **rtdp)
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	struct tdesc *tdp;
545*0Sstevel@tonic-gate 	int size;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	cp = number(cp, &size);
548*0Sstevel@tonic-gate 	tdp = malloc(sizeof (*tdp));
549*0Sstevel@tonic-gate 	tdp->type = INTRINSIC;
550*0Sstevel@tonic-gate 	tdp->size = size;
551*0Sstevel@tonic-gate 	tdp->name = NULL;
552*0Sstevel@tonic-gate 	debug(3, NULL, "intrinsic: size=%ld", size);
553*0Sstevel@tonic-gate 	*rtdp = tdp;
554*0Sstevel@tonic-gate 	return (cp);
555*0Sstevel@tonic-gate }
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate static char *
soudef(char * cp,enum type type,struct tdesc ** rtdp)558*0Sstevel@tonic-gate soudef(char *cp, enum type type, struct tdesc **rtdp)
559*0Sstevel@tonic-gate {
560*0Sstevel@tonic-gate 	struct mlist **next_pp, *prev_p = NULL;
561*0Sstevel@tonic-gate 	char *w;
562*0Sstevel@tonic-gate 	int size;
563*0Sstevel@tonic-gate 	struct tdesc *tdp;
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 	cp = number(cp, &size);
566*0Sstevel@tonic-gate 	(*rtdp)->size = size;
567*0Sstevel@tonic-gate 	(*rtdp)->type = type; /* s or u */
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	/*
570*0Sstevel@tonic-gate 	 * An '@' here indicates a bitmask follows.   This is so the
571*0Sstevel@tonic-gate 	 * compiler can pass information to debuggers about how structures
572*0Sstevel@tonic-gate 	 * are passed in the v9 world.  We don't need this information
573*0Sstevel@tonic-gate 	 * so we skip over it.
574*0Sstevel@tonic-gate 	 */
575*0Sstevel@tonic-gate 	if (cp[0] == '@')
576*0Sstevel@tonic-gate 		cp += 3;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	debug(3, cp, "soudef: %s size=%d",
579*0Sstevel@tonic-gate 	    (*rtdp)->name ? (*rtdp)->name : "(anonsou)",
580*0Sstevel@tonic-gate 	    (*rtdp)->size);
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	next_pp = &((*rtdp)->data.members.forw); /* head for forward linklist */
583*0Sstevel@tonic-gate 	/* fill up the fields */
584*0Sstevel@tonic-gate 	while ((*cp != '"') && (*cp != ';')) { /* signifies end of fields */
585*0Sstevel@tonic-gate 		int h;
586*0Sstevel@tonic-gate 		struct mlist *mlp = malloc(sizeof (*mlp));
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 		mlp->prev = prev_p;	/* links for the backward list */
589*0Sstevel@tonic-gate 		prev_p = mlp;
590*0Sstevel@tonic-gate 		*next_pp = mlp;		/* links for the forward list */
591*0Sstevel@tonic-gate 		next_pp = &mlp->next;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 		cp = name(cp, &w);
594*0Sstevel@tonic-gate 		mlp->name = w;
595*0Sstevel@tonic-gate 		cp = id(cp, &h);
596*0Sstevel@tonic-gate 		/*
597*0Sstevel@tonic-gate 		 * find the tdesc struct in the hash table for this type
598*0Sstevel@tonic-gate 		 * and stick a ptr in here
599*0Sstevel@tonic-gate 		 */
600*0Sstevel@tonic-gate 		tdp = lookup(h);
601*0Sstevel@tonic-gate 		if (tdp == NULL) { /* not in hash list */
602*0Sstevel@tonic-gate 			debug(3, NULL, "      defines %s (%d)", w, h);
603*0Sstevel@tonic-gate 			if (*cp++ != '=')
604*0Sstevel@tonic-gate 				expected("soudef", "=", cp - 1);
605*0Sstevel@tonic-gate 			cp = tdefdecl(cp, h, &tdp);
606*0Sstevel@tonic-gate 			addhash(tdp, h);
607*0Sstevel@tonic-gate 			debug(4, cp, "     soudef now looking at    ");
608*0Sstevel@tonic-gate 			cp++;
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 		} else {
611*0Sstevel@tonic-gate 			debug(3, NULL, "      refers to %s (%d, %s)",
612*0Sstevel@tonic-gate 			    w ? w : "anon", h, tdp->name ? tdp->name : "anon");
613*0Sstevel@tonic-gate 		}
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		mlp->fdesc = tdp;
616*0Sstevel@tonic-gate 		cp = offsize(cp, mlp);	/* cp is now pointing to next field */
617*0Sstevel@tonic-gate 		if (*cp == '\\')	/* could be a continuation */
618*0Sstevel@tonic-gate 			cp = get_continuation();
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 	(*rtdp)->data.members.back = prev_p;	/* head for backward linklist */
621*0Sstevel@tonic-gate 	return (cp);
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate static char *
offsize(char * cp,struct mlist * mlp)625*0Sstevel@tonic-gate offsize(char *cp, struct mlist *mlp)
626*0Sstevel@tonic-gate {
627*0Sstevel@tonic-gate 	int offset, size;
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 	if (*cp == ',')
630*0Sstevel@tonic-gate 		cp++;
631*0Sstevel@tonic-gate 	cp = number(cp, &offset);
632*0Sstevel@tonic-gate 	if (*cp++ != ',')
633*0Sstevel@tonic-gate 		expected("offsize/2", ",", cp - 1);
634*0Sstevel@tonic-gate 	cp = number(cp, &size);
635*0Sstevel@tonic-gate 	if (*cp++ != ';')
636*0Sstevel@tonic-gate 		expected("offsize/3", ";", cp - 1);
637*0Sstevel@tonic-gate 	mlp->offset = offset;
638*0Sstevel@tonic-gate 	mlp->size = size;
639*0Sstevel@tonic-gate 	return (cp);
640*0Sstevel@tonic-gate }
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate static char *
arraydef(char * cp,struct tdesc ** rtdp)643*0Sstevel@tonic-gate arraydef(char *cp, struct tdesc **rtdp)
644*0Sstevel@tonic-gate {
645*0Sstevel@tonic-gate 	int h;
646*0Sstevel@tonic-gate 	int start, end;
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	cp = id(cp, &h);
649*0Sstevel@tonic-gate 	if (*cp++ != ';')
650*0Sstevel@tonic-gate 		expected("arraydef/1", ";", cp - 1);
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	(*rtdp)->data.ardef = malloc(sizeof (struct ardef));
653*0Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices = malloc(sizeof (struct element));
654*0Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->index_type = lookup(h);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	cp = number(cp, &start); /* lower */
657*0Sstevel@tonic-gate 	if (*cp++ != ';')
658*0Sstevel@tonic-gate 		expected("arraydef/2", ";", cp - 1);
659*0Sstevel@tonic-gate 	cp = number(cp, &end);	/* upper */
660*0Sstevel@tonic-gate 	if (*cp++ != ';')
661*0Sstevel@tonic-gate 		expected("arraydef/3", ";", cp - 1);
662*0Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->range_start = start;
663*0Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->range_end = end;
664*0Sstevel@tonic-gate #if 0
665*0Sstevel@tonic-gate 	if (isdigit(*cp)) {
666*0Sstevel@tonic-gate 		cp = number(cp, &contents_type); /* lower */
667*0Sstevel@tonic-gate 		tdp = lookup(contents_type);
668*0Sstevel@tonic-gate 		if (tdp != NULL) {
669*0Sstevel@tonic-gate 			(*rtdp)->data.ardef->contents = tdp;
670*0Sstevel@tonic-gate 		} else {
671*0Sstevel@tonic-gate 			if (*cp != '=')
672*0Sstevel@tonic-gate 				expected("arraydef/4", "=", cp);
673*0Sstevel@tonic-gate 			cp = tdefdecl(cp + 1, h, &tdp);
674*0Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
675*0Sstevel@tonic-gate 			(*rtdp)->data.ardef->contents = tdp;
676*0Sstevel@tonic-gate 		}
677*0Sstevel@tonic-gate 	} /* else  */
678*0Sstevel@tonic-gate #endif
679*0Sstevel@tonic-gate 	cp = tdefdecl(cp, h, &((*rtdp)->data.ardef->contents));
680*0Sstevel@tonic-gate 	return (cp);
681*0Sstevel@tonic-gate }
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate static void
enumdef(char * cp,struct tdesc ** rtdp)684*0Sstevel@tonic-gate enumdef(char *cp, struct tdesc **rtdp)
685*0Sstevel@tonic-gate {
686*0Sstevel@tonic-gate 	struct elist *elp, **prev;
687*0Sstevel@tonic-gate 	char *w;
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	(*rtdp)->type = ENUM;
690*0Sstevel@tonic-gate 	(*rtdp)->data.emem = NULL;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	prev = &((*rtdp)->data.emem);
693*0Sstevel@tonic-gate 	while (*cp != ';') {
694*0Sstevel@tonic-gate 		elp = malloc(sizeof (*elp));
695*0Sstevel@tonic-gate 		elp->next = NULL;
696*0Sstevel@tonic-gate 		*prev = elp;
697*0Sstevel@tonic-gate 		cp = name(cp, &w);
698*0Sstevel@tonic-gate 		elp->name = w;
699*0Sstevel@tonic-gate 		cp = number(cp, &elp->number);
700*0Sstevel@tonic-gate 		debug(3, NULL, "enum %s: %s=%ld",
701*0Sstevel@tonic-gate 		    (*rtdp)->name ? (*rtdp)->name : "(anon enum)",
702*0Sstevel@tonic-gate 		    elp->name, elp->number);
703*0Sstevel@tonic-gate 		prev = &elp->next;
704*0Sstevel@tonic-gate 		if (*cp++ != ',')
705*0Sstevel@tonic-gate 			expected("enumdef", ",", cp - 1);
706*0Sstevel@tonic-gate 		if (*cp == '\\')
707*0Sstevel@tonic-gate 			cp = get_continuation();
708*0Sstevel@tonic-gate 	}
709*0Sstevel@tonic-gate }
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate /*
712*0Sstevel@tonic-gate  * Add a node to the hash queues.
713*0Sstevel@tonic-gate  */
714*0Sstevel@tonic-gate static void
addhash(struct tdesc * tdp,int num)715*0Sstevel@tonic-gate addhash(struct tdesc *tdp, int num)
716*0Sstevel@tonic-gate {
717*0Sstevel@tonic-gate 	int hash = HASH(num);
718*0Sstevel@tonic-gate 	struct tdesc *ttdp;
719*0Sstevel@tonic-gate 	char added_num = 0, added_name = 0;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	/*
722*0Sstevel@tonic-gate 	 * If it already exists in the hash table don't add it again
723*0Sstevel@tonic-gate 	 * (but still check to see if the name should be hashed).
724*0Sstevel@tonic-gate 	 */
725*0Sstevel@tonic-gate 	ttdp = lookup(num);
726*0Sstevel@tonic-gate 	if (ttdp == NULL) {
727*0Sstevel@tonic-gate 		tdp->id = num;
728*0Sstevel@tonic-gate 		tdp->hash = hash_table[hash];
729*0Sstevel@tonic-gate 		hash_table[hash] = tdp;
730*0Sstevel@tonic-gate 		added_num = 1;
731*0Sstevel@tonic-gate 	}
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 	if (tdp->name != NULL) {
734*0Sstevel@tonic-gate 		ttdp = lookupname(tdp->name);
735*0Sstevel@tonic-gate 		if (ttdp == NULL) {
736*0Sstevel@tonic-gate 			hash = compute_sum(tdp->name);
737*0Sstevel@tonic-gate 			tdp->next = name_table[hash];
738*0Sstevel@tonic-gate 			name_table[hash] = tdp;
739*0Sstevel@tonic-gate 			added_name = 1;
740*0Sstevel@tonic-gate 		}
741*0Sstevel@tonic-gate 	}
742*0Sstevel@tonic-gate 	if (!added_num && !added_name) {
743*0Sstevel@tonic-gate 		fprintf(stderr, "stabs: broken hash\n");
744*0Sstevel@tonic-gate 		exit(1);
745*0Sstevel@tonic-gate 	}
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate struct tdesc *
lookupname(char * name)749*0Sstevel@tonic-gate lookupname(char *name)
750*0Sstevel@tonic-gate {
751*0Sstevel@tonic-gate 	int hash = compute_sum(name);
752*0Sstevel@tonic-gate 	struct tdesc *tdp, *ttdp = NULL;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	for (tdp = name_table[hash]; tdp != NULL; tdp = tdp->next) {
755*0Sstevel@tonic-gate 		if (tdp->name != NULL && strcmp(tdp->name, name) == 0) {
756*0Sstevel@tonic-gate 			if (tdp->type == STRUCT || tdp->type == UNION ||
757*0Sstevel@tonic-gate 			    tdp->type == ENUM || tdp->type == INTRINSIC)
758*0Sstevel@tonic-gate 				return (tdp);
759*0Sstevel@tonic-gate 			if (tdp->type == TYPEOF)
760*0Sstevel@tonic-gate 				ttdp = tdp;
761*0Sstevel@tonic-gate 		}
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 	return (ttdp);
764*0Sstevel@tonic-gate }
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate static int
compute_sum(char * w)767*0Sstevel@tonic-gate compute_sum(char *w)
768*0Sstevel@tonic-gate {
769*0Sstevel@tonic-gate 	char c;
770*0Sstevel@tonic-gate 	int sum;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	for (sum = 0; (c = *w) != '\0'; sum += c, w++)
773*0Sstevel@tonic-gate 		;
774*0Sstevel@tonic-gate 	return (HASH(sum));
775*0Sstevel@tonic-gate }
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate static void
reset(void)778*0Sstevel@tonic-gate reset(void)
779*0Sstevel@tonic-gate {
780*0Sstevel@tonic-gate 	longjmp(resetbuf, 1);
781*0Sstevel@tonic-gate 	/* NOTREACHED */
782*0Sstevel@tonic-gate }
783