ival.c 15 KB

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