]> git.sur5r.net Git - cc65/blobdiff - src/cc65/datatype.c
Fixed a bug
[cc65] / src / cc65 / datatype.c
index 21d2320def5dc5225268fc8bd35c7250420deca6..3996e04a52d324d10588ea2306dd14bf90f07c73 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 1998-2002 Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 #include <string.h>
 
+/* common */
 #include "check.h"
+#include "xmalloc.h"
+
+/* cc65 */
 #include "codegen.h"
 #include "datatype.h"
 #include "error.h"
 #include "funcdesc.h"
 #include "global.h"
-#include "mem.h"
 #include "util.h"
 #include "symtab.h"
 
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
 
 /* Predefined type strings */
+type type_uchar []             = { T_UCHAR,    T_END };
 type type_int []       = { T_INT,      T_END };
 type type_uint []      = { T_UINT,     T_END };
 type type_long []      = { T_LONG,     T_END };
 type type_ulong []     = { T_ULONG,    T_END };
 type type_void []      = { T_VOID,     T_END };
-type type_pschar []    = { T_PTR, T_CHAR, T_END };
-type type_puchar []    = { T_PTR, T_UCHAR, T_END };
+type type_size_t []    = { T_UINT,     T_END };
 
 
 
@@ -74,7 +77,7 @@ unsigned TypeLen (const type* T)
 /* Return the length of the type string */
 {
     const type* Start = T;
-    while (*T) {
+    while (*T != T_END) {
        ++T;
     }
     return T - Start;
@@ -82,20 +85,6 @@ unsigned TypeLen (const type* T)
 
 
 
-int TypeCmp (const type* T1, const type* T2)
-/* Compare two type strings */
-{
-    int A, B, D;
-    do {
-       A = *T1++;
-       B = *T2++;
-       D = A - B;
-    } while (D == 0 && A != 0);
-    return D;
-}
-
-
-
 type* TypeCpy (type* Dest, const type* Src)
 /* Copy a type string */
 {
@@ -123,7 +112,7 @@ type* TypeDup (const type* T)
 /* Create a copy of the given type on the heap */
 {
     unsigned Len = (TypeLen (T) + 1) * sizeof (type);
-    return memcpy (xmalloc (Len), T, Len);
+    return (type*) memcpy (xmalloc (Len), T, Len);
 }
 
 
@@ -133,7 +122,7 @@ type* TypeAlloc (unsigned Len)
  * trailing T_END.
  */
 {
-    return xmalloc (Len * sizeof (type));
+    return (type*) xmalloc (Len * sizeof (type));
 }
 
 
@@ -146,10 +135,22 @@ void TypeFree (type* T)
 
 
 
+int SignExtendChar (int C)
+/* Do correct sign extension of a character */
+{
+    if (SignedChars && (C & 0x80) != 0) {
+               return C | ~0xFF;
+    } else {
+               return C & 0xFF;
+    }
+}
+
+
+
 type GetDefaultChar (void)
 /* Return the default char type (signed/unsigned) depending on the settings */
 {
-    return SignedChars? T_CHAR : T_UCHAR;
+    return SignedChars? T_SCHAR : T_UCHAR;
 }
 
 
@@ -184,10 +185,9 @@ type* GetImplicitFuncType (void)
     type* T = TypeAlloc (1 + DECODE_SIZE + 2);
 
     /* Prepare the function descriptor */
-    F->Flags    = FD_IMPLICIT | FD_ELLIPSIS;
-    F->SymTab   = &EmptySymTab;
-    F->StructTab = &EmptySymTab;
-    F->EnumTab   = &EmptySymTab;
+    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
+    F->SymTab = &EmptySymTab;
+    F->TagTab = &EmptySymTab;
 
     /* Fill the type string */
     T [0]            = T_FUNC;
@@ -203,68 +203,156 @@ type* GetImplicitFuncType (void)
 
 
 
-void PrintType (FILE* F, const type* tarray)
+type* PointerTo (const type* T)
+/* Return a type string that is "pointer to T". The type string is allocated
+ * on the heap and may be freed after use.
+ */
+{
+    /* Get the size of the type string including the terminator */
+    unsigned Size = TypeLen (T) + 1;
+
+    /* Allocate the new type string */
+    type* P = TypeAlloc        (Size + 1);
+
+    /* Create the return type... */
+    P[0] = T_PTR;
+    memcpy (P+1, T, Size * sizeof (type));
+
+    /* ...and return it */
+    return P;
+}
+
+
+
+static type PrintTypeComp (FILE* F, type T, type Mask, const char* Name)
+/* Check for a specific component of the type. If it is there, print the
+ * name and remove it. Return the type with the component removed.
+ */
+{
+    if ((T & Mask) == Mask) {
+       fprintf (F, "%s ", Name);
+       T &= ~Mask;
+    }
+    return T;
+}
+
+
+
+void PrintType (FILE* F, const type* Type)
 /* Output translation of type array. */
 {
-    const type* p;
+    type T;
+    unsigned long Size;
+
+    /* Walk over the complete string */
+    while ((T = *Type++) != T_END) {
+
+       /* Print any qualifiers */
+       T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
+       T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
 
-    for (p = tarray; *p != T_END; ++p) {
-       if (*p & T_UNSIGNED) {
-           fprintf (F, "unsigned ");
+       /* Signedness. Omit the signedness specifier for long and int */
+       if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
+           T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
        }
-       switch (*p) {
-           case T_VOID:
-               fprintf (F, "void\n");
-               break;
-           case T_CHAR:
-           case T_UCHAR:
-               fprintf (F, "char\n");
-               break;
-           case T_INT:
-           case T_UINT:
-               fprintf (F, "int\n");
-               break;
-           case T_SHORT:
-           case T_USHORT:
-               fprintf (F, "short\n");
-               break;
-           case T_LONG:
-           case T_ULONG:
-               fprintf (F, "long\n");
+       T = PrintTypeComp (F, T, T_SIGN_UNSIGNED, "unsigned");
+
+       /* Now check the real type */
+       switch (T & T_MASK_TYPE) {
+           case T_TYPE_CHAR:
+               fprintf (F, "char");
+               break;
+           case T_TYPE_SHORT:
+               fprintf (F, "short");
+               break;
+           case T_TYPE_INT:
+               fprintf (F, "int");
+               break;
+           case T_TYPE_LONG:
+               fprintf (F, "long");
+               break;
+           case T_TYPE_LONGLONG:
+               fprintf (F, "long long");
+               break;
+           case T_TYPE_FLOAT:
+               fprintf (F, "float");
+               break;
+           case T_TYPE_DOUBLE:
+               fprintf (F, "double");
+               break;
+           case T_TYPE_VOID:
+               fprintf (F, "void");
+               break;
+           case T_TYPE_STRUCT:
+               fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
+                       Type += DECODE_SIZE;
                break;
-           case T_FLOAT:
-               fprintf (F, "float\n");
+           case T_TYPE_UNION:
+               fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
+               Type += DECODE_SIZE;
                break;
-           case T_DOUBLE:
-               fprintf (F, "double\n");
-               break;
-           case T_PTR:
-               fprintf (F, "pointer to ");
-               break;
-           case T_ARRAY:
-                       fprintf (F, "array[%lu] of ", Decode (p + 1));
-               p += DECODE_SIZE;
-               break;
-           case T_STRUCT:
-               fprintf (F, "struct %s\n", ((SymEntry*) Decode (p + 1))->Name);
-               p += DECODE_SIZE;
-               break;
-           case T_UNION:
-               fprintf (F, "union %s\n", ((SymEntry*) Decode (p + 1))->Name);
-               p += DECODE_SIZE;
-               break;
-           case T_FUNC:
+           case T_TYPE_ARRAY:
+               /* Recursive call */
+               PrintType (F, Type + DECODE_SIZE);
+               Size = Decode (Type);
+               if (Size == 0) {
+                   fprintf (F, "[]");
+               } else {
+                   fprintf (F, "[%lu]", Size);
+               }
+               return;
+           case T_TYPE_PTR:
+               /* Recursive call */
+               PrintType (F, Type);
+               fprintf (F, "*");
+               return;
+           case T_TYPE_FUNC:
                fprintf (F, "function returning ");
-               p += DECODE_SIZE;
+               Type += DECODE_SIZE;
                break;
            default:
-               fprintf (F, "unknown type: %04X\n", *p);
+               fprintf (F, "unknown type: %04X", T);
        }
+
     }
 }
 
 
 
+void PrintFuncSig (FILE* F, const char* Name, type* Type)
+/* Print a function signature. */
+{
+    /* Get the function descriptor */
+    const FuncDesc* D = GetFuncDesc (Type);
+
+    /* Print a comment with the function signature */
+    PrintType (F, GetFuncReturn (Type));
+    if (D->Flags & FD_FASTCALL) {
+       fprintf (F, " __fastcall__");
+    }
+    fprintf (F, " %s (", Name);
+
+    /* Parameters */
+    if (D->Flags & FD_VOID_PARAM) {
+       fprintf (F, "void");
+    } else {
+       unsigned I;
+       SymEntry* E = D->SymTab->SymHead;
+       for (I = 0; I < D->ParamCount; ++I) {
+           if (I > 0) {
+               fprintf (F, ", ");
+           }
+           PrintType (F, E->Type);
+           E = E->NextSym;
+       }
+    }
+
+    /* End of parameter list */
+    fprintf (F, ")");
+}
+
+
+
 void PrintRawType (FILE* F, const type* Type)
 /* Print a type string in raw format (for debugging) */
 {
@@ -321,7 +409,7 @@ void* DecodePtr (const type* Type)
 int HasEncode (const type* Type)
 /* Return true if the given type has encoded data */
 {
-    return IsStruct (Type) || IsArray (Type) || IsFunc (Type);
+    return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
 }
 
 
@@ -334,42 +422,59 @@ void CopyEncode (const type* Source, type* Target)
 
 
 
-unsigned SizeOf (const type* tarray)
+unsigned SizeOf (const type* T)
 /* Compute size of object represented by type array. */
 {
     SymEntry* Entry;
 
-    switch (*tarray) {
+    switch (UnqualifiedType (T[0])) {
 
-       case T_VOID:
-           return 0;
+       case T_VOID:
+                   return 0;   /* Assume voids have size zero */
 
-       case T_CHAR:
+       case T_SCHAR:
        case T_UCHAR:
-           return 1;
+           return SIZEOF_CHAR;
 
-       case T_INT:
-       case T_UINT:
                case T_SHORT:
        case T_USHORT:
+            return SIZEOF_SHORT;
+
+       case T_INT:
+       case T_UINT:
+            return SIZEOF_INT;
+
        case T_PTR:
-        case T_ENUM:
-           return 2;
+       case T_FUNC:    /* Maybe pointer to function */
+           return SIZEOF_PTR;
 
         case T_LONG:
        case T_ULONG:
-           return 4;
+           return SIZEOF_LONG;
 
-       case T_ARRAY:
-           return (Decode (tarray + 1) * SizeOf (tarray + DECODE_SIZE + 1));
+       case T_LONGLONG:
+       case T_ULONGLONG:
+           return SIZEOF_LONGLONG;
+
+        case T_ENUM:
+           return SIZEOF_INT;
+
+       case T_FLOAT:
+            return SIZEOF_FLOAT;
+
+       case T_DOUBLE:
+           return SIZEOF_DOUBLE;
 
        case T_STRUCT:
        case T_UNION:
-                   Entry = DecodePtr (tarray+1);
+                   Entry = (SymEntry*) DecodePtr (T+1);
                    return Entry->V.S.Size;
 
+       case T_ARRAY:
+           return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
+
        default:
-           Internal ("Unknown type: %04X", *tarray);
+           Internal ("Unknown type in SizeOf: %04X", *T);
            return 0;
 
     }
@@ -377,30 +482,62 @@ unsigned SizeOf (const type* tarray)
 
 
 
-unsigned PSizeOf (const type* tptr)
+unsigned PSizeOf (const type* T)
 /* Compute size of pointer object. */
 {
     /* We are expecting a pointer expression */
-    CHECK (*tptr & T_POINTER);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
-    if (*tptr == T_ARRAY) {
-               return SizeOf (tptr + DECODE_SIZE + 1);
+    if (IsTypeArray (T)) {
+               return SizeOf (T + DECODE_SIZE + 1);
     } else {
-       return SizeOf (tptr + 1);
+       return SizeOf (T + 1);
+    }
+}
+
+
+
+unsigned CheckedSizeOf (const type* T)
+/* Return the size of a data type. If the size is zero, emit an error and
+ * return some valid size instead (so the rest of the compiler doesn't have
+ * to work with invalid sizes).
+ */
+{
+    unsigned Size = SizeOf (T);
+    if (Size == 0) {
+        Error ("Size of data type is unknown");
+        Size = SIZEOF_CHAR;     /* Don't return zero */
+    }
+    return Size;
+}
+
+
+
+unsigned CheckedPSizeOf (const type* T)
+/* Return the size of a data type that is pointed to by a pointer. If the
+ * size is zero, emit an error and return some valid size instead (so the
+ * rest of the compiler doesn't have to work with invalid sizes).
+ */
+{
+    unsigned Size = PSizeOf (T);
+    if (Size == 0) {
+        Error ("Size of data type is unknown");
+        Size = SIZEOF_CHAR;     /* Don't return zero */
     }
+    return Size;
 }
 
 
 
-unsigned TypeOf (const type* Type)
+unsigned TypeOf (const type* T)
 /* Get the code generator base type of the object */
 {
     FuncDesc* F;
 
-    switch (*Type) {
+    switch (UnqualifiedType (T[0])) {
 
-       case T_CHAR:
+       case T_SCHAR:
            return CF_CHAR;
 
        case T_UCHAR:
@@ -424,8 +561,8 @@ unsigned TypeOf (const type* Type)
                    return CF_LONG | CF_UNSIGNED;
 
         case T_FUNC:
-           F = DecodePtr (Type+1);
-           return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
+           F = (FuncDesc*) DecodePtr (T+1);
+           return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
 
         case T_STRUCT:
         case T_UNION:
@@ -433,135 +570,154 @@ unsigned TypeOf (const type* Type)
                    return CF_INT | CF_UNSIGNED;
 
                default:
-                   Error (ERR_ILLEGAL_TYPE);
+                   Error ("Illegal type");
                    return CF_INT;
     }
 }
 
 
 
-type* Indirect (type* Type)
+type* Indirect (type* T)
 /* Do one indirection for the given type, that is, return the type where the
  * given type points to.
  */
 {
     /* We are expecting a pointer expression */
-    CHECK (Type[0] & T_POINTER);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
-    if (Type[0] == T_ARRAY) {
-               return Type + DECODE_SIZE + 1;
+    if (IsTypeArray (T)) {
+               return T + DECODE_SIZE + 1;
     } else {
-       return Type + 1;
+       return T + 1;
     }
 }
 
 
 
-int IsVoid (const type* Type)
-/* Return true if this is a void type */
+int IsClassInt (const type* T)
+/* Return true if this is an integer type */
 {
-    return (Type[0] == T_VOID && Type[1] == T_END);
+    return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
 }
 
 
 
-int IsPtr (const type* Type)
-/* Return true if this is a pointer type */
+int IsClassFloat (const type* T)
+/* Return true if this is a float type */
 {
-    return (Type[0] & T_POINTER) != 0;
+    return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
 }
 
 
 
-int IsChar (const type* Type)
-/* Return true if this is a character type */
+int IsClassPtr (const type* T)
+/* Return true if this is a pointer type */
 {
-    return (Type[0] == T_CHAR || Type[0] == T_UCHAR) && Type[1] == T_END;
+    return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
 }
 
 
 
-int IsInt (const type* Type)
-/* Return true if this is an integer type */
+int IsClassStruct (const type* T)
+/* Return true if this is a struct type */
 {
-    return (Type[0] & T_INTEGER) != 0;
+    return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
 }
 
 
 
-int IsLong (const type* Type)
-/* Return true if this is a long type (signed or unsigned) */
+int IsSignUnsigned (const type* T)
+/* Return true if this is an unsigned type */
 {
-    return (Type[0] & T_LONG) == T_LONG;
+    return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
 }
 
 
 
-int IsUnsigned (const type* Type)
-/* Return true if this is an unsigned type */
+int IsQualConst (const type* T)
+/* Return true if the given type has a const memory image */
 {
-    return (Type[0] & T_UNSIGNED) != 0;
+    return (GetQualifier (T) & T_QUAL_CONST) != 0;
 }
 
 
 
-int IsStruct (const type* Type)
-/* Return true if this is a struct type */
+int IsQualVolatile (const type* T)
+/* Return true if the given type has a volatile type qualifier */
 {
-    return (Type[0] == T_STRUCT || Type[0] == T_UNION);
+    return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
 }
 
 
 
-int IsFunc (const type* Type)
-/* Return true if this is a function type */
+int IsFastCallFunc (const type* T)
+/* Return true if this is a function type or pointer to function with
+ * __fastcall__ calling conventions
+ */
 {
-    return (Type[0] == T_FUNC);
+    FuncDesc* F        = GetFuncDesc (T);
+    return (F->Flags & FD_FASTCALL) != 0;
 }
 
 
 
-int IsFastCallFunc (const type* Type)
-/* Return true if this is a function type with __fastcall__ calling conventions */
+int IsVariadicFunc (const type* T)
+/* Return true if this is a function type or pointer to function type with
+ * variable parameter list
+ */
 {
-    FuncDesc* F;
-    CHECK (*Type == T_FUNC);
-    F = DecodePtr (Type+1);
-    return (F->Flags & FD_FASTCALL) != 0;
+    FuncDesc* F = GetFuncDesc (T);
+    return (F->Flags & FD_VARIADIC) != 0;
 }
 
 
 
-int IsFuncPtr (const type* Type)
-/* Return true if this is a function pointer */
+type GetQualifier (const type* T)
+/* Get the qualifier from the given type string */
 {
-    return (Type[0] == T_PTR && Type[1] == T_FUNC);
+    /* If this is an array, look at the element type, otherwise look at the
+     * type itself.
+     */
+    if (IsTypeArray (T)) {
+       T += DECODE_SIZE + 1;
+    }
+    return (T[0] & T_QUAL_CONST);
 }
 
 
 
-int IsArray (const type* Type)
-/* Return true if this is an array type */
+FuncDesc* GetFuncDesc (const type* T)
+/* Get the FuncDesc pointer from a function or pointer-to-function type */
 {
-    return (Type[0] == T_ARRAY);
+    if (UnqualifiedType (T[0]) == T_PTR) {
+       /* Pointer to function */
+       ++T;
+    }
+
+    /* Be sure it's a function type */
+    CHECK (T[0] == T_FUNC);
+
+    /* Decode the function descriptor and return it */
+    return (FuncDesc*) DecodePtr (T+1);
 }
 
 
 
-struct FuncDesc* GetFuncDesc (const type* Type)
-/* Get the FuncDesc pointer from a function or pointer-to-function type */
+type* GetFuncReturn (type* T)
+/* Return a pointer to the return type of a function or pointer-to-function type */
 {
-    if (Type[0] == T_PTR) {
+    if (UnqualifiedType (T[0]) == T_PTR) {
        /* Pointer to function */
-       ++Type;
+       ++T;
     }
 
     /* Be sure it's a function type */
-    CHECK (Type[0] == T_FUNC);
+    CHECK (T[0] == T_FUNC);
+
+    /* Return a pointer to the return type */
+    return T + 1 + DECODE_SIZE;
 
-    /* Decode the function descriptor and return it */
-    return DecodePtr (Type+1);
 }