]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.c
Working on the new backend
[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
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                 fprintf (F, "array[%lu] of ", Decode (Type));
284                 Type += DECODE_SIZE;
285                 break;
286             case T_TYPE_PTR:
287                 fprintf (F, "pointer to ");
288                 break;
289             case T_TYPE_FUNC:
290                 fprintf (F, "function returning ");
291                 Type += DECODE_SIZE;
292                 break;
293             default:
294                 fprintf (F, "unknown type: %04X", T);
295         }
296
297     }
298 }
299
300
301
302 void PrintFuncSig (FILE* F, const char* Name, type* Type)
303 /* Print a function signature. */
304 {
305     /* Get the function descriptor */
306     const FuncDesc* D = GetFuncDesc (Type);
307
308     /* Print a comment with the function signature */
309     PrintType (F, GetFuncReturn (Type));
310     if (D->Flags & FD_FASTCALL) {
311         fprintf (F, " __fastcall__");
312     }
313     fprintf (F, " %s (", Name);
314
315     /* Parameters */
316     if (D->Flags & FD_VOID_PARAM) {
317         fprintf (F, "void");
318     } else {
319         unsigned I;
320         SymEntry* E = D->SymTab->SymHead;
321         for (I = 0; I < D->ParamCount; ++I) {
322             if (I > 0) {
323                 fprintf (F, ", ");
324             }
325             PrintType (F, E->Type);
326             E = E->NextSym;
327         }
328     }
329
330     /* End of parameter list */
331     fprintf (F, ")");
332 }
333
334
335
336 void PrintRawType (FILE* F, const type* Type)
337 /* Print a type string in raw format (for debugging) */
338 {
339     while (*Type != T_END) {
340         fprintf (F, "%04X ", *Type++);
341     }
342     fprintf (F, "\n");
343 }
344
345
346
347 void Encode (type* Type, unsigned long Val)
348 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
349 {
350     int I;
351     for (I = 0; I < DECODE_SIZE; ++I) {
352         *Type++ = ((type) Val) | 0x8000;
353         Val >>= 15;
354     }
355 }
356
357
358
359 void EncodePtr (type* Type, void* P)
360 /* Encode a pointer into a type array */
361 {
362     Encode (Type, (unsigned long) P);
363 }
364
365
366
367 unsigned long Decode (const type* Type)
368 /* Decode */
369 {
370     int I;
371     unsigned long Val = 0;
372     for (I = DECODE_SIZE-1; I >= 0; I--) {
373         Val <<= 15;
374         Val |= (Type[I] & 0x7FFF);
375     }
376     return Val;
377 }
378
379
380
381 void* DecodePtr (const type* Type)
382 /* Decode a pointer from a type array */
383 {
384     return (void*) Decode (Type);
385 }
386
387
388
389 int HasEncode (const type* Type)
390 /* Return true if the given type has encoded data */
391 {
392     return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
393 }
394
395
396
397 void CopyEncode (const type* Source, type* Target)
398 /* Copy encoded data from Source to Target */
399 {
400     memcpy (Target, Source, DECODE_SIZE * sizeof (type));
401 }
402
403
404
405 type UnqualifiedType (type T)
406 /* Return the unqalified type */
407 {
408     return (T & ~T_MASK_QUAL);
409 }
410
411
412
413 unsigned SizeOf (const type* T)
414 /* Compute size of object represented by type array. */
415 {
416     SymEntry* Entry;
417
418     switch (UnqualifiedType (T[0])) {
419
420         case T_VOID:
421             Error ("Variable has unknown size");
422             return 1;   /* Return something that makes sense */
423
424         case T_SCHAR:
425         case T_UCHAR:
426             return 1;
427
428         case T_SHORT:
429         case T_USHORT:
430         case T_INT:
431         case T_UINT:
432         case T_PTR:
433             return 2;
434
435         case T_LONG:
436         case T_ULONG:
437             return 4;
438
439         case T_LONGLONG:
440         case T_ULONGLONG:
441             return 8;
442
443         case T_ENUM:
444             return 2;
445
446         case T_FLOAT:
447         case T_DOUBLE:
448             return 4;
449
450         case T_STRUCT:
451         case T_UNION:
452             Entry = (SymEntry*) DecodePtr (T+1);
453             return Entry->V.S.Size;
454
455         case T_ARRAY:
456             return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
457
458         default:
459             Internal ("Unknown type in SizeOf: %04X", *T);
460             return 0;
461
462     }
463 }
464
465
466
467 unsigned PSizeOf (const type* T)
468 /* Compute size of pointer object. */
469 {
470     /* We are expecting a pointer expression */
471     CHECK ((*T & T_CLASS_PTR) != 0);
472
473     /* Skip the pointer or array token itself */
474     if (IsTypeArray (T)) {
475         return SizeOf (T + DECODE_SIZE + 1);
476     } else {
477         return SizeOf (T + 1);
478     }
479 }
480
481
482
483 unsigned TypeOf (const type* T)
484 /* Get the code generator base type of the object */
485 {
486     FuncDesc* F;
487
488     switch (UnqualifiedType (T[0])) {
489
490         case T_SCHAR:
491             return CF_CHAR;
492
493         case T_UCHAR:
494             return CF_CHAR | CF_UNSIGNED;
495
496         case T_SHORT:
497         case T_INT:
498         case T_ENUM:
499             return CF_INT;
500
501         case T_USHORT:
502         case T_UINT:
503         case T_PTR:
504         case T_ARRAY:
505             return CF_INT | CF_UNSIGNED;
506
507         case T_LONG:
508             return CF_LONG;
509
510         case T_ULONG:
511             return CF_LONG | CF_UNSIGNED;
512
513         case T_FUNC:
514             F = (FuncDesc*) DecodePtr (T+1);
515             return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
516
517         case T_STRUCT:
518         case T_UNION:
519             /* Address of ... */
520             return CF_INT | CF_UNSIGNED;
521
522         default:
523             Error ("Illegal type");
524             return CF_INT;
525     }
526 }
527
528
529
530 type* Indirect (type* T)
531 /* Do one indirection for the given type, that is, return the type where the
532  * given type points to.
533  */
534 {
535     /* We are expecting a pointer expression */
536     CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
537
538     /* Skip the pointer or array token itself */
539     if (IsTypeArray (T)) {
540         return T + DECODE_SIZE + 1;
541     } else {
542         return T + 1;
543     }
544 }
545
546
547
548 int IsTypeChar (const type* T)
549 /* Return true if this is a character type */
550 {
551     return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
552 }
553
554
555
556 int IsTypeInt (const type* T)
557 /* Return true if this is an int type (signed or unsigned) */
558 {
559     return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
560 }
561
562
563
564 int IsTypeLong (const type* T)
565 /* Return true if this is a long type (signed or unsigned) */
566 {
567     return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
568 }
569
570
571
572 int IsTypeFloat (const type* T)
573 /* Return true if this is a float type */
574 {
575     return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
576 }
577
578
579
580 int IsTypeDouble (const type* T)
581 /* Return true if this is a double type */
582 {
583     return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
584 }
585
586
587
588 int IsTypePtr (const type* T)
589 /* Return true if this is a pointer type */
590 {
591     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
592 }
593
594
595
596 int IsTypeArray (const type* T)
597 /* Return true if this is an array type */
598 {
599     return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
600 }
601
602
603
604 int IsTypeVoid (const type* T)
605 /* Return true if this is a void type */
606 {
607     return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
608 }
609
610
611
612 int IsTypeFunc (const type* T)
613 /* Return true if this is a function class */
614 {
615     return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
616 }
617
618
619
620 int IsClassInt (const type* T)
621 /* Return true if this is an integer type */
622 {
623     return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
624 }
625
626
627
628 int IsClassFloat (const type* T)
629 /* Return true if this is a float type */
630 {
631     return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
632 }
633
634
635
636 int IsClassPtr (const type* T)
637 /* Return true if this is a pointer type */
638 {
639     return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
640 }
641
642
643
644 int IsClassStruct (const type* T)
645 /* Return true if this is a struct type */
646 {
647     return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
648 }
649
650
651
652 int IsSignUnsigned (const type* T)
653 /* Return true if this is an unsigned type */
654 {
655     return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
656 }
657
658
659
660 int IsQualConst (const type* T)
661 /* Return true if the given type has a const memory image */
662 {
663     return (GetQualifier (T) & T_QUAL_CONST) != 0;
664 }
665
666
667
668 int IsQualVolatile (const type* T)
669 /* Return true if the given type has a volatile type qualifier */
670 {
671     return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
672 }
673
674
675
676 int IsFastCallFunc (const type* T)
677 /* Return true if this is a function type with __fastcall__ calling conventions */
678 {
679     FuncDesc* F;
680     CHECK (IsTypeFunc (T));
681     F = (FuncDesc*) DecodePtr (T+1);
682     return (F->Flags & FD_FASTCALL) != 0;
683 }
684
685
686
687 int IsVariadicFunc (const type* T)
688 /* Return true if this is a function type with variable parameter list */
689 {
690     FuncDesc* F;
691     CHECK (IsTypeFunc (T));
692     F = (FuncDesc*) DecodePtr (T+1);
693     return (F->Flags & FD_VARIADIC) != 0;
694 }
695
696
697
698 int IsTypeFuncPtr (const type* T)
699 /* Return true if this is a function pointer */
700 {
701     return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
702 }
703
704
705
706 type GetType (const type* T)
707 /* Get the raw type */
708 {
709     PRECONDITION (T[0] != T_END);
710     return (T[0] & T_MASK_TYPE);
711 }
712
713
714
715 type GetClass (const type* T)
716 /* Get the class of a type string */
717 {
718     PRECONDITION (T[0] != T_END);
719     return (T[0] & T_MASK_CLASS);
720 }
721
722
723
724 type GetSignedness (const type* T)
725 /* Get the sign of a type */
726 {
727     PRECONDITION (T[0] != T_END);
728     return (T[0] & T_MASK_SIGN);
729 }
730
731
732
733 type GetSizeModifier (const type* T)
734 /* Get the size modifier of a type */
735 {
736     PRECONDITION (T[0] != T_END);
737     return (T[0] & T_MASK_SIZE);
738 }
739
740
741
742 type GetQualifier (const type* T)
743 /* Get the qualifier from the given type string */
744 {
745     /* If this is an array, look at the element type, otherwise look at the
746      * type itself.
747      */
748     if (IsTypeArray (T)) {
749         T += DECODE_SIZE + 1;
750     }
751     return (T[0] & T_QUAL_CONST);
752 }
753
754
755
756 FuncDesc* GetFuncDesc (const type* T)
757 /* Get the FuncDesc pointer from a function or pointer-to-function type */
758 {
759     if (T[0] == T_PTR) {
760         /* Pointer to function */
761         ++T;
762     }
763
764     /* Be sure it's a function type */
765     CHECK (T[0] == T_FUNC);
766
767     /* Decode the function descriptor and return it */
768     return (FuncDesc*) DecodePtr (T+1);
769 }
770
771
772
773 type* GetFuncReturn (type* T)
774 /* Return a pointer to the return type of a function or pointer-to-function type */
775 {
776     if (T[0] == T_PTR) {
777         /* Pointer to function */
778         ++T;
779     }
780
781     /* Be sure it's a function type */
782     CHECK (T[0] == T_FUNC);
783
784     /* Return a pointer to the return type */
785     return T + 1 + DECODE_SIZE;
786
787 }
788
789
790