Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / ival.g
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 /* $Id: ival.g,v 1.24 2001/07/03 10:10:56 ceriel Exp $ */
6 /* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
7
8 {
9 #include        "lint.h"
10 #ifndef LINT
11 #include        <em.h>
12 #else
13 #include        "l_em.h"
14 #include        "l_lint.h"
15 #endif  /* LINT */
16 #include        "debug.h"
17 #include        <alloc.h>
18 #include        <assert.h>
19 #include        "nobitfield.h"
20 #include        <flt_arith.h>
21 #include        "arith.h"
22 #include        "label.h"
23 #include        "expr.h"
24 #include        "type.h"
25 #include        "proto.h"
26 #include        "struct.h"
27 #include        "field.h"
28 #include        "assert.h"
29 #include        "Lpars.h"
30 #include        "sizes.h"
31 #include        "align.h"
32 #include        "idf.h"
33 #include        "level.h"
34 #include        "def.h"
35 #include        "LLlex.h"
36 #include        "estack.h"
37
38 #define con_nullbyte()  C_con_ucon("0", (arith)1)
39 #define aggregate_type(tp) ((tp)->tp_fund == ARRAY || (tp)->tp_fund == STRUCT)
40
41 char *long2str();
42 char *strncpy();
43 extern char options[];
44 static int gen_error;
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;
49 }
50
51 /* initial_value recursively guides the initialisation expression.
52  */
53 /* 3.5 */
54
55 initial_value(register struct type **tpp; register struct expr **expp;) :
56         { if (tpp) gen_tpcheck(tpp); }
57 [
58                 { if (pack_level == 0) gen_error = 0; }
59         assignment_expression(expp)
60                 {
61 #ifdef  LINT
62                         lint_expr(*expp, USED);
63 #endif  /* LINT */
64                         if ((*expp)->ex_type->tp_fund == ARRAY)
65                                 array2pointer(*expp);
66                         if (tpp) {
67                                 if (level >= L_LOCAL
68                                     || is_ld_cst(*expp)
69                                     || is_fp_cst(*expp)
70                                     || (*expp)->ex_class == String) {
71                                         gen_simple_exp(tpp, expp);
72                                         free_expression(*expp);
73                                         *expp = 0;
74                                 } else {
75                                         expr_error(*expp,"illegal initialization");
76                                         free_expression(*expp);
77                                         *expp = 0;
78                                 }
79                         }
80                 }
81 |
82         initial_value_pack(tpp, expp)
83 ]
84 ;
85
86 initial_value_pack(struct type **tpp; struct expr **expp;)
87 :
88         '{'
89                         { if (pack_level == 0) gen_error = 0; pack_level++; }
90         initial_value_list(tpp, expp)
91                         { pack_level--;
92                           if (!pack_level) {
93                                 while (p_stack) {
94                                         struct e_stack *p = p_stack->next;
95
96                                         free_e_stack(p_stack);
97                                         p_stack = p;
98                                 }
99                           }
100                           if (pack_level < gen_error) gen_error = 0;
101                         }
102         '}'
103 ;
104
105 initial_value_list(register struct type **tpp; struct expr **expp;)
106         { struct expr *e1;
107           register struct type **tpp2 = 0;
108           int err_flag = gen_error;
109         }
110 :
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 ',' */
115                 ','
116                         { if (tpp) tpp2 = gen_tpmiddle(); }
117                 initial_value(tpp2, &e1)
118                         { if (!tpp) init_expression(&expp, e1); }
119         ]*
120                         { if (tpp && ! err_flag) gen_tpend(); }
121         ','?                            /* optional trailing comma */
122 ;
123
124 {
125 gen_tpcheck(tpp)
126         struct type **tpp;
127 {
128         register struct type *tp;
129
130         if (gen_error) return;
131         switch((tp = *tpp)->tp_fund) {
132         case ARRAY:
133                 if (! valid_type(tp->tp_up, "array element"))
134                         gen_error = pack_level;
135                 break;
136         case STRUCT:
137                 if (! valid_type(tp, "struct"))
138                         gen_error = pack_level;
139                 break;
140         case UNION:
141                 if (! valid_type(tp, "union"))
142                         gen_error = pack_level;
143                 break;
144         case ERRONEOUS:
145                 if (! gen_error) gen_error = pack_level;
146                 break;
147         }
148 }
149
150 gen_simple_exp(tpp, expp)
151         struct type **tpp;
152         struct expr **expp;
153 {
154         register struct type *tp;
155
156         if (gen_error) return;
157         tp = *tpp;
158         switch(tp->tp_fund) {
159         case ARRAY:
160                 if ((*expp)->ex_class == String && tp->tp_up->tp_fund == CHAR) {
161                         ch_array(tpp,*expp);
162                         break;
163                 }
164                 /* Fall through */
165         case UNION:
166         case STRUCT:
167                 check_and_pad(expp, tpp);
168                 break;
169         case ERRONEOUS:
170         case FUNCTION:
171         case VOID:
172                 gen_error = pack_level;
173                 break;
174         default:
175                 check_ival(expp, tp);
176                 break;
177         }
178 }
179
180 struct type **
181 arr_elem(tpp, p)
182         struct type **tpp;
183         struct e_stack *p;
184 {
185         register struct type *tp = *tpp;
186
187         if (tp->tp_up->tp_fund == CHAR && AHEAD == STRING && p->elem_count == 1) {
188                 p->nelem = 1;
189                 return tpp;
190         }
191         if (AHEAD == '{' || (! aggregate_type(tp->tp_up) && tp->tp_up->tp_fund != UNION))
192                 return &(tp->tp_up);
193         return gen_tphead(&(tp->tp_up), 1);
194 }
195
196 struct sdef *
197 next_field(sd, p)
198         register struct sdef *sd;
199         register struct e_stack *p;
200 {
201         if (sd->sd_sdef)
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;
206         return sd->sd_sdef;
207 }
208
209 struct type **
210 gen_tphead(tpp, nest)
211         struct type **tpp;
212 {
213         register struct type *tp = *tpp;
214         register struct e_stack *p;
215         register struct sdef *sd;
216
217         if (tpp && *tpp == error_type) {
218                 gen_error = pack_level;
219                 return 0;
220         }
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 ...
226                 */
227                 sd = tp->tp_sdef;
228                 if (AHEAD != '{' &&
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:
233                         */
234                         return gen_tphead(&(tp->tp_sdef->sd_type), nest);
235                 }
236         }
237         p = new_e_stack();
238         p->next = p_stack;
239         p_stack = p;
240         p->s_nested = nest;
241         p->s_tpp = tpp;
242         switch(tp->tp_fund) {
243         case UNION:
244                 p->s_def = sd = tp->tp_sdef;
245                 p->bytes_upto_here = 0;
246                 return &(sd->sd_type);
247         case ARRAY:
248                 p->nelem = -1;
249                 p->elem_count = 1;
250                 if (tp->tp_size != (arith) -1) {
251                         p->nelem = (tp->tp_size / tp->tp_up->tp_size);
252                 }
253                 return arr_elem(tpp, p);
254         case STRUCT:
255                 p->s_def = sd = tp->tp_sdef;
256                 p->bytes_upto_here = 0;
257                 p->last_offset = -1;
258 #ifndef NOBITFIELD
259                 while (sd && is_anon_idf(sd->sd_idf)) {
260                         put_bf(sd->sd_type, (arith) 0);
261                         sd = next_field(sd, p);
262                 }
263 #endif
264                 if (! sd) {
265                         /* something wrong with this struct */
266                         gen_error = pack_level;
267                         p_stack = p->next;
268                         free_e_stack(p);
269                         return 0;
270                 }
271                 p->s_def = sd;
272                 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
273                         return gen_tphead(&(sd->sd_type), 1);
274                 }
275                 return &(sd->sd_type);
276         case ERRONEOUS:
277                 if (! gen_error) gen_error = pack_level;
278                 /* fall through */
279         default:
280                 p->nelem = 1;
281                 p->elem_count = 1;
282                 return tpp;
283         }
284 }
285
286 struct type **
287 gen_tpmiddle()
288 {
289         register struct type *tp;
290         register struct sdef *sd;
291         register struct e_stack *p = p_stack;
292
293         if (gen_error) {
294                 if (p) return p->s_tpp;
295                 return 0;
296         }
297 again:
298         tp = *(p->s_tpp);
299         switch(tp->tp_fund) {
300         case ERRONEOUS:
301                 if (! gen_error) gen_error = pack_level;
302                 return p->s_tpp;
303         case UNION:
304                 sd = p->s_def;
305                 p->bytes_upto_here +=
306                         size_of_type(sd->sd_type, "selector");
307                 return p->s_tpp;
308         default:
309                 if (p->elem_count == p->nelem && p->s_nested) {
310                         p = p->next;
311                         free_e_stack(p_stack);
312                         p_stack = p;
313                         goto again;
314                 }
315                 p->elem_count++;
316                 if (p->nelem >= 0 && p->elem_count > p->nelem) {
317                         too_many_initialisers();
318                         return p->s_tpp;
319                 }
320                 if (tp->tp_fund == ARRAY) {
321                         return arr_elem(p->s_tpp, p);
322                 }
323                 return p->s_tpp;
324         case STRUCT:
325                 sd = gen_align_to_next(p);
326                 if (! sd) {
327                         while (p->bytes_upto_here++ < tp->tp_size)
328                                 con_nullbyte();
329                         if (p->s_nested) {
330                                 p = p->next;
331                                 free_e_stack(p_stack);
332                                 p_stack = p;
333                                 goto again;
334                         }
335                         too_many_initialisers();
336                         return p->s_tpp;
337                 }
338                 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
339                         return gen_tphead(&(sd->sd_type), 1);
340                 }
341                 return &(sd->sd_type);
342         }
343 }
344
345 struct sdef *
346 gen_align_to_next(p)
347         register struct e_stack *p;
348 {
349         register struct sdef *sd = p->s_def;
350
351         if (! sd) return sd;
352 #ifndef NOBITFIELD
353         do {
354                 if (is_anon_idf(sd->sd_idf)) put_bf(sd->sd_type, (arith) 0);
355 #endif
356                 sd = next_field(sd, p);
357 #ifndef NOBITFIELD
358         } while (sd && is_anon_idf(sd->sd_idf));
359 #endif
360         p->s_def = sd;
361         return sd;
362 }
363
364 gen_tpend()
365 {
366         register struct e_stack *p = p_stack;
367         register struct type *tp;
368         register struct sdef *sd;
369         int getout = 0;
370
371         while (!getout && p) {
372             if (!gen_error) {
373                 tp = *(p->s_tpp);
374                 switch(tp->tp_fund) {
375                 case UNION:
376                         sd = p->s_def;
377                         p->bytes_upto_here +=
378                                 size_of_type(sd->sd_type, "selector");
379                         while (p->bytes_upto_here++ < tp->tp_size)
380                                 con_nullbyte();
381                         break;
382                 case ARRAY:
383                         if (tp->tp_size == -1) {
384                                 *(p->s_tpp) = construct_type(ARRAY, tp->tp_up,
385                                         0, p->elem_count, NO_PROTO);
386                         }
387                         else {
388                                 while (p->nelem-- > p->elem_count) {
389                                         pad(tp->tp_up);
390                                 }
391                         }
392                         break;
393                 case STRUCT:
394                         sd = gen_align_to_next(p);
395                         while (sd) {
396                                 pad(sd->sd_type);
397                                 if (sd->sd_sdef)
398                                         p->bytes_upto_here += zero_bytes(sd);
399                                 p->bytes_upto_here +=
400                                         size_of_type(sd->sd_type, "selector");
401                                 sd = sd->sd_sdef;
402                         }
403                         while (p->bytes_upto_here++ < tp->tp_size)
404                                 con_nullbyte();
405                         break;
406                 }
407             }
408             if (! p->s_nested) getout = 1;
409             p = p->next;
410             free_e_stack(p_stack);
411             p_stack = p;
412         }
413 }
414
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
418         the rest is zeroed.
419 */
420 check_and_pad(expp, tpp)
421         struct type **tpp;
422         struct expr **expp;
423 {
424         register struct type *tp = *tpp;
425
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.
431                         */
432                         tp = *tpp = construct_type(ARRAY, tp->tp_up,
433                                         0, (arith)1, NO_PROTO);
434                 else {
435                         register int dim = tp->tp_size / tp->tp_up->tp_size;
436                         /* pad remaining members with zeroes */
437                         while (--dim > 0)
438                                 pad(tp->tp_up);
439                 }
440         }
441         else
442         if (tp->tp_fund == STRUCT) {
443                 register struct sdef *sd = tp->tp_sdef;
444
445                 check_and_pad(expp, &(sd->sd_type));
446                 /* next selector is aligned by adding extra zeroes */
447                 if (sd->sd_sdef)
448                         zero_bytes(sd);
449                 while (sd = sd->sd_sdef) { /* pad remaining selectors   */
450                         pad(sd->sd_type);
451                         if (sd->sd_sdef)
452                                 zero_bytes(sd);
453                 }
454         }
455         else if (tp->tp_fund == UNION) {
456                 /* only the first selector can be initialized */
457                 register struct sdef *sd = tp->tp_sdef;
458
459                 check_and_pad(expp, &(sd->sd_type));
460         }
461         else    /* simple type  */
462                 check_ival(expp, tp);
463 }
464
465 /*      pad() fills an element of type tp with zeroes.
466         If the element is an aggregate, pad() is called recursively.
467 */
468 pad(tpx)
469         struct type *tpx;
470 {
471         register struct type *tp = tpx;
472         register arith sz = tp->tp_size;
473
474         gen_tpcheck(&tpx);
475         if (gen_error) return;
476 #ifndef NOBITFIELD
477         if (tp->tp_fund == FIELD) {
478                 put_bf(tp, (arith)0);
479                 return;
480         }
481 #endif /* NOBITFIELD */
482
483         if (tp->tp_align >= word_align) while (sz >= word_size) {
484                 C_con_cst((arith) 0);
485                 sz -= word_size;
486         }
487         while (sz) {
488                 C_con_icon("0", (arith) 1);
489                 sz--;
490         }
491 }
492
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.
498 */
499 check_ival(expp, tp)
500         register struct type *tp;
501         struct expr **expp;
502 {
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.
507         */
508         register struct expr *expr = *expp;
509         
510         switch (tp->tp_fund) {
511         case CHAR:
512         case SHORT:
513         case INT:
514         case LONG:
515         case ENUM:
516         case POINTER:
517                 ch3cast(expp, '=', tp);
518                 expr = *expp;
519 #ifdef DEBUG
520                 print_expr("init-expr after cast", expr);
521 #endif /* DEBUG */
522                 if (!is_ld_cst(expr))
523                         illegal_init_cst(expr);
524                 else
525                 if (expr->VL_CLASS == Const)
526                         con_int(expr);
527                 else
528                 if (expr->VL_CLASS == Name) {
529                         register struct idf *idf = expr->VL_IDF;
530
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);
535                         }
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);
541                 }
542                 else {
543                         ASSERT(expr->VL_CLASS == Label);
544                         C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
545                 }
546                 break;
547         case FLOAT:
548         case DOUBLE:
549         case LNGDBL:
550                 ch3cast(expp, '=', tp);
551                 expr = *expp;
552 #ifdef DEBUG
553                 print_expr("init-expr after cast", expr);
554 #endif /* DEBUG */
555                 if (expr->ex_class == Float) {
556                         char buf[FLT_STRLEN];
557
558                         flt_flt2str(&(expr->FL_ARITH), buf, FLT_STRLEN);
559                         C_con_fcon(buf, expr->ex_type->tp_size);
560                 }
561 #ifdef NOTDEF
562
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.
567
568                 else
569                 if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
570                         /* float f = 1; */
571                         expr = expr->OP_RIGHT;
572                         if (is_cp_cst(expr))
573                                 C_con_fcon(long2str((long)expr->VL_VALUE, 10),
574                                         tp->tp_size);
575                         else 
576                                 illegal_init_cst(expr);
577                 }
578 #endif /* NOTDEF */
579                 else
580                         illegal_init_cst(expr);
581                 break;
582
583 #ifndef NOBITFIELD
584         case FIELD:
585                 ch3cast(expp, '=', tp->tp_up);
586                 expr = *expp;
587 #ifdef DEBUG
588                 print_expr("init-expr after cast", expr);
589 #endif /* DEBUG */
590                 if (is_cp_cst(expr))
591                         put_bf(tp, expr->VL_VALUE);
592                 else
593                         illegal_init_cst(expr);
594                 break;
595 #endif /* NOBITFIELD */
596
597         case ERRONEOUS:
598                 if (! gen_error) gen_error = pack_level;
599                 /* fall through */
600         case VOID:
601                 break;
602         default:
603                 crash("check_ival");
604                 /*NOTREACHED*/
605         }
606 }
607
608 /*      ch_array() initialises an array of characters when given
609         a string constant.
610         Alignment is taken care of.
611 */
612 ch_array(tpp, ex)
613         struct type **tpp;      /* type tp = array of characters        */
614         struct expr *ex;
615 {
616         register struct type *tp = *tpp;
617         register int length = ex->SG_LEN, i;
618         register char *to, *from, *s;
619
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);
624         }
625         else {
626                 arith dim = tp->tp_size / tp->tp_up->tp_size;
627
628 #ifdef LINT
629                 if (length == dim + 1) {
630                             expr_warning(ex, "array is not null-terminated");
631                 } else
632 #endif
633                 if (length > dim + 1) {
634                         expr_strict(ex, "too many initializers");
635                 }
636                 length = dim;
637         }
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;
643         while(--i >= 0) {
644                 *to++ = *from++;
645         }
646         free(ex->SG_VALUE);
647         str_cst(s, length, 0);          /* a string, but not in rom */
648         free(s);
649 }
650
651 /*      As long as some parts of the pipeline cannot handle very long string
652         constants, string constants are written out in chunks
653 */
654 str_cst(str, len, inrom)
655         register char *str;
656         register int len;
657         int inrom;
658 {
659         int chunksize = ((127 + (int) word_size) / (int) word_size) * (int) word_size;
660
661         while (len > chunksize) {
662                 if (inrom)
663                         C_rom_scon(str, (arith) chunksize);
664                 else    C_con_scon(str, (arith) chunksize);
665                 len -= chunksize;
666                 str += chunksize;
667         }
668         if (inrom)
669                 C_rom_scon(str, (arith) len);
670         else    C_con_scon(str, (arith) len);
671 }
672
673 #ifndef NOBITFIELD
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.
680 */
681 put_bf(tp, val)
682         struct type *tp;
683         arith val;
684 {
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;
690
691         ASSERT(sd);
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;
698         }
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;
704                 con_int(&exp);
705                 field = (arith)0;
706                 offset = (arith)-1;
707         }
708 }
709 #endif /* NOBITFIELD */
710
711 int
712 zero_bytes(sd)
713         register struct sdef *sd;
714 {
715         /*      fills the space between a selector of a struct
716                 and the next selector of that struct with zero-bytes.
717         */
718         register int n = sd->sd_sdef->sd_offset - sd->sd_offset -
719                 size_of_type(sd->sd_type, "struct member");
720         int count = n;
721
722         while (n-- > 0)
723                 con_nullbyte();
724         return count;
725 }
726
727 int
728 valid_type(tp, str)
729         struct type *tp;
730         char *str;
731 {
732         ASSERT(tp!=(struct type *)0);
733         if (tp->tp_size < 0) {
734                 error("size of %s unknown", str);
735                 return 0;
736         }
737         return 1;
738 }
739
740 con_int(ex)
741         register struct expr *ex;
742 {
743         register struct type *tp = ex->ex_type;
744
745         ASSERT(is_cp_cst(ex));
746         if (tp->tp_unsigned)
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);
750         else
751                 C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
752 }
753
754 illegal_init_cst(ex)
755         struct expr *ex;
756 {
757         expr_error(ex, "illegal initialization constant");
758         gen_error = pack_level;
759 }
760
761 too_many_initialisers()
762 {
763         error("too many initializers");
764         gen_error = pack_level;
765 }
766 }