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