xref: /illumos-gate/usr/src/tools/stabs/stabs.c (revision f896265f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1996-2002 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <limits.h>
287c478bd9Sstevel@tonic-gate #include <stdarg.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "stabs.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static struct tdesc *hash_table[BUCKETS];
347c478bd9Sstevel@tonic-gate static struct tdesc *name_table[BUCKETS];
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static void reset(void);
377c478bd9Sstevel@tonic-gate static jmp_buf	resetbuf;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static char *get_line(void);
407c478bd9Sstevel@tonic-gate static void parseline(char *cp);
417c478bd9Sstevel@tonic-gate static char *soudef(char *cp, enum type type, struct tdesc **rtdp);
427c478bd9Sstevel@tonic-gate static void enumdef(char *cp, struct tdesc **rtdp);
437c478bd9Sstevel@tonic-gate static int compute_sum(char *w);
447c478bd9Sstevel@tonic-gate static struct tdesc *lookup(int h);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char *number(char *cp, int *n);
477c478bd9Sstevel@tonic-gate static char *name(char *cp, char **w);
487c478bd9Sstevel@tonic-gate static char *id(char *cp, int *h);
497c478bd9Sstevel@tonic-gate static char *offsize(char *cp, struct mlist *mlp);
507c478bd9Sstevel@tonic-gate static char *whitesp(char *cp);
517c478bd9Sstevel@tonic-gate static void addhash(struct tdesc *tdp, int num);
527c478bd9Sstevel@tonic-gate static void tagadd(char *w, int h, struct tdesc *tdp);
537c478bd9Sstevel@tonic-gate static void tagdecl(char *cp, struct tdesc **rtdp, int h, char *w);
547c478bd9Sstevel@tonic-gate static char *tdefdecl(char *cp, int h, struct tdesc **rtdp);
557c478bd9Sstevel@tonic-gate static char *intrinsic(char *cp, struct tdesc **rtdp);
567c478bd9Sstevel@tonic-gate static char *arraydef(char *cp, struct tdesc **rtdp);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static int line_number = 0;
597c478bd9Sstevel@tonic-gate static int debug_line  = 0;
607c478bd9Sstevel@tonic-gate static char linebuf[MAXLINE];
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern int debug_level;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static void
debug(int level,char * cp,char * fmt,...)657c478bd9Sstevel@tonic-gate debug(int level, char *cp, char *fmt, ...)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	va_list ap;
687c478bd9Sstevel@tonic-gate 	char buf[1024];
697c478bd9Sstevel@tonic-gate 	char tmp[32];
707c478bd9Sstevel@tonic-gate 	int i;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (level > debug_level)
737c478bd9Sstevel@tonic-gate 		return;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
767c478bd9Sstevel@tonic-gate 		for (i = 0; i < 30; i++) {
777c478bd9Sstevel@tonic-gate 			if (cp[i] == '\0')
787c478bd9Sstevel@tonic-gate 				break;
797c478bd9Sstevel@tonic-gate 			if (!iscntrl(cp[i]))
807c478bd9Sstevel@tonic-gate 				tmp[i] = cp[i];
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 		tmp[i] = '\0';
8312661a24SToomas Soome 		(void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
847c478bd9Sstevel@tonic-gate 	} else {
8512661a24SToomas Soome 		(void) snprintf(buf, sizeof (buf), "%s\n", fmt);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
897c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, buf, ap);
907c478bd9Sstevel@tonic-gate 	va_end(ap);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Report unexpected syntax in stabs. */
957c478bd9Sstevel@tonic-gate static void
expected(char * who,char * what,char * where)967c478bd9Sstevel@tonic-gate expected(
977c478bd9Sstevel@tonic-gate 	char *who,	/* what function, or part thereof, is reporting */
987c478bd9Sstevel@tonic-gate 	char *what,	/* what was expected */
997c478bd9Sstevel@tonic-gate 	char *where)	/* where we were in the line of input */
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s, input line %d: expecting \"%s\" at \"%s\"\n",
10212661a24SToomas Soome 	    who, line_number, what, where);
1037c478bd9Sstevel@tonic-gate 	exit(1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Read a line from stdin into linebuf and increment line_number. */
1077c478bd9Sstevel@tonic-gate static char *
get_line(void)1087c478bd9Sstevel@tonic-gate get_line(void)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	char *cp = fgets(linebuf, MAXLINE, stdin);
1117c478bd9Sstevel@tonic-gate 	line_number++;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* For debugging, you can set debug_line to a line to stop on. */
1147c478bd9Sstevel@tonic-gate 	if (line_number == debug_line) {
1157c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Hit debug line number %d\n", line_number);
1167c478bd9Sstevel@tonic-gate 		for (;;)
11712661a24SToomas Soome 			(void) sleep(1);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	return (cp);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* Get the continuation of the current input line. */
1237c478bd9Sstevel@tonic-gate static char *
get_continuation(void)1247c478bd9Sstevel@tonic-gate get_continuation(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	char *cp = get_line();
1277c478bd9Sstevel@tonic-gate 	if (!cp) {
1287c478bd9Sstevel@tonic-gate 		fprintf(stderr, "expecting continuation line, "
1297c478bd9Sstevel@tonic-gate 		    "got end of input\n");
1307c478bd9Sstevel@tonic-gate 		exit(1);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/* Skip to the quoted stuff. */
1347c478bd9Sstevel@tonic-gate 	while (*cp++ != '"')
1357c478bd9Sstevel@tonic-gate 		;
1367c478bd9Sstevel@tonic-gate 	return (cp);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate void
parse_input(void)1407c478bd9Sstevel@tonic-gate parse_input(void)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	char *cp;
1437c478bd9Sstevel@tonic-gate 	int i = 0;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	for (i = 0; i < BUCKETS; i++) {
1467c478bd9Sstevel@tonic-gate 		hash_table[i] = NULL;
1477c478bd9Sstevel@tonic-gate 		name_table[i] = NULL;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * get a line at a time from the .s stabs file and parse.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	while ((cp = get_line()) != NULL)
1547c478bd9Sstevel@tonic-gate 		parseline(cp);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Parse each line of the .s file (stabs entry) gather meaningful information
1597c478bd9Sstevel@tonic-gate  * like name of type, size, offsets of fields etc.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static void
parseline(char * cp)1627c478bd9Sstevel@tonic-gate parseline(char *cp)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	struct tdesc *tdp;
1657c478bd9Sstevel@tonic-gate 	char c, *w;
1667c478bd9Sstevel@tonic-gate 	int h, tagdef;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * setup for reset()
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	if (setjmp(resetbuf))
1727c478bd9Sstevel@tonic-gate 		return;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/*
1757c478bd9Sstevel@tonic-gate 	 * Look for lines of the form
1767c478bd9Sstevel@tonic-gate 	 *	.stabs	"str",n,n,n,n
1777c478bd9Sstevel@tonic-gate 	 * The part in '"' is then parsed.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	cp = whitesp(cp);
1807c478bd9Sstevel@tonic-gate #define	STLEN	6
1817c478bd9Sstevel@tonic-gate 	debug(2, cp, "parseline");
1827c478bd9Sstevel@tonic-gate 	if (strncmp(cp, ".stabs", STLEN) != 0)
1837c478bd9Sstevel@tonic-gate 		reset();
1847c478bd9Sstevel@tonic-gate 	cp += STLEN;
1857c478bd9Sstevel@tonic-gate #undef STLEN
1867c478bd9Sstevel@tonic-gate 	cp = whitesp(cp);
1877c478bd9Sstevel@tonic-gate 	if (*cp++ != '"')
1887c478bd9Sstevel@tonic-gate 		reset();
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * name:type		variable (ignored)
1927c478bd9Sstevel@tonic-gate 	 * name:ttype		typedef
1937c478bd9Sstevel@tonic-gate 	 * name:Ttype		struct tag define
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	cp = whitesp(cp);
1967c478bd9Sstevel@tonic-gate 	cp = name(cp, &w);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	tagdef = 0;
1997c478bd9Sstevel@tonic-gate 	switch (c = *cp++) {
2007c478bd9Sstevel@tonic-gate 	case 't': /* type */
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 	case 'T': /* struct, union, enum */
2037c478bd9Sstevel@tonic-gate 		tagdef = 1;
2047c478bd9Sstevel@tonic-gate 		break;
2057c478bd9Sstevel@tonic-gate 	default:
2067c478bd9Sstevel@tonic-gate 		reset();
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * The type id and definition follow.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	cp = id(cp, &h);
2137c478bd9Sstevel@tonic-gate 	if (*cp == '"') {
2147c478bd9Sstevel@tonic-gate 		struct tdesc *ntdp;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		cp++;
2177c478bd9Sstevel@tonic-gate 		ntdp = lookup(h);
2187c478bd9Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
2197c478bd9Sstevel@tonic-gate 			if (*cp++ != '=')  /* better be defining it now */
2207c478bd9Sstevel@tonic-gate 				expected("parseline/'0-9'", "=", cp - 1);
2217c478bd9Sstevel@tonic-gate 			cp = tdefdecl(cp, h, &tdp);
2227c478bd9Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
2237c478bd9Sstevel@tonic-gate 		} else { /* that type is already defined */
2247c478bd9Sstevel@tonic-gate 			tdp = malloc(sizeof (*tdp));
2257c478bd9Sstevel@tonic-gate 			tdp->type = TYPEOF;
2267c478bd9Sstevel@tonic-gate 			tdp->name = (w != NULL) ? strdup(w) : NULL;
2277c478bd9Sstevel@tonic-gate 			tdp->data.tdesc = ntdp;
2287c478bd9Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
2297c478bd9Sstevel@tonic-gate 			debug(3, NULL, "    %s defined as %s(%d)", w,
2307c478bd9Sstevel@tonic-gate 			    (ntdp->name != NULL) ? ntdp->name : "anon", h);
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 		return;
2337c478bd9Sstevel@tonic-gate 	} else if (*cp++ != '=') {
2347c478bd9Sstevel@tonic-gate 		expected("parseline", "=", cp - 1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	if (tagdef) {
2377c478bd9Sstevel@tonic-gate 		tagdecl(cp, &tdp, h, w);
2387c478bd9Sstevel@tonic-gate 	} else {
23912661a24SToomas Soome 		(void) tdefdecl(cp, h, &tdp);
2407c478bd9Sstevel@tonic-gate 		tagadd(w, h, tdp);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate  * Check if we have this node in the hash table already
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate static struct tdesc *
lookup(int h)2487c478bd9Sstevel@tonic-gate lookup(int h)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	int hash = HASH(h);
2517c478bd9Sstevel@tonic-gate 	struct tdesc *tdp = hash_table[hash];
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	while (tdp != NULL) {
2547c478bd9Sstevel@tonic-gate 		if (tdp->id == h)
2557c478bd9Sstevel@tonic-gate 			return (tdp);
2567c478bd9Sstevel@tonic-gate 		tdp = tdp->hash;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	return (NULL);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static char *
whitesp(char * cp)2627c478bd9Sstevel@tonic-gate whitesp(char *cp)
2637c478bd9Sstevel@tonic-gate {
264*f896265fSToomas Soome 	char c;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	for (c = *cp++; isspace(c); c = *cp++)
2677c478bd9Sstevel@tonic-gate 		;
2687c478bd9Sstevel@tonic-gate 	--cp;
2697c478bd9Sstevel@tonic-gate 	return (cp);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static char *
name(char * cp,char ** w)2737c478bd9Sstevel@tonic-gate name(char *cp, char **w)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	char *new, *orig, c;
2767c478bd9Sstevel@tonic-gate 	int len;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	orig = cp;
2797c478bd9Sstevel@tonic-gate 	c = *cp++;
2807c478bd9Sstevel@tonic-gate 	if (c == ':')
2817c478bd9Sstevel@tonic-gate 		*w = NULL;
2827c478bd9Sstevel@tonic-gate 	else if (isalpha(c) || c == '_') {
2837c478bd9Sstevel@tonic-gate 		for (c = *cp++; isalnum(c) || c == ' ' || c == '_'; c = *cp++)
2847c478bd9Sstevel@tonic-gate 			;
2857c478bd9Sstevel@tonic-gate 		if (c != ':')
2867c478bd9Sstevel@tonic-gate 			reset();
2877c478bd9Sstevel@tonic-gate 		len = cp - orig;
2887c478bd9Sstevel@tonic-gate 		new = malloc(len);
2897c478bd9Sstevel@tonic-gate 		while (orig < cp - 1)
2907c478bd9Sstevel@tonic-gate 			*new++ = *orig++;
2917c478bd9Sstevel@tonic-gate 		*new = '\0';
2927c478bd9Sstevel@tonic-gate 		*w = new - (len - 1);
2937c478bd9Sstevel@tonic-gate 	} else
2947c478bd9Sstevel@tonic-gate 		reset();
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	return (cp);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static char *
number(char * cp,int * n)3007c478bd9Sstevel@tonic-gate number(char *cp, int *n)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	char *next;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	*n = (int)strtol(cp, &next, 10);
3057c478bd9Sstevel@tonic-gate 	if (next == cp)
3067c478bd9Sstevel@tonic-gate 		expected("number", "<number>", cp);
3077c478bd9Sstevel@tonic-gate 	return (next);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static char *
id(char * cp,int * h)3117c478bd9Sstevel@tonic-gate id(char *cp, int *h)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	int n1, n2;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (*cp == '(') {	/* SunPro style */
3167c478bd9Sstevel@tonic-gate 		cp++;
3177c478bd9Sstevel@tonic-gate 		cp = number(cp, &n1);
3187c478bd9Sstevel@tonic-gate 		if (*cp++ != ',')
3197c478bd9Sstevel@tonic-gate 			expected("id", ",", cp - 1);
3207c478bd9Sstevel@tonic-gate 		cp = number(cp, &n2);
3217c478bd9Sstevel@tonic-gate 		if (*cp++ != ')')
3227c478bd9Sstevel@tonic-gate 			expected("id", ")", cp - 1);
3237c478bd9Sstevel@tonic-gate 		*h = n1 * 1000 + n2;
3247c478bd9Sstevel@tonic-gate 	} else if (isdigit(*cp)) { /* gcc style */
3257c478bd9Sstevel@tonic-gate 		cp = number(cp, &n1);
3267c478bd9Sstevel@tonic-gate 		*h = n1;
3277c478bd9Sstevel@tonic-gate 	} else {
3287c478bd9Sstevel@tonic-gate 		expected("id", "(/0-9", cp);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	return (cp);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static void
tagadd(char * w,int h,struct tdesc * tdp)3347c478bd9Sstevel@tonic-gate tagadd(char *w, int h, struct tdesc *tdp)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	struct tdesc *otdp;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	tdp->name = w;
3397c478bd9Sstevel@tonic-gate 	if (!(otdp = lookup(h)))
3407c478bd9Sstevel@tonic-gate 		addhash(tdp, h);
3417c478bd9Sstevel@tonic-gate 	else if (otdp != tdp) {
3427c478bd9Sstevel@tonic-gate 		fprintf(stderr, "duplicate entry\n");
3437c478bd9Sstevel@tonic-gate 		fprintf(stderr, "old: %s %d %d %d\n",
3447c478bd9Sstevel@tonic-gate 		    otdp->name ? otdp->name : "NULL",
3457c478bd9Sstevel@tonic-gate 		    otdp->type, otdp->id / 1000, otdp->id % 1000);
3467c478bd9Sstevel@tonic-gate 		fprintf(stderr, "new: %s %d %d %d\n",
3477c478bd9Sstevel@tonic-gate 		    tdp->name ? tdp->name : "NULL",
3487c478bd9Sstevel@tonic-gate 		    tdp->type, tdp->id / 1000, tdp->id % 1000);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static void
tagdecl(char * cp,struct tdesc ** rtdp,int h,char * w)3537c478bd9Sstevel@tonic-gate tagdecl(char *cp, struct tdesc **rtdp, int h, char *w)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	debug(1, NULL, "tagdecl: declaring '%s'", w ? w : "(anon)");
3567c478bd9Sstevel@tonic-gate 	if ((*rtdp = lookup(h)) != NULL) {
3577c478bd9Sstevel@tonic-gate 		if (w != NULL) {
3587c478bd9Sstevel@tonic-gate 			if ((*rtdp)->name != NULL &&
3597c478bd9Sstevel@tonic-gate 			    strcmp((*rtdp)->name, w) != 0) {
3607c478bd9Sstevel@tonic-gate 				struct tdesc *tdp;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 				tdp = malloc(sizeof (*tdp));
3637c478bd9Sstevel@tonic-gate 				tdp->name = strdup(w);
3647c478bd9Sstevel@tonic-gate 				tdp->type = TYPEOF;
3657c478bd9Sstevel@tonic-gate 				tdp->data.tdesc = *rtdp;
3667c478bd9Sstevel@tonic-gate 				addhash(tdp, h); /* for *(x,y) types */
3677c478bd9Sstevel@tonic-gate 				debug(3, NULL, "    %s defined as %s(%d)", w,
3687c478bd9Sstevel@tonic-gate 				    ((*rtdp)->name != NULL) ?
3697c478bd9Sstevel@tonic-gate 				    (*rtdp)->name : "anon", h);
3707c478bd9Sstevel@tonic-gate 			} else if ((*rtdp)->name == NULL) {
3717c478bd9Sstevel@tonic-gate 				(*rtdp)->name = w;
3727c478bd9Sstevel@tonic-gate 				addhash(*rtdp, h);
3737c478bd9Sstevel@tonic-gate 			}
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	} else {
3767c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
3777c478bd9Sstevel@tonic-gate 		(*rtdp)->name = w;
3787c478bd9Sstevel@tonic-gate 		addhash(*rtdp, h);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	switch (*cp++) {
3827c478bd9Sstevel@tonic-gate 	case 's':
38312661a24SToomas Soome 		(void) soudef(cp, STRUCT, rtdp);
3847c478bd9Sstevel@tonic-gate 		break;
3857c478bd9Sstevel@tonic-gate 	case 'u':
38612661a24SToomas Soome 		(void) soudef(cp, UNION, rtdp);
3877c478bd9Sstevel@tonic-gate 		break;
3887c478bd9Sstevel@tonic-gate 	case 'e':
3897c478bd9Sstevel@tonic-gate 		enumdef(cp, rtdp);
3907c478bd9Sstevel@tonic-gate 		break;
3917c478bd9Sstevel@tonic-gate 	default:
3927c478bd9Sstevel@tonic-gate 		expected("tagdecl", "<tag type s/u/e>", cp - 1);
3937c478bd9Sstevel@tonic-gate 		break;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static char *
tdefdecl(char * cp,int h,struct tdesc ** rtdp)3987c478bd9Sstevel@tonic-gate tdefdecl(char *cp, int h, struct tdesc **rtdp)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	struct tdesc *ntdp;
4017c478bd9Sstevel@tonic-gate 	char *w;
4027c478bd9Sstevel@tonic-gate 	int c, h2;
4037c478bd9Sstevel@tonic-gate 	char type;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	debug(3, cp, "tdefdecl h=%d", h);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/* Type codes */
4087c478bd9Sstevel@tonic-gate 	switch (type = *cp) {
4097c478bd9Sstevel@tonic-gate 	case 'b': /* integer */
4107c478bd9Sstevel@tonic-gate 		c = *++cp;
4117c478bd9Sstevel@tonic-gate 		if (c != 's' && c != 'u')
4127c478bd9Sstevel@tonic-gate 			expected("tdefdecl/b", "[su]", cp - 1);
4137c478bd9Sstevel@tonic-gate 		c = *++cp;
4147c478bd9Sstevel@tonic-gate 		if (c == 'c')
4157c478bd9Sstevel@tonic-gate 			cp++;
4167c478bd9Sstevel@tonic-gate 		cp = intrinsic(cp, rtdp);
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 	case 'R': /* fp */
4197c478bd9Sstevel@tonic-gate 		/* skip up to and past ';' */
4207c478bd9Sstevel@tonic-gate 		while (*cp++ != ';')
4217c478bd9Sstevel@tonic-gate 			/* NULL */;
4227c478bd9Sstevel@tonic-gate 		cp = intrinsic(cp, rtdp);
4237c478bd9Sstevel@tonic-gate 		break;
4247c478bd9Sstevel@tonic-gate 	case '(': /* equiv to another type */
4257c478bd9Sstevel@tonic-gate 		cp = id(cp, &h2);
4267c478bd9Sstevel@tonic-gate 		ntdp = lookup(h2);
4277c478bd9Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
4287c478bd9Sstevel@tonic-gate 			if (*cp++ != '=')  /* better be defining it now */
4297c478bd9Sstevel@tonic-gate 				expected("tdefdecl/'('", "=", cp - 1);
4307c478bd9Sstevel@tonic-gate 			cp = tdefdecl(cp, h2, rtdp);
4317c478bd9Sstevel@tonic-gate 			ntdp = malloc(sizeof (*ntdp));
4327c478bd9Sstevel@tonic-gate 			ntdp->type = TYPEOF;
4337c478bd9Sstevel@tonic-gate 			ntdp->data.tdesc = *rtdp;
4347c478bd9Sstevel@tonic-gate 			addhash(ntdp, h2);
4357c478bd9Sstevel@tonic-gate 		} else { /* that type is already defined */
4367c478bd9Sstevel@tonic-gate 			*rtdp = malloc(sizeof (**rtdp));
4377c478bd9Sstevel@tonic-gate 			(*rtdp)->type = TYPEOF;
4387c478bd9Sstevel@tonic-gate 			(*rtdp)->data.tdesc = ntdp;
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 		break;
4417c478bd9Sstevel@tonic-gate 	case '*':
4427c478bd9Sstevel@tonic-gate 		ntdp = NULL;
4437c478bd9Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
4447c478bd9Sstevel@tonic-gate 		if (ntdp == NULL)
4457c478bd9Sstevel@tonic-gate 			expected("tdefdecl/*", "id", cp);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4487c478bd9Sstevel@tonic-gate 		(*rtdp)->type = POINTER;
4497c478bd9Sstevel@tonic-gate 		(*rtdp)->size = model->pointersize;
4507c478bd9Sstevel@tonic-gate 		(*rtdp)->name = "pointer";
4517c478bd9Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 	case 'f':
4547c478bd9Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
4557c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4567c478bd9Sstevel@tonic-gate 		(*rtdp)->type = FUNCTION;
4577c478bd9Sstevel@tonic-gate 		(*rtdp)->size = model->pointersize;
4587c478bd9Sstevel@tonic-gate 		(*rtdp)->name = "function";
4597c478bd9Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	case 'a':
4627c478bd9Sstevel@tonic-gate 		cp++;
4637c478bd9Sstevel@tonic-gate 		if (*cp++ != 'r')
4647c478bd9Sstevel@tonic-gate 			expected("tdefdecl/a", "r", cp - 1);
4657c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4667c478bd9Sstevel@tonic-gate 		(*rtdp)->type = ARRAY;
4677c478bd9Sstevel@tonic-gate 		(*rtdp)->name = "array";
4687c478bd9Sstevel@tonic-gate 		cp = arraydef(cp, rtdp);
4697c478bd9Sstevel@tonic-gate 		break;
4707c478bd9Sstevel@tonic-gate 	case 'x':
4717c478bd9Sstevel@tonic-gate 		c = *++cp;
4727c478bd9Sstevel@tonic-gate 		if (c != 's' && c != 'u' && c != 'e')
4737c478bd9Sstevel@tonic-gate 			expected("tdefdecl/x", "[sue]", cp - 1);
4747c478bd9Sstevel@tonic-gate 		cp = name(cp + 1, &w);
4757c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4767c478bd9Sstevel@tonic-gate 		(*rtdp)->type = FORWARD;
4777c478bd9Sstevel@tonic-gate 		(*rtdp)->name = w;
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	case 'B': /* volatile */
4807c478bd9Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
4817c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4827c478bd9Sstevel@tonic-gate 		(*rtdp)->type = VOLATILE;
4837c478bd9Sstevel@tonic-gate 		(*rtdp)->size = 0;
4847c478bd9Sstevel@tonic-gate 		(*rtdp)->name = "volatile";
4857c478bd9Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
4867c478bd9Sstevel@tonic-gate 		break;
4877c478bd9Sstevel@tonic-gate 	case 'k': /* const */
4887c478bd9Sstevel@tonic-gate 		cp = tdefdecl(cp + 1, h, &ntdp);
4897c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
4907c478bd9Sstevel@tonic-gate 		(*rtdp)->type = CONST;
4917c478bd9Sstevel@tonic-gate 		(*rtdp)->size = 0;
4927c478bd9Sstevel@tonic-gate 		(*rtdp)->name = "const";
4937c478bd9Sstevel@tonic-gate 		(*rtdp)->data.tdesc = ntdp;
4947c478bd9Sstevel@tonic-gate 		break;
4957c478bd9Sstevel@tonic-gate 	case '0': case '1': case '2': case '3':	case '4':
4967c478bd9Sstevel@tonic-gate 	case '5': case '6': case '7': case '8': case '9':
4977c478bd9Sstevel@tonic-gate 		/* gcc equiv to another type */
4987c478bd9Sstevel@tonic-gate 		cp = id(cp, &h2);
4997c478bd9Sstevel@tonic-gate 		ntdp = lookup(h2);
5007c478bd9Sstevel@tonic-gate 		if (ntdp == NULL) {  /* if that type isn't defined yet */
5017c478bd9Sstevel@tonic-gate 			/* better be defining it now */
5027c478bd9Sstevel@tonic-gate 			if (*cp++ != '=') {
5037c478bd9Sstevel@tonic-gate 				if (h != h2)
5047c478bd9Sstevel@tonic-gate 					expected("tdefdecl/'0-9'", "=", cp - 1);
5057c478bd9Sstevel@tonic-gate 				/* defined in terms of itself */
5067c478bd9Sstevel@tonic-gate 				*rtdp = malloc(sizeof (**rtdp));
5077c478bd9Sstevel@tonic-gate 				(*rtdp)->type = INTRINSIC;
5087c478bd9Sstevel@tonic-gate 				(*rtdp)->name = "void";
5097c478bd9Sstevel@tonic-gate 				(*rtdp)->size = 0;
5107c478bd9Sstevel@tonic-gate 			} else {
5117c478bd9Sstevel@tonic-gate 				cp = tdefdecl(cp, h2, rtdp);
5127c478bd9Sstevel@tonic-gate 				ntdp = malloc(sizeof (*ntdp));
5137c478bd9Sstevel@tonic-gate 				ntdp->type = TYPEOF;
5147c478bd9Sstevel@tonic-gate 				ntdp->data.tdesc = *rtdp;
5157c478bd9Sstevel@tonic-gate 				addhash(ntdp, h2);
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 		} else { /* that type is already defined */
5187c478bd9Sstevel@tonic-gate 			*rtdp = malloc(sizeof (**rtdp));
5197c478bd9Sstevel@tonic-gate 			(*rtdp)->type = TYPEOF;
5207c478bd9Sstevel@tonic-gate 			(*rtdp)->data.tdesc = ntdp;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 	case 'u':
5247c478bd9Sstevel@tonic-gate 	case 's':
5257c478bd9Sstevel@tonic-gate 		cp++;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		*rtdp = malloc(sizeof (**rtdp));
5287c478bd9Sstevel@tonic-gate 		(*rtdp)->name = NULL;
5297c478bd9Sstevel@tonic-gate 		cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
5307c478bd9Sstevel@tonic-gate 		break;
5317c478bd9Sstevel@tonic-gate 	default:
5327c478bd9Sstevel@tonic-gate 		expected("tdefdecl", "<type code>", cp);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	return (cp);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static char *
intrinsic(char * cp,struct tdesc ** rtdp)5387c478bd9Sstevel@tonic-gate intrinsic(char *cp, struct tdesc **rtdp)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	struct tdesc *tdp;
5417c478bd9Sstevel@tonic-gate 	int size;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	cp = number(cp, &size);
5447c478bd9Sstevel@tonic-gate 	tdp = malloc(sizeof (*tdp));
5457c478bd9Sstevel@tonic-gate 	tdp->type = INTRINSIC;
5467c478bd9Sstevel@tonic-gate 	tdp->size = size;
5477c478bd9Sstevel@tonic-gate 	tdp->name = NULL;
5487c478bd9Sstevel@tonic-gate 	debug(3, NULL, "intrinsic: size=%ld", size);
5497c478bd9Sstevel@tonic-gate 	*rtdp = tdp;
5507c478bd9Sstevel@tonic-gate 	return (cp);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static char *
soudef(char * cp,enum type type,struct tdesc ** rtdp)5547c478bd9Sstevel@tonic-gate soudef(char *cp, enum type type, struct tdesc **rtdp)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	struct mlist **next_pp, *prev_p = NULL;
5577c478bd9Sstevel@tonic-gate 	char *w;
5587c478bd9Sstevel@tonic-gate 	int size;
5597c478bd9Sstevel@tonic-gate 	struct tdesc *tdp;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	cp = number(cp, &size);
5627c478bd9Sstevel@tonic-gate 	(*rtdp)->size = size;
5637c478bd9Sstevel@tonic-gate 	(*rtdp)->type = type; /* s or u */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * An '@' here indicates a bitmask follows.   This is so the
5677c478bd9Sstevel@tonic-gate 	 * compiler can pass information to debuggers about how structures
5687c478bd9Sstevel@tonic-gate 	 * are passed in the v9 world.  We don't need this information
5697c478bd9Sstevel@tonic-gate 	 * so we skip over it.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	if (cp[0] == '@')
5727c478bd9Sstevel@tonic-gate 		cp += 3;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	debug(3, cp, "soudef: %s size=%d",
5757c478bd9Sstevel@tonic-gate 	    (*rtdp)->name ? (*rtdp)->name : "(anonsou)",
5767c478bd9Sstevel@tonic-gate 	    (*rtdp)->size);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	next_pp = &((*rtdp)->data.members.forw); /* head for forward linklist */
5797c478bd9Sstevel@tonic-gate 	/* fill up the fields */
5807c478bd9Sstevel@tonic-gate 	while ((*cp != '"') && (*cp != ';')) { /* signifies end of fields */
5817c478bd9Sstevel@tonic-gate 		int h;
5827c478bd9Sstevel@tonic-gate 		struct mlist *mlp = malloc(sizeof (*mlp));
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		mlp->prev = prev_p;	/* links for the backward list */
5857c478bd9Sstevel@tonic-gate 		prev_p = mlp;
5867c478bd9Sstevel@tonic-gate 		*next_pp = mlp;		/* links for the forward list */
5877c478bd9Sstevel@tonic-gate 		next_pp = &mlp->next;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		cp = name(cp, &w);
5907c478bd9Sstevel@tonic-gate 		mlp->name = w;
5917c478bd9Sstevel@tonic-gate 		cp = id(cp, &h);
5927c478bd9Sstevel@tonic-gate 		/*
5937c478bd9Sstevel@tonic-gate 		 * find the tdesc struct in the hash table for this type
5947c478bd9Sstevel@tonic-gate 		 * and stick a ptr in here
5957c478bd9Sstevel@tonic-gate 		 */
5967c478bd9Sstevel@tonic-gate 		tdp = lookup(h);
5977c478bd9Sstevel@tonic-gate 		if (tdp == NULL) { /* not in hash list */
5987c478bd9Sstevel@tonic-gate 			debug(3, NULL, "      defines %s (%d)", w, h);
5997c478bd9Sstevel@tonic-gate 			if (*cp++ != '=')
6007c478bd9Sstevel@tonic-gate 				expected("soudef", "=", cp - 1);
6017c478bd9Sstevel@tonic-gate 			cp = tdefdecl(cp, h, &tdp);
6027c478bd9Sstevel@tonic-gate 			addhash(tdp, h);
6037c478bd9Sstevel@tonic-gate 			debug(4, cp, "     soudef now looking at    ");
6047c478bd9Sstevel@tonic-gate 			cp++;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		} else {
6077c478bd9Sstevel@tonic-gate 			debug(3, NULL, "      refers to %s (%d, %s)",
6087c478bd9Sstevel@tonic-gate 			    w ? w : "anon", h, tdp->name ? tdp->name : "anon");
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		mlp->fdesc = tdp;
6127c478bd9Sstevel@tonic-gate 		cp = offsize(cp, mlp);	/* cp is now pointing to next field */
6137c478bd9Sstevel@tonic-gate 		if (*cp == '\\')	/* could be a continuation */
6147c478bd9Sstevel@tonic-gate 			cp = get_continuation();
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	(*rtdp)->data.members.back = prev_p;	/* head for backward linklist */
6177c478bd9Sstevel@tonic-gate 	return (cp);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static char *
offsize(char * cp,struct mlist * mlp)6217c478bd9Sstevel@tonic-gate offsize(char *cp, struct mlist *mlp)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int offset, size;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (*cp == ',')
6267c478bd9Sstevel@tonic-gate 		cp++;
6277c478bd9Sstevel@tonic-gate 	cp = number(cp, &offset);
6287c478bd9Sstevel@tonic-gate 	if (*cp++ != ',')
6297c478bd9Sstevel@tonic-gate 		expected("offsize/2", ",", cp - 1);
6307c478bd9Sstevel@tonic-gate 	cp = number(cp, &size);
6317c478bd9Sstevel@tonic-gate 	if (*cp++ != ';')
6327c478bd9Sstevel@tonic-gate 		expected("offsize/3", ";", cp - 1);
6337c478bd9Sstevel@tonic-gate 	mlp->offset = offset;
6347c478bd9Sstevel@tonic-gate 	mlp->size = size;
6357c478bd9Sstevel@tonic-gate 	return (cp);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static char *
arraydef(char * cp,struct tdesc ** rtdp)6397c478bd9Sstevel@tonic-gate arraydef(char *cp, struct tdesc **rtdp)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	int h;
6427c478bd9Sstevel@tonic-gate 	int start, end;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	cp = id(cp, &h);
6457c478bd9Sstevel@tonic-gate 	if (*cp++ != ';')
6467c478bd9Sstevel@tonic-gate 		expected("arraydef/1", ";", cp - 1);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	(*rtdp)->data.ardef = malloc(sizeof (struct ardef));
6497c478bd9Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices = malloc(sizeof (struct element));
6507c478bd9Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->index_type = lookup(h);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	cp = number(cp, &start); /* lower */
6537c478bd9Sstevel@tonic-gate 	if (*cp++ != ';')
6547c478bd9Sstevel@tonic-gate 		expected("arraydef/2", ";", cp - 1);
6557c478bd9Sstevel@tonic-gate 	cp = number(cp, &end);	/* upper */
6567c478bd9Sstevel@tonic-gate 	if (*cp++ != ';')
6577c478bd9Sstevel@tonic-gate 		expected("arraydef/3", ";", cp - 1);
6587c478bd9Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->range_start = start;
6597c478bd9Sstevel@tonic-gate 	(*rtdp)->data.ardef->indices->range_end = end;
6607c478bd9Sstevel@tonic-gate #if 0
6617c478bd9Sstevel@tonic-gate 	if (isdigit(*cp)) {
6627c478bd9Sstevel@tonic-gate 		cp = number(cp, &contents_type); /* lower */
6637c478bd9Sstevel@tonic-gate 		tdp = lookup(contents_type);
6647c478bd9Sstevel@tonic-gate 		if (tdp != NULL) {
6657c478bd9Sstevel@tonic-gate 			(*rtdp)->data.ardef->contents = tdp;
6667c478bd9Sstevel@tonic-gate 		} else {
6677c478bd9Sstevel@tonic-gate 			if (*cp != '=')
6687c478bd9Sstevel@tonic-gate 				expected("arraydef/4", "=", cp);
6697c478bd9Sstevel@tonic-gate 			cp = tdefdecl(cp + 1, h, &tdp);
6707c478bd9Sstevel@tonic-gate 			addhash(tdp, h); /* for *(x,y) types */
6717c478bd9Sstevel@tonic-gate 			(*rtdp)->data.ardef->contents = tdp;
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 	} /* else  */
6747c478bd9Sstevel@tonic-gate #endif
6757c478bd9Sstevel@tonic-gate 	cp = tdefdecl(cp, h, &((*rtdp)->data.ardef->contents));
6767c478bd9Sstevel@tonic-gate 	return (cp);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static void
enumdef(char * cp,struct tdesc ** rtdp)6807c478bd9Sstevel@tonic-gate enumdef(char *cp, struct tdesc **rtdp)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	struct elist *elp, **prev;
6837c478bd9Sstevel@tonic-gate 	char *w;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	(*rtdp)->type = ENUM;
6867c478bd9Sstevel@tonic-gate 	(*rtdp)->data.emem = NULL;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	prev = &((*rtdp)->data.emem);
6897c478bd9Sstevel@tonic-gate 	while (*cp != ';') {
6907c478bd9Sstevel@tonic-gate 		elp = malloc(sizeof (*elp));
6917c478bd9Sstevel@tonic-gate 		elp->next = NULL;
6927c478bd9Sstevel@tonic-gate 		*prev = elp;
6937c478bd9Sstevel@tonic-gate 		cp = name(cp, &w);
6947c478bd9Sstevel@tonic-gate 		elp->name = w;
6957c478bd9Sstevel@tonic-gate 		cp = number(cp, &elp->number);
6967c478bd9Sstevel@tonic-gate 		debug(3, NULL, "enum %s: %s=%ld",
6977c478bd9Sstevel@tonic-gate 		    (*rtdp)->name ? (*rtdp)->name : "(anon enum)",
6987c478bd9Sstevel@tonic-gate 		    elp->name, elp->number);
6997c478bd9Sstevel@tonic-gate 		prev = &elp->next;
7007c478bd9Sstevel@tonic-gate 		if (*cp++ != ',')
7017c478bd9Sstevel@tonic-gate 			expected("enumdef", ",", cp - 1);
7027c478bd9Sstevel@tonic-gate 		if (*cp == '\\')
7037c478bd9Sstevel@tonic-gate 			cp = get_continuation();
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * Add a node to the hash queues.
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate static void
addhash(struct tdesc * tdp,int num)7117c478bd9Sstevel@tonic-gate addhash(struct tdesc *tdp, int num)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	int hash = HASH(num);
7147c478bd9Sstevel@tonic-gate 	struct tdesc *ttdp;
7157c478bd9Sstevel@tonic-gate 	char added_num = 0, added_name = 0;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * If it already exists in the hash table don't add it again
7197c478bd9Sstevel@tonic-gate 	 * (but still check to see if the name should be hashed).
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	ttdp = lookup(num);
7227c478bd9Sstevel@tonic-gate 	if (ttdp == NULL) {
7237c478bd9Sstevel@tonic-gate 		tdp->id = num;
7247c478bd9Sstevel@tonic-gate 		tdp->hash = hash_table[hash];
7257c478bd9Sstevel@tonic-gate 		hash_table[hash] = tdp;
7267c478bd9Sstevel@tonic-gate 		added_num = 1;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (tdp->name != NULL) {
7307c478bd9Sstevel@tonic-gate 		ttdp = lookupname(tdp->name);
7317c478bd9Sstevel@tonic-gate 		if (ttdp == NULL) {
7327c478bd9Sstevel@tonic-gate 			hash = compute_sum(tdp->name);
7337c478bd9Sstevel@tonic-gate 			tdp->next = name_table[hash];
7347c478bd9Sstevel@tonic-gate 			name_table[hash] = tdp;
7357c478bd9Sstevel@tonic-gate 			added_name = 1;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	if (!added_num && !added_name) {
7397c478bd9Sstevel@tonic-gate 		fprintf(stderr, "stabs: broken hash\n");
7407c478bd9Sstevel@tonic-gate 		exit(1);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate struct tdesc *
lookupname(char * name)7457c478bd9Sstevel@tonic-gate lookupname(char *name)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	int hash = compute_sum(name);
7487c478bd9Sstevel@tonic-gate 	struct tdesc *tdp, *ttdp = NULL;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	for (tdp = name_table[hash]; tdp != NULL; tdp = tdp->next) {
7517c478bd9Sstevel@tonic-gate 		if (tdp->name != NULL && strcmp(tdp->name, name) == 0) {
7527c478bd9Sstevel@tonic-gate 			if (tdp->type == STRUCT || tdp->type == UNION ||
7537c478bd9Sstevel@tonic-gate 			    tdp->type == ENUM || tdp->type == INTRINSIC)
7547c478bd9Sstevel@tonic-gate 				return (tdp);
7557c478bd9Sstevel@tonic-gate 			if (tdp->type == TYPEOF)
7567c478bd9Sstevel@tonic-gate 				ttdp = tdp;
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	return (ttdp);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate static int
compute_sum(char * w)7637c478bd9Sstevel@tonic-gate compute_sum(char *w)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	char c;
7667c478bd9Sstevel@tonic-gate 	int sum;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	for (sum = 0; (c = *w) != '\0'; sum += c, w++)
7697c478bd9Sstevel@tonic-gate 		;
7707c478bd9Sstevel@tonic-gate 	return (HASH(sum));
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate static void
reset(void)7747c478bd9Sstevel@tonic-gate reset(void)
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	longjmp(resetbuf, 1);
7777c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
7787c478bd9Sstevel@tonic-gate }
779