]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.c
6aad02ea36ef65381cff0a3ef87df890be64a535
[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-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 "check.h"
40 #include "xmalloc.h"
41
42 /* cc65 */
43 #include "codegen.h"
44 #include "datatype.h"
45 #include "error.h"
46 #include "funcdesc.h"
47 #include "global.h"
48 #include "util.h"
49 #include "symtab.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Predefined type strings */
60 type type_uchar []      = { T_UCHAR,    T_END };
61 type type_int []        = { T_INT,      T_END };
62 type type_uint []       = { T_UINT,     T_END };
63 type type_long []       = { T_LONG,     T_END };
64 type type_ulong []      = { T_ULONG,    T_END };
65 type type_void []       = { T_VOID,     T_END };
66 type type_size_t []     = { T_UINT,     T_END };
67
68
69
70 /*****************************************************************************/
71 /*                                   Code                                    */
72 /*****************************************************************************/
73
74
75
76 unsigned TypeLen (const type* T)
77 /* Return the length of the type string */
78 {
79     const type* Start = T;
80     while (*T != T_END) {
81         ++T;
82     }
83     return T - Start;
84 }
85
86
87
88 type* TypeCpy (type* Dest, const type* Src)
89 /* Copy a type string */
90 {
91     type T;
92     type* Orig = Dest;
93     do {
94         T = *Src++;
95         *Dest++ = T;
96     } while (T);
97     return Orig;
98 }
99
100
101
102 type* TypeCat (type* Dest, const type* Src)
103 /* Append Src */
104 {
105     TypeCpy (Dest + TypeLen (Dest), Src);
106     return Dest;
107 }
108
109
110
111 type* TypeDup (const type* T)
112 /* Create a copy of the given type on the heap */
113 {
114     unsigned Len = (TypeLen (T) + 1) * sizeof (type);
115     return (type*) memcpy (xmalloc (Len), T, Len);
116 }
117
118
119
120 type* TypeAlloc (unsigned Len)
121 /* Allocate memory for a type string of length Len. Len *must* include the
122  * trailing T_END.
123  */
124 {
125     return (type*) xmalloc (Len * sizeof (type));
126 }
127
128
129
130 void TypeFree (type* T)
131 /* Free a type string */
132 {
133     xfree (T);
134 }
135
136
137
138 int SignExtendChar (int C)
139 /* Do correct sign extension of a character */
140 {
141     if (SignedChars && (C & 0x80) != 0) {
142         return C | ~0xFF;
143     } else {
144         return C & 0xFF;
145     }
146 }
147
148
149
150 type GetDefaultChar (void)
151 /* Return the default char type (signed/unsigned) depending on the settings */
152 {
153     return SignedChars? T_SCHAR : T_UCHAR;
154 }
155
156
157
158 type* GetCharArrayType (unsigned Len)
159 /* Return the type for a char array of the given length */
160 {
161     /* Allocate memory for the type string */
162     type* T = TypeAlloc (1 + DECODE_SIZE + 2);
163
164     /* Fill the type string */
165     T [0]             = T_ARRAY;
166     T [DECODE_SIZE+1] = GetDefaultChar();
167     T [DECODE_SIZE+2] = T_END;
168
169     /* Encode the length in the type string */
170     Encode (T+1, Len);
171
172     /* Return the new type */
173     return T;
174 }
175
176
177
178 type* GetImplicitFuncType (void)
179 /* Return a type string for an inplicitly declared function */
180 {
181     /* Get a new function descriptor */
182     FuncDesc* F = NewFuncDesc ();
183
184     /* Allocate memory for the type string */
185     type* T = TypeAlloc (1 + DECODE_SIZE + 2);
186
187     /* Prepare the function descriptor */
188     F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
189     F->SymTab = &EmptySymTab;
190     F->TagTab = &EmptySymTab;
191
192     /* Fill the type string */
193     T [0]             = T_FUNC;
194     T [DECODE_SIZE+1] = T_INT;
195     T [DECODE_SIZE+2] = T_END;
196
197     /* Encode the function descriptor into the type string */
198     EncodePtr (T+1, F);
199
200     /* Return the new type */
201     return T;
202 }
203
204
205
206 type* PointerTo (const type* T)
207 /* Return a type string that is "pointer to T". The type string is allocated
208  * on the heap and may be freed after use.
209  */
210 {
211     /* Get the size of the type string including the terminator */
212     unsigned Size = TypeLen (T) + 1;
213
214     /* Allocate the new type string */
215     type* P = TypeAlloc (Size + 1);
216
217     /* Create the return type... */
218     P[0] = T_PTR;
219     memcpy (P+1, T, Size * sizeof (type));
220
221     /* ...and return it */
222     return P;
223 }
224
225
226
227 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
228 /* Check for a specific component of the type. If it is there, print the
229  * name and remove it. Return the type with the component removed.
230  */
231 {
232     if ((T & Mask) == Mask) {
233         fprintf (F, "%s ", Name);
234         T &= ~Mask;
235     }
236     return T;
237 }
238
239
240
241 void PrintType (FILE* F, const type* Type)
242 /* Output translation of type array. */
243 {
244     type T;
245     unsigned long Size;
246
247     /* Walk over the complete string */
248     while ((T = *Type++) != T_END) {
249
250         /* Print any qualifiers */
251         T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
252         T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
253
254         /* Signedness. Omit the signedness specifier for long and int */
255         if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
256             T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
257         }
258         T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
259
260         /* Now check the real type */
261         switch (T & T_MASK_TYPE) {
262             case T_TYPE_CHAR:
263                 fprintf (F, "char");
264                 break;
265             case T_TYPE_SHORT:
266                 fprintf (F, "short");
267                 break;
268             case T_TYPE_INT:
269                 fprintf (F, "int");
270                 break;
271             case T_TYPE_LONG:
272                 fprintf (F, "long");
273                 break;
274             case T_TYPE_LONGLONG:
275                 fprintf (F, "long long");
276                 break;
277             case T_TYPE_FLOAT:
278                 fprintf (F, "float");
279                 break;
280             case T_TYPE_DOUBLE:
281                 fprintf (F, "double");
282                 break;
283             case T_TYPE_VOID:
284                 fprintf (F, "void");
285                 break;
286             case T_TYPE_STRUCT:
287                 fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
288                 Type += DECODE_SIZE;
289                 break;
290             case T_TYPE_UNION:
291                 fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
292                 Type += DECODE_SIZE;
293                 break;
294             case T_TYPE_ARRAY:
295                 /* Recursive call */
296                 PrintType (F, Type + DECODE_SIZE);
297                 Size = Decode (Type);
298                 if (Size == 0) {
299                     fprintf (F, "[]");
300                 } else {
301                     fprintf (F, "[%lu]", Size);
302                 }
303                 return;
304             case T_TYPE_PTR:
305                 /* Recursive call */
306                 PrintType (F, Type);
307                 fprintf (F, "*");
308                 return;
309             case T_TYPE_FUNC:
310                 fprintf (F, "function returning ");
311                 Type += DECODE_SIZE;
312                 break;
313             default:
314                 fprintf (F, "unknown type: %04X", T);
315         }
316
317     }
318 }
319
320
321
322 void PrintFuncSig (FILE* F, const char* Name, type* Type)
323 /* Print a function signature. */
324 {
325     /* Get the function descriptor */
326     const FuncDesc* D = GetFuncDesc (Type);
327
328     /* Print a comment with the function signature */
329     PrintType (F, GetFuncReturn (Type));
330     if (D->Flags & FD_FASTCALL) {
331         fprintf (F, " __fastcall__");
332     }
333     fprintf (F, " %s (", Name);
334
335     /* Parameters */
336     if (D->Flags & FD_VOID_PARAM) {
337         fprintf (F, "void");
338     } else {
339         unsigned I;
340         SymEntry* E = D->SymTab->SymHead;
341         for (I = 0; I < D->ParamCount; ++I) {
342             if (I > 0) {
343                 fprintf (F, ", ");
344             }
345             PrintType (F, E->Type);
346             E = E->NextSym;
347         }
348     }
349
350     /* End of parameter list */
351     fprintf (F, ")");
352 }
353
354
355
356 void PrintRawType (FILE* F, const type* Type)
357 /* Print a type string in raw format (for debugging) */
358 {
359     while (*Type != T_END) {
360         fprintf (F, "%04X ", *Type++);
361     }
362     fprintf (F, "\n");
363 }
364
365
366
367 void Encode (type* Type, unsigned long Val)
368 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
369 {
370     int I;
371     for (I = 0; I < DECODE_SIZE; ++I) {
372         *Type++ = ((type) Val) | 0x8000;
373         Val >>= 15;
374     }
375 }
376
377
378
379 void EncodePtr (type* Type, void* P)
380 /* Encode a pointer into a type array */
381 {
382     Encode (Type, (unsigned long) P);
383 }
384
385
386
387 unsigned long Decode (const type* Type)
388 /* Decode */
389 {
390     int I;
391     unsigned long Val = 0;
392     for (I = DECODE_SIZE-1; I >= 0; I--) {
393         Val <<= 15;
394         Val |= (Type[I] & 0x7FFF);
395     }
396     return Val;
397 }
398
399
400
401 void* DecodePtr (const type* Type)
402 /* Decode a pointer from a type array */
403 {
404     return (void*) Decode (Type);
405 }
406
407
408
409 int HasEncode (const type* Type)
410 /* Return true if the given type has encoded data */
411 {
412     return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
413 }
414
415
416
417 void CopyEncode (const type* Source, type* Target)
418 /* Copy encoded data from Source to Target */
419 {
420     memcpy (Target, Source, DECODE_SIZE * sizeof (type));
421 }
422
423
424
425 type UnqualifiedType (type T)
426 /* Return the unqalified type */
427 {
428     return (T & ~T_MASK_QUAL);
429 }
430
431
432
433 unsigned SizeOf (const type* T)
434 /* Compute size of object represented by type array. */
435 {
436     SymEntry* Entry;
437
438     switch (UnqualifiedType (T[0])) {
439
440         case T_VOID:
441             return 0;   /* Assume voids have size zero */
442
443         case T_SCHAR:
444         case T_UCHAR:
445             return 1;
446
447         case T_SHORT:
448         case T_USHORT:
449         case T_INT:
450         case T_UINT:
451         case T_PTR:
452         case T_FUNC:    /* Maybe pointer to function */
453             return 2;
454
455         case T_LONG:
456         case T_ULONG:
457             return 4;
458
459         case T_LONGLONG:
460         case T_ULONGLONG:
461             return 8;
462
463         case T_ENUM:
464             return 2;
465
466         case T_FLOAT:
467         case T_DOUBLE:
468             return 4;
469
470         case T_STRUCT:
471         case T_UNION:
472             Entry = (SymEntry*) DecodePtr (T+1);
473             return Entry->V.S.Size;
474
475         case T_ARRAY:
476             return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
477
478         default:
479             Internal ("Unknown type in SizeOf: %04X", *T);
480             return 0;
481
482     }
483 }
484
485
486
487 unsigned PSizeOf (const type* T)
488 /* Compute size of pointer object. */
489 {
490     /* We are expecting a pointer expression */
491     CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
492
493     /* Skip the pointer or array token itself */
494     if (IsTypeArray (T)) {
495         return SizeOf (T + DECODE_SIZE + 1);
496     } else {
497         return SizeOf (T + 1);
498     }
499 }
500
501
502
503 unsigned CheckedSizeOf (const type* T)
504 /* Return the size of a data type. If the size is zero, emit an error and
505  * return some valid size instead (so the rest of the compiler doesn't have
506  * to work with invalid sizes).
507  */
508 {
509     unsigned Size = SizeOf (T);
510     if (Size == 0) {
511         Error ("Size of data type is unknown");
512         Size = 1;
513     }
514     return Size;
515 }
516
517
518
519 unsigned CheckedPSizeOf (const type* T)
520 /* Return the size of a data type that is pointed to by a pointer. If the
521  * size is zero, emit an error and return some valid size instead (so the
522  * rest of the compiler doesn't have to work with invalid sizes).
523  */
524 {
525     unsigned Size = PSizeOf (T);
526     if (Size == 0) {
527         Error ("Size of data type is unknown");
528         Size = 1;
529     }
530     return Size;
531 }
532
533
534
535 unsigned TypeOf (const type* T)
536 /* Get the code generator base type of the object */
537 {
538     FuncDesc* F;
539
540     switch (UnqualifiedType (T[0])) {
541
542         case T_SCHAR:
543             return CF_CHAR;
544
545         case T_UCHAR:
546             return CF_CHAR | CF_UNSIGNED;
547
548         case T_SHORT:
549         case T_INT:
550         case T_ENUM:
551             return CF_INT;
552
553         case T_USHORT:
554         case T_UINT:
555         case T_PTR:
556         case T_ARRAY:
557             return CF_INT | CF_UNSIGNED;
558
559         case T_LONG:
560             return CF_LONG;
561
562         case T_ULONG:
563             return CF_LONG | CF_UNSIGNED;
564
565         case T_FUNC:
566             F = (FuncDesc*) DecodePtr (T+1);
567             return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
568
569         case T_STRUCT:
570         case T_UNION:
571             /* Address of ... */
572             return CF_INT | CF_UNSIGNED;
573
574         default:
575             Error ("Illegal type");
576             return CF_INT;
577     }
578 }
579
580
581
582 type* Indirect (type* T)
583 /* Do one indirection for the given type, that is, return the type where the
584  * given type points to.
585  */
586 {
587     /* We are expecting a pointer expression */
588     CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
589
590     /* Skip the pointer or array token itself */
591     if (IsTypeArray (T)) {
592         return T + DECODE_SIZE + 1;
593     } else {
594         return T + 1;
595     }
596 }
597
598
599
600 int IsClassInt (const type* T)
601 /* Return true if this is an integer type */
602 {
603     return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
604 }
605
606
607
608 int IsClassFloat (const type* T)
609 /* Return true if this is a float type */
610 {
611     return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
612 }
613
614
615
616 int IsClassPtr (const type* T)
617 /* Return true if this is a pointer type */
618 {
619     return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
620 }
621
622
623
624 int IsClassStruct (const type* T)
625 /* Return true if this is a struct type */
626 {
627     return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
628 }
629
630
631
632 int IsSignUnsigned (const type* T)
633 /* Return true if this is an unsigned type */
634 {
635     return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
636 }
637
638
639
640 int IsQualConst (const type* T)
641 /* Return true if the given type has a const memory image */
642 {
643     return (GetQualifier (T) & T_QUAL_CONST) != 0;
644 }
645
646
647
648 int IsQualVolatile (const type* T)
649 /* Return true if the given type has a volatile type qualifier */
650 {
651     return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
652 }
653
654
655
656 int IsFastCallFunc (const type* T)
657 /* Return true if this is a function type or pointer to function with
658  * __fastcall__ calling conventions
659  */
660 {
661     FuncDesc* F = GetFuncDesc (T);
662     return (F->Flags & FD_FASTCALL) != 0;
663 }
664
665
666
667 int IsVariadicFunc (const type* T)
668 /* Return true if this is a function type or pointer to function type with
669  * variable parameter list
670  */
671 {
672     FuncDesc* F = GetFuncDesc (T);
673     return (F->Flags & FD_VARIADIC) != 0;
674 }
675
676
677
678 type GetType (const type* T)
679 /* Get the raw type */
680 {
681     PRECONDITION (T[0] != T_END);
682     return (T[0] & T_MASK_TYPE);
683 }
684
685
686
687 type GetClass (const type* T)
688 /* Get the class of a type string */
689 {
690     PRECONDITION (T[0] != T_END);
691     return (T[0] & T_MASK_CLASS);
692 }
693
694
695
696 type GetSignedness (const type* T)
697 /* Get the sign of a type */
698 {
699     PRECONDITION (T[0] != T_END);
700     return (T[0] & T_MASK_SIGN);
701 }
702
703
704
705 type GetSizeModifier (const type* T)
706 /* Get the size modifier of a type */
707 {
708     PRECONDITION (T[0] != T_END);
709     return (T[0] & T_MASK_SIZE);
710 }
711
712
713
714 type GetQualifier (const type* T)
715 /* Get the qualifier from the given type string */
716 {
717     /* If this is an array, look at the element type, otherwise look at the
718      * type itself.
719      */
720     if (IsTypeArray (T)) {
721         T += DECODE_SIZE + 1;
722     }
723     return (T[0] & T_QUAL_CONST);
724 }
725
726
727
728 FuncDesc* GetFuncDesc (const type* T)
729 /* Get the FuncDesc pointer from a function or pointer-to-function type */
730 {
731     if (UnqualifiedType (T[0]) == T_PTR) {
732         /* Pointer to function */
733         ++T;
734     }
735
736     /* Be sure it's a function type */
737     CHECK (T[0] == T_FUNC);
738
739     /* Decode the function descriptor and return it */
740     return (FuncDesc*) DecodePtr (T+1);
741 }
742
743
744
745 type* GetFuncReturn (type* T)
746 /* Return a pointer to the return type of a function or pointer-to-function type */
747 {
748     if (UnqualifiedType (T[0]) == T_PTR) {
749         /* Pointer to function */
750         ++T;
751     }
752
753     /* Be sure it's a function type */
754     CHECK (T[0] == T_FUNC);
755
756     /* Return a pointer to the return type */
757     return T + 1 + DECODE_SIZE;
758
759 }
760
761
762