db_symtab.g 20 KB

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