2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* datatype.c */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Type string handling for the cc65 C compiler */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2015-03-09 22:53:45 +00:00
|
|
|
/* (C) 1998-2015, Ullrich von Bassewitz */
|
2012-03-24 14:28:59 +00:00
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* 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 */
|
2008-08-24 17:33:30 +00:00
|
|
|
#include "addrsize.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "check.h"
|
2008-08-24 17:33:30 +00:00
|
|
|
#include "mmodel.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"
|
2008-08-11 17:51:00 +00:00
|
|
|
#include "fp.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "funcdesc.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-18 15:30:09 +00:00
|
|
|
const char* GetBasicTypeName (const Type* T)
|
|
|
|
/* Return a const name string of the basic type.
|
|
|
|
** Return "type" for unknown basic types.
|
|
|
|
*/
|
|
|
|
{
|
2020-07-26 12:12:55 +00:00
|
|
|
switch (GetRawType (T)) {
|
2020-07-18 15:30:09 +00:00
|
|
|
case T_TYPE_ENUM: return "enum";
|
2020-07-26 12:12:55 +00:00
|
|
|
case T_TYPE_FLOAT: return "float";
|
2020-07-18 15:30:09 +00:00
|
|
|
case T_TYPE_DOUBLE: return "double";
|
|
|
|
case T_TYPE_VOID: return "void";
|
|
|
|
case T_TYPE_STRUCT: return "struct";
|
|
|
|
case T_TYPE_UNION: return "union";
|
|
|
|
case T_TYPE_ARRAY: return "array";
|
|
|
|
case T_TYPE_PTR: return "pointer";
|
|
|
|
case T_TYPE_FUNC: return "function";
|
|
|
|
case T_TYPE_NONE: /* FALLTHROUGH */
|
2020-07-26 12:12:55 +00:00
|
|
|
default: break;
|
2020-07-18 15:30:09 +00:00
|
|
|
}
|
2020-07-26 12:12:55 +00:00
|
|
|
if (IsClassInt (T)) {
|
|
|
|
if (IsSignSigned (T)) {
|
|
|
|
switch (GetRawType (T)) {
|
|
|
|
case T_TYPE_CHAR: return "signed char";
|
|
|
|
case T_TYPE_SHORT: return "short";
|
|
|
|
case T_TYPE_INT: return "int";
|
|
|
|
case T_TYPE_LONG: return "long";
|
|
|
|
case T_TYPE_LONGLONG: return "long long";
|
|
|
|
default:
|
|
|
|
return "signed integer";
|
|
|
|
}
|
|
|
|
} else if (IsSignUnsigned (T)) {
|
|
|
|
switch (GetRawType (T)) {
|
|
|
|
case T_TYPE_CHAR: return "unsigned char";
|
|
|
|
case T_TYPE_SHORT: return "unsigned short";
|
|
|
|
case T_TYPE_INT: return "unsigned int";
|
|
|
|
case T_TYPE_LONG: return "unsigned long";
|
|
|
|
case T_TYPE_LONGLONG: return "unsigned long long";
|
|
|
|
default:
|
|
|
|
return "unsigned integer";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (GetRawType (T)) {
|
|
|
|
case T_TYPE_CHAR: return "char";
|
|
|
|
case T_TYPE_SHORT: return "short";
|
|
|
|
case T_TYPE_INT: return "int";
|
|
|
|
case T_TYPE_LONG: return "long";
|
|
|
|
case T_TYPE_LONGLONG: return "long long";
|
|
|
|
default:
|
|
|
|
return "integer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "type";
|
2020-07-18 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
++T;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
return T - Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-31 18:24:33 +00:00
|
|
|
Type* TypeCopy (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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
*Dest = *Src;
|
2006-02-19 12:29:37 +00:00
|
|
|
if (Src->C == T_END) {
|
|
|
|
break;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
Src++;
|
2006-02-19 12:29:37 +00:00
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** trailing T_END.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
return C | ~0xFF;
|
2002-09-30 18:46:08 +00:00
|
|
|
} else {
|
2013-05-09 11:56:54 +00:00
|
|
|
return C & 0xFF;
|
2002-09-30 18:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
2009-02-22 16:38:07 +00:00
|
|
|
T[0].C = T_FUNC | CodeAddrSizeQualifier ();
|
2006-02-19 12:29:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
const Type* GetStructReplacementType (const Type* SType)
|
2020-07-18 15:30:09 +00:00
|
|
|
/* Get a replacement type for passing a struct/union in the primary register */
|
|
|
|
{
|
|
|
|
const Type* NewType;
|
|
|
|
/* If the size is less than or equal to that of a long, we will copy the
|
|
|
|
** struct using the primary register, otherwise we will use memcpy.
|
|
|
|
*/
|
|
|
|
switch (SizeOf (SType)) {
|
|
|
|
case 1: NewType = type_uchar; break;
|
|
|
|
case 2: NewType = type_uint; break;
|
|
|
|
case 3: /* FALLTHROUGH */
|
|
|
|
case 4: NewType = type_ulong; break;
|
|
|
|
default: NewType = SType; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
long GetIntegerTypeMin (const Type* Type)
|
|
|
|
/* Get the smallest possible value of the integer type */
|
|
|
|
{
|
|
|
|
if (IsSignSigned (Type)) {
|
2020-07-27 10:25:28 +00:00
|
|
|
/* The smallest possible signed value of N-byte integer is -pow(2, 8*N-1) */
|
|
|
|
return (long)((unsigned long)(-1L) << (CHAR_BITS * SizeOf (Type) - 1U));
|
2020-07-26 12:12:55 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long GetIntegerTypeMax (const Type* Type)
|
|
|
|
/* Get the largest possible value of the integer type */
|
|
|
|
{
|
|
|
|
if (IsSignSigned (Type)) {
|
2020-07-27 10:25:28 +00:00
|
|
|
/* Min signed value of N-byte integer is pow(2, 8*N-1) - 1 */
|
2020-07-26 12:12:55 +00:00
|
|
|
return (1UL << (CHAR_BITS * SizeOf (Type) - 1U)) - 1UL;
|
|
|
|
} else {
|
2020-07-27 10:25:28 +00:00
|
|
|
/* Max signed value of N-byte integer is pow(2, 8*N) - 1. However,
|
|
|
|
** workaround is needed as in ISO C it is UB if the shift count is
|
|
|
|
** equal to the bit width of the left operand type.
|
|
|
|
*/
|
|
|
|
return (1UL << 1U << (CHAR_BITS * SizeOf (Type) - 1U)) - 1UL;
|
2020-07-26 12:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned TypeOfBySize (const Type* Type)
|
|
|
|
/* Get the code generator replacement type of the object by its size */
|
|
|
|
{
|
|
|
|
unsigned NewType;
|
|
|
|
/* If the size is less than or equal to that of a a long, we will copy
|
|
|
|
** the struct using the primary register, otherwise we use memcpy.
|
|
|
|
*/
|
|
|
|
switch (SizeOf (Type)) {
|
|
|
|
case 1: NewType = CF_CHAR; break;
|
|
|
|
case 2: NewType = CF_INT; break;
|
|
|
|
case 3: /* FALLTHROUGH */
|
|
|
|
case 4: NewType = CF_LONG; break;
|
|
|
|
default: NewType = CF_NONE; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** on the heap and may be freed after use.
|
|
|
|
*/
|
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 */
|
2013-05-09 11:56:54 +00:00
|
|
|
Type* P = TypeAlloc (Size + 1);
|
2000-08-20 09:25:47 +00:00
|
|
|
|
|
|
|
/* Create the return type... */
|
2008-08-24 17:33:30 +00:00
|
|
|
P[0].C = T_PTR | (T[0].C & T_QUAL_ADDRSIZE);
|
2006-02-19 12:29:37 +00:00
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** name and remove it. Return the type with the component removed.
|
|
|
|
*/
|
2000-08-01 15:04:35 +00:00
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
if ((C & Mask) == Mask) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, "%s ", Name);
|
|
|
|
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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Print any qualifiers */
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_CONST, "const");
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_VOLATILE, "volatile");
|
2006-02-19 12:29:37 +00:00
|
|
|
C = PrintTypeComp (F, C, T_QUAL_RESTRICT, "restrict");
|
2008-08-24 17:33:30 +00:00
|
|
|
C = PrintTypeComp (F, C, T_QUAL_NEAR, "__near__");
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_FAR, "__far__");
|
|
|
|
C = PrintTypeComp (F, C, T_QUAL_FASTCALL, "__fastcall__");
|
2010-04-29 20:30:49 +00:00
|
|
|
C = PrintTypeComp (F, C, T_QUAL_CDECL, "__cdecl__");
|
2000-08-01 15:04:35 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Signedness. Omit the signedness specifier for long and int */
|
|
|
|
if ((C & T_MASK_TYPE) != T_TYPE_INT && (C & T_MASK_TYPE) != T_TYPE_LONG) {
|
|
|
|
C = PrintTypeComp (F, C, T_SIGN_SIGNED, "signed");
|
|
|
|
}
|
|
|
|
C = PrintTypeComp (F, C, T_SIGN_UNSIGNED, "unsigned");
|
|
|
|
|
|
|
|
/* Now check the real type */
|
|
|
|
switch (C & 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;
|
2020-07-26 12:12:55 +00:00
|
|
|
case T_TYPE_ENUM:
|
|
|
|
fprintf (F, "enum");
|
|
|
|
break;
|
2013-05-09 11:56:54 +00:00
|
|
|
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*) T->A.P)->Name);
|
|
|
|
break;
|
|
|
|
case T_TYPE_UNION:
|
|
|
|
fprintf (F, "union %s", ((SymEntry*) T->A.P)->Name);
|
|
|
|
break;
|
|
|
|
case T_TYPE_ARRAY:
|
2015-03-19 10:27:25 +00:00
|
|
|
/* Recursive call */
|
|
|
|
PrintType (F, T + 1);
|
2013-05-09 11:56:54 +00:00
|
|
|
if (T->A.L == UNSPECIFIED) {
|
2015-03-19 10:27:25 +00:00
|
|
|
fprintf (F, " []");
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2015-03-19 10:27:25 +00:00
|
|
|
fprintf (F, " [%ld]", T->A.L);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case T_TYPE_PTR:
|
|
|
|
/* Recursive call */
|
|
|
|
PrintType (F, T + 1);
|
2015-03-19 10:27:25 +00:00
|
|
|
fprintf (F, " *");
|
2013-05-09 11:56:54 +00:00
|
|
|
return;
|
|
|
|
case T_TYPE_FUNC:
|
|
|
|
fprintf (F, "function returning ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf (F, "unknown type: %04lX", T->C);
|
|
|
|
}
|
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));
|
2008-08-24 17:33:30 +00:00
|
|
|
if (IsQualNear (T)) {
|
2003-11-28 22:12:14 +00:00
|
|
|
fprintf (F, " __near__");
|
|
|
|
}
|
2008-08-24 17:33:30 +00:00
|
|
|
if (IsQualFar (T)) {
|
2003-11-28 22:12:14 +00:00
|
|
|
fprintf (F, " __far__");
|
|
|
|
}
|
2008-08-24 17:33:30 +00:00
|
|
|
if (IsQualFastcall (T)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, " __fastcall__");
|
2001-05-03 17:34:54 +00:00
|
|
|
}
|
2010-04-29 20:30:49 +00:00
|
|
|
if (IsQualCDecl (T)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, " __cdecl__");
|
2010-04-29 20:30:49 +00:00
|
|
|
}
|
2001-05-03 17:34:54 +00:00
|
|
|
fprintf (F, " %s (", Name);
|
|
|
|
|
|
|
|
/* Parameters */
|
|
|
|
if (D->Flags & FD_VOID_PARAM) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, "void");
|
2001-05-03 17:34:54 +00:00
|
|
|
} else {
|
2013-05-09 11:56:54 +00:00
|
|
|
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 ");
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
PrintType (F, E->Type);
|
|
|
|
E = E->NextSym;
|
|
|
|
}
|
2001-05-03 17:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, "%04lX ", T->C);
|
2006-02-19 12:29:37 +00:00
|
|
|
++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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
const Type* GetUnderlyingType (const Type* Type)
|
|
|
|
/* Get the underlying type of an enum or other integer class type */
|
|
|
|
{
|
|
|
|
if (IsTypeEnum (Type)) {
|
|
|
|
|
|
|
|
/* This should not happen, but just in case */
|
|
|
|
if (Type->A.P == 0) {
|
|
|
|
Internal ("Enum tag type error in GetUnderlyingTypeCode");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((SymEntry*)Type->A.P)->V.E.Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TypeCode GetUnderlyingTypeCode (const Type* Type)
|
|
|
|
/* Get the type code of the unqualified underlying type of TCode.
|
|
|
|
** Return UnqualifiedType (TCode) if TCode is not scalar.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
TypeCode Underlying = UnqualifiedType (Type->C);
|
|
|
|
TypeCode TCode;
|
|
|
|
|
|
|
|
/* We could also support other T_CLASS_INT types, but just enums for now */
|
|
|
|
if (IsTypeEnum (Type)) {
|
|
|
|
|
|
|
|
/* This should not happen, but just in case */
|
|
|
|
if (Type->A.P == 0) {
|
|
|
|
Internal ("Enum tag type error in GetUnderlyingTypeCode");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inspect the underlying type of the enum */
|
|
|
|
if (((SymEntry*)Type->A.P)->V.E.Type == 0) {
|
|
|
|
/* Incomplete enum type is used */
|
|
|
|
return Underlying;
|
|
|
|
}
|
|
|
|
TCode = UnqualifiedType (((SymEntry*)Type->A.P)->V.E.Type->C);
|
|
|
|
|
|
|
|
/* Replace the type code with integer */
|
|
|
|
Underlying = (TCode & ~T_MASK_TYPE);
|
|
|
|
switch (TCode & T_MASK_SIZE) {
|
|
|
|
case T_SIZE_INT: Underlying |= T_TYPE_INT; break;
|
|
|
|
case T_SIZE_LONG: Underlying |= T_TYPE_LONG; break;
|
|
|
|
case T_SIZE_SHORT: Underlying |= T_TYPE_SHORT; break;
|
|
|
|
case T_SIZE_CHAR: Underlying |= T_TYPE_CHAR; break;
|
|
|
|
case T_SIZE_LONGLONG: Underlying |= T_TYPE_LONGLONG; break;
|
|
|
|
default: Underlying |= T_TYPE_INT; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Underlying;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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. */
|
|
|
|
{
|
2020-07-26 12:12:55 +00:00
|
|
|
switch (GetUnderlyingTypeCode (T)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_VOID:
|
2017-03-12 16:55:31 +00:00
|
|
|
/* A void variable is a cc65 extension.
|
|
|
|
** Get its size (in bytes).
|
|
|
|
*/
|
|
|
|
return T->A.U;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2015-07-10 12:27:49 +00:00
|
|
|
/* Beware: There's a chance that this triggers problems in other parts
|
|
|
|
of the compiler. The solution is to fix the callers, because calling
|
|
|
|
SizeOf() with a function type as argument is bad. */
|
|
|
|
case T_FUNC:
|
|
|
|
return 0; /* Size of function is unknown */
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_SCHAR:
|
|
|
|
case T_UCHAR:
|
|
|
|
return SIZEOF_CHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_SHORT:
|
|
|
|
case T_USHORT:
|
2002-11-02 12:39:10 +00:00
|
|
|
return SIZEOF_SHORT;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_INT:
|
|
|
|
case T_UINT:
|
2002-11-02 12:39:10 +00:00
|
|
|
return SIZEOF_INT;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_PTR:
|
|
|
|
return SIZEOF_PTR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case T_LONG:
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_ULONG:
|
|
|
|
return SIZEOF_LONG;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_LONGLONG:
|
|
|
|
case T_ULONGLONG:
|
|
|
|
return SIZEOF_LONGLONG;
|
2000-08-01 15:04:35 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_FLOAT:
|
2002-11-02 12:39:10 +00:00
|
|
|
return SIZEOF_FLOAT;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_DOUBLE:
|
|
|
|
return SIZEOF_DOUBLE;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +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
|
|
|
|
2013-05-09 11:56:54 +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 {
|
2017-03-12 16:55:31 +00:00
|
|
|
return T->A.U * SizeOf (T + 1);
|
2003-02-03 22:14:20 +00:00
|
|
|
}
|
2000-08-01 15:04:35 +00:00
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
case T_ENUM:
|
|
|
|
/* Incomplete enum type */
|
|
|
|
return 0;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
default:
|
2020-07-26 12:12:55 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Internal ("Unknown type in SizeOf: %04lX", T->C);
|
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** return some valid size instead (so the rest of the compiler doesn't have
|
|
|
|
** to work with invalid sizes).
|
|
|
|
*/
|
2002-03-10 20:44:58 +00:00
|
|
|
{
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** 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).
|
|
|
|
*/
|
2002-03-10 20:44:58 +00:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
{
|
2020-07-26 12:12:55 +00:00
|
|
|
unsigned NewType;
|
|
|
|
|
|
|
|
switch (GetUnderlyingTypeCode (T)) {
|
2001-03-22 21:02:01 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_SCHAR:
|
|
|
|
return CF_CHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_UCHAR:
|
|
|
|
return CF_CHAR | CF_UNSIGNED;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_SHORT:
|
|
|
|
case T_INT:
|
|
|
|
return CF_INT;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_USHORT:
|
|
|
|
case T_UINT:
|
|
|
|
case T_PTR:
|
|
|
|
case T_ARRAY:
|
|
|
|
return CF_INT | CF_UNSIGNED;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case T_LONG:
|
2013-05-09 11:56:54 +00:00
|
|
|
return CF_LONG;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
case T_ULONG:
|
|
|
|
return CF_LONG | CF_UNSIGNED;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
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:
|
2013-05-09 11:56:54 +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:
|
2020-07-26 12:12:55 +00:00
|
|
|
NewType = TypeOfBySize (T);
|
|
|
|
if (NewType != CF_NONE) {
|
|
|
|
return NewType;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Address of ... */
|
|
|
|
return CF_INT | CF_UNSIGNED;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
default:
|
|
|
|
Error ("Illegal type %04lX", T->C);
|
|
|
|
return CF_INT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** given type points to.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-28 18:56:16 +00:00
|
|
|
Type* ArrayToPtr (Type* T)
|
2002-12-17 12:12:44 +00:00
|
|
|
/* Convert an array to a pointer to it's first element */
|
|
|
|
{
|
|
|
|
/* Return pointer to first element */
|
2009-07-28 18:56:16 +00:00
|
|
|
return PointerTo (GetElementType (T));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** 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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* 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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* 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) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* 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
|
2014-06-30 09:10:35 +00:00
|
|
|
** array type).
|
|
|
|
*/
|
2003-02-03 22:14:20 +00:00
|
|
|
{
|
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** array type).
|
|
|
|
*/
|
2006-02-19 12:29:37 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-24 14:28:59 +00:00
|
|
|
Type* GetBaseElementType (Type* T)
|
|
|
|
/* Return the base element type of a given type. If T is not an array, this
|
2014-06-30 09:10:35 +00:00
|
|
|
** will return. Otherwise it will return the base element type, which means
|
|
|
|
** the element type that is not an array.
|
|
|
|
*/
|
2015-03-09 22:53:45 +00:00
|
|
|
{
|
2012-03-24 14:28:59 +00:00
|
|
|
while (IsTypeArray (T)) {
|
|
|
|
++T;
|
|
|
|
}
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
SymEntry* GetSymEntry (const Type* T)
|
|
|
|
/* Return a SymEntry pointer from a type */
|
|
|
|
{
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Only enums, structs or unions have a SymEntry attribute */
|
|
|
|
CHECK (IsClassStruct (T) || IsTypeEnum (T));
|
2006-02-19 12:29:37 +00:00
|
|
|
|
|
|
|
/* Return the attribute */
|
|
|
|
return T->A.P;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetSymEntry (Type* T, SymEntry* S)
|
|
|
|
/* Set the SymEntry pointer for a type */
|
|
|
|
{
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Only enums, structs or unions have a SymEntry attribute */
|
|
|
|
CHECK (IsClassStruct (T) || IsTypeEnum (T));
|
2006-02-19 12:29:37 +00:00
|
|
|
|
|
|
|
/* 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
|
2014-06-30 09:10:35 +00:00
|
|
|
** string may be T if there is no need to change it.
|
|
|
|
*/
|
2004-06-29 20:26:27 +00:00
|
|
|
{
|
|
|
|
/* We must have an int to apply int promotions */
|
|
|
|
PRECONDITION (IsClassInt (T));
|
|
|
|
|
|
|
|
/* An integer can represent all values from either signed or unsigned char,
|
2014-06-30 09:10:35 +00:00
|
|
|
** so convert chars to int and leave all other types alone.
|
|
|
|
*/
|
2004-06-29 20:26:27 +00:00
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** expression is an array, convert it to pointer to first element. Otherwise
|
|
|
|
** return T.
|
|
|
|
*/
|
2004-08-02 16:37:00 +00:00
|
|
|
{
|
2009-07-28 18:56:16 +00:00
|
|
|
if (IsTypeFunc (T)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
return PointerTo (T);
|
2009-07-28 18:56:16 +00:00
|
|
|
} else if (IsTypeArray (T)) {
|
|
|
|
return ArrayToPtr (T);
|
2004-08-02 16:37:00 +00:00
|
|
|
} else {
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-31 18:24:33 +00:00
|
|
|
TypeCode AddrSizeQualifier (unsigned AddrSize)
|
|
|
|
/* Return T_QUAL_NEAR or T_QUAL_FAR depending on the address size */
|
2008-08-24 17:33:30 +00:00
|
|
|
{
|
2008-08-31 18:24:33 +00:00
|
|
|
switch (AddrSize) {
|
2008-08-24 17:33:30 +00:00
|
|
|
|
2008-08-31 18:24:33 +00:00
|
|
|
case ADDR_SIZE_ABS:
|
|
|
|
return T_QUAL_NEAR;
|
2008-08-24 17:33:30 +00:00
|
|
|
|
2008-08-31 18:24:33 +00:00
|
|
|
case ADDR_SIZE_FAR:
|
|
|
|
return T_QUAL_FAR;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Error ("Invalid address size");
|
|
|
|
return T_QUAL_NEAR;
|
2008-08-24 17:33:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|