db_symtab.g 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /* $Id$ */
  2. /* Symbol table reader
  3. */
  4. {
  5. #include <alloc.h>
  6. #include <stb.h>
  7. #include <assert.h>
  8. #include "position.h"
  9. #include "file.h"
  10. #include "type.h"
  11. #include "symbol.h"
  12. #include "scope.h"
  13. #include "class.h"
  14. #include "idf.h"
  15. #include "rd.h"
  16. #include "misc.h"
  17. extern char *strchr();
  18. extern long str2long();
  19. extern double atof();
  20. static char *DbPtr; /* current pointer in db string */
  21. static int AllowName; /* set if NAME legal at this point */
  22. static long ival;
  23. static double fval;
  24. static char *strval;
  25. static int last_index[2];
  26. static struct outname *currnam;
  27. static int saw_code;
  28. static struct literal *get_literal_space();
  29. static struct fields *get_field_space();
  30. static end_field();
  31. static char *string_val();
  32. }
  33. %start DbParser, debugger_string;
  34. %prefix DBS;
  35. %lexical DBSlex;
  36. %onerror DBSonerror;
  37. %token INTEGER, REAL, STRING, NAME;
  38. debugger_string
  39. { register p_symbol s;
  40. char *str;
  41. p_type tmp = 0;
  42. int upb = 0;
  43. }
  44. :
  45. name(&str)
  46. [ /* constant name */
  47. { s = NewSymbol(str, CurrentScope, CONST, currnam); }
  48. 'c' const_name(s)
  49. | /* type name */
  50. { s = NewSymbol(str, CurrentScope, TYPE, currnam); }
  51. 't' type_name(&(s->sy_type), s)
  52. { if (! s->sy_type->ty_sym) s->sy_type->ty_sym = s;
  53. if ((s->sy_type->ty_class == T_ENUM ||
  54. s->sy_type->ty_class == T_SUBRANGE) &&
  55. currnam->on_desc != 0) {
  56. s->sy_type->ty_size = currnam->on_desc;
  57. }
  58. }
  59. | /* tag name (only C?) */
  60. { s = NewSymbol(str, CurrentScope, TAG, currnam); }
  61. 'T' type_name(&(s->sy_type), s)
  62. { if (! s->sy_type->ty_sym) s->sy_type->ty_sym = s;
  63. if (s->sy_type->ty_class != T_CROSS) {
  64. resolve_cross(s->sy_type);
  65. }
  66. }
  67. | /* end scope */
  68. 'E' INTEGER
  69. { close_scope(); }
  70. | /* module begin */
  71. { s = NewSymbol(str, CurrentScope, MODULE, currnam); }
  72. 'M' INTEGER
  73. { open_scope(s, 1);
  74. s->sy_name.nm_scope = CurrentScope;
  75. CurrentScope->sc_start = currnam->on_valu;
  76. CurrentScope->sc_proclevel = currnam->on_desc;
  77. add_scope_addr(CurrentScope);
  78. }
  79. | /* external procedure */
  80. { s = NewSymbol(str, FileScope, PROC, currnam); }
  81. 'P' routine(s)
  82. | /* private procedure */
  83. { s = NewSymbol(str, CurrentScope, PROC, currnam); }
  84. 'Q' routine(s)
  85. | /* external function */
  86. { s = NewSymbol(str, FileScope, FUNCTION, currnam); }
  87. 'F' function(s)
  88. | /* private function */
  89. { s = NewSymbol(str, CurrentScope, FUNCTION, currnam); }
  90. 'f' function(s)
  91. | /* global variable, external */
  92. /* maybe we already know it; but we need
  93. the type information anyway for other
  94. types.
  95. */
  96. { s = Lookup(findidf(str), FileScope, VAR);
  97. if (s) {
  98. tmp = s->sy_type;
  99. s->sy_type = 0;
  100. } else s = NewSymbol(str, FileScope, VAR, currnam);
  101. }
  102. 'G' type(&(s->sy_type), (int *) 0, s)
  103. { if (tmp) s->sy_type = tmp; }
  104. | /* static variable */
  105. { s = NewSymbol(str, CurrentScope, VAR, currnam); }
  106. 'S' type(&(s->sy_type), (int *) 0, s)
  107. | /* static variable, local scope */
  108. { s = NewSymbol(str, CurrentScope, VAR, currnam); }
  109. 'V' type(&(s->sy_type), (int *) 0, s)
  110. | /* register variable */
  111. { s = NewSymbol(str, CurrentScope, REGVAR, currnam); }
  112. 'r' type(&(s->sy_type), (int *) 0, s)
  113. | /* value parameter */
  114. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  115. 'p' type(&(s->sy_type), (int *) 0, s)
  116. { add_param_type('p', s); }
  117. | /* value parameter but address passed */
  118. { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
  119. 'i' type(&(s->sy_type), (int *) 0, s)
  120. { add_param_type('i', s); }
  121. | /* variable parameter */
  122. { s = NewSymbol(str, CurrentScope, VARPAR, currnam); }
  123. 'v' type(&(s->sy_type), (int *) 0, s)
  124. { add_param_type('v', s); }
  125. | /* local variable */
  126. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  127. type_name(&(s->sy_type), s)
  128. | /* lower or upper bound of array descriptor */
  129. [ 'A' { upb = LBOUND; }
  130. | 'Z' { upb = UBOUND; }
  131. ]
  132. [ ['p' | ] { s = NewSymbol(str, CurrentScope, LOCVAR, currnam);
  133. if (upb == UBOUND) add_param_type('Z', s);
  134. }
  135. | [ 'V' | 'S' ] { s = NewSymbol(str, CurrentScope, VAR, currnam); }
  136. ]
  137. type_name(&(s->sy_type), s)
  138. { p_symbol s1 = new_symbol();
  139. *s1 = *s;
  140. s->sy_class = upb;
  141. s->sy_descr = s1;
  142. }
  143. | /* function result in Pascal; ignore ??? */
  144. { s = NewSymbol(str, CurrentScope, LOCVAR, currnam); }
  145. 'X' type_name(&(s->sy_type), s)
  146. ]
  147. ';'?
  148. ;
  149. name(char **s;)
  150. :
  151. /* anything up to a ':' */
  152. NAME { *s = strval; }
  153. ;
  154. const_name(p_symbol cst;)
  155. { int type_index[2];
  156. long iconst;
  157. register char *p;
  158. }
  159. :
  160. '='
  161. [
  162. /*
  163. 'b' integer_const(&(cst->sy_const.co_ival)) /* boolean */
  164. /* |
  165. */
  166. 'c' integer_const(&(cst->sy_const.co_ival)) /* character */
  167. { cst->sy_type = char_type; }
  168. |
  169. 'i' integer_const(&(cst->sy_const.co_ival)) /* integer */
  170. { cst->sy_type = long_type; }
  171. |
  172. 'r' real_const(&(cst->sy_const.co_rval)) /* real */
  173. { cst->sy_type = double_type; }
  174. |
  175. 's' string_const /* string */
  176. { cst->sy_const.co_sval = string_val(strval);
  177. cst->sy_type = string_type;
  178. }
  179. |
  180. 'e' type_index(type_index) ',' integer_const(&(cst->sy_const.co_ival))
  181. /* enumeration constant;
  182. * enumeration type, value
  183. */
  184. { cst->sy_type = *tp_lookup(type_index); }
  185. |
  186. 'S' type_index(type_index)
  187. { cst->sy_type = *tp_lookup(type_index);
  188. cst->sy_const.co_setval = p =
  189. Malloc((unsigned) cst->sy_type->ty_size);
  190. }
  191. [ ',' integer_const(&iconst)
  192. { *p++ = iconst; }
  193. ]+
  194. /* set constant:
  195. * settype, values of the bytes
  196. * in the set.
  197. */
  198. ]
  199. ;
  200. integer_const(long *iconst;)
  201. { int sign = 0; }
  202. :
  203. [ '+' | '-' { sign = 1; } ]?
  204. INTEGER { *iconst = sign ? -ival : ival; }
  205. ;
  206. real_const(double *f;)
  207. { int sign = 0; }
  208. :
  209. [ '+' | '-' { sign = 1; } ]?
  210. REAL { *f = sign ? fval : -fval; }
  211. ;
  212. string_const
  213. :
  214. STRING /* has SINGLE quotes! */
  215. ;
  216. type_name(p_type *t; p_symbol sy;)
  217. { int type_index[2]; p_type *p; }
  218. :
  219. type_index(type_index) { p = tp_lookup(type_index); }
  220. [
  221. { if (*p && (*p)->ty_class != 0 &&
  222. (*p)->ty_class != T_CROSS) {
  223. error("Redefining (%d,%d) %d",
  224. type_index[0],
  225. type_index[1],
  226. (*p)->ty_class);
  227. }
  228. }
  229. '='
  230. type(p, type_index, sy)
  231. |
  232. { if (*t && ! *p) *p = *t;
  233. else if (*t) **t = **p;
  234. }
  235. ]
  236. { if (*p == 0) *p = new_type();
  237. *t = *p;
  238. }
  239. ;
  240. type_index(int *type_index;)
  241. :
  242. [
  243. INTEGER { type_index[0] = 0; type_index[1] = ival; }
  244. |
  245. '(' INTEGER { type_index[0] = ival; }
  246. ',' INTEGER { type_index[1] = ival; }
  247. ')'
  248. ]
  249. { last_index[0] = type_index[0];
  250. last_index[1] = type_index[1];
  251. }
  252. ;
  253. function(p_symbol p;)
  254. :
  255. { p->sy_type = new_type();
  256. p->sy_type->ty_class = T_PROCEDURE;
  257. p->sy_type->ty_size = pointer_size;
  258. }
  259. type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
  260. { if (CurrentScope != FileScope &&
  261. saw_code) {
  262. /* if saw_code is not set, it is a nested
  263. procedure
  264. */
  265. close_scope();
  266. }
  267. saw_code = 0;
  268. open_scope(p, 1);
  269. p->sy_name.nm_scope = CurrentScope;
  270. CurrentScope->sc_start = currnam->on_valu;
  271. add_scope_addr(CurrentScope);
  272. CurrentScope->sc_proclevel = currnam->on_desc;
  273. }
  274. ;
  275. routine(p_symbol p;)
  276. :
  277. { p->sy_type = new_type();
  278. p->sy_type->ty_class = T_PROCEDURE;
  279. p->sy_type->ty_size = pointer_size;
  280. if (CurrentScope != FileScope &&
  281. saw_code) {
  282. /* if saw_code is not set, it is a nested
  283. procedure
  284. */
  285. close_scope();
  286. }
  287. saw_code = 0;
  288. open_scope(p, 1);
  289. p->sy_name.nm_scope = CurrentScope;
  290. CurrentScope->sc_start = currnam->on_valu;
  291. add_scope_addr(CurrentScope);
  292. CurrentScope->sc_proclevel = currnam->on_desc;
  293. }
  294. INTEGER ';'
  295. type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
  296. ;
  297. type(p_type *ptp; int *type_index; p_symbol sy;)
  298. { register p_type tp = *ptp ? *ptp : new_type();
  299. p_type t1 = 0, t2 = 0;
  300. long ic1, ic2;
  301. int A_used = 0;
  302. int tclass;
  303. int tp_index[2];
  304. char *str;
  305. }
  306. :
  307. [
  308. /* type cross reference */
  309. /* these are used in C for references to a struct, union or
  310. * enum that has not been declared (yet)
  311. */
  312. 'x'
  313. [ 's' /* struct */
  314. { tclass = T_STRUCT; }
  315. | 'u' /* union */
  316. { tclass = T_UNION; }
  317. | 'e' /* enum */
  318. { tclass = T_ENUM; }
  319. ]
  320. { AllowName = 1; }
  321. name(&str)
  322. { sy = Lookfromscope(str2idf(str,0),TAG,CurrentScope);
  323. if (sy &&
  324. (sy->sy_type->ty_class == tclass ||
  325. sy->sy_type->ty_class == T_CROSS)) {
  326. if (tp != *ptp) free_type(tp);
  327. tp = sy->sy_type;
  328. }
  329. else {
  330. tp->ty_class = T_CROSS;
  331. tp->ty_size = tclass;
  332. sy = NewSymbol(str, CurrentScope, TAG, (struct outname *) 0);
  333. sy->sy_type = tp;
  334. }
  335. }
  336. |
  337. /* subrange */
  338. /* the integer_const's represent the lower and the upper bound.
  339. * A subrange type defined as subrange of itself is an integer type.
  340. * If the second integer_const == 0, but the first is not, we
  341. * have a floating point type with size equal to the first
  342. * integer_const.
  343. * Upperbound -1 means unsigned int or unsigned long.
  344. */
  345. 'r' type_index(tp_index) ';'
  346. [ 'A' integer_const(&ic1) { A_used = 1; }
  347. | integer_const(&ic1)
  348. ]
  349. ';'
  350. [ 'A' integer_const(&ic2) { A_used |= 2; }
  351. | integer_const(&ic2)
  352. | 'Z' integer_const(&ic2) { A_used |= 0200; }
  353. ]
  354. { if (tp != *ptp) free_type(tp);
  355. tp = subrange_type(A_used,
  356. tp_index,
  357. ic1,
  358. ic2,
  359. type_index);
  360. }
  361. |
  362. /* array; first type is bound type, next type
  363. * is element type
  364. */
  365. 'a' type(&t1, (int *) 0, (p_symbol) 0) ';' type(&t2, (int *) 0, (p_symbol) 0)
  366. { if (tp != *ptp) free_type(tp);
  367. tp = array_type(t1, t2);
  368. }
  369. |
  370. /* structure type */
  371. 's' { tp->ty_class = T_STRUCT; }
  372. structure_type(tp, sy)
  373. |
  374. /* union type */
  375. 'u' { tp->ty_class = T_UNION; }
  376. structure_type(tp, sy)
  377. |
  378. /* enumeration type */
  379. 'e' { tp->ty_class = T_ENUM; }
  380. enum_type(tp)
  381. |
  382. /* pointer type */
  383. '*' { tp->ty_class = T_POINTER;
  384. tp->ty_size = pointer_size;
  385. }
  386. type(&(tp->ty_ptrto), (int *) 0, (p_symbol) 0)
  387. |
  388. /* function type */
  389. 'f' { tp->ty_class = T_PROCEDURE;
  390. tp->ty_size = pointer_size;
  391. }
  392. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  393. /*
  394. [ %prefer
  395. ',' param_list(tp)
  396. |
  397. ]
  398. */
  399. |
  400. /* procedure type */
  401. 'Q' { tp->ty_class = T_PROCEDURE;
  402. tp->ty_size = pointer_size;
  403. }
  404. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  405. ',' param_list(tp)
  406. |
  407. /* another procedure type */
  408. 'p' { tp->ty_class = T_PROCEDURE;
  409. tp->ty_size = pointer_size;
  410. tp->ty_retval = void_type;
  411. }
  412. param_list(tp)
  413. |
  414. /* set type */
  415. /* the first integer_const represents the size in bytes,
  416. * the second one represents the low bound
  417. */
  418. 'S' { tp->ty_class = T_SET; }
  419. type(&(tp->ty_setbase), (int *) 0, (p_symbol) 0) ';'
  420. [
  421. integer_const(&(tp->ty_size)) ';'
  422. integer_const(&(tp->ty_setlow)) ';'
  423. |
  424. { set_bounds(tp); }
  425. ]
  426. |
  427. /* file type of Pascal */
  428. 'L' { tp->ty_class = T_FILE; }
  429. type(&(tp->ty_fileof), (int *) 0, (p_symbol) 0)
  430. |
  431. type_name(ptp, (p_symbol) 0)
  432. { if (type_index &&
  433. (*ptp)->ty_class == 0 &&
  434. type_index[0] == last_index[0] &&
  435. type_index[1] == last_index[1]) {
  436. **ptp = *void_type;
  437. if (*ptp != tp) free_type(tp);
  438. }
  439. tp = *ptp;
  440. }
  441. ]
  442. { if (*ptp && *ptp != tp) **ptp = *tp;
  443. else *ptp = tp;
  444. }
  445. ;
  446. structure_type(register p_type tp; p_symbol sy;)
  447. { register struct fields *fldp;
  448. char *str;
  449. }
  450. :
  451. integer_const(&(tp->ty_size)) /* size in bytes */
  452. { open_scope(sy, 0);
  453. if (sy) sy->sy_name.nm_scope = CurrentScope;
  454. }
  455. [
  456. name(&str) { fldp = get_field_space(tp, str); }
  457. type(&(fldp->fld_type), (int *) 0, (p_symbol) 0) ','
  458. integer_const(&(fldp->fld_pos)) ',' /* offset in bits */
  459. integer_const(&(fldp->fld_bitsize)) ';' /* size in bits */
  460. ]*
  461. ';' { end_field(tp);
  462. close_scope();
  463. }
  464. ;
  465. enum_type(register p_type tp;)
  466. { register struct literal *litp;
  467. long maxval = 0;
  468. register p_symbol s;
  469. }
  470. :
  471. [ { litp = get_literal_space(tp); }
  472. name(&(litp->lit_name))
  473. integer_const(&(litp->lit_val)) ','
  474. { if (maxval < litp->lit_val) maxval = litp->lit_val;
  475. AllowName = 1;
  476. s = NewSymbol(litp->lit_name, CurrentScope, CONST, (struct outname *) 0);
  477. s->sy_const.co_ival = litp->lit_val;
  478. s->sy_type = tp;
  479. }
  480. ]*
  481. ';' { end_literal(tp, maxval); }
  482. ;
  483. param_list(p_type t;)
  484. { register struct param *p;
  485. long iconst;
  486. }
  487. :
  488. integer_const(&iconst) ';' /* number of parameters */
  489. { t->ty_nparams = iconst;
  490. t->ty_params = p = (struct param *)
  491. Malloc((unsigned)(t->ty_nparams * sizeof(struct param)));
  492. }
  493. [
  494. [ 'p' { p->par_kind = 'p'; }
  495. | 'v' { p->par_kind = 'v'; }
  496. | 'i' { p->par_kind = 'i'; }
  497. ]
  498. { p->par_type = 0; }
  499. type(&(p->par_type), (int *) 0, (p_symbol) 0) ';'
  500. { p->par_off = t->ty_nbparams;
  501. t->ty_nbparams +=
  502. param_size(p->par_type, p->par_kind);
  503. p++;
  504. }
  505. ]*
  506. ;
  507. {
  508. static char *db_string;
  509. static char *DbOldPtr;
  510. static struct outname *
  511. DbString(n)
  512. struct outname *n;
  513. {
  514. currnam = n;
  515. DbPtr = n->on_mptr;
  516. db_string = DbPtr;
  517. AllowName = 1;
  518. DbParser();
  519. return currnam;
  520. }
  521. /*ARGSUSED*/
  522. DBSmessage(n)
  523. {
  524. fatal("error in symbol table string \"%s\", DbPtr = \"%s\", DbOldPtr = \"%s\"",
  525. db_string,
  526. DbPtr,
  527. DbOldPtr);
  528. }
  529. DBSonerror(tk, p)
  530. int *p;
  531. {
  532. DbPtr = DbOldPtr;
  533. /* ??? if (DBSsymb < 0) {
  534. while (*p && *p != ';') p++;
  535. if (*p) DbPtr = ";";
  536. return;
  537. }
  538. */
  539. if (! tk) {
  540. while (*p && *p != NAME) p++;
  541. if (*p) {
  542. AllowName = 1;
  543. }
  544. }
  545. else if (tk == NAME) AllowName = 1;
  546. }
  547. DBSlex()
  548. {
  549. register char *cp = DbPtr;
  550. int allow_name = AllowName;
  551. register int c;
  552. AllowName = 0;
  553. DbOldPtr = cp;
  554. c = *cp;
  555. if (c == '\\' && *(cp+1) == '\0') {
  556. currnam++;
  557. cp = currnam->on_mptr;
  558. DbOldPtr = cp;
  559. c = *cp;
  560. }
  561. if (! c) {
  562. DbPtr = cp;
  563. return -1;
  564. }
  565. if ((! allow_name && is_token(c)) || c == ';') {
  566. DbPtr = cp+1;
  567. return c;
  568. }
  569. if (is_dig(c)) {
  570. int retval = INTEGER;
  571. while (++cp, is_dig(*cp)) /* nothing */;
  572. c = *cp;
  573. if (c == '.') {
  574. retval = REAL;
  575. while (++cp, is_dig(*cp)) /* nothing */;
  576. c = *cp;
  577. }
  578. if (c == 'e' || c == 'E') {
  579. char *oldcp = cp;
  580. cp++;
  581. c = *cp;
  582. if (c == '-' || c == '+') {
  583. cp++;
  584. c = *cp;
  585. }
  586. if (is_dig(c)) {
  587. retval = REAL;
  588. while (++cp, is_dig(*cp)) /* nothing */;
  589. }
  590. else cp = oldcp;
  591. }
  592. c = *cp;
  593. *cp = 0;
  594. if (retval == INTEGER) {
  595. ival = str2long(DbOldPtr, 10);
  596. }
  597. else {
  598. fval = atof(DbOldPtr);
  599. }
  600. *cp = c;
  601. DbPtr = cp;
  602. return retval;
  603. }
  604. if (c == '\'') {
  605. cp++;
  606. strval = cp;
  607. while ((c = *cp) && c != '\'') {
  608. if (c == '\\') cp++; /* backslash escapes next character */
  609. if (!(c = *cp)) break; /* but not a null byte */
  610. cp++;
  611. }
  612. if (! c) DBSmessage(0); /* no return */
  613. *cp = 0;
  614. DbPtr = cp + 1;
  615. return STRING;
  616. }
  617. strval = cp;
  618. while ((c = *cp) && c != ':' && c != ',') cp++;
  619. DbPtr = *cp ? cp+1 : cp;
  620. *cp = 0;
  621. return NAME;
  622. }
  623. static struct fields *
  624. get_field_space(tp, s)
  625. register p_type tp;
  626. char *s;
  627. {
  628. register struct fields *p;
  629. p_symbol sy;
  630. if (! (tp->ty_nfields & 07)) {
  631. tp->ty_fields = (struct fields *)
  632. Realloc((char *) tp->ty_fields,
  633. (tp->ty_nfields+8)*sizeof(struct fields));
  634. }
  635. p = &tp->ty_fields[tp->ty_nfields++];
  636. p->fld_name = s;
  637. p->fld_type = 0;
  638. sy = NewSymbol(s, CurrentScope, FIELD, currnam);
  639. sy->sy_field = p;
  640. return p;
  641. }
  642. static
  643. end_field(tp)
  644. register p_type tp;
  645. {
  646. tp->ty_fields = (struct fields *)
  647. Realloc((char *) tp->ty_fields,
  648. tp->ty_nfields * sizeof(struct fields));
  649. }
  650. static struct literal *
  651. get_literal_space(tp)
  652. register p_type tp;
  653. {
  654. if (! (tp->ty_nenums & 07)) {
  655. tp->ty_literals = (struct literal *)
  656. Realloc((char *) tp->ty_literals,
  657. (tp->ty_nenums+8)*sizeof(struct literal));
  658. }
  659. return &tp->ty_literals[tp->ty_nenums++];
  660. }
  661. static char *
  662. string_val(s)
  663. char *s;
  664. {
  665. register char *ns = s, *os = s;
  666. register unsigned int i = 1;
  667. for (;;) {
  668. if (!*os) break;
  669. i++;
  670. if (*os == '\\') {
  671. os++;
  672. *ns++ = *os++;
  673. }
  674. else *ns++ = *os++;
  675. }
  676. *ns = '\0';
  677. return Salloc(s, i);
  678. }
  679. static char *AckStrings; /* ACK a.out string table */
  680. static struct outname *AckNames; /* ACK a.out symbol table entries */
  681. static unsigned int NAckNames; /* Number of ACK symbol table entries */
  682. static struct outname *EndAckNames; /* &AckNames[NAckNames] */
  683. /* Read the symbol table from file 'f', which is supposed to be an
  684. ACK a.out format file. Offer db strings to the db string parser.
  685. */
  686. int
  687. DbRead(f)
  688. char *f;
  689. {
  690. struct outhead h;
  691. register struct outname *n;
  692. register struct outname *line_file = 0;
  693. long OffsetStrings;
  694. int lbrac_required = 0;
  695. int needs_newscope = 0;
  696. int lbrac_level = 0;
  697. /* Open file, read header, and check magic word */
  698. if (! rd_open(f)) {
  699. fatal("%s: could not open", f);
  700. }
  701. rd_ohead(&h);
  702. if (BADMAGIC(h) && h.oh_magic != O_CONVERTED) {
  703. fatal("%s: not an object file", f);
  704. }
  705. /* Allocate space for name table and read it */
  706. AckNames = (struct outname *)
  707. Malloc((unsigned)(sizeof(struct outname) * h.oh_nname));
  708. AckStrings = Malloc((unsigned) h.oh_nchar);
  709. rd_name(AckNames, h.oh_nname);
  710. rd_string(AckStrings, h.oh_nchar);
  711. /* Adjust file offsets in name table to point at strings */
  712. OffsetStrings = OFF_CHAR(h);
  713. NAckNames = h.oh_nname;
  714. EndAckNames = &AckNames[h.oh_nname];
  715. for (n = EndAckNames; --n >= AckNames;) {
  716. if (n->on_foff) {
  717. if ((unsigned)(n->on_foff - OffsetStrings) >= h.oh_nchar) {
  718. fatal("%s: error in object file", f);
  719. }
  720. n->on_mptr = AckStrings + (n->on_foff - OffsetStrings);
  721. }
  722. else n->on_mptr = 0;
  723. }
  724. /* Offer strings to the db string parser if they contain a ':'.
  725. Also offer filename-line number information to add_position_addr().
  726. Here, the order may be important.
  727. */
  728. for (n = &AckNames[0]; n < EndAckNames; n++) {
  729. int tp = n->on_type >> 8;
  730. register p_symbol sym;
  731. if (tp & (S_STB >> 8)) {
  732. switch(tp) {
  733. #ifdef N_BINCL
  734. case N_BINCL:
  735. n->on_valu = (long) line_file;
  736. line_file = n;
  737. break;
  738. case N_EINCL:
  739. if (line_file) {
  740. line_file = (struct outname *) line_file->on_valu;
  741. }
  742. break;
  743. #endif
  744. case N_SO:
  745. if (n->on_mptr[strlen(n->on_mptr)-1] == '/') {
  746. /* another N_SO follows ... */
  747. break;
  748. }
  749. clean_tp_tab();
  750. while (CurrentScope != PervasiveScope) {
  751. close_scope();
  752. }
  753. saw_code = 0;
  754. sym = add_file(n->on_mptr);
  755. if (! listfile) newfile(sym->sy_idf);
  756. open_scope(sym, 0);
  757. sym->sy_file->f_scope = CurrentScope;
  758. FileScope = CurrentScope;
  759. CurrentScope->sc_start = n->on_valu;
  760. /* fall through */
  761. case N_SOL:
  762. if (line_file) n->on_valu = line_file->on_valu;
  763. line_file = n;
  764. break;
  765. case N_MAIN:
  766. if (! FileScope) fatal("No file scope");
  767. newfile(FileScope->sc_definedby->sy_idf);
  768. break;
  769. case N_SLINE:
  770. assert(line_file);
  771. if (CurrentScope->sc_start) {
  772. register p_scope sc =
  773. get_scope_from_addr(n->on_valu);
  774. if (sc) CurrentScope = sc;
  775. }
  776. else if (! saw_code) {
  777. CurrentScope->sc_start = n->on_valu;
  778. add_scope_addr(CurrentScope);
  779. }
  780. if (!CurrentScope->sc_bp_opp
  781. || CurrentScope->sc_bp_lineno > n->on_desc) {
  782. CurrentScope->sc_bp_opp = n->on_valu;
  783. CurrentScope->sc_bp_lineno = n->on_desc;
  784. }
  785. saw_code = 1;
  786. add_position_addr(line_file->on_mptr, n);
  787. break;
  788. case N_LBRAC: /* block, desc = nesting level */
  789. if (lbrac_level && ! lbrac_required) {
  790. open_scope((p_symbol) 0, 0);
  791. saw_code = 0;
  792. }
  793. else {
  794. /* Sun-4 ld does not relocate LBRAC
  795. values, so we cannot use it
  796. register p_scope sc =
  797. get_scope_from_addr(n->on_valu);
  798. if (!sc || sc->sc_bp_opp) {
  799. }
  800. else CurrentScope = sc;
  801. */
  802. }
  803. lbrac_level++;
  804. needs_newscope = 1;
  805. break;
  806. #ifdef N_SCOPE
  807. case N_SCOPE:
  808. if (n->on_mptr && strchr(n->on_mptr, ':')) {
  809. n = DbString(n);
  810. }
  811. break;
  812. #endif
  813. case N_RBRAC: /* end block, desc = nesting level */
  814. if (CurrentScope != FileScope) close_scope();
  815. needs_newscope = 1;
  816. if (--lbrac_level == 0) needs_newscope = 0;
  817. saw_code = 0;
  818. break;
  819. case N_FUN: /* function, value = address */
  820. case N_GSYM: /* global variable */
  821. case N_STSYM: /* data, static, value = address */
  822. case N_LCSYM: /* bss, static, value = address */
  823. case N_RSYM: /* register var, value = reg number */
  824. case N_SSYM: /* struct/union el, value = offset */
  825. case N_PSYM: /* parameter, value = offset from AP */
  826. case N_LSYM: /* local sym, value = offset from FP */
  827. if (needs_newscope) {
  828. open_scope((p_symbol) 0, 0);
  829. saw_code = 0;
  830. needs_newscope = 0;
  831. lbrac_required = 1;
  832. }
  833. if (n->on_mptr && strchr(n->on_mptr, ':')) {
  834. n = DbString(n);
  835. }
  836. break;
  837. default:
  838. /*
  839. if (n->on_mptr && (n->on_type&S_TYP) >= S_MIN) {
  840. struct idf *id = str2idf(n->on_mptr, 0);
  841. sym = new_symbol();
  842. sym->sy_next = id->id_def;
  843. id->id_def = sym;
  844. sym->sy_class = SYMENTRY;
  845. sym->sy_onam = *n;
  846. sym->sy_idf = id;
  847. }
  848. */
  849. break;
  850. }
  851. }
  852. }
  853. close_scope();
  854. add_position_addr((char *) 0, (struct outname *) 0);
  855. clean_tp_tab();
  856. rd_close();
  857. return (h.oh_magic == O_CONVERTED);
  858. }
  859. }