2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* datatype.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Type string handling for the cc65 C compiler */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* (C) 1998-2006 Ullrich von Bassewitz */
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* R<>merstra<72>e 52 */
|
2003-02-03 22:14:20 +00:00
|
|
|
|
/* D-70794 Filderstadt */
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
/* common */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "check.h"
|
2000-08-01 15:04:35 +00:00
|
|
|
|
#include "xmalloc.h"
|
|
|
|
|
|
|
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "codegen.h"
|
|
|
|
|
#include "datatype.h"
|
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "funcdesc.h"
|
|
|
|
|
#include "global.h"
|
|
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-08-20 09:25:47 +00:00
|
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Predefined type strings */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type type_schar[] = { TYPE(T_SCHAR), TYPE(T_END) };
|
|
|
|
|
Type type_uchar[] = { TYPE(T_UCHAR), TYPE(T_END) };
|
|
|
|
|
Type type_int[] = { TYPE(T_INT), TYPE(T_END) };
|
|
|
|
|
Type type_uint[] = { TYPE(T_UINT), TYPE(T_END) };
|
|
|
|
|
Type type_long[] = { TYPE(T_LONG), TYPE(T_END) };
|
|
|
|
|
Type type_ulong[] = { TYPE(T_ULONG), TYPE(T_END) };
|
|
|
|
|
Type type_void[] = { TYPE(T_VOID), TYPE(T_END) };
|
|
|
|
|
Type type_size_t[] = { TYPE(T_SIZE_T), TYPE(T_END) };
|
|
|
|
|
Type type_float[] = { TYPE(T_FLOAT), TYPE(T_END) };
|
|
|
|
|
Type type_double[] = { TYPE(T_DOUBLE), TYPE(T_END) };
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned TypeLen (const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return the length of the type string */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
const Type* Start = T;
|
|
|
|
|
while (T->C != T_END) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
++T;
|
|
|
|
|
}
|
|
|
|
|
return T - Start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* TypeCpy (Type* Dest, const Type* Src)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Copy a type string */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* Orig = Dest;
|
|
|
|
|
while (1) {
|
|
|
|
|
*Dest = *Src;
|
|
|
|
|
if (Src->C == T_END) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Src++;
|
|
|
|
|
Dest++;
|
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return Orig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* TypeDup (const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a copy of the given type on the heap */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned Len = (TypeLen (T) + 1) * sizeof (Type);
|
|
|
|
|
return memcpy (xmalloc (Len), T, Len);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* TypeAlloc (unsigned Len)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Allocate memory for a type string of length Len. Len *must* include the
|
|
|
|
|
* trailing T_END.
|
|
|
|
|
*/
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return xmalloc (Len * sizeof (Type));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
void TypeFree (Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Free a type string */
|
|
|
|
|
{
|
|
|
|
|
xfree (T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-30 18:46:08 +00:00
|
|
|
|
int SignExtendChar (int C)
|
|
|
|
|
/* Do correct sign extension of a character */
|
|
|
|
|
{
|
2004-03-07 21:53:35 +00:00
|
|
|
|
if (IS_Get (&SignedChars) && (C & 0x80) != 0) {
|
2002-09-30 18:46:08 +00:00
|
|
|
|
return C | ~0xFF;
|
|
|
|
|
} else {
|
|
|
|
|
return C & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
TypeCode GetDefaultChar (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return the default char type (signed/unsigned) depending on the settings */
|
|
|
|
|
{
|
2004-03-07 21:53:35 +00:00
|
|
|
|
return IS_Get (&SignedChars)? T_SCHAR : T_UCHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* GetCharArrayType (unsigned Len)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return the type for a char array of the given length */
|
|
|
|
|
{
|
|
|
|
|
/* Allocate memory for the type string */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* T = TypeAlloc (3); /* array/char/terminator */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Fill the type string */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
T[0].C = T_ARRAY;
|
|
|
|
|
T[0].A.L = Len; /* Array length is in the L attribute */
|
|
|
|
|
T[1].C = GetDefaultChar ();
|
|
|
|
|
T[2].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Return the new type */
|
|
|
|
|
return T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* GetImplicitFuncType (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Return a type string for an inplicitly declared function */
|
|
|
|
|
{
|
|
|
|
|
/* Get a new function descriptor */
|
|
|
|
|
FuncDesc* F = NewFuncDesc ();
|
|
|
|
|
|
|
|
|
|
/* Allocate memory for the type string */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* T = TypeAlloc (3); /* func/returns int/terminator */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Prepare the function descriptor */
|
2008-08-03 20:55:36 +00:00
|
|
|
|
F->Flags = FD_EMPTY | FD_VARIADIC;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
F->SymTab = &EmptySymTab;
|
|
|
|
|
F->TagTab = &EmptySymTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Fill the type string */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
T[0].C = T_FUNC;
|
|
|
|
|
T[0].A.P = F;
|
|
|
|
|
T[1].C = T_INT;
|
|
|
|
|
T[2].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Return the new type */
|
|
|
|
|
return T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* PointerTo (const Type* T)
|
2000-08-20 09:25:47 +00:00
|
|
|
|
/* Return a type string that is "pointer to T". The type string is allocated
|
|
|
|
|
* on the heap and may be freed after use.
|
2000-11-02 21:22:33 +00:00
|
|
|
|
*/
|
2000-08-20 09:25:47 +00:00
|
|
|
|
{
|
|
|
|
|
/* Get the size of the type string including the terminator */
|
|
|
|
|
unsigned Size = TypeLen (T) + 1;
|
|
|
|
|
|
|
|
|
|
/* Allocate the new type string */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* P = TypeAlloc (Size + 1);
|
2000-08-20 09:25:47 +00:00
|
|
|
|
|
|
|
|
|
/* Create the return type... */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
P[0].C = T_PTR;
|
|
|
|
|
memcpy (P+1, T, Size * sizeof (Type));
|
2000-08-20 09:25:47 +00:00
|
|
|
|
|
|
|
|
|
/* ...and return it */
|
|
|
|
|
return P;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
static TypeCode PrintTypeComp (FILE* F, TypeCode C, TypeCode Mask, const char* Name)
|
2000-08-01 15:04:35 +00:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if ((C & Mask) == Mask) {
|
2000-08-20 09:25:47 +00:00
|
|
|
|
fprintf (F, "%s ", Name);
|
2006-02-19 12:29:37 +00:00
|
|
|
|
C &= ~Mask;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
}
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return C;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
void PrintType (FILE* F, const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Output translation of type array. */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* Walk over the type string */
|
|
|
|
|
while (T->C != T_END) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* Get the type code */
|
|
|
|
|
TypeCode C = T->C;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
|
/* Print any qualifiers */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_CONST, "const");
|
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_VOLATILE, "volatile");
|
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_RESTRICT, "restrict");
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2001-05-03 17:34:54 +00:00
|
|
|
|
/* Signedness. Omit the signedness specifier for long and int */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if ((C & T_MASK_TYPE) != T_TYPE_INT && (C & T_MASK_TYPE) != T_TYPE_LONG) {
|
|
|
|
|
C = PrintTypeComp (F, C, T_SIGN_SIGNED, "signed");
|
2001-05-03 17:34:54 +00:00
|
|
|
|
}
|
2006-02-19 12:29:37 +00:00
|
|
|
|
C = PrintTypeComp (F, C, T_SIGN_UNSIGNED, "unsigned");
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
|
/* Now check the real type */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
switch (C & T_MASK_TYPE) {
|
2000-08-01 18:45:08 +00:00
|
|
|
|
case T_TYPE_CHAR:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "char");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_SHORT:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "short");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_INT:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "int");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_LONG:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "long");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_LONGLONG:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "long long");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_FLOAT:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "float");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_DOUBLE:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "double");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_TYPE_VOID:
|
2001-05-03 17:34:54 +00:00
|
|
|
|
fprintf (F, "void");
|
2000-08-01 18:45:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case T_TYPE_STRUCT:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
fprintf (F, "struct %s", ((SymEntry*) T->A.P)->Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_TYPE_UNION:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
fprintf (F, "union %s", ((SymEntry*) T->A.P)->Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_TYPE_ARRAY:
|
2001-05-05 21:42:58 +00:00
|
|
|
|
/* Recursive call */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
PrintType (F, T + 1);
|
|
|
|
|
if (T->A.L == UNSPECIFIED) {
|
2001-05-05 21:42:58 +00:00
|
|
|
|
fprintf (F, "[]");
|
|
|
|
|
} else {
|
2006-02-19 12:29:37 +00:00
|
|
|
|
fprintf (F, "[%ld]", T->A.L);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_TYPE_PTR:
|
2001-05-05 21:42:58 +00:00
|
|
|
|
/* Recursive call */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
PrintType (F, T + 1);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
fprintf (F, "*");
|
|
|
|
|
return;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_TYPE_FUNC:
|
2000-05-28 13:40:48 +00:00
|
|
|
|
fprintf (F, "function returning ");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
fprintf (F, "unknown type: %04lX", T->C);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* Next element */
|
|
|
|
|
++T;
|
2000-08-01 18:45:08 +00:00
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
void PrintFuncSig (FILE* F, const char* Name, Type* T)
|
2001-05-03 17:34:54 +00:00
|
|
|
|
/* Print a function signature. */
|
|
|
|
|
{
|
|
|
|
|
/* Get the function descriptor */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
const FuncDesc* D = GetFuncDesc (T);
|
2001-05-03 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
/* Print a comment with the function signature */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
PrintType (F, GetFuncReturn (T));
|
2003-11-28 22:12:14 +00:00
|
|
|
|
if (D->Flags & FD_NEAR) {
|
|
|
|
|
fprintf (F, " __near__");
|
|
|
|
|
}
|
|
|
|
|
if (D->Flags & FD_FAR) {
|
|
|
|
|
fprintf (F, " __far__");
|
|
|
|
|
}
|
2001-05-03 17:34:54 +00:00
|
|
|
|
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, ", ");
|
|
|
|
|
}
|
2002-11-25 17:06:19 +00:00
|
|
|
|
if (SymIsRegVar (E)) {
|
|
|
|
|
fprintf (F, "register ");
|
|
|
|
|
}
|
2001-05-03 17:34:54 +00:00
|
|
|
|
PrintType (F, E->Type);
|
|
|
|
|
E = E->NextSym;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* End of parameter list */
|
|
|
|
|
fprintf (F, ")");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
void PrintRawType (FILE* F, const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Print a type string in raw format (for debugging) */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
while (T->C != T_END) {
|
|
|
|
|
fprintf (F, "%04lX ", T->C);
|
|
|
|
|
++T;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
fprintf (F, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
int TypeHasAttr (const Type* T)
|
|
|
|
|
/* Return true if the given type has attribute data */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return IsClassStruct (T) || IsTypeArray (T) || IsClassFunc (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned SizeOf (const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Compute size of object represented by type array. */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
switch (UnqualifiedType (T->C)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
|
case T_VOID:
|
2002-03-10 20:44:58 +00:00
|
|
|
|
return 0; /* Assume voids have size zero */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_SCHAR:
|
2000-05-28 13:40:48 +00:00
|
|
|
|
case T_UCHAR:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_CHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
|
case T_USHORT:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_SHORT;
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_INT:
|
|
|
|
|
case T_UINT:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_INT;
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
case T_PTR:
|
2001-11-12 14:20:56 +00:00
|
|
|
|
case T_FUNC: /* Maybe pointer to function */
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_PTR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
case T_ULONG:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_LONG;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_LONGLONG:
|
|
|
|
|
case T_ULONGLONG:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_LONGLONG;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
|
|
|
|
case T_ENUM:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_INT;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
|
|
|
|
case T_FLOAT:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_FLOAT;
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_DOUBLE:
|
2002-11-02 12:39:10 +00:00
|
|
|
|
return SIZEOF_DOUBLE;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case T_STRUCT:
|
|
|
|
|
case T_UNION:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return ((SymEntry*) T->A.P)->V.S.Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_ARRAY:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if (T->A.L == UNSPECIFIED) {
|
2003-02-03 22:14:20 +00:00
|
|
|
|
/* Array with unspecified size */
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return T->A.L * SizeOf (T + 1);
|
2003-02-03 22:14:20 +00:00
|
|
|
|
}
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Internal ("Unknown type in SizeOf: %04lX", T->C);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned PSizeOf (const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Compute size of pointer object. */
|
|
|
|
|
{
|
|
|
|
|
/* We are expecting a pointer expression */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
CHECK (IsClassPtr (T));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Skip the pointer or array token itself */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return SizeOf (T + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned CheckedSizeOf (const Type* T)
|
2002-03-10 20:44:58 +00:00
|
|
|
|
/* 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");
|
2002-11-02 12:39:10 +00:00
|
|
|
|
Size = SIZEOF_CHAR; /* Don't return zero */
|
2002-03-10 20:44:58 +00:00
|
|
|
|
}
|
|
|
|
|
return Size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned CheckedPSizeOf (const Type* T)
|
2002-03-10 20:44:58 +00:00
|
|
|
|
/* 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");
|
2002-11-02 12:39:10 +00:00
|
|
|
|
Size = SIZEOF_CHAR; /* Don't return zero */
|
2002-03-10 20:44:58 +00:00
|
|
|
|
}
|
|
|
|
|
return Size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
unsigned TypeOf (const Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Get the code generator base type of the object */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
switch (UnqualifiedType (T->C)) {
|
2001-03-22 21:02:01 +00:00
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
case T_SCHAR:
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return CF_CHAR;
|
|
|
|
|
|
|
|
|
|
case T_UCHAR:
|
|
|
|
|
return CF_CHAR | CF_UNSIGNED;
|
|
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
|
case T_INT:
|
|
|
|
|
case T_ENUM:
|
|
|
|
|
return CF_INT;
|
|
|
|
|
|
|
|
|
|
case T_USHORT:
|
|
|
|
|
case T_UINT:
|
|
|
|
|
case T_PTR:
|
|
|
|
|
case T_ARRAY:
|
|
|
|
|
return CF_INT | CF_UNSIGNED;
|
|
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
return CF_LONG;
|
|
|
|
|
|
|
|
|
|
case T_ULONG:
|
|
|
|
|
return CF_LONG | CF_UNSIGNED;
|
|
|
|
|
|
2003-10-10 11:39:30 +00:00
|
|
|
|
case T_FLOAT:
|
2003-11-28 22:12:14 +00:00
|
|
|
|
case T_DOUBLE:
|
2003-10-10 11:39:30 +00:00
|
|
|
|
/* These two are identical in the backend */
|
|
|
|
|
return CF_FLOAT;
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
case T_FUNC:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return (((FuncDesc*) T->A.P)->Flags & FD_VARIADIC)? 0 : CF_FIXARGC;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case T_STRUCT:
|
|
|
|
|
case T_UNION:
|
|
|
|
|
/* Address of ... */
|
|
|
|
|
return CF_INT | CF_UNSIGNED;
|
|
|
|
|
|
|
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Error ("Illegal type %04lX", T->C);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return CF_INT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* Indirect (Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Do one indirection for the given type, that is, return the type where the
|
|
|
|
|
* given type points to.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* We are expecting a pointer expression */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
CHECK (IsClassPtr (T));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Skip the pointer or array token itself */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return T + 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* ArrayToPtr (const Type* T)
|
2002-12-17 12:12:44 +00:00
|
|
|
|
/* Convert an array to a pointer to it's first element */
|
|
|
|
|
{
|
|
|
|
|
/* Function must only be called for an array */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
CHECK (IsTypeArray (T));
|
2002-12-17 12:12:44 +00:00
|
|
|
|
|
|
|
|
|
/* Return pointer to first element */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return PointerTo (T + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
TypeCode GetQualifier (const Type* T)
|
|
|
|
|
/* Get the qualifier from the given type string */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* If this is an array, look at the element type, otherwise look at the
|
|
|
|
|
* type itself.
|
|
|
|
|
*/
|
|
|
|
|
if (IsTypeArray (T)) {
|
|
|
|
|
++T;
|
|
|
|
|
}
|
|
|
|
|
return (T->C & T_MASK_QUAL);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
int IsFastCallFunc (const Type* T)
|
2001-09-24 22:36:35 +00:00
|
|
|
|
/* Return true if this is a function type or pointer to function with
|
|
|
|
|
* __fastcall__ calling conventions
|
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2001-09-24 22:36:35 +00:00
|
|
|
|
FuncDesc* F = GetFuncDesc (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return (F->Flags & FD_FASTCALL) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
int IsVariadicFunc (const Type* T)
|
2001-09-24 22:36:35 +00:00
|
|
|
|
/* Return true if this is a function type or pointer to function type with
|
|
|
|
|
* variable parameter list
|
|
|
|
|
*/
|
2001-03-22 21:02:01 +00:00
|
|
|
|
{
|
2001-09-24 22:36:35 +00:00
|
|
|
|
FuncDesc* F = GetFuncDesc (T);
|
2001-03-24 13:00:16 +00:00
|
|
|
|
return (F->Flags & FD_VARIADIC) != 0;
|
2001-03-22 21:02:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
FuncDesc* GetFuncDesc (const Type* T)
|
|
|
|
|
/* Get the FuncDesc pointer from a function or pointer-to-function type */
|
2000-08-14 22:18:26 +00:00
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if (UnqualifiedType (T->C) == T_PTR) {
|
|
|
|
|
/* Pointer to function */
|
|
|
|
|
++T;
|
2000-08-14 22:18:26 +00:00
|
|
|
|
}
|
2006-02-19 12:29:37 +00:00
|
|
|
|
|
|
|
|
|
/* Be sure it's a function type */
|
|
|
|
|
CHECK (IsClassFunc (T));
|
|
|
|
|
|
|
|
|
|
/* Get the function descriptor from the type attributes */
|
|
|
|
|
return T->A.P;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
void SetFuncDesc (Type* T, FuncDesc* F)
|
|
|
|
|
/* Set the FuncDesc pointer in a function or pointer-to-function type */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if (UnqualifiedType (T->C) == T_PTR) {
|
|
|
|
|
/* Pointer to function */
|
|
|
|
|
++T;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Be sure it's a function type */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
CHECK (IsClassFunc (T));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* Set the function descriptor */
|
|
|
|
|
T->A.P = F;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* GetFuncReturn (Type* T)
|
2001-05-03 17:34:54 +00:00
|
|
|
|
/* Return a pointer to the return type of a function or pointer-to-function type */
|
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
|
if (UnqualifiedType (T->C) == T_PTR) {
|
|
|
|
|
/* Pointer to function */
|
|
|
|
|
++T;
|
2001-05-03 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Be sure it's a function type */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
CHECK (IsClassFunc (T));
|
2001-05-03 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
/* Return a pointer to the return type */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return T + 1;
|
2001-05-03 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
long GetElementCount (const Type* T)
|
2003-02-03 22:14:20 +00:00
|
|
|
|
/* Get the element count of the array specified in T (which must be of
|
|
|
|
|
* array type).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
CHECK (IsTypeArray (T));
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return T->A.L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetElementCount (Type* T, long Count)
|
|
|
|
|
/* Set the element count of the array specified in T (which must be of
|
|
|
|
|
* array type).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
CHECK (IsTypeArray (T));
|
|
|
|
|
T->A.L = Count;
|
2003-02-03 22:14:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* GetElementType (Type* T)
|
2003-02-05 22:02:48 +00:00
|
|
|
|
/* Return the element type of the given array type. */
|
|
|
|
|
{
|
|
|
|
|
CHECK (IsTypeArray (T));
|
2006-02-19 12:29:37 +00:00
|
|
|
|
return T + 1;
|
2003-02-05 22:02:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
SymEntry* GetSymEntry (const Type* T)
|
|
|
|
|
/* Return a SymEntry pointer from a type */
|
|
|
|
|
{
|
|
|
|
|
/* Only structs or unions have a SymEntry attribute */
|
|
|
|
|
CHECK (IsClassStruct (T));
|
|
|
|
|
|
|
|
|
|
/* Return the attribute */
|
|
|
|
|
return T->A.P;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetSymEntry (Type* T, SymEntry* S)
|
|
|
|
|
/* Set the SymEntry pointer for a type */
|
|
|
|
|
{
|
|
|
|
|
/* Only structs or unions have a SymEntry attribute */
|
|
|
|
|
CHECK (IsClassStruct (T));
|
|
|
|
|
|
|
|
|
|
/* Set the attribute */
|
|
|
|
|
T->A.P = S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Type* IntPromotion (Type* T)
|
2004-06-29 20:26:27 +00:00
|
|
|
|
/* Apply the integer promotions to T and return the result. The returned type
|
|
|
|
|
* string may be T if there is no need to change it.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* We must have an int to apply int promotions */
|
|
|
|
|
PRECONDITION (IsClassInt (T));
|
|
|
|
|
|
|
|
|
|
/* An integer can represent all values from either signed or unsigned char,
|
|
|
|
|
* so convert chars to int and leave all other types alone.
|
|
|
|
|
*/
|
|
|
|
|
if (IsTypeChar (T)) {
|
|
|
|
|
return type_int;
|
|
|
|
|
} else {
|
|
|
|
|
return T;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Type* PtrConversion (Type* T)
|
2004-08-02 16:37:00 +00:00
|
|
|
|
/* If the type is a function, convert it to pointer to function. If the
|
|
|
|
|
* expression is an array, convert it to pointer to first element. Otherwise
|
|
|
|
|
* return T.
|
2006-02-19 12:29:37 +00:00
|
|
|
|
*/
|
2004-08-02 16:37:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsTypeFunc (T)) {
|
|
|
|
|
return PointerTo (T);
|
|
|
|
|
} else if (IsTypeArray (T)) {
|
|
|
|
|
return ArrayToPtr (T);
|
|
|
|
|
} else {
|
|
|
|
|
return T;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
|
|