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_uchar [] = { T_UCHAR, T_END };
61 type type_int [] = { T_INT, T_END };
62 type type_uint [] = { T_UINT, T_END };
63 type type_long [] = { T_LONG, T_END };
64 type type_ulong [] = { T_ULONG, T_END };
65 type type_void [] = { T_VOID, T_END };
69 /*****************************************************************************/
71 /*****************************************************************************/
75 unsigned TypeLen (const type* T)
76 /* Return the length of the type string */
78 const type* Start = T;
87 type* TypeCpy (type* Dest, const type* Src)
88 /* Copy a type string */
101 type* TypeCat (type* Dest, const type* Src)
104 TypeCpy (Dest + TypeLen (Dest), Src);
110 type* TypeDup (const type* T)
111 /* Create a copy of the given type on the heap */
113 unsigned Len = (TypeLen (T) + 1) * sizeof (type);
114 return memcpy (xmalloc (Len), T, Len);
119 type* TypeAlloc (unsigned Len)
120 /* Allocate memory for a type string of length Len. Len *must* include the
124 return xmalloc (Len * sizeof (type));
129 void TypeFree (type* T)
130 /* Free a type string */
137 type GetDefaultChar (void)
138 /* Return the default char type (signed/unsigned) depending on the settings */
140 return SignedChars? T_SCHAR : T_UCHAR;
145 type* GetCharArrayType (unsigned Len)
146 /* Return the type for a char array of the given length */
148 /* Allocate memory for the type string */
149 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
151 /* Fill the type string */
153 T [DECODE_SIZE+1] = GetDefaultChar();
154 T [DECODE_SIZE+2] = T_END;
156 /* Encode the length in the type string */
159 /* Return the new type */
165 type* GetImplicitFuncType (void)
166 /* Return a type string for an inplicitly declared function */
168 /* Get a new function descriptor */
169 FuncDesc* F = NewFuncDesc ();
171 /* Allocate memory for the type string */
172 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
174 /* Prepare the function descriptor */
175 F->Flags = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
176 F->SymTab = &EmptySymTab;
177 F->TagTab = &EmptySymTab;
179 /* Fill the type string */
181 T [DECODE_SIZE+1] = T_INT;
182 T [DECODE_SIZE+2] = T_END;
184 /* Encode the function descriptor into the type string */
187 /* Return the new type */
193 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
194 /* Check for a specific component of the type. If it is there, print the
195 * name and remove it. Return the type with the component removed.
198 if ((T & Mask) == Mask) {
199 fprintf (F, "%s ", Name);
207 void PrintType (FILE* F, const type* Type)
208 /* Output translation of type array. */
213 /* Walk over the complete string */
214 while ((T = *Type++) != T_END) {
216 /* Print any qualifiers */
217 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
218 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
221 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
222 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
224 /* Now check the real type */
225 switch (T & T_MASK_TYPE) {
227 fprintf (F, "char\n");
230 fprintf (F, "short\n");
233 fprintf (F, "int\n");
236 fprintf (F, "long\n");
238 case T_TYPE_LONGLONG:
239 fprintf (F, "long long\n");
242 fprintf (F, "float\n");
245 fprintf (F, "double\n");
248 fprintf (F, "void\n");
251 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
255 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
259 fprintf (F, "array[%lu] of ", Decode (Type));
263 fprintf (F, "pointer to ");
266 fprintf (F, "function returning ");
270 fprintf (F, "unknown type: %04X\n", T);
278 void PrintRawType (FILE* F, const type* Type)
279 /* Print a type string in raw format (for debugging) */
281 while (*Type != T_END) {
282 fprintf (F, "%04X ", *Type++);
289 void Encode (type* Type, unsigned long Val)
290 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
293 for (I = 0; I < DECODE_SIZE; ++I) {
294 *Type++ = ((type) Val) | 0x8000;
301 void EncodePtr (type* Type, void* P)
302 /* Encode a pointer into a type array */
304 Encode (Type, (unsigned long) P);
309 unsigned long Decode (const type* Type)
313 unsigned long Val = 0;
314 for (I = DECODE_SIZE-1; I >= 0; I--) {
316 Val |= (Type[I] & 0x7FFF);
323 void* DecodePtr (const type* Type)
324 /* Decode a pointer from a type array */
326 return (void*) Decode (Type);
331 int HasEncode (const type* Type)
332 /* Return true if the given type has encoded data */
334 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
339 void CopyEncode (const type* Source, type* Target)
340 /* Copy encoded data from Source to Target */
342 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
347 type UnqualifiedType (type T)
348 /* Return the unqalified type */
350 return (T & ~T_MASK_QUAL);
355 unsigned SizeOf (const type* T)
356 /* Compute size of object represented by type array. */
360 switch (UnqualifiedType (T[0])) {
363 Error (ERR_ILLEGAL_SIZE);
394 Entry = DecodePtr (T+1);
395 return Entry->V.S.Size;
398 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
401 Internal ("Unknown type in SizeOf: %04X", *T);
409 unsigned PSizeOf (const type* T)
410 /* Compute size of pointer object. */
412 /* We are expecting a pointer expression */
413 CHECK ((*T & T_CLASS_PTR) != 0);
415 /* Skip the pointer or array token itself */
416 if (IsTypeArray (T)) {
417 return SizeOf (T + DECODE_SIZE + 1);
419 return SizeOf (T + 1);
425 unsigned TypeOf (const type* T)
426 /* Get the code generator base type of the object */
430 switch (UnqualifiedType (T[0])) {
436 return CF_CHAR | CF_UNSIGNED;
447 return CF_INT | CF_UNSIGNED;
453 return CF_LONG | CF_UNSIGNED;
457 return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
462 return CF_INT | CF_UNSIGNED;
465 Error (ERR_ILLEGAL_TYPE);
472 type* Indirect (type* T)
473 /* Do one indirection for the given type, that is, return the type where the
474 * given type points to.
477 /* We are expecting a pointer expression */
478 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
480 /* Skip the pointer or array token itself */
481 if (IsTypeArray (T)) {
482 return T + DECODE_SIZE + 1;
490 int IsTypeChar (const type* T)
491 /* Return true if this is a character type */
493 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
498 int IsTypeInt (const type* T)
499 /* Return true if this is an int type (signed or unsigned) */
501 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
506 int IsTypeLong (const type* T)
507 /* Return true if this is a long type (signed or unsigned) */
509 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
514 int IsTypePtr (const type* T)
515 /* Return true if this is a pointer type */
517 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
522 int IsTypeArray (const type* T)
523 /* Return true if this is an array type */
525 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
530 int IsTypeVoid (const type* T)
531 /* Return true if this is a void type */
533 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
538 int IsTypeFunc (const type* T)
539 /* Return true if this is a function class */
541 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
546 int IsClassInt (const type* T)
547 /* Return true if this is an integer type */
549 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
554 int IsClassPtr (const type* T)
555 /* Return true if this is a pointer type */
557 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
562 int IsClassStruct (const type* T)
563 /* Return true if this is a struct type */
565 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
570 int IsSignUnsigned (const type* T)
571 /* Return true if this is an unsigned type */
573 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
578 int IsQualConst (const type* T)
579 /* Return true if the given type has a const memory image */
581 return (GetQualifier (T) & T_QUAL_CONST) != 0;
586 int IsQualVolatile (const type* T)
587 /* Return true if the given type has a volatile type qualifier */
589 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
594 int IsFastCallFunc (const type* T)
595 /* Return true if this is a function type with __fastcall__ calling conventions */
598 CHECK (IsTypeFunc (T));
600 return (F->Flags & FD_FASTCALL) != 0;
605 int IsTypeFuncPtr (const type* T)
606 /* Return true if this is a function pointer */
608 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
613 type GetType (const type* T)
614 /* Get the raw type */
616 PRECONDITION (T[0] != T_END);
617 return (T[0] & T_MASK_TYPE);
622 type GetClass (const type* T)
623 /* Get the class of a type string */
625 PRECONDITION (T[0] != T_END);
626 return (T[0] & T_MASK_CLASS);
631 type GetSignedness (const type* T)
632 /* Get the sign of a type */
634 PRECONDITION (T[0] != T_END);
635 return (T[0] & T_MASK_SIGN);
640 type GetSizeModifier (const type* T)
641 /* Get the size modifier of a type */
643 PRECONDITION (T[0] != T_END);
644 return (T[0] & T_MASK_SIZE);
649 type GetQualifier (const type* T)
650 /* Get the qualifier from the given type string */
652 /* If this is an array, look at the element type, otherwise look at the
655 if (IsTypeArray (T)) {
656 T += DECODE_SIZE + 1;
658 return (T[0] & T_QUAL_CONST);
663 struct FuncDesc* GetFuncDesc (const type* T)
664 /* Get the FuncDesc pointer from a function or pointer-to-function type */
667 /* Pointer to function */
671 /* Be sure it's a function type */
672 CHECK (T[0] == T_FUNC);
674 /* Decode the function descriptor and return it */
675 return DecodePtr (T+1);