dbx_string.g 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. char *str;
  281. }
  282. :
  283. [
  284. /* type cross reference */
  285. /* these are used in C for references to a struct, union or
  286. * enum that has not been declared (yet)
  287. */
  288. 'x'
  289. [ 's' /* struct */
  290. { tclass = T_STRUCT; }
  291. | 'u' /* union */
  292. { tclass = T_UNION; }
  293. | 'e' /* enum */
  294. { tclass = T_ENUM; }
  295. ]
  296. { AllowName = 1; }
  297. name(&str)
  298. { sy = Lookfromscope(str2idf(str,0),TAG,CurrentScope);
  299. if (sy &&
  300. (sy->sy_type->ty_class == tclass ||
  301. sy->sy_type->ty_class == T_CROSS)) {
  302. if (tp != *ptp) free_type(tp);
  303. tp = sy->sy_type;
  304. }
  305. else {
  306. tp->ty_class = T_CROSS;
  307. tp->ty_size = tclass;
  308. sy = NewSymbol(str, CurrentScope, TAG, (struct outname *) 0);
  309. sy->sy_type = tp;
  310. }
  311. }
  312. |
  313. /* subrange */
  314. /* the integer_const's represent the lower and the upper bound.
  315. * A subrange type defined as subrange of itself is an integer type.
  316. * If the second integer_const == 0, but the first is not, we
  317. * have a floating point type with size equal to the first
  318. * integer_const.
  319. * Upperbound -1 means unsigned int or unsigned long.
  320. */
  321. 'r' type_name(&t1, (p_symbol) 0) ';'
  322. [ 'A' integer_const(&ic1) { A_used = 1; }
  323. | integer_const(&ic1)
  324. ]
  325. ';'
  326. [ 'A' integer_const(&ic2) { A_used |= 2; }
  327. | integer_const(&ic2)
  328. ]
  329. { if (tp != *ptp) free_type(tp);
  330. tp = subrange_type(A_used,
  331. last_index,
  332. ic1,
  333. ic2,
  334. type_index);
  335. }
  336. |
  337. /* array; first type is bound type, next type
  338. * is element type
  339. */
  340. 'a' type(&t1, (int *) 0, (p_symbol) 0) ';' type(&t2, (int *) 0, (p_symbol) 0)
  341. { if (tp != *ptp) free_type(tp);
  342. tp = array_type(t1, t2);
  343. }
  344. |
  345. /* structure type */
  346. 's' { tp->ty_class = T_STRUCT; }
  347. structure_type(tp, sy)
  348. |
  349. /* union type */
  350. 'u' { tp->ty_class = T_UNION; }
  351. structure_type(tp, sy)
  352. |
  353. /* enumeration type */
  354. 'e' { tp->ty_class = T_ENUM; }
  355. enum_type(tp)
  356. |
  357. /* pointer type */
  358. '*' { tp->ty_class = T_POINTER;
  359. tp->ty_size = pointer_size;
  360. }
  361. type(&(tp->ty_ptrto), (int *) 0, (p_symbol) 0)
  362. |
  363. /* function type */
  364. 'f' { tp->ty_class = T_PROCEDURE;
  365. tp->ty_size = pointer_size;
  366. }
  367. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  368. /*
  369. [ %prefer
  370. ',' param_list(tp)
  371. |
  372. ]
  373. */
  374. |
  375. /* procedure type */
  376. 'Q' { tp->ty_class = T_PROCEDURE;
  377. tp->ty_size = pointer_size;
  378. }
  379. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  380. ',' param_list(tp)
  381. |
  382. /* another procedure type */
  383. 'p' { tp->ty_class = T_PROCEDURE;
  384. tp->ty_size = pointer_size;
  385. tp->ty_retval = void_type;
  386. }
  387. param_list(tp)
  388. |
  389. /* set type */
  390. /* the first integer_const represents the size in bytes,
  391. * the second one represents the low bound
  392. */
  393. 'S' { tp->ty_class = T_SET; }
  394. type(&(tp->ty_setbase), (int *) 0, (p_symbol) 0) ';'
  395. [
  396. integer_const(&(tp->ty_size)) ';'
  397. integer_const(&(tp->ty_setlow)) ';'
  398. |
  399. { set_bounds(tp); }
  400. ]
  401. |
  402. /* file type of Pascal */
  403. 'L' { tp->ty_class = T_FILE; }
  404. type(&(tp->ty_fileof), (int *) 0, (p_symbol) 0)
  405. |
  406. type_name(ptp, (p_symbol) 0)
  407. { if (type_index &&
  408. (*ptp)->ty_class == 0 &&
  409. type_index[0] == last_index[0] &&
  410. type_index[1] == last_index[1]) {
  411. **ptp = *void_type;
  412. if (*ptp != tp) free_type(tp);
  413. }
  414. tp = *ptp;
  415. }
  416. ]
  417. { if (*ptp && *ptp != tp) **ptp = *tp;
  418. else *ptp = tp;
  419. }
  420. ;
  421. structure_type(register p_type tp; p_symbol sy;)
  422. { register struct fields *fldp;
  423. char *str;
  424. }
  425. :
  426. integer_const(&(tp->ty_size)) /* size in bytes */
  427. { open_scope(sy, 0);
  428. if (sy) sy->sy_name.nm_scope = CurrentScope;
  429. }
  430. [
  431. name(&str) { fldp = get_field_space(tp, str); }
  432. type(&(fldp->fld_type), (int *) 0, (p_symbol) 0) ','
  433. integer_const(&(fldp->fld_pos)) ',' /* offset in bits */
  434. integer_const(&(fldp->fld_bitsize)) ';' /* size in bits */
  435. ]*
  436. ';' { end_field(tp);
  437. close_scope();
  438. }
  439. ;
  440. enum_type(register p_type tp;)
  441. { register struct literal *litp;
  442. long maxval = 0;
  443. register p_symbol s;
  444. }
  445. :
  446. [ { litp = get_literal_space(tp); }
  447. name(&(litp->lit_name))
  448. integer_const(&(litp->lit_val)) ','
  449. { if (maxval < litp->lit_val) maxval = litp->lit_val;
  450. AllowName = 1;
  451. s = NewSymbol(litp->lit_name, CurrentScope, CONST, (struct outname *) 0);
  452. s->sy_const.co_ival = litp->lit_val;
  453. s->sy_type = tp;
  454. }
  455. ]*
  456. ';' { end_literal(tp, maxval); }
  457. ;
  458. param_list(p_type t;)
  459. { register struct param *p;
  460. long iconst;
  461. }
  462. :
  463. integer_const(&iconst) ';' /* number of parameters */
  464. { t->ty_nparams = iconst;
  465. t->ty_params = p = (struct param *)
  466. Malloc((unsigned)(t->ty_nparams * sizeof(struct param)));
  467. }
  468. [
  469. [ 'p' { p->par_kind = 'p'; }
  470. | 'v' { p->par_kind = 'v'; }
  471. | 'i' { p->par_kind = 'i'; }
  472. ]
  473. type(&(p->par_type), (int *) 0, (p_symbol) 0) ';'
  474. { t->ty_nbparams +=
  475. param_size(p->par_type, p->par_kind);
  476. p++;
  477. }
  478. ]*
  479. ;
  480. {
  481. static char *dbx_string;
  482. static char *DbxOldPtr;
  483. struct outname *
  484. DbxString(n)
  485. struct outname *n;
  486. {
  487. currnam = n;
  488. DbxPtr = n->on_mptr;
  489. dbx_string = DbxPtr;
  490. AllowName = 1;
  491. DbxParser();
  492. return currnam;
  493. }
  494. /*ARGSUSED*/
  495. DBSmessage(n)
  496. {
  497. fatal("error in Dbx string \"%s\", DbxPtr = \"%s\", DbxOldPtr = \"%s\"",
  498. dbx_string,
  499. DbxPtr,
  500. DbxOldPtr);
  501. }
  502. DBSonerror(tk, p)
  503. int *p;
  504. {
  505. DbxPtr = DbxOldPtr;
  506. /* ??? if (DBSsymb < 0) {
  507. while (*p && *p != ';') p++;
  508. if (*p) DbxPtr = ";";
  509. return;
  510. }
  511. */
  512. if (! tk) {
  513. while (*p && *p != NAME) p++;
  514. if (*p) {
  515. AllowName = 1;
  516. }
  517. }
  518. else if (tk == NAME) AllowName = 1;
  519. }
  520. DBSlex()
  521. {
  522. register char *cp = DbxPtr;
  523. int allow_name = AllowName;
  524. register int c;
  525. AllowName = 0;
  526. DbxOldPtr = cp;
  527. c = *cp;
  528. if (c == '\\' && *(cp+1) == '\0') {
  529. currnam++;
  530. cp = currnam->on_mptr;
  531. DbxOldPtr = cp;
  532. c = *cp;
  533. }
  534. if (! c) {
  535. DbxPtr = cp;
  536. return -1;
  537. }
  538. if ((! allow_name && is_token(c)) || c == ';') {
  539. DbxPtr = cp+1;
  540. return c;
  541. }
  542. if (is_dig(c)) {
  543. int retval = INTEGER;
  544. while (++cp, is_dig(*cp)) /* nothing */;
  545. c = *cp;
  546. if (c == '.') {
  547. retval = REAL;
  548. while (++cp, is_dig(*cp)) /* nothing */;
  549. c = *cp;
  550. }
  551. if (c == 'e' || c == 'E') {
  552. char *oldcp = cp;
  553. cp++;
  554. c = *cp;
  555. if (c == '-' || c == '+') {
  556. cp++;
  557. c = *cp;
  558. }
  559. if (is_dig(c)) {
  560. retval = REAL;
  561. while (++cp, is_dig(*cp)) /* nothing */;
  562. }
  563. else cp = oldcp;
  564. }
  565. c = *cp;
  566. *cp = 0;
  567. if (retval == INTEGER) {
  568. ival = str2long(DbxOldPtr, 10);
  569. }
  570. else {
  571. fval = atof(DbxOldPtr);
  572. }
  573. *cp = c;
  574. DbxPtr = cp;
  575. return retval;
  576. }
  577. if (c == '\'') {
  578. cp++;
  579. strval = cp;
  580. while ((c = *cp) && c != '\'') {
  581. if (c == '\\') cp++; /* backslash escapes next character */
  582. if (!(c = *cp)) break; /* but not a null byte */
  583. cp++;
  584. }
  585. if (! c) DBSmessage(0); /* no return */
  586. *cp = 0;
  587. DbxPtr = cp + 1;
  588. return STRING;
  589. }
  590. strval = cp;
  591. while ((c = *cp) && c != ':' && c != ',') cp++;
  592. DbxPtr = *cp ? cp+1 : cp;
  593. *cp = 0;
  594. return NAME;
  595. }
  596. static struct fields *
  597. get_field_space(tp, s)
  598. register p_type tp;
  599. char *s;
  600. {
  601. register struct fields *p;
  602. p_symbol sy;
  603. if (! (tp->ty_nfields & 07)) {
  604. tp->ty_fields = (struct fields *)
  605. Realloc((char *) tp->ty_fields,
  606. (tp->ty_nfields+8)*sizeof(struct fields));
  607. }
  608. p = &tp->ty_fields[tp->ty_nfields++];
  609. p->fld_name = s;
  610. p->fld_type = 0;
  611. sy = NewSymbol(s, CurrentScope, FIELD, currnam);
  612. sy->sy_field = p;
  613. return p;
  614. }
  615. static
  616. end_field(tp)
  617. register p_type tp;
  618. {
  619. tp->ty_fields = (struct fields *)
  620. Realloc((char *) tp->ty_fields,
  621. tp->ty_nfields * sizeof(struct fields));
  622. }
  623. static struct literal *
  624. get_literal_space(tp)
  625. register p_type tp;
  626. {
  627. if (! (tp->ty_nenums & 07)) {
  628. tp->ty_literals = (struct literal *)
  629. Realloc((char *) tp->ty_literals,
  630. (tp->ty_nenums+8)*sizeof(struct literal));
  631. }
  632. return &tp->ty_literals[tp->ty_nenums++];
  633. }
  634. static char *
  635. string_val(s)
  636. char *s;
  637. {
  638. register char *ns = s, *os = s;
  639. register unsigned int i = 1;
  640. for (;;) {
  641. if (!*os) break;
  642. i++;
  643. if (*os == '\\') {
  644. os++;
  645. *ns++ = *os++;
  646. }
  647. else *ns++ = *os++;
  648. }
  649. *ns = '\0';
  650. return Salloc(s, i);
  651. }
  652. }