]> git.sur5r.net Git - cc65/blob - src/cc65/datatype.c
c7070725735768c8e2b76e76a369d9011b9ef276
[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     /* If the first field has const and/or volatile qualifiers, print and
226      * remove them.
227      */
228     type T = *Type++;
229     T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
230     T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
231
232     /* Walk over the complete string */
233     do {
234
235         /* Check for the sizes */
236         T = PrintTypeComp (F, T, T_SIZE_SHORT, "short");
237         T = PrintTypeComp (F, T, T_SIZE_LONG, "long");
238         T = PrintTypeComp (F, T, T_SIZE_LONGLONG, "long long");
239
240         /* Signedness */
241         T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
242         T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
243
244         /* Now check the real type */
245         switch (T & T_MASK_TYPE) {
246             case T_TYPE_CHAR:
247                 fprintf (F, "char\n");
248                 break;
249             case T_TYPE_INT:
250                 fprintf (F, "int\n");
251                 break;
252             case T_TYPE_FLOAT:
253                 fprintf (F, "float\n");
254                 break;
255             case T_TYPE_DOUBLE:
256                 fprintf (F, "double\n");
257                 break;
258             case T_TYPE_VOID:
259                 fprintf (F, "void\n");
260                 break;
261             case T_TYPE_STRUCT:
262                 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
263                 Type += DECODE_SIZE;
264                 break;
265             case T_TYPE_UNION:
266                 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
267                 Type += DECODE_SIZE;
268                 break;
269             case T_TYPE_ARRAY:
270                 fprintf (F, "array[%lu] of ", Decode (Type));
271                 Type += DECODE_SIZE;
272                 break;
273             case T_TYPE_PTR:
274                 fprintf (F, "pointer to ");
275                 break;
276             case T_TYPE_FUNC:
277                 fprintf (F, "function returning ");
278                 Type += DECODE_SIZE;
279                 break;
280             default:
281                 fprintf (F, "unknown type: %04X\n", T);
282         }
283
284         /* Get the next type element */
285         T = *Type++;
286
287     } while (T != T_END);
288 }
289
290
291
292 void PrintRawType (FILE* F, const type* Type)
293 /* Print a type string in raw format (for debugging) */
294 {
295     while (*Type != T_END) {
296         fprintf (F, "%04X ", *Type++);
297     }
298     fprintf (F, "\n");
299 }
300
301
302
303 void Encode (type* Type, unsigned long Val)
304 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
305 {
306     int I;
307     for (I = 0; I < DECODE_SIZE; ++I) {
308         *Type++ = ((type) Val) | 0x8000;
309         Val >>= 15;
310     }
311 }
312
313
314
315 void EncodePtr (type* Type, void* P)
316 /* Encode a pointer into a type array */
317 {
318     Encode (Type, (unsigned long) P);
319 }
320
321
322
323 unsigned long Decode (const type* Type)
324 /* Decode */
325 {
326     int I;
327     unsigned long Val = 0;
328     for (I = DECODE_SIZE-1; I >= 0; I--) {
329         Val <<= 15;
330         Val |= (Type[I] & 0x7FFF);
331     }
332     return Val;
333 }
334
335
336
337 void* DecodePtr (const type* Type)
338 /* Decode a pointer from a type array */
339 {
340     return (void*) Decode (Type);
341 }
342
343
344
345 int HasEncode (const type* Type)
346 /* Return true if the given type has encoded data */
347 {
348     return IsStruct (Type) || IsArray (Type) || IsFunc (Type);
349 }
350
351
352
353 void CopyEncode (const type* Source, type* Target)
354 /* Copy encoded data from Source to Target */
355 {
356     memcpy (Target, Source, DECODE_SIZE * sizeof (type));
357 }
358
359
360
361 unsigned SizeOf (const type* T)
362 /* Compute size of object represented by type array. */
363 {
364     SymEntry* Entry;
365
366     switch (*T) {
367
368         case T_VOID:
369             Error (ERR_ILLEGAL_SIZE);
370             return 0;
371
372         case T_SCHAR:
373         case T_UCHAR:
374             return 1;
375
376         case T_SHORT:
377         case T_USHORT:
378         case T_INT:
379         case T_UINT:
380         case T_PTR:
381             return 2;
382
383         case T_LONG:
384         case T_ULONG:
385             return 4;
386
387         case T_LONGLONG:
388         case T_ULONGLONG:
389             return 8;
390
391         case T_ENUM:
392             return 2;
393
394         case T_FLOAT:
395         case T_DOUBLE:
396             return 4;
397
398         case T_STRUCT:
399         case T_UNION:
400             Entry = DecodePtr (T+1);
401             return Entry->V.S.Size;
402
403         case T_ARRAY:
404             return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
405
406         default:
407             Internal ("Unknown type in SizeOf: %04X", *T);
408             return 0;
409
410     }
411 }
412
413
414
415 unsigned PSizeOf (const type* T)
416 /* Compute size of pointer object. */
417 {
418     /* We are expecting a pointer expression */
419     CHECK ((*T & T_CLASS_PTR) != 0);
420
421     /* Skip the pointer or array token itself */
422     if (*T == T_ARRAY) {
423         return SizeOf (T + DECODE_SIZE + 1);
424     } else {
425         return SizeOf (T + 1);
426     }
427 }
428
429
430
431 unsigned TypeOf (const type* Type)
432 /* Get the code generator base type of the object */
433 {
434     FuncDesc* F;
435
436     switch (*Type) {
437
438         case T_SCHAR:
439             return CF_CHAR;
440
441         case T_UCHAR:
442             return CF_CHAR | CF_UNSIGNED;
443
444         case T_SHORT:
445         case T_INT:
446         case T_ENUM:
447             return CF_INT;
448
449         case T_USHORT:
450         case T_UINT:
451         case T_PTR:
452         case T_ARRAY:
453             return CF_INT | CF_UNSIGNED;
454
455         case T_LONG:
456             return CF_LONG;
457
458         case T_ULONG:
459             return CF_LONG | CF_UNSIGNED;
460
461         case T_FUNC:
462             F = DecodePtr (Type+1);
463             return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
464
465         case T_STRUCT:
466         case T_UNION:
467             /* Address of ... */
468             return CF_INT | CF_UNSIGNED;
469
470         default:
471             Error (ERR_ILLEGAL_TYPE);
472             return CF_INT;
473     }
474 }
475
476
477
478 type* Indirect (type* T)
479 /* Do one indirection for the given type, that is, return the type where the
480  * given type points to.
481  */
482 {
483     /* We are expecting a pointer expression */
484     CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
485
486     /* Skip the pointer or array token itself */
487     if (*T == T_ARRAY) {
488         return T + DECODE_SIZE + 1;
489     } else {
490         return T + 1;
491     }
492 }
493
494
495
496 int IsTypeVoid (const type* T)
497 /* Return true if this is a void type */
498 {
499     return (T[0] == T_VOID && T[1] == T_END);
500 }
501
502
503
504 int IsPtr (const type* T)
505 /* Return true if this is a pointer type */
506 {
507     return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
508 }
509
510
511
512 int IsChar (const type* T)
513 /* Return true if this is a character type */
514 {
515     return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR && T[1] == T_END;
516 }
517
518
519
520 int IsInt (const type* T)
521 /* Return true if this is an integer type */
522 {
523     return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
524 }
525
526
527
528 int IsLong (const type* T)
529 /* Return true if this is a long type (signed or unsigned) */
530 {
531     return (T[0] & T_MASK_SIZE) == T_SIZE_LONG;
532 }
533
534
535
536 int IsUnsigned (const type* T)
537 /* Return true if this is an unsigned type */
538 {
539     return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
540 }
541
542
543
544 int IsStruct (const type* T)
545 /* Return true if this is a struct type */
546 {
547     return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
548 }
549
550
551
552 int IsFunc (const type* T)
553 /* Return true if this is a function type */
554 {
555     return (T[0] == T_FUNC);
556 }
557
558
559
560 int IsFastCallFunc (const type* T)
561 /* Return true if this is a function type with __fastcall__ calling conventions */
562 {
563     FuncDesc* F;
564     CHECK (T[0] == T_FUNC);
565     F = DecodePtr (T+1);
566     return (F->Flags & FD_FASTCALL) != 0;
567 }
568
569
570
571 int IsFuncPtr (const type* T)
572 /* Return true if this is a function pointer */
573 {
574     return (T[0] == T_PTR && T[1] == T_FUNC);
575 }
576
577
578
579 int IsArray (const type* T)
580 /* Return true if this is an array type */
581 {
582     return (T[0] == T_ARRAY);
583 }
584
585
586
587 struct FuncDesc* GetFuncDesc (const type* T)
588 /* Get the FuncDesc pointer from a function or pointer-to-function type */
589 {
590     if (T[0] == T_PTR) {
591         /* Pointer to function */
592         ++T;
593     }
594
595     /* Be sure it's a function type */
596     CHECK (T[0] == T_FUNC);
597
598     /* Decode the function descriptor and return it */
599     return DecodePtr (T+1);
600 }
601
602
603
604