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