1 /* $Id: db_symtab.g,v 1.13 1994/06/24 10:59:34 ceriel Exp $ */
21 extern char *strindex();
22 extern long str2long();
25 static char *DbPtr; /* current pointer in db string */
26 static int AllowName; /* set if NAME legal at this point */
30 static int last_index[2];
31 static struct outname *currnam;
34 static struct literal *get_literal_space();
35 static struct fields *get_field_space();
37 static char *string_val();
40 %start DbParser, debugger_string;
48 %token INTEGER, REAL, STRING, NAME;
51 { register p_symbol s;
59 { s = NewSymbol(str, CurrentScope, CONST, currnam); }
63 { s = NewSymbol(str, CurrentScope, TYPE, currnam); }
64 't' type_name(&(s->sy_type), s)
65 { if (! s->sy_type->ty_sym) s->sy_type->ty_sym = s;
66 if ((s->sy_type->ty_class == T_ENUM ||
67 s->sy_type->ty_class == T_SUBRANGE) &&
68 currnam->on_desc != 0) {
69 s->sy_type->ty_size = currnam->on_desc;
72 | /* tag name (only C?) */
73 { s = NewSymbol(str, CurrentScope, TAG, currnam); }
74 'T' type_name(&(s->sy_type), s)
75 { if (! s->sy_type->ty_sym) s->sy_type->ty_sym = s;
76 if (s->sy_type->ty_class != T_CROSS) {
77 resolve_cross(s->sy_type);
85 { s = NewSymbol(str, CurrentScope, MODULE, currnam); }
88 s->sy_name.nm_scope = CurrentScope;
89 CurrentScope->sc_start = currnam->on_valu;
90 CurrentScope->sc_proclevel = currnam->on_desc;
91 add_scope_addr(CurrentScope);
94 | /* external procedure */
95 { s = NewSymbol(str, FileScope, PROC, currnam); }
98 | /* private procedure */
99 { s = NewSymbol(str, CurrentScope, PROC, currnam); }
102 | /* external function */
103 { s = NewSymbol(str, FileScope, FUNCTION, currnam); }
106 | /* private function */
107 { s = NewSymbol(str, CurrentScope, FUNCTION, currnam); }
110 | /* global variable, external */
111 /* maybe we already know it; but we need
112 the type information anyway for other
115 { s = Lookup(findidf(str), FileScope, VAR);
119 } else s = NewSymbol(str, FileScope, VAR, currnam);
121 'G' type(&(s->sy_type), (int *) 0, s)
122 { if (tmp) s->sy_type = tmp; }
124 | /* static variable */
125 { s = NewSymbol(str, CurrentScope, VAR, currnam); }
126 'S' type(&(s->sy_type), (int *) 0, s)
128 | /* static variable, local scope */
129 { s = NewSymbol(str, CurrentScope, VAR, currnam); }
130 'V' type(&(s->sy_type), (int *) 0, s)
132 | /* register variable */
133 { s = NewSymbol(str, CurrentScope, REGVAR, currnam); }
134 'r' type(&(s->sy_type), (int *) 0, s)
136 | /* value parameter */
137 { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
138 'p' type(&(s->sy_type), (int *) 0, s)
139 { add_param_type('p', s); }
141 | /* value parameter but address passed */
142 { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
143 'i' type(&(s->sy_type), (int *) 0, s)
144 { add_param_type('i', s); }
146 | /* variable parameter */
147 { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
148 'v' type(&(s->sy_type), (int *) 0, s)
149 { add_param_type('v', s); }
151 | /* local variable */
152 { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
153 type_name(&(s->sy_type), s)
155 | /* lower or upper bound of array descriptor */
156 [ 'A' { upb = LBOUND; }
157 | 'Z' { upb = UBOUND; }
159 [ ['p' | ] { s = NewSymbol(str, CurrentScope, LOCVAR, currnam);
160 if (upb == UBOUND) add_param_type('Z', s);
162 | [ 'V' | 'S' ] { s = NewSymbol(str, CurrentScope, VAR, currnam); }
164 type_name(&(s->sy_type), s)
165 { p_symbol s1 = new_symbol();
171 | /* function result in Pascal; ignore ??? */
172 { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
173 'X' type_name(&(s->sy_type), s)
180 /* anything up to a ':' */
181 NAME { *s = strval; }
184 const_name(p_symbol cst;)
193 'b' integer_const(&(cst->sy_const.co_ival)) /* boolean */
196 'c' integer_const(&(cst->sy_const.co_ival)) /* character */
197 { cst->sy_type = char_type; }
199 'i' integer_const(&(cst->sy_const.co_ival)) /* integer */
200 { cst->sy_type = long_type; }
202 'r' real_const(&(cst->sy_const.co_rval)) /* real */
203 { cst->sy_type = double_type; }
205 's' string_const /* string */
206 { cst->sy_const.co_sval = string_val(strval);
207 cst->sy_type = string_type;
210 'e' type_index(type_index) ',' integer_const(&(cst->sy_const.co_ival))
211 /* enumeration constant;
212 * enumeration type, value
214 { cst->sy_type = *tp_lookup(type_index); }
217 'S' type_index(type_index)
218 { cst->sy_type = *tp_lookup(type_index);
219 cst->sy_const.co_setval = p =
220 Malloc((unsigned) cst->sy_type->ty_size);
222 [ ',' integer_const(&iconst)
226 * settype, values of the bytes
232 integer_const(long *iconst;)
235 [ '+' | '-' { sign = 1; } ]?
236 INTEGER { *iconst = sign ? -ival : ival; }
239 real_const(double *f;)
242 [ '+' | '-' { sign = 1; } ]?
243 REAL { *f = sign ? fval : -fval; }
248 STRING /* has SINGLE quotes! */
251 type_name(p_type *t; p_symbol sy;)
252 { int type_index[2]; p_type *p; }
254 type_index(type_index) { p = tp_lookup(type_index); }
256 { if (*p && (*p)->ty_class != 0 &&
257 (*p)->ty_class != T_CROSS) {
258 error("Redefining (%d,%d) %d",
265 type(p, type_index, sy)
267 { if (*t && ! *p) *p = *t;
268 else if (*t) **t = **p;
271 { if (*p == 0) *p = new_type();
276 type_index(int *type_index;)
279 INTEGER { type_index[0] = 0; type_index[1] = ival; }
281 '(' INTEGER { type_index[0] = ival; }
282 ',' INTEGER { type_index[1] = ival; }
285 { last_index[0] = type_index[0];
286 last_index[1] = type_index[1];
290 function(p_symbol p;)
292 { p->sy_type = new_type();
293 p->sy_type->ty_class = T_PROCEDURE;
294 p->sy_type->ty_size = pointer_size;
296 type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
297 { if (CurrentScope != FileScope &&
299 /* if saw_code is not set, it is a nested
306 p->sy_name.nm_scope = CurrentScope;
307 CurrentScope->sc_start = currnam->on_valu;
308 add_scope_addr(CurrentScope);
309 CurrentScope->sc_proclevel = currnam->on_desc;
315 { p->sy_type = new_type();
316 p->sy_type->ty_class = T_PROCEDURE;
317 p->sy_type->ty_size = pointer_size;
318 if (CurrentScope != FileScope &&
320 /* if saw_code is not set, it is a nested
327 p->sy_name.nm_scope = CurrentScope;
328 CurrentScope->sc_start = currnam->on_valu;
329 add_scope_addr(CurrentScope);
330 CurrentScope->sc_proclevel = currnam->on_desc;
333 type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
336 type(p_type *ptp; int *type_index; p_symbol sy;)
337 { register p_type tp = *ptp ? *ptp : new_type();
338 p_type t1 = 0, t2 = 0;
347 /* type cross reference */
348 /* these are used in C for references to a struct, union or
349 * enum that has not been declared (yet)
353 { tclass = T_STRUCT; }
355 { tclass = T_UNION; }
361 { sy = Lookfromscope(str2idf(str,0),TAG,CurrentScope);
363 (sy->sy_type->ty_class == tclass ||
364 sy->sy_type->ty_class == T_CROSS)) {
365 if (tp != *ptp) free_type(tp);
369 tp->ty_class = T_CROSS;
370 tp->ty_size = tclass;
371 sy = NewSymbol(str, CurrentScope, TAG, (struct outname *) 0);
377 /* the integer_const's represent the lower and the upper bound.
378 * A subrange type defined as subrange of itself is an integer type.
379 * If the second integer_const == 0, but the first is not, we
380 * have a floating point type with size equal to the first
382 * Upperbound -1 means unsigned int or unsigned long.
384 'r' type_index(tp_index) ';'
385 [ 'A' integer_const(&ic1) { A_used = 1; }
386 | integer_const(&ic1)
389 [ 'A' integer_const(&ic2) { A_used |= 2; }
390 | integer_const(&ic2)
391 | 'Z' integer_const(&ic2) { A_used |= 0200; }
393 { if (tp != *ptp) free_type(tp);
394 tp = subrange_type(A_used,
401 /* array; first type is bound type, next type
404 'a' type(&t1, (int *) 0, (p_symbol) 0) ';' type(&t2, (int *) 0, (p_symbol) 0)
405 { if (tp != *ptp) free_type(tp);
406 tp = array_type(t1, t2);
410 's' { tp->ty_class = T_STRUCT; }
411 structure_type(tp, sy)
414 'u' { tp->ty_class = T_UNION; }
415 structure_type(tp, sy)
417 /* enumeration type */
418 'e' { tp->ty_class = T_ENUM; }
422 '*' { tp->ty_class = T_POINTER;
423 tp->ty_size = pointer_size;
425 type(&(tp->ty_ptrto), (int *) 0, (p_symbol) 0)
428 'f' { tp->ty_class = T_PROCEDURE;
429 tp->ty_size = pointer_size;
431 type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
440 'Q' { tp->ty_class = T_PROCEDURE;
441 tp->ty_size = pointer_size;
443 type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
446 /* another procedure type */
447 'p' { tp->ty_class = T_PROCEDURE;
448 tp->ty_size = pointer_size;
449 tp->ty_retval = void_type;
454 /* the first integer_const represents the size in bytes,
455 * the second one represents the low bound
457 'S' { tp->ty_class = T_SET; }
458 type(&(tp->ty_setbase), (int *) 0, (p_symbol) 0) ';'
460 integer_const(&(tp->ty_size)) ';'
461 integer_const(&(tp->ty_setlow)) ';'
466 /* file type of Pascal */
467 'L' { tp->ty_class = T_FILE; }
468 type(&(tp->ty_fileof), (int *) 0, (p_symbol) 0)
470 type_name(ptp, (p_symbol) 0)
472 (*ptp)->ty_class == 0 &&
473 type_index[0] == last_index[0] &&
474 type_index[1] == last_index[1]) {
476 if (*ptp != tp) free_type(tp);
481 { if (*ptp && *ptp != tp) **ptp = *tp;
486 structure_type(register p_type tp; p_symbol sy;)
487 { register struct fields *fldp;
491 integer_const(&(tp->ty_size)) /* size in bytes */
493 if (sy) sy->sy_name.nm_scope = CurrentScope;
496 name(&str) { fldp = get_field_space(tp, str); }
497 type(&(fldp->fld_type), (int *) 0, (p_symbol) 0) ','
498 integer_const(&(fldp->fld_pos)) ',' /* offset in bits */
499 integer_const(&(fldp->fld_bitsize)) ';' /* size in bits */
506 enum_type(register p_type tp;)
507 { register struct literal *litp;
512 [ { litp = get_literal_space(tp); }
513 name(&(litp->lit_name))
514 integer_const(&(litp->lit_val)) ','
515 { if (maxval < litp->lit_val) maxval = litp->lit_val;
517 s = NewSymbol(litp->lit_name, CurrentScope, CONST, (struct outname *) 0);
518 s->sy_const.co_ival = litp->lit_val;
522 ';' { end_literal(tp, maxval); }
525 param_list(p_type t;)
526 { register struct param *p;
530 integer_const(&iconst) ';' /* number of parameters */
531 { t->ty_nparams = iconst;
532 t->ty_params = p = (struct param *)
533 Malloc((unsigned)(t->ty_nparams * sizeof(struct param)));
536 [ 'p' { p->par_kind = 'p'; }
537 | 'v' { p->par_kind = 'v'; }
538 | 'i' { p->par_kind = 'i'; }
541 type(&(p->par_type), (int *) 0, (p_symbol) 0) ';'
542 { p->par_off = t->ty_nbparams;
544 param_size(p->par_type, p->par_kind);
551 static char *db_string;
552 static char *DbOldPtr;
554 static struct outname *
569 fatal("error in symbol table string \"%s\", DbPtr = \"%s\", DbOldPtr = \"%s\"",
580 /* ??? if (DBSsymb < 0) {
581 while (*p && *p != ';') p++;
587 while (*p && *p != NAME) p++;
592 else if (tk == NAME) AllowName = 1;
597 register char *cp = DbPtr;
598 int allow_name = AllowName;
604 if (c == '\\' && *(cp+1) == '\0') {
606 cp = currnam->on_mptr;
614 if ((! allow_name && is_token(c)) || c == ';') {
619 int retval = INTEGER;
621 while (++cp, is_dig(*cp)) /* nothing */;
625 while (++cp, is_dig(*cp)) /* nothing */;
628 if (c == 'e' || c == 'E') {
633 if (c == '-' || c == '+') {
639 while (++cp, is_dig(*cp)) /* nothing */;
645 if (retval == INTEGER) {
646 ival = str2long(DbOldPtr, 10);
649 fval = atof(DbOldPtr);
658 while ((c = *cp) && c != '\'') {
659 if (c == '\\') cp++; /* backslash escapes next character */
660 if (!(c = *cp)) break; /* but not a null byte */
663 if (! c) DBSmessage(0); /* no return */
669 while ((c = *cp) && c != ':' && c != ',') cp++;
670 DbPtr = *cp ? cp+1 : cp;
675 static struct fields *
676 get_field_space(tp, s)
680 register struct fields *p;
683 if (! (tp->ty_nfields & 07)) {
684 tp->ty_fields = (struct fields *)
685 Realloc((char *) tp->ty_fields,
686 (tp->ty_nfields+8)*sizeof(struct fields));
688 p = &tp->ty_fields[tp->ty_nfields++];
691 sy = NewSymbol(s, CurrentScope, FIELD, currnam);
700 tp->ty_fields = (struct fields *)
701 Realloc((char *) tp->ty_fields,
702 tp->ty_nfields * sizeof(struct fields));
705 static struct literal *
706 get_literal_space(tp)
709 if (! (tp->ty_nenums & 07)) {
710 tp->ty_literals = (struct literal *)
711 Realloc((char *) tp->ty_literals,
712 (tp->ty_nenums+8)*sizeof(struct literal));
714 return &tp->ty_literals[tp->ty_nenums++];
721 register char *ns = s, *os = s;
722 register unsigned int i = 1;
737 static char *AckStrings; /* ACK a.out string table */
738 static struct outname *AckNames; /* ACK a.out symbol table entries */
739 static unsigned int NAckNames; /* Number of ACK symbol table entries */
740 static struct outname *EndAckNames; /* &AckNames[NAckNames] */
742 /* Read the symbol table from file 'f', which is supposed to be an
743 ACK a.out format file. Offer db strings to the db string parser.
750 register struct outname *n;
751 register struct outname *line_file = 0;
753 int lbrac_required = 0;
754 int needs_newscope = 0;
757 /* Open file, read header, and check magic word */
759 fatal("%s: could not open", f);
762 if (BADMAGIC(h) && h.oh_magic != O_CONVERTED) {
763 fatal("%s: not an object file", f);
766 /* Allocate space for name table and read it */
767 AckNames = (struct outname *)
768 Malloc((unsigned)(sizeof(struct outname) * h.oh_nname));
769 AckStrings = Malloc((unsigned) h.oh_nchar);
770 rd_name(AckNames, h.oh_nname);
771 rd_string(AckStrings, h.oh_nchar);
773 /* Adjust file offsets in name table to point at strings */
774 OffsetStrings = OFF_CHAR(h);
775 NAckNames = h.oh_nname;
776 EndAckNames = &AckNames[h.oh_nname];
777 for (n = EndAckNames; --n >= AckNames;) {
779 if ((unsigned)(n->on_foff - OffsetStrings) >= h.oh_nchar) {
780 fatal("%s: error in object file", f);
782 n->on_mptr = AckStrings + (n->on_foff - OffsetStrings);
787 /* Offer strings to the db string parser if they contain a ':'.
788 Also offer filename-line number information to add_position_addr().
789 Here, the order may be important.
791 for (n = &AckNames[0]; n < EndAckNames; n++) {
792 int tp = n->on_type >> 8;
793 register p_symbol sym;
795 if (tp & (S_STB >> 8)) {
799 n->on_valu = (long) line_file;
804 line_file = (struct outname *) line_file->on_valu;
809 if (n->on_mptr[strlen(n->on_mptr)-1] == '/') {
810 /* another N_SO follows ... */
814 while (CurrentScope != PervasiveScope) {
818 sym = add_file(n->on_mptr);
820 if (! listfile) newfile(sym->sy_idf);
822 sym->sy_file->f_scope = CurrentScope;
823 FileScope = CurrentScope;
824 CurrentScope->sc_start = n->on_valu;
827 if (line_file) n->on_valu = line_file->on_valu;
831 if (! FileScope) fatal("No file scope");
832 newfile(FileScope->sc_definedby->sy_idf);
836 if (CurrentScope->sc_start) {
837 register p_scope sc =
838 get_scope_from_addr(n->on_valu);
840 if (sc) CurrentScope = sc;
842 else if (! saw_code) {
843 CurrentScope->sc_start = n->on_valu;
844 add_scope_addr(CurrentScope);
846 if (!CurrentScope->sc_bp_opp
847 || CurrentScope->sc_bp_lineno > n->on_desc) {
848 CurrentScope->sc_bp_opp = n->on_valu;
849 CurrentScope->sc_bp_lineno = n->on_desc;
852 add_position_addr(line_file->on_mptr, n);
854 case N_LBRAC: /* block, desc = nesting level */
855 if (lbrac_level && ! lbrac_required) {
856 open_scope((p_symbol) 0, 0);
860 /* Sun-4 ld does not relocate LBRAC
861 values, so we cannot use it
863 register p_scope sc =
864 get_scope_from_addr(n->on_valu);
866 if (!sc || sc->sc_bp_opp) {
868 else CurrentScope = sc;
876 if (n->on_mptr && strindex(n->on_mptr, ':')) {
881 case N_RBRAC: /* end block, desc = nesting level */
882 if (CurrentScope != FileScope) close_scope();
884 if (--lbrac_level == 0) needs_newscope = 0;
887 case N_FUN: /* function, value = address */
888 case N_GSYM: /* global variable */
889 case N_STSYM: /* data, static, value = address */
890 case N_LCSYM: /* bss, static, value = address */
891 case N_RSYM: /* register var, value = reg number */
892 case N_SSYM: /* struct/union el, value = offset */
893 case N_PSYM: /* parameter, value = offset from AP */
894 case N_LSYM: /* local sym, value = offset from FP */
895 if (needs_newscope) {
896 open_scope((p_symbol) 0, 0);
901 if (n->on_mptr && strindex(n->on_mptr, ':')) {
907 if (n->on_mptr && (n->on_type&S_TYP) >= S_MIN) {
908 struct idf *id = str2idf(n->on_mptr, 0);
911 sym->sy_next = id->id_def;
913 sym->sy_class = SYMENTRY;
923 add_position_addr((char *) 0, (struct outname *) 0);
926 return (h.oh_magic == O_CONVERTED);