/* common */
+#include "check.h"
#include "xmalloc.h"
-/* cc65 */
+/* cc65 */
#include "codeent.h"
#include "codelab.h"
-unsigned RemoveLabelRef (CodeLabel* L, const struct CodeEntry* E)
-/* Remove a reference to this label, return the number of remaining references */
+void MoveLabelRefs (CodeLabel* OldLabel, CodeLabel* NewLabel)
+/* Move all references to OldLabel to point to NewLabel. OldLabel will have no
+ * more references on return.
+ */
{
- /* Delete the item */
- CollDeleteItem (&L->JumpFrom, E);
+ /* Walk through all instructions referencing the old label */
+ unsigned Count = CollCount (&OldLabel->JumpFrom);
+ while (Count--) {
- /* Return the number of remaining references */
- return CollCount (&L->JumpFrom);
+ /* Get the instruction that references the old label */
+ CodeEntry* E = CollAt (&OldLabel->JumpFrom, Count);
+
+ /* Change the reference to the new label */
+ CHECK (E->JumpTo == OldLabel);
+ AddLabelRef (NewLabel, E);
+
+ }
+
+ /* There are no more references to the old label */
+ CollDeleteAll (&OldLabel->JumpFrom);
}
-void OutputCodeLabel (FILE* F, const CodeLabel* L)
+void OutputCodeLabel (const CodeLabel* L, FILE* F)
/* Output the code label to a file */
{
fprintf (F, "%s:", L->Name);