dbx_string.g 15 KB

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