1 /*-------------------------------------------------------------------------
2 SDCCsymt.c - Code file for Symbols table related structures and MACRO's.
3 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22 -------------------------------------------------------------------------*/
26 #include "dbuf_string.h"
30 value
*aggregateToPointer (value
* val
);
31 void printTypeChainRaw (sym_link
* start
, FILE * of
);
34 printFromToType (sym_link
* from
, sym_link
* to
)
37 dbuf_init (&dbuf
, 1024);
38 dbuf_append_str (&dbuf
, "from type '");
39 dbuf_printTypeChain (from
, &dbuf
);
40 dbuf_append_str (&dbuf
, "'\n to type '");
41 dbuf_printTypeChain (to
, &dbuf
);
42 dbuf_append_str (&dbuf
, "'\n");
43 dbuf_write_and_destroy (&dbuf
, stderr
);
48 nounName (sym_link
* sl
)
50 switch (SPEC_NOUN (sl
))
54 if (SPEC_LONGLONG (sl
))
83 return "_Boolbitfield";
84 case V_BITINTBITFIELD
:
85 return "_BitIntbitfield";
96 bucket
*SymbolTab
[HASHTAB_SIZE
]; /* the symbol table */
97 bucket
*StructTab
[HASHTAB_SIZE
]; /* the structure table */
98 bucket
*TypedefTab
[HASHTAB_SIZE
]; /* the typedef table */
99 bucket
*LabelTab
[HASHTAB_SIZE
]; /* the Label table */
100 bucket
*enumTab
[HASHTAB_SIZE
]; /* enumerated table */
101 bucket
*AddrspaceTab
[HASHTAB_SIZE
]; /* the named address space table */
103 /*------------------------------------------------------------------*/
104 /* initSymt () - initialises symbol table related stuff */
105 /*------------------------------------------------------------------*/
111 for (i
= 0; i
< HASHTAB_SIZE
; i
++)
112 SymbolTab
[i
] = StructTab
[i
] = (void *) NULL
;
115 /*-----------------------------------------------------------------*/
116 /* newBucket - allocates & returns a new bucket */
117 /*-----------------------------------------------------------------*/
123 bp
= Safe_alloc (sizeof (bucket
));
128 /*-----------------------------------------------------------------*/
129 /* hashKey - computes the hashkey given a symbol name */
130 /*-----------------------------------------------------------------*/
132 hashKey (const char *s
)
134 unsigned long key
= 0;
138 return key
% HASHTAB_SIZE
;
141 /*-----------------------------------------------------------------*/
142 /* addSym - adds a symbol to the hash Table */
143 /*-----------------------------------------------------------------*/
145 addSym (bucket
** stab
, void *sym
, char *sname
, long level
, int block
, int checkType
)
147 int i
; /* index into the hash Table */
148 bucket
*bp
; /* temp bucket * */
152 symbol
*csym
= (symbol
*) sym
;
154 if (getenv ("DEBUG_SANITY"))
156 fprintf (stderr
, "addSym: %s ", sname
);
158 /* make sure the type is complete and sane */
159 checkTypeSanity (csym
->etype
, csym
->name
);
162 /* prevent overflow of the (r)name buffers */
163 if (strlen (sname
) > SDCC_SYMNAME_MAX
)
165 werror (W_SYMBOL_NAME_TOO_LONG
, SDCC_SYMNAME_MAX
);
166 sname
[SDCC_SYMNAME_MAX
] = '\0';
169 /* the symbols are always added at the head of the list */
171 /* get a free entry */
172 bp
= Safe_alloc (sizeof (bucket
));
174 bp
->sym
= sym
; /* update the symbol pointer */
175 bp
->level
= level
; /* update the nest level */
177 strncpyz (bp
->name
, sname
, sizeof (bp
->name
)); /* copy the name into place */
179 /* if this is the first entry */
182 bp
->prev
= bp
->next
= (void *) NULL
; /* point to nothing */
185 /* not first entry then add @ head of list */
195 /*-----------------------------------------------------------------*/
196 /* deleteSym - deletes a symbol from the hash Table entry */
197 /*-----------------------------------------------------------------*/
199 deleteSym (bucket
** stab
, void *sym
, const char *sname
)
207 /* find the symbol */
210 if (bp
->sym
== sym
) /* found it then break out */
211 break; /* of the loop */
215 if (!bp
) /* did not find it */
218 /* if this is the first one in the chain */
222 if (stab
[i
]) /* if chain ! empty */
223 stab
[i
]->prev
= (void *) NULL
;
225 /* middle || end of chain */
228 if (bp
->next
) /* if not end of chain */
229 bp
->next
->prev
= bp
->prev
;
231 bp
->prev
->next
= bp
->next
;
235 /*-----------------------------------------------------------------*/
236 /* findSym - finds a symbol in a table */
237 /*-----------------------------------------------------------------*/
239 findSym (bucket
** stab
, void *sym
, const char *sname
)
243 bp
= stab
[hashKey (sname
)];
246 if (bp
->sym
== sym
|| strcmp (bp
->name
, sname
) == 0)
251 return (bp
? bp
->sym
: (void *) NULL
);
254 /*-----------------------------------------------------------------*/
255 /* findSymWithLevel - finds a symbol with a name & level */
256 /*-----------------------------------------------------------------*/
258 findSymWithLevel (bucket
** stab
, symbol
* sym
)
265 bp
= stab
[hashKey (sym
->name
)];
268 ** do the search from the head of the list since the
269 ** elements are added at the head it is ensured that
270 ** we will find the deeper definitions before we find
271 ** the global ones. we need to check for symbols with
272 ** level <= to the level given, if levels match then block
273 ** numbers need to match as well
277 if (strcmp (bp
->name
, sym
->name
) == 0 && bp
->level
<= sym
->level
)
279 /* if this is parameter then nothing else need to be checked */
280 if (((symbol
*) (bp
->sym
))->_isparm
)
282 /* if levels match then block numbers should also match */
283 if (bp
->level
&& bp
->level
== sym
->level
&& bp
->block
== sym
->block
284 && ((symbol
*)(bp
->sym
))->seqPoint
<= sym
->seqPoint
)
286 /* if levels don't match then we are okay if the symbol is in scope */
287 if (bp
->level
&& bp
->level
!= sym
->level
&& bp
->block
<= sym
->block
288 && ((symbol
*) (bp
->sym
))->isinscope
289 && (stab
== LabelTab
|| ((symbol
*)(bp
->sym
))->seqPoint
<= sym
->seqPoint
))
291 /* if this is a global variable then we are ok too */
299 return (void *) NULL
;
302 /*-----------------------------------------------------------------*/
303 /* findSymWithBlock - finds a symbol with name in a block */
304 /*-----------------------------------------------------------------*/
306 findSymWithBlock (bucket
** stab
, symbol
* sym
, int block
, long level
)
313 bp
= stab
[hashKey (sym
->name
)];
316 if (strcmp (bp
->name
, sym
->name
) == 0 && (bp
->block
== block
|| (bp
->block
< block
&& bp
->level
< level
)))
321 return (bp
? bp
->sym
: (void *) NULL
);
324 /*------------------------------------------------------------------*/
325 /* newSymbol () - returns a new pointer to a symbol */
326 /*------------------------------------------------------------------*/
328 newSymbol (const char *name
, long scope
)
332 sym
= Safe_alloc (sizeof (symbol
));
334 strncpyz (sym
->name
, name
, sizeof (sym
->name
)); /* copy the name */
335 sym
->level
= scope
; /* set the level */
336 sym
->block
= currBlockno
;
337 sym
->seqPoint
= seqPointNo
;
338 sym
->lineDef
= lexLineno
; /* set the line number */
339 sym
->fileDef
= lexFilename
;
340 sym
->for_newralloc
= 0;
342 sym
->usl
.spillLoc
= 0;
344 // Err on the safe side, when in doubt disabling optimizations.
345 sym
->funcDivFlagSafe
= false;
346 sym
->funcUsesVolatile
= true;
347 sym
->funcRestartAtomicSupport
= true; //options.std_c11;
352 /*------------------------------------------------------------------*/
353 /* newLink - creates a new link (declarator,specifier) */
354 /*------------------------------------------------------------------*/
356 newLink (SYM_LINK_CLASS select
)
360 p
= Safe_alloc (sizeof (sym_link
));
362 p
->funcAttrs
.z88dk_params_offset
= 0;
363 FUNC_SDCCCALL (p
) = -1;
368 /*------------------------------------------------------------------*/
369 /* newStruct - creats a new structdef from the free list */
370 /*------------------------------------------------------------------*/
372 newStruct (const char *tag
)
376 s
= Safe_alloc (sizeof (structdef
));
378 strncpyz (s
->tag
, tag
, sizeof (s
->tag
)); /* copy the tag */
382 /*------------------------------------------------------------------*/
383 /* sclsFromPtr - Return the storage class a pointer points into. */
384 /* S_FIXED is returned for generic pointers or other */
385 /* unexpected cases */
386 /*------------------------------------------------------------------*/
388 sclsFromPtr (sym_link
* ptr
)
390 switch (DCL_TYPE (ptr
))
413 /*------------------------------------------------------------------*/
414 /* pointerTypes - do the computation for the pointer types */
415 /*------------------------------------------------------------------*/
417 pointerTypes (sym_link
* ptr
, sym_link
* type
)
422 sym_link
*otype
= type
;
423 sym_link
*optr
= ptr
;
428 /* find the last unknown pointer type */
432 if (IS_PTR (p
) && DCL_TYPE (p
) == UPOINTER
)
437 /* could not find it */
438 if (!ptr
|| IS_SPEC (ptr
) || !IS_PTR (ptr
))
441 if (IS_PTR (ptr
) && DCL_TYPE (ptr
) != UPOINTER
)
443 pointerTypes (ptr
->next
, type
);
447 /* change the pointer type depending on the
448 storage class of the etype */
449 etype
= getSpec (type
);
452 switch (SPEC_SCLS (etype
))
455 DCL_TYPE (ptr
) = FPOINTER
;
458 DCL_TYPE (ptr
) = IPOINTER
;
461 DCL_TYPE (ptr
) = PPOINTER
;
464 DCL_TYPE (ptr
) = POINTER
;
467 DCL_TYPE (ptr
) = CPOINTER
;
470 DCL_TYPE (ptr
) = EEPPOINTER
;
473 if (!port
->mem
.sfrupointer
)
474 werror (E_SFR_POINTER
);
476 DCL_TYPE (ptr
) = port
->unqualified_pointer
;
479 /* the storage class of etype ends here */
480 SPEC_SCLS (etype
) = 0;
483 /* now change all the remaining unknown pointers
484 to generic pointers */
487 if (!IS_SPEC (optr
) && DCL_TYPE (optr
) == UPOINTER
)
488 DCL_TYPE (optr
) = port
->unqualified_pointer
;
492 /* same for the type although it is highly unlikely that
493 type will have a pointer */
496 if (!IS_SPEC (otype
) && DCL_TYPE (otype
) == UPOINTER
)
497 DCL_TYPE (otype
) = port
->unqualified_pointer
;
502 /*------------------------------------------------------------------*/
503 /* addDecl - adds a declarator @ the end of a chain */
504 /*------------------------------------------------------------------*/
506 addDecl (symbol
*sym
, int type
, sym_link
*p
)
512 if (getenv ("SDCC_DEBUG_FUNCTION_POINTERS"))
513 fprintf (stderr
, "SDCCsymt.c:addDecl(%s,%d,%p)\n", sym
->name
, type
, (void *)p
);
515 /* if we are passed a link then set head & tail */
524 head
= tail
= newLink (DECLARATOR
);
525 DCL_TYPE (head
) = type
;
528 // no type yet: make p the type
534 // type ends in spec, p is single spec element: merge specs
535 else if (IS_SPEC (sym
->etype
) && IS_SPEC (head
) && head
== tail
)
537 sym
->etype
= mergeSpec (sym
->etype
, head
, sym
->name
);
539 // type ends in spec, p is single decl element: p goes before spec
540 else if (IS_SPEC (sym
->etype
) && !IS_SPEC (head
) && head
== tail
)
543 while (t
->next
!= sym
->etype
)
546 tail
->next
= sym
->etype
;
548 // type ends in spec, p ends in spec: merge specs, p's decls go before spec
549 else if (IS_SPEC (sym
->etype
) && IS_SPEC (tail
))
551 sym
->etype
= mergeSpec (sym
->etype
, tail
, sym
->name
);
555 while (t
->next
!= tail
)
561 while (t
->next
!= sym
->etype
)
564 tail
->next
= sym
->etype
;
566 // append p to the type
569 sym
->etype
->next
= head
;
573 /* if the type is an unknown pointer and has
574 a tspec then take the storage class and address
575 attribute from the tspec & make it those of this
577 if (p
&& !IS_SPEC (p
) &&
578 //DCL_TYPE (p) == UPOINTER &&
581 if (!IS_SPEC (sym
->etype
))
583 sym
->etype
= sym
->etype
->next
= newLink (SPECIFIER
);
585 SPEC_SCLS (sym
->etype
) = SPEC_SCLS (DCL_TSPEC (p
));
586 SPEC_ABSA (sym
->etype
) |= SPEC_ABSA (DCL_TSPEC (p
));
587 SPEC_ADDR (sym
->etype
) |= SPEC_ADDR (DCL_TSPEC (p
));
588 DCL_TSPEC (p
) = NULL
;
591 // if there is a function in this type chain
592 if (p
&& funcInChain (sym
->type
))
594 processFuncArgs (sym
, NULL
);
600 /*------------------------------------------------------------------
601 checkTypeSanity: prevent the user from doing e.g.:
603 ------------------------------------------------------------------*/
605 checkTypeSanity (sym_link
*etype
, const char *name
)
611 if (getenv ("DEBUG_SANITY"))
613 fprintf (stderr
, "sanity check skipped for %s (etype==0)\n", name
);
618 if (!IS_SPEC (etype
))
620 if (getenv ("DEBUG_SANITY"))
622 fprintf (stderr
, "sanity check skipped for %s (!IS_SPEC)\n", name
);
627 noun
= nounName (etype
);
629 if (getenv ("DEBUG_SANITY"))
631 fprintf (stderr
, "checking sanity for %s %p\n", name
, (void *)etype
);
634 /* transitional support for double and long double as aliases for float */
635 if (SPEC_NOUN (etype
) == V_DOUBLE
)
637 SPEC_NOUN (etype
) = V_FLOAT
;
638 SPEC_LONG (etype
) = 0;
639 werror (W_DOUBLE_UNSUPPORTED
);
642 if ((SPEC_NOUN (etype
) == V_BITINT
||
643 SPEC_NOUN (etype
) == V_BOOL
||
644 SPEC_NOUN (etype
) == V_CHAR
||
645 SPEC_NOUN (etype
) == V_FLOAT
||
646 SPEC_NOUN (etype
) == V_FIXED16X16
||
647 SPEC_NOUN (etype
) == V_DOUBLE
||
648 SPEC_NOUN (etype
) == V_VOID
)
649 && (SPEC_SHORT (etype
) || SPEC_LONG (etype
) || SPEC_LONGLONG (etype
)))
650 { // long or short for char float double or void
651 werror (E_LONG_OR_SHORT_INVALID
, noun
, name
);
653 if ((SPEC_NOUN (etype
) == V_BOOL
||
654 SPEC_NOUN (etype
) == V_FLOAT
||
655 SPEC_NOUN (etype
) == V_FIXED16X16
||
656 SPEC_NOUN (etype
) == V_DOUBLE
|| SPEC_NOUN (etype
) == V_VOID
) && (etype
->select
.s
.b_signed
|| SPEC_USIGN (etype
)))
657 { // signed or unsigned for float double or void
658 werror (E_SIGNED_OR_UNSIGNED_INVALID
, noun
, name
);
662 "const a;" or "data b;" or "signed s" or "long l"
664 if (!SPEC_NOUN (etype
))
666 SPEC_NOUN (etype
) = V_INT
;
667 if (!(SPEC_SHORT (etype
) || SPEC_LONG (etype
) || SPEC_LONGLONG (etype
) || SPEC_SIGN (etype
) || SPEC_USIGN (etype
)))
668 werror (options
.std_c99
? E_NO_TYPE_SPECIFIER
: W_NO_TYPE_SPECIFIER
, name
);
671 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
672 /* a "plain" int bitfield is unsigned */
673 if (SPEC_NOUN (etype
) == V_BIT
|| SPEC_NOUN (etype
) == V_SBIT
)
675 if (!etype
->select
.s
.b_signed
)
676 SPEC_USIGN (etype
) = 1;
679 if (etype
->select
.s
.b_signed
&& SPEC_USIGN (etype
))
680 { // signed AND unsigned
681 werror (E_SIGNED_AND_UNSIGNED_INVALID
, noun
, name
);
683 if (SPEC_SHORT (etype
) && SPEC_LONG (etype
))
685 werror (E_LONG_AND_SHORT_INVALID
, noun
, name
);
688 if (SPEC_NOUN (etype
) == V_BITINT
)
690 if (SPEC_BITINTWIDTH (etype
) > port
->s
.bitint_maxwidth
|| // Check that port supports bit-precise integers this wide.
691 SPEC_BITINTWIDTH (etype
) < (SPEC_USIGN (etype
) ? 1 : 2)) // Check minimum width mandated by standard.
692 werror (E_INVALID_BITINTWIDTH
);
696 /*------------------------------------------------------------------*/
698 /* currently just a V_CHAR is forced to be unsigned */
699 /* when it's neither signed nor unsigned */
700 /* unless the --fsigned-char command line switch is active */
701 /*------------------------------------------------------------------*/
703 finalizeSpec (sym_link
* lnk
)
706 while (p
&& !IS_SPEC (p
))
708 if (SPEC_NOUN (p
) == V_CHAR
&& !SPEC_USIGN (p
) && !p
->select
.s
.b_signed
)
710 SPEC_USIGN (p
) = !options
.signed_char
;
711 p
->select
.s
.b_implicit_sign
= true;
716 /*------------------------------------------------------------------*/
717 /* mergeSpec - merges two specifiers and returns the new one */
718 /*------------------------------------------------------------------*/
720 mergeSpec (sym_link
* dest
, sym_link
* src
, const char *name
)
724 if (!IS_SPEC (dest
) || !IS_SPEC (src
))
727 werror (E_INTERNAL_ERROR
, __FILE__
, __LINE__
, "cannot merge declarator");
730 werror (E_SYNTAX_ERROR
, yytext
);
731 // the show must go on
732 return newIntLink ();
736 if (!options
.std_c11
&& !options
.std_c99
)
738 if (SPEC_SIGN (dest
) && SPEC_SIGN (src
))
739 werror (W_REPEAT_QUALIFIER
, "signed");
740 if (SPEC_USIGN (dest
) && SPEC_USIGN (src
))
741 werror (W_REPEAT_QUALIFIER
, "unsigned");
742 if (SPEC_CONST (dest
) && SPEC_CONST (src
))
743 werror (W_REPEAT_QUALIFIER
, "const");
744 if (SPEC_VOLATILE (dest
) && SPEC_VOLATILE (src
))
745 werror (W_REPEAT_QUALIFIER
, "volatile");
746 if (SPEC_STAT (dest
) && SPEC_STAT (src
))
747 werror (W_REPEAT_QUALIFIER
, "static");
748 if (SPEC_EXTR (dest
) && SPEC_EXTR (src
))
749 werror (W_REPEAT_QUALIFIER
, "extern");
750 if (SPEC_TYPEDEF (dest
) && SPEC_TYPEDEF (src
))
751 werror (W_REPEAT_QUALIFIER
, "typedef");
752 if (SPEC_SCLS (dest
) == S_REGISTER
&& SPEC_SCLS (src
) == S_REGISTER
)
753 werror (W_REPEAT_QUALIFIER
, "register");
754 if (SPEC_SCLS (dest
) == S_AUTO
&& SPEC_SCLS (src
) == S_AUTO
)
755 werror (W_REPEAT_QUALIFIER
, "auto");
760 if (!SPEC_NOUN (dest
))
762 SPEC_NOUN (dest
) = SPEC_NOUN (src
);
766 /* we shouldn't redeclare the type */
767 if (getenv ("DEBUG_SANITY"))
769 fprintf (stderr
, "mergeSpec: ");
771 werror (E_TWO_OR_MORE_DATA_TYPES
, name
);
775 if ((SPEC_SHORT (src
) || SPEC_LONG (src
) || SPEC_LONGLONG (src
)) &&
776 (SPEC_SHORT (dest
) || SPEC_LONG (dest
) || SPEC_LONGLONG (dest
)))
778 if (!(options
.std_c99
&& SPEC_LONG (src
) && SPEC_LONG (dest
) && !TARGET_IS_PIC14
)) /* C99 has long long */
779 werror (E_SHORTLONG
, name
);
784 /* if destination has no storage class */
785 if (!SPEC_SCLS (dest
))
787 SPEC_SCLS (dest
) = SPEC_SCLS (src
);
789 else if (SPEC_SCLS (dest
) == S_REGISTER
&& SPEC_SCLS (src
) != S_AUTO
)
791 SPEC_SCLS (dest
) = SPEC_SCLS (src
);
795 if (getenv ("DEBUG_SANITY"))
797 fprintf (stderr
, "mergeSpec: ");
799 werror (E_TWO_OR_MORE_STORAGE_CLASSES
, name
);
803 /* copy all the specifications */
805 // we really should do:
809 if (SPEC_what (dest
))
811 werror (W_DUPLICATE_SPEC
, "what");
813 SPEC_what (dst
) |= SPEC_what (src
);
816 // but there are more important thing right now
818 if (options
.std_c99
&& SPEC_LONG (src
) && SPEC_LONG (dest
))
820 SPEC_LONG (dest
) = 0;
821 SPEC_LONGLONG (dest
) = 1;
824 SPEC_LONG (dest
) |= SPEC_LONG (src
);
825 SPEC_LONGLONG (dest
) |= SPEC_LONGLONG (src
);
826 SPEC_SHORT (dest
) |= SPEC_SHORT (src
);
827 SPEC_USIGN (dest
) |= SPEC_USIGN (src
);
828 SPEC_BITINTWIDTH (dest
) |= SPEC_BITINTWIDTH (src
);
829 dest
->select
.s
.b_signed
|= src
->select
.s
.b_signed
;
830 SPEC_STAT (dest
) |= SPEC_STAT (src
);
831 SPEC_EXTR (dest
) |= SPEC_EXTR (src
);
832 SPEC_INLINE (dest
) |= SPEC_INLINE (src
);
833 SPEC_NORETURN (dest
) |= SPEC_NORETURN(src
);
834 SPEC_CONST (dest
) |= SPEC_CONST (src
);
835 SPEC_ABSA (dest
) |= SPEC_ABSA (src
);
836 SPEC_VOLATILE (dest
) |= SPEC_VOLATILE (src
);
837 SPEC_RESTRICT (dest
) |= SPEC_RESTRICT (src
);
838 SPEC_ADDR (dest
) |= SPEC_ADDR (src
);
839 SPEC_OCLS (dest
) = SPEC_OCLS (src
);
840 SPEC_BLEN (dest
) |= SPEC_BLEN (src
);
841 SPEC_BSTR (dest
) |= SPEC_BSTR (src
);
842 SPEC_TYPEDEF (dest
) |= SPEC_TYPEDEF (src
);
843 SPEC_ENUM (dest
) |= SPEC_ENUM (src
);
844 if (SPEC_ARGREG (src
) && !SPEC_ARGREG (dest
))
845 SPEC_ARGREG (dest
) = SPEC_ARGREG (src
);
847 if (SPEC_STAT (dest
) && SPEC_EXTR (dest
))
848 werror (E_TWO_OR_MORE_STORAGE_CLASSES
, name
);
850 if (IS_STRUCT (dest
) && SPEC_STRUCT (dest
) == NULL
)
851 SPEC_STRUCT (dest
) = SPEC_STRUCT (src
);
853 if (FUNC_ISISR (dest
) && FUNC_ISISR (src
))
854 werror (E_INT_MULTIPLE
, name
);
856 /* these are the only function attributes that will be set
857 in a specifier while parsing */
858 FUNC_NONBANKED (dest
) |= FUNC_NONBANKED (src
);
859 FUNC_BANKED (dest
) |= FUNC_BANKED (src
);
860 FUNC_ISCRITICAL (dest
) |= FUNC_ISCRITICAL (src
);
861 FUNC_ISREENT (dest
) |= FUNC_ISREENT (src
);
862 FUNC_ISNAKED (dest
) |= FUNC_ISNAKED (src
);
863 FUNC_ISISR (dest
) |= FUNC_ISISR (src
);
864 FUNC_ISJAVANATIVE (dest
) |= FUNC_ISJAVANATIVE (src
);
865 FUNC_ISBUILTIN (dest
) |= FUNC_ISBUILTIN (src
);
866 FUNC_ISOVERLAY (dest
) |= FUNC_ISOVERLAY (src
);
867 FUNC_INTNO (dest
) |= FUNC_INTNO (src
);
868 FUNC_REGBANK (dest
) |= FUNC_REGBANK (src
);
869 FUNC_ISINLINE (dest
) |= FUNC_ISINLINE (src
);
870 FUNC_ISNORETURN (dest
) |= FUNC_ISNORETURN (src
);
871 if (FUNC_ISRAISONANCE (dest
) && (FUNC_ISIAR (src
) || FUNC_ISCOSMIC (src
) || FUNC_SDCCCALL (src
) >= 0 || FUNC_ISZ88DK_CALLEE (src
)) ||
872 FUNC_ISIAR (dest
) && (FUNC_ISRAISONANCE (src
) || FUNC_ISCOSMIC (src
) || FUNC_SDCCCALL (src
) >= 0 || FUNC_ISZ88DK_CALLEE (src
)) ||
873 FUNC_ISCOSMIC (dest
) && (FUNC_ISRAISONANCE (src
) || FUNC_ISIAR (src
) || FUNC_SDCCCALL (src
) >= 0 || FUNC_ISZ88DK_CALLEE (src
)) ||
874 FUNC_SDCCCALL (dest
) >= 0 && (FUNC_ISRAISONANCE (src
) || FUNC_ISIAR (src
) || FUNC_ISCOSMIC (src
) || FUNC_SDCCCALL (src
) >= 0 && FUNC_SDCCCALL (dest
) != FUNC_SDCCCALL (src
)) || // __sdcccall can be combined with __z88dk_callee.
875 FUNC_ISZ88DK_CALLEE (src
) && (FUNC_ISRAISONANCE (src
) || FUNC_ISIAR (dest
) || FUNC_ISCOSMIC (dest
)))
876 werror (E_MULTIPLE_CALLINGCONVENTIONS
, name
);
877 if (FUNC_SDCCCALL (dest
) == -1)
878 FUNC_SDCCCALL (dest
) = FUNC_SDCCCALL (src
);
879 FUNC_ISSMALLC (dest
) |= FUNC_ISSMALLC (src
);
880 FUNC_ISRAISONANCE (dest
) |= FUNC_ISRAISONANCE (src
);
881 FUNC_ISIAR (dest
) |= FUNC_ISIAR (src
);
882 FUNC_ISCOSMIC (dest
) |= FUNC_ISCOSMIC (src
);
883 FUNC_ISZ88DK_FASTCALL (dest
) |= FUNC_ISZ88DK_FASTCALL (src
);
884 FUNC_ISZ88DK_CALLEE (dest
) |= FUNC_ISZ88DK_CALLEE (src
);
885 for (i
= 0; i
< 9; i
++)
886 dest
->funcAttrs
.preserved_regs
[i
] |= src
->funcAttrs
.preserved_regs
[i
];
888 if (SPEC_ADDRSPACE (src
) && SPEC_ADDRSPACE (dest
))
889 werror (E_TWO_OR_MORE_STORAGE_CLASSES
, name
);
890 if (SPEC_ADDRSPACE (src
))
891 SPEC_ADDRSPACE (dest
) = SPEC_ADDRSPACE (src
);
893 if (SPEC_ALIGNAS (src
) > SPEC_ALIGNAS (dest
))
894 SPEC_ALIGNAS (dest
) = SPEC_ALIGNAS (src
);
895 if (SPEC_SCLS (dest
) == S_REGISTER
&& SPEC_ALIGNAS (dest
))
896 werror (E_ALIGNAS
, SPEC_ALIGNAS (dest
));
901 /*------------------------------------------------------------------*/
902 /* mergeDeclSpec - merges a specifier and a declarator */
903 /*------------------------------------------------------------------*/
905 mergeDeclSpec (sym_link
* dest
, sym_link
* src
, const char *name
)
907 sym_link
*decl
, *spec
, *lnk
;
913 return mergeSpec (dest
, src
, name
);
930 werror (E_SYNTAX_ERROR
, yytext
);
931 // the show must go on
932 return newIntLink ();
936 // for pointers, type qualifiers go in the declarator
937 if (DCL_TYPE (decl
) != ARRAY
&& DCL_TYPE (decl
) != FUNCTION
)
939 DCL_PTR_CONST (decl
) |= SPEC_CONST (spec
);
940 DCL_PTR_VOLATILE (decl
) |= SPEC_VOLATILE (spec
);
941 DCL_PTR_RESTRICT (decl
) |= SPEC_RESTRICT (spec
);
942 if (DCL_PTR_ADDRSPACE (decl
) && SPEC_ADDRSPACE (spec
) &&
943 strcmp (DCL_PTR_ADDRSPACE (decl
)->name
, SPEC_ADDRSPACE (spec
)->name
))
944 werror (E_SYNTAX_ERROR
, yytext
);
945 if (SPEC_ADDRSPACE (spec
))
946 DCL_PTR_ADDRSPACE (decl
) = SPEC_ADDRSPACE (spec
);
948 SPEC_CONST (spec
) = 0;
949 SPEC_VOLATILE (spec
) = 0;
950 SPEC_RESTRICT (spec
) = 0;
951 SPEC_ADDRSPACE (spec
) = 0;
955 while (lnk
&& !IS_SPEC (lnk
->next
))
957 lnk
->next
= mergeSpec (spec
, lnk
->next
, name
);
961 /*-------------------------------------------------------------------*/
962 /* genSymName - generates and returns a name used for anonymous vars */
963 /*-------------------------------------------------------------------*/
965 genSymName (long level
)
967 static int gCount
= 0;
968 static char gname
[SDCC_NAME_MAX
+ 1];
970 SNPRINTF (gname
, sizeof (gname
), "__%04d%04d", level
, gCount
++);
974 /*------------------------------------------------------------------*/
975 /* getSpec - returns the specifier part from a declaration chain */
976 /*------------------------------------------------------------------*/
978 getSpec (sym_link
*p
)
980 while (p
&& !(IS_SPEC (p
)))
986 /*------------------------------------------------------------------*/
987 /* newCharLink() - creates an char type */
988 /*------------------------------------------------------------------*/
994 p
= newLink (SPECIFIER
);
995 SPEC_NOUN (p
) = V_CHAR
;
1001 /*------------------------------------------------------------------*/
1002 /* newFloatLink - a new Float type */
1003 /*------------------------------------------------------------------*/
1009 p
= newLink (SPECIFIER
);
1010 SPEC_NOUN (p
) = V_FLOAT
;
1015 /*------------------------------------------------------------------*/
1016 /* newFixed16x16Link - a new Float type */
1017 /*------------------------------------------------------------------*/
1019 newFixed16x16Link ()
1023 p
= newLink (SPECIFIER
);
1024 SPEC_NOUN (p
) = V_FIXED16X16
;
1029 /*------------------------------------------------------------------*/
1030 /* newLongLink() - new long type */
1031 /*------------------------------------------------------------------*/
1037 p
= newLink (SPECIFIER
);
1038 SPEC_NOUN (p
) = V_INT
;
1044 /*------------------------------------------------------------------*/
1045 /* newLongLongLink() - new long long type */
1046 /*------------------------------------------------------------------*/
1052 p
= newLink (SPECIFIER
);
1053 SPEC_NOUN (p
) = V_INT
;
1054 SPEC_LONGLONG (p
) = 1;
1059 /*------------------------------------------------------------------*/
1060 /* newBitIntLink() - creates a BitInt type */
1061 /*------------------------------------------------------------------*/
1063 newBitIntLink (unsigned int width
)
1065 wassert (width
<= port
->s
.bitint_maxwidth
);
1069 p
= newLink (SPECIFIER
);
1070 SPEC_NOUN (p
) = V_BITINT
;
1071 SPEC_BITINTWIDTH (p
) = width
;
1076 /*------------------------------------------------------------------*/
1077 /* newIntLink() - creates an int type */
1078 /*------------------------------------------------------------------*/
1084 p
= newLink (SPECIFIER
);
1085 SPEC_NOUN (p
) = V_INT
;
1090 /*------------------------------------------------------------------*/
1091 /* newBoolLink() - creates an bool type */
1092 /*------------------------------------------------------------------*/
1098 p
= newLink (SPECIFIER
);
1100 SPEC_NOUN (p
) = V_BIT
;
1102 SPEC_NOUN (p
) = V_BOOL
;
1107 /*------------------------------------------------------------------*/
1108 /* newPtrDiffLink() - creates a ptrdiff type */
1109 /*------------------------------------------------------------------*/
1113 if (GPTRSIZE
<= INTSIZE
)
1114 return newIntLink ();
1115 else if (GPTRSIZE
<= LONGSIZE
)
1116 return newLongLink ();
1117 else if (GPTRSIZE
<= LONGLONGSIZE
)
1118 return newLongLongLink();
1126 /*------------------------------------------------------------------*/
1127 /* newVoidLink() - creates an void type */
1128 /*------------------------------------------------------------------*/
1134 p
= newLink (SPECIFIER
);
1135 SPEC_NOUN (p
) = V_VOID
;
1140 /*------------------------------------------------------------------*/
1141 /* getSize - returns size of a type chain in bytes */
1142 /*------------------------------------------------------------------*/
1144 getSize (sym_link
* p
)
1146 /* if nothing return 0 */
1150 { /* if this is the specifier then */
1151 switch (SPEC_NOUN (p
))
1152 { /* depending on the specifier type */
1154 return (IS_LONGLONG (p
) ? LONGLONGSIZE
: (IS_LONG (p
) ? LONGSIZE
: INTSIZE
));
1156 return ((SPEC_BITINTWIDTH (p
) / 8) + (SPEC_BITINTWIDTH (p
) % 8 ? 1 : 0));
1170 return SPEC_STRUCT (p
)->size
;
1178 case V_BITINTBITFIELD
:
1179 return ((SPEC_BLEN (p
) / 8) + (SPEC_BLEN (p
) % 8 ? 1 : 0));
1185 /* this is a declarator */
1186 switch (DCL_TYPE (p
))
1191 return DCL_ELEM (p
) * getSize (p
->next
);
1200 return (NEARPTRSIZE
);
1205 return (FARPTRSIZE
);
1211 return ((IFFUNC_ISBANKEDCALL (p
->next
) || TARGET_IS_STM8
&& IFFUNC_ISCOSMIC (p
->next
)) ? BFUNCPTRSIZE
: FUNCPTRSIZE
);
1212 return ((IFFUNC_ISBANKEDCALL (p
) || TARGET_IS_STM8
&& IFFUNC_ISCOSMIC (p
)) ? BFUNCPTRSIZE
: FUNCPTRSIZE
);
1220 #define INCOMPLETE 2
1222 /*------------------------------------------------------------------*/
1223 /* checkStructFlexArray - check tree behind a struct */
1224 /*------------------------------------------------------------------*/
1226 checkStructFlexArray (symbol
* sym
, sym_link
* p
)
1228 /* if nothing return FALSE */
1234 /* (nested) struct with flexible array member? */
1235 if (IS_STRUCT (p
) && SPEC_STRUCT (p
)->b_flexArrayMember
)
1237 werror (W_INVALID_FLEXARRAY
);
1240 /* or otherwise incomplete (nested) struct? */
1241 if (IS_STRUCT (p
) && ((SPEC_STRUCT (p
)->size
== 0) || !SPEC_STRUCT (p
)->fields
))
1248 /* this is a declarator */
1251 /* flexible array member? */
1254 if (!options
.std_c99
)
1255 werror (W_C89_NO_FLEXARRAY
);
1256 if (checkStructFlexArray (sym
, p
->next
) == INCOMPLETE
)
1257 werror (E_INCOMPLETE_FIELD
, sym
->name
);
1261 return checkStructFlexArray (sym
, p
->next
);
1266 /*------------------------------------------------------------------*/
1267 /* bitsForType - returns # of bits required to store this type */
1268 /*------------------------------------------------------------------*/
1270 bitsForType (sym_link
*p
)
1272 /* if nothing return 0 */
1277 { /* if this is the specifier then */
1278 switch (SPEC_NOUN (p
))
1279 { /* depending on the specifier type */
1281 if (IS_LONGLONG (p
))
1282 return LONGLONGSIZE
* 8;
1284 return LONGSIZE
* 8;
1287 return SPEC_BITINTWIDTH (p
);
1289 return FLOATSIZE
* 8;
1293 return BOOLSIZE
* 8;
1295 return CHARSIZE
* 8;
1299 return SPEC_STRUCT (p
)->size
* 8;
1307 case V_BITINTBITFIELD
:
1308 return SPEC_BLEN (p
);
1314 /* this is a specifier */
1315 switch (DCL_TYPE (p
))
1318 return DCL_ELEM (p
) * getSize (p
->next
) * 8;
1322 return (NEARPTRSIZE
* 8);
1327 return (FARPTRSIZE
* 8);
1329 return (GPTRSIZE
* 8);
1335 /*------------------------------------------------------------------*/
1336 /* copySymbolChain - copies a symbol chain */
1337 /*------------------------------------------------------------------*/
1339 copySymbolChain (const symbol
* src
)
1346 dest
= copySymbol (src
);
1347 dest
->next
= copySymbolChain (src
->next
);
1351 /*------------------------------------------------------------------*/
1352 /* copySymbol - makes a copy of a symbol */
1353 /*------------------------------------------------------------------*/
1355 copySymbol (const symbol
* src
)
1362 dest
= newSymbol (src
->name
, src
->level
);
1363 memcpy (dest
, src
, sizeof (symbol
));
1364 dest
->level
= src
->level
;
1365 dest
->block
= src
->block
;
1366 dest
->ival
= copyIlist (src
->ival
);
1367 dest
->type
= copyLinkChain (src
->type
);
1368 dest
->etype
= getSpec (dest
->type
);
1370 dest
->key
= src
->key
;
1371 dest
->allocreq
= src
->allocreq
;
1375 /*------------------------------------------------------------------*/
1376 /* reverseSyms - reverses the links for a symbol chain */
1377 /*------------------------------------------------------------------*/
1379 reverseSyms (symbol
* sym
)
1381 symbol
*prev
, *curr
, *next
;
1396 sym
->next
= (void *) NULL
;
1400 /*------------------------------------------------------------------*/
1401 /* reverseLink - reverses the links for a type chain */
1402 /*------------------------------------------------------------------*/
1404 reverseLink (sym_link
* type
)
1406 sym_link
*prev
, *curr
, *next
;
1421 type
->next
= (void *) NULL
;
1425 /*------------------------------------------------------------------*/
1426 /*arraySizes - fill in missing known array sizes */
1427 /*------------------------------------------------------------------*/
1429 arraySizes (sym_link
*type
, const char *name
)
1432 if (IS_DECL(type
) && type
->select
.d
.vla_check_visited
)
1435 if (IS_ARRAY (type
) && !DCL_ELEM (type
) && DCL_ELEM_AST (type
))
1437 value
*tval
= constExprValue(DCL_ELEM_AST (type
), true);
1438 if (!tval
|| (SPEC_SCLS(tval
->etype
) != S_LITERAL
))
1440 if (!options
.std_c99
)
1441 werror(E_VLA_TYPE_C99
);
1442 DCL_ARRAY_VLA(type
) = true;
1446 int size
= ulFromVal(tval
);
1447 if (floatFromVal(tval
) < 0.0)
1449 werror(E_NEGATIVE_ARRAY_SIZE
, name
);
1452 DCL_ELEM(type
) = size
;
1456 type
->select
.d
.vla_check_visited
= true;
1459 arraySizes (type
->next
, name
);
1460 else if (IS_STRUCT (type
))
1461 for(symbol
*fields
= SPEC_STRUCT (type
)->fields
; fields
; fields
= fields
->next
)
1462 arraySizes (fields
->type
, name
);
1465 /*------------------------------------------------------------------*/
1466 /* addSymChain - adds a symbol chain to the symboltable */
1467 /*------------------------------------------------------------------*/
1469 addSymChain (symbol
** symHead
)
1472 symbol
*csym
= NULL
;
1475 int elemsFromIval
= 0;
1477 for (sym
= *symHead
; sym
; sym
= sym
->next
)
1479 changePointer (sym
->type
);
1480 checkTypeSanity (sym
->etype
, sym
->name
);
1482 printf("addSymChain for %p %s level %ld\n", sym
, sym
->name
, sym
->level
);
1484 arraySizes (sym
->type
, sym
->name
);
1485 if (IS_NORETURN (sym
->etype
))
1487 SPEC_NORETURN (sym
->etype
) = 0;
1488 FUNC_ISNORETURN (sym
->type
) = 1;
1491 if (!sym
->level
&& IS_ARRAY (sym
->type
) && IS_ARRAY (sym
->type
) && DCL_ARRAY_VLA (sym
->type
))
1493 werror (E_VLA_SCOPE
);
1497 if (!sym
->level
&& !(IS_SPEC (sym
->etype
) && IS_TYPEDEF (sym
->etype
)))
1498 elemsFromIval
= checkDecl (sym
, 0);
1501 if (IS_ARRAY (sym
->type
) && DCL_ELEM_AST (sym
->type
))
1502 arraySizes (sym
->type
, sym
->name
);
1503 // if this is an array without any dimension then update the dimension from the initial value
1504 else if (IS_ARRAY (sym
->type
) && !DCL_ELEM_AST (sym
->type
) && !DCL_ELEM (sym
->type
))
1505 elemsFromIval
= DCL_ELEM (sym
->type
) = getNelements (sym
->type
, sym
->ival
);
1508 /* if already exists in the symbol table on the same level, ignoring sublevels */
1509 if ((csym
= findSymWithLevel (SymbolTab
, sym
)) && csym
->level
/ LEVEL_UNIT
== sym
->level
/ LEVEL_UNIT
)
1511 /* if not formal parameter and not in file scope
1512 then show symbol redefined error
1513 else check if symbols have compatible types */
1514 if (!sym
->_isparm
&& sym
->level
> 0)
1518 /* If the previous definition was for an array with incomplete
1519 type, and the new definition has completed the type, update
1520 the original type to match (or the otehr way round) */
1521 if (IS_ARRAY (csym
->type
) && IS_ARRAY (sym
->type
))
1523 if (!DCL_ELEM (csym
->type
) && DCL_ELEM (sym
->type
))
1524 DCL_ELEM (csym
->type
) = DCL_ELEM (sym
->type
);
1525 else if (DCL_ELEM (csym
->type
) && !DCL_ELEM (sym
->type
))
1526 DCL_ELEM (sym
->type
) = DCL_ELEM (csym
->type
);
1527 if ((DCL_ELEM (csym
->type
) > DCL_ELEM (sym
->type
)) && elemsFromIval
)
1528 DCL_ELEM (sym
->type
) = DCL_ELEM (csym
->type
);
1530 // Is one is a function declarator without a prototype (valid up to C17), and the other one with a prototype, use the prototype for both. */
1531 if (IS_FUNC (csym
->type
) && IS_FUNC (sym
->type
) && (FUNC_NOPROTOTYPE (csym
->type
) ^ FUNC_NOPROTOTYPE (sym
->type
)))
1533 if (FUNC_NOPROTOTYPE (csym
->type
))
1534 FUNC_ARGS(csym
->type
) = FUNC_ARGS(sym
->type
);
1536 FUNC_ARGS(sym
->type
) = FUNC_ARGS(csym
->type
);
1537 FUNC_NOPROTOTYPE (csym
->type
) = false;
1538 FUNC_NOPROTOTYPE (sym
->type
) = false;
1542 /* If only one of the definitions used the "at" keyword, copy */
1543 /* the address to the other. */
1544 if (IS_SPEC (csym
->etype
) && SPEC_ABSA (csym
->etype
) && IS_SPEC (sym
->etype
) && !SPEC_ABSA (sym
->etype
))
1546 SPEC_ABSA (sym
->etype
) = 1;
1547 SPEC_ADDR (sym
->etype
) = SPEC_ADDR (csym
->etype
);
1549 if (IS_SPEC (csym
->etype
) && !SPEC_ABSA (csym
->etype
) && IS_SPEC (sym
->etype
) && SPEC_ABSA (sym
->etype
))
1551 SPEC_ABSA (csym
->etype
) = 1;
1552 SPEC_ADDR (csym
->etype
) = SPEC_ADDR (sym
->etype
);
1557 if (csym
->ival
&& sym
->ival
)
1559 if (compareTypeExact (csym
->type
, sym
->type
, sym
->level
) != 1)
1565 /* one definition extern ? */
1566 if (IS_EXTERN (csym
->etype
) || IS_EXTERN (sym
->etype
))
1567 werror (E_EXTERN_MISMATCH
, sym
->name
);
1569 werror (E_DUPLICATE
, sym
->name
);
1570 werrorfl (csym
->fileDef
, csym
->lineDef
, E_PREVIOUS_DEF
);
1572 fprintf (stderr
, "from type '");
1573 printTypeChain (csym
->type
, stderr
);
1574 if (IS_SPEC (csym
->etype
) && SPEC_ABSA (csym
->etype
))
1575 fprintf (stderr
, " at 0x%x", SPEC_ADDR (csym
->etype
));
1576 fprintf (stderr
, "'\nto type '");
1577 printTypeChain (sym
->type
, stderr
);
1578 if (IS_SPEC (sym
->etype
) && SPEC_ABSA (sym
->etype
))
1579 fprintf (stderr
, " at 0x%x", SPEC_ADDR (sym
->etype
));
1580 fprintf (stderr
, "'\n");
1585 if (FUNC_BANKED (csym
->type
) || FUNC_BANKED (sym
->type
))
1587 if (FUNC_NONBANKED (csym
->type
) || FUNC_NONBANKED (sym
->type
))
1589 werror (W_BANKED_WITH_NONBANKED
);
1590 FUNC_BANKED (sym
->type
) = 0;
1591 FUNC_NONBANKED (sym
->type
) = 1;
1595 FUNC_BANKED (sym
->type
) = 1;
1600 if (FUNC_NONBANKED (csym
->type
) || FUNC_NONBANKED (sym
->type
))
1602 FUNC_NONBANKED (sym
->type
) = 1;
1606 if (csym
->ival
&& !sym
->ival
)
1607 sym
->ival
= csym
->ival
;
1609 if (!csym
->cdef
&& !sym
->cdef
&& IS_EXTERN (sym
->etype
))
1611 /* if none of symbols is a compiler defined function
1612 and at least one is not extern
1613 then set the new symbol to non extern */
1614 SPEC_EXTR (sym
->etype
) = SPEC_EXTR (csym
->etype
);
1617 /* delete current entry */
1618 deleteSym (SymbolTab
, csym
, csym
->name
);
1619 deleteFromSeg (csym
);
1621 symPtrPtr
= symHead
;
1622 while (*symPtrPtr
&& *symPtrPtr
!= csym
)
1623 symPtrPtr
= &(*symPtrPtr
)->next
;
1624 if (*symPtrPtr
== csym
)
1625 *symPtrPtr
= csym
->next
;
1629 addSym (SymbolTab
, sym
, sym
->name
, sym
->level
, sym
->block
, 1);
1634 /*------------------------------------------------------------------*/
1635 /* funcInChain - DCL Type 'FUNCTION' found in type chain */
1636 /*------------------------------------------------------------------*/
1638 funcInChain (sym_link
* lnk
)
1649 /*------------------------------------------------------------------*/
1650 /* structElemType - returns the type info of a struct member */
1651 /*------------------------------------------------------------------*/
1653 structElemType (sym_link
* stype
, value
* id
)
1655 symbol
*fields
= (SPEC_STRUCT (stype
) ? SPEC_STRUCT (stype
)->fields
: NULL
);
1656 sym_link
*type
, *etype
;
1657 sym_link
*petype
= getSpec (stype
);
1661 /* look for the id */
1664 if (strcmp (fields
->rname
, id
->name
) == 0)
1667 type
= copyLinkChain (fields
->type
);
1668 etype
= getSpec (type
);
1669 SPEC_SCLS (etype
) = (SPEC_SCLS (petype
) == S_REGISTER
? SPEC_SCLS (etype
) : SPEC_SCLS (petype
));
1670 SPEC_OCLS (etype
) = (SPEC_SCLS (petype
) == S_REGISTER
? SPEC_OCLS (etype
) : SPEC_OCLS (petype
));
1671 /* find the first non-array link */
1673 while (IS_ARRAY (t
))
1676 SPEC_CONST (t
) |= SPEC_CONST (stype
);
1678 DCL_PTR_CONST (t
) |= SPEC_CONST (stype
);
1681 fields
= fields
->next
;
1685 werror (E_NOT_MEMBER
, id
->name
);
1687 // the show must go on
1688 return newIntLink ();
1691 /*------------------------------------------------------------------*/
1692 /* getStructElement - returns element of a tructure definition */
1693 /*------------------------------------------------------------------*/
1695 getStructElement (structdef
* sdef
, symbol
* sym
)
1699 for (field
= sdef
->fields
; field
; field
= field
->next
)
1700 if (strcmp (field
->name
, sym
->name
) == 0)
1703 werror (E_NOT_MEMBER
, sym
->name
);
1705 return sdef
->fields
;
1708 /*------------------------------------------------------------------*/
1709 /* compStructSize - computes the size of a structure */
1710 /*------------------------------------------------------------------*/
1712 compStructSize (int su
, structdef
* sdef
)
1714 int sum
= 0, usum
= 0;
1717 const int oldlineno
= lineno
;
1721 werror (E_UNKNOWN_SIZE
, sdef
->tag
);
1724 /* for the identifiers */
1725 loop
= sdef
->fields
;
1728 lineno
= loop
->lineDef
;
1730 /* create the internal name for this variable */
1731 SNPRINTF (loop
->rname
, sizeof (loop
->rname
), "_%s", loop
->name
);
1737 SPEC_VOLATILE (loop
->etype
) |= (su
== UNION
? 1 : 0);
1739 /* if this is a bit field */
1742 SPEC_BUNNAMED (loop
->etype
) = loop
->bitUnnamed
;
1744 /* change it to a unsigned bit */
1745 switch (SPEC_NOUN (loop
->etype
))
1748 SPEC_NOUN( loop
->etype
) = V_BBITFIELD
;
1749 if (loop
->bitVar
> 1)
1750 werror (E_BITFLD_SIZE
, 1);
1753 SPEC_NOUN (loop
->etype
) = V_BITFIELD
;
1754 if (loop
->bitVar
> 8)
1755 werror (E_BITFLD_SIZE
, 8);
1758 SPEC_NOUN (loop
->etype
) = V_BITFIELD
;
1759 if (loop
->bitVar
> port
->s
.int_size
* 8)
1760 werror (E_BITFLD_SIZE
, port
->s
.int_size
* 8);
1763 SPEC_NOUN (loop
->etype
) = V_BITINTBITFIELD
;
1764 if (loop
->bitVar
> SPEC_BITINTWIDTH (loop
->etype
))
1765 werror (E_BITFLD_SIZE
, SPEC_BITINTWIDTH (loop
->etype
));
1768 werror (E_BITFLD_TYPE
);
1771 /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
1772 /* a "plain" int bitfield is unsigned */
1773 if (!loop
->etype
->select
.s
.b_signed
&& SPEC_NOUN (loop
->etype
) != V_BITINTBITFIELD
)
1774 SPEC_USIGN (loop
->etype
) = 1;
1776 if (loop
->bitVar
== BITVAR_PAD
)
1778 /* A zero length bitfield forces padding */
1779 SPEC_BLEN (loop
->etype
) = 0;
1780 SPEC_BSTR (loop
->etype
) = bitOffset
;
1782 bitOffset
= 8; /* padding is not needed when at bit 0 */
1787 SPEC_BLEN (loop
->etype
) = loop
->bitVar
;
1794 /* check if this fit into the remaining */
1795 /* bits of this byte else align it to the */
1796 /* next byte boundary */
1797 if (loop
->bitVar
<= (8 - bitOffset
))
1799 /* fits into current byte */
1801 SPEC_BSTR (loop
->etype
) = bitOffset
;
1802 bitOffset
+= loop
->bitVar
;
1804 else if (!bitOffset
)
1806 /* does not fit, but is already byte aligned */
1808 SPEC_BSTR (loop
->etype
) = bitOffset
;
1809 bitOffset
+= loop
->bitVar
;
1813 if (TARGET_IS_PIC16
&& getenv ("PIC16_PACKED_BITFIELDS"))
1815 /* if PIC16 && environment variable is set, then
1816 * tightly pack bitfields, this means that when a
1817 * bitfield goes beyond byte alignment, do not
1818 * automatically start allocatint from next byte,
1819 * but also use the available bits first */
1820 fprintf (stderr
, ": packing bitfields in structures\n");
1821 SPEC_BSTR (loop
->etype
) = bitOffset
;
1822 bitOffset
+= loop
->bitVar
;
1823 loop
->offset
= (su
== UNION
? sum
= 0 : sum
);
1827 /* does not fit; need to realign first */
1829 loop
->offset
= (su
== UNION
? sum
= 0 : sum
);
1831 SPEC_BSTR (loop
->etype
) = bitOffset
;
1832 bitOffset
+= loop
->bitVar
;
1835 while (bitOffset
> 8)
1844 /* This is a non-bit field. Make sure we are */
1845 /* byte aligned first */
1849 loop
->offset
= (su
== UNION
? sum
= 0 : sum
);
1853 checkDecl (loop
, 1);
1854 sum
+= getSize (loop
->type
);
1856 /* search for "flexible array members" */
1857 /* and do some syntax checks */
1860 int ret
= checkStructFlexArray (loop
, loop
->type
);
1861 if (ret
== FLEXARRAY
)
1863 /* found a "flexible array member" */
1864 sdef
->b_flexArrayMember
= TRUE
;
1865 /* is another struct-member following? */
1867 werror (E_FLEXARRAY_NOTATEND
, loop
->name
);
1868 /* is it the first struct-member? */
1869 else if (loop
== sdef
->fields
)
1870 werror (E_FLEXARRAY_INEMPTYSTRCT
, loop
->name
);
1872 else if (ret
== INCOMPLETE
)
1874 werror (E_INCOMPLETE_FIELD
, loop
->name
);
1881 /* if union then size = sizeof largest field */
1884 /* For UNION, round up after each field */
1885 sum
+= ((bitOffset
+ 7) / 8);
1886 usum
= max (usum
, sum
);
1890 /* For STRUCT, round up after all fields processed */
1892 sum
+= ((bitOffset
+ 7) / 8);
1896 return (su
== UNION
? usum
: sum
);
1899 /*-------------------------------------------------------------------*/
1900 /* promoteAnonStructs - promote anonymous struct/union's fields into */
1901 /* an enclosing struct/union */
1902 /*-------------------------------------------------------------------*/
1904 promoteAnonStructs (int su
, structdef
* sdef
)
1913 tofield
= &sdef
->fields
;
1914 for (field
= sdef
->fields
; field
; field
= nextfield
)
1916 nextfield
= field
->next
;
1917 if (!*field
->name
&& IS_STRUCT (field
->type
))
1919 /* Found an anonymous struct/union. Replace it */
1920 /* with the fields it contains and adjust all */
1923 /* tagged anonymous struct/union is rejected here, though gcc allow it */
1924 if (SPEC_STRUCT (field
->type
)->tagsym
!= NULL
)
1925 werrorfl (field
->fileDef
, field
->lineDef
, E_ANONYMOUS_STRUCT_TAG
, SPEC_STRUCT (field
->type
)->tag
);
1927 base
= field
->offset
;
1928 subfield
= copySymbolChain (SPEC_STRUCT (field
->type
)->fields
);
1930 continue; /* just in case it's empty */
1932 *tofield
= subfield
;
1935 /* check for field name conflicts resulting from promotion */
1936 dupfield
= sdef
->fields
;
1937 while (dupfield
&& dupfield
!= subfield
)
1939 if (*subfield
->name
&& !strcmp (dupfield
->name
, subfield
->name
))
1941 werrorfl (subfield
->fileDef
, subfield
->lineDef
,
1942 E_DUPLICATE_MEMBER
, su
== STRUCT
? "struct" : "union", subfield
->name
);
1943 werrorfl (dupfield
->fileDef
, dupfield
->lineDef
, E_PREVIOUS_DEF
);
1945 dupfield
= dupfield
->next
;
1948 subfield
->offset
+= base
;
1950 subfield
= subfield
->next
;
1954 subfield
->next
= nextfield
;
1955 tofield
= &subfield
->next
;
1958 tofield
= &field
->next
;
1963 /*------------------------------------------------------------------*/
1964 /* checkSClass - check the storage class specification */
1965 /*------------------------------------------------------------------*/
1967 checkSClass (symbol
*sym
, int isProto
)
1971 if (getenv ("DEBUG_SANITY"))
1973 fprintf (stderr
, "checkSClass: %s \n", sym
->name
);
1976 if (!sym
->level
&& SPEC_SCLS (sym
->etype
) == S_AUTO
)
1978 werrorfl (sym
->fileDef
, sym
->lineDef
, E_AUTO_FILE_SCOPE
);
1979 SPEC_SCLS (sym
->etype
) = S_FIXED
;
1982 if (SPEC_SCLS (sym
->etype
) == S_AUTO
&& SPEC_EXTR(sym
->etype
) ||
1983 SPEC_SCLS (sym
->etype
) == S_AUTO
&& SPEC_STAT(sym
->etype
))
1985 werrorfl (sym
->fileDef
, sym
->lineDef
, E_TWO_OR_MORE_STORAGE_CLASSES
, sym
->name
);
1988 /* type is literal can happen for enums change to auto */
1989 if (SPEC_SCLS (sym
->etype
) == S_LITERAL
&& !SPEC_ENUM (sym
->etype
))
1990 SPEC_SCLS (sym
->etype
) = S_AUTO
;
1992 /* if sfr or sbit then must also be volatile */
1993 if (SPEC_SCLS (sym
->etype
) == S_SBIT
|| SPEC_SCLS (sym
->etype
) == S_SFR
)
1995 SPEC_VOLATILE (sym
->etype
) = 1;
1998 if (SPEC_NEEDSPAR (sym
->etype
))
2000 werrorfl (sym
->fileDef
, sym
->lineDef
, E_QUALIFIED_ARRAY_NOPARAM
);
2001 SPEC_NEEDSPAR (sym
->etype
) = 0;
2004 /* make sure restrict is only used with pointers */
2005 if (SPEC_RESTRICT (sym
->etype
))
2007 werrorfl (sym
->fileDef
, sym
->lineDef
, E_BAD_RESTRICT
);
2008 SPEC_RESTRICT (sym
->etype
) = 0;
2014 if (IS_DECL (t
) && DCL_PTR_RESTRICT (t
) && !(IS_PTR (t
) && !IS_FUNCPTR(t
)))
2016 werrorfl (sym
->fileDef
, sym
->lineDef
, E_BAD_RESTRICT
);
2017 DCL_PTR_RESTRICT (t
) = 0;
2023 /* if absolute address given then it mark it as
2024 volatile -- except in the PIC port */
2026 #if !OPT_DISABLE_PIC14 || !OPT_DISABLE_PIC16
2027 /* The PIC port uses a different peep hole optimizer based on "pCode" */
2028 if (!TARGET_PIC_LIKE
)
2031 if (IS_ABSOLUTE (sym
->etype
))
2032 SPEC_VOLATILE (sym
->etype
) = 1;
2034 if (TARGET_IS_MCS51
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2039 if (SPEC_NOUN (sym
->etype
) == V_CHAR
)
2041 else if (SPEC_LONG (sym
->etype
) == 0)
2043 else if (SPEC_LONGLONG (sym
->etype
) == 0)
2048 addr
= SPEC_ADDR (sym
->etype
);
2049 for (n
= 0; n
< size
; n
+= 8)
2050 if (((addr
>> n
) & 0xFF) < 0x80)
2051 werror (W_SFR_ABSRANGE
, sym
->name
);
2053 else if (TARGET_IS_MCS51
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_DATA
)
2055 if (SPEC_ADDR (sym
->etype
) + getSize (sym
->type
) - 1 > 0x7f)
2056 werror (W_DATA_ABSRANGE
, sym
->name
);
2058 else if (TARGET_IS_MCS51
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_IDATA
)
2060 if (SPEC_ADDR (sym
->etype
) + getSize (sym
->type
) - 1 > 0xff)
2061 werror (W_IDATA_ABSRANGE
, sym
->name
);
2063 else if ((TARGET_HC08_LIKE
|| TARGET_MOS6502_LIKE
) && IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_DATA
)
2065 if (SPEC_ADDR (sym
->etype
) + getSize (sym
->type
) - 1 > 0xff)
2066 werror (W_DATA_ABSRANGE
, sym
->name
);
2068 else if (TARGET_IS_SM83
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2069 {// Unlike the other z80-like ports, sm83 has memory mapped I/O in the 0xff00-0xffff range.
2070 if (SPEC_ADDR (sym
->etype
) < 0xff00 || SPEC_ADDR (sym
->etype
) > 0xffff)
2071 werror (W_SFR_ABSRANGE
, sym
->name
);
2073 else if (TARGET_Z80_LIKE
&& !TARGET_IS_SM83
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2075 if (SPEC_ADDR (sym
->etype
) > (FUNC_REGBANK (sym
->type
) ? 0xffff : 0xff))
2076 werror (W_SFR_ABSRANGE
, sym
->name
);
2078 else if (TARGET_IS_PDK13
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2080 if (SPEC_ADDR (sym
->etype
) > 0x1f)
2081 werror (W_SFR_ABSRANGE
, sym
->name
);
2083 else if (TARGET_IS_PDK14
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2085 if (SPEC_ADDR (sym
->etype
) > 0x3f)
2086 werror (W_SFR_ABSRANGE
, sym
->name
);
2088 else if (TARGET_IS_PDK15
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2090 if (SPEC_ADDR (sym
->etype
) > 0x7f)
2091 werror (W_SFR_ABSRANGE
, sym
->name
);
2093 else if (TARGET_IS_PDK16
&& IS_ABSOLUTE (sym
->etype
) && SPEC_SCLS (sym
->etype
) == S_SFR
)
2095 if (SPEC_ADDR (sym
->etype
) > 0x1f)
2096 werror (W_SFR_ABSRANGE
, sym
->name
);
2099 /* If code memory is read only, then pointers to code memory */
2100 /* implicitly point to constants -- make this explicit */
2101 CodePtrPointsToConst (sym
->type
);
2103 /* global variables declared const put into code */
2104 /* if no other storage class specified */
2105 if ((sym
->level
== 0 || SPEC_STAT(sym
->etype
)) && SPEC_SCLS (sym
->etype
) == S_FIXED
&& !IS_FUNC (sym
->type
))
2107 /* find the first non-array link */
2109 while (IS_ARRAY (t
))
2111 if (IS_CONSTANT (t
))
2113 SPEC_SCLS (sym
->etype
) = S_CODE
;
2114 SPEC_SCLS_IMPLICITINTRINSIC (sym
->etype
) = true;
2118 /* global variable in code space is a constant */
2119 if ((sym
->level
== 0 || SPEC_STAT(sym
->etype
)) && SPEC_SCLS (sym
->etype
) == S_CODE
&& port
->mem
.code_ro
)
2121 /* find the first non-array link */
2123 while (IS_ARRAY (t
))
2128 DCL_PTR_CONST (t
) = 1;
2131 /* if bit variable then no storage class can be */
2132 /* specified since bit is already a storage */
2133 if (IS_BITVAR (sym
->etype
) &&
2134 (SPEC_SCLS (sym
->etype
) != S_FIXED
&& SPEC_SCLS (sym
->etype
) != S_SBIT
&& SPEC_SCLS (sym
->etype
) != S_BIT
))
2136 /* find out if this is the return type of a function */
2138 while (t
&& t
->next
!= sym
->etype
)
2140 if (!t
|| t
->next
!= sym
->etype
|| !IS_FUNC (t
))
2142 werror (E_BITVAR_STORAGE
, sym
->name
);
2143 SPEC_SCLS (sym
->etype
) = S_FIXED
;
2147 /* if this is an automatic symbol */
2148 if (sym
->level
&& (options
.stackAuto
|| reentrant
))
2150 if (SPEC_SCLS (sym
->etype
) != S_BIT
&&
2151 SPEC_SCLS (sym
->etype
) != S_REGISTER
)
2153 if ((SPEC_SCLS (sym
->etype
) == S_AUTO
||
2154 SPEC_SCLS (sym
->etype
) == S_FIXED
||
2155 SPEC_SCLS (sym
->etype
) == S_STACK
||
2156 SPEC_SCLS (sym
->etype
) == S_XSTACK
))
2158 SPEC_SCLS (sym
->etype
) = S_AUTO
;
2162 /* storage class may only be specified for statics */
2163 if (!IS_STATIC (sym
->etype
))
2165 werror (E_AUTO_ASSUMED
, sym
->name
);
2171 /* automatic symbols cannot be given */
2172 /* an absolute address ignore it */
2173 if (sym
->level
&& !IS_STATIC (sym
->etype
) && SPEC_ABSA (sym
->etype
) && (options
.stackAuto
|| reentrant
))
2175 werror (E_AUTO_ABSA
, sym
->name
);
2176 SPEC_ABSA (sym
->etype
) = 0;
2179 if (sym
->level
&& !IS_STATIC (sym
->etype
) && (IS_DECL (sym
->type
) ? DCL_PTR_ADDRSPACE (sym
->type
) : SPEC_ADDRSPACE (sym
->type
)) && (options
.stackAuto
|| reentrant
))
2181 werror (E_AUTO_ADDRSPACE
, sym
->name
);
2182 if (IS_DECL (sym
->type
))
2183 DCL_PTR_ADDRSPACE (sym
->type
) = 0;
2185 SPEC_ADDRSPACE (sym
->type
) = 0;
2188 /* arrays & pointers cannot be defined for bits */
2189 /* SBITS or SFRs or BIT */
2190 if ((IS_ARRAY (sym
->type
) || IS_PTR (sym
->type
)) &&
2191 (SPEC_NOUN (sym
->etype
) == V_BIT
|| SPEC_NOUN (sym
->etype
) == V_SBIT
||
2192 SPEC_NOUN (sym
->etype
) == V_BITFIELD
|| SPEC_NOUN (sym
->etype
) == V_BBITFIELD
|| SPEC_NOUN (sym
->etype
) == V_BITINTBITFIELD
||
2193 SPEC_SCLS (sym
->etype
) == S_SFR
))
2195 /* find out if this is the return type of a function */
2197 while (t
&& t
->next
!= sym
->etype
)
2199 if (t
->next
!= sym
->etype
|| !IS_FUNC (t
))
2201 werror (E_BIT_ARRAY
, sym
->name
);
2205 /* if this is a bit|sbit then set length & start */
2206 if (SPEC_NOUN (sym
->etype
) == V_BIT
|| SPEC_NOUN (sym
->etype
) == V_SBIT
)
2208 SPEC_BLEN (sym
->etype
) = 1;
2209 SPEC_BSTR (sym
->etype
) = 0;
2214 /* variables declared in CODE space must have */
2215 /* initializers if not an extern, a global or a static */
2216 if (SPEC_SCLS (sym
->etype
) == S_CODE
&& sym
->ival
== NULL
&& !sym
->_isparm
&&
2218 port
->mem
.code_ro
&& !SPEC_ABSA (sym
->etype
) && !funcInChain (sym
->type
))
2219 werror (E_CODE_NO_INIT
, sym
->name
);
2222 /* if parameter or local variable then change */
2223 /* the storage class to reflect where the var will go */
2224 if (sym
->level
&& SPEC_SCLS (sym
->etype
) == S_FIXED
&& !IS_STATIC (sym
->etype
))
2226 if (options
.stackAuto
|| (currFunc
&& IFFUNC_ISREENT (currFunc
->type
)))
2228 SPEC_SCLS (sym
->etype
) = (options
.useXstack
? S_XSTACK
: S_STACK
);
2233 /*------------------------------------------------------------------*/
2234 /* changePointer - change pointer to functions */
2235 /*------------------------------------------------------------------*/
2237 changePointer (sym_link
* p
)
2239 /* go thru the chain of declarations */
2240 /* if we find a pointer to a function */
2241 /* change it to a ptr to code area */
2242 /* unless the function is banked. */
2243 for (; p
; p
= p
->next
)
2245 if (IS_DECL (p
) && DCL_TYPE (p
) == UPOINTER
)
2246 DCL_TYPE (p
) = port
->unqualified_pointer
;
2247 if (IS_PTR (p
) && IS_FUNC (p
->next
))
2248 if (!IFFUNC_ISBANKEDCALL (p
->next
))
2249 DCL_TYPE (p
) = CPOINTER
;
2253 /*------------------------------------------------------------------*/
2254 /* checkDecl - does semantic validation of a declaration */
2255 /*------------------------------------------------------------------*/
2257 checkDecl (symbol
* sym
, int isProto
)
2259 checkSClass (sym
, isProto
); /* check the storage class */
2260 changePointer (sym
->type
); /* change pointers if required */
2261 arraySizes (sym
->type
, sym
->name
);
2263 if (IS_ARRAY (sym
->type
) && DCL_ARRAY_VLA (sym
->type
) && sym
->ival
&& !sym
->ival
->isempty
)
2264 werror (E_VLA_INIT
);
2265 /* if this is an array without any dimension
2266 then update the dimension from the initial value */
2267 if (IS_ARRAY (sym
->type
) && !DCL_ELEM (sym
->type
))
2268 if (sym
->ival
&& sym
->ival
->isempty
)
2269 werror (E_EMPTY_INIT_UNKNOWN_SIZE
);
2271 return DCL_ELEM (sym
->type
) = getNelements (sym
->type
, sym
->ival
);
2276 /*------------------------------------------------------------------*/
2277 /* copyLinkChain - makes a copy of the link chain & rets ptr 2 head */
2278 /*------------------------------------------------------------------*/
2280 copyLinkChain (const sym_link
*p
)
2282 sym_link
*head
, *loop
;
2283 const sym_link
*curr
;
2285 /* note: v_struct and v_struct->fields are not copied! */
2287 head
= loop
= (curr
? newLink (p
->xclass
) : (void *) NULL
);
2290 memcpy (loop
, curr
, sizeof (sym_link
)); /* copy it */
2291 loop
->next
= (curr
->next
? newLink (curr
->next
->xclass
) : (void *) NULL
);
2299 /*------------------------------------------------------------------*/
2300 /* cleanUpBlock - cleansup the symbol table specified for all the */
2301 /* symbols in the given block */
2302 /*------------------------------------------------------------------*/
2304 cleanUpBlock (bucket
** table
, int block
)
2309 /* go thru the entire table */
2310 for (i
= 0; i
< HASHTAB_SIZE
; i
++)
2312 for (chain
= table
[i
]; chain
; chain
= chain
->next
)
2314 if (chain
->block
>= block
)
2316 deleteSym (table
, chain
->sym
, chain
->name
);
2322 /*------------------------------------------------------------------*/
2323 /* cleanUpLevel - cleans up the symbol table specified for all the */
2324 /* symbols in the given level */
2325 /*------------------------------------------------------------------*/
2327 cleanUpLevel (bucket
** table
, long level
)
2332 /* go thru the entire table */
2333 for (i
= 0; i
< HASHTAB_SIZE
; i
++)
2335 for (chain
= table
[i
]; chain
; chain
= chain
->next
)
2337 if (chain
->level
>= level
)
2339 deleteSym (table
, chain
->sym
, chain
->name
);
2345 /*------------------------------------------------------------------*/
2346 /* leaveBlockScope - mark items in SymbolTab from a particular */
2347 /* block as out-of-scope */
2348 /*------------------------------------------------------------------*/
2350 leaveBlockScope (int block
)
2355 /* go thru the entire table */
2356 for (i
= 0; i
< HASHTAB_SIZE
; i
++)
2358 for (chain
= SymbolTab
[i
]; chain
; chain
= chain
->next
)
2360 if (chain
->block
== block
)
2362 symbol
*sym
= (symbol
*)chain
->sym
;
2364 /* Temporary fix for bug #3289 - leave enums in scope. */
2365 /* This is also buggy but compatible with 4.1.0 and */
2366 /* earlier behavior and less likely to trigger errors. */
2367 if (sym
->etype
&& SPEC_ENUM(sym
->etype
))
2369 /* Everything else, mark as out of scope. */
2377 getAddrspace (sym_link
*type
)
2379 while(IS_ARRAY (type
))
2383 return (DCL_PTR_ADDRSPACE (type
));
2384 return (SPEC_ADDRSPACE (type
));
2387 /*------------------------------------------------------------------*/
2388 /* computeTypeOr - computes the resultant type from two types */
2389 /*------------------------------------------------------------------*/
2391 computeTypeOr (sym_link
*etype1
, sym_link
*etype2
, sym_link
*reType
)
2394 wassert ((IS_CHAR (etype1
) || IS_BOOLEAN (etype1
)) &&
2395 (IS_CHAR (etype2
) || IS_BOOLEAN (etype2
)));
2397 if (SPEC_USIGN (etype1
) == SPEC_USIGN (etype2
))
2399 SPEC_USIGN (reType
) = SPEC_USIGN (etype1
);
2403 if (SPEC_USIGN (etype1
))
2405 if (IS_LITERAL (etype2
) && floatFromVal (valFromType (etype2
)) >= 0)
2406 SPEC_USIGN (reType
) = 1;
2409 /* promote to int */
2410 SPEC_USIGN (reType
) = 0;
2411 SPEC_NOUN (reType
) = V_INT
;
2414 else /* etype1 signed */
2416 if (IS_LITERAL (etype2
) && floatFromVal (valFromType (etype2
)) <= 127)
2417 SPEC_USIGN (reType
) = 0;
2420 /* promote to int */
2421 SPEC_USIGN (reType
) = 0;
2422 SPEC_NOUN (reType
) = V_INT
;
2426 if (SPEC_USIGN (etype2
))
2428 if (IS_LITERAL (etype1
) && floatFromVal (valFromType (etype1
)) >= 0)
2429 SPEC_USIGN (reType
) = 1;
2432 /* promote to int */
2433 SPEC_USIGN (reType
) = 0;
2434 SPEC_NOUN (reType
) = V_INT
;
2437 else /* etype2 signed */
2439 if (IS_LITERAL (etype1
) && floatFromVal (valFromType (etype1
)) <= 127)
2440 SPEC_USIGN (reType
) = 0;
2443 /* promote to int */
2444 SPEC_USIGN (reType
) = 0;
2445 SPEC_NOUN (reType
) = V_INT
;
2451 /*------------------------------------------------------------------*/
2452 /* computeType - computes the resultant type from two types */
2453 /*------------------------------------------------------------------*/
2455 computeType (sym_link
* type1
, sym_link
* type2
, RESULT_TYPE resultType
, int op
)
2459 sym_link
*etype1
= getSpec (type1
);
2462 etype2
= type2
? getSpec (type2
) : type1
;
2465 printf("computeType %d types ", op
); printTypeChain (type1
, stdout
); printf (" vs. "); printTypeChain (type2
, 0);
2468 /* Conditional operator has some special type conversion rules */
2471 /* Function types are really pointers to functions */
2472 if (IS_FUNC (type1
))
2475 fptr
= newLink (DECLARATOR
);
2476 DCL_TYPE (fptr
) = CPOINTER
;
2480 if (IS_FUNC (type2
))
2483 fptr
= newLink (DECLARATOR
);
2484 DCL_TYPE (fptr
) = CPOINTER
;
2488 /* If either type is an array, convert to pointer */
2489 if (IS_ARRAY(type1
))
2491 value
* val
= aggregateToPointer (valFromType (type1
));
2494 etype1
= getSpec (type1
);
2496 if (IS_ARRAY(type2
))
2498 value
* val
= aggregateToPointer (valFromType (type2
));
2501 etype2
= getSpec (type2
);
2504 /* If NULL and another pointer, use the non-NULL pointer type. */
2505 /* Note that NULL can be defined as either 0 or (void *)0. */
2506 if (IS_LITERAL (etype1
) &&
2507 ((IS_PTR (type1
) && IS_VOID (type1
->next
)) || IS_INTEGRAL (type1
)) &&
2508 (floatFromVal (valFromType (etype1
)) == 0) &&
2510 return copyLinkChain (type2
);
2511 else if (IS_LITERAL (etype2
) &&
2512 ((IS_PTR (type2
) && IS_VOID (type2
->next
)) || IS_INTEGRAL (type2
)) &&
2513 (floatFromVal (valFromType (etype2
)) == 0) &&
2515 return copyLinkChain (type1
);
2517 /* If a void pointer, use the void pointer type */
2518 else if (IS_PTR(type1
) && IS_VOID(type1
->next
))
2519 return copyLinkChain (type1
);
2520 else if (IS_PTR(type2
) && IS_VOID(type2
->next
))
2521 return copyLinkChain (type2
);
2523 /* Otherwise fall through to the general case */
2526 /* shift operators have the important type in the left operand */
2527 if (op
== LEFT_OP
|| op
== RIGHT_OP
|| op
== ROT
)
2528 rType
= copyLinkChain(type1
);
2529 /* If difference between pointers or arrays then the result is a ptrdiff */
2530 else if ((op
== '-') && (IS_PTR (type1
) || IS_ARRAY (type1
)) && (IS_PTR (type2
) || IS_ARRAY (type2
)))
2531 rType
= newPtrDiffLink();
2532 /* if one of them is a pointer or array then that prevails */
2533 else if (IS_PTR (type1
) || IS_ARRAY (type1
))
2534 rType
= copyLinkChain (type1
);
2535 else if (IS_PTR (type2
) || IS_ARRAY (type2
))
2536 rType
= copyLinkChain (type2
);
2538 /* if one of them is a float then result is a float */
2539 /* here we assume that the types passed are okay */
2540 /* and can be cast to one another */
2541 /* which ever is greater in size */
2542 else if (IS_FLOAT (etype1
) || IS_FLOAT (etype2
))
2543 rType
= newFloatLink ();
2544 /* if both are fixed16x16 then result is float */
2545 else if (IS_FIXED16X16 (etype1
) && IS_FIXED16X16 (etype2
))
2546 rType
= newFixed16x16Link ();
2547 else if (IS_FIXED16X16 (etype1
) && IS_FLOAT (etype2
))
2548 rType
= newFloatLink ();
2549 else if (IS_FLOAT (etype1
) && IS_FIXED16X16 (etype2
))
2550 rType
= newFloatLink ();
2552 /* if only one of them is a bool variable then the other one prevails */
2553 else if (IS_BOOLEAN (etype1
) && !IS_BOOLEAN (etype2
))
2555 rType
= copyLinkChain (type2
);
2557 else if (IS_BOOLEAN (etype2
) && !IS_BOOLEAN (etype1
))
2559 rType
= copyLinkChain (type1
);
2562 /* if both are bitvars choose the larger one */
2563 else if (IS_BITVAR (etype1
) && IS_BITVAR (etype2
))
2564 rType
= SPEC_BLEN (etype1
) >= SPEC_BLEN (etype2
) ? copyLinkChain (type1
) : copyLinkChain (type2
);
2566 /* otherwise if only one of them is a bit variable then the other one prevails
2567 exceptions for _BitInt apply */
2568 else if (IS_BITVAR (etype1
) && !IS_BITVAR (etype2
))
2570 if (SPEC_NOUN (etype1
) == V_BITINTBITFIELD
&& SPEC_BITINTWIDTH(etype1
) > bitsForType (type2
))
2572 rType
= copyLinkChain (type1
);
2576 rType
= copyLinkChain (type2
);
2577 /* int bitfield can have up to 16 bits */
2578 if (getSize (etype1
) > 1)
2579 SPEC_NOUN (getSpec (rType
)) = V_INT
;
2582 else if (IS_BITVAR (etype2
) && !IS_BITVAR (etype1
))
2584 if (SPEC_NOUN (etype2
) == V_BITINTBITFIELD
&& SPEC_BITINTWIDTH(etype2
) > bitsForType (type1
))
2586 rType
= copyLinkChain (type2
);
2590 rType
= copyLinkChain (type1
);
2591 /* int bitfield can have up to 16 bits */
2592 if (getSize (etype2
) > 1)
2593 SPEC_NOUN (getSpec (rType
)) = V_INT
;
2596 else if (bitsForType (type1
) > bitsForType (type2
))
2597 rType
= copyLinkChain (type1
);
2599 rType
= copyLinkChain (type2
);
2601 reType
= getSpec (rType
);
2603 /* avoid conflicting types */
2604 reType
->select
.s
.b_signed
= 0;
2606 /* if result is a literal then make not so */
2607 if (IS_LITERAL (reType
))
2609 SPEC_SCLS (reType
) = S_REGISTER
;
2610 SPEC_CONST (reType
) = 0;
2615 case RESULT_TYPE_IFX
:
2616 if (TARGET_HC08_LIKE
)
2619 case RESULT_TYPE_BOOL
:
2622 SPEC_NOUN (reType
) = TARGET_MCS51_LIKE
? V_BIT
: V_BOOL
;
2626 case RESULT_TYPE_CHAR
:
2627 if (IS_BOOL (reType
) || IS_BITVAR (reType
))
2629 SPEC_NOUN (reType
) = V_CHAR
;
2630 SPEC_SCLS (reType
) = 0;
2631 SPEC_USIGN (reType
) = 0;
2635 case RESULT_TYPE_INT
:
2636 case RESULT_TYPE_NONE
:
2637 case RESULT_TYPE_OTHER
:
2638 case RESULT_TYPE_GPTR
:
2639 if (!IS_SPEC (rType
))
2643 if (IS_BOOLEAN (reType
))
2645 SPEC_NOUN (reType
) = V_CHAR
;
2646 SPEC_SCLS (reType
) = 0;
2647 SPEC_USIGN (reType
) = 0;
2650 else if (SPEC_NOUN (reType
) == V_BITINTBITFIELD
) // _BitInt(N) bit-field promotes to _BitInt(N).
2652 SPEC_NOUN (reType
) = V_BITINT
;
2654 else if (IS_BITFIELD (reType
))
2656 /* could be smarter, but it depends on the op */
2657 /* this is for the worst case: a multiplication of 4 * 4 bit */
2658 SPEC_NOUN (reType
) = SPEC_BLEN (reType
) <= 4 ? V_CHAR
: V_INT
;
2659 SPEC_SCLS (reType
) = 0;
2660 SPEC_USIGN (reType
) = 0;
2663 else if (IS_CHAR (reType
))
2665 /* promotion of some special cases */
2671 if (!IS_BITFIELD (etype1
) && !IS_BITFIELD (etype2
))
2672 return computeTypeOr (etype1
, etype2
, reType
);
2675 if (SPEC_USIGN (etype1
) != SPEC_USIGN (etype2
))
2677 SPEC_USIGN (reType
) = 1;
2682 SPEC_NOUN (reType
) = V_INT
;
2683 SPEC_USIGN (reType
) = 0;
2686 /* if both are unsigned char then no promotion required */
2687 if (!(SPEC_USIGN (etype1
) && SPEC_USIGN (etype2
)))
2689 SPEC_NOUN (reType
) = V_INT
;
2690 SPEC_USIGN (reType
) = 0;
2703 /* SDCC's sign promotion:
2704 - if one or both operands are unsigned, the resultant type will be unsigned
2705 (except char, see below)
2706 - if an operand is promoted to a larger type (char -> int, int -> long),
2707 the larger type will be signed
2709 SDCC tries hard to avoid promotion to int and does 8 bit calculation as
2710 much as possible. We're leaving ISO IEC 9899 here and have to extrapolate
2711 the standard. The standard demands, that the result has to be the same
2712 "as if" the promotion would have been performed:
2714 - if the result of an operation with two char's is promoted to a
2715 larger type, the result will be signed.
2717 More sophisticated are these:
2718 - if the result of an operation with two char's is a char again,
2719 the result will only then be unsigned, if both operands are
2720 unsigned. In all other cases the result will be signed.
2722 This seems to be contradictionary to the first two rules, but it makes
2723 real sense (all types are char's):
2725 A signed char can be negative; this must be preserved in the result
2728 Only if both operands are unsigned it's safe to make the result
2729 unsigned; this helps to avoid overflow:
2732 - ToDo: document '|', '^' and '&'
2734 Homework: - why is (200 * 200 < 0) true?
2735 - why is { char l = 200, r = 200; (r * l > 0) } true?
2738 if (!IS_FLOAT (reType
) && ((SPEC_USIGN (etype1
)
2739 /* if this operand is promoted to a larger type,
2740 then it will be promoted to a signed type */
2741 && !(bitsForType (etype1
) < bitsForType (reType
))
2742 /* char require special handling */
2743 && !IS_CHAR (etype1
)) || /* same for 2nd operand */
2744 (SPEC_USIGN (etype2
) && !(bitsForType (etype2
) < bitsForType (reType
)) && !IS_CHAR (etype2
)) || /* if both are 'unsigned char' and not promoted
2745 let the result be unsigned too */
2746 (SPEC_USIGN (etype1
) && IS_CHAR (etype1
)
2747 && (SPEC_USIGN (etype2
) && IS_CHAR (etype2
) || op
== LEFT_OP
|| op
== RIGHT_OP
|| op
== ROT
) && IS_CHAR (reType
))) ||
2748 SPEC_USIGN (etype1
) && SPEC_USIGN (etype2
) && IS_BITINT (rType
) || // unsigned _BitInt stays unsigned.
2749 SPEC_USIGN (etype1
) && SPEC_USIGN (etype2
) && bitsForType (etype1
) <= bitsForType (reType
) && bitsForType (etype2
) < bitsForType (reType
)) // keep operations on small unsigned bit-fields unsigned.
2750 SPEC_USIGN (reType
) = 1;
2752 SPEC_USIGN (reType
) = 0;
2757 /*------------------------------------------------------------------*/
2758 /* compareFuncType - compare function prototypes */
2759 /*------------------------------------------------------------------*/
2761 compareFuncType (sym_link
* dest
, sym_link
* src
)
2763 value
*exargs
, *acargs
;
2768 /* if not type then some kind of error */
2772 /* check the return value type */
2773 if (compareType (dest
->next
, src
->next
, false) <= 0)
2776 /* Really, reentrant should match regardless of argCnt, but */
2777 /* this breaks some existing code (the fp lib functions). If */
2778 /* the first argument is always passed the same way, this */
2779 /* lax checking is ok (but may not be true for in future ports) */
2780 if (IFFUNC_ISREENT (dest
) != IFFUNC_ISREENT (src
) && argCnt
> 1)
2782 //printf("argCnt = %d\n",argCnt);
2786 if (IFFUNC_ISBANKEDCALL (dest
) != IFFUNC_ISBANKEDCALL (src
))
2789 if (IFFUNC_ISWPARAM (dest
) != IFFUNC_ISWPARAM (src
))
2794 if (IFFUNC_ISSHADOWREGS (dest
) != IFFUNC_ISSHADOWREGS (src
))
2799 if (IFFUNC_ISZ88DK_FASTCALL (dest
) != IFFUNC_ISZ88DK_FASTCALL (src
) ||
2800 IFFUNC_ISZ88DK_CALLEE (dest
) != IFFUNC_ISZ88DK_CALLEE (src
))
2803 if (IFFUNC_ISRAISONANCE (dest
) != IFFUNC_ISRAISONANCE (src
) ||
2804 IFFUNC_ISCOSMIC (dest
) != IFFUNC_ISCOSMIC (src
) ||
2805 IFFUNC_ISIAR (dest
) != IFFUNC_ISIAR (src
))
2808 if (FUNC_SDCCCALL (dest
) >= 0 && FUNC_SDCCCALL (src
) >= 0 &&
2809 FUNC_SDCCCALL (dest
) != FUNC_SDCCCALL (src
))
2812 for (i
= 0; i
< 9; i
++)
2813 if (dest
->funcAttrs
.preserved_regs
[i
] > src
->funcAttrs
.preserved_regs
[i
])
2816 /* compare register bank */
2817 if (FUNC_REGBANK (dest
) != FUNC_REGBANK (src
))
2818 { /* except for ISR's whose prototype need not match
2819 since they are the top of a call-tree and
2820 the prototype is only necessary for its vector in main */
2821 if (!IFFUNC_ISISR (dest
) || !IFFUNC_ISISR (src
))
2827 /* compare expected args with actual args */
2828 exargs
= FUNC_ARGS (dest
);
2829 acargs
= FUNC_ARGS (src
);
2831 /* for all the expected args do */
2832 for (argCnt
= 1; exargs
&& acargs
; exargs
= exargs
->next
, acargs
= acargs
->next
, argCnt
++)
2834 /* If the actual argument is an array, any prototype
2835 * will have modified it to a pointer. Duplicate that
2838 if (IS_AGGREGATE (acargs
->type
))
2840 checkValue
= copyValue (acargs
);
2841 aggregateToPointer (checkValue
);
2845 checkValue
= acargs
;
2847 if (IFFUNC_ISREENT (dest
) && compareType (exargs
->type
, checkValue
->type
, false) <= 0)
2851 if (!IFFUNC_ISREENT (dest
) && compareTypeExact (exargs
->type
, checkValue
->type
, 1) <= 0)
2857 /* if one them ended we have a problem */
2858 if ((exargs
&& !acargs
&& !IS_VOID (exargs
->type
)) || (!exargs
&& acargs
&& !IS_VOID (acargs
->type
)))
2867 comparePtrType (sym_link
*dest
, sym_link
*src
, bool mustCast
, bool ignoreimplicitintrinsic
)
2872 printf("comparePtrType (must cast %d): ", mustCast
); printTypeChain (dest
, stdout
); printf(" vs. "); printTypeChain (src
, 0);
2875 if (getAddrspace (src
->next
) != getAddrspace (dest
->next
))
2878 if (IS_VOID (src
->next
) && IS_VOID (dest
->next
))
2879 return mustCast
? -1 : 1;
2880 if ((IS_VOID (src
->next
) && !IS_VOID (dest
->next
)) || (!IS_VOID (src
->next
) && IS_VOID (dest
->next
)))
2882 if (IS_STRUCT (src
->next
) && IS_STRUCT (dest
->next
) && SPEC_STRUCT (src
->next
) == SPEC_STRUCT (dest
->next
))
2883 return mustCast
? -1 : 1;
2884 res
= compareType (dest
->next
, src
->next
, ignoreimplicitintrinsic
);
2886 /* All function pointers can be cast (6.3.2.3 in the ISO C23 standard), similar for objects. TODO: What about address spaces? */
2887 if (res
== 0 && !mustCast
&& IS_DECL (src
) && IS_DECL (dest
) && (IS_FUNC (src
->next
) == IS_FUNC (dest
->next
)))
2890 return mustCast
? -1 : 1;
2892 return mustCast
? -1 : -2;
2897 /*--------------------------------------------------------------------*/
2898 /* compareType - will do type check return 1 if match, 0 if no match, */
2899 /* -1 if castable, -2 if only signedness differs */
2900 /* ignoreimplicitintrinsic - ignore implicitly assigned intrinsic named address spaces */
2901 /*--------------------------------------------------------------------*/
2903 compareType (sym_link
*dest
, sym_link
*src
, bool ignoreimplicitintrinsic
)
2915 printf("compareType: "); printTypeChain (dest
, stdout
); printf(" vs. "); printTypeChain (src
, 0);
2918 /* if dest is a declarator then */
2923 // UPOINTER results in false negatives if it reaches here.
2924 wassertl (!IS_PTR (dest
) || dest
->select
.d
.dcl_type
!= UPOINTER
, "UPOINTER is only for use during parsing");
2926 if (IS_GENPTR (dest
) && IS_GENPTR (src
))
2928 /* banked function pointer */
2929 if (IS_FUNC (src
->next
) && IS_VOID (dest
->next
))
2931 if (IS_FUNC (dest
->next
) && IS_VOID (src
->next
))
2933 return comparePtrType (dest
, src
, false, ignoreimplicitintrinsic
);
2936 if (DCL_TYPE (src
) == DCL_TYPE (dest
) ||
2937 (IS_PTR (src
) && ignoreimplicitintrinsic
&& DCL_TYPE_IMPLICITINTRINSIC (src
) || IS_GENPTR (src
)) &&
2938 (IS_PTR (dest
) && ignoreimplicitintrinsic
&& DCL_TYPE_IMPLICITINTRINSIC (dest
) || IS_GENPTR (dest
)))
2942 return compareFuncType (dest
, src
);
2944 return comparePtrType (dest
, src
, false, ignoreimplicitintrinsic
);
2946 if (IS_PTR (dest
) && IS_GENPTR (src
) && IS_VOID (src
->next
))
2950 if (IS_PTR (src
) && (IS_GENPTR (dest
) || ((DCL_TYPE (src
) == POINTER
) && (DCL_TYPE (dest
) == IPOINTER
))))
2952 return comparePtrType (dest
, src
, true, ignoreimplicitintrinsic
);
2954 if (IS_PTR (dest
) && IS_ARRAY (src
))
2956 value
*val
= aggregateToPointer (valFromType (src
));
2957 int res
= compareType (dest
, val
->type
, ignoreimplicitintrinsic
);
2958 Safe_free (val
->type
);
2962 if (IS_PTR (dest
) && IS_FUNC (dest
->next
) && IS_FUNC (src
))
2964 return compareType (dest
->next
, src
, ignoreimplicitintrinsic
);
2966 if (IS_PTR (dest
) && IS_VOID (dest
->next
) && IS_FUNC (src
))
2971 else if (IS_PTR (dest
) && (IS_INTEGRAL (src
) || IS_NULLPTR (src
)))
2977 if (IS_PTR (src
) && (IS_INTEGRAL (dest
) || IS_VOID (dest
)))
2980 if (IS_NULLPTR (src
) && IS_BOOL (dest
))
2983 /* if one is a specifier and the other is not */
2984 if ((IS_SPEC (src
) && !IS_SPEC (dest
)) || (IS_SPEC (dest
) && !IS_SPEC (src
)))
2987 /* if one of them is a void then ok */
2988 if (SPEC_NOUN (dest
) == V_VOID
&& SPEC_NOUN (src
) != V_VOID
)
2991 if (SPEC_NOUN (dest
) != V_VOID
&& SPEC_NOUN (src
) == V_VOID
)
2994 if (SPEC_NOUN (src
) == V_BBITFIELD
&& SPEC_NOUN (dest
) != V_BBITFIELD
|| SPEC_NOUN (src
) != V_BBITFIELD
&& SPEC_NOUN (dest
) == V_BBITFIELD
)
2997 /* if they are both bitfields then if the lengths
2998 and starts don't match */
2999 if (IS_BITFIELD (dest
) && IS_BITFIELD (src
) && (SPEC_BLEN (dest
) != SPEC_BLEN (src
) || SPEC_BSTR (dest
) != SPEC_BSTR (src
)))
3002 if ((SPEC_NOUN (dest
) == V_BITINT
|| SPEC_NOUN (dest
) == V_BITINTBITFIELD
) && (SPEC_NOUN (src
) == V_BITINT
|| SPEC_NOUN (src
) == V_BITINTBITFIELD
))
3004 if (SPEC_BITINTWIDTH (dest
) != SPEC_BITINTWIDTH (src
) ||
3005 SPEC_USIGN (dest
) && !SPEC_USIGN (src
) && SPEC_BITINTWIDTH (dest
) % 8) // Cast from sgined to unsigned type cannot be omitted, since it requires masking top bits.
3007 return (SPEC_USIGN (dest
) == SPEC_USIGN (src
) ? 1 : -2);
3009 else if (IS_ARITHMETIC (dest
) && IS_ARITHMETIC (src
) &&
3010 ((SPEC_NOUN (dest
) == V_BITINT
) ^ (SPEC_NOUN (src
) == V_BITINT
)))
3013 /* it is a specifier */
3014 if (SPEC_NOUN (dest
) != SPEC_NOUN (src
))
3016 if ((SPEC_USIGN (dest
) == SPEC_USIGN (src
)) &&
3017 IS_INTEGRAL (dest
) && IS_INTEGRAL (src
) &&
3019 bitsForType (dest) == bitsForType (src))
3020 instead of the next two lines, but the regression tests fail with
3021 them; I guess it's a problem with replaceCheaperOp */
3022 (getSize (dest
) == getSize (src
)) &&
3023 (IS_BOOLEAN (dest
) == IS_BOOLEAN (src
)))
3025 else if (IS_ARITHMETIC (dest
) && IS_ARITHMETIC (src
))
3030 else if (IS_STRUCT (dest
))
3032 if (SPEC_STRUCT (dest
) != SPEC_STRUCT (src
))
3035 structdef
*destsdef
= SPEC_STRUCT (dest
);
3036 structdef
*srcsdef
= SPEC_STRUCT (src
);
3038 for (symbol
*dstfieldsym
= destsdef
->fields
, *srcfieldsym
= srcsdef
->fields
; srcfieldsym
|| dstfieldsym
; dstfieldsym
= dstfieldsym
->next
, srcfieldsym
= srcfieldsym
->next
)
3040 if (!srcfieldsym
|| !dstfieldsym
)
3042 if (compareType (srcfieldsym
->type
, dstfieldsym
->type
, ignoreimplicitintrinsic
) <= 0)
3049 if (SPEC_SHORT (dest
) != SPEC_SHORT (src
))
3052 if (SPEC_LONG (dest
) != SPEC_LONG (src
))
3055 if (SPEC_LONGLONG (dest
) != SPEC_LONGLONG (src
))
3058 if (SPEC_USIGN (dest
) != SPEC_USIGN (src
))
3064 /*--------------------------------------------------------------------*/
3065 /* compareTypeExact - will do type check return 1 if match exactly */
3066 /*--------------------------------------------------------------------*/
3068 compareTypeExact (sym_link
* dest
, sym_link
* src
, long level
)
3070 STORAGE_CLASS srcScls
, destScls
;
3081 /* if dest is a declarator then */
3086 if (DCL_TYPE (src
) == DCL_TYPE (dest
))
3088 if ((DCL_TYPE (src
) == ARRAY
) && (DCL_ELEM (src
) != DCL_ELEM (dest
)))
3090 if (DCL_PTR_CONST (src
) != DCL_PTR_CONST (dest
))
3092 if (DCL_PTR_VOLATILE (src
) != DCL_PTR_VOLATILE (dest
))
3096 value
*exargs
, *acargs
, *checkValue
;
3098 /* verify function return type */
3099 if (!compareTypeExact (dest
->next
, src
->next
, -1))
3101 if (FUNC_ISISR (dest
) != FUNC_ISISR (src
))
3103 if (FUNC_REGBANK (dest
) != FUNC_REGBANK (src
))
3105 if (IFFUNC_ISNAKED (dest
) != IFFUNC_ISNAKED (src
))
3107 if (IFFUNC_ISBANKEDCALL (dest
) != IFFUNC_ISBANKEDCALL (src
))
3109 if (IFFUNC_ISZ88DK_FASTCALL (dest
) != IFFUNC_ISZ88DK_FASTCALL (src
))
3111 if (IFFUNC_ISRAISONANCE (dest
) != IFFUNC_ISRAISONANCE (src
))
3113 if (IFFUNC_ISCOSMIC (dest
) != IFFUNC_ISCOSMIC (src
))
3115 if (IFFUNC_ISIAR (dest
) != IFFUNC_ISIAR (src
))
3117 if (FUNC_SDCCCALL (dest
) >= 0 && FUNC_SDCCCALL (src
) >= 0 &&
3118 FUNC_SDCCCALL (dest
) != FUNC_SDCCCALL (src
))
3122 if (IFFUNC_ISREENT (dest
) != IFFUNC_ISREENT (src
) && argCnt
> 1)
3126 /* compare expected args with actual args */
3127 exargs
= FUNC_ARGS (dest
);
3128 acargs
= FUNC_ARGS (src
);
3130 /* for all the expected args do */
3131 for (; exargs
&& acargs
; exargs
= exargs
->next
, acargs
= acargs
->next
)
3133 //checkTypeSanity(acargs->etype, acargs->name);
3135 if (IS_AGGREGATE (acargs
->type
))
3137 checkValue
= copyValue (acargs
);
3138 aggregateToPointer (checkValue
);
3141 checkValue
= acargs
;
3144 if (!compareTypeExact (exargs
->type
, checkValue
->type
, -1))
3149 /* if one them ended we have a problem */
3150 if ((exargs
&& !acargs
&& !IS_VOID (exargs
->type
)) || (!exargs
&& acargs
&& !IS_VOID (acargs
->type
)))
3154 return compareTypeExact (dest
->next
, src
->next
, level
);
3161 /* if one is a specifier and the other is not */
3162 if ((IS_SPEC (src
) && !IS_SPEC (dest
)) || (IS_SPEC (dest
) && !IS_SPEC (src
)))
3165 /* if they have a different noun */
3166 if (SPEC_NOUN (dest
) != SPEC_NOUN (src
))
3168 /* if they are both bitfields then if the lengths
3169 and starts don't match */
3170 if (IS_BITFIELD (dest
) && IS_BITFIELD (src
) && (SPEC_BLEN (dest
) != SPEC_BLEN (src
) || SPEC_BSTR (dest
) != SPEC_BSTR (src
)))
3173 if (IS_INTEGRAL (dest
))
3175 /* signedness must match */
3176 if (SPEC_USIGN (dest
) != SPEC_USIGN (src
))
3178 /* size must match */
3179 if (SPEC_SHORT (dest
) != SPEC_SHORT (src
))
3181 if (SPEC_LONG (dest
) != SPEC_LONG (src
))
3183 if (SPEC_LONGLONG (dest
) != SPEC_LONGLONG (src
))
3185 // width must be the same for bit-precise types
3186 if (SPEC_NOUN (dest
) == V_BITINT
&& SPEC_BITINTWIDTH (dest
) != SPEC_BITINTWIDTH (src
))
3190 if (IS_STRUCT (dest
))
3192 if (SPEC_STRUCT (dest
) != SPEC_STRUCT (src
))
3196 if (SPEC_CONST (dest
) != SPEC_CONST (src
))
3198 if (SPEC_VOLATILE (dest
) != SPEC_VOLATILE (src
))
3200 if (SPEC_STAT (dest
) != SPEC_STAT (src
))
3202 if (SPEC_ABSA (dest
) != SPEC_ABSA (src
))
3204 if (SPEC_ABSA (dest
) && SPEC_ADDR (dest
) != SPEC_ADDR (src
))
3207 destScls
= SPEC_SCLS (dest
);
3208 srcScls
= SPEC_SCLS (src
);
3210 /* Compensate for const to const code change in checkSClass() */
3211 if (((!level
) & port
->mem
.code_ro
) && SPEC_CONST (dest
))
3213 if (srcScls
== S_CODE
&& destScls
== S_FIXED
)
3215 if (destScls
== S_CODE
&& srcScls
== S_FIXED
)
3219 /* compensate for allocGlobal() */
3220 if ((srcScls
== S_FIXED
|| srcScls
== S_AUTO
) &&
3221 (port
->mem
.default_globl_map
== xdata
) && (destScls
== S_XDATA
) && (level
<= 0))
3226 if ((level
> 0) && !SPEC_STAT (dest
))
3228 /* Compensate for hack-o-matic in checkSClass() */
3229 if (options
.stackAuto
|| (currFunc
&& IFFUNC_ISREENT (currFunc
->type
)))
3231 if (destScls
== S_FIXED
)
3232 destScls
= (options
.useXstack
? S_XSTACK
: S_STACK
);
3233 if (srcScls
== S_FIXED
)
3234 srcScls
= (options
.useXstack
? S_XSTACK
: S_STACK
);
3236 else if (TARGET_IS_DS390
|| TARGET_IS_DS400
|| options
.useXstack
|| TARGET_IS_HC08
|| TARGET_IS_S08
)
3238 if (destScls
== S_FIXED
)
3240 if (srcScls
== S_FIXED
)
3245 if (srcScls
!= destScls
)
3248 printf ("level = %ld:%ld\n", level
/ LEVEL_UNIT
, level
% LEVEL_UNIT
);
3249 printf ("SPEC_SCLS (src) = %d, SPEC_SCLS (dest) = %d\n", SPEC_SCLS (src
), SPEC_SCLS (dest
));
3250 printf ("srcScls = %d, destScls = %d\n", srcScls
, destScls
);
3258 /*---------------------------------------------------------------------------*/
3259 /* compareTypeInexact - will do type check return 1 if representation is same. */
3260 /* Useful for redundancy elimination. */
3261 /*---------------------------------------------------------------------------*/
3263 compareTypeInexact (sym_link
*dest
, sym_link
*src
)
3274 if (IS_BITFIELD (dest
) != IS_BITFIELD (src
))
3277 if (IS_BITFIELD (dest
) && IS_BITFIELD (src
) && (SPEC_BLEN (dest
) != SPEC_BLEN (src
) || SPEC_BSTR (dest
) != SPEC_BSTR (src
)))
3280 if (getSize (dest
) != getSize (src
))
3286 /*------------------------------------------------------------------*/
3287 /* inCalleeSaveList - return 1 if found in callee save list */
3288 /*------------------------------------------------------------------*/
3290 calleeCmp (void *p1
, void *p2
)
3292 return (strcmp ((char *) p1
, (char *) (p2
)) == 0);
3296 inCalleeSaveList (char *s
)
3298 if (options
.all_callee_saves
)
3300 return isinSetWith (options
.calleeSavesSet
, s
, calleeCmp
);
3303 /*-----------------------------------------------------------------*/
3304 /* aggregateToPointer: change an aggregate type function */
3305 /* argument to a pointer to that type. */
3306 /*-----------------------------------------------------------------*/
3308 aggregateToPointer (value
*val
)
3310 if (IS_ARRAY (val
->type
))
3312 /* change to a pointer depending on the */
3313 /* storage class specified */
3314 switch (SPEC_SCLS (val
->etype
))
3317 DCL_TYPE (val
->type
) = IPOINTER
;
3320 DCL_TYPE (val
->type
) = PPOINTER
;
3323 if (SPEC_OCLS (val
->etype
))
3325 DCL_TYPE (val
->type
) = PTR_TYPE (SPEC_OCLS (val
->etype
));
3328 { // this happens for (external) function parameters
3329 DCL_TYPE (val
->type
) = port
->unqualified_pointer
;
3333 DCL_TYPE (val
->type
) = PTR_TYPE (SPEC_OCLS (val
->etype
));
3337 DCL_TYPE (val
->type
) = POINTER
;
3340 DCL_TYPE (val
->type
) = CPOINTER
;
3343 DCL_TYPE (val
->type
) = FPOINTER
;
3346 DCL_TYPE (val
->type
) = EEPPOINTER
;
3349 DCL_TYPE (val
->type
) = port
->unqualified_pointer
;
3351 DCL_TYPE_IMPLICITINTRINSIC (val
->type
) = SPEC_SCLS_IMPLICITINTRINSIC (val
->etype
);
3353 /* is there is a symbol associated then */
3354 /* change the type of the symbol as well */
3357 val
->sym
->type
= copyLinkChain (val
->type
);
3358 val
->sym
->etype
= getSpec (val
->sym
->type
);
3364 /*------------------------------------------------------------------*/
3365 /* checkFunction - does all kinds of check on a function */
3366 /*------------------------------------------------------------------*/
3368 checkFunction (symbol
* sym
, symbol
* csym
)
3370 value
*exargs
, *acargs
;
3374 if (getenv ("DEBUG_SANITY"))
3376 fprintf (stderr
, "checkFunction: %s ", sym
->name
);
3379 if (!IS_FUNC (sym
->type
))
3381 werrorfl (sym
->fileDef
, sym
->lineDef
, E_SYNTAX_ERROR
, sym
->name
);
3385 /* move function specifier from return type to function attributes */
3386 if (IS_INLINE (sym
->etype
))
3388 SPEC_INLINE (sym
->etype
) = 0;
3389 FUNC_ISINLINE (sym
->type
) = 1;
3391 if (IS_NORETURN (sym
->etype
))
3393 SPEC_NORETURN (sym
->etype
) = 0;
3394 FUNC_ISNORETURN (sym
->type
) = 1;
3397 /* If no ABI version specified, use port default */
3398 if (FUNC_SDCCCALL (sym
->type
) < 0)
3399 FUNC_SDCCCALL (sym
->type
) = options
.sdcccall
;
3401 /* make sure the type is complete and sane */
3402 checkTypeSanity (sym
->etype
, sym
->name
);
3404 /* if not type then some kind of error */
3408 /* if the function has no type then make it return int */
3409 if (!sym
->type
->next
)
3410 sym
->type
->next
= sym
->etype
= newIntLink ();
3412 /* function cannot return aggregate */
3413 if ((TARGET_IS_DS390
) && IS_AGGREGATE (sym
->type
->next
))
3415 werrorfl (sym
->fileDef
, sym
->lineDef
, E_FUNC_AGGR
, sym
->name
);
3419 /* check if this function is defined as calleeSaves
3420 then mark it as such */
3421 FUNC_CALLEESAVES (sym
->type
) = inCalleeSaveList (sym
->name
);
3423 /* if interrupt service routine */
3424 /* then it cannot have arguments */
3425 if (IFFUNC_ARGS (sym
->type
) && FUNC_ISISR (sym
->type
))
3427 if (!IS_VOID (FUNC_ARGS (sym
->type
)->type
))
3429 werrorfl (sym
->fileDef
, sym
->lineDef
, E_INT_ARGS
, sym
->name
);
3430 FUNC_ARGS (sym
->type
) = NULL
;
3434 if (IFFUNC_ISSHADOWREGS (sym
->type
) && !FUNC_ISISR (sym
->type
))
3436 werrorfl (sym
->fileDef
, sym
->lineDef
, E_SHADOWREGS_NO_ISR
, sym
->name
);
3439 for (argCnt
= 1, acargs
= FUNC_ARGS (sym
->type
); acargs
; acargs
= acargs
->next
, argCnt
++)
3443 // this can happen for reentrant functions
3444 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PARAM_NAME_OMITTED
, sym
->name
, argCnt
);
3445 // the show must go on: synthesize a name and symbol
3446 SNPRINTF (acargs
->name
, sizeof (acargs
->name
), "_%s_PARM_%d", sym
->name
, argCnt
);
3447 acargs
->sym
= newSymbol (acargs
->name
, 1);
3448 SPEC_OCLS (acargs
->etype
) = istack
;
3449 acargs
->sym
->type
= copyLinkChain (acargs
->type
);
3450 acargs
->sym
->etype
= getSpec (acargs
->sym
->type
);
3451 acargs
->sym
->_isparm
= 1;
3452 strncpyz (acargs
->sym
->rname
, acargs
->name
, sizeof (acargs
->sym
->rname
));
3454 else if (strcmp (acargs
->sym
->name
, acargs
->sym
->rname
) == 0)
3457 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PARAM_NAME_OMITTED
, sym
->name
, argCnt
);
3462 /*JCF: Mark the register bank as used */
3463 RegBankUsed
[FUNC_REGBANK (sym
->type
)] = 1;
3465 if (!csym
&& !(csym
= findSymWithLevel (SymbolTab
, sym
)))
3466 return 1; /* not defined nothing more to check */
3468 /* check if body already present */
3469 if (csym
&& IFFUNC_HASBODY (csym
->type
))
3471 werrorfl (sym
->fileDef
, sym
->lineDef
, E_FUNC_BODY
, sym
->name
);
3475 /* check the return value type */
3476 if (FUNC_NOPROTOTYPE (csym
->type
))
3478 if (compareType (csym
->type
->next
, sym
->type
->next
, false) <= 0)
3480 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "return type", csym
->fileDef
, csym
->lineDef
);
3481 printFromToType (csym
->type
->next
, sym
->type
->next
);
3485 else if (compareType (csym
->type
, sym
->type
, false) <= 0)
3487 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "type", csym
->fileDef
, csym
->lineDef
);
3488 printFromToType (csym
->type
, sym
->type
);
3492 if (FUNC_ISISR (csym
->type
) != FUNC_ISISR (sym
->type
))
3493 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "interrupt", csym
->fileDef
, csym
->lineDef
);
3495 /* I don't think this is necessary for interrupts. An isr is a */
3496 /* root in the calling tree. */
3497 if ((FUNC_REGBANK (csym
->type
) != FUNC_REGBANK (sym
->type
)) && (!FUNC_ISISR (sym
->type
)))
3498 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "using", csym
->fileDef
, csym
->lineDef
);
3500 if (IFFUNC_ISNAKED (csym
->type
) != IFFUNC_ISNAKED (sym
->type
))
3502 // disabled since __naked has no influence on the calling convention
3503 // werror (E_PREV_DECL_CONFLICT, csym->name, "__naked", csym->fileDef, csym->lineDef);
3504 FUNC_ISNAKED (sym
->type
) = 1;
3507 if (FUNC_BANKED (csym
->type
) || FUNC_BANKED (sym
->type
))
3509 if (FUNC_NONBANKED (csym
->type
) || FUNC_NONBANKED (sym
->type
))
3511 werrorfl (sym
->fileDef
, sym
->lineDef
, W_BANKED_WITH_NONBANKED
);
3512 FUNC_BANKED (sym
->type
) = 0;
3513 FUNC_NONBANKED (sym
->type
) = 1;
3517 FUNC_BANKED (sym
->type
) = 1;
3522 if (FUNC_NONBANKED (csym
->type
) || FUNC_NONBANKED (sym
->type
))
3524 FUNC_NONBANKED (sym
->type
) = 1;
3528 /* Really, reentrant should match regardless of argCnt, but */
3529 /* this breaks some existing code (the fp lib functions). If */
3530 /* the first argument is always passed the same way, this */
3531 /* lax checking is ok (but may not be true for in future backends) */
3532 if (IFFUNC_ISREENT (csym
->type
) != IFFUNC_ISREENT (sym
->type
) && argCnt
> 1)
3534 //printf("argCnt = %d\n",argCnt);
3535 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "reentrant", csym
->fileDef
, csym
->lineDef
);
3538 if (IFFUNC_ISWPARAM (csym
->type
) != IFFUNC_ISWPARAM (sym
->type
))
3539 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "wparam", csym
->fileDef
, csym
->lineDef
);
3541 if (IFFUNC_ISSHADOWREGS (csym
->type
) != IFFUNC_ISSHADOWREGS (sym
->type
))
3542 werrorfl (sym
->fileDef
, sym
->lineDef
, E_PREV_DECL_CONFLICT
, csym
->name
, "shadowregs", csym
->fileDef
, csym
->lineDef
);
3544 /* compare expected args with actual args */
3545 exargs
= FUNC_ARGS (csym
->type
);
3546 acargs
= FUNC_ARGS (sym
->type
);
3548 /* for all the expected args do */
3549 for (argCnt
= 1; exargs
&& acargs
; exargs
= exargs
->next
, acargs
= acargs
->next
, argCnt
++)
3551 if (getenv ("DEBUG_SANITY"))
3553 fprintf (stderr
, "checkFunction: %s ", exargs
->name
);
3555 /* make sure the type is complete and sane */
3556 checkTypeSanity (exargs
->etype
, exargs
->name
);
3558 /* If the actual argument is an array, any prototype
3559 * will have modified it to a pointer. Duplicate that
3562 if (IS_AGGREGATE (acargs
->type
))
3564 checkValue
= copyValue (acargs
);
3565 aggregateToPointer (checkValue
);
3569 checkValue
= acargs
;
3572 if (compareType (exargs
->type
, checkValue
->type
, false) <= 0)
3574 werror (E_ARG_TYPE
, argCnt
);
3575 printFromToType (exargs
->type
, checkValue
->type
);
3580 /* if one of them ended we have a problem */
3581 if (((exargs
&& !acargs
&& !IS_VOID (exargs
->type
)) || (!exargs
&& acargs
&& !IS_VOID (acargs
->type
))) && !FUNC_NOPROTOTYPE (csym
->type
))
3582 werror (E_ARG_COUNT
);
3584 /* replace with this definition */
3585 sym
->cdef
= csym
->cdef
;
3586 deleteSym (SymbolTab
, csym
, csym
->name
);
3587 deleteFromSeg (csym
);
3588 addSym (SymbolTab
, sym
, sym
->name
, sym
->level
, sym
->block
, 1);
3589 if (IS_EXTERN (csym
->etype
) && !IS_EXTERN (sym
->etype
))
3591 SPEC_EXTR (sym
->etype
) = 1;
3592 addSet (&publics
, sym
);
3595 SPEC_STAT (sym
->etype
) |= SPEC_STAT (csym
->etype
);
3596 if (SPEC_STAT (sym
->etype
) && SPEC_EXTR (sym
->etype
))
3598 werrorfl (sym
->fileDef
, sym
->lineDef
, E_TWO_OR_MORE_STORAGE_CLASSES
, sym
->name
);
3604 /*------------------------------------------------------------------*/
3605 /* cdbStructBlock - calls struct printing for a blocks */
3606 /*------------------------------------------------------------------*/
3608 cdbStructBlock (int block
)
3611 bucket
**table
= StructTab
;
3614 /* go thru the entire table */
3615 for (i
= 0; i
< HASHTAB_SIZE
; i
++)
3617 for (chain
= table
[i
]; chain
; chain
= chain
->next
)
3619 if (chain
->block
>= block
)
3622 debugFile
->writeType ((structdef
*) chain
->sym
, chain
->block
, 0, NULL
);
3628 /*-----------------------------------------------------------------*/
3629 /* processFuncPtrArgs - does some processing with args of func ptrs*/
3630 /*-----------------------------------------------------------------*/
3632 processFuncPtrArgs (sym_link
* funcType
)
3634 processFuncArgs (NULL
, funcType
);
3637 /*-----------------------------------------------------------------*/
3638 /* processFuncArgs - does some processing with function args */
3640 /* Leave func NULL if processing a type rather than a symbol */
3641 /*-----------------------------------------------------------------*/
3643 processFuncArgs (symbol
*func
, sym_link
*funcType
)
3647 char *funcName
= NULL
;
3650 if (func
&& !funcType
)
3651 funcType
= func
->type
;
3654 funcCdef
= func
->cdef
;
3655 funcName
= func
->name
;
3660 funcName
= "unnamed function type";
3663 if (getenv ("SDCC_DEBUG_FUNCTION_POINTERS"))
3664 fprintf (stderr
, "SDCCsymt.c:processFuncArgs(%s)\n", funcName
);
3666 /* find the function declaration within the type */
3667 while (funcType
&& !IS_FUNC (funcType
))
3668 funcType
= funcType
->next
;
3670 /* Nothing to do if no function type found */
3674 /* if this function has variable argument list */
3675 /* then make the function a reentrant one */
3676 if (IFFUNC_HASVARARGS (funcType
) || (options
.stackAuto
&& !funcCdef
))
3677 FUNC_ISREENT (funcType
) = 1;
3679 /* check if this function is defined as calleeSaves
3680 then mark it as such */
3681 FUNC_CALLEESAVES (funcType
) = inCalleeSaveList (funcName
);
3683 /* If no ABI version specified, use port default */
3684 if (FUNC_SDCCCALL (funcType
) < 0)
3685 FUNC_SDCCCALL (funcType
) = options
.sdcccall
;
3687 /* loop thru all the arguments */
3688 val
= FUNC_ARGS (funcType
);
3690 /* if it is void then remove parameters */
3691 if (val
&& IS_VOID (val
->type
))
3693 FUNC_ARGS (funcType
) = NULL
;
3697 /* reset regparm for the port */
3698 (*port
->reset_regparms
) (funcType
);
3700 /* if any of the arguments is an aggregate */
3701 /* change it to pointer to the same type */
3702 for (; val
; val
=val
->next
, pNum
++)
3708 for (value
*val2
= val
->next
; val2
; val2
= val2
->next
)
3709 if (val2
->sym
&& !strcmp (val
->sym
->name
, val2
->sym
->name
))
3710 werror (E_DUPLICATE_PARAMTER_NAME
, val
->sym
->name
, funcName
);
3712 dbuf_init (&dbuf
, 128);
3713 dbuf_printf (&dbuf
, "%s parameter %d", funcName
, pNum
);
3714 checkTypeSanity (val
->etype
, dbuf_c_str (&dbuf
));
3715 dbuf_destroy (&dbuf
);
3717 if (IS_AGGREGATE (val
->type
))
3719 aggregateToPointer (val
);
3722 /* mark it as a register parameter if
3723 the function does not have VA_ARG
3724 and as port dictates */
3725 if (argreg
= (*port
->reg_parm
) (val
->type
, FUNC_ISREENT (funcType
)))
3727 SPEC_REGPARM (val
->etype
) = 1;
3728 SPEC_ARGREG (val
->etype
) = argreg
;
3730 /* is there is a symbol associated then */
3731 /* change the type of the symbol as well */
3734 SPEC_REGPARM (val
->sym
->etype
) = 1;
3735 SPEC_ARGREG (val
->sym
->etype
) = argreg
;
3738 else if (IFFUNC_ISREENT (funcType
))
3740 FUNC_HASSTACKPARM (funcType
) = 1;
3743 /* if this is an internal generated function call */
3746 /* ignore --stack-auto for this one, we don't know how it is compiled */
3747 /* simply trust on --int-long-reent or --float-reent */
3748 if (IFFUNC_ISREENT (funcType
))
3753 /* if this function is reentrant or */
3754 /* automatics r 2b stacked then nothing */
3755 if (IFFUNC_ISREENT (funcType
) || options
.stackAuto
)
3759 /* Don't create parameter symbols without a function symbol */
3763 /* if a symbolname is not given */
3764 /* synthesize a variable name */
3767 SNPRINTF (val
->name
, sizeof (val
->name
), "_%s_PARM_%d", func
->name
, pNum
);
3768 val
->sym
= newSymbol (val
->name
, 1);
3769 val
->sym
->type
= copyLinkChain (val
->type
);
3770 val
->sym
->etype
= getSpec (val
->sym
->type
);
3771 val
->sym
->_isparm
= 1;
3772 if (!defaultOClass (val
->sym
))
3773 SPEC_OCLS (val
->sym
->etype
) = port
->mem
.default_local_map
;
3774 SPEC_OCLS (val
->etype
) = SPEC_OCLS (val
->sym
->etype
);
3775 strncpyz (val
->sym
->rname
, val
->name
, sizeof (val
->sym
->rname
));
3776 addSymChain (&val
->sym
);
3778 else /* symbol name given create synth name */
3780 SNPRINTF (val
->name
, sizeof (val
->name
), "_%s_PARM_%d", func
->name
, pNum
);
3781 strncpyz (val
->sym
->rname
, val
->name
, sizeof (val
->sym
->rname
));
3782 val
->sym
->_isparm
= 1;
3783 if (!defaultOClass (val
->sym
))
3784 SPEC_OCLS (val
->sym
->etype
) = port
->mem
.default_local_map
;
3785 SPEC_OCLS (val
->etype
) = SPEC_OCLS (val
->sym
->etype
);
3787 if (SPEC_OCLS (val
->sym
->etype
) == pdata
)
3788 val
->sym
->iaccess
= 1;
3789 if (!isinSet (operKeyReset
, val
->sym
))
3791 addSet (&operKeyReset
, val
->sym
);
3792 applyToSet (operKeyReset
, resetParmKey
);
3797 /*-----------------------------------------------------------------*/
3798 /* isSymbolEqual - compares two symbols return 1 if they match */
3799 /*-----------------------------------------------------------------*/
3801 isSymbolEqual (const symbol
* dest
, const symbol
* src
)
3803 /* if pointers match then equal */
3807 /* if one of them is null then don't match */
3811 /* if both of them have rname match on rname */
3812 if (dest
->rname
[0] && src
->rname
[0])
3813 return (!strcmp (dest
->rname
, src
->rname
));
3815 /* otherwise match on name */
3816 return (!strcmp (dest
->name
, src
->name
));
3820 PT (sym_link
* type
)
3822 printTypeChain (type
, 0);
3825 /*-----------------------------------------------------------------*/
3826 /* printTypeChain - prints the type chain in human readable form */
3827 /*-----------------------------------------------------------------*/
3829 printTypeChain (sym_link
* start
, FILE * of
)
3840 dbuf_init (&dbuf
, 1024);
3841 dbuf_printTypeChain (start
, &dbuf
);
3842 dbuf_write_and_destroy (&dbuf
, of
);
3849 dbuf_printTypeChain (sym_link
* start
, struct dbuf_s
*dbuf
)
3852 sym_link
*type
, *search
;
3854 static struct dbuf_s dbuf2
;
3858 dbuf_append_str (dbuf
, "void");
3862 /* Print the chain as it is written in the source: */
3863 /* start with the last entry. */
3864 /* However, the storage class at the end of the */
3865 /* chain really applies to the first in the chain! */
3867 for (type
= start
; type
&& type
->next
; type
= type
->next
)
3870 scls
= SPEC_SCLS (type
);
3877 switch (DCL_TYPE (type
))
3880 dbuf_printf (dbuf
, "function %s%s",
3881 (IFFUNC_ISBUILTIN (type
) ? "__builtin__ " : ""),
3882 (IFFUNC_ISJAVANATIVE (type
) ? "_JavaNative " : ""));
3883 dbuf_append_str (dbuf
, "( ");
3884 if (!FUNC_ARGS (type
) && !FUNC_HASVARARGS(type
) && !FUNC_NOPROTOTYPE(type
))
3885 dbuf_append_str (dbuf
, "void ");
3886 for (args
= FUNC_ARGS (type
); args
; args
= args
->next
)
3888 dbuf_printTypeChain (args
->type
, dbuf
);
3889 if (args
->next
|| FUNC_HASVARARGS(type
))
3890 dbuf_append_str (dbuf
, ", ");
3892 if (FUNC_HASVARARGS(type
))
3893 dbuf_append_str (dbuf
, "...");
3894 dbuf_append_str (dbuf
, ")");
3895 if (IFFUNC_ISREENT (type
) && isTargetKeyword("__reentrant"))
3896 dbuf_append_str (dbuf
, " __reentrant");
3897 if (FUNC_REGBANK (type
))
3899 dbuf_set_length (&dbuf2
, 0);
3900 dbuf_printf (&dbuf2
, " __using(%d)", FUNC_REGBANK (type
));
3901 dbuf_append_str (dbuf
, dbuf_c_str (&dbuf2
));
3903 if (IFFUNC_ISBANKEDCALL (type
))
3904 dbuf_append_str (dbuf
, " __banked");
3905 if (IFFUNC_ISSMALLC (type
))
3906 dbuf_append_str (dbuf
, " __smallc");
3907 if (IFFUNC_ISRAISONANCE (type
))
3908 dbuf_append_str (dbuf
, " __raisonance");
3909 if (IFFUNC_ISIAR (type
))
3910 dbuf_append_str (dbuf
, " __iar");
3911 if (IFFUNC_ISCOSMIC (type
))
3912 dbuf_append_str (dbuf
, " __cosmic");
3913 if (IFFUNC_ISZ88DK_CALLEE (type
))
3914 dbuf_append_str (dbuf
, " __z88dk_callee");
3915 if (IFFUNC_ISZ88DK_FASTCALL (type
))
3916 dbuf_append_str (dbuf
, " __z88dk_fastcall");
3917 if (FUNC_SDCCCALL (type
) >= 0 && FUNC_SDCCCALL (type
) != options
.sdcccall
)
3918 dbuf_printf (dbuf
, " __sdcccall(%d)", FUNC_SDCCCALL (type
));
3919 for (unsigned char i
= 0; i
< 9; i
++)
3920 if (type
->funcAttrs
.preserved_regs
[i
])
3922 dbuf_append_str (dbuf
, " __preserves_regs(");
3924 if (type
->funcAttrs
.preserved_regs
[i
])
3925 dbuf_printf (dbuf
, " %d", i
);
3926 dbuf_append_str (dbuf
, " )");
3931 if (type
->next
&& !IS_DECL (type
->next
) && SPEC_ADDRSPACE (type
->next
))
3932 dbuf_printf (dbuf
, "%s*", SPEC_ADDRSPACE (type
->next
)->name
);
3934 dbuf_append_str (dbuf
, "generic*");
3937 dbuf_append_str (dbuf
, "__code*");
3940 dbuf_append_str (dbuf
, "__xdata*");
3943 dbuf_append_str (dbuf
, "eeprom*");
3946 dbuf_append_str (dbuf
, "near*");
3949 dbuf_append_str (dbuf
, "__idata*");
3952 dbuf_append_str (dbuf
, "__pdata*");
3955 dbuf_append_str (dbuf
, "unknown*");
3958 if (DCL_ELEM (type
))
3960 dbuf_printf (dbuf
, "[%u]", (unsigned int) DCL_ELEM (type
));
3964 dbuf_append_str (dbuf
, "[]");
3968 dbuf_append_str (dbuf
, "unknown?");
3971 if (!IS_FUNC (type
))
3973 if (DCL_PTR_VOLATILE (type
))
3975 dbuf_append_str (dbuf
, " volatile");
3977 if (DCL_PTR_CONST (type
))
3979 dbuf_append_str (dbuf
, " const");
3981 if (DCL_PTR_RESTRICT (type
))
3983 dbuf_append_str (dbuf
, " restrict");
3989 if (SPEC_VOLATILE (type
))
3990 dbuf_append_str (dbuf
, "volatile-");
3991 if (SPEC_CONST (type
))
3992 dbuf_append_str (dbuf
, "const-");
3993 if (SPEC_NOUN (type
) == V_CHAR
) // char is a different type from both unsigned char and signed char
3995 if (!getSpec (type
)->select
.s
.b_implicit_sign
)
3996 dbuf_append_str (dbuf
, SPEC_USIGN (type
) ? "unsigned-" : "signed-");
3998 else if (SPEC_USIGN (type
))
3999 dbuf_append_str (dbuf
, "unsigned-");
4000 switch (SPEC_NOUN (type
))
4003 if (IS_LONGLONG (type
))
4004 dbuf_append_str (dbuf
, "longlong-");
4005 else if (IS_LONG (type
))
4006 dbuf_append_str (dbuf
, "long-");
4007 dbuf_append_str (dbuf
, "int");
4011 dbuf_printf (dbuf
, "_BitInt(%u)", SPEC_BITINTWIDTH (type
));
4015 dbuf_append_str (dbuf
, "_Bool");
4019 dbuf_append_str (dbuf
, "char");
4023 dbuf_append_str (dbuf
, "void");
4027 dbuf_append_str (dbuf
, "float");
4031 dbuf_append_str (dbuf
, "fixed16x16");
4035 dbuf_printf (dbuf
, "struct %s", SPEC_STRUCT (type
)->tag
);
4039 dbuf_append_str (dbuf
, "sbit");
4043 dbuf_append_str (dbuf
, "bit");
4047 dbuf_printf (dbuf
, "int-bitfield {%d,%d}", SPEC_BSTR (type
), SPEC_BLEN (type
));
4051 dbuf_printf (dbuf
, "_Bool-bitfield {%d,%d}", SPEC_BSTR (type
), SPEC_BLEN (type
));
4054 case V_BITINTBITFIELD
:
4055 dbuf_printf (dbuf
, "_BitInt(%d)-bitfield {%d,%d}", SPEC_BITINTWIDTH (type
), SPEC_BSTR (type
), SPEC_BLEN (type
));
4059 dbuf_append_str (dbuf
, "double");
4063 dbuf_append_str (dbuf
, "nullptr_t");
4067 dbuf_append_str (dbuf
, "unknown type");
4076 dbuf_append_str (dbuf
, " fixed");
4079 dbuf_append_str (dbuf
, " auto");
4082 dbuf_append_str (dbuf
, " register");
4085 dbuf_append_str (dbuf
, " data");
4088 dbuf_append_str (dbuf
, " __xdata");
4091 dbuf_append_str (dbuf
, " sfr");
4094 dbuf_append_str (dbuf
, " __sbit");
4097 dbuf_append_str (dbuf
, " __code");
4100 dbuf_append_str (dbuf
, " __idata");
4103 dbuf_append_str (dbuf
, " __pdata");
4106 dbuf_append_str (dbuf
, " literal");
4109 dbuf_append_str (dbuf
, " stack");
4112 dbuf_append_str (dbuf
, " xstack");
4115 dbuf_append_str (dbuf
, " __bit");
4118 dbuf_append_str (dbuf
, " eeprom");
4125 /* search entry in list before "type" */
4126 for (search
= start
; search
&& search
->next
!= type
;)
4127 search
= search
->next
;
4130 dbuf_append_char (dbuf
, ' ');
4134 /*--------------------------------------------------------------------*/
4135 /* printTypeChainRaw - prints the type chain in human readable form */
4136 /* in the raw data structure ordering */
4137 /*--------------------------------------------------------------------*/
4139 printTypeChainRaw (sym_link
* start
, FILE * of
)
4153 fprintf (of
, "void");
4163 if (!IS_FUNC (type
))
4165 if (DCL_PTR_VOLATILE (type
))
4167 fprintf (of
, "volatile-");
4169 if (DCL_PTR_CONST (type
))
4171 fprintf (of
, "const-");
4173 if (DCL_PTR_RESTRICT (type
))
4175 fprintf (of
, "restrict-");
4178 switch (DCL_TYPE (type
))
4181 if (IFFUNC_ISINLINE (type
))
4183 fprintf (of
, "inline-");
4185 if (IFFUNC_ISNORETURN (type
))
4187 fprintf (of
, "_Noreturn-");
4189 fprintf (of
, "function %s %s",
4190 (IFFUNC_ISBUILTIN (type
) ? "__builtin__" : " "), (IFFUNC_ISJAVANATIVE (type
) ? "_JavaNative" : " "));
4192 for (args
= FUNC_ARGS (type
); args
; args
= args
->next
)
4194 printTypeChain (args
->type
, of
);
4201 fprintf (of
, "generic* ");
4204 fprintf (of
, "code* ");
4207 fprintf (of
, "xdata* ");
4210 fprintf (of
, "eeprom* ");
4213 fprintf (of
, "near* ");
4216 fprintf (of
, "idata* ");
4219 fprintf (of
, "pdata* ");
4222 fprintf (of
, "unknown* ");
4225 if (DCL_ELEM (type
))
4227 fprintf (of
, "[%ud] ", (unsigned int) DCL_ELEM (type
));
4231 fprintf (of
, "[] ");
4235 if (DCL_TSPEC (type
))
4238 printTypeChainRaw (DCL_TSPEC (type
), of
);
4242 else if (IS_SPEC (type
))
4244 switch (SPEC_SCLS (type
))
4247 fprintf (of
, "data-");
4250 fprintf (of
, "xdata-");
4253 fprintf (of
, "sfr-");
4256 fprintf (of
, "sbit-");
4259 fprintf (of
, "code-");
4262 fprintf (of
, "idata-");
4265 fprintf (of
, "pdata-");
4268 fprintf (of
, "literal-");
4271 fprintf (of
, "stack-");
4274 fprintf (of
, "xstack-");
4277 fprintf (of
, "bit-");
4280 fprintf (of
, "eeprom-");
4285 if (SPEC_VOLATILE (type
))
4286 fprintf (of
, "volatile-");
4287 if (SPEC_CONST (type
))
4288 fprintf (of
, "const-");
4289 if (SPEC_USIGN (type
))
4290 fprintf (of
, "unsigned-");
4291 switch (SPEC_NOUN (type
))
4294 if (IS_LONGLONG (type
))
4295 fprintf (of
, "longlong-");
4296 else if (IS_LONG (type
))
4297 fprintf (of
, "long-");
4298 fprintf (of
, "int");
4302 fprintf (of
, "_Bool");
4306 fprintf (of
, "char");
4310 fprintf (of
, "void");
4314 fprintf (of
, "float");
4318 fprintf (of
, "fixed16x16");
4322 fprintf (of
, "struct %s", SPEC_STRUCT (type
)->tag
);
4326 fprintf (of
, "sbit");
4330 fprintf (of
, "bit");
4334 fprintf (of
, "bitfield {%d,%d}", SPEC_BSTR (type
), SPEC_BLEN (type
));
4338 fprintf (of
, "_Boolbitfield {%d,%d}", SPEC_BSTR (type
), SPEC_BLEN (type
));
4341 case V_BITINTBITFIELD
:
4342 fprintf (of
, "_BitIntbitfield {%d,%d}", SPEC_BSTR (type
), SPEC_BLEN (type
));
4346 fprintf (of
, "double");
4350 fprintf (of
, "unknown type");
4355 fprintf (of
, "NOT_SPEC_OR_DECL");
4365 /*-----------------------------------------------------------------*/
4366 /* powof2 - returns power of two for the number if number is pow 2 */
4367 /*-----------------------------------------------------------------*/
4369 powof2 (TYPE_TARGET_ULONGLONG num
)
4382 if (n1s
> 1 || nshifts
== 0)
4395 symbol
*fps16x16_add
;
4396 symbol
*fps16x16_sub
;
4397 symbol
*fps16x16_mul
;
4398 symbol
*fps16x16_div
;
4399 symbol
*fps16x16_eq
;
4400 symbol
*fps16x16_neq
;
4401 symbol
*fps16x16_lt
;
4402 symbol
*fps16x16_lteq
;
4403 symbol
*fps16x16_gt
;
4404 symbol
*fps16x16_gteq
;
4406 /* Dims: mul/div/mod, BYTE/WORD/DWORD/QWORD, SIGNED/UNSIGNED/BOTH */
4407 symbol
*muldiv
[3][4][4];
4408 symbol
*muls16tos32
[2];
4409 symbol
*mulu32u8tou64
;
4410 /* Dims: BYTE/WORD/DWORD/QWORD SIGNED/UNSIGNED */
4411 sym_link
*multypes
[4][2];
4412 /* Dims: to/from float, BYTE/WORD/DWORD/QWORD, SIGNED/UNSIGNED */
4413 symbol
*conv
[2][4][2];
4414 /* Dims: to/from fixed16x16, BYTE/WORD/DWORD/QWORD/FLOAT, SIGNED/UNSIGNED */
4415 symbol
*fp16x16conv
[2][5][2];
4416 /* Dims: shift left/shift right, BYTE/WORD/DWORD/QWORD, SIGNED/UNSIGNED */
4417 symbol
*rlrr
[2][4][2];
4419 sym_link
*floatType
;
4420 sym_link
*fixed16x16Type
;
4422 symbol
*builtin_memcpy
;
4423 symbol
*nonbuiltin_memcpy
;
4424 symbol
*builtin_unreachable
;
4427 _mangleFunctionName (const char *in
)
4429 if (port
->getMangledFunctionName
)
4431 return port
->getMangledFunctionName (in
);
4439 /*-----------------------------------------------------------------*/
4440 /* typeFromStr - create a typechain from an encoded string */
4441 /* basic types - 'b' - bool */
4446 /* 'L' - long long */
4448 /* 'q' - fixed16x16 */
4450 /* '*' - pointer - default (GPOINTER) */
4451 /* modifiers - 'S' - signed */
4452 /* 'U' - unsigned */
4454 /* pointer modifiers - 'g' - generic */
4458 /* 'F' - function */
4459 /* examples : "ig*" - generic int * */
4460 /* "cx*" - char xdata * */
4461 /* "Ui" - unsigned int */
4462 /* "Sc" - signed char */
4463 /*-----------------------------------------------------------------*/
4465 typeFromStr (const char *s
)
4467 sym_link
*r
= newLink (DECLARATOR
);
4487 r
->xclass
= SPECIFIER
;
4488 SPEC_NOUN (r
) = V_BOOL
;
4491 r
->xclass
= SPECIFIER
;
4492 SPEC_NOUN (r
) = V_CHAR
;
4493 if (!sign
&& !usign
)
4494 r
->select
.s
.b_implicit_sign
= true;
4500 else if (!sign
&& !options
.signed_char
)
4505 r
->xclass
= SPECIFIER
;
4506 SPEC_NOUN (r
) = V_INT
;
4509 r
->xclass
= SPECIFIER
;
4510 SPEC_NOUN (r
) = V_INT
;
4514 r
->xclass
= SPECIFIER
;
4515 SPEC_NOUN (r
) = V_INT
;
4516 SPEC_LONGLONG (r
) = 1;
4519 r
->xclass
= SPECIFIER
;
4520 SPEC_NOUN (r
) = V_FLOAT
;
4523 r
->xclass
= SPECIFIER
;
4524 SPEC_NOUN (r
) = V_FIXED16X16
;
4527 r
->xclass
= SPECIFIER
;
4528 SPEC_NOUN (r
) = V_VOID
;
4531 DCL_TYPE (r
) = port
->unqualified_pointer
;
4538 assert (*(s
+ 1) == '*');
4539 nr
= newLink (DECLARATOR
);
4545 DCL_TYPE (r
) = GPOINTER
;
4548 DCL_TYPE (r
) = FPOINTER
;
4551 DCL_TYPE (r
) = CPOINTER
;
4554 DCL_TYPE (r
) = POINTER
;
4557 DCL_TYPE (r
) = FUNCTION
;
4558 nr
= newLink (DECLARATOR
);
4561 DCL_TYPE (r
) = CPOINTER
;
4567 werror (E_INTERNAL_ERROR
, __FILE__
, __LINE__
, "typeFromStr: unknown type");
4568 fprintf(stderr
, "unknown: %s\n", s
);
4572 werror (E_INTERNAL_ERROR
, __FILE__
, __LINE__
, "typeFromStr: both signed and unsigned specified");
4573 if (IS_SPEC (r
) && usign
)
4578 if (IS_SPEC (r
) && constant
)
4589 /*-----------------------------------------------------------------*/
4590 /* initCSupport - create functions for C support routines */
4591 /*-----------------------------------------------------------------*/
4595 const char *smuldivmod
[] = {
4598 const char *sbwd
[] = {
4599 "char", "int", "long", "longlong", "fixed16x16",
4601 const char *fp16x16sbwd
[] = {
4602 "char", "int", "long", "longlong", "float",
4604 const char *ssu
[] = {
4605 "s", "su", "us", "u"
4607 const char *srlrr
[] = {
4610 /* type as character codes for typeFromStr() */
4611 const char *sbwdCodes
[] = {
4613 "Uc", "Ui", "Ul", "UL"
4616 int bwd
, su
, muldivmod
, tofrom
, slsr
;
4618 if (getenv ("SDCC_NO_C_SUPPORT"))
4620 /* for debugging only */
4624 for (bwd
= 0; bwd
< 4; bwd
++)
4639 l
= newLongLongLink ();
4644 multypes
[bwd
][0] = l
;
4645 multypes
[bwd
][1] = copyLinkChain (l
);
4646 SPEC_USIGN (multypes
[bwd
][0]) = 0;
4647 SPEC_USIGN (multypes
[bwd
][1]) = 1;
4650 floatType
= newFloatLink ();
4651 fixed16x16Type
= newFixed16x16Link ();
4652 sym_link
*boolType
= newLink (SPECIFIER
); SPEC_NOUN (boolType
) = V_BOOL
; // Can't use newBoolLink, as it might give us a __bit.
4653 sym_link
*charType
= (options
.signed_char
) ? SCHARTYPE
: UCHARTYPE
;
4655 fsadd
= funcOfType ("__fsadd", floatType
, floatType
, 2, options
.float_rent
);
4656 fssub
= funcOfType ("__fssub", floatType
, floatType
, 2, options
.float_rent
);
4657 fsmul
= funcOfType ("__fsmul", floatType
, floatType
, 2, options
.float_rent
);
4658 fsdiv
= funcOfType ("__fsdiv", floatType
, floatType
, 2, options
.float_rent
);
4659 fseq
= funcOfType ("__fseq", boolType
, floatType
, 2, options
.float_rent
);
4660 fsneq
= funcOfType ("__fsneq", boolType
, floatType
, 2, options
.float_rent
);
4661 fslt
= funcOfType ("__fslt", boolType
, floatType
, 2, options
.float_rent
);
4663 fps16x16_add
= funcOfType ("__fps16x16_add", fixed16x16Type
, fixed16x16Type
, 2, options
.float_rent
);
4664 fps16x16_sub
= funcOfType ("__fps16x16_sub", fixed16x16Type
, fixed16x16Type
, 2, options
.float_rent
);
4665 fps16x16_mul
= funcOfType ("__fps16x16_mul", fixed16x16Type
, fixed16x16Type
, 2, options
.float_rent
);
4666 fps16x16_div
= funcOfType ("__fps16x16_div", fixed16x16Type
, fixed16x16Type
, 2, options
.float_rent
);
4667 fps16x16_eq
= funcOfType ("__fps16x16_eq", charType
, fixed16x16Type
, 2, options
.float_rent
);
4668 fps16x16_neq
= funcOfType ("__fps16x16_neq", charType
, fixed16x16Type
, 2, options
.float_rent
);
4669 fps16x16_lt
= funcOfType ("__fps16x16_lt", charType
, fixed16x16Type
, 2, options
.float_rent
);
4670 fps16x16_lteq
= funcOfType ("__fps16x16_lteq", charType
, fixed16x16Type
, 2, options
.float_rent
);
4671 fps16x16_gt
= funcOfType ("__fps16x16_gt", charType
, fixed16x16Type
, 2, options
.float_rent
);
4672 fps16x16_gteq
= funcOfType ("__fps16x16_gteq", charType
, fixed16x16Type
, 2, options
.float_rent
);
4674 for (tofrom
= 0; tofrom
< 2; tofrom
++)
4676 for (bwd
= 0; bwd
< 4; bwd
++)
4678 for (su
= 0; su
< 2; su
++)
4682 dbuf_init (&dbuf
, 128);
4685 dbuf_printf (&dbuf
, "__fs2%s%s", ssu
[su
* 3], sbwd
[bwd
]);
4686 conv
[tofrom
][bwd
][su
] = funcOfType (dbuf_c_str (&dbuf
), multypes
[bwd
][su
], floatType
, 1, options
.float_rent
);
4690 dbuf_printf (&dbuf
, "__%s%s2fs", ssu
[su
* 3], sbwd
[bwd
]);
4691 conv
[tofrom
][bwd
][su
] = funcOfType (dbuf_c_str (&dbuf
), floatType
, multypes
[bwd
][su
], 1, options
.float_rent
);
4693 dbuf_destroy (&dbuf
);
4698 for (tofrom
= 0; tofrom
< 2; tofrom
++)
4700 for (bwd
= 0; bwd
< 5; bwd
++)
4702 for (su
= 0; su
< 2; su
++)
4706 dbuf_init (&dbuf
, 128);
4709 dbuf_printf (&dbuf
, "__fps16x162%s%s", ssu
[su
* 3], fp16x16sbwd
[bwd
]);
4711 fp16x16conv
[tofrom
][bwd
][su
] =
4712 funcOfType (dbuf_c_str (&dbuf
), floatType
, fixed16x16Type
, 1, options
.float_rent
);
4714 fp16x16conv
[tofrom
][bwd
][su
] =
4715 funcOfType (dbuf_c_str (&dbuf
), multypes
[bwd
][su
], fixed16x16Type
, 1, options
.float_rent
);
4719 dbuf_printf (&dbuf
, "__%s%s2fps16x16", ssu
[su
* 3], fp16x16sbwd
[bwd
]);
4721 fp16x16conv
[tofrom
][bwd
][su
] =
4722 funcOfType (dbuf_c_str (&dbuf
), fixed16x16Type
, floatType
, 1, options
.float_rent
);
4724 fp16x16conv
[tofrom
][bwd
][su
] =
4725 funcOfType (dbuf_c_str (&dbuf
), fixed16x16Type
, multypes
[bwd
][su
], 1, options
.float_rent
);
4727 dbuf_destroy (&dbuf
);
4733 for (muldivmod = 0; muldivmod < 3; muldivmod++)
4735 for (bwd = 0; bwd < 4; bwd++)
4737 for (su = 0; su < 2; su++)
4741 dbuf_init (&dbuf, 128);
4742 dbuf_printf (&dbuf, "_%s%s%s", smuldivmod[muldivmod], ssu[su*3], sbwd[bwd]);
4743 muldiv[muldivmod][bwd][su] = funcOfType (_mangleFunctionName(dbuf_c_str (&dbuf)), multypes[bwd][su], multypes[bwd][su], 2, options.intlong_rent);
4744 dbuf_destroy (&dbuf);
4745 FUNC_NONBANKED (muldiv[muldivmod][bwd][su]->type) = 1;
4750 muluint() and mulsint() resp. mululong() and mulslong() return the same result.
4751 Therefore they've been merged into mulint() and mullong().
4756 /* _divschar/_modschar return int, so that both
4757 * 100 / -4 = -25 and -128 / -1 = 128 can be handled correctly
4758 * (first one would have to be sign extended, second one must not be).
4759 * Similarly, modschar should be handled, but the iCode introduces cast
4760 * here and forces '% : s8 x s8 -> s8' ... */
4762 for (su
= 0; su
< 4; su
++)
4764 for (muldivmod
= 0; muldivmod
< 3; muldivmod
++)
4766 /* muluchar, mulschar, mulsuchar and muluschar are separate functions, because e.g. the z80
4767 port is sign/zero-extending to int before calling mulint() */
4768 /* div and mod : s8_t x s8_t -> s8_t should be s8_t x s8_t -> s16_t, see below */
4771 dbuf_init (&dbuf
, 128);
4772 dbuf_printf (&dbuf
, "_%s%s%s", smuldivmod
[muldivmod
], ssu
[su
], sbwd
[bwd
]);
4773 muldiv
[muldivmod
][bwd
][su
] =
4774 funcOfType (_mangleFunctionName (dbuf_c_str (&dbuf
)),
4775 multypes
[(TARGET_IS_PIC16
&& muldivmod
== 1 && bwd
== 0 && su
== 0 || (TARGET_IS_PIC14
|| TARGET_IS_STM8
|| TARGET_Z80_LIKE
|| TARGET_PDK_LIKE
|| TARGET_MOS6502_LIKE
|| TARGET_IS_F8
) && bwd
== 0) ? 1 : bwd
][su
% 2],
4776 multypes
[bwd
][su
/ 2],
4778 options
.intlong_rent
);
4779 dbuf_destroy (&dbuf
);
4783 for (bwd
= 1; bwd
< 4; bwd
++)
4785 for (su
= 0; su
< 2; su
++)
4787 for (muldivmod
= 1; muldivmod
< 3; muldivmod
++)
4791 dbuf_init (&dbuf
, 128);
4792 dbuf_printf (&dbuf
, "_%s%s%s", smuldivmod
[muldivmod
], ssu
[su
* 3], sbwd
[bwd
]);
4793 muldiv
[muldivmod
][bwd
][su
] =
4794 funcOfType (_mangleFunctionName (dbuf_c_str (&dbuf
)),
4795 multypes
[(TARGET_IS_PIC16
&& muldivmod
== 1 && bwd
== 0 && su
== 0 || (TARGET_IS_STM8
|| TARGET_Z80_LIKE
|| TARGET_PDK_LIKE
|| TARGET_IS_F8
) && bwd
== 0) ? 1 : bwd
][su
],
4798 options
.intlong_rent
);
4799 dbuf_destroy (&dbuf
);
4808 /* word, doubleword, and quadword */
4809 for (bwd
= 1; bwd
< 4; bwd
++)
4814 dbuf_init (&dbuf
, 128);
4815 dbuf_printf (&dbuf
, "_%s%s", smuldivmod
[muldivmod
], sbwd
[bwd
]);
4816 muldiv
[muldivmod
][bwd
][0] =
4817 funcOfType (_mangleFunctionName (dbuf_c_str (&dbuf
)), multypes
[bwd
][su
], multypes
[bwd
][su
], 2, options
.intlong_rent
);
4818 dbuf_destroy (&dbuf
);
4819 /* signed = unsigned */
4820 muldiv
[muldivmod
][bwd
][1] = muldiv
[muldivmod
][bwd
][0];
4823 for (slsr
= 0; slsr
< 2; slsr
++)
4825 for (bwd
= 0; bwd
< 4; bwd
++)
4827 for (su
= 0; su
< 2; su
++)
4831 const char *params
[2];
4833 params
[0] = sbwdCodes
[bwd
+ 4*su
];
4834 params
[1] = sbwdCodes
[0];
4836 dbuf_init (&dbuf
, 128);
4837 dbuf_printf (&dbuf
, "_%s%s%s", srlrr
[slsr
], ssu
[su
* 3], sbwd
[bwd
]);
4838 rlrr
[slsr
][bwd
][su
] = sym
=
4839 funcOfTypeVarg (_mangleFunctionName (dbuf_c_str (&dbuf
)),
4840 sbwdCodes
[bwd
+ 4*su
], 2, ¶ms
[0]);
4841 FUNC_ISREENT (sym
->type
) = options
.intlong_rent
? 1 : 0;
4842 FUNC_NONBANKED (sym
->type
) = 1;
4843 dbuf_destroy (&dbuf
);
4849 const char *iparams
[] = {"i", "i"};
4850 const char *uiparams
[] = {"Ui", "Ui"};
4851 muls16tos32
[0] = port
->support
.has_mulint2long
? funcOfTypeVarg ("__mulsint2slong", "l", 2, iparams
) : 0;
4852 muls16tos32
[1] = port
->support
.has_mulint2long
? funcOfTypeVarg ("__muluint2ulong", "Ul", 2, uiparams
) : 0;
4855 const char *uiparams
[] = {"Ul", "Uc"};
4856 mulu32u8tou64
= port
->support
.has_mululonguchar2ulonglong
? funcOfTypeVarg ("__mululonguchar2ulonglong", "UL", 2, uiparams
) : 0;
4860 /*-----------------------------------------------------------------*/
4861 /* initBuiltIns - create prototypes for builtin functions */
4862 /*-----------------------------------------------------------------*/
4869 if (port
->builtintable
)
4871 for (i
= 0; port
->builtintable
[i
].name
; i
++)
4873 sym
= funcOfTypeVarg (port
->builtintable
[i
].name
, port
->builtintable
[i
].rtype
,
4874 port
->builtintable
[i
].nParms
, (const char **)port
->builtintable
[i
].parm_types
);
4875 FUNC_ISBUILTIN (sym
->type
) = 1;
4876 FUNC_ISREENT (sym
->type
) = 0; /* can never be reentrant */
4880 /* initialize memcpy symbol for struct assignment */
4881 builtin_memcpy
= findSym (SymbolTab
, NULL
, "__builtin_memcpy");
4882 nonbuiltin_memcpy
= findSym (SymbolTab
, NULL
, "__memcpy");
4884 if (!nonbuiltin_memcpy
)
4886 const char *argTypeStrs
[] = {"vg*", "Cvg*", "Ui"};
4887 nonbuiltin_memcpy
= funcOfTypeVarg ("__memcpy", "vg*", 3, argTypeStrs
);
4888 FUNC_ISBUILTIN (nonbuiltin_memcpy
->type
) = 0;
4889 FUNC_ISREENT (nonbuiltin_memcpy
->type
) = options
.stackAuto
;
4891 /* if there is no __builtin_memcpy, use __memcpy instead of an actual builtin */
4892 if (!builtin_memcpy
)
4893 builtin_memcpy
= nonbuiltin_memcpy
;
4895 builtin_unreachable
= funcOfTypeVarg ("__builtin_unreachable", "v", 0, 0);
4899 validateLink (sym_link
* l
, const char *macro
, const char *args
, const char select
, const char *file
, unsigned line
)
4901 if (l
&& l
->xclass
== select
)
4906 "Internal error: validateLink failed in %s(%s) @ %s:%u:"
4907 " expected %s, got %s\n",
4908 macro
, args
, file
, line
, DECLSPEC2TXT (select
), l
? DECLSPEC2TXT (l
->xclass
) : "null-link");
4909 exit (EXIT_FAILURE
);
4910 return l
; // never reached, makes compiler happy.
4914 llFitsInIntType (long long ll
, sym_link
*type
)
4916 long long min
= 0, max
= 0;
4918 // determine min and max values for explicitly or implicitly unsigned integers
4919 if (SPEC_USIGN (type
) || SPEC_NOUN (type
) == V_BOOL
)
4921 switch (SPEC_NOUN (type
))
4930 if (SPEC_LONGLONG (type
))
4931 max
= 0x7fffffffffffffffll
; // actual ull max would not fit and input is ll, anyway
4932 else if (SPEC_LONG (type
))
4938 assert (0); // not implemented for non-integer types
4941 else // determine min and max values for signed integers
4943 switch (SPEC_NOUN (type
))
4950 if (SPEC_LONGLONG (type
))
4952 min
= -9223372036854775808ull; // the "-" is not part of the literal, which does not fit in ll
4953 max
= 9223372036854775807ll;
4955 else if (SPEC_LONG (type
))
4957 min
= -2147483648ll;
4967 assert (0); // not implemented for non-integer types
4971 return ll
>= min
&& ll
<= max
;
4974 /*--------------------------------------------------------------------*/
4975 /* newEnumType - create an integer type compatible with enumerations */
4976 /*--------------------------------------------------------------------*/
4978 newEnumType (symbol
*enumlist
, sym_link
*userRequestedType
)
4980 long long int min
, max
, v
;
4982 sym_link
*type
= newLink (SPECIFIER
);
4983 SPEC_ENUM (type
) = 1;
4985 /* Catch user-requested types that make no sense for an enum; provide fallback if none specified */
4986 if (userRequestedType
)
4988 checkTypeSanity (userRequestedType
, NULL
);
4989 if ((SPEC_NOUN (userRequestedType
) != V_INT
&& SPEC_NOUN (userRequestedType
) != V_CHAR
&& SPEC_NOUN (userRequestedType
) != V_BOOL
) || SPEC_ENUM (userRequestedType
))
4991 werror (E_ENUM_UNDERLYING_TYPE
);
4992 /* try to keep going */
4993 SPEC_NOUN (type
) = V_INT
;
4997 SPEC_NOUN (type
) = SPEC_NOUN (userRequestedType
);
4998 SPEC_SIGN (type
) = SPEC_SIGN (userRequestedType
);
4999 SPEC_USIGN (type
) = SPEC_USIGN (userRequestedType
);
5000 SPEC_LONG (type
) = SPEC_LONG (userRequestedType
);
5001 SPEC_LONGLONG (type
) = SPEC_LONGLONG (userRequestedType
);
5005 SPEC_NOUN (type
) = V_INT
;
5013 /* Determine the range of the enumerated values */
5015 min
= max
= (long long int) ullFromVal (valFromType (sym
->type
));
5016 for (sym
= sym
->next
; sym
; sym
= sym
->next
)
5018 v
= (long long int) ullFromVal (valFromType (sym
->type
));
5025 /* Figure out if everything fits in the user requested (or default int) type */
5026 if (!llFitsInIntType (min
, type
) || !llFitsInIntType (max
, type
))
5027 werror (userRequestedType
? E_ENUM_TYPE_RANGE_TOO_SMALL
: W_ENUM_INT_RANGE_C23
);
5029 /* It does: If the type was explicitly requested, return it! */
5030 if (userRequestedType
)
5033 /* Otherwise: Use the smallest integer type that is compatible with this range and not a bit-precise type. */
5034 if (min
>= 0 && max
<= 1)
5036 SPEC_NOUN (type
) = V_BOOL
;
5038 else if (min
>= 0 && max
<= 255)
5040 SPEC_NOUN (type
) = V_CHAR
;
5041 SPEC_USIGN (type
) = 1;
5043 else if (min
>= -128 && max
<= 127)
5045 SPEC_NOUN (type
) = V_CHAR
;
5046 SPEC_SIGN (type
) = 1;
5048 else if (min
>= 0 && max
<= 65535)
5050 SPEC_NOUN (type
) = V_INT
;
5051 SPEC_USIGN (type
) = 1;
5053 else if (min
>= -32768 && max
<= 32767)
5055 SPEC_NOUN (type
) = V_INT
;
5057 else if (min
>= 0 && max
<= 4294967295)
5059 SPEC_NOUN (type
) = V_INT
;
5060 SPEC_LONG (type
) = 1;
5061 SPEC_USIGN (type
) = 1;
5063 else if (min
>= -2147483648 && max
<= 2147483647)
5065 SPEC_NOUN (type
) = V_INT
;
5066 SPEC_LONG (type
) = 1;
5070 SPEC_NOUN (type
) = V_INT
;
5071 SPEC_LONGLONG (type
) = 1;
5073 SPEC_USIGN (type
) = 1;
5079 /*-------------------------------------------------------------------*/
5080 /* isConstant - check if the type is constant */
5081 /*-------------------------------------------------------------------*/
5083 isConstant (sym_link
* type
)
5088 while (IS_ARRAY (type
))
5092 return SPEC_CONST (type
);
5094 return DCL_PTR_CONST (type
);
5097 /*-------------------------------------------------------------------*/
5098 /* isVolatile - check if the type is volatile */
5099 /*-------------------------------------------------------------------*/
5101 isVolatile (sym_link
* type
)
5106 while (IS_ARRAY (type
))
5110 return SPEC_VOLATILE (type
);
5112 return DCL_PTR_VOLATILE (type
);
5115 /*-------------------------------------------------------------------*/
5116 /* isRestrict - check if the type is restricted */
5117 /*-------------------------------------------------------------------*/
5119 isRestrict (sym_link
* type
)
5124 while (IS_ARRAY (type
))
5128 return SPEC_RESTRICT (type
);
5130 return DCL_PTR_RESTRICT (type
);
5133 /*-------------------------------------------------------------------*/
5134 /* mergeKRDeclListIntoFuncDecl - merge the type information from the */
5135 /* declaration list between function declaration and body into */
5136 /* the function declaration, replacing the default type int */
5137 /*-------------------------------------------------------------------*/
5139 mergeKRDeclListIntoFuncDecl (symbol
*funcDecl
, symbol
*kr_decls
)
5141 if (kr_decls
!= NULL
)
5143 if (options
.std_c23
)
5145 werror (E_OLD_STYLE
, (funcDecl
? funcDecl
->name
: ""));
5154 funcType
= funcDecl
->type
;
5155 while (funcType
&& !IS_FUNC (funcType
))
5156 funcType
= funcType
->next
;
5161 * use FUNC_NOPROTOTYPE, once prototype-less functions are fully
5162 * supported and K&R functions can be treated as such, because
5163 * a function with prototype cannot have been declared in K&R style
5165 if (!funcType
->funcAttrs
.oldStyle
)
5166 werror (E_MIXED_FUNCTION_STYLES
, (funcDecl
? funcDecl
->name
: ""));
5168 /* iterate over members of declaration list */
5169 for (declLoop
= kr_decls
; declLoop
; declLoop
= declLoop
->next
)
5172 /* iterate over parameters */
5173 for (parLoop
= FUNC_ARGS (funcType
); parLoop
; parLoop
= parLoop
->next
)
5175 if (strcmp (declLoop
->name
, parLoop
->sym
->name
) == 0)
5179 werror (E_ID_UNDEF
, declLoop
->name
);
5182 /* iterate over parameters */
5183 for (parLoop
= FUNC_ARGS (funcType
); parLoop
; parLoop
= parLoop
->next
)
5186 /* iterate over members of declaration list */
5187 for (declLoop
= kr_decls
; declLoop
; declLoop
= declLoop
->next
)
5189 if (strcmp (declLoop
->name
, parLoop
->sym
->name
) == 0)
5193 werror (E_DUPLICATE
, declLoop
->name
);
5197 /* delete default int type */
5198 Safe_free (parLoop
->type
);
5199 /* propagate type */
5200 parLoop
->type
= declLoop
->type
;
5201 parLoop
->etype
= declLoop
->etype
;
5202 parLoop
->sym
->type
= copyLinkChain (parLoop
->type
);
5203 parLoop
->sym
->etype
= getSpec (parLoop
->sym
->type
);