Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / idf.c
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: idf.c,v 3.35 1994/06/24 12:03:59 ceriel Exp $ */
6 /*      IDENTIFIER  FIDDLING & SYMBOL TABLE HANDLING    */
7
8 #include        "lint.h"
9 #include        <em_reg.h>
10 #include        "nofloat.h"
11 #include        "debug.h"
12 #include        "idfsize.h"
13 #include        "botch_free.h"
14 #include        "nopp.h"
15 #include        <alloc.h>
16 #include        "arith.h"
17 #include        "align.h"
18 #include        "LLlex.h"
19 #include        "level.h"
20 #include        "stack.h"
21 #include        "idf.h"
22 #include        "label.h"
23 #include        "def.h"
24 #include        "type.h"
25 #include        "struct.h"
26 #include        "declar.h"
27 #include        "decspecs.h"
28 #include        "sizes.h"
29 #include        "Lpars.h"
30 #include        "assert.h"
31 #include        "specials.h"    /* registration of special identifiers  */
32 #include        "noRoption.h"
33
34 int idfsize = IDFSIZE;
35 extern char options[];
36 extern arith NewLocal();
37
38 char sp_occurred[SP_TOTAL+1];   /* indicate occurrence of special id    */
39
40 struct idf *idf_hashtable[HASHSIZE];
41         /*      All identifiers can in principle be reached through
42                 idf_hashtable; idf_hashtable[hc] is the start of a chain of
43                 idf's whose tags all hash to hc. Each idf is the start of
44                 a chain of def's for that idf, sorted according to level,
45                 with the most recent one on top.
46                 Any identifier occurring on a level is entered into this
47                 list, regardless of the nature of its declaration
48                 (variable, selector, structure tag, etc.).
49         */
50
51 struct idf *
52 idf_hashed(tg, size, hc)
53         char *tg;
54         int size;               /* includes the '\0' character */
55         int hc;
56 {
57         /*      The tag tg with length size and known hash value hc is
58                 looked up in the identifier table; if not found, it is
59                 entered. A pointer to it is returned.
60                 The identifier has already been truncated to idfsize
61                 characters.
62         */
63         register struct idf **hook = &idf_hashtable[hc], *notch;
64
65         while ((notch = *hook)) {
66                 register char *s1 = tg;
67                 register char *cp = notch->id_text;
68                 register int cmp;
69
70                 while (!(cmp = (*s1 - *cp++))) {
71                         if (*s1++ == '\0') {
72                                 break;
73                         }
74                 }
75
76                 if (cmp < 0)
77                         break;
78                 if (cmp == 0)   {
79                         /*      suppose that special identifiers, as
80                                 "setjmp", are already inserted
81                         */
82                         sp_occurred[notch->id_special] = 1;
83                         return notch;
84                 }
85                 hook = &notch->next;
86         }
87         /* a new struct idf must be inserted at the hook */
88         notch = new_idf();
89         notch->next = *hook;
90         *hook = notch;          /* hooked in */
91         notch->id_text = Salloc(tg, (unsigned) size);
92 #ifndef NOPP
93         notch->id_resmac = 0;
94 #endif /* NOPP */
95         return notch;
96 }
97
98 #ifdef  DEBUG
99 hash_stat()
100 {
101         if (options['h'])       {
102                 register int i;
103
104                 print("Hash table tally:\n");
105                 for (i = 0; i < HASHSIZE; i++)  {
106                         register struct idf *notch = idf_hashtable[i];
107                         int cnt = 0;
108
109                         while (notch)   {
110                                 cnt++;
111                                 notch = notch->next;
112                         }
113                         print("%d %d\n", i, cnt);
114                 }
115                 print("End hash table tally\n");
116         }
117 }
118 #endif  /* DEBUG */
119
120 struct idf *
121 str2idf(tg)
122         char tg[];
123 {
124         /*      str2idf() returns an entry in the symbol table for the
125                 identifier tg.  If necessary, an entry is created.
126                 It is used where the text of the identifier is available
127                 but its hash value is not; otherwise idf_hashed() is to
128                 be used.
129         */
130         register char *cp = tg;
131         register int hash;
132         register int pos = -1;
133         register int ch;
134         char ntg[IDFSIZE + 1];
135         register char *ncp = ntg;
136
137         hash = STARTHASH();
138         while (++pos < idfsize && (ch = *cp++)) {
139                 *ncp++ = ch;
140                 hash = ENHASH(hash, ch, pos);
141         }
142         hash = STOPHASH(hash);
143         *ncp++ = '\0';
144         return idf_hashed(ntg, ncp - ntg, hash);
145 }
146
147 struct idf *
148 gen_idf()
149 {
150         /*      A new idf is created out of nowhere, to serve as an
151                 anonymous name.
152         */
153         static int name_cnt;
154         struct idf *id;
155         char *s = Malloc(strlen(dot.tk_file)+50);
156
157         sprint(s, "#%d in %s, line %u",
158                         ++name_cnt, dot.tk_file, dot.tk_line);
159         id = str2idf(s);
160         free(s);
161         return id;
162 }
163
164 int
165 is_anon_idf(idf)
166         struct idf *idf;
167 {
168         return idf->id_text[0] == '#';
169 }
170
171 declare_idf(ds, dc, lvl)
172         struct decspecs *ds;
173         struct declarator *dc;
174 {
175         /*      The identifier inside dc is declared on the level lvl, with
176                 properties deduced from the decspecs ds and the declarator
177                 dc.
178                 The level is given explicitly to be able to insert, e.g.,
179                 labels on the outermost level inside the function.
180                 This routine implements the rich semantics of C
181                 declarations.
182         */
183         register struct idf *idf = dc->dc_idf;
184         register int sc = ds->ds_sc;
185                 /*      This local copy is essential:
186                                 char b(), c;
187                         makes b GLOBAL and c AUTO.
188                 */
189         register struct def *def = idf->id_def;         /* may be NULL */
190         register struct type *type;
191         struct stack_level *stl = stack_level_of(lvl);
192         char formal_array = 0;
193
194         /* determine the present type */
195         if (ds->ds_type == 0)   {
196                 /*      at the L_FORMAL1 level there is no type specified yet
197                 */
198                 ASSERT(lvl == L_FORMAL1);
199                 type = int_type;        /* may change at L_FORMAL2 */
200         }
201         else    {
202                 /* combine the decspecs and the declarator into one type */
203                 type = declare_type(ds->ds_type, dc);
204                 if (type->tp_size <= (arith)0 &&
205                     actual_declaration(sc, type))       {
206                         if (type->tp_size == (arith) -1) {
207                                 /* the type is not yet known,
208                                    but it has to be:
209                                 */
210                                 extern char *symbol2str();
211                                 error("unknown %s-type",
212                                         symbol2str(type->tp_fund));
213                         }
214                         else if (type->tp_fund != LABEL) {
215                                 /* CJ */
216                                 warning("%s has size 0", idf->id_text);
217                         }
218                 }
219         }
220
221         /* some additional work for formal definitions */
222         if (lvl == L_FORMAL2)   {
223                 switch (type->tp_fund)  {
224                 case FUNCTION:
225                         warning("%s is a function; cannot be formal",
226                                 idf->id_text);
227                         type = construct_type(POINTER, type, (arith)0);
228                         break;
229                 case ARRAY:     /* RM 10.1      */
230                         type = construct_type(POINTER, type->tp_up, (arith)0);
231                         formal_array = 1;
232                         break;
233 #ifndef NOFLOAT
234                 case FLOAT:     /* RM 10.1      */
235                         type = double_type;
236                         break;
237 #endif /* NOFLOAT */
238                 case CHAR:
239                 case SHORT:
240                         /*      The RM is not clear about this: we must
241                                 convert the parameter from int (they have
242                                 been pushed as ints) to the specified type.
243                                 The conversion to type int or uint is not
244                                 allowed.
245                         */
246                         break;
247                 }
248         }
249         /*      The tests on types, postponed from do_decspecs(), can now
250                 be performed.
251         */
252         /* update the storage class */
253         if (type && type->tp_fund == FUNCTION)  {
254                 if (sc == 0 || (ds->ds_sc_given && sc == AUTO)) /* RM 8.1 */
255                         sc = GLOBAL;
256                 else
257                 if (sc == REGISTER) {
258                         error("function storage class cannot be register");
259                         ds->ds_sc = sc = GLOBAL;
260                 }
261         }
262         else    /* non-FUNCTION */
263                 if (sc == 0)
264                         sc =    lvl == L_GLOBAL ? GLOBAL
265                                 : lvl == L_FORMAL1 || lvl == L_FORMAL2 ? FORMAL
266                                 : AUTO;
267 #ifndef NOROPTION
268         if (options['R']) { /* some special K & R tests */
269                 /* is it also an enum? */
270                 if (idf->id_enum && idf->id_enum->tg_level == level)
271                         warning("%s is also an enum tag", idf->id_text);
272                 /* is it a universal typedef? */
273                 if (def && def->df_level == L_UNIVERSAL)
274                         warning("redeclaring reserved word %s", idf->id_text);
275         }
276 #endif
277
278 #ifdef  LINT
279         check_hiding(idf, lvl, sc);     /* of some idf by this idf */
280 #endif  /* LINT */
281
282         if (def &&
283             ( def->df_level == lvl ||
284               ( lvl != L_GLOBAL && def->df_level > lvl )
285             )
286            )    {
287                 /*      There is already a declaration for idf on this
288                         level, or even more inside.
289                         The rules differ for different levels.
290                 */
291                 switch (lvl)    {
292                 case L_GLOBAL:
293                         global_redecl(idf, sc, type);
294                         def->df_file = idf->id_file;
295                         def->df_line = idf->id_line;
296                         break;
297                 case L_FORMAL1: /* formal declaration */
298                         error("formal %s redeclared", idf->id_text);
299                         break;
300                 case L_FORMAL2: /* formal definition */
301                 default:        /* local */
302                         error("%s redeclared", idf->id_text);
303                         break;
304                 }
305         }
306         else    /* the idf is unknown on this level */
307         if (lvl == L_FORMAL2 && sc != ENUM && good_formal(def, idf))    {
308                 /* formal declaration, update only */
309                 def->df_type = type;
310                 def->df_formal_array = formal_array;
311                 def->df_sc = sc;
312                 def->df_level = L_FORMAL2;      /* CJ */
313                 def->df_file = idf->id_file;
314                 def->df_line = idf->id_line;
315         }
316         else
317         if (    lvl >= L_LOCAL &&
318                 (type->tp_fund == FUNCTION || sc == EXTERN)
319         )       {
320                 /*      extern declaration inside function is treated the
321                         same way as global extern declaration
322                 */
323 #ifndef NOROPTION
324                 if (    options['R'] &&
325                         (sc == STATIC && type->tp_fund == FUNCTION)
326                 )
327                         if (!is_anon_idf(idf))
328                                 warning("non-global static function %s",
329                                         idf->id_text);
330 #endif
331                 declare_idf(ds, dc, L_GLOBAL);
332         }
333         else    { /* fill in the def block */
334                 register struct def *newdef = new_def();
335
336                 newdef->next = def;
337                 newdef->df_level = lvl;
338                 newdef->df_type = type;
339                 newdef->df_sc = sc;
340                 newdef->df_file = idf->id_file;
341                 newdef->df_line = idf->id_line;
342 #ifdef  LINT
343                 newdef->df_set = 0;
344                 newdef->df_firstbrace = 0;
345 #endif  /* LINT */
346
347                 /* link it into the name list in the proper place */
348                 idf->id_def = newdef;
349                 update_ahead(idf);
350                 stack_idf(idf, stl);
351                 /*      We now calculate the address.
352                         Globals have names and don't get addresses, they
353                         get numbers instead (through data_label()).
354                         Formals are handled by declare_formals().
355                         So here we hand out local addresses only.
356                 */
357                 if (lvl >= L_LOCAL)     {
358                         ASSERT(sc);
359                         switch (sc)     {
360                         case REGISTER:
361                         case AUTO:
362                                 if (type->tp_size == (arith)-1) {
363                                         error("size of local %s unknown",
364                                                 idf->id_text);
365                                 /** type = idf->id_def->df_type = int_type; **/
366                                 }
367                                 newdef->df_address =
368                                         NewLocal(type->tp_size,
369                                                  type->tp_align,
370                                                  regtype(type),
371                                                  sc);
372                                 break;
373                         case STATIC:
374                                 newdef->df_address = (arith) data_label();
375                                 break;
376                         }
377                 }
378         }
379 }
380
381 actual_declaration(sc, tp)
382         int sc;
383         struct type *tp;
384 {
385         /*      An actual_declaration needs space, right here and now.
386         */
387         register int fund = tp->tp_fund;
388
389         if (sc == ENUM || sc == TYPEDEF) /* virtual declarations */
390                 return 0;
391         if (fund == FUNCTION || fund == ARRAY)
392                 /* allocation solved in other ways */
393                 return 0;
394         /* to be allocated */
395         return 1;
396 }
397
398 global_redecl(idf, new_sc, tp)
399         register struct idf *idf;
400         register struct type *tp;
401 {
402         /*      A global identifier may be declared several times,
403                 provided the declarations do not conflict; they might
404                 conflict in type (or supplement each other in the case of
405                 an array) or they might conflict or supplement each other
406                 in storage class.
407         */
408         register struct def *def = idf->id_def;
409
410         if (tp != def->df_type) {
411                 register struct type *otp = def->df_type;
412                 if (    tp->tp_fund != ARRAY || otp->tp_fund != ARRAY ||
413                         tp->tp_up != otp->tp_up
414                 )       {
415                         error("redeclaration of %s with different type",
416                                 idf->id_text);
417                         return;
418                 }
419                 /* Multiple array declaration; this may be interesting */
420                 if (tp->tp_size < 0)    {       /* new decl has [] */
421                         /* nothing new */
422                 }
423                 else
424                 if (otp->tp_size < 0)   {       /* old decl has [] */
425                         def->df_type = tp;
426                 }
427                 else
428                 if (tp->tp_size != otp->tp_size)
429                         error("inconsistent size in redeclaration of array %s",
430                                 idf->id_text);
431         }
432         /*      Now we may be able to update the storage class.
433                 Clean out this mess as soon as we know all the possibilities
434                 for new_sc.
435                 For now we have:
436                         EXTERN:         we have seen the word "extern"
437                         GLOBAL:         the item was declared on the outer
438                                         level, without either "extern" or
439                                         "static".
440                         STATIC:         we have seen the word "static"
441                         IMPLICIT:       function declaration inferred from
442                                         call
443         */
444         if (new_sc == IMPLICIT)
445                 return;                 /* no new information */
446         switch (def->df_sc)     {       /* the old storage class */
447         case EXTERN:
448                 switch (new_sc) {       /* the new storage class */
449                 case EXTERN:
450                 case GLOBAL:
451                         break;
452                 case STATIC:
453                         if (def->df_initialized)        {
454                                 error("cannot redeclare %s to static",
455                                         idf->id_text);
456                         }
457                         else    {
458                                 warning("%s redeclared to static",
459                                                 idf->id_text);
460                         }
461                         def->df_sc = new_sc;
462                         break;
463                 default:
464                         crash("bad storage class");
465                         /*NOTREACHED*/
466                 }
467                 break;
468         case GLOBAL:
469                 switch (new_sc) {       /* the new storage class */
470                 case EXTERN:
471                         def->df_sc = EXTERN;
472                         break;
473                 case GLOBAL:
474                         break;
475                 case STATIC:
476                         if (def->df_initialized)
477                                 error("cannot redeclare %s to static",
478                                         idf->id_text);
479                         else    {
480 #ifdef  LINT
481                                 /* warn unconditionally */
482                                 warning("%s redeclared to static",
483                                                 idf->id_text);
484 #else   /* LINT */
485 #ifndef NOROPTION
486                                 /* warn conditionally */
487                                 if (options['R'])
488                                         warning("%s redeclared to static",
489                                                 idf->id_text);
490 #endif  /* NOROPTION */
491 #endif  /* LINT */
492                                 def->df_sc = STATIC;
493                         }
494                         break;
495                 default:
496                         crash("bad storage class");
497                         /*NOTREACHED*/
498                 }
499                 break;
500         case STATIC:
501                 switch (new_sc) {       /* the new storage class */
502                 case EXTERN:
503                         if (def->df_initialized)
504                                 error("cannot redeclare %s to extern",
505                                         idf->id_text);
506                         else    {
507                                 warning("%s redeclared to extern",
508                                         idf->id_text);
509                                 def->df_sc = EXTERN;
510                         }
511                         break;
512                 case GLOBAL:
513                 case STATIC:
514                         if (def->df_type->tp_fund != FUNCTION)
515                                 warning("%s was already static",
516                                         idf->id_text);
517                         break;
518                 default:
519                         crash("bad storage class");
520                         /*NOTREACHED*/
521                 }
522                 break;
523         case IMPLICIT:
524                 switch (new_sc) {       /* the new storage class */
525                 case EXTERN:
526                 case GLOBAL:
527                         def->df_sc = new_sc;
528                         break;
529                 case STATIC:
530 #ifndef NOROPTION
531                         if (options['R'])
532                                 warning("%s was implicitly declared as extern",
533                                         idf->id_text);
534 #endif
535                         def->df_sc = new_sc;
536                         break;
537                 default:
538                         crash("bad storage class");
539                         /*NOTREACHED*/
540                 }
541                 break;
542         case ENUM:
543         case TYPEDEF:
544                 error("illegal redeclaration of %s", idf->id_text);
545                 break;
546         default:
547                 crash("bad storage class");
548                 /*NOTREACHED*/
549         }
550 }
551
552 int
553 good_formal(def, idf)
554         register struct def *def;
555         register struct idf *idf;
556 {
557         /*      Succeeds if def is a proper L_FORMAL1 definition and
558                 gives an error message otherwise.
559         */
560         if (!def || def->df_level != L_FORMAL1) { /* not in parameter list */
561                 if (!is_anon_idf(idf))
562                         error("%s not in parameter list", idf->id_text);
563                 return 0;
564         }
565         ASSERT(def->df_sc == FORMAL);   /* CJ */
566         return 1;
567 }
568
569 declare_params(dc)
570         register struct declarator *dc;
571 {
572         /*      Declares the formal parameters if they exist.
573         */
574         register struct formal *fm = dc->dc_formal;
575
576         while (fm)      {
577                 declare_parameter(fm->fm_idf);
578                 fm = fm->next;
579         }
580         free_formals(dc->dc_formal);
581         dc->dc_formal = 0;
582 }
583
584 init_idf(idf)
585         register struct idf *idf;
586 {
587         /*      The topmost definition of idf is set to initialized.
588         */
589         register struct def *def = idf->id_def; /* the topmost */
590
591         if (def->df_initialized)
592                 error("multiple initialization of %s", idf->id_text);
593         if (def->df_sc == TYPEDEF)      {
594                 warning("typedef cannot be initialized");
595                 def->df_sc = EXTERN;            /* ??? *//* What else ? */
596         }
597         def->df_initialized = 1;
598 }
599
600 declare_parameter(idf)
601         struct idf *idf;
602 {
603         /*      idf is declared as a formal.
604         */
605         add_def(idf, FORMAL, int_type, level);
606 }
607
608 declare_enum(tp, idf, l)
609         struct type *tp;
610         struct idf *idf;
611         arith l;
612 {
613         /*      idf is declared as an enum constant with value l.
614         */
615         add_def(idf, ENUM, tp, level);
616         idf->id_def->df_address = l;
617 }
618
619 declare_formals(fp)
620         arith *fp;
621 {
622         /*      Declares those formals as int that haven't been declared
623                 by the user.
624                 An address is assigned to each formal parameter.
625                 The total size of the formals is returned in *fp;
626         */
627         register struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
628         arith f_offset = (arith)0;
629
630 #ifdef  DEBUG
631         if (options['t'])
632                 dumpidftab("start declare_formals", 0);
633 #endif  /* DEBUG */
634         while (se)      {
635                 register struct def *def = se->se_idf->id_def;
636
637                 def->df_address = f_offset;
638                 /*      the alignment convention for parameters is: align on
639                         word boundaries, i.e. take care that the following
640                         parameter starts on a new word boundary.
641                 */
642                 f_offset = align(f_offset + def->df_type->tp_size, (int) word_size);
643                 formal_cvt(def); /* cvt int to char or short, if necessary */
644                 def->df_level = L_FORMAL2;      /* CJ */
645                 RegisterAccount(def->df_address, def->df_type->tp_size,
646                                 regtype(def->df_type),
647                                 def->df_sc);
648 #ifdef DBSYMTAB
649                 if (options['g']) {
650                         stb_string(def, FORMAL, se->se_idf->id_text);
651                 }
652 #endif /* DBSYMTAB */
653                 se = se->next;
654         }
655         *fp = f_offset;
656 }
657
658 int
659 regtype(tp)
660         struct type *tp;
661 {
662         switch(tp->tp_fund) {
663         case INT:
664         case LONG:
665                 return reg_any;
666 #ifndef NOFLOAT
667         case FLOAT:
668         case DOUBLE:
669                 return reg_float;
670 #endif /* NOFLOAT */
671         case POINTER:
672                 return reg_pointer;
673         }
674         return -1;
675 }
676
677 add_def(idf, sc, tp, lvl)
678         struct idf *idf;
679         struct type *tp;
680         int lvl;
681         int sc;
682 {
683         /*      The identifier idf is declared on level lvl with storage
684                 class sc and type tp, through a faked C declaration.
685                 This is probably the wrong way to structure the problem,
686                 but it will have to do for the time being.
687         */
688         struct decspecs Ds; struct declarator Dc;
689
690         Ds = null_decspecs;
691         Ds.ds_type = tp;
692         Ds.ds_sc = sc;
693         Dc = null_declarator;
694         Dc.dc_idf = idf;
695         declare_idf(&Ds, &Dc, lvl);
696 }
697
698 update_ahead(idf)
699         register struct idf *idf;
700 {
701         /*      The tk_symb of the token ahead is updated in the light of new
702                 information about the identifier idf.
703         */
704         register int tk_symb = AHEAD;
705
706         if (    (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
707                 ahead.tk_idf == idf
708         )
709                 AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
710                                 TYPE_IDENTIFIER : IDENTIFIER;
711 }
712
713 free_formals(fm)
714         register struct formal *fm;
715 {
716         while (fm)      {
717                 struct formal *tmp = fm->next;
718
719                 free_formal(fm);
720                 fm = tmp;
721         }
722 }
723
724 char hmask[IDFSIZE];
725
726 init_hmask()
727 {
728         /*      A simple congruence random number generator, as
729                 described in Knuth, vol 2.
730         */
731         register int h, rnd = HASH_X;
732
733         for (h = 0; h < IDFSIZE; h++)   {
734                 hmask[h] = rnd;
735                 rnd = (HASH_A * rnd + HASH_C) & HASHMASK;
736         }
737 }