2001-04-30 20:00:13 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* codelab.c */
|
2001-04-30 20:00:13 +00:00
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code label structure */
|
2001-04-30 20:00:13 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2009-02-23 21:25:59 +00:00
|
|
|
/* (C) 2001-2009, Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2001-04-30 20:00:13 +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. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* common */
|
2001-05-05 21:42:58 +00:00
|
|
|
#include "check.h"
|
2001-04-30 20:00:13 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
|
2001-05-05 13:51:42 +00:00
|
|
|
/* cc65 */
|
2001-05-05 11:46:02 +00:00
|
|
|
#include "codeent.h"
|
|
|
|
#include "codelab.h"
|
2009-02-23 21:25:59 +00:00
|
|
|
#include "output.h"
|
2001-04-30 20:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2001-04-30 20:00:13 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CodeLabel* NewCodeLabel (const char* Name, unsigned Hash)
|
|
|
|
/* Create a new code label, initialize and return it */
|
|
|
|
{
|
|
|
|
/* Allocate memory */
|
|
|
|
CodeLabel* L = xmalloc (sizeof (CodeLabel));
|
|
|
|
|
|
|
|
/* Initialize the fields */
|
|
|
|
L->Next = 0;
|
|
|
|
L->Name = xstrdup (Name);
|
|
|
|
L->Hash = Hash;
|
|
|
|
L->Owner = 0;
|
|
|
|
InitCollection (&L->JumpFrom);
|
|
|
|
|
|
|
|
/* Return the new label */
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FreeCodeLabel (CodeLabel* L)
|
|
|
|
/* Free the given code label */
|
|
|
|
{
|
|
|
|
/* Free the name */
|
|
|
|
xfree (L->Name);
|
|
|
|
|
|
|
|
/* Free the collection */
|
|
|
|
DoneCollection (&L->JumpFrom);
|
|
|
|
|
|
|
|
/* Delete the struct */
|
|
|
|
xfree (L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-31 21:08:49 +00:00
|
|
|
void CL_AddRef (CodeLabel* L, struct CodeEntry* E)
|
2001-05-04 16:41:23 +00:00
|
|
|
/* Let the CodeEntry E reference the label L */
|
|
|
|
{
|
|
|
|
/* The insn at E jumps to this label */
|
|
|
|
E->JumpTo = L;
|
|
|
|
|
2020-04-16 09:17:53 +00:00
|
|
|
if (CE_HasArgBase (E)) {
|
|
|
|
/* Replace the code entry argument base with the name of the new label */
|
|
|
|
CE_SetArgBase (E, L->Name);
|
|
|
|
} else {
|
|
|
|
CE_SetArgBaseAndOff (E, L->Name, 0);
|
|
|
|
}
|
2004-06-05 16:21:07 +00:00
|
|
|
|
2001-05-04 16:41:23 +00:00
|
|
|
/* Remember that in the label */
|
|
|
|
CollAppend (&L->JumpFrom, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-31 21:08:49 +00:00
|
|
|
void CL_MoveRefs (CodeLabel* OldLabel, CodeLabel* NewLabel)
|
2001-05-05 21:42:58 +00:00
|
|
|
/* Move all references to OldLabel to point to NewLabel. OldLabel will have no
|
2014-06-30 09:10:35 +00:00
|
|
|
** more references on return.
|
|
|
|
*/
|
2001-05-05 21:42:58 +00:00
|
|
|
{
|
|
|
|
/* Walk through all instructions referencing the old label */
|
2001-10-11 08:02:03 +00:00
|
|
|
unsigned Count = CL_GetRefCount (OldLabel);
|
2001-05-05 21:42:58 +00:00
|
|
|
while (Count--) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Get the instruction that references the old label */
|
|
|
|
CodeEntry* E = CL_GetRef (OldLabel, Count);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Change the reference to the new label */
|
2020-08-24 08:01:58 +00:00
|
|
|
CHECK (E->JumpTo != NULL);
|
2013-05-09 11:56:54 +00:00
|
|
|
CHECK (E->JumpTo == OldLabel);
|
|
|
|
CL_AddRef (NewLabel, E);
|
2001-05-05 21:42:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There are no more references to the old label */
|
|
|
|
CollDeleteAll (&OldLabel->JumpFrom);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-23 21:25:59 +00:00
|
|
|
void CL_Output (const CodeLabel* L)
|
|
|
|
/* Output the code label to the output file */
|
2001-04-30 20:00:13 +00:00
|
|
|
{
|
2009-02-23 21:25:59 +00:00
|
|
|
WriteOutput ("%s:", L->Name);
|
2001-10-12 07:58:11 +00:00
|
|
|
if (strlen (L->Name) > 6) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Label is too long, add a linefeed */
|
|
|
|
WriteOutput ("\n");
|
2001-10-12 07:58:11 +00:00
|
|
|
}
|
2001-04-30 20:00:13 +00:00
|
|
|
}
|