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 };
68 /*****************************************************************************/
70 /*****************************************************************************/
74 unsigned TypeLen (const type* T)
75 /* Return the length of the type string */
77 const type* Start = T;
86 type* TypeCpy (type* Dest, const type* Src)
87 /* Copy a type string */
100 type* TypeCat (type* Dest, const type* Src)
103 TypeCpy (Dest + TypeLen (Dest), Src);
109 type* TypeDup (const type* T)
110 /* Create a copy of the given type on the heap */
112 unsigned Len = (TypeLen (T) + 1) * sizeof (type);
113 return memcpy (xmalloc (Len), T, Len);
118 type* TypeAlloc (unsigned Len)
119 /* Allocate memory for a type string of length Len. Len *must* include the
123 return xmalloc (Len * sizeof (type));
128 void TypeFree (type* T)
129 /* Free a type string */
136 type GetDefaultChar (void)
137 /* Return the default char type (signed/unsigned) depending on the settings */
139 return SignedChars? T_SCHAR : T_UCHAR;
144 type* GetCharArrayType (unsigned Len)
145 /* Return the type for a char array of the given length */
147 /* Allocate memory for the type string */
148 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
150 /* Fill the type string */
152 T [DECODE_SIZE+1] = GetDefaultChar();
153 T [DECODE_SIZE+2] = T_END;
155 /* Encode the length in the type string */
158 /* Return the new type */
164 type* GetImplicitFuncType (void)
165 /* Return a type string for an inplicitly declared function */
167 /* Get a new function descriptor */
168 FuncDesc* F = NewFuncDesc ();
170 /* Allocate memory for the type string */
171 type* T = TypeAlloc (1 + DECODE_SIZE + 2);
173 /* Prepare the function descriptor */
174 F->Flags = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
175 F->SymTab = &EmptySymTab;
176 F->TagTab = &EmptySymTab;
178 /* Fill the type string */
180 T [DECODE_SIZE+1] = T_INT;
181 T [DECODE_SIZE+2] = T_END;
183 /* Encode the function descriptor into the type string */
186 /* Return the new type */
192 static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
193 /* Check for a specific component of the type. If it is there, print the
194 * name and remove it. Return the type with the component removed.
197 if ((T & Mask) == Mask) {
198 fprintf (F, "%s ", Name);
206 void PrintType (FILE* F, const type* Type)
207 /* Output translation of type array. */
212 /* Walk over the complete string */
213 while ((T = *Type++) != T_END) {
215 /* Print any qualifiers */
216 T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
217 T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
220 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
221 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
223 /* Now check the real type */
224 switch (T & T_MASK_TYPE) {
226 fprintf (F, "char\n");
229 fprintf (F, "short\n");
232 fprintf (F, "int\n");
235 fprintf (F, "long\n");
237 case T_TYPE_LONGLONG:
238 fprintf (F, "long long\n");
241 fprintf (F, "float\n");
244 fprintf (F, "double\n");
247 fprintf (F, "void\n");
250 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
254 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
258 fprintf (F, "array[%lu] of ", Decode (Type));
262 fprintf (F, "pointer to ");
265 fprintf (F, "function returning ");
269 fprintf (F, "unknown type: %04X\n", T);
277 void PrintRawType (FILE* F, const type* Type)
278 /* Print a type string in raw format (for debugging) */
280 while (*Type != T_END) {
281 fprintf (F, "%04X ", *Type++);
288 void Encode (type* Type, unsigned long Val)
289 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
292 for (I = 0; I < DECODE_SIZE; ++I) {
293 *Type++ = ((type) Val) | 0x8000;
300 void EncodePtr (type* Type, void* P)
301 /* Encode a pointer into a type array */
303 Encode (Type, (unsigned long) P);
308 unsigned long Decode (const type* Type)
312 unsigned long Val = 0;
313 for (I = DECODE_SIZE-1; I >= 0; I--) {
315 Val |= (Type[I] & 0x7FFF);
322 void* DecodePtr (const type* Type)
323 /* Decode a pointer from a type array */
325 return (void*) Decode (Type);
330 int HasEncode (const type* Type)
331 /* Return true if the given type has encoded data */
333 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
338 void CopyEncode (const type* Source, type* Target)
339 /* Copy encoded data from Source to Target */
341 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
346 type UnqualifiedType (type T)
347 /* Return the unqalified type */
349 return (T & ~T_MASK_QUAL);
354 unsigned SizeOf (const type* T)
355 /* Compute size of object represented by type array. */
359 switch (UnqualifiedType (T[0])) {
362 Error (ERR_ILLEGAL_SIZE);
393 Entry = DecodePtr (T+1);
394 return Entry->V.S.Size;
397 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
400 Internal ("Unknown type in SizeOf: %04X", *T);
408 unsigned PSizeOf (const type* T)
409 /* Compute size of pointer object. */
411 /* We are expecting a pointer expression */
412 CHECK ((*T & T_CLASS_PTR) != 0);
414 /* Skip the pointer or array token itself */
415 if (IsTypeArray (T)) {
416 return SizeOf (T + DECODE_SIZE + 1);
418 return SizeOf (T + 1);
424 unsigned TypeOf (const type* T)
425 /* Get the code generator base type of the object */
429 switch (UnqualifiedType (T[0])) {
435 return CF_CHAR | CF_UNSIGNED;
446 return CF_INT | CF_UNSIGNED;
452 return CF_LONG | CF_UNSIGNED;
456 return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
461 return CF_INT | CF_UNSIGNED;
464 Error (ERR_ILLEGAL_TYPE);
471 type* Indirect (type* T)
472 /* Do one indirection for the given type, that is, return the type where the
473 * given type points to.
476 /* We are expecting a pointer expression */
477 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
479 /* Skip the pointer or array token itself */
480 if (IsTypeArray (T)) {
481 return T + DECODE_SIZE + 1;
489 int IsTypeChar (const type* T)
490 /* Return true if this is a character type */
492 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
497 int IsTypeInt (const type* T)
498 /* Return true if this is an int type (signed or unsigned) */
500 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
505 int IsTypeLong (const type* T)
506 /* Return true if this is a long type (signed or unsigned) */
508 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
513 int IsTypePtr (const type* T)
514 /* Return true if this is a pointer type */
516 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
521 int IsTypeArray (const type* T)
522 /* Return true if this is an array type */
524 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
529 int IsTypeVoid (const type* T)
530 /* Return true if this is a void type */
532 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
537 int IsTypeFunc (const type* T)
538 /* Return true if this is a function class */
540 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
545 int IsClassInt (const type* T)
546 /* Return true if this is an integer type */
548 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
553 int IsClassPtr (const type* T)
554 /* Return true if this is a pointer type */
556 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
561 int IsClassStruct (const type* T)
562 /* Return true if this is a struct type */
564 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
569 int IsSignUnsigned (const type* T)
570 /* Return true if this is an unsigned type */
572 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
577 int IsQualConst (const type* T)
578 /* Return true if the given type has a const memory image */
580 return (GetQualifier (T) & T_QUAL_CONST) != 0;
585 int IsQualVolatile (const type* T)
586 /* Return true if the given type has a volatile type qualifier */
588 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
593 int IsFastCallFunc (const type* T)
594 /* Return true if this is a function type with __fastcall__ calling conventions */
597 CHECK (IsTypeFunc (T));
599 return (F->Flags & FD_FASTCALL) != 0;
604 int IsTypeFuncPtr (const type* T)
605 /* Return true if this is a function pointer */
607 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
612 type GetType (const type* T)
613 /* Get the raw type */
615 PRECONDITION (T[0] != T_END);
616 return (T[0] & T_MASK_TYPE);
621 type GetClass (const type* T)
622 /* Get the class of a type string */
624 PRECONDITION (T[0] != T_END);
625 return (T[0] & T_MASK_CLASS);
630 type GetSignedness (const type* T)
631 /* Get the sign of a type */
633 PRECONDITION (T[0] != T_END);
634 return (T[0] & T_MASK_SIGN);
639 type GetSizeModifier (const type* T)
640 /* Get the size modifier of a type */
642 PRECONDITION (T[0] != T_END);
643 return (T[0] & T_MASK_SIZE);
648 type GetQualifier (const type* T)
649 /* Get the qualifier from the given type string */
651 /* If this is an array, look at the element type, otherwise look at the
654 if (IsTypeArray (T)) {
655 T += DECODE_SIZE + 1;
657 return (T[0] & T_QUAL_CONST);
662 struct FuncDesc* GetFuncDesc (const type* T)
663 /* Get the FuncDesc pointer from a function or pointer-to-function type */
666 /* Pointer to function */
670 /* Be sure it's a function type */
671 CHECK (T[0] == T_FUNC);
673 /* Decode the function descriptor and return it */
674 return DecodePtr (T+1);