2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* litpool.c */
|
|
|
|
/* */
|
|
|
|
/* Literal string handling for the cc65 C compiler */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* (C) 1998 Ullrich von Bassewitz */
|
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@musoftware.de */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* 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 <stdio.h>
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* common */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "check.h"
|
2001-09-09 13:23:54 +00:00
|
|
|
#include "strbuf.h"
|
2000-08-23 06:45:55 +00:00
|
|
|
#include "tgttrans.h"
|
2000-08-01 15:04:35 +00:00
|
|
|
|
|
|
|
/* cc65 */
|
|
|
|
#include "asmlabel.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "codegen.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "litpool.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-09 13:23:54 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
unsigned LiteralPoolLabel = 0; /* Pool asm label */
|
2001-09-09 13:23:54 +00:00
|
|
|
static StrBuf LiteralPool = STATIC_STRBUF_INITIALIZER;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-03-26 21:57:07 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
void InitLiteralPool (void)
|
|
|
|
/* Initialize the literal pool */
|
|
|
|
{
|
|
|
|
/* Get the pool label */
|
2001-04-19 06:49:08 +00:00
|
|
|
LiteralPoolLabel = GetLocalLabel ();
|
2001-03-26 21:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void TranslateLiteralPool (unsigned Offs)
|
|
|
|
/* Translate the literals starting from the given offset into the target
|
|
|
|
* charset.
|
|
|
|
*/
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
TgtTranslateBuf (SB_GetBuf (&LiteralPool) + Offs, SB_GetLen (&LiteralPool) - Offs);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DumpLiteralPool (void)
|
|
|
|
/* Dump the literal pool */
|
|
|
|
{
|
2001-03-26 21:57:07 +00:00
|
|
|
/* If nothing there, exit... */
|
2001-09-09 13:23:54 +00:00
|
|
|
if (SB_GetLen (&LiteralPool) == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the data segment */
|
|
|
|
if (WriteableStrings) {
|
|
|
|
g_usedata ();
|
|
|
|
} else {
|
|
|
|
g_userodata ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Define the label */
|
2001-05-03 20:16:41 +00:00
|
|
|
g_defdatalabel (LiteralPoolLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Translate the buffer contents into the target charset */
|
|
|
|
TranslateLiteralPool (0);
|
|
|
|
|
|
|
|
/* Output the buffer data */
|
2001-09-09 13:23:54 +00:00
|
|
|
g_defbytes (SB_GetConstBuf (&LiteralPool), SB_GetLen (&LiteralPool));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
unsigned GetLiteralPoolOffs (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Return the current offset into the literal pool */
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
return SB_GetLen (&LiteralPool);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
void ResetLiteralPoolOffs (unsigned Offs)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Reset the offset into the literal pool to some earlier value, effectively
|
|
|
|
* removing values from the pool.
|
|
|
|
*/
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
CHECK (Offs <= SB_GetLen (&LiteralPool));
|
|
|
|
SB_Cut (&LiteralPool, Offs);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AddLiteralChar (char C)
|
|
|
|
/* Add one character to the literal pool */
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
SB_AppendChar (&LiteralPool, C);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned AddLiteral (const char* S)
|
|
|
|
/* Add a literal string to the literal pool. Return the starting offset into
|
|
|
|
* the pool
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Remember the starting offset */
|
2001-09-09 13:23:54 +00:00
|
|
|
unsigned Start = SB_GetLen (&LiteralPool);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
/* Copy the string including the terminator growing the buffer if needed */
|
2001-09-09 13:23:54 +00:00
|
|
|
SB_AppendBuf (&LiteralPool, S, strlen (S) + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Return the starting offset */
|
|
|
|
return Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* GetLiteral (unsigned Offs)
|
|
|
|
/* Get a pointer to the literal with the given offset in the pool */
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
CHECK (Offs < SB_GetLen (&LiteralPool));
|
|
|
|
return SB_GetConstBuf (&LiteralPool) + Offs;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
void PrintLiteralPoolStats (FILE* F)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Print statistics about the literal space used */
|
|
|
|
{
|
2001-09-09 13:23:54 +00:00
|
|
|
fprintf (F, "Literal space used: %u bytes\n", SB_GetLen (&LiteralPool));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|