idf.c 17 KB

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