/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2004 Ullrich von Bassewitz */
+/* Römerstraße 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <string.h>
-#include "../common/xmalloc.h"
-
+/* common */
#include "check.h"
+#include "xmalloc.h"
+
+/* cc65 */
#include "codegen.h"
#include "datatype.h"
#include "error.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 };
/* Return the length of the type string */
{
const type* Start = T;
- while (*T) {
+ while (*T != T_END) {
++T;
}
return T - Start;
-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 */
{
/* 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);
}
* trailing T_END.
*/
{
- return xmalloc (Len * sizeof (type));
+ return (type*) xmalloc (Len * sizeof (type));
}
+int SignExtendChar (int C)
+/* Do correct sign extension of a character */
+{
+ if (IS_Get (&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 IS_Get (&SignedChars)? T_SCHAR : T_UCHAR;
}
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;
-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) {
- for (p = tarray; *p != T_END; ++p) {
- if (*p & T_UNSIGNED) {
- fprintf (F, "unsigned ");
+ /* Print any qualifiers */
+ T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
+ T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
+
+ /* 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");
- break;
- case T_FLOAT:
- fprintf (F, "float\n");
- break;
- case T_DOUBLE:
- fprintf (F, "double\n");
- break;
- case T_PTR:
- fprintf (F, "pointer to ");
+ 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_ARRAY:
- fprintf (F, "array[%lu] of ", Decode (p + 1));
- p += DECODE_SIZE;
+ case T_TYPE_UNION:
+ fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
+ Type += 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_NEAR) {
+ fprintf (F, " __near__");
+ }
+ if (D->Flags & FD_FAR) {
+ fprintf (F, " __far__");
+ }
+ 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, ", ");
+ }
+ if (SymIsRegVar (E)) {
+ fprintf (F, "register ");
+ }
+ PrintType (F, E->Type);
+ E = E->NextSym;
+ }
}
+
+ /* End of parameter list */
+ fprintf (F, ")");
}
void Encode (type* Type, unsigned long Val)
-/* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
+/* Encode Val into the given type string */
{
int I;
for (I = 0; I < DECODE_SIZE; ++I) {
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);
}
-unsigned SizeOf (const type* tarray)
+unsigned SizeOf (const type* T)
/* Compute size of object represented by type array. */
{
SymEntry* Entry;
+ long ElementCount;
- 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 = DecodePtr (T+1);
return Entry->V.S.Size;
+ case T_ARRAY:
+ ElementCount = GetElementCount (T);
+ if (ElementCount < 0) {
+ /* Array with unspecified size */
+ return 0;
+ } else {
+ return ElementCount * SizeOf (T + DECODE_SIZE + 1);
+ }
+
default:
- Internal ("Unknown type: %04X", *tarray);
+ Internal ("Unknown type in SizeOf: %04X", *T);
return 0;
}
-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:
case T_ULONG:
return CF_LONG | CF_UNSIGNED;
+ case T_FLOAT:
+ case T_DOUBLE:
+ /* These two are identical in the backend */
+ return CF_FLOAT;
+
case T_FUNC:
- F = DecodePtr (Type+1);
- return (F->Flags & FD_ELLIPSIS)? 0 : CF_FIXARGC;
+ F = DecodePtr (T+1);
+ return (F->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
case T_STRUCT:
case T_UNION:
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 */
+type* ArrayToPtr (const type* T)
+/* Convert an array to a pointer to it's first element */
{
- return (Type[0] == T_VOID && Type[1] == T_END);
+ /* Function must only be called for an array */
+ CHECK ((T[0] & T_MASK_TYPE) == T_TYPE_ARRAY);
+
+ /* Return pointer to first element */
+ return PointerTo (T + DECODE_SIZE + 1);
}
-int IsPtr (const type* Type)
-/* Return true if this is a pointer type */
+int IsClassInt (const type* T)
+/* Return true if this is an integer type */
{
- return (Type[0] & T_POINTER) != 0;
+ return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
}
-int IsChar (const type* Type)
-/* Return true if this is a character type */
+int IsClassFloat (const type* T)
+/* Return true if this is a float type */
{
- return (Type[0] == T_CHAR || Type[0] == T_UCHAR) && Type[1] == T_END;
+ return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
}
-int IsInt (const type* Type)
-/* Return true if this is an integer type */
+int IsClassPtr (const type* T)
+/* Return true if this is a pointer type */
{
- return (Type[0] & T_INTEGER) != 0;
+ return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
}
-int IsLong (const type* Type)
-/* Return true if this is a long type (signed or unsigned) */
+int IsClassStruct (const type* T)
+/* Return true if this is a struct type */
{
- return (Type[0] & T_LONG) == T_LONG;
+ return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
}
-int IsUnsigned (const type* Type)
+int IsSignUnsigned (const type* T)
/* Return true if this is an unsigned type */
{
- return (Type[0] & T_UNSIGNED) != 0;
+ return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
}
-int IsStruct (const type* Type)
-/* Return true if this is a struct type */
+int IsQualConst (const type* T)
+/* Return true if the given type has a const memory image */
{
- return (Type[0] == T_STRUCT || Type[0] == T_UNION);
+ return (GetQualifier (T) & T_QUAL_CONST) != 0;
}
-int IsFunc (const type* Type)
-/* Return true if this is a function type */
+int IsQualVolatile (const type* T)
+/* Return true if the given type has a volatile type qualifier */
{
- return (Type[0] == T_FUNC);
+ return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
}
-int IsFastCallFunc (const type* Type)
-/* Return true if this is a function type with __fastcall__ calling conventions */
+int IsFastCallFunc (const type* T)
+/* Return true if this is a function type or pointer to function with
+ * __fastcall__ calling conventions
+ */
{
- FuncDesc* F;
- CHECK (*Type == T_FUNC);
- F = DecodePtr (Type+1);
+ FuncDesc* F = GetFuncDesc (T);
return (F->Flags & FD_FASTCALL) != 0;
}
-int IsFuncPtr (const type* Type)
-/* Return true if this is a function pointer */
+int IsVariadicFunc (const type* T)
+/* Return true if this is a function type or pointer to function type with
+ * variable parameter list
+ */
{
- return (Type[0] == T_PTR && Type[1] == T_FUNC);
+ FuncDesc* F = GetFuncDesc (T);
+ return (F->Flags & FD_VARIADIC) != 0;
}
-int IsArray (const type* Type)
-/* Return true if this is an array type */
+type GetQualifier (const type* T)
+/* Get the qualifier from the given type string */
{
- return (Type[0] == T_ARRAY);
+ /* 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);
}
-struct FuncDesc* GetFuncDesc (const type* Type)
+FuncDesc* GetFuncDesc (const type* T)
/* Get the FuncDesc pointer from 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);
/* Decode the function descriptor and return it */
- return DecodePtr (Type+1);
+ return 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;
+
+}
+
+
+
+long GetElementCount (const type* T)
+/* Get the element count of the array specified in T (which must be of
+ * array type).
+ */
+{
+ CHECK (IsTypeArray (T));
+ return (unsigned) Decode (T+1);
+}
+
+
+
+type* GetElementType (type* T)
+/* Return the element type of the given array type. */
+{
+ CHECK (IsTypeArray (T));
+ return T + DECODE_SIZE + 1;
}