ival.g 14 KB

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