ival.c 18 KB

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