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