]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.c
Warn when structs are passed by value to a function.
[cc65] / src / cc65 / datatype.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                datatype.c                                 */
4 /*                                                                           */
5 /*               Type string handling for the cc65 C compiler                */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2008 Ullrich von Bassewitz                                       */
10 /*               Roemerstrasse 52                                            */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "addrsize.h"
40 #include "check.h"
41 #include "mmodel.h"
42 #include "xmalloc.h"
43
44 /* cc65 */
45 #include "codegen.h"
46 #include "datatype.h"
47 #include "error.h"
48 #include "fp.h"
49 #include "funcdesc.h"
50 #include "global.h"
51 #include "symtab.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Data                                    */
57 /*****************************************************************************/
58
59
60
61 /* Predefined type strings */
62 Type type_schar[]       = { TYPE(T_SCHAR),  TYPE(T_END) };
63 Type type_uchar[]       = { TYPE(T_UCHAR),  TYPE(T_END) };
64 Type type_int[]         = { TYPE(T_INT),    TYPE(T_END) };
65 Type type_uint[]        = { TYPE(T_UINT),   TYPE(T_END) };
66 Type type_long[]        = { TYPE(T_LONG),   TYPE(T_END) };
67 Type type_ulong[]       = { TYPE(T_ULONG),  TYPE(T_END) };
68 Type type_void[]        = { TYPE(T_VOID),   TYPE(T_END) };
69 Type type_size_t[]      = { TYPE(T_SIZE_T), TYPE(T_END) };
70 Type type_float[]       = { TYPE(T_FLOAT),  TYPE(T_END) };
71 Type type_double[]      = { TYPE(T_DOUBLE), TYPE(T_END) };
72
73
74
75 /*****************************************************************************/
76 /*                                   Code                                    */
77 /*****************************************************************************/
78
79
80
81 unsigned TypeLen (const Type* T)
82 /* Return the length of the type string */
83 {
84     const Type* Start = T;
85     while (T->C != T_END) {
86         ++T;
87     }
88     return T - Start;
89 }
90
91
92
93 Type* TypeCopy (Type* Dest, const Type* Src)
94 /* Copy a type string */
95 {
96     Type* Orig = Dest;
97     while (1) {
98         *Dest = *Src;
99         if (Src->C == T_END) {
100             break;
101         }
102         Src++;
103         Dest++;
104     }
105     return Orig;
106 }
107
108
109
110 Type* TypeDup (const Type* T)
111 /* Create a copy of the given type on the heap */
112 {
113     unsigned Len = (TypeLen (T) + 1) * sizeof (Type);
114     return memcpy (xmalloc (Len), T, Len);
115 }
116
117
118
119 Type* TypeAlloc (unsigned Len)
120 /* Allocate memory for a type string of length Len. Len *must* include the
121  * trailing T_END.
122  */
123 {
124     return xmalloc (Len * sizeof (Type));
125 }
126
127
128
129 void TypeFree (Type* T)
130 /* Free a type string */
131 {
132     xfree (T);
133 }
134
135
136
137 int SignExtendChar (int C)
138 /* Do correct sign extension of a character */
139 {
140     if (IS_Get (&SignedChars) && (C & 0x80) != 0) {
141         return C | ~0xFF;
142     } else {
143         return C & 0xFF;
144     }
145 }
146
147
148
149 TypeCode GetDefaultChar (void)
150 /* Return the default char type (signed/unsigned) depending on the settings */
151 {
152     return IS_Get (&SignedChars)? T_SCHAR : T_UCHAR;
153 }
154
155
156
157 Type* GetCharArrayType (unsigned Len)
158 /* Return the type for a char array of the given length */
159 {
160     /* Allocate memory for the type string */
161     Type* T = TypeAlloc (3);    /* array/char/terminator */
162
163     /* Fill the type string */
164     T[0].C   = T_ARRAY;
165     T[0].A.L = Len;             /* Array length is in the L attribute */
166     T[1].C   = GetDefaultChar ();
167     T[2].C   = T_END;
168
169     /* Return the new type */
170     return T;
171 }
172
173
174
175 Type* GetImplicitFuncType (void)
176 /* Return a type string for an inplicitly declared function */
177 {
178     /* Get a new function descriptor */
179     FuncDesc* F = NewFuncDesc ();
180
181     /* Allocate memory for the type string */
182     Type* T = TypeAlloc (3);    /* func/returns int/terminator */
183
184     /* Prepare the function descriptor */
185     F->Flags  = FD_EMPTY | FD_VARIADIC;
186     F->SymTab = &EmptySymTab;
187     F->TagTab = &EmptySymTab;
188
189     /* Fill the type string */
190     T[0].C   = T_FUNC | CodeAddrSizeQualifier ();
191     T[0].A.P = F;
192     T[1].C   = T_INT;
193     T[2].C   = T_END;
194
195     /* Return the new type */
196     return T;
197 }
198
199
200
201 Type* PointerTo (const Type* T)
202 /* Return a type string that is "pointer to T". The type string is allocated
203  * on the heap and may be freed after use.
204  */
205 {
206     /* Get the size of the type string including the terminator */
207     unsigned Size = TypeLen (T) + 1;
208
209     /* Allocate the new type string */
210     Type* P = TypeAlloc (Size + 1);
211
212     /* Create the return type... */
213     P[0].C = T_PTR | (T[0].C & T_QUAL_ADDRSIZE);
214     memcpy (P+1, T, Size * sizeof (Type));
215
216     /* ...and return it */
217     return P;
218 }
219
220
221
222 static TypeCode PrintTypeComp (FILE* F, TypeCode C, TypeCode Mask, const char* Name)
223 /* Check for a specific component of the type. If it is there, print the
224  * name and remove it. Return the type with the component removed.
225  */
226 {
227     if ((C & Mask) == Mask) {
228         fprintf (F, "%s ", Name);
229         C &= ~Mask;
230     }
231     return C;
232 }
233
234
235
236 void PrintType (FILE* F, const Type* T)
237 /* Output translation of type array. */
238 {
239     /* Walk over the type string */
240     while (T->C != T_END) {
241
242         /* Get the type code */
243         TypeCode C = T->C;
244
245         /* Print any qualifiers */
246         C = PrintTypeComp (F, C, T_QUAL_CONST, "const");
247         C = PrintTypeComp (F, C, T_QUAL_VOLATILE, "volatile");
248         C = PrintTypeComp (F, C, T_QUAL_RESTRICT, "restrict");
249         C = PrintTypeComp (F, C, T_QUAL_NEAR, "__near__");
250         C = PrintTypeComp (F, C, T_QUAL_FAR, "__far__");
251         C = PrintTypeComp (F, C, T_QUAL_FASTCALL, "__fastcall__");
252
253         /* Signedness. Omit the signedness specifier for long and int */
254         if ((C & T_MASK_TYPE) != T_TYPE_INT && (C & T_MASK_TYPE) != T_TYPE_LONG) {
255             C = PrintTypeComp (F, C, T_SIGN_SIGNED, "signed");
256         }
257         C = PrintTypeComp (F, C, T_SIGN_UNSIGNED, "unsigned");
258
259         /* Now check the real type */
260         switch (C & T_MASK_TYPE) {
261             case T_TYPE_CHAR:
262                 fprintf (F, "char");
263                 break;
264             case T_TYPE_SHORT:
265                 fprintf (F, "short");
266                 break;
267             case T_TYPE_INT:
268                 fprintf (F, "int");
269                 break;
270             case T_TYPE_LONG:
271                 fprintf (F, "long");
272                 break;
273             case T_TYPE_LONGLONG:
274                 fprintf (F, "long long");
275                 break;
276             case T_TYPE_FLOAT:
277                 fprintf (F, "float");
278                 break;
279             case T_TYPE_DOUBLE:
280                 fprintf (F, "double");
281                 break;
282             case T_TYPE_VOID:
283                 fprintf (F, "void");
284                 break;
285             case T_TYPE_STRUCT:
286                 fprintf (F, "struct %s", ((SymEntry*) T->A.P)->Name);
287                 break;
288             case T_TYPE_UNION:
289                 fprintf (F, "union %s", ((SymEntry*) T->A.P)->Name);
290                 break;
291             case T_TYPE_ARRAY:
292                 /* Recursive call */
293                 PrintType (F, T + 1);
294                 if (T->A.L == UNSPECIFIED) {
295                     fprintf (F, "[]");
296                 } else {
297                     fprintf (F, "[%ld]", T->A.L);
298                 }
299                 return;
300             case T_TYPE_PTR:
301                 /* Recursive call */
302                 PrintType (F, T + 1);
303                 fprintf (F, "*");
304                 return;
305             case T_TYPE_FUNC:
306                 fprintf (F, "function returning ");
307                 break;
308             default:
309                 fprintf (F, "unknown type: %04lX", T->C);
310         }
311
312         /* Next element */
313         ++T;
314     }
315 }
316
317
318
319 void PrintFuncSig (FILE* F, const char* Name, Type* T)
320 /* Print a function signature. */
321 {
322     /* Get the function descriptor */
323     const FuncDesc* D = GetFuncDesc (T);
324
325     /* Print a comment with the function signature */
326     PrintType (F, GetFuncReturn (T));
327     if (IsQualNear (T)) {
328         fprintf (F, " __near__");
329     }
330     if (IsQualFar (T)) {
331         fprintf (F, " __far__");
332     }
333     if (IsQualFastcall (T)) {
334         fprintf (F, " __fastcall__");
335     }
336     fprintf (F, " %s (", Name);
337
338     /* Parameters */
339     if (D->Flags & FD_VOID_PARAM) {
340         fprintf (F, "void");
341     } else {
342         unsigned I;
343         SymEntry* E = D->SymTab->SymHead;
344         for (I = 0; I < D->ParamCount; ++I) {
345             if (I > 0) {
346                 fprintf (F, ", ");
347             }
348             if (SymIsRegVar (E)) {
349                 fprintf (F, "register ");
350             }
351             PrintType (F, E->Type);
352             E = E->NextSym;
353         }
354     }
355
356     /* End of parameter list */
357     fprintf (F, ")");
358 }
359
360
361
362 void PrintRawType (FILE* F, const Type* T)
363 /* Print a type string in raw format (for debugging) */
364 {
365     while (T->C != T_END) {
366         fprintf (F, "%04lX ", T->C);
367         ++T;
368     }
369     fprintf (F, "\n");
370 }
371
372
373
374 int TypeHasAttr (const Type* T)
375 /* Return true if the given type has attribute data */
376 {
377     return IsClassStruct (T) || IsTypeArray (T) || IsClassFunc (T);
378 }
379
380
381
382 unsigned SizeOf (const Type* T)
383 /* Compute size of object represented by type array. */
384 {
385     switch (UnqualifiedType (T->C)) {
386
387         case T_VOID:
388             return 0;   /* Assume voids have size zero */
389
390         case T_SCHAR:
391         case T_UCHAR:
392             return SIZEOF_CHAR;
393
394         case T_SHORT:
395         case T_USHORT:
396             return SIZEOF_SHORT;
397
398         case T_INT:
399         case T_UINT:
400             return SIZEOF_INT;
401
402         case T_PTR:
403         case T_FUNC:    /* Maybe pointer to function */
404             return SIZEOF_PTR;
405
406         case T_LONG:
407         case T_ULONG:
408             return SIZEOF_LONG;
409
410         case T_LONGLONG:
411         case T_ULONGLONG:
412             return SIZEOF_LONGLONG;
413
414         case T_ENUM:
415             return SIZEOF_INT;
416
417         case T_FLOAT:
418             return SIZEOF_FLOAT;
419
420         case T_DOUBLE:
421             return SIZEOF_DOUBLE;
422
423         case T_STRUCT:
424         case T_UNION:
425             return ((SymEntry*) T->A.P)->V.S.Size;
426
427         case T_ARRAY:
428             if (T->A.L == UNSPECIFIED) {
429                 /* Array with unspecified size */
430                 return 0;
431             } else {
432                 return T->A.L * SizeOf (T + 1);
433             }
434
435         default:
436             Internal ("Unknown type in SizeOf: %04lX", T->C);
437             return 0;
438
439     }
440 }
441
442
443
444 unsigned PSizeOf (const Type* T)
445 /* Compute size of pointer object. */
446 {
447     /* We are expecting a pointer expression */
448     CHECK (IsClassPtr (T));
449
450     /* Skip the pointer or array token itself */
451     return SizeOf (T + 1);
452 }
453
454
455
456 unsigned CheckedSizeOf (const Type* T)
457 /* Return the size of a data type. If the size is zero, emit an error and
458  * return some valid size instead (so the rest of the compiler doesn't have
459  * to work with invalid sizes).
460  */
461 {
462     unsigned Size = SizeOf (T);
463     if (Size == 0) {
464         Error ("Size of data type is unknown");
465         Size = SIZEOF_CHAR;     /* Don't return zero */
466     }
467     return Size;
468 }
469
470
471
472 unsigned CheckedPSizeOf (const Type* T)
473 /* Return the size of a data type that is pointed to by a pointer. If the
474  * size is zero, emit an error and return some valid size instead (so the
475  * rest of the compiler doesn't have to work with invalid sizes).
476  */
477 {
478     unsigned Size = PSizeOf (T);
479     if (Size == 0) {
480         Error ("Size of data type is unknown");
481         Size = SIZEOF_CHAR;     /* Don't return zero */
482     }
483     return Size;
484 }
485
486
487
488 unsigned TypeOf (const Type* T)
489 /* Get the code generator base type of the object */
490 {
491     switch (UnqualifiedType (T->C)) {
492
493         case T_SCHAR:
494             return CF_CHAR;
495
496         case T_UCHAR:
497             return CF_CHAR | CF_UNSIGNED;
498
499         case T_SHORT:
500         case T_INT:
501         case T_ENUM:
502             return CF_INT;
503
504         case T_USHORT:
505         case T_UINT:
506         case T_PTR:
507         case T_ARRAY:
508             return CF_INT | CF_UNSIGNED;
509
510         case T_LONG:
511             return CF_LONG;
512
513         case T_ULONG:
514             return CF_LONG | CF_UNSIGNED;
515
516         case T_FLOAT:
517         case T_DOUBLE:
518             /* These two are identical in the backend */
519             return CF_FLOAT;
520
521         case T_FUNC:
522             return (((FuncDesc*) T->A.P)->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
523
524         case T_STRUCT:
525         case T_UNION:
526             /* Address of ... */
527             return CF_INT | CF_UNSIGNED;
528
529         default:
530             Error ("Illegal type %04lX", T->C);
531             return CF_INT;
532     }
533 }
534
535
536
537 Type* Indirect (Type* T)
538 /* Do one indirection for the given type, that is, return the type where the
539  * given type points to.
540  */
541 {
542     /* We are expecting a pointer expression */
543     CHECK (IsClassPtr (T));
544
545     /* Skip the pointer or array token itself */
546     return T + 1;
547 }
548
549
550
551 Type* ArrayToPtr (Type* T)
552 /* Convert an array to a pointer to it's first element */
553 {
554     /* Return pointer to first element */
555     return PointerTo (GetElementType (T));
556 }
557
558
559
560 int IsVariadicFunc (const Type* T)
561 /* Return true if this is a function type or pointer to function type with
562  * variable parameter list
563  */
564 {
565     FuncDesc* F = GetFuncDesc (T);
566     return (F->Flags & FD_VARIADIC) != 0;
567 }
568
569
570
571 FuncDesc* GetFuncDesc (const Type* T)
572 /* Get the FuncDesc pointer from a function or pointer-to-function type */
573 {
574     if (UnqualifiedType (T->C) == T_PTR) {
575         /* Pointer to function */
576         ++T;
577     }
578
579     /* Be sure it's a function type */
580     CHECK (IsClassFunc (T));
581
582     /* Get the function descriptor from the type attributes */
583     return T->A.P;
584 }
585
586
587
588 void SetFuncDesc (Type* T, FuncDesc* F)
589 /* Set the FuncDesc pointer in a function or pointer-to-function type */
590 {
591     if (UnqualifiedType (T->C) == T_PTR) {
592         /* Pointer to function */
593         ++T;
594     }
595
596     /* Be sure it's a function type */
597     CHECK (IsClassFunc (T));
598
599     /* Set the function descriptor */
600     T->A.P = F;
601 }
602
603
604
605 Type* GetFuncReturn (Type* T)
606 /* Return a pointer to the return type of a function or pointer-to-function type */
607 {
608     if (UnqualifiedType (T->C) == T_PTR) {
609         /* Pointer to function */
610         ++T;
611     }
612
613     /* Be sure it's a function type */
614     CHECK (IsClassFunc (T));
615
616     /* Return a pointer to the return type */
617     return T + 1;
618 }
619
620
621
622 long GetElementCount (const Type* T)
623 /* Get the element count of the array specified in T (which must be of
624  * array type).
625  */
626 {
627     CHECK (IsTypeArray (T));
628     return T->A.L;
629 }
630
631
632
633 void SetElementCount (Type* T, long Count)
634 /* Set the element count of the array specified in T (which must be of
635  * array type).
636  */
637 {
638     CHECK (IsTypeArray (T));
639     T->A.L = Count;
640 }
641
642
643
644 Type* GetElementType (Type* T)
645 /* Return the element type of the given array type. */
646 {
647     CHECK (IsTypeArray (T));
648     return T + 1;
649 }
650
651
652
653 SymEntry* GetSymEntry (const Type* T)
654 /* Return a SymEntry pointer from a type */
655 {
656     /* Only structs or unions have a SymEntry attribute */
657     CHECK (IsClassStruct (T));
658
659     /* Return the attribute */
660     return T->A.P;
661 }
662
663
664
665 void SetSymEntry (Type* T, SymEntry* S)
666 /* Set the SymEntry pointer for a type */
667 {
668     /* Only structs or unions have a SymEntry attribute */
669     CHECK (IsClassStruct (T));
670
671     /* Set the attribute */
672     T->A.P = S;
673 }
674
675
676
677 Type* IntPromotion (Type* T)
678 /* Apply the integer promotions to T and return the result. The returned type
679  * string may be T if there is no need to change it.
680  */
681 {
682     /* We must have an int to apply int promotions */
683     PRECONDITION (IsClassInt (T));
684
685     /* An integer can represent all values from either signed or unsigned char,
686      * so convert chars to int and leave all other types alone.
687      */
688     if (IsTypeChar (T)) {
689         return type_int;
690     } else {
691         return T;
692     }
693 }
694
695
696
697 Type* PtrConversion (Type* T)
698 /* If the type is a function, convert it to pointer to function. If the
699  * expression is an array, convert it to pointer to first element. Otherwise
700  * return T.
701  */
702 {
703     if (IsTypeFunc (T)) {
704         return PointerTo (T);
705     } else if (IsTypeArray (T)) {
706         return ArrayToPtr (T);
707     } else {
708         return T;
709     }
710 }
711
712
713
714 TypeCode AddrSizeQualifier (unsigned AddrSize)
715 /* Return T_QUAL_NEAR or T_QUAL_FAR depending on the address size */
716 {
717     switch (AddrSize) {
718
719         case ADDR_SIZE_ABS:
720             return T_QUAL_NEAR;
721
722         case ADDR_SIZE_FAR:
723             return T_QUAL_FAR;
724
725         default:
726             Error ("Invalid address size");
727             return T_QUAL_NEAR;
728
729     }
730 }
731
732
733