db_symtab.g 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. { if (*t && ! *p) *p = *t;
  232. else if (*t) **t = **p;
  233. }
  234. ]
  235. { if (*p == 0) *p = new_type();
  236. *t = *p;
  237. }
  238. ;
  239. type_index(int *type_index;)
  240. :
  241. [
  242. INTEGER { type_index[0] = 0; type_index[1] = ival; }
  243. |
  244. '(' INTEGER { type_index[0] = ival; }
  245. ',' INTEGER { type_index[1] = ival; }
  246. ')'
  247. ]
  248. { last_index[0] = type_index[0];
  249. last_index[1] = type_index[1];
  250. }
  251. ;
  252. function(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. }
  258. type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
  259. { if (CurrentScope != FileScope &&
  260. saw_code) {
  261. /* if saw_code is not set, it is a nested
  262. procedure
  263. */
  264. close_scope();
  265. }
  266. saw_code = 0;
  267. open_scope(p, 1);
  268. p->sy_name.nm_scope = CurrentScope;
  269. CurrentScope->sc_start = currnam->on_valu;
  270. add_scope_addr(CurrentScope);
  271. CurrentScope->sc_proclevel = currnam->on_desc;
  272. }
  273. ;
  274. routine(p_symbol p;)
  275. :
  276. { p->sy_type = new_type();
  277. p->sy_type->ty_class = T_PROCEDURE;
  278. p->sy_type->ty_size = pointer_size;
  279. if (CurrentScope != FileScope &&
  280. saw_code) {
  281. /* if saw_code is not set, it is a nested
  282. procedure
  283. */
  284. close_scope();
  285. }
  286. saw_code = 0;
  287. open_scope(p, 1);
  288. p->sy_name.nm_scope = CurrentScope;
  289. CurrentScope->sc_start = currnam->on_valu;
  290. add_scope_addr(CurrentScope);
  291. CurrentScope->sc_proclevel = currnam->on_desc;
  292. }
  293. INTEGER ';'
  294. type(&(p->sy_type->ty_retval), (int *) 0, (p_symbol) 0)
  295. ;
  296. type(p_type *ptp; int *type_index; p_symbol sy;)
  297. { register p_type tp = *ptp ? *ptp : new_type();
  298. p_type t1 = 0, t2 = 0;
  299. long ic1, ic2;
  300. int A_used = 0;
  301. int tclass;
  302. int tp_index[2];
  303. char *str;
  304. }
  305. :
  306. [
  307. /* type cross reference */
  308. /* these are used in C for references to a struct, union or
  309. * enum that has not been declared (yet)
  310. */
  311. 'x'
  312. [ 's' /* struct */
  313. { tclass = T_STRUCT; }
  314. | 'u' /* union */
  315. { tclass = T_UNION; }
  316. | 'e' /* enum */
  317. { tclass = T_ENUM; }
  318. ]
  319. { AllowName = 1; }
  320. name(&str)
  321. { sy = Lookfromscope(str2idf(str,0),TAG,CurrentScope);
  322. if (sy &&
  323. (sy->sy_type->ty_class == tclass ||
  324. sy->sy_type->ty_class == T_CROSS)) {
  325. if (tp != *ptp) free_type(tp);
  326. tp = sy->sy_type;
  327. }
  328. else {
  329. tp->ty_class = T_CROSS;
  330. tp->ty_size = tclass;
  331. sy = NewSymbol(str, CurrentScope, TAG, (struct outname *) 0);
  332. sy->sy_type = tp;
  333. }
  334. }
  335. |
  336. /* subrange */
  337. /* the integer_const's represent the lower and the upper bound.
  338. * A subrange type defined as subrange of itself is an integer type.
  339. * If the second integer_const == 0, but the first is not, we
  340. * have a floating point type with size equal to the first
  341. * integer_const.
  342. * Upperbound -1 means unsigned int or unsigned long.
  343. */
  344. 'r' type_index(tp_index) ';'
  345. [ 'A' integer_const(&ic1) { A_used = 1; }
  346. | integer_const(&ic1)
  347. ]
  348. ';'
  349. [ 'A' integer_const(&ic2) { A_used |= 2; }
  350. | integer_const(&ic2)
  351. | 'Z' integer_const(&ic2) { A_used |= 0200; }
  352. ]
  353. { if (tp != *ptp) free_type(tp);
  354. tp = subrange_type(A_used,
  355. tp_index,
  356. ic1,
  357. ic2,
  358. type_index);
  359. }
  360. |
  361. /* array; first type is bound type, next type
  362. * is element type
  363. */
  364. 'a' type(&t1, (int *) 0, (p_symbol) 0) ';' type(&t2, (int *) 0, (p_symbol) 0)
  365. { if (tp != *ptp) free_type(tp);
  366. tp = array_type(t1, t2);
  367. }
  368. |
  369. /* structure type */
  370. 's' { tp->ty_class = T_STRUCT; }
  371. structure_type(tp, sy)
  372. |
  373. /* union type */
  374. 'u' { tp->ty_class = T_UNION; }
  375. structure_type(tp, sy)
  376. |
  377. /* enumeration type */
  378. 'e' { tp->ty_class = T_ENUM; }
  379. enum_type(tp)
  380. |
  381. /* pointer type */
  382. '*' { tp->ty_class = T_POINTER;
  383. tp->ty_size = pointer_size;
  384. }
  385. type(&(tp->ty_ptrto), (int *) 0, (p_symbol) 0)
  386. |
  387. /* function type */
  388. 'f' { tp->ty_class = T_PROCEDURE;
  389. tp->ty_size = pointer_size;
  390. }
  391. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  392. /*
  393. [ %prefer
  394. ',' param_list(tp)
  395. |
  396. ]
  397. */
  398. |
  399. /* procedure type */
  400. 'Q' { tp->ty_class = T_PROCEDURE;
  401. tp->ty_size = pointer_size;
  402. }
  403. type(&(tp->ty_retval), (int *) 0, (p_symbol) 0)
  404. ',' param_list(tp)
  405. |
  406. /* another procedure type */
  407. 'p' { tp->ty_class = T_PROCEDURE;
  408. tp->ty_size = pointer_size;
  409. tp->ty_retval = void_type;
  410. }
  411. param_list(tp)
  412. |
  413. /* set type */
  414. /* the first integer_const represents the size in bytes,
  415. * the second one represents the low bound
  416. */
  417. 'S' { tp->ty_class = T_SET; }
  418. type(&(tp->ty_setbase), (int *) 0, (p_symbol) 0) ';'
  419. [
  420. integer_const(&(tp->ty_size)) ';'
  421. integer_const(&(tp->ty_setlow)) ';'
  422. |
  423. { set_bounds(tp); }
  424. ]
  425. |
  426. /* file type of Pascal */
  427. 'L' { tp->ty_class = T_FILE; }
  428. type(&(tp->ty_fileof), (int *) 0, (p_symbol) 0)
  429. |
  430. type_name(ptp, (p_symbol) 0)
  431. { if (type_index &&
  432. (*ptp)->ty_class == 0 &&
  433. type_index[0] == last_index[0] &&
  434. type_index[1] == last_index[1]) {
  435. **ptp = *void_type;
  436. if (*ptp != tp) free_type(tp);
  437. }
  438. tp = *ptp;
  439. }
  440. ]
  441. { if (*ptp && *ptp != tp) **ptp = *tp;
  442. else *ptp = tp;
  443. }
  444. ;
  445. structure_type(register p_type tp; p_symbol sy;)
  446. { register struct fields *fldp;
  447. char *str;
  448. }
  449. :
  450. integer_const(&(tp->ty_size)) /* size in bytes */
  451. { open_scope(sy, 0);
  452. if (sy) sy->sy_name.nm_scope = CurrentScope;
  453. }
  454. [
  455. name(&str) { fldp = get_field_space(tp, str); }
  456. type(&(fldp->fld_type), (int *) 0, (p_symbol) 0) ','
  457. integer_const(&(fldp->fld_pos)) ',' /* offset in bits */
  458. integer_const(&(fldp->fld_bitsize)) ';' /* size in bits */
  459. ]*
  460. ';' { end_field(tp);
  461. close_scope();
  462. }
  463. ;
  464. enum_type(register p_type tp;)
  465. { register struct literal *litp;
  466. long maxval = 0;
  467. register p_symbol s;
  468. }
  469. :
  470. [ { litp = get_literal_space(tp); }
  471. name(&(litp->lit_name))
  472. integer_const(&(litp->lit_val)) ','
  473. { if (maxval < litp->lit_val) maxval = litp->lit_val;
  474. AllowName = 1;
  475. s = NewSymbol(litp->lit_name, CurrentScope, CONST, (struct outname *) 0);
  476. s->sy_const.co_ival = litp->lit_val;
  477. s->sy_type = tp;
  478. }
  479. ]*
  480. ';' { end_literal(tp, maxval); }
  481. ;
  482. param_list(p_type t;)
  483. { register struct param *p;
  484. long iconst;
  485. }
  486. :
  487. integer_const(&iconst) ';' /* number of parameters */
  488. { t->ty_nparams = iconst;
  489. t->ty_params = p = (struct param *)
  490. Malloc((unsigned)(t->ty_nparams * sizeof(struct param)));
  491. }
  492. [
  493. [ 'p' { p->par_kind = 'p'; }
  494. | 'v' { p->par_kind = 'v'; }
  495. | 'i' { p->par_kind = 'i'; }
  496. ]
  497. type(&(p->par_type), (int *) 0, (p_symbol) 0) ';'
  498. { p->par_off = t->ty_nbparams;
  499. t->ty_nbparams +=
  500. param_size(p->par_type, p->par_kind);
  501. p++;
  502. }
  503. ]*
  504. ;
  505. {
  506. static char *db_string;
  507. static char *DbOldPtr;
  508. static struct outname *
  509. DbString(n)
  510. struct outname *n;
  511. {
  512. currnam = n;
  513. DbPtr = n->on_mptr;
  514. db_string = DbPtr;
  515. AllowName = 1;
  516. DbParser();
  517. return currnam;
  518. }
  519. /*ARGSUSED*/
  520. DBSmessage(n)
  521. {
  522. fatal("error in symbol table string \"%s\", DbPtr = \"%s\", DbOldPtr = \"%s\"",
  523. db_string,
  524. DbPtr,
  525. DbOldPtr);
  526. }
  527. DBSonerror(tk, p)
  528. int *p;
  529. {
  530. DbPtr = DbOldPtr;
  531. /* ??? if (DBSsymb < 0) {
  532. while (*p && *p != ';') p++;
  533. if (*p) DbPtr = ";";
  534. return;
  535. }
  536. */
  537. if (! tk) {
  538. while (*p && *p != NAME) p++;
  539. if (*p) {
  540. AllowName = 1;
  541. }
  542. }
  543. else if (tk == NAME) AllowName = 1;
  544. }
  545. DBSlex()
  546. {
  547. register char *cp = DbPtr;
  548. int allow_name = AllowName;
  549. register int c;
  550. AllowName = 0;
  551. DbOldPtr = cp;
  552. c = *cp;
  553. if (c == '\\' && *(cp+1) == '\0') {
  554. currnam++;
  555. cp = currnam->on_mptr;
  556. DbOldPtr = cp;
  557. c = *cp;
  558. }
  559. if (! c) {
  560. DbPtr = cp;
  561. return -1;
  562. }
  563. if ((! allow_name && is_token(c)) || c == ';') {
  564. DbPtr = cp+1;
  565. return c;
  566. }
  567. if (is_dig(c)) {
  568. int retval = INTEGER;
  569. while (++cp, is_dig(*cp)) /* nothing */;
  570. c = *cp;
  571. if (c == '.') {
  572. retval = REAL;
  573. while (++cp, is_dig(*cp)) /* nothing */;
  574. c = *cp;
  575. }
  576. if (c == 'e' || c == 'E') {
  577. char *oldcp = cp;
  578. cp++;
  579. c = *cp;
  580. if (c == '-' || c == '+') {
  581. cp++;
  582. c = *cp;
  583. }
  584. if (is_dig(c)) {
  585. retval = REAL;
  586. while (++cp, is_dig(*cp)) /* nothing */;
  587. }
  588. else cp = oldcp;
  589. }
  590. c = *cp;
  591. *cp = 0;
  592. if (retval == INTEGER) {
  593. ival = str2long(DbOldPtr, 10);
  594. }
  595. else {
  596. fval = atof(DbOldPtr);
  597. }
  598. *cp = c;
  599. DbPtr = cp;
  600. return retval;
  601. }
  602. if (c == '\'') {
  603. cp++;
  604. strval = cp;
  605. while ((c = *cp) && c != '\'') {
  606. if (c == '\\') cp++; /* backslash escapes next character */
  607. if (!(c = *cp)) break; /* but not a null byte */
  608. cp++;
  609. }
  610. if (! c) DBSmessage(0); /* no return */
  611. *cp = 0;
  612. DbPtr = cp + 1;
  613. return STRING;
  614. }
  615. strval = cp;
  616. while ((c = *cp) && c != ':' && c != ',') cp++;
  617. DbPtr = *cp ? cp+1 : cp;
  618. *cp = 0;
  619. return NAME;
  620. }
  621. static struct fields *
  622. get_field_space(tp, s)
  623. register p_type tp;
  624. char *s;
  625. {
  626. register struct fields *p;
  627. p_symbol sy;
  628. if (! (tp->ty_nfields & 07)) {
  629. tp->ty_fields = (struct fields *)
  630. Realloc((char *) tp->ty_fields,
  631. (tp->ty_nfields+8)*sizeof(struct fields));
  632. }
  633. p = &tp->ty_fields[tp->ty_nfields++];
  634. p->fld_name = s;
  635. p->fld_type = 0;
  636. sy = NewSymbol(s, CurrentScope, FIELD, currnam);
  637. sy->sy_field = p;
  638. return p;
  639. }
  640. static
  641. end_field(tp)
  642. register p_type tp;
  643. {
  644. tp->ty_fields = (struct fields *)
  645. Realloc((char *) tp->ty_fields,
  646. tp->ty_nfields * sizeof(struct fields));
  647. }
  648. static struct literal *
  649. get_literal_space(tp)
  650. register p_type tp;
  651. {
  652. if (! (tp->ty_nenums & 07)) {
  653. tp->ty_literals = (struct literal *)
  654. Realloc((char *) tp->ty_literals,
  655. (tp->ty_nenums+8)*sizeof(struct literal));
  656. }
  657. return &tp->ty_literals[tp->ty_nenums++];
  658. }
  659. static char *
  660. string_val(s)
  661. char *s;
  662. {
  663. register char *ns = s, *os = s;
  664. register unsigned int i = 1;
  665. for (;;) {
  666. if (!*os) break;
  667. i++;
  668. if (*os == '\\') {
  669. os++;
  670. *ns++ = *os++;
  671. }
  672. else *ns++ = *os++;
  673. }
  674. *ns = '\0';
  675. return Salloc(s, i);
  676. }
  677. static char *AckStrings; /* ACK a.out string table */
  678. static struct outname *AckNames; /* ACK a.out symbol table entries */
  679. static unsigned int NAckNames; /* Number of ACK symbol table entries */
  680. static struct outname *EndAckNames; /* &AckNames[NAckNames] */
  681. /* Read the symbol table from file 'f', which is supposed to be an
  682. ACK a.out format file. Offer db strings to the db string parser.
  683. */
  684. int
  685. DbRead(f)
  686. char *f;
  687. {
  688. struct outhead h;
  689. register struct outname *n;
  690. register struct outname *line_file = 0;
  691. long OffsetStrings;
  692. int lbrac_required = 0;
  693. int needs_newscope = 0;
  694. int lbrac_level = 0;
  695. /* Open file, read header, and check magic word */
  696. if (! rd_open(f)) {
  697. fatal("%s: could not open", f);
  698. }
  699. rd_ohead(&h);
  700. if (BADMAGIC(h) && h.oh_magic != O_CONVERTED) {
  701. fatal("%s: not an object file", f);
  702. }
  703. /* Allocate space for name table and read it */
  704. AckNames = (struct outname *)
  705. Malloc((unsigned)(sizeof(struct outname) * h.oh_nname));
  706. AckStrings = Malloc((unsigned) h.oh_nchar);
  707. rd_name(AckNames, h.oh_nname);
  708. rd_string(AckStrings, h.oh_nchar);
  709. /* Adjust file offsets in name table to point at strings */
  710. OffsetStrings = OFF_CHAR(h);
  711. NAckNames = h.oh_nname;
  712. EndAckNames = &AckNames[h.oh_nname];
  713. for (n = EndAckNames; --n >= AckNames;) {
  714. if (n->on_foff) {
  715. if ((unsigned)(n->on_foff - OffsetStrings) >= h.oh_nchar) {
  716. fatal("%s: error in object file", f);
  717. }
  718. n->on_mptr = AckStrings + (n->on_foff - OffsetStrings);
  719. }
  720. else n->on_mptr = 0;
  721. }
  722. /* Offer strings to the db string parser if they contain a ':'.
  723. Also offer filename-line number information to add_position_addr().
  724. Here, the order may be important.
  725. */
  726. for (n = &AckNames[0]; n < EndAckNames; n++) {
  727. int tp = n->on_type >> 8;
  728. register p_symbol sym;
  729. if (tp & (S_STB >> 8)) {
  730. switch(tp) {
  731. #ifdef N_BINCL
  732. case N_BINCL:
  733. n->on_valu = (long) line_file;
  734. line_file = n;
  735. break;
  736. case N_EINCL:
  737. if (line_file) {
  738. line_file = (struct outname *) line_file->on_valu;
  739. }
  740. break;
  741. #endif
  742. case N_SO:
  743. if (n->on_mptr[strlen(n->on_mptr)-1] == '/') {
  744. /* another N_SO follows ... */
  745. break;
  746. }
  747. clean_tp_tab();
  748. while (CurrentScope != PervasiveScope) {
  749. close_scope();
  750. }
  751. saw_code = 0;
  752. sym = add_file(n->on_mptr);
  753. if (! listfile) newfile(sym->sy_idf);
  754. open_scope(sym, 0);
  755. sym->sy_file->f_scope = CurrentScope;
  756. FileScope = CurrentScope;
  757. CurrentScope->sc_start = n->on_valu;
  758. /* fall through */
  759. case N_SOL:
  760. if (! line_file) line_file = n;
  761. else line_file->on_mptr = n->on_mptr;
  762. break;
  763. case N_MAIN:
  764. if (! FileScope) fatal("No file scope");
  765. newfile(FileScope->sc_definedby->sy_idf);
  766. break;
  767. case N_SLINE:
  768. assert(line_file);
  769. if (CurrentScope->sc_start) {
  770. register p_scope sc =
  771. get_scope_from_addr(n->on_valu);
  772. if (sc) CurrentScope = sc;
  773. }
  774. if (! saw_code && !CurrentScope->sc_bp_opp) {
  775. CurrentScope->sc_bp_opp = n->on_valu;
  776. if (! CurrentScope->sc_start) {
  777. CurrentScope->sc_start = n->on_valu;
  778. add_scope_addr(CurrentScope);
  779. }
  780. }
  781. saw_code = 1;
  782. add_position_addr(line_file->on_mptr, n);
  783. break;
  784. case N_LBRAC: /* block, desc = nesting level */
  785. if (lbrac_level && ! lbrac_required) {
  786. open_scope((p_symbol) 0, 0);
  787. saw_code = 0;
  788. }
  789. else {
  790. /* Sun-4 ld does not relocate LBRAC
  791. values, so we cannot use it
  792. register p_scope sc =
  793. get_scope_from_addr(n->on_valu);
  794. if (!sc || sc->sc_bp_opp) {
  795. }
  796. else CurrentScope = sc;
  797. */
  798. }
  799. lbrac_level++;
  800. needs_newscope = 1;
  801. break;
  802. #ifdef N_SCOPE
  803. case N_SCOPE:
  804. if (n->on_mptr && strindex(n->on_mptr, ':')) {
  805. n = DbString(n);
  806. }
  807. break;
  808. #endif
  809. case N_RBRAC: /* end block, desc = nesting level */
  810. if (CurrentScope != FileScope) close_scope();
  811. needs_newscope = 1;
  812. if (--lbrac_level == 0) needs_newscope = 0;
  813. saw_code = 0;
  814. break;
  815. case N_FUN: /* function, value = address */
  816. case N_GSYM: /* global variable */
  817. case N_STSYM: /* data, static, value = address */
  818. case N_LCSYM: /* bss, static, value = address */
  819. case N_RSYM: /* register var, value = reg number */
  820. case N_SSYM: /* struct/union el, value = offset */
  821. case N_PSYM: /* parameter, value = offset from AP */
  822. case N_LSYM: /* local sym, value = offset from FP */
  823. if (needs_newscope) {
  824. open_scope((p_symbol) 0, 0);
  825. saw_code = 0;
  826. needs_newscope = 0;
  827. lbrac_required = 1;
  828. }
  829. if (n->on_mptr && strindex(n->on_mptr, ':')) {
  830. n = DbString(n);
  831. }
  832. break;
  833. default:
  834. /*
  835. if (n->on_mptr && (n->on_type&S_TYP) >= S_MIN) {
  836. struct idf *id = str2idf(n->on_mptr, 0);
  837. sym = new_symbol();
  838. sym->sy_next = id->id_def;
  839. id->id_def = sym;
  840. sym->sy_class = SYMENTRY;
  841. sym->sy_onam = *n;
  842. sym->sy_idf = id;
  843. }
  844. */
  845. break;
  846. }
  847. }
  848. }
  849. close_scope();
  850. add_position_addr((char *) 0, (struct outname *) 0);
  851. clean_tp_tab();
  852. rd_close();
  853. return (h.oh_magic == O_CONVERTED);
  854. }
  855. }