ival.c 15 KB

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