ival.c 16 KB

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