]> git.sur5r.net Git - cc65/blobdiff - src/cc65/datatype.c
Fixed a bug
[cc65] / src / cc65 / datatype.c
index ac86cfa4963b564838862a40287c8c611c799705..3996e04a52d324d10588ea2306dd14bf90f07c73 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
+/* (C) 1998-2002 Ullrich von Bassewitz                                       */
 /*               Wacholderweg 14                                             */
 /*               D-70597 Stuttgart                                           */
 /* EMail:        uz@musoftware.de                                            */
@@ -51,7 +51,7 @@
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
@@ -63,6 +63,7 @@ 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_size_t []    = { T_UINT,     T_END };
 
 
 
@@ -111,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);
 }
 
 
@@ -121,7 +122,7 @@ type* TypeAlloc (unsigned Len)
  * trailing T_END.
  */
 {
-    return xmalloc (Len * sizeof (type));
+    return (type*) xmalloc (Len * sizeof (type));
 }
 
 
@@ -134,6 +135,18 @@ 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 */
 {
@@ -172,7 +185,7 @@ type* GetImplicitFuncType (void)
     type* T = TypeAlloc (1 + DECODE_SIZE + 2);
 
     /* Prepare the function descriptor */
-    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_ELLIPSIS;
+    F->Flags  = FD_IMPLICIT | FD_EMPTY | FD_VARIADIC;
     F->SymTab = &EmptySymTab;
     F->TagTab = &EmptySymTab;
 
@@ -190,13 +203,34 @@ type* GetImplicitFuncType (void)
 
 
 
+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);
+       fprintf (F, "%s ", Name);
        T &= ~Mask;
     }
     return T;
@@ -208,7 +242,7 @@ void PrintType (FILE* F, const type* Type)
 /* Output translation of type array. */
 {
     type T;
-
+    unsigned long Size;
 
     /* Walk over the complete string */
     while ((T = *Type++) != T_END) {
@@ -217,57 +251,67 @@ void PrintType (FILE* F, const type* Type)
        T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
        T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
 
-       /* Signedness */
-       T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
+       /* 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");
+       }
        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\n");
+               fprintf (F, "char");
                break;
            case T_TYPE_SHORT:
-               fprintf (F, "short\n");
+               fprintf (F, "short");
                break;
            case T_TYPE_INT:
-               fprintf (F, "int\n");
+               fprintf (F, "int");
                break;
            case T_TYPE_LONG:
-               fprintf (F, "long\n");
+               fprintf (F, "long");
                break;
            case T_TYPE_LONGLONG:
-               fprintf (F, "long long\n");
+               fprintf (F, "long long");
                break;
            case T_TYPE_FLOAT:
-               fprintf (F, "float\n");
+               fprintf (F, "float");
                break;
            case T_TYPE_DOUBLE:
-               fprintf (F, "double\n");
+               fprintf (F, "double");
                break;
            case T_TYPE_VOID:
-               fprintf (F, "void\n");
+               fprintf (F, "void");
                break;
            case T_TYPE_STRUCT:
-               fprintf (F, "struct %s\n", ((SymEntry*) DecodePtr (Type))->Name);
+               fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
                        Type += DECODE_SIZE;
                break;
            case T_TYPE_UNION:
-               fprintf (F, "union %s\n", ((SymEntry*) DecodePtr (Type))->Name);
+               fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
                Type += DECODE_SIZE;
                break;
            case T_TYPE_ARRAY:
-                       fprintf (F, "array[%lu] of ", Decode (Type));
-               Type += DECODE_SIZE;
-               break;
+               /* 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:
-               fprintf (F, "pointer to ");
-               break;
+               /* Recursive call */
+               PrintType (F, Type);
+               fprintf (F, "*");
+               return;
            case T_TYPE_FUNC:
                fprintf (F, "function returning ");
                Type += DECODE_SIZE;
                break;
            default:
-               fprintf (F, "unknown type: %04X\n", T);
+               fprintf (F, "unknown type: %04X", T);
        }
 
     }
@@ -275,6 +319,40 @@ void PrintType (FILE* F, const type* Type)
 
 
 
+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) */
 {
@@ -344,14 +422,6 @@ void CopyEncode (const type* Source, type* Target)
 
 
 
-type UnqualifiedType (type T)
-/* Return the unqalified type */
-{
-    return (T & ~T_MASK_QUAL);
-}
-
-
-
 unsigned SizeOf (const type* T)
 /* Compute size of object represented by type array. */
 {
@@ -360,38 +430,44 @@ unsigned SizeOf (const type* T)
     switch (UnqualifiedType (T[0])) {
 
        case T_VOID:
-           Error (ERR_ILLEGAL_SIZE);
-           return 0;
+                   return 0;   /* Assume voids have size zero */
 
        case T_SCHAR:
        case T_UCHAR:
-           return 1;
+           return SIZEOF_CHAR;
 
                case T_SHORT:
        case T_USHORT:
+            return SIZEOF_SHORT;
+
        case T_INT:
        case T_UINT:
+            return SIZEOF_INT;
+
        case T_PTR:
-           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_LONGLONG:
        case T_ULONGLONG:
-           return 8;
+           return SIZEOF_LONGLONG;
 
         case T_ENUM:
-           return 2;
+           return SIZEOF_INT;
 
        case T_FLOAT:
+            return SIZEOF_FLOAT;
+
        case T_DOUBLE:
-           return 4;
+           return SIZEOF_DOUBLE;
 
        case T_STRUCT:
        case T_UNION:
-                   Entry = DecodePtr (T+1);
+                   Entry = (SymEntry*) DecodePtr (T+1);
                    return Entry->V.S.Size;
 
        case T_ARRAY:
@@ -410,7 +486,7 @@ unsigned PSizeOf (const type* T)
 /* Compute size of pointer object. */
 {
     /* We are expecting a pointer expression */
-    CHECK ((*T & T_CLASS_PTR) != 0);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
     if (IsTypeArray (T)) {
@@ -422,6 +498,38 @@ unsigned PSizeOf (const type* T)
 
 
 
+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* T)
 /* Get the code generator base type of the object */
 {
@@ -453,8 +561,8 @@ unsigned TypeOf (const type* T)
                    return CF_LONG | CF_UNSIGNED;
 
         case T_FUNC:
-           F = DecodePtr (T+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:
@@ -462,7 +570,7 @@ unsigned TypeOf (const type* T)
                    return CF_INT | CF_UNSIGNED;
 
                default:
-                   Error (ERR_ILLEGAL_TYPE);
+                   Error ("Illegal type");
                    return CF_INT;
     }
 }
@@ -475,7 +583,7 @@ type* Indirect (type* T)
  */
 {
     /* We are expecting a pointer expression */
-    CHECK ((*T & T_MASK_CLASS) == T_CLASS_PTR);
+    CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
 
     /* Skip the pointer or array token itself */
     if (IsTypeArray (T)) {
@@ -487,66 +595,18 @@ type* Indirect (type* T)
 
 
 
-int IsTypeChar (const type* T)
-/* Return true if this is a character type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_CHAR;
-}
-
-
-
-int IsTypeInt (const type* T)
-/* Return true if this is an int type (signed or unsigned) */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_INT;
-}
-
-
-
-int IsTypeLong (const type* T)
-/* Return true if this is a long type (signed or unsigned) */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_LONG;
-}
-
-
-
-int IsTypePtr (const type* T)
-/* Return true if this is a pointer type */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR);
-}
-
-
-
-int IsTypeArray (const type* T)
-/* Return true if this is an array type */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
-}
-
-
-
-int IsTypeVoid (const type* T)
-/* Return true if this is a void type */
-{
-    return (T[0] & T_MASK_TYPE) == T_TYPE_VOID;
-}
-
-
-
-int IsTypeFunc (const type* T)
-/* Return true if this is a function class */
+int IsClassInt (const type* T)
+/* Return true if this is an integer type */
 {
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_FUNC);
+    return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
 }
 
 
 
-int IsClassInt (const type* T)
-/* Return true if this is an integer type */
+int IsClassFloat (const type* T)
+/* Return true if this is a float type */
 {
-    return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
+    return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
 }
 
 
@@ -592,56 +652,23 @@ int IsQualVolatile (const type* T)
 
 
 int IsFastCallFunc (const type* T)
-/* Return true if this is a function type with __fastcall__ calling conventions */
+/* Return true if this is a function type or pointer to function with
+ * __fastcall__ calling conventions
+ */
 {
-    FuncDesc* F;
-    CHECK (IsTypeFunc (T));
-    F = DecodePtr (T+1);
+    FuncDesc* F        = GetFuncDesc (T);
     return (F->Flags & FD_FASTCALL) != 0;
 }
 
 
 
-int IsTypeFuncPtr (const type* T)
-/* Return true if this is a function pointer */
-{
-    return ((T[0] & T_MASK_TYPE) == T_TYPE_PTR && (T[1] & T_MASK_TYPE) == T_TYPE_FUNC);
-}
-
-
-
-type GetType (const type* T)
-/* Get the raw type */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_TYPE);
-}
-
-
-
-type GetClass (const type* T)
-/* Get the class of a type string */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_CLASS);
-}
-
-
-
-type GetSignedness (const type* T)
-/* Get the sign of a type */
-{
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIGN);
-}
-
-
-
-type GetSizeModifier (const type* T)
-/* Get the size modifier of a type */
+int IsVariadicFunc (const type* T)
+/* Return true if this is a function type or pointer to function type with
+ * variable parameter list
+ */
 {
-    PRECONDITION (T[0] != T_END);
-    return (T[0] & T_MASK_SIZE);
+    FuncDesc* F = GetFuncDesc (T);
+    return (F->Flags & FD_VARIADIC) != 0;
 }
 
 
@@ -660,10 +687,10 @@ type GetQualifier (const type* T)
 
 
 
-struct FuncDesc* GetFuncDesc (const type* T)
+FuncDesc* GetFuncDesc (const type* T)
 /* Get the FuncDesc pointer from a function or pointer-to-function type */
 {
-    if (T[0] == T_PTR) {
+    if (UnqualifiedType (T[0]) == T_PTR) {
        /* Pointer to function */
        ++T;
     }
@@ -672,9 +699,26 @@ struct FuncDesc* GetFuncDesc (const type* T)
     CHECK (T[0] == T_FUNC);
 
     /* Decode the function descriptor and return it */
-    return DecodePtr (T+1);
+    return (FuncDesc*) DecodePtr (T+1);
 }
 
 
 
+type* GetFuncReturn (type* T)
+/* Return a pointer to the return type of a function or pointer-to-function type */
+{
+    if (UnqualifiedType (T[0]) == T_PTR) {
+       /* Pointer to function */
+       ++T;
+    }
+
+    /* Be sure it's a function type */
+    CHECK (T[0] == T_FUNC);
+
+    /* Return a pointer to the return type */
+    return T + 1 + DECODE_SIZE;
+
+}
+
+