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".
5 /* $Id: ival.g,v 1.24 2001/07/03 10:10:56 ceriel Exp $ */
6 /* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
19 #include "nobitfield.h"
20 #include <flt_arith.h>
38 #define con_nullbyte() C_con_ucon("0", (arith)1)
39 #define aggregate_type(tp) ((tp)->tp_fund == ARRAY || (tp)->tp_fund == STRUCT)
43 extern char options[];
45 static int pack_level;
46 struct type **gen_tphead(), **gen_tpmiddle();
47 struct sdef *gen_align_to_next();
48 struct e_stack *p_stack;
51 /* initial_value recursively guides the initialisation expression.
55 initial_value(register struct type **tpp; register struct expr **expp;) :
56 { if (tpp) gen_tpcheck(tpp); }
58 { if (pack_level == 0) gen_error = 0; }
59 assignment_expression(expp)
62 lint_expr(*expp, USED);
64 if ((*expp)->ex_type->tp_fund == ARRAY)
70 || (*expp)->ex_class == String) {
71 gen_simple_exp(tpp, expp);
72 free_expression(*expp);
75 expr_error(*expp,"illegal initialization");
76 free_expression(*expp);
82 initial_value_pack(tpp, expp)
86 initial_value_pack(struct type **tpp; struct expr **expp;)
89 { if (pack_level == 0) gen_error = 0; pack_level++; }
90 initial_value_list(tpp, expp)
94 struct e_stack *p = p_stack->next;
96 free_e_stack(p_stack);
100 if (pack_level < gen_error) gen_error = 0;
105 initial_value_list(register struct type **tpp; struct expr **expp;)
107 register struct type **tpp2 = 0;
108 int err_flag = gen_error;
111 { if (tpp) tpp2 = gen_tphead(tpp, 0); }
112 initial_value(tpp2, &e1)
113 { if (!tpp) init_expression(&expp, e1); }
114 [%while (AHEAD != '}') /* >>> conflict on ',' */
116 { if (tpp) tpp2 = gen_tpmiddle(); }
117 initial_value(tpp2, &e1)
118 { if (!tpp) init_expression(&expp, e1); }
120 { if (tpp && ! err_flag) gen_tpend(); }
121 ','? /* optional trailing comma */
128 register struct type *tp;
130 if (gen_error) return;
131 switch((tp = *tpp)->tp_fund) {
133 if (! valid_type(tp->tp_up, "array element"))
134 gen_error = pack_level;
137 if (! valid_type(tp, "struct"))
138 gen_error = pack_level;
141 if (! valid_type(tp, "union"))
142 gen_error = pack_level;
145 if (! gen_error) gen_error = pack_level;
150 gen_simple_exp(tpp, expp)
154 register struct type *tp;
156 if (gen_error) return;
158 switch(tp->tp_fund) {
160 if ((*expp)->ex_class == String && tp->tp_up->tp_fund == CHAR) {
167 check_and_pad(expp, tpp);
172 gen_error = pack_level;
175 check_ival(expp, tp);
185 register struct type *tp = *tpp;
187 if (tp->tp_up->tp_fund == CHAR && AHEAD == STRING && p->elem_count == 1) {
191 if (AHEAD == '{' || (! aggregate_type(tp->tp_up) && tp->tp_up->tp_fund != UNION))
193 return gen_tphead(&(tp->tp_up), 1);
198 register struct sdef *sd;
199 register struct e_stack *p;
202 p->bytes_upto_here += zero_bytes(sd);
203 p->bytes_upto_here +=
204 size_of_type(sd->sd_type, "selector");
205 p->last_offset = sd->sd_offset;
210 gen_tphead(tpp, nest)
213 register struct type *tp = *tpp;
214 register struct e_stack *p;
215 register struct sdef *sd;
217 if (tpp && *tpp == error_type) {
218 gen_error = pack_level;
221 if (gen_error) return tpp;
222 if (tp->tp_fund == UNION) {
223 /* Here, we saw a {, which could be the start of a union
224 initializer. It could, however, also be the start of the
225 initializer for the first union field ...
229 (aggregate_type(sd->sd_type) ||
230 sd->sd_type->tp_fund == UNION)) {
231 /* In this case, assume that it is the start of the
232 initializer of the union field, so:
234 return gen_tphead(&(tp->tp_sdef->sd_type), nest);
242 switch(tp->tp_fund) {
244 p->s_def = sd = tp->tp_sdef;
245 p->bytes_upto_here = 0;
246 return &(sd->sd_type);
250 if (tp->tp_size != (arith) -1) {
251 p->nelem = (tp->tp_size / tp->tp_up->tp_size);
253 return arr_elem(tpp, p);
255 p->s_def = sd = tp->tp_sdef;
256 p->bytes_upto_here = 0;
259 while (sd && is_anon_idf(sd->sd_idf)) {
260 put_bf(sd->sd_type, (arith) 0);
261 sd = next_field(sd, p);
265 /* something wrong with this struct */
266 gen_error = pack_level;
272 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
273 return gen_tphead(&(sd->sd_type), 1);
275 return &(sd->sd_type);
277 if (! gen_error) gen_error = pack_level;
289 register struct type *tp;
290 register struct sdef *sd;
291 register struct e_stack *p = p_stack;
294 if (p) return p->s_tpp;
299 switch(tp->tp_fund) {
301 if (! gen_error) gen_error = pack_level;
305 p->bytes_upto_here +=
306 size_of_type(sd->sd_type, "selector");
309 if (p->elem_count == p->nelem && p->s_nested) {
311 free_e_stack(p_stack);
316 if (p->nelem >= 0 && p->elem_count > p->nelem) {
317 too_many_initialisers();
320 if (tp->tp_fund == ARRAY) {
321 return arr_elem(p->s_tpp, p);
325 sd = gen_align_to_next(p);
327 while (p->bytes_upto_here++ < tp->tp_size)
331 free_e_stack(p_stack);
335 too_many_initialisers();
338 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
339 return gen_tphead(&(sd->sd_type), 1);
341 return &(sd->sd_type);
347 register struct e_stack *p;
349 register struct sdef *sd = p->s_def;
354 if (is_anon_idf(sd->sd_idf)) put_bf(sd->sd_type, (arith) 0);
356 sd = next_field(sd, p);
358 } while (sd && is_anon_idf(sd->sd_idf));
366 register struct e_stack *p = p_stack;
367 register struct type *tp;
368 register struct sdef *sd;
371 while (!getout && p) {
374 switch(tp->tp_fund) {
377 p->bytes_upto_here +=
378 size_of_type(sd->sd_type, "selector");
379 while (p->bytes_upto_here++ < tp->tp_size)
383 if (tp->tp_size == -1) {
384 *(p->s_tpp) = construct_type(ARRAY, tp->tp_up,
385 0, p->elem_count, NO_PROTO);
388 while (p->nelem-- > p->elem_count) {
394 sd = gen_align_to_next(p);
398 p->bytes_upto_here += zero_bytes(sd);
399 p->bytes_upto_here +=
400 size_of_type(sd->sd_type, "selector");
403 while (p->bytes_upto_here++ < tp->tp_size)
408 if (! p->s_nested) getout = 1;
410 free_e_stack(p_stack);
415 /* check_and_pad() is given a simple initialisation expression
416 where the type can be either a simple or an aggregate type.
417 In the latter case, only the first member is initialised and
420 check_and_pad(expp, tpp)
424 register struct type *tp = *tpp;
426 if (tp->tp_fund == ARRAY) {
427 check_and_pad(expp, &(tp->tp_up)); /* first member */
428 if (tp->tp_size == (arith)-1)
429 /* no size specified upto here: just
430 set it to the size of one member.
432 tp = *tpp = construct_type(ARRAY, tp->tp_up,
433 0, (arith)1, NO_PROTO);
435 register int dim = tp->tp_size / tp->tp_up->tp_size;
436 /* pad remaining members with zeroes */
442 if (tp->tp_fund == STRUCT) {
443 register struct sdef *sd = tp->tp_sdef;
445 check_and_pad(expp, &(sd->sd_type));
446 /* next selector is aligned by adding extra zeroes */
449 while (sd = sd->sd_sdef) { /* pad remaining selectors */
455 else if (tp->tp_fund == UNION) {
456 /* only the first selector can be initialized */
457 register struct sdef *sd = tp->tp_sdef;
459 check_and_pad(expp, &(sd->sd_type));
461 else /* simple type */
462 check_ival(expp, tp);
465 /* pad() fills an element of type tp with zeroes.
466 If the element is an aggregate, pad() is called recursively.
471 register struct type *tp = tpx;
472 register arith sz = tp->tp_size;
475 if (gen_error) return;
477 if (tp->tp_fund == FIELD) {
478 put_bf(tp, (arith)0);
481 #endif /* NOBITFIELD */
483 if (tp->tp_align >= word_align) while (sz >= word_size) {
484 C_con_cst((arith) 0);
488 C_con_icon("0", (arith) 1);
493 /* check_ival() checks whether the initialisation of an element
494 of a fundamental type is legal and, if so, performs the initialisation
495 by directly generating the necessary code.
496 No further comment is needed to explain the internal structure
497 of this straightforward function.
500 register struct type *tp;
503 /* The philosophy here is that ch3cast puts an explicit
504 conversion node in front of the expression if the types
505 are not compatible. In this case, the initialisation
506 expression is no longer a constant.
508 register struct expr *expr = *expp;
510 switch (tp->tp_fund) {
517 ch3cast(expp, '=', tp);
520 print_expr("init-expr after cast", expr);
522 if (!is_ld_cst(expr))
523 illegal_init_cst(expr);
525 if (expr->VL_CLASS == Const)
528 if (expr->VL_CLASS == Name) {
529 register struct idf *idf = expr->VL_IDF;
531 if (idf->id_def->df_level >= L_LOCAL
532 && idf->id_def->df_sc != GLOBAL
533 && idf->id_def->df_sc != EXTERN) {
534 illegal_init_cst(expr);
536 else /* e.g., int f(); int p = f; */
537 if (idf->id_def->df_type->tp_fund == FUNCTION)
538 C_con_pnam(idf->id_text);
539 else /* e.g., int a; int *p = &a; */
540 C_con_dnam(idf->id_text, expr->VL_VALUE);
543 ASSERT(expr->VL_CLASS == Label);
544 C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
550 ch3cast(expp, '=', tp);
553 print_expr("init-expr after cast", expr);
555 if (expr->ex_class == Float) {
556 char buf[FLT_STRLEN];
558 flt_flt2str(&(expr->FL_ARITH), buf, FLT_STRLEN);
559 C_con_fcon(buf, expr->ex_type->tp_size);
563 Coercion from int to float is now always done compile time.
564 This, to accept declarations like
565 double x = -(double)1;
566 and also to prevent runtime coercions for compile-time constants.
569 if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
571 expr = expr->OP_RIGHT;
573 C_con_fcon(long2str((long)expr->VL_VALUE, 10),
576 illegal_init_cst(expr);
580 illegal_init_cst(expr);
585 ch3cast(expp, '=', tp->tp_up);
588 print_expr("init-expr after cast", expr);
591 put_bf(tp, expr->VL_VALUE);
593 illegal_init_cst(expr);
595 #endif /* NOBITFIELD */
598 if (! gen_error) gen_error = pack_level;
608 /* ch_array() initialises an array of characters when given
610 Alignment is taken care of.
613 struct type **tpp; /* type tp = array of characters */
616 register struct type *tp = *tpp;
617 register int length = ex->SG_LEN, i;
618 register char *to, *from, *s;
620 ASSERT(ex->ex_class == String);
621 if (tp->tp_size == (arith)-1) {
622 /* set the dimension */
623 tp = *tpp = construct_type(ARRAY, tp->tp_up, 0, (arith)length, NO_PROTO);
626 arith dim = tp->tp_size / tp->tp_up->tp_size;
629 if (length == dim + 1) {
630 expr_warning(ex, "array is not null-terminated");
633 if (length > dim + 1) {
634 expr_strict(ex, "too many initializers");
638 /* throw out the characters of the already prepared string */
639 s = Malloc((unsigned) (length));
640 clear(s, (unsigned)length);
641 i = length <= ex->SG_LEN ? length : ex->SG_LEN;
642 to = s; from = ex->SG_VALUE;
647 str_cst(s, length, 0); /* a string, but not in rom */
651 /* As long as some parts of the pipeline cannot handle very long string
652 constants, string constants are written out in chunks
654 str_cst(str, len, inrom)
659 int chunksize = ((127 + (int) word_size) / (int) word_size) * (int) word_size;
661 while (len > chunksize) {
663 C_rom_scon(str, (arith) chunksize);
664 else C_con_scon(str, (arith) chunksize);
669 C_rom_scon(str, (arith) len);
670 else C_con_scon(str, (arith) len);
674 /* put_bf() takes care of the initialisation of (bit-)field
675 selectors of a struct: each time such an initialisation takes place,
676 put_bf() is called instead of the normal code generating routines.
677 Put_bf() stores the given integral value into "field" and
678 "throws" the result of "field" out if the current selector
679 is the last of this number of fields stored at the same address.
685 static long field = (arith)0;
686 static arith offset = (arith)-1;
687 register struct field *fd = tp->tp_field;
688 register struct sdef *sd = fd->fd_sdef;
689 static struct expr exp;
692 if (offset == (arith)-1) {
693 /* first bitfield in this field */
694 offset = sd->sd_offset;
695 exp.ex_type = tp->tp_up;
696 exp.ex_class = Value;
697 exp.VL_CLASS = Const;
699 if (val != 0) /* insert the value into "field" */
700 field |= (val & fd->fd_mask) << fd->fd_shift;
701 if (sd->sd_sdef == 0 || sd->sd_sdef->sd_offset != offset) {
702 /* the selector was the last stored at this address */
703 exp.VL_VALUE = field;
709 #endif /* NOBITFIELD */
713 register struct sdef *sd;
715 /* fills the space between a selector of a struct
716 and the next selector of that struct with zero-bytes.
718 register int n = sd->sd_sdef->sd_offset - sd->sd_offset -
719 size_of_type(sd->sd_type, "struct member");
732 ASSERT(tp!=(struct type *)0);
733 if (tp->tp_size < 0) {
734 error("size of %s unknown", str);
741 register struct expr *ex;
743 register struct type *tp = ex->ex_type;
745 ASSERT(is_cp_cst(ex));
747 C_con_ucon(long2str((long)ex->VL_VALUE, -10), tp->tp_size);
748 else if (tp->tp_size == word_size)
749 C_con_cst(ex->VL_VALUE);
751 C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
757 expr_error(ex, "illegal initialization constant");
758 gen_error = pack_level;
761 too_many_initialisers()
763 error("too many initializers");
764 gen_error = pack_level;