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 };
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 type GetDefaultChar (void)
139 /* Return the default char type (signed/unsigned) depending on the settings */
141 return SignedChars? T_SCHAR : T_UCHAR;
146 type* GetCharArrayType (unsigned Len)
147 /* Return the type for a char array of the given length */
149 /* Allocate memory for the type string */
150 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
152 /* Fill the type string */
154 T [DECODE_SIZE+1] = GetDefaultChar();
155 T [DECODE_SIZE+2] = T_END;
157 /* Encode the length in the type string */
160 /* Return the new type */
166 type* GetImplicitFuncType (void)
167 /* Return a type string for an inplicitly declared function */
169 /* Get a new function descriptor */
170 FuncDesc* F = NewFuncDesc ();
172 /* Allocate memory for the type string */
173 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
175 /* Prepare the function descriptor */
176 F->Flags = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
177 F->SymTab = &EmptySymTab;
178 F->TagTab = &EmptySymTab;
180 /* Fill the type string */
182 T [DECODE_SIZE+1] = T_INT;
183 T [DECODE_SIZE+2] = T_END;
185 /* Encode the function descriptor into the type string */
188 /* Return the new type */
194 type* PointerTo (const type* T)
195 /* Return a type string that is "pointer to T". The type string is allocated
196 * on the heap and may be freed after use.
199 /* Get the size of the type string including the terminator */
200 unsigned Size = TypeLen (T) + 1;
202 /* Allocate the new type string */
203 type* P = TypeAlloc (Size + 1);
205 /* Create the return type... */
207 memcpy (P+1, T, Size * sizeof (type));
209 /* ...and return it */
215 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
216 /* Check for a specific component of the type. If it is there, print the
217 * name and remove it. Return the type with the component removed.
220 if ((T & Mask) == Mask) {
221 fprintf (F, "%s ", Name);
229 void PrintType (FILE* F, const type* Type)
230 /* Output translation of type array. */
235 /* Walk over the complete string */
236 while ((T = *Type++) != T_END) {
238 /* Print any qualifiers */
239 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
240 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
242 /* Signedness. Omit the signedness specifier for long and int */
243 if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
244 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
246 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
248 /* Now check the real type */
249 switch (T & T_MASK_TYPE) {
254 fprintf (F, "short");
262 case T_TYPE_LONGLONG:
263 fprintf (F, "long long");
266 fprintf (F, "float");
269 fprintf (F, "double");
275 fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
279 fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
284 PrintType (F, Type + DECODE_SIZE);
285 Size = Decode (Type);
289 fprintf (F, "[%lu]", Size);
298 fprintf (F, "function returning ");
302 fprintf (F, "unknown type: %04X", T);
310 void PrintFuncSig (FILE* F, const char* Name, type* Type)
311 /* Print a function signature. */
313 /* Get the function descriptor */
314 const FuncDesc* D = GetFuncDesc (Type);
316 /* Print a comment with the function signature */
317 PrintType (F, GetFuncReturn (Type));
318 if (D->Flags & FD_FASTCALL) {
319 fprintf (F, " __fastcall__");
321 fprintf (F, " %s (", Name);
324 if (D->Flags & FD_VOID_PARAM) {
328 SymEntry* E = D->SymTab->SymHead;
329 for (I = 0; I < D->ParamCount; ++I) {
333 PrintType (F, E->Type);
338 /* End of parameter list */
344 void PrintRawType (FILE* F, const type* Type)
345 /* Print a type string in raw format (for debugging) */
347 while (*Type != T_END) {
348 fprintf (F, "%04X ", *Type++);
355 void Encode (type* Type, unsigned long Val)
356 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
359 for (I = 0; I < DECODE_SIZE; ++I) {
360 *Type++ = ((type) Val) | 0x8000;
367 void EncodePtr (type* Type, void* P)
368 /* Encode a pointer into a type array */
370 Encode (Type, (unsigned long) P);
375 unsigned long Decode (const type* Type)
379 unsigned long Val = 0;
380 for (I = DECODE_SIZE-1; I >= 0; I--) {
382 Val |= (Type[I] & 0x7FFF);
389 void* DecodePtr (const type* Type)
390 /* Decode a pointer from a type array */
392 return (void*) Decode (Type);
397 int HasEncode (const type* Type)
398 /* Return true if the given type has encoded data */
400 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
405 void CopyEncode (const type* Source, type* Target)
406 /* Copy encoded data from Source to Target */
408 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
413 type UnqualifiedType (type T)
414 /* Return the unqalified type */
416 return (T & ~T_MASK_QUAL);
421 unsigned SizeOf (const type* T)
422 /* Compute size of object represented by type array. */
426 switch (UnqualifiedType (T[0])) {
429 Error ("Variable has unknown size");
430 return 1; /* Return something that makes sense */
460 Entry = (SymEntry*) DecodePtr (T+1);
461 return Entry->V.S.Size;
464 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
467 Internal ("Unknown type in SizeOf: %04X", *T);
475 unsigned PSizeOf (const type* T)
476 /* Compute size of pointer object. */
478 /* We are expecting a pointer expression */
479 CHECK ((*T & T_CLASS_PTR) != 0);
481 /* Skip the pointer or array token itself */
482 if (IsTypeArray (T)) {
483 return SizeOf (T + DECODE_SIZE + 1);
485 return SizeOf (T + 1);
491 unsigned TypeOf (const type* T)
492 /* Get the code generator base type of the object */
496 switch (UnqualifiedType (T[0])) {
502 return CF_CHAR | CF_UNSIGNED;
513 return CF_INT | CF_UNSIGNED;
519 return CF_LONG | CF_UNSIGNED;
522 F = (FuncDesc*) DecodePtr (T+1);
523 return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
528 return CF_INT | CF_UNSIGNED;
531 Error ("Illegal type");
538 type* Indirect (type* T)
539 /* Do one indirection for the given type, that is, return the type where the
540 * given type points to.
543 /* We are expecting a pointer expression */
544 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
546 /* Skip the pointer or array token itself */
547 if (IsTypeArray (T)) {
548 return T + DECODE_SIZE + 1;
556 int IsTypeChar (const type* T)
557 /* Return true if this is a character type */
559 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
564 int IsTypeInt (const type* T)
565 /* Return true if this is an int type (signed or unsigned) */
567 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
572 int IsTypeLong (const type* T)
573 /* Return true if this is a long type (signed or unsigned) */
575 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
580 int IsTypeFloat (const type* T)
581 /* Return true if this is a float type */
583 return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
588 int IsTypeDouble (const type* T)
589 /* Return true if this is a double type */
591 return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
596 int IsTypePtr (const type* T)
597 /* Return true if this is a pointer type */
599 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
604 int IsTypeArray (const type* T)
605 /* Return true if this is an array type */
607 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
612 int IsTypeVoid (const type* T)
613 /* Return true if this is a void type */
615 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
620 int IsTypeFunc (const type* T)
621 /* Return true if this is a function class */
623 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
628 int IsClassInt (const type* T)
629 /* Return true if this is an integer type */
631 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
636 int IsClassFloat (const type* T)
637 /* Return true if this is a float type */
639 return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
644 int IsClassPtr (const type* T)
645 /* Return true if this is a pointer type */
647 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
652 int IsClassStruct (const type* T)
653 /* Return true if this is a struct type */
655 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
660 int IsSignUnsigned (const type* T)
661 /* Return true if this is an unsigned type */
663 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
668 int IsQualConst (const type* T)
669 /* Return true if the given type has a const memory image */
671 return (GetQualifier (T) & T_QUAL_CONST) != 0;
676 int IsQualVolatile (const type* T)
677 /* Return true if the given type has a volatile type qualifier */
679 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
684 int IsFastCallFunc (const type* T)
685 /* Return true if this is a function type with __fastcall__ calling conventions */
688 CHECK (IsTypeFunc (T));
689 F = (FuncDesc*) DecodePtr (T+1);
690 return (F->Flags & FD_FASTCALL) != 0;
695 int IsVariadicFunc (const type* T)
696 /* Return true if this is a function type with variable parameter list */
699 CHECK (IsTypeFunc (T));
700 F = (FuncDesc*) DecodePtr (T+1);
701 return (F->Flags & FD_VARIADIC) != 0;
706 int IsTypeFuncPtr (const type* T)
707 /* Return true if this is a function pointer */
709 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
714 type GetType (const type* T)
715 /* Get the raw type */
717 PRECONDITION (T[0] != T_END);
718 return (T[0] & T_MASK_TYPE);
723 type GetClass (const type* T)
724 /* Get the class of a type string */
726 PRECONDITION (T[0] != T_END);
727 return (T[0] & T_MASK_CLASS);
732 type GetSignedness (const type* T)
733 /* Get the sign of a type */
735 PRECONDITION (T[0] != T_END);
736 return (T[0] & T_MASK_SIGN);
741 type GetSizeModifier (const type* T)
742 /* Get the size modifier of a type */
744 PRECONDITION (T[0] != T_END);
745 return (T[0] & T_MASK_SIZE);
750 type GetQualifier (const type* T)
751 /* Get the qualifier from the given type string */
753 /* If this is an array, look at the element type, otherwise look at the
756 if (IsTypeArray (T)) {
757 T += DECODE_SIZE + 1;
759 return (T[0] & T_QUAL_CONST);
764 FuncDesc* GetFuncDesc (const type* T)
765 /* Get the FuncDesc pointer from a function or pointer-to-function type */
768 /* Pointer to function */
772 /* Be sure it's a function type */
773 CHECK (T[0] == T_FUNC);
775 /* Decode the function descriptor and return it */
776 return (FuncDesc*) DecodePtr (T+1);
781 type* GetFuncReturn (type* T)
782 /* Return a pointer to the return type of a function or pointer-to-function type */
785 /* Pointer to function */
789 /* Be sure it's a function type */
790 CHECK (T[0] == T_FUNC);
792 /* Return a pointer to the return type */
793 return T + 1 + DECODE_SIZE;