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. */
225 /* If the first field has const and/or volatile qualifiers, print and
229 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
230 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
232 /* Walk over the complete string */
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");
241 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
242 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
244 /* Now check the real type */
245 switch (T & T_MASK_TYPE) {
247 fprintf (F, "char\n");
250 fprintf (F, "int\n");
253 fprintf (F, "float\n");
256 fprintf (F, "double\n");
259 fprintf (F, "void\n");
262 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
266 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
270 fprintf (F, "array[%lu] of ", Decode (Type));
274 fprintf (F, "pointer to ");
277 fprintf (F, "function returning ");
281 fprintf (F, "unknown type: %04X\n", T);
284 /* Get the next type element */
287 } while (T != T_END);
292 void PrintRawType (FILE* F, const type* Type)
293 /* Print a type string in raw format (for debugging) */
295 while (*Type != T_END) {
296 fprintf (F, "%04X ", *Type++);
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 */
307 for (I = 0; I < DECODE_SIZE; ++I) {
308 *Type++ = ((type) Val) | 0x8000;
315 void EncodePtr (type* Type, void* P)
316 /* Encode a pointer into a type array */
318 Encode (Type, (unsigned long) P);
323 unsigned long Decode (const type* Type)
327 unsigned long Val = 0;
328 for (I = DECODE_SIZE-1; I >= 0; I--) {
330 Val |= (Type[I] & 0x7FFF);
337 void* DecodePtr (const type* Type)
338 /* Decode a pointer from a type array */
340 return (void*) Decode (Type);
345 int HasEncode (const type* Type)
346 /* Return true if the given type has encoded data */
348 return IsStruct (Type) || IsArray (Type) || IsFunc (Type);
353 void CopyEncode (const type* Source, type* Target)
354 /* Copy encoded data from Source to Target */
356 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
361 unsigned SizeOf (const type* T)
362 /* Compute size of object represented by type array. */
369 Error (ERR_ILLEGAL_SIZE);
400 Entry = DecodePtr (T+1);
401 return Entry->V.S.Size;
404 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
407 Internal ("Unknown type in SizeOf: %04X", *T);
415 unsigned PSizeOf (const type* T)
416 /* Compute size of pointer object. */
418 /* We are expecting a pointer expression */
419 CHECK ((*T & T_CLASS_PTR) != 0);
421 /* Skip the pointer or array token itself */
423 return SizeOf (T + DECODE_SIZE + 1);
425 return SizeOf (T + 1);
431 unsigned TypeOf (const type* Type)
432 /* Get the code generator base type of the object */
442 return CF_CHAR | CF_UNSIGNED;
453 return CF_INT | CF_UNSIGNED;
459 return CF_LONG | CF_UNSIGNED;
462 F = DecodePtr (Type+1);
463 return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
468 return CF_INT | CF_UNSIGNED;
471 Error (ERR_ILLEGAL_TYPE);
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.
483 /* We are expecting a pointer expression */
484 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
486 /* Skip the pointer or array token itself */
488 return T + DECODE_SIZE + 1;
496 int IsTypeVoid (const type* T)
497 /* Return true if this is a void type */
499 return (T[0] == T_VOID && T[1] == T_END);
504 int IsPtr (const type* T)
505 /* Return true if this is a pointer type */
507 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
512 int IsChar (const type* T)
513 /* Return true if this is a character type */
515 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR && T[1] == T_END;
520 int IsInt (const type* T)
521 /* Return true if this is an integer type */
523 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
528 int IsLong (const type* T)
529 /* Return true if this is a long type (signed or unsigned) */
531 return (T[0] & T_MASK_SIZE) == T_SIZE_LONG;
536 int IsUnsigned (const type* T)
537 /* Return true if this is an unsigned type */
539 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
544 int IsStruct (const type* T)
545 /* Return true if this is a struct type */
547 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
552 int IsFunc (const type* T)
553 /* Return true if this is a function type */
555 return (T[0] == T_FUNC);
560 int IsFastCallFunc (const type* T)
561 /* Return true if this is a function type with __fastcall__ calling conventions */
564 CHECK (T[0] == T_FUNC);
566 return (F->Flags & FD_FASTCALL) != 0;
571 int IsFuncPtr (const type* T)
572 /* Return true if this is a function pointer */
574 return (T[0] == T_PTR && T[1] == T_FUNC);
579 int IsArray (const type* T)
580 /* Return true if this is an array type */
582 return (T[0] == T_ARRAY);
587 struct FuncDesc* GetFuncDesc (const type* T)
588 /* Get the FuncDesc pointer from a function or pointer-to-function type */
591 /* Pointer to function */
595 /* Be sure it's a function type */
596 CHECK (T[0] == T_FUNC);
598 /* Decode the function descriptor and return it */
599 return DecodePtr (T+1);