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