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