1 /*****************************************************************************/
5 /* Type string handling for the cc65 C compiler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
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 };
70 /*****************************************************************************/
72 /*****************************************************************************/
76 unsigned TypeLen (const type* T)
77 /* Return the length of the type string */
79 const type* Start = T;
88 int TypeCmp (const type* T1, const type* T2)
89 /* Compare two type strings */
96 } while (D == 0 && A != 0);
102 type* TypeCpy (type* Dest, const type* Src)
103 /* Copy a type string */
116 type* TypeCat (type* Dest, const type* Src)
119 TypeCpy (Dest + TypeLen (Dest), Src);
125 type* TypeDup (const type* T)
126 /* Create a copy of the given type on the heap */
128 unsigned Len = (TypeLen (T) + 1) * sizeof (type);
129 return memcpy (xmalloc (Len), T, Len);
134 type* TypeAlloc (unsigned Len)
135 /* Allocate memory for a type string of length Len. Len *must* include the
139 return xmalloc (Len * sizeof (type));
144 void TypeFree (type* T)
145 /* Free a type string */
152 type GetDefaultChar (void)
153 /* Return the default char type (signed/unsigned) depending on the settings */
155 return SignedChars? T_SCHAR : T_UCHAR;
160 type* GetCharArrayType (unsigned Len)
161 /* Return the type for a char array of the given length */
163 /* Allocate memory for the type string */
164 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
166 /* Fill the type string */
168 T [DECODE_SIZE+1] = GetDefaultChar();
169 T [DECODE_SIZE+2] = T_END;
171 /* Encode the length in the type string */
174 /* Return the new type */
180 type* GetImplicitFuncType (void)
181 /* Return a type string for an inplicitly declared function */
183 /* Get a new function descriptor */
184 FuncDesc* F = NewFuncDesc ();
186 /* Allocate memory for the type string */
187 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
189 /* Prepare the function descriptor */
190 F->Flags = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
191 F->SymTab = &EmptySymTab;
192 F->TagTab = &EmptySymTab;
194 /* Fill the type string */
196 T [DECODE_SIZE+1] = T_INT;
197 T [DECODE_SIZE+2] = T_END;
199 /* Encode the function descriptor into the type string */
202 /* Return the new type */
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.
213 if ((T & Mask) == Mask) {
214 fprintf (F, "%s ", Name);
222 void PrintType (FILE* F, const type* Type)
223 /* Output translation of type array. */
228 /* Walk over the complete string */
229 while ((T = *Type++) != T_END) {
231 /* Print any qualifiers */
232 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
233 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
236 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
237 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
239 /* Now check the real type */
240 switch (T & T_MASK_TYPE) {
242 fprintf (F, "char\n");
245 fprintf (F, "short\n");
248 fprintf (F, "int\n");
251 fprintf (F, "long\n");
253 case T_TYPE_LONGLONG:
254 fprintf (F, "long long\n");
257 fprintf (F, "float\n");
260 fprintf (F, "double\n");
263 fprintf (F, "void\n");
266 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
270 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
274 fprintf (F, "array[%lu] of ", Decode (Type));
278 fprintf (F, "pointer to ");
281 fprintf (F, "function returning ");
285 fprintf (F, "unknown type: %04X\n", T);
293 void PrintRawType (FILE* F, const type* Type)
294 /* Print a type string in raw format (for debugging) */
296 while (*Type != T_END) {
297 fprintf (F, "%04X ", *Type++);
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 */
308 for (I = 0; I < DECODE_SIZE; ++I) {
309 *Type++ = ((type) Val) | 0x8000;
316 void EncodePtr (type* Type, void* P)
317 /* Encode a pointer into a type array */
319 Encode (Type, (unsigned long) P);
324 unsigned long Decode (const type* Type)
328 unsigned long Val = 0;
329 for (I = DECODE_SIZE-1; I >= 0; I--) {
331 Val |= (Type[I] & 0x7FFF);
338 void* DecodePtr (const type* Type)
339 /* Decode a pointer from a type array */
341 return (void*) Decode (Type);
346 int HasEncode (const type* Type)
347 /* Return true if the given type has encoded data */
349 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
354 void CopyEncode (const type* Source, type* Target)
355 /* Copy encoded data from Source to Target */
357 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
362 type UnqualifiedType (type T)
363 /* Return the unqalified type */
365 return (T & ~T_MASK_QUAL);
370 unsigned SizeOf (const type* T)
371 /* Compute size of object represented by type array. */
375 switch (UnqualifiedType (T[0])) {
378 Error (ERR_ILLEGAL_SIZE);
409 Entry = DecodePtr (T+1);
410 return Entry->V.S.Size;
413 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
416 Internal ("Unknown type in SizeOf: %04X", *T);
424 unsigned PSizeOf (const type* T)
425 /* Compute size of pointer object. */
427 /* We are expecting a pointer expression */
428 CHECK ((*T & T_CLASS_PTR) != 0);
430 /* Skip the pointer or array token itself */
431 if (IsTypeArray (T)) {
432 return SizeOf (T + DECODE_SIZE + 1);
434 return SizeOf (T + 1);
440 unsigned TypeOf (const type* T)
441 /* Get the code generator base type of the object */
445 switch (UnqualifiedType (T[0])) {
451 return CF_CHAR | CF_UNSIGNED;
462 return CF_INT | CF_UNSIGNED;
468 return CF_LONG | CF_UNSIGNED;
472 return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
477 return CF_INT | CF_UNSIGNED;
480 Error (ERR_ILLEGAL_TYPE);
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.
492 /* We are expecting a pointer expression */
493 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
495 /* Skip the pointer or array token itself */
496 if (IsTypeArray (T)) {
497 return T + DECODE_SIZE + 1;
505 int IsConst (const type* T)
506 /* Return true if the given type has a const memory image */
508 /* If this is an array, look at the element type, otherwise look at the
511 if (IsTypeArray (T)) {
512 T += DECODE_SIZE + 1;
514 return ((T[0] & T_QUAL_CONST) == T_QUAL_CONST);
519 int IsTypeVoid (const type* T)
520 /* Return true if this is a void type */
522 return ((T[0] & T_MASK_TYPE) == T_TYPE_VOID && T[1] == T_END);
527 int IsClassPtr (const type* T)
528 /* Return true if this is a pointer type */
530 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
535 int IsTypeChar (const type* T)
536 /* Return true if this is a character type */
538 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR && T[1] == T_END;
543 int IsClassInt (const type* T)
544 /* Return true if this is an integer type */
546 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
551 int IsTypeLong (const type* T)
552 /* Return true if this is a long type (signed or unsigned) */
554 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
559 int IsUnsigned (const type* T)
560 /* Return true if this is an unsigned type */
562 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
567 int IsClassStruct (const type* T)
568 /* Return true if this is a struct type */
570 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
575 int IsTypeFunc (const type* T)
576 /* Return true if this is a function class */
578 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
583 int IsFastCallFunc (const type* T)
584 /* Return true if this is a function type with __fastcall__ calling conventions */
587 CHECK (IsTypeFunc (T));
589 return (F->Flags & FD_FASTCALL) != 0;
594 int IsTypeFuncPtr (const type* T)
595 /* Return true if this is a function pointer */
597 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
602 int IsTypeArray (const type* T)
603 /* Return true if this is an array type */
605 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
610 struct FuncDesc* GetFuncDesc (const type* T)
611 /* Get the FuncDesc pointer from a function or pointer-to-function type */
614 /* Pointer to function */
618 /* Be sure it's a function type */
619 CHECK (T[0] == T_FUNC);
621 /* Decode the function descriptor and return it */
622 return DecodePtr (T+1);