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 3.30 1994/06/24 12:04:18 ceriel Exp $ */
6 /* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
19 #include "nobitfield.h"
34 #include "noRoption.h"
37 #define con_nullbyte() C_con_ucon("0", (arith)1)
38 #define aggregate_type(tp) ((tp)->tp_fund == ARRAY || (tp)->tp_fund == STRUCT)
42 extern char options[];
44 static int pack_level;
45 struct type **gen_tphead(), **gen_tpmiddle();
46 struct sdef *gen_align_to_next();
47 struct e_stack *p_stack;
50 /* initial_value recursively guides the initialisation expression.
51 Upto now, the initialisation of a union is not allowed!
54 initial_value(register struct type **tpp; register struct expr **expp;) :
55 { if (tpp) gen_tpcheck(tpp, 0); }
57 assignment_expression(expp)
60 lint_expr(*expp, USED);
62 if ((*expp)->ex_type->tp_fund == ARRAY)
65 gen_simple_exp(tpp, expp);
66 free_expression(*expp);
71 initial_value_pack(tpp, expp)
75 initial_value_pack(struct type **tpp; struct expr **expp;)
78 { if (pack_level == 0) gen_error = 0; pack_level++; }
79 initial_value_list(tpp, expp)
83 struct e_stack *p = p_stack->next;
85 free_e_stack(p_stack);
89 if (pack_level < gen_error) gen_error = 0;
94 initial_value_list(register struct type **tpp; struct expr **expp;)
96 register struct type **tpp2 = 0;
97 int err_flag = gen_error;
100 { if (tpp) tpp2 = gen_tphead(tpp, 0); }
101 initial_value(tpp2, &e1)
102 { if (!tpp) init_expression(&expp, e1); }
103 [%while (AHEAD != '}') /* >>> conflict on ',' */
105 { if (tpp) tpp2 = gen_tpmiddle(); }
106 initial_value(tpp2, &e1)
107 { if (!tpp) init_expression(&expp, e1); }
109 { if (tpp && ! err_flag) gen_tpend(); }
110 ','? /* optional trailing comma */
114 gen_tpcheck(tpp, union_allowed)
117 register struct type *tp;
119 if (gen_error) return;
120 switch((tp = *tpp)->tp_fund) {
122 if (! valid_type(tp->tp_up, "array element"))
123 gen_error = pack_level;
126 if (! valid_type(tp, "struct"))
127 gen_error = pack_level;
130 if (! union_allowed) {
131 error("union initialization not allowed");
132 gen_error = pack_level;
135 if (! valid_type(tp, "union"))
136 gen_error = pack_level;
139 if (! gen_error) gen_error = pack_level;
144 gen_simple_exp(tpp, expp)
148 register struct type *tp;
150 if (gen_error) return;
152 switch(tp->tp_fund) {
154 if ((*expp)->ex_class == String && tp->tp_up->tp_fund == CHAR) {
160 check_and_pad(expp, tpp);
163 gen_error = pack_level;
166 check_ival(expp, tp);
176 register struct type *tp = *tpp;
178 if (tp->tp_up->tp_fund == CHAR && AHEAD == STRING && p->elem_count == 1) {
182 if (AHEAD == '{' || ! aggregate_type(tp->tp_up))
184 return gen_tphead(&(tp->tp_up), 1);
189 register struct sdef *sd;
190 register struct e_stack *p;
193 p->bytes_upto_here += zero_bytes(sd);
194 p->bytes_upto_here +=
195 size_of_type(sd->sd_type, "selector");
196 p->last_offset = sd->sd_offset;
201 gen_tphead(tpp, nest)
204 register struct type *tp = *tpp;
205 register struct e_stack *p;
206 register struct sdef *sd;
208 if (tpp && *tpp == error_type) {
209 gen_error = pack_level;
212 if (gen_error) return tpp;
218 switch(tp->tp_fund) {
222 if (tp->tp_size != (arith) -1) {
223 p->nelem = (tp->tp_size / tp->tp_up->tp_size);
225 return arr_elem(tpp, p);
227 p->s_def = sd = tp->tp_sdef;
228 p->bytes_upto_here = 0;
231 while (sd && is_anon_idf(sd->sd_idf)) {
232 put_bf(sd->sd_type, (arith) 0);
233 sd = next_field(sd, p);
237 /* something wrong with this struct */
238 gen_error = pack_level;
244 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
245 return gen_tphead(&(sd->sd_type), 1);
247 return &(sd->sd_type);
249 if (! gen_error) gen_error = pack_level;
261 register struct type *tp;
262 register struct sdef *sd;
263 register struct e_stack *p = p_stack;
266 if (p) return p->s_tpp;
271 switch(tp->tp_fund) {
273 if (! gen_error) gen_error = pack_level;
276 if (p->elem_count == p->nelem && p->s_nested) {
278 free_e_stack(p_stack);
283 if (p->nelem >= 0 && p->elem_count > p->nelem) {
284 too_many_initialisers();
287 if (tp->tp_fund == ARRAY) {
288 return arr_elem(p->s_tpp, p);
292 sd = gen_align_to_next(p);
294 while (p->bytes_upto_here++ < tp->tp_size)
298 free_e_stack(p_stack);
302 too_many_initialisers();
305 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
306 return gen_tphead(&(sd->sd_type), 1);
308 return &(sd->sd_type);
314 register struct e_stack *p;
316 register struct sdef *sd = p->s_def;
321 if (is_anon_idf(sd->sd_idf)) put_bf(sd->sd_type, (arith) 0);
323 sd = next_field(sd, p);
325 } while (sd && is_anon_idf(sd->sd_idf));
333 register struct e_stack *p = p_stack;
334 register struct type *tp;
335 register struct sdef *sd;
338 while (!getout && p) {
341 switch(tp->tp_fund) {
343 if (tp->tp_size == -1) {
344 *(p->s_tpp) = construct_type(ARRAY,
345 tp->tp_up, p->elem_count);
348 while (p->nelem-- > p->elem_count) {
354 sd = gen_align_to_next(p);
358 p->bytes_upto_here += zero_bytes(sd);
359 p->bytes_upto_here +=
360 size_of_type(sd->sd_type, "selector");
363 while (p->bytes_upto_here++ < tp->tp_size)
368 if (! p->s_nested) getout = 1;
370 free_e_stack(p_stack);
375 /* check_and_pad() is given a simple initialisation expression
376 where the type can be either a simple or an aggregate type.
377 In the latter case, only the first member is initialised and
380 check_and_pad(expp, tpp)
384 register struct type *tp = *tpp;
386 if (tp->tp_fund == ARRAY) {
387 check_and_pad(expp, &(tp->tp_up)); /* first member */
388 if (tp->tp_size == (arith)-1)
389 /* no size specified upto here: just
390 set it to the size of one member.
392 tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)1);
394 register int dim = tp->tp_size / tp->tp_up->tp_size;
395 /* pad remaining members with zeroes */
401 if (tp->tp_fund == STRUCT) {
402 register struct sdef *sd = tp->tp_sdef;
404 check_and_pad(expp, &(sd->sd_type));
405 /* next selector is aligned by adding extra zeroes */
408 while (sd = sd->sd_sdef) { /* pad remaining selectors */
414 else /* simple type */
415 check_ival(expp, tp);
418 /* pad() fills an element of type tp with zeroes.
419 If the element is an aggregate, pad() is called recursively.
424 register struct type *tp = tpx;
425 register arith sz = tp->tp_size;
427 gen_tpcheck(&tpx, 1);
428 if (gen_error) return;
429 switch (tp->tp_fund) {
433 warning("initialization of unions not allowed");
439 put_bf(tp, (arith)0);
441 #endif /* NOBITFIELD */
446 if (tp->tp_align >= word_align) while (sz >= word_size) {
447 C_con_cst((arith) 0);
451 C_con_icon("0", (arith) 1);
456 /* check_ival() checks whether the initialisation of an element
457 of a fundamental type is legal and, if so, performs the initialisation
458 by directly generating the necessary code.
459 No further comment is needed to explain the internal structure
460 of this straightforward function.
463 register struct type *tp;
466 /* The philosophy here is that ch7cast puts an explicit
467 conversion node in front of the expression if the types
468 are not compatible. In this case, the initialisation
469 expression is no longer a constant.
471 register struct expr *expr = *expp;
473 switch (tp->tp_fund) {
480 ch7cast(expp, '=', tp);
483 print_expr("init-expr after cast", expr);
485 if (!is_ld_cst(expr))
486 illegal_init_cst(expr);
488 if (expr->VL_CLASS == Const)
491 if (expr->VL_CLASS == Name) {
492 register struct idf *idf = expr->VL_IDF;
494 if (idf->id_def->df_level >= L_LOCAL)
495 illegal_init_cst(expr);
496 else /* e.g., int f(); int p = f; */
497 if (idf->id_def->df_type->tp_fund == FUNCTION)
498 C_con_pnam(idf->id_text);
499 else /* e.g., int a; int *p = &a; */
500 C_con_dnam(idf->id_text, expr->VL_VALUE);
503 ASSERT(expr->VL_CLASS == Label);
504 C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
510 ch7cast(expp, '=', tp);
513 print_expr("init-expr after cast", expr);
515 if (expr->ex_class == Float)
516 C_con_fcon(expr->FL_VALUE, expr->ex_type->tp_size);
519 Coercion from int to float is now always done compile time.
520 This, to accept declarations like
521 double x = -(double)1;
522 and also to prevent runtime coercions for compile-time constants.
525 if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
527 expr = expr->OP_RIGHT;
529 C_con_fcon(long2str((long)expr->VL_VALUE, 10),
532 illegal_init_cst(expr);
536 illegal_init_cst(expr);
542 ch7cast(expp, '=', tp->tp_up);
545 print_expr("init-expr after cast", expr);
548 put_bf(tp, expr->VL_VALUE);
550 illegal_init_cst(expr);
552 #endif /* NOBITFIELD */
555 if (! gen_error) gen_error = pack_level;
563 /* ch_array() initialises an array of characters when given
565 Alignment is taken care of.
568 struct type **tpp; /* type tp = array of characters */
571 register struct type *tp = *tpp;
572 register int length = ex->SG_LEN, i;
573 register char *to, *from, *s;
575 ASSERT(ex->ex_class == String);
576 if (tp->tp_size == (arith)-1) {
577 /* set the dimension */
578 tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)length);
581 arith dim = tp->tp_size / tp->tp_up->tp_size;
584 expr_warning(ex, "too many initializers");
588 /* throw out the characters of the already prepared string */
589 s = Malloc((unsigned) (length));
591 i = length <= ex->SG_LEN ? length : ex->SG_LEN;
592 to = s; from = ex->SG_VALUE;
601 /* As long as some parts of the pipeline cannot handle very long string
602 constants, string constants are written out in chunks
608 int chunksize = ((127 + (int) word_size) / (int) word_size) * (int) word_size;
610 while (len > chunksize) {
611 C_con_scon(str, (arith) chunksize);
615 C_con_scon(str, (arith) len);
619 /* put_bf() takes care of the initialisation of (bit-)field
620 selectors of a struct: each time such an initialisation takes place,
621 put_bf() is called instead of the normal code generating routines.
622 Put_bf() stores the given integral value into "field" and
623 "throws" the result of "field" out if the current selector
624 is the last of this number of fields stored at the same address.
630 static long field = (arith)0;
631 static arith offset = (arith)-1;
632 register struct field *fd = tp->tp_field;
633 register struct sdef *sd = fd->fd_sdef;
634 static struct expr exp;
637 if (offset == (arith)-1) {
638 /* first bitfield in this field */
639 offset = sd->sd_offset;
640 exp.ex_type = tp->tp_up;
641 exp.ex_class = Value;
642 exp.VL_CLASS = Const;
644 if (val != 0) /* insert the value into "field" */
645 field |= (val & fd->fd_mask) << fd->fd_shift;
646 if (sd->sd_sdef == 0 || sd->sd_sdef->sd_offset != offset) {
647 /* the selector was the last stored at this address */
648 exp.VL_VALUE = field;
654 #endif /* NOBITFIELD */
658 register struct sdef *sd;
660 /* fills the space between a selector of a struct
661 and the next selector of that struct with zero-bytes.
663 register int n = sd->sd_sdef->sd_offset - sd->sd_offset -
664 size_of_type(sd->sd_type, "struct member");
665 register int count = n;
677 if (tp->tp_size < 0) {
678 error("size of %s unknown", str);
685 register struct expr *ex;
687 register struct type *tp = ex->ex_type;
689 ASSERT(is_cp_cst(ex));
691 C_con_ucon(long2str((long)ex->VL_VALUE, -10), tp->tp_size);
692 else if (tp->tp_size == word_size)
693 C_con_cst(ex->VL_VALUE);
695 C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
701 expr_error(ex, "illegal initialization constant");
702 gen_error = pack_level;
705 too_many_initialisers()
707 error("too many initializers");
708 gen_error = pack_level;