dbx_string.g 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* $Header$
  2. Grammar of a string of a debugger symbol table entry.
  3. */
  4. {
  5. #include <out.h>
  6. #include <alloc.h>
  7. #include "type.h"
  8. #include "symbol.h"
  9. #include "scope.h"
  10. #include "class.h"
  11. #include "idf.h"
  12. extern char *strindex();
  13. extern long str2long();
  14. extern double atof();
  15. extern int saw_code;
  16. extern long pointer_size;
  17. static char *DbxPtr; /* current pointer in DBX string */
  18. static int AllowName; /* set if NAME legal at this point */
  19. static long ival;
  20. static double fval;
  21. static char *strval;
  22. static int last_index[2];
  23. static struct outname *currnam;
  24. static struct literal *get_literal_space();
  25. static struct fields *get_field_space();
  26. static end_field();
  27. static char *string_val();
  28. }
  29. %start DbxParser, debugger_string;
  30. %prefix DBS;
  31. %lexical DBSlex;
  32. %onerror DBSonerror;
  33. %token INTEGER, REAL, STRING, NAME;
  34. debugger_string
  35. { register p_symbol s;
  36. char *str;
  37. p_type tmp = 0;
  38. }
  39. :
  40. name(&str)
  41. [ /* constant name */
  42. { s = NewSymbol(str, CurrentScope, CONST, currnam); }
  43. 'c' const_name(s)
  44. | /* type name */
  45. { s = NewSymbol(str, CurrentScope, TYPE, currnam); }
  46. 't' type_name(&(s->sy_type))
  47. | /* tag name (only C?) */
  48. { s = NewSymbol(str, CurrentScope, TAG, currnam); }
  49. 'T' tag_name(s)
  50. | /* end scope */
  51. 'E' INTEGER
  52. { close_scope(); }
  53. | /* module begin */
  54. { s = NewSymbol(str, CurrentScope, MODULE, currnam); }
  55. 'M' INTEGER
  56. { open_scope(s, 1);
  57. s->sy_name.nm_scope = CurrentScope;
  58. CurrentScope->sc_start = currnam->on_valu;
  59. CurrentScope->sc_proclevel = currnam->on_desc;
  60. add_scope_addr(CurrentScope);
  61. }
  62. | /* external procedure */
  63. { s = NewSymbol(str, PervasiveScope, PROC, currnam); }
  64. 'P' routine(s)
  65. | /* private procedure */
  66. { s = NewSymbol(str, CurrentScope, PROC, currnam); }
  67. 'Q' routine(s)
  68. | /* external function */
  69. { s = NewSymbol(str, PervasiveScope, FUNCTION, currnam); }
  70. 'F' function(s)
  71. | /* private function */
  72. { s = NewSymbol(str, CurrentScope, FUNCTION, currnam); }
  73. 'f' function(s)
  74. | /* global variable, external */
  75. /* maybe we already know it; but we need
  76. the type information anyway for other
  77. types.
  78. */
  79. { s = Lookup(findidf(str), PervasiveScope, VAR);
  80. if (s) {
  81. tmp = s->sy_type;
  82. } else s = NewSymbol(str, PervasiveScope, VAR, currnam);
  83. }
  84. 'G' type(&(s->sy_type), (int *) 0)
  85. { if (tmp) s->sy_type = tmp; }
  86. | /* static variable */
  87. { s = NewSymbol(str, CurrentScope, VAR, currnam); }
  88. 'S' type(&(s->sy_type), (int *) 0)
  89. | /* static variable, local scope */
  90. { s = NewSymbol(str, CurrentScope, VAR, currnam); }
  91. 'V' type(&(s->sy_type), (int *) 0)
  92. | /* register variable */
  93. { s = NewSymbol(str, CurrentScope, REGVAR, currnam); }
  94. 'r' type(&(s->sy_type), (int *) 0)
  95. | /* value parameter */
  96. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  97. 'p' type(&(s->sy_type), (int *) 0)
  98. { add_param_type('p', s); }
  99. | /* value parameter but address passed */
  100. { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
  101. 'i' type(&(s->sy_type), (int *) 0)
  102. { add_param_type('i', s); }
  103. | /* variable parameter */
  104. { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
  105. 'v' type(&(s->sy_type), (int *) 0)
  106. { add_param_type('v', s); }
  107. | /* local variable */
  108. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  109. type_name(&(s->sy_type))
  110. | /* function result in Pascal; ignore ??? */
  111. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  112. 'X' type_name(&(s->sy_type))
  113. ]
  114. ';'?
  115. ;
  116. name(char **s;)
  117. :
  118. /* anything up to a ':' */
  119. NAME { *s = strval; }
  120. ;
  121. const_name(p_symbol cst;)
  122. { int type_index[2];
  123. long iconst;
  124. register char *p;
  125. }
  126. :
  127. '='
  128. [
  129. 'b' integer_const(&(cst->sy_const.co_ival)) /* boolean */
  130. |
  131. 'c' integer_const(&(cst->sy_const.co_ival)) /* character */
  132. { cst->sy_type = char_type; }
  133. |
  134. 'i' integer_const(&(cst->sy_const.co_ival)) /* integer */
  135. { cst->sy_type = long_type; }
  136. |
  137. 'r' real_const(&(cst->sy_const.co_rval)) /* real */
  138. { cst->sy_type = double_type; }
  139. |
  140. 's' string_const /* string */
  141. { cst->sy_const.co_sval = string_val(strval);
  142. cst->sy_type = string_type;
  143. }
  144. |
  145. 'e' type_index(type_index) ',' integer_const(&(cst->sy_const.co_ival))
  146. /* enumeration constant;
  147. * enumeration type, value
  148. */
  149. { cst->sy_type = *tp_lookup(type_index); }
  150. |
  151. 'S' type_index(type_index)
  152. { cst->sy_type = *tp_lookup(type_index);
  153. cst->sy_const.co_setval = p =
  154. Malloc((unsigned) cst->sy_type->ty_size);
  155. }
  156. [ ',' integer_const(&iconst)
  157. { *p++ = iconst; }
  158. ]+
  159. /* set constant:
  160. * settype, values of the bytes
  161. * in the set.
  162. */
  163. ]
  164. ;
  165. integer_const(long *iconst;)
  166. { int sign = 0; }
  167. :
  168. [ '+' | '-' { sign = 1; } ]?
  169. INTEGER { *iconst = sign ? -ival : ival; }
  170. ;
  171. real_const(double *f;)
  172. { int sign = 0; }
  173. :
  174. [ '+' | '-' { sign = 1; } ]?
  175. REAL { *f = sign ? fval : -fval; }
  176. ;
  177. string_const
  178. :
  179. STRING /* has SINGLE quotes! */
  180. ;
  181. type_name(p_type *t;)
  182. { int type_index[2]; p_type *p; }
  183. :
  184. type_index(type_index)
  185. [
  186. '='
  187. type(t, type_index)
  188. { p = tp_lookup(type_index);
  189. if (*p && *p != incomplete_type) {
  190. if (!((*p)->ty_flags & T_CROSS))
  191. error("Redefining (%d,%d) %d",
  192. type_index[0],
  193. type_index[1],
  194. (*p)->ty_class);
  195. if (*t && *p != *t) free_type(*p);
  196. }
  197. if (*t) *p = *t;
  198. }
  199. |
  200. { p = tp_lookup(type_index); }
  201. ]
  202. { if (*p == 0) *p = incomplete_type;
  203. *t = *p;
  204. }
  205. ;
  206. type_index(int *type_index;)
  207. :
  208. [
  209. INTEGER { type_index[0] = 0; type_index[1] = ival; }
  210. |
  211. '(' INTEGER { type_index[0] = ival; }
  212. ',' INTEGER { type_index[1] = ival; }
  213. ')'
  214. ]
  215. { last_index[0] = type_index[0];
  216. last_index[1] = type_index[1];
  217. }
  218. ;
  219. tag_name(p_symbol t;)
  220. { int type_index[2]; p_type *p; }
  221. :
  222. type_index(type_index)
  223. '='
  224. type(&(t->sy_type), type_index)
  225. { p = tp_lookup(type_index);
  226. if (*p && *p != incomplete_type) {
  227. if (!((*p)->ty_flags & T_CROSS))
  228. error("Redefining (%d,%d) %d",
  229. type_index[0],
  230. type_index[1],
  231. (*p)->ty_class);
  232. if (t->sy_type && *p != t->sy_type) {
  233. free_type(*p);
  234. }
  235. }
  236. if (t->sy_type) *p = t->sy_type;
  237. if (*p == 0) *p = incomplete_type;
  238. }
  239. ;
  240. function(p_symbol p;)
  241. :
  242. { p->sy_type = new_type();
  243. p->sy_type->ty_class = T_PROCEDURE;
  244. p->sy_type->ty_size = pointer_size;
  245. }
  246. type(&(p->sy_type->ty_retval), (int *) 0)
  247. { if (CurrentScope != FileScope &&
  248. saw_code) {
  249. /* if saw_code is not set, it is a nested
  250. procedure
  251. */
  252. close_scope();
  253. }
  254. saw_code = 0;
  255. open_scope(p, 1);
  256. p->sy_name.nm_scope = CurrentScope;
  257. CurrentScope->sc_start = currnam->on_valu;
  258. add_scope_addr(CurrentScope);
  259. CurrentScope->sc_proclevel = currnam->on_desc;
  260. }
  261. ;
  262. routine(p_symbol p;)
  263. :
  264. { p->sy_type = new_type();
  265. p->sy_type->ty_class = T_PROCEDURE;
  266. p->sy_type->ty_size = pointer_size;
  267. if (CurrentScope != FileScope &&
  268. saw_code) {
  269. /* if saw_code is not set, it is a nested
  270. procedure
  271. */
  272. close_scope();
  273. }
  274. saw_code = 0;
  275. open_scope(p, 1);
  276. p->sy_name.nm_scope = CurrentScope;
  277. CurrentScope->sc_start = currnam->on_valu;
  278. add_scope_addr(CurrentScope);
  279. CurrentScope->sc_proclevel = currnam->on_desc;
  280. }
  281. INTEGER ';'
  282. type(&(p->sy_type->ty_retval), (int *) 0)
  283. ;
  284. type(p_type *ptp; int *type_index;)
  285. { register p_type tp = 0;
  286. p_type t1, t2;
  287. long ic1, ic2;
  288. int A_used = 0;
  289. }
  290. : { *ptp = 0; }
  291. [
  292. /* type cross reference */
  293. /* these are used in C for references to a struct, union or
  294. * enum that has not been declared (yet)
  295. */
  296. 'x' { tp = new_type(); tp->ty_flags = T_CROSS; }
  297. [ 's' /* struct */
  298. { tp->ty_class = T_STRUCT; }
  299. | 'u' /* union */
  300. { tp->ty_class = T_UNION; }
  301. | 'e' /* enum */
  302. { tp->ty_class = T_ENUM; }
  303. ]
  304. { AllowName = 1; }
  305. name(&(tp->ty_tag))
  306. |
  307. /* subrange */
  308. /* the integer_const's represent the lower and the upper bound.
  309. * A subrange type defined as subrange of itself is an integer type.
  310. * If the second integer_const == 0, but the first is not, we
  311. * have a floating point type with size equal to the first
  312. * integer_const.
  313. * Upperbound -1 means unsigned int or unsigned long.
  314. */
  315. 'r' type_name(&t1) ';'
  316. [ 'A' integer_const(&ic1) { A_used = 1; }
  317. | integer_const(&ic1)
  318. ]
  319. ';'
  320. [ 'A' integer_const(&ic2) { A_used |= 2; }
  321. | integer_const(&ic2)
  322. ]
  323. { *ptp = subrange_type(A_used,
  324. last_index,
  325. ic1,
  326. ic2,
  327. type_index);
  328. }
  329. |
  330. /* array; first type is bound type, next type
  331. * is element type
  332. */
  333. 'a' type(&t1, (int *) 0) ';' type(&t2, (int *) 0)
  334. { *ptp = array_type(t1, t2); }
  335. |
  336. /* structure type */
  337. 's' { tp = new_type(); tp->ty_class = T_STRUCT; }
  338. structure_type(tp)
  339. |
  340. /* union type */
  341. 'u' { tp = new_type(); tp->ty_class = T_UNION; }
  342. structure_type(tp)
  343. |
  344. /* enumeration type */
  345. 'e' { tp = new_type(); tp->ty_class = T_ENUM; }
  346. enum_type(tp)
  347. |
  348. /* pointer type */
  349. '*' { tp = new_type(); tp->ty_class =T_POINTER;
  350. tp->ty_size = pointer_size;
  351. }
  352. type(&(tp->ty_ptrto), (int *) 0)
  353. |
  354. /* function type */
  355. 'f' { tp = new_type(); tp->ty_class = T_PROCEDURE;
  356. tp->ty_size = pointer_size;
  357. }
  358. type(&(tp->ty_retval), (int *) 0)
  359. /*
  360. [ %prefer
  361. ',' param_list(tp)
  362. |
  363. ]
  364. */
  365. |
  366. /* procedure type */
  367. 'Q' { tp = new_type(); tp->ty_class = T_PROCEDURE;
  368. tp->ty_size = pointer_size;
  369. }
  370. type(&(tp->ty_retval), (int *) 0)
  371. ',' param_list(tp)
  372. |
  373. /* another procedure type */
  374. 'p' { tp = new_type(); tp->ty_class = T_PROCEDURE;
  375. tp->ty_size = pointer_size;
  376. tp->ty_retval = void_type;
  377. }
  378. param_list(tp)
  379. |
  380. /* set type */
  381. /* the first integer_const represents the size in bytes,
  382. * the second one represents the low bound
  383. */
  384. 'S' { tp = new_type(); tp->ty_class = T_SET; }
  385. type(&(tp->ty_setbase), (int *) 0) ';'
  386. [
  387. integer_const(&(tp->ty_size)) ';'
  388. integer_const(&(tp->ty_setlow)) ';'
  389. |
  390. { set_bounds(tp); }
  391. ]
  392. |
  393. /* file type of Pascal */
  394. 'L' { tp = new_type(); tp->ty_class = T_FILE; }
  395. type(&(tp->ty_fileof), (int *) 0)
  396. |
  397. type_name(ptp)
  398. { if (type_index &&
  399. *ptp == incomplete_type &&
  400. type_index[0] == last_index[0] &&
  401. type_index[1] == last_index[1]) {
  402. *ptp = void_type;
  403. }
  404. }
  405. ]
  406. { if (! *ptp) *ptp = tp; }
  407. ;
  408. structure_type(register p_type tp;)
  409. { register struct fields *fldp; }
  410. :
  411. integer_const(&(tp->ty_size)) /* size in bytes */
  412. [ { fldp = get_field_space(tp); }
  413. name(&(fldp->fld_name))
  414. type(&(fldp->fld_type), (int *) 0) ','
  415. integer_const(&(fldp->fld_pos)) ',' /* offset in bits */
  416. integer_const(&(fldp->fld_bitsize)) ';' /* size in bits */
  417. ]*
  418. ';' { end_field(tp); }
  419. ;
  420. enum_type(register p_type tp;)
  421. { register struct literal *litp;
  422. long maxval = 0;
  423. }
  424. :
  425. [ { litp = get_literal_space(tp);
  426. }
  427. name(&(litp->lit_name))
  428. integer_const(&(litp->lit_val)) ','
  429. { if (maxval < litp->lit_val) maxval = litp->lit_val;
  430. AllowName = 1;
  431. }
  432. ]*
  433. ';' { end_literal(tp, maxval); }
  434. ;
  435. param_list(p_type t;)
  436. { register struct param *p;
  437. long iconst;
  438. }
  439. :
  440. integer_const(&iconst) ';' /* number of parameters */
  441. { t->ty_nparams = iconst;
  442. t->ty_params = p = (struct param *)
  443. Malloc((unsigned)(t->ty_nparams * sizeof(struct param)));
  444. }
  445. [
  446. [ 'p' { p->par_kind = 'p'; }
  447. | 'v' { p->par_kind = 'v'; }
  448. | 'i' { p->par_kind = 'i'; }
  449. ]
  450. type(&(p->par_type), (int *) 0) ';'
  451. { t->ty_nbparams +=
  452. param_size(p->par_type, p->par_kind);
  453. p++;
  454. }
  455. ]*
  456. ;
  457. {
  458. static char *dbx_string;
  459. static char *DbxOldPtr;
  460. struct outname *
  461. DbxString(n)
  462. struct outname *n;
  463. {
  464. currnam = n;
  465. DbxPtr = n->on_mptr;
  466. dbx_string = DbxPtr;
  467. AllowName = 1;
  468. DbxParser();
  469. return currnam;
  470. }
  471. /*ARGSUSED*/
  472. DBSmessage(n)
  473. {
  474. fatal("error in Dbx string \"%s\", DbxPtr = \"%s\", DbxOldPtr = \"%s\"",
  475. dbx_string,
  476. DbxPtr,
  477. DbxOldPtr);
  478. }
  479. DBSonerror(tk, p)
  480. int *p;
  481. {
  482. DbxPtr = DbxOldPtr;
  483. /* ??? if (DBSsymb < 0) {
  484. while (*p && *p != ';') p++;
  485. if (*p) DbxPtr = ";";
  486. return;
  487. }
  488. */
  489. if (! tk) {
  490. while (*p && *p != NAME) p++;
  491. if (*p) {
  492. AllowName = 1;
  493. }
  494. }
  495. else if (tk == NAME) AllowName = 1;
  496. }
  497. DBSlex()
  498. {
  499. register char *cp = DbxPtr;
  500. int allow_name = AllowName;
  501. register int c;
  502. AllowName = 0;
  503. DbxOldPtr = cp;
  504. c = *cp;
  505. if (c == '\\' && *(cp+1) == '\0') {
  506. currnam++;
  507. cp = currnam->on_mptr;
  508. DbxOldPtr = cp;
  509. c = *cp;
  510. }
  511. if (! c) {
  512. DbxPtr = cp;
  513. return -1;
  514. }
  515. if ((! allow_name && is_token(c)) || c == ';') {
  516. DbxPtr = cp+1;
  517. return c;
  518. }
  519. if (is_dig(c)) {
  520. int retval = INTEGER;
  521. while (++cp, is_dig(*cp)) /* nothing */;
  522. c = *cp;
  523. if (c == '.') {
  524. retval = REAL;
  525. while (++cp, is_dig(*cp)) /* nothing */;
  526. c = *cp;
  527. }
  528. if (c == 'e' || c == 'E') {
  529. char *oldcp = cp;
  530. cp++;
  531. c = *cp;
  532. if (c == '-' || c == '+') {
  533. cp++;
  534. c = *cp;
  535. }
  536. if (is_dig(c)) {
  537. retval = REAL;
  538. while (++cp, is_dig(*cp)) /* nothing */;
  539. }
  540. else cp = oldcp;
  541. }
  542. c = *cp;
  543. *cp = 0;
  544. if (retval == INTEGER) {
  545. ival = str2long(DbxOldPtr, 10);
  546. }
  547. else {
  548. fval = atof(DbxOldPtr);
  549. }
  550. *cp = c;
  551. DbxPtr = cp;
  552. return retval;
  553. }
  554. if (c == '\'') {
  555. cp++;
  556. strval = cp;
  557. while ((c = *cp) && c != '\'') {
  558. if (c == '\\') cp++; /* backslash escapes next character */
  559. if (!(c = *cp)) break; /* but not a null byte */
  560. cp++;
  561. }
  562. if (! c) DBSmessage(0); /* no return */
  563. *cp = 0;
  564. DbxPtr = cp + 1;
  565. return STRING;
  566. }
  567. strval = cp;
  568. while ((c = *cp) && c != ':' && c != ',') cp++;
  569. DbxPtr = *cp ? cp+1 : cp;
  570. *cp = 0;
  571. return NAME;
  572. }
  573. static struct fields *
  574. get_field_space(tp)
  575. register p_type tp;
  576. {
  577. if (! (tp->ty_nfields & 07)) {
  578. tp->ty_fields = (struct fields *)
  579. Realloc((char *) tp->ty_fields,
  580. (tp->ty_nfields+8)*sizeof(struct fields));
  581. }
  582. return &tp->ty_fields[tp->ty_nfields++];
  583. }
  584. static
  585. end_field(tp)
  586. register p_type tp;
  587. {
  588. tp->ty_fields = (struct fields *)
  589. Realloc((char *) tp->ty_fields,
  590. tp->ty_nfields * sizeof(struct fields));
  591. }
  592. static struct literal *
  593. get_literal_space(tp)
  594. register p_type tp;
  595. {
  596. if (! (tp->ty_nenums & 07)) {
  597. tp->ty_literals = (struct literal *)
  598. Realloc((char *) tp->ty_literals,
  599. (tp->ty_nenums+8)*sizeof(struct literal));
  600. }
  601. return &tp->ty_literals[tp->ty_nenums++];
  602. }
  603. static char *
  604. string_val(s)
  605. char *s;
  606. {
  607. register char *ns = s, *os = s;
  608. register unsigned int i = 1;
  609. for (;;) {
  610. if (!*os) break;
  611. i++;
  612. if (*os == '\\') {
  613. os++;
  614. *ns++ = *os++;
  615. }
  616. else *ns++ = *os++;
  617. }
  618. *ns = '\0';
  619. return Salloc(s, i);
  620. }
  621. }