2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* datatype.c */
|
|
|
|
/* */
|
|
|
|
/* Type string handling for the cc65 C compiler */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2002-09-30 18:46:08 +00:00
|
|
|
/* (C) 1998-2002 Ullrich von Bassewitz */
|
2000-07-29 12:07:04 +00:00
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@musoftware.de */
|
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 "util.h"
|
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-08-20 09:25:47 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Predefined type strings */
|
2000-08-16 09:10:50 +00:00
|
|
|
type type_uchar [] = { T_UCHAR, T_END };
|
2000-05-28 13:40:48 +00:00
|
|
|
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 };
|
2000-08-20 09:25:47 +00:00
|
|
|
type type_size_t [] = { T_UINT, T_END };
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned TypeLen (const type* T)
|
|
|
|
/* Return the length of the type string */
|
|
|
|
{
|
|
|
|
const type* Start = T;
|
2000-08-14 22:18:26 +00:00
|
|
|
while (*T != T_END) {
|
2000-05-28 13:40:48 +00:00
|
|
|
++T;
|
|
|
|
}
|
|
|
|
return T - Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* TypeCpy (type* Dest, const type* Src)
|
|
|
|
/* Copy a type string */
|
|
|
|
{
|
|
|
|
type T;
|
|
|
|
type* Orig = Dest;
|
|
|
|
do {
|
|
|
|
T = *Src++;
|
|
|
|
*Dest++ = T;
|
|
|
|
} while (T);
|
|
|
|
return Orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* TypeCat (type* Dest, const type* Src)
|
|
|
|
/* Append Src */
|
|
|
|
{
|
|
|
|
TypeCpy (Dest + TypeLen (Dest), Src);
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* TypeDup (const type* T)
|
|
|
|
/* Create a copy of the given type on the heap */
|
|
|
|
{
|
|
|
|
unsigned Len = (TypeLen (T) + 1) * sizeof (type);
|
2001-03-23 19:17:04 +00:00
|
|
|
return (type*) memcpy (xmalloc (Len), T, Len);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* TypeAlloc (unsigned Len)
|
|
|
|
/* Allocate memory for a type string of length Len. Len *must* include the
|
|
|
|
* trailing T_END.
|
|
|
|
*/
|
|
|
|
{
|
2001-03-23 19:17:04 +00:00
|
|
|
return (type*) xmalloc (Len * sizeof (type));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TypeFree (type* T)
|
|
|
|
/* 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 */
|
|
|
|
{
|
|
|
|
if (SignedChars && (C & 0x80) != 0) {
|
|
|
|
return C | ~0xFF;
|
|
|
|
} else {
|
|
|
|
return C & 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
type GetDefaultChar (void)
|
|
|
|
/* Return the default char type (signed/unsigned) depending on the settings */
|
|
|
|
{
|
2000-08-01 15:04:35 +00:00
|
|
|
return SignedChars? T_SCHAR : T_UCHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* GetCharArrayType (unsigned Len)
|
|
|
|
/* Return the type for a char array of the given length */
|
|
|
|
{
|
|
|
|
/* Allocate memory for the type string */
|
|
|
|
type* T = TypeAlloc (1 + DECODE_SIZE + 2);
|
|
|
|
|
|
|
|
/* Fill the type string */
|
|
|
|
T [0] = T_ARRAY;
|
|
|
|
T [DECODE_SIZE+1] = GetDefaultChar();
|
|
|
|
T [DECODE_SIZE+2] = T_END;
|
|
|
|
|
|
|
|
/* Encode the length in the type string */
|
|
|
|
Encode (T+1, Len);
|
|
|
|
|
|
|
|
/* Return the new type */
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* GetImplicitFuncType (void)
|
|
|
|
/* Return a type string for an inplicitly declared function */
|
|
|
|
{
|
|
|
|
/* Get a new function descriptor */
|
|
|
|
FuncDesc* F = NewFuncDesc ();
|
|
|
|
|
|
|
|
/* Allocate memory for the type string */
|
|
|
|
type* T = TypeAlloc (1 + DECODE_SIZE + 2);
|
|
|
|
|
|
|
|
/* Prepare the function descriptor */
|
2001-03-24 13:00:16 +00:00
|
|
|
F->Flags = FD_IMPLICIT | 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 */
|
|
|
|
T [0] = T_FUNC;
|
|
|
|
T [DECODE_SIZE+1] = T_INT;
|
|
|
|
T [DECODE_SIZE+2] = T_END;
|
|
|
|
|
|
|
|
/* Encode the function descriptor into the type string */
|
|
|
|
EncodePtr (T+1, F);
|
|
|
|
|
|
|
|
/* Return the new type */
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-20 09:25:47 +00:00
|
|
|
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.
|
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 */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
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) {
|
2000-08-20 09:25:47 +00:00
|
|
|
fprintf (F, "%s ", Name);
|
2000-08-01 15:04:35 +00:00
|
|
|
T &= ~Mask;
|
|
|
|
}
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintType (FILE* F, const type* Type)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Output translation of type array. */
|
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
type T;
|
2001-05-05 21:42:58 +00:00
|
|
|
unsigned long Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* Walk over the complete string */
|
2000-08-01 18:45:08 +00:00
|
|
|
while ((T = *Type++) != T_END) {
|
2000-08-01 15:04:35 +00:00
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* Print any qualifiers */
|
|
|
|
T = PrintTypeComp (F, T, T_QUAL_CONST, "const");
|
|
|
|
T = PrintTypeComp (F, T, T_QUAL_VOLATILE, "volatile");
|
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 */
|
|
|
|
if ((T & T_MASK_TYPE) != T_TYPE_INT && (T & T_MASK_TYPE) != T_TYPE_LONG) {
|
|
|
|
T = PrintTypeComp (F, T, T_SIGN_SIGNED, "signed");
|
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
T = PrintTypeComp (F, T, 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 */
|
2000-08-01 15:04:35 +00:00
|
|
|
switch (T & 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:
|
2001-05-03 17:34:54 +00:00
|
|
|
fprintf (F, "struct %s", ((SymEntry*) DecodePtr (Type))->Name);
|
2000-08-01 18:45:08 +00:00
|
|
|
Type += DECODE_SIZE;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_TYPE_UNION:
|
2001-05-03 17:34:54 +00:00
|
|
|
fprintf (F, "union %s", ((SymEntry*) DecodePtr (Type))->Name);
|
2000-08-01 15:04:35 +00:00
|
|
|
Type += DECODE_SIZE;
|
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 */
|
|
|
|
PrintType (F, Type + DECODE_SIZE);
|
|
|
|
Size = Decode (Type);
|
|
|
|
if (Size == 0) {
|
|
|
|
fprintf (F, "[]");
|
|
|
|
} else {
|
|
|
|
fprintf (F, "[%lu]", Size);
|
|
|
|
}
|
|
|
|
return;
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_TYPE_PTR:
|
2001-05-05 21:42:58 +00:00
|
|
|
/* Recursive call */
|
|
|
|
PrintType (F, Type);
|
|
|
|
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 ");
|
2000-08-01 15:04:35 +00:00
|
|
|
Type += DECODE_SIZE;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-03 17:34:54 +00:00
|
|
|
fprintf (F, "unknown type: %04X", T);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-08-01 15:04:35 +00:00
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-03 17:34:54 +00:00
|
|
|
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, ", ");
|
|
|
|
}
|
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, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void PrintRawType (FILE* F, const type* Type)
|
|
|
|
/* Print a type string in raw format (for debugging) */
|
|
|
|
{
|
|
|
|
while (*Type != T_END) {
|
|
|
|
fprintf (F, "%04X ", *Type++);
|
|
|
|
}
|
|
|
|
fprintf (F, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Encode (type* Type, unsigned long Val)
|
|
|
|
/* Encode p[0] and p[1] so that neither p[0] nore p[1] is zero */
|
|
|
|
{
|
|
|
|
int I;
|
|
|
|
for (I = 0; I < DECODE_SIZE; ++I) {
|
|
|
|
*Type++ = ((type) Val) | 0x8000;
|
|
|
|
Val >>= 15;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EncodePtr (type* Type, void* P)
|
|
|
|
/* Encode a pointer into a type array */
|
|
|
|
{
|
|
|
|
Encode (Type, (unsigned long) P);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long Decode (const type* Type)
|
|
|
|
/* Decode */
|
|
|
|
{
|
|
|
|
int I;
|
|
|
|
unsigned long Val = 0;
|
|
|
|
for (I = DECODE_SIZE-1; I >= 0; I--) {
|
|
|
|
Val <<= 15;
|
|
|
|
Val |= (Type[I] & 0x7FFF);
|
|
|
|
}
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* DecodePtr (const type* Type)
|
|
|
|
/* Decode a pointer from a type array */
|
|
|
|
{
|
|
|
|
return (void*) Decode (Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int HasEncode (const type* Type)
|
|
|
|
/* Return true if the given type has encoded data */
|
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
return IsClassStruct (Type) || IsTypeArray (Type) || IsTypeFunc (Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CopyEncode (const type* Source, type* Target)
|
|
|
|
/* Copy encoded data from Source to Target */
|
|
|
|
{
|
|
|
|
memcpy (Target, Source, DECODE_SIZE * sizeof (type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
unsigned SizeOf (const type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Compute size of object represented by type array. */
|
|
|
|
{
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
switch (UnqualifiedType (T[0])) {
|
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:
|
2001-03-23 19:17:04 +00:00
|
|
|
Entry = (SymEntry*) DecodePtr (T+1);
|
2000-05-28 13:40:48 +00:00
|
|
|
return Entry->V.S.Size;
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_ARRAY:
|
|
|
|
return (Decode (T+ 1) * SizeOf (T + DECODE_SIZE + 1));
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
default:
|
2000-08-01 15:04:35 +00:00
|
|
|
Internal ("Unknown type in SizeOf: %04X", *T);
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +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 */
|
2002-10-10 20:23:41 +00:00
|
|
|
CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the pointer or array token itself */
|
2000-08-01 18:45:08 +00:00
|
|
|
if (IsTypeArray (T)) {
|
2000-08-01 15:04:35 +00:00
|
|
|
return SizeOf (T + DECODE_SIZE + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-08-01 15:04:35 +00:00
|
|
|
return SizeOf (T + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-03-10 20:44:58 +00:00
|
|
|
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");
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
unsigned TypeOf (const type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Get the code generator base type of the object */
|
|
|
|
{
|
|
|
|
FuncDesc* F;
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
switch (UnqualifiedType (T[0])) {
|
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;
|
|
|
|
|
|
|
|
case T_FUNC:
|
2001-03-23 19:17:04 +00:00
|
|
|
F = (FuncDesc*) DecodePtr (T+1);
|
2001-03-24 13:00:16 +00:00
|
|
|
return (F->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:
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type");
|
2000-05-28 13:40:48 +00:00
|
|
|
return CF_INT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +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 */
|
2002-10-10 20:23:41 +00:00
|
|
|
CHECK ((T[0] & T_MASK_CLASS) == T_CLASS_PTR);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the pointer or array token itself */
|
2000-08-01 18:45:08 +00:00
|
|
|
if (IsTypeArray (T)) {
|
2000-08-01 15:04:35 +00:00
|
|
|
return T + DECODE_SIZE + 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-08-01 15:04:35 +00:00
|
|
|
return T + 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
int IsClassInt (const type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Return true if this is an integer type */
|
|
|
|
{
|
2000-08-01 15:04:35 +00:00
|
|
|
return (T[0] & T_MASK_CLASS) == T_CLASS_INT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-19 21:55:06 +00:00
|
|
|
int IsClassFloat (const type* T)
|
|
|
|
/* Return true if this is a float type */
|
|
|
|
{
|
|
|
|
return (T[0] & T_MASK_CLASS) == T_CLASS_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
int IsClassPtr (const type* T)
|
|
|
|
/* Return true if this is a pointer type */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-08-14 22:18:26 +00:00
|
|
|
return (T[0] & T_MASK_CLASS) == T_CLASS_PTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int IsClassStruct (const type* T)
|
|
|
|
/* Return true if this is a struct type */
|
|
|
|
{
|
|
|
|
return (T[0] & T_MASK_CLASS) == T_CLASS_STRUCT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
int IsSignUnsigned (const type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Return true if this is an unsigned type */
|
|
|
|
{
|
2000-08-01 15:04:35 +00:00
|
|
|
return (T[0] & T_MASK_SIGN) == T_SIGN_UNSIGNED;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
int IsQualConst (const type* T)
|
|
|
|
/* Return true if the given type has a const memory image */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-08-14 22:18:26 +00:00
|
|
|
return (GetQualifier (T) & T_QUAL_CONST) != 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
int IsQualVolatile (const type* T)
|
|
|
|
/* Return true if the given type has a volatile type qualifier */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-08-14 22:18:26 +00:00
|
|
|
return (GetQualifier (T) & T_QUAL_VOLATILE) != 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-23 19:17:04 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
type GetQualifier (const type* T)
|
|
|
|
/* Get the qualifier from the given type string */
|
|
|
|
{
|
|
|
|
/* 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);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-23 19:17:04 +00:00
|
|
|
FuncDesc* GetFuncDesc (const type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Get the FuncDesc pointer from a function or pointer-to-function type */
|
|
|
|
{
|
2001-11-12 14:20:56 +00:00
|
|
|
if (UnqualifiedType (T[0]) == T_PTR) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Pointer to function */
|
2000-08-01 15:04:35 +00:00
|
|
|
++T;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Be sure it's a function type */
|
2000-08-01 15:04:35 +00:00
|
|
|
CHECK (T[0] == T_FUNC);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Decode the function descriptor and return it */
|
2001-03-23 19:17:04 +00:00
|
|
|
return (FuncDesc*) DecodePtr (T+1);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-03 17:34:54 +00:00
|
|
|
type* GetFuncReturn (type* T)
|
|
|
|
/* Return a pointer to the return type of a function or pointer-to-function type */
|
|
|
|
{
|
2001-11-08 09:15:58 +00:00
|
|
|
if (UnqualifiedType (T[0]) == T_PTR) {
|
2001-05-03 17:34:54 +00:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
|