1 /*****************************************************************************/
5 /* Type string handling for the cc65 C compiler */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 };
66 type type_size_t [] = { T_UINT, T_END };
70 /*****************************************************************************/
72 /*****************************************************************************/
76 unsigned TypeLen (const type* T)
77 /* Return the length of the type string */
79 const type* Start = T;
88 type* TypeCpy (type* Dest, const type* Src)
89 /* Copy a type string */
102 type* TypeCat (type* Dest, const type* Src)
105 TypeCpy (Dest + TypeLen (Dest), Src);
111 type* TypeDup (const type* T)
112 /* Create a copy of the given type on the heap */
114 unsigned Len = (TypeLen (T) + 1) * sizeof (type);
115 return (type*) memcpy (xmalloc (Len), T, Len);
120 type* TypeAlloc (unsigned Len)
121 /* Allocate memory for a type string of length Len. Len *must* include the
125 return (type*) xmalloc (Len * sizeof (type));
130 void TypeFree (type* T)
131 /* Free a type string */
138 int SignExtendChar (int C)
139 /* Do correct sign extension of a character */
141 if (IS_Get (&SignedChars) && (C & 0x80) != 0) {
150 type GetDefaultChar (void)
151 /* Return the default char type (signed/unsigned) depending on the settings */
153 return IS_Get (&SignedChars)? T_SCHAR : T_UCHAR;
158 type* GetCharArrayType (unsigned Len)
159 /* Return the type for a char array of the given length */
161 /* Allocate memory for the type string */
162 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
164 /* Fill the type string */
166 T [DECODE_SIZE+1] = GetDefaultChar();
167 T [DECODE_SIZE+2] = T_END;
169 /* Encode the length in the type string */
172 /* Return the new type */
178 type* GetImplicitFuncType (void)
179 /* Return a type string for an inplicitly declared function */
181 /* Get a new function descriptor */
182 FuncDesc* F = NewFuncDesc ();
184 /* Allocate memory for the type string */
185 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
187 /* Prepare the function descriptor */
188 F->Flags = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
189 F->SymTab = &EmptySymTab;
190 F->TagTab = &EmptySymTab;
192 /* Fill the type string */
194 T [DECODE_SIZE+1] = T_INT;
195 T [DECODE_SIZE+2] = T_END;
197 /* Encode the function descriptor into the type string */
200 /* Return the new type */
206 type* PointerTo (const type* T)
207 /* Return a type string that is "pointer to T". The type string is allocated
208 * on the heap and may be freed after use.
211 /* Get the size of the type string including the terminator */
212 unsigned Size = TypeLen (T) + 1;
214 /* Allocate the new type string */
215 type* P = TypeAlloc (Size + 1);
217 /* Create the return type... */
219 memcpy (P+1, T, Size * sizeof (type));
221 /* ...and return it */
227 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
228 /* Check for a specific component of the type. If it is there, print the
229 * name and remove it. Return the type with the component removed.
232 if ((T & Mask) == Mask) {
233 fprintf (F, "%s ", Name);
241 void PrintType (FILE* F, const type* Type)
242 /* Output translation of type array. */
247 /* Walk over the complete string */
248 while ((T = *Type++) != T_END) {
250 /* Print any qualifiers */
251 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
252 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
254 /* Signedness. Omit the signedness specifier for long and int */
255 if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
256 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
258 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
260 /* Now check the real type */
261 switch (T & T_MASK_TYPE) {
266 fprintf (F, "short");
274 case T_TYPE_LONGLONG:
275 fprintf (F, "long long");
278 fprintf (F, "float");
281 fprintf (F, "double");
287 fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
291 fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
296 PrintType (F, Type + DECODE_SIZE);
297 Size = Decode (Type);
301 fprintf (F, "[%lu]", Size);
310 fprintf (F, "function returning ");
314 fprintf (F, "unknown type: %04X", T);
322 void PrintFuncSig (FILE* F, const char* Name, type* Type)
323 /* Print a function signature. */
325 /* Get the function descriptor */
326 const FuncDesc* D = GetFuncDesc (Type);
328 /* Print a comment with the function signature */
329 PrintType (F, GetFuncReturn (Type));
330 if (D->Flags & FD_NEAR) {
331 fprintf (F, " __near__");
333 if (D->Flags & FD_FAR) {
334 fprintf (F, " __far__");
336 if (D->Flags & FD_FASTCALL) {
337 fprintf (F, " __fastcall__");
339 fprintf (F, " %s (", Name);
342 if (D->Flags & FD_VOID_PARAM) {
346 SymEntry* E = D->SymTab->SymHead;
347 for (I = 0; I < D->ParamCount; ++I) {
351 if (SymIsRegVar (E)) {
352 fprintf (F, "register ");
354 PrintType (F, E->Type);
359 /* End of parameter list */
365 void PrintRawType (FILE* F, const type* Type)
366 /* Print a type string in raw format (for debugging) */
368 while (*Type != T_END) {
369 fprintf (F, "%04X ", *Type++);
376 void Encode (type* Type, unsigned long Val)
377 /* Encode Val into the given type string */
380 for (I = 0; I < DECODE_SIZE; ++I) {
381 *Type++ = ((type) Val) | 0x8000;
388 void EncodePtr (type* Type, void* P)
389 /* Encode a pointer into a type array */
391 Encode (Type, (unsigned long) P);
396 unsigned long Decode (const type* Type)
400 unsigned long Val = 0;
401 for (I = DECODE_SIZE-1; I >= 0; I--) {
403 Val |= (Type[I] & 0x7FFF);
410 void* DecodePtr (const type* Type)
411 /* Decode a pointer from a type array */
413 return (void*) Decode (Type);
418 int HasEncode (const type* Type)
419 /* Return true if the given type has encoded data */
421 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
426 void CopyEncode (const type* Source, type* Target)
427 /* Copy encoded data from Source to Target */
429 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
434 unsigned SizeOf (const type* T)
435 /* Compute size of object represented by type array. */
440 switch (UnqualifiedType (T[0])) {
443 return 0; /* Assume voids have size zero */
458 case T_FUNC: /* Maybe pointer to function */
467 return SIZEOF_LONGLONG;
476 return SIZEOF_DOUBLE;
480 Entry = DecodePtr (T+1);
481 return Entry->V.S.Size;
484 ElementCount = GetElementCount (T);
485 if (ElementCount < 0) {
486 /* Array with unspecified size */
489 return ElementCount * SizeOf (T + DECODE_SIZE + 1);
493 Internal ("Unknown type in SizeOf: %04X", *T);
501 unsigned PSizeOf (const type* T)
502 /* Compute size of pointer object. */
504 /* We are expecting a pointer expression */
505 CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
507 /* Skip the pointer or array token itself */
508 if (IsTypeArray (T)) {
509 return SizeOf (T + DECODE_SIZE + 1);
511 return SizeOf (T + 1);
517 unsigned CheckedSizeOf (const type* T)
518 /* Return the size of a data type. If the size is zero, emit an error and
519 * return some valid size instead (so the rest of the compiler doesn't have
520 * to work with invalid sizes).
523 unsigned Size = SizeOf (T);
525 Error ("Size of data type is unknown");
526 Size = SIZEOF_CHAR; /* Don't return zero */
533 unsigned CheckedPSizeOf (const type* T)
534 /* Return the size of a data type that is pointed to by a pointer. If the
535 * size is zero, emit an error and return some valid size instead (so the
536 * rest of the compiler doesn't have to work with invalid sizes).
539 unsigned Size = PSizeOf (T);
541 Error ("Size of data type is unknown");
542 Size = SIZEOF_CHAR; /* Don't return zero */
549 unsigned TypeOf (const type* T)
550 /* Get the code generator base type of the object */
554 switch (UnqualifiedType (T[0])) {
560 return CF_CHAR | CF_UNSIGNED;
571 return CF_INT | CF_UNSIGNED;
577 return CF_LONG | CF_UNSIGNED;
581 /* These two are identical in the backend */
586 return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
591 return CF_INT | CF_UNSIGNED;
594 Error ("Illegal type");
601 type* Indirect (type* T)
602 /* Do one indirection for the given type, that is, return the type where the
603 * given type points to.
606 /* We are expecting a pointer expression */
607 CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
609 /* Skip the pointer or array token itself */
610 if (IsTypeArray (T)) {
611 return T + DECODE_SIZE + 1;
619 type* ArrayToPtr (const type* T)
620 /* Convert an array to a pointer to it's first element */
622 /* Function must only be called for an array */
623 CHECK ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
625 /* Return pointer to first element */
626 return PointerTo (T + DECODE_SIZE + 1);
631 int IsClassInt (const type* T)
632 /* Return true if this is an integer type */
634 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
639 int IsClassFloat (const type* T)
640 /* Return true if this is a float type */
642 return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
647 int IsClassPtr (const type* T)
648 /* Return true if this is a pointer type */
650 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
655 int IsClassStruct (const type* T)
656 /* Return true if this is a struct type */
658 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
663 int IsSignUnsigned (const type* T)
664 /* Return true if this is an unsigned type */
666 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
671 int IsQualConst (const type* T)
672 /* Return true if the given type has a const memory image */
674 return (GetQualifier (T) & T_QUAL_CONST) != 0;
679 int IsQualVolatile (const type* T)
680 /* Return true if the given type has a volatile type qualifier */
682 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
687 int IsFastCallFunc (const type* T)
688 /* Return true if this is a function type or pointer to function with
689 * __fastcall__ calling conventions
692 FuncDesc* F = GetFuncDesc (T);
693 return (F->Flags & FD_FASTCALL) != 0;
698 int IsVariadicFunc (const type* T)
699 /* Return true if this is a function type or pointer to function type with
700 * variable parameter list
703 FuncDesc* F = GetFuncDesc (T);
704 return (F->Flags & FD_VARIADIC) != 0;
709 type GetQualifier (const type* T)
710 /* Get the qualifier from the given type string */
712 /* If this is an array, look at the element type, otherwise look at the
715 if (IsTypeArray (T)) {
716 T += DECODE_SIZE + 1;
718 return (T[0] & T_QUAL_CONST);
723 FuncDesc* GetFuncDesc (const type* T)
724 /* Get the FuncDesc pointer from a function or pointer-to-function type */
726 if (UnqualifiedType (T[0]) == T_PTR) {
727 /* Pointer to function */
731 /* Be sure it's a function type */
732 CHECK (T[0] == T_FUNC);
734 /* Decode the function descriptor and return it */
735 return DecodePtr (T+1);
740 type* GetFuncReturn (type* T)
741 /* Return a pointer to the return type of a function or pointer-to-function type */
743 if (UnqualifiedType (T[0]) == T_PTR) {
744 /* Pointer to function */
748 /* Be sure it's a function type */
749 CHECK (T[0] == T_FUNC);
751 /* Return a pointer to the return type */
752 return T + 1 + DECODE_SIZE;
758 long GetElementCount (const type* T)
759 /* Get the element count of the array specified in T (which must be of
763 CHECK (IsTypeArray (T));
764 return (unsigned) Decode (T+1);
769 type* GetElementType (type* T)
770 /* Return the element type of the given array type. */
772 CHECK (IsTypeArray (T));
773 return T + DECODE_SIZE + 1;