idf.c 16 KB

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