ival.c 15 KB

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