idf.c 16 KB

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