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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * This file is a sewer.
39 #include <uts/common/sys/ctf.h>
45 #define HASH(NUM) ((int)(NUM & (BUCKETS - 1)))
48 #define TYPEPAIRMULT 10000
49 #define MAKETYPEID(file, num) ((file) * TYPEPAIRMULT + num)
50 #define TYPEFILE(tid) ((tid) / TYPEPAIRMULT)
51 #define TYPENUM(tid) ((tid) % TYPEPAIRMULT)
53 #define expected(a, b, c) _expected(a, b, c, __LINE__)
55 static int faketypenumber = 100000000;
57 static tdesc_t *hash_table[BUCKETS];
58 static tdesc_t *name_table[BUCKETS];
60 list_t *typedbitfldmems;
62 static void reset(void);
63 static jmp_buf resetbuf;
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);
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);
79 int debug_parse = DEBUG_PARSE;
83 parse_debug(int level, char *cp, const char *fmt, ...)
90 if (level > debug_level || !debug_parse)
94 for (i = 0; i < 30; i++) {
101 (void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
108 vadebug(level, buf, ap);
112 /* Report unexpected syntax in stabs. */
115 const char *who, /* what function, or part thereof, is reporting */
116 const char *what, /* what was expected */
117 const char *where, /* where we were in the line of input */
120 fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
121 fprintf(stderr, "code line: %d, file %s\n", line,
122 (curhdr ? curhdr : "NO FILE"));
128 parse_init(tdata_t *td __unused)
132 for (i = 0; i < BUCKETS; i++) {
133 hash_table[i] = NULL;
134 name_table[i] = NULL;
137 if (typedbitfldmems != NULL) {
138 list_free(typedbitfldmems, NULL, NULL);
139 typedbitfldmems = NULL;
144 parse_finish(tdata_t *td)
146 td->td_nextid = ++faketypenumber;
154 tdp = xcalloc(sizeof (*tdp));
155 tdp->t_type = TYPEDEF_UNRES;
162 read_tid(char *cp, tdesc_t **tdpp)
172 if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
174 if (tdp->t_id && tdp->t_id != tid) {
175 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
177 ntdp->t_type = TYPEDEF;
182 } else if ((tdp = lookup(tid)) == NULL)
183 tdp = unres_new(tid);
190 parse_fun(char *cp, iidesc_t *ii)
194 tdesc_t **args = NULL;
200 * name:F global function
201 * name:f static function
205 iitype = II_NOT; /* not interesting */
217 expected("parse_nfun", "[PfF]", cp - 1);
220 if (!(cp = read_tid(cp, &tdp)))
224 args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
226 while (*cp && *++cp) {
233 if (nargs > FUNCARG_DEF)
234 args = xrealloc(args, sizeof (tdesc_t *) * nargs);
235 if (!(cp = read_tid(cp, &args[nargs - 1])))
239 ii->ii_type = iitype;
241 ii->ii_nargs = nargs;
249 parse_sym(char *cp, iidesc_t *ii)
255 * name:G global variable
256 * name:S static variable
273 iitype = II_NOT; /* not interesting */
276 expected("parse_sym", "[GprSV(]", cp - 1);
279 if (!(cp = read_tid(cp, &tdp)))
282 ii->ii_type = iitype;
289 parse_type(char *cp, iidesc_t *ii)
295 expected("parse_type", "t (type)", cp - 1);
298 if ((tdp = lookup(tid)) == NULL) {
300 expected("parse_type", "= (definition)", cp - 1);
302 (void) tdefdecl(cp, tid, &tdp);
304 if (tdp->t_id == tid) {
305 assert(tdp->t_type != TYPEDEF);
306 assert(!lookup(tdp->t_id));
308 if (!streq(tdp->t_name, ii->ii_name)) {
309 ntdp = xcalloc(sizeof (*ntdp));
310 ntdp->t_name = xstrdup(ii->ii_name);
311 ntdp->t_type = TYPEDEF;
313 tdp->t_id = faketypenumber++;
316 } else if (tdp->t_id == 0) {
317 assert(tdp->t_type == FORWARD ||
318 tdp->t_type == INTRINSIC);
320 if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
321 ntdp = xcalloc(sizeof (*ntdp));
322 ntdp->t_name = xstrdup(ii->ii_name);
323 ntdp->t_type = TYPEDEF;
325 tdp->t_id = faketypenumber++;
328 } else if (tdp->t_id != tid) {
329 ntdp = xcalloc(sizeof (*ntdp));
330 ntdp->t_name = xstrdup(ii->ii_name);
331 ntdp->t_type = TYPEDEF;
336 if (tagadd(ii->ii_name, tid, tdp) < 0)
340 ii->ii_type = II_TYPE;
346 parse_sou(char *cp, iidesc_t *idp)
352 expected("parse_sou", "T (sou)", cp - 1);
356 expected("parse_sou", "= (definition)", cp - 1);
358 parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
359 idp->ii_name : "(anon)");
360 if ((rtdp = lookup(tid)) != NULL) {
361 if (idp->ii_name != NULL) {
362 if (rtdp->t_name != NULL &&
363 strcmp(rtdp->t_name, idp->ii_name) != 0) {
366 tdp = xcalloc(sizeof (*tdp));
367 tdp->t_name = xstrdup(idp->ii_name);
368 tdp->t_type = TYPEDEF;
370 addhash(tdp, tid); /* for *(x,y) types */
371 parse_debug(3, NULL, " %s defined as %s(%d)",
372 idp->ii_name, tdesc_name(rtdp), tid);
373 } else if (rtdp->t_name == NULL) {
374 rtdp->t_name = xstrdup(idp->ii_name);
379 rtdp = xcalloc(sizeof (*rtdp));
380 rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
386 (void) soudef(cp, STRUCT, &rtdp);
389 (void) soudef(cp, UNION, &rtdp);
395 expected("parse_sou", "<tag type s/u/e>", cp - 1);
399 idp->ii_type = II_SOU;
400 idp->ii_dtype = rtdp;
405 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
408 iitype_t (*parse)(char *, iidesc_t *);
414 if (setjmp(resetbuf))
418 ii = iidesc_new(NULL);
419 cp = name(cp, &ii->ii_name);
421 switch (stab->n_type) {
444 parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
445 bzero(&resetbuf, sizeof (resetbuf));
450 bzero(&resetbuf, sizeof (resetbuf));
452 if (rc < 0 || ii->ii_type == II_NOT) {
453 iidesc_free(ii, NULL);
463 * Check if we have this node in the hash table already
468 int bucket = HASH(h);
469 tdesc_t *tdp = hash_table[bucket];
471 while (tdp != NULL) {
484 for (c = *cp++; isspace(c); c = *cp++);
490 name(char *cp, char **w)
499 else if (isalpha(c) || strchr("_.$", c)) {
500 for (c = *cp++; isalnum(c) || strchr(" _.$", c); c = *cp++)
506 while (orig < cp - 1)
509 *w = new - (len - 1);
517 number(char *cp, int *n)
521 *n = (int)strtol(cp, &next, 10);
523 expected("number", "<number>", cp);
532 if (*cp == '(') { /* SunPro style */
534 cp = number(cp, &n1);
536 expected("id", ",", cp - 1);
537 cp = number(cp, &n2);
539 expected("id", ")", cp - 1);
540 *h = MAKETYPEID(n1, n2);
541 } else if (isdigit(*cp)) { /* gcc style */
542 cp = number(cp, &n1);
545 expected("id", "(/0-9", cp);
551 tagadd(char *w, int h, tdesc_t *tdp)
556 if (!(otdp = lookup(h)))
558 else if (otdp != tdp) {
559 warning("duplicate entry\n");
560 warning(" old: %s %d (%d,%d)\n", tdesc_name(otdp),
561 otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
562 warning(" new: %s %d (%d,%d)\n", tdesc_name(tdp),
563 tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
571 tdefdecl(char *cp, int h, tdesc_t **rtdp)
578 parse_debug(3, cp, "tdefdecl h=%d", h);
581 switch (type = *cp) {
582 case 'b': /* integer */
584 cp = intrinsic(cp, rtdp);
586 case '(': /* equiv to another type */
590 if (ntdp != NULL && *cp == '=') {
591 if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
593 * The 6.2 compiler, and possibly others, will
594 * sometimes emit the same stab for a forward
595 * declaration twice. That is, "(1,2)=xsfoo:"
596 * will sometimes show up in two different
597 * places. This is, of course, quite fun. We
598 * want CTF to work in spite of the compiler,
599 * so we'll let this one through.
604 if (!strchr("sue", *c2++)) {
605 expected("tdefdecl/x-redefine", "[sue]",
610 if (strcmp(nm, ntdp->t_name) != 0) {
611 terminate("Stabs error: Attempt to "
612 "redefine type (%d,%d) as "
613 "something else: %s\n",
614 TYPEFILE(h2), TYPENUM(h2),
619 h2 = faketypenumber++;
622 terminate("Stabs error: Attempting to "
623 "redefine type (%d,%d)\n", TYPEFILE(h2),
628 if (ntdp == NULL) { /* if that type isn't defined yet */
630 /* record it as unresolved */
631 parse_debug(3, NULL, "tdefdecl unres type %d",
633 *rtdp = calloc(sizeof (**rtdp), 1);
634 (*rtdp)->t_type = TYPEDEF_UNRES;
640 /* define a new type */
641 cp = tdefdecl(cp, h2, rtdp);
642 if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
643 ntdp = calloc(sizeof (*ntdp), 1);
644 ntdp->t_type = TYPEDEF;
645 ntdp->t_tdesc = *rtdp;
651 } else { /* that type is already defined */
652 if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
656 "No duplicate typedef anon for ref");
663 cp = tdefdecl(cp + 1, h, &ntdp);
665 expected("tdefdecl/*", "id", cp);
668 ntdp->t_id = faketypenumber++;
670 *rtdp = xcalloc(sizeof (**rtdp));
671 (*rtdp)->t_type = POINTER;
674 (*rtdp)->t_tdesc = ntdp;
677 cp = tdefdecl(cp + 1, h, &ntdp);
678 *rtdp = xcalloc(sizeof (**rtdp));
679 (*rtdp)->t_type = FUNCTION;
682 (*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
684 * The 6.1 compiler will sometimes generate incorrect stabs for
685 * function pointers (it'll get the return type wrong). This
686 * causes merges to fail. We therefore treat function pointers
687 * as if they all point to functions that return int. When
688 * 4432549 is fixed, the lookupname() call below should be
689 * replaced with `ntdp'.
691 (*rtdp)->t_fndef->fn_ret = lookupname("int");
697 expected("tdefdecl/[az]", "r", cp - 1);
698 *rtdp = xcalloc(sizeof (**rtdp));
699 (*rtdp)->t_type = ARRAY;
701 cp = arraydef(cp, rtdp);
705 if (c != 's' && c != 'u' && c != 'e')
706 expected("tdefdecl/x", "[sue]", cp - 1);
707 cp = name(cp + 1, &w);
709 ntdp = xcalloc(sizeof (*ntdp));
710 ntdp->t_type = FORWARD;
713 * We explicitly don't set t_id here - the caller will do it.
714 * The caller may want to use a real type ID, or they may
715 * choose to make one up.
721 case 'B': /* volatile */
722 cp = tdefdecl(cp + 1, h, &ntdp);
725 ntdp->t_id = faketypenumber++;
727 *rtdp = xcalloc(sizeof (**rtdp));
728 (*rtdp)->t_type = VOLATILE;
730 (*rtdp)->t_tdesc = ntdp;
734 case 'k': /* const */
735 cp = tdefdecl(cp + 1, h, &ntdp);
738 ntdp->t_id = faketypenumber++;
740 *rtdp = xcalloc(sizeof (**rtdp));
741 (*rtdp)->t_type = CONST;
743 (*rtdp)->t_tdesc = ntdp;
747 case 'K': /* restricted */
748 cp = tdefdecl(cp + 1, h, &ntdp);
751 ntdp->t_id = faketypenumber++;
753 *rtdp = xcalloc(sizeof (**rtdp));
754 (*rtdp)->t_type = RESTRICT;
756 (*rtdp)->t_tdesc = ntdp;
764 *rtdp = xcalloc(sizeof (**rtdp));
765 (*rtdp)->t_name = NULL;
766 cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
769 expected("tdefdecl", "<type code>", cp);
775 intrinsic(char *cp, tdesc_t **rtdp)
777 intr_t *intr = xcalloc(sizeof (intr_t));
783 intr->intr_type = INTR_INT;
785 intr->intr_signed = 1;
787 expected("intrinsic/b", "[su]", cp);
790 if (strchr("cbv", *cp))
791 intr->intr_iformat = *cp++;
793 cp = number(cp, &width);
795 expected("intrinsic/b", "; (post-width)", cp - 1);
797 cp = number(cp, &intr->intr_offset);
799 expected("intrinsic/b", "; (post-offset)", cp - 1);
801 cp = number(cp, &intr->intr_nbits);
805 intr->intr_type = INTR_REAL;
806 for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
807 fmt = fmt * 10 + (*(cp + i) - '0');
809 if (fmt < 1 || fmt > CTF_FP_MAX)
810 expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
812 intr->intr_fformat = fmt;
816 expected("intrinsic/R", ";", cp - 1);
817 cp = number(cp, &width);
819 intr->intr_nbits = width * 8;
823 tdp = xcalloc(sizeof (*tdp));
824 tdp->t_type = INTRINSIC;
828 parse_debug(3, NULL, "intrinsic: size=%d", width);
835 bitintrinsic(tdesc_t *template, int nbits)
837 tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
839 newtdp->t_name = xstrdup(template->t_name);
840 newtdp->t_id = faketypenumber++;
841 newtdp->t_type = INTRINSIC;
842 newtdp->t_size = template->t_size;
843 newtdp->t_intr = xmalloc(sizeof (intr_t));
844 bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
845 newtdp->t_intr->intr_nbits = nbits;
851 offsize(char *cp, mlist_t *mlp)
857 cp = number(cp, &offset);
859 expected("offsize/2", ",", cp - 1);
860 cp = number(cp, &size);
862 expected("offsize/3", ";", cp - 1);
863 mlp->ml_offset = offset;
869 find_intrinsic(tdesc_t *tdp)
872 switch (tdp->t_type) {
887 soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
889 mlist_t *mlp, **prev;
895 cp = number(cp, &size);
896 (*rtdp)->t_size = size;
897 (*rtdp)->t_type = type; /* s or u */
900 * An '@' here indicates a bitmask follows. This is so the
901 * compiler can pass information to debuggers about how structures
902 * are passed in the v9 world. We don't need this information
903 * so we skip over it.
909 parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
912 prev = &((*rtdp)->t_members);
913 /* now fill up the fields */
914 while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
915 mlp = xcalloc(sizeof (*mlp));
921 * find the tdesc struct in the hash table for this type
922 * and stick a ptr in here
925 if (tdp == NULL) { /* not in hash list */
926 parse_debug(3, NULL, " defines %s (%d)", w, h);
930 " refers to %s (unresolved %d)",
931 (w ? w : "anon"), h);
933 cp = tdefdecl(cp, h, &tdp);
935 if (tdp->t_id && tdp->t_id != h) {
936 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
938 ntdp->t_type = TYPEDEF;
945 " soudef now looking at ");
949 parse_debug(3, NULL, " refers to %s (%d, %s)",
950 w ? w : "anon", h, tdesc_name(tdp));
953 cp = offsize(cp, mlp);
955 itdp = find_intrinsic(tdp);
956 if (itdp->t_type == INTRINSIC) {
957 if (mlp->ml_size != itdp->t_intr->intr_nbits) {
958 parse_debug(4, cp, "making %d bit intrinsic "
959 "from %s", mlp->ml_size, tdesc_name(itdp));
960 mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
963 } else if (itdp->t_type == TYPEDEF_UNRES) {
964 list_add(&typedbitfldmems, mlp);
970 /* cp is now pointing to next field */
971 prev = &mlp->ml_next;
977 arraydef(char *cp, tdesc_t **rtdp)
983 expected("arraydef/1", ";", cp - 1);
985 (*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
986 (*rtdp)->t_ardef->ad_idxtype = lookup(h);
988 cp = number(cp, &start); /* lower */
990 expected("arraydef/2", ";", cp - 1);
993 /* variable length array - treat as null dimensioned */
996 expected("arraydef/fpoff-sep", "-", cp - 1);
997 cp = number(cp, &end);
1000 /* normal fixed-dimension array */
1001 cp = number(cp, &end); /* upper */
1005 expected("arraydef/3", ";", cp - 1);
1006 (*rtdp)->t_ardef->ad_nelems = end - start + 1;
1007 cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
1009 parse_debug(3, cp, "defined array idx type %d %d-%d next ",
1016 enumdef(char *cp, tdesc_t **rtdp)
1018 elist_t *elp, **prev;
1021 (*rtdp)->t_type = ENUM;
1022 (*rtdp)->t_emem = NULL;
1024 prev = &((*rtdp)->t_emem);
1025 while (*cp != ';') {
1026 elp = xcalloc(sizeof (*elp));
1027 elp->el_next = NULL;
1031 cp = number(cp, &elp->el_number);
1032 parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
1033 elp->el_name, elp->el_number);
1034 prev = &elp->el_next;
1036 expected("enumdef", ",", cp - 1);
1041 lookup_name(tdesc_t **hash, const char *name1)
1043 int bucket = compute_sum(name1);
1044 tdesc_t *tdp, *ttdp = NULL;
1046 for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
1047 if (tdp->t_name != NULL && strcmp(tdp->t_name, name1) == 0) {
1048 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1049 tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
1051 if (tdp->t_type == TYPEDEF)
1059 lookupname(const char *name1)
1061 return (lookup_name(name_table, name1));
1065 * Add a node to the hash queues.
1068 addhash(tdesc_t *tdp, int num)
1070 int hash = HASH(num);
1072 char added_num = 0, added_name = 0;
1075 * If it already exists in the hash table don't add it again
1076 * (but still check to see if the name should be hashed).
1082 tdp->t_hash = hash_table[hash];
1083 hash_table[hash] = tdp;
1087 if (tdp->t_name != NULL) {
1088 ttdp = lookupname(tdp->t_name);
1090 hash = compute_sum(tdp->t_name);
1091 tdp->t_next = name_table[hash];
1092 name_table[hash] = tdp;
1096 if (!added_num && !added_name) {
1097 terminate("stabs: broken hash\n");
1102 compute_sum(const char *w)
1107 for (sum = 0; (c = *w) != '\0'; sum += c, w++)
1115 longjmp(resetbuf, 1);
1124 printf("checking hash\n");
1125 for (i = 0; i < BUCKETS; i++) {
1126 if (hash_table[i]) {
1127 for (tdp = hash_table[i]->t_hash;
1128 tdp && tdp != hash_table[i];
1132 terminate("cycle in hash bucket %d\n", i);
1137 if (name_table[i]) {
1138 for (tdp = name_table[i]->t_next;
1139 tdp && tdp != name_table[i];
1143 terminate("cycle in name bucket %d\n", i);
1153 resolve_typed_bitfields_cb(void *arg, void *private __unused)
1156 tdesc_t *tdp = ml->ml_type;
1158 debug(3, "Resolving typed bitfields (member %s)\n",
1159 (ml->ml_name ? ml->ml_name : "(anon)"));
1162 switch (tdp->t_type) {
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);
1169 debug(3, "using existing %d bit %s intrinsic",
1170 ml->ml_size, tdesc_name(tdp));
1188 terminate("type chain for bitfield member %s has a NULL", ml->ml_name);
1194 resolve_typed_bitfields(void)
1196 (void) list_iter(typedbitfldmems,
1197 resolve_typed_bitfields_cb, NULL);