]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.c
Fixed a bug
[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             return 2;
442
443         case T_LONG:
444         case T_ULONG:
445             return 4;
446
447         case T_LONGLONG:
448         case T_ULONGLONG:
449             return 8;
450
451         case T_ENUM:
452             return 2;
453
454         case T_FLOAT:
455         case T_DOUBLE:
456             return 4;
457
458         case T_STRUCT:
459         case T_UNION:
460             Entry = (SymEntry*) DecodePtr (T+1);
461             return Entry->V.S.Size;
462
463         case T_ARRAY:
464             return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
465
466         default:
467             Internal ("Unknown type in SizeOf: %04X", *T);
468             return 0;
469
470     }
471 }
472
473
474
475 unsigned PSizeOf (const type* T)
476 /* Compute size of pointer object. */
477 {
478     /* We are expecting a pointer expression */
479     CHECK ((*T & T_CLASS_PTR) != 0);
480
481     /* Skip the pointer or array token itself */
482     if (IsTypeArray (T)) {
483         return SizeOf (T + DECODE_SIZE + 1);
484     } else {
485         return SizeOf (T + 1);
486     }
487 }
488
489
490
491 unsigned TypeOf (const type* T)
492 /* Get the code generator base type of the object */
493 {
494     FuncDesc* F;
495
496     switch (UnqualifiedType (T[0])) {
497
498         case T_SCHAR:
499             return CF_CHAR;
500
501         case T_UCHAR:
502             return CF_CHAR | CF_UNSIGNED;
503
504         case T_SHORT:
505         case T_INT:
506         case T_ENUM:
507             return CF_INT;
508
509         case T_USHORT:
510         case T_UINT:
511         case T_PTR:
512         case T_ARRAY:
513             return CF_INT | CF_UNSIGNED;
514
515         case T_LONG:
516             return CF_LONG;
517
518         case T_ULONG:
519             return CF_LONG | CF_UNSIGNED;
520
521         case T_FUNC:
522             F = (FuncDesc*) DecodePtr (T+1);
523             return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
524
525         case T_STRUCT:
526         case T_UNION:
527             /* Address of ... */
528             return CF_INT | CF_UNSIGNED;
529
530         default:
531             Error ("Illegal type");
532             return CF_INT;
533     }
534 }
535
536
537
538 type* Indirect (type* T)
539 /* Do one indirection for the given type, that is, return the type where the
540  * given type points to.
541  */
542 {
543     /* We are expecting a pointer expression */
544     CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
545
546     /* Skip the pointer or array token itself */
547     if (IsTypeArray (T)) {
548         return T + DECODE_SIZE + 1;
549     } else {
550         return T + 1;
551     }
552 }
553
554
555
556 int IsTypeChar (const type* T)
557 /* Return true if this is a character type */
558 {
559     return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
560 }
561
562
563
564 int IsTypeInt (const type* T)
565 /* Return true if this is an int type (signed or unsigned) */
566 {
567     return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
568 }
569
570
571
572 int IsTypeLong (const type* T)
573 /* Return true if this is a long type (signed or unsigned) */
574 {
575     return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
576 }
577
578
579
580 int IsTypeFloat (const type* T)
581 /* Return true if this is a float type */
582 {
583     return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
584 }
585
586
587
588 int IsTypeDouble (const type* T)
589 /* Return true if this is a double type */
590 {
591     return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
592 }
593
594
595
596 int IsTypePtr (const type* T)
597 /* Return true if this is a pointer type */
598 {
599     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
600 }
601
602
603
604 int IsTypeArray (const type* T)
605 /* Return true if this is an array type */
606 {
607     return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
608 }
609
610
611
612 int IsTypeVoid (const type* T)
613 /* Return true if this is a void type */
614 {
615     return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
616 }
617
618
619
620 int IsTypeFunc (const type* T)
621 /* Return true if this is a function class */
622 {
623     return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
624 }
625
626
627
628 int IsClassInt (const type* T)
629 /* Return true if this is an integer type */
630 {
631     return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
632 }
633
634
635
636 int IsClassFloat (const type* T)
637 /* Return true if this is a float type */
638 {
639     return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
640 }
641
642
643
644 int IsClassPtr (const type* T)
645 /* Return true if this is a pointer type */
646 {
647     return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
648 }
649
650
651
652 int IsClassStruct (const type* T)
653 /* Return true if this is a struct type */
654 {
655     return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
656 }
657
658
659
660 int IsSignUnsigned (const type* T)
661 /* Return true if this is an unsigned type */
662 {
663     return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
664 }
665
666
667
668 int IsQualConst (const type* T)
669 /* Return true if the given type has a const memory image */
670 {
671     return (GetQualifier (T) & T_QUAL_CONST) != 0;
672 }
673
674
675
676 int IsQualVolatile (const type* T)
677 /* Return true if the given type has a volatile type qualifier */
678 {
679     return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
680 }
681
682
683
684 int IsFastCallFunc (const type* T)
685 /* Return true if this is a function type or pointer to function with
686  * __fastcall__ calling conventions
687  */
688 {
689     FuncDesc* F = GetFuncDesc (T);
690     return (F->Flags & FD_FASTCALL) != 0;
691 }
692
693
694
695 int IsVariadicFunc (const type* T)
696 /* Return true if this is a function type or pointer to function type with
697  * variable parameter list
698  */
699 {
700     FuncDesc* F = GetFuncDesc (T);
701     return (F->Flags & FD_VARIADIC) != 0;
702 }
703
704
705
706 int IsTypeFuncPtr (const type* T)
707 /* Return true if this is a function pointer */
708 {
709     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
710 }
711
712
713
714 type GetType (const type* T)
715 /* Get the raw type */
716 {
717     PRECONDITION (T[0] != T_END);
718     return (T[0] & T_MASK_TYPE);
719 }
720
721
722
723 type GetClass (const type* T)
724 /* Get the class of a type string */
725 {
726     PRECONDITION (T[0] != T_END);
727     return (T[0] & T_MASK_CLASS);
728 }
729
730
731
732 type GetSignedness (const type* T)
733 /* Get the sign of a type */
734 {
735     PRECONDITION (T[0] != T_END);
736     return (T[0] & T_MASK_SIGN);
737 }
738
739
740
741 type GetSizeModifier (const type* T)
742 /* Get the size modifier of a type */
743 {
744     PRECONDITION (T[0] != T_END);
745     return (T[0] & T_MASK_SIZE);
746 }
747
748
749
750 type GetQualifier (const type* T)
751 /* Get the qualifier from the given type string */
752 {
753     /* If this is an array, look at the element type, otherwise look at the
754      * type itself.
755      */
756     if (IsTypeArray (T)) {
757         T += DECODE_SIZE + 1;
758     }
759     return (T[0] & T_QUAL_CONST);
760 }
761
762
763
764 FuncDesc* GetFuncDesc (const type* T)
765 /* Get the FuncDesc pointer from a function or pointer-to-function type */
766 {
767     if (UnqualifiedType (T[0]) == T_PTR) {      
768         /* Pointer to function */
769         ++T;
770     }
771
772     /* Be sure it's a function type */
773     CHECK (T[0] == T_FUNC);
774
775     /* Decode the function descriptor and return it */
776     return (FuncDesc*) DecodePtr (T+1);
777 }
778
779
780
781 type* GetFuncReturn (type* T)
782 /* Return a pointer to the return type of a function or pointer-to-function type */
783 {
784     if (UnqualifiedType (T[0]) == T_PTR) {
785         /* Pointer to function */
786         ++T;
787     }
788
789     /* Be sure it's a function type */
790     CHECK (T[0] == T_FUNC);
791
792     /* Return a pointer to the return type */
793     return T + 1 + DECODE_SIZE;
794
795 }
796
797
798