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);
283 fprintf (F, "array[%lu] of ", Decode (Type));
287 fprintf (F, "pointer to ");
290 fprintf (F, "function returning ");
294 fprintf (F, "unknown type: %04X", T);
302 void PrintFuncSig (FILE* F, const char* Name, type* Type)
303 /* Print a function signature. */
305 /* Get the function descriptor */
306 const FuncDesc* D = GetFuncDesc (Type);
308 /* Print a comment with the function signature */
309 PrintType (F, GetFuncReturn (Type));
310 if (D->Flags & FD_FASTCALL) {
311 fprintf (F, " __fastcall__");
313 fprintf (F, " %s (", Name);
316 if (D->Flags & FD_VOID_PARAM) {
320 SymEntry* E = D->SymTab->SymHead;
321 for (I = 0; I < D->ParamCount; ++I) {
325 PrintType (F, E->Type);
330 /* End of parameter list */
336 void PrintRawType (FILE* F, const type* Type)
337 /* Print a type string in raw format (for debugging) */
339 while (*Type != T_END) {
340 fprintf (F, "%04X ", *Type++);
347 void Encode (type* Type, unsigned long Val)
348 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
351 for (I = 0; I < DECODE_SIZE; ++I) {
352 *Type++ = ((type) Val) | 0x8000;
359 void EncodePtr (type* Type, void* P)
360 /* Encode a pointer into a type array */
362 Encode (Type, (unsigned long) P);
367 unsigned long Decode (const type* Type)
371 unsigned long Val = 0;
372 for (I = DECODE_SIZE-1; I >= 0; I--) {
374 Val |= (Type[I] & 0x7FFF);
381 void* DecodePtr (const type* Type)
382 /* Decode a pointer from a type array */
384 return (void*) Decode (Type);
389 int HasEncode (const type* Type)
390 /* Return true if the given type has encoded data */
392 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
397 void CopyEncode (const type* Source, type* Target)
398 /* Copy encoded data from Source to Target */
400 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
405 type UnqualifiedType (type T)
406 /* Return the unqalified type */
408 return (T & ~T_MASK_QUAL);
413 unsigned SizeOf (const type* T)
414 /* Compute size of object represented by type array. */
418 switch (UnqualifiedType (T[0])) {
421 Error ("Variable has unknown size");
422 return 1; /* Return something that makes sense */
452 Entry = (SymEntry*) DecodePtr (T+1);
453 return Entry->V.S.Size;
456 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
459 Internal ("Unknown type in SizeOf: %04X", *T);
467 unsigned PSizeOf (const type* T)
468 /* Compute size of pointer object. */
470 /* We are expecting a pointer expression */
471 CHECK ((*T & T_CLASS_PTR) != 0);
473 /* Skip the pointer or array token itself */
474 if (IsTypeArray (T)) {
475 return SizeOf (T + DECODE_SIZE + 1);
477 return SizeOf (T + 1);
483 unsigned TypeOf (const type* T)
484 /* Get the code generator base type of the object */
488 switch (UnqualifiedType (T[0])) {
494 return CF_CHAR | CF_UNSIGNED;
505 return CF_INT | CF_UNSIGNED;
511 return CF_LONG | CF_UNSIGNED;
514 F = (FuncDesc*) DecodePtr (T+1);
515 return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
520 return CF_INT | CF_UNSIGNED;
523 Error ("Illegal type");
530 type* Indirect (type* T)
531 /* Do one indirection for the given type, that is, return the type where the
532 * given type points to.
535 /* We are expecting a pointer expression */
536 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
538 /* Skip the pointer or array token itself */
539 if (IsTypeArray (T)) {
540 return T + DECODE_SIZE + 1;
548 int IsTypeChar (const type* T)
549 /* Return true if this is a character type */
551 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
556 int IsTypeInt (const type* T)
557 /* Return true if this is an int type (signed or unsigned) */
559 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
564 int IsTypeLong (const type* T)
565 /* Return true if this is a long type (signed or unsigned) */
567 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
572 int IsTypeFloat (const type* T)
573 /* Return true if this is a float type */
575 return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
580 int IsTypeDouble (const type* T)
581 /* Return true if this is a double type */
583 return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
588 int IsTypePtr (const type* T)
589 /* Return true if this is a pointer type */
591 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
596 int IsTypeArray (const type* T)
597 /* Return true if this is an array type */
599 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
604 int IsTypeVoid (const type* T)
605 /* Return true if this is a void type */
607 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
612 int IsTypeFunc (const type* T)
613 /* Return true if this is a function class */
615 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
620 int IsClassInt (const type* T)
621 /* Return true if this is an integer type */
623 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
628 int IsClassFloat (const type* T)
629 /* Return true if this is a float type */
631 return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
636 int IsClassPtr (const type* T)
637 /* Return true if this is a pointer type */
639 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
644 int IsClassStruct (const type* T)
645 /* Return true if this is a struct type */
647 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
652 int IsSignUnsigned (const type* T)
653 /* Return true if this is an unsigned type */
655 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
660 int IsQualConst (const type* T)
661 /* Return true if the given type has a const memory image */
663 return (GetQualifier (T) & T_QUAL_CONST) != 0;
668 int IsQualVolatile (const type* T)
669 /* Return true if the given type has a volatile type qualifier */
671 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
676 int IsFastCallFunc (const type* T)
677 /* Return true if this is a function type with __fastcall__ calling conventions */
680 CHECK (IsTypeFunc (T));
681 F = (FuncDesc*) DecodePtr (T+1);
682 return (F->Flags & FD_FASTCALL) != 0;
687 int IsVariadicFunc (const type* T)
688 /* Return true if this is a function type with variable parameter list */
691 CHECK (IsTypeFunc (T));
692 F = (FuncDesc*) DecodePtr (T+1);
693 return (F->Flags & FD_VARIADIC) != 0;
698 int IsTypeFuncPtr (const type* T)
699 /* Return true if this is a function pointer */
701 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
706 type GetType (const type* T)
707 /* Get the raw type */
709 PRECONDITION (T[0] != T_END);
710 return (T[0] & T_MASK_TYPE);
715 type GetClass (const type* T)
716 /* Get the class of a type string */
718 PRECONDITION (T[0] != T_END);
719 return (T[0] & T_MASK_CLASS);
724 type GetSignedness (const type* T)
725 /* Get the sign of a type */
727 PRECONDITION (T[0] != T_END);
728 return (T[0] & T_MASK_SIGN);
733 type GetSizeModifier (const type* T)
734 /* Get the size modifier of a type */
736 PRECONDITION (T[0] != T_END);
737 return (T[0] & T_MASK_SIZE);
742 type GetQualifier (const type* T)
743 /* Get the qualifier from the given type string */
745 /* If this is an array, look at the element type, otherwise look at the
748 if (IsTypeArray (T)) {
749 T += DECODE_SIZE + 1;
751 return (T[0] & T_QUAL_CONST);
756 FuncDesc* GetFuncDesc (const type* T)
757 /* Get the FuncDesc pointer from a function or pointer-to-function type */
760 /* Pointer to function */
764 /* Be sure it's a function type */
765 CHECK (T[0] == T_FUNC);
767 /* Decode the function descriptor and return it */
768 return (FuncDesc*) DecodePtr (T+1);
773 type* GetFuncReturn (type* T)
774 /* Return a pointer to the return type of a function or pointer-to-function type */
777 /* Pointer to function */
781 /* Be sure it's a function type */
782 CHECK (T[0] == T_FUNC);
784 /* Return a pointer to the return type */
785 return T + 1 + DECODE_SIZE;