ival.c 15 KB

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