Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / 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 3.30 1994/06/24 12:04:18 ceriel Exp $ */
6 /* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
7
8 {
9 #include        "lint.h"
10 #include        "nofloat.h"
11 #ifndef LINT
12 #include        <em.h>
13 #else
14 #include        "l_em.h"
15 #include        "l_lint.h"
16 #endif  /* LINT */
17 #include        "debug.h"
18 #include        <alloc.h>
19 #include        "nobitfield.h"
20 #include        "arith.h"
21 #include        "label.h"
22 #include        "expr.h"
23 #include        "type.h"
24 #include        "struct.h"
25 #include        "field.h"
26 #include        "assert.h"
27 #include        "Lpars.h"
28 #include        "sizes.h"
29 #include        "align.h"
30 #include        "idf.h"
31 #include        "level.h"
32 #include        "def.h"
33 #include        "LLlex.h"
34 #include        "noRoption.h"
35 #include        "estack.h"
36
37 #define con_nullbyte()  C_con_ucon("0", (arith)1)
38 #define aggregate_type(tp) ((tp)->tp_fund == ARRAY || (tp)->tp_fund == STRUCT)
39
40 char *long2str();
41 char *strncpy();
42 extern char options[];
43 static int gen_error;
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;
48 }
49
50 /*      initial_value recursively guides the initialisation expression.
51         Upto now, the initialisation of a union is not allowed!
52 */
53 /* 7 */
54 initial_value(register struct type **tpp; register struct expr **expp;) :
55                         { if (tpp) gen_tpcheck(tpp, 0); }
56 [
57         assignment_expression(expp)
58                 {
59 #ifdef  LINT
60                         lint_expr(*expp, USED);
61 #endif  /* LINT */
62                         if ((*expp)->ex_type->tp_fund == ARRAY)
63                                 array2pointer(*expp);
64                         if (tpp) {
65                                 gen_simple_exp(tpp, expp);
66                                 free_expression(*expp);
67                                 *expp = 0;
68                         }
69                 }
70 |
71         initial_value_pack(tpp, expp)
72 ]
73 ;
74
75 initial_value_pack(struct type **tpp; struct expr **expp;)
76 :
77         '{'
78                         { if (pack_level == 0) gen_error = 0; pack_level++; }
79         initial_value_list(tpp, expp)
80                         { pack_level--;
81                           if (!pack_level) {
82                                 while (p_stack) {
83                                         struct e_stack *p = p_stack->next;
84
85                                         free_e_stack(p_stack);
86                                         p_stack = p;
87                                 }
88                           }
89                           if (pack_level < gen_error) gen_error = 0;
90                         }
91         '}'
92 ;
93
94 initial_value_list(register struct type **tpp; struct expr **expp;)
95         { struct expr *e1;
96           register struct type **tpp2 = 0;
97           int err_flag = gen_error;
98         }
99 :
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 ',' */
104                 ','
105                         { if (tpp) tpp2 = gen_tpmiddle(); }
106                 initial_value(tpp2, &e1)
107                         { if (!tpp) init_expression(&expp, e1); }
108         ]*
109                         { if (tpp && ! err_flag) gen_tpend(); }
110         ','?                            /* optional trailing comma */
111 ;
112
113 {
114 gen_tpcheck(tpp, union_allowed)
115         struct type **tpp;
116 {
117         register struct type *tp;
118
119         if (gen_error) return;
120         switch((tp = *tpp)->tp_fund) {
121         case ARRAY:
122                 if (! valid_type(tp->tp_up, "array element"))
123                         gen_error = pack_level;
124                 break;
125         case STRUCT:
126                 if (! valid_type(tp, "struct"))
127                         gen_error = pack_level;
128                 break;
129         case UNION:
130                 if (! union_allowed) {
131                         error("union initialization not allowed");
132                         gen_error = pack_level;
133                         break;
134                 }
135                 if (! valid_type(tp, "union"))
136                         gen_error = pack_level;
137                 break;
138         case ERRONEOUS:
139                 if (! gen_error) gen_error = pack_level;
140                 break;
141         }
142 }
143
144 gen_simple_exp(tpp, expp)
145         struct type **tpp;
146         struct expr **expp;
147 {
148         register struct type *tp;
149
150         if (gen_error) return;
151         tp = *tpp;
152         switch(tp->tp_fund) {
153         case ARRAY:
154                 if ((*expp)->ex_class == String && tp->tp_up->tp_fund == CHAR) {
155                         ch_array(tpp,*expp);
156                         break;
157                 }
158                 /* Fall through */
159         case STRUCT:
160                 check_and_pad(expp, tpp);
161                 break;
162         case ERRONEOUS:
163                 gen_error = pack_level;
164                 break;
165         default:
166                 check_ival(expp, tp);
167                 break;
168         }
169 }
170
171 struct type **
172 arr_elem(tpp, p)
173         struct type **tpp;
174         struct e_stack *p;
175 {
176         register struct type *tp = *tpp;
177
178         if (tp->tp_up->tp_fund == CHAR && AHEAD == STRING && p->elem_count == 1) {
179                 p->nelem = 1;
180                 return tpp;
181         }
182         if (AHEAD == '{' || ! aggregate_type(tp->tp_up))
183                 return &(tp->tp_up);
184         return gen_tphead(&(tp->tp_up), 1);
185 }
186
187 struct sdef *
188 next_field(sd, p)
189         register struct sdef *sd;
190         register struct e_stack *p;
191 {
192         if (sd->sd_sdef)
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;
197         return sd->sd_sdef;
198 }
199
200 struct type **
201 gen_tphead(tpp, nest)
202         struct type **tpp;
203 {
204         register struct type *tp = *tpp;
205         register struct e_stack *p;
206         register struct sdef *sd;
207
208         if (tpp && *tpp == error_type) {
209                 gen_error = pack_level;
210                 return 0;
211         }
212         if (gen_error) return tpp;
213         p = new_e_stack();
214         p->next = p_stack;
215         p_stack = p;
216         p->s_nested = nest;
217         p->s_tpp = tpp;
218         switch(tp->tp_fund) {
219         case ARRAY:
220                 p->nelem = -1;
221                 p->elem_count = 1;
222                 if (tp->tp_size != (arith) -1) {
223                         p->nelem = (tp->tp_size / tp->tp_up->tp_size);
224                 }
225                 return arr_elem(tpp, p);
226         case STRUCT:
227                 p->s_def = sd = tp->tp_sdef;
228                 p->bytes_upto_here = 0;
229                 p->last_offset = -1;
230 #ifndef NOBITFIELD
231                 while (sd && is_anon_idf(sd->sd_idf)) {
232                         put_bf(sd->sd_type, (arith) 0);
233                         sd = next_field(sd, p);
234                 }
235 #endif
236                 if (! sd) {
237                         /* something wrong with this struct */
238                         gen_error = pack_level;
239                         p_stack = p->next;
240                         free_e_stack(p);
241                         return 0;
242                 }
243                 p->s_def = sd;
244                 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
245                         return gen_tphead(&(sd->sd_type), 1);
246                 }
247                 return &(sd->sd_type);
248         case ERRONEOUS:
249                 if (! gen_error) gen_error = pack_level;
250                 /* fall through */
251         default:
252                 p->nelem = 1;
253                 p->elem_count = 1;
254                 return tpp;
255         }
256 }
257
258 struct type **
259 gen_tpmiddle()
260 {
261         register struct type *tp;
262         register struct sdef *sd;
263         register struct e_stack *p = p_stack;
264
265         if (gen_error) {
266                 if (p) return p->s_tpp;
267                 return 0;
268         }
269 again:
270         tp = *(p->s_tpp);
271         switch(tp->tp_fund) {
272         case ERRONEOUS:
273                 if (! gen_error) gen_error = pack_level;
274                 return p->s_tpp;
275         default:
276                 if (p->elem_count == p->nelem && p->s_nested) {
277                         p = p->next;
278                         free_e_stack(p_stack);
279                         p_stack = p;
280                         goto again;
281                 }
282                 p->elem_count++;
283                 if (p->nelem >= 0 && p->elem_count > p->nelem) {
284                         too_many_initialisers();
285                         return p->s_tpp;
286                 }
287                 if (tp->tp_fund == ARRAY) {
288                         return arr_elem(p->s_tpp, p);
289                 }
290                 return p->s_tpp;
291         case STRUCT:
292                 sd = gen_align_to_next(p);
293                 if (! sd) {
294                         while (p->bytes_upto_here++ < tp->tp_size)
295                                 con_nullbyte();
296                         if (p->s_nested) {
297                                 p = p->next;
298                                 free_e_stack(p_stack);
299                                 p_stack = p;
300                                 goto again;
301                         }
302                         too_many_initialisers();
303                         return p->s_tpp;
304                 }
305                 if (AHEAD != '{' && aggregate_type(sd->sd_type)) {
306                         return gen_tphead(&(sd->sd_type), 1);
307                 }
308                 return &(sd->sd_type);
309         }
310 }
311
312 struct sdef *
313 gen_align_to_next(p)
314         register struct e_stack *p;
315 {
316         register struct sdef *sd = p->s_def;
317
318         if (! sd) return sd;
319 #ifndef NOBITFIELD
320         do {
321                 if (is_anon_idf(sd->sd_idf)) put_bf(sd->sd_type, (arith) 0);
322 #endif
323                 sd = next_field(sd, p);
324 #ifndef NOBITFIELD
325         } while (sd && is_anon_idf(sd->sd_idf));
326 #endif
327         p->s_def = sd;
328         return sd;
329 }
330
331 gen_tpend()
332 {
333         register struct e_stack *p = p_stack;
334         register struct type *tp;
335         register struct sdef *sd;
336         int getout = 0;
337
338         while (!getout && p) {
339             if (!gen_error) {
340                 tp = *(p->s_tpp);
341                 switch(tp->tp_fund) {
342                 case ARRAY:
343                         if (tp->tp_size == -1) {
344                                 *(p->s_tpp) = construct_type(ARRAY,
345                                         tp->tp_up, p->elem_count);
346                         }
347                         else {
348                                 while (p->nelem-- > p->elem_count) {
349                                         pad(tp->tp_up);
350                                 }
351                         }
352                         break;
353                 case STRUCT:
354                         sd = gen_align_to_next(p);
355                         while (sd) {
356                                 pad(sd->sd_type);
357                                 if (sd->sd_sdef)
358                                         p->bytes_upto_here += zero_bytes(sd);
359                                 p->bytes_upto_here +=
360                                         size_of_type(sd->sd_type, "selector");
361                                 sd = sd->sd_sdef;
362                         }
363                         while (p->bytes_upto_here++ < tp->tp_size)
364                                 con_nullbyte();
365                         break;
366                 }
367             }
368             if (! p->s_nested) getout = 1;
369             p = p->next;
370             free_e_stack(p_stack);
371             p_stack = p;
372         }
373 }
374
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
378         the rest is zeroed.
379 */
380 check_and_pad(expp, tpp)
381         struct type **tpp;
382         struct expr **expp;
383 {
384         register struct type *tp = *tpp;
385
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.
391                         */
392                         tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)1);
393                 else {
394                         register int dim = tp->tp_size / tp->tp_up->tp_size;
395                         /* pad remaining members with zeroes */
396                         while (--dim > 0)
397                                 pad(tp->tp_up);
398                 }
399         }
400         else
401         if (tp->tp_fund == STRUCT) {
402                 register struct sdef *sd = tp->tp_sdef;
403
404                 check_and_pad(expp, &(sd->sd_type));
405                 /* next selector is aligned by adding extra zeroes */
406                 if (sd->sd_sdef)
407                         zero_bytes(sd);
408                 while (sd = sd->sd_sdef) { /* pad remaining selectors   */
409                         pad(sd->sd_type);
410                         if (sd->sd_sdef)
411                                 zero_bytes(sd);
412                 }
413         }
414         else    /* simple type  */
415                 check_ival(expp, tp);
416 }
417
418 /*      pad() fills an element of type tp with zeroes.
419         If the element is an aggregate, pad() is called recursively.
420 */
421 pad(tpx)
422         struct type *tpx;
423 {
424         register struct type *tp = tpx;
425         register arith sz = tp->tp_size;
426
427         gen_tpcheck(&tpx, 1);
428         if (gen_error) return;
429         switch (tp->tp_fund) {
430         case UNION:
431 #ifndef NOROPTION
432                 if (options['R']) {
433                         warning("initialization of unions not allowed");
434                 }
435 #endif
436                 break;
437 #ifndef NOBITFIELD
438         case FIELD:
439                 put_bf(tp, (arith)0);
440                 return;
441 #endif /* NOBITFIELD */
442                 default:
443                         break;
444         }
445
446         if (tp->tp_align >= word_align) while (sz >= word_size) {
447                 C_con_cst((arith) 0);
448                 sz -= word_size;
449         }
450         while (sz) {
451                 C_con_icon("0", (arith) 1);
452                 sz--;
453         }
454 }
455
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.
461 */
462 check_ival(expp, tp)
463         register struct type *tp;
464         struct expr **expp;
465 {
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.
470         */
471         register struct expr *expr = *expp;
472         
473         switch (tp->tp_fund) {
474         case CHAR:
475         case SHORT:
476         case INT:
477         case LONG:
478         case ENUM:
479         case POINTER:
480                 ch7cast(expp, '=', tp);
481                 expr = *expp;
482 #ifdef DEBUG
483                 print_expr("init-expr after cast", expr);
484 #endif /* DEBUG */
485                 if (!is_ld_cst(expr))
486                         illegal_init_cst(expr);
487                 else
488                 if (expr->VL_CLASS == Const)
489                         con_int(expr);
490                 else
491                 if (expr->VL_CLASS == Name) {
492                         register struct idf *idf = expr->VL_IDF;
493
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);
501                 }
502                 else {
503                         ASSERT(expr->VL_CLASS == Label);
504                         C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
505                 }
506                 break;
507 #ifndef NOFLOAT
508         case FLOAT:
509         case DOUBLE:
510                 ch7cast(expp, '=', tp);
511                 expr = *expp;
512 #ifdef DEBUG
513                 print_expr("init-expr after cast", expr);
514 #endif /* DEBUG */
515                 if (expr->ex_class == Float)
516                         C_con_fcon(expr->FL_VALUE, expr->ex_type->tp_size);
517 #ifdef NOTDEF
518
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.
523
524                 else
525                 if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
526                         /* float f = 1; */
527                         expr = expr->OP_RIGHT;
528                         if (is_cp_cst(expr))
529                                 C_con_fcon(long2str((long)expr->VL_VALUE, 10),
530                                         tp->tp_size);
531                         else 
532                                 illegal_init_cst(expr);
533                 }
534 #endif /* NOTDEF */
535                 else
536                         illegal_init_cst(expr);
537                 break;
538 #endif /* NOFLOAT */
539
540 #ifndef NOBITFIELD
541         case FIELD:
542                 ch7cast(expp, '=', tp->tp_up);
543                 expr = *expp;
544 #ifdef DEBUG
545                 print_expr("init-expr after cast", expr);
546 #endif /* DEBUG */
547                 if (is_cp_cst(expr))
548                         put_bf(tp, expr->VL_VALUE);
549                 else
550                         illegal_init_cst(expr);
551                 break;
552 #endif /* NOBITFIELD */
553
554         case ERRONEOUS:
555                 if (! gen_error) gen_error = pack_level;
556                 break;
557         default:
558                 crash("check_ival");
559                 /*NOTREACHED*/
560         }
561 }
562
563 /*      ch_array() initialises an array of characters when given
564         a string constant.
565         Alignment is taken care of.
566 */
567 ch_array(tpp, ex)
568         struct type **tpp;      /* type tp = array of characters        */
569         struct expr *ex;
570 {
571         register struct type *tp = *tpp;
572         register int length = ex->SG_LEN, i;
573         register char *to, *from, *s;
574
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);
579         }
580         else {
581                 arith dim = tp->tp_size / tp->tp_up->tp_size;
582
583                 if (length > dim) {
584                         expr_warning(ex, "too many initializers");
585                 }
586                 length = dim;
587         }
588         /* throw out the characters of the already prepared string      */
589         s = Malloc((unsigned) (length));
590         clear(s, length);
591         i = length <= ex->SG_LEN ? length : ex->SG_LEN;
592         to = s; from = ex->SG_VALUE;
593         while(--i >= 0) {
594                 *to++ = *from++;
595         }
596         free(ex->SG_VALUE);
597         str_cst(s, length);
598         free(s);
599 }
600
601 /*      As long as some parts of the pipeline cannot handle very long string
602         constants, string constants are written out in chunks
603 */
604 str_cst(str, len)
605         register char *str;
606         register int len;
607 {
608         int chunksize = ((127 + (int) word_size) / (int) word_size) * (int) word_size;
609
610         while (len > chunksize) {
611                 C_con_scon(str, (arith) chunksize);
612                 len -= chunksize;
613                 str += chunksize;
614         }
615         C_con_scon(str, (arith) len);
616 }
617
618 #ifndef NOBITFIELD
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.
625 */
626 put_bf(tp, val)
627         struct type *tp;
628         arith val;
629 {
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;
635
636         ASSERT(sd);
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;
643         }
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;
649                 con_int(&exp);
650                 field = (arith)0;
651                 offset = (arith)-1;
652         }
653 }
654 #endif /* NOBITFIELD */
655
656 int
657 zero_bytes(sd)
658         register struct sdef *sd;
659 {
660         /*      fills the space between a selector of a struct
661                 and the next selector of that struct with zero-bytes.
662         */
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;
666
667         while (n-- > 0)
668                 con_nullbyte();
669         return count;
670 }
671
672 int
673 valid_type(tp, str)
674         struct type *tp;
675         char *str;
676 {
677         if (tp->tp_size < 0) {
678                 error("size of %s unknown", str);
679                 return 0;
680         }
681         return 1;
682 }
683
684 con_int(ex)
685         register struct expr *ex;
686 {
687         register struct type *tp = ex->ex_type;
688
689         ASSERT(is_cp_cst(ex));
690         if (tp->tp_unsigned)
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);
694         else
695                 C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
696 }
697
698 illegal_init_cst(ex)
699         struct expr *ex;
700 {
701         expr_error(ex, "illegal initialization constant");
702         gen_error = pack_level;
703 }
704
705 too_many_initialisers()
706 {
707         error("too many initializers");
708         gen_error = pack_level;
709 }
710 }