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 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 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_ELLIPSIS;
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");
243 T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
244 T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
246 /* Now check the real type */
247 switch (T & T_MASK_TYPE) {
249 fprintf (F, "char\n");
252 fprintf (F, "short\n");
255 fprintf (F, "int\n");
258 fprintf (F, "long\n");
260 case T_TYPE_LONGLONG:
261 fprintf (F, "long long\n");
264 fprintf (F, "float\n");
267 fprintf (F, "double\n");
270 fprintf (F, "void\n");
273 fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
277 fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
281 fprintf (F, "array[%lu] of ", Decode (Type));
285 fprintf (F, "pointer to ");
288 fprintf (F, "function returning ");
292 fprintf (F, "unknown type: %04X\n", T);
300 void PrintRawType (FILE* F, const type* Type)
301 /* Print a type string in raw format (for debugging) */
303 while (*Type != T_END) {
304 fprintf (F, "%04X ", *Type++);
311 void Encode (type* Type, unsigned long Val)
312 /* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
315 for (I = 0; I < DECODE_SIZE; ++I) {
316 *Type++ = ((type) Val) | 0x8000;
323 void EncodePtr (type* Type, void* P)
324 /* Encode a pointer into a type array */
326 Encode (Type, (unsigned long) P);
331 unsigned long Decode (const type* Type)
335 unsigned long Val = 0;
336 for (I = DECODE_SIZE-1; I >= 0; I--) {
338 Val |= (Type[I] & 0x7FFF);
345 void* DecodePtr (const type* Type)
346 /* Decode a pointer from a type array */
348 return (void*) Decode (Type);
353 int HasEncode (const type* Type)
354 /* Return true if the given type has encoded data */
356 return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
361 void CopyEncode (const type* Source, type* Target)
362 /* Copy encoded data from Source to Target */
364 memcpy (Target, Source, DECODE_SIZE * sizeof (type));
369 type UnqualifiedType (type T)
370 /* Return the unqalified type */
372 return (T & ~T_MASK_QUAL);
377 unsigned SizeOf (const type* T)
378 /* Compute size of object represented by type array. */
382 switch (UnqualifiedType (T[0])) {
385 Error ("Variable has unknown size");
386 return 1; /* Return something that makes sense */
416 Entry = DecodePtr (T+1);
417 return Entry->V.S.Size;
420 return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
423 Internal ("Unknown type in SizeOf: %04X", *T);
431 unsigned PSizeOf (const type* T)
432 /* Compute size of pointer object. */
434 /* We are expecting a pointer expression */
435 CHECK ((*T & T_CLASS_PTR) != 0);
437 /* Skip the pointer or array token itself */
438 if (IsTypeArray (T)) {
439 return SizeOf (T + DECODE_SIZE + 1);
441 return SizeOf (T + 1);
447 unsigned TypeOf (const type* T)
448 /* Get the code generator base type of the object */
452 switch (UnqualifiedType (T[0])) {
458 return CF_CHAR | CF_UNSIGNED;
469 return CF_INT | CF_UNSIGNED;
475 return CF_LONG | CF_UNSIGNED;
479 return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
484 return CF_INT | CF_UNSIGNED;
487 Error ("Illegal type");
494 type* Indirect (type* T)
495 /* Do one indirection for the given type, that is, return the type where the
496 * given type points to.
499 /* We are expecting a pointer expression */
500 CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
502 /* Skip the pointer or array token itself */
503 if (IsTypeArray (T)) {
504 return T + DECODE_SIZE + 1;
512 int IsTypeChar (const type* T)
513 /* Return true if this is a character type */
515 return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
520 int IsTypeInt (const type* T)
521 /* Return true if this is an int type (signed or unsigned) */
523 return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
528 int IsTypeLong (const type* T)
529 /* Return true if this is a long type (signed or unsigned) */
531 return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
536 int IsTypeFloat (const type* T)
537 /* Return true if this is a float type */
539 return (T[0] & T_MASK_TYPE) == T_TYPE_FLOAT;
544 int IsTypeDouble (const type* T)
545 /* Return true if this is a double type */
547 return (T[0] & T_MASK_TYPE) == T_TYPE_DOUBLE;
552 int IsTypePtr (const type* T)
553 /* Return true if this is a pointer type */
555 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
560 int IsTypeArray (const type* T)
561 /* Return true if this is an array type */
563 return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
568 int IsTypeVoid (const type* T)
569 /* Return true if this is a void type */
571 return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
576 int IsTypeFunc (const type* T)
577 /* Return true if this is a function class */
579 return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
584 int IsClassInt (const type* T)
585 /* Return true if this is an integer type */
587 return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
592 int IsClassFloat (const type* T)
593 /* Return true if this is a float type */
595 return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
600 int IsClassPtr (const type* T)
601 /* Return true if this is a pointer type */
603 return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
608 int IsClassStruct (const type* T)
609 /* Return true if this is a struct type */
611 return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
616 int IsSignUnsigned (const type* T)
617 /* Return true if this is an unsigned type */
619 return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
624 int IsQualConst (const type* T)
625 /* Return true if the given type has a const memory image */
627 return (GetQualifier (T) & T_QUAL_CONST) != 0;
632 int IsQualVolatile (const type* T)
633 /* Return true if the given type has a volatile type qualifier */
635 return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
640 int IsFastCallFunc (const type* T)
641 /* Return true if this is a function type with __fastcall__ calling conventions */
644 CHECK (IsTypeFunc (T));
646 return (F->Flags & FD_FASTCALL) != 0;
651 int IsTypeFuncPtr (const type* T)
652 /* Return true if this is a function pointer */
654 return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
659 type GetType (const type* T)
660 /* Get the raw type */
662 PRECONDITION (T[0] != T_END);
663 return (T[0] & T_MASK_TYPE);
668 type GetClass (const type* T)
669 /* Get the class of a type string */
671 PRECONDITION (T[0] != T_END);
672 return (T[0] & T_MASK_CLASS);
677 type GetSignedness (const type* T)
678 /* Get the sign of a type */
680 PRECONDITION (T[0] != T_END);
681 return (T[0] & T_MASK_SIGN);
686 type GetSizeModifier (const type* T)
687 /* Get the size modifier of a type */
689 PRECONDITION (T[0] != T_END);
690 return (T[0] & T_MASK_SIZE);
695 type GetQualifier (const type* T)
696 /* Get the qualifier from the given type string */
698 /* If this is an array, look at the element type, otherwise look at the
701 if (IsTypeArray (T)) {
702 T += DECODE_SIZE + 1;
704 return (T[0] & T_QUAL_CONST);
709 struct FuncDesc* GetFuncDesc (const type* T)
710 /* Get the FuncDesc pointer from a function or pointer-to-function type */
713 /* Pointer to function */
717 /* Be sure it's a function type */
718 CHECK (T[0] == T_FUNC);
720 /* Decode the function descriptor and return it */
721 return DecodePtr (T+1);