idf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* $Header$ */
  2. /* IDENTIFIER FIDDLING & SYMBOL TABLE HANDLING */
  3. #include "debug.h"
  4. #include "idfsize.h"
  5. #include "botch_free.h"
  6. #include "nopp.h"
  7. #include "alloc.h"
  8. #include "arith.h"
  9. #include "align.h"
  10. #include "LLlex.h"
  11. #include "level.h"
  12. #include "stack.h"
  13. #include "idf.h"
  14. #include "label.h"
  15. #include "def.h"
  16. #include "type.h"
  17. #include "struct.h"
  18. #include "declarator.h"
  19. #include "decspecs.h"
  20. #include "sizes.h"
  21. #include "Lpars.h"
  22. #include "assert.h"
  23. #include "specials.h" /* registration of special identifiers */
  24. #include "storage.h"
  25. int idfsize = IDFSIZE;
  26. extern char options[];
  27. char sp_occurred[SP_TOTAL]; /* indicate occurrence of special id */
  28. struct idf *idf_hashtable[HASHSIZE];
  29. /* All identifiers can in principle be reached through
  30. idf_hashtable; idf_hashtable[hc] is the start of a chain of
  31. idf's whose tags all hash to hc. Each idf is the start of
  32. a chain of def's for that idf, sorted according to level,
  33. with the most recent one on top.
  34. Any identifier occurring on a level is entered into this
  35. list, regardless of the nature of its declaration
  36. (variable, selector, structure tag, etc.).
  37. */
  38. struct idf *
  39. idf_hashed(tg, size, hc)
  40. char *tg;
  41. int size; /* includes the '\0' character */
  42. int hc;
  43. {
  44. /* The tag tg with length size and known hash value hc is
  45. looked up in the identifier table; if not found, it is
  46. entered. A pointer to it is returned.
  47. The identifier has already been truncated to idfsize
  48. characters.
  49. */
  50. register struct idf **hook = &idf_hashtable[hc], *notch;
  51. while ((notch = *hook)) {
  52. register cmp = strcmp(tg, notch->id_text);
  53. if (cmp < 0)
  54. break;
  55. else
  56. if (cmp == 0) {
  57. /* suppose that special identifiers, as
  58. "setjmp", are already inserted
  59. */
  60. sp_occurred[notch->id_special] = 1;
  61. return notch;
  62. }
  63. else
  64. hook = &notch->next;
  65. }
  66. /* a new struct idf must be inserted at the hook */
  67. notch = new_idf();
  68. clear((char *)notch, sizeof(struct idf));
  69. notch->next = *hook;
  70. *hook = notch; /* hooked in */
  71. notch->id_text = Salloc(tg, size);
  72. #ifndef NOPP
  73. notch->id_resmac = 0;
  74. #endif NOPP
  75. return notch;
  76. }
  77. #ifdef DEBUG
  78. hash_stat()
  79. {
  80. if (options['h']) {
  81. int i;
  82. printf("Hash table tally:\n");
  83. for (i = 0; i < HASHSIZE; i++) {
  84. struct idf *notch = idf_hashtable[i];
  85. int cnt = 0;
  86. while (notch) {
  87. cnt++;
  88. notch = notch->next;
  89. }
  90. printf("%d %d\n", i, cnt);
  91. }
  92. printf("End hash table tally\n");
  93. }
  94. }
  95. #endif DEBUG
  96. struct idf *
  97. str2idf(tg)
  98. char tg[];
  99. {
  100. /* str2idf() returns an entry in the symbol table for the
  101. identifier tg. If necessary, an entry is created.
  102. It is used where the text of the identifier is available
  103. but its hash value is not; otherwise idf_hashed() is to
  104. be used.
  105. */
  106. register char *cp = tg;
  107. register int hash;
  108. register int pos = -1;
  109. register int ch;
  110. char ntg[IDFSIZE + 1];
  111. register char *ncp = ntg;
  112. hash = STARTHASH();
  113. while (++pos < idfsize && (ch = *cp++)) {
  114. *ncp++ = ch;
  115. hash = ENHASH(hash, ch, pos);
  116. }
  117. hash = STOPHASH(hash);
  118. *ncp++ = '\0';
  119. return idf_hashed(ntg, ncp - ntg, hash);
  120. }
  121. struct idf *
  122. gen_idf()
  123. {
  124. /* A new idf is created out of nowhere, to serve as an
  125. anonymous name.
  126. */
  127. static int name_cnt;
  128. char buff[100];
  129. char *sprintf();
  130. sprintf(buff, "#%d in %s, line %u",
  131. ++name_cnt, dot.tk_file, dot.tk_line);
  132. return str2idf(buff);
  133. }
  134. int
  135. is_anon_idf(idf)
  136. struct idf *idf;
  137. {
  138. return idf->id_text[0] == '#';
  139. }
  140. declare_idf(ds, dc, lvl)
  141. struct decspecs *ds;
  142. struct declarator *dc;
  143. {
  144. /* The identifier inside dc is declared on the level lvl, with
  145. properties deduced from the decspecs ds and the declarator
  146. dc.
  147. The level is given explicitly to be able to insert, e.g.,
  148. labels on the outermost level inside the function.
  149. This routine implements the rich semantics of C
  150. declarations.
  151. */
  152. register struct idf *idf = dc->dc_idf;
  153. register int sc = ds->ds_sc;
  154. /* This local copy is essential:
  155. char b(), c;
  156. makes b GLOBAL and c AUTO.
  157. */
  158. register struct def *def = idf->id_def; /* may be NULL */
  159. register struct type *type;
  160. struct stack_level *stl = stack_level_of(lvl);
  161. char formal_array = 0;
  162. /* determine the present type */
  163. if (ds->ds_type == 0) {
  164. /* at the L_FORMAL1 level there is no type specified yet
  165. */
  166. ASSERT(lvl == L_FORMAL1);
  167. type = 0;
  168. }
  169. else {
  170. /* combine the decspecs and the declarator into one type */
  171. type = declare_type(ds->ds_type, dc);
  172. if (type->tp_size == (arith)-1) {
  173. /* the type is not yet known */
  174. if (actual_declaration(sc, type)) {
  175. /* but it has to be: */
  176. extern char *symbol2str();
  177. error("unknown %s-type",
  178. symbol2str(type->tp_fund));
  179. }
  180. }
  181. }
  182. /* some additional work for formal definitions */
  183. if (lvl == L_FORMAL2) {
  184. switch (type->tp_fund) {
  185. case FUNCTION:
  186. warning("%s is a function; cannot be formal",
  187. idf->id_text);
  188. type = construct_type(POINTER, type, (arith)0);
  189. break;
  190. case ARRAY: /* RM 10.1 */
  191. type = construct_type(POINTER, type->tp_up, (arith)0);
  192. formal_array = 1;
  193. break;
  194. case FLOAT: /* RM 10.1 */
  195. type = double_type;
  196. break;
  197. case CHAR:
  198. case SHORT:
  199. /* The RM is not clear about this: we must
  200. convert the parameter from int (they have
  201. been pushed as ints) to the specified type.
  202. The conversion to type int or uint is not
  203. allowed.
  204. */
  205. break;
  206. }
  207. }
  208. /* The tests on types, postponed from do_decspecs(), can now
  209. be performed.
  210. */
  211. /* update the storage class */
  212. if (type && type->tp_fund == FUNCTION) {
  213. if (sc == 0 || (ds->ds_sc_given && sc == AUTO)) /* RM 8.1 */
  214. sc = GLOBAL;
  215. else
  216. if (sc == REGISTER) {
  217. error("function has illegal storage class");
  218. ds->ds_sc = sc = GLOBAL;
  219. }
  220. }
  221. else { /* non-FUNCTION */
  222. if (sc == 0)
  223. sc =
  224. lvl == L_GLOBAL ?
  225. GLOBAL :
  226. lvl == L_FORMAL1 || lvl == L_FORMAL2 ?
  227. FORMAL :
  228. AUTO;
  229. }
  230. if (options['R']) {
  231. /* some special K & R tests */
  232. /* is it also an enum? */
  233. if (idf->id_enum && idf->id_enum->tg_level == level)
  234. warning("%s is also an enum tag", idf->id_text);
  235. /* is it a universal typedef? */
  236. if (def && def->df_level == L_UNIVERSAL)
  237. warning("redeclaring reserved word %s", idf->id_text);
  238. }
  239. if (def && def->df_level >= lvl) {
  240. /* There is already a declaration for idf on this
  241. level, or even more inside.
  242. The rules differ for different levels.
  243. */
  244. switch (lvl) {
  245. case L_GLOBAL:
  246. global_redecl(idf, sc, type);
  247. break;
  248. case L_FORMAL1: /* formal declaration */
  249. error("formal %s redeclared", idf->id_text);
  250. break;
  251. case L_FORMAL2: /* formal definition */
  252. default: /* local */
  253. error("%s redeclared", idf->id_text);
  254. break;
  255. }
  256. }
  257. else /* the idf is unknown on this level */
  258. if (lvl == L_FORMAL2 && sc != ENUM && good_formal(def, idf)) {
  259. /* formal declaration, update only */
  260. def->df_type = type;
  261. def->df_formal_array = formal_array;
  262. def->df_sc = sc;
  263. if (def->df_sc != FORMAL)
  264. crash("non-formal formal");
  265. def->df_register = (sc == REGISTER) ? REG_BONUS : REG_DEFAULT;
  266. }
  267. else
  268. if ( lvl >= L_LOCAL &&
  269. (type->tp_fund == FUNCTION || sc == EXTERN)
  270. ) {
  271. /* extern declaration inside function is treated the
  272. same way as global extern declaration
  273. */
  274. if ( options['R'] &&
  275. (sc == STATIC && type->tp_fund == FUNCTION)
  276. ) {
  277. if (!is_anon_idf(idf))
  278. warning("non-global static function %s",
  279. idf->id_text);
  280. }
  281. declare_idf(ds, dc, L_GLOBAL);
  282. }
  283. else {
  284. /* fill in the def block */
  285. register struct def *newdef = new_def();
  286. clear((char *)newdef, sizeof(struct def));
  287. newdef->next = def;
  288. newdef->df_level = lvl;
  289. newdef->df_type = type;
  290. newdef->df_sc = sc;
  291. /* link it into the name list in the proper place */
  292. idf->id_def = newdef;
  293. update_ahead(idf);
  294. stack_idf(idf, stl);
  295. /* We now calculate the address.
  296. Globals have names and don't get addresses, they
  297. get numbers instead (through data_label()).
  298. Formals are handled by declare_formals().
  299. So here we hand out local addresses only.
  300. */
  301. if (lvl >= L_LOCAL) {
  302. switch (sc) {
  303. case 0:
  304. crash("local sc == 0");
  305. break;
  306. case REGISTER:
  307. case AUTO:
  308. if (type->tp_size == (arith)-1) {
  309. error("size of local \"%s\" unknown",
  310. idf->id_text);
  311. type = idf->id_def->df_type = int_type;
  312. }
  313. idf->id_def->df_register =
  314. (sc == REGISTER)
  315. ? REG_BONUS : REG_DEFAULT;
  316. idf->id_def->df_address =
  317. stl->sl_max_block =
  318. stl->sl_local_offset =
  319. -align(-stl->sl_local_offset +
  320. type->tp_size, type->tp_align);
  321. break;
  322. case STATIC:
  323. idf->id_def->df_address = (arith) data_label();
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. actual_declaration(sc, tp)
  330. struct type *tp;
  331. {
  332. /* An actual_declaration needs space, right here and now.
  333. */
  334. register int fund = tp->tp_fund;
  335. /* virtual declarations */
  336. if (sc == ENUM || sc == TYPEDEF)
  337. return 0;
  338. /* allocation solved in other ways */
  339. if (fund == FUNCTION || fund == ARRAY)
  340. return 0;
  341. /* to be allocated */
  342. return 1;
  343. }
  344. global_redecl(idf, new_sc, tp)
  345. struct idf *idf;
  346. struct type *tp;
  347. {
  348. /* A global identifier may be declared several times,
  349. provided the declarations do not conflict; they might
  350. conflict in type (or supplement each other in the case of
  351. an array) or they might conflict or supplement each other
  352. in storage class.
  353. */
  354. register struct def *def = idf->id_def;
  355. if (tp != def->df_type) {
  356. struct type *otp = def->df_type;
  357. if ( tp->tp_fund != ARRAY || otp->tp_fund != ARRAY ||
  358. tp->tp_up != otp->tp_up
  359. ) {
  360. error("redeclaration of %s with different type",
  361. idf->id_text);
  362. return;
  363. }
  364. /* Multiple array declaration; this may be interesting */
  365. if (tp->tp_size < 0) { /* new decl has [] */
  366. /* nothing new */
  367. }
  368. else
  369. if (otp->tp_size < 0) { /* old decl has [] */
  370. def->df_type = tp;
  371. }
  372. else
  373. if (tp->tp_size != otp->tp_size)
  374. error("inconsistent size in redeclaration of array %s",
  375. idf->id_text);
  376. }
  377. /* Now we may be able to update the storage class. */
  378. /* Clean out this mess as soon as we know all the possibilities
  379. for new_sc.
  380. For now we have:
  381. EXTERN: we have seen the word "extern"
  382. GLOBAL: the item was declared on the outer
  383. level, without either "extern" or
  384. "static".
  385. STATIC: we have seen the word "static"
  386. IMPLICIT: function declaration inferred from
  387. call
  388. */
  389. if (new_sc == IMPLICIT)
  390. return; /* no new information */
  391. switch (def->df_sc) { /* the old storage class */
  392. case EXTERN:
  393. switch (new_sc) { /* the new storage class */
  394. case EXTERN:
  395. case GLOBAL:
  396. break;
  397. case STATIC:
  398. if (def->df_initialized) {
  399. error("cannot redeclare %s to static",
  400. idf->id_text);
  401. }
  402. else {
  403. warning("%s redeclared to static",
  404. idf->id_text);
  405. def->df_sc = STATIC;
  406. }
  407. def->df_sc = new_sc;
  408. break;
  409. default:
  410. crash("bad storage class");
  411. break;
  412. }
  413. break;
  414. case GLOBAL:
  415. switch (new_sc) { /* the new storage class */
  416. case EXTERN:
  417. def->df_sc = EXTERN;
  418. break;
  419. case GLOBAL:
  420. break;
  421. case STATIC:
  422. if (def->df_initialized) {
  423. error("cannot redeclare %s to static",
  424. idf->id_text);
  425. }
  426. else {
  427. if (options['R'])
  428. warning("%s redeclared to static",
  429. idf->id_text);
  430. def->df_sc = STATIC;
  431. }
  432. break;
  433. default:
  434. crash("bad storage class");
  435. break;
  436. }
  437. break;
  438. case STATIC:
  439. switch (new_sc) { /* the new storage class */
  440. case EXTERN:
  441. if (def->df_initialized) {
  442. error("cannot redeclare %s to extern",
  443. idf->id_text);
  444. }
  445. else {
  446. warning("%s redeclared to extern",
  447. idf->id_text);
  448. def->df_sc = EXTERN;
  449. }
  450. break;
  451. case GLOBAL:
  452. case STATIC:
  453. if (def->df_type->tp_fund != FUNCTION)
  454. warning("%s was already static",
  455. idf->id_text);
  456. break;
  457. default:
  458. crash("bad storage class");
  459. break;
  460. }
  461. break;
  462. case IMPLICIT:
  463. switch (new_sc) { /* the new storage class */
  464. case EXTERN:
  465. case GLOBAL:
  466. def->df_sc = new_sc;
  467. break;
  468. case STATIC:
  469. if (options['R'])
  470. warning("%s was implicitly declared as extern",
  471. idf->id_text);
  472. def->df_sc = new_sc;
  473. break;
  474. default:
  475. crash("bad storage class");
  476. break;
  477. }
  478. break;
  479. case ENUM:
  480. case TYPEDEF:
  481. error("illegal redeclaration of %s", idf->id_text);
  482. break;
  483. default:
  484. crash("bad storage class");
  485. break;
  486. }
  487. }
  488. int
  489. good_formal(def, idf)
  490. register struct def *def;
  491. struct idf *idf;
  492. {
  493. /* Succeeds if def is a proper L_FORMAL1 definition and
  494. gives an error message otherwise.
  495. */
  496. if (!def || def->df_level != L_FORMAL1) {
  497. /* not in parameter list */
  498. if (!is_anon_idf(idf))
  499. error("%s not in parameter list",
  500. idf->id_text);
  501. return 0;
  502. }
  503. return 1;
  504. }
  505. declare_params(dc)
  506. struct declarator *dc;
  507. {
  508. /* Declares the formal parameters if they exist.
  509. */
  510. register struct idstack_item *is = dc->dc_fparams;
  511. while (is) {
  512. declare_parameter(is->is_idf);
  513. is = is->next;
  514. }
  515. del_idfstack(dc->dc_fparams);
  516. dc->dc_fparams = 0;
  517. }
  518. init_idf(idf)
  519. struct idf *idf;
  520. {
  521. /* The topmost definition of idf is set to initialized.
  522. */
  523. register struct def *def = idf->id_def; /* the topmost */
  524. if (def->df_initialized)
  525. error("multiple initialization of %s", idf->id_text);
  526. if (def->df_sc == TYPEDEF) {
  527. warning("typedef cannot be initialized");
  528. def->df_sc == EXTERN; /* ??? *//* What else ? */
  529. }
  530. def->df_initialized = 1;
  531. }
  532. declare_parameter(idf)
  533. struct idf *idf;
  534. {
  535. /* idf is declared as a formal.
  536. */
  537. add_def(idf, FORMAL, (struct type *)0, level);
  538. }
  539. declare_enum(tp, idf, l)
  540. struct type *tp;
  541. struct idf *idf;
  542. arith l;
  543. {
  544. /* idf is declared as an enum constant with value l.
  545. */
  546. add_def(idf, ENUM, tp, level);
  547. idf->id_def->df_address = l;
  548. }
  549. declare_formals(fp)
  550. arith *fp;
  551. {
  552. /* Declares those formals as int that haven't been declared
  553. by the user.
  554. An address is assigned to each formal parameter.
  555. The total size of the formals is returned in *fp;
  556. */
  557. struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
  558. arith f_offset = (arith)0;
  559. #ifdef DEBUG
  560. if (options['t'])
  561. dumpidftab("start declare_formals", 0);
  562. #endif DEBUG
  563. while (se) {
  564. struct idf *idf = se->se_idf;
  565. struct def *def = idf->id_def;
  566. if (def->df_type == 0)
  567. def->df_type = int_type; /* default type */
  568. def->df_address = f_offset;
  569. /* the alignment convention for parameters is: align on
  570. word boundaries, i.e. take care that the following
  571. parameter starts on a new word boundary.
  572. */
  573. f_offset = align(f_offset + def->df_type->tp_size,
  574. word_align);
  575. /* the following is absurd: any char or short formal
  576. must be converted from integer to that type
  577. */
  578. formal_cvt(def);
  579. se = se->next;
  580. }
  581. *fp = f_offset;
  582. }
  583. add_def(idf, sc, tp, lvl)
  584. struct idf *idf;
  585. struct type *tp;
  586. int lvl;
  587. int sc;
  588. {
  589. /* The identifier idf is declared on level lvl with storage
  590. class sc and type tp, through a faked C declaration.
  591. This is probably the wrong way to structure the problem,
  592. but it will have to do for the time being.
  593. */
  594. struct decspecs Ds; struct declarator Dc;
  595. Ds = null_decspecs;
  596. Ds.ds_type = tp;
  597. Ds.ds_sc = sc;
  598. Dc = null_declarator;
  599. Dc.dc_idf = idf;
  600. declare_idf(&Ds, &Dc, lvl);
  601. }
  602. update_ahead(idf)
  603. register struct idf *idf;
  604. {
  605. /* The tk_symb of the token ahead is updated in the light of new
  606. information about the identifier idf.
  607. */
  608. register int tk_symb = AHEAD;
  609. if ( (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
  610. ahead.tk_idf == idf
  611. )
  612. AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
  613. TYPE_IDENTIFIER : IDENTIFIER;
  614. }
  615. del_idfstack(is)
  616. struct idstack_item *is;
  617. {
  618. while (is) {
  619. register struct idstack_item *tmp = is->next;
  620. free_idstack_item(is);
  621. is = tmp;
  622. }
  623. }
  624. char hmask[IDFSIZE];
  625. init_hmask() {
  626. /* A simple congruence random number generator, as
  627. described in Knuth, vol 2.
  628. */
  629. int h, rnd = HASH_X;
  630. for (h = 0; h < IDFSIZE; h++) {
  631. hmask[h] = rnd;
  632. rnd = (HASH_A * rnd + HASH_C) & HASHMASK;
  633. }
  634. }