idf.c 17 KB

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