From: uz Date: Tue, 23 Aug 2011 16:15:41 +0000 (+0000) Subject: Implement variable sized element count for arrays. X-Git-Tag: V2.13.3~196 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=25171465d3783516aca69270672dc2371049f91d;p=cc65 Implement variable sized element count for arrays. git-svn-id: svn://svn.cc65.org/cc65/trunk@5266 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/common/gentype.c b/src/common/gentype.c index 9c25f4255..dd1082528 100644 --- a/src/common/gentype.c +++ b/src/common/gentype.c @@ -50,16 +50,24 @@ void GT_AddArray (StrBuf* Type, unsigned ArraySize) * NOT add the element type! */ { - unsigned I; + unsigned SizeBytes; + + /* Remember the current position */ + char* A = SB_GetBuf (Type) + SB_GetLen (Type); - /* Add the array token */ + /* Add a dummy array token */ SB_AppendChar (Type, GT_TYPE_ARRAY); /* Add the size. */ - for (I = 0; I < 4; ++I) { + SizeBytes = 0; + do { SB_AppendChar (Type, ArraySize & 0xFF); ArraySize >>= 8; - } + ++SizeBytes; + } while (ArraySize); + + /* Write the correct array token */ + *A = GT_ARRAY (SizeBytes); } @@ -69,11 +77,18 @@ unsigned GT_GetArraySize (StrBuf* Type) * The index position will get moved past the array size. */ { - unsigned Size; - Size = (unsigned)SB_Get (Type); - Size |= (unsigned)SB_Get (Type) << 8; - Size |= (unsigned)SB_Get (Type) << 16; - Size |= (unsigned)SB_Get (Type) << 24; + /* Get the number of bytes for the element count */ + unsigned SizeBytes = GT_GET_SIZE (SB_Get (Type)); + + /* Read the size */ + unsigned Size = 0; + const char* Buf = SB_GetConstBuf (Type) + SB_GetLen (Type); + while (SizeBytes--) { + Size <<= 8; + Size |= Buf[SizeBytes]; + } + + /* Return it */ return Size; } @@ -100,7 +115,7 @@ const char* GT_AsString (const StrBuf* Type, StrBuf* String) } /* Terminate the string so it can be used with string functions */ - SB_Terminate (String); + SB_Terminate (String); /* Return the contents of String */ return SB_GetConstBuf (String); diff --git a/src/common/gentype.h b/src/common/gentype.h index 5d0425fda..3147fa987 100644 --- a/src/common/gentype.h +++ b/src/common/gentype.h @@ -89,10 +89,7 @@ #define GT_IS_LITTLE_ENDIAN(x) (((x) & GT_BYTEORDER_MASK) == GT_LITTLE_ENDIAN) #define GT_IS_BIG_ENDIAN(x) (((x) & GT_BYTEORDER_MASK) == GT_BIG_ENDIAN) -/* Type of the data. Since we want to have zero as a terminator, we must - * introduce one thing that cannot be zero for normal data. This is the - * type. - */ +/* Type of the data. */ #define GT_TYPE_INT 0x20U #define GT_TYPE_PTR 0x40U #define GT_TYPE_FLOAT 0x60U @@ -117,6 +114,7 @@ #define GT_DBYTE (GT_TYPE_PTR | GT_BIG_ENDIAN | GT_UNSIGNED | GT_SIZE_2) #define GT_PTR (GT_TYPE_PTR | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_2) #define GT_FAR_PTR (GT_TYPE_PTR | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_3) +#define GT_ARRAY(size) (GT_TYPE_ARRAY | ((size) - 1)) @@ -133,7 +131,9 @@ void GT_AddArray (StrBuf* Type, unsigned ArraySize); unsigned GT_GetArraySize (StrBuf* Type); /* Retrieve the size of an array stored in Type at the current index position. - * The index position will get moved past the array size. + * Note: Index must point to the array token itself, since the size of the + * element count is encoded there. The index position will get moved past the + * array. */ const char* GT_AsString (const StrBuf* Type, StrBuf* String);