ival.g 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. /* $Id$ */
  6. /* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
  7. {
  8. #include "lint.h"
  9. #include "nofloat.h"
  10. #ifndef LINT
  11. #include <em.h>
  12. #else
  13. #include "l_em.h"
  14. #include "l_lint.h"
  15. #endif /* LINT */
  16. #include "debug.h"
  17. #include <alloc.h>
  18. #include "nobitfield.h"
  19. #include "arith.h"
  20. #include "label.h"
  21. #include "expr.h"
  22. #include "type.h"
  23. #include "struct.h"
  24. #include "field.h"
  25. #include "assert.h"
  26. #include "Lpars.h"
  27. #include "sizes.h"
  28. #include "align.h"
  29. #include "idf.h"
  30. #include "level.h"
  31. #include "def.h"
  32. #include "LLlex.h"
  33. #include "noRoption.h"
  34. #include "estack.h"
  35. #define con_nullbyte() C_con_ucon("0", (arith)1)
  36. #define aggregate_type(tp) ((tp)->tp_fund == ARRAY || (tp)->tp_fund == STRUCT)
  37. char *long2str();
  38. char *strncpy();
  39. extern char options[];
  40. static int gen_error;
  41. static int pack_level;
  42. struct type **gen_tphead(), **gen_tpmiddle();
  43. struct sdef *gen_align_to_next();
  44. struct e_stack *p_stack;
  45. }
  46. /* initial_value recursively guides the initialisation expression.
  47. Upto now, the initialisation of a union is not allowed!
  48. */
  49. /* 7 */
  50. initial_value(register struct type **tpp; register struct expr **expp;) :
  51. { if (tpp) gen_tpcheck(tpp, 0); }
  52. [
  53. assignment_expression(expp)
  54. {
  55. #ifdef LINT
  56. lint_expr(*expp, USED);
  57. #endif /* LINT */
  58. if ((*expp)->ex_type->tp_fund == ARRAY)
  59. array2pointer(*expp);
  60. if (tpp) {
  61. gen_simple_exp(tpp, expp);
  62. free_expression(*expp);
  63. *expp = 0;
  64. }
  65. }
  66. |
  67. initial_value_pack(tpp, expp)
  68. ]
  69. ;
  70. initial_value_pack(struct type **tpp; struct expr **expp;)
  71. :
  72. '{'
  73. { if (pack_level == 0) gen_error = 0; pack_level++; }
  74. initial_value_list(tpp, expp)
  75. { pack_level--;
  76. if (!pack_level) {
  77. while (p_stack) {
  78. struct e_stack *p = p_stack->next;
  79. free_e_stack(p_stack);
  80. p_stack = p;
  81. }
  82. }
  83. if (pack_level < gen_error) gen_error = 0;
  84. }
  85. '}'
  86. ;
  87. initial_value_list(register struct type **tpp; struct expr **expp;)
  88. { struct expr *e1;
  89. register struct type **tpp2 = 0;
  90. int err_flag = gen_error;
  91. }
  92. :
  93. { if (tpp) tpp2 = gen_tphead(tpp, 0); }
  94. initial_value(tpp2, &e1)
  95. { if (!tpp) init_expression(&expp, e1); }
  96. [%while (AHEAD != '}') /* >>> conflict on ',' */
  97. ','
  98. { if (tpp) tpp2 = gen_tpmiddle(); }
  99. initial_value(tpp2, &e1)
  100. { if (!tpp) init_expression(&expp, e1); }
  101. ]*
  102. { if (tpp && ! err_flag) gen_tpend(); }
  103. ','? /* optional trailing comma */
  104. ;
  105. {
  106. gen_tpcheck(tpp, union_allowed)
  107. struct type **tpp;
  108. {
  109. register struct type *tp;
  110. if (gen_error) return;
  111. switch((tp = *tpp)->tp_fund) {
  112. case ARRAY:
  113. if (! valid_type(tp->tp_up, "array element"))
  114. gen_error = pack_level;
  115. break;
  116. case STRUCT:
  117. if (! valid_type(tp, "struct"))
  118. gen_error = pack_level;
  119. break;
  120. case UNION:
  121. if (! union_allowed) {
  122. error("union initialization not allowed");
  123. gen_error = pack_level;
  124. break;
  125. }
  126. if (! valid_type(tp, "union"))
  127. gen_error = pack_level;
  128. break;
  129. case ERRONEOUS:
  130. if (! gen_error) gen_error = pack_level;
  131. break;
  132. }
  133. }
  134. gen_simple_exp(tpp, expp)
  135. struct type **tpp;
  136. struct expr **expp;
  137. {
  138. register struct type *tp;
  139. if (gen_error) return;
  140. tp = *tpp;
  141. switch(tp->tp_fund) {
  142. case ARRAY:
  143. if ((*expp)->ex_class == String && tp->tp_up->tp_fund == CHAR) {
  144. ch_array(tpp,*expp);
  145. break;
  146. }
  147. /* Fall through */
  148. case STRUCT:
  149. check_and_pad(expp, tpp);
  150. break;
  151. case ERRONEOUS:
  152. gen_error = pack_level;
  153. break;
  154. default:
  155. check_ival(expp, tp);
  156. break;
  157. }
  158. }
  159. struct type **
  160. arr_elem(tpp, p)
  161. struct type **tpp;
  162. struct e_stack *p;
  163. {
  164. register struct type *tp = *tpp;
  165. if (tp->tp_up->tp_fund == CHAR && AHEAD == STRING && p->elem_count == 1) {
  166. p->nelem = 1;
  167. return tpp;
  168. }
  169. if (AHEAD == '{' || ! aggregate_type(tp->tp_up))
  170. return &(tp->tp_up);
  171. return gen_tphead(&(tp->tp_up), 1);
  172. }
  173. struct sdef *
  174. next_field(sd, p)
  175. register struct sdef *sd;
  176. register struct e_stack *p;
  177. {
  178. if (sd->sd_sdef)
  179. p->bytes_upto_here += zero_bytes(sd);
  180. p->bytes_upto_here +=
  181. size_of_type(sd->sd_type, "selector");
  182. p->last_offset = sd->sd_offset;
  183. return sd->sd_sdef;
  184. }
  185. struct type **
  186. gen_tphead(tpp, nest)
  187. struct type **tpp;
  188. {
  189. register struct type *tp = *tpp;
  190. register struct e_stack *p;
  191. register struct sdef *sd;
  192. if (tpp && *tpp == error_type) {
  193. gen_error = pack_level;
  194. return 0;
  195. }
  196. if (gen_error) return tpp;
  197. p = new_e_stack();
  198. p->next = p_stack;
  199. p_stack = p;
  200. p->s_nested = nest;
  201. p->s_tpp = tpp;
  202. switch(tp->tp_fund) {
  203. case ARRAY:
  204. p->nelem = -1;
  205. p->elem_count = 1;
  206. if (tp->tp_size != (arith) -1) {
  207. p->nelem = (tp->tp_size / tp->tp_up->tp_size);
  208. }
  209. return arr_elem(tpp, p);
  210. case STRUCT:
  211. p->s_def = sd = tp->tp_sdef;
  212. p->bytes_upto_here = 0;
  213. p->last_offset = -1;
  214. #ifndef NOBITFIELD
  215. while (sd && is_anon_idf(sd->sd_idf)) {
  216. put_bf(sd->sd_type, (arith) 0);
  217. sd = next_field(sd, p);
  218. }
  219. #endif
  220. if (! sd) {
  221. /* something wrong with this struct */
  222. gen_error = pack_level;
  223. p_stack = p->next;
  224. free_e_stack(p);
  225. return 0;
  226. }
  227. p->s_def = sd;
  228. if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
  229. return gen_tphead(&(sd->sd_type), 1);
  230. }
  231. return &(sd->sd_type);
  232. case ERRONEOUS:
  233. if (! gen_error) gen_error = pack_level;
  234. /* fall through */
  235. default:
  236. p->nelem = 1;
  237. p->elem_count = 1;
  238. return tpp;
  239. }
  240. }
  241. struct type **
  242. gen_tpmiddle()
  243. {
  244. register struct type *tp;
  245. register struct sdef *sd;
  246. register struct e_stack *p = p_stack;
  247. if (gen_error) {
  248. if (p) return p->s_tpp;
  249. return 0;
  250. }
  251. again:
  252. tp = *(p->s_tpp);
  253. switch(tp->tp_fund) {
  254. case ERRONEOUS:
  255. if (! gen_error) gen_error = pack_level;
  256. return p->s_tpp;
  257. default:
  258. if (p->elem_count == p->nelem && p->s_nested) {
  259. p = p->next;
  260. free_e_stack(p_stack);
  261. p_stack = p;
  262. goto again;
  263. }
  264. p->elem_count++;
  265. if (p->nelem >= 0 && p->elem_count > p->nelem) {
  266. too_many_initialisers();
  267. return p->s_tpp;
  268. }
  269. if (tp->tp_fund == ARRAY) {
  270. return arr_elem(p->s_tpp, p);
  271. }
  272. return p->s_tpp;
  273. case STRUCT:
  274. sd = gen_align_to_next(p);
  275. if (! sd) {
  276. while (p->bytes_upto_here++ < tp->tp_size)
  277. con_nullbyte();
  278. if (p->s_nested) {
  279. p = p->next;
  280. free_e_stack(p_stack);
  281. p_stack = p;
  282. goto again;
  283. }
  284. too_many_initialisers();
  285. return p->s_tpp;
  286. }
  287. if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
  288. return gen_tphead(&(sd->sd_type), 1);
  289. }
  290. return &(sd->sd_type);
  291. }
  292. }
  293. struct sdef *
  294. gen_align_to_next(p)
  295. register struct e_stack *p;
  296. {
  297. register struct sdef *sd = p->s_def;
  298. if (! sd) return sd;
  299. #ifndef NOBITFIELD
  300. do {
  301. if (is_anon_idf(sd->sd_idf)) put_bf(sd->sd_type, (arith) 0);
  302. #endif
  303. sd = next_field(sd, p);
  304. #ifndef NOBITFIELD
  305. } while (sd && is_anon_idf(sd->sd_idf));
  306. #endif
  307. p->s_def = sd;
  308. return sd;
  309. }
  310. gen_tpend()
  311. {
  312. register struct e_stack *p = p_stack;
  313. register struct type *tp;
  314. register struct sdef *sd;
  315. int getout = 0;
  316. while (!getout && p) {
  317. if (!gen_error) {
  318. tp = *(p->s_tpp);
  319. switch(tp->tp_fund) {
  320. case ARRAY:
  321. if (tp->tp_size == -1) {
  322. *(p->s_tpp) = construct_type(ARRAY,
  323. tp->tp_up, p->elem_count);
  324. }
  325. else {
  326. while (p->nelem-- > p->elem_count) {
  327. pad(tp->tp_up);
  328. }
  329. }
  330. break;
  331. case STRUCT:
  332. sd = gen_align_to_next(p);
  333. while (sd) {
  334. pad(sd->sd_type);
  335. if (sd->sd_sdef)
  336. p->bytes_upto_here += zero_bytes(sd);
  337. p->bytes_upto_here +=
  338. size_of_type(sd->sd_type, "selector");
  339. sd = sd->sd_sdef;
  340. }
  341. while (p->bytes_upto_here++ < tp->tp_size)
  342. con_nullbyte();
  343. break;
  344. }
  345. }
  346. if (! p->s_nested) getout = 1;
  347. p = p->next;
  348. free_e_stack(p_stack);
  349. p_stack = p;
  350. }
  351. }
  352. /* check_and_pad() is given a simple initialisation expression
  353. where the type can be either a simple or an aggregate type.
  354. In the latter case, only the first member is initialised and
  355. the rest is zeroed.
  356. */
  357. check_and_pad(expp, tpp)
  358. struct type **tpp;
  359. struct expr **expp;
  360. {
  361. register struct type *tp = *tpp;
  362. if (tp->tp_fund == ARRAY) {
  363. check_and_pad(expp, &(tp->tp_up)); /* first member */
  364. if (tp->tp_size == (arith)-1)
  365. /* no size specified upto here: just
  366. set it to the size of one member.
  367. */
  368. tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)1);
  369. else {
  370. register int dim = tp->tp_size / tp->tp_up->tp_size;
  371. /* pad remaining members with zeroes */
  372. while (--dim > 0)
  373. pad(tp->tp_up);
  374. }
  375. }
  376. else
  377. if (tp->tp_fund == STRUCT) {
  378. register struct sdef *sd = tp->tp_sdef;
  379. check_and_pad(expp, &(sd->sd_type));
  380. /* next selector is aligned by adding extra zeroes */
  381. if (sd->sd_sdef)
  382. zero_bytes(sd);
  383. while (sd = sd->sd_sdef) { /* pad remaining selectors */
  384. pad(sd->sd_type);
  385. if (sd->sd_sdef)
  386. zero_bytes(sd);
  387. }
  388. }
  389. else /* simple type */
  390. check_ival(expp, tp);
  391. }
  392. /* pad() fills an element of type tp with zeroes.
  393. If the element is an aggregate, pad() is called recursively.
  394. */
  395. pad(tpx)
  396. struct type *tpx;
  397. {
  398. register struct type *tp = tpx;
  399. register arith sz = tp->tp_size;
  400. gen_tpcheck(&tpx, 1);
  401. if (gen_error) return;
  402. switch (tp->tp_fund) {
  403. case UNION:
  404. #ifndef NOROPTION
  405. if (options['R']) {
  406. warning("initialization of unions not allowed");
  407. }
  408. #endif
  409. break;
  410. #ifndef NOBITFIELD
  411. case FIELD:
  412. put_bf(tp, (arith)0);
  413. return;
  414. #endif /* NOBITFIELD */
  415. default:
  416. break;
  417. }
  418. if (tp->tp_align >= word_align) while (sz >= word_size) {
  419. C_con_cst((arith) 0);
  420. sz -= word_size;
  421. }
  422. while (sz) {
  423. C_con_icon("0", (arith) 1);
  424. sz--;
  425. }
  426. }
  427. /* check_ival() checks whether the initialisation of an element
  428. of a fundamental type is legal and, if so, performs the initialisation
  429. by directly generating the necessary code.
  430. No further comment is needed to explain the internal structure
  431. of this straightforward function.
  432. */
  433. check_ival(expp, tp)
  434. register struct type *tp;
  435. struct expr **expp;
  436. {
  437. /* The philosophy here is that ch7cast puts an explicit
  438. conversion node in front of the expression if the types
  439. are not compatible. In this case, the initialisation
  440. expression is no longer a constant.
  441. */
  442. register struct expr *expr = *expp;
  443. switch (tp->tp_fund) {
  444. case CHAR:
  445. case SHORT:
  446. case INT:
  447. case LONG:
  448. case ENUM:
  449. case POINTER:
  450. ch7cast(expp, '=', tp);
  451. expr = *expp;
  452. #ifdef DEBUG
  453. print_expr("init-expr after cast", expr);
  454. #endif /* DEBUG */
  455. if (!is_ld_cst(expr))
  456. illegal_init_cst(expr);
  457. else
  458. if (expr->VL_CLASS == Const)
  459. con_int(expr);
  460. else
  461. if (expr->VL_CLASS == Name) {
  462. register struct idf *idf = expr->VL_IDF;
  463. if (idf->id_def->df_level >= L_LOCAL)
  464. illegal_init_cst(expr);
  465. else /* e.g., int f(); int p = f; */
  466. if (idf->id_def->df_type->tp_fund == FUNCTION)
  467. C_con_pnam(idf->id_text);
  468. else /* e.g., int a; int *p = &a; */
  469. C_con_dnam(idf->id_text, expr->VL_VALUE);
  470. }
  471. else {
  472. ASSERT(expr->VL_CLASS == Label);
  473. C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
  474. }
  475. break;
  476. #ifndef NOFLOAT
  477. case FLOAT:
  478. case DOUBLE:
  479. ch7cast(expp, '=', tp);
  480. expr = *expp;
  481. #ifdef DEBUG
  482. print_expr("init-expr after cast", expr);
  483. #endif /* DEBUG */
  484. if (expr->ex_class == Float)
  485. C_con_fcon(expr->FL_VALUE, expr->ex_type->tp_size);
  486. #ifdef NOTDEF
  487. Coercion from int to float is now always done compile time.
  488. This, to accept declarations like
  489. double x = -(double)1;
  490. and also to prevent runtime coercions for compile-time constants.
  491. else
  492. if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
  493. /* float f = 1; */
  494. expr = expr->OP_RIGHT;
  495. if (is_cp_cst(expr))
  496. C_con_fcon(long2str((long)expr->VL_VALUE, 10),
  497. tp->tp_size);
  498. else
  499. illegal_init_cst(expr);
  500. }
  501. #endif /* NOTDEF */
  502. else
  503. illegal_init_cst(expr);
  504. break;
  505. #endif /* NOFLOAT */
  506. #ifndef NOBITFIELD
  507. case FIELD:
  508. ch7cast(expp, '=', tp->tp_up);
  509. expr = *expp;
  510. #ifdef DEBUG
  511. print_expr("init-expr after cast", expr);
  512. #endif /* DEBUG */
  513. if (is_cp_cst(expr))
  514. put_bf(tp, expr->VL_VALUE);
  515. else
  516. illegal_init_cst(expr);
  517. break;
  518. #endif /* NOBITFIELD */
  519. case ERRONEOUS:
  520. if (! gen_error) gen_error = pack_level;
  521. break;
  522. default:
  523. crash("check_ival");
  524. /*NOTREACHED*/
  525. }
  526. }
  527. /* ch_array() initialises an array of characters when given
  528. a string constant.
  529. Alignment is taken care of.
  530. */
  531. ch_array(tpp, ex)
  532. struct type **tpp; /* type tp = array of characters */
  533. struct expr *ex;
  534. {
  535. register struct type *tp = *tpp;
  536. register int length = ex->SG_LEN, i;
  537. register char *to, *from, *s;
  538. ASSERT(ex->ex_class == String);
  539. if (tp->tp_size == (arith)-1) {
  540. /* set the dimension */
  541. tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)length);
  542. }
  543. else {
  544. arith dim = tp->tp_size / tp->tp_up->tp_size;
  545. if (length > dim) {
  546. expr_warning(ex, "too many initializers");
  547. }
  548. length = dim;
  549. }
  550. /* throw out the characters of the already prepared string */
  551. s = Malloc((unsigned) (length));
  552. clear(s, length);
  553. i = length <= ex->SG_LEN ? length : ex->SG_LEN;
  554. to = s; from = ex->SG_VALUE;
  555. while(--i >= 0) {
  556. *to++ = *from++;
  557. }
  558. free(ex->SG_VALUE);
  559. str_cst(s, length);
  560. free(s);
  561. }
  562. /* As long as some parts of the pipeline cannot handle very long string
  563. constants, string constants are written out in chunks
  564. */
  565. str_cst(str, len)
  566. register char *str;
  567. register int len;
  568. {
  569. int chunksize = ((127 + (int) word_size) / (int) word_size) * (int) word_size;
  570. while (len > chunksize) {
  571. C_con_scon(str, (arith) chunksize);
  572. len -= chunksize;
  573. str += chunksize;
  574. }
  575. C_con_scon(str, (arith) len);
  576. }
  577. #ifndef NOBITFIELD
  578. /* put_bf() takes care of the initialisation of (bit-)field
  579. selectors of a struct: each time such an initialisation takes place,
  580. put_bf() is called instead of the normal code generating routines.
  581. Put_bf() stores the given integral value into "field" and
  582. "throws" the result of "field" out if the current selector
  583. is the last of this number of fields stored at the same address.
  584. */
  585. put_bf(tp, val)
  586. struct type *tp;
  587. arith val;
  588. {
  589. static long field = (arith)0;
  590. static arith offset = (arith)-1;
  591. register struct field *fd = tp->tp_field;
  592. register struct sdef *sd = fd->fd_sdef;
  593. static struct expr exp;
  594. ASSERT(sd);
  595. if (offset == (arith)-1) {
  596. /* first bitfield in this field */
  597. offset = sd->sd_offset;
  598. exp.ex_type = tp->tp_up;
  599. exp.ex_class = Value;
  600. exp.VL_CLASS = Const;
  601. }
  602. if (val != 0) /* insert the value into "field" */
  603. field |= (val & fd->fd_mask) << fd->fd_shift;
  604. if (sd->sd_sdef == 0 || sd->sd_sdef->sd_offset != offset) {
  605. /* the selector was the last stored at this address */
  606. exp.VL_VALUE = field;
  607. con_int(&exp);
  608. field = (arith)0;
  609. offset = (arith)-1;
  610. }
  611. }
  612. #endif /* NOBITFIELD */
  613. int
  614. zero_bytes(sd)
  615. register struct sdef *sd;
  616. {
  617. /* fills the space between a selector of a struct
  618. and the next selector of that struct with zero-bytes.
  619. */
  620. register int n = sd->sd_sdef->sd_offset - sd->sd_offset -
  621. size_of_type(sd->sd_type, "struct member");
  622. register int count = n;
  623. while (n-- > 0)
  624. con_nullbyte();
  625. return count;
  626. }
  627. int
  628. valid_type(tp, str)
  629. struct type *tp;
  630. char *str;
  631. {
  632. if (tp->tp_size < 0) {
  633. error("size of %s unknown", str);
  634. return 0;
  635. }
  636. return 1;
  637. }
  638. con_int(ex)
  639. register struct expr *ex;
  640. {
  641. register struct type *tp = ex->ex_type;
  642. ASSERT(is_cp_cst(ex));
  643. if (tp->tp_unsigned)
  644. C_con_ucon(long2str((long)ex->VL_VALUE, -10), tp->tp_size);
  645. else if (tp->tp_size == word_size)
  646. C_con_cst(ex->VL_VALUE);
  647. else
  648. C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
  649. }
  650. illegal_init_cst(ex)
  651. struct expr *ex;
  652. {
  653. expr_error(ex, "illegal initialization constant");
  654. gen_error = pack_level;
  655. }
  656. too_many_initialisers()
  657. {
  658. error("too many initializers");
  659. gen_error = pack_level;
  660. }
  661. }